From cfec9606a46b363336c211cd6eb15a59519dc33c Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 17 Oct 2026 09:02:13 -0600 Subject: [PATCH] Require observed MTD geometry before resolving flash target --- crates/alkane-access/src/lib.rs | 94 +++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/crates/alkane-access/src/lib.rs b/crates/alkane-access/src/lib.rs index 79c55be5..5a229476 100644 --- a/crates/alkane-access/src/lib.rs +++ b/crates/alkane-access/src/lib.rs @@ -28,6 +28,58 @@ pub struct MtdPartition { pub name: String, } +/// The partition identity and geometry required by an SDK flash plan. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct MtdRequirement { + pub index: u32, + pub name: String, + pub size_bytes: u64, + pub erase_bytes: u64, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct MtdTarget { + pub device: PathBuf, + pub size_bytes: u64, +} + +#[derive(Debug, Eq, PartialEq)] +pub enum MtdMatchError { + MissingIndex, + AmbiguousIndex, + NameMismatch, + SizeMismatch, + EraseMismatch, +} + +/// Resolve a character device only after comparing the observed partition +/// with the expected identity and geometry. This does not authorize a write. +pub fn resolve_mtd_target( + observed: &[MtdPartition], + expected: &MtdRequirement, +) -> Result { + let mut matches = observed + .iter() + .filter(|entry| entry.index == expected.index); + let entry = matches.next().ok_or(MtdMatchError::MissingIndex)?; + if matches.next().is_some() { + return Err(MtdMatchError::AmbiguousIndex); + } + if entry.name != expected.name { + return Err(MtdMatchError::NameMismatch); + } + if entry.size_bytes != expected.size_bytes { + return Err(MtdMatchError::SizeMismatch); + } + if entry.erase_bytes != expected.erase_bytes { + return Err(MtdMatchError::EraseMismatch); + } + Ok(MtdTarget { + device: PathBuf::from(format!("/dev/mtd{}", entry.index)), + size_bytes: entry.size_bytes, + }) +} + #[derive(Debug, Eq, PartialEq)] pub enum MtdError { InvalidHeader, @@ -329,4 +381,46 @@ mtd0: 00020000 00020000 \"root\"\n"; Err(MtdError::InvalidHeader) ); } + + #[test] + fn flash_target_requires_captured_identity_and_geometry() { + let observed = parse_mtd_table( + b"dev: size erasesize name\n\ +mtd0: 02800000 00020000 \"BOOT.bin-dts-marker-kernel\"\n\ +mtd1: 02000000 00020000 \"ramfs\"\n", + ) + .unwrap(); + let mut required = MtdRequirement { + index: 0, + name: "BOOT.bin-dts-marker-kernel".to_owned(), + size_bytes: 0x0280_0000, + erase_bytes: 0x0002_0000, + }; + assert_eq!( + resolve_mtd_target(&observed, &required).unwrap().device, + Path::new("/dev/mtd0") + ); + required.name = "ramfs".to_owned(); + assert_eq!( + resolve_mtd_target(&observed, &required), + Err(MtdMatchError::NameMismatch) + ); + required.name = observed[0].name.clone(); + required.size_bytes -= 1; + assert_eq!( + resolve_mtd_target(&observed, &required), + Err(MtdMatchError::SizeMismatch) + ); + required.size_bytes = observed[0].size_bytes; + required.erase_bytes *= 2; + assert_eq!( + resolve_mtd_target(&observed, &required), + Err(MtdMatchError::EraseMismatch) + ); + required.index = 2; + assert_eq!( + resolve_mtd_target(&observed, &required), + Err(MtdMatchError::MissingIndex) + ); + } }