blob: c77d9fdd8044b4daecb09ba13a17e2da2c1d56c7 [file] [log] [blame]
Balint Dobszay72c3f042020-11-23 18:23:57 +01001/*
Imre Kisd0ed5c22021-12-15 17:05:47 +01002 * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
Balint Dobszay72c3f042020-11-23 18:23:57 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Imre Kis3ed2e052023-06-28 14:19:31 +02007#include "components/rpc/common/endpoint/rpc_service_interface.h"
8#include "components/rpc/ts_rpc/endpoint/sp/ts_rpc_endpoint_sp.h"
Imre Kis317eb2c2022-07-05 16:43:42 +02009#include "components/service/secure_storage/factory/storage_factory.h"
10#include "components/service/secure_storage/frontend/secure_storage_provider/secure_storage_provider.h"
Imre Kis3ed2e052023-06-28 14:19:31 +020011#include "components/service/secure_storage/frontend/secure_storage_provider/secure_storage_uuid.h"
Imre Kis317eb2c2022-07-05 16:43:42 +020012#include "sp_api.h"
13#include "sp_discovery.h"
14#include "sp_messaging.h"
15#include "sp_rxtx.h"
16#include "trace.h"
Balint Dobszay72c3f042020-11-23 18:23:57 +010017
Balint Dobszay72c3f042020-11-23 18:23:57 +010018static uint8_t tx_buffer[4096] __aligned(4096);
19static uint8_t rx_buffer[4096] __aligned(4096);
20
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020021void sp_main(union ffa_boot_info *boot_info)
Balint Dobszay72c3f042020-11-23 18:23:57 +010022{
Imre Kis317eb2c2022-07-05 16:43:42 +020023 sp_result result = SP_RESULT_INTERNAL_ERROR;
Imre Kis3ed2e052023-06-28 14:19:31 +020024 struct rpc_service_interface *secure_storage_iface = NULL;
25 struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
Imre Kis76e8a3c2021-04-16 16:54:17 +020026 struct sp_msg req_msg = { 0 };
27 struct sp_msg resp_msg = { 0 };
Imre Kis317eb2c2022-07-05 16:43:42 +020028 struct secure_storage_provider secure_storage_provider = { 0 };
29 struct storage_backend *storage_backend = NULL;
Imre Kisf6562652022-07-04 15:33:13 +020030 uint16_t own_id = 0;
Imre Kis3ed2e052023-06-28 14:19:31 +020031 const struct rpc_uuid service_uuid = { .uuid = TS_PSA_PROTECTED_STORAGE_UUID };
32 rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
Balint Dobszay72c3f042020-11-23 18:23:57 +010033
34 /* Boot */
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020035 (void)boot_info;
Balint Dobszay72c3f042020-11-23 18:23:57 +010036
Imre Kis317eb2c2022-07-05 16:43:42 +020037 result = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
38 if (result != SP_RESULT_OK) {
39 EMSG("Failed to map RXTX buffers: %d", result);
40 goto fatal_error;
Balint Dobszay72c3f042020-11-23 18:23:57 +010041 }
42
Imre Kis317eb2c2022-07-05 16:43:42 +020043 result = sp_discovery_own_id_get(&own_id);
44 if (result != SP_RESULT_OK) {
45 EMSG("Failed to query own ID: %d", result);
46 goto fatal_error;
Balint Dobszay72c3f042020-11-23 18:23:57 +010047 }
48
julhal013a4207d2021-03-08 13:32:08 +000049 storage_backend = storage_factory_create(storage_factory_security_class_PROTECTED);
Imre Kis317eb2c2022-07-05 16:43:42 +020050 if (!storage_backend) {
Julian Hall072daf82022-08-04 12:04:41 +010051 EMSG("Failed to create storage backend");
Imre Kis317eb2c2022-07-05 16:43:42 +020052 goto fatal_error;
53 }
54
55 secure_storage_iface = secure_storage_provider_init(&secure_storage_provider,
Imre Kis3ed2e052023-06-28 14:19:31 +020056 storage_backend, &service_uuid);
Imre Kis317eb2c2022-07-05 16:43:42 +020057 if (!secure_storage_iface) {
58 EMSG("Failed to init secure storage provider");
59 goto fatal_error;
60 }
61
Imre Kis3ed2e052023-06-28 14:19:31 +020062 rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 1, 16);
63 if (rpc_status != RPC_SUCCESS) {
64 EMSG("Failed to initialize RPC endpoint: %d", rpc_status);
65 goto fatal_error;
66 }
67
68 rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, secure_storage_iface);
69 if (rpc_status != RPC_SUCCESS) {
70 EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
71 goto fatal_error;
72 }
Balint Dobszay72c3f042020-11-23 18:23:57 +010073
74 /* End of boot phase */
Imre Kis317eb2c2022-07-05 16:43:42 +020075 result = sp_msg_wait(&req_msg);
76 if (result != SP_RESULT_OK) {
77 EMSG("Failed to send message wait %d", result);
78 goto fatal_error;
79 }
Balint Dobszay72c3f042020-11-23 18:23:57 +010080
81 while (1) {
Imre Kis3ed2e052023-06-28 14:19:31 +020082 ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
Balint Dobszay72c3f042020-11-23 18:23:57 +010083
Imre Kis317eb2c2022-07-05 16:43:42 +020084 result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
85 if (result != SP_RESULT_OK) {
86 EMSG("Failed to send direct response %d", result);
87 result = sp_msg_wait(&req_msg);
88 if (result != SP_RESULT_OK) {
89 EMSG("Failed to send message wait %d", result);
90 goto fatal_error;
91 }
92 }
Balint Dobszay72c3f042020-11-23 18:23:57 +010093 }
Imre Kis317eb2c2022-07-05 16:43:42 +020094
95fatal_error:
96 /* SP is not viable */
97 EMSG("ITS SP error");
98 while (1) {}
Balint Dobszay72c3f042020-11-23 18:23:57 +010099}
100
101void sp_interrupt_handler(uint32_t interrupt_id)
102{
103 (void)interrupt_id;
104}