Core: Refine 'logging' and 'assert' symbols

* Define a generic (non-core) TFM_ASSERT.
* Define core dedicated TFM_CORE_ASSERT.
* Remove redundant LOG_MSG defination in 'tfm_integ_test.h'.

Change-Id: I05437550844b29b2bc660a70566b48ccc3d5bca4
Signed-off-by: Ken Liu <ken.liu@arm.com>
diff --git a/app/tfm_integ_test.h b/app/tfm_integ_test.h
index 236e9ef..1c867a6 100644
--- a/app/tfm_integ_test.h
+++ b/app/tfm_integ_test.h
@@ -7,7 +7,6 @@
 
 #include <stdio.h>
 #include <cmsis_compiler.h>
-#include "log/tfm_log_raw.h"
 
 #ifndef __TFM_INTEG_TEST_H__
 #define __TFM_INTEG_TEST_H__
@@ -42,18 +41,6 @@
  */
 void execute_ns_interactive_tests(void);
 
-/**
- * \brief Logging function
- *
- */
-__attribute__((always_inline)) __STATIC_INLINE void LOG_MSG(const char *MSG)
-{
-    /* if IPSR is non-zero, exception is active. NOT banked S/NS */
-    if (!__get_IPSR()) {
-        tfm_log_printf("\t\033[1;32m[Non-Sec] %s\033[0m\r\n", MSG);
-    }
-}
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/interface/include/log/tfm_assert.h b/interface/include/log/tfm_assert.h
new file mode 100644
index 0000000..67d114d
--- /dev/null
+++ b/interface/include/log/tfm_assert.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_ASSERT_H__
+#define __TFM_ASSERT_H__
+
+#include "tfm_log_raw.h"
+
+/* Functions and macros in this file is for 'thread mode' usage. */
+
+#define TFM_ASSERT(cond)                                                    \
+            do {                                                            \
+                if (!(cond)) {                                              \
+                    tfm_log_printf("Assert:%s:%d", __FUNCTION__, __LINE__); \
+                    while (1)                                               \
+                        ;                                                   \
+                }                                                           \
+            } while (0)
+
+#endif /* __TFM_ASSERT_H__ */
diff --git a/secure_fw/core/include/tfm_utils.h b/secure_fw/core/include/tfm_utils.h
index 993bf5a..7a33655 100644
--- a/secure_fw/core/include/tfm_utils.h
+++ b/secure_fw/core/include/tfm_utils.h
@@ -7,23 +7,24 @@
 #ifndef __TFM_UTILS_H__
 #define __TFM_UTILS_H__
 
-#include <stdio.h>
-
 /* CPU spin here */
 void tfm_core_panic(void);
 
-/* Assert and spin */
-#define TFM_ASSERT(cond)                                            \
+/* Core assert and spin */
+#ifndef NDEBUG
+#define TFM_CORE_ASSERT(cond)                                       \
             do {                                                    \
                 if (!(cond)) {                                      \
-                    printf("Assert:%s:%d", __FUNCTION__, __LINE__); \
                     while (1)                                       \
                         ;                                           \
                 }                                                   \
             } while (0)
+#else
+#define TFM_CORE_ASSERT(cond)
+#endif
 
 /* Get container structure start address from member */
 #define TFM_GET_CONTAINER_PTR(ptr, type, member) \
     (type *)((unsigned long)(ptr) - offsetof(type, member))
 
-#endif /* __TFM_UTILS_H__ */
\ No newline at end of file
+#endif /* __TFM_UTILS_H__ */
diff --git a/secure_fw/core/ipc/tfm_rpc.c b/secure_fw/core/ipc/tfm_rpc.c
index b173e21..234cdfe 100644
--- a/secure_fw/core/ipc/tfm_rpc.c
+++ b/secure_fw/core/ipc/tfm_rpc.c
@@ -33,7 +33,7 @@
 uint32_t tfm_rpc_psa_version(const struct client_call_params_t *params,
                              bool ns_caller)
 {
-    TFM_ASSERT(params != NULL);
+    TFM_CORE_ASSERT(params != NULL);
 
     return tfm_psa_version(params->sid, ns_caller);
 }
@@ -41,7 +41,7 @@
 psa_status_t tfm_rpc_psa_connect(const struct client_call_params_t *params,
                                  bool ns_caller)
 {
-    TFM_ASSERT(params != NULL);
+    TFM_CORE_ASSERT(params != NULL);
 
     return tfm_psa_connect(params->sid, params->version, ns_caller);
 }
