SPM: Optimize the PSA API error handling process

In SPM implementation, message is a struct inside connection
handle struct and therefore it is unnecessary to check whether
the message pointer is NULL.

This patch:
- Skips checking message pointer after getting it from the
  connection handle.
- Returns an error code instead of calling tfm_core_panic()
  in some error processing.

Change-Id: Id45e63f8bbdd3d21db3ee32b43f4553ab7520a8f
Signed-off-by: Mingyang Sun <mingyang.sun@arm.com>
diff --git a/secure_fw/spm/ffm/backend_ipc.c b/secure_fw/spm/ffm/backend_ipc.c
index 7dc2a22..1572674 100644
--- a/secure_fw/spm/ffm/backend_ipc.c
+++ b/secure_fw/spm/ffm/backend_ipc.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
@@ -53,7 +53,7 @@
     struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
 
     if (!msg || !service || !service->p_ldinf || !service->partition) {
-        tfm_core_panic();
+        return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
     p_owner = service->partition;
diff --git a/secure_fw/spm/ffm/backend_sfn.c b/secure_fw/spm/ffm/backend_sfn.c
index 6cf39d5..3edde0c 100644
--- a/secure_fw/spm/ffm/backend_sfn.c
+++ b/secure_fw/spm/ffm/backend_sfn.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -42,7 +42,7 @@
     psa_status_t status;
 
     if (!msg || !service || !service->p_ldinf || !service->partition) {
-        tfm_core_panic();
+        return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
     msg->sfn_magic = TFM_MSG_MAGIC_SFN;
@@ -56,7 +56,7 @@
             status = ((sfn_init_fn_t)p_target->p_ldinf->entry)();
             /* Negative value indicates errors. */
             if (status < PSA_SUCCESS) {
-                tfm_core_panic();
+                return PSA_ERROR_PROGRAMMER_ERROR;
             }
         }
         p_target->state = SFN_PARTITION_STATE_INITED;
diff --git a/secure_fw/spm/ffm/psa_api.c b/secure_fw/spm/ffm/psa_api.c
index ec5580d..5d5d2d8 100644
--- a/secure_fw/spm/ffm/psa_api.c
+++ b/secure_fw/spm/ffm/psa_api.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -193,10 +193,6 @@
     }
 
     msg = tfm_spm_get_msg_buffer_from_conn_handle(connect_handle);
-    if (!msg) {
-        /* Have no enough resource to create message */
-        return PSA_ERROR_CONNECTION_BUSY;
-    }
 
     handle = tfm_spm_to_user_handle(connect_handle);
     /* No input or output needed for connect message */
@@ -250,7 +246,7 @@
 
         service = GET_STATELESS_SERVICE(index);
         if (!service) {
-            tfm_core_panic();
+            return PSA_ERROR_PROGRAMMER_ERROR;
         }
 
         sid = service->p_ldinf->sid;
@@ -306,11 +302,11 @@
         }
 
         service = conn_handle->internal_msg.service;
-    }
 
-    if (!service) {
-        /* FixMe: Need to implement one mechanism to resolve this failure. */
-        tfm_core_panic();
+        if (!service) {
+            /* FixMe: Need to implement a mechanism to resolve this failure. */
+            return PSA_ERROR_PROGRAMMER_ERROR;
+        }
     }
 
     privileged = tfm_spm_get_caller_privilege_mode();
@@ -385,10 +381,6 @@
      * Service or incorrectly formatted.
      */
     msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle);
-    if (!msg) {
-        /* FixMe: Need to implement one mechanism to resolve this failure. */
-        return PSA_ERROR_PROGRAMMER_ERROR;
-    }
 
     tfm_spm_fill_msg(msg, service, handle, type, client_id,
                      invecs, in_num, outvecs, out_num, outptr);
@@ -428,14 +420,10 @@
     service = conn_handle->internal_msg.service;
     if (!service) {
         /* FixMe: Need to implement one mechanism to resolve this failure. */
-        tfm_core_panic();
+        return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
     msg = tfm_spm_get_msg_buffer_from_conn_handle(conn_handle);
-    if (!msg) {
-        /* FixMe: Need to implement one mechanism to resolve this failure. */
-        tfm_core_panic();
-    }
 
     /*
      * It is a PROGRAMMER ERROR if the connection is currently handling a