SST: Implement SST flash on top of CMSIS flash drv

This patch implements SST flash interface on top
of CMSIS flash driver.
It also adds a missing include in sst_utils.h.

Change-Id: I70610584a124d72c2ec6bd911618e0c60e3c7463
Signed-off-by: Marc Moreno <marc.morenoberengue@arm.com>
diff --git a/platform/ext/Mps2AN519.cmake b/platform/ext/Mps2AN519.cmake
index 95a21aa..dfa6c21 100755
--- a/platform/ext/Mps2AN519.cmake
+++ b/platform/ext/Mps2AN519.cmake
@@ -112,5 +112,12 @@
   message(FATAL_ERROR "Configuration variable BUILD_FLASH (true|false) is undefined!")
 elseif(BUILD_FLASH)
   list(APPEND ALL_SRC_C "${PLATFORM_DIR}/target/mps2/an519/cmsis_drivers/Driver_Flash.c")
+  # There is no real flash memory for code on MPS2 board. Instead a code SRAM is
+  # used for code storage: ZBT SSRAM1. The Driver_Flash driver just emulates a flash
+  # interface and behaviour on top of the SRAM memory.
+  # As the SST area is going to be in RAM, it is required to set SST_RAM_FS to be sure the
+  # SST service knows that when it starts the SST area does not contain any valid block and
+  # it needs  to create an empty one.
+  set(SST_RAM_FS True)
   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 8dc53a8..a740066 100755
--- a/platform/ext/Mps2AN521.cmake
+++ b/platform/ext/Mps2AN521.cmake
@@ -129,5 +129,12 @@
   message(FATAL_ERROR "Configuration variable BUILD_FLASH (true|false) is undefined!")
 elseif(BUILD_FLASH)
   list(APPEND ALL_SRC_C "${PLATFORM_DIR}/target/mps2/an521/cmsis_drivers/Driver_Flash.c")
+  # There is no real flash memory for code on MPS2 board. Instead a code SRAM is
+  # used for code storage: ZBT SSRAM1. The Driver_Flash driver just emulates a flash
+  # interface and behaviour on top of the SRAM memory.
+  # As the SST area is going to be in RAM, it is required to set SST_RAM_FS to be sure the
+  # SST service knows that when it starts the SST area does not contain any valid block and
+  # it needs  to create an empty one.
+  set(SST_RAM_FS True)
   embedded_include_directories(PATH "${PLATFORM_DIR}/target/mps2/an521/cmsis_drivers" ABSOLUTE)
 endif()
diff --git a/platform/ext/readme.md b/platform/ext/readme.md
index 7b94540..8cd2bce 100644
--- a/platform/ext/readme.md
+++ b/platform/ext/readme.md
@@ -16,6 +16,7 @@
 CMSIS_5 project.
 
 ### common
+
 This folder contains stdout redirection to UART, a temporary memory mapped
 flash implementation for the bootloader and mbedtls_config.h for all
 the targets.
@@ -26,8 +27,82 @@
 that TF-M project expects a target to provide.
 
 ### target
+
 This folder contains the files for individual target.
 
+#### Flash layout header file
+
+Target must provide a header file, called flash_layout.h, which defines the
+information explained in the follow subsections. The defines must be named
+as they are in the subsections.
+
+##### BL2 bootloader
+
+The BL2 bootloader requires the following definitions:
+
+ - `FLASH_BASE_ADDRESS`
+   Defines the first valid address in the flash.
+ - `FLASH_AREA_BL2_OFFSET`
+   Defines the offset from the flash base address
+   where the BL2 - MCUBOOT area starts.
+ - `FLASH_AREA_BL2_SIZE`
+   Defines the size of the BL2 area.
+ - `FLASH_AREA_IMAGE_0_OFFSET`
+   Defines the offset from the flash base address
+   where the image 0 area starts, which hosts the
+   active firmware image.
+ - `FLASH_AREA_IMAGE_0_SIZE`
+   Defines the size of the image 0 area.
+ - `FLASH_AREA_IMAGE_1_OFFSET`
+   Defines the offset from the flash base address
+   where the image 1 area starts, which is a placeholder
+   for new firmware images.
+ - `FLASH_AREA_IMAGE_1_SIZE`
+   Defines the size of the image 1 area.
+ - `FLASH_AREA_IMAGE_SCRATCH_OFFSET`
+   Defines the offset from the flash base address
+   where the scratch area starts, which is used during
+   image swapping.
+ - `FLASH_AREA_IMAGE_SCRATCH_SIZE`
+   Defines the size of the scratch area. The minimal size
+   must be as the biggest sector size in the flash.
+ - `FLASH_DEV_NAME`
+   Specifies the flash device used by BL2 and SST.
+
+##### Assemble tool
+
+The assemble.py tools is used to concatenate secure and non-secure
+binary to a single binary blob. It requires the following definitions:
+
+ - `SECURE_IMAGE_OFFSET`
+   Defines the offset from the single binary blob base address,
+   where the secure image starts.
+ - `SECURE_IMAGE_MAX_SIZE`
+   Defines the maximum size of the secure image area.
+ - `NON_SECURE_IMAGE_OFFSET`
+   Defines the offset from the single binary blob base address,
+   where the non-secure image starts.
+ - `NON_SECURE_IMAGE_MAX_SIZE`
+   Defines the maximum size of the non-secure image area.
+
+##### Secure Storage (SST) Service definitions
+
+The SST service requires the following definitions:
+
+ - `SST_FLASH_AREA_ADDR`
+   Defines the flash area address where the secure
+   store area starts.
+ - `SST_SECTOR_SIZE`
+   Defines the size of the flash sectors.
+ - `SST_NBR_OF_SECTORS`
+   Defines the number of sectors avilable for
+   the secure area.
+ - `FLASH_DEV_NAME`
+   Specifies the flash device used by BL2 and SST.
+ - `SST_FLASH_PROGRAM_UNIT`
+   Defines the smallest flash programmable unit in bytes.
+
+**Note**: The sectors must be consecutive.
 
 --------------
 
