From 0442d6d509b1b05e726660d26aaf63f100a99f33 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 7 Mar 2019 07:24:06 -0500 Subject: [PATCH] Only compress kernel and ramdisk if input not compressed --- native/jni/magiskboot/bootimg.cpp | 50 +++++++++++++++++++++--------- native/jni/magiskboot/compress.cpp | 15 --------- native/jni/magiskboot/compress.h | 3 -- native/jni/magiskboot/format.h | 8 +---- native/jni/magiskboot/main.cpp | 34 +++++++++----------- 5 files changed, 50 insertions(+), 60 deletions(-) diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 710ced1ee..e38e04c6a 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -1,8 +1,11 @@ +#include +#include #include #include #include #include -#include +#include +#include #include #include @@ -13,9 +16,26 @@ #include "magiskboot.h" #include "compress.h" +using namespace std; + uint32_t dyn_img_hdr::j32 = 0; uint64_t dyn_img_hdr::j64 = 0; +static int64_t one_step(unique_ptr &&ptr, int fd, const void *in, size_t size) { + ptr->set_out(make_unique(fd)); + if (!ptr->write(in, size)) + return -1; + return ptr->finalize(); +} + +static int64_t decompress(format_t type, int fd, const void *in, size_t size) { + return one_step(unique_ptr(get_decoder(type)), fd, in, size); +} + +static int64_t compress(format_t type, int fd, const void *in, size_t size) { + return one_step(unique_ptr(get_encoder(type)), fd, in, size); +} + static void dump(void *buf, size_t size, const char *filename) { if (size == 0) return; @@ -329,15 +349,15 @@ void repack(const char* orig_image, const char* out_image) { write_zero(fd, 512); } if (access(KERNEL_FILE, R_OK) == 0) { - if (COMPRESSED(boot.k_fmt)) { - size_t raw_size; - void *kernel_raw; - mmap_ro(KERNEL_FILE, kernel_raw, raw_size); - boot.hdr->kernel_size = compress(boot.k_fmt, fd, kernel_raw, raw_size); - munmap(kernel_raw, raw_size); + size_t raw_size; + void *raw_buf; + mmap_ro(KERNEL_FILE, raw_buf, raw_size); + if (!COMPRESSED(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) { + boot.hdr->kernel_size = compress(boot.k_fmt, fd, raw_buf, raw_size); } else { - boot.hdr->kernel_size = restore(KERNEL_FILE, fd); + boot.hdr->kernel_size = write(fd, raw_buf, raw_size); } + munmap(raw_buf, raw_size); } // dtb @@ -352,15 +372,15 @@ void repack(const char* orig_image, const char* out_image) { write_zero(fd, 512); } if (access(RAMDISK_FILE, R_OK) == 0) { - if (COMPRESSED(boot.r_fmt)) { - size_t cpio_size; - void *cpio; - mmap_ro(RAMDISK_FILE, cpio, cpio_size); - boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, cpio, cpio_size); - munmap(cpio, cpio_size); + size_t raw_size; + void *raw_buf; + mmap_ro(RAMDISK_FILE, raw_buf, raw_size); + if (!COMPRESSED(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) { + boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, raw_buf, raw_size); } else { - boot.hdr->ramdisk_size = restore(RAMDISK_FILE, fd); + boot.hdr->ramdisk_size = write(fd, raw_buf, raw_size); } + munmap(raw_buf, raw_size); file_align(); } diff --git a/native/jni/magiskboot/compress.cpp b/native/jni/magiskboot/compress.cpp index f437b1c0d..848d9ac26 100644 --- a/native/jni/magiskboot/compress.cpp +++ b/native/jni/magiskboot/compress.cpp @@ -14,14 +14,6 @@ using namespace std; -int64_t decompress(format_t type, int fd, const void *from, size_t size) { - return unique_ptr(get_decoder(type))->one_step(fd, from, size); -} - -int64_t compress(format_t type, int fd, const void *from, size_t size) { - return unique_ptr(get_encoder(type))->one_step(fd, from, size); -} - static bool read_file(FILE *fp, const function &fn) { char buf[4096]; size_t len; @@ -164,13 +156,6 @@ Compression *get_decoder(format_t type) { } } -int64_t Compression::one_step(int outfd, const void *in, size_t size) { - set_out(make_unique(outfd)); - if (!write(in, size)) - return -1; - return finalize(); -} - GZStream::GZStream(int mode) : mode(mode), strm({}) { switch(mode) { case 0: diff --git a/native/jni/magiskboot/compress.h b/native/jni/magiskboot/compress.h index f23460ac2..2b39b315e 100644 --- a/native/jni/magiskboot/compress.h +++ b/native/jni/magiskboot/compress.h @@ -14,7 +14,6 @@ class Compression : public FilterOutStream { public: - int64_t one_step(int outfd, const void *in, size_t size); virtual uint64_t finalize() = 0; }; @@ -171,7 +170,5 @@ private: Compression *get_encoder(format_t type); Compression *get_decoder(format_t type); -int64_t compress(format_t type, int fd, const void *from, size_t size); -int64_t decompress(format_t type, int fd, const void *from, size_t size); void compress(const char *method, const char *infile, const char *outfile); void decompress(char *infile, const char *outfile); diff --git a/native/jni/magiskboot/format.h b/native/jni/magiskboot/format.h index 6c51cf2ad..dba133ff8 100644 --- a/native/jni/magiskboot/format.h +++ b/native/jni/magiskboot/format.h @@ -1,5 +1,4 @@ -#ifndef _FORMAT_H_ -#define _FORMAT_H_ +#pragma once #include #include @@ -50,9 +49,6 @@ typedef enum { #define ACCLAIM_MAGIC "BauwksBoot" #define ACCLAIM_PRE_HEADER_SZ 262144 -#define SUP_LIST ((const char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL }) -#define SUP_EXT_LIST ((const char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL }) - class Fmt2Name { public: const char *operator[](format_t fmt); @@ -68,5 +64,3 @@ format_t check_fmt(const void *buf, size_t len); extern std::map name2fmt; extern Fmt2Name fmt2name; extern Fmt2Ext fmt2ext; - -#endif diff --git a/native/jni/magiskboot/main.cpp b/native/jni/magiskboot/main.cpp index a89ca78f4..08ed56a5a 100644 --- a/native/jni/magiskboot/main.cpp +++ b/native/jni/magiskboot/main.cpp @@ -14,20 +14,21 @@ static void usage(char *arg0) { fprintf(stderr, + FULL_VER(MagiskBoot) " - Boot Image Modification Tool\n" "Usage: %s [args...]\n" "\n" "Supported actions:\n" " --unpack \n" - " Unpack to kernel, ramdisk.cpio, and if available, second, dtb,\n" - " and extra into the current directory. Return values:\n" + " Unpack to, if available, kernel, ramdisk.cpio, \n" + " second, dtb, extra, and recovery_dtbo into current directory.\n" + " Return values:\n" " 0:valid 1:error 2:chromeos 3:ELF32 4:ELF64\n" "\n" " --repack [outbootimg]\n" - " Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n" + " Repack boot image components from current directory\n" " to [outbootimg], or new-boot.img if not specified.\n" - " It will compress ramdisk.cpio with the same method used in ,\n" - " or attempt to find ramdisk.cpio.[ext], and repack directly with the\n" - " compressed ramdisk file\n" + " It will compress ramdisk.cpio and kernel with the same method in\n" + " if the file provided is not already compressed.\n" "\n" " --hexpatch \n" " Search in , and replace with \n" @@ -80,16 +81,16 @@ static void usage(char *arg0) { " /[outfile] can be '-' to be STDIN/STDOUT\n" " Supported methods: " , arg0); - for (int i = 0; SUP_LIST[i]; ++i) - fprintf(stderr, "%s ", SUP_LIST[i]); + for (auto &it : name2fmt) + fprintf(stderr, "%s ", it.first.data()); fprintf(stderr, "\n\n" " --decompress [outfile]\n" " Detect method and decompress , optionally to [outfile]\n" " /[outfile] can be '-' to be STDIN/STDOUT\n" " Supported methods: "); - for (int i = 0; SUP_LIST[i]; ++i) - fprintf(stderr, "%s ", SUP_LIST[i]); + for (auto &it : name2fmt) + fprintf(stderr, "%s ", it.first.data()); fprintf(stderr, "\n\n" " --sha1 \n" @@ -104,31 +105,24 @@ static void usage(char *arg0) { int main(int argc, char *argv[]) { cmdline_logging(); - fprintf(stderr, FULL_VER(MagiskBoot) " - Boot Image Modification Tool\n"); - umask(0); + if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) { fprintf(stderr, "Cleaning up...\n"); - char name[PATH_MAX]; unlink(KERNEL_FILE); unlink(RAMDISK_FILE); - unlink(RAMDISK_FILE ".raw"); unlink(SECOND_FILE); unlink(DTB_FILE); unlink(EXTRA_FILE); unlink(RECV_DTBO_FILE); - for (int i = 0; SUP_EXT_LIST[i]; ++i) { - sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); - unlink(name); - } } else if (argc > 2 && strcmp(argv[1], "--sha1") == 0) { uint8_t sha1[SHA_DIGEST_SIZE]; void *buf; size_t size; mmap_ro(argv[2], buf, size); SHA_hash(buf, size, sha1); - for (int i = 0; i < SHA_DIGEST_SIZE; ++i) - printf("%02x", sha1[i]); + for (uint8_t i : sha1) + printf("%02x", i); printf("\n"); munmap(buf, size); } else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) {