Support header_version 3
This commit is contained in:
parent
d625beb7f3
commit
4aeac3b8f4
@ -61,12 +61,13 @@ void dyn_img_hdr::print() {
|
|||||||
fprintf(stderr, "HEADER_VER [%u]\n", ver);
|
fprintf(stderr, "HEADER_VER [%u]\n", ver);
|
||||||
fprintf(stderr, "KERNEL_SZ [%u]\n", kernel_size());
|
fprintf(stderr, "KERNEL_SZ [%u]\n", kernel_size());
|
||||||
fprintf(stderr, "RAMDISK_SZ [%u]\n", ramdisk_size());
|
fprintf(stderr, "RAMDISK_SZ [%u]\n", ramdisk_size());
|
||||||
fprintf(stderr, "SECOND_SZ [%u]\n", second_size());
|
if (ver < 3)
|
||||||
|
fprintf(stderr, "SECOND_SZ [%u]\n", second_size());
|
||||||
if (ver == 0)
|
if (ver == 0)
|
||||||
fprintf(stderr, "EXTRA_SZ [%u]\n", extra_size());
|
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());
|
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", recovery_dtbo_size());
|
||||||
if (ver >= 2)
|
if (ver == 2)
|
||||||
fprintf(stderr, "DTB_SZ [%u]\n", dtb_size());
|
fprintf(stderr, "DTB_SZ [%u]\n", dtb_size());
|
||||||
|
|
||||||
ver = os_version();
|
ver = os_version();
|
||||||
@ -87,22 +88,25 @@ void dyn_img_hdr::print() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "PAGESIZE [%u]\n", page_size());
|
fprintf(stderr, "PAGESIZE [%u]\n", page_size());
|
||||||
fprintf(stderr, "NAME [%s]\n", name());
|
if (ver < 3)
|
||||||
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", cmdline(), extra_cmdline());
|
fprintf(stderr, "NAME [%s]\n", name());
|
||||||
fprintf(stderr, "CHECKSUM [");
|
fprintf(stderr, "CMDLINE [%.*s%.*s]\n", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
|
||||||
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
|
if (auto chksum = reinterpret_cast<uint8_t*>(id())) {
|
||||||
fprintf(stderr, "%02hhx", static_cast<uint8_t>(id()[i]));
|
fprintf(stderr, "CHECKSUM [");
|
||||||
fprintf(stderr, "]\n");
|
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
|
||||||
|
fprintf(stderr, "%02hhx", chksum[i]);
|
||||||
|
fprintf(stderr, "]\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dyn_img_hdr::dump_hdr_file() {
|
void dyn_img_hdr::dump_hdr_file() {
|
||||||
FILE *fp = xfopen(HEADER_FILE, "w");
|
FILE *fp = xfopen(HEADER_FILE, "w");
|
||||||
fprintf(fp, "pagesize=%u\n", page_size());
|
fprintf(fp, "pagesize=%u\n", page_size());
|
||||||
fprintf(fp, "name=%s\n", name());
|
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();
|
uint32_t ver = os_version();
|
||||||
if (ver) {
|
if (ver) {
|
||||||
int a, b, c, y, m = 0;
|
int a, b, c, y, m;
|
||||||
int version, patch_level;
|
int version, patch_level;
|
||||||
version = ver >> 11;
|
version = ver >> 11;
|
||||||
patch_level = ver & 0x7ff;
|
patch_level = ver & 0x7ff;
|
||||||
@ -127,11 +131,12 @@ void dyn_img_hdr::load_hdr_file() {
|
|||||||
memset(name(), 0, 16);
|
memset(name(), 0, 16);
|
||||||
memcpy(name(), value.data(), value.length() > 15 ? 15 : value.length());
|
memcpy(name(), value.data(), value.length() > 15 ? 15 : value.length());
|
||||||
} else if (key == "cmdline") {
|
} else if (key == "cmdline") {
|
||||||
memset(cmdline(), 0, 512);
|
memset(cmdline(), 0, BOOT_ARGS_SIZE);
|
||||||
memset(extra_cmdline(), 0, 1024);
|
memset(extra_cmdline(), 0, BOOT_EXTRA_ARGS_SIZE);
|
||||||
if (value.length() > 512) {
|
if (value.length() > BOOT_ARGS_SIZE) {
|
||||||
memcpy(cmdline(), value.data(), 512);
|
memcpy(cmdline(), value.data(), BOOT_ARGS_SIZE);
|
||||||
memcpy(extra_cmdline(), &value[512], value.length() - 511);
|
auto len = std::min(value.length() - BOOT_ARGS_SIZE, (size_t) BOOT_EXTRA_ARGS_SIZE);
|
||||||
|
memcpy(extra_cmdline(), &value[BOOT_ARGS_SIZE], len);
|
||||||
} else {
|
} else {
|
||||||
memcpy(cmdline(), value.data(), value.length());
|
memcpy(cmdline(), value.data(), value.length());
|
||||||
}
|
}
|
||||||
@ -212,18 +217,28 @@ void boot_img::parse_image(uint8_t *addr) {
|
|||||||
addr += ACCLAIM_PRE_HEADER_SZ;
|
addr += ACCLAIM_PRE_HEADER_SZ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hp->header_version == 1)
|
switch (hp->header_version) {
|
||||||
|
case 1:
|
||||||
hdr = new dyn_img_v1(addr);
|
hdr = new dyn_img_v1(addr);
|
||||||
else if (hp->header_version == 2)
|
break;
|
||||||
|
case 2:
|
||||||
hdr = new dyn_img_v2(addr);
|
hdr = new dyn_img_v2(addr);
|
||||||
else
|
break;
|
||||||
|
case 3:
|
||||||
|
hdr = new dyn_img_v3(addr);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
hdr = new dyn_img_v0(addr);
|
hdr = new dyn_img_v0(addr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
|
if (char *id = hdr->id()) {
|
||||||
if (hdr->id()[i]) {
|
for (int i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
|
||||||
flags |= SHA256_FLAG;
|
if (id[i]) {
|
||||||
break;
|
flags |= SHA256_FLAG;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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);
|
boot_img boot(image);
|
||||||
|
|
||||||
if (hdr)
|
if (hdr)
|
||||||
boot.hdr->dump_hdr_file();
|
boot.hdr->dump_hdr_file();
|
||||||
|
|
||||||
// Dump kernel
|
// Dump kernel
|
||||||
if (!nodecomp && COMPRESSED(boot.k_fmt)) {
|
if (!skip_decomp && COMPRESSED(boot.k_fmt)) {
|
||||||
int fd = creat(KERNEL_FILE, 0644);
|
int fd = creat(KERNEL_FILE, 0644);
|
||||||
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
|
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
|
||||||
close(fd);
|
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(boot.kernel_dtb, boot.kernel_dt_size, KER_DTB_FILE);
|
||||||
|
|
||||||
// Dump ramdisk
|
// Dump ramdisk
|
||||||
if (!nodecomp && COMPRESSED(boot.r_fmt)) {
|
if (!skip_decomp && COMPRESSED(boot.r_fmt)) {
|
||||||
int fd = creat(RAMDISK_FILE, 0644);
|
int fd = creat(RAMDISK_FILE, 0644);
|
||||||
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
|
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
|
||||||
close(fd);
|
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(boot.second, boot.hdr->second_size(), SECOND_FILE);
|
||||||
|
|
||||||
// Dump extra
|
// Dump extra
|
||||||
if (!nodecomp && COMPRESSED(boot.e_fmt)) {
|
if (!skip_decomp && COMPRESSED(boot.e_fmt)) {
|
||||||
int fd = creat(EXTRA_FILE, 0644);
|
int fd = creat(EXTRA_FILE, 0644);
|
||||||
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
|
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
|
||||||
close(fd);
|
close(fd);
|
||||||
@ -392,7 +407,7 @@ int unpack(const char *image, bool nodecomp, bool hdr) {
|
|||||||
#define file_align() \
|
#define file_align() \
|
||||||
write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - off.header, boot.hdr->page_size()))
|
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);
|
boot_img boot(src_img);
|
||||||
|
|
||||||
auto is_flag = [&](unsigned flag) -> bool { return (boot.flags & flag); };
|
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)) {
|
if (is_flag(DHTB_FLAG)) {
|
||||||
// Skip DHTB header
|
// Skip DHTB header
|
||||||
write_zero(fd, sizeof(dhtb_hdr));
|
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));
|
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);
|
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);
|
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;
|
size_t raw_size;
|
||||||
void *raw_buf;
|
void *raw_buf;
|
||||||
mmap_ro(RAMDISK_FILE, raw_buf, raw_size);
|
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);
|
boot.hdr->ramdisk_size() = compress(boot.r_fmt, fd, raw_buf, raw_size);
|
||||||
} else {
|
} else {
|
||||||
boot.hdr->ramdisk_size() = xwrite(fd, raw_buf, raw_size);
|
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;
|
size_t raw_size;
|
||||||
void *raw_buf;
|
void *raw_buf;
|
||||||
mmap_ro(EXTRA_FILE, raw_buf, raw_size);
|
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);
|
boot.hdr->extra_size() = compress(boot.e_fmt, fd, raw_buf, raw_size);
|
||||||
} else {
|
} else {
|
||||||
boot.hdr->extra_size() = xwrite(fd, raw_buf, raw_size);
|
boot.hdr->extra_size() = xwrite(fd, raw_buf, raw_size);
|
||||||
@ -557,43 +572,46 @@ void repack(const char* src_img, const char* out_img, bool nocomp) {
|
|||||||
boot.hdr->ramdisk_size() += sizeof(*hdr);
|
boot.hdr->ramdisk_size() += sizeof(*hdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update checksum
|
// Make sure header size matches
|
||||||
HASH_CTX ctx;
|
|
||||||
is_flag(SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx);
|
|
||||||
uint32_t size = boot.hdr->kernel_size();
|
|
||||||
HASH_update(&ctx, boot.map_addr + off.kernel, size);
|
|
||||||
HASH_update(&ctx, &size, sizeof(size));
|
|
||||||
size = boot.hdr->ramdisk_size();
|
|
||||||
HASH_update(&ctx, boot.map_addr + off.ramdisk, size);
|
|
||||||
HASH_update(&ctx, &size, sizeof(size));
|
|
||||||
size = boot.hdr->second_size();
|
|
||||||
HASH_update(&ctx, boot.map_addr + off.second, size);
|
|
||||||
HASH_update(&ctx, &size, sizeof(size));
|
|
||||||
size = boot.hdr->extra_size();
|
|
||||||
if (size) {
|
|
||||||
HASH_update(&ctx, boot.map_addr + off.extra, size);
|
|
||||||
HASH_update(&ctx, &size, sizeof(size));
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
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();
|
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();
|
||||||
|
HASH_update(&ctx, boot.map_addr + off.kernel, size);
|
||||||
|
HASH_update(&ctx, &size, sizeof(size));
|
||||||
|
size = boot.hdr->ramdisk_size();
|
||||||
|
HASH_update(&ctx, boot.map_addr + off.ramdisk, size);
|
||||||
|
HASH_update(&ctx, &size, sizeof(size));
|
||||||
|
size = boot.hdr->second_size();
|
||||||
|
HASH_update(&ctx, boot.map_addr + off.second, size);
|
||||||
|
HASH_update(&ctx, &size, sizeof(size));
|
||||||
|
size = boot.hdr->extra_size();
|
||||||
|
if (size) {
|
||||||
|
HASH_update(&ctx, boot.map_addr + off.extra, size);
|
||||||
|
HASH_update(&ctx, &size, sizeof(size));
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
HASH_update(&ctx, boot.map_addr + off.dtb, size);
|
||||||
|
HASH_update(&ctx, &size, sizeof(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
|
// Print new image info
|
||||||
boot.hdr->print();
|
boot.hdr->print();
|
||||||
|
|
||||||
// Main header
|
// 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)) {
|
if (is_flag(DHTB_FLAG)) {
|
||||||
// DHTB header
|
// DHTB header
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include "format.hpp"
|
#include "format.hpp"
|
||||||
|
|
||||||
/****************
|
/******************
|
||||||
* Other Headers
|
* Special Headers
|
||||||
****************/
|
*****************/
|
||||||
|
|
||||||
struct mtk_hdr {
|
struct mtk_hdr {
|
||||||
uint32_t magic; /* MTK magic */
|
uint32_t magic; /* MTK magic */
|
||||||
@ -45,8 +45,39 @@ struct blob_hdr {
|
|||||||
* Boot Image Headers
|
* Boot Image Headers
|
||||||
*********************/
|
*********************/
|
||||||
|
|
||||||
struct boot_img_hdr_base {
|
#define BOOT_MAGIC_SIZE 8
|
||||||
char magic[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_size; /* size in bytes */
|
||||||
uint32_t kernel_addr; /* physical load addr */
|
uint32_t kernel_addr; /* physical load addr */
|
||||||
@ -58,32 +89,31 @@ struct boot_img_hdr_base {
|
|||||||
uint32_t second_addr; /* physical load addr */
|
uint32_t second_addr; /* physical load addr */
|
||||||
} __attribute__((packed));
|
} __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 tags_addr; /* physical addr for kernel tags */
|
||||||
uint32_t page_size; /* flash page size we assume */
|
uint32_t page_size; /* flash page size we assume */
|
||||||
|
|
||||||
/* In header v1, this field is used for header version
|
// In header v1, this field is used for header version
|
||||||
* However, on some devices like Samsung, this field is used to store DTB
|
// However, on some devices like Samsung, this field is used to store DTB
|
||||||
* We will treat this field differently based on its value */
|
// We treat this field differently based on its value
|
||||||
union {
|
union {
|
||||||
uint32_t header_version; /* the version of the header */
|
uint32_t header_version; /* the version of the header */
|
||||||
uint32_t extra_size; /* extra blob size in bytes */
|
uint32_t extra_size; /* extra blob size in bytes */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* operating system version and security patch level; for
|
// Operating system version and security patch level.
|
||||||
* version "A.B.C" and patch level "Y-M-D":
|
// 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)
|
// (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
|
||||||
* lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M)
|
// os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
|
||||||
* os_version = ver << 11 | lvl */
|
|
||||||
uint32_t os_version;
|
uint32_t os_version;
|
||||||
|
|
||||||
char name[16]; /* asciiz product name */
|
char name[BOOT_NAME_SIZE]; /* asciiz product name */
|
||||||
char cmdline[512];
|
char cmdline[BOOT_ARGS_SIZE];
|
||||||
char id[32]; /* timestamp / checksum / sha1 / etc */
|
char id[BOOT_ID_SIZE]; /* timestamp / checksum / sha1 / etc */
|
||||||
|
|
||||||
/* Supplemental command line data; kept here to maintain
|
// Supplemental command line data; kept here to maintain
|
||||||
* binary compatibility with older versions of mkbootimg */
|
// binary compatibility with older versions of mkbootimg.
|
||||||
char extra_cmdline[1024];
|
char extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
|
||||||
} __attribute__((packed));
|
} __attribute__((packed));
|
||||||
|
|
||||||
struct boot_img_hdr_v1 : public boot_img_hdr_v0 {
|
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;
|
using boot_img_hdr = boot_img_hdr_v2;
|
||||||
|
|
||||||
// Special Samsung header
|
// 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 extra_size; /* extra blob size in bytes */
|
||||||
uint32_t unknown;
|
uint32_t unknown;
|
||||||
uint32_t tags_addr; /* physical addr for kernel tags */
|
uint32_t tags_addr; /* physical addr for kernel tags */
|
||||||
uint32_t page_size; /* flash page size we assume */
|
uint32_t page_size; /* flash page size we assume */
|
||||||
|
|
||||||
char name[24]; /* asciiz product name */
|
char name[24]; /* asciiz product name */
|
||||||
char cmdline[512];
|
char cmdline[BOOT_ARGS_SIZE];
|
||||||
char id[32]; /* timestamp / checksum / sha1 / etc */
|
char id[BOOT_ID_SIZE]; /* timestamp / checksum / sha1 / etc */
|
||||||
|
|
||||||
/* Supplemental command line data; kept here to maintain
|
char extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
|
||||||
* binary compatibility with older versions of mkbootimg */
|
|
||||||
char extra_cmdline[1024];
|
|
||||||
} __attribute__((packed));
|
} __attribute__((packed));
|
||||||
|
|
||||||
/*
|
/* When the boot image header has a version of 3, the structure of the boot
|
||||||
** +-----------------+
|
* image is as follows:
|
||||||
** | boot header | 1 page
|
*
|
||||||
** +-----------------+
|
* +---------------------+
|
||||||
** | kernel | n pages
|
* | boot header | 4096 bytes
|
||||||
** +-----------------+
|
* +---------------------+
|
||||||
** | ramdisk | m pages
|
* | kernel | m pages
|
||||||
** +-----------------+
|
* +---------------------+
|
||||||
** | second stage | o pages
|
* | ramdisk | n pages
|
||||||
** +-----------------+
|
* +---------------------+
|
||||||
** | extra blob | p pages
|
*
|
||||||
** +-----------------+
|
* m = (kernel_size + 4096 - 1) / 4096
|
||||||
** | recovery dtbo | q pages
|
* n = (ramdisk_size + 4096 - 1) / 4096
|
||||||
** +-----------------+
|
*
|
||||||
** | dtb | r pages
|
* Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
|
||||||
** +-----------------+
|
*/
|
||||||
**
|
|
||||||
** n = (kernel_size + page_size - 1) / page_size
|
struct boot_img_hdr_v3 {
|
||||||
** m = (ramdisk_size + page_size - 1) / page_size
|
uint8_t magic[BOOT_MAGIC_SIZE];
|
||||||
** o = (second_size + page_size - 1) / page_size
|
|
||||||
** p = (extra_size + page_size - 1) / page_size
|
uint32_t kernel_size; /* size in bytes */
|
||||||
** q = (recovery_dtbo_size + page_size - 1) / page_size
|
uint32_t ramdisk_size; /* size in bytes */
|
||||||
** r = (dtb_size + page_size - 1) / page_size
|
uint32_t os_version;
|
||||||
**
|
uint32_t header_size;
|
||||||
** 0. all entities are page_size aligned in flash
|
uint32_t reserved[4];
|
||||||
** 1. kernel and ramdisk are required (size != 0)
|
|
||||||
** 2. second is optional (second_size == 0 -> no second)
|
uint32_t header_version;
|
||||||
** 3. load each element (kernel, ramdisk, second) at
|
|
||||||
** the specified physical address (kernel_addr, etc)
|
char cmdline[BOOT_ARGS_SIZE + BOOT_EXTRA_ARGS_SIZE];
|
||||||
** 4. prepare tags at tag_addr. kernel_args[] is
|
} __attribute__((packed));
|
||||||
** 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
|
* Polymorphic Universal Header
|
||||||
** else: jump to kernel_addr
|
*******************************/
|
||||||
*/
|
|
||||||
|
|
||||||
#define drct_var(name) \
|
|
||||||
auto &name() { return img_hdr->name; }
|
|
||||||
#define decl_var(name, len) \
|
#define decl_var(name, len) \
|
||||||
virtual uint##len##_t &name() { j##len = 0; return j##len; }
|
virtual uint##len##_t &name() { j##len = 0; return j##len; }
|
||||||
#define decl_val(name, type) \
|
#define decl_val(name, type) \
|
||||||
@ -161,12 +186,10 @@ virtual type name() { return 0; }
|
|||||||
|
|
||||||
struct dyn_img_hdr {
|
struct dyn_img_hdr {
|
||||||
|
|
||||||
// Direct entries
|
|
||||||
drct_var(kernel_size)
|
|
||||||
drct_var(ramdisk_size)
|
|
||||||
drct_var(second_size)
|
|
||||||
|
|
||||||
// Standard entries
|
// Standard entries
|
||||||
|
decl_var(kernel_size, 32)
|
||||||
|
decl_var(ramdisk_size, 32)
|
||||||
|
decl_var(second_size, 32)
|
||||||
decl_var(page_size, 32)
|
decl_var(page_size, 32)
|
||||||
decl_val(header_version, uint32_t)
|
decl_val(header_version, uint32_t)
|
||||||
decl_var(extra_size, 32)
|
decl_var(extra_size, 32)
|
||||||
@ -188,20 +211,16 @@ struct dyn_img_hdr {
|
|||||||
|
|
||||||
virtual size_t hdr_size() = 0;
|
virtual size_t hdr_size() = 0;
|
||||||
|
|
||||||
boot_img_hdr_base * const &operator* () const {
|
const void *raw_hdr() const { return raw; }
|
||||||
return img_hdr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void print();
|
void print();
|
||||||
void dump_hdr_file();
|
void dump_hdr_file();
|
||||||
void load_hdr_file();
|
void load_hdr_file();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
union {
|
union {
|
||||||
/* Main header could be either AOSP or PXA,
|
// Main header could be either AOSP or PXA
|
||||||
* but both of them are base headers. */
|
|
||||||
boot_img_hdr_base *img_hdr; /* Common base header */
|
|
||||||
boot_img_hdr_v2 *v2_hdr; /* AOSP v2 header */
|
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 */
|
boot_img_hdr_pxa *hdr_pxa; /* Samsung PXA header */
|
||||||
void *raw; /* Raw pointer */
|
void *raw; /* Raw pointer */
|
||||||
};
|
};
|
||||||
@ -212,7 +231,6 @@ private:
|
|||||||
static uint64_t j64;
|
static uint64_t j64;
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef drct_var
|
|
||||||
#undef decl_var
|
#undef decl_var
|
||||||
#undef decl_val
|
#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_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) \
|
#define impl_val(name) \
|
||||||
decltype(std::declval<dyn_img_hdr>().name()) name() override { return v2_hdr->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_cls(v0)
|
||||||
|
|
||||||
impl_val(page_size)
|
impl_val(page_size)
|
||||||
@ -265,9 +274,7 @@ struct dyn_img_v1 : public dyn_img_v0 {
|
|||||||
impl_val(recovery_dtbo_offset)
|
impl_val(recovery_dtbo_offset)
|
||||||
impl_val(header_size)
|
impl_val(header_size)
|
||||||
|
|
||||||
uint32_t &extra_size() override {
|
uint32_t &extra_size() override { return dyn_img_hdr::extra_size(); }
|
||||||
return dyn_img_hdr::extra_size();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct dyn_img_v2 : public dyn_img_v1 {
|
struct dyn_img_v2 : public dyn_img_v1 {
|
||||||
@ -276,11 +283,51 @@ struct dyn_img_v2 : public dyn_img_v1 {
|
|||||||
impl_val(dtb_size)
|
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_cls
|
#undef impl_cls
|
||||||
#undef impl_val
|
#undef impl_val
|
||||||
|
|
||||||
// Flags
|
/******************
|
||||||
|
* Full Boot Image
|
||||||
|
******************/
|
||||||
|
|
||||||
#define MTK_KERNEL (1 << 0)
|
#define MTK_KERNEL (1 << 0)
|
||||||
#define MTK_RAMDISK (1 << 1)
|
#define MTK_RAMDISK (1 << 1)
|
||||||
#define CHROMEOS_FLAG (1 << 2)
|
#define CHROMEOS_FLAG (1 << 2)
|
||||||
|
@ -12,8 +12,8 @@
|
|||||||
#define DTB_FILE "dtb"
|
#define DTB_FILE "dtb"
|
||||||
#define NEW_BOOT "new-boot.img"
|
#define NEW_BOOT "new-boot.img"
|
||||||
|
|
||||||
int unpack(const char *image, bool nodecomp = false, bool hdr = false);
|
int unpack(const char *image, bool skip_decomp = false, bool hdr = false);
|
||||||
void repack(const char* src_img, const char* out_img, bool nocomp = false);
|
void repack(const char* src_img, const char* out_img, bool skip_comp = false);
|
||||||
int split_image_dtb(const char *filename);
|
int split_image_dtb(const char *filename);
|
||||||
int hexpatch(const char *image, const char *from, const char *to);
|
int hexpatch(const char *image, const char *from, const char *to);
|
||||||
int cpio_commands(int argc, char *argv[]);
|
int cpio_commands(int argc, char *argv[]);
|
||||||
|
Loading…
Reference in New Issue
Block a user