blob: 1e5c6e46a19f2db2f0a74181f6a1f3fbd2e7fec2 [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>
Yumi Yukimura23c409d2024-07-24 03:44:22 +080028#include <filesystem>
xunchangea2912f2019-03-17 16:45:12 -070029#include <functional>
xunchang5e6832a2019-03-15 16:04:32 -070030#include <memory>
Tianjie Xuf6158eb2019-06-11 16:09:07 -070031#include <string>
xunchang3cc23d52019-03-18 15:03:33 -070032#include <vector>
xunchangea2912f2019-03-17 16:45:12 -070033
xunchang3cc23d52019-03-18 15:03:33 -070034#include <android-base/logging.h>
35#include <android-base/strings.h>
36
37#include "bootloader_message/bootloader_message.h"
xunchangea2912f2019-03-17 16:45:12 -070038#include "fuse_provider.h"
39#include "fuse_sideload.h"
xunchang24788852019-03-22 16:08:52 -070040#include "install/install.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070041#include "recovery_utils/roots.h"
xunchangea2912f2019-03-17 16:45:12 -070042
Yumi Yukimura23c409d2024-07-24 03:44:22 +080043namespace fs = std::filesystem;
44
Tom Marshallaecfe0e2019-01-04 14:37:31 -080045using android::volmgr::VolumeInfo;
46using android::volmgr::VolumeManager;
47
xunchang3cc23d52019-03-18 15:03:33 -070048// How long (in seconds) we wait for the fuse-provided package file to
49// appear, before timing out.
50static 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).
54static 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 Yukimura7ebecd02024-08-04 00:58:07 +080063std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) {
Yumi Yukimura23c409d2024-07-24 03:44:22 +080064 if (access(path.c_str(), R_OK | X_OK)) {
xunchang3cc23d52019-03-18 15:03:33 -070065 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 Yukimura23c409d2024-07-24 03:44:22 +080072 for (const auto& entry : fs::directory_iterator(path)) {
73 std::string name = entry.path().filename().string();
xunchang3cc23d52019-03-18 15:03:33 -070074
Yumi Yukimura23c409d2024-07-24 03:44:22 +080075 // Skip "." and ".." entries.
76 if (name == "." || name == "..") continue;
77
78 if (entry.is_directory()) {
xunchang3cc23d52019-03-18 15:03:33 -070079 dirs.push_back(name + "/");
Yumi Yukimura23c409d2024-07-24 03:44:22 +080080 } 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 }
xunchang3cc23d52019-03-18 15:03:33 -070085 }
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 Marshall07571042017-08-24 13:50:01 +0000106 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).
xunchang3cc23d52019-03-18 15:03:33 -0700111 return "";
112 }
113
Tom Marshall07571042017-08-24 13:50:01 +0000114 const std::string& item = entries[chosen_item];
115
xunchang3cc23d52019-03-18 15:03:33 -0700116 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 Xuf6158eb2019-06-11 16:09:07 -0700131static 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 Xue5218612019-06-25 13:59:39 -0700138 auto fuse_data_provider =
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700139 is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE)
140 : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE);
xunchangea2912f2019-03-17 16:45:12 -0700141
Tianjie Xue5218612019-06-25 13:59:39 -0700142 if (!fuse_data_provider || !fuse_data_provider->Valid()) {
143 LOG(ERROR) << "Failed to create fuse data provider.";
xunchangea2912f2019-03-17 16:45:12 -0700144 return false;
145 }
146
Tianjie Xue5218612019-06-25 13:59:39 -0700147 return run_fuse_sideload(std::move(fuse_data_provider)) == 0;
xunchangea2912f2019-03-17 16:45:12 -0700148}
xunchang3cc23d52019-03-18 15:03:33 -0700149
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800150InstallResult InstallWithFuseFromPath(std::string_view path, Device* device) {
xunchang3cc23d52019-03-18 15:03:33 -0700151 // 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 Zhanga4208b52022-02-14 18:38:06 -0800154 auto ui = device->GetUI();
xunchang3cc23d52019-03-18 15:03:33 -0700155 pid_t child;
156 if ((child = fork()) == 0) {
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700157 bool status = StartInstallPackageFuse(path);
xunchang3cc23d52019-03-18 15:03:33 -0700158
159 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
160 }
161
Tao Baoadc99ef2019-04-29 23:48:02 -0700162 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready.
163 InstallResult result = INSTALL_ERROR;
xunchang3cc23d52019-03-18 15:03:33 -0700164 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 Xu980f92e2019-06-11 15:43:43 -0700185 auto package =
186 Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME,
187 std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
Kelvin Zhanga4208b52022-02-14 18:38:06 -0800188 result = InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */,
189 device);
xunchang3cc23d52019-03-18 15:03:33 -0700190 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 Xuf6158eb2019-06-11 16:09:07 -0700206 return result;
207}
208
Tom Marshallaecfe0e2019-01-04 14:37:31 -0800209InstallResult ApplyFromStorage(Device* device, VolumeInfo& vi) {
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700210 auto ui = device->GetUI();
Tom Marshallaecfe0e2019-01-04 14:37:31 -0800211 if (!VolumeManager::Instance()->volumeMount(vi.mId)) {
Alessandro Astone5a627cd2020-10-04 21:59:52 +0200212 return INSTALL_NONE;
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700213 }
214
Tom Marshallaecfe0e2019-01-04 14:37:31 -0800215 std::string path = BrowseDirectory(vi.mPath, device, ui);
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700216 if (path.empty()) {
Tom Marshallaecfe0e2019-01-04 14:37:31 -0800217 VolumeManager::Instance()->volumeUnmount(vi.mId);
Alessandro Astone5a627cd2020-10-04 21:59:52 +0200218 return INSTALL_NONE;
Tianjie Xuf6158eb2019-06-11 16:09:07 -0700219 }
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 Zhanga4208b52022-02-14 18:38:06 -0800229 auto result = InstallWithFuseFromPath(path, device);
Tom Marshallaecfe0e2019-01-04 14:37:31 -0800230
231 VolumeManager::Instance()->volumeUnmount(vi.mId);
xunchang3cc23d52019-03-18 15:03:33 -0700232 return result;
233}