Boot: introduce CMSIS flash interface and driver

Replace legacy flash interface and driver with
CMSIS compliant version:
 -- remove legacy flash interface
 -- add CMSIS compliant version

Change-Id: I8798d191d8f806500621f76c3c3f62d75aceb7cb
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index 744e83c..12336fd 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -59,7 +59,6 @@
 #Append all our source files to global lists.
 list(APPEND ALL_SRC_C "${MCUBOOT_DIR}/bl2_main.c"
 		"${MCUBOOT_DIR}/flash_map.c"
-		"${MCUBOOT_DIR}/hal_flash.c"
 		"${MCUBOOT_DIR}/os.c"
 		"${MCUBOOT_DIR}/keys.c"
 		"${MCUBOOT_DIR}/bootutil/src/loader.c"
@@ -122,7 +121,6 @@
 							MCUBOOT_VALIDATE_SLOT0
 							MCUBOOT_USE_FLASH_AREA_GET_SECTORS
 							MBEDTLS_CONFIG_FILE="config-boot.h"
-							MCUBOOT_TARGET_CONFIG="flash_layout.h"
 						)
 
 #Finally let cmake system apply changes after the whole project is defined.
diff --git a/bl2/ext/mcuboot/bl2_main.c b/bl2/ext/mcuboot/bl2_main.c
index 565c547..952e528 100644
--- a/bl2/ext/mcuboot/bl2_main.c
+++ b/bl2/ext/mcuboot/bl2_main.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2012-2014 Wind River Systems, Inc.
- * Copyright (c) 2017, Arm Limited.
+ * Copyright (c) 2017-2018 Arm Limited.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
 #include "target.h"
 #include "cmsis.h"
 #include "uart_stdout.h"
-
+#include "Driver_Flash.h"
 
 #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
@@ -33,9 +33,8 @@
 __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;
+/* Flash device name must be specified by target */
+extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
 
 void os_heap_init(void);
 
@@ -85,6 +84,8 @@
 
     os_heap_init();
 
