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>
diff --git a/sim/src/image.rs b/sim/src/image.rs
index 02ceb29..cc9dd6a 100644
--- a/sim/src/image.rs
+++ b/sim/src/image.rs
@@ -94,6 +94,7 @@
/// is just the unencrypted payload. For encrypted images, we store both
/// the encrypted and the plaintext.
struct ImageData {
+ size: usize,
plain: Vec<u8>,
cipher: Option<Vec<u8>>,
}
@@ -1067,13 +1068,13 @@
let place = self.ram.lookup(&image.slots[0]);
let ram_image = ram.borrow_part(place.offset as usize - RAM_LOAD_ADDR as usize,
place.size as usize);
- let src_image = &image.upgrades.plain;
- if src_image.len() > ram_image.len() {
+ let src_sz = image.upgrades.size();
+ if src_sz > ram_image.len() {
error!("Image ended up too large, nonsensical");
return true;
}
-
- let ram_image = &ram_image[0..src_image.len()];
+ let src_image = &image.upgrades.plain[0..src_sz];
+ let ram_image = &ram_image[0..src_sz];
if ram_image != src_image {
error!("Image not loaded correctly");
return true;
@@ -1510,6 +1511,7 @@
// Pad the buffer to a multiple of the flash alignment.
let align = dev.align();
+ let image_sz = buf.len();
while buf.len() % align != 0 {
buf.push(dev.erased_val());
}
@@ -1552,6 +1554,7 @@
dev.read(offset, &mut copy).unwrap();
ImageData {
+ size: image_sz,
plain: copy,
cipher: enc_copy,
}
@@ -1578,6 +1581,7 @@
}
ImageData {
+ size: image_sz,
plain: copy,
cipher: enc_copy,
}
@@ -1587,6 +1591,7 @@
/// Install no image. This is used when no upgrade happens.
fn install_no_image() -> ImageData {
ImageData {
+ size: 0,
plain: vec![],
cipher: None,
}
@@ -1656,6 +1661,10 @@
_ => panic!("Invalid slot requested"),
}
}
+
+ fn size(&self) -> usize {
+ self.size
+ }
}
/// Verify that given image is present in the flash at the given offset.