Boot: Sync MCUBoot code base

The MCUBoot files were added from the original MCUBoot
repository with version 1.0.0 and since then the code bases
have diverged from each other significantly. This patch
performs a partial synchronization between the MCUBoot files
in TF-M and in the original repo.
The hash of source commit in the original MCUBoot repo:
178be54bd6e5f035cc60e98205535682acd26e64.

The flash interface has also been extended and this patch
includes the implementation of these new functions too.

Change-Id: I16897ba884b56f90c9d35f99b709de86704a11ad
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
index a22e6c1..79fdc9a 100644
--- a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
+++ b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
@@ -26,8 +26,6 @@
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
 #include "bootutil_priv.h"
-
-#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
 
 int boot_current_slot;
@@ -39,11 +37,13 @@
     0x8079b62c,
 };
 
+#define BOOT_MAGIC_ARR_SZ \
+    (sizeof boot_img_magic / sizeof boot_img_magic[0])
+
 const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic);
 const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
 
 struct boot_swap_table {
-    /** * For each field, a value of 0 means "any". */
     uint8_t magic_slot0;
     uint8_t magic_slot1;
     uint8_t image_ok_slot0;
@@ -65,27 +65,27 @@
  */
 static const struct boot_swap_table boot_swap_tables[] = {
     {
-        .magic_slot0 =      0,
+        .magic_slot0 =      BOOT_MAGIC_ANY,
         .magic_slot1 =      BOOT_MAGIC_GOOD,
-        .image_ok_slot0 =   0,
-        .image_ok_slot1 =   0xff,
-        .copy_done_slot0 =  0,
+        .image_ok_slot0 =   BOOT_FLAG_ANY,
+        .image_ok_slot1 =   BOOT_FLAG_UNSET,
+        .copy_done_slot0 =  BOOT_FLAG_ANY,
         .swap_type =        BOOT_SWAP_TYPE_TEST,
     },
     {
-        .magic_slot0 =      0,
+        .magic_slot0 =      BOOT_MAGIC_ANY,
         .magic_slot1 =      BOOT_MAGIC_GOOD,
-        .image_ok_slot0 =   0,
-        .image_ok_slot1 =   0x01,
-        .copy_done_slot0 =  0,
+        .image_ok_slot0 =   BOOT_FLAG_ANY,
+        .image_ok_slot1 =   BOOT_FLAG_SET,
+        .copy_done_slot0 =  BOOT_FLAG_ANY,
         .swap_type =        BOOT_SWAP_TYPE_PERM,
     },
     {
         .magic_slot0 =      BOOT_MAGIC_GOOD,
         .magic_slot1 =      BOOT_MAGIC_UNSET,
-        .image_ok_slot0 =   0xff,
-        .image_ok_slot1 =   0,
-        .copy_done_slot0 =  0x01,
+        .image_ok_slot0 =   BOOT_FLAG_UNSET,
+        .image_ok_slot1 =   BOOT_FLAG_ANY,
+        .copy_done_slot0 =  BOOT_FLAG_SET,
         .swap_type =        BOOT_SWAP_TYPE_REVERT,
     },
 };
@@ -93,22 +93,22 @@
 #define BOOT_SWAP_TABLES_COUNT \
     (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0]))
 
-int
-boot_magic_code(const uint32_t *magic)
+static int
+boot_magic_decode(const uint32_t *magic)
 {
-    int i;
-
     if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
         return BOOT_MAGIC_GOOD;
     }
+    return BOOT_MAGIC_BAD;
+}
 
-    for (i = 0; i < BOOT_MAGIC_SZ / sizeof(*magic); i++) {
-        if (magic[i] != 0xffffffff) {
-            return BOOT_MAGIC_BAD;
-        }
+static int
+boot_flag_decode(uint8_t flag)
+{
+    if (flag != BOOT_FLAG_SET) {
+        return BOOT_FLAG_BAD;
     }
-
-    return BOOT_MAGIC_UNSET;
+    return BOOT_FLAG_SET;
 }
 
 uint32_t
@@ -200,31 +200,45 @@
 boot_read_swap_state(const struct flash_area *fap,
                      struct boot_swap_state *state)
 {
-    uint32_t magic[BOOT_MAGIC_SZ];
+    uint32_t magic[BOOT_MAGIC_ARR_SZ];
     uint32_t off;
     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_code(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;
         }
+        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;
     }
+    if (rc == 1) {
+        state->image_ok = BOOT_FLAG_UNSET;
+    } else {
+        state->image_ok = boot_flag_decode(state->image_ok);
+    }
 
     return 0;
 }