+    /* Initialize Flash driver */
+    FLASH_DEV_NAME.Initialize(NULL);
     rc = boot_go(&rsp);
     if (rc != 0) {
         BOOT_LOG_ERR("Unable to find bootable image");
diff --git a/bl2/ext/mcuboot/bootutil/include/bootutil/image.h b/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
index 9a4535c..56cd5f4 100644
--- a/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
+++ b/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
@@ -48,11 +48,6 @@
 #define IMAGE_F_RAM_LOAD                 0x00000020
 
 /*
- * ECSDA224 is with NIST P-224
- * ECSDA256 is with NIST P-256
- */
-
-/*
  * Image trailer TLV types.
  *
  * Signature is generated by computing signature over the image hash.
diff --git a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
index 494879c..a22e6c1 100644
--- a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
+++ b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
@@ -22,7 +22,6 @@
 #include <inttypes.h>
 #include <stddef.h>
 
-#include "hal/hal_flash.h"
 #include "flash_map/flash_map.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
@@ -354,7 +353,7 @@
         return BOOT_EBADARGS;
     }
 
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     assert(align <= BOOT_MAX_ALIGN);
     memset(buf, 0xFF, BOOT_MAX_ALIGN);
     buf[0] = BOOT_FLAG_SET;
@@ -388,7 +387,7 @@
     uint8_t align;
 
     off = boot_swap_size_off(fap);
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     assert(align <= BOOT_MAX_ALIGN);
     if (align < sizeof(swap_size)) {
         align = sizeof(swap_size);
diff --git a/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h b/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
index 4753673..9c3d258 100644
--- a/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
+++ b/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
@@ -84,7 +84,7 @@
 };
 
 #define BOOT_STATUS_STATE_COUNT 3
-#define BOOT_STATUS_MAX_ENTRIES 128
+#define BOOT_STATUS_MAX_ENTRIES 256
 
 #define BOOT_STATUS_SOURCE_NONE    0
 #define BOOT_STATUS_SOURCE_SCRATCH 1
@@ -102,7 +102,7 @@
 #define BOOT_NUM_SLOTS              2
 
 /** Maximum number of image sectors supported by the bootloader. */
-#define BOOT_MAX_IMG_SECTORS        120
+#define BOOT_MAX_IMG_SECTORS        256
 
 /**
  * Compatibility shim for flash sector type.
diff --git a/bl2/ext/mcuboot/bootutil/src/image_validate.c b/bl2/ext/mcuboot/bootutil/src/image_validate.c
index 2dca5bd..d7fb7cd 100644
--- a/bl2/ext/mcuboot/bootutil/src/image_validate.c
+++ b/bl2/ext/mcuboot/bootutil/src/image_validate.c
@@ -22,7 +22,6 @@
 #include <inttypes.h>
 #include <string.h>
 
-#include "hal/hal_flash.h"
 #include "flash_map/flash_map.h"
 #include "bootutil/image.h"
 #include "bootutil/sha256.h"
diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c
index d091ec5..29becfa 100644
--- a/bl2/ext/mcuboot/bootutil/src/loader.c
+++ b/bl2/ext/mcuboot/bootutil/src/loader.c
@@ -34,7 +34,7 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
-#include <hal/hal_flash.h>
+#include "flash_map/flash_map.h"
 #include <os/os_malloc.h>
 #include "bootutil/bootutil.h"
 #include "bootutil/image.h"
@@ -302,15 +302,25 @@
 static uint8_t
 boot_write_sz(void)
 {
+    const struct flash_area *fap;
     uint8_t elem_sz;
     uint8_t align;
+    int rc;
 
     /* Figure out what size to write update status update as.  The size depends
      * on what the minimum write size is for scratch area, active image slot.
      * We need to use the bigger of those 2 values.
      */
-    elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
-    align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+    assert(rc == 0);
+    elem_sz = flash_area_align(fap);
+    flash_area_close(fap);
+
+    rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
+    assert(rc == 0);
+    align = flash_area_align(fap);
+    flash_area_close(fap);
+
     if (align > elem_sz) {
         elem_sz = align;
     }
@@ -507,7 +517,7 @@
           boot_status_internal_off(bs->idx, bs->state,
                                    BOOT_WRITE_SZ(&boot_data));
 
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     memset(buf, 0xFF, BOOT_MAX_ALIGN);
     buf[0] = bs->state;
 
diff --git a/bl2/ext/mcuboot/flash_map.c b/bl2/ext/mcuboot/flash_map.c
index 0fb8b1a..b476517 100644
--- a/bl2/ext/mcuboot/flash_map.c
+++ b/bl2/ext/mcuboot/flash_map.c
@@ -21,15 +21,16 @@
 #include <stdbool.h>
 
 #include "target.h"
-#include <flash.h>
+#include "bl2_util.h"
+#include "Driver_Flash.h"
 
 #include <flash_map/flash_map.h>
-#include <hal/hal_flash.h>
 
 #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
 
-extern struct device *boot_flash_device;
+/* Flash device name must be specified by target */
+extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
 
 /*
  * For now, we only support one flash device.
@@ -157,36 +158,51 @@
 int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
             uint32_t len)
 {
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    return flash_read(boot_flash_device, area->fa_off + off, dst, len);
+    BOOT_LOG_DBG("read  area=%d, off=%#x, len=%#x", area->fa_id, off, len);
+    return FLASH_DEV_NAME.ReadData(area->fa_off + off, dst, len);
 }
 
 int flash_area_write(const struct flash_area *area, uint32_t off,
                      const void *src, uint32_t len)
 {
-    int rc = 0;
-
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    flash_write_protection_set(boot_flash_device, false);
-    rc = flash_write(boot_flash_device, area->fa_off + off, src, len);
-    flash_write_protection_set(boot_flash_device, true);
-    return rc;
+    BOOT_LOG_DBG("write area=%d, off=%#x, len=%#x", area->fa_id, off, len);
+    return FLASH_DEV_NAME.ProgramData(area->fa_off + off, src, len);
 }
 
 int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
 {
-    int rc;
+    ARM_FLASH_INFO *flash_info;
+    uint32_t deleted_len = 0;
+    int32_t rc = 0;
 
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    flash_write_protection_set(boot_flash_device, false);
-    rc = flash_erase(boot_flash_device, area->fa_off + off, len);
-    flash_write_protection_set(boot_flash_device, true);
+    BOOT_LOG_DBG("erase area=%d, off=%#x, len=%#x", area->fa_id, off, len);
+    flash_info = FLASH_DEV_NAME.GetInfo();
+
+    if (flash_info->sector_info == NULL) {
+        /* Uniform sector layout */
+        while (deleted_len < len) {
+            rc = FLASH_DEV_NAME.EraseSector(area->fa_off + off);
+            if (rc != 0) {
+                break;
+            }
+            deleted_len += flash_info->sector_size;
+            off         += flash_info->sector_size;
+        }
+    } else {
+        /* Inhomogeneous sector layout, explicitly defined
+         * Currently not supported.
+         */
+    }
+
     return rc;
 }
 
 uint8_t flash_area_align(const struct flash_area *area)
 {
-    return hal_flash_align(area->fa_id);
+    ARM_FLASH_INFO *flash_info;
+
+    flash_info = FLASH_DEV_NAME.GetInfo();
+    return flash_info->program_unit;
 }
 
 /*
@@ -261,7 +277,7 @@
         rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
     }
 
-    if (*cnt >= max_cnt) {
+    if (*cnt > max_cnt) {
         BOOT_LOG_ERR("flash area %d sector count overflow", idx);
         return -1;
     }
@@ -305,7 +321,7 @@
         rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
     }
 
-    if (*cnt >= max_cnt) {
+    if (*cnt > max_cnt) {
         BOOT_LOG_ERR("flash area %d sector count overflow", idx);
         return -1;
     }
diff --git a/bl2/ext/mcuboot/hal_flash.c b/bl2/ext/mcuboot/hal_flash.c
deleted file mode 100644
index 60d9271..0000000
--- a/bl2/ext/mcuboot/hal_flash.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 "target.h"
-
-#include "hal/hal_flash.h"
-
-uint8_t hal_flash_align(uint8_t flash_id)
-{
-    return FLASH_ALIGN;
-}
diff --git a/bl2/ext/mcuboot/include/bl2_util.h b/bl2/ext/mcuboot/include/bl2_util.h
index 334c2a0..8315681 100644
--- a/bl2/ext/mcuboot/include/bl2_util.h
+++ b/bl2/ext/mcuboot/include/bl2_util.h
@@ -30,10 +30,6 @@
 #define CONTAINER_OF(ptr, type, field) \
         ((type *)(((char *)(ptr)) - offsetof(type, field)))
 
-struct device {
-    int device_id;
-};
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/bl2/ext/mcuboot/include/hal/hal_flash.h b/bl2/ext/mcuboot/include/hal/hal_flash.h
deleted file mode 100644
index 2895479..0000000
--- a/bl2/ext/mcuboot/include/hal/hal_flash.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-
-#ifndef H_HAL_FLASH_
-#define H_HAL_FLASH_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <inttypes.h>
-
-int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst,
-  uint32_t num_bytes);
-int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src,
-  uint32_t num_bytes);
-int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address);
-int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes);
-uint8_t hal_flash_align(uint8_t flash_id);
-int hal_flash_init(void);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_HAL_FLASH_ */
diff --git a/bl2/ext/mcuboot/include/target.h b/bl2/ext/mcuboot/include/target.h
index 6f85e5b..47d5a84 100644
--- a/bl2/ext/mcuboot/include/target.h
+++ b/bl2/ext/mcuboot/include/target.h
@@ -1,15 +1,52 @@
 /*
  *  Copyright (C) 2017, Linaro Ltd
+ *  Copyright (c) 2018, Arm Limited.
+ *
  *  SPDX-License-Identifier: Apache-2.0
  */
 
 #ifndef H_TARGETS_TARGET_
 #define H_TARGETS_TARGET_
 
