//! 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; pub const MAX_SERVICE_SECONDS: u32 = 300; pub const MAX_SERVICE_WORK_ROUNDS: u32 = 4096; pub const SERVICE_MAX_ROUNDS_PER_SECOND: u64 = 20; pub const SERVICE_MIN_WORK_RATE_LIMIT: u32 = 10; pub const SERVICE_WORK_GRACE_SECONDS: u32 = 5; // These are requests and measured dispatcher observations, not product acceptance. pub const SERVICE_SECONDS: usize = 22; pub const SERVICE_MIN_ROUNDS: usize = 23; pub const SERVICE_START: usize = 24; pub const SERVICE_FINISH: usize = 25; pub const SERVICE_COMPLETED_ROUNDS: usize = 26; pub const SERVICE_DEADLINE: usize = 27; pub const SERVICE_STATE: usize = 28; pub const SERVICE_INTERVAL: usize = 29; pub const SERVICE_RECORD: usize = 30; pub const SERVICE_RECORD_WORDS: usize = 31; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct BootConfig { pub boot_id: u64, pub route: u32, pub stage_mask: u32, /// Zero selects the historical bounded stage path. Nonzero selects the /// continuing D1/D2/D3 dispatcher and requires an initial host capture. pub service_seconds: u32, /// Minimum complete INIT+shell dispatcher rounds, in addition to duration. pub service_work_rounds: u32, /// Explicit request for D2 resource failure and hardware lease diagnostics. /// Normal startup does not acquire their extra frames or virtio devices. pub d2_diagnostics: bool, } pub fn service_deadline(start: u64, frequency: u64, seconds: u32) -> Result { if frequency == 0 || seconds == 0 || seconds > MAX_SERVICE_SECONDS { return Err("native-product-service-clock"); } let ticks = frequency.checked_mul(u64::from(seconds)).ok_or("native-product-service-clock-overflow")?; start.checked_add(ticks).ok_or("native-product-service-clock-overflow") } pub fn service_complete(now: u64, deadline: u64, completed: u64, minimum: u32) -> bool { now >= deadline && completed >= u64::from(minimum) } /// Round up so the cadence never requests more than twenty real rounds/second, /// including hardware whose timer frequency is not divisible by twenty. pub fn service_interval(frequency: u64) -> Result { if frequency == 0 { return Err("native-product-service-clock"); } Ok(frequency / SERVICE_MAX_ROUNDS_PER_SECOND + u64::from(frequency % SERVICE_MAX_ROUNDS_PER_SECOND != 0)) } pub fn service_hard_deadline(start: u64, frequency: u64, seconds: u32) -> Result { let deadline = service_deadline(start, frequency, seconds)?; let grace = frequency.checked_mul(u64::from(SERVICE_WORK_GRACE_SECONDS)).ok_or("native-product-service-clock-overflow")?; deadline.checked_add(grace).ok_or("native-product-service-clock-overflow") } 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 service_seconds = chosen(blob, b"turkos,product-service-seconds")?; let service_work_rounds = chosen(blob, b"turkos,product-service-work-rounds")?; let d2_diagnostics = match chosen(blob, b"turkos,product-d2-diagnostics")? { None => false, Some(bytes) => match u32::from_be_bytes(bytes.try_into().map_err(|_| "native-product-d2-diagnostics-width")?) { 0 => false, 1 => true, _ => return Err("native-product-d2-diagnostics-value"), }, }; 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")?), service_seconds: match service_seconds { Some(bytes) => u32::from_be_bytes(bytes.try_into().map_err(|_| "native-product-service-seconds-width")?), None => 0, }, service_work_rounds: match service_work_rounds { Some(bytes) => u32::from_be_bytes(bytes.try_into().map_err(|_| "native-product-service-work-rounds-width")?), None => 1, }, d2_diagnostics, }; 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"); } if config.service_seconds > MAX_SERVICE_SECONDS || config.service_work_rounds == 0 || config.service_work_rounds > MAX_SERVICE_WORK_ROUNDS || config.service_seconds != 0 && config.stage_mask != 7 || config.service_seconds != 0 && config.service_work_rounds > config.service_seconds * SERVICE_MIN_WORK_RATE_LIMIT { return Err("native-product-service-config"); } if config.d2_diagnostics && config.stage_mask & 2 == 0 { return Err("native-product-d2-diagnostics-stages"); } 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,service_seconds:0,service_work_rounds:1,d2_diagnostics:false})); } #[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());} } #[test] fn invalid_identity_route_and_stage_masks_rejected() { for (id,route,mask) in [(0u64,1u32,1u32),(1,0,1),(1,3,1),(1,1,0),(1,1,2),(1,1,257)] { let id=id.to_be_bytes();let route=route.to_be_bytes();let mask=mask.to_be_bytes(); let b=fixture(&[(b"turkos,product-boot-id",&id),(b"turkos,product-route",&route),(b"turkos,product-stage-mask",&mask)]); assert!(BootConfig::parse(&b).is_err()); } } #[test] fn a_second_even_empty_chosen_node_is_rejected() { let mut b=config(&[]);let end=word(&b,8).unwrap() as usize+word(&b,36).unwrap() as usize; let mut node=std::vec::Vec::new();node.extend(1u32.to_be_bytes());node.extend(b"chosen\0\0");node.extend(2u32.to_be_bytes()); let extra=node.len() as u32;b.splice(end-8..end-8,node); for offset in [4usize,12,36] {let value=word(&b,offset).unwrap()+extra;b[offset..offset+4].copy_from_slice(&value.to_be_bytes());} assert_eq!(BootConfig::parse(&b),Err("native-fdt-duplicate-chosen")); } fn service_config(mask: u32, extra: &[(&[u8], &[u8])]) -> std::vec::Vec { let mask = mask.to_be_bytes(); 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", &mask), ]; p.extend_from_slice(extra); fixture(&p) } #[test] fn service_defaults_and_bounded_minimum_work() { for (seconds, rounds) in [(1u32, 1u32), (MAX_SERVICE_SECONDS, MAX_SERVICE_SECONDS * SERVICE_MIN_WORK_RATE_LIMIT)] { let seconds = seconds.to_be_bytes(); let rounds = rounds.to_be_bytes(); let parsed = BootConfig::parse(&service_config(7, &[ (b"turkos,product-service-seconds", &seconds), (b"turkos,product-service-work-rounds", &rounds), ])).unwrap(); assert_eq!(parsed.service_seconds, u32::from_be_bytes(seconds)); assert_eq!(parsed.service_work_rounds, u32::from_be_bytes(rounds)); } assert_eq!(BootConfig::parse(&service_config(7, &[ (b"turkos,product-service-seconds", &1u32.to_be_bytes()), ])).unwrap().service_work_rounds, 1); // Explicit zero preserves the original stage selection even outside mask7. assert_eq!(BootConfig::parse(&service_config(255, &[ (b"turkos,product-service-seconds", &0u32.to_be_bytes()), ])).unwrap().service_seconds, 0); } #[test] fn service_rejects_duplicate_width_bounds_and_incompatible_stages() { for key in [b"turkos,product-service-seconds".as_slice(), b"turkos,product-service-work-rounds".as_slice()] { let one = 1u32.to_be_bytes(); assert_eq!(BootConfig::parse(&service_config(7, &[(key, &one), (key, &one)])), Err("native-fdt-duplicate-control")); for bytes in [&[][..], &[1][..], &[0; 8][..]] { assert!(BootConfig::parse(&service_config(7, &[(key, bytes)])).is_err()); } } for seconds in [MAX_SERVICE_SECONDS + 1, u32::MAX] { assert_eq!(BootConfig::parse(&service_config(7, &[(b"turkos,product-service-seconds", &seconds.to_be_bytes())])), Err("native-product-service-config")); } for rounds in [0u32, MAX_SERVICE_WORK_ROUNDS + 1, u32::MAX] { assert_eq!(BootConfig::parse(&service_config(7, &[(b"turkos,product-service-work-rounds", &rounds.to_be_bytes())])), Err("native-product-service-config")); } for mask in [1u32, 3, 5, 15, 255] { assert_eq!(BootConfig::parse(&service_config(mask, &[(b"turkos,product-service-seconds", &1u32.to_be_bytes())])), Err("native-product-service-config")); } assert_eq!(BootConfig::parse(&service_config(7, &[ (b"turkos,product-service-seconds", &1u32.to_be_bytes()), (b"turkos,product-service-work-rounds", &11u32.to_be_bytes()), ])), Err("native-product-service-config")); assert_eq!(BootConfig::parse(&service_config(7, &[ (b"turkos,product-service-seconds", &5u32.to_be_bytes()), (b"turkos,product-service-work-rounds", &50u32.to_be_bytes()), ])).unwrap().service_work_rounds, 50); } #[test] fn service_completion_requires_measured_time_and_work() { assert_eq!(service_deadline(100, 24_000_000, 5), Ok(120_000_100)); for (start, freq, seconds) in [(0, 0, 1), (0, 1, 0), (0, 1, MAX_SERVICE_SECONDS + 1), (0, u64::MAX, 2), (u64::MAX, 1, 1)] { assert!(service_deadline(start, freq, seconds).is_err()); } assert!(!service_complete(999, 1000, 50, 1)); assert!(!service_complete(1000, 1000, 4, 5)); assert!(service_complete(1000, 1000, 5, 5)); assert!(service_complete(1001, 1000, 6, 5)); } #[test] fn measured_cadence_rounds_up_and_work_timeout_is_bounded() { assert!(service_interval(0).is_err()); for (frequency, ticks) in [(1, 1), (19, 1), (20, 1), (21, 2), (24_000_000, 1_200_000), (u64::MAX, u64::MAX / 20 + 1)] { assert_eq!(service_interval(frequency), Ok(ticks)); } assert_eq!(service_hard_deadline(100, 24_000_000, 5), Ok(240_000_100)); assert!(service_hard_deadline(u64::MAX - 10, 1, 10).is_err()); assert!(service_hard_deadline(0, u64::MAX / 2, 1).is_err()); } #[test] fn d2_diagnostics_are_explicit_boolean_and_require_the_stage() { let key = b"turkos,product-d2-diagnostics".as_slice(); assert!(!BootConfig::parse(&service_config(7, &[])).unwrap().d2_diagnostics); for mask in [3u32, 7, 255] { assert!(BootConfig::parse(&service_config(mask, &[(key, &1u32.to_be_bytes())])).unwrap().d2_diagnostics); } assert!(!BootConfig::parse(&service_config(1, &[(key, &0u32.to_be_bytes())])).unwrap().d2_diagnostics); for mask in [1u32, 5, 253] { assert_eq!(BootConfig::parse(&service_config(mask, &[(key, &1u32.to_be_bytes())])), Err("native-product-d2-diagnostics-stages")); } for value in [2u32, u32::MAX] { assert_eq!(BootConfig::parse(&service_config(7, &[(key, &value.to_be_bytes())])), Err("native-product-d2-diagnostics-value")); } for bytes in [&[][..], &[1][..], &[0; 8][..]] { assert_eq!(BootConfig::parse(&service_config(7, &[(key, bytes)])), Err("native-product-d2-diagnostics-width")); } assert_eq!(BootConfig::parse(&service_config(7, &[(key, &1u32.to_be_bytes()), (key, &0u32.to_be_bytes())])), Err("native-fdt-duplicate-control")); } }