diff --git a/platform/ext/target/mps2/an519/partition/flash_layout.h b/platform/ext/target/mps2/an519/partition/flash_layout.h
index 91ebf73..a0881d1 100644
--- a/platform/ext/target/mps2/an519/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an519/partition/flash_layout.h
@@ -27,7 +27,8 @@
  *    0x0018_0000 Secure     image secondary
  *    0x0020_0000 Non-secure image secondary
  * 0x0028_0000 Scratch area(1 MB)
- * 0x0038_0000 Unused(0.5 MB)
+ * 0x0038_0000 Secure Storage Area (0.02 MB)
+ * 0x0038_5000 Unused(0.482 MB)
  *
  * Flash layout on MPS2 AN519, if BL2 not defined:
  * 0x0000_0000 Secure     image
@@ -71,6 +72,9 @@
 #define FLASH_AREA_IMAGE_SCRATCH_OFFSET (0x280000)
 #define FLASH_AREA_IMAGE_SCRATCH_SIZE   (2 * FLASH_PARTITION_SIZE)
 
+#define FLASH_SST_AREA_OFFSET           (0x380000)
+#define FLASH_SST_AREA_SIZE             (0x5000)   /* 20 KB */
+
 /* Offset and size definition in flash area, used by assemble.py */
 #define SECURE_IMAGE_OFFSET             0x0
 #define SECURE_IMAGE_MAX_SIZE           0x80000
@@ -83,4 +87,15 @@
  */
 #define FLASH_DEV_NAME Driver_FLASH0
 
+/* Secure Storage (SST) Service definitions */
+/* In this target the CMSIS driver requires only the offset from the base
+ * address instead of the full memory address.
+ */
+#define SST_FLASH_AREA_ADDR  FLASH_SST_AREA_OFFSET
+#define SST_SECTOR_SIZE      FLASH_AREA_IMAGE_SECTOR_SIZE
+/* The sectors must be in consecutive memory location */
+#define SST_NBR_OF_SECTORS  (FLASH_SST_AREA_SIZE / SST_SECTOR_SIZE)
+/* Specifies the smallest flash programmable unit in bytes */
+#define SST_FLASH_PROGRAM_UNIT  0x1
+
 #endif /* __FLASH_LAYOUT_H__ */
diff --git a/platform/ext/target/mps2/an521/partition/flash_layout.h b/platform/ext/target/mps2/an521/partition/flash_layout.h
index 9477cf8..21b57a1 100644
--- a/platform/ext/target/mps2/an521/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an521/partition/flash_layout.h
@@ -27,7 +27,8 @@
  *    0x0018_0000 Secure     image secondary
  *    0x0020_0000 Non-secure image secondary
  * 0x0028_0000 Scratch area(1 MB)
- * 0x0038_0000 Unused(0.5 MB)
+ * 0x0038_0000 Secure Storage Area (0.02 MB)
+ * 0x0038_5000 Unused(0.482 MB)
  *
  * Flash layout on MPS2 AN521, if BL2 not defined:
  * 0x0000_0000 Secure     image
@@ -71,6 +72,9 @@
 #define FLASH_AREA_IMAGE_SCRATCH_OFFSET (0x280000)
 #define FLASH_AREA_IMAGE_SCRATCH_SIZE   (2 * FLASH_PARTITION_SIZE)
 
+#define FLASH_SST_AREA_OFFSET           (0x380000)
+#define FLASH_SST_AREA_SIZE             (0x5000)   /* 20 KB */
+
 /* Offset and size definition in flash area, used by assemble.py */
 #define SECURE_IMAGE_OFFSET             0x0
 #define SECURE_IMAGE_MAX_SIZE           0x80000
@@ -83,4 +87,15 @@
  */
 #define FLASH_DEV_NAME Driver_FLASH0
 
+/* Secure Storage (SST) Service definitions */
+/* In this target the CMSIS driver requires only the offset from the base
+ * address instead of the full memory address.
+ */
+#define SST_FLASH_AREA_ADDR  FLASH_SST_AREA_OFFSET
+#define SST_SECTOR_SIZE      FLASH_AREA_IMAGE_SECTOR_SIZE
+/* The sectors must be in consecutive memory location */
+#define SST_NBR_OF_SECTORS  (FLASH_SST_AREA_SIZE / SST_SECTOR_SIZE)
+/* Specifies the smallest flash programmable unit in bytes */
+#define SST_FLASH_PROGRAM_UNIT  0x1
+
 #endif /* __FLASH_LAYOUT_H__ */