Interface: Make the NS interface functions common

This patch implements common TF-M NS interface functions defined in
tfm_ns_interface.h, by calling the os_wrapper APIs.
With this change, the NSPE does not need to implement the TF-M NS
interface functions but only the os_wrapper APIs.
Still NSPE can overwrite the whole TF-M NS interface functions to
fulfill their special requirements.

Change-Id: Ifec47b99d6cba73acd94cfc0fde584f08333ab3c
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 20d8d19..7d3016c 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -54,7 +54,7 @@
 	"${INTERFACE_DIR}/src/tfm_sst_api.c"
 	"${INTERFACE_DIR}/src/tfm_crypto_api.c"
 	"${INTERFACE_DIR}/src/tfm_initial_attestation_api.c"
-	"${INTERFACE_DIR}/src/tfm_ns_interface_cmsis_rtos.c"
+	"${INTERFACE_DIR}/src/tfm_ns_interface.c"
 	)
 
 if (TFM_PARTITION_AUDIT_LOG)
diff --git a/interface/include/tfm_ns_interface.h b/interface/include/tfm_ns_interface.h
index 89b29471..21857be 100644
--- a/interface/include/tfm_ns_interface.h
+++ b/interface/include/tfm_ns_interface.h
@@ -32,9 +32,9 @@
  *
  * \return Returns the same return value of the requested veneer function
  */
-uint32_t tfm_ns_interface_dispatch(veneer_fn fn,
-                                   uint32_t arg0, uint32_t arg1,
-                                   uint32_t arg2, uint32_t arg3);
+int32_t tfm_ns_interface_dispatch(veneer_fn fn,
+                                  uint32_t arg0, uint32_t arg1,
+                                  uint32_t arg2, uint32_t arg3);
 
 /**
  * \brief NS interface, Initialise the NS interface
@@ -46,7 +46,7 @@
  *
  * \return  A value according to \ref enum tfm_status_e
  */
-enum tfm_status_e tfm_ns_interface_init();
+enum tfm_status_e tfm_ns_interface_init(void);
 #ifdef __cplusplus
 }
 #endif
diff --git a/interface/src/tfm_ns_interface.c b/interface/src/tfm_ns_interface.c
new file mode 100644
index 0000000..916af19
--- /dev/null
+++ b/interface/src/tfm_ns_interface.c
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "os_wrapper.h"
+
+#include "tfm_api.h"
+#include "tfm_ns_interface.h"
+
+/**
+ * \brief the ns_lock ID
+ */
+static uint32_t ns_lock_id = (uint32_t)NULL;
+
+__attribute__((weak))
+int32_t tfm_ns_interface_dispatch(veneer_fn fn,
+                                  uint32_t arg0, uint32_t arg1,
+                                  uint32_t arg2, uint32_t arg3)
+{
+    int32_t result;
+
+    /* TFM request protected by NS lock */
+    if (os_wrapper_mutex_acquire(ns_lock_id, OS_WRAPPER_WAIT_FOREVER)
+            != OS_WRAPPER_SUCCESS) {
+        return (int32_t)TFM_ERROR_GENERIC;
+    }
+
+    result = fn(arg0, arg1, arg2, arg3);
+
+    if (os_wrapper_mutex_release(ns_lock_id) != OS_WRAPPER_SUCCESS) {
+        return (int32_t)TFM_ERROR_GENERIC;
+    }
+
+    return result;
+}
+
+__attribute__((weak))
+enum tfm_status_e tfm_ns_interface_init(void)
+{
+    uint32_t id;
+
+    id = os_wrapper_mutex_create();
+    if (id == OS_WRAPPER_ERROR) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    ns_lock_id = id;
+    return TFM_SUCCESS;
+}
diff --git a/interface/src/tfm_ns_interface_cmsis_rtos.c b/interface/src/tfm_ns_interface_cmsis_rtos.c
deleted file mode 100644
index 7f4c685..0000000
--- a/interface/src/tfm_ns_interface_cmsis_rtos.c
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-#include <stdint.h>
-#include <stdbool.h>
-
-#include "cmsis_os2.h"
-
-#include "tfm_api.h"
-#include "tfm_ns_interface.h"
-
-/**
- * This file contains an example implementation of the NS interface APIs
- * described in tfm_ns_interface.h
- *
- */
-
-/**
- * \brief struct ns_lock_state type
- */
-struct ns_lock_state {
-    bool        init;
-    osMutexId_t id;
-};
-
-/**
- * \brief ns_lock status
- */
-static struct ns_lock_state ns_lock = {.init=false, .id=NULL};
-
-/**
- * \brief Mutex properties, NS lock
- */
-static const osMutexAttr_t ns_lock_attrib = {
-    .name = "ns_lock",
-    .attr_bits = osMutexPrioInherit,
-    .cb_mem = NULL,
-    .cb_size = 0U
-};
-
-__attribute__((weak))
-uint32_t tfm_ns_interface_dispatch(veneer_fn fn,
-                                   uint32_t arg0, uint32_t arg1,
-                                   uint32_t arg2, uint32_t arg3)
-{
-    int32_t result;
-
-    /* Check the NS lock has been initialized */
-    if (ns_lock.init == false) {
-        return (int32_t)TFM_ERROR_GENERIC;
-    }
-
-    /* TFM request protected by NS lock */
-    if (osMutexAcquire(ns_lock.id, osWaitForever) != osOK) {
-        return (int32_t)TFM_ERROR_GENERIC;
-    }
-
-    result = fn(arg0, arg1, arg2, arg3);
-
-    if (osMutexRelease(ns_lock.id) != osOK) {
-        return (int32_t)TFM_ERROR_GENERIC;
-    }
-
-    return result;
-}
-
-__attribute__((weak))
-enum tfm_status_e tfm_ns_interface_init()
-{
-    if (ns_lock.init == false) {
-        ns_lock.id = osMutexNew(&ns_lock_attrib);
-        ns_lock.init = true;
-        return TFM_SUCCESS;
-    } else {
-        return TFM_ERROR_GENERIC;
-    }
-}