Require observed MTD geometry before resolving flash target
This commit is contained in:
@@ -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<MtdTarget, MtdMatchError> {
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user