sim: add bootstrap test

Add caps for bootstrap option and implement simple bootstrap test
for the simulator.

Signed-off-by: Fabio Utzig <fabio.utzig@nordicsemi.no>
diff --git a/sim/src/caps.rs b/sim/src/caps.rs
index edffd0c..1d9a612 100644
--- a/sim/src/caps.rs
+++ b/sim/src/caps.rs
@@ -24,6 +24,7 @@
     SwapUsingMove        = (1 << 11),
     DowngradePrevention  = (1 << 12),
     EncX25519            = (1 << 13),
+    Bootstrap            = (1 << 14),
 }
 
 impl Caps {
diff --git a/sim/src/image.rs b/sim/src/image.rs
index e9865e4..a700698 100644
--- a/sim/src/image.rs
+++ b/sim/src/image.rs
@@ -258,6 +258,25 @@
         }
     }
 
+    pub fn make_bootstrap_image(self) -> Images {
+        let mut flash = self.flash;
+        let images = self.slots.into_iter().enumerate().map(|(image_num, slots)| {
+            let dep = BoringDep::new(image_num, &NO_DEPS);
+            let primaries = install_no_image();
+            let upgrades = install_image(&mut flash, &slots[1], 32784, &dep, false);
+            OneImage {
+                slots: slots,
+                primaries: primaries,
+                upgrades: upgrades,
+            }}).collect();
+        Images {
+            flash: flash,
+            areadesc: self.areadesc,
+            images: images,
+            total_count: None,
+        }
+    }
+
     /// Build the Flash and area descriptor for a given device.
     pub fn make_device(device: DeviceName, align: usize, erased_val: u8) -> (SimMultiFlash, AreaDesc, &'static [Caps]) {
         match device {
@@ -399,6 +418,39 @@
         }
     }
 
+    pub fn run_bootstrap(&self) -> bool {
+        let mut flash = self.flash.clone();
+        let mut fails = 0;
+
+        if Caps::Bootstrap.present() {
+            info!("Try bootstraping image in the primary");
+
+            let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false);
+            if result != 0 {
+                warn!("Failed first boot");
+                fails += 1;
+            }
+
+            if !self.verify_images(&flash, 0, 1) {
+                warn!("Image in the first slot was not bootstrapped");
+                fails += 1;
+            }
+
+            if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD,
+                                     BOOT_FLAG_SET, BOOT_FLAG_SET) {
+                warn!("Mismatched trailer for the primary slot");
+                fails += 1;
+            }
+        }
+
+        if fails > 0 {
+            error!("Expected trailer on secondary slot to be erased");
+        }
+
+        fails > 0
+    }
+
+
     /// Test a simple upgrade, with dependencies given, and verify that the
     /// image does as is described in the test.
     pub fn run_check_deps(&self, deps: &DepTest) -> bool {
diff --git a/sim/tests/core.rs b/sim/tests/core.rs
index c65ead2..4e2d9c1 100644
--- a/sim/tests/core.rs
+++ b/sim/tests/core.rs
@@ -48,6 +48,7 @@
 
 sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
 sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer());
+sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap());
 sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
 sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
 sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());