//! Bounded product boot configuration and retained physical evidence layout. pub const MAGIC: u64 = 0x5455_524b_5052_4f44; pub const VERSION: u64 = 1; pub const WORDS: usize = 320; pub const BLOCK_WORDS: usize = 32; pub const BLOCK_BASE: usize = 32; pub const D1_MAGIC: u64 = 0x4431_5f4e_4154_4956; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct BootConfig { pub boot_id: u64, pub route: u32, pub stage_mask: u32, } fn word(bytes: &[u8], at: usize) -> Result { Ok(u32::from_be_bytes(bytes.get(at..at.checked_add(4).ok_or("native-fdt-overflow")?) .ok_or("native-fdt-truncated")?.try_into().map_err(|_| "native-fdt-word")?)) } fn aligned(value: usize) -> Result { value.checked_add(3).map(|n| n & !3).ok_or("native-fdt-overflow") } /// Walk the validated FDT again rather than accepting host-written result fields. /// Only root/chosen properties are controls; duplicate controls are rejected. pub fn chosen<'a>(blob: &'a [u8], name: &[u8]) -> Result, &'static str> { if word(blob, 0)? != 0xd00d_feed || blob.len() > 2 * 1024 * 1024 || word(blob, 4)? as usize != blob.len() { return Err("native-fdt-header"); } let start = word(blob, 8)? as usize; let strings = word(blob, 12)? as usize; let strings_end = strings.checked_add(word(blob, 32)? as usize).ok_or("native-fdt-overflow")?; let end = start.checked_add(word(blob, 36)? as usize).ok_or("native-fdt-overflow")?; if start < 40 || end > blob.len() || strings_end > blob.len() || strings < end { return Err("native-fdt-ranges"); } let mut at = start; let mut depth = 0usize; let mut chosen_depth = None; let mut chosen_seen = false; let mut found = None; let mut tokens = 0usize; loop { if at.checked_add(4).is_none_or(|n| n > end) || tokens > 8192 { return Err("native-fdt-token-limit"); } tokens += 1; let token = word(blob, at)?; at += 4; match token { 1 => { let zero = blob.get(at..end).ok_or("native-fdt-node")?.iter().position(|&b| b == 0).ok_or("native-fdt-node-name")?; depth += 1; if depth > 16 { return Err("native-fdt-depth"); } if depth == 2 && &blob[at..at + zero] == b"chosen" { if chosen_seen { return Err("native-fdt-duplicate-chosen"); } chosen_seen = true; chosen_depth = Some(depth); } at = aligned(at.checked_add(zero + 1).ok_or("native-fdt-overflow")?)?; } 2 => { if depth == 0 { return Err("native-fdt-depth"); } if chosen_depth == Some(depth) { chosen_depth = None; } depth -= 1; } 3 => { if at.checked_add(8).is_none_or(|n| n > end) { return Err("native-fdt-property"); } let size = word(blob, at)? as usize; let offset = word(blob, at + 4)? as usize; at += 8; let finish = at.checked_add(size).ok_or("native-fdt-overflow")?; if finish > end { return Err("native-fdt-property"); } let key = strings.checked_add(offset).ok_or("native-fdt-overflow")?; let rest = blob.get(key..strings_end).ok_or("native-fdt-property-name")?; let zero = rest.iter().position(|&b| b == 0).ok_or("native-fdt-property-name")?; if depth == 2 && chosen_depth == Some(2) && &rest[..zero] == name { if found.is_some() { return Err("native-fdt-duplicate-control"); } found = Some(&blob[at..finish]); } at = aligned(finish)?; } 4 => {} 9 if depth == 0 => return Ok(found), _ => return Err("native-fdt-token"), } } } impl BootConfig { pub fn parse(blob: &[u8]) -> Result { if chosen(blob, b"turkos,feature-experiment")?.is_some() { return Err("native-product-experiment-selector"); } let id = chosen(blob, b"turkos,product-boot-id")?.ok_or("native-product-boot-id-missing")?; let route = chosen(blob, b"turkos,product-route")?.ok_or("native-product-route-missing")?; let stages = chosen(blob, b"turkos,product-stage-mask")?.ok_or("native-product-stages-missing")?; let config = Self { boot_id: u64::from_be_bytes(id.try_into().map_err(|_| "native-product-boot-id-width")?), route: u32::from_be_bytes(route.try_into().map_err(|_| "native-product-route-width")?), stage_mask: u32::from_be_bytes(stages.try_into().map_err(|_| "native-product-stages-width")?), }; if config.boot_id == 0 || !matches!(config.route, 1 | 2) || config.stage_mask & 1 == 0 || config.stage_mask & !255 != 0 { return Err("native-product-config"); } Ok(config) } } #[cfg(test)] mod tests { use super::*; fn fixture(properties: &[(&[u8], &[u8])]) -> std::vec::Vec { let mut strings = std::vec::Vec::new(); let mut structure = std::vec::Vec::new(); structure.extend(1u32.to_be_bytes()); structure.extend([0;4]); structure.extend(1u32.to_be_bytes()); structure.extend(b"chosen\0\0"); for (key, value) in properties { structure.extend(3u32.to_be_bytes()); structure.extend((value.len() as u32).to_be_bytes()); structure.extend((strings.len() as u32).to_be_bytes()); strings.extend(*key); strings.push(0); structure.extend(*value); while structure.len() & 3 != 0 { structure.push(0); } } for token in [2u32, 2, 9] { structure.extend(token.to_be_bytes()); } let start=56; let off_strings=start+structure.len(); let total=off_strings+strings.len(); let mut blob=std::vec![0;56]; for (offset,value) in [(0,0xd00d_feed),(4,total as u32),(8,start as u32),(12,off_strings as u32),(16,40),(20,17),(24,16),(32,strings.len() as u32),(36,structure.len() as u32)] { blob[offset..offset+4].copy_from_slice(&value.to_be_bytes()); } blob.extend(structure); blob.extend(strings); blob } fn config(extra: &[(&[u8], &[u8])]) -> std::vec::Vec { let mut p:std::vec::Vec<(&[u8], &[u8])>=std::vec![(b"turkos,product-boot-id",b"\0\0\0\0\0\0\0\x01"),(b"turkos,product-route",b"\0\0\0\x01"),(b"turkos,product-stage-mask",b"\0\0\0\x01")];p.extend_from_slice(extra);fixture(&p) } #[test] fn real_configuration() { assert_eq!(BootConfig::parse(&config(&[])),Ok(BootConfig{boot_id:1,route:1,stage_mask:1})); } #[test] fn experiment_even_zero_rejected() { assert_eq!(BootConfig::parse(&config(&[(b"turkos,feature-experiment",&[0;4])])),Err("native-product-experiment-selector")); } #[test] fn duplicate_id_rejected() { assert!(BootConfig::parse(&config(&[(b"turkos,product-boot-id",&[1;8])])).is_err()); } #[test] fn malformed_property_rejected() { let mut b=config(&[]);b.truncate(b.len()-1);assert!(BootConfig::parse(&b).is_err()); } #[test] fn absent_controls_rejected() { assert!(BootConfig::parse(&fixture(&[])).is_err()); } #[test] fn all_prefix_truncations_rejected() { let b=config(&[]);for n in 0..b.len(){assert!(BootConfig::parse(&b[..n]).is_err());} } }