diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 2fa81571f..cdef1d7bf 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -541,6 +541,14 @@ void repack(const char *src_img, const char *out_img, bool skip_comp) { } else { hdr->kernel_size() = xwrite(fd, raw_buf, raw_size); } + + if (boot.flags[ZIMAGE_KERNEL] && + boot.k_fmt == GZIP && hdr->kernel_size() > boot.hdr->kernel_size()) { + // Revert and try zipfoli + ftruncate(fd, lseek(fd, -hdr->kernel_size(), SEEK_CUR)); + hdr->kernel_size() = compress(ZOPFLI, fd, raw_buf, raw_size); + } + munmap(raw_buf, raw_size); } if (boot.flags[ZIMAGE_KERNEL]) { diff --git a/native/jni/magiskboot/compress.cpp b/native/jni/magiskboot/compress.cpp index e62f106db..79a0734e6 100644 --- a/native/jni/magiskboot/compress.cpp +++ b/native/jni/magiskboot/compress.cpp @@ -624,8 +624,9 @@ stream_ptr get_encoder(format_t type, stream_ptr &&base) { return make_unique(std::move(base), false); case LZ4_LG: return make_unique(std::move(base), true); - case GZIP: + case ZOPFLI: return make_unique(std::move(base)); + case GZIP: default: return make_unique(std::move(base)); } @@ -643,6 +644,7 @@ stream_ptr get_decoder(format_t type, stream_ptr &&base) { case LZ4_LEGACY: case LZ4_LG: return make_unique(std::move(base)); + case ZOPFLI: case GZIP: default: return make_unique(std::move(base)); diff --git a/native/jni/magiskboot/format.cpp b/native/jni/magiskboot/format.cpp index 4a4fc81ad..35e5bb35b 100644 --- a/native/jni/magiskboot/format.cpp +++ b/native/jni/magiskboot/format.cpp @@ -47,6 +47,8 @@ const char *Fmt2Name::operator[](format_t fmt) { switch (fmt) { case GZIP: return "gzip"; + case ZOPFLI: + return "zopfli"; case LZOP: return "lzop"; case XZ: @@ -73,6 +75,7 @@ const char *Fmt2Name::operator[](format_t fmt) { const char *Fmt2Ext::operator[](format_t fmt) { switch (fmt) { case GZIP: + case ZOPFLI: return ".gz"; case LZOP: return ".lzo"; @@ -96,6 +99,7 @@ const char *Fmt2Ext::operator[](format_t fmt) { format_t Name2Fmt::operator[](std::string_view name) { if (0) {} CHECK("gzip", GZIP) + CHECK("zopfli", ZOPFLI) CHECK("xz", XZ) CHECK("lzma", LZMA) CHECK("bzip2", BZIP2) diff --git a/native/jni/magiskboot/format.hpp b/native/jni/magiskboot/format.hpp index 6ff9828e0..590193836 100644 --- a/native/jni/magiskboot/format.hpp +++ b/native/jni/magiskboot/format.hpp @@ -12,6 +12,7 @@ typedef enum { BLOB, /* Compression formats */ GZIP, + ZOPFLI, XZ, LZMA, BZIP2,