From f6158eb918fb86f94e418244a9351379ad568563 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 11 Jun 2019 16:09:07 -0700 Subject: Support starting fuse from a block map Factor out a new function from ApplyFromSdcard that installs a package from a local path. Inside this function, we start the fuse and choose the type of data provider depending on the path string. And similar to the existing logic, we treat the package as a block map if the path starts with a '@'. This is part of the effort to install larger than 2GiB packages on ILP32 devices. Bug: 127071893 Test: Build a 32 bit sailfish and create a 3GiB OTA package. Sideload the package, uncrypt and install the package from sdcard. Change-Id: I328ea34fa530731acbce7554bfc3059313ad6ece --- fuse_sideload/fuse_provider.cpp | 7 +- fuse_sideload/include/fuse_provider.h | 17 +- install/Android.bp | 2 +- install/fuse_install.cpp | 231 ++++++++++++++++++++++++++ install/fuse_sdcard_install.cpp | 208 ----------------------- install/include/install/fuse_install.h | 30 ++++ install/include/install/fuse_sdcard_install.h | 23 --- minadbd/fuse_adb_provider.h | 4 + recovery.cpp | 4 +- tests/unit/fuse_provider_test.cpp | 3 +- tests/unit/fuse_sideload_test.cpp | 4 + 11 files changed, 294 insertions(+), 239 deletions(-) create mode 100644 install/fuse_install.cpp delete mode 100644 install/fuse_sdcard_install.cpp create mode 100644 install/include/install/fuse_install.h delete mode 100644 install/include/install/fuse_sdcard_install.h diff --git a/fuse_sideload/fuse_provider.cpp b/fuse_sideload/fuse_provider.cpp index 5ee6e247f..8fa1b5c2e 100644 --- a/fuse_sideload/fuse_provider.cpp +++ b/fuse_sideload/fuse_provider.cpp @@ -49,6 +49,11 @@ FuseFileDataProvider::FuseFileDataProvider(const std::string& path, uint32_t blo fuse_block_size_ = block_size; } +std::unique_ptr FuseFileDataProvider::CreateFromFile(const std::string& path, + uint32_t block_size) { + return std::make_unique(path, block_size); +} + bool FuseFileDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, uint32_t start_block) const { uint64_t offset = static_cast(start_block) * fuse_block_size_; @@ -127,7 +132,7 @@ bool FuseBlockDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch return true; } -std::unique_ptr FuseBlockDataProvider::CreateFromBlockMap( +std::unique_ptr FuseBlockDataProvider::CreateFromBlockMap( const std::string& block_map_path, uint32_t fuse_block_size) { auto block_map = BlockMapData::ParseBlockMapFile(block_map_path); if (!block_map) { diff --git a/fuse_sideload/include/fuse_provider.h b/fuse_sideload/include/fuse_provider.h index 8d4ea4073..3cdaef33d 100644 --- a/fuse_sideload/include/fuse_provider.h +++ b/fuse_sideload/include/fuse_provider.h @@ -44,6 +44,8 @@ class FuseDataProvider { virtual bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, uint32_t start_block) const = 0; + virtual bool Valid() const = 0; + virtual void Close() {} protected: @@ -60,10 +62,13 @@ class FuseFileDataProvider : public FuseDataProvider { public: FuseFileDataProvider(const std::string& path, uint32_t block_size); + static std::unique_ptr CreateFromFile(const std::string& path, + uint32_t block_size); + bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, uint32_t start_block) const override; - bool Valid() const { + bool Valid() const override { return fd_ != -1; } @@ -78,14 +83,20 @@ class FuseFileDataProvider : public FuseDataProvider { class FuseBlockDataProvider : public FuseDataProvider { public: // Constructs the fuse provider from the block map. - static std::unique_ptr CreateFromBlockMap( - const std::string& block_map_path, uint32_t fuse_block_size); + static std::unique_ptr CreateFromBlockMap(const std::string& block_map_path, + uint32_t fuse_block_size); RangeSet ranges() const { return ranges_; } + bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, uint32_t start_block) const override; + + bool Valid() const override { + return fd_ != -1; + } + void Close() override; private: diff --git a/install/Android.bp b/install/Android.bp index 4696e501e..89cc3f23e 100644 --- a/install/Android.bp +++ b/install/Android.bp @@ -61,7 +61,7 @@ cc_library_static { srcs: [ "adb_install.cpp", "asn1_decoder.cpp", - "fuse_sdcard_install.cpp", + "fuse_install.cpp", "install.cpp", "package.cpp", "verifier.cpp", diff --git a/install/fuse_install.cpp b/install/fuse_install.cpp new file mode 100644 index 000000000..ffde4a348 --- /dev/null +++ b/install/fuse_install.cpp @@ -0,0 +1,231 @@ +/* + * 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. + */ + +#include "install/fuse_install.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include "bootloader_message/bootloader_message.h" +#include "fuse_provider.h" +#include "fuse_sideload.h" +#include "install/install.h" +#include "otautil/roots.h" + +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 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); + + std::unique_ptr d(opendir(path.c_str()), closedir); + if (!d) { + PLOG(ERROR) << "error opening " << path; + return ""; + } + + std::vector dirs; + std::vector 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 + "/"); + } else if (de->d_type == DT_REG && (android::base::EndsWithIgnoreCase(name, ".zip") || + android::base::EndsWithIgnoreCase(name, ".map"))) { + 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 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(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. +} + +static bool StartInstallPackageFuse(std::string_view path) { + if (path.empty()) { + return false; + } + + constexpr auto FUSE_BLOCK_SIZE = 65536; + bool is_block_map = android::base::ConsumePrefix(&path, "@"); + auto file_data_reader = + is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE) + : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE); + + if (!file_data_reader->Valid()) { + return false; + } + + 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); + } + + return run_fuse_sideload(std::move(file_data_reader)) == 0; +} + +InstallResult InstallWithFuseFromPath(std::string_view path, RecoveryUI* ui) { + // 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) + pid_t child; + if ((child = fork()) == 0) { + bool status = StartInstallPackageFuse(path); + + _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); + } + + // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready. + InstallResult result = INSTALL_ERROR; + 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; + } + } + auto package = + Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME, + std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); + result = + InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */, ui); + 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); + } + + 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(); + + auto result = InstallWithFuseFromPath(path, ui); + ensure_path_unmounted(SDCARD_ROOT); + return result; +} diff --git a/install/fuse_sdcard_install.cpp b/install/fuse_sdcard_install.cpp deleted file mode 100644 index 9fdb2f341..000000000 --- a/install/fuse_sdcard_install.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 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. - */ - -#include "install/fuse_sdcard_install.h" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "bootloader_message/bootloader_message.h" -#include "fuse_provider.h" -#include "fuse_sideload.h" -#include "install/install.h" -#include "otautil/roots.h" - -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 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); - - std::unique_ptr d(opendir(path.c_str()), closedir); - if (!d) { - PLOG(ERROR) << "error opening " << path; - return ""; - } - - std::vector dirs; - std::vector 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 + "/"); - } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) { - 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 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(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. -} - -static bool StartSdcardFuse(const std::string& path) { - auto file_data_reader = std::make_unique(path, 65536); - - if (!file_data_reader->Valid()) { - return false; - } - - // 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", MNT_DETACH); - - return run_fuse_sideload(std::move(file_data_reader)) == 0; -} - -InstallResult ApplyFromSdcard(Device* device, RecoveryUI* ui) { - 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; - } - - ui->Print("\n-- Install %s ...\n", path.c_str()); - SetSdcardUpdateBootloaderMessage(); - - // 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) - pid_t child; - if ((child = fork()) == 0) { - bool status = StartSdcardFuse(path); - - _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); - } - - // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child process is ready. - InstallResult result = INSTALL_ERROR; - 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; - } - } - auto package = - Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME, - std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); - result = - InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0 /* retry_count */, ui); - 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); - } - - ensure_path_unmounted(SDCARD_ROOT); - return result; -} diff --git a/install/include/install/fuse_install.h b/install/include/install/fuse_install.h new file mode 100644 index 000000000..63b116aeb --- /dev/null +++ b/install/include/install/fuse_install.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#pragma once + +#include + +#include "install/install.h" +#include "recovery_ui/device.h" +#include "recovery_ui/ui.h" + +// Starts FUSE with the package from |path| as the data source. And installs the package from +// |FUSE_SIDELOAD_HOST_PATHNAME|. The |path| can point to the location of a package zip file or a +// block map file with the prefix '@'; e.g. /sdcard/package.zip, @/cache/recovery/block.map. +InstallResult InstallWithFuseFromPath(std::string_view path, RecoveryUI* ui); + +InstallResult ApplyFromSdcard(Device* device); diff --git a/install/include/install/fuse_sdcard_install.h b/install/include/install/fuse_sdcard_install.h deleted file mode 100644 index e5bb01f09..000000000 --- a/install/include/install/fuse_sdcard_install.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "install/install.h" -#include "recovery_ui/device.h" -#include "recovery_ui/ui.h" - -InstallResult ApplyFromSdcard(Device* device, RecoveryUI* ui); diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index c5561e57d..43c07d28e 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -29,6 +29,10 @@ class FuseAdbDataProvider : public FuseDataProvider { bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, uint32_t start_block) const override; + bool Valid() const override { + return fd_ != -1; + } + private: // The underlying source to read data from (i.e. the one that talks to the host). int fd_; diff --git a/recovery.cpp b/recovery.cpp index db66ea7e9..b18a8e74a 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -49,7 +49,7 @@ #include "common.h" #include "fsck_unshare_blocks.h" #include "install/adb_install.h" -#include "install/fuse_sdcard_install.h" +#include "install/fuse_install.h" #include "install/install.h" #include "install/package.h" #include "install/wipe_data.h" @@ -408,7 +408,7 @@ static Device::BuiltinAction PromptAndWait(Device* device, InstallResult status) status = ApplyFromAdb(device, false /* rescue_mode */, &reboot_action); } else { adb = false; - status = ApplyFromSdcard(device, ui); + status = ApplyFromSdcard(device); } ui->Print("\nInstall from %s completed with status %d.\n", adb ? "ADB" : "SD card", status); diff --git a/tests/unit/fuse_provider_test.cpp b/tests/unit/fuse_provider_test.cpp index c5995dd7d..37f99f92e 100644 --- a/tests/unit/fuse_provider_test.cpp +++ b/tests/unit/fuse_provider_test.cpp @@ -44,7 +44,8 @@ TEST(FuseBlockMapTest, CreateFromBlockMap_smoke) { ASSERT_TRUE(block_map_data); ASSERT_EQ(10000, block_map_data->file_size()); ASSERT_EQ(4096, block_map_data->fuse_block_size()); - ASSERT_EQ(RangeSet({ { 10, 11 }, { 20, 21 }, { 22, 23 } }), block_map_data->ranges()); + ASSERT_EQ(RangeSet({ { 10, 11 }, { 20, 21 }, { 22, 23 } }), + static_cast(block_map_data.get())->ranges()); } TEST(FuseBlockMapTest, ReadBlockAlignedData_smoke) { diff --git a/tests/unit/fuse_sideload_test.cpp b/tests/unit/fuse_sideload_test.cpp index 6add99f41..ea895038c 100644 --- a/tests/unit/fuse_sideload_test.cpp +++ b/tests/unit/fuse_sideload_test.cpp @@ -40,6 +40,10 @@ class FuseTestDataProvider : public FuseDataProvider { bool ReadBlockAlignedData(uint8_t*, uint32_t, uint32_t) const override { return true; } + + bool Valid() const override { + return true; + } }; TEST(SideloadTest, run_fuse_sideload_wrong_parameters) { -- cgit v1.2.3