blob: 2e6018ec35cab7c5cbf32ef34e6d93aee10ae7d9 [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 Kis6ef4d0d2022-07-05 16:43:17 +02006#include "rpc/ffarpc/endpoint/ffarpc_call_ep.h"
7#include "service/secure_storage/factory/storage_factory.h"
8#include "service/crypto/factory/crypto_provider_factory.h"
9#include "service/crypto/backend/mbedcrypto/mbedcrypto_backend.h"
10#include "protocols/rpc/common/packed-c/status.h"
11#include "config/ramstore/config_ramstore.h"
12#include "config/loader/sp/sp_config_loader.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"
julhal013a4207d2021-03-08 13:32:08 +000018
Imre Kis6ef4d0d2022-07-05 16:43:17 +020019static bool sp_init(uint16_t *own_sp_id);
Julian Hall4061ed62020-11-23 18:24:06 +010020
21void __noreturn sp_main(struct ffa_init_info *init_info)
22{
Imre Kis6ef4d0d2022-07-05 16:43:17 +020023 struct crypto_provider *crypto_provider = NULL;
24 struct ffa_call_ep ffarpc_call_ep = { 0 };
25 struct rpc_interface *crypto_iface = NULL;
Imre Kis76e8a3c2021-04-16 16:54:17 +020026 struct sp_msg req_msg = { 0 };
27 struct sp_msg resp_msg = { 0 };
Imre Kis6ef4d0d2022-07-05 16:43:17 +020028 struct storage_backend *storage_backend = NULL;
Imre Kisf6562652022-07-04 15:33:13 +020029 uint16_t own_id = 0;
Imre Kis6ef4d0d2022-07-05 16:43:17 +020030 psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
31 sp_result result = SP_RESULT_INTERNAL_ERROR;
Julian Hall4061ed62020-11-23 18:24:06 +010032
julhal013a4207d2021-03-08 13:32:08 +000033 /* Boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020034 if (!sp_init(&own_id)) {
35 EMSG("Failed to init SP");
36 goto fatal_error;
37 }
Julian Hall4061ed62020-11-23 18:24:06 +010038
julhal012c18fbf2021-02-01 08:29:28 +000039 config_ramstore_init();
Imre Kis6ef4d0d2022-07-05 16:43:17 +020040
41 if (!sp_config_load(init_info)) {
42 EMSG("Failed to load SP config");
43 goto fatal_error;
44 }
julhal012c18fbf2021-02-01 08:29:28 +000045
julhal013a4207d2021-03-08 13:32:08 +000046 /* Create a storage backend for persistent key storage - prefer ITS */
47 storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
Imre Kis6ef4d0d2022-07-05 16:43:17 +020048 if (!storage_backend) {
49 EMSG("Failed to create storage factory");
50 goto fatal_error;
51 }
Julian Hall4061ed62020-11-23 18:24:06 +010052
53 /* Initialize the crypto service */
Imre Kis6ef4d0d2022-07-05 16:43:17 +020054 psa_status = mbedcrypto_backend_init(storage_backend, 0);
55 if (psa_status != PSA_SUCCESS) {
56 EMSG("Failed to init Mbed TLS backend: %d", psa_status);
57 goto fatal_error;
58 }
julhal01734dbad2020-12-21 10:27:41 +000059
Imre Kis6ef4d0d2022-07-05 16:43:17 +020060 crypto_provider = crypto_provider_factory_create();
61 if (!crypto_provider) {
62 EMSG("Failed to create crypto provider factory");
63 goto fatal_error;
64 }
Julian Hall9061e6c2021-06-29 14:24:20 +010065
Imre Kis6ef4d0d2022-07-05 16:43:17 +020066 crypto_iface = service_provider_get_rpc_interface(&crypto_provider->base_provider);
67 if (!crypto_iface) {
68 EMSG("Failed to create service provider RPC interface");
69 goto fatal_error;
Julian Hall9061e6c2021-06-29 14:24:20 +010070 }
julhal01734dbad2020-12-21 10:27:41 +000071
Imre Kisd92645c2022-06-28 17:26:53 +020072 ffa_call_ep_init(&ffarpc_call_ep, crypto_iface, own_id);
Julian Hall4061ed62020-11-23 18:24:06 +010073
julhal011260f102021-02-15 17:34:08 +000074 /* End of boot phase */
Imre Kis6ef4d0d2022-07-05 16:43:17 +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 }
Julian Hall4061ed62020-11-23 18:24:06 +010080
81 while (1) {
Imre Kis76e8a3c2021-04-16 16:54:17 +020082 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
Julian Hall4061ed62020-11-23 18:24:06 +010083
Imre Kis6ef4d0d2022-07-05 16:43:17 +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 }
Julian Hall4061ed62020-11-23 18:24:06 +010093 }
94
95fatal_error:
96 /* SP is not viable */
97 EMSG("Crypto SP error");
98 while (1) {}
99}
100
101void sp_interrupt_handler(uint32_t interrupt_id)
102{
103 (void)interrupt_id;
104}
105
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200106static bool sp_init(uint16_t *own_id)
Julian Hall4061ed62020-11-23 18:24:06 +0100107{
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200108 sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
Julian Hall4061ed62020-11-23 18:24:06 +0100109 static uint8_t tx_buffer[4096] __aligned(4096);
110 static uint8_t rx_buffer[4096] __aligned(4096);
111
112 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200113 if (sp_res != SP_RESULT_OK) {
114 EMSG("Failed to map RXTX buffers: %d", sp_res);
115 return false;
Julian Hall4061ed62020-11-23 18:24:06 +0100116 }
117
Imre Kis6ef4d0d2022-07-05 16:43:17 +0200118 sp_res = sp_discovery_own_id_get(own_id);
119 if (sp_res != SP_RESULT_OK) {
120 EMSG("Failed to query own ID: %d", sp_res);
121 return false;
122 }
123
124 return true;
Julian Hall4061ed62020-11-23 18:24:06 +0100125}