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
2 changes: 0 additions & 2 deletions arch/arm64/boot/dts/apple/t6000-j316s.dts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@
&aop {
accel {
compatible = "apple,t6000-aop-accel", "apple,aop-accel";
status = "disabled";
};

gyro {
compatible = "apple,t6000-aop-gyro", "apple,aop-gyro";
status = "disabled";
};
};

Expand Down
2 changes: 0 additions & 2 deletions arch/arm64/boot/dts/apple/t8112-j413.dts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,10 @@

accel {
compatible = "apple,t8112-aop-accel", "apple,aop-accel";
status = "disabled";
};

gyro {
compatible = "apple,t8112-aop-gyro", "apple,aop-gyro";
status = "disabled";
};
};

Expand Down
31 changes: 19 additions & 12 deletions drivers/soc/apple/aop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ impl AopData {
fn patch_bootargs(
&self,
aop_mmio: &Mmio<'_, Region<AOP_MMIO_SIZE>>,
patches: &[(u32, u64)],
patches: &[(u32, Option<u64>)],
word_access: bool,
) -> Result<()> {
let offset = aop_mmio.read32(BOOTARGS_OFFSET) as usize;
Expand Down Expand Up @@ -1648,6 +1648,10 @@ impl AopData {
if *k != key {
continue;
}
// `None` keeps the value the firmware image carries.
let Some(v) = v else {
break;
};
if len > mem::size_of::<u64>() {
return Err(EINVAL);
}
Expand Down Expand Up @@ -2562,8 +2566,8 @@ struct AopDriver(Arc<dyn AOP>);

struct AopHwConfig {
ec0p: u64,
/// The `nCal` boot argument.
ncal: u64,
/// The `nCal` boot argument, or `None` to keep the firmware's own value.
ncal: Option<u64>,
alig: u64,
aopt: u64,
/// Complete the firmware's boot arguments before starting it.
Expand All @@ -2588,7 +2592,7 @@ struct AopHwConfig {

const HW_CFG_T8103: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
ncal: Some(0),
aopt: 1,
alig: 128,
patch_bootargs: true,
Expand All @@ -2600,9 +2604,11 @@ const HW_CFG_T8103: AopHwConfig = AopHwConfig {
bootargs_word_access: false,
halt_on_failed_start: false,
};
/// `nCal` keeps the firmware's own value, as on T6040: the J413 image carries 1,
/// and with 0 the firmware never announces its accelerometer and gyroscope.
const HW_CFG_T8112: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
ncal: None,
aopt: 0,
alig: 128,
patch_bootargs: true,
Expand All @@ -2614,9 +2620,10 @@ const HW_CFG_T8112: AopHwConfig = AopHwConfig {
bootargs_word_access: false,
halt_on_failed_start: false,
};
/// `nCal` keeps the firmware's own value, as on T8112.
const HW_CFG_T6000: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
ncal: None,
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2630,7 +2637,7 @@ const HW_CFG_T6000: AopHwConfig = AopHwConfig {
};
const HW_CFG_T6020: AopHwConfig = AopHwConfig {
ec0p: 0x0100_00000000,
ncal: 0,
ncal: Some(0),
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2649,7 +2656,7 @@ const HW_CFG_T6020: AopHwConfig = AopHwConfig {
/// and report interval but never send a report.
const HW_CFG_T6040: AopHwConfig = AopHwConfig {
ec0p: 0x0100_00000000,
ncal: 1,
ncal: Some(1),
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2672,7 +2679,7 @@ const HW_CFG_T8132: AopHwConfig = HW_CFG_T6040;
/// are started if they will.
const HW_CFG_T8140: AopHwConfig = AopHwConfig {
ec0p: 0,
ncal: 0,
ncal: Some(0),
aopt: 0,
alig: 0,
patch_bootargs: false,
Expand Down Expand Up @@ -2737,10 +2744,10 @@ impl platform::Driver for AopDriver {
data.patch_bootargs(
&aop_mmio,
&[
(from_fourcc(b"EC0p"), cfg.ec0p),
(from_fourcc(b"EC0p"), Some(cfg.ec0p)),
(from_fourcc(b"nCal"), cfg.ncal),
(from_fourcc(b"alig"), cfg.alig),
(from_fourcc(b"AOPt"), cfg.aopt),
(from_fourcc(b"alig"), Some(cfg.alig)),
(from_fourcc(b"AOPt"), Some(cfg.aopt)),
],
cfg.bootargs_word_access,
)?;
Expand Down
52 changes: 48 additions & 4 deletions tools/asahi/aop-bootargs-tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only OR MIT
"""Exercise production bootarg patching with synthetic MMIO."""
"""Exercise production bootarg patching and the per-SoC nCal choice with synthetic MMIO."""
import argparse
import os
from pathlib import Path
Expand Down Expand Up @@ -40,7 +40,10 @@
if offset+table.len()<=bytes.len(){bytes[offset..offset+table.len()].copy_from_slice(table);}
Device{bytes:RefCell::new(bytes),writes:RefCell::new(Vec::new()),fail:Cell::new(false)}
}
fn patch(dev:&Device,word:bool)->Result{Aop.patch_bootargs(&Mmio{dev,_p:PhantomData::<Region<AOP_MMIO_SIZE>>},&[(1,0x12345678),(2,0x87654321)],word)}
fn patch(dev:&Device,word:bool)->Result{Aop.patch_bootargs(&Mmio{dev,_p:PhantomData::<Region<AOP_MMIO_SIZE>>},&[(1,Some(0x12345678)),(2,Some(0x87654321))],word)}
fn patch_with(dev:&Device,patches:&[(u32,Option<u64>)])->Result{Aop.patch_bootargs(&Mmio{dev,_p:PhantomData::<Region<AOP_MMIO_SIZE>>},patches,true)}
fn from_fourcc(b:&[u8;4])->u32{u32::from_le_bytes(*b)}
CONFIGS
fn main(){
let table:[u8;24]=[1,0,0,0,4,0,0,0,0,0,0,0,2,0,0,0,4,0,0,0,0,0,0,0];
for offset in 32..36 {for word in [false,true] {
Expand All @@ -56,7 +59,21 @@
let d=fixture(35,&table);d.fail.set(true);assert_eq!(patch(&d,true),Err(EIO),"readback error ignored");
assert_eq!(patch(&fixture(35,&[]),true),Err(EINVAL));assert_eq!(patch(&fixture(35,&[]),false),Ok(()));
assert_eq!(patch(&fixture(120,&table),true),Err(EINVAL));
println!("PASS aligned/unaligned byte and word access, unchanged writes, malformed tables, bounds and failed readback");
// None keeps the firmware value of its key and still patches the others.
let d=fixture(32,&table);let mut expected=d.bytes.borrow().clone();expected[32+8..32+12].copy_from_slice(&0x12345678u32.to_le_bytes());
let fw=0x0badf00du32;d.bytes.borrow_mut()[32+20..32+24].copy_from_slice(&fw.to_le_bytes());expected[32+20..32+24].copy_from_slice(&fw.to_le_bytes());
assert_eq!(patch_with(&d,&[(1,Some(0x12345678)),(2,None)]),Ok(()));assert_eq!(*d.bytes.borrow(),expected,"None overwrote the firmware value");
// Per-SoC nCal: T8112 and T6000 keep the image value, T6040/T8132 write 1, the others write 0.
let ncal=from_fourcc(b"nCal");
for (name,cfg,want) in [("T8103",&HW_CFG_T8103,None),("T8112",&HW_CFG_T8112,Some(1u32)),("T6000",&HW_CFG_T6000,Some(1)),("T6020",&HW_CFG_T6020,None),("T6040",&HW_CFG_T6040,None),("T8132",&HW_CFG_T8132,None),("T8140",&HW_CFG_T8140,None)] {
let expect=match name {"T8112"|"T6000"=>None,"T6040"|"T8132"=>Some(1u64),_=>Some(0u64)};
assert_eq!(cfg.ncal,expect,"{} nCal choice",name);
let mut t=[0u8;12];t[0..4].copy_from_slice(&ncal.to_le_bytes());t[4..8].copy_from_slice(&4u32.to_le_bytes());t[8..12].copy_from_slice(&1u32.to_le_bytes());
let d=fixture(32,&t);assert_eq!(patch_with(&d,&[(ncal,cfg.ncal)]),Ok(()));
let got=u32::from_le_bytes(d.bytes.borrow()[40..44].try_into().unwrap());
let wanted=want.unwrap_or_else(||cfg.ncal.unwrap() as u32);assert_eq!(got,wanted,"{} nCal written",name);
}
println!("PASS aligned/unaligned byte and word access, unchanged writes, malformed tables, bounds, failed readback, None keeps the firmware value, per-SoC nCal");
}
'''

Expand All @@ -72,13 +89,25 @@ def method(source):
return source[start:end]


def configs(source):
start = source.index('struct AopHwConfig {')
end = source.index('kernel::of_device_table!', start)
return source[start:end]


def compile_run(rustc, p, binary, code):
p.write_text(code)
subprocess.run([rustc, '--edition=2021', str(p), '-o', str(binary)], check=True)
return subprocess.run([str(binary)], capture_output=True, text=True)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--source', type=Path, default=Path(__file__).resolve().parents[2] / 'drivers/soc/apple/aop.rs')
args = parser.parse_args()
production = method(args.source.read_text())
macros = ''.join('macro_rules! ' + name + ' {($($t:tt)*)=>{()};}\n' for name in ['dev_info', 'dev_err'] if name + '!' in production)
code = HARNESS.replace('METHOD', production).replace('struct Aop;', macros + 'struct Aop;')
code = HARNESS.replace('METHOD', production).replace('CONFIGS', configs(args.source.read_text())).replace('struct Aop;', macros + 'struct Aop;')
with tempfile.TemporaryDirectory(prefix='aop-bootargs-') as tmp:
p = Path(tmp) / 'test.rs'
binary = p.with_suffix('')
Expand All @@ -92,6 +121,21 @@ def main():
result = subprocess.run([str(binary)], capture_output=True, text=True)
assert result.returncode != 0 and 'readback error ignored' in result.stderr
print('PASS negative control: missing write verification')
keep = 'let Some(v) = v else {'
assert code.count(keep) == 1
rustc = os.environ.get('RUSTC', 'rustc')
bad = code.replace(keep, 'let v = &v.unwrap_or(0); if false {')
result = compile_run(rustc, p, binary, bad)
assert result.returncode != 0 and 'None overwrote the firmware value' in result.stderr, result.stderr
print('PASS negative control: None written as 0')
for soc, val in (('T8112', 'Some(0)'), ('T6040', 'Some(0)'), ('T6000', 'Some(1)')):
start = code.index('const HW_CFG_%s: AopHwConfig' % soc)
at = code.index(' ncal: ', start)
line_end = code.index('\n', at)
bad = code[:at] + ' ncal: %s,' % val + code[line_end:]
result = compile_run(rustc, p, binary, bad)
assert result.returncode != 0 and '%s nCal choice' % soc in result.stderr, (soc, result.stderr)
print('PASS negative control: %s nCal %s' % (soc, val))


if __name__ == '__main__':
Expand Down
Loading