Pedantic boot_img_hdr multi-version support

This commit is contained in:
topjohnwu 2019-10-15 01:46:29 -04:00
parent 5ffb9eaa5b
commit 9867a3bd60
2 changed files with 40 additions and 41 deletions

View File

@ -63,12 +63,12 @@ void dyn_img_hdr::print() {
fprintf(stderr, "KERNEL_SZ [%u]\n", kernel_size());
fprintf(stderr, "RAMDISK_SZ [%u]\n", ramdisk_size());
fprintf(stderr, "SECOND_SZ [%u]\n", second_size());
if (ver) {
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", recovery_dtbo_size());
fprintf(stderr, "DTB [%u]\n", dtb_size());
} else {
if (ver == 0)
fprintf(stderr, "EXTRA_SZ [%u]\n", extra_size());
}
if (ver >= 1)
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", recovery_dtbo_size());
if (ver >= 2)
fprintf(stderr, "DTB_SZ [%u]\n", dtb_size());
ver = os_version();
if (ver) {
@ -91,8 +91,8 @@ void dyn_img_hdr::print() {
fprintf(stderr, "NAME [%s]\n", name());
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", cmdline(), extra_cmdline());
fprintf(stderr, "CHECKSUM [");
for (int i = 0; id()[i]; ++i)
fprintf(stderr, "%02x", id()[i]);
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
fprintf(stderr, "%02hhx", static_cast<uint8_t>(id()[i]));
fprintf(stderr, "]\n");
}
@ -220,13 +220,18 @@ void boot_img::parse_image(uint8_t *addr) {
else
hdr = new dyn_img_v0(addr);
}
img_start = addr;
flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0;
for (int i = SHA_DIGEST_SIZE; i < SHA256_DIGEST_SIZE; ++i) {
if (hdr->id()[i]) {
flags |= SHA256_FLAG;
break;
}
}
hdr->print();
size_t off = hdr->page_size();
hdr_addr = addr;
get_block(kernel);
get_block(ramdisk);
get_block(second);
@ -396,7 +401,7 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
// Copy a page for header
off.header = lseek(fd, 0, SEEK_CUR);
restore_buf(fd, boot.img_start, boot.hdr->page_size());
restore_buf(fd, boot.hdr_addr, boot.hdr->page_size());
// kernel
off.kernel = lseek(fd, 0, SEEK_CUR);
@ -515,21 +520,20 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
HASH_update(&ctx, boot.map_addr + off.extra, size);
HASH_update(&ctx, &size, sizeof(size));
}
if (boot.hdr->header_version()) {
if (boot.hdr->header_version() >= 1) {
size = boot.hdr->recovery_dtbo_size();
HASH_update(&ctx, boot.map_addr + boot.hdr->recovery_dtbo_offset(), size);
HASH_update(&ctx, &size, sizeof(size));
}
if (boot.hdr->header_version() >= 2) {
size = boot.hdr->dtb_size();
if (size) {
HASH_update(&ctx, boot.map_addr + off.dtb, size);
HASH_update(&ctx, &size, sizeof(size));
}
boot.hdr->header_size() = boot.hdr->hdr_size();
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();
// Print new image info
boot.hdr->print();

View File

@ -216,10 +216,22 @@ private:
#undef decl_var
#undef decl_val
#define __impl_cls(name, hdr) \
protected: name() = default; \
public: \
name(void *ptr) { \
raw = xmalloc(sizeof(hdr)); \
memcpy(raw, ptr, sizeof(hdr)); \
} \
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)
@ -227,15 +239,6 @@ struct dyn_img_pxa : public dyn_img_hdr {
impl_val(cmdline)
impl_val(id)
impl_val(extra_cmdline)
dyn_img_pxa(void *ptr) {
raw = xmalloc(sizeof(boot_img_hdr_pxa));
memcpy(raw, ptr, sizeof(boot_img_hdr_pxa));
}
size_t hdr_size() override {
return sizeof(boot_img_hdr_pxa);
}
};
#undef impl_val
@ -243,6 +246,7 @@ struct dyn_img_pxa : public dyn_img_hdr {
decltype(std::declval<dyn_img_hdr>().name()) name() override { return v2_hdr->name; }
struct dyn_img_v0 : public dyn_img_hdr {
impl_cls(v0)
impl_val(page_size)
impl_val(extra_size)
@ -251,38 +255,29 @@ struct dyn_img_v0 : public dyn_img_hdr {
impl_val(cmdline)
impl_val(id)
impl_val(extra_cmdline)
dyn_img_v0(void *ptr) {
raw = xmalloc(sizeof(boot_img_hdr_v2));
memcpy(raw, ptr, sizeof(boot_img_hdr_v2));
}
size_t hdr_size() override {
return sizeof(boot_img_hdr_v2);
}
};
struct dyn_img_v1 : public dyn_img_v0 {
impl_cls(v1)
impl_val(header_version)
impl_val(recovery_dtbo_size)
impl_val(recovery_dtbo_offset)
impl_val(header_size)
dyn_img_v1(void *ptr) : dyn_img_v0(ptr) {}
uint32_t &extra_size() override {
return dyn_img_hdr::extra_size();
}
};
struct dyn_img_v2 : public dyn_img_v1 {
impl_cls(v2)
impl_val(dtb_size)
dyn_img_v2(void *ptr) : dyn_img_v1(ptr) {}
};
#undef __impl_cls
#undef impl_cls
#undef impl_val
// Flags
@ -329,7 +324,7 @@ struct boot_img {
size_t tail_size = 0;
// Pointers to blocks defined in header
uint8_t *img_start;
uint8_t *hdr_addr;
uint8_t *kernel;
uint8_t *ramdisk;
uint8_t *second;