diff --git a/native/src/boot/bootimg.cpp b/native/src/boot/bootimg.cpp index dab17c49a..562c7ebb8 100644 --- a/native/src/boot/bootimg.cpp +++ b/native/src/boot/bootimg.cpp @@ -53,55 +53,6 @@ static bool check_env(const char *name) { return val != nullptr && val == "true"sv; } -static bool guess_lzma(const uint8_t *buf, size_t len) { - // 0 : (pb * 5 + lp) * 9 + lc - // 1 - 4 : dict size, must be 2^n - // 5 - 12: all 0xFF - if (len <= 13) return false; - if (memcmp(buf, "\x5d", 1) != 0) return false; - uint32_t dict_sz = 0; - memcpy(&dict_sz, buf + 1, sizeof(dict_sz)); - if (dict_sz == 0 || (dict_sz & (dict_sz - 1)) != 0) return false; - if (memcmp(buf + 5, "\xff\xff\xff\xff\xff\xff\xff\xff", 8) != 0) return false; - return true; -} - -FileFormat check_fmt(const void *buf, size_t len) { - if (CHECKED_MATCH(CHROMEOS_MAGIC)) { - return FileFormat::CHROMEOS; - } else if (CHECKED_MATCH(BOOT_MAGIC)) { - return FileFormat::AOSP; - } else if (CHECKED_MATCH(VENDOR_BOOT_MAGIC)) { - return FileFormat::AOSP_VENDOR; - } else if (CHECKED_MATCH(GZIP1_MAGIC) || CHECKED_MATCH(GZIP2_MAGIC)) { - return FileFormat::GZIP; - } else if (CHECKED_MATCH(LZOP_MAGIC)) { - return FileFormat::LZOP; - } else if (CHECKED_MATCH(XZ_MAGIC)) { - return FileFormat::XZ; - } else if (guess_lzma(static_cast(buf), len)) { - return FileFormat::LZMA; - } else if (CHECKED_MATCH(BZIP_MAGIC)) { - return FileFormat::BZIP2; - } else if (CHECKED_MATCH(LZ41_MAGIC) || CHECKED_MATCH(LZ42_MAGIC)) { - return FileFormat::LZ4; - } else if (CHECKED_MATCH(LZ4_LEG_MAGIC)) { - return FileFormat::LZ4_LEGACY; - } else if (CHECKED_MATCH(MTK_MAGIC)) { - return FileFormat::MTK; - } else if (CHECKED_MATCH(DTB_MAGIC)) { - return FileFormat::DTB; - } else if (CHECKED_MATCH(DHTB_MAGIC)) { - return FileFormat::DHTB; - } else if (CHECKED_MATCH(TEGRABLOB_MAGIC)) { - return FileFormat::BLOB; - } else if (len >= 0x28 && memcmp(&((char *)buf)[0x24], ZIMAGE_MAGIC, 4) == 0) { - return FileFormat::ZIMAGE; - } else { - return FileFormat::UNKNOWN; - } -} - void dyn_img_hdr::print() const { uint32_t ver = header_version(); fprintf(stderr, "%-*s [%u]\n", PADDING, "HEADER_VER", ver); @@ -479,7 +430,6 @@ std::span boot_img::vendor_ramdisk_tbl() co return span(reinterpret_cast(vendor_ramdisk_table), hdr->vendor_ramdisk_table_entry_num()); } - #define assert_off() \ if ((addr + off) > (map.data() + map_end)) { \ fprintf(stderr, "Corrupted boot image!\n"); \ diff --git a/native/src/boot/compress.rs b/native/src/boot/compress.rs index cb2cc50f8..4aa044f6b 100644 --- a/native/src/boot/compress.rs +++ b/native/src/boot/compress.rs @@ -1,4 +1,5 @@ -use crate::ffi::{FileFormat, check_fmt}; +use crate::ffi::FileFormat; +use crate::format::check_fmt; use base::nix::fcntl::OFlag; use base::{Chunker, FileOrStd, LoggedResult, ReadExt, Utf8CStr, Utf8CString, WriteExt, log_err}; use bzip2::Compression as BzCompression; diff --git a/native/src/boot/format.rs b/native/src/boot/format.rs index 87bf9b7a3..5c74f5d67 100644 --- a/native/src/boot/format.rs +++ b/native/src/boot/format.rs @@ -102,3 +102,73 @@ pub fn fmt_compressed(fmt: FileFormat) -> bool { pub fn fmt_compressed_any(fmt: FileFormat) -> bool { fmt.is_compressed() || matches!(fmt, FileFormat::LZOP) } + +const CHROMEOS_MAGIC: &[u8] = b"CHROMEOS"; +const BOOT_MAGIC: &[u8] = b"ANDROID!"; +const VENDOR_BOOT_MAGIC: &[u8] = b"VNDRBOOT"; +const GZIP1_MAGIC: &[u8] = b"\x1f\x8b"; +const GZIP2_MAGIC: &[u8] = b"\x1f\x9e"; +const LZOP_MAGIC: &[u8] = b"\x89LZO"; +const XZ_MAGIC: &[u8] = b"\xfd7zXZ"; +const BZIP_MAGIC: &[u8] = b"BZh"; +const LZ41_MAGIC: &[u8] = b"\x03\x21\x4c\x18"; +const LZ42_MAGIC: &[u8] = b"\x04\x22\x4d\x18"; +const LZ4_LEG_MAGIC: &[u8] = b"\x02\x21\x4c\x18"; +const MTK_MAGIC: &[u8] = b"\x88\x16\x88\x58"; +const DTB_MAGIC: &[u8] = b"\xd0\x0d\xfe\xed"; +const DHTB_MAGIC: &[u8] = b"\x44\x48\x54\x42\x01\x00\x00\x00"; +const TEGRABLOB_MAGIC: &[u8] = b"-SIGNED-BY-SIGNBLOB-"; +const ZIMAGE_MAGIC: &[u8] = b"\x18\x28\x6f\x01"; + +fn guess_lzma(buf: &[u8]) -> bool { + // 0 : (pb * 5 + lp) * 9 + lc + // 1 - 4 : dict size, must be 2^n + // 5 - 12: all 0xFF + if buf.len() <= 13 || buf[0] != 0x5d { + return false; + } + let dict_sz = match buf[1..5].try_into() { + Ok(bytes) => u32::from_le_bytes(bytes), + Err(_) => return false, + }; + if dict_sz == 0 || (dict_sz & (dict_sz - 1)) != 0 { + return false; + } + buf[5..13] == [0xff; 8] +} + +pub fn check_fmt(buf: &[u8]) -> FileFormat { + if buf.starts_with(CHROMEOS_MAGIC) { + FileFormat::CHROMEOS + } else if buf.starts_with(BOOT_MAGIC) { + FileFormat::AOSP + } else if buf.starts_with(VENDOR_BOOT_MAGIC) { + FileFormat::AOSP_VENDOR + } else if buf.starts_with(GZIP1_MAGIC) || buf.starts_with(GZIP2_MAGIC) { + FileFormat::GZIP + } else if buf.starts_with(LZOP_MAGIC) { + FileFormat::LZOP + } else if buf.starts_with(XZ_MAGIC) { + FileFormat::XZ + } else if guess_lzma(buf) { + FileFormat::LZMA + } else if buf.starts_with(BZIP_MAGIC) { + FileFormat::BZIP2 + } else if buf.starts_with(LZ41_MAGIC) || buf.starts_with(LZ42_MAGIC) { + FileFormat::LZ4 + } else if buf.starts_with(LZ4_LEG_MAGIC) { + FileFormat::LZ4_LEGACY + } else if buf.starts_with(MTK_MAGIC) { + FileFormat::MTK + } else if buf.starts_with(DTB_MAGIC) { + FileFormat::DTB + } else if buf.starts_with(DHTB_MAGIC) { + FileFormat::DHTB + } else if buf.starts_with(TEGRABLOB_MAGIC) { + FileFormat::BLOB + } else if buf.len() >= 0x28 && &buf[0x24..0x28] == ZIMAGE_MAGIC { + FileFormat::ZIMAGE + } else { + FileFormat::UNKNOWN + } +} diff --git a/native/src/boot/lib.rs b/native/src/boot/lib.rs index c7fca8241..25988fef2 100644 --- a/native/src/boot/lib.rs +++ b/native/src/boot/lib.rs @@ -3,7 +3,7 @@ pub use base; use compress::{compress_bytes, decompress_bytes}; -use format::{fmt_compressed, fmt_compressed_any, fmt2name}; +use format::{check_fmt, fmt_compressed, fmt_compressed_any, fmt2name}; use sign::{SHA, get_sha, sha256_hash, sign_payload_for_cxx}; use std::env; @@ -56,7 +56,6 @@ pub mod ffi { fn unpack(image: Utf8CStrRef, skip_decomp: bool, hdr: bool) -> i32; fn repack(src_img: Utf8CStrRef, out_img: Utf8CStrRef, skip_comp: bool); fn split_image_dtb(filename: Utf8CStrRef, skip_decomp: bool) -> i32; - fn check_fmt(buf: &[u8]) -> FileFormat; } extern "Rust" { @@ -67,6 +66,7 @@ pub mod ffi { fn output_size(self: &SHA) -> usize; fn sha256_hash(data: &[u8], out: &mut [u8]); + fn check_fmt(buf: &[u8]) -> FileFormat; fn compress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32); fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: i32); fn fmt2name(fmt: FileFormat) -> *const c_char; diff --git a/native/src/boot/magiskboot.hpp b/native/src/boot/magiskboot.hpp index 06148f469..deaf52e11 100644 --- a/native/src/boot/magiskboot.hpp +++ b/native/src/boot/magiskboot.hpp @@ -12,29 +12,15 @@ #define RECV_DTBO_FILE "recovery_dtbo" #define DTB_FILE "dtb" #define BOOTCONFIG_FILE "bootconfig" -#define NEW_BOOT "new-boot.img" #define BUFFER_MATCH(buf, s) (memcmp(buf, s, sizeof(s) - 1) == 0) #define BUFFER_CONTAIN(buf, sz, s) (memmem(buf, sz, s, sizeof(s) - 1) != nullptr) -#define CHECKED_MATCH(s) (len >= (sizeof(s) - 1) && BUFFER_MATCH(buf, s)) #define BOOT_MAGIC "ANDROID!" #define VENDOR_BOOT_MAGIC "VNDRBOOT" -#define CHROMEOS_MAGIC "CHROMEOS" -#define GZIP1_MAGIC "\x1f\x8b" -#define GZIP2_MAGIC "\x1f\x9e" -#define LZOP_MAGIC "\x89""LZO" -#define XZ_MAGIC "\xfd""7zXZ" -#define BZIP_MAGIC "BZh" -#define LZ4_LEG_MAGIC "\x02\x21\x4c\x18" -#define LZ41_MAGIC "\x03\x21\x4c\x18" -#define LZ42_MAGIC "\x04\x22\x4d\x18" -#define MTK_MAGIC "\x88\x16\x88\x58" #define DTB_MAGIC "\xd0\x0d\xfe\xed" #define LG_BUMP_MAGIC "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79" -#define DHTB_MAGIC "\x44\x48\x54\x42\x01\x00\x00\x00" #define SEANDROID_MAGIC "SEANDROIDENFORCE" -#define TEGRABLOB_MAGIC "-SIGNED-BY-SIGNBLOB-" #define NOOKHD_RL_MAGIC "Red Loader" #define NOOKHD_GL_MAGIC "Green Loader" #define NOOKHD_GR_MAGIC "Green Recovery" @@ -47,7 +33,6 @@ #define AMONET_MICROLOADER_SZ 1024 #define AVB_FOOTER_MAGIC "AVBf" #define AVB_MAGIC "AVB0" -#define ZIMAGE_MAGIC "\x18\x28\x6f\x01" enum class FileFormat : uint8_t; @@ -55,8 +40,8 @@ int unpack(Utf8CStr image, bool skip_decomp = false, bool hdr = false); void repack(Utf8CStr src_img, Utf8CStr out_img, bool skip_comp = false); int split_image_dtb(Utf8CStr filename, bool skip_decomp = false); void cleanup(); -FileFormat check_fmt(const void *buf, size_t len); +FileFormat check_fmt(rust::Slice buf) noexcept; -static inline FileFormat check_fmt(rust::Slice bytes) { - return check_fmt(bytes.data(), bytes.size()); +static inline FileFormat check_fmt(const void *buf, size_t len) { + return check_fmt({static_cast(buf), len}); } diff --git a/native/src/boot/payload.rs b/native/src/boot/payload.rs index 676b7cc15..570ca0805 100644 --- a/native/src/boot/payload.rs +++ b/native/src/boot/payload.rs @@ -1,5 +1,5 @@ use crate::compress::get_decoder; -use crate::ffi::check_fmt; +use crate::format::check_fmt; use crate::proto::update_metadata::DeltaArchiveManifest; use crate::proto::update_metadata::mod_InstallOperation::Type; use base::{LoggedError, LoggedResult, ReadSeekExt, ResultExt, WriteExt, error};