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