From e50295d33761b7c9cf486f0d112bf6d041265d3d Mon Sep 17 00:00:00 2001 From: osm0sis Date: Mon, 25 May 2020 01:09:36 -0300 Subject: [PATCH] magiskboot: add support for lz4 compressed dt (extra) - legacy devices brought up to Android 10 may now use a compressed dt in a hdr_v0 AOSP dt variant extra section, so detect, decompress and recompress this - so far these have only been done using lz4 compression (latest format revision magic), e.g. LOS 17.1 victara (Moto X) --- native/jni/magiskboot/bootimg.cpp | 20 ++++++++++++++++++-- native/jni/magiskboot/bootimg.hpp | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 0571d4904..0a088e04b 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -255,6 +255,7 @@ void boot_img::parse_image(uint8_t *addr) { k_fmt = check_fmt(kernel, hdr->kernel_size()); r_fmt = check_fmt(ramdisk, hdr->ramdisk_size()); + e_fmt = check_fmt(extra, hdr->extra_size()); // Check MTK if (k_fmt == MTK) { @@ -280,6 +281,7 @@ void boot_img::parse_image(uint8_t *addr) { fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]); + fprintf(stderr, "EXTRA_FMT [%s]\n", fmt2name[e_fmt]); } static int find_dtb_offset(uint8_t *buf, int sz) { @@ -371,7 +373,13 @@ int unpack(const char *image, bool nodecomp, bool hdr) { dump(boot.second, boot.hdr->second_size(), SECOND_FILE); // Dump extra - dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE); + if (!nodecomp && COMPRESSED(boot.e_fmt)) { + int fd = creat(EXTRA_FILE, 0644); + decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size()); + close(fd); + } else { + dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE); + } // Dump recovery_dtbo dump(boot.recovery_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE); @@ -483,7 +491,15 @@ void repack(const char* src_img, const char* out_img, bool nocomp) { // extra off.extra = lseek(fd, 0, SEEK_CUR); if (access(EXTRA_FILE, R_OK) == 0) { - boot.hdr->extra_size() = restore(fd, EXTRA_FILE); + size_t raw_size; + void *raw_buf; + mmap_ro(EXTRA_FILE, raw_buf, raw_size); + if (!nocomp && !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); + } else { + boot.hdr->extra_size() = xwrite(fd, raw_buf, raw_size); + } + munmap(raw_buf, raw_size); file_align(); } diff --git a/native/jni/magiskboot/bootimg.hpp b/native/jni/magiskboot/bootimg.hpp index b37e5d2ac..94fb86c2f 100644 --- a/native/jni/magiskboot/bootimg.hpp +++ b/native/jni/magiskboot/bootimg.hpp @@ -303,9 +303,10 @@ struct boot_img { // Flags to indicate the state of current boot image uint16_t flags = 0; - // The format of kernel and ramdisk + // The format of kernel, ramdisk and extra format_t k_fmt; format_t r_fmt; + format_t e_fmt; /*************************************************** * Following pointers points within the mmap region