Interface: Refactor the NS interface

This patch refactors the NS interface source code
to provide an easier paradigm for integration by
marking the NS interface function as weak in order
for integrators to provide their own implementation
to fulfill their requirements.

Change-Id: Id8231cf91773d6850149a028e1b639432540efa0
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/interface/src/tfm_audit_api.c b/interface/src/tfm_audit_api.c
index fd4fc67..3439635 100644
--- a/interface/src/tfm_audit_api.c
+++ b/interface/src/tfm_audit_api.c
@@ -7,23 +7,23 @@
 
 #include "psa_audit_api.h"
 #include "tfm_veneers.h"
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 
 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
 
-#define API_DISPATCH(sfn_name)                               \
-    tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
-        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),      \
+#define API_DISPATCH(sfn_name)                                    \
+    tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
+        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),           \
         (uint32_t)out_vec, (uint32_t)ARRAY_SIZE(out_vec))
 
-#define API_DISPATCH_NO_INVEC(sfn_name)                      \
-    tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
-        (uint32_t)NULL, 0,                                   \
+#define API_DISPATCH_NO_INVEC(sfn_name)                           \
+    tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
+        (uint32_t)NULL, 0,                                        \
         (uint32_t)out_vec, (uint32_t)ARRAY_SIZE(out_vec))
 
-#define API_DISPATCH_NO_OUTVEC(sfn_name)                     \
-    tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
-        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),      \
+#define API_DISPATCH_NO_OUTVEC(sfn_name)                          \
+    tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
+        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),           \
         (uint32_t)NULL, 0)
 
 psa_status_t psa_audit_retrieve_record(const uint32_t record_index,
diff --git a/interface/src/tfm_crypto_api.c b/interface/src/tfm_crypto_api.c
index 152d785..400e123 100644
--- a/interface/src/tfm_crypto_api.c
+++ b/interface/src/tfm_crypto_api.c
@@ -8,7 +8,7 @@
 #include "tfm_veneers.h"
 #include "tfm_crypto_defs.h"
 #include "psa/crypto.h"
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 #ifdef TFM_PSA_API
 #include "psa_manifest/sid.h"
 #endif
@@ -41,14 +41,14 @@
         in_vec, ARRAY_SIZE(in_vec),                             \
         (psa_outvec *)NULL, 0)
 #else
-#define API_DISPATCH(sfn_name, sfn_id)                          \
-    tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer,    \
-        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                   \
+#define API_DISPATCH(sfn_name, sfn_id)                               \
+        tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
+        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                        \
         (uint32_t)out_vec, ARRAY_SIZE(out_vec))
 
-#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id)                \
-    tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer,    \
-        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                   \
+#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id)                     \
+        tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
+        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                        \
         (uint32_t)NULL, 0)
 #endif
 
diff --git a/interface/src/tfm_initial_attestation_api.c b/interface/src/tfm_initial_attestation_api.c
index 7324b1f..0f6377e 100644
--- a/interface/src/tfm_initial_attestation_api.c
+++ b/interface/src/tfm_initial_attestation_api.c
@@ -7,7 +7,7 @@
 
 #include "psa/initial_attestation.h"
 #include "tfm_veneers.h"
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 #include "psa/client.h"
 #ifdef TFM_PSA_API
 #include "psa_manifest/sid.h"
@@ -56,7 +56,8 @@
 
     return (enum psa_attest_err_t)status;
 #else
