blob: 7c841473fc0a721ba7e773a6a64d308459797c59 [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"
Gabor Ambrus70908d02023-08-15 14:42:38 +020011#include "service/log/factory/log_factory.h"
Imre Kis6ef4d0d2022-07-05 16:43:17 +020012#include "protocols/rpc/common/packed-c/status.h"
13#include "config/ramstore/config_ramstore.h"
14#include "config/loader/sp/sp_config_loader.h"
15#include "sp_api.h"
16#include "sp_discovery.h"
17#include "sp_messaging.h"
18#include "sp_rxtx.h"
19#include "trace.h"
julhal013a4207d2021-03-08 13:32:08 +000020
Imre Kis6ef4d0d2022-07-05 16:43:17 +020021static bool sp_init(uint16_t *own_sp_id);
Julian Hall4061ed62020-11-23 18:24:06 +010022
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020023void __noreturn sp_main(union ffa_boot_info *boot_info)
Julian Hall4061ed62020-11-23 18:24:06 +010024{
Imre Kis6ef4d0d2022-07-05 16:43:17 +020025 struct crypto_provider *crypto_provider = NULL;
Imre Kisbd8011a2023-07-04 18:00:39 +020026 struct crypto_provider *crypto_protobuf_provider = NULL;
27 struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
28 struct rpc_service_interface *crypto_iface = NULL;
29 struct rpc_service_interface *crypto_iface_protobuf = NULL;
Imre Kis76e8a3c2021-04-16 16:54:17 +020030 struct sp_msg req_msg = { 0 };
31 struct sp_msg resp_msg = { 0 };
Imre Kis6ef4d0d2022-07-05 16:43:17 +020032 struct storage_backend *storage_backend = NULL;
Imre Kisf6562652022-07-04 15:33:13 +020033 uint16_t own_id = 0;
Imre Kis6ef4d0d2022-07-05 16:43:17 +020034 psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
35 sp_result result = SP_RESULT_INTERNAL_ERROR;
Imre Kisbd8011a2023-07-04 18:00:39 +020036 rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
Julian Hall4061ed62020-11-23 18:24:06 +010037
julhal013a4207d2021-03-08 13:32:08 +000038 /* Boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020039 if (!sp_init(&own_id)) {
40 EMSG("Failed to init SP");
41 goto fatal_error;
42 }
Julian Hall4061ed62020-11-23 18:24:06 +010043
julhal012c18fbf2021-02-01 08:29:28 +000044 config_ramstore_init();
Imre Kis6ef4d0d2022-07-05 16:43:17 +020045
Balint Dobszay4f9d8e32023-04-13 13:55:08 +020046 if (!sp_config_load(boot_info)) {
Imre Kis6ef4d0d2022-07-05 16:43:17 +020047 EMSG("Failed to load SP config");
48 goto fatal_error;
49 }
julhal012c18fbf2021-02-01 08:29:28 +000050
julhal013a4207d2021-03-08 13:32:08 +000051 /* Create a storage backend for persistent key storage - prefer ITS */
52 storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
Imre Kis6ef4d0d2022-07-05 16:43:17 +020053 if (!storage_backend) {
54 EMSG("Failed to create storage factory");
55 goto fatal_error;
56 }
Julian Hall4061ed62020-11-23 18:24:06 +010057
58 /* Initialize the crypto service */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020059 psa_status = mbedcrypto_backend_init(storage_backend, 0);
60 if (psa_status != PSA_SUCCESS) {
61 EMSG("Failed to init Mbed TLS backend: %d", psa_status);
62 goto fatal_error;
63 }
julhal01734dbad2020-12-21 10:27:41 +000064
Imre Kis6ef4d0d2022-07-05 16:43:17 +020065 crypto_provider = crypto_provider_factory_create();
66 if (!crypto_provider) {
67 EMSG("Failed to create crypto provider factory");
68 goto fatal_error;
69 }
Julian Hall9061e6c2021-06-29 14:24:20 +010070
Imre Kisbd8011a2023-07-04 18:00:39 +020071 crypto_protobuf_provider = crypto_protobuf_provider_factory_create();
72 if (!crypto_protobuf_provider) {
73 EMSG("Failed to create crypto protobuf provider factory");
74 goto fatal_error;
75 }
76
Imre Kis6ef4d0d2022-07-05 16:43:17 +020077 crypto_iface = service_provider_get_rpc_interface(&crypto_provider->base_provider);
78 if (!crypto_iface) {
79 EMSG("Failed to create service provider RPC interface");
80 goto fatal_error;
Julian Hall9061e6c2021-06-29 14:24:20 +010081 }
julhal01734dbad2020-12-21 10:27:41 +000082
Imre Kisbd8011a2023-07-04 18:00:39 +020083 crypto_iface_protobuf = service_provider_get_rpc_interface(
84 &crypto_protobuf_provider->base_provider);
85 if (!crypto_iface_protobuf) {
86 EMSG("Failed to create service provider RPC interface");
87 goto fatal_error;
88 }
89
90 rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 2, 16);
91 if (rpc_status != RPC_SUCCESS) {
92 EMSG("Failed to initialize RPC endpoint: %d", rpc_status);
93 goto fatal_error;
94 }
95
96 rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface);
97 if (rpc_status != RPC_SUCCESS) {
98 EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
99 goto fatal_error;
100 }
101
102 rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface_protobuf);
103 if (rpc_status != RPC_SUCCESS) {
104 EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
105 goto fatal_error;
106 }
Julian Hall4061ed62020-11-23 18:24:06 +0100107
julhal011260f102021-02-15 17:34:08 +0000108 /* End of boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200109 result = sp_msg_wait(&req_msg);
110 if (result != SP_RESULT_OK) {
111 EMSG("Failed to send message wait %d", result);
112 goto fatal_error;
113 }
Julian Hall4061ed62020-11-23 18:24:06 +0100114
115 while (1) {
Imre Kisbd8011a2023-07-04 18:00:39 +0200116 ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
Julian Hall4061ed62020-11-23 18:24:06 +0100117
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200118 result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
119 if (result != SP_RESULT_OK) {
120 EMSG("Failed to send direct response %d", result);
121 result = sp_msg_wait(&req_msg);
122 if (result != SP_RESULT_OK) {
123 EMSG("Failed to send message wait %d", result);
124 goto fatal_error;
125 }
126 }
Julian Hall4061ed62020-11-23 18:24:06 +0100127 }
128
129fatal_error:
130 /* SP is not viable */
131 EMSG("Crypto SP error");
132 while (1) {}
133}
134
135void sp_interrupt_handler(uint32_t interrupt_id)
136{
137 (void)interrupt_id;
138}
139
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200140static bool sp_init(uint16_t *own_id)
Julian Hall4061ed62020-11-23 18:24:06 +0100141{
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200142 sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
Julian Hall4061ed62020-11-23 18:24:06 +0100143 static uint8_t tx_buffer[4096] __aligned(4096);
144 static uint8_t rx_buffer[4096] __aligned(4096);
145
146 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200147 if (sp_res != SP_RESULT_OK) {
148 EMSG("Failed to map RXTX buffers: %d", sp_res);
149 return false;
Julian Hall4061ed62020-11-23 18:24:06 +0100150 }
151
Gabor Ambrus70908d02023-08-15 14:42:38 +0200152 IMSG("Start discovering logging service");
153 if (log_factory_create()) {
154 IMSG("Logging service discovery successful");
155 } else {
156 EMSG("Logging service discovery failed, falling back to console log");
157 }
158
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200159 sp_res = sp_discovery_own_id_get(own_id);
160 if (sp_res != SP_RESULT_OK) {
161 EMSG("Failed to query own ID: %d", sp_res);
162 return false;
163 }
164
165 return true;
Julian Hall4061ed62020-11-23 18:24:06 +0100166}