Skip to content

Commit 66ed29f

Browse files
committed
sim: update ram load to use image size comparison
When building images for testing they are aligned to a flash write size, which works fine for flash based comparisons, but since RAM is always reset to 0, if the flash is erased to 0xff, the final bytes of the alignment will differ even though they are not relevant (not part of the image itself). This commit adds a real image size parameter to the ImageData, so it can be used by the RAM load to compare only the relevant bits of the image (ignore the padding), and also updates the RAM test routine to use the correct image size. Signed-off-by: Fabio Utzig <utzig@apache.org>
1 parent 4b82b20 commit 66ed29f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

sim/src/image.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct OneImage {
9494
/// is just the unencrypted payload. For encrypted images, we store both
9595
/// the encrypted and the plaintext.
9696
struct ImageData {
97+
size: usize,
9798
plain: Vec<u8>,
9899
cipher: Option<Vec<u8>>,
99100
}
@@ -1067,13 +1068,13 @@ impl Images {
10671068
let place = self.ram.lookup(&image.slots[0]);
10681069
let ram_image = ram.borrow_part(place.offset as usize - RAM_LOAD_ADDR as usize,
10691070
place.size as usize);
1070-
let src_image = &image.upgrades.plain;
1071-
if src_image.len() > ram_image.len() {
1071+
let src_sz = image.upgrades.size();
1072+
if src_sz > ram_image.len() {
10721073
error!("Image ended up too large, nonsensical");
10731074
return true;
10741075
}
1075-
1076-
let ram_image = &ram_image[0..src_image.len()];
1076+
let src_image = &image.upgrades.plain[0..src_sz];
1077+
let ram_image = &ram_image[0..src_sz];
10771078
if ram_image != src_image {
10781079
error!("Image not loaded correctly");
10791080
return true;
@@ -1510,6 +1511,7 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize,
15101511

15111512
// Pad the buffer to a multiple of the flash alignment.
15121513
let align = dev.align();
1514+
let image_sz = buf.len();
15131515
while buf.len() % align != 0 {
15141516
buf.push(dev.erased_val());
15151517
}
@@ -1552,6 +1554,7 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize,
15521554
dev.read(offset, &mut copy).unwrap();
15531555

15541556
ImageData {
1557+
size: image_sz,
15551558
plain: copy,
15561559
cipher: enc_copy,
15571560
}
@@ -1578,6 +1581,7 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize,
15781581
}
15791582

15801583
ImageData {
1584+
size: image_sz,
15811585
plain: copy,
15821586
cipher: enc_copy,
15831587
}
@@ -1587,6 +1591,7 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize,
15871591
/// Install no image. This is used when no upgrade happens.
15881592
fn install_no_image() -> ImageData {
15891593
ImageData {
1594+
size: 0,
15901595
plain: vec![],
15911596
cipher: None,
15921597
}
@@ -1656,6 +1661,10 @@ impl ImageData {
16561661
_ => panic!("Invalid slot requested"),
16571662
}
16581663
}
1664+
1665+
fn size(&self) -> usize {
1666+
self.size
1667+
}
16591668
}
16601669

16611670
/// Verify that given image is present in the flash at the given offset.

0 commit comments

Comments
 (0)