Add flash_area_read_is_empty to sim

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/sim/mcuboot-sys/csupport/flash_map.h b/sim/mcuboot-sys/csupport/flash_map.h
index 7622dd0..cc02e7e 100644
--- a/sim/mcuboot-sys/csupport/flash_map.h
+++ b/sim/mcuboot-sys/csupport/flash_map.h
@@ -131,6 +131,14 @@
 uint8_t flash_area_erased_val(const struct flash_area *);
 
 /*
+ * Reads len bytes from off, and checks if the read data is erased.
+ *
+ * Returns 1 if erased, 0 if non-erased, and -1 on failure.
+ */
+int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
+        void *dst, uint32_t len);
+
+/*
  * Given flash area ID, return info about sectors within the area.
  */
 int flash_area_get_sectors(int fa_id, uint32_t *count,
diff --git a/sim/mcuboot-sys/csupport/run.c b/sim/mcuboot-sys/csupport/run.c
index 8238b39..17a10f1 100644
--- a/sim/mcuboot-sys/csupport/run.c
+++ b/sim/mcuboot-sys/csupport/run.c
@@ -200,6 +200,29 @@
                            len);
 }
 
+int flash_area_read_is_empty(const struct flash_area *area, uint32_t off,
+        void *dst, uint32_t len)
+{
+    uint8_t i;
+    uint8_t *u8dst;
+    int rc;
+
+    BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, area->fa_id, off, len);
+
+    rc = hal_flash_read(area->fa_device_id, area->fa_off + off, dst, len);
+    if (rc) {
+        return -1;
+    }
+
+    for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
+        if (u8dst[i] != sim_flash_erased_val) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
 int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
 {
     uint32_t i;