sim: Use a trait object for TlvGen
In preparation for adding support for differing manifest types, abstract
the TlvGen with a trait object `ManifestGen`. This will allow alternate
implementations to be made.
Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/src/image.rs b/sim/src/image.rs
index e41d1cd..3404ddd 100644
--- a/sim/src/image.rs
+++ b/sim/src/image.rs
@@ -19,7 +19,7 @@
use simflash::{Flash, SimFlashMap};
use mcuboot_sys::{c, AreaDesc};
use crate::caps::Caps;
-use crate::tlv::{TlvGen, TlvFlags, AES_SEC_KEY};
+use crate::tlv::{ManifestGen, TlvGen, TlvFlags, AES_SEC_KEY};
impl Images {
/// A simple upgrade without forced failures.
@@ -662,7 +662,7 @@
let slot_len = slots[slot].len;
let dev_id = slots[slot].dev_id;
- let mut tlv = make_tlv();
+ let mut tlv: Box<dyn ManifestGen> = Box::new(make_tlv());
const HDR_SIZE: usize = 32;
diff --git a/sim/src/tlv.rs b/sim/src/tlv.rs
index 03390db..1ff7b75 100644
--- a/sim/src/tlv.rs
+++ b/sim/src/tlv.rs
@@ -41,6 +41,20 @@
RAM_LOAD = 0x20,
}
+/// A generator for manifests. The format of the manifest can be either a
+/// traditional "TLV" or a SUIT-style manifest.
+pub trait ManifestGen {
+ /// Retrieve the flags value for this particular manifest type.
+ fn get_flags(&self) -> u32;
+
+ /// Add a sequence of bytes to the payload that the manifest is
+ /// protecting.
+ fn add_bytes(&mut self, bytes: &[u8]);
+
+ /// Construct the manifest for this payload.
+ fn make_tlv(self: Box<Self>) -> Vec<u8>;
+}
+
pub struct TlvGen {
flags: u32,
kinds: Vec<TlvKinds>,
@@ -132,23 +146,25 @@
}
}
- /// Retrieve the header flags for this configuration. This can be called at any time.
- pub fn get_flags(&self) -> u32 {
- self.flags
- }
-
/// Retrieve the size that the TLV will occupy. This can be called at any time.
pub fn get_size(&self) -> u16 {
4 + self.size
}
+}
+
+impl ManifestGen for TlvGen {
+ /// Retrieve the header flags for this configuration. This can be called at any time.
+ fn get_flags(&self) -> u32 {
+ self.flags
+ }
/// Add bytes to the covered hash.
- pub fn add_bytes(&mut self, bytes: &[u8]) {
+ fn add_bytes(&mut self, bytes: &[u8]) {
self.payload.extend_from_slice(bytes);
}
/// Compute the TLV given the specified block of data.
- pub fn make_tlv(self) -> Vec<u8> {
+ fn make_tlv(self: Box<Self>) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
let size = self.get_size();