Platform: Add NV counter interface

This patch adds the TF-M non-volatile(NV) counter interface.

Change-Id: I2b87dfadf9c53bd755f26867de678a4e837bbdcb
Signed-off-by: Marc Moreno <marc.morenoberengue@arm.com>
diff --git a/CommonConfig.cmake b/CommonConfig.cmake
old mode 100755
new mode 100644
index 0aaf3ff..af7af02
--- a/CommonConfig.cmake
+++ b/CommonConfig.cmake
@@ -28,6 +28,7 @@
 set(BUILD_STARTUP Off)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS Off)
 set(BUILD_UART_STDOUT Off)
 set(BUILD_FLASH Off)
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index eddfd50..285c0df 100755
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -57,6 +57,7 @@
 set(BUILD_STARTUP On)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS On)
 set(BUILD_UART_STDOUT Off)
 set(BUILD_FLASH Off)
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index 0d8258b..a661fbd 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -44,6 +44,7 @@
 set(BUILD_STARTUP On)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS On)
 set(BUILD_TIME Off)
 set(BUILD_UART_STDOUT On)
diff --git a/platform/ext/readme.md b/platform/ext/readme.md
index 55c9057..78c465a 100644
--- a/platform/ext/readme.md
+++ b/platform/ext/readme.md
@@ -116,6 +116,16 @@
 
 **Note**: The sectors must be consecutive.
 
+## Expose target support for HW components
+
+Services may require HW components to be supported by the target
+to enable some features (e.g. SST service with rollback protection, etc).
+The following definitions need to be set in the <TARGET_NAME>.cmake file if the
+target has the following HW components:
+
+ - `TARGET_NV_COUNTERS_ENABLE`
+   Specifies that the target has non-volatile (NV) counters.
+
 --------------
 
 *Copyright (c) 2017-2018, Arm Limited. All rights reserved.*
diff --git a/platform/include/tfm_plat_defs.h b/platform/include/tfm_plat_defs.h
index 308b002..5b6d169 100644
--- a/platform/include/tfm_plat_defs.h
+++ b/platform/include/tfm_plat_defs.h
@@ -18,6 +18,7 @@
 enum tfm_plat_err_t {
     TFM_PLAT_ERR_SUCCESS = 0,
     TFM_PLAT_ERR_SYSTEM_ERR,
+    TFM_PLAT_ERR_MAX_VALUE,
     /* Following entry is only to ensure the error code of int size */
     TFM_PLAT_ERR_FORCE_INT_SIZE = INT_MAX
 };
