xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 17 | #include "install/fuse_install.h" |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 18 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <signal.h> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 21 | #include <sys/mount.h> |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/wait.h> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 27 | #include <algorithm> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 28 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 29 | #include <memory> |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 30 | #include <string> |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 31 | #include <vector> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 32 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
| 34 | #include <android-base/strings.h> |
| 35 | |
| 36 | #include "bootloader_message/bootloader_message.h" |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 37 | #include "fuse_provider.h" |
| 38 | #include "fuse_sideload.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 39 | #include "install/install.h" |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 40 | #include "recovery_utils/roots.h" |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 41 | |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 42 | using android::volmgr::VolumeInfo; |
| 43 | using android::volmgr::VolumeManager; |
| 44 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 45 | // How long (in seconds) we wait for the fuse-provided package file to |
| 46 | // appear, before timing out. |
| 47 | static constexpr int SDCARD_INSTALL_TIMEOUT = 10; |
| 48 | |
| 49 | // Set the BCB to reboot back into recovery (it won't resume the install from |
| 50 | // sdcard though). |
| 51 | static void SetSdcardUpdateBootloaderMessage() { |
| 52 | std::vector<std::string> options; |
| 53 | std::string err; |
| 54 | if (!update_bootloader_message(options, &err)) { |
| 55 | LOG(ERROR) << "Failed to set BCB message: " << err; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Returns the selected filename, or an empty string. |
| 60 | static std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) { |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 61 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir); |
| 62 | if (!d) { |
| 63 | PLOG(ERROR) << "error opening " << path; |
| 64 | return ""; |
| 65 | } |
| 66 | |
| 67 | std::vector<std::string> dirs; |
| 68 | std::vector<std::string> entries{ "../" }; // "../" is always the first entry. |
| 69 | |
| 70 | dirent* de; |
| 71 | while ((de = readdir(d.get())) != nullptr) { |
| 72 | std::string name(de->d_name); |
| 73 | |
| 74 | if (de->d_type == DT_DIR) { |
| 75 | // Skip "." and ".." entries. |
| 76 | if (name == "." || name == "..") continue; |
| 77 | dirs.push_back(name + "/"); |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 78 | } else if (de->d_type == DT_REG && (android::base::EndsWithIgnoreCase(name, ".zip") || |
| 79 | android::base::EndsWithIgnoreCase(name, ".map"))) { |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 80 | entries.push_back(name); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | std::sort(dirs.begin(), dirs.end()); |
| 85 | std::sort(entries.begin(), entries.end()); |
| 86 | |
| 87 | // Append dirs to the entries list. |
| 88 | entries.insert(entries.end(), dirs.begin(), dirs.end()); |
| 89 | |
| 90 | std::vector<std::string> headers{ "Choose a package to install:", path }; |
| 91 | |
| 92 | size_t chosen_item = 0; |
| 93 | while (true) { |
| 94 | chosen_item = ui->ShowMenu( |
| 95 | headers, entries, chosen_item, true, |
| 96 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 97 | |
| 98 | // Return if WaitKey() was interrupted. |
| 99 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { |
| 100 | return ""; |
| 101 | } |
Tom Marshall | af3d500 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 102 | if (chosen_item == Device::kGoHome) { |
| 103 | return "@"; |
| 104 | } |
| 105 | if (chosen_item == Device::kGoBack || chosen_item == 0) { |
| 106 | // Go up but continue browsing (if the caller is browse_directory). |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 107 | return ""; |
| 108 | } |
| 109 | |
Tom Marshall | af3d500 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 110 | const std::string& item = entries[chosen_item]; |
| 111 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 112 | std::string new_path = path + "/" + item; |
| 113 | if (new_path.back() == '/') { |
| 114 | // Recurse down into a subdirectory. |
| 115 | new_path.pop_back(); |
| 116 | std::string result = BrowseDirectory(new_path, device, ui); |
| 117 | if (!result.empty()) return result; |
| 118 | } else { |
| 119 | // Selected a zip file: return the path to the caller. |
| 120 | return new_path; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Unreachable. |
| 125 | } |
| 126 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 127 | static bool StartInstallPackageFuse(std::string_view path) { |
| 128 | if (path.empty()) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | constexpr auto FUSE_BLOCK_SIZE = 65536; |
| 133 | bool is_block_map = android::base::ConsumePrefix(&path, "@"); |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 134 | auto fuse_data_provider = |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 135 | is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE) |
| 136 | : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE); |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 137 | |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 138 | if (!fuse_data_provider || !fuse_data_provider->Valid()) { |
| 139 | LOG(ERROR) << "Failed to create fuse data provider."; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 143 | return run_fuse_sideload(std::move(fuse_data_provider)) == 0; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 144 | } |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 145 | |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 146 | InstallResult InstallWithFuseFromPath(std::string_view path, Device* device) { |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 147 | // We used to use fuse in a thread as opposed to a process. Since accessing |
| 148 | // through fuse involves going from kernel to userspace to kernel, it leads |
| 149 | // to deadlock when a page fault occurs. (Bug: 26313124) |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 150 | auto ui = device->GetUI(); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 151 | pid_t child; |
| 152 | if ((child = fork()) == 0) { |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 153 | bool status = StartInstallPackageFuse(path); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 154 | |
| 155 | _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); |
| 156 | } |
| 157 | |
Tao Bao | adc99ef | 2019-04-29 23:48:02 -0700 | [diff] [blame] | 158 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready. |
| 159 | InstallResult result = INSTALL_ERROR; |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 160 | int status; |
| 161 | bool waited = false; |
| 162 | for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) { |
| 163 | if (waitpid(child, &status, WNOHANG) == -1) { |
| 164 | result = INSTALL_ERROR; |
| 165 | waited = true; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | struct stat sb; |
| 170 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) { |
| 171 | if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT - 1) { |
| 172 | sleep(1); |
| 173 | continue; |
| 174 | } else { |
| 175 | LOG(ERROR) << "Timed out waiting for the fuse-provided package."; |
| 176 | result = INSTALL_ERROR; |
| 177 | kill(child, SIGKILL); |
| 178 | break; |
| 179 | } |
| 180 | } |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 181 | auto package = |
| 182 | Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME, |
| 183 | std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 184 | result = InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */, |
| 185 | device); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | |
| 189 | if (!waited) { |
| 190 | // Calling stat() on this magic filename signals the fuse |
| 191 | // filesystem to shut down. |
| 192 | struct stat sb; |
| 193 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb); |
| 194 | |
| 195 | waitpid(child, &status, 0); |
| 196 | } |
| 197 | |
| 198 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 199 | LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status); |
| 200 | } |
| 201 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 202 | return result; |
| 203 | } |
| 204 | |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 205 | InstallResult ApplyFromStorage(Device* device, VolumeInfo& vi) { |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 206 | auto ui = device->GetUI(); |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 207 | if (!VolumeManager::Instance()->volumeMount(vi.mId)) { |
Alessandro Astone | 11b1acf | 2020-10-04 21:59:52 +0200 | [diff] [blame] | 208 | return INSTALL_NONE; |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 211 | std::string path = BrowseDirectory(vi.mPath, device, ui); |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 212 | if (path.empty()) { |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 213 | VolumeManager::Instance()->volumeUnmount(vi.mId); |
Alessandro Astone | 11b1acf | 2020-10-04 21:59:52 +0200 | [diff] [blame] | 214 | return INSTALL_NONE; |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Hint the install function to read from a block map file. |
| 218 | if (android::base::EndsWithIgnoreCase(path, ".map")) { |
| 219 | path = "@" + path; |
| 220 | } |
| 221 | |
| 222 | ui->Print("\n-- Install %s ...\n", path.c_str()); |
| 223 | SetSdcardUpdateBootloaderMessage(); |
| 224 | |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 225 | auto result = InstallWithFuseFromPath(path, device); |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame^] | 226 | |
| 227 | VolumeManager::Instance()->volumeUnmount(vi.mId); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 228 | return result; |
| 229 | } |