diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 6fc6f5ff7..eee0e93d0 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -76,6 +76,7 @@ #include "constants.h" #include "diagnose_usb.h" #include "fastboot_driver.h" +#include "fastboot_driver_interface.h" #include "fs.h" #include "storage.h" #include "super_flash_helper.h" @@ -996,7 +997,7 @@ static std::vector load_sparse_files(int fd, int64_t max_size) { return resparse_file(s.get(), max_size); } -static uint64_t get_uint_var(const char* var_name) { +static uint64_t get_uint_var(const char* var_name, fastboot::IFastBootDriver* fb) { std::string value_str; if (fb->GetVar(var_name, &value_str) != fastboot::SUCCESS || value_str.empty()) { verbose("target didn't report %s", var_name); @@ -1021,7 +1022,7 @@ int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp) { // Unlimited, so see what the target device's limit is. // TODO: shouldn't we apply this limit even if you've used -S? if (target_sparse_limit == -1) { - target_sparse_limit = static_cast(get_uint_var("max-download-size")); + target_sparse_limit = static_cast(get_uint_var("max-download-size", fp->fb)); } if (target_sparse_limit > 0) { limit = target_sparse_limit; @@ -1420,8 +1421,9 @@ bool is_retrofit_device() { // Fetch a partition from the device to a given fd. This is a wrapper over FetchToFd to fetch // the full image. -static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd) { - uint64_t fetch_size = get_uint_var(FB_VAR_MAX_FETCH_SIZE); +static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd, + fastboot::IFastBootDriver* fb) { + uint64_t fetch_size = get_uint_var(FB_VAR_MAX_FETCH_SIZE, fb); if (fetch_size == 0) { die("Unable to get %s. Device does not support fetch command.", FB_VAR_MAX_FETCH_SIZE); } @@ -1443,17 +1445,18 @@ static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd) { } static void do_fetch(const std::string& partition, const std::string& slot_override, - const std::string& outfile) { + const std::string& outfile, fastboot::IFastBootDriver* fb) { unique_fd fd(TEMP_FAILURE_RETRY( open(outfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0644))); - auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd)); + auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd), fb); do_for_partitions(partition, slot_override, fetch, false /* force slot */); } // Return immediately if not flashing a vendor boot image. If flashing a vendor boot image, // repack vendor_boot image with an updated ramdisk. After execution, buf is set // to the new image to flash, and return value is the real partition name to flash. -static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf) { +static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf, + fastboot::IFastBootDriver* fb) { std::string_view pname_sv{pname}; if (!android::base::StartsWith(pname_sv, "vendor_boot:") && @@ -1471,7 +1474,7 @@ static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf std::string ramdisk(pname_sv.substr(pname_sv.find(':') + 1)); unique_fd vendor_boot(make_temporary_fd("vendor boot repack")); - uint64_t vendor_boot_size = fetch_partition(partition, vendor_boot); + uint64_t vendor_boot_size = fetch_partition(partition, vendor_boot, fb); auto repack_res = replace_vendor_ramdisk(vendor_boot, vendor_boot_size, ramdisk, buf->fd, static_cast(buf->sz)); if (!repack_res.ok()) { @@ -1508,7 +1511,7 @@ void do_flash(const char* pname, const char* fname, const bool apply_vbmeta, if (is_logical(pname)) { fb->ResizePartition(pname, std::to_string(buf.image_size)); } - std::string flash_pname = repack_ramdisk(pname, &buf); + std::string flash_pname = repack_ramdisk(pname, &buf, fp->fb); flash_buf(flash_pname, &buf, apply_vbmeta); } @@ -2576,7 +2579,7 @@ int FastBootTool::Main(int argc, char* argv[]) { } else if (command == FB_CMD_FETCH) { std::string partition = next_arg(&args); std::string outfile = next_arg(&args); - do_fetch(partition, fp->slot_override, outfile); + do_fetch(partition, fp->slot_override, outfile, fp->fb); } else { syntax_error("unknown command %s", command.c_str()); } diff --git a/fastboot/fastboot_driver.h b/fastboot/fastboot_driver.h index 6ac26ce31..8774eadc6 100644 --- a/fastboot/fastboot_driver.h +++ b/fastboot/fastboot_driver.h @@ -105,7 +105,7 @@ class FastBootDriver : public IFastBootDriver { std::vector* info = nullptr); RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd, int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ RetCode FlashPartition(const std::string& partition, const std::vector& data); diff --git a/fastboot/fastboot_driver_interface.h b/fastboot/fastboot_driver_interface.h index 795938fb4..7cb8a6b6a 100644 --- a/fastboot/fastboot_driver_interface.h +++ b/fastboot/fastboot_driver_interface.h @@ -45,6 +45,10 @@ class IFastBootDriver { std::vector* info = nullptr) = 0; RetCode virtual GetVar(const std::string& key, std::string* val, std::vector* info = nullptr) = 0; + RetCode virtual FetchToFd(const std::string& partition, android::base::borrowed_fd fd, + int64_t offset = -1, int64_t size = -1, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; RetCode virtual Download(const std::string& name, android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, std::vector* info = nullptr) = 0; diff --git a/fastboot/fastboot_driver_mock.h b/fastboot/fastboot_driver_mock.h index d2a123b19..7c41d78a1 100644 --- a/fastboot/fastboot_driver_mock.h +++ b/fastboot/fastboot_driver_mock.h @@ -28,15 +28,16 @@ class MockFastbootDriver : public IFastBootDriver { MOCK_METHOD(RetCode, Reboot, (std::string*, std::vector*), (override)); MOCK_METHOD(RetCode, RebootTo, (std::string, std::string*, std::vector*), (override)); - MOCK_METHOD(RetCode, GetVar, (const std::string&, std::string*, std::vector*), (override)); - + MOCK_METHOD(RetCode, FetchToFd, + (const std::string&, android::base::borrowed_fd, int64_t offset, int64_t size, + std::string*, std::vector*), + (override)); MOCK_METHOD(RetCode, Download, (const std::string&, android::base::borrowed_fd, size_t, std::string*, std::vector*), (override)); - MOCK_METHOD(RetCode, RawCommand, (const std::string&, const std::string&, std::string*, std::vector*, int*),