diff --git a/platform/include/tfm_plat_nv_counters.h b/platform/include/tfm_plat_nv_counters.h
new file mode 100644
index 0000000..c987ed7
--- /dev/null
+++ b/platform/include/tfm_plat_nv_counters.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_PLAT_NV_COUNTERS_H__
+#define __TFM_PLAT_NV_COUNTERS_H__
+
+/**
+ * \file tfm_plat_nv_counters.h
+ *
+ * \note The interfaces defined in this file must be implemented for each
+ *       SoC.
+ */
+
+#include <stdint.h>
+#include "tfm_plat_defs.h"
+
+enum tfm_nv_counter_t {
+    TFM_SST_NV_COUNTER_1 = 0,
+    TFM_SST_NV_COUNTER_2,
+    TFM_SST_NV_COUNTER_3,
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Initialises all non-volatile (NV) counters.
+ *
+ * \return  TFM_PLAT_ERR_SUCCESS if the initialization succeeds, otherwise
+ *          TFM_PLAT_ERR_SYSTEM_ERR
+ */
+enum tfm_plat_err_t tfm_plat_init_nv_counter(void);
+
+/**
+ * \brief Reads the given non-volatile (NV) counter.
+ *
+ * \param[in]  counter_id  NV counter ID.
+ * \param[in]  size        Size of the buffer to store NV counter value
+ *                         in bytes.
+ * \param[out] val         Pointer to store the current NV counter value.
+ *
+ * \return  TFM_PLAT_ERR_SUCCESS if the value is read correctly. Otherwise,
+ *          it returns TFM_PLAT_ERR_SYSTEM_ERR.
+ */
+enum tfm_plat_err_t tfm_plat_read_nv_counter(enum tfm_nv_counter_t counter_id,
+                                             uint32_t size, uint8_t *val);
+
+/**
+ * \brief Increments the given non-volatile (NV) counter.
+ *
+ * \param[in] counter_id  NV counter ID.
+ *
+ * \return  When the NV counter reaches its maximum value, the
+ *          TFM_PLAT_ERR_MAX_VALUE error is returned to indicate the value
+ *          cannot be incremented. Otherwise, it returns TFM_PLAT_ERR_SUCCESS.
+ */
+enum tfm_plat_err_t tfm_plat_increment_nv_counter(
+                                              enum tfm_nv_counter_t counter_id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_PLAT_NV_COUNTERS_H__ */
diff --git a/secure_fw/CMakeLists.txt b/secure_fw/CMakeLists.txt
index 9645b80..c57e657 100644
--- a/secure_fw/CMakeLists.txt
+++ b/secure_fw/CMakeLists.txt
@@ -36,7 +36,12 @@
 set(BUILD_NATIVE_DRIVERS On)
 set(BUILD_STARTUP On)
 set(BUILD_TARGET_CFG On)
+# FIXME: The following TARGET flags are platform dependent.
+#        It is required to add a mechanism to expose the
+#        target capabilities and, based on them, set the
+#        flags properly.
 set(BUILD_TARGET_HARDWARE_KEYS On)
+set(BUILD_TARGET_NV_COUNTERS On)
 set(BUILD_CMSIS_DRIVERS On)
 set(BUILD_TIME Off)
 set(BUILD_UART_STDOUT On)
@@ -63,7 +68,15 @@
 #Set common compiler flags
 config_setting_shared_compiler_flags(${PROJECT_OBJ_LIB})
 
-if (CORE_TEST)
+if(NOT DEFINED TARGET_NV_COUNTERS_ENABLE)
+	set(TARGET_NV_COUNTERS_ENABLE OFF)
+endif()
+
+if(TARGET_NV_COUNTERS_ENABLE)
+	embedded_set_target_compile_defines(TARGET ${PROJECT_OBJ_LIB} LANGUAGE C DEFINES TFM_NVCOUNTERS_ENABLE APPEND)
+endif()
+
+if(CORE_TEST)
 	embedded_set_target_compile_defines(TARGET ${PROJECT_OBJ_LIB} LANGUAGE C DEFINES TFM_CORE_DEBUG TFM_PARTITION_TEST_CORE APPEND)
 endif()
 
diff --git a/secure_fw/core/CMakeLists.inc b/secure_fw/core/CMakeLists.inc
index d5bb9e2..91ba969 100644
--- a/secure_fw/core/CMakeLists.inc
+++ b/secure_fw/core/CMakeLists.inc
@@ -52,6 +52,7 @@
 set(BUILD_STARTUP Off)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS Off)
 set(BUILD_TIME Off)
 set(BUILD_UART_STDOUT Off)
diff --git a/secure_fw/services/secure_storage/CMakeLists.inc b/secure_fw/services/secure_storage/CMakeLists.inc
index 88639fc..2231a24 100644
--- a/secure_fw/services/secure_storage/CMakeLists.inc
+++ b/secure_fw/services/secure_storage/CMakeLists.inc
@@ -110,6 +110,7 @@
 	set(BUILD_STARTUP Off)
 	set(BUILD_TARGET_CFG Off)
 	set(BUILD_TARGET_HARDWARE_KEYS Off)
+	set(BUILD_TARGET_NV_COUNTERS Off)
 	set(BUILD_CMSIS_DRIVERS Off)
 	set(BUILD_TIME Off)
 	set(BUILD_UART_STDOUT Off)
diff --git a/secure_fw/spm/CMakeLists.inc b/secure_fw/spm/CMakeLists.inc
index be6bdf8..cdc2e7c 100644
--- a/secure_fw/spm/CMakeLists.inc
+++ b/secure_fw/spm/CMakeLists.inc
@@ -44,6 +44,7 @@
 set(BUILD_STARTUP Off)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS Off)
 set(BUILD_TIME Off)
 set(BUILD_UART_STDOUT Off)
diff --git a/test/test_services/CMakeLists.inc b/test/test_services/CMakeLists.inc
index e4ae2e9..d2fd100 100644
--- a/test/test_services/CMakeLists.inc
+++ b/test/test_services/CMakeLists.inc
@@ -71,6 +71,7 @@
 set(BUILD_STARTUP Off)
 set(BUILD_TARGET_CFG Off)
 set(BUILD_TARGET_HARDWARE_KEYS Off)
+set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS Off)
 set(BUILD_TIME Off)
 set(BUILD_UART_STDOUT Off)