blob: c43bccbe20a68d4a04e121dcd1f2c8ae2bd69e42 [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"
Gabor Ambrus70908d02023-08-15 14:42:38 +020012#include "components/service/log/factory/log_factory.h"
Imre Kis317eb2c2022-07-05 16:43:42 +020013#include "sp_api.h"
14#include "sp_discovery.h"
15#include "sp_messaging.h"
16#include "sp_rxtx.h"
17#include "trace.h"
Balint Dobszay72c3f042020-11-23 18:23:57 +010018
Balint Dobszay72c3f042020-11-23 18:23:57 +010019static uint8_t tx_buffer[4096] __aligned(4096);
20static uint8_t rx_buffer[4096] __aligned(4096);
21
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020022void sp_main(union ffa_boot_info *boot_info)
Balint Dobszay72c3f042020-11-23 18:23:57 +010023{
Imre Kis317eb2c2022-07-05 16:43:42 +020024 sp_result result = SP_RESULT_INTERNAL_ERROR;
Imre Kis3ed2e052023-06-28 14:19:31 +020025 struct rpc_service_interface *secure_storage_iface = NULL;
26 struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
Imre Kis76e8a3c2021-04-16 16:54:17 +020027 struct sp_msg req_msg = { 0 };
28 struct sp_msg resp_msg = { 0 };
Imre Kis317eb2c2022-07-05 16:43:42 +020029 struct secure_storage_provider secure_storage_provider = { 0 };
30 struct storage_backend *storage_backend = NULL;
Imre Kisf6562652022-07-04 15:33:13 +020031 uint16_t own_id = 0;
Imre Kis3ed2e052023-06-28 14:19:31 +020032 const struct rpc_uuid service_uuid = { .uuid = TS_PSA_PROTECTED_STORAGE_UUID };
33 rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
Balint Dobszay72c3f042020-11-23 18:23:57 +010034
35 /* Boot */
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020036 (void)boot_info;
Balint Dobszay72c3f042020-11-23 18:23:57 +010037
Imre Kis317eb2c2022-07-05 16:43:42 +020038 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;
Balint Dobszay72c3f042020-11-23 18:23:57 +010042 }
43
Gabor Ambrus70908d02023-08-15 14:42:38 +020044 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
Imre Kis317eb2c2022-07-05 16:43:42 +020051 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;
Balint Dobszay72c3f042020-11-23 18:23:57 +010055 }
56
julhal013a4207d2021-03-08 13:32:08 +000057 storage_backend = storage_factory_create(storage_factory_security_class_PROTECTED);
Imre Kis317eb2c2022-07-05 16:43:42 +020058 if (!storage_backend) {
Julian Hall072daf82022-08-04 12:04:41 +010059 EMSG("Failed to create storage backend");
Imre Kis317eb2c2022-07-05 16:43:42 +020060 goto fatal_error;
61 }
62
63 secure_storage_iface = secure_storage_provider_init(&secure_storage_provider,
Imre Kis3ed2e052023-06-28 14:19:31 +020064 storage_backend, &service_uuid);
Imre Kis317eb2c2022-07-05 16:43:42 +020065 if (!secure_storage_iface) {
66 EMSG("Failed to init secure storage provider");
67 goto fatal_error;
68 }
69
Imre Kis3ed2e052023-06-28 14:19:31 +020070 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 }
Balint Dobszay72c3f042020-11-23 18:23:57 +010081
82 /* End of boot phase */
Imre Kis317eb2c2022-07-05 16:43:42 +020083 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 }
Balint Dobszay72c3f042020-11-23 18:23:57 +010088
89 while (1) {
Imre Kis3ed2e052023-06-28 14:19:31 +020090 ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
Balint Dobszay72c3f042020-11-23 18:23:57 +010091
Imre Kis317eb2c2022-07-05 16:43:42 +020092 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 }
Balint Dobszay72c3f042020-11-23 18:23:57 +0100101 }
Imre Kis317eb2c2022-07-05 16:43:42 +0200102
103fatal_error:
104 /* SP is not viable */
105 EMSG("ITS SP error");
106 while (1) {}
Balint Dobszay72c3f042020-11-23 18:23:57 +0100107}
108
109void sp_interrupt_handler(uint32_t interrupt_id)
110{
111 (void)interrupt_id;
112}