Commit Graph
49 Commits
Author SHA1 Message Date
topjohnwu cdb02ea12a Several small improvements and bug fixes 2026-10-07 02:58:28 +00:00
topjohnwu 43afdf16a8 Automatically re-sign ChromeOS images on repack
Instead of requiring scripts to detect ChromeOS images and sign them
after repacking, preallocate the vblock and append the kernel blob
trailer while repacking, then generate the vblock after all other
patching is done. The image is padded to its original size after
signing, same as other boot images.

The signer now only takes the kernel payload and returns the vblock,
hashing the kernel blob incrementally. The sign-chromeos action
streams its output to disk instead of building the whole signed
image in memory.

Since ChromeOS images no longer need special handling, unpack no
longer returns 2 for them, and all ChromeOS logic is removed from
the scripts.

Assisted-by: Claude Opus 5.5
2026-10-06 23:36:12 +00:00
topjohnwu 70bf88c383 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
2026-10-04 04:07:03 +00:00
LoveSy 07390a7860 Revert "Update AVB hash header image_size to match unpacked original_image_size."
This reverts commit d9a9b02c27.
2026-09-23 18:57:19 -07:00
topjohnwu da320823ac magiskboot: enable BCJ filter for XZ compression
Enable the architecture-specific BCJ pre-filter when creating XzWriter
for supported ABIs. For ARM32 kernel compression, use the standard ARM
filter rather than Thumb-2.

Assisted-by: Gemini 3.8 Flash
2026-09-20 02:21:59 -07:00
topjohnwu 6d391dd5b1 magiskboot: handle decompression errors in unpack
Previously, decompress_bytes dropped the LoggedResult from decoding,
returning void. In bootimg.cpp, unpack and split_image_dtb never checked
whether decompression succeeded, leaving empty 0-byte output files and
returning RETURN_OK (0).

Return a boolean status from decompress_bytes and check it during unpack
and split_image_dtb. On decompression failure, remove any incomplete
output files and return RETURN_ERROR (1).

Assisted-by: Gemini 3.8 Flash
2026-09-18 16:50:34 -07:00
topjohnwu 15062974a3 magiskboot: allow free zImage piggy payload size
Support dynamic zImage piggy payload sizes during repacking by updating
the zImage decompressor stub headers, symbol tables, and GOT entries.

Previously, magiskboot padded repacked zImage files to preserve the
exact original file size. For non-gzip compressed zImages (such as XZ),
repacking with a different payload size broke stub symbol resolution
and resulted in ununpackable or unbootable images.

Key changes:
- Encapsulate zImage parsing and patching in a dedicated struct ZImage
  in Rust with CXX FFI, new_head, and new_tail methods.
- Locate piggy_end using table references or GOT table entries bounded
  by LC0.
- Relocate LC0 table entries, LC1 table, and GOT table entries with
  4-byte padding/alignment when payload size changes.
- Relocate R_ARM_GOTPC PC-relative literal pools in decompressor .text.
- Append uncompressed size for non-gzip algorithms per size_append.
- Remove zopfli compression support across magiskboot as dynamic zImage
  piggy sizing supersedes the need for exact size matching.

Fix #9880

Assisted-by: Gemini 3.7 Flash
2026-08-25 19:09:29 +00:00
topjohnwu de60431a0d magiskboot: port find_dtb_offset to Rust
Port find_dtb_offset from C++ to Rust in dtb.rs, using the fdt crate
for header parsing and root node validation.

Key changes:
- Upgrade fdt crate to 0.2.0-alpha2 and adapt dtb.rs to updated APIs.
- Implement find_dtb_offset using Fdt::new_unaligned_fallible.
- Define MIN_NON_EMPTY_DTB_SIZE (0x48) per Linux kernel ARM zImage
  specification to ignore empty or false positive DTB headers.
- Expose find_dtb_offset to C++ via CXX bridge and clean up legacy
  structs and declarations.

Assisted-by: Gemini 3.7 Flash
2026-08-25 19:09:29 +00:00
topjohnwu c02705d969 magiskboot: migrate check_fmt to Rust
Migrate check_fmt and guess_lzma to Rust in format.rs.

Assisted-by: Gemini 3.6 Flash
2026-08-25 19:09:29 +00:00
kousuandClaude Sonnet 4.6 d9a9b02c27 Update AVB hash header image_size to match unpacked original_image_size.
Fixes https://github.com/topjohnwu/Magisk/issues/8389

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-08-05 10:32:24 -07:00
Wang Han e590ffbdcb Fix formatting and update comments in bootimg.cpp 2026-01-31 13:50:36 -08:00
Wang Han 97138f1e91 magiskboot: Fix tail offset calculation 2026-01-31 13:50:36 -08:00
Pzqqt 0919db6b11 magiskboot: Avoid implicit type conversion
Fix #9607
2026-01-19 23:33:00 +08:00
topjohnwu 9ac4b5ce7d Add proper lzma format detection 2025-12-05 03:40:18 -08:00
Wang Han c8d51b38ba Enhance fdt_header validation for empty dtb 2025-11-02 02:42:48 -08:00
topjohnwu cd8820f563 Refactor code for more readability 2025-09-29 01:41:55 -07:00
topjohnwu 742913ebcb Support installing Magisk on vendor_boot
Close #9238, fix #8835
2025-09-28 01:10:11 -07:00
topjohnwu 2e4fa6864c Make Utf8CStr a first class citizen in C++ codebase
Utf8CStr is in many cases a better string view class than
std::string_view, because it provides "view" access to a string buffer
that is guaranteed to be null terminated. It also has the additional
benefit of being UTF-8 verified and can seemlessly cross FFI boundaries.

