1 /*
2  * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "components/rpc/common/endpoint/rpc_service_interface.h"
8 #include "components/rpc/ts_rpc/endpoint/sp/ts_rpc_endpoint_sp.h"
9 #include "components/service/secure_storage/factory/storage_factory.h"
10 #include "components/service/secure_storage/frontend/secure_storage_provider/secure_storage_provider.h"
11 #include "components/service/secure_storage/frontend/secure_storage_provider/secure_storage_uuid.h"
12 #include "components/service/log/factory/log_factory.h"
13 #include "sp_api.h"
14 #include "sp_discovery.h"
15 #include "sp_messaging.h"
16 #include "sp_rxtx.h"
17 #include "trace.h"
18 
19 static uint8_t tx_buffer[4096] __aligned(4096);
20 static uint8_t rx_buffer[4096] __aligned(4096);
21 
sp_main(union ffa_boot_info * boot_info)22 void sp_main(union ffa_boot_info *boot_info)
23 {
24 	sp_result result = SP_RESULT_INTERNAL_ERROR;
25 	struct rpc_service_interface *secure_storage_iface = NULL;
26 	struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
27 	struct sp_msg req_msg = { 0 };
28 	struct sp_msg resp_msg = { 0 };
29 	struct secure_storage_provider secure_storage_provider = { 0 };
30 	struct storage_backend *storage_backend = NULL;
31 	uint16_t own_id = 0;
32 	const struct rpc_uuid service_uuid = { .uuid = TS_PSA_INTERNAL_TRUSTED_STORAGE_UUID };
33 	rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
34 
35 	/* Boot */
36 	(void)boot_info;
37 
38 	result = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
39 	if (result != SP_RESULT_OK) {
40 		EMSG("Failed to map RXTX buffers: %d", result);
41 		goto fatal_error;
42 	}
43 
44 	IMSG("Start discovering logging service");
45 	if (log_factory_create()) {
46 		IMSG("Logging service discovery successful");
47 	} else {
48 		EMSG("Logging service discovery failed, falling back to console log");
49 	}
50 
51 	result = sp_discovery_own_id_get(&own_id);
52 	if (result != SP_RESULT_OK) {
53 		EMSG("Failed to query own ID: %d", result);
54 		goto fatal_error;
55 	}
56 
57 	storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
58 	if (!storage_backend) {
59 		EMSG("Failed to create storage backend");
60 		goto fatal_error;
61 	}
62 
63 	secure_storage_iface = secure_storage_provider_init(&secure_storage_provider,
64 							    storage_backend, &service_uuid);
65 	if (!secure_storage_iface) {
66 		EMSG("Failed to init secure storage provider");
67 		goto fatal_error;
68 	}
69 
70 	rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 1, 16);
71 	if (rpc_status != RPC_SUCCESS) {
72 		EMSG("Failed to initialize RPC endpoint: %d", rpc_status);
73 		goto fatal_error;
74 	}
75 
76 	rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, secure_storage_iface);
77 	if (rpc_status != RPC_SUCCESS) {
78 		EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
79 		goto fatal_error;
80 	}
81 
82 	/* End of boot phase */
83 	result = sp_msg_wait(&req_msg);
84 	if (result != SP_RESULT_OK) {
85 		EMSG("Failed to send message wait %d", result);
86 		goto fatal_error;
87 	}
88 
89 	while (1) {
90 		ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
91 
92 		result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
93 		if (result != SP_RESULT_OK) {
94 			EMSG("Failed to send direct response %d", result);
95 			result = sp_msg_wait(&req_msg);
96 			if (result != SP_RESULT_OK) {
97 				EMSG("Failed to send message wait %d", result);
98 				goto fatal_error;
99 			}
100 		}
101 	}
102 
103 fatal_error:
104 	/* SP is not viable */
105 	EMSG("ITS SP error");
106 	while (1) {}
107 }
108 
sp_interrupt_handler(uint32_t interrupt_id)109 void sp_interrupt_handler(uint32_t interrupt_id)
110 {
111 	(void)interrupt_id;
112 }
113 
ffa_vm_created_handler(uint16_t vm_id,uint64_t handle)114 ffa_result ffa_vm_created_handler(uint16_t vm_id, uint64_t handle)
115 {
116 	(void)vm_id;
117 	(void)handle;
118 
119 	return FFA_OK;
120 }
121 
ffa_vm_destroyed_handler(uint16_t vm_id,uint64_t handle)122 ffa_result ffa_vm_destroyed_handler(uint16_t vm_id, uint64_t handle)
123 {
124 	(void)vm_id;
125 	(void)handle;
126 
127 	return FFA_OK;
128 }
129