Platform: Add new NV counter due to multiple image boot

Add new NV counter to the NV counter flash area to be used
by the bootloader as a separate second security counter
for another firmware image to enable multiple image boot.

Change-Id: I6cd3bf675317240e40cd0bc31a714070885791aa
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c
index c7c7173..ef2e812 100644
--- a/bl2/ext/mcuboot/bootutil/src/loader.c
+++ b/bl2/ext/mcuboot/bootutil/src/loader.c
@@ -392,7 +392,7 @@
         goto done;
     }
 
-    rc = boot_nv_security_counter_update(0, img_security_cnt);
+    rc = boot_nv_security_counter_update(current_image, img_security_cnt);
     if (rc != 0) {
         goto done;
     }
diff --git a/bl2/src/security_cnt.c b/bl2/src/security_cnt.c
index 03c438c..825a2d6 100644
--- a/bl2/src/security_cnt.c
+++ b/bl2/src/security_cnt.c
@@ -11,6 +11,7 @@
 #include <stdint.h>
 
 #define TFM_BOOT_NV_COUNTER_0    PLAT_NV_COUNTER_3   /* NV counter of Image 0 */
+#define TFM_BOOT_NV_COUNTER_1    PLAT_NV_COUNTER 4   /* NV counter of Image 1 */
 #define TFM_BOOT_NV_COUNTER_MAX  PLAT_NV_COUNTER_MAX
 
 static enum tfm_nv_counter_t get_nv_counter_from_image_id(uint32_t image_id)
diff --git a/platform/ext/target/mps2/an519/partition/flash_layout.h b/platform/ext/target/mps2/an519/partition/flash_layout.h
index 37938b4..c37188a 100644
--- a/platform/ext/target/mps2/an519/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an519/partition/flash_layout.h
@@ -40,8 +40,8 @@
  *    0x0020_0000 Non-secure image secondary
  * 0x0028_0000 Scratch area (1 MB)
  * 0x0038_0000 Secure Storage Area (0.02 MB)
- * 0x0038_5000 NV counters area (20 Bytes)
- * 0x0038_5014 Unused (0.480 MB)
+ * 0x0038_5000 NV counters area (24 Bytes)
+ * 0x0038_5018 Unused (0.480 MB)
  *
  * Flash layout on MPS2 AN519, if BL2 not defined:
  *
@@ -156,7 +156,7 @@
 /* NV Counters definitions */
 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_SST_AREA_OFFSET + \
                                          FLASH_SST_AREA_SIZE)
-#define FLASH_NV_COUNTERS_AREA_SIZE     (0x14)     /* 20 Bytes */
+#define FLASH_NV_COUNTERS_AREA_SIZE     (0x18)     /* 24 Bytes */
 
 /* Offset and size definition in flash area used by assemble.py */
 #define SECURE_IMAGE_OFFSET             (0x0)
diff --git a/platform/ext/target/mps2/an521/partition/flash_layout.h b/platform/ext/target/mps2/an521/partition/flash_layout.h
index d09ef07..a1f414f 100644
--- a/platform/ext/target/mps2/an521/partition/flash_layout.h
+++ b/platform/ext/target/mps2/an521/partition/flash_layout.h
@@ -40,8 +40,8 @@
  *    0x0020_0000 Non-secure image secondary
  * 0x0028_0000 Scratch area (1 MB)
  * 0x0038_0000 Secure Storage Area (0.02 MB)
- * 0x0038_5000 NV counters area (20 Bytes)
- * 0x0038_5014 Unused (0.480 MB)
+ * 0x0038_5000 NV counters area (24 Bytes)
+ * 0x0038_5018 Unused (0.480 MB)
  *
  * Flash layout on MPS2 AN521, if BL2 not defined:
  *
@@ -156,7 +156,7 @@
 /* NV Counters definitions */
 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_SST_AREA_OFFSET + \
                                          FLASH_SST_AREA_SIZE)
-#define FLASH_NV_COUNTERS_AREA_SIZE     (0x14)     /* 20 Bytes */
+#define FLASH_NV_COUNTERS_AREA_SIZE     (0x18)     /* 24 Bytes */
 
 /* Offset and size definition in flash area used by assemble.py */
 #define SECURE_IMAGE_OFFSET             (0x0)
diff --git a/platform/ext/target/mps3/an524/partition/flash_layout.h b/platform/ext/target/mps3/an524/partition/flash_layout.h
index 35d472a..c2441ac 100644
--- a/platform/ext/target/mps3/an524/partition/flash_layout.h
+++ b/platform/ext/target/mps3/an524/partition/flash_layout.h
@@ -35,16 +35,16 @@
  *     0x0010_0000 Non-secure image     (256 KB)
  * 0x0014_0000 Scratch area             (512 KB)
  * 0x001C_0000 SST area                 (20 KB)
