Add align/erased_val params per flash device
The previous c/rust ffi functions were hardcoding the values of align
and erased_val before each run through static globals. This adds new sim
flash functions that get the align/erased_val from the sim flash device
that is being run on, allowing that later multiple flash devices can
each use its own params.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/sim/mcuboot-sys/src/api.rs b/sim/mcuboot-sys/src/api.rs
index 7a932a5..0205164 100644
--- a/sim/mcuboot-sys/src/api.rs
+++ b/sim/mcuboot-sys/src/api.rs
@@ -5,12 +5,31 @@
use log::LogLevel;
use std::mem;
use std::slice;
+use std::collections::HashMap;
+use std::sync::Mutex;
// The current active flash device. The 'static is a lie, and we manage the lifetime ourselves.
static mut FLASH: Option<*mut Flash> = None;
+struct FlashParams {
+ align: u8,
+ erased_val: u8,
+}
+
+lazy_static! {
+ static ref FLASH_PARAMS: Mutex<HashMap<u8, FlashParams>> = {
+ Mutex::new(HashMap::new())
+ };
+}
+
// Set the flash device to be used by the simulation. The pointer is unsafely stashed away.
-pub unsafe fn set_flash(dev: &mut Flash) {
+pub unsafe fn set_flash(dev_id: u8, dev: &mut Flash) {
+ let mut flash_params = FLASH_PARAMS.lock().unwrap();
+ flash_params.insert(dev_id, FlashParams {
+ align: dev.align() as u8,
+ erased_val: dev.erased_val(),
+ });
+
let dev: &'static mut Flash = mem::transmute(dev);
FLASH = Some(dev as *mut Flash);
}
@@ -52,6 +71,20 @@
map_err(dev.write(offset as usize, &buf))
}
+#[no_mangle]
+pub extern fn sim_flash_align(id: u8) -> u8 {
+ let flash_params = FLASH_PARAMS.lock().unwrap();
+ let params = flash_params.get(&id).unwrap();
+ params.align
+}
+
+#[no_mangle]
+pub extern fn sim_flash_erased_val(id: u8) -> u8 {
+ let flash_params = FLASH_PARAMS.lock().unwrap();
+ let params = flash_params.get(&id).unwrap();
+ params.erased_val
+}
+
fn map_err(err: Result<()>) -> libc::c_int {
match err {
Ok(()) => 0,
diff --git a/sim/mcuboot-sys/src/area.rs b/sim/mcuboot-sys/src/area.rs
index c4f437e..08896db 100644
--- a/sim/mcuboot-sys/src/area.rs
+++ b/sim/mcuboot-sys/src/area.rs
@@ -54,7 +54,7 @@
area.push(FlashArea {
flash_id: id,
- device_id: 42,
+ device_id: 0,
pad16: 0,
off: sector.base as u32,
size: sector.size as u32,
@@ -71,7 +71,7 @@
self.areas.push(area);
self.whole.push(FlashArea {
flash_id: id,
- device_id: 42,
+ device_id: 0,
pad16: 0,
off: orig_base as u32,
size: orig_len as u32,
@@ -85,7 +85,7 @@
pub fn add_simple_image(&mut self, base: usize, len: usize, id: FlashId) {
let area = vec![FlashArea {
flash_id: id,
- device_id: 42,
+ device_id: 0,
pad16: 0,
off: base as u32,
size: len as u32,
@@ -94,7 +94,7 @@
self.areas.push(area);
self.whole.push(FlashArea {
flash_id: id,
- device_id: 42,
+ device_id: 0,
pad16: 0,
off: base as u32,
size: len as u32,
diff --git a/sim/mcuboot-sys/src/c.rs b/sim/mcuboot-sys/src/c.rs
index 1d12da5..666b038 100644
--- a/sim/mcuboot-sys/src/c.rs
+++ b/sim/mcuboot-sys/src/c.rs
@@ -18,11 +18,9 @@
let _lock = BOOT_LOCK.lock().unwrap();
unsafe {
- api::set_flash(flash);
+ api::set_flash(0, flash);
raw::c_catch_asserts = if catch_asserts { 1 } else { 0 };
raw::c_asserts = 0u8;
- raw::sim_flash_align = flash.align() as u8;
- raw::sim_flash_erased_val = flash.erased_val();
raw::flash_counter = match counter {
None => 0,
Some(ref c) => **c as libc::c_int
@@ -95,8 +93,6 @@
pub static mut c_asserts: u8;
pub static mut c_catch_asserts: u8;
- pub static mut sim_flash_align: u8;
- pub static mut sim_flash_erased_val: u8;
pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32;
pub static BOOT_MAGIC_SZ: u32;