Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 63 additions & 14 deletions crates/boards/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)?)?)?;
Expand Down Expand Up @@ -592,6 +597,7 @@ fn parse(raw: &catalog::Step, recipe: &Recipe) -> Result<Option<Step>> {
"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, &[]),
Expand Down Expand Up @@ -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]
Expand Down
47 changes: 35 additions & 12 deletions crates/formats/src/kernelcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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", &sections)?;
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", &sections)?;
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)
Expand Down Expand Up @@ -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<u8> {
let mut v = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/formats/tests/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading