Test imgtool generated FW written to slot0

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/sim/src/main.rs b/sim/src/main.rs
index eed9855..106b423 100644
--- a/sim/src/main.rs
+++ b/sim/src/main.rs
@@ -150,7 +150,7 @@
         error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures);
         process::exit(1);
     } else {
-        warn!("{} Tests ran successfully", status.passes);
+        error!("{} Tests ran successfully", status.passes);
         process::exit(0);
     }
 }
@@ -257,6 +257,8 @@
         // Set an alignment, and position the magic value.
         c::set_sim_flash_align(align);
 
+        failed |= run_norevert_newimage(&flash, &areadesc, &images);
+
         mark_upgrade(&mut flash, &images.slot1);
 
         // upgrades without fails, counts number of flash operations
@@ -272,7 +274,7 @@
         failed |= run_revert_with_fails(&flash, &areadesc, &images, total_count);
         failed |= run_perm_with_fails(&flash, &areadesc, &images, total_count);
         failed |= run_perm_with_random_fails(&flash, &areadesc, &images,
-                                                    total_count, 5);
+                                             total_count, 5);
         failed |= run_norevert(&flash, &areadesc, &images);
 
         //show_flash(&flash);
@@ -305,12 +307,13 @@
 fn run_basic_revert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool {
     let mut fails = 0;
 
+    // FIXME: this test would also pass if no swap is ever performed???
     if Caps::SwapUpgrade.present() {
         for count in 2 .. 5 {
             info!("Try revert: {}", count);
             let fl = try_revert(&flash, &areadesc, count);
             if !verify_image(&fl, images.slot0.base_off, &images.primary) {
-                warn!("Revert failure on count {}", count);
+                error!("Revert failure on count {}", count);
                 fails += 1;
             }
         }
@@ -353,8 +356,10 @@
         }
     }
 
-    info!("{} out of {} failed {:.2}%", fails, total_flash_ops,
-           fails as f32 * 100.0 / total_flash_ops as f32);
+    if fails > 0 {
+        error!("{} out of {} failed {:.2}%", fails, total_flash_ops,
+               fails as f32 * 100.0 / total_flash_ops as f32);
+    }
 
     fails > 0
 }
@@ -390,6 +395,10 @@
         fails += 1;
     }
 
+    if fails > 0 {
+        error!("Error testing perm upgrade with {} fails", total_fails);
+    }
+
     fails > 0
 }
 
@@ -401,6 +410,7 @@
         for i in 1 .. (total_count - 1) {
             info!("Try interruption at {}", i);
             if try_revert_with_fail_at(&flash, &areadesc, &images, i) {
+                error!("Revert failed at interruption {}", i);
                 fails += 1;
             }
         }
@@ -422,6 +432,9 @@
         fails += 1;
     }
 
+    //FIXME: copy_done is written by boot_go, is it ok if no copy
+    //       was ever done?
+
     if !verify_image(&fl, images.slot0.base_off, &images.upgrade) {
         warn!("Slot 0 image verification FAIL");
         fails += 1;
@@ -440,6 +453,12 @@
     // Marks image in slot0 as permanent, no revert should happen...
     mark_permanent_upgrade(&mut fl, &images.slot0);
 
+    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
+                       COPY_DONE) {
+        warn!("Mismatched trailer for Slot 0");
+        fails += 1;
+    }
+
     if c::boot_go(&mut fl, &areadesc) != 0 {
         warn!("Failed second boot");
         fails += 1;
@@ -455,6 +474,57 @@
         fails += 1;
     }
 
+    if fails > 0 {
+        error!("Error running upgrade without revert");
+    }
+
+    fails > 0
+}
+
+// Tests a new image written to slot0 that already has magic and image_ok set
+// while there is no image on slot1, so no revert should ever happen...
+fn run_norevert_newimage(flash: &SimFlash, areadesc: &AreaDesc,
+                         images: &Images) -> bool {
+    let mut fl = flash.clone();
+    let mut fails = 0;
+
+    info!("Try non-revert on imgtool generated image");
+    c::set_flash_counter(0);
+
+    mark_upgrade(&mut fl, &images.slot0);
+
+    // This simulates writing an image created by imgtool to Slot 0
+    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, UNSET) {
+        warn!("Mismatched trailer for Slot 0");
+        fails += 1;
+    }
+
+    // Run the bootloader...
+    if c::boot_go(&mut fl, &areadesc) != 0 {
+        warn!("Failed first boot");
+        fails += 1;
+    }
+
+    // State should not have changed
+    if !verify_image(&fl, images.slot0.base_off, &images.primary) {
+        warn!("Failed image verification");
+        fails += 1;
+    }
+    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET,
+                       UNSET) {
+        warn!("Mismatched trailer for Slot 0");
+        fails += 1;
+    }
+    if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET,
+                       UNSET) {
+        warn!("Mismatched trailer for Slot 1");
+        fails += 1;
+    }
+
+    if fails > 0 {
+        error!("Expected a non revert with new image");
+    }
+
     fails > 0
 }