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