Commit 43643dd2 authored by David Brown's avatar David Brown Committed by David Brown
Browse files

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: default avatarDavid Brown <david.brown@linaro.org>
parent 8d66c920
Loading
Loading
Loading
Loading
+2 −2
Changes for sim/src/image.rs: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ use aes_ctr::{
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 @@ pub fn install_image(flashmap: &mut SimFlashMap, slots: &[SlotInfo], slot: usize
    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;

+23 −7
Changes for sim/src/tlv.rs: 23 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -41,6 +41,20 @@ pub enum TlvFlags {
    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 @@ impl TlvGen {
        }
    }

    /// 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();