diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 716fc4f67..7a5a78203 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -107,8 +107,6 @@ static bool g_disable_verification = false; fastboot::FastBootDriver* fb = nullptr; -using SparsePtr = std::unique_ptr; - enum fb_buffer_type { FB_BUFFER_FD, FB_BUFFER_SPARSE, @@ -249,14 +247,6 @@ static void InfoMessage(const std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); } -static int64_t get_file_size(borrowed_fd fd) { - struct stat sb; - if (fstat(fd.get(), &sb) == -1) { - die("could not get file size"); - } - return sb.st_size; -} - bool ReadFileToVector(const std::string& file, std::vector* out) { out->clear(); @@ -827,19 +817,16 @@ static void DumpInfo() { fprintf(stderr, "--------------------------------------------\n"); } -static std::vector load_sparse_files(int fd, int64_t max_size) { - SparsePtr s(sparse_file_import_auto(fd, false, true), sparse_file_destroy); - if (!s) die("cannot sparse read file"); - +static std::vector resparse_file(sparse_file* s, int64_t max_size) { if (max_size <= 0 || max_size > std::numeric_limits::max()) { die("invalid max size %" PRId64, max_size); } - const int files = sparse_file_resparse(s.get(), max_size, nullptr, 0); + const int files = sparse_file_resparse(s, max_size, nullptr, 0); if (files < 0) die("Failed to resparse"); auto temp = std::make_unique(files); - const int rv = sparse_file_resparse(s.get(), max_size, temp.get(), files); + const int rv = sparse_file_resparse(s, max_size, temp.get(), files); if (rv < 0) die("Failed to resparse"); std::vector out_s; @@ -849,6 +836,13 @@ static std::vector load_sparse_files(int fd, int64_t max_size) { return out_s; } +static std::vector load_sparse_files(int fd, int64_t max_size) { + SparsePtr s(sparse_file_import_auto(fd, false, true), sparse_file_destroy); + if (!s) die("cannot sparse read file"); + + return resparse_file(s.get(), max_size); +} + static uint64_t get_uint_var(const char* var_name) { std::string value_str; if (fb->GetVar(var_name, &value_str) != fastboot::SUCCESS || value_str.empty()) { @@ -998,6 +992,8 @@ static bool has_vbmeta_partition() { fb->GetVar("partition-type:vbmeta_b", &partition_type) == fastboot::SUCCESS; } +// Note: this only works in userspace fastboot. In the bootloader, use +// should_flash_in_userspace(). static bool is_logical(const std::string& partition) { std::string value; return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes"; @@ -1080,6 +1076,15 @@ static void copy_avb_footer(const std::string& partition, struct fastboot_buffer lseek(buf->fd.get(), 0, SEEK_SET); } +static void flash_partition_files(const std::string& partition, + const std::vector& files) { + for (size_t i = 0; i < files.size(); i++) { + sparse_file* s = files[i].get(); + int64_t sz = sparse_file_len(s, true, false); + fb->FlashPartition(partition, s, sz, i + 1, files.size()); + } +} + static void flash_buf(const std::string& partition, struct fastboot_buffer* buf) { if (partition == "boot" || partition == "boot_a" || partition == "boot_b" || partition == "init_boot" || partition == "init_boot_a" || partition == "init_boot_b" || @@ -1103,11 +1108,7 @@ static void flash_buf(const std::string& partition, struct fastboot_buffer* buf) switch (buf->type) { case FB_BUFFER_SPARSE: { - for (size_t i = 0; i < buf->files.size(); i++) { - sparse_file* s = buf->files[i].get(); - int64_t sz = sparse_file_len(s, true, false); - fb->FlashPartition(partition, s, sz, i + 1, buf->files.size()); - } + flash_partition_files(partition, buf->files); break; } case FB_BUFFER_FD: @@ -1395,13 +1396,6 @@ static void CancelSnapshotIfNeeded() { } } -class ImageSource { - public: - virtual ~ImageSource(){}; - virtual bool ReadFile(const std::string& name, std::vector* out) const = 0; - virtual unique_fd OpenFile(const std::string& name) const = 0; -}; - class FlashAllTool { public: FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, @@ -1775,20 +1769,7 @@ static bool should_flash_in_userspace(const std::string& partition_name) { if (!metadata) { return false; } - for (const auto& partition : metadata->partitions) { - auto candidate = android::fs_mgr::GetPartitionName(partition); - if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) { - // On retrofit devices, we don't know if, or whether, the A or B - // slot has been flashed for dynamic partitions. Instead we add - // both names to the list as a conservative guess. - if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) { - return true; - } - } else if (candidate == partition_name) { - return true; - } - } - return false; + return should_flash_in_userspace(*metadata.get(), partition_name); } static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::string& slot, diff --git a/fastboot/util.cpp b/fastboot/util.cpp index 900d6ea87..ded54a5b9 100644 --- a/fastboot/util.cpp +++ b/fastboot/util.cpp @@ -30,11 +30,13 @@ #include #include #include - +#include #include #include "util.h" +using android::base::borrowed_fd; + static bool g_verbose = false; double now() { @@ -73,3 +75,34 @@ void verbose(const char* fmt, ...) { } fprintf(stderr, "\n"); } + +bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, + const std::string& partition_name) { + for (const auto& partition : metadata.partitions) { + auto candidate = android::fs_mgr::GetPartitionName(partition); + if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) { + // On retrofit devices, we don't know if, or whether, the A or B + // slot has been flashed for dynamic partitions. Instead we add + // both names to the list as a conservative guess. + if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) { + return true; + } + } else if (candidate == partition_name) { + return true; + } + } + return false; +} + +bool is_sparse_file(borrowed_fd fd) { + SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy); + return !!s; +} + +int64_t get_file_size(borrowed_fd fd) { + struct stat sb; + if (fstat(fd.get(), &sb) == -1) { + die("could not get file size"); + } + return sb.st_size; +} diff --git a/fastboot/util.h b/fastboot/util.h index c719df2b5..290d0d5fe 100644 --- a/fastboot/util.h +++ b/fastboot/util.h @@ -4,8 +4,14 @@ #include #include +#include +#include #include +#include +#include + +using SparsePtr = std::unique_ptr; /* util stuff */ double now(); @@ -19,3 +25,15 @@ __attribute__((__format__(__printf__, 1, 2))); void verbose(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); void die(const std::string& str) __attribute__((__noreturn__)); + +bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, + const std::string& partition_name); +bool is_sparse_file(android::base::borrowed_fd fd); +int64_t get_file_size(android::base::borrowed_fd fd); + +class ImageSource { + public: + virtual ~ImageSource(){}; + virtual bool ReadFile(const std::string& name, std::vector* out) const = 0; + virtual android::base::unique_fd OpenFile(const std::string& name) const = 0; +};