Skip to content

Commit de8d425

Browse files
sim: Test for boot_load_image_from_flash_to_sram API
Directly load an image to RAM and see if that works. Signed-off-by: Ederson de Souza <[email protected]>
1 parent 9e0bebc commit de8d425

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

sim/mcuboot-sys/csupport/run.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,57 @@ int invoke_boot_go(struct sim_context *ctx, struct area_desc *adesc,
289289
}
290290
}
291291

292+
int invoke_boot_load_image_from_flash_to_sram(struct sim_context *ctx, struct area_desc *adesc)
293+
{
294+
int res;
295+
struct boot_loader_state *state;
296+
const struct flash_area *fa_p;
297+
struct image_header hdr;
298+
299+
#if defined(MCUBOOT_SIGN_RSA) || \
300+
(defined(MCUBOOT_SIGN_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
301+
(defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
302+
(defined(MCUBOOT_ENCRYPT_X25519) && defined(MCUBOOT_USE_MBED_TLS))
303+
mbedtls_platform_set_calloc_free(calloc, free);
304+
#endif
305+
306+
state = malloc(sizeof(struct boot_loader_state));
307+
308+
sim_set_flash_areas(adesc);
309+
sim_set_context(ctx);
310+
boot_state_clear(state);
311+
312+
res = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fa_p);
313+
if (res != 0) {
314+
printf("Failed to open primary image area: %d\n", res);
315+
sim_reset_flash_areas();
316+
sim_reset_context();
317+
free(state);
318+
return res;
319+
}
320+
321+
res = boot_image_load_header(fa_p, &hdr);
322+
if (res != 0) {
323+
printf("Failed to load image header: %d\n", res);
324+
flash_area_close(fa_p);
325+
sim_reset_flash_areas();
326+
sim_reset_context();
327+
free(state);
328+
return res;
329+
}
330+
331+
res = boot_load_image_from_flash_to_sram(state, &hdr, fa_p);
332+
if (res != 0) {
333+
printf("Failed to load image from flash to SRAM: %d\n", res);
334+
}
335+
336+
flash_area_close(fa_p);
337+
sim_reset_flash_areas();
338+
sim_reset_context();
339+
free(state);
340+
return res;
341+
}
342+
292343
void *os_malloc(size_t size)
293344
{
294345
// printf("os_malloc 0x%x bytes\n", size);

sim/mcuboot-sys/src/c.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc,
115115
}
116116
}
117117

118+
pub fn boot_load_image_from_flash_to_sram(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc) -> bool {
119+
init_crypto();
120+
121+
for (&dev_id, flash) in multiflash.iter_mut() {
122+
api::set_flash(dev_id, flash);
123+
}
124+
let mut sim_ctx = api::CSimContext {
125+
flash_counter: 0,
126+
c_catch_asserts: 0,
127+
.. Default::default()
128+
};
129+
let result: i32 = unsafe {
130+
let adesc = areadesc.get_c();
131+
raw::invoke_boot_load_image_from_flash_to_sram(&mut sim_ctx as *mut _,
132+
adesc.borrow() as *const _) as i32
133+
};
134+
for &dev_id in multiflash.keys() {
135+
api::clear_flash(dev_id);
136+
}
137+
result == 0
138+
}
139+
118140
pub fn boot_trailer_sz(align: u32) -> u32 {
119141
unsafe { raw::boot_trailer_sz(align) }
120142
}
@@ -181,6 +203,9 @@ mod raw {
181203
pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc,
182204
rsp: *mut BootRsp, image_index: libc::c_int) -> libc::c_int;
183205

206+
pub fn invoke_boot_load_image_from_flash_to_sram(sim_ctx: *mut CSimContext,
207+
areadesc: *const CAreaDesc) -> libc::c_int;
208+
184209
pub fn boot_trailer_sz(min_write_sz: u32) -> u32;
185210
pub fn boot_status_sz(min_write_sz: u32) -> u32;
186211

sim/src/image.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,40 @@ impl Images {
14131413
false
14141414
}
14151415

1416+
pub fn run_ram_load_from_flash(&self) -> bool {
1417+
if !Caps::RamLoad.present() {
1418+
return false;
1419+
}
1420+
1421+
// Clone the flash so we can tell if unchanged.
1422+
let mut flash = self.flash.clone();
1423+
1424+
// Create RAM config.
1425+
let ram = RamBlock::new(self.ram.total - RAM_LOAD_ADDR, RAM_LOAD_ADDR);
1426+
1427+
// Run boot_load_image_from_flash_to_sram directly
1428+
let result = ram.invoke(|| c::boot_load_image_from_flash_to_sram(&mut flash, &self.areadesc));
1429+
1430+
if !result {
1431+
error!("RAM load from flash failed!");
1432+
return true;
1433+
}
1434+
1435+
// Compare loaded image with the first image in the flash.
1436+
let image = &self.images[0].primaries;
1437+
let ram_image = ram.borrow();
1438+
let src_sz = image.plain.len();
1439+
let src_image = &image.plain[0..src_sz];
1440+
let ram_image = &ram_image[0..src_sz];
1441+
1442+
if ram_image != src_image {
1443+
error!("Image not loaded correctly");
1444+
return true;
1445+
}
1446+
1447+
false
1448+
}
1449+
14161450
/// Adds a new flash area that fails statistically
14171451
fn mark_bad_status_with_rate(&self, flash: &mut SimMultiFlash, slot: usize,
14181452
rate: f32) {

sim/tests/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());
6868
sim_test!(direct_xip_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_direct_xip());
6969
sim_test!(ram_load_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_ram_load());
7070
sim_test!(ram_load_split, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_split_ram_load());
71+
sim_test!(ram_load_from_flash, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_ram_load_from_flash());
7172
sim_test!(hw_prot_failed_security_cnt_check, make_image_with_security_counter(Some(0)), run_hw_rollback_prot());
7273
sim_test!(hw_prot_missing_security_cnt, make_image_with_security_counter(None), run_hw_rollback_prot());
7374
sim_test!(ram_load_out_of_bounds, make_no_upgrade_image(&NO_DEPS, ImageManipulation::WrongOffset), run_ram_load_boot_with_result(false));

0 commit comments

Comments
 (0)