Add a header-version argument to fastboot

The argument would set the boot header
version for flash:raw and boot
commands when they are used for boot
image creation.

Bug: 77154616
Test: Dumped the created boot image and checked
the header version using unpack_bootimg

Change-Id: I2c458996817615f4351db102b4234984108d47c0
Merged-In: I2c458996817615f4351db102b4234984108d47c0
(cherry picked from commit 88de5556ec)
This commit is contained in:
Hridya Valsaraju 2018-03-30 16:15:33 -07:00
parent a5bf7bad2c
commit 7d824136ae
3 changed files with 39 additions and 24 deletions

View file

@ -34,26 +34,25 @@
#include <stdlib.h>
#include <string.h>
void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline) {
void bootimg_set_cmdline(boot_img_hdr_v1* h, const char* cmdline) {
if (strlen(cmdline) >= sizeof(h->cmdline)) die("command line too large: %zu", strlen(cmdline));
strcpy(reinterpret_cast<char*>(h->cmdline), cmdline);
}
boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
void* second, int64_t second_size, off_t second_offset,
size_t page_size, size_t base, off_t tags_offset,
int64_t* bootimg_size)
{
boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, void* ramdisk,
int64_t ramdisk_size, off_t ramdisk_offset, void* second,
int64_t second_size, off_t second_offset, size_t page_size, size_t base,
off_t tags_offset, uint32_t header_version, int64_t* bootimg_size) {
size_t page_mask = page_size - 1;
int64_t header_actual = sizeof(boot_img_hdr_v1) & (~page_mask);
int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask);
int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
int64_t second_actual = (second_size + page_mask) & (~page_mask);
*bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual;
*bootimg_size = header_actual + kernel_actual + ramdisk_actual + second_actual;
boot_img_hdr* hdr = reinterpret_cast<boot_img_hdr*>(calloc(*bootimg_size, 1));
boot_img_hdr_v1* hdr = reinterpret_cast<boot_img_hdr_v1*>(calloc(*bootimg_size, 1));
if (hdr == nullptr) {
return hdr;
}
@ -71,9 +70,13 @@ boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
hdr->page_size = page_size;
if (header_version) {
hdr->header_version = header_version;
hdr->header_size = sizeof(boot_img_hdr_v1);
}
memcpy(hdr->magic + page_size, kernel, kernel_size);
memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size);
memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size);
return hdr;
}

View file

@ -33,11 +33,10 @@
#include <inttypes.h>
#include <sys/types.h>
void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline);
boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
void* second, int64_t second_size, off_t second_offset,
size_t page_size, size_t base, off_t tags_offset,
int64_t* bootimg_size);
void bootimg_set_cmdline(boot_img_hdr_v1* h, const char* cmdline);
boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, void* ramdisk,
int64_t ramdisk_size, off_t ramdisk_offset, void* second,
int64_t second_size, off_t second_offset, size_t page_size, size_t base,
off_t tags_offset, uint32_t header_version, int64_t* bootimg_size);
#endif

View file

@ -435,6 +435,9 @@ static int show_help() {
#endif
" --unbuffered Do not buffer input or output.\n"
" --version Display version.\n"
" --header-version Set boot image header version while\n"
" using flash:raw and boot commands to \n"
" to create a boot image.\n"
" -h, --help show this message.\n"
);
// clang-format off
@ -443,17 +446,23 @@ static int show_help() {
static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
const std::string& second_stage, int64_t* sz,
const char* cmdline) {
const char* cmdline, uint32_t header_version) {
int64_t ksize;
void* kdata = load_file(kernel.c_str(), &ksize);
if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
// Is this actually a boot image?
if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr))) {
if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
die("cannot load '%s': too short", kernel.c_str());
}
if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr*>(kdata), cmdline);
if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), cmdline);
uint32_t header_version_existing =
reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
if (header_version != header_version_existing) {
die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
header_version, header_version_existing);
}
if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
@ -477,13 +486,13 @@ static void* load_bootable_image(const std::string& kernel, const std::string& r
fprintf(stderr,"creating boot image...\n");
int64_t bsize = 0;
void* bdata = mkbootimg(kdata, ksize, kernel_offset,
boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, kernel_offset,
rdata, rsize, ramdisk_offset,
sdata, ssize, second_offset,
page_size, base_addr, tags_offset, &bsize);
page_size, base_addr, tags_offset, header_version, &bsize);
if (bdata == nullptr) die("failed to create boot.img");
if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
if (cmdline) bootimg_set_cmdline(bdata, cmdline);
fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
*sz = bsize;
@ -1500,6 +1509,7 @@ int main(int argc, char **argv)
bool erase_first = true;
bool set_fbe_marker = false;
void *data;
uint32_t header_version = 0;
int64_t sz;
int longindex;
std::string slot_override;
@ -1525,6 +1535,7 @@ int main(int argc, char **argv)
{"skip-reboot", no_argument, 0, 0},
{"disable-verity", no_argument, 0, 0},
{"disable-verification", no_argument, 0, 0},
{"header-version", required_argument, 0, 0},
#if !defined(_WIN32)
{"wipe-and-use-fbe", no_argument, 0, 0},
#endif
@ -1617,6 +1628,8 @@ int main(int argc, char **argv)
wants_wipe = true;
set_fbe_marker = true;
#endif
} else if (strcmp("header-version", longopts[longindex].name) == 0) {
header_version = strtoul(optarg, nullptr, 0);
} else {
fprintf(stderr, "Internal error in options processing for %s\n",
longopts[longindex].name);
@ -1749,7 +1762,7 @@ int main(int argc, char **argv)
std::string second_stage;
if (!args.empty()) second_stage = next_arg(&args);
data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
fb_queue_download("boot.img", data, sz);
fb_queue_command("boot", "booting");
} else if (command == "flash") {
@ -1778,7 +1791,7 @@ int main(int argc, char **argv)
std::string second_stage;
if (!args.empty()) second_stage = next_arg(&args);
data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
auto flashraw = [&](const std::string& partition) {
fb_queue_flash(partition, data, sz);
};