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 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 42 | static constexpr const char* SDCARD_ROOT = "/sdcard"; |
| 43 | // How long (in seconds) we wait for the fuse-provided package file to |
| 44 | // appear, before timing out. |
| 45 | static constexpr int SDCARD_INSTALL_TIMEOUT = 10; |
| 46 | |
| 47 | // Set the BCB to reboot back into recovery (it won't resume the install from |
| 48 | // sdcard though). |
| 49 | static void SetSdcardUpdateBootloaderMessage() { |
| 50 | std::vector<std::string> options; |
| 51 | std::string err; |
| 52 | if (!update_bootloader_message(options, &err)) { |
| 53 | LOG(ERROR) << "Failed to set BCB message: " << err; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Returns the selected filename, or an empty string. |
| 58 | static std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) { |
| 59 | ensure_path_mounted(path); |
| 60 | |
| 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 | } |
| 102 | |
| 103 | const std::string& item = entries[chosen_item]; |
| 104 | if (chosen_item == 0) { |
| 105 | // Go up but continue browsing (if the caller is BrowseDirectory). |
| 106 | return ""; |
| 107 | } |
| 108 | |
| 109 | std::string new_path = path + "/" + item; |
| 110 | if (new_path.back() == '/') { |
| 111 | // Recurse down into a subdirectory. |
| 112 | new_path.pop_back(); |
| 113 | std::string result = BrowseDirectory(new_path, device, ui); |
| 114 | if (!result.empty()) return result; |
| 115 | } else { |
| 116 | // Selected a zip file: return the path to the caller. |
| 117 | return new_path; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Unreachable. |
| 122 | } |
| 123 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 124 | static bool StartInstallPackageFuse(std::string_view path) { |
| 125 | if (path.empty()) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | constexpr auto FUSE_BLOCK_SIZE = 65536; |
| 130 | bool is_block_map = android::base::ConsumePrefix(&path, "@"); |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 131 | auto fuse_data_provider = |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 132 | is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE) |
| 133 | : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE); |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 134 | |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 135 | if (!fuse_data_provider || !fuse_data_provider->Valid()) { |
| 136 | LOG(ERROR) << "Failed to create fuse data provider."; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 140 | if (android::base::StartsWith(path, SDCARD_ROOT)) { |
| 141 | // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so |
| 142 | // that our open file continues to work but new references see it as unmounted. |
| 143 | umount2(SDCARD_ROOT, MNT_DETACH); |
| 144 | } |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 145 | |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 146 | return run_fuse_sideload(std::move(fuse_data_provider)) == 0; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 147 | } |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 148 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 149 | InstallResult InstallWithFuseFromPath(std::string_view path, RecoveryUI* ui) { |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 150 | // We used to use fuse in a thread as opposed to a process. Since accessing |
| 151 | // through fuse involves going from kernel to userspace to kernel, it leads |
| 152 | // to deadlock when a page fault occurs. (Bug: 26313124) |
| 153 | pid_t child; |
| 154 | if ((child = fork()) == 0) { |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 155 | bool status = StartInstallPackageFuse(path); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 156 | |
| 157 | _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); |
| 158 | } |
| 159 | |
Tao Bao | adc99ef | 2019-04-29 23:48:02 -0700 | [diff] [blame] | 160 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready. |
| 161 | InstallResult result = INSTALL_ERROR; |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 162 | int status; |
| 163 | bool waited = false; |
| 164 | for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) { |
| 165 | if (waitpid(child, &status, WNOHANG) == -1) { |
| 166 | result = INSTALL_ERROR; |
| 167 | waited = true; |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | struct stat sb; |
| 172 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) { |
| 173 | if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT - 1) { |
| 174 | sleep(1); |
| 175 | continue; |
| 176 | } else { |
| 177 | LOG(ERROR) << "Timed out waiting for the fuse-provided package."; |
| 178 | result = INSTALL_ERROR; |
| 179 | kill(child, SIGKILL); |
| 180 | break; |
| 181 | } |
| 182 | } |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 183 | auto package = |
| 184 | Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME, |
| 185 | std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); |
| 186 | result = |
| 187 | InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */, ui); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | |
| 191 | if (!waited) { |
| 192 | // Calling stat() on this magic filename signals the fuse |
| 193 | // filesystem to shut down. |
| 194 | struct stat sb; |
| 195 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb); |
| 196 | |
| 197 | waitpid(child, &status, 0); |
| 198 | } |
| 199 | |
| 200 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 201 | LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status); |
| 202 | } |
| 203 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | |
| 207 | InstallResult ApplyFromSdcard(Device* device) { |
| 208 | auto ui = device->GetUI(); |
| 209 | if (ensure_path_mounted(SDCARD_ROOT) != 0) { |
| 210 | LOG(ERROR) << "\n-- Couldn't mount " << SDCARD_ROOT << ".\n"; |
| 211 | return INSTALL_ERROR; |
| 212 | } |
| 213 | |
| 214 | std::string path = BrowseDirectory(SDCARD_ROOT, device, ui); |
| 215 | if (path.empty()) { |
| 216 | LOG(ERROR) << "\n-- No package file selected.\n"; |
| 217 | ensure_path_unmounted(SDCARD_ROOT); |
| 218 | return INSTALL_ERROR; |
| 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 | |
| 229 | auto result = InstallWithFuseFromPath(path, ui); |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 230 | ensure_path_unmounted(SDCARD_ROOT); |
| 231 | return result; |
| 232 | } |