-#if defined(MCUBOOT_TARGET_CONFIG)
-#include MCUBOOT_TARGET_CONFIG
-#else
-#error "Board is currently not supported by bootloader"
+/* Target specific defines: flash partitions; flash driver name, etc.
+ * Comes from: platform/ext/target/<BOARD>/<SUBSYSTEM>/partition
+ */
+#include "flash_layout.h"
+
+#ifndef FLASH_BASE_ADDRESS
+#error "FLASH_BASE_ADDRESS must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_SECTOR_SIZE
+#error "FLASH_AREA_IMAGE_SECTOR_SIZE must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_0_OFFSET
+#error "FLASH_AREA_IMAGE_0_OFFSET must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_0_SIZE
+#error "FLASH_AREA_IMAGE_0_SIZE must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_1_OFFSET
+#error "FLASH_AREA_IMAGE_1_OFFSET must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_1_SIZE
+#error "FLASH_AREA_IMAGE_1_SIZE must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_SCRATCH_OFFSET
+#error "FLASH_AREA_IMAGE_SCRATCH_OFFSET must be defined by the target"
+#endif
+
+#ifndef FLASH_AREA_IMAGE_SCRATCH_SIZE
+#error "FLASH_AREA_IMAGE_SCRATCH_SIZE must be defined by the target"
+#endif
+
+#ifndef FLASH_DEV_NAME
+#error "BL2 supports CMSIS flash interface and device name must be specified"
 #endif
 
 #endif
