Skip to content

Commit 06ef06e

Browse files
committed
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: David Brown <[email protected]>
1 parent 4c9883b commit 06ef06e

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

sim/mcuboot-sys/src/area.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ impl AreaDesc {
107107
}
108108

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

121121
pub fn get_c(&self) -> CAreaDesc {
@@ -176,9 +176,8 @@ pub enum FlashId {
176176
Image0 = 1,
177177
Image1 = 2,
178178
ImageScratch = 3,
179-
Nffs = 4,
180-
Core = 5,
181-
RebootLog = 6
179+
Image2 = 4,
180+
Image3 = 5,
182181
}
183182

184183
impl Default for FlashId {

sim/src/image.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,57 @@ impl ImagesBuilder {
6868
pub fn new(device: DeviceName, align: u8, erased_val: u8) -> Option<Self> {
6969
let (flash, areadesc) = Self::make_device(device, align, erased_val);
7070

71-
let (slot0_base, slot0_len, slot0_dev_id) = areadesc.find(FlashId::Image0);
72-
let (slot1_base, slot1_len, slot1_dev_id) = areadesc.find(FlashId::Image1);
73-
74-
// NOTE: not accounting "swap_size" because it is not used by sim...
75-
let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2;
76-
77-
// Construct a primary image.
78-
let slot0 = SlotInfo {
79-
base_off: slot0_base as usize,
80-
trailer_off: slot0_base + slot0_len - offset_from_end,
81-
len: slot0_len as usize,
82-
dev_id: slot0_dev_id,
83-
};
84-
85-
// And an upgrade image.
86-
let slot1 = SlotInfo {
87-
base_off: slot1_base as usize,
88-
trailer_off: slot1_base + slot1_len - offset_from_end,
89-
len: slot1_len as usize,
90-
dev_id: slot1_dev_id,
91-
};
71+
let num_images = Caps::get_num_images();
72+
73+
let mut slots = Vec::with_capacity(num_images);
74+
for image in 0..num_images {
75+
// This mapping must match that defined in
76+
// `boot/zephyr/include/sysflash/sysflash.h`.
77+
let id0 = match image {
78+
0 => FlashId::Image0,
79+
1 => FlashId::Image2,
80+
_ => panic!("More than 2 images not supported"),
81+
};
82+
let (primary_base, primary_len, primary_dev_id) = match areadesc.find(id0) {
83+
Some(info) => info,
84+
None => return None,
85+
};
86+
let id1 = match image {
87+
0 => FlashId::Image1,
88+
1 => FlashId::Image3,
89+
_ => panic!("More than 2 images not supported"),
90+
};
91+
let (secondary_base, secondary_len, secondary_dev_id) = match areadesc.find(id1) {
92+
Some(info) => info,
93+
None => return None,
94+
};
95+
96+
// NOTE: not accounting "swap_size" because it is not used by sim...
97+
let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2;
98+
99+
// Construct a primary image.
100+
let primary = SlotInfo {
101+
base_off: primary_base as usize,
102+
trailer_off: primary_base + primary_len - offset_from_end,
103+
len: primary_len as usize,
104+
dev_id: primary_dev_id,
105+
};
106+
107+
// And an upgrade image.
108+
let secondary = SlotInfo {
109+
base_off: secondary_base as usize,
110+
trailer_off: secondary_base + secondary_len - offset_from_end,
111+
len: secondary_len as usize,
112+
dev_id: secondary_dev_id,
113+
};
114+
115+
slots.push([primary, secondary]);
116+
}
92117

93118
Some(ImagesBuilder {
94119
flash: flash,
95120
areadesc: areadesc,
96-
slots: vec![[slot0, slot1]],
121+
slots: slots,
97122
})
98123
}
99124

0 commit comments

Comments
 (0)