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