Update main simulator routines for multi-flash

This adds an initial device with multiple flash (nrf52840 + SPI flash)
and updates all test routines to use a HashMap of flash devices (added
as type SimFlashMap).

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 1c34b5e..3914b22 100644
--- a/sim/mcuboot-sys/src/api.rs
+++ b/sim/mcuboot-sys/src/api.rs
@@ -10,7 +10,7 @@
 use std::ops::Deref;
 
 /// A FlashMap maintain a table of [device_id -> Flash trait]
-type FlashMap = HashMap<u8, FlashPtr>;
+pub type FlashMap = HashMap<u8, FlashPtr>;
 
 lazy_static! {
     static ref FLASH: Mutex<FlashMap> = {
diff --git a/sim/mcuboot-sys/src/area.rs b/sim/mcuboot-sys/src/area.rs
index 841bc78..8666aa1 100644
--- a/sim/mcuboot-sys/src/area.rs
+++ b/sim/mcuboot-sys/src/area.rs
@@ -106,13 +106,13 @@
         });
     }
 
-    // Look for the image with the given ID, and return its base and size.  Panics if the area is
-    // not present.
-    pub fn find(&self, id: FlashId) -> (usize, usize) {
+    // Look for the image with the given ID, and return its offset, size and
+    // device id. Panics if the area is not present.
+    pub fn find(&self, id: FlashId) -> (usize, usize, u8) {
         for area in &self.whole {
             // FIXME: should we ensure id is not duplicated over multiple devices?
             if area.flash_id == id {
-                return (area.off as usize, area.size as usize);
+                return (area.off as usize, area.size as usize, area.device_id);
             }
         }
         panic!("Requesting area that is not present in flash");
diff --git a/sim/mcuboot-sys/src/c.rs b/sim/mcuboot-sys/src/c.rs
index 65ff71f..d828607 100644
--- a/sim/mcuboot-sys/src/c.rs
+++ b/sim/mcuboot-sys/src/c.rs
@@ -1,7 +1,7 @@
 /// Interface wrappers to C API entering to the bootloader
 
 use area::AreaDesc;
-use simflash::Flash;
+use simflash::SimFlashMap;
 use libc;
 use api;
 use std::sync::Mutex;
@@ -13,12 +13,14 @@
 }
 
 /// Invoke the bootloader on this flash device.
-pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>,
-               catch_asserts: bool) -> (i32, u8) {
+pub fn boot_go(flashmap: &mut SimFlashMap, areadesc: &AreaDesc,
+               counter: Option<&mut i32>, catch_asserts: bool) -> (i32, u8) {
     let _lock = BOOT_LOCK.lock().unwrap();
 
     unsafe {
-        api::set_flash(0, flash);
+        for (&dev_id, flash) in flashmap.iter_mut() {
+            api::set_flash(dev_id, flash);
+        }
         raw::c_catch_asserts = if catch_asserts { 1 } else { 0 };
         raw::c_asserts = 0u8;
         raw::flash_counter = match counter {
@@ -30,7 +32,9 @@
     let asserts = unsafe { raw::c_asserts };
     unsafe {
         counter.map(|c| *c = raw::flash_counter as i32);
-        api::clear_flash(0);
+        for (&dev_id, _) in flashmap {
+            api::clear_flash(dev_id);
+        }
     };
     (result, asserts)
 }