Skip to content

Commit 3136315

Browse files
committed
sim: Add test for MCUBOOT_CHECK_HEADER_LOAD_ADDRESS
Testing MCUBOOT_CHECK_HEADER_LOAD_ADDRESS for non-RAM load binaries. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 2583309 commit 3136315

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

sim/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ direct-xip = ["mcuboot-sys/direct-xip"]
3434
downgrade-prevention = ["mcuboot-sys/downgrade-prevention"]
3535
max-align-32 = ["mcuboot-sys/max-align-32"]
3636
hw-rollback-protection = ["mcuboot-sys/hw-rollback-protection"]
37+
check-load-addr = ["mcuboot-sys/check-load-addr"]
3738

3839
[dependencies]
3940
byteorder = "1.4"

sim/mcuboot-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ hw-rollback-protection = []
9999
# Enable the PSA Crypto APIs where supported for cryptography related operations.
100100
psa-crypto-api = []
101101

102+
# Test for ih_load_addr in upgrade/next boot slot
103+
check-load-addr = []
104+
102105
[build-dependencies]
103106
cc = "1.0.25"
104107

sim/mcuboot-sys/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn main() {
3939
let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
4040
let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok();
4141
let hw_rollback_protection = env::var("CARGO_FEATURE_HW_ROLLBACK_PROTECTION").is_ok();
42+
let check_load_addr = env::var("CARGO_FEATURE_CHECK_LOAD_ADDR").is_ok();
4243

4344
let mut conf = CachedBuild::new();
4445
conf.conf.define("__BOOTSIM__", None);
@@ -64,6 +65,10 @@ fn main() {
6465
conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
6566
}
6667

68+
if check_load_addr {
69+
conf.conf.define("MCUBOOT_CHECK_HEADER_LOAD_ADDRESS", None);
70+
}
71+
6772
if validate_primary_slot {
6873
conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
6974
}

sim/src/image.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,8 @@ impl Images {
899899
fails > 0
900900
}
901901

902-
// Test taht too big upgrade image will be rejected
903-
pub fn run_oversizefail_upgrade(&self) -> bool {
902+
// Test expecting failed upgrade and primary slot left untouched
903+
pub fn run_fail_upgrade_primary_intact(&self) -> bool {
904904
let mut flash = self.flash.clone();
905905
let mut fails = 0;
906906

@@ -940,7 +940,7 @@ impl Images {
940940
}
941941

942942
if fails > 0 {
943-
error!("Expected an upgrade failure when image has to big size");
943+
error!("Expected an upgrade failure and primary slot left untouched");
944944
}
945945

946946
fails > 0
@@ -1930,7 +1930,21 @@ fn install_image(flash: &mut SimMultiFlash, areadesc: &AreaDesc, slots: &[SlotIn
19301930
_ => place.offset
19311931
}
19321932
} else {
1933-
0
1933+
if cfg!(feature = "check-load-addr") {
1934+
let wrong_off = match img_manipulation {
1935+
ImageManipulation::WrongOffset => true,
1936+
_ => false
1937+
};
1938+
if wrong_off {
1939+
u32::MAX
1940+
} else if cfg!(feature = "direct-xip") {
1941+
slots[slot_ind].base_off as u32
1942+
} else {
1943+
slots[0].base_off as u32
1944+
}
1945+
} else {
1946+
0
1947+
}
19341948
};
19351949

19361950
let len = match len {

sim/tests/core.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,33 @@ sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails(
5959
sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
6060
sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
6161
sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
62-
sim_test!(oversized_secondary_slot, make_oversized_secondary_slot_image(), run_oversizefail_upgrade());
62+
sim_test!(oversized_secondary_slot, make_oversized_secondary_slot_image(), run_fail_upgrade_primary_intact());
63+
#[cfg(feature = "check-load-addr")]
64+
sim_test!(wrong_load_addr, make_bad_secondary_slot_image(ImageManipulation::WrongOffset), run_fail_upgrade_primary_intact());
6365

6466
sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
6567
sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
6668
sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());
6769

6870
sim_test!(direct_xip_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_direct_xip());
71+
#[cfg(not(feature = "check-load-addr"))]
6972
sim_test!(ram_load_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_ram_load());
73+
#[cfg(not(feature = "check-load-addr"))]
7074
sim_test!(ram_load_split, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_split_ram_load());
75+
#[cfg(not(feature = "check-load-addr"))]
7176
sim_test!(ram_load_from_flash, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_ram_load_from_flash());
72-
sim_test!(hw_prot_failed_security_cnt_check, make_image_with_security_counter(Some(0)), run_hw_rollback_prot());
73-
sim_test!(hw_prot_missing_security_cnt, make_image_with_security_counter(None), run_hw_rollback_prot());
77+
#[cfg(not(feature = "check-load-addr"))]
7478
sim_test!(ram_load_out_of_bounds, make_no_upgrade_image(&NO_DEPS, ImageManipulation::WrongOffset), run_ram_load_boot_with_result(false));
79+
#[cfg(not(feature = "check-load-addr"))]
7580
sim_test!(ram_load_missing_header_flag, make_no_upgrade_image(&NO_DEPS, ImageManipulation::IgnoreRamLoadFlag), run_ram_load_boot_with_result(false));
81+
#[cfg(not(feature = "check-load-addr"))]
7682
sim_test!(ram_load_failed_validation, make_no_upgrade_image(&NO_DEPS, ImageManipulation::BadSignature), run_ram_load_boot_with_result(false));
83+
#[cfg(not(feature = "check-load-addr"))]
7784
sim_test!(ram_load_corrupt_higher_version_image, make_no_upgrade_image(&NO_DEPS, ImageManipulation::CorruptHigherVersionImage), run_ram_load_boot_with_result(true));
7885

86+
sim_test!(hw_prot_missing_security_cnt, make_image_with_security_counter(None), run_hw_rollback_prot());
87+
sim_test!(hw_prot_failed_security_cnt_check, make_image_with_security_counter(Some(0)), run_hw_rollback_prot());
88+
7989
#[cfg(feature = "multiimage")]
8090
sim_test!(ram_load_overlapping_images_same_base, make_no_upgrade_image(&NO_DEPS, ImageManipulation::OverlapImages(true)), run_ram_load_boot_with_result(false));
8191
#[cfg(feature = "multiimage")]

0 commit comments

Comments
 (0)