From 791a83bb4565b3b2a4e3d40ea2c0ec592cfb0d04 Mon Sep 17 00:00:00 2001 From: Daniel Zheng Date: Tue, 23 May 2023 10:42:39 -0700 Subject: [PATCH] Chang flash task to take in source Old changes didn't actually take into account sources. fastboot update {update.zip} should read fastboot-info from the update package. It was instead flashing local images + reading local fastboot-info.txt Test: m fastboot, fastboot update, fastboot flashall Bug: 283330320 Change-Id: I9587a7735ddfbfb661153eb12ebc3caa7c32f141 --- fastboot/fastboot.cpp | 47 +++++++++++++++++++++---------------------- fastboot/fastboot.h | 3 ++- fastboot/task.cpp | 6 +++--- fastboot/task.h | 3 ++- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index f09616a49..f5fd3a1ea 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -1487,13 +1487,20 @@ static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf return partition; } -void do_flash(const char* pname, const char* fname, const bool apply_vbmeta) { +void do_flash(const char* pname, const char* fname, const bool apply_vbmeta, + const FlashingPlan* fp) { verbose("Do flash %s %s", pname, fname); struct fastboot_buffer buf; - if (!load_buf(fname, &buf)) { + if (fp->source) { + unique_fd fd = fp->source->OpenFile(fname); + if (fd < 0 || !load_buf_fd(std::move(fd), &buf)) { + die("could not load '%s': %s", fname, strerror(errno)); + } + } else if (!load_buf(fname, &buf)) { die("cannot load '%s': %s", fname, strerror(errno)); } + if (is_logical(pname)) { fb->ResizePartition(pname, std::to_string(buf.image_size)); } @@ -1592,7 +1599,7 @@ std::unique_ptr ParseFlashCommand(const FlashingPlan* fp, if (img_name.empty()) { img_name = partition + ".img"; } - return std::make_unique(slot, partition, img_name, apply_vbmeta); + return std::make_unique(slot, partition, img_name, apply_vbmeta, fp); } std::unique_ptr ParseRebootCommand(const FlashingPlan* fp, @@ -1666,7 +1673,7 @@ void AddResizeTasks(const FlashingPlan* fp, std::vector>* } static bool IsIgnore(const std::vector& command) { - if (command[0][0] == '#') { + if (command.size() == 0 || command[0][0] == '#') { return true; } return false; @@ -1748,16 +1755,6 @@ std::vector> ParseFastbootInfo(const FlashingPlan* fp, return tasks; } -std::vector> ParseFastbootInfo(const FlashingPlan* fp, std::ifstream& fs) { - std::string text; - std::vector file; - // Get os_partitions that need to be resized - while (std::getline(fs, text)) { - file.emplace_back(text); - } - return ParseFastbootInfo(fp, file); -} - FlashAllTool::FlashAllTool(FlashingPlan* fp) : fp_(fp) {} void FlashAllTool::Flash() { @@ -1777,16 +1774,17 @@ void FlashAllTool::Flash() { CancelSnapshotIfNeeded(); - std::string path = find_item_given_name("fastboot-info.txt"); - std::ifstream stream(path); - if (!stream || stream.eof()) { + std::vector contents; + if (!fp_->source->ReadFile("fastboot-info.txt", &contents)) { LOG(VERBOSE) << "Flashing from hardcoded images. fastboot-info.txt is empty or does not " "exist"; HardcodedFlash(); return; } - std::vector> tasks = ParseFastbootInfo(fp_, stream); + std::vector> tasks = + ParseFastbootInfo(fp_, Split({contents.data(), contents.size()}, "\n")); + if (tasks.empty()) { LOG(FATAL) << "Invalid fastboot-info.txt file."; } @@ -2115,7 +2113,7 @@ bool should_flash_in_userspace(const std::string& partition_name) { } static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::string& slot, - std::string* message) { + std::string* message, const FlashingPlan* fp) { auto super_device = GetMetadataSuperBlockDevice(metadata); auto block_size = metadata.geometry.logical_block_size; auto super_bdev_name = android::fs_mgr::GetBlockDevicePartitionName(*super_device); @@ -2155,7 +2153,7 @@ static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::s auto image_path = temp_dir.path + "/"s + image_name; auto flash = [&](const std::string& partition_name) { - do_flash(partition_name.c_str(), image_path.c_str(), false); + do_flash(partition_name.c_str(), image_path.c_str(), false, fp); }; do_for_partitions(partition, slot, flash, force_slot); @@ -2164,7 +2162,8 @@ static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::s return true; } -static void do_wipe_super(const std::string& image, const std::string& slot_override) { +static void do_wipe_super(const std::string& image, const std::string& slot_override, + const FlashingPlan* fp) { if (access(image.c_str(), R_OK) != 0) { die("Could not read image: %s", image.c_str()); } @@ -2179,7 +2178,7 @@ static void do_wipe_super(const std::string& image, const std::string& slot_over } std::string message; - if (!wipe_super(*metadata.get(), slot, &message)) { + if (!wipe_super(*metadata.get(), slot, &message, fp)) { die(message); } } @@ -2476,7 +2475,7 @@ int FastBootTool::Main(int argc, char* argv[]) { } if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str()); - FlashTask task(fp->slot_override, pname, fname, is_vbmeta_partition(pname)); + FlashTask task(fp->slot_override, pname, fname, is_vbmeta_partition(pname), fp.get()); task.Run(); } else if (command == "flash:raw") { std::string partition = next_arg(&args); @@ -2571,7 +2570,7 @@ int FastBootTool::Main(int argc, char* argv[]) { } else { image = next_arg(&args); } - do_wipe_super(image, fp->slot_override); + do_wipe_super(image, fp->slot_override, fp.get()); } else if (command == "snapshot-update") { std::string arg; if (!args.empty()) { diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h index 80b77a0e9..0a1f1eb66 100644 --- a/fastboot/fastboot.h +++ b/fastboot/fastboot.h @@ -124,7 +124,8 @@ class FlashAllTool { bool should_flash_in_userspace(const std::string& partition_name); bool is_userspace_fastboot(); -void do_flash(const char* pname, const char* fname, const bool apply_vbmeta); +void do_flash(const char* pname, const char* fname, const bool apply_vbmeta, + const FlashingPlan* fp); void do_for_partitions(const std::string& part, const std::string& slot, const std::function& func, bool force_slot); std::string find_item(const std::string& item); diff --git a/fastboot/task.cpp b/fastboot/task.cpp index e98effb26..cb12060b8 100644 --- a/fastboot/task.cpp +++ b/fastboot/task.cpp @@ -27,8 +27,8 @@ using namespace std::string_literals; FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, - const bool apply_vbmeta) - : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta) {} + const bool apply_vbmeta, const FlashingPlan* fp) + : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta), fp_(fp) {} void FlashTask::Run() { auto flash = [&](const std::string& partition) { @@ -41,7 +41,7 @@ void FlashTask::Run() { "And try again. If you are intentionally trying to " "overwrite a fixed partition, use --force."); } - do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_); + do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_); }; do_for_partitions(pname_, slot_, flash, true); } diff --git a/fastboot/task.h b/fastboot/task.h index d113f18d5..82e8ebf51 100644 --- a/fastboot/task.h +++ b/fastboot/task.h @@ -46,7 +46,7 @@ class Task { class FlashTask : public Task { public: FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, - const bool apply_vbmeta); + const bool apply_vbmeta, const FlashingPlan* fp); virtual FlashTask* AsFlashTask() override { return this; } std::string GetPartition() { return pname_; } @@ -60,6 +60,7 @@ class FlashTask : public Task { const std::string fname_; const std::string slot_; const bool apply_vbmeta_; + const FlashingPlan* fp_; }; class RebootTask : public Task {