Add Version validation
The FF-A spec mandates that bit[31] of a version number must be 0.
Add checks to validate this.
Change-Id: Iee1a6d2112f8245f4c67a2920dcd8a9777283fe7
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
diff --git a/src/lib.rs b/src/lib.rs
index ae66d12..dc965ef 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -203,6 +203,9 @@
pub struct Version(pub u16, pub u16);
impl Version {
+ // The FF-A spec mandates that bit[31] of a version number must be 0
+ const MBZ_BITS: u32 = 1 << 31;
+
/// Returns whether the caller's version (self) is compatible with the callee's version (input
/// parameter)
pub fn is_compatible_to(&self, callee_version: &Version) -> bool {
@@ -210,15 +213,23 @@
}
}
-impl From<u32> for Version {
- fn from(val: u32) -> Self {
- Self((val >> 16) as u16, val as u16)
+impl TryFrom<u32> for Version {
+ type Error = Error;
+
+ fn try_from(val: u32) -> Result<Self, Self::Error> {
+ if (val & Self::MBZ_BITS) != 0 {
+ Err(Error::InvalidVersion(val))
+ } else {
+ Ok(Self((val >> 16) as u16, val as u16))
+ }
}
}
impl From<Version> for u32 {
fn from(v: Version) -> Self {
- ((v.0 as u32) << 16) | v.1 as u32
+ let v_u32 = ((v.0 as u32) << 16) | v.1 as u32;
+ assert!(v_u32 & Version::MBZ_BITS == 0);
+ v_u32
}
}
@@ -705,7 +716,7 @@
interrupt_id: regs[2] as u32,
},
FuncId::Version => Self::Version {
- input_version: (regs[1] as u32).into(),
+ input_version: (regs[1] as u32).try_into()?,
},
FuncId::Features => Self::Features {
feat_id: (regs[1] as u32).into(),