diff --git a/platform/ext/Mps2AN519.cmake b/platform/ext/Mps2AN519.cmake
index 257d5fc..95a21aa 100755
--- a/platform/ext/Mps2AN519.cmake
+++ b/platform/ext/Mps2AN519.cmake
@@ -111,6 +111,6 @@
 if (NOT DEFINED BUILD_FLASH)
   message(FATAL_ERROR "Configuration variable BUILD_FLASH (true|false) is undefined!")
 elseif(BUILD_FLASH)
-  list(APPEND ALL_SRC_C "${PLATFORM_DIR}/common/flash_memory_mapped.c")
-  embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
+  list(APPEND ALL_SRC_C "${PLATFORM_DIR}/target/mps2/an519/cmsis_drivers/Driver_Flash.c")
+  embedded_include_directories(PATH "${PLATFORM_DIR}/target/mps2/an519/cmsis_drivers" ABSOLUTE)
 endif()
diff --git a/platform/ext/Mps2AN521.cmake b/platform/ext/Mps2AN521.cmake
index 4edda4f..8dc53a8 100755
--- a/platform/ext/Mps2AN521.cmake
+++ b/platform/ext/Mps2AN521.cmake
@@ -128,6 +128,6 @@
 if (NOT DEFINED BUILD_FLASH)
   message(FATAL_ERROR "Configuration variable BUILD_FLASH (true|false) is undefined!")
 elseif(BUILD_FLASH)
-  list(APPEND ALL_SRC_C "${PLATFORM_DIR}/common/flash_memory_mapped.c")
-  embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
+  list(APPEND ALL_SRC_C "${PLATFORM_DIR}/target/mps2/an521/cmsis_drivers/Driver_Flash.c")
+  embedded_include_directories(PATH "${PLATFORM_DIR}/target/mps2/an521/cmsis_drivers" ABSOLUTE)
 endif()