@@ -49,7 +49,7 @@
 psa_status_t tfm_rpc_psa_call(const struct client_call_params_t *params,
                               bool ns_caller)
 {
-    TFM_ASSERT(params != NULL);
+    TFM_CORE_ASSERT(params != NULL);
 
     return tfm_psa_call(params->handle, params->type,
                         params->in_vec, params->in_len,
@@ -60,7 +60,7 @@
 void tfm_rpc_psa_close(const struct client_call_params_t *params,
                        bool ns_caller)
 {
-    TFM_ASSERT(params != NULL);
+    TFM_CORE_ASSERT(params != NULL);
 
     tfm_psa_close(params->handle, ns_caller);
 }
diff --git a/secure_fw/core/ipc/tfm_spe_mailbox.c b/secure_fw/core/ipc/tfm_spe_mailbox.c
index 3723cee..758b735 100644
--- a/secure_fw/core/ipc/tfm_spe_mailbox.c
+++ b/secure_fw/core/ipc/tfm_spe_mailbox.c
@@ -23,8 +23,8 @@
 {
     struct client_call_params_t spm_params = {0};
 
-    TFM_ASSERT(params != NULL);
-    TFM_ASSERT(psa_ret != NULL);
+    TFM_CORE_ASSERT(params != NULL);
+    TFM_CORE_ASSERT(psa_ret != NULL);
 
     (void)client_id;
 
@@ -188,7 +188,7 @@
     struct ns_mailbox_queue_t *ns_queue = spe_mailbox_queue.ns_queue;
     struct mailbox_msg_t *msg_ptr;
 
-    TFM_ASSERT(ns_queue != NULL);
+    TFM_CORE_ASSERT(ns_queue != NULL);
 
     tfm_mailbox_hal_enter_critical();
 
@@ -291,7 +291,7 @@
     int32_t ret;
     struct ns_mailbox_queue_t *ns_queue = spe_mailbox_queue.ns_queue;
 
-    TFM_ASSERT(ns_queue != NULL);
+    TFM_CORE_ASSERT(ns_queue != NULL);
 
     /*
      * If handle == MAILBOX_MSG_NULL_HANDLE, reply to the mailbox message
diff --git a/secure_fw/core/ipc/tfm_svcalls.c b/secure_fw/core/ipc/tfm_svcalls.c
index 27f1258..8676347 100644
--- a/secure_fw/core/ipc/tfm_svcalls.c
+++ b/secure_fw/core/ipc/tfm_svcalls.c
@@ -50,7 +50,7 @@
 {
     uint32_t sid;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     sid = (uint32_t)args[0];
 
     return tfm_psa_version(sid, ns_caller);
@@ -61,7 +61,7 @@
     uint32_t sid;
     uint32_t version;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     sid = (uint32_t)args[0];
     version = (uint32_t)args[1];
 
@@ -78,7 +78,7 @@
     uint32_t privileged;
     int32_t type;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     handle = (psa_handle_t)args[0];
 
     partition = tfm_spm_get_running_partition();
@@ -149,7 +149,7 @@
 {
     psa_handle_t handle;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     handle = args[0];
 
     return tfm_psa_close(handle, ns_caller);
@@ -173,7 +173,7 @@
     uint32_t timeout;
     struct spm_partition_desc_t *partition = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     signal_mask = (psa_signal_t)args[0];
     timeout = args[1];
 
@@ -243,7 +243,7 @@
     struct spm_partition_desc_t *partition = NULL;
     uint32_t privileged;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     signal = (psa_signal_t)args[0];
     msg = (psa_msg_t *)args[1];
 
@@ -334,7 +334,7 @@
     void *rhandle = NULL;
     struct tfm_msg_body_t *msg = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     msg_handle = (psa_handle_t)args[0];
     rhandle = (void *)args[1];
 
@@ -380,7 +380,7 @@
     uint32_t privileged;
     struct spm_partition_desc_t *partition = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     msg_handle = (psa_handle_t)args[0];
     invec_idx = args[1];
     buffer = (void *)args[2];
@@ -462,7 +462,7 @@
     size_t num_bytes;
     struct tfm_msg_body_t *msg = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     msg_handle = (psa_handle_t)args[0];
     invec_idx = args[1];
     num_bytes = (size_t)args[2];
@@ -537,7 +537,7 @@
     uint32_t privileged;
     struct spm_partition_desc_t *partition = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     msg_handle = (psa_handle_t)args[0];
     outvec_idx = args[1];
     buffer = (void *)args[2];
@@ -604,11 +604,11 @@
      */
     /* If it is a NS request via RPC, the owner of this message is not set */
     if (!is_tfm_rpc_msg(msg)) {
-        TFM_ASSERT(msg->ack_evnt.owner->status == THRD_STAT_BLOCK);
+        TFM_CORE_ASSERT(msg->ack_evnt.owner->status == THRD_STAT_BLOCK);
     }
 
     while (msg->msg.out_size[i] != 0) {
-        TFM_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
+        TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
         msg->caller_outvec[i].len = msg->outvec[i].len;
         i++;
     }
@@ -634,7 +634,7 @@
     struct tfm_msg_body_t *msg = NULL;
     int32_t ret = PSA_SUCCESS;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     msg_handle = (psa_handle_t)args[0];
     status = (psa_status_t)args[1];
 
@@ -773,7 +773,7 @@
 {
     int32_t partition_id;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     partition_id = (int32_t)args[0];
 
     return notify_with_signal(partition_id, PSA_DOORBELL);
@@ -877,7 +877,7 @@
     int32_t ret;
     struct spm_partition_desc_t *partition = NULL;
 
-    TFM_ASSERT(args != NULL);
+    TFM_CORE_ASSERT(args != NULL);
     irq_signal = (psa_signal_t)args[0];
 
     /* It is a fatal error if passed signal indicates more than one signals. */
diff --git a/secure_fw/core/ipc/tfm_thread.c b/secure_fw/core/ipc/tfm_thread.c
index 65aa018..6b8d773 100644
--- a/secure_fw/core/ipc/tfm_thread.c
+++ b/secure_fw/core/ipc/tfm_thread.c
@@ -155,7 +155,7 @@
 
 void tfm_thrd_set_status(struct tfm_thrd_ctx *pth, uint32_t new_status)
 {
-    TFM_ASSERT(pth != NULL && new_status < THRD_STAT_INVALID);
+    TFM_CORE_ASSERT(pth != NULL && new_status < THRD_STAT_INVALID);
 
     pth->status = new_status;
     update_running_head(&RUNN_HEAD, pth);
@@ -174,8 +174,8 @@
      * a caller provided thread as current thread. This function
      * should get called only ONCE; further calling triggers assert.
      */
-    TFM_ASSERT(CURR_THRD == NULL);
-    TFM_ASSERT(pth != NULL);
+    TFM_CORE_ASSERT(CURR_THRD == NULL);
+    TFM_CORE_ASSERT(pth != NULL);
 
     CURR_THRD = pth;
     tfm_thrd_activate_schedule();
@@ -201,8 +201,8 @@
                              struct tfm_thrd_ctx *prev,
                              struct tfm_thrd_ctx *next)
 {
-    TFM_ASSERT(prev != NULL);
-    TFM_ASSERT(next != NULL);
+    TFM_CORE_ASSERT(prev != NULL);
+    TFM_CORE_ASSERT(next != NULL);
 
     /*
      * First, update latest context into the current thread context.
diff --git a/secure_fw/core/ipc/tfm_wait.c b/secure_fw/core/ipc/tfm_wait.c
index 8bbe64a..549b63b 100644
--- a/secure_fw/core/ipc/tfm_wait.c
+++ b/secure_fw/core/ipc/tfm_wait.c
@@ -10,7 +10,7 @@
 
 void tfm_event_wait(struct tfm_event_t *pevnt)
 {
-    TFM_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
+    TFM_CORE_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
 
     pevnt->owner = tfm_thrd_curr_thread();
     tfm_thrd_set_status(pevnt->owner, THRD_STAT_BLOCK);
@@ -19,7 +19,7 @@
 
 void tfm_event_wake(struct tfm_event_t *pevnt, uint32_t retval)
 {
-    TFM_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
+    TFM_CORE_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
 
     if (pevnt->owner && pevnt->owner->status == THRD_STAT_BLOCK) {
         tfm_thrd_set_status(pevnt->owner, THRD_STAT_RUNNING);
diff --git a/secure_fw/core/tfm_core_utils.c b/secure_fw/core/tfm_core_utils.c
index 9c38f77..ba9a4ea 100644
--- a/secure_fw/core/tfm_core_utils.c
+++ b/secure_fw/core/tfm_core_utils.c
@@ -20,7 +20,7 @@
     union tfm_core_addr_t p_dest;
     union tfm_core_addr_t p_src;
 
-    TFM_ASSERT(dest != src);
+    TFM_CORE_ASSERT(dest != src);
 
     p_dest.p_byte = (uint8_t *)dest;
     p_src.p_byte = (uint8_t *)src;
diff --git a/secure_fw/core/tfm_nspm_ipc.c b/secure_fw/core/tfm_nspm_ipc.c
index 13d78db..47e47b0 100644
--- a/secure_fw/core/tfm_nspm_ipc.c
+++ b/secure_fw/core/tfm_nspm_ipc.c
@@ -11,6 +11,7 @@
 #include "tfm_nspm.h"
 #include "tfm_utils.h"
 #include "tfm_internal.h"
+#include "log/tfm_assert.h"
 #include "log/tfm_log.h"
 
 #define DEFAULT_NS_CLIENT_ID ((int32_t)-1)
diff --git a/secure_fw/spm/spm_api_func.c b/secure_fw/spm/spm_api_func.c
index bf2b4b8..45059d1 100644
--- a/secure_fw/spm/spm_api_func.c
+++ b/secure_fw/spm/spm_api_func.c
@@ -36,19 +36,9 @@
     sp_error_type_t err_type,
     int32_t err_code)
 {
-#ifdef TFM_CORE_DEBUG
-    if (err_type == TFM_INIT_FAILURE) {
-        printf("Partition init failed for partition id 0x%08X\r\n",
-               partition->static_data->partition_id);
-    } else {
-        printf(
-            "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
-            err_type, err_code, partition->static_data->partition_id);
-    }
-#else
     (void)err_type;
     (void)err_code;
-#endif
+
     tfm_spm_partition_set_state(partition->static_data->partition_id,
                                 SPM_PARTITION_STATE_CLOSED);
 }
diff --git a/secure_fw/spm/spm_api_ipc.c b/secure_fw/spm/spm_api_ipc.c
index 593698b..e126b7e 100644
--- a/secure_fw/spm/spm_api_ipc.c
+++ b/secure_fw/spm/spm_api_ipc.c
@@ -55,7 +55,7 @@
 {
     struct tfm_conn_handle_t *p_handle;
 
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
 
     /* Get buffer for handle list structure from handle pool */
     p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
@@ -94,7 +94,7 @@
     tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
                                   psa_handle_t conn_handle)
 {
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
 
     return (struct tfm_conn_handle_t *)conn_handle;
 }
@@ -104,7 +104,7 @@
 {
     struct tfm_conn_handle_t *p_handle;
 
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
 
     /* There are many handles for each RoT Service */
     p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
@@ -129,9 +129,9 @@
 {
     struct tfm_conn_handle_t *p_handle;
 
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
     /* Set reverse handle value only be allowed for a connected handle */
-    TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
+    TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
 
     /* There are many handles for each RoT Service */
     p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
@@ -148,9 +148,9 @@
 {
     struct tfm_conn_handle_t *p_handle;
 
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
     /* Get reverse handle value only be allowed for a connected handle */
-    TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
+    TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
 
     /* There are many handles for each RoT Service */
     p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
@@ -169,7 +169,7 @@
     struct tfm_list_node_t *node, *head;
     struct tfm_spm_service_t *service;
 
-    TFM_ASSERT(partition);
+    TFM_CORE_ASSERT(partition);
 
     if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
         tfm_core_panic();
@@ -243,7 +243,7 @@
 int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
                                      uint32_t version)
 {
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
 
     switch (service->service_db->version_policy) {
     case TFM_VERSION_POLICY_RELAXED:
@@ -269,7 +269,7 @@
     struct spm_partition_desc_t *partition = NULL;
     int32_t i;
 
-    TFM_ASSERT(service);
+    TFM_CORE_ASSERT(service);
 
     if (ns_caller) {
         if (!service->service_db->non_secure_client) {
@@ -347,7 +347,7 @@
 struct tfm_msg_body_t *
     tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
 {
-    TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
+    TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
 
     return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
 }
@@ -362,13 +362,13 @@
 {
     uint32_t i;
 
-    TFM_ASSERT(msg);
-    TFM_ASSERT(service);
-    TFM_ASSERT(!(invec == NULL && in_len != 0));
-    TFM_ASSERT(!(outvec == NULL && out_len != 0));
-    TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
-    TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
-    TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
+    TFM_CORE_ASSERT(msg);
+    TFM_CORE_ASSERT(service);
+    TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
+    TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
+    TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
+    TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
+    TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
 
     /* Clear message buffer before using it */
     tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
@@ -410,8 +410,8 @@
     struct spm_partition_runtime_data_t *p_runtime_data =
                                             &service->partition->runtime_data;
 
-    TFM_ASSERT(service);
-    TFM_ASSERT(msg);
+    TFM_CORE_ASSERT(service);
+    TFM_CORE_ASSERT(msg);
 
     /* Enqueue message to service message queue */
     if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {