Skip to content

Commit 1cac1eb

Browse files
taltenbachd3zd3z
authored andcommitted
sim: Use non-equal security counters when testing upgrades
When testing upgrades, the simulator was always using two images having the same security counter. This was preventing to test that the security counters are updated at the right time in the scenarios where a revert is possible. The upgrade image is now generated with a higher security counter than the original image, enabling to detect e.g. the issue fixed by the previous commit. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent 0eaf666 commit 1cac1eb

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

sim/mcuboot-sys/src/api.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,13 @@ pub extern "C" fn sim_get_nv_counter_for_image(image_index: u32, security_counte
388388
});
389389
return rc;
390390
}
391+
392+
pub fn sim_reset_nv_counters() {
393+
NV_COUNTER_CTX.with(|ctx| {
394+
let mut counter_storage = ctx.borrow_mut();
395+
396+
for i in 0..counter_storage.storage.len() {
397+
counter_storage.storage[i] = 0;
398+
}
399+
});
400+
}

sim/mcuboot-sys/src/c.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ pub fn get_security_counter(image_index: u32) -> u32 {
166166
return counter_val;
167167
}
168168

169+
pub fn reset_security_counters() {
170+
api::sim_reset_nv_counters();
171+
}
172+
169173
mod raw {
170174
use crate::area::CAreaDesc;
171175
use crate::api::{BootRsp, CSimContext};

sim/src/image.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl ImagesBuilder {
239239
let upgr = match deps.depends[image_num] {
240240
DepType::NoUpgrade => install_no_image(),
241241
_ => install_image(&mut flash, &self.areadesc, &slots, 1,
242-
maximal(46928), &ram, &*dep, ImageManipulation::BadSignature, Some(0))
242+
maximal(46928), &ram, &*dep, ImageManipulation::BadSignature, Some(1))
243243
};
244244
(prim, upgr)
245245
} else {
@@ -248,7 +248,7 @@ impl ImagesBuilder {
248248
let upgr = match deps.depends[image_num] {
249249
DepType::NoUpgrade => install_no_image(),
250250
_ => install_image(&mut flash, &self.areadesc, &slots, 1,
251-
maximal(46928), &ram, &*dep, img_manipulation, Some(0))
251+
maximal(46928), &ram, &*dep, img_manipulation, Some(1))
252252
};
253253
(prim, upgr)
254254
};
@@ -289,6 +289,10 @@ impl ImagesBuilder {
289289
}
290290
};
291291

292+
// As a side effect, the upgrade performed above has updated the security counters. Reset
293+
// them to their original value.
294+
c::reset_security_counters();
295+
292296
images.total_count = Some(total_count);
293297
images
294298
}

sim/tests/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
env,
2222
sync::atomic::{AtomicUsize, Ordering},
2323
};
24+
use mcuboot_sys::c;
2425

2526
/// A single test, after setting up logging and such. Within the $body,
2627
/// $arg will be bound to each device.
@@ -90,6 +91,7 @@ test_shell!(dependency_combos, r, {
9091
let image = r.clone().make_image(&dep, true);
9192
dump_image(&image, "dependency_combos");
9293
assert!(!image.run_check_deps(&dep));
94+
c::reset_security_counters();
9395
}
9496
});
9597

0 commit comments

Comments
 (0)