Support header_version 3

This commit is contained in:
topjohnwu 2020-10-12 01:06:42 -07:00
parent d625beb7f3
commit 4aeac3b8f4
3 changed files with 227 additions and 162 deletions

View File

@ -61,12 +61,13 @@ void dyn_img_hdr::print() {
fprintf(stderr, "HEADER_VER [%u]\n", ver);
fprintf(stderr, "KERNEL_SZ [%u]\n", kernel_size());
fprintf(stderr, "RAMDISK_SZ [%u]\n", ramdisk_size());
if (ver < 3)
fprintf(stderr, "SECOND_SZ [%u]\n", second_size());
if (ver == 0)
fprintf(stderr, "EXTRA_SZ [%u]\n", extra_size());
if (ver >= 1)
if (ver == 1 || ver == 2)
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", recovery_dtbo_size());
if (ver >= 2)
if (ver == 2)
fprintf(stderr, "DTB_SZ [%u]\n", dtb_size());
ver = os_version();
@ -87,22 +88,25 @@ void dyn_img_hdr::print() {
}
fprintf(stderr, "PAGESIZE [%u]\n", page_size());
if (ver < 3)
fprintf(stderr, "NAME [%s]\n", name());
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", cmdline(), extra_cmdline());
fprintf(stderr, "CMDLINE [%.*s%.*s]\n", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
if (auto chksum = reinterpret_cast<uint8_t*>(id())) {
fprintf(stderr, "CHECKSUM [");
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
fprintf(stderr, "%02hhx", static_cast<uint8_t>(id()[i]));
fprintf(stderr, "%02hhx", chksum[i]);
fprintf(stderr, "]\n");
}
}
void dyn_img_hdr::dump_hdr_file() {
FILE *fp = xfopen(HEADER_FILE, "w");
fprintf(fp, "pagesize=%u\n", page_size());
fprintf(fp, "name=%s\n", name());
fprintf(fp, "cmdline=%.512s%.1024s\n", cmdline(), extra_cmdline());
fprintf(fp, "cmdline=%.*s%.*s\n", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
uint32_t ver = os_version();
if (ver) {
int a, b, c, y, m = 0;
int a, b, c, y, m;
int version, patch_level;
version = ver >> 11;
patch_level = ver & 0x7ff;
@ -127,11 +131,12 @@ void dyn_img_hdr::load_hdr_file() {
memset(name(), 0, 16);
memcpy(name(), value.data(), value.length() > 15 ? 15 : value.length());
} else if (key == "cmdline") {
memset(cmdline(), 0, 512);
memset(extra_cmdline(), 0, 1024);
if (value.length() > 512) {
memcpy(cmdline(), value.data(), 512);
memcpy(extra_cmdline(), &value[512], value.length() - 511);
memset(cmdline(), 0, BOOT_ARGS_SIZE);
memset(extra_cmdline(), 0, BOOT_EXTRA_ARGS_SIZE);
if (value.length() > BOOT_ARGS_SIZE) {
memcpy(cmdline(), value.data(), BOOT_ARGS_SIZE);
auto len = std::min(value.length() - BOOT_ARGS_SIZE, (size_t) BOOT_EXTRA_ARGS_SIZE);
memcpy(extra_cmdline(), &value[BOOT_ARGS_SIZE], len);
} else {
memcpy(cmdline(), value.data(), value.length());
}
@ -212,20 +217,30 @@ void boot_img::parse_image(uint8_t *addr) {
addr += ACCLAIM_PRE_HEADER_SZ;
}
if (hp->header_version == 1)
switch (hp->header_version) {
case 1:
hdr = new dyn_img_v1(addr);
else if (hp->header_version == 2)
break;
case 2:
hdr = new dyn_img_v2(addr);
else
break;
case 3:
hdr = new dyn_img_v3(addr);
break;
default:
hdr = new dyn_img_v0(addr);
break;
}
}
if (char *id = hdr->id()) {
for (int i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
if (hdr->id()[i]) {
if (id[i]) {
flags |= SHA256_FLAG;
break;
}
}
}
hdr->print();
@ -341,14 +356,14 @@ int split_image_dtb(const char *filename) {
}
}
int unpack(const char *image, bool nodecomp, bool hdr) {
int unpack(const char *image, bool skip_decomp, bool hdr) {
boot_img boot(image);
if (hdr)
boot.hdr->dump_hdr_file();
// Dump kernel
if (!nodecomp && COMPRESSED(boot.k_fmt)) {
if (!skip_decomp && COMPRESSED(boot.k_fmt)) {
int fd = creat(KERNEL_FILE, 0644);
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
close(fd);
@ -360,7 +375,7 @@ int unpack(const char *image, bool nodecomp, bool hdr) {
dump(boot.kernel_dtb, boot.kernel_dt_size, KER_DTB_FILE);
// Dump ramdisk
if (!nodecomp && COMPRESSED(boot.r_fmt)) {
if (!skip_decomp && COMPRESSED(boot.r_fmt)) {
int fd = creat(RAMDISK_FILE, 0644);
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
close(fd);
@ -372,7 +387,7 @@ int unpack(const char *image, bool nodecomp, bool hdr) {
dump(boot.second, boot.hdr->second_size(), SECOND_FILE);
// Dump extra
if (!nodecomp && COMPRESSED(boot.e_fmt)) {
if (!skip_decomp && COMPRESSED(boot.e_fmt)) {
int fd = creat(EXTRA_FILE, 0644);
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
close(fd);
@ -392,7 +407,7 @@ int unpack(const char *image, bool nodecomp, bool hdr) {
#define file_align() \
write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - off.header, boot.hdr->page_size()))
void repack(const char* src_img, const char* out_img, bool nocomp) {
void repack(const char* src_img, const char* out_img, bool skip_comp) {
boot_img boot(src_img);
auto is_flag = [&](unsigned flag) -> bool { return (boot.flags & flag); };
@ -428,11 +443,11 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
if (is_flag(DHTB_FLAG)) {
// Skip DHTB header
write_zero(fd, sizeof(dhtb_hdr));
} else if (boot.flags & BLOB_FLAG) {
} else if (is_flag(BLOB_FLAG)) {
restore_buf(fd, boot.map_addr, sizeof(blob_hdr));
} else if (boot.flags & NOOKHD_FLAG) {
} else if (is_flag(NOOKHD_FLAG)) {
restore_buf(fd, boot.map_addr, NOOKHD_PRE_HEADER_SZ);
} else if (boot.flags & ACCLAIM_FLAG) {
} else if (is_flag(ACCLAIM_FLAG)) {
restore_buf(fd, boot.map_addr, ACCLAIM_PRE_HEADER_SZ);
}
@ -473,7 +488,7 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
size_t raw_size;
void *raw_buf;
mmap_ro(RAMDISK_FILE, raw_buf, raw_size);
if (!nocomp && !COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) {
if (!skip_comp && !COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) {
boot.hdr->ramdisk_size() = compress(boot.r_fmt, fd, raw_buf, raw_size);
} else {
boot.hdr->ramdisk_size() = xwrite(fd, raw_buf, raw_size);
@ -495,7 +510,7 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
size_t raw_size;
void *raw_buf;
mmap_ro(EXTRA_FILE, raw_buf, raw_size);
if (!nocomp && !COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.e_fmt)) {
if (!skip_comp && !COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.e_fmt)) {
boot.hdr->extra_size() = compress(boot.e_fmt, fd, raw_buf, raw_size);
} else {
boot.hdr->extra_size() = xwrite(fd, raw_buf, raw_size);
@ -557,7 +572,11 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
boot.hdr->ramdisk_size() += sizeof(*hdr);
}
// Make sure header size matches
boot.hdr->header_size() = boot.hdr->hdr_size();
// Update checksum
if (char *id = boot.hdr->id()) {
HASH_CTX ctx;
is_flag(SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx);
uint32_t size = boot.hdr->kernel_size();
@ -584,16 +603,15 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
HASH_update(&ctx, boot.map_addr + off.dtb, size);
HASH_update(&ctx, &size, sizeof(size));
}
memset(boot.hdr->id(), 0, 32);
memcpy(boot.hdr->id(), HASH_final(&ctx),
(boot.flags & SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE);
boot.hdr->header_size() = boot.hdr->hdr_size();
memset(id, 0, BOOT_ID_SIZE);
memcpy(id, HASH_final(&ctx), is_flag(SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE);
}
// Print new image info
boot.hdr->print();
// Main header
memcpy(boot.map_addr + off.header, **boot.hdr, boot.hdr->hdr_size());
memcpy(boot.map_addr + off.header, boot.hdr->raw_hdr(), boot.hdr->hdr_size());
if (is_flag(DHTB_FLAG)) {
// DHTB header

View File

@ -4,9 +4,9 @@
#include <utility>
#include "format.hpp"
/****************
* Other Headers
****************/
/******************
* Special Headers
*****************/
struct mtk_hdr {
uint32_t magic; /* MTK magic */
@ -45,8 +45,39 @@ struct blob_hdr {
* Boot Image Headers
*********************/
struct boot_img_hdr_base {
char magic[8];
#define BOOT_MAGIC_SIZE 8
#define BOOT_NAME_SIZE 16
#define BOOT_ID_SIZE 32
#define BOOT_ARGS_SIZE 512
#define BOOT_EXTRA_ARGS_SIZE 1024
/*
* +-----------------+
* | boot header | 1 page
* +-----------------+
* | kernel | n pages
* +-----------------+
* | ramdisk | m pages
* +-----------------+
* | second stage | o pages
* +-----------------+
* | extra blob | x pages (non standard)
* +-----------------+
* | recovery dtbo | p pages
* +-----------------+
* | dtb | q pages
* +-----------------+
*
* n = (kernel_size + page_size - 1) / page_size
* m = (ramdisk_size + page_size - 1) / page_size
* o = (second_size + page_size - 1) / page_size
* p = (recovery_dtbo_size + page_size - 1) / page_size
* q = (dtb_size + page_size - 1) / page_size
* x = (extra_size + page_size - 1) / page_size
*/
struct boot_img_hdr_common {
char magic[BOOT_MAGIC_SIZE];
uint32_t kernel_size; /* size in bytes */
uint32_t kernel_addr; /* physical load addr */
@ -58,32 +89,31 @@ struct boot_img_hdr_base {
uint32_t second_addr; /* physical load addr */
} __attribute__((packed));
struct boot_img_hdr_v0 : public boot_img_hdr_base {
struct boot_img_hdr_v0 : public boot_img_hdr_common {
uint32_t tags_addr; /* physical addr for kernel tags */
uint32_t page_size; /* flash page size we assume */
/* In header v1, this field is used for header version
* However, on some devices like Samsung, this field is used to store DTB
* We will treat this field differently based on its value */
// In header v1, this field is used for header version
// However, on some devices like Samsung, this field is used to store DTB
// We treat this field differently based on its value
union {
uint32_t header_version; /* the version of the header */
uint32_t extra_size; /* extra blob size in bytes */
};
/* operating system version and security patch level; for
* version "A.B.C" and patch level "Y-M-D":
* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
* lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M)
* os_version = ver << 11 | lvl */
// Operating system version and security patch level.
// For version "A.B.C" and patch level "Y-M-D":
// (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
// os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
uint32_t os_version;
char name[16]; /* asciiz product name */
char cmdline[512];
char id[32]; /* timestamp / checksum / sha1 / etc */
char name[BOOT_NAME_SIZE]; /* asciiz product name */
char cmdline[BOOT_ARGS_SIZE];
char id[BOOT_ID_SIZE]; /* timestamp / checksum / sha1 / etc */
/* Supplemental command line data; kept here to maintain
* binary compatibility with older versions of mkbootimg */
char extra_cmdline[1024];
// Supplemental command line data; kept here to maintain
// binary compatibility with older versions of mkbootimg.
char extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
} __attribute__((packed));
struct boot_img_hdr_v1 : public boot_img_hdr_v0 {
@ -101,59 +131,54 @@ struct boot_img_hdr_v2 : public boot_img_hdr_v1 {
using boot_img_hdr = boot_img_hdr_v2;
// Special Samsung header
struct boot_img_hdr_pxa : public boot_img_hdr_base {
struct boot_img_hdr_pxa : public boot_img_hdr_common {
uint32_t extra_size; /* extra blob size in bytes */
uint32_t unknown;
uint32_t tags_addr; /* physical addr for kernel tags */
uint32_t page_size; /* flash page size we assume */
char name[24]; /* asciiz product name */
char cmdline[512];
char id[32]; /* timestamp / checksum / sha1 / etc */
char cmdline[BOOT_ARGS_SIZE];
char id[BOOT_ID_SIZE]; /* timestamp / checksum / sha1 / etc */
/* Supplemental command line data; kept here to maintain
* binary compatibility with older versions of mkbootimg */
char extra_cmdline[1024];
char extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
} __attribute__((packed));
/*
** +-----------------+
** | boot header | 1 page
** +-----------------+
** | kernel | n pages
** +-----------------+
** | ramdisk | m pages
** +-----------------+
** | second stage | o pages
** +-----------------+
** | extra blob | p pages
** +-----------------+
** | recovery dtbo | q pages
** +-----------------+
** | dtb | r pages
** +-----------------+
**
** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size
** p = (extra_size + page_size - 1) / page_size
** q = (recovery_dtbo_size + page_size - 1) / page_size
** r = (dtb_size + page_size - 1) / page_size
**
** 0. all entities are page_size aligned in flash
** 1. kernel and ramdisk are required (size != 0)
** 2. second is optional (second_size == 0 -> no second)
** 3. load each element (kernel, ramdisk, second) at
** the specified physical address (kernel_addr, etc)
** 4. prepare tags at tag_addr. kernel_args[] is
** appended to the kernel commandline in the tags.
** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
** 6. if second_size != 0: jump to second_addr
** else: jump to kernel_addr
*/
/* 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.
*/
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));
/*******************************
* Polymorphic Universal Header
*******************************/
#define drct_var(name) \
auto &name() { return img_hdr->name; }
#define decl_var(name, len) \
virtual uint##len##_t &name() { j##len = 0; return j##len; }
#define decl_val(name, type) \
@ -161,12 +186,10 @@ virtual type name() { return 0; }
struct dyn_img_hdr {
// Direct entries
drct_var(kernel_size)
drct_var(ramdisk_size)
drct_var(second_size)
// Standard entries
decl_var(kernel_size, 32)
decl_var(ramdisk_size, 32)
decl_var(second_size, 32)
decl_var(page_size, 32)
decl_val(header_version, uint32_t)
decl_var(extra_size, 32)
@ -188,20 +211,16 @@ struct dyn_img_hdr {
virtual size_t hdr_size() = 0;
boot_img_hdr_base * const &operator* () const {
return img_hdr;
}
const void *raw_hdr() const { return raw; }
void print();
void dump_hdr_file();
void load_hdr_file();
protected:
union {
/* Main header could be either AOSP or PXA,
* but both of them are base headers. */
boot_img_hdr_base *img_hdr; /* Common base header */
// Main header could be either AOSP or PXA
boot_img_hdr_v2 *v2_hdr; /* AOSP v2 header */
boot_img_hdr_v3 *v3_hdr; /* AOSP v3 header */
boot_img_hdr_pxa *hdr_pxa; /* Samsung PXA header */
void *raw; /* Raw pointer */
};
@ -212,7 +231,6 @@ private:
static uint64_t j64;
};
#undef drct_var
#undef decl_var
#undef decl_val
@ -227,25 +245,16 @@ size_t hdr_size() override { return sizeof(hdr); }
#define impl_cls(ver) __impl_cls(dyn_img_##ver, boot_img_hdr_##ver)
#define impl_val(name) \
decltype(std::declval<dyn_img_hdr>().name()) name() override { return hdr_pxa->name; }
struct dyn_img_pxa : public dyn_img_hdr {
impl_cls(pxa)
impl_val(extra_size)
impl_val(page_size)
impl_val(name)
impl_val(cmdline)
impl_val(id)
impl_val(extra_cmdline)
};
#undef impl_val
#define impl_val(name) \
decltype(std::declval<dyn_img_hdr>().name()) name() override { return v2_hdr->name; }
struct dyn_img_v0 : public dyn_img_hdr {
struct dyn_img_common : public dyn_img_hdr {
impl_val(kernel_size)
impl_val(ramdisk_size)
impl_val(second_size)
};
struct dyn_img_v0 : public dyn_img_common {
impl_cls(v0)
impl_val(page_size)
@ -265,9 +274,7 @@ struct dyn_img_v1 : public dyn_img_v0 {
impl_val(recovery_dtbo_offset)
impl_val(header_size)
uint32_t &extra_size() override {
return dyn_img_hdr::extra_size();
}
uint32_t &extra_size() override { return dyn_img_hdr::extra_size(); }
};
struct dyn_img_v2 : public dyn_img_v1 {
@ -276,11 +283,51 @@ struct dyn_img_v2 : public dyn_img_v1 {
impl_val(dtb_size)
};
#undef impl_val
#define impl_val(name) \
decltype(std::declval<dyn_img_hdr>().name()) name() override { return hdr_pxa->name; }
struct dyn_img_pxa : public dyn_img_common {
impl_cls(pxa)
impl_val(extra_size)
impl_val(page_size)
impl_val(name)
impl_val(cmdline)
impl_val(id)
impl_val(extra_cmdline)
};
#undef impl_val
#define impl_val(name) \
decltype(std::declval<dyn_img_hdr>().name()) name() override { return v3_hdr->name; }
struct dyn_img_v3 : public dyn_img_hdr {
impl_cls(v3)
impl_val(kernel_size)
impl_val(ramdisk_size)
impl_val(os_version)
impl_val(header_size)
impl_val(header_version)
impl_val(cmdline)
// Make API compatible
uint32_t &page_size() override { page_sz = 4096; return page_sz; }
char *extra_cmdline() override { return &v3_hdr->cmdline[BOOT_ARGS_SIZE]; }
private:
uint32_t page_sz;
};
#undef __impl_cls
#undef impl_cls
#undef impl_val
// Flags
/******************
* Full Boot Image
******************/
#define MTK_KERNEL (1 << 0)
#define MTK_RAMDISK (1 << 1)
#define CHROMEOS_FLAG (1 << 2)

View File

@ -12,8 +12,8 @@
#define DTB_FILE "dtb"
#define NEW_BOOT "new-boot.img"
int unpack(const char *image, bool nodecomp = false, bool hdr = false);
void repack(const char* src_img, const char* out_img, bool nocomp = false);
int unpack(const char *image, bool skip_decomp = false, bool hdr = false);
void repack(const char* src_img, const char* out_img, bool skip_comp = false);
int split_image_dtb(const char *filename);
int hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]);