blob: e528e4834c5bd093ed15a148d04e2a8180213dba [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
xunchang24788852019-03-22 16:08:52 -070017#include "install/fuse_sdcard_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>
xunchang3cc23d52019-03-18 15:03:33 -070030#include <vector>
xunchangea2912f2019-03-17 16:45:12 -070031
xunchang3cc23d52019-03-18 15:03:33 -070032#include <android-base/logging.h>
33#include <android-base/strings.h>
34
35#include "bootloader_message/bootloader_message.h"
xunchangea2912f2019-03-17 16:45:12 -070036#include "fuse_provider.h"
37#include "fuse_sideload.h"
xunchang24788852019-03-22 16:08:52 -070038#include "install/install.h"
39#include "otautil/roots.h"
xunchangea2912f2019-03-17 16:45:12 -070040
xunchang3cc23d52019-03-18 15:03:33 -070041static constexpr const char* SDCARD_ROOT = "/sdcard";
42// How long (in seconds) we wait for the fuse-provided package file to
43// appear, before timing out.
44static constexpr int SDCARD_INSTALL_TIMEOUT = 10;
45
46// Set the BCB to reboot back into recovery (it won't resume the install from
47// sdcard though).
48static void SetSdcardUpdateBootloaderMessage() {
49 std::vector<std::string> options;
50 std::string err;
51 if (!update_bootloader_message(options, &err)) {
52 LOG(ERROR) << "Failed to set BCB message: " << err;
53 }
54}
55
56// Returns the selected filename, or an empty string.
57static std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) {
58 ensure_path_mounted(path);
59
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 Marshalld23b5102017-08-24 13:50:01 +0000100 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).
xunchang3cc23d52019-03-18 15:03:33 -0700105 return "";
106 }
107
Tom Marshalld23b5102017-08-24 13:50:01 +0000108 const std::string& item = entries[chosen_item];
109
xunchang3cc23d52019-03-18 15:03:33 -0700110 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
125static bool StartSdcardFuse(const std::string& path) {
xunchang5e6832a2019-03-15 16:04:32 -0700126 auto file_data_reader = std::make_unique<FuseFileDataProvider>(path, 65536);
xunchangea2912f2019-03-17 16:45:12 -0700127
xunchang5e6832a2019-03-15 16:04:32 -0700128 if (!file_data_reader->Valid()) {
xunchangea2912f2019-03-17 16:45:12 -0700129 return false;
130 }
131
xunchangea2912f2019-03-17 16:45:12 -0700132 // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
133 // that our open file continues to work but new references see it as unmounted.
134 umount2("/sdcard", MNT_DETACH);
135
xunchang5e6832a2019-03-15 16:04:32 -0700136 return run_fuse_sideload(std::move(file_data_reader)) == 0;
xunchangea2912f2019-03-17 16:45:12 -0700137}
xunchang3cc23d52019-03-18 15:03:33 -0700138
xunchang388d2532019-04-12 16:22:15 -0700139int ApplyFromSdcard(Device* device, RecoveryUI* ui) {
xunchang3cc23d52019-03-18 15:03:33 -0700140 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
141 LOG(ERROR) << "\n-- Couldn't mount " << SDCARD_ROOT << ".\n";
142 return INSTALL_ERROR;
143 }
144
145 std::string path = BrowseDirectory(SDCARD_ROOT, device, ui);
Tom Marshalld23b5102017-08-24 13:50:01 +0000146 if (path == "@") {
147 return INSTALL_NONE;
148 }
xunchang3cc23d52019-03-18 15:03:33 -0700149 if (path.empty()) {
150 LOG(ERROR) << "\n-- No package file selected.\n";
151 ensure_path_unmounted(SDCARD_ROOT);
152 return INSTALL_ERROR;
153 }
154
155 ui->Print("\n-- Install %s ...\n", path.c_str());
156 SetSdcardUpdateBootloaderMessage();
157
158 // We used to use fuse in a thread as opposed to a process. Since accessing
159 // through fuse involves going from kernel to userspace to kernel, it leads
160 // to deadlock when a page fault occurs. (Bug: 26313124)
161 pid_t child;
162 if ((child = fork()) == 0) {
163 bool status = StartSdcardFuse(path);
164
165 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
166 }
167
168 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
169 // process is ready.
170 int result = INSTALL_ERROR;
171 int status;
172 bool waited = false;
173 for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
174 if (waitpid(child, &status, WNOHANG) == -1) {
175 result = INSTALL_ERROR;
176 waited = true;
177 break;
178 }
179
180 struct stat sb;
181 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
182 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT - 1) {
183 sleep(1);
184 continue;
185 } else {
186 LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
187 result = INSTALL_ERROR;
188 kill(child, SIGKILL);
189 break;
190 }
191 }
192
xunchang388d2532019-04-12 16:22:15 -0700193 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, false, false, 0 /*retry_count*/, ui);
xunchang3cc23d52019-03-18 15:03:33 -0700194 break;
195 }
196
197 if (!waited) {
198 // Calling stat() on this magic filename signals the fuse
199 // filesystem to shut down.
200 struct stat sb;
201 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
202
203 waitpid(child, &status, 0);
204 }
205
206 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
207 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
208 }
209
210 ensure_path_unmounted(SDCARD_ROOT);
211 return result;
212}