From 84e19ceef04d8040050bdf3ffcb854af54da3261 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 31 Oct 2021 10:52:12 -0700 Subject: [PATCH] Tidy up bootimg.h Close #4796 --- native/jni/magiskboot/bootimg.cpp | 22 ++--- native/jni/magiskboot/bootimg.hpp | 151 +++++++++++++----------------- 2 files changed, 77 insertions(+), 96 deletions(-) diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 71336f19d..28178163b 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -60,7 +60,7 @@ void dyn_img_hdr::print() { fprintf(stderr, "%-*s [%u]\n", PADDING, "EXTRA_SZ", extra_size()); if (ver == 1 || ver == 2) fprintf(stderr, "%-*s [%u]\n", PADDING, "RECOV_DTBO_SZ", recovery_dtbo_size()); - if (ver == 2) + if (ver == 2 || is_vendor) fprintf(stderr, "%-*s [%u]\n", PADDING, "DTB_SZ", dtb_size()); if (uint32_t os_ver = os_version()) { @@ -79,15 +79,15 @@ void dyn_img_hdr::print() { } fprintf(stderr, "%-*s [%u]\n", PADDING, "PAGESIZE", page_size()); - if (ver < 3) { - fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", name()); + if (char *n = name()) { + fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", n); } fprintf(stderr, "%-*s [%.*s%.*s]\n", PADDING, "CMDLINE", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline()); - if (auto chksum = reinterpret_cast(id())) { + if (char *checksum = id()) { fprintf(stderr, "%-*s [", PADDING, "CHECKSUM"); for (int i = 0; i < SHA256_DIGEST_SIZE; ++i) - fprintf(stderr, "%02hhx", chksum[i]); + fprintf(stderr, "%02hhx", checksum[i]); fprintf(stderr, "]\n"); } } @@ -241,13 +241,13 @@ off += hdr->name##_size(); \ off = do_align(off, hdr->page_size()); \ } -#define CMD_MATCH(s) BUFFER_MATCH(hp->cmdline, s) +#define CMD_MATCH(s) BUFFER_MATCH(h->cmdline, s) void boot_img::parse_image(uint8_t *addr, format_t type) { - auto hp = reinterpret_cast(addr); + auto h = reinterpret_cast(addr); if (type == AOSP_VENDOR) { fprintf(stderr, "VENDOR_BOOT_HDR\n"); - switch (hp->header_version) { + switch (h->header_version) { case 4: hdr = new dyn_img_vnd_v4(addr); break; @@ -256,7 +256,7 @@ void boot_img::parse_image(uint8_t *addr, format_t type) { hdr = new dyn_img_vnd_v3(addr); break; } - } else if (hp->page_size >= 0x02000000) { + } else if (h->page_size >= 0x02000000) { fprintf(stderr, "PXA_BOOT_HDR\n"); hdr = new dyn_img_pxa(addr); } else { @@ -268,13 +268,13 @@ void boot_img::parse_image(uint8_t *addr, format_t type) { flags[NOOKHD_FLAG] = true; fprintf(stderr, "NOOKHD_LOADER\n"); addr += NOOKHD_PRE_HEADER_SZ; - } else if (memcmp(hp->name, ACCLAIM_MAGIC, 10) == 0) { + } else if (memcmp(h->name, ACCLAIM_MAGIC, 10) == 0) { flags[ACCLAIM_FLAG] = true; fprintf(stderr, "ACCLAIM_LOADER\n"); addr += ACCLAIM_PRE_HEADER_SZ; } - switch (hp->header_version) { + switch (h->header_version) { case 1: hdr = new dyn_img_v1(addr); break; diff --git a/native/jni/magiskboot/bootimg.hpp b/native/jni/magiskboot/bootimg.hpp index 4e29d0869..2b9dc4893 100644 --- a/native/jni/magiskboot/bootimg.hpp +++ b/native/jni/magiskboot/bootimg.hpp @@ -98,6 +98,8 @@ struct AvbVBMetaImageHeader { * Boot Image Headers *********************/ +// https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/android12-release/include/bootimg/bootimg.h + #define BOOT_MAGIC_SIZE 8 #define BOOT_NAME_SIZE 16 #define BOOT_ID_SIZE 32 @@ -107,7 +109,9 @@ struct AvbVBMetaImageHeader { #define VENDOR_RAMDISK_NAME_SIZE 32 #define VENDOR_RAMDISK_TABLE_ENTRY_BOARD_ID_SIZE 16 -/* +/* When the boot image header has a version of 0 - 2, the structure of the boot + * image is as follows: + * * +-----------------+ * | boot header | 1 page * +-----------------+ @@ -132,7 +136,7 @@ struct AvbVBMetaImageHeader { * x = (extra_size + page_size - 1) / page_size */ -struct boot_img_hdr_common { +struct boot_img_hdr_v0_common { char magic[BOOT_MAGIC_SIZE]; uint32_t kernel_size; /* size in bytes */ @@ -145,7 +149,7 @@ struct boot_img_hdr_common { uint32_t second_addr; /* physical load addr */ } __attribute__((packed)); -struct boot_img_hdr_v0 : public boot_img_hdr_common { +struct boot_img_hdr_v0 : public boot_img_hdr_v0_common { uint32_t tags_addr; /* physical addr for kernel tags */ uint32_t page_size; /* flash page size we assume */ @@ -183,11 +187,8 @@ struct boot_img_hdr_v2 : public boot_img_hdr_v1 { uint64_t dtb_addr; /* physical load address for DTB image */ } __attribute__((packed)); -// Default to hdr v2 -using boot_img_hdr = boot_img_hdr_v2; - // Special Samsung header -struct boot_img_hdr_pxa : public boot_img_hdr_common { +struct boot_img_hdr_pxa : public boot_img_hdr_v0_common { uint32_t extra_size; /* extra blob size in bytes */ uint32_t unknown; uint32_t tags_addr; /* physical addr for kernel tags */ @@ -200,70 +201,7 @@ struct boot_img_hdr_pxa : public boot_img_hdr_common { char extra_cmdline[BOOT_EXTRA_ARGS_SIZE]; } __attribute__((packed)); -/* When the boot image header has a version of 3, the structure of the boot - * image is as follows: - * - * +---------------------+ - * | boot header | 4096 bytes - * +---------------------+ - * | kernel | m pages - * +---------------------+ - * | ramdisk | n pages - * +---------------------+ - * - * m = (kernel_size + 4096 - 1) / 4096 - * n = (ramdisk_size + 4096 - 1) / 4096 - * - * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes. - * - * The structure of the vendor boot image (introduced with version 3 and - * required to be present when a v3 boot image is used) is as follows: - * - * +---------------------+ - * | vendor boot header | o pages - * +---------------------+ - * | vendor ramdisk | p pages - * +---------------------+ - * | dtb | q pages - * +---------------------+ - * - * o = (2112 + page_size - 1) / page_size - * p = (ramdisk_size + page_size - 1) / page_size - * q = (dtb_size + page_size - 1) / page_size - */ - -struct boot_img_hdr_v3 { - uint8_t magic[BOOT_MAGIC_SIZE]; - - uint32_t kernel_size; /* size in bytes */ - uint32_t ramdisk_size; /* size in bytes */ - uint32_t os_version; - uint32_t header_size; - uint32_t reserved[4]; - - uint32_t header_version; - - char cmdline[BOOT_ARGS_SIZE + BOOT_EXTRA_ARGS_SIZE]; -} __attribute__((packed)); - -struct boot_img_hdr_vnd_v3 { - // Must be VENDOR_BOOT_MAGIC. - uint8_t magic[BOOT_MAGIC_SIZE]; - // Version of the vendor boot image header. - uint32_t header_version; - uint32_t page_size; /* flash page size we assume */ - uint32_t kernel_addr; /* physical load addr */ - uint32_t ramdisk_addr; /* physical load addr */ - uint32_t ramdisk_size; /* size in bytes */ - char cmdline[VENDOR_BOOT_ARGS_SIZE]; - uint32_t tags_addr; /* physical addr for kernel tags (if required) */ - char name[BOOT_NAME_SIZE]; /* asciiz product name */ - uint32_t header_size; - uint32_t dtb_size; /* size in bytes for DTB image */ - uint64_t dtb_addr; /* physical load address for DTB image */ -} __attribute__((packed)); - -/* When the boot image header has a version of 4, the structure of the boot +/* When the boot image header has a version of 3 - 4, the structure of the boot * image is as follows: * * +---------------------+ @@ -280,11 +218,9 @@ struct boot_img_hdr_vnd_v3 { * n = (ramdisk_size + 4096 - 1) / 4096 * g = (signature_size + 4096 - 1) / 4096 * - * Note that in version 4 of the boot image header, page size is fixed at 4096 - * bytes. + * Page size is fixed at 4096 bytes. * - * The structure of the vendor boot image version 4, which is required to be - * present when a version 4 boot image is used, is as follows: + * The structure of the vendor boot image is as follows: * * +------------------------+ * | vendor boot header | o pages @@ -336,6 +272,38 @@ struct boot_img_hdr_vnd_v3 { * after the generic ramdisk, followed by the bootconfig trailer, before * entering the kernel. */ + +struct boot_img_hdr_v3 { + uint8_t magic[BOOT_MAGIC_SIZE]; + + uint32_t kernel_size; /* size in bytes */ + uint32_t ramdisk_size; /* size in bytes */ + uint32_t os_version; + uint32_t header_size; + uint32_t reserved[4]; + + uint32_t header_version; + + char cmdline[BOOT_ARGS_SIZE + BOOT_EXTRA_ARGS_SIZE]; +} __attribute__((packed)); + +struct boot_img_hdr_vnd_v3 { + // Must be VENDOR_BOOT_MAGIC. + uint8_t magic[BOOT_MAGIC_SIZE]; + // Version of the vendor boot image header. + uint32_t header_version; + uint32_t page_size; /* flash page size we assume */ + uint32_t kernel_addr; /* physical load addr */ + uint32_t ramdisk_addr; /* physical load addr */ + uint32_t ramdisk_size; /* size in bytes */ + char cmdline[VENDOR_BOOT_ARGS_SIZE]; + uint32_t tags_addr; /* physical addr for kernel tags (if required) */ + char name[BOOT_NAME_SIZE]; /* asciiz product name */ + uint32_t header_size; + uint32_t dtb_size; /* size in bytes for DTB image */ + uint64_t dtb_addr; /* physical load address for DTB image */ +} __attribute__((packed)); + struct boot_img_hdr_v4 : public boot_img_hdr_v3 { uint32_t signature_size; /* size in bytes */ } __attribute__((packed)); @@ -369,6 +337,8 @@ virtual type name() { return 0; } struct dyn_img_hdr { + const bool is_vendor; + // Standard entries decl_var(kernel_size, 32) decl_var(ramdisk_size, 32) @@ -413,6 +383,7 @@ protected: boot_img_hdr_vnd_v4 *v4_vnd; /* AOSP vendor v4 header */ void *raw; /* Raw pointer */ }; + dyn_img_hdr(bool b) : is_vendor(b) {} private: // Junk for references @@ -443,7 +414,12 @@ decltype(std::declval().name()) name() override { return hdr_name-> #define impl_cls(ver) __impl_cls(dyn_img_##ver, boot_img_hdr_##ver) #define impl_val(name) __impl_val(name, v2_hdr) -struct dyn_img_common : public dyn_img_hdr { +struct dyn_img_hdr_boot : public dyn_img_hdr { +protected: + dyn_img_hdr_boot() : dyn_img_hdr(false) {} +}; + +struct dyn_img_common : public dyn_img_hdr_boot { impl_val(kernel_size) impl_val(ramdisk_size) impl_val(second_size) @@ -495,7 +471,7 @@ struct dyn_img_pxa : public dyn_img_common { #undef impl_val #define impl_val(name) __impl_val(name, v3_hdr) -struct dyn_img_v3 : public dyn_img_hdr { +struct dyn_img_v3 : public dyn_img_hdr_boot { impl_cls(v3) impl_val(kernel_size) @@ -513,10 +489,22 @@ private: uint32_t page_sz = 4096; }; +#undef impl_val +#define impl_val(name) __impl_val(name, v4_hdr) + +struct dyn_img_v4 : public dyn_img_v3 { + impl_cls(v4) +}; + +struct dyn_img_hdr_vendor : public dyn_img_hdr { +protected: + dyn_img_hdr_vendor() : dyn_img_hdr(true) {} +}; + #undef impl_val #define impl_val(name) __impl_val(name, v3_vnd) -struct dyn_img_vnd_v3 : public dyn_img_hdr { +struct dyn_img_vnd_v3 : public dyn_img_hdr_vendor { impl_cls(vnd_v3) impl_val(header_version) @@ -533,13 +521,6 @@ struct dyn_img_vnd_v3 : public dyn_img_hdr { char *extra_cmdline() override { return &v3_vnd->cmdline[BOOT_ARGS_SIZE]; } }; -#undef impl_val -#define impl_val(name) __impl_val(name, v4_hdr) - -struct dyn_img_v4 : public dyn_img_v3 { - impl_cls(v4) -}; - #undef impl_val #define impl_val(name) __impl_val(name, v4_vnd)