Updating fastboot to modern c++ standards

Test: tested fastboot on raven device
Change-Id: I0c1fa4cd09378282edf5df54ae26d747e6d4f3eb
This commit is contained in:
Daniel Zheng 2022-11-30 02:25:31 +00:00
parent f9e6c5104d
commit 65452d036e
1 changed files with 94 additions and 98 deletions

View File

@ -129,23 +129,23 @@ enum class ImageType {
};
struct Image {
const char* nickname;
const char* img_name;
const char* sig_name;
const char* part_name;
std::string nickname;
std::string img_name;
std::string sig_name;
std::string part_name;
bool optional_if_no_image;
ImageType type;
bool IsSecondary() const { return nickname == nullptr; }
bool IsSecondary() const { return nickname.empty(); }
};
static Image images[] = {
static std::vector<Image> images = {
// clang-format off
{ "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
{ "init_boot",
"init_boot.img", "init_boot.sig",
"init_boot",
true, ImageType::BootCritical },
{ nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
{ "", "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
{ "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
{ "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
{ "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
@ -164,7 +164,7 @@ static Image images[] = {
"system_ext.img", "system_ext.sig",
"system_ext",
true, ImageType::Normal },
{ nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
{ "", "system_other.img", "system.sig", "system", true, ImageType::Normal },
{ "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
{ "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
{ "vbmeta_system",
@ -191,7 +191,7 @@ static Image images[] = {
"vendor_kernel_boot.sig",
"vendor_kernel_boot",
true, ImageType::BootCritical },
{ nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
{ "", "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
// clang-format on
};
@ -212,8 +212,8 @@ static std::string find_item_given_name(const std::string& img_name) {
}
static std::string find_item(const std::string& item) {
for (size_t i = 0; i < arraysize(images); ++i) {
if (images[i].nickname && item == images[i].nickname) {
for (size_t i = 0; i < images.size(); ++i) {
if (!images[i].nickname.empty() && item == images[i].nickname) {
return find_item_given_name(images[i].img_name);
}
}
@ -274,7 +274,8 @@ static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_seri
// require matching serial number or device path if requested
// at the command line with the -s option.
if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
strcmp(local_serial, info->device_path) != 0)) return -1;
strcmp(local_serial, info->device_path) != 0))
return -1;
return 0;
}
@ -569,8 +570,8 @@ static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
out->size() / 1024 / 1024);
int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
out->size());
int error =
ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()), out->size());
if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
return true;
@ -618,8 +619,8 @@ static int make_temporary_fd(const char* what) {
std::string path_template(make_temporary_template());
int fd = mkstemp(&path_template[0]);
if (fd == -1) {
die("failed to create temporary file for %s with template %s: %s\n",
path_template.c_str(), what, strerror(errno));
die("failed to create temporary file for %s with template %s: %s\n", path_template.c_str(),
what, strerror(errno));
}
unlink(path_template.c_str());
return fd;
@ -673,16 +674,15 @@ static bool CheckRequirement(const std::string& cur_product, const std::string&
std::string var_value;
if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
fprintf(stderr, "FAILED\n\n");
fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
fb->Error().c_str());
fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(), fb->Error().c_str());
return false;
}
bool match = false;
for (const auto& option : options) {
if (option == var_value || (option.back() == '*' &&
!var_value.compare(0, option.length() - 1, option, 0,
option.length() - 1))) {
if (option == var_value ||
(option.back() == '*' &&
!var_value.compare(0, option.length() - 1, option, 0, option.length() - 1))) {
match = true;
break;
}
@ -757,8 +757,8 @@ static void HandlePartitionExists(const std::vector<std::string>& options) {
die("device doesn't have required partition %s!", partition_name.c_str());
}
bool known_partition = false;
for (size_t i = 0; i < arraysize(images); ++i) {
if (images[i].nickname && images[i].nickname == partition_name) {
for (size_t i = 0; i < images.size(); ++i) {
if (!images[i].nickname.empty() && images[i].nickname == partition_name) {
images[i].optional_if_no_image = false;
known_partition = true;
}
@ -822,7 +822,6 @@ static void DumpInfo() {
DisplayVarOrError("Baseband Version.....", "version-baseband");
DisplayVarOrError("Serial Number........", "serialno");
fprintf(stderr, "--------------------------------------------\n");
}
static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
@ -836,7 +835,8 @@ static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
int files = sparse_file_resparse(s, max_size, nullptr, 0);
if (files < 0) die("Failed to resparse");
sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
sparse_file** out_s =
reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file*), files + 1));
if (!out_s) die("Failed to allocate sparse file array");
files = sparse_file_resparse(s, max_size, out_s, files);
@ -1078,8 +1078,7 @@ static void copy_avb_footer(const std::string& partition, struct fastboot_buffer
lseek(buf->fd.get(), 0, SEEK_SET);
}
static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
{
static void flash_buf(const std::string& partition, struct fastboot_buffer* buf) {
sparse_file** s;
if (partition == "boot" || partition == "boot_a" || partition == "boot_b" ||
@ -1243,7 +1242,8 @@ static void do_for_partition(const std::string& part, const std::string& slot,
* partition does not support slots.
*/
static void do_for_partitions(const std::string& part, const std::string& slot,
const std::function<void(const std::string&)>& func, bool force_slot) {
const std::function<void(const std::string&)>& func,
bool force_slot) {
std::string has_slot;
// |part| can be vendor_boot:default. Query has-slot on the first token only.
auto part_tokens = android::base::Split(part, ":");
@ -1439,9 +1439,7 @@ FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_ov
slot_override_(slot_override),
skip_secondary_(skip_secondary),
wipe_(wipe),
force_flash_(force_flash)
{
}
force_flash_(force_flash) {}
void FlashAllTool::Flash() {
DumpInfo();
@ -1508,7 +1506,7 @@ void FlashAllTool::DetermineSecondarySlot() {
}
void FlashAllTool::CollectImages() {
for (size_t i = 0; i < arraysize(images); ++i) {
for (size_t i = 0; i < images.size(); ++i) {
std::string slot = slot_override_;
if (images[i].IsSecondary()) {
if (skip_secondary_) {
@ -1532,7 +1530,7 @@ void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::st
if (image->optional_if_no_image) {
continue;
}
die("could not load '%s': %s", image->img_name, strerror(errno));
die("could not load '%s': %s", image->img_name.c_str(), strerror(errno));
}
FlashImage(*image, slot, &buf);
}
@ -1734,8 +1732,7 @@ static void fb_perform_format(const std::string& partition, int skip_if_not_supp
fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
return;
}
die("Formatting is not supported for file system with type '%s'.",
partition_type.c_str());
die("Formatting is not supported for file system with type '%s'.", partition_type.c_str());
}
int64_t size;
@ -1892,8 +1889,7 @@ int FastBootTool::Main(int argc, char* argv[]) {
g_boot_img_hdr.page_size = 2048;
g_boot_img_hdr.dtb_addr = 0x01100000;
const struct option longopts[] = {
{"base", required_argument, 0, 0},
const struct option longopts[] = {{"base", required_argument, 0, 0},
{"cmdline", required_argument, 0, 0},
{"disable-verification", no_argument, 0, 0},
{"disable-verity", no_argument, 0, 0},
@ -1916,8 +1912,7 @@ int FastBootTool::Main(int argc, char* argv[]) {
{"unbuffered", no_argument, 0, 0},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 0},
{0, 0, 0, 0}
};
{0, 0, 0, 0}};
serial = getenv("ANDROID_SERIAL");
@ -1966,7 +1961,8 @@ int FastBootTool::Main(int argc, char* argv[]) {
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
} else if (name == "version") {
fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION,
android::build::GetBuildNumber().c_str());
fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
return 0;
} else {
@ -2063,7 +2059,8 @@ int FastBootTool::Main(int argc, char* argv[]) {
std::string partition = next_arg(&args);
auto erase = [&](const std::string& partition) {
std::string partition_type;
if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
if (fb->GetVar("partition-type:" + partition, &partition_type) ==
fastboot::SUCCESS &&
fs_get_generator(partition_type) != nullptr) {
fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
partition_type.c_str());
@ -2118,7 +2115,6 @@ int FastBootTool::Main(int argc, char* argv[]) {
} else {
syntax_error("unknown reboot target %s", what.c_str());
}
}
if (!args.empty()) syntax_error("junk after reboot command");
} else if (command == FB_CMD_REBOOT_BOOTLOADER) {
@ -2178,7 +2174,8 @@ int FastBootTool::Main(int argc, char* argv[]) {
do_for_partitions(partition, slot_override, flashraw, true);
} else if (command == "flashall") {
if (slot_override == "all") {
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
fprintf(stderr,
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
do_flashall(slot_override, true, wants_wipe, force_flash);
} else {
do_flashall(slot_override, skip_secondary, wants_wipe, force_flash);
@ -2187,7 +2184,8 @@ int FastBootTool::Main(int argc, char* argv[]) {
} else if (command == "update") {
bool slot_all = (slot_override == "all");
if (slot_all) {
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
fprintf(stderr,
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
}
std::string filename = "update.zip";
if (!args.empty()) {
@ -2214,10 +2212,9 @@ int FastBootTool::Main(int argc, char* argv[]) {
} else if (command == "flashing") {
if (args.empty()) {
syntax_error("missing 'flashing' command");
} else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
args[0] == "unlock_critical" ||
args[0] == "lock_critical" ||
args[0] == "get_unlock_ability")) {
} else if (args.size() == 1 &&
(args[0] == "unlock" || args[0] == "lock" || args[0] == "unlock_critical" ||
args[0] == "lock_critical" || args[0] == "get_unlock_ability")) {
do_oem_command("flashing", &args);
} else {
syntax_error("unknown 'flashing' command %s", args[0].c_str());
@ -2334,8 +2331,7 @@ unsigned FastBootTool::ParseFsOption(const char* arg) {
unsigned fsOptions = 0;
std::vector<std::string> options = android::base::Split(arg, ",");
if (options.size() < 1)
syntax_error("bad options: %s", arg);
if (options.size() < 1) syntax_error("bad options: %s", arg);
for (size_t i = 0; i < options.size(); ++i) {
if (options[i] == "casefold")