Commit 06ef06e6 authored by David Brown's avatar David Brown Committed by David Brown
Browse files

sim: Base slots on number of images used

Construct the ImagesBuilder based on the number of images compiled into
the code.  If the flash device doesn't have enough areas for the test,
the test will be skipped.

Extend the FlashId to include Image2, and Image3.  Remove the unused
ones, so that these can be placed immediately after the scratcharea.
The current simulator code assumes the flash areas are numbered
contiguously, requiring these extraneous partitions to be eliminated.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent 4c9883b0
Loading
Loading
Loading
Loading
+6 −7
Changes for sim/mcuboot-sys/src/area.rs: 6 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -107,15 +107,15 @@ impl AreaDesc {
    }

    // Look for the image with the given ID, and return its offset, size and
    // device id. Panics if the area is not present.
    pub fn find(&self, id: FlashId) -> (usize, usize, u8) {
    // device id. Returns None if the area is not present.
    pub fn find(&self, id: FlashId) -> Option<(usize, usize, u8)> {
        for area in &self.whole {
            // FIXME: should we ensure id is not duplicated over multiple devices?
            if area.flash_id == id {
                return (area.off as usize, area.size as usize, area.device_id);
                return Some((area.off as usize, area.size as usize, area.device_id));
            }
        }
        panic!("Requesting area that is not present in flash");
        None
    }

    pub fn get_c(&self) -> CAreaDesc {
@@ -176,9 +176,8 @@ pub enum FlashId {
    Image0 = 1,
    Image1 = 2,
    ImageScratch = 3,
    Nffs = 4,
    Core = 5,
    RebootLog = 6
    Image2 = 4,
    Image3 = 5,
}

impl Default for FlashId {
+38 −13
Changes for sim/src/image.rs: 38 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -68,32 +68,57 @@ impl ImagesBuilder {
    pub fn new(device: DeviceName, align: u8, erased_val: u8) -> Option<Self> {
        let (flash, areadesc) = Self::make_device(device, align, erased_val);

        let (slot0_base, slot0_len, slot0_dev_id) = areadesc.find(FlashId::Image0);
        let (slot1_base, slot1_len, slot1_dev_id) = areadesc.find(FlashId::Image1);
        let num_images = Caps::get_num_images();

        let mut slots = Vec::with_capacity(num_images);
        for image in 0..num_images {
            // This mapping must match that defined in
            // `boot/zephyr/include/sysflash/sysflash.h`.
            let id0 = match image {
                0 => FlashId::Image0,
                1 => FlashId::Image2,
                _ => panic!("More than 2 images not supported"),
            };
            let (primary_base, primary_len, primary_dev_id) = match areadesc.find(id0) {
                Some(info) => info,
                None => return None,
            };
            let id1 = match image {
                0 => FlashId::Image1,
                1 => FlashId::Image3,
                _ => panic!("More than 2 images not supported"),
            };
            let (secondary_base, secondary_len, secondary_dev_id) = match areadesc.find(id1) {
                Some(info) => info,
                None => return None,
            };

            // NOTE: not accounting "swap_size" because it is not used by sim...
            let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2;

            // Construct a primary image.
        let slot0 = SlotInfo {
            base_off: slot0_base as usize,
            trailer_off: slot0_base + slot0_len - offset_from_end,
            len: slot0_len as usize,
            dev_id: slot0_dev_id,
            let primary = SlotInfo {
                base_off: primary_base as usize,
                trailer_off: primary_base + primary_len - offset_from_end,
                len: primary_len as usize,
                dev_id: primary_dev_id,
            };

            // And an upgrade image.
        let slot1 = SlotInfo {
            base_off: slot1_base as usize,
            trailer_off: slot1_base + slot1_len - offset_from_end,
            len: slot1_len as usize,
            dev_id: slot1_dev_id,
            let secondary = SlotInfo {
                base_off: secondary_base as usize,
                trailer_off: secondary_base + secondary_len - offset_from_end,
                len: secondary_len as usize,
                dev_id: secondary_dev_id,
            };

            slots.push([primary, secondary]);
        }

        Some(ImagesBuilder {
            flash: flash,
            areadesc: areadesc,
            slots: vec![[slot0, slot1]],
            slots: slots,
        })
    }