git clone https://garage44.eu/jeroen/pinnacle.git.. /wifi/src/iwl/tests.rs
//! The firmware container, the file a chip needs, the structures the chip
//! reads and what it answers: on bytes made here (always), and on Intel's
//! own files where `cargo xtask firmware` has fetched them.
extern crate std;
use super::*;
use alloc::vec;
use std::println;
fn tlv(kind: u32, data: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
out.extend_from_slice(&kind.to_le_bytes());
out.extend_from_slice(&(data.len() as u32).to_le_bytes());
out.extend_from_slice(data);
while out.len() % 4 != 0 {
out.push(0);
}
out
}
fn section(offset: u32, data: &[u8]) -> Vec<u8> {
let mut out = offset.to_le_bytes().to_vec();
out.extend_from_slice(data);
tlv(tlv::SEC_RT, &out)
}
fn header(name: &str) -> Vec<u8> {
let mut out = vec![0u8; 88];
out[4..8].copy_from_slice(&MAGIC.to_le_bytes());
out[8..8 + name.len()].copy_from_slice(name.as_bytes());
out[72..76].copy_from_slice(&0x5900_0000u32.to_le_bytes());
out[76..80].copy_from_slice(&42u32.to_le_bytes());
out
}
/// A file as Intel's are laid out: two LMAC sections, a separator, a UMAC
/// one, the paging separator, a paged one; and the TLVs around them.
fn synthetic() -> Vec<u8> {
let mut f = header("release/test:synthetic");
f.extend(tlv(
tlv::FW_VERSION,
&[89, 0, 0, 0, 0xb2, 0x23, 0x70, 0xa6, 0, 0, 0, 0],
));
f.extend(tlv(tlv::NUM_OF_CPU, &2u32.to_le_bytes()));
f.extend(tlv(tlv::PHY_SKU, &0x0033_0018u32.to_le_bytes()));
f.extend(tlv(tlv::API_CHANGES_SET, &[0, 0, 0, 0, 1, 0, 0, 0]));
f.extend(tlv(tlv::ENABLED_CAPABILITIES, &[1, 0, 0, 0, 0xff, 0, 0, 0]));
f.extend(tlv(0x0100_0005, &[9; 7]));
f.extend(section(0x0044_0000, &[1; 10]));
f.extend(section(0x0080_0000, &[2; 8]));
f.extend(section(CPU1_CPU2_SEPARATOR, &[]));
f.extend(section(0x8044_0000, &[3; 12]));
f.extend(section(PAGING_SEPARATOR, &[]));
f.extend(section(0, &[4; 4096]));
f.extend(tlv(tlv::SEC_INIT, &[0, 0, 0, 0, 5, 5]));
f.extend(tlv(tlv::PAGING, &8192u32.to_le_bytes()));
// The last TLV without its padding, as a file may end.
let mut iml = tlv(tlv::IML, &[7, 7, 7]);
iml.truncate(8 + 3);
f.extend(iml);
f
}
#[test]
fn a_synthetic_file_reads_whole() {
let bytes = synthetic();
let u = parse(&bytes).unwrap();
assert_eq!(u.human_readable, "release/test:synthetic");
assert_eq!(u.build, 42);
assert_eq!(u.version(), "89.a67023b2.0");
assert_eq!(u.cpus, Some(2));
assert_eq!(u.phy_sku, Some(0x0033_0018));
assert_eq!(u.api, vec![(0, 1)]);
assert_eq!(u.capa, vec![(1, 0xff)]);
assert_eq!(u.paging_mem_size, Some(8192));
assert_eq!(u.iml, Some(&[7u8, 7, 7][..]));
assert_eq!(u.other, 1);
assert_eq!(u.regular.sections.len(), 6);
assert_eq!(
u.init.sections,
vec![Section {
offset: 0,
data: &[5, 5]
}]
);
assert_eq!(u.regular.bytes(), 10 + 8 + 12 + 4096);
let layout = u.regular.layout().unwrap();
assert_eq!(
layout.lmac.iter().map(|s| s.offset).collect::<Vec<_>>(),
[0x0044_0000, 0x0080_0000]
);
assert_eq!(layout.umac.iter().map(|s| s.data.len()).collect::<Vec<_>>(), [12]);
assert_eq!(layout.paging.len(), 1);
assert_eq!(layout.paging[0].data.len(), 4096);
}
#[test]
fn a_broken_file_says_where() {
let good = synthetic();
assert_eq!(parse(&good[..40]).unwrap_err(), Error::TooShort(40));
let mut old = good.clone();
old[0] = 1;
assert_eq!(parse(&old).unwrap_err(), Error::NotTlv);
let mut magic = good.clone();
magic[4] = b'X';
assert!(matches!(parse(&magic).unwrap_err(), Error::Magic(_)));
// The first TLV (at 88) claims more than there is.
let mut long = good.clone();
long[92..96].copy_from_slice(&0x00ff_ffffu32.to_le_bytes());
assert_eq!(
parse(&long).unwrap_err(),
Error::Overrun {
at: 88,
tlv: tlv::FW_VERSION,
len: 0x00ff_ffff
}
);
let mut f = header("x");
f.extend(tlv(tlv::FW_VERSION, &[1, 2, 3, 4]));
assert_eq!(
parse(&f).unwrap_err(),
Error::Length {
tlv: tlv::FW_VERSION,
len: 4
}
);
let mut f = header("x");
f.extend(tlv(tlv::PAGING, &1000u32.to_le_bytes()));
assert_eq!(parse(&f).unwrap_err(), Error::Paging(1000));
let f = header("nothing");
assert_eq!(parse(&f).unwrap_err(), Error::NoImage);
}
#[test]
fn each_chip_gets_the_file_linux_names() {
// CSR_HW_REV: type in bits 4-15, step in 0-3; CSR_HW_RF_ID: type in
// 12-23, step in 8-11, CDB bit 28.
let chip = |mac: u16, step: u32, rf: u16, rf_step: u32| {
Chip::new(u32::from(mac) << 4 | step, u32::from(rf) << 12 | rf_step << 8)
};
let name = |c: Chip| firmware_name(&c).map(|(u, p)| (u, p.unwrap_or_default()));
assert_eq!(
name(chip(mac::QU, 1, rf::HR2, 1)).unwrap().0,
"iwlwifi-Qu-b0-hr-b0-77.ucode"
);
assert_eq!(
name(chip(mac::QU, 2, rf::HR2, 1)).unwrap().0,
"iwlwifi-Qu-c0-hr-b0-77.ucode"
);
assert_eq!(
name(chip(mac::QU, 2, rf::HR1, 0)).unwrap().0,
"iwlwifi-Qu-c0-hr-b0-77.ucode"
);
assert_eq!(
name(chip(mac::QUZ, 1, rf::HR2, 1)).unwrap().0,
"iwlwifi-QuZ-a0-hr-b0-77.ucode"
);
assert_eq!(
name(chip(mac::SO, 0, rf::GF, 0)).unwrap(),
(
"iwlwifi-so-a0-gf-a0-89.ucode".into(),
"iwlwifi-so-a0-gf-a0.pnvm".into()
)
);
assert_eq!(
name(chip(mac::SOF, 2, rf::GF, 1)).unwrap().0,
"iwlwifi-so-a0-gf-a0-89.ucode"
);
assert_eq!(
name(chip(mac::TY, 0, rf::GF, 0)).unwrap(),
(
"iwlwifi-ty-a0-gf-a0-89.ucode".into(),
"iwlwifi-ty-a0-gf-a0.pnvm".into()
)
);
assert_eq!(
name(chip(mac::MA, 1, rf::GF, 0)).unwrap().0,
"iwlwifi-ma-b0-gf-a0-89.ucode"
);
// What is not carried says why.
assert_eq!(name(chip(mac::MA, 0, rf::GF, 0)), Err(NoFirmware::Step(0)));
assert_eq!(name(chip(mac::QU, 0, rf::HR2, 1)), Err(NoFirmware::Step(0)));
assert_eq!(name(chip(mac::SO, 0, rf::FM, 0)), Err(NoFirmware::Rf(rf::FM)));
assert_eq!(name(chip(mac::QU, 1, rf::JF2, 1)), Err(NoFirmware::Rf(rf::JF2)));
assert_eq!(name(chip(0x46, 0, rf::FM, 0)), Err(NoFirmware::Mac(0x46)));
let cdb = Chip::new(u32::from(mac::SO) << 4, u32::from(rf::GF) << 12 | 1 << 28);
assert_eq!(firmware_name(&cdb), Err(NoFirmware::Rf(rf::GF)));
// And every file named is one the description carries.
let description = include_str!("../../../firmware/intel-wifi.txt");
for c in [
chip(mac::QU, 1, rf::HR2, 1),
chip(mac::QU, 2, rf::HR2, 1),
chip(mac::QUZ, 0, rf::HR2, 1),
chip(mac::SO, 0, rf::GF, 0),
chip(mac::TY, 0, rf::GF, 0),
chip(mac::MA, 1, rf::GF, 0),
] {
let (ucode, pnvm) = firmware_name(&c).unwrap();
for file in core::iter::once(ucode).chain(pnvm) {
assert!(
description.contains(&alloc::format!("file: {file}\n")),
"{file} not described"
);
}
}
}
#[test]
fn a_blank_otp_reads_the_radio_from_its_crf() {
// GF, step 1: CRF type 0x410, step in bits 24-27.
assert_eq!(
rf_id_from_crf(0x0100_0410, 0),
Some(u32::from(rf::GF) << 12 | 1 << 8)
);
assert_eq!(
rf_id_from_crf(0x0000_0504, 0x10),
Some((u32::from(rf::HR2) << 12) + (1 << 28))
);
assert_eq!(rf_id_from_crf(0x0000_0123, 0), None);
let chip = Chip::new(u32::from(mac::SO) << 4, rf_id_from_crf(0x0000_0410, 0).unwrap());
assert_eq!((chip.rf_type, chip.cdb), (rf::GF, false));
}
#[test]
fn the_context_info_is_where_the_chip_reads_it() {
let dram = DramMap {
lmac: vec![0x1000, 0x2000],
umac: vec![0x3000],
paging: vec![0x4000],
};
let b = context_info(&ContextInfo {
mac_id: 0x0351,
rx_log: 9,
free_rbd: 0xa000,
used_rbd: 0xb000,
status: 0xc000,
cmd_queue: 0xd000,
cmd_log: 5,
dram: &dram,
});
let at64 = |at: usize| u64::from_le_bytes(b[at..at + 8].try_into().unwrap());
assert_eq!(u16::from_le_bytes([b[0], b[1]]), 0x0351);
assert_eq!(u16::from_le_bytes([b[4], b[5]]), 448);
// Long TFDs, 512 RBDs (2^9 in bits 4-7), 4 KiB buffers (4 in bits 9-12).
assert_eq!(
u32::from_le_bytes(b[8..12].try_into().unwrap()),
0x100 | 9 << 4 | 4 << 9
);
assert_eq!((at64(24), at64(32), at64(40)), (0xa000, 0xb000, 0xc000));
assert_eq!((at64(48), b[56]), (0xd000, 2));
// UMAC's map first, then LMAC's, then the paged image's.
assert_eq!(at64(192), 0x3000);
assert_eq!((at64(192 + 512), at64(192 + 520)), (0x1000, 0x2000));
assert_eq!(at64(192 + 1024), 0x4000);
assert!(b[1728..].iter().all(|&x| x == 0));
let s = prph_scratch(0x0370, 0xe000, &dram);
assert_eq!(u16::from_le_bytes([s[4], s[5]]), 431);
assert_eq!(
u32::from_le_bytes(s[8..12].try_into().unwrap()),
1 << 16 | 1 << 17 | 0xc_0000
);
assert_eq!(u64::from_le_bytes(s[48..56].try_into().unwrap()), 0xe000);
assert_eq!(u64::from_le_bytes(s[124..132].try_into().unwrap()), 0x3000);
assert_eq!(
u64::from_le_bytes(s[124 + 512..132 + 512].try_into().unwrap()),
0x1000
);
let v = context_info_v2(&ContextInfoV2 {
prph_info: 0x10_0000,
prph_scratch: 0x20_0000,
status: 0x30_0000,
used_rbd: 0x40_0000,
rx_log: 9,
cmd_queue: 0x50_0000,
cmd_log: 7,
});
let v64 = |at: usize| u64::from_le_bytes(v[at..at + 8].try_into().unwrap());
assert_eq!(
(v64(8), v64(16), v64(24), v64(32)),
(0x10_0000, 0x30_0000, 0x10_0800, 0x10_0c00)
);
assert_eq!((v64(52), v64(60)), (0x50_0000, 0x40_0000));
assert_eq!(
(
u16::from_le_bytes([v[68], v[69]]),
u16::from_le_bytes([v[70], v[71]])
),
(4, 9)
);
assert_eq!(v64(88), 0x20_0000);
assert_eq!(u32::from_le_bytes(v[96..100].try_into().unwrap()), 1660);
}
fn alive_packet(payload: &[u8]) -> Vec<u8> {
let mut rb = vec![0u8; 4096];
let len = (payload.len() + 4) as u32;
rb[0..4].copy_from_slice(&len.to_le_bytes());
rb[4] = ALIVE;
rb[5] = 0;
rb[6..8].copy_from_slice(&0x8000u16.to_le_bytes());
rb[8..8 + payload.len()].copy_from_slice(payload);
rb
}
#[test]
fn the_alive_notification_reads_in_both_shapes() {
let mut v3 = vec![0u8; 68];
v3[0..2].copy_from_slice(&0xcafeu16.to_le_bytes());
v3[4..8].copy_from_slice(&77u32.to_le_bytes());
v3[8..12].copy_from_slice(&0x3cda_2a2cu32.to_le_bytes());
v3[20..24].copy_from_slice(&0x0080_0000u32.to_le_bytes());
v3[52..56].copy_from_slice(&77u32.to_le_bytes());
v3[60..64].copy_from_slice(&0x00c0_0000u32.to_le_bytes());
let rb = alive_packet(&v3);
let (cmd, group, _, payload) = rx_packet(&rb).unwrap();
assert_eq!((cmd, group, payload.len()), (ALIVE, 0, 68));
let a = Alive::parse(payload).unwrap();
assert_eq!(a.status, Alive::OK);
assert_eq!(a.lmac, (77, 0x3cda_2a2c));
assert_eq!(a.umac.0, 77);
assert_eq!(
(a.lmac_error_table, a.umac_error_table),
(0x0080_0000, 0x00c0_0000)
);
assert_eq!(a.sku_id, None);
let mut v6 = vec![0u8; 144];
v6[0..2].copy_from_slice(&0xdeadu16.to_le_bytes());
v6[100..104].copy_from_slice(&89u32.to_le_bytes());
v6[116..120].copy_from_slice(&0x11u32.to_le_bytes());
v6[124..128].copy_from_slice(&0x33u32.to_le_bytes());
let a = Alive::parse(&v6).unwrap();
assert_eq!(
(a.status, a.umac.0, a.sku_id),
(0xdead, 89, Some([0x11, 0, 0x33]))
);
assert_eq!(Alive::parse(&[0; 70]), None);
let mut empty = vec![0u8; 64];
empty[0..4].copy_from_slice(&FRAME_INVALID.to_le_bytes());
assert_eq!(rx_packet(&empty), None);
let mut long = alive_packet(&v3);
long[0..4].copy_from_slice(&5000u32.to_le_bytes());
assert_eq!(rx_packet(&long[..4096]), None);
}
#[test]
fn a_pnvm_file_gives_each_sku_its_section() {
let mut f = tlv(tlv::PNVM_VERSION, &[1, 0, 0, 0]);
f.extend(tlv(tlv::PNVM_SKU, &[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]));
f.extend(tlv(tlv::PNVM_VERSION, &0xabcdu32.to_le_bytes()));
f.extend(tlv(tlv::HW_TYPE, &[0x37, 0, 0x0d, 0x01]));
f.extend(tlv(tlv::SEC_RT, &[0xee, 0xee, 0xdd, 0xdd]));
f.extend(tlv(tlv::SEC_RT, &[0, 0, 0, 0, 9, 9, 9]));
f.extend(tlv(tlv::PNVM_SKU, &[4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0]));
f.extend(tlv(tlv::HW_TYPE, &[0x42, 0, 0x0d, 0x01]));
f.extend(tlv(tlv::SEC_RT, &[0, 0, 0, 0, 8]));
let p = Pnvm::parse(&f).unwrap();
assert_eq!(p.skus.len(), 2);
assert_eq!(p.skus[0].version, Some(0xabcd));
assert_eq!(p.skus[0].chunks, vec![&[9u8, 9, 9][..]]);
assert_eq!(
p.for_chip([1, 2, 3], mac::SO, rf::GF).map(|s| s.chunks.len()),
Some(1)
);
assert_eq!(p.for_chip([1, 2, 3], mac::TY, rf::GF), None);
assert_eq!(
p.for_chip([4, 5, 6], mac::TY, rf::GF).unwrap().chunks,
vec![&[8u8][..]]
);
assert_eq!(p.for_chip([7, 7, 7], mac::TY, rf::GF), None);
}
/// Intel's files, where `cargo xtask firmware` put them.
fn fetched() -> Option<Vec<(std::string::String, Vec<u8>)>> {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../target/firmware/intel-wifi");
let Ok(entries) = std::fs::read_dir(&dir) else {
println!(
"iwl: {} is absent (`cargo xtask firmware` fetches Intel's files): skipped",
dir.display()
);
return None;
};
let mut files: Vec<_> = entries
.filter_map(|e| e.ok())
.map(|e| (e.file_name().to_string_lossy().into_owned(), e.path()))
.filter(|(n, _)| n.ends_with(".ucode") || n.ends_with(".pnvm"))
.map(|(n, p)| (n, std::fs::read(p).unwrap()))
.collect();
files.sort();
Some(files)
}
#[test]
fn intels_files_read_as_their_chips_need() {
let Some(files) = fetched() else {
return;
};
for (name, bytes) in files.iter().filter(|(n, _)| n.ends_with(".ucode")) {
let u = parse(bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
let ax210 = !name.contains("-Qu");
let layout = u.regular.layout().unwrap();
assert!(!layout.lmac.is_empty() && !layout.umac.is_empty(), "{name}");
// Each section fits one DRAM chunk of at most 32 KiB.
assert!(
u.regular.sections.iter().all(|s| s.data.len() <= 32 * 1024),
"{name}"
);
assert_eq!(u.iml.is_some(), ax210, "{name}: the image loader");
assert_eq!(u.cpus, Some(2), "{name}");
let api = if ax210 { 89 } else { 77 };
assert!(
u.fw_version.is_some_and(|v| v.0 == api),
"{name}: {:?}",
u.fw_version
);
println!(
"iwl: {name}: {} ({}), {} LMAC + {} UMAC + {} paged sections, {} KiB, IML {} bytes",
u.version(),
u.human_readable,
layout.lmac.len(),
layout.umac.len(),
layout.paging.len(),
u.regular.bytes() / 1024,
u.iml.map_or(0, <[u8]>::len)
);
}
for (name, bytes) in files.iter().filter(|(n, _)| n.ends_with(".pnvm")) {
let p = Pnvm::parse(bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
assert!(!p.skus.is_empty(), "{name}");
assert!(
p.skus
.iter()
.all(|s| !s.chunks.is_empty() && !s.hw_types.is_empty()),
"{name}"
);
let mac = if name.contains("-so-") {
mac::SO
} else if name.contains("-ty-") {
mac::TY
} else {
mac::MA
};
assert!(
p.skus.iter().any(|s| s.fits(mac, rf::GF)),
"{name}: no section for its chip"
);
println!("iwl: {name}: {} SKU sections", p.skus.len());
}
}