Extract vbmeta from footer

Do not scan manually, extract properly from footer like libavb
This commit is contained in:
topjohnwu 2022-05-13 02:49:18 -07:00
parent 6ef86d8d20
commit c11ccbae2d
2 changed files with 25 additions and 19 deletions

View File

@ -423,16 +423,16 @@ void boot_img::parse_image(uint8_t *addr, format_t type) {
flags[LG_BUMP_FLAG] = true; flags[LG_BUMP_FLAG] = true;
} }
// Find AVB structures // Find AVB footer
void *meta = memmem(tail, tail_size, AVB_MAGIC, AVB_MAGIC_LEN); void *footer = tail + tail_size - sizeof(AvbFooter);
if (meta) { if (BUFFER_MATCH(footer, AVB_FOOTER_MAGIC)) {
// Double check if footer exists avb_footer = reinterpret_cast<AvbFooter*>(footer);
void *footer = tail + tail_size - sizeof(AvbFooter); // Double check if meta header exists
if (BUFFER_MATCH(footer, AVB_FOOTER_MAGIC)) { void *meta = hdr_addr + __builtin_bswap64(avb_footer->vbmeta_offset);
if (BUFFER_MATCH(meta, AVB_MAGIC)) {
fprintf(stderr, "VBMETA\n"); fprintf(stderr, "VBMETA\n");
flags[AVB_FLAG] = true; flags[AVB_FLAG] = true;
avb_meta = reinterpret_cast<AvbVBMetaImageHeader*>(meta); vbmeta = reinterpret_cast<AvbVBMetaImageHeader*>(meta);
avb_footer = reinterpret_cast<AvbFooter*>(footer);
} }
} }
} }
@ -466,9 +466,11 @@ int unpack(const char *image, bool skip_decomp, bool hdr) {
// Dump kernel // Dump kernel
if (!skip_decomp && COMPRESSED(boot.k_fmt)) { if (!skip_decomp && COMPRESSED(boot.k_fmt)) {
int fd = creat(KERNEL_FILE, 0644); if (boot.hdr->kernel_size() != 0) {
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size()); int fd = creat(KERNEL_FILE, 0644);
close(fd); decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
close(fd);
}
} else { } else {
dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE); dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE);
} }
@ -478,9 +480,11 @@ int unpack(const char *image, bool skip_decomp, bool hdr) {
// Dump ramdisk // Dump ramdisk
if (!skip_decomp && COMPRESSED(boot.r_fmt)) { if (!skip_decomp && COMPRESSED(boot.r_fmt)) {
int fd = creat(RAMDISK_FILE, 0644); if (boot.hdr->ramdisk_size() != 0) {
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size()); int fd = creat(RAMDISK_FILE, 0644);
close(fd); decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
close(fd);
}
} else { } else {
dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE); dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE);
} }
@ -490,9 +494,11 @@ int unpack(const char *image, bool skip_decomp, bool hdr) {
// Dump extra // Dump extra
if (!skip_decomp && COMPRESSED(boot.e_fmt)) { if (!skip_decomp && COMPRESSED(boot.e_fmt)) {
int fd = creat(EXTRA_FILE, 0644); if (boot.hdr->extra_size() != 0) {
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size()); int fd = creat(EXTRA_FILE, 0644);
close(fd); decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
close(fd);
}
} else { } else {
dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE); dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE);
} }
@ -682,7 +688,7 @@ void repack(const char *src_img, const char *out_img, bool skip_comp) {
file_align_with(4096); file_align_with(4096);
off.vbmeta = lseek(fd, 0, SEEK_CUR); off.vbmeta = lseek(fd, 0, SEEK_CUR);
uint64_t vbmeta_size = __builtin_bswap64(boot.avb_footer->vbmeta_size); uint64_t vbmeta_size = __builtin_bswap64(boot.avb_footer->vbmeta_size);
xwrite(fd, boot.avb_meta, vbmeta_size); xwrite(fd, boot.vbmeta, vbmeta_size);
} }
// Pad image to original size if not chromeos (as it requires post processing) // Pad image to original size if not chromeos (as it requires post processing)

View File

@ -608,7 +608,7 @@ struct boot_img {
// AVB structs // AVB structs
AvbFooter *avb_footer; AvbFooter *avb_footer;
AvbVBMetaImageHeader *avb_meta; AvbVBMetaImageHeader *vbmeta;
// Pointers to blocks defined in header // Pointers to blocks defined in header
uint8_t *hdr_addr; uint8_t *hdr_addr;