magiskboot: search compressed formats in zImage

When locating the compressed piggy payload in a zImage, check only
formats where fmt.is_compressed() to avoid false positives on container
or header formats (such as DTB or Android boot magic).

Additionally, prioritize 4-byte aligned offsets since ARM kernel linker
scripts align .piggydata to 4 bytes, falling back to an unaligned scan
only if no candidate is found.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-09-18 16:50:34 -07:00
committed by John Wu
parent 8376f7b27c
commit 887a97822b
+18 -7
View File
@@ -120,17 +120,29 @@ impl<'a> ZImage<'a> {
let table_magic = read_u32(zimg, 0x34);
let table_offset = read_u32(zimg, 0x38);
// Step 2: Locate the start of compressed piggy payload by scanning forward from 0x28
// Step 2: Locate the start of compressed piggy payload by scanning forward from 0x28.
// Compressed payload sections (.piggydata) are 4-byte aligned in Linux kernel linker scripts.
let mut piggy_ptr = None;
for curr in 0x28..zimg.len() {
if check_fmt(&zimg[curr..]) != FileFormat::UNKNOWN {
piggy_ptr = Some(curr);
for curr in (0x28..zimg.len()).step_by(4) {
let fmt = check_fmt(&zimg[curr..]);
if fmt.is_compressed() {
piggy_ptr = Some((curr, fmt));
break;
}
}
// Fallback to unaligned scan if 4-byte aligned scan yields no candidate
if piggy_ptr.is_none() {
for curr in 0x28..zimg.len() {
let fmt = check_fmt(&zimg[curr..]);
if fmt.is_compressed() {
piggy_ptr = Some((curr, fmt));
break;
}
}
}
let piggy_off = match piggy_ptr {
Some(off) => off,
let (piggy_off, fmt) = match piggy_ptr {
Some((off, fmt)) => (off, fmt),
None => {
eprintln!("! Could not find zImage piggy, keeping raw kernel");
return None;
@@ -138,7 +150,6 @@ impl<'a> ZImage<'a> {
};
let head = &zimg[..piggy_off];
let fmt = check_fmt(&zimg[piggy_off..]);
// Step 3: Scan for the LC0 position table (arch/arm/boot/compressed/head.S)
// LC0 contains position-independent runtime pointers: _start, _got_start, _got_end, _edata, etc.