blob: 09d04a8ed7e1835745e567defdc89f1f1cb098a4 [file] [log] [blame]
xunchangea2912f2019-03-17 16:45:12 -07001/*
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 Xuf6158eb2019-06-11 16:09:07 -070017#include "install/fuse_install.h"
xunchangea2912f2019-03-17 16:45:12 -070018
xunchang3cc23d52019-03-18 15:03:33 -070019#include <dirent.h>
20#include <signal.h>
xunchangea2912f2019-03-17 16:45:12 -070021#include <sys/mount.h>
xunchang3cc23d52019-03-18 15:03:33 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
xunchangea2912f2019-03-17 16:45:12 -070025#include <unistd.h>
26
xunchang3cc23d52019-03-18 15:03:33 -070027#include <algorithm>
xunchangea2912f2019-03-17 16:45:12 -070028#include <functional>
xunchang5e6832a2019-03-15 16:04:32 -070029#include <memory>
Tianjie Xuf6158eb2019-06-11 16:09:07 -070030#include <string>
xunchang3cc23d52019-03-18 15:03:33 -070031#include <vector>
xunchangea2912f2019-03-17 16:45:12 -070032
xunchang3cc23d52019-03-18 15:03:33 -070033#include <android-base/logging.h>
34#include <android-base/strings.h>
35
36#include "bootloader_message/bootloader_message.h"
xunchangea2912f2019-03-17 16:45:12 -070037#include "fuse_provider.h"
38#include "fuse_sideload.h"
xunchang24788852019-03-22 16:08:52 -070039#include "install/install.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070040#include "recovery_utils/roots.h"
xunchangea2912f2019-03-17 16:45:12 -070041
xunchang3cc23d52019-03-18 15:03:33 -070042static 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.
45static 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).
49static 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.
58static 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 Xuf6158eb2019-06-11 16:09:07 -070078 } else if (de->d_type == DT_REG && (android::base::EndsWithIgnoreCase(name, ".zip") ||
79 android::base::EndsWithIgnoreCase(name, ".map"))) {
xunchang3cc23d52019-03-18 15:03:33 -070080 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 Marshallaf3d5002017-08-24 13:50:01 +0000102 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).
xunchang3cc23d52019-03-18 15:03:33 -0700107 return "";
108 }
109
Tom Marshallaf3d5002017-08-24 13:50:01 +0000110 const std::string& item = entries[chosen_item];
111
xunchang3cc23d52019-03-18 15:03:33 -0700112 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 Xuf6158eb2019-06-11 16:09:07 -0700127static 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 Xue5218612019-06-25 13:59:39 -0700134 auto fuse_data_provider =
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700135 is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE)
136 : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE);
xunchangea2912f2019-03-17 16:45:12 -0700137
Tianjie Xue5218612019-06-25 13:59:39 -0700138 if (!fuse_data_provider || !fuse_data_provider->Valid()) {
139 LOG(ERROR) << "Failed to create fuse data provider.";
xunchangea2912f2019-03-17 16:45:12 -0700140 return false;
141 }
142
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700143 if (android::base::StartsWith(path, SDCARD_ROOT)) {
144 // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
145 // that our open file continues to work but new references see it as unmounted.
146 umount2(SDCARD_ROOT, MNT_DETACH);
147 }
xunchangea2912f2019-03-17 16:45:12 -0700148
Tianjie Xue5218612019-06-25 13:59:39 -0700149 return run_fuse_sideload(std::move(fuse_data_provider)) == 0;
xunchangea2912f2019-03-17 16:45:12 -0700150}
xunchang3cc23d52019-03-18 15:03:33 -0700151
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800152InstallResult InstallWithFuseFromPath(std::string_view path, Device* device) {
xunchang3cc23d52019-03-18 15:03:33 -0700153 // 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)
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800156 auto ui = device->GetUI();
xunchang3cc23d52019-03-18 15:03:33 -0700157 pid_t child;
158 if ((child = fork()) == 0) {
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700159 bool status = StartInstallPackageFuse(path);
xunchang3cc23d52019-03-18 15:03:33 -0700160
161 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
162 }
163
Tao Baoadc99ef2019-04-29 23:48:02 -0700164 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready.
165 InstallResult result = INSTALL_ERROR;
xunchang3cc23d52019-03-18 15:03:33 -0700166 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 }
Tianjie Xu980f92e2019-06-11 15:43:43 -0700187 auto package =
188 Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME,
189 std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800190 result = InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */,
191 device);
xunchang3cc23d52019-03-18 15:03:33 -0700192 break;
193 }
194
195 if (!waited) {
196 // Calling stat() on this magic filename signals the fuse
197 // filesystem to shut down.
198 struct stat sb;
199 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
200
201 waitpid(child, &status, 0);
202 }
203
204 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
205 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
206 }
207
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700208 return result;
209}
210
211InstallResult ApplyFromSdcard(Device* device) {
212 auto ui = device->GetUI();
213 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
214 LOG(ERROR) << "\n-- Couldn't mount " << SDCARD_ROOT << ".\n";
215 return INSTALL_ERROR;
216 }
217
218 std::string path = BrowseDirectory(SDCARD_ROOT, device, ui);
219 if (path.empty()) {
220 LOG(ERROR) << "\n-- No package file selected.\n";
221 ensure_path_unmounted(SDCARD_ROOT);
222 return INSTALL_ERROR;
223 }
224
225 // Hint the install function to read from a block map file.
226 if (android::base::EndsWithIgnoreCase(path, ".map")) {
227 path = "@" + path;
228 }
229
230 ui->Print("\n-- Install %s ...\n", path.c_str());
231 SetSdcardUpdateBootloaderMessage();
232
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800233 auto result = InstallWithFuseFromPath(path, device);
xunchang3cc23d52019-03-18 15:03:33 -0700234 ensure_path_unmounted(SDCARD_ROOT);
235 return result;
236}