Boot: integrate MCUBoot with TF-M to act as a BL2 bootloader

Modifications in MCUBoot to be aligned with BL2 requirements in TF-M:
 -- OS dependency was removed, no need to copy any OS repo to build it
 -- CMSIS serial driver is used
 -- flash driver interface is aligned with original version
 -- S and NS images are handeled as a single binary blob
 -- automatic image concatenation and signing at build time
 -- authentication based on SHA256 and RSA-2048 digital signature
 -- mbedTLS library is used for cryptographic operation
 -- static analyser warnings fixed in some files

Change-Id: I54891762eac8d0df634e954ff19a9505b16f3028
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
diff --git a/bl2/ext/mcuboot/bl2_main.c b/bl2/ext/mcuboot/bl2_main.c
new file mode 100644
index 0000000..565c547
--- /dev/null
+++ b/bl2/ext/mcuboot/bl2_main.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ * Copyright (c) 2017, Arm Limited.
+ *
+ * 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.
+ */
+
+#include <assert.h>
+#include "bl2_util.h"
+#include "target.h"
+#include "cmsis.h"
+#include "uart_stdout.h"
+
+
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
+#include "bootutil/bootutil_log.h"
+#include "bootutil/image.h"
+#include "bootutil/bootutil.h"
+#include "flash_map/flash_map.h"
+
+/* Avoids the semihosting issue */
+#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
+__asm("  .global __ARM_use_no_argv\n");
+#endif
+
+/* Keep these variables to be compatible with flash API */
+struct device tmp;
+struct device *boot_flash_device = &tmp;
+
+void os_heap_init(void);
+
+struct arm_vector_table {
+    uint32_t msp;
+    uint32_t reset;
+};
+
+static void do_boot(struct boot_rsp *rsp)
+{
+    /* Clang at O0, stores variables on the stack with SP relative addressing.
+     * When manually set the SP then the place of reset vector is lost.
+     * Static variables are stored in 'data' or 'bss' section, change of SP has
+     * no effect on them.
+     */
+    static struct arm_vector_table *vt;
+    uintptr_t flash_base;
+    int rc;
+
+    /* The beginning of the image is the ARM vector table, containing
+     * the initial stack pointer address and the reset vector
+     * consecutively. Manually set the stack pointer and jump into the
+     * reset vector
+     */
+    rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
+    assert(rc == 0);
+
+    vt = (struct arm_vector_table *)(flash_base +
+                                     rsp->br_image_off +
+                                     rsp->br_hdr->ih_hdr_size);
+    __disable_irq();
+    __set_MSP(vt->msp);
+    __DSB();
+    __ISB();
+
+    ((void (*)(void))vt->reset)();
+}
+
+int main(void)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    uart_init(UART0_CHANNEL);
+
+    BOOT_LOG_INF("Starting bootloader");
+
+    os_heap_init();
+
+    rc = boot_go(&rsp);
+    if (rc != 0) {
+        BOOT_LOG_ERR("Unable to find bootable image");
+        while (1)
+            ;
+    }
+
+    BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
+                 rsp.br_image_off);
+    flash_area_warn_on_open();
+    BOOT_LOG_INF("Jumping to the first image slot");
+    do_boot(&rsp);
+
+    BOOT_LOG_ERR("Never should get here");
+    while (1)
+        ;
+}