Some code reorganizing

This commit is contained in:
topjohnwu 2019-10-09 16:01:21 -04:00
parent f5d054b93c
commit 23c1a1dab8
3 changed files with 129 additions and 129 deletions

View File

@ -44,7 +44,7 @@ static void dump(void *buf, size_t size, const char *filename) {
close(fd); close(fd);
} }
static size_t restore(const char *filename, int fd) { static size_t restore(int fd, const char *filename) {
int ifd = xopen(filename, O_RDONLY); int ifd = xopen(filename, O_RDONLY);
size_t size = lseek(ifd, 0, SEEK_END); size_t size = lseek(ifd, 0, SEEK_END);
lseek(ifd, 0, SEEK_SET); lseek(ifd, 0, SEEK_SET);
@ -57,12 +57,102 @@ static void restore_buf(int fd, const void *buf, size_t size) {
xwrite(fd, buf, size); xwrite(fd, buf, size);
} }
boot_img::~boot_img() { void dyn_img_hdr::print() {
munmap(map_addr, map_size); uint32_t ver = header_version();
delete hdr; fprintf(stderr, "HEADER_VER [%u]\n", ver);
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 {
fprintf(stderr, "EXTRA_SZ [%u]\n", extra_size());
}
ver = 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;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
fprintf(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c);
y = (patch_level >> 4) + 2000;
m = patch_level & 0xf;
fprintf(stderr, "OS_PATCH_LEVEL [%d-%02d]\n", y, m);
}
fprintf(stderr, "PAGESIZE [%u]\n", page_size());
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]);
fprintf(stderr, "]\n");
} }
void boot_img::parse_file(const char *image) { 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());
uint32_t ver = 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;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
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);
}
void dyn_img_hdr::load_hdr_file() {
parse_prop_file(HEADER_FILE, [=](string_view key, string_view value) -> bool {
if (key == "page_size") {
page_size() = parse_int(value);
} else if (key == "name") {
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);
} else {
memcpy(cmdline(), value.data(), value.length());
}
} else if (key == "os_version") {
int patch_level = os_version() & 0x7ff;
int a, b, c;
sscanf(value.data(), "%d.%d.%d", &a, &b, &c);
os_version() = (((a << 14) | (b << 7) | c) << 11) | patch_level;
} else if (key == "os_patch_level") {
int os_ver = os_version() >> 11;
int y, m;
sscanf(value.data(), "%d-%d", &y, &m);
y -= 2000;
os_version() = (os_ver << 11) | (y << 4) | m;
}
return true;
});
}
boot_img::boot_img(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 *addr = map_addr; addr < map_addr + map_size; ++addr) { for (uint8_t *addr = map_addr; addr < map_addr + map_size; ++addr) {
@ -92,6 +182,11 @@ void boot_img::parse_file(const char *image) {
exit(1); exit(1);
} }
boot_img::~boot_img() {
munmap(map_addr, map_size);
delete hdr;
}
#define get_block(name) {\ #define get_block(name) {\
name = addr + off; \ name = addr + off; \
off += hdr->name##_size(); \ off += hdr->name##_size(); \
@ -128,7 +223,7 @@ void boot_img::parse_image(uint8_t *addr) {
img_start = addr; img_start = addr;
flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0; flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0;
print_hdr(); hdr->print();
size_t off = hdr->page_size(); size_t off = hdr->page_size();
@ -212,107 +307,11 @@ void boot_img::find_kernel_dtb() {
} }
} }
void boot_img::print_hdr() {
uint32_t ver = hdr->header_version();
fprintf(stderr, "HEADER_VER [%u]\n", ver);
fprintf(stderr, "KERNEL_SZ [%u]\n", hdr->kernel_size());
fprintf(stderr, "RAMDISK_SZ [%u]\n", hdr->ramdisk_size());
fprintf(stderr, "SECOND_SZ [%u]\n", hdr->second_size());
if (ver) {
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", hdr->recovery_dtbo_size());
fprintf(stderr, "DTB [%u]\n", hdr->dtb_size());
} else {
fprintf(stderr, "EXTRA_SZ [%u]\n", hdr->extra_size());
}
ver = 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;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
fprintf(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c);
y = (patch_level >> 4) + 2000;
m = patch_level & 0xf;
fprintf(stderr, "OS_PATCH_LEVEL [%d-%02d]\n", y, m);
}
fprintf(stderr, "PAGESIZE [%u]\n", hdr->page_size());
fprintf(stderr, "NAME [%s]\n", hdr->name());
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", hdr->cmdline(), hdr->extra_cmdline());
fprintf(stderr, "CHECKSUM [");
for (int i = 0; hdr->id()[i]; ++i)
fprintf(stderr, "%02x", hdr->id()[i]);
fprintf(stderr, "]\n");
}
static void dump_hdr_file(dyn_img_hdr *hdr) {
FILE *fp = xfopen(HEADER_FILE, "w");
fprintf(fp, "pagesize=%u\n", hdr->page_size());
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;
a = (version >> 14) & 0x7f;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
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);
}
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) { int unpack(const char *image, bool hdr) {
boot_img boot{}; boot_img boot(image);
boot.parse_file(image);
if (hdr) if (hdr)
dump_hdr_file(boot.hdr); boot.hdr->dump_hdr_file();
// Dump kernel // Dump kernel
if (COMPRESSED(boot.k_fmt)) { if (COMPRESSED(boot.k_fmt)) {
@ -353,8 +352,8 @@ int unpack(const char *image, 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* orig_image, const char* out_image, bool nocomp) { void repack(const char* src_img, const char* out_img, bool nocomp) {
boot_img boot{}; boot_img boot(src_img);
struct { struct {
uint32_t header; uint32_t header;
@ -365,10 +364,7 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
uint32_t dtb; uint32_t dtb;
} off; } off;
// Parse original image fprintf(stderr, "Repack to boot image: [%s]\n", out_img);
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;
@ -378,14 +374,14 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
boot.kernel_dt_size = 0; boot.kernel_dt_size = 0;
if (access(HEADER_FILE, R_OK) == 0) if (access(HEADER_FILE, R_OK) == 0)
load_hdr_file(boot.hdr); boot.hdr->load_hdr_file();
/***************** /*****************
* Writing blocks * Writing blocks
*****************/ *****************/
// Create new image // Create new image
int fd = creat(out_image, 0644); int fd = creat(out_img, 0644);
if (boot.flags & DHTB_FLAG) { if (boot.flags & DHTB_FLAG) {
// Skip DHTB header // Skip DHTB header
@ -422,7 +418,7 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
// kernel dtb // kernel dtb
if (access(KER_DTB_FILE, R_OK) == 0) if (access(KER_DTB_FILE, R_OK) == 0)
boot.hdr->kernel_size() += restore(KER_DTB_FILE, fd); boot.hdr->kernel_size() += restore(fd, KER_DTB_FILE);
file_align(); file_align();
// ramdisk // ramdisk
@ -447,28 +443,28 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
// second // second
off.second = 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(fd, SECOND_FILE);
file_align(); file_align();
} }
// extra // extra
off.extra = 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(fd, EXTRA_FILE);
file_align(); file_align();
} }
// recovery_dtbo // recovery_dtbo
if (access(RECV_DTBO_FILE, R_OK) == 0) { if (access(RECV_DTBO_FILE, R_OK) == 0) {
boot.hdr->recovery_dtbo_offset() = lseek(fd, 0, SEEK_CUR); boot.hdr->recovery_dtbo_offset() = lseek(fd, 0, SEEK_CUR);
boot.hdr->recovery_dtbo_size() = restore(RECV_DTBO_FILE, fd); boot.hdr->recovery_dtbo_size() = restore(fd, RECV_DTBO_FILE);
file_align(); file_align();
} }
// dtb // dtb
off.dtb = 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(fd, DTB_FILE);
file_align(); file_align();
} }
@ -488,7 +484,7 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
// 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_img, boot.map_addr, boot.map_size);
// MTK headers // MTK headers
if (boot.flags & MTK_KERNEL) { if (boot.flags & MTK_KERNEL) {
@ -536,7 +532,7 @@ void repack(const char* orig_image, const char* out_image, bool nocomp) {
(boot.flags & SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE); (boot.flags & SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE);
// Print new image info // Print new image info
boot.print_hdr(); 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, boot.hdr->hdr_size());

View File

@ -192,6 +192,10 @@ struct dyn_img_hdr {
return img_hdr; return img_hdr;
} }
void print();
void dump_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,
@ -298,11 +302,11 @@ struct boot_img {
uint8_t *map_addr; uint8_t *map_addr;
size_t map_size; size_t map_size;
// Headers // Android image header
dyn_img_hdr *hdr; /* Android image header */ dyn_img_hdr *hdr;
// Flags to indicate the state of current boot image // Flags to indicate the state of current boot image
uint16_t flags; uint16_t flags = 0;
// The format of kernel and ramdisk // The format of kernel and ramdisk
format_t k_fmt; format_t k_fmt;
@ -312,16 +316,17 @@ struct boot_img {
* Following pointers points within the mmap region * Following pointers points within the mmap region
***************************************************/ ***************************************************/
mtk_hdr *k_hdr; /* MTK kernel header */ // MTK headers
mtk_hdr *r_hdr; /* MTK ramdisk header */ mtk_hdr *k_hdr;
mtk_hdr *r_hdr;
// 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 = 0;
// Pointer to end of image // Pointer to end of image
uint8_t *tail; uint8_t *tail;
size_t tail_size; size_t tail_size = 0;
// Pointers to blocks defined in header // Pointers to blocks defined in header
uint8_t *img_start; uint8_t *img_start;
@ -332,10 +337,9 @@ struct boot_img {
uint8_t *recovery_dtbo; uint8_t *recovery_dtbo;
uint8_t *dtb; uint8_t *dtb;
boot_img(const char *);
~boot_img(); ~boot_img();
void parse_file(const char *);
void parse_image(uint8_t *addr); void parse_image(uint8_t *addr);
void find_kernel_dtb(); void find_kernel_dtb();
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 nocomp = false); void repack(const char* src_img, const char* out_img, 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[]);