Skip to content

Commit 9324d2b

Browse files
committed
sim: Added test for over-sized image update
Added test which checks whether too big update image will be rejected. Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent 26d19d3 commit 9324d2b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

sim/src/image.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,29 @@ impl ImagesBuilder {
291291
}
292292
}
293293

294+
pub fn make_oversized_secondary_slot_image(self) -> Images {
295+
let mut bad_flash = self.flash;
296+
let ram = self.ram.clone(); // TODO: Avoid this clone.
297+
let images = self.slots.into_iter().enumerate().map(|(image_num, slots)| {
298+
let dep = BoringDep::new(image_num, &NO_DEPS);
299+
let primaries = install_image(&mut bad_flash, &slots[0],
300+
maximal(32784), &ram, &dep, false);
301+
let upgrades = install_image(&mut bad_flash, &slots[1],
302+
ImageSize::Oversized, &ram, &dep, false);
303+
OneImage {
304+
slots,
305+
primaries,
306+
upgrades,
307+
}}).collect();
308+
Images {
309+
flash: bad_flash,
310+
areadesc: self.areadesc,
311+
images,
312+
total_count: None,
313+
ram: self.ram,
314+
}
315+
}
316+
294317
pub fn make_erased_secondary_image(self) -> Images {
295318
let mut flash = self.flash;
296319
let ram = self.ram.clone(); // TODO: Avoid this clone.
@@ -779,6 +802,53 @@ impl Images {
779802
fails > 0
780803
}
781804

805+
// Test taht too big upgrade image will be rejected
806+
pub fn run_oversizefail_upgrade(&self) -> bool {
807+
let mut flash = self.flash.clone();
808+
let mut fails = 0;
809+
810+
info!("Try upgrade image with to big size");
811+
812+
// Only perform this test if an upgrade is expected to happen.
813+
if !Caps::modifies_flash() {
814+
info!("Skipping upgrade image with bad signature");
815+
return false;
816+
}
817+
818+
self.mark_upgrades(&mut flash, 0);
819+
self.mark_permanent_upgrades(&mut flash, 0);
820+
self.mark_upgrades(&mut flash, 1);
821+
822+
if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD,
823+
BOOT_FLAG_SET, BOOT_FLAG_UNSET) {
824+
warn!("1. Mismatched trailer for the primary slot");
825+
fails += 1;
826+
}
827+
828+
// Run the bootloader...
829+
if !c::boot_go(&mut flash, &self.areadesc, None, None, false).success() {
830+
warn!("Failed first boot");
831+
fails += 1;
832+
}
833+
834+
// State should not have changed
835+
if !self.verify_images(&flash, 0, 0) {
836+
warn!("Failed image verification");
837+
fails += 1;
838+
}
839+
if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD,
840+
BOOT_FLAG_SET, BOOT_FLAG_UNSET) {
841+
warn!("2. Mismatched trailer for the primary slot");
842+
fails += 1;
843+
}
844+
845+
if fails > 0 {
846+
error!("Expected an upgrade failure when image has to big size");
847+
}
848+
849+
fails > 0
850+
}
851+
782852
// Test that an upgrade is rejected. Assumes that the image was build
783853
// such that the upgrade is instead a downgrade.
784854
pub fn run_nodowngrade(&self) -> bool {

sim/tests/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails(
5656
sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
5757
sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
5858
sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
59+
60+
#[cfg(not(feature = "max-align-32"))]
61+
sim_test!(oversized_secondary_slot, make_oversized_secondary_slot_image(), run_oversizefail_upgrade());
62+
5963
sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
6064
sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
6165
sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());

0 commit comments

Comments
 (0)