2019-03-18 00:45:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-06-12 01:09:07 +02:00
|
|
|
#include "install/fuse_install.h"
|
2019-03-18 00:45:12 +01:00
|
|
|
|
2019-03-18 23:03:33 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <signal.h>
|
2019-03-18 00:45:12 +01:00
|
|
|
#include <sys/mount.h>
|
2019-03-18 23:03:33 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2019-03-18 00:45:12 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-03-18 23:03:33 +01:00
|
|
|
#include <algorithm>
|
2019-03-18 00:45:12 +01:00
|
|
|
#include <functional>
|
2019-03-16 00:04:32 +01:00
|
|
|
#include <memory>
|
2019-06-12 01:09:07 +02:00
|
|
|
#include <string>
|
2019-03-18 23:03:33 +01:00
|
|
|
#include <vector>
|
2019-03-18 00:45:12 +01:00
|
|
|
|
2019-03-18 23:03:33 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
|
|
|
|
#include "bootloader_message/bootloader_message.h"
|
2019-03-18 00:45:12 +01:00
|
|
|
#include "fuse_provider.h"
|
|
|
|
#include "fuse_sideload.h"
|
2019-03-23 00:08:52 +01:00
|
|
|
#include "install/install.h"
|
2019-10-01 20:55:36 +02:00
|
|
|
#include "recovery_utils/roots.h"
|
2019-03-18 23:03:33 +01:00
|
|
|
|
|
|
|
static constexpr const char* SDCARD_ROOT = "/sdcard";
|
|
|
|
// How long (in seconds) we wait for the fuse-provided package file to
|
|
|
|
// appear, before timing out.
|
|
|
|
static constexpr int SDCARD_INSTALL_TIMEOUT = 10;
|
|
|
|
|
|
|
|
// Set the BCB to reboot back into recovery (it won't resume the install from
|
|
|
|
// sdcard though).
|
|
|
|
static void SetSdcardUpdateBootloaderMessage() {
|
|
|
|
std::vector<std::string> options;
|
|
|
|
std::string err;
|
|
|
|
if (!update_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set BCB message: " << err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the selected filename, or an empty string.
|
|
|
|
static std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) {
|
|
|
|
ensure_path_mounted(path);
|
2019-03-18 00:45:12 +01:00
|
|
|
|
2019-03-18 23:03:33 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
|
|
|
|
if (!d) {
|
|
|
|
PLOG(ERROR) << "error opening " << path;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> dirs;
|
|
|
|
std::vector<std::string> entries{ "../" }; // "../" is always the first entry.
|
|
|
|
|
|
|
|
dirent* de;
|
|
|
|
while ((de = readdir(d.get())) != nullptr) {
|
|
|
|
std::string name(de->d_name);
|
|
|
|
|
|
|
|
if (de->d_type == DT_DIR) {
|
|
|
|
// Skip "." and ".." entries.
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
dirs.push_back(name + "/");
|
2019-06-12 01:09:07 +02:00
|
|
|
} else if (de->d_type == DT_REG && (android::base::EndsWithIgnoreCase(name, ".zip") ||
|
|
|
|
android::base::EndsWithIgnoreCase(name, ".map"))) {
|
2019-03-18 23:03:33 +01:00
|
|
|
entries.push_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(dirs.begin(), dirs.end());
|
|
|
|
std::sort(entries.begin(), entries.end());
|
|
|
|
|
|
|
|
// Append dirs to the entries list.
|
|
|
|
entries.insert(entries.end(), dirs.begin(), dirs.end());
|
|
|
|
|
|
|
|
std::vector<std::string> headers{ "Choose a package to install:", path };
|
|
|
|
|
|
|
|
size_t chosen_item = 0;
|
|
|
|
while (true) {
|
|
|
|
chosen_item = ui->ShowMenu(
|
|
|
|
headers, entries, chosen_item, true,
|
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
|
|
|
|
// Return if WaitKey() was interrupted.
|
|
|
|
if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& item = entries[chosen_item];
|
|
|
|
if (chosen_item == 0) {
|
|
|
|
// Go up but continue browsing (if the caller is BrowseDirectory).
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string new_path = path + "/" + item;
|
|
|
|
if (new_path.back() == '/') {
|
|
|
|
// Recurse down into a subdirectory.
|
|
|
|
new_path.pop_back();
|
|
|
|
std::string result = BrowseDirectory(new_path, device, ui);
|
|
|
|
if (!result.empty()) return result;
|
|
|
|
} else {
|
|
|
|
// Selected a zip file: return the path to the caller.
|
|
|
|
return new_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unreachable.
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:09:07 +02:00
|
|
|
static bool StartInstallPackageFuse(std::string_view path) {
|
|
|
|
if (path.empty()) {
|
2019-03-18 00:45:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:09:07 +02:00
|
|
|
constexpr auto FUSE_BLOCK_SIZE = 65536;
|
|
|
|
bool is_block_map = android::base::ConsumePrefix(&path, "@");
|
2019-06-25 22:59:39 +02:00
|
|
|
auto fuse_data_provider =
|
2019-06-12 01:09:07 +02:00
|
|
|
is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE)
|
|
|
|
: FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE);
|
2019-03-18 23:03:33 +01:00
|
|
|
|
2019-06-25 22:59:39 +02:00
|
|
|
if (!fuse_data_provider || !fuse_data_provider->Valid()) {
|
|
|
|
LOG(ERROR) << "Failed to create fuse data provider.";
|
2019-06-12 01:09:07 +02:00
|
|
|
return false;
|
2019-03-18 23:03:33 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 01:09:07 +02:00
|
|
|
if (android::base::StartsWith(path, SDCARD_ROOT)) {
|
|
|
|
// The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
|
|
|
|
// that our open file continues to work but new references see it as unmounted.
|
|
|
|
umount2(SDCARD_ROOT, MNT_DETACH);
|
2019-03-18 23:03:33 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:59:39 +02:00
|
|
|
return run_fuse_sideload(std::move(fuse_data_provider)) == 0;
|
2019-06-12 01:09:07 +02:00
|
|
|
}
|
2019-03-18 23:03:33 +01:00
|
|
|
|
2022-02-15 03:38:06 +01:00
|
|
|
InstallResult InstallWithFuseFromPath(std::string_view path, Device* device) {
|
2019-03-18 23:03:33 +01:00
|
|
|
// We used to use fuse in a thread as opposed to a process. Since accessing
|
|
|
|
// through fuse involves going from kernel to userspace to kernel, it leads
|
|
|
|
// to deadlock when a page fault occurs. (Bug: 26313124)
|
2022-02-15 03:38:06 +01:00
|
|
|
auto ui = device->GetUI();
|
2019-03-18 23:03:33 +01:00
|
|
|
pid_t child;
|
|
|
|
if ((child = fork()) == 0) {
|
2019-06-12 01:09:07 +02:00
|
|
|
bool status = StartInstallPackageFuse(path);
|
2019-03-18 23:03:33 +01:00
|
|
|
|
|
|
|
_exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:48:02 +02:00
|
|
|
// FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready.
|
|
|
|
InstallResult result = INSTALL_ERROR;
|
2019-03-18 23:03:33 +01:00
|
|
|
int status;
|
|
|
|
bool waited = false;
|
|
|
|
for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
|
|
|
|
if (waitpid(child, &status, WNOHANG) == -1) {
|
|
|
|
result = INSTALL_ERROR;
|
|
|
|
waited = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
|
|
|
|
if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT - 1) {
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
|
|
|
|
result = INSTALL_ERROR;
|
|
|
|
kill(child, SIGKILL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 00:43:43 +02:00
|
|
|
auto package =
|
|
|
|
Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME,
|
|
|
|
std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
|
2022-02-15 03:38:06 +01:00
|
|
|
result = InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */,
|
|
|
|
device);
|
2019-03-18 23:03:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!waited) {
|
|
|
|
// Calling stat() on this magic filename signals the fuse
|
|
|
|
// filesystem to shut down.
|
|
|
|
struct stat sb;
|
|
|
|
stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
|
|
|
|
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
|
|
|
LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:09:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallResult ApplyFromSdcard(Device* device) {
|
|
|
|
auto ui = device->GetUI();
|
|
|
|
if (ensure_path_mounted(SDCARD_ROOT) != 0) {
|
|
|
|
LOG(ERROR) << "\n-- Couldn't mount " << SDCARD_ROOT << ".\n";
|
|
|
|
return INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path = BrowseDirectory(SDCARD_ROOT, device, ui);
|
|
|
|
if (path.empty()) {
|
|
|
|
LOG(ERROR) << "\n-- No package file selected.\n";
|
|
|
|
ensure_path_unmounted(SDCARD_ROOT);
|
|
|
|
return INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hint the install function to read from a block map file.
|
|
|
|
if (android::base::EndsWithIgnoreCase(path, ".map")) {
|
|
|
|
path = "@" + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->Print("\n-- Install %s ...\n", path.c_str());
|
|
|
|
SetSdcardUpdateBootloaderMessage();
|
|
|
|
|
2022-02-15 03:38:06 +01:00
|
|
|
auto result = InstallWithFuseFromPath(path, device);
|
2019-03-18 23:03:33 +01:00
|
|
|
ensure_path_unmounted(SDCARD_ROOT);
|
|
|
|
return result;
|
|
|
|
}
|