diff --git a/CHANGELOG.md b/CHANGELOG.md index b44e632..794df05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,10 @@ is on main, the release is published with that section as its notes (AGENTS.md, tree now goes decrypted where its key is known, which an iBEC started from kDFU needs; a DFU mode that turns out to be the untouched boot ROM is said so instead of a timeout. +- Firmware recipes can take the iPhone 8, 8 Plus and X's SmartIO firmware + (`smartio-from-kernelcache@2` with `com.apple.driver.AppleT8015SmartIO`) + and their GPU firmware (`agx-g10p-firmware-from-kernelcache@1`) from the + kernelcache. - Firmware recipes can take the A10 video encoder's firmware from the AppleAVE2 kext in a kernelcache (`ave2-firmware-from-kernelcache@1`), for hardware H.264 and HEVC encoding on the iPhone 7 and 7 Plus and the diff --git a/crates/boards/src/peripheral.rs b/crates/boards/src/peripheral.rs index 7f47ffe..c3c84ab 100644 --- a/crates/boards/src/peripheral.rs +++ b/crates/boards/src/peripheral.rs @@ -59,6 +59,8 @@ enum Step { Ave2, AgxG9p, AgxG9g, + /// A11 GPU firmware, laid out as the A10's. + AgxG10p, /// A9 GPU firmware, the one C0 image the driver knows. AgxG5p, /// A8 GPU firmware and its border colour table; the recipe's hash picks @@ -250,6 +252,9 @@ fn run( Step::AgxG9g => { kernelcache::agx_g9g(&Kernelcache::new(kernel(kernels, first, &data)?)?)?.to_vec() } + Step::AgxG10p => { + kernelcache::agx_g10p(&Kernelcache::new(kernel(kernels, first, &data)?)?)?.to_vec() + } Step::AgxG5p => { let image = kernelcache::agx_g5p(&Kernelcache::new(kernel(kernels, first, &data)?)?)?; @@ -592,6 +597,7 @@ fn parse(raw: &catalog::Step, recipe: &Recipe) -> Result> { "ave2-firmware-from-kernelcache@1" => (Step::Ave2, &[]), "agx-g9p-firmware-from-kernelcache@1" => (Step::AgxG9p, &[]), "agx-g9g-firmware-from-kernelcache@1" => (Step::AgxG9g, &[]), + "agx-g10p-firmware-from-kernelcache@1" => (Step::AgxG10p, &[]), "agx-g5p-ldr-from-kernelcache@1" => (Step::AgxG5p, &[]), "agx-g4p-ldr-from-kernelcache@1" => (Step::AgxG4pLdr, &[]), "agx-g4p-border-from-kernelcache@1" => (Step::AgxG4pBorder, &[]), @@ -712,22 +718,65 @@ mod tests { } #[test] - fn gpu_firmware_step_is_supported_without_parameters() { - let recipe = recipe( - &[("kernelcache", b"kernel")], - vec![output( - "lib/firmware/apple/gfx-t8010.bin", - b"gpu", - vec![step( - "agx-g9p-firmware-from-kernelcache@1", - Some("kernelcache"), - &[], + fn gpu_firmware_steps_are_supported_without_parameters() { + let steps = [ + ("agx-g9p-firmware-from-kernelcache@1", "t8010", Step::AgxG9p), + ( + "agx-g10p-firmware-from-kernelcache@1", + "t8015", + Step::AgxG10p, + ), + ]; + for (name, chip, built) in steps { + let plan_of = |params: &[(&str, &str)]| { + let recipe = recipe( + &[("kernelcache", b"kernel")], + vec![output( + &format!("lib/firmware/apple/gfx-{chip}.bin"), + b"gpu", + vec![step(name, Some("kernelcache"), params)], + )], + ); + Plan::new(&recipe).map(|plan| { + assert!(plan.unsupported.is_empty()); + plan.build[0].1.clone() + }) + }; + assert_eq!(plan_of(&[]).unwrap(), [built]); + assert_eq!(plan_of(&[("x", "1")]).unwrap_err().code, Code::Format); + } + } + + #[test] + fn smartio_step_takes_a_known_kext() { + let plan_of = |kext: &str| { + let recipe = recipe( + &[("kernelcache", b"kernel")], + vec![output( + "lib/firmware/apple/smartio.bin", + b"fw", + vec![step( + "smartio-from-kernelcache@2", + Some("kernelcache"), + &[("kext", kext)], + )], )], - )], + ); + Plan::new(&recipe).map(|plan| plan.build[0].1.clone()) + }; + for kext in kernelcache::SMARTIO_KEXTS { + assert_eq!( + plan_of(kext).unwrap(), + [Step::Smartio { + kext: (*kext).into() + }] + ); + } + assert!(kernelcache::SMARTIO_KEXTS.contains(&"com.apple.driver.AppleT8015SmartIO")); + assert_eq!( + plan_of("com.apple.driver.Other").unwrap_err().code, + Code::Format ); - let plan = Plan::new(&recipe).unwrap(); - assert!(plan.unsupported.is_empty()); - assert_eq!(plan.build[0].1, [Step::AgxG9p]); } #[test] diff --git a/crates/formats/src/kernelcache.rs b/crates/formats/src/kernelcache.rs index fb1c09a..61be5df 100644 --- a/crates/formats/src/kernelcache.rs +++ b/crates/formats/src/kernelcache.rs @@ -10,14 +10,15 @@ use crate::{im4p, img4, lzfse, lzss, Error, Result}; /// Decompressed kernels larger than this are refused. const MAX_KERNEL: usize = 256 << 20; -/// The bundle ids `smartio` accepts: A10, A9, A8 and A8X, A7, A10X. The -/// first is what `smartio-from-kernelcache@1` means. +/// The bundle ids `smartio` accepts: A10, A9, A8 and A8X, A7, A10X, A11. +/// The first is what `smartio-from-kernelcache@1` means. pub const SMARTIO_KEXTS: &[&str] = &[ "com.apple.driver.AppleT8010SmartIO", "com.apple.driver.AppleS8000SmartIO", "com.apple.driver.AppleT7000SmartIO", "com.apple.driver.AppleS5L8960XSmartIO", "com.apple.driver.AppleT8011SmartIO", + "com.apple.driver.AppleT8015SmartIO", ]; /// Decodes a kernelcache IM4P to the ARM64 kernel Mach-O. @@ -189,19 +190,29 @@ pub fn smartio<'a>(kc: &Kernelcache<'a>, bundle: &str) -> Result<&'a [u8]> { /// A10 GPU firmware: a build-variant C string followed by an ARM image in /// AGXFirmwareKextG9PRTBuddy's __TEXT.__const. The recipe pins the image hash. pub fn agx_g9p<'a>(kc: &Kernelcache<'a>) -> Result<&'a [u8]> { - let sections = kc.kext("com.apple.AGXFirmwareKextG9PRTBuddy")?; - let section = kc.section("__TEXT", "__const", §ions)?; - agx_g9p_image(kc.read(section.addr, section.size)?) + agx_rtbuddy(kc, "com.apple.AGXFirmwareKextG9PRTBuddy") } /// A10X uses the G9P image layout in its own RTBuddy bundle. pub fn agx_g9g<'a>(kc: &Kernelcache<'a>) -> Result<&'a [u8]> { - let sections = kc.kext("com.apple.AGXFirmwareKextG9GRTBuddy")?; + agx_rtbuddy(kc, "com.apple.AGXFirmwareKextG9GRTBuddy") +} + +/// A11 GPU firmware: laid out as the A10's, in AGXFirmwareKextG10PRTBuddy's +/// __TEXT.__const. The recipe pins the image hash. +pub fn agx_g10p<'a>(kc: &Kernelcache<'a>) -> Result<&'a [u8]> { + agx_rtbuddy(kc, "com.apple.AGXFirmwareKextG10PRTBuddy") +} + +fn agx_rtbuddy<'a>(kc: &Kernelcache<'a>, bundle: &str) -> Result<&'a [u8]> { + let sections = kc.kext(bundle)?; let section = kc.section("__TEXT", "__const", §ions)?; - agx_g9p_image(kc.read(section.addr, section.size)?) + agx_rtbuddy_image(kc.read(section.addr, section.size)?) } -fn agx_g9p_image(constant: &[u8]) -> Result<&[u8]> { +/// The image behind the build-variant C string (`\0Release\0` on A10 and +/// A11): what the kext's `requestFirmwareBinary` hands to the GPU. +fn agx_rtbuddy_image(constant: &[u8]) -> Result<&[u8]> { let start = constant .iter() .position(|b| *b != 0) @@ -624,15 +635,27 @@ mod tests { let start = constant.len(); constant.extend(vec![0; 4096]); constant[start..start + 4].copy_from_slice(&[0x0e, 0, 0, 0xea]); - assert_eq!(agx_g9p_image(&constant).unwrap(), &constant[start..]); - assert!(agx_g9p_image(&constant[..constant.len() - 1]).is_err()); + assert_eq!(agx_rtbuddy_image(&constant).unwrap(), &constant[start..]); + assert!(agx_rtbuddy_image(&constant[..constant.len() - 1]).is_err()); constant[start + 3] = 0; - assert!(agx_g9p_image(&constant).is_err()); + assert!(agx_rtbuddy_image(&constant).is_err()); for malformed in [b"".as_slice(), b"\0\0", b"Release", b"\x01\0", b"Release\0"] { - assert!(agx_g9p_image(malformed).is_err()); + assert!(agx_rtbuddy_image(malformed).is_err()); } } + #[test] + fn a11_gpu_firmware_is_laid_out_as_the_a10s() { + // AGXFirmwareKextG10PRTBuddy's __TEXT.__const: one NUL, the variant, + // then the image `requestFirmwareBinary` returns, 9 bytes in. + let mut constant = b"\0Release\0".to_vec(); + assert_eq!(constant.len(), 9); + let mut image = vec![0; 0x5d320]; + image[..8].copy_from_slice(&[0x06, 0, 0, 0xea, 0xfe, 0xff, 0xff, 0xea]); + constant.extend(&image); + assert_eq!(agx_rtbuddy_image(&constant).unwrap(), image); + } + /// A META LDR: header, one L1 record (command 0) and its L2 block. fn ldr(fill: &[u8]) -> Vec { let mut v = Vec::new(); diff --git a/crates/formats/tests/steps.rs b/crates/formats/tests/steps.rs index 3d2e196..7ea70b3 100644 --- a/crates/formats/tests/steps.rs +++ b/crates/formats/tests/steps.rs @@ -123,5 +123,5 @@ fn smartio_of_a_kext_merged_into_the_kernel() { // A kext the kernel does not hold, and one the list does not know. assert!(kernelcache::smartio(&cache, "com.apple.driver.AppleT7000SmartIO").is_err()); assert!(kernelcache::smartio(&cache, "com.apple.driver.Other").is_err()); - assert_eq!(kernelcache::SMARTIO_KEXTS.len(), 5); + assert_eq!(kernelcache::SMARTIO_KEXTS.len(), 6); }