@@ -259,7 +273,7 @@
 int
 boot_read_swap_size(uint32_t *swap_size)
 {
-    uint32_t magic[BOOT_MAGIC_SZ];
+    uint32_t magic[BOOT_MAGIC_ARR_SZ];
     uint32_t off;
     const struct flash_area *fap;
     int rc;
@@ -341,6 +355,7 @@
     int rc;
     uint8_t buf[BOOT_MAX_ALIGN];
     uint8_t align;
+    uint8_t erased_val;
 
     switch (flag) {
     case BOOT_FLAG_COPY_DONE:
@@ -355,7 +370,8 @@
 
     align = flash_area_align(fap);
     assert(align <= BOOT_MAX_ALIGN);
-    memset(buf, 0xFF, BOOT_MAX_ALIGN);
+    erased_val = flash_area_erased_val(fap);
+    memset(buf, erased_val, BOOT_MAX_ALIGN);
     buf[0] = BOOT_FLAG_SET;
 
     rc = flash_area_write(fap, off, buf, align);
@@ -385,6 +401,7 @@
     int rc;
     uint8_t buf[BOOT_MAX_ALIGN];
     uint8_t align;
+    uint8_t erased_val;
 
     off = boot_swap_size_off(fap);
     align = flash_area_align(fap);
@@ -392,7 +409,8 @@
     if (align < sizeof(swap_size)) {
         align = sizeof(swap_size);
     }
-    memset(buf, 0xFF, BOOT_MAX_ALIGN);
+    erased_val = flash_area_erased_val(fap);
+    memset(buf, erased_val, BOOT_MAX_ALIGN);
     memcpy(buf, (uint8_t *)&swap_size, sizeof(swap_size));
 
     rc = flash_area_write(fap, off, buf, align);
@@ -410,7 +428,7 @@
     struct boot_swap_state slot0;
     struct boot_swap_state slot1;
     int rc;
-    int i;
+    size_t i;
 
     rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
     if (rc) {
@@ -425,11 +443,16 @@
     for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
         table = boot_swap_tables + i;
 
-        if ((!table->magic_slot0     || table->magic_slot0     == slot0.magic    ) &&
-            (!table->magic_slot1     || table->magic_slot1     == slot1.magic    ) &&
-            (!table->image_ok_slot0  || table->image_ok_slot0  == slot0.image_ok ) &&
-            (!table->image_ok_slot1  || table->image_ok_slot1  == slot1.image_ok ) &&
-            (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) {
+        if ((table->magic_slot0 == BOOT_MAGIC_ANY ||
+                    table->magic_slot0 == slot0.magic) &&
+            (table->magic_slot1 == BOOT_MAGIC_ANY ||
+                    table->magic_slot1 == slot1.magic) &&
+            (table->image_ok_slot0 == BOOT_FLAG_ANY ||
+                    table->image_ok_slot0 == slot0.image_ok) &&
+            (table->image_ok_slot1 == BOOT_FLAG_ANY ||
+                    table->image_ok_slot1 == slot1.image_ok) &&
+            (table->copy_done_slot0 == BOOT_FLAG_ANY ||
+                    table->copy_done_slot0 == slot0.copy_done)) {
             BOOT_LOG_INF("Swap type: %s",
                          table->swap_type == BOOT_SWAP_TYPE_TEST   ? "test"   :
                          table->swap_type == BOOT_SWAP_TYPE_PERM   ? "perm"   :
@@ -530,12 +553,13 @@
 
     if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
         /* Swap never completed.  This is unexpected. */
-        return BOOT_EBADVECT;
+        rc = BOOT_EBADVECT;
+        goto done;
     }
 
     if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
         /* Already confirmed. */
-        return 0;
+        goto done;
     }
 
     rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);