blob: e671954b60488525f35e098fea32ee7e437ce607 [file] [log] [blame]
Julian Hall4061ed62020-11-23 18:24:06 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
Imre Kis9757f6b2022-07-26 17:19:46 +02003 * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
Julian Hall4061ed62020-11-23 18:24:06 +01004 */
5
Imre Kisbd8011a2023-07-04 18:00:39 +02006#include "components/rpc/common/endpoint/rpc_service_interface.h"
7#include "components/rpc/ts_rpc/endpoint/sp/ts_rpc_endpoint_sp.h"
Imre Kis6ef4d0d2022-07-05 16:43:17 +02008#include "service/secure_storage/factory/storage_factory.h"
9#include "service/crypto/factory/crypto_provider_factory.h"
10#include "service/crypto/backend/mbedcrypto/mbedcrypto_backend.h"
11#include "protocols/rpc/common/packed-c/status.h"
12#include "config/ramstore/config_ramstore.h"
13#include "config/loader/sp/sp_config_loader.h"
14#include "sp_api.h"
15#include "sp_discovery.h"
16#include "sp_messaging.h"
17#include "sp_rxtx.h"
18#include "trace.h"
julhal013a4207d2021-03-08 13:32:08 +000019
Imre Kis6ef4d0d2022-07-05 16:43:17 +020020static bool sp_init(uint16_t *own_sp_id);
Julian Hall4061ed62020-11-23 18:24:06 +010021
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020022void __noreturn sp_main(union ffa_boot_info *boot_info)
Julian Hall4061ed62020-11-23 18:24:06 +010023{
Imre Kis6ef4d0d2022-07-05 16:43:17 +020024 struct crypto_provider *crypto_provider = NULL;
Imre Kisbd8011a2023-07-04 18:00:39 +020025 struct crypto_provider *crypto_protobuf_provider = NULL;
26 struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
27 struct rpc_service_interface *crypto_iface = NULL;
28 struct rpc_service_interface *crypto_iface_protobuf = NULL;
Imre Kis76e8a3c2021-04-16 16:54:17 +020029 struct sp_msg req_msg = { 0 };
30 struct sp_msg resp_msg = { 0 };
Imre Kis6ef4d0d2022-07-05 16:43:17 +020031 struct storage_backend *storage_backend = NULL;
Imre Kisf6562652022-07-04 15:33:13 +020032 uint16_t own_id = 0;
Imre Kis6ef4d0d2022-07-05 16:43:17 +020033 psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
34 sp_result result = SP_RESULT_INTERNAL_ERROR;
Imre Kisbd8011a2023-07-04 18:00:39 +020035 rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
Julian Hall4061ed62020-11-23 18:24:06 +010036
julhal013a4207d2021-03-08 13:32:08 +000037 /* Boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020038 if (!sp_init(&own_id)) {
39 EMSG("Failed to init SP");
40 goto fatal_error;
41 }
Julian Hall4061ed62020-11-23 18:24:06 +010042
julhal012c18fbf2021-02-01 08:29:28 +000043 config_ramstore_init();
Imre Kis6ef4d0d2022-07-05 16:43:17 +020044
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020045 if (!sp_config_load(boot_info)) {
Imre Kis6ef4d0d2022-07-05 16:43:17 +020046 EMSG("Failed to load SP config");
47 goto fatal_error;
48 }
julhal012c18fbf2021-02-01 08:29:28 +000049
julhal013a4207d2021-03-08 13:32:08 +000050 /* Create a storage backend for persistent key storage - prefer ITS */
51 storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
Imre Kis6ef4d0d2022-07-05 16:43:17 +020052 if (!storage_backend) {
53 EMSG("Failed to create storage factory");
54 goto fatal_error;
55 }
Julian Hall4061ed62020-11-23 18:24:06 +010056
57 /* Initialize the crypto service */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020058 psa_status = mbedcrypto_backend_init(storage_backend, 0);
59 if (psa_status != PSA_SUCCESS) {
60 EMSG("Failed to init Mbed TLS backend: %d", psa_status);
61 goto fatal_error;
62 }
julhal01734dbad2020-12-21 10:27:41 +000063
Imre Kis6ef4d0d2022-07-05 16:43:17 +020064 crypto_provider = crypto_provider_factory_create();
65 if (!crypto_provider) {
66 EMSG("Failed to create crypto provider factory");
67 goto fatal_error;
68 }
Julian Hall9061e6c2021-06-29 14:24:20 +010069
Imre Kisbd8011a2023-07-04 18:00:39 +020070 crypto_protobuf_provider = crypto_protobuf_provider_factory_create();
71 if (!crypto_protobuf_provider) {
72 EMSG("Failed to create crypto protobuf provider factory");
73 goto fatal_error;
74 }
75
Imre Kis6ef4d0d2022-07-05 16:43:17 +020076 crypto_iface = service_provider_get_rpc_interface(&crypto_provider->base_provider);
77 if (!crypto_iface) {
78 EMSG("Failed to create service provider RPC interface");
79 goto fatal_error;
Julian Hall9061e6c2021-06-29 14:24:20 +010080 }
julhal01734dbad2020-12-21 10:27:41 +000081
Imre Kisbd8011a2023-07-04 18:00:39 +020082 crypto_iface_protobuf = service_provider_get_rpc_interface(
83 &crypto_protobuf_provider->base_provider);
84 if (!crypto_iface_protobuf) {
85 EMSG("Failed to create service provider RPC interface");
86 goto fatal_error;
87 }
88
89 rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 2, 16);
90 if (rpc_status != RPC_SUCCESS) {
91 EMSG("Failed to initialize RPC endpoint: %d", rpc_status);
92 goto fatal_error;
93 }
94
95 rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface);
96 if (rpc_status != RPC_SUCCESS) {
97 EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
98 goto fatal_error;
99 }
100
101 rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface_protobuf);
102 if (rpc_status != RPC_SUCCESS) {
103 EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
104 goto fatal_error;
105 }
Julian Hall4061ed62020-11-23 18:24:06 +0100106
julhal011260f102021-02-15 17:34:08 +0000107 /* End of boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200108 result = sp_msg_wait(&req_msg);
109 if (result != SP_RESULT_OK) {
110 EMSG("Failed to send message wait %d", result);
111 goto fatal_error;
112 }
Julian Hall4061ed62020-11-23 18:24:06 +0100113
114 while (1) {
Imre Kisbd8011a2023-07-04 18:00:39 +0200115 ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
Julian Hall4061ed62020-11-23 18:24:06 +0100116
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200117 result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
118 if (result != SP_RESULT_OK) {
119 EMSG("Failed to send direct response %d", result);
120 result = sp_msg_wait(&req_msg);
121 if (result != SP_RESULT_OK) {
122 EMSG("Failed to send message wait %d", result);
123 goto fatal_error;
124 }
125 }
Julian Hall4061ed62020-11-23 18:24:06 +0100126 }
127
128fatal_error:
129 /* SP is not viable */
130 EMSG("Crypto SP error");
131 while (1) {}
132}
133
134void sp_interrupt_handler(uint32_t interrupt_id)
135{
136 (void)interrupt_id;
137}
138
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200139static bool sp_init(uint16_t *own_id)
Julian Hall4061ed62020-11-23 18:24:06 +0100140{
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200141 sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
Julian Hall4061ed62020-11-23 18:24:06 +0100142 static uint8_t tx_buffer[4096] __aligned(4096);
143 static uint8_t rx_buffer[4096] __aligned(4096);
144
145 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200146 if (sp_res != SP_RESULT_OK) {
147 EMSG("Failed to map RXTX buffers: %d", sp_res);
148 return false;
Julian Hall4061ed62020-11-23 18:24:06 +0100149 }
150
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200151 sp_res = sp_discovery_own_id_get(own_id);
152 if (sp_res != SP_RESULT_OK) {
153 EMSG("Failed to query own ID: %d", sp_res);
154 return false;
155 }
156
157 return true;
Julian Hall4061ed62020-11-23 18:24:06 +0100158}