From 6a466a611f1a3b1948b8ff45e35a970649dbcc07 Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 15 Oct 2026 22:07:41 -0600 Subject: [PATCH] Reject invalid BM1362 geometry and baud requests --- src/bm1362.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/bm1362.rs b/src/bm1362.rs index 536b081..c802c40 100644 --- a/src/bm1362.rs +++ b/src/bm1362.rs @@ -11,8 +11,8 @@ pub const CHIP_ADDRESS_STEP: u8 = 2; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct ModelGeometry { - pub chips_per_chain: u8, - pub ticket_mask: u8, + chips_per_chain: u8, + ticket_mask: u8, } pub const S19J_PRO: ModelGeometry = ModelGeometry { @@ -29,6 +29,24 @@ pub const S19_88: ModelGeometry = ModelGeometry { }; impl ModelGeometry { + pub const fn new(chips_per_chain: u8, ticket_mask: u8) -> Option { + if chips_per_chain == 0 || chips_per_chain > 128 { + return None; + } + Some(Self { + chips_per_chain, + ticket_mask, + }) + } + + pub const fn chips_per_chain(self) -> u8 { + self.chips_per_chain + } + + pub const fn ticket_mask(self) -> u8 { + self.ticket_mask + } + /// Addresses for an observed two-address-step enumeration walk. pub fn chip_addresses(self) -> impl Iterator { (0..self.chips_per_chain).map(|index| index * CHIP_ADDRESS_STEP) @@ -105,13 +123,10 @@ pub fn encode_job(job: &Job) -> [u8; JOB_BYTES] { /// BT8D for a 25 MHz input clock with BCLK_SEL=0. A model must still choose /// the actual target baud and confirm switching on silicon. pub fn bt8d_for_baud(baud: u32) -> Option { - if baud == 0 { + if baud == 0 || baud > 3_125_000 { return None; } let divider = 25_000_000_u64 / (8 * u64::from(baud)); - if divider == 0 { - return Some(0); - } u8::try_from(divider - 1).ok() } @@ -141,6 +156,12 @@ mod tests { #[test] fn model_address_limits() { + assert_eq!(ModelGeometry::new(0, 0), None); + assert_eq!(ModelGeometry::new(129, 0), None); + assert_eq!( + ModelGeometry::new(128, 0).unwrap().chip_addresses().last(), + Some(0xfe) + ); assert_eq!(S19J_PRO.chip_addresses().last(), Some(0xfa)); assert_eq!(S19J.chip_addresses().last(), Some(0xd6)); assert_eq!(S19_88.chip_addresses().last(), Some(0xae)); @@ -187,6 +208,7 @@ mod tests { assert_eq!(bt8d_for_baud(3_000_000), Some(0)); assert_eq!(bt8d_for_baud(115_200), Some(26)); assert_eq!(bt8d_for_baud(1), None); - assert_eq!(bt8d_for_baud(u32::MAX), Some(0)); + assert_eq!(bt8d_for_baud(3_125_001), None); + assert_eq!(bt8d_for_baud(u32::MAX), None); } }