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,