We would want to start use more Utf8CStr in our existing C++ codebase.
2025-08-27 22:48:48 -07:00
topjohnwu b861671391 Cleanup libbase 2025-08-25 01:31:47 -07:00
topjohnwu 2c39d0234d Fix compression format detection 2025-08-21 12:21:22 -07:00
topjohnwu c313812129 Simplify magiskboot FFI 2025-08-21 12:21:22 -07:00
LoveSy db8d832707 Move magiskboot cli to argh 2025-08-20 21:40:34 -07:00
5ec1cff a54bdb54e4 Skip avb 1,0 verify if tail contains avb 2.0 header
This way, magiskboot will not print "unexpected ASN.1 DER tag: expected SEQUENCE, got APPLICATION [1] (primitive)".
2025-07-21 00:51:14 -07:00
topjohnwu 1460317ebd Cleanup C++ headers 2025-06-16 02:25:38 -07:00
topjohnwu 78d1200608 Migrate all compression code to Rust 2025-05-20 03:26:00 -07:00
David K. 0e82df9e10 Support zImage compression types other than gzip.
Instead of just searching for the gzip magic, it now incrementally searches the kernel for the first thing that `check_fmt_lg` doesn't report as `UNKNOWN`.
2025-03-02 13:35:13 -08:00
topjohnwu 7188de1205 Support unaligned boot image file
Fix #7733
2024-10-06 03:01:08 -07:00
残页 46a34e19bc Check vendor boot ramdisk table size 2024-07-31 16:59:51 -07:00
topjohnwu 5ac7dc0b37 Support vendor boot unpack/repack
Fix #6460, close #6620
2024-07-30 04:00:12 -07:00
osm0sis 991802ab82 Add no decompression flag to magiskboot split 2024-03-06 16:15:30 -08:00
topjohnwu 625a1d6f44 Remove seek support from streams 2024-02-28 11:07:53 -08:00
LoveSy 8d7c7c3dfb Refactor dtb in rust 2023-09-19 00:41:42 -07:00
LoveSy 0243610c09 No trailing zeros if the signed boot img is larger 2023-09-12 18:09:20 -07:00
topjohnwu 41226c12b8 Update to ONDK r25.7 2023-08-15 17:10:20 -07:00
topjohnwu af65d07456 Support AVB1.0 signing and verification in magiskboot 2023-07-17 18:57:50 -07:00
topjohnwu c97ab690b6 Segment memory mapped boot image region 2023-07-13 21:01:49 -07:00
topjohnwu ee943afbc9 Cleanup SHA hash implementation 2023-06-30 15:50:52 -07:00
LoveSy 1f7c3e9f14 Use rust to calculate sha 2023-06-30 14:06:02 -07:00
topjohnwu 305e8b3d14 Improve bootimg const correctness 2023-06-03 05:10:22 -07:00
topjohnwu 2a654e5d7f Improve byte_data const correctness 2023-06-03 03:16:03 -07:00
topjohnwu f5aaff2b1e Cleanup filter_out_stream implementation 2023-05-20 01:28:10 -07:00
topjohnwu 57bd450798 Split input and output streams 2023-05-19 04:19:43 -07:00
LoveSy 7bf2e3875f Support extract boot image from payload.bin 2023-05-04 21:37:08 -07:00
vvb2060 f54daa3469 Force ramdisk format to lz4_legacy for v4 2023-03-23 20:50:27 -07:00
topjohnwu 4417997749 Make sure ro mmap region is not overwritten 2023-03-21 15:50:43 -07:00
LoveSy 2eef542054 Add amonet microloader support 2023-03-21 15:50:43 -07:00
LoveSy d9f0aed571 Fix unpack -n and repack -n of ZIMAGE kernel 2023-03-17 02:00:46 -07:00
ae34659b26 No kernel repack if it isn't patched at all
It turns out that decompressing and recompressing the kernel is enough to break booting on many devices that use MT6763.
Fix #5124, fix #6204, fix #6566 

Co-authored-by: LoveSy <shana@zju.edu.cn>
Co-authored-by: 南宫雪珊 <vvb2060@gmail.com>
2023-02-17 15:00:14 -08:00
topjohnwu b9e89a1a2d Restructure the native module
Consolidate all code into the src folder
2022-07-23 13:51:56 -07:00