Boot: Add multiple flash driver support

(T862) External flash support on stm uses 2 drivers :
- internal flash slot 0/1, scratch : driver internal flash
- external flash slot 2/3 : octospi driver external flash

Change-Id: I58d86a48081ad0bca3dda961318a15fe17248a93
Signed-off-by: Michel Jaouen <michel.jaouen@st.com>
diff --git a/bl2/ext/mcuboot/flash_map_extended.c b/bl2/ext/mcuboot/flash_map_extended.c
index cf7d3f5..e854b55 100644
--- a/bl2/ext/mcuboot/flash_map_extended.c
+++ b/bl2/ext/mcuboot/flash_map_extended.c
@@ -14,16 +14,14 @@
 
 #include <errno.h>
 #include "target.h"
+#include "cmsis.h"
 #include "Driver_Flash.h"
 #include "sysflash/sysflash.h"
 #include "flash_map/flash_map.h"
 #include "flash_map_backend/flash_map_backend.h"
 #include "bootutil/bootutil_log.h"
 
-/* Flash device name must be specified by target */
-extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
-
-int flash_device_base(uint8_t fd_id, uintptr_t *ret)
+__WEAK int flash_device_base(uint8_t fd_id, uintptr_t *ret)
 {
     if (fd_id != FLASH_DEVICE_ID) {
         BOOT_LOG_ERR("invalid flash ID %d; expected %d",
@@ -75,9 +73,7 @@
 
 uint8_t flash_area_erased_val(const struct flash_area *fap)
 {
-    (void)fap;
-
-    return FLASH_DEV_NAME.GetInfo()->erased_value;
+    return DRV_FLASH_AREA(fap)->GetInfo()->erased_value;
 }
 
 int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
@@ -90,7 +86,7 @@
     BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x",
                  fa->fa_id, off, len);
 
-    rc = FLASH_DEV_NAME.ReadData(fa->fa_off + off, dst, len);
+    rc = DRV_FLASH_AREA(fa)->ReadData(fa->fa_off + off, dst, len);
     if (rc) {
         return -1;
     }
diff --git a/bl2/ext/mcuboot/include/flash_map/flash_map.h b/bl2/ext/mcuboot/include/flash_map/flash_map.h
index d858ef4..b66dfc0 100644
--- a/bl2/ext/mcuboot/include/flash_map/flash_map.h
+++ b/bl2/ext/mcuboot/include/flash_map/flash_map.h
@@ -50,6 +50,7 @@
  */
 #include <inttypes.h>
 #include "region_defs.h"
+#include "Driver_Flash.h"
 
 /*
  * For now, we only support one flash device.
@@ -91,6 +92,11 @@
     uint16_t pad16;
 
     /**
+     * Pointer to driver
+     */
+    ARM_DRIVER_FLASH *fa_driver;
+
+    /**
      * This area's offset, relative to the beginning of its flash
      * device's storage.
      */
@@ -121,6 +127,12 @@
     uint32_t fs_size;
 };
 
+/**
+ * @brief Macro retrieving driver from struct flash area
+ *
+ */
+#define DRV_FLASH_AREA(area) ((area)->fa_driver)
+
 /*
  * Start using flash area.
  */
diff --git a/bl2/ext/mcuboot/include/target.h b/bl2/ext/mcuboot/include/target.h
index d4350b0..9ead129 100644
--- a/bl2/ext/mcuboot/include/target.h
+++ b/bl2/ext/mcuboot/include/target.h
@@ -47,6 +47,11 @@
 #error "FLASH_AREA_0_SIZE must be defined by the target"
 #endif
 
+#if defined(FLASH_DEV_NAME_0) != defined(FLASH_DEVICE_ID_0)
+#error "FLASH DEV_NAME_0 and DEVICE_ID_0 must be simultaneously defined or not \
+by target"
+#endif
+
 #ifndef FLASH_AREA_2_OFFSET
 #error "FLASH_AREA_2_OFFSET must be defined by the target"
 #endif
@@ -55,6 +60,11 @@
 #error "FLASH_AREA_2_SIZE must be defined by the target"
 #endif
 
+#if defined(FLASH_DEV_NAME_2) != defined(FLASH_DEVICE_ID_2)
+#error "FLASH DEV_NAME_2 and DEVICE_ID_2 must be simultaneously defined or not \
+by target"
+#endif
+
 #if (MCUBOOT_IMAGE_NUMBER == 2)
 #ifndef FLASH_AREA_1_OFFSET
 #error "FLASH_AREA_1_OFFSET must be defined by the target"
@@ -64,6 +74,11 @@
 #error "FLASH_AREA_1_SIZE must be defined by the target"
 #endif
 
+#if defined(FLASH_DEV_NAME_1) != defined(FLASH_DEVICE_ID_1)
+#error "FLASH DEV_NAME_1 and DEVICE_ID_1 must be simultaneously defined or not \
+by target"
+#endif
+
 #ifndef FLASH_AREA_3_OFFSET
 #error "FLASH_AREA_3_OFFSET must be defined by the target"
 #endif
@@ -71,6 +86,11 @@
 #ifndef FLASH_AREA_3_SIZE
 #error "FLASH_AREA_3_SIZE must be defined by the target"
 #endif
+
+#if defined(FLASH_DEV_NAME_3) != defined(FLASH_DEVICE_ID_3)
+#error "FLASH DEV_NAME_3 and DEVICE_ID_3 must be simultaneously defined or not \
+by target"
+#endif
 #endif /* (MCUBOOT_IMAGE_NUMBER == 2) */
 
 #ifndef FLASH_AREA_SCRATCH_OFFSET
@@ -81,6 +101,11 @@
 #error "FLASH_AREA_SCRATCH_SIZE must be defined by the target"
 #endif
 
+#if defined(FLASH_DEV_NAME_SCRATCH) != defined(FLASH_DEVICE_ID_SCRATCH)
+#error "FLASH DEV_NAME_SCRATCH and DEVICE_ID_SCRATCH must be simultaneously defined \
+or not by target"
+#endif
+
 #ifndef FLASH_DEV_NAME
 #error "BL2 supports CMSIS flash interface and device name must be specified"
 #endif