Make lz4_lg an exception of lz4_legacy

This commit is contained in:
topjohnwu 2020-10-14 23:45:06 -07:00
parent 872c55207c
commit f3cd4da026
5 changed files with 40 additions and 13 deletions

View File

@ -18,7 +18,7 @@ buildscript {
extra["vNav"] = vNav extra["vNav"] = vNav
dependencies { 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("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${vNav}") classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${vNav}")

View File

@ -88,8 +88,9 @@ void dyn_img_hdr::print() {
} }
fprintf(stderr, "%-*s [%u]\n", PADDING, "PAGESIZE", page_size()); 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]\n", PADDING, "NAME", name());
}
fprintf(stderr, "%-*s [%.*s%.*s]\n", PADDING, "CMDLINE", fprintf(stderr, "%-*s [%.*s%.*s]\n", PADDING, "CMDLINE",
BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline()); BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
if (auto chksum = reinterpret_cast<uint8_t*>(id())) { if (auto chksum = reinterpret_cast<uint8_t*>(id())) {
@ -192,6 +193,23 @@ boot_img::~boot_img() {
delete hdr; 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) {\ #define get_block(name) {\
name = addr + off; \ name = addr + off; \
off += hdr->name##_size(); \ off += hdr->name##_size(); \
@ -269,7 +287,7 @@ void boot_img::parse_image(uint8_t *addr) {
find_kernel_dtb(); find_kernel_dtb();
if (auto size = hdr->kernel_size()) { if (auto size = hdr->kernel_size()) {
k_fmt = check_fmt(kernel, size); k_fmt = check_fmt_lg(kernel, size);
if (k_fmt == MTK) { if (k_fmt == MTK) {
fprintf(stderr, "MTK_KERNEL_HDR\n"); fprintf(stderr, "MTK_KERNEL_HDR\n");
flags |= MTK_KERNEL; 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); fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", k_hdr->name);
kernel += sizeof(mtk_hdr); kernel += sizeof(mtk_hdr);
hdr->kernel_size() -= 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]); fprintf(stderr, "%-*s [%s]\n", PADDING, "KERNEL_FMT", fmt2name[k_fmt]);
} }
if (auto size = hdr->ramdisk_size()) { if (auto size = hdr->ramdisk_size()) {
r_fmt = check_fmt(ramdisk, size); r_fmt = check_fmt_lg(ramdisk, size);
if (r_fmt == MTK) { if (r_fmt == MTK) {
fprintf(stderr, "MTK_RAMDISK_HDR\n"); fprintf(stderr, "MTK_RAMDISK_HDR\n");
flags |= MTK_RAMDISK; 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); fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", r_hdr->name);
ramdisk += sizeof(mtk_hdr); ramdisk += sizeof(mtk_hdr);
hdr->ramdisk_size() -= 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]); fprintf(stderr, "%-*s [%s]\n", PADDING, "RAMDISK_FMT", fmt2name[r_fmt]);
} }
if (auto size = hdr->extra_size()) { 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]); 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); }); run_finally f([=]{ munmap(buf, sz); });
if (int off = find_dtb_offset(buf, sz); off > 0) { 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)) { if (COMPRESSED(fmt)) {
int fd = creat(KERNEL_FILE, 0644); int fd = creat(KERNEL_FILE, 0644);
decompress(fmt, fd, buf, off); decompress(fmt, fd, buf, off);

View File

@ -447,9 +447,9 @@ private:
class LZ4_encoder : public cpr_stream { class LZ4_encoder : public cpr_stream {
public: 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]), : 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 write(const void *in, size_t size) override {
int ret = 0; int ret = 0;
@ -490,7 +490,8 @@ public:
~LZ4_encoder() override { ~LZ4_encoder() override {
if (buf_off) if (buf_off)
write_block(); write_block();
bwrite(&in_total, sizeof(in_total)); if (lg)
bwrite(&in_total, sizeof(in_total));
delete[] outbuf; delete[] outbuf;
delete[] buf; delete[] buf;
} }
@ -499,11 +500,12 @@ private:
char *outbuf; char *outbuf;
char *buf; char *buf;
bool init; bool init;
bool lg;
int buf_off; int buf_off;
unsigned in_total; unsigned in_total;
int write_block() { 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) { if (written == 0) {
LOGW("LZ4HC compression failure\n"); LOGW("LZ4HC compression failure\n");
return -1; return -1;
@ -525,7 +527,9 @@ stream_ptr get_encoder(format_t type, stream_ptr &&base) {
case LZ4: case LZ4:
return make_unique<LZ4F_encoder>(std::move(base)); return make_unique<LZ4F_encoder>(std::move(base));
case LZ4_LEGACY: case LZ4_LEGACY:
return make_unique<LZ4_encoder>(std::move(base)); return make_unique<LZ4_encoder>(std::move(base), false);
case LZ4_LG:
return make_unique<LZ4_encoder>(std::move(base), true);
case GZIP: case GZIP:
default: default:
return make_unique<gz_encoder>(std::move(base)); return make_unique<gz_encoder>(std::move(base));

View File

@ -15,6 +15,7 @@ public:
name2fmt["bzip2"] = BZIP2; name2fmt["bzip2"] = BZIP2;
name2fmt["lz4"] = LZ4; name2fmt["lz4"] = LZ4;
name2fmt["lz4_legacy"] = LZ4_LEGACY; name2fmt["lz4_legacy"] = LZ4_LEGACY;
name2fmt["lz4_lg"] = LZ4_LG;
} }
}; };
@ -75,6 +76,8 @@ const char *Fmt2Name::operator[](format_t fmt) {
return "lz4"; return "lz4";
case LZ4_LEGACY: case LZ4_LEGACY:
return "lz4_legacy"; return "lz4_legacy";
case LZ4_LG:
return "lz4_lg";
case MTK: case MTK:
return "mtk"; return "mtk";
case DTB: case DTB:
@ -98,6 +101,7 @@ const char *Fmt2Ext::operator[](format_t fmt) {
return ".bz2"; return ".bz2";
case LZ4: case LZ4:
case LZ4_LEGACY: case LZ4_LEGACY:
case LZ4_LG:
return ".lz4"; return ".lz4";
default: default:
return ""; return "";

View File

@ -17,6 +17,7 @@ typedef enum {
BZIP2, BZIP2,
LZ4, LZ4,
LZ4_LEGACY, LZ4_LEGACY,
LZ4_LG,
/* Unsupported compression */ /* Unsupported compression */
LZOP, LZOP,
/* Misc */ /* Misc */