-    res = tfm_ns_lock_dispatch((veneer_fn)tfm_initial_attest_get_token_veneer,
+    res = tfm_ns_interface_dispatch(
+                               (veneer_fn)tfm_initial_attest_get_token_veneer,
                                (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                                (uint32_t)out_vec, IOVEC_LEN(out_vec));
 
@@ -101,7 +102,7 @@
 
     return (enum psa_attest_err_t)status;
 #else
-    return (enum psa_attest_err_t)tfm_ns_lock_dispatch(
+    return (enum psa_attest_err_t)tfm_ns_interface_dispatch(
                             (veneer_fn)tfm_initial_attest_get_token_size_veneer,
                             (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                             (uint32_t)out_vec, IOVEC_LEN(out_vec));
diff --git a/interface/src/tfm_ns_lock_cmsis_rtos.c b/interface/src/tfm_ns_interface_cmsis_rtos.c
similarity index 72%
rename from interface/src/tfm_ns_lock_cmsis_rtos.c
rename to interface/src/tfm_ns_interface_cmsis_rtos.c
index 3747319..7f4c685 100644
--- a/interface/src/tfm_ns_lock_cmsis_rtos.c
+++ b/interface/src/tfm_ns_interface_cmsis_rtos.c
@@ -10,7 +10,13 @@
 #include "cmsis_os2.h"
 
 #include "tfm_api.h"
-#include "tfm_ns_lock.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
@@ -23,7 +29,7 @@
 /**
  * \brief ns_lock status
  */
-static struct ns_lock_state ns_lock = {.init = false, .id = NULL};
+static struct ns_lock_state ns_lock = {.init=false, .id=NULL};
 
 /**
  * \brief Mutex properties, NS lock
@@ -35,12 +41,10 @@
     .cb_size = 0U
 };
 
-/**
- * \brief NS world, NS lock based dispatcher
- */
-int32_t tfm_ns_lock_dispatch(veneer_fn fn,
-                             uint32_t arg0, uint32_t arg1,
-                             uint32_t arg2, uint32_t arg3)
+__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;
 
@@ -63,10 +67,8 @@
     return result;
 }
 
-/**
- * \brief NS world, Init NS lock
- */
-enum tfm_status_e tfm_ns_lock_init(void)
+__attribute__((weak))
+enum tfm_status_e tfm_ns_interface_init()
 {
     if (ns_lock.init == false) {
         ns_lock.id = osMutexNew(&ns_lock_attrib);
diff --git a/interface/src/tfm_platform_api.c b/interface/src/tfm_platform_api.c
index b3daa07..3ef0c86 100644
--- a/interface/src/tfm_platform_api.c
+++ b/interface/src/tfm_platform_api.c
@@ -7,12 +7,12 @@
 
 #include <stdbool.h>
 #include "tfm_platform_api.h"
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 #include "tfm_veneers.h"
 
 enum tfm_platform_err_t tfm_platform_system_reset(void)
 {
-    return (enum tfm_platform_err_t) tfm_ns_lock_dispatch(
+    return (enum tfm_platform_err_t) tfm_ns_interface_dispatch(
                                 (veneer_fn)tfm_platform_sp_system_reset_veneer,
                                 0,
                                 0,
@@ -44,7 +44,7 @@
         outlen = 0;
     }
 
-    return (enum tfm_platform_err_t) tfm_ns_lock_dispatch(
+    return (enum tfm_platform_err_t) tfm_ns_interface_dispatch(
                                 (veneer_fn)tfm_platform_sp_ioctl_veneer,
                                 (uint32_t)in_vec, (uint32_t)inlen,
                                 (uint32_t)output, (uint32_t)outlen);
diff --git a/interface/src/tfm_psa_ns_api.c b/interface/src/tfm_psa_ns_api.c
index 838e7b0..1c83084 100644
--- a/interface/src/tfm_psa_ns_api.c
+++ b/interface/src/tfm_psa_ns_api.c
@@ -6,14 +6,15 @@
  */
 
 #include "psa/client.h"
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 #include "tfm_api.h"
 
 /**** API functions ****/
 
 uint32_t psa_framework_version(void)
 {
-    return tfm_ns_lock_dispatch((veneer_fn)tfm_psa_framework_version_veneer,
+    return tfm_ns_interface_dispatch(
+                                (veneer_fn)tfm_psa_framework_version_veneer,
                                 0,
                                 0,
                                 0,
@@ -22,7 +23,8 @@
 
 uint32_t psa_version(uint32_t sid)
 {
-    return tfm_ns_lock_dispatch((veneer_fn)tfm_psa_version_veneer,
+    return tfm_ns_interface_dispatch(
+                                (veneer_fn)tfm_psa_version_veneer,
                                 sid,
                                 0,
                                 0,
@@ -31,7 +33,8 @@
 
 psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version)
 {
-    return tfm_ns_lock_dispatch((veneer_fn)tfm_psa_connect_veneer,
+    return tfm_ns_interface_dispatch(
+                                (veneer_fn)tfm_psa_connect_veneer,
                                 sid,
                                 minor_version,
                                 0,
@@ -57,7 +60,8 @@
     in_vecs.len = in_len;
     out_vecs.base = out_vec;
     out_vecs.len = out_len;
-    return tfm_ns_lock_dispatch((veneer_fn)tfm_psa_call_veneer,
+    return tfm_ns_interface_dispatch(
+                                (veneer_fn)tfm_psa_call_veneer,
                                 (uint32_t)handle,
                                 (uint32_t)&in_vecs,
                                 (uint32_t)&out_vecs,
@@ -66,7 +70,8 @@
 
 void psa_close(psa_handle_t handle)
 {
-    tfm_ns_lock_dispatch((veneer_fn)tfm_psa_close_veneer,
+    (void)tfm_ns_interface_dispatch(
+                         (veneer_fn)tfm_psa_close_veneer,
                          (uint32_t)handle,
                          0,
                          0,
diff --git a/interface/src/tfm_sst_api.c b/interface/src/tfm_sst_api.c
index af06424..2722c67 100644
--- a/interface/src/tfm_sst_api.c
+++ b/interface/src/tfm_sst_api.c
@@ -7,7 +7,7 @@
 
 #include "psa/protected_storage.h"
 
-#include "tfm_ns_lock.h"
+#include "tfm_ns_interface.h"
 #include "tfm_veneers.h"
 #ifdef TFM_PSA_API
 #include "psa_manifest/sid.h"
@@ -53,7 +53,8 @@
         return PSA_PS_ERROR_OPERATION_FAILED;
     }
 #else
-    status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_set_req_veneer,
+    status = tfm_ns_interface_dispatch(
+                                  (veneer_fn)tfm_tfm_sst_set_req_veneer,
                                   (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                                   (uint32_t)out_vec, IOVEC_LEN(out_vec));
     if (status != PSA_SUCCESS) {
@@ -100,7 +101,8 @@
         return PSA_PS_ERROR_OPERATION_FAILED;
     }
 #else
-    status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_req_veneer,
+    status = tfm_ns_interface_dispatch(
+                                  (veneer_fn)tfm_tfm_sst_get_req_veneer,
                                   (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                                   (uint32_t)out_vec, IOVEC_LEN(out_vec));
 
@@ -144,7 +146,8 @@
         return PSA_PS_ERROR_OPERATION_FAILED;
     }
 #else
-    status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_info_req_veneer,
+    status = tfm_ns_interface_dispatch(
+                                  (veneer_fn)tfm_tfm_sst_get_info_req_veneer,
                                   (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                                   (uint32_t)out_vec, IOVEC_LEN(out_vec));
 
@@ -187,7 +190,8 @@
         return PSA_PS_ERROR_OPERATION_FAILED;
     }
 #else
-    status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_remove_req_veneer,
+    status = tfm_ns_interface_dispatch(
+                                  (veneer_fn)tfm_tfm_sst_remove_req_veneer,
                                   (uint32_t)in_vec,  IOVEC_LEN(in_vec),
                                   (uint32_t)out_vec, IOVEC_LEN(out_vec));
 
@@ -247,7 +251,8 @@
 
     psa_close(handle);
 #else
-    (void)tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_support_req_veneer,
+    (void)tfm_ns_interface_dispatch(
+                               (veneer_fn)tfm_tfm_sst_get_support_req_veneer,
                                (uint32_t)NULL,  0,
                                (uint32_t)out_vec, IOVEC_LEN(out_vec));
 #endif