Test erased flash with "flash_area_read_is_empty()"

Mynewt has recently added an encrypted flash layer driver, that runs
transparently on any flash, handling reads and writes, and bypassing
other flash operations to the HW driver. As a result of this change,
checking for erased data cannot be done by read + compare to erased_val
but need to be routed to an empty check on the lower level. To do this
Mynewt added a new flash_map function called "flash_area_read_is_empty"
which checks for erased blocks (and reads/decrypts the data as well).

This commit uses `flash_area_read_is_empty` to determine if magic,
flags and swap status are erased. For Zephyr/sim commits were added
previously that mimic this functionality by simply doing the
read/compare.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index f4c5d2f..8dcca2a 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -100,34 +100,17 @@
     (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
 
 static int
-boot_magic_decode(const struct flash_area *fap, const uint32_t *magic)
+boot_magic_decode(const uint32_t *magic)
 {
-    size_t i;
-    uint8_t erased_val;
-
     if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
         return BOOT_MAGIC_GOOD;
     }
-
-    erased_val = flash_area_erased_val(fap);
-    for (i = 0; i < BOOT_MAGIC_SZ; i++) {
-        if (((uint8_t *)magic)[i] != erased_val) {
-            return BOOT_MAGIC_BAD;
-        }
-    }
-
-    return BOOT_MAGIC_UNSET;
+    return BOOT_MAGIC_BAD;
 }
 
 static int
-boot_flag_decode(const struct flash_area *fap, uint8_t flag)
+boot_flag_decode(uint8_t flag)
 {
-    uint8_t erased_val;
-
-    erased_val = flash_area_erased_val(fap);
-    if (flag == erased_val) {
-        return BOOT_FLAG_UNSET;
-    }
     if (flag != BOOT_FLAG_SET) {
         return BOOT_FLAG_BAD;
     }
@@ -228,27 +211,40 @@
     int rc;
 
     off = boot_magic_off(fap);
-    rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
-    if (rc != 0) {
+    rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
+    if (rc < 0) {
         return BOOT_EFLASH;
     }
-    state->magic = boot_magic_decode(fap, magic);
+    if (rc == 1) {
+        state->magic = BOOT_MAGIC_UNSET;
+    } else {
+        state->magic = boot_magic_decode(magic);
+    }
 
     if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
         off = boot_copy_done_off(fap);
-        rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
-        if (rc != 0) {
+        rc = flash_area_read_is_empty(fap, off, &state->copy_done,
+                sizeof state->copy_done);
+        if (rc < 0) {
             return BOOT_EFLASH;
         }
-        state->copy_done = boot_flag_decode(fap, state->copy_done);
+        if (rc == 1) {
+            state->copy_done = BOOT_FLAG_UNSET;
+        } else {
+            state->copy_done = boot_flag_decode(state->copy_done);
+        }
     }
 
     off = boot_image_ok_off(fap);
-    rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
-    if (rc != 0) {
+    rc = flash_area_read_is_empty(fap, off, &state->image_ok, sizeof state->image_ok);
+    if (rc < 0) {
         return BOOT_EFLASH;
     }
-    state->image_ok = boot_flag_decode(fap, state->image_ok);
+    if (rc == 1) {
+        state->image_ok = BOOT_FLAG_UNSET;
+    } else {
+        state->image_ok = boot_flag_decode(state->image_ok);
+    }
 
     return 0;
 }
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 66c8843..f61a29f 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -404,23 +404,21 @@
     int invalid;
     int rc;
     int i;
-    uint8_t erased_val;
 
     off = boot_status_off(fap);
     max_entries = boot_status_entries(fap);
-    erased_val = flash_area_erased_val(fap);
 
     found = 0;
     found_idx = 0;
     invalid = 0;
     for (i = 0; i < max_entries; i++) {
-        rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
-                             &status, 1);
-        if (rc != 0) {
+        rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
+                &status, 1);
+        if (rc < 0) {
             return BOOT_EFLASH;
         }
 
-        if (status == erased_val) {
+        if (rc == 1) {
             if (found && !found_idx) {
                 found_idx = i;
             }