Refactor se_proxy_sp.c

Add error handling and logging.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I809885a874de04c1a213e543aff31f07ac68d837
diff --git a/deployments/se-proxy/common/se_proxy_sp.c b/deployments/se-proxy/common/se_proxy_sp.c
index 70eddb8..a37396f 100644
--- a/deployments/se-proxy/common/se_proxy_sp.c
+++ b/deployments/se-proxy/common/se_proxy_sp.c
@@ -3,59 +3,99 @@
  * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  */
 
-#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
-#include <rpc/common/demux/rpc_demux.h>
-#include <config/ramstore/config_ramstore.h>
-#include <config/loader/sp/sp_config_loader.h>
-#include <ffa_api.h>
-#include <sp_api.h>
-#include <sp_rxtx.h>
-#include <trace.h>
+#include "rpc/ffarpc/endpoint/ffarpc_call_ep.h"
+#include "rpc/common/demux/rpc_demux.h"
+#include "config/ramstore/config_ramstore.h"
+#include "config/loader/sp/sp_config_loader.h"
+#include "sp_api.h"
+#include "sp_discovery.h"
+#include "sp_rxtx.h"
+#include "trace.h"
 #include "service_proxy_factory.h"
 #include "../se_proxy_interfaces.h"
 
-static int sp_init(uint16_t *own_sp_id);
+static bool sp_init(uint16_t *own_sp_id);
 
 void __noreturn sp_main(struct ffa_init_info *init_info)
 {
-	struct ffa_call_ep ffarpc_call_ep;
-	struct sp_msg req_msg;
-	struct rpc_demux rpc_demux;
-	struct rpc_interface *rpc_iface;
+	struct ffa_call_ep ffarpc_call_ep = { 0 };
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
+	struct rpc_demux rpc_demux = { 0 };
+	struct rpc_interface *rpc_iface = NULL;
 	uint16_t own_id = 0;
+	sp_result result = SP_RESULT_INTERNAL_ERROR;
 
 	/* Boot phase */
-	if (sp_init(&own_id) != 0) goto fatal_error;
+	if (!sp_init(&own_id)) {
+		EMSG("Failed to init SP");
+		goto fatal_error;
+	}
 
 	config_ramstore_init();
-	sp_config_load(init_info);
+
+	if (!sp_config_load(init_info)) {
+		EMSG("Failed to load SP config");
+		goto fatal_error;
+	}
 
 	rpc_iface = rpc_demux_init(&rpc_demux);
+	if (!rpc_iface) {
+		EMSG("Failed to initialize RPC demux");
+		goto fatal_error;
+	}
+
 	ffa_call_ep_init(&ffarpc_call_ep, rpc_iface, own_id);
 
 	/* Create service proxies */
 	rpc_iface = its_proxy_create();
+	if (!rpc_iface) {
+		EMSG("Failed to create ITS proxy");
+		goto fatal_error;
+	}
+
 	rpc_demux_attach(&rpc_demux, SE_PROXY_INTERFACE_ID_ITS, rpc_iface);
 
 	rpc_iface = ps_proxy_create();
+	if (!rpc_iface) {
+		EMSG("Failed to create PS proxy");
+		goto fatal_error;
+	}
 	rpc_demux_attach(&rpc_demux, SE_PROXY_INTERFACE_ID_PS, rpc_iface);
 
 	rpc_iface = crypto_proxy_create();
+	if (!rpc_iface) {
+		EMSG("Failed to create Crypto proxy");
+		goto fatal_error;
+	}
 	rpc_demux_attach(&rpc_demux, SE_PROXY_INTERFACE_ID_CRYPTO, rpc_iface);
 
 	rpc_iface = attest_proxy_create();
+	if (!rpc_iface) {
+		EMSG("Failed to create Attestation proxy");
+		goto fatal_error;
+	}
 	rpc_demux_attach(&rpc_demux, SE_PROXY_INTERFACE_ID_ATTEST, rpc_iface);
 
 	/* End of boot phase */
-	sp_msg_wait(&req_msg);
+	result = sp_msg_wait(&req_msg);
+	if (result != SP_RESULT_OK) {
+		EMSG("Failed to send message wait %d", result);
+		goto fatal_error;
+	}
 
 	while (1) {
-
-		struct sp_msg resp_msg;
-
 		ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
 
-		sp_msg_send_direct_resp(&resp_msg, &req_msg);
+		result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
+		if (result != SP_RESULT_OK) {
+			EMSG("Failed to send direct response %d", result);
+			result = sp_msg_wait(&req_msg);
+			if (result != SP_RESULT_OK) {
+				EMSG("Failed to send message wait %d", result);
+				goto fatal_error;
+			}
+		}
 	}
 
 fatal_error:
@@ -69,21 +109,23 @@
 	(void)interrupt_id;
 }
 
-static int sp_init(uint16_t *own_sp_id)
+static bool sp_init(uint16_t *own_id)
 {
-	int status = -1;
-	ffa_result ffa_res;
-	sp_result sp_res;
+	sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
 	static uint8_t tx_buffer[4096] __aligned(4096);
 	static uint8_t rx_buffer[4096] __aligned(4096);
 
 	sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
-	if (sp_res == SP_RESULT_OK) {
-		ffa_res = ffa_id_get(own_sp_id);
-		if (ffa_res == FFA_OK) {
-			status = 0;
-		}
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to map RXTX buffers: %d", sp_res);
+		return false;
 	}
 
-	return status;
+	sp_res = sp_discovery_own_id_get(own_id);
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to query own ID: %d", sp_res);
+		return false;
+	}
+
+	return true;
 }