Boot: Synchronize MCUBoot code base

Perform a partial synchronization between the MCUBoot files in TF-M
and in the original repository. The hash of the source commit in the
original repository: 4f0ea747c314547daa6b6299ccbd77ae4dee6758.

Main changes:
- Remove current_image global variable and make it part
  of the boot state struct
- Update routines to receive the boot state by parameter
- Refactor dependency check functions
- Reorganize the flash map and related files
- Fix swap status control

Change-Id: Ibe948792b306e96282fb82447bb3f05a0c6389ef
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/include/config-boot.h b/bl2/ext/mcuboot/include/config-rsa.h
similarity index 85%
rename from bl2/ext/mcuboot/include/config-boot.h
rename to bl2/ext/mcuboot/include/config-rsa.h
index 352c117..33a356b 100644
--- a/bl2/ext/mcuboot/include/config-boot.h
+++ b/bl2/ext/mcuboot/include/config-rsa.h
@@ -1,9 +1,8 @@
 /*
  *  Minimal configuration for using TLS in the bootloader
  *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ *  Copyright (C) 2006-2019, Arm Limited. All rights reserved.
  *  Copyright (C) 2016, Linaro Ltd
- *  Copyright (c) 2019, Arm Limited.
  *
  *  SPDX-License-Identifier: Apache-2.0
  *
@@ -23,13 +22,19 @@
  */
 
 /*
+ * Original code taken from mcuboot project at:
+ * https://github.com/JuulLabs-OSS/mcuboot
+ * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
+ */
+
+/*
  * Minimal configuration for using TLS in the bootloader
  *
  * - RSA signature verification
  */
 
-#ifndef MBEDTLS_CONFIG_BOOT_H
-#define MBEDTLS_CONFIG_BOOT_H
+#ifndef MCUBOOT_MBEDTLS_CONFIG_RSA
+#define MCUBOOT_MBEDTLS_CONFIG_RSA
 
 /* System support */
 #define MBEDTLS_PLATFORM_C
@@ -80,4 +85,4 @@
 
 #include "mbedtls/check_config.h"
 
-#endif /* MBEDTLS_CONFIG_BOOT_H */
+#endif /* MCUBOOT_MBEDTLS_CONFIG_RSA */
diff --git a/bl2/ext/mcuboot/include/flash_map/flash_map.h b/bl2/ext/mcuboot/include/flash_map/flash_map.h
index 8c73d8f..df8c6dd 100644
--- a/bl2/ext/mcuboot/include/flash_map/flash_map.h
+++ b/bl2/ext/mcuboot/include/flash_map/flash_map.h
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c
+ * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
@@ -50,27 +50,41 @@
  */
 #include <inttypes.h>
 
-extern uint8_t current_image;
-
 #if (MCUBOOT_IMAGE_NUMBER == 1)
-#define FLASH_AREA_IMAGE_PRIMARY     FLASH_AREA_0_ID
-#define FLASH_AREA_IMAGE_SECONDARY   FLASH_AREA_2_ID
+/*
+ * NOTE: the definition below returns the same values for true/false on
+ * purpose, to avoid having to mark x as non-used by all callers when
+ * running in single image mode.
+ */
+#define FLASH_AREA_IMAGE_PRIMARY(x)     (((x) == 0) ? FLASH_AREA_0_ID : \
+                                                      FLASH_AREA_0_ID)
+#define FLASH_AREA_IMAGE_SECONDARY(x)   (((x) == 0) ? FLASH_AREA_2_ID : \
+                                                      FLASH_AREA_2_ID)
 #elif (MCUBOOT_IMAGE_NUMBER == 2)
 /* MCUBoot currently supports only up to 2 updatable firmware images.
  * If the number of the current image is greater than MCUBOOT_IMAGE_NUMBER - 1
  * then a dummy value will be assigned to the flash area macros.
  */
-#define FLASH_AREA_IMAGE_PRIMARY     ((current_image == 0) ? FLASH_AREA_0_ID : \
-                                      (current_image == 1) ? FLASH_AREA_1_ID : \
-                                                             255 )
-#define FLASH_AREA_IMAGE_SECONDARY   ((current_image == 0) ? FLASH_AREA_2_ID : \
-                                      (current_image == 1) ? FLASH_AREA_3_ID : \
-                                                             255 )
+#define FLASH_AREA_IMAGE_PRIMARY(x)     (((x) == 0) ? FLASH_AREA_0_ID : \
+                                         ((x) == 1) ? FLASH_AREA_1_ID : \
+                                                      255 )
+#define FLASH_AREA_IMAGE_SECONDARY(x)   (((x) == 0) ? FLASH_AREA_2_ID : \
+                                         ((x) == 1) ? FLASH_AREA_3_ID : \
+                                                      255 )
 #else
 #error "Image slot and flash area mapping is not defined"
 #endif
 
-#define FLASH_AREA_IMAGE_SCRATCH     FLASH_AREA_SCRATCH_ID
+#define FLASH_AREA_IMAGE_SCRATCH        FLASH_AREA_SCRATCH_ID
+
+/*
+ * For now, we only support one flash device.
+ *
+ * Pick a random device ID for it that's unlikely to collide with
+ * anything "real".
+ */
+#define FLASH_DEVICE_ID                 100
+#define FLASH_DEVICE_BASE               FLASH_BASE_ADDRESS
 
 /**
  * @brief Structure describing an area on a flash device.
@@ -146,27 +160,29 @@
 int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
                     uint32_t len);
 
-/*
- * Returns 1 if read data is erased, 0 if non-erased, and -1 on failure.
- */
-int flash_area_read_is_empty(const struct flash_area *area, uint32_t off,
-                             void *dst, uint32_t len);
-
 int flash_area_write(const struct flash_area *area, uint32_t off,
                      const void *src, uint32_t len);
 
 int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len);
 
 /*
+ * Alignment restriction for flash writes.
+ */
+uint32_t flash_area_align(const struct flash_area *area);
+
+/*
  * Returns the value expected to be read when accessing any erased
  * flash byte.
  */
 uint8_t flash_area_erased_val(const struct flash_area *area);
 
 /*
- * Alignment restriction for flash writes.
+ * 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.
  */
-uint32_t flash_area_align(const struct flash_area *area);
+int flash_area_read_is_empty(const struct flash_area *area, uint32_t off,
+                             void *dst, uint32_t len);
 
 /*
  * Given flash area ID, return info about sectors within the area.
@@ -182,8 +198,9 @@
 int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
 
 int flash_area_id_from_image_slot(int slot);
+int flash_area_id_from_multi_image_slot(int image_index, int slot);
 int flash_area_id_to_image_slot(int area_id);
-void flash_area_warn_on_open(void);
+int flash_area_id_to_multi_image_slot(int image_index, int area_id);
 
 #ifdef __cplusplus
 }
diff --git a/bl2/ext/mcuboot/include/target.h b/bl2/ext/mcuboot/include/target.h
index 256f725..69f691a 100644
--- a/bl2/ext/mcuboot/include/target.h
+++ b/bl2/ext/mcuboot/include/target.h
@@ -5,6 +5,12 @@
  *  SPDX-License-Identifier: Apache-2.0
  */
 
+/*
+ * Original code taken from mcuboot project at:
+ * https://github.com/JuulLabs-OSS/mcuboot
+ * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
+ */
+
 #ifndef H_TARGETS_TARGET_
 #define H_TARGETS_TARGET_