- * 0x001C_5000 NV counters              (20 B)
- * 0x001C_5014 Unused
+ * 0x001C_5000 NV counters              (24 B)
+ * 0x001C_5018 Unused
  *
  * Flash layout without BL2
  *
  * 0x0000_0000 Secure image             (256 KB)
  * 0x0008_0000 Non-secure image         (256 KB)
  * 0x001C_0000 SST area                 (20 KB)
- * 0x001C_5000 NV counter               (20 B)
- * 0x001C_5014 Unused
+ * 0x001C_5000 NV counter               (24 B)
+ * 0x001C_5018 Unused
  */
 
 /* Size of a Secure and of a Non-secure image */
@@ -132,7 +132,7 @@
 /* NV Counters definitions */
 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_SST_AREA_OFFSET + \
                                          FLASH_SST_AREA_SIZE)
-#define FLASH_NV_COUNTERS_AREA_SIZE     (0x14)     /* 20 Bytes */
+#define FLASH_NV_COUNTERS_AREA_SIZE     (0x18)     /* 24 Bytes */
 
 /* Offset and size definition in flash area used by assemble.py */
 #define SECURE_IMAGE_OFFSET             (0x0)
diff --git a/platform/ext/target/musca_a/partition/flash_layout.h b/platform/ext/target/musca_a/partition/flash_layout.h
index 89d8083..5348e28 100644
--- a/platform/ext/target/musca_a/partition/flash_layout.h
+++ b/platform/ext/target/musca_a/partition/flash_layout.h
@@ -27,8 +27,8 @@
  *    0x0032_0000 Secure     image secondary
  *    0x003A_0000 Non-secure image secondary
  * 0x0042_0000 Secure Storage Area (0.02 MB)
- * 0x0042_5000 NV counters area (20 Bytes)
- * 0x0042_5014 Unused
+ * 0x0042_5000 NV counters area (24 Bytes)
+ * 0x0042_5018 Unused
  */
 
 /* Code SRAM layout on Musca (with BL2, which is mandatory) after the newest
@@ -136,7 +136,7 @@
 /* NV Counters definitions */
 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_SST_AREA_OFFSET + \
                                          FLASH_SST_AREA_SIZE)
-#define FLASH_NV_COUNTERS_AREA_SIZE     (0x14)     /* 20 Bytes */
+#define FLASH_NV_COUNTERS_AREA_SIZE     (0x18)     /* 24 Bytes */
 
 /* Offset and size definition in flash area used by assemble.py */
 #define SECURE_IMAGE_OFFSET             (0x0)
diff --git a/platform/ext/target/musca_b1/partition/flash_layout.h b/platform/ext/target/musca_b1/partition/flash_layout.h
index 10108d9..717f8b2 100644
--- a/platform/ext/target/musca_b1/partition/flash_layout.h
+++ b/platform/ext/target/musca_b1/partition/flash_layout.h
@@ -27,8 +27,8 @@
  *    0x0A0E_0000 Secure     image secondary (256 KB)
  *    0x0A12_0000 Non-secure image secondary (512 KB)
  * 0x0A1E_0000 Secure Storage Area (0.02 MB)
- * 0x0A1E_5000 NV counters area (20 Bytes)
- * 0x0A1E_5014 Unused
+ * 0x0A1E_5000 NV counters area (24 Bytes)
+ * 0x0A1E_5018 Unused
  *
  * Flash layout on Musca-B1 without BL2:
  * 0x0A00_0000 Secure     image
@@ -134,7 +134,7 @@
 /* NV Counters definitions */
 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_SST_AREA_OFFSET + \
                                          FLASH_SST_AREA_SIZE)
-#define FLASH_NV_COUNTERS_AREA_SIZE     (0x14)     /* 20 Bytes */
+#define FLASH_NV_COUNTERS_AREA_SIZE     (0x18)     /* 24 Bytes */
 
 /* Offset and size definition in flash area used by assemble.py */
 #define SECURE_IMAGE_OFFSET             (0x0)
diff --git a/platform/include/tfm_plat_nv_counters.h b/platform/include/tfm_plat_nv_counters.h
index 9b86112..ff34a91 100644
--- a/platform/include/tfm_plat_nv_counters.h
+++ b/platform/include/tfm_plat_nv_counters.h
@@ -31,6 +31,7 @@
     PLAT_NV_COUNTER_1,      /* Used by SST service */
     PLAT_NV_COUNTER_2,      /* Used by SST service */
     PLAT_NV_COUNTER_3,      /* Used by bootloader */
+    PLAT_NV_COUNTER_4,      /* Used by bootloader */
     PLAT_NV_COUNTER_MAX
 };