fastboot: Move some helpers into util.h/.cpp.

Bug: 266982466
Test: builds
Change-Id: Ib744d763e11d8a7f7e3f417b331defff61fe4559
This commit is contained in:
David Anderson 2023-01-27 20:39:06 -08:00
parent a67fc32a8a
commit aa87dc5a0d
3 changed files with 75 additions and 43 deletions

View file

@ -107,8 +107,6 @@ static bool g_disable_verification = false;
fastboot::FastBootDriver* fb = nullptr;
using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>;
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<char>* out) {
out->clear();
@ -827,19 +817,16 @@ static void DumpInfo() {
fprintf(stderr, "--------------------------------------------\n");
}
static std::vector<SparsePtr> 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<SparsePtr> resparse_file(sparse_file* s, int64_t max_size) {
if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::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<sparse_file*[]>(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<SparsePtr> out_s;
@ -849,6 +836,13 @@ static std::vector<SparsePtr> load_sparse_files(int fd, int64_t max_size) {
return out_s;
}
static std::vector<SparsePtr> 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<SparsePtr>& 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<char>* 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,

View file

@ -30,11 +30,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#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;
}

View file

@ -4,8 +4,14 @@
#include <stdlib.h>
#include <string>
#include <vector>
#include <android-base/unique_fd.h>
#include <bootimg.h>
#include <liblp/liblp.h>
#include <sparse/sparse.h>
using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>;
/* 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<char>* out) const = 0;
virtual android::base::unique_fd OpenFile(const std::string& name) const = 0;
};