diff --git a/platform/ext/common/flash.h b/platform/ext/common/flash.h
deleted file mode 100644
index 12cd9eb..0000000
--- a/platform/ext/common/flash.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (c) 2017 Nordic Semiconductor ASA
- * Copyright (c) 2016 Intel Corporation
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-/**
- * @file
- * @brief Public API for FLASH drivers
- */
-
-#ifndef __FLASH_H__
-#define __FLASH_H__
-
-/**
- * @brief FLASH Interface
- * @defgroup flash_interface FLASH Interface
- * @ingroup io_interfaces
- * @{
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include "bl2_util.h"  /* struct device */
-
-#define off_t int32_t
-
-/**
- *  @brief  Read data from flash
- *  @param  dev             : flash device
- *  @param  offset          : Offset (byte aligned) to read
- *  @param  data            : Buffer to store read data
- *  @param  len             : Number of bytes to read.
- *
- *  @return  0 on success, negative errno code on fail.
- */
-int
-flash_read(struct device *dev, off_t offset, void *data, size_t len);
-
-/**
- *  @brief  Write buffer into flash memory.
- *
- *  Prior to the invocation of this API, the flash_write_protection_set needs
- *  to be called first to disable the write protection.
- *
- *  @param  dev             : flash device
- *  @param  offset          : starting offset for the write
- *  @param  data            : data to write
- *  @param  len             : Number of bytes to write
- *
- *  @return  0 on success, negative errno code on fail.
- */
-int
-flash_write(struct device *dev, off_t offset, const void *data, size_t len);
-
-/**
- *  @brief  Erase part or all of a flash memory
- *
- *  Acceptable values of erase size and offset are subject to
- *  hardware-specific multiples of sector size and offset. Please check the
- *  API implemented by the underlying sub driver.
- *
- *  Prior to the invocation of this API, the flash_write_protection_set needs
- *  to be called first to disable the write protection.
- *
- *  @param  dev             : flash device
- *  @param  offset          : erase area starting offset
- *  @param  size            : size of area to be erased
- *
- *  @return  0 on success, negative errno code on fail.
- */
-int
-flash_erase(struct device *dev, off_t offset, size_t size);
-
-/**
- *  @brief  Enable or disable write protection for a flash memory
- *
- *  This API is required to be called before the invocation of write or erase
- *  API. Please note that on some flash components, the write protection is
- *  automatically turned on again by the device after the completion of each
- *  write or erase calls. Therefore, on those flash parts, write protection
- *  needs to be disabled before each invocation of the write or erase API.
- *  Please refer to the sub-driver API or the data sheet of the flash component
- *  to get details on the write protection behavior.
- *
- *  @param  dev             : flash device
- *  @param  enable          : enable or disable flash write protection
- *
- *  @return  0 on success, negative errno code on fail.
- */
-int
-flash_write_protection_set(struct device *dev, bool enable);
-
-#ifdef __cplusplus
-}
-#endif
-
-/**
- * @}
- */
-
-#endif /* __FLASH_H__ */
diff --git a/platform/ext/common/flash_memory_mapped.c b/platform/ext/common/flash_memory_mapped.c
deleted file mode 100644
index 6d38f05..0000000
--- a/platform/ext/common/flash_memory_mapped.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2017, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#include <flash.h>
-#include <string.h>
-#include "target.h"
-
-
-int flash_erase(struct device *dev, off_t offset, size_t size)
-{
-    uint32_t address = FLASH_BASE_ADDRESS + offset;
-
-    memset((void *)address, 0xff, size);
-    return 0;
-}
-
-int flash_read(struct device *dev, off_t offset, void *data, size_t len)
-{
-    uint32_t address = FLASH_BASE_ADDRESS + offset;
-
-    memcpy(data, (void *)address, len);
-    return 0;
-}
-
-int flash_write(struct device *dev, off_t offset, const void *data, size_t len)
-{
-   uint32_t address = FLASH_BASE_ADDRESS + offset;
-
-   memcpy((void *)address, data, len);
-   return 0;
-}
-
-int flash_write_protection_set(struct device *dev, bool enable)
-{
-    /* Do nothing */
-    return 0;
-}
diff --git a/platform/ext/target/mps2/an519/partition/flash_layout.h b/platform/ext/target/mps2/an519/partition/flash_layout.h
index a957832..91ebf73 100644
--- a/platform/ext/target/mps2/an519/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an519/partition/flash_layout.h
@@ -19,31 +19,45 @@
 
 /* Flash layout on MPS2 AN519 with BL2:
  *
- * 0x0000_0000 BL2 - MCUBoot
- * 0x0008_0000 Flash_area_image_0:
+ * 0x0000_0000 BL2 - MCUBoot(0.5 MB)
+ * 0x0008_0000 Flash_area_image_0(1 MB):
  *    0x0008_0000 Secure     image primary
  *    0x0010_0000 Non-secure image primary
- * 0x0018_0000 Flash_area_image_1:
+ * 0x0018_0000 Flash_area_image_1(1 MB):
  *    0x0018_0000 Secure     image secondary
  *    0x0020_0000 Non-secure image secondary
- * 0x0028_0000 Scratch area
+ * 0x0028_0000 Scratch area(1 MB)
+ * 0x0038_0000 Unused(0.5 MB)
+ *
+ * Flash layout on MPS2 AN519, if BL2 not defined:
+ * 0x0000_0000 Secure     image
+ * 0x0010_0000 Non-secure image
  */
-#define FLASH_BASE_ADDRESS              (0x0)
 
-#define FLASH_ALIGN                     (1)
+/* This header file is included from linker scatter file as well, where only a
+ * limited C constructs are allowed. Therefore it is not possible to include
+ * here the platform_retarget.h to access flash related defines. To resolve this
+ * some of the values are redefined here with different names, these are marked
+ * with comment.
+ */
 
 /* The size of a partition. This should be large enough to contain a S or NS
  * sw binary. Each FLASH_AREA_IMAGE contains two partitions. See Flash layout
  * above.
  */
-#define FLASH_PARTITION_SIZE            (0x80000)
+#define FLASH_PARTITION_SIZE            (0x80000)    /* 512 kB */
 
-/* Sector size of the flash hardware */
-#define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x4000)
+/* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
+#define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x1000)     /* 4 kB */
+/* Same as FLASH0_SIZE */
+#define FLASH_TOTAL_SIZE                (0x00400000) /* 4 MB */
+
+/* Flash layout info for BL2 bootloader */
+#define FLASH_BASE_ADDRESS              (0x10000000) /* same as FLASH0_BASE_S */
 
 /* Offset and size definitions of the flash partitions that are handled by the
  * bootloader. The image swapping is done between IMAGE_0 and IMAGE_1, SCRATCH
- * is used to make notes of the progress of the image swapping.
+ * is used as a temporary storage during image swapping.
  */
 #define FLASH_AREA_BL2_OFFSET           (0x0)
 #define FLASH_AREA_BL2_SIZE             (FLASH_PARTITION_SIZE)
@@ -64,4 +78,9 @@
 #define NON_SECURE_IMAGE_OFFSET         0x80000
 #define NON_SECURE_IMAGE_MAX_SIZE       0x80000
 
+/* Flash device name used by BL2 and SST
+ * Name is defined in flash driver file: Driver_Flash.c
+ */
+#define FLASH_DEV_NAME Driver_FLASH0
+
 #endif /* __FLASH_LAYOUT_H__ */
diff --git a/platform/ext/target/mps2/an519/partition/region_defs.h b/platform/ext/target/mps2/an519/partition/region_defs.h
index 5dd7a22..71e4b48 100644
--- a/platform/ext/target/mps2/an519/partition/region_defs.h
+++ b/platform/ext/target/mps2/an519/partition/region_defs.h
@@ -19,30 +19,14 @@
 
 #include "flash_layout.h"
 
-#define TOTAL_ROM_SIZE (0x00400000) /* 4MB */
-#define TOTAL_RAM_SIZE (0x00200000) /* 2MB */
+#define TOTAL_ROM_SIZE FLASH_TOTAL_SIZE
+#define TOTAL_RAM_SIZE (0x00200000)     /* 2 MB */
 
 /*
  * MPC granularity is 128 KB on AN519 MPS2 FPGA image. Alignment
  * of partitions is defined in accordance with this constraint.
  */
 
-/*Flash partitions on MPS2 AN519 with BL2:
- *
- * 0x0000_0000 BL2 - MCUBoot
- * 0x0008_0000 Flash_area_image_0:
- *    0x0008_0000 Secure     image primary
- *    0x0010_0000 Non-secure image primary
- * 0x0018_0000 Flash_area_image_1:
- *    0x0018_0000 Secure     image secondary
- *    0x0020_0000 Non-secure image secondary
- * 0x0028_0000 Scratch area
- *
- * Flash partitions on bare metal, if BL2 not defined:
- * 0x0000_0000 Secure     image
- * 0x0010_0000 Non-secure image
- */
-
 #ifdef BL2
 #define  S_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_IMAGE_0_OFFSET)
 #else
@@ -54,7 +38,7 @@
 /*
  * Boot partition structure if MCUBoot is used:
  * 0x0_0000 Bootloader header
- * 0x0_0200 Image area
+ * 0x0_0400 Image area
  * 0x7_0000 Trailer
  */
 /* IMAGE_CODE_SIZE is the space available for the software binary image.
diff --git a/platform/ext/target/mps2/an521/partition/flash_layout.h b/platform/ext/target/mps2/an521/partition/flash_layout.h
index a0cc0d0..9477cf8 100644
--- a/platform/ext/target/mps2/an521/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an521/partition/flash_layout.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2018 Arm Limited. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,22 +19,46 @@
 
 /* Flash layout on MPS2 AN521 with BL2:
  *
- * 0x0000_0000 BL2 - MCUBoot
- * 0x0008_0000 Flash_area_image_0:
+ * 0x0000_0000 BL2 - MCUBoot(0.5 MB)
+ * 0x0008_0000 Flash_area_image_0(1 MB):
  *    0x0008_0000 Secure     image primary
  *    0x0010_0000 Non-secure image primary
- * 0x0018_0000 Flash_area_image_1:
+ * 0x0018_0000 Flash_area_image_1(1 MB):
  *    0x0018_0000 Secure     image secondary
  *    0x0020_0000 Non-secure image secondary
- * 0x0028_0000 Scratch area
+ * 0x0028_0000 Scratch area(1 MB)
+ * 0x0038_0000 Unused(0.5 MB)
+ *
+ * Flash layout on MPS2 AN521, if BL2 not defined:
+ * 0x0000_0000 Secure     image
+ * 0x0010_0000 Non-secure image
  */
