mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 03:20:46 -05:00
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
This commit is contained in:
@@ -39,6 +39,13 @@ 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;
|
||||
@@ -649,7 +656,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(boot.k_fmt, m, fd);
|
||||
payload_sz = compress_len_kernel(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();
|
||||
|
||||
@@ -14,7 +14,9 @@ use lz4::{
|
||||
BlockMode, BlockSize, ContentChecksum, Decoder as LZ4FrameDecoder, Encoder as LZ4FrameEncoder,
|
||||
EncoderBuilder as LZ4FrameEncoderBuilder,
|
||||
};
|
||||
use lzma_rust2::{CheckType, LzmaOptions, LzmaReader, LzmaWriter, XzOptions, XzReader, XzWriter};
|
||||
use lzma_rust2::{
|
||||
CheckType, FilterType, LzmaOptions, LzmaReader, LzmaWriter, XzOptions, XzReader, XzWriter,
|
||||
};
|
||||
use std::cmp::min;
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use std::fs::File;
|
||||
@@ -206,6 +208,15 @@ impl<R: Read> Read for LZ4BlockDecoder<R> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
const BCJ_FILTER: FilterType = FilterType::BcjX86;
|
||||
#[cfg(target_arch = "arm")]
|
||||
const BCJ_FILTER: FilterType = FilterType::BcjArmThumb;
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
const BCJ_FILTER: FilterType = FilterType::BcjArm64;
|
||||
#[cfg(target_arch = "riscv64")]
|
||||
const BCJ_FILTER: FilterType = FilterType::BcjRiscv;
|
||||
|
||||
// Top-level APIs
|
||||
|
||||
pub fn get_encoder<'a, W: Write + 'a>(
|
||||
@@ -216,6 +227,7 @@ pub fn get_encoder<'a, W: Write + 'a>(
|
||||
FileFormat::XZ => {
|
||||
let mut opt = XzOptions::with_preset(6);
|
||||
opt.set_check_sum_type(CheckType::Crc32);
|
||||
opt.prepend_pre_filter(BCJ_FILTER, 0);
|
||||
Box::new(XzWriter::new(w, opt)?)
|
||||
}
|
||||
FileFormat::LZMA => Box::new(LzmaWriter::new_use_header(
|
||||
@@ -270,6 +282,30 @@ pub fn compress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) {
|
||||
}();
|
||||
}
|
||||
|
||||
pub fn compress_bytes_kernel(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) {
|
||||
let mut out_file = unsafe { ManuallyDrop::new(File::from_raw_fd(out_fd)) };
|
||||
|
||||
let _ = || -> LoggedResult<()> {
|
||||
let mut encoder = match format {
|
||||
FileFormat::XZ => {
|
||||
let mut opt = XzOptions::with_preset(6);
|
||||
opt.set_check_sum_type(CheckType::Crc32);
|
||||
let filter = match BCJ_FILTER {
|
||||
// ARM32 kernel uses ARM instructions, not thumb2
|
||||
FilterType::BcjArmThumb => FilterType::BcjArm,
|
||||
_ => BCJ_FILTER,
|
||||
};
|
||||
opt.prepend_pre_filter(filter, 0);
|
||||
Box::new(XzWriter::new(out_file.deref_mut(), opt)?)
|
||||
}
|
||||
_ => get_encoder(format, out_file.deref_mut())?,
|
||||
};
|
||||
std::io::copy(&mut Cursor::new(in_bytes), encoder.deref_mut())?;
|
||||
encoder.finish()?;
|
||||
Ok(())
|
||||
}();
|
||||
}
|
||||
|
||||
pub fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) -> bool {
|
||||
let mut out_file = unsafe { ManuallyDrop::new(File::from_raw_fd(out_fd)) };
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#![feature(iter_intersperse)]
|
||||
|
||||
pub use base;
|
||||
use compress::{compress_bytes, decompress_bytes};
|
||||
use compress::{compress_bytes, compress_bytes_kernel, decompress_bytes};
|
||||
use dtb::find_dtb_offset_for_cxx;
|
||||
use format::{check_fmt, fmt_compressed, fmt_compressed_any, fmt2name};
|
||||
use sign::{SHA, get_sha, sha256_hash, sign_payload_for_cxx};
|
||||
@@ -80,6 +80,7 @@ pub mod ffi {
|
||||
|
||||
fn check_fmt(buf: &[u8]) -> FileFormat;
|
||||
fn compress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32);
|
||||
fn compress_bytes_kernel(format: FileFormat, in_bytes: &[u8], out_fd: i32);
|
||||
fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32) -> bool;
|
||||
fn fmt2name(fmt: FileFormat) -> *const c_char;
|
||||
fn fmt_compressed(fmt: FileFormat) -> bool;
|
||||
|
||||
Reference in New Issue
Block a user