magiskboot: detect format for XZ BCJ filter

Detect the architecture and file format from input bytes to apply the
proper BCJ pre-filter when compressing with XZ:

- ELF binaries: map e_machine to x86, ARM-Thumb, ARM64, or RISC-V.
- Linux kernels: detect ARM64 raw Image, RISC-V Image, and ARM32 raw
  vmlinux.bin via condition-code density.
- Non-code data: omit BCJ filters to avoid degradation.

Introduce get_encoder_bcj_detection and eliminate the redundant
compress_bytes_kernel and compress_len_kernel helpers.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-04 04:07:03 +00:00
parent e2dc5984ea
commit 70bf88c383
5 changed files with 79 additions and 51 deletions
+1 -8
View File
@@ -39,13 +39,6 @@ static off_t compress_len(FileFormat type, byte_view in, int fd) {
return now - prev;
}
static off_t compress_len_kernel(FileFormat type, byte_view in, int fd) {
auto prev = lseek(fd, 0, SEEK_CUR);
compress_bytes_kernel(type, in, fd);
auto now = lseek(fd, 0, SEEK_CUR);
return now - prev;
}
static void dump(const void *buf, size_t size, const char *filename) {
if (size == 0)
return;
@@ -656,7 +649,7 @@ void repack(Utf8CStr src_img, Utf8CStr out_img, bool skip_comp) {
mmap_data m(KERNEL_FILE);
uint32_t payload_sz = 0;
if (!skip_comp && !fmt_compressed_any(check_fmt(m.data(), m.size())) && fmt_compressed(boot.k_fmt)) {
payload_sz = compress_len_kernel(boot.k_fmt, m, fd);
payload_sz = compress_len(boot.k_fmt, m, fd);
if (boot.flags[ZIMAGE_KERNEL] && boot.k_fmt != FileFormat::GZIP) {
// For non-gzip compression in zImage, size_append appends the 4-byte LE uncompressed size
uint32_t sz = m.size();