-#define FLASH_BASE_ADDRESS              (0x0)
 
-#define FLASH_ALIGN                     (1)
-#define FLASH_PARTITION_SIZE            (0x80000)
-#define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x4000)
+/* This header file is included from linker scatter file as well, where only a
+ * limited C constructs are allowed. Therefore it is not possible to include
+ * here the platform_retarget.h to access flash related defines. To resolve this
+ * some of the values are redefined here with different names, these are marked
+ * with comment.
+ */
 
+/* The size of a partition. This should be large enough to contain a S or NS
+ * sw binary. Each FLASH_AREA_IMAGE contains two partitions. See Flash layout
+ * above.
+ */
+#define FLASH_PARTITION_SIZE            (0x80000)    /* 512 kB */
 
+/* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
+#define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x1000)     /* 4 kB */
+/* Same as FLASH0_SIZE */
+#define FLASH_TOTAL_SIZE                (0x00400000) /* 4 MB */
+
+/* Flash layout info for BL2 bootloader */
+#define FLASH_BASE_ADDRESS              (0x10000000) /* same as FLASH0_BASE_S */
+
+/* Offset and size definitions of the flash partitions that are handled by the
+ * bootloader. The image swapping is done between IMAGE_0 and IMAGE_1, SCRATCH
+ * is used as a temporary storage during image swapping.
+ */
 #define FLASH_AREA_BL2_OFFSET           (0x0)
 #define FLASH_AREA_BL2_SIZE             (FLASH_PARTITION_SIZE)
 
@@ -54,4 +78,9 @@
 #define NON_SECURE_IMAGE_OFFSET         0x80000
 #define NON_SECURE_IMAGE_MAX_SIZE       0x80000
 
+/* Flash device name used by BL2 and SST
+ * Name is defined in flash driver file: Driver_Flash.c
+ */
+#define FLASH_DEV_NAME Driver_FLASH0
+
 #endif /* __FLASH_LAYOUT_H__ */
diff --git a/platform/ext/target/mps2/an521/partition/region_defs.h b/platform/ext/target/mps2/an521/partition/region_defs.h
index 49f3292..7a6921e 100644
--- a/platform/ext/target/mps2/an521/partition/region_defs.h
+++ b/platform/ext/target/mps2/an521/partition/region_defs.h
@@ -19,30 +19,14 @@
 
 #include "flash_layout.h"
 
-#define TOTAL_ROM_SIZE (0x00400000) /* 4MB */
-#define TOTAL_RAM_SIZE (0x00200000) /* 2MB */
+#define TOTAL_ROM_SIZE FLASH_TOTAL_SIZE
+#define TOTAL_RAM_SIZE (0x00200000)     /* 2 MB */
 
 /*
  * MPC granularity is 128 KB on AN521 Castor MPS2 FPGA image. Alignment
  * of partitions is defined in accordance with this constraint.
  */
 
-/*Flash partitions on MPS2 AN521 with BL2:
- *
- * 0x0000_0000 BL2 - MCUBoot
- * 0x0008_0000 Flash_area_image_0:
- *    0x0008_0000 Secure     image primary
- *    0x0010_0000 Non-secure image primary
- * 0x0018_0000 Flash_area_image_1:
- *    0x0018_0000 Secure     image secondary
- *    0x0020_0000 Non-secure image secondary
- * 0x0028_0000 Scratch area
- *
- * Flash partitions on bare metal, if BL2 not defined:
- * 0x0000_0000 Secure     image
- * 0x0010_0000 Non-secure image
- */
-
 #ifdef BL2
 #define  S_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_IMAGE_0_OFFSET)
 #else
@@ -54,7 +38,7 @@
 /*
  * Boot partition structure if MCUBoot is used:
  * 0x0_0000 Bootloader header
- * 0x0_0200 Image area
+ * 0x0_0400 Image area
  * 0x7_0000 Trailer
  */
 /* IMAGE_CODE_SIZE is the space available for the software binary image.