Enable support for building mcuboot for Mbed with direct-xip

This commit also introduces changes that allow users to build for other non-swap type update methods (overwrite only, swap using move, direct xip, or RAM loading). Changes include:

- Adding configuration options relating to XIP
- Updating the Mbed flash map backend to be compatible with XIP updates
- Add default secondary_bd in internal flash for XIP on Mbed OS.

Signed-off-by: George Beckstein <becksteing@embeddedplanet.com>
diff --git a/boot/mbed/mbed_lib.json b/boot/mbed/mbed_lib.json
index 7c9ccba..ef16f0c 100644
--- a/boot/mbed/mbed_lib.json
+++ b/boot/mbed/mbed_lib.json
@@ -160,6 +160,20 @@
             "help": "Share data (NOT TESTED)",
             "macro_name": "MCUBOOT_DATA_SHARING",
             "value": null
+        },
+        "direct-xip": {
+            "help": "Enable ability to boot update candidates in-place.",
+            "macro_name": "MCUBOOT_DIRECT_XIP",
+            "value": null
+        },
+        "direct-xip-revert": {
+            "help": "Enable XIP revert mechanism. Only valid if direct-xip is also enabled.",
+            "macro_name": "MCUBOOT_DIRECT_XIP_REVERT",
+            "value": null
+        },
+        "xip-secondary-slot-address": {
+            "help": "Specify start address for secondary slot address in XIP-accessible memory. This is required if direct-xip is enabled.",
+            "value": null
         }
     }
 }
diff --git a/boot/mbed/src/flash_map_backend.cpp b/boot/mbed/src/flash_map_backend.cpp
index a867e49..28afec8 100644
--- a/boot/mbed/src/flash_map_backend.cpp
+++ b/boot/mbed/src/flash_map_backend.cpp
@@ -27,6 +27,8 @@
 
 #include "mcuboot_config/mcuboot_logging.h"
 
+#include "bootutil_priv.h"
+
 #define FLASH_DEVICE_INTERNAL_FLASH 0
 #define FLASH_AREAS 3
 
@@ -36,7 +38,7 @@
 /** Internal application block device */
 static FlashIAPBlockDevice mcuboot_primary_bd(MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE);
 
-#ifndef MCUBOOT_OVERWRITE_ONLY
+#if MCUBOOT_SWAP_USING_SCRATCH
 /** Scratch space is at the end of internal flash, after the main application */
 static FlashIAPBlockDevice mcuboot_scratch_bd(MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE);
 #endif
@@ -44,7 +46,7 @@
 static mbed::BlockDevice* flash_map_bd[FLASH_AREAS] = {
         (mbed::BlockDevice*) &mcuboot_primary_bd,       /** Primary (loadable) image area */
         mcuboot_secondary_bd,                           /** Secondary (update candidate) image area */
-#ifndef MCUBOOT_OVERWRITE_ONLY
+#if MCUBOOT_SWAP_USING_SCRATCH
         (mbed::BlockDevice*) &mcuboot_scratch_bd        /** Scratch space for swapping images */
 #else
         nullptr
@@ -66,10 +68,13 @@
             fap->fa_off = MCUBOOT_PRIMARY_SLOT_START_ADDR;
             break;
         case SECONDARY_ID:
-            // The offset of the secondary slot is not currently used.
+#if MCUBOOT_DIRECT_XIP
+            fap->fa_off = MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS;
+#else
             fap->fa_off = 0;
+#endif
             break;
-#ifndef MCUBOOT_OVERWRITE_ONLY
+#if MCUBOOT_SWAP_USING_SCRATCH
         case SCRATCH_ID:
             fap->fa_off = MCUBOOT_SCRATCH_START_ADDR;
             break;
@@ -140,13 +145,15 @@
     if (len != 0) {
 #endif
         if (!bd->is_valid_read(off, len)) {
-            MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off, len);
+            MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
+                    (unsigned int) off, (unsigned int) len);
             return -1;
         }
         else {
             int ret = bd->read(dst, off, len);
             if (ret != 0) {
-                MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x (%d)", fap->fa_id, off, len, ret);
+                MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
+                        (unsigned int) off, (unsigned int) len);
                 return ret;
             }
         }
@@ -155,7 +162,8 @@
 
     if (remainder) {
         if (!bd->is_valid_read(off + len, read_size)) {
-            MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off + len, read_size);
+            MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
+                    (unsigned int) (off + len), (unsigned int) read_size);
             return -1;
         }
         else {
diff --git a/boot/mbed/src/secondary_bd.cpp b/boot/mbed/src/secondary_bd.cpp
new file mode 100644
index 0000000..40eb4c8
--- /dev/null
+++ b/boot/mbed/src/secondary_bd.cpp
@@ -0,0 +1,40 @@
+/*
+ * Mbed-OS Microcontroller Library
+ * Copyright (c) 2020 Embedded Planet
+ * Copyright (c) 2020 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#if MCUBOOT_DIRECT_XIP
+
+#include "flash_map_backend/secondary_bd.h"
+#include "platform/mbed_toolchain.h"
+#include "FlashIAPBlockDevice.h"
+
+/**
+ * For an XIP build, the secondary BD is provided by mcuboot by default.
+ *
+ * This is a weak symbol so the user can override it.
+ */
+MBED_WEAK mbed::BlockDevice* get_secondary_bd(void) {
+    static FlashIAPBlockDevice secondary_bd(MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS,
+            MCUBOOT_SLOT_SIZE);
+
+    return &secondary_bd;
+}
+
+#endif
+
+