From ee407472cfce2bbf2b671876e059f7ef8620e3bb Mon Sep 17 00:00:00 2001 From: osm0sis Date: Fri, 7 Jun 2019 15:56:20 -0300 Subject: [PATCH] magiskboot: allow forcing no recompression on ramdisk.cpio - when input image had a compressed ramdisk magiskboot had no way to force the repack with the uncompressed ramdisk.cpio since it does not formally recognize cpio as its own format, so add a switch to support forcing repacking to any possible ramdisk format regardless of input image --- native/jni/magiskboot/bootimg.cpp | 4 ++-- native/jni/magiskboot/magiskboot.h | 2 +- native/jni/magiskboot/main.cpp | 15 +++++++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 73e971069..7c598dc93 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -340,7 +340,7 @@ int unpack(const char *image, bool hdr) { } #define file_align() write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - header_off, boot.hdr.page_size())) -void repack(const char* orig_image, const char* out_image) { +void repack(const char* orig_image, const char* out_image, bool force_nocomp) { boot_img boot {}; off_t header_off, kernel_off, ramdisk_off, second_off, extra_off, dtb_off; @@ -442,7 +442,7 @@ void repack(const char* orig_image, const char* out_image) { size_t raw_size; void *raw_buf; mmap_ro(RAMDISK_FILE, raw_buf, raw_size); - if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) { + if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt) && !force_nocomp) { boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, raw_buf, raw_size); } else { boot.hdr->ramdisk_size = write(fd, raw_buf, raw_size); diff --git a/native/jni/magiskboot/magiskboot.h b/native/jni/magiskboot/magiskboot.h index 98acaf815..6621b94f1 100644 --- a/native/jni/magiskboot/magiskboot.h +++ b/native/jni/magiskboot/magiskboot.h @@ -14,7 +14,7 @@ // Main entries int unpack(const char *image, bool hdr = false); -void repack(const char* orig_image, const char* out_image); +void repack(const char* orig_image, const char* out_image, bool force_nocomp = false); int hexpatch(const char *image, const char *from, const char *to); int cpio_commands(int argc, char *argv[]); int dtb_commands(const char *cmd, int argc, char *argv[]); diff --git a/native/jni/magiskboot/main.cpp b/native/jni/magiskboot/main.cpp index bc32814c1..c082b7e69 100644 --- a/native/jni/magiskboot/main.cpp +++ b/native/jni/magiskboot/main.cpp @@ -26,11 +26,12 @@ static void usage(char *arg0) { " Return values:\n" " 0:valid 1:error 2:chromeos\n" "\n" - " repack [outbootimg]\n" + " repack [-n] [outbootimg]\n" " Repack boot image components from current directory\n" " to [outbootimg], or new-boot.img if not specified.\n" - " It will compress ramdisk.cpio and kernel with the same method in\n" - " if the file provided is not already compressed.\n" + " If '-n' is provided, it will not attempt to recompress ramdisk.cpio,\n" + " otherwise it will compress ramdisk.cpio and kernel with the same method\n" + " in if the file provided is not already compressed.\n" "\n" " hexpatch \n" " Search in , and replace with \n" @@ -145,7 +146,13 @@ int main(int argc, char *argv[]) { return unpack(argv[2]); } } else if (argc > 2 && strcmp(argv[1], "repack") == 0) { - repack(argv[2], argv[3] ? argv[3] : NEW_BOOT); + if (strcmp(argv[2], "-n") == 0) { + if (argc == 4) + usage(argv[0]); + repack(argv[3], argv[4] ? argv[4] : NEW_BOOT, true); + } else { + repack(argv[2], argv[3] ? argv[3] : NEW_BOOT); + } } else if (argc > 2 && strcmp(argv[1], "decompress") == 0) { decompress(argv[2], argv[3]); } else if (argc > 2 && strncmp(argv[1], "compress", 8) == 0) {