diff --git a/native/src/boot/zimage.rs b/native/src/boot/zimage.rs index 5cd1c78bd..f783bdcaa 100644 --- a/native/src/boot/zimage.rs +++ b/native/src/boot/zimage.rs @@ -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.