Refactor boot image unpack/repack code base

This commit is contained in:
topjohnwu 2019-10-07 04:35:02 -04:00
parent e0927cd763
commit f87ee3fcf9
3 changed files with 238 additions and 237 deletions

View File

@ -60,50 +60,49 @@ static void restore_buf(int fd, const void *buf, size_t size) {
boot_img::~boot_img() { boot_img::~boot_img() {
munmap(map_addr, map_size); munmap(map_addr, map_size);
delete hdr; delete hdr;
delete k_hdr;
delete r_hdr;
delete b_hdr;
} }
#define UNSUPP_RET 1 void boot_img::parse_file(const char *image) {
#define CHROME_RET 2
int boot_img::parse_file(const char *image) {
mmap_ro(image, map_addr, map_size); mmap_ro(image, map_addr, map_size);
fprintf(stderr, "Parsing boot image: [%s]\n", image); fprintf(stderr, "Parsing boot image: [%s]\n", image);
for (uint8_t *head = map_addr; head < map_addr + map_size; ++head) { for (uint8_t *addr = map_addr; addr < map_addr + map_size; ++addr) {
switch (check_fmt(head, map_size)) { switch (check_fmt(addr, map_size)) {
case CHROMEOS: case CHROMEOS:
// The caller should know it's chromeos, as it needs additional signing // chromeos require external signing
flags |= CHROMEOS_FLAG; flags |= CHROMEOS_FLAG;
addr += 65535;
break; break;
case DHTB: case DHTB:
flags |= DHTB_FLAG; flags |= (DHTB_FLAG | SEANDROID_FLAG);
flags |= SEANDROID_FLAG;
fprintf(stderr, "DHTB_HDR\n"); fprintf(stderr, "DHTB_HDR\n");
addr += sizeof(dhtb_hdr) - 1;
break; break;
case BLOB: case BLOB:
flags |= BLOB_FLAG; flags |= BLOB_FLAG;
fprintf(stderr, "TEGRA_BLOB\n"); fprintf(stderr, "TEGRA_BLOB\n");
b_hdr = new blob_hdr(); addr += sizeof(blob_hdr) - 1;
memcpy(b_hdr, head, sizeof(blob_hdr));
head += sizeof(blob_hdr) - 1;
break; break;
case AOSP: case AOSP:
return parse_image(head); parse_image(addr);
return;
default: default:
break; break;
} }
} }
exit(UNSUPP_RET); exit(1);
} }
#define pos_align() pos = do_align(pos, hdr->page_size()) #define get_block(name) {\
int boot_img::parse_image(uint8_t *head) { name = addr + off; \
auto hp = reinterpret_cast<boot_img_hdr*>(head); off += hdr->name##_size(); \
off = do_align(off, hdr->page_size()); \
}
void boot_img::parse_image(uint8_t *addr) {
auto hp = reinterpret_cast<boot_img_hdr*>(addr);
if (hp->page_size >= 0x02000000) { if (hp->page_size >= 0x02000000) {
fprintf(stderr, "PXA_BOOT_HDR\n"); fprintf(stderr, "PXA_BOOT_HDR\n");
hdr = new dyn_img_pxa(head); hdr = new dyn_img_pxa(addr);
} else { } else {
if (memcmp(hp->cmdline, NOOKHD_RL_MAGIC, 10) == 0 || if (memcmp(hp->cmdline, NOOKHD_RL_MAGIC, 10) == 0 ||
memcmp(hp->cmdline, NOOKHD_GL_MAGIC, 12) == 0 || memcmp(hp->cmdline, NOOKHD_GL_MAGIC, 12) == 0 ||
@ -112,53 +111,36 @@ int boot_img::parse_image(uint8_t *head) {
memcmp(hp->cmdline, NOOKHD_ER_MAGIC, 30) == 0) { memcmp(hp->cmdline, NOOKHD_ER_MAGIC, 30) == 0) {
flags |= NOOKHD_FLAG; flags |= NOOKHD_FLAG;
fprintf(stderr, "NOOKHD_LOADER\n"); fprintf(stderr, "NOOKHD_LOADER\n");
head += NOOKHD_PRE_HEADER_SZ; addr += NOOKHD_PRE_HEADER_SZ;
} else if (memcmp(hp->name, ACCLAIM_MAGIC, 10) == 0) { } else if (memcmp(hp->name, ACCLAIM_MAGIC, 10) == 0) {
flags |= ACCLAIM_FLAG; flags |= ACCLAIM_FLAG;
fprintf(stderr, "ACCLAIM_LOADER\n"); fprintf(stderr, "ACCLAIM_LOADER\n");
head += ACCLAIM_PRE_HEADER_SZ; addr += ACCLAIM_PRE_HEADER_SZ;
} }
if (hp->header_version == 1) if (hp->header_version == 1)
hdr = new dyn_img_v1(head); hdr = new dyn_img_v1(addr);
else if (hp->header_version == 2) else if (hp->header_version == 2)
hdr = new dyn_img_v2(head); hdr = new dyn_img_v2(addr);
else else
hdr = new dyn_img_v0(head); hdr = new dyn_img_v0(addr);
} }
img_start = addr;
size_t pos = hdr->page_size();
flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0; flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0;
print_hdr(); print_hdr();
kernel = head + pos; size_t off = hdr->page_size();
pos += hdr->kernel_size();
pos_align();
ramdisk = head + pos; get_block(kernel);
pos += hdr->ramdisk_size(); get_block(ramdisk);
pos_align(); get_block(second);
get_block(extra);
get_block(recovery_dtbo);
get_block(dtb);
second = head + pos; if (addr + off < map_addr + map_size) {
pos += hdr->second_size(); tail = addr + off;
pos_align();
extra = head + pos;
pos += hdr->extra_size();
pos_align();
recov_dtbo = head + pos;
pos += hdr->recovery_dtbo_size();
pos_align();
dtb = head + pos;
pos += hdr->dtb_size();
pos_align();
if (head + pos < map_addr + map_size) {
tail = head + pos;
tail_size = map_size - (tail - map_addr); tail_size = map_size - (tail - map_addr);
} }
@ -169,7 +151,7 @@ int boot_img::parse_image(uint8_t *head) {
flags |= LG_BUMP_FLAG; flags |= LG_BUMP_FLAG;
} }
find_dtb(); find_kernel_dtb();
k_fmt = check_fmt(kernel, hdr->kernel_size()); k_fmt = check_fmt(kernel, hdr->kernel_size());
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size()); r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
@ -178,62 +160,48 @@ int boot_img::parse_image(uint8_t *head) {
if (k_fmt == MTK) { if (k_fmt == MTK) {
fprintf(stderr, "MTK_KERNEL_HDR\n"); fprintf(stderr, "MTK_KERNEL_HDR\n");
flags |= MTK_KERNEL; flags |= MTK_KERNEL;
k_hdr = new mtk_hdr(); k_hdr = reinterpret_cast<mtk_hdr *>(kernel);
memcpy(k_hdr, kernel, sizeof(mtk_hdr));
fprintf(stderr, "KERNEL [%u]\n", k_hdr->size); fprintf(stderr, "KERNEL [%u]\n", k_hdr->size);
fprintf(stderr, "NAME [%s]\n", k_hdr->name); fprintf(stderr, "NAME [%s]\n", k_hdr->name);
kernel += 512; kernel += sizeof(mtk_hdr);
hdr->kernel_size() -= 512; hdr->kernel_size() -= sizeof(mtk_hdr);
k_fmt = check_fmt(kernel, hdr->kernel_size()); k_fmt = check_fmt(kernel, hdr->kernel_size());
} }
if (r_fmt == MTK) { if (r_fmt == MTK) {
fprintf(stderr, "MTK_RAMDISK_HDR\n"); fprintf(stderr, "MTK_RAMDISK_HDR\n");
flags |= MTK_RAMDISK; flags |= MTK_RAMDISK;
r_hdr = new mtk_hdr(); r_hdr = reinterpret_cast<mtk_hdr *>(ramdisk);
memcpy(r_hdr, ramdisk, sizeof(mtk_hdr));
fprintf(stderr, "RAMDISK [%u]\n", r_hdr->size); fprintf(stderr, "RAMDISK [%u]\n", r_hdr->size);
fprintf(stderr, "NAME [%s]\n", r_hdr->name); fprintf(stderr, "NAME [%s]\n", r_hdr->name);
ramdisk += 512; ramdisk += sizeof(mtk_hdr);
hdr->ramdisk_size() -= 512; hdr->ramdisk_size() -= sizeof(mtk_hdr);
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size()); r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
} }
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]); fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]);
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]);
return (flags & CHROMEOS_FLAG) ? CHROME_RET : 0;
} }
void boot_img::find_dtb() { void boot_img::find_kernel_dtb() {
for (uint32_t i = 0; i < hdr->kernel_size(); ++i) { for (int i = 0; i < hdr->kernel_size() - 4; ++i) {
auto fdt_hdr = reinterpret_cast<fdt_header *>(kernel + i); auto fdt_hdr = reinterpret_cast<fdt_header *>(kernel + i);
if (fdt32_to_cpu(fdt_hdr->magic) != FDT_MAGIC) if (fdt32_to_cpu(fdt_hdr->magic) != FDT_MAGIC)
continue; continue;
// Check that fdt_header.totalsize does not overflow kernel image size // Check that fdt_header.totalsize does not overflow kernel image size
uint32_t totalsize = fdt32_to_cpu(fdt_hdr->totalsize); uint32_t totalsize = fdt32_to_cpu(fdt_hdr->totalsize);
if (totalsize > hdr->kernel_size() - i) { if (totalsize + i > hdr->kernel_size())
fprintf(stderr, "Invalid DTB detection at 0x%x: size (%u) > remaining (%u)\n",
i, totalsize, hdr->kernel_size() - i);
continue; continue;
}
// Check that fdt_header.off_dt_struct does not overflow kernel image size // Check that fdt_header.off_dt_struct does not overflow kernel image size
uint32_t off_dt_struct = fdt32_to_cpu(fdt_hdr->off_dt_struct); uint32_t off_dt_struct = fdt32_to_cpu(fdt_hdr->off_dt_struct);
if (off_dt_struct > hdr->kernel_size() - i) { if (off_dt_struct + i > hdr->kernel_size())
fprintf(stderr, "Invalid DTB detection at 0x%x: "
"struct offset (%u) > remaining (%u)\n",
i, off_dt_struct, hdr->kernel_size() - i);
continue; continue;
}
// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE // Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
auto fdt_node_hdr = reinterpret_cast<fdt_node_header *>(kernel + i + off_dt_struct); auto fdt_node_hdr = reinterpret_cast<fdt_node_header *>(kernel + i + off_dt_struct);
if (fdt32_to_cpu(fdt_node_hdr->tag) != FDT_BEGIN_NODE) { if (fdt32_to_cpu(fdt_node_hdr->tag) != FDT_BEGIN_NODE)
fprintf(stderr, "Invalid DTB detection at 0x%x: "
"header tag of first node != FDT_BEGIN_NODE\n", i);
continue; continue;
}
kernel_dtb = kernel + i; kernel_dtb = kernel + i;
kernel_dt_size = hdr->kernel_size() - i; kernel_dt_size = hdr->kernel_size() - i;
@ -282,55 +250,87 @@ void boot_img::print_hdr() {
fprintf(stderr, "]\n"); fprintf(stderr, "]\n");
} }
int unpack(const char *image, bool hdr) { static void dump_hdr_file(dyn_img_hdr *hdr) {
boot_img boot {}; FILE *fp = xfopen(HEADER_FILE, "w");
int ret = boot.parse_file(image); fprintf(fp, "pagesize=%u\n", hdr->page_size());
int fd; fprintf(fp, "name=%s\n", hdr->name());
fprintf(fp, "cmdline=%.512s%.1024s\n", hdr->cmdline(), hdr->extra_cmdline());
uint32_t ver = hdr->os_version();
if (ver) {
int a, b, c, y, m = 0;
int version, patch_level;
version = ver >> 11;
patch_level = ver & 0x7ff;
if (hdr) { a = (version >> 14) & 0x7f;
FILE *fp = xfopen(HEADER_FILE, "w"); b = (version >> 7) & 0x7f;
fprintf(fp, "pagesize=%u\n", boot.hdr->page_size()); c = version & 0x7f;
fprintf(fp, "name=%s\n", boot.hdr->name()); fprintf(fp, "os_version=%d.%d.%d\n", a, b, c);
fprintf(fp, "cmdline=%.512s%.1024s\n", boot.hdr->cmdline(), boot.hdr->extra_cmdline());
uint32_t ver = boot.hdr->os_version();
if (ver) {
int a, b, c, y, m = 0;
int version, patch_level;
version = ver >> 11;
patch_level = ver & 0x7ff;
a = (version >> 14) & 0x7f; y = (patch_level >> 4) + 2000;
b = (version >> 7) & 0x7f; m = patch_level & 0xf;
c = version & 0x7f; fprintf(fp, "os_patch_level=%d-%02d\n", y, m);
fprintf(fp, "os_version=%d.%d.%d\n", a, b, c);
y = (patch_level >> 4) + 2000;
m = patch_level & 0xf;
fprintf(fp, "os_patch_level=%d-%02d\n", y, m);
}
fclose(fp);
} }
fclose(fp);
}
static void load_hdr_file(dyn_img_hdr *hdr) {
parse_prop_file(HEADER_FILE, [=](string_view key, string_view value) -> bool {
if (key == "page_size") {
hdr->page_size() = parse_int(value);
} else if (key == "name") {
memset(hdr->name(), 0, 16);
memcpy(hdr->name(), value.data(), value.length() > 15 ? 15 : value.length());
} else if (key == "cmdline") {
memset(hdr->cmdline(), 0, 512);
memset(hdr->extra_cmdline(), 0, 1024);
if (value.length() > 512) {
memcpy(hdr->cmdline(), value.data(), 512);
memcpy(hdr->extra_cmdline(), &value[512], value.length() - 511);
} else {
memcpy(hdr->cmdline(), value.data(), value.length());
}
} else if (key == "os_version") {
int patch_level = hdr->os_version() & 0x7ff;
int a, b, c;
sscanf(value.data(), "%d.%d.%d", &a, &b, &c);
hdr->os_version() = (((a << 14) | (b << 7) | c) << 11) | patch_level;
} else if (key == "os_patch_level") {
int os_version = hdr->os_version() >> 11;
int y, m;
sscanf(value.data(), "%d-%d", &y, &m);
y -= 2000;
hdr->os_version() = (os_version << 11) | (y << 4) | m;
}
return true;
});
}
int unpack(const char *image, bool hdr) {
boot_img boot{};
boot.parse_file(image);
if (hdr)
dump_hdr_file(boot.hdr);
// Dump kernel // Dump kernel
if (COMPRESSED(boot.k_fmt)) { if (COMPRESSED(boot.k_fmt)) {
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);
} else { } else {
fprintf(stderr, "Kernel is uncompressed or not a supported compressed type!\n");
dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE); dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE);
} }
// Dump dtb // Dump kernel_dtb
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 (COMPRESSED(boot.r_fmt)) { if (COMPRESSED(boot.r_fmt)) {
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);
} else { } else {
fprintf(stderr, "Ramdisk is uncompressed or not a supported compressed type!\n");
dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE); dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE);
} }
@ -341,24 +341,34 @@ int unpack(const char *image, bool hdr) {
dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE); dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE);
// Dump recovery_dtbo // Dump recovery_dtbo
dump(boot.recov_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE); dump(boot.recovery_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE);
// Dump dtb // Dump dtb
dump(boot.dtb, boot.hdr->dtb_size(), DTB_FILE); dump(boot.dtb, boot.hdr->dtb_size(), DTB_FILE);
return ret;
return (boot.flags & CHROMEOS_FLAG) ? 2 : 0;
} }
#define file_align() \ #define file_align() \
write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - header_off, boot.hdr->page_size())) write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - off.header, boot.hdr->page_size()))
void repack(const char* orig_image, const char* out_image, bool force_nocomp) { void repack(const char* orig_image, const char* out_image, bool nocomp) {
boot_img boot {}; boot_img boot{};
off_t header_off, kernel_off, ramdisk_off, second_off, extra_off, dtb_off; struct {
uint32_t header;
uint32_t kernel;
uint32_t ramdisk;
uint32_t second;
uint32_t extra;
uint32_t dtb;
} off;
// Parse original image // Parse original image
boot.parse_file(orig_image); boot.parse_file(orig_image);
fprintf(stderr, "Repack to boot image: [%s]\n", out_image);
// Reset sizes // Reset sizes
boot.hdr->kernel_size() = 0; boot.hdr->kernel_size() = 0;
boot.hdr->ramdisk_size() = 0; boot.hdr->ramdisk_size() = 0;
@ -366,65 +376,36 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
boot.hdr->dtb_size() = 0; boot.hdr->dtb_size() = 0;
boot.kernel_dt_size = 0; boot.kernel_dt_size = 0;
fprintf(stderr, "Repack to boot image: [%s]\n", out_image); if (access(HEADER_FILE, R_OK) == 0)
load_hdr_file(boot.hdr);
/*****************
* Writing blocks
*****************/
// Create new image // Create new image
int fd = creat(out_image, 0644); int fd = creat(out_image, 0644);
if (boot.flags & DHTB_FLAG) { if (boot.flags & DHTB_FLAG) {
// Skip DHTB header // Skip DHTB header
write_zero(fd, 512); write_zero(fd, sizeof(dhtb_hdr));
} else if (boot.flags & BLOB_FLAG) { } else if (boot.flags & BLOB_FLAG) {
// Skip blob header restore_buf(fd, boot.map_addr, sizeof(blob_hdr));
write_zero(fd, sizeof(blob_hdr));
} else if (boot.flags & NOOKHD_FLAG) { } else if (boot.flags & 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 (boot.flags & ACCLAIM_FLAG) {
restore_buf(fd, boot.map_addr, ACCLAIM_PRE_HEADER_SZ); restore_buf(fd, boot.map_addr, ACCLAIM_PRE_HEADER_SZ);
} }
// header // Copy a page for header
if (access(HEADER_FILE, R_OK) == 0) { off.header = lseek(fd, 0, SEEK_CUR);
parse_prop_file(HEADER_FILE, [&](string_view key, string_view value) -> bool { restore_buf(fd, boot.img_start, boot.hdr->page_size());
if (key == "page_size") {
boot.hdr->page_size() = parse_int(value);
} else if (key == "name") {
memset(boot.hdr->name(), 0, 16);
memcpy(boot.hdr->name(), value.data(), value.length() > 15 ? 15 : value.length());
} else if (key == "cmdline") {
memset(boot.hdr->cmdline(), 0, 512);
memset(boot.hdr->extra_cmdline(), 0, 1024);
if (value.length() > 512) {
memcpy(boot.hdr->cmdline(), value.data(), 512);
memcpy(boot.hdr->extra_cmdline(), &value[512], value.length() - 511);
} else {
memcpy(boot.hdr->cmdline(), value.data(), value.length());
}
} else if (key == "os_version") {
int patch_level = boot.hdr->os_version() & 0x7ff;
int a, b, c;
sscanf(value.data(), "%d.%d.%d", &a, &b, &c);
boot.hdr->os_version() = (((a << 14) | (b << 7) | c) << 11) | patch_level;
} else if (key == "os_patch_level") {
int os_version = boot.hdr->os_version() >> 11;
int y, m;
sscanf(value.data(), "%d-%d", &y, &m);
y -= 2000;
boot.hdr->os_version() = (os_version << 11) | (y << 4) | m;
}
return true;
});
}
// Skip a page for header
header_off = lseek(fd, 0, SEEK_CUR);
write_zero(fd, boot.hdr->page_size());
// kernel // kernel
kernel_off = lseek(fd, 0, SEEK_CUR); off.kernel = lseek(fd, 0, SEEK_CUR);
if (boot.flags & MTK_KERNEL) { if (boot.flags & MTK_KERNEL) {
// Skip MTK header // Copy MTK headers
write_zero(fd, 512); restore_buf(fd, boot.k_hdr, sizeof(mtk_hdr));
} }
if (access(KERNEL_FILE, R_OK) == 0) { if (access(KERNEL_FILE, R_OK) == 0) {
size_t raw_size; size_t raw_size;
@ -433,7 +414,7 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) { if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) {
boot.hdr->kernel_size() = compress(boot.k_fmt, fd, raw_buf, raw_size); boot.hdr->kernel_size() = compress(boot.k_fmt, fd, raw_buf, raw_size);
} else { } else {
boot.hdr->kernel_size() = write(fd, raw_buf, raw_size); boot.hdr->kernel_size() = xwrite(fd, raw_buf, raw_size);
} }
munmap(raw_buf, raw_size); munmap(raw_buf, raw_size);
} }
@ -444,33 +425,33 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
file_align(); file_align();
// ramdisk // ramdisk
ramdisk_off = lseek(fd, 0, SEEK_CUR); off.ramdisk = lseek(fd, 0, SEEK_CUR);
if (boot.flags & MTK_RAMDISK) { if (boot.flags & MTK_RAMDISK) {
// Skip MTK header // Copy MTK headers
write_zero(fd, 512); restore_buf(fd, boot.r_hdr, sizeof(mtk_hdr));
} }
if (access(RAMDISK_FILE, R_OK) == 0) { if (access(RAMDISK_FILE, R_OK) == 0) {
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 (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt) && !force_nocomp) { if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt) && !nocomp) {
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() = write(fd, raw_buf, raw_size); boot.hdr->ramdisk_size() = xwrite(fd, raw_buf, raw_size);
} }
munmap(raw_buf, raw_size); munmap(raw_buf, raw_size);
file_align(); file_align();
} }
// second // second
second_off = lseek(fd, 0, SEEK_CUR); off.second = lseek(fd, 0, SEEK_CUR);
if (access(SECOND_FILE, R_OK) == 0) { if (access(SECOND_FILE, R_OK) == 0) {
boot.hdr->second_size() = restore(SECOND_FILE, fd); boot.hdr->second_size() = restore(SECOND_FILE, fd);
file_align(); file_align();
} }
// extra // extra
extra_off = lseek(fd, 0, SEEK_CUR); off.extra = lseek(fd, 0, SEEK_CUR);
if (access(EXTRA_FILE, R_OK) == 0) { if (access(EXTRA_FILE, R_OK) == 0) {
boot.hdr->extra_size() = restore(EXTRA_FILE, fd); boot.hdr->extra_size() = restore(EXTRA_FILE, fd);
file_align(); file_align();
@ -484,7 +465,7 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
} }
// dtb // dtb
dtb_off = lseek(fd, 0, SEEK_CUR); off.dtb = lseek(fd, 0, SEEK_CUR);
if (access(DTB_FILE, R_OK) == 0) { if (access(DTB_FILE, R_OK) == 0) {
boot.hdr->dtb_size() = restore(DTB_FILE, fd); boot.hdr->dtb_size() = restore(DTB_FILE, fd);
file_align(); file_align();
@ -500,37 +481,41 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
close(fd); close(fd);
/*********************
* Patching the image
*********************/
// Map output image as rw // Map output image as rw
munmap(boot.map_addr, boot.map_size); munmap(boot.map_addr, boot.map_size);
mmap_rw(out_image, boot.map_addr, boot.map_size); mmap_rw(out_image, boot.map_addr, boot.map_size);
// MTK headers // MTK headers
if (boot.flags & MTK_KERNEL) { if (boot.flags & MTK_KERNEL) {
boot.k_hdr->size = boot.hdr->kernel_size(); auto hdr = reinterpret_cast<mtk_hdr *>(boot.map_addr + off.kernel);
boot.hdr->kernel_size() += 512; hdr->size = boot.hdr->kernel_size();
memcpy(boot.map_addr + kernel_off, boot.k_hdr, sizeof(mtk_hdr)); boot.hdr->kernel_size() += sizeof(*hdr);
} }
if (boot.flags & MTK_RAMDISK) { if (boot.flags & MTK_RAMDISK) {
boot.r_hdr->size = boot.hdr->ramdisk_size(); auto hdr = reinterpret_cast<mtk_hdr *>(boot.map_addr + off.ramdisk);
boot.hdr->ramdisk_size() += 512; hdr->size = boot.hdr->ramdisk_size();
memcpy(boot.map_addr + ramdisk_off, boot.r_hdr, sizeof(mtk_hdr)); boot.hdr->ramdisk_size() += sizeof(*hdr);
} }
// Update checksum // Update checksum
HASH_CTX ctx; HASH_CTX ctx;
(boot.flags & SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx); (boot.flags & SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx);
uint32_t size = boot.hdr->kernel_size(); uint32_t size = boot.hdr->kernel_size();
HASH_update(&ctx, boot.map_addr + kernel_off, size); HASH_update(&ctx, boot.map_addr + off.kernel, size);
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
size = boot.hdr->ramdisk_size(); size = boot.hdr->ramdisk_size();
HASH_update(&ctx, boot.map_addr + ramdisk_off, size); HASH_update(&ctx, boot.map_addr + off.ramdisk, size);
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
size = boot.hdr->second_size(); size = boot.hdr->second_size();
HASH_update(&ctx, boot.map_addr + second_off, size); HASH_update(&ctx, boot.map_addr + off.second, size);
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
size = boot.hdr->extra_size(); size = boot.hdr->extra_size();
if (size) { if (size) {
HASH_update(&ctx, boot.map_addr + extra_off, size); HASH_update(&ctx, boot.map_addr + off.extra, size);
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
} }
if (boot.hdr->header_version()) { if (boot.hdr->header_version()) {
@ -539,9 +524,11 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
size = boot.hdr->dtb_size(); size = boot.hdr->dtb_size();
if (size) { if (size) {
HASH_update(&ctx, boot.map_addr + dtb_off, size); HASH_update(&ctx, boot.map_addr + off.dtb, size);
HASH_update(&ctx, &size, sizeof(size)); HASH_update(&ctx, &size, sizeof(size));
} }
boot.hdr->header_size() = boot.hdr->hdr_size();
} }
memset(boot.hdr->id(), 0, 32); memset(boot.hdr->id(), 0, 32);
memcpy(boot.hdr->id(), HASH_final(&ctx), memcpy(boot.hdr->id(), HASH_final(&ctx),
@ -550,22 +537,18 @@ void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
// Print new image info // Print new image info
boot.print_hdr(); boot.print_hdr();
// Try to fix the header
if (boot.hdr->header_version() && boot.hdr->header_size() == 0)
boot.hdr->header_size() = sizeof(boot_img_hdr);
// Main header // Main header
memcpy(boot.map_addr + header_off, **boot.hdr, boot.hdr->hdr_size()); memcpy(boot.map_addr + off.header, **boot.hdr, boot.hdr->hdr_size());
if (boot.flags & DHTB_FLAG) { if (boot.flags & DHTB_FLAG) {
// DHTB header // DHTB header
dhtb_hdr *hdr = reinterpret_cast<dhtb_hdr *>(boot.map_addr); auto hdr = reinterpret_cast<dhtb_hdr *>(boot.map_addr);
memcpy(hdr, DHTB_MAGIC, 8); memcpy(hdr, DHTB_MAGIC, 8);
hdr->size = boot.map_size - 512; hdr->size = boot.map_size - sizeof(dhtb_hdr);
SHA256_hash(boot.map_addr + 512, hdr->size, hdr->checksum); SHA256_hash(boot.map_addr + sizeof(dhtb_hdr), hdr->size, hdr->checksum);
} else if (boot.flags & BLOB_FLAG) { } else if (boot.flags & BLOB_FLAG) {
// Blob headers // Blob header
boot.b_hdr->size = boot.map_size - sizeof(blob_hdr); auto hdr = reinterpret_cast<blob_hdr *>(boot.map_addr);
memcpy(boot.map_addr, boot.b_hdr, sizeof(blob_hdr)); hdr->size = boot.map_size - sizeof(blob_hdr);
} }
} }

View File

@ -4,6 +4,47 @@
#include <utility> #include <utility>
#include "format.h" #include "format.h"
/****************
* Other Headers
****************/
struct mtk_hdr {
uint32_t magic; /* MTK magic */
uint32_t size; /* Size of the content */
char name[32]; /* The type of the header */
char padding[472]; /* Padding to 512 bytes */
} __attribute__((packed));
struct dhtb_hdr {
char magic[8]; /* DHTB magic */
uint8_t checksum[40]; /* Payload SHA256, whole image + SEANDROIDENFORCE + 0xFFFFFFFF */
uint32_t size; /* Payload size, whole image + SEANDROIDENFORCE + 0xFFFFFFFF */
char padding[460]; /* Padding to 512 bytes */
} __attribute__((packed));
struct blob_hdr {
char secure_magic[20]; /* "-SIGNED-BY-SIGNBLOB-" */
uint32_t datalen; /* 0x00000000 */
uint32_t signature; /* 0x00000000 */
char magic[16]; /* "MSM-RADIO-UPDATE" */
uint32_t hdr_version; /* 0x00010000 */
uint32_t hdr_size; /* Size of header */
uint32_t part_offset; /* Same as size */
uint32_t num_parts; /* Number of partitions */
uint32_t unknown[7]; /* All 0x00000000 */
char name[4]; /* Name of partition */
uint32_t offset; /* offset in blob where this partition starts */
uint32_t size; /* Size of data */
uint32_t version; /* 0x00000001 */
} __attribute__((packed));
/*********************
* Boot Image Headers
*********************/
struct boot_img_hdr_base { struct boot_img_hdr_base {
char magic[8]; char magic[8];
@ -57,12 +98,12 @@ struct boot_img_hdr_v2 : public boot_img_hdr_v1 {
} __attribute__((packed)); } __attribute__((packed));
// Default to hdr v2 // Default to hdr v2
typedef boot_img_hdr_v2 boot_img_hdr; 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_base {
uint32_t extra_size; /* extra blob size in bytes */ uint32_t extra_size; /* extra blob size in bytes */
uint32_t unknown; /* unknown value */ 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 */
@ -111,34 +152,6 @@ struct boot_img_hdr_pxa : public boot_img_hdr_base {
** else: jump to kernel_addr ** else: jump to kernel_addr
*/ */
struct mtk_hdr {
uint32_t magic; /* MTK magic */
uint32_t size; /* Size of the content */
char name[32]; /* The type of the header */
} __attribute__((packed));
struct dhtb_hdr {
char magic[8]; /* DHTB magic */
uint8_t checksum[40]; /* Payload SHA256, whole image + SEANDROIDENFORCE + 0xFFFFFFFF */
uint32_t size; /* Payload size, whole image + SEANDROIDENFORCE + 0xFFFFFFFF */
} __attribute__((packed));
struct blob_hdr {
char secure_magic[20]; /* "-SIGNED-BY-SIGNBLOB-" */
uint32_t datalen; /* 0x00000000 */
uint32_t signature; /* 0x00000000 */
char magic[16]; /* "MSM-RADIO-UPDATE" */
uint32_t hdr_version; /* 0x00010000 */
uint32_t hdr_size; /* Size of header */
uint32_t part_offset; /* Same as size */
uint32_t num_parts; /* Number of partitions */
uint32_t unknown[7]; /* All 0x00000000 */
char name[4]; /* Name of partition */
uint32_t offset; /* offset in blob where this partition starts */
uint32_t size; /* Size of data */
uint32_t version; /* 0x00000001 */
} __attribute__((packed));
#define drct_var(name) \ #define drct_var(name) \
auto &name() { return img_hdr->name; } auto &name() { return img_hdr->name; }
#define decl_var(name, len) \ #define decl_var(name, len) \
@ -269,16 +282,16 @@ struct dyn_img_v2 : public dyn_img_v1 {
#undef impl_val #undef impl_val
// Flags // Flags
#define MTK_KERNEL 1 << 1 #define MTK_KERNEL (1 << 0)
#define MTK_RAMDISK 1 << 2 #define MTK_RAMDISK (1 << 1)
#define CHROMEOS_FLAG 1 << 3 #define CHROMEOS_FLAG (1 << 2)
#define DHTB_FLAG 1 << 4 #define DHTB_FLAG (1 << 3)
#define SEANDROID_FLAG 1 << 5 #define SEANDROID_FLAG (1 << 4)
#define LG_BUMP_FLAG 1 << 6 #define LG_BUMP_FLAG (1 << 5)
#define SHA256_FLAG 1 << 7 #define SHA256_FLAG (1 << 6)
#define BLOB_FLAG 1 << 8 #define BLOB_FLAG (1 << 7)
#define NOOKHD_FLAG 1 << 9 #define NOOKHD_FLAG (1 << 8)
#define ACCLAIM_FLAG 1 << 10 #define ACCLAIM_FLAG (1 << 9)
struct boot_img { struct boot_img {
// Memory map of the whole image // Memory map of the whole image
@ -287,9 +300,6 @@ struct boot_img {
// Headers // Headers
dyn_img_hdr *hdr; /* Android image header */ dyn_img_hdr *hdr; /* Android image header */
mtk_hdr *k_hdr; /* MTK kernel header */
mtk_hdr *r_hdr; /* MTK ramdisk header */
blob_hdr *b_hdr; /* Tegra blob header */
// Flags to indicate the state of current boot image // Flags to indicate the state of current boot image
uint16_t flags; uint16_t flags;
@ -298,6 +308,13 @@ struct boot_img {
format_t k_fmt; format_t k_fmt;
format_t r_fmt; format_t r_fmt;
/***************************************************
* Following pointers points within the mmap region
***************************************************/
mtk_hdr *k_hdr; /* MTK kernel header */
mtk_hdr *r_hdr; /* MTK ramdisk header */
// Pointer to dtb that is appended after kernel // Pointer to dtb that is appended after kernel
uint8_t *kernel_dtb; uint8_t *kernel_dtb;
uint32_t kernel_dt_size; uint32_t kernel_dt_size;
@ -307,17 +324,18 @@ struct boot_img {
size_t tail_size; size_t tail_size;
// Pointers to blocks defined in header // Pointers to blocks defined in header
uint8_t *img_start;
uint8_t *kernel; uint8_t *kernel;
uint8_t *ramdisk; uint8_t *ramdisk;
uint8_t *second; uint8_t *second;
uint8_t *extra; uint8_t *extra;
uint8_t *recov_dtbo; uint8_t *recovery_dtbo;
uint8_t *dtb; uint8_t *dtb;
~boot_img(); ~boot_img();
int parse_file(const char *); void parse_file(const char *);
int parse_image(uint8_t *); void parse_image(uint8_t *addr);
void find_dtb(); void find_kernel_dtb();
void print_hdr(); void print_hdr();
}; };

View File

@ -13,7 +13,7 @@
#define NEW_BOOT "new-boot.img" #define NEW_BOOT "new-boot.img"
int unpack(const char *image, bool hdr = false); int unpack(const char *image, bool hdr = false);
void repack(const char* orig_image, const char* out_image, bool force_nocomp = false); void repack(const char* orig_image, const char* out_image, bool nocomp = false);
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[]);
int dtb_commands(int argc, char *argv[]); int dtb_commands(int argc, char *argv[]);