diff --git a/build.gradle.kts b/build.gradle.kts index 2606ee406..12bbfd093 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,7 @@ buildscript { extra["vNav"] = vNav dependencies { - classpath("com.android.tools.build:gradle:4.0.2") + classpath("com.android.tools.build:gradle:4.1.0") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10") classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${vNav}") diff --git a/native/jni/magiskboot/bootimg.cpp b/native/jni/magiskboot/bootimg.cpp index 42193fbd8..1bd895f7d 100644 --- a/native/jni/magiskboot/bootimg.cpp +++ b/native/jni/magiskboot/bootimg.cpp @@ -88,8 +88,9 @@ void dyn_img_hdr::print() { } fprintf(stderr, "%-*s [%u]\n", PADDING, "PAGESIZE", page_size()); - if (ver < 3) + if (ver < 3) { fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", name()); + } fprintf(stderr, "%-*s [%.*s%.*s]\n", PADDING, "CMDLINE", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline()); if (auto chksum = reinterpret_cast(id())) { @@ -192,6 +193,23 @@ boot_img::~boot_img() { delete hdr; } +static format_t check_fmt_lg(uint8_t *buf, unsigned size) { + format_t fmt = check_fmt(buf, size); + if (fmt == LZ4_LEGACY) { + // We need to check if it is LZ4_LG + unsigned off = 4; + unsigned block_sz; + while (off + sizeof(block_sz) <= size) { + memcpy(&block_sz, buf + off, sizeof(block_sz)); + off += sizeof(block_sz); + if (off + block_sz > size) + return LZ4_LG; + off += block_sz; + } + } + return fmt; +} + #define get_block(name) {\ name = addr + off; \ off += hdr->name##_size(); \ @@ -269,7 +287,7 @@ void boot_img::parse_image(uint8_t *addr) { find_kernel_dtb(); if (auto size = hdr->kernel_size()) { - k_fmt = check_fmt(kernel, size); + k_fmt = check_fmt_lg(kernel, size); if (k_fmt == MTK) { fprintf(stderr, "MTK_KERNEL_HDR\n"); flags |= MTK_KERNEL; @@ -278,12 +296,12 @@ void boot_img::parse_image(uint8_t *addr) { fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", k_hdr->name); kernel += sizeof(mtk_hdr); hdr->kernel_size() -= sizeof(mtk_hdr); - k_fmt = check_fmt(kernel, hdr->kernel_size()); + k_fmt = check_fmt_lg(kernel, hdr->kernel_size()); } fprintf(stderr, "%-*s [%s]\n", PADDING, "KERNEL_FMT", fmt2name[k_fmt]); } if (auto size = hdr->ramdisk_size()) { - r_fmt = check_fmt(ramdisk, size); + r_fmt = check_fmt_lg(ramdisk, size); if (r_fmt == MTK) { fprintf(stderr, "MTK_RAMDISK_HDR\n"); flags |= MTK_RAMDISK; @@ -292,12 +310,12 @@ void boot_img::parse_image(uint8_t *addr) { fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", r_hdr->name); ramdisk += sizeof(mtk_hdr); hdr->ramdisk_size() -= sizeof(mtk_hdr); - r_fmt = check_fmt(ramdisk, hdr->ramdisk_size()); + r_fmt = check_fmt_lg(ramdisk, hdr->ramdisk_size()); } fprintf(stderr, "%-*s [%s]\n", PADDING, "RAMDISK_FMT", fmt2name[r_fmt]); } if (auto size = hdr->extra_size()) { - e_fmt = check_fmt(extra, size); + e_fmt = check_fmt_lg(extra, size); fprintf(stderr, "%-*s [%s]\n", PADDING, "EXTRA_FMT", fmt2name[e_fmt]); } } @@ -344,7 +362,7 @@ int split_image_dtb(const char *filename) { run_finally f([=]{ munmap(buf, sz); }); if (int off = find_dtb_offset(buf, sz); off > 0) { - format_t fmt = check_fmt(buf, sz); + format_t fmt = check_fmt_lg(buf, sz); if (COMPRESSED(fmt)) { int fd = creat(KERNEL_FILE, 0644); decompress(fmt, fd, buf, off); diff --git a/native/jni/magiskboot/compress.cpp b/native/jni/magiskboot/compress.cpp index 0cee735bd..4a5c2ae0b 100644 --- a/native/jni/magiskboot/compress.cpp +++ b/native/jni/magiskboot/compress.cpp @@ -447,9 +447,9 @@ private: class LZ4_encoder : public cpr_stream { public: - explicit LZ4_encoder(stream_ptr &&base) + explicit LZ4_encoder(stream_ptr &&base, bool lg) : cpr_stream(std::move(base)), outbuf(new char[LZ4_COMPRESSED]), - buf(new char[LZ4_UNCOMPRESSED]), init(false), buf_off(0), in_total(0) {} + buf(new char[LZ4_UNCOMPRESSED]), init(false), lg(lg), buf_off(0), in_total(0) {} int write(const void *in, size_t size) override { int ret = 0; @@ -490,7 +490,8 @@ public: ~LZ4_encoder() override { if (buf_off) write_block(); - bwrite(&in_total, sizeof(in_total)); + if (lg) + bwrite(&in_total, sizeof(in_total)); delete[] outbuf; delete[] buf; } @@ -499,11 +500,12 @@ private: char *outbuf; char *buf; bool init; + bool lg; int buf_off; unsigned in_total; int write_block() { - int written = LZ4_compress_HC(buf, outbuf, buf_off, LZ4_COMPRESSED, 9); + int written = LZ4_compress_HC(buf, outbuf, buf_off, LZ4_COMPRESSED, LZ4HC_CLEVEL_MAX); if (written == 0) { LOGW("LZ4HC compression failure\n"); return -1; @@ -525,7 +527,9 @@ stream_ptr get_encoder(format_t type, stream_ptr &&base) { case LZ4: return make_unique(std::move(base)); case LZ4_LEGACY: - return make_unique(std::move(base)); + return make_unique(std::move(base), false); + case LZ4_LG: + return make_unique(std::move(base), true); case GZIP: default: return make_unique(std::move(base)); diff --git a/native/jni/magiskboot/format.cpp b/native/jni/magiskboot/format.cpp index ebda752a4..e34b73128 100644 --- a/native/jni/magiskboot/format.cpp +++ b/native/jni/magiskboot/format.cpp @@ -15,6 +15,7 @@ public: name2fmt["bzip2"] = BZIP2; name2fmt["lz4"] = LZ4; name2fmt["lz4_legacy"] = LZ4_LEGACY; + name2fmt["lz4_lg"] = LZ4_LG; } }; @@ -75,6 +76,8 @@ const char *Fmt2Name::operator[](format_t fmt) { return "lz4"; case LZ4_LEGACY: return "lz4_legacy"; + case LZ4_LG: + return "lz4_lg"; case MTK: return "mtk"; case DTB: @@ -98,6 +101,7 @@ const char *Fmt2Ext::operator[](format_t fmt) { return ".bz2"; case LZ4: case LZ4_LEGACY: + case LZ4_LG: return ".lz4"; default: return ""; diff --git a/native/jni/magiskboot/format.hpp b/native/jni/magiskboot/format.hpp index a445e6200..05d5f39e8 100644 --- a/native/jni/magiskboot/format.hpp +++ b/native/jni/magiskboot/format.hpp @@ -17,6 +17,7 @@ typedef enum { BZIP2, LZ4, LZ4_LEGACY, + LZ4_LG, /* Unsupported compression */ LZOP, /* Misc */