blob: b198d1989993184e924f9f9878e234ec77ab19ec [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
julhal013a4207d2021-03-08 13:32:08 +00006
Julian Hall4061ed62020-11-23 18:24:06 +01007#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
julhal013a4207d2021-03-08 13:32:08 +00008#include <service/secure_storage/factory/storage_factory.h>
Julian Hall7bfb18e2021-07-13 15:48:13 +01009#include <service/crypto/factory/crypto_provider_factory.h>
Julian Hall9061e6c2021-06-29 14:24:20 +010010#include <service/crypto/backend/mbedcrypto/mbedcrypto_backend.h>
Julian Hall4061ed62020-11-23 18:24:06 +010011#include <protocols/rpc/common/packed-c/status.h>
julhal0137e1aea2021-02-09 15:22:20 +000012#include <config/ramstore/config_ramstore.h>
13#include <config/loader/sp/sp_config_loader.h>
Julian Hall4061ed62020-11-23 18:24:06 +010014#include <ffa_api.h>
15#include <sp_api.h>
Imre Kis76e8a3c2021-04-16 16:54:17 +020016#include <sp_messaging.h>
Julian Hall4061ed62020-11-23 18:24:06 +010017#include <sp_rxtx.h>
18#include <trace.h>
19
20
Julian Hall4061ed62020-11-23 18:24:06 +010021uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
Julian Hall4061ed62020-11-23 18:24:06 +010022
23
24static int sp_init(uint16_t *own_sp_id);
25
26void __noreturn sp_main(struct ffa_init_info *init_info)
27{
Julian Hall7bfb18e2021-07-13 15:48:13 +010028 struct crypto_provider *crypto_provider;
Julian Hall4061ed62020-11-23 18:24:06 +010029 struct ffa_call_ep ffarpc_call_ep;
julhal01c3f4e9a2020-12-15 13:39:01 +000030 struct rpc_interface *crypto_iface;
Imre Kis76e8a3c2021-04-16 16:54:17 +020031 struct sp_msg req_msg = { 0 };
32 struct sp_msg resp_msg = { 0 };
julhal013a4207d2021-03-08 13:32:08 +000033 struct storage_backend *storage_backend;
Julian Hall4061ed62020-11-23 18:24:06 +010034
julhal013a4207d2021-03-08 13:32:08 +000035 /* Boot phase */
Julian Hall4061ed62020-11-23 18:24:06 +010036 if (sp_init(&own_id) != 0) goto fatal_error;
37
julhal012c18fbf2021-02-01 08:29:28 +000038 config_ramstore_init();
julhal0137e1aea2021-02-09 15:22:20 +000039 sp_config_load(init_info);
julhal012c18fbf2021-02-01 08:29:28 +000040
julhal013a4207d2021-03-08 13:32:08 +000041 /* Create a storage backend for persistent key storage - prefer ITS */
42 storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
43 if (!storage_backend) goto fatal_error;
Julian Hall4061ed62020-11-23 18:24:06 +010044
45 /* Initialize the crypto service */
Julian Hall9061e6c2021-06-29 14:24:20 +010046 crypto_iface = NULL;
julhal01734dbad2020-12-21 10:27:41 +000047
Julian Hall7bfb18e2021-07-13 15:48:13 +010048 if (mbedcrypto_backend_init(storage_backend, 0) == PSA_SUCCESS) {
Julian Hall9061e6c2021-06-29 14:24:20 +010049
Julian Hall7bfb18e2021-07-13 15:48:13 +010050 crypto_provider = crypto_provider_factory_create();
51 crypto_iface = service_provider_get_rpc_interface(&crypto_provider->base_provider);
Julian Hall9061e6c2021-06-29 14:24:20 +010052 }
julhal01734dbad2020-12-21 10:27:41 +000053
Imre Kisd92645c2022-06-28 17:26:53 +020054 ffa_call_ep_init(&ffarpc_call_ep, crypto_iface, own_id);
Julian Hall4061ed62020-11-23 18:24:06 +010055
julhal011260f102021-02-15 17:34:08 +000056 /* End of boot phase */
Imre Kis76e8a3c2021-04-16 16:54:17 +020057 sp_msg_wait(&req_msg);
Julian Hall4061ed62020-11-23 18:24:06 +010058
59 while (1) {
Imre Kis76e8a3c2021-04-16 16:54:17 +020060 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
Julian Hall4061ed62020-11-23 18:24:06 +010061
Imre Kis76e8a3c2021-04-16 16:54:17 +020062 sp_msg_send_direct_resp(&resp_msg, &req_msg);
Julian Hall4061ed62020-11-23 18:24:06 +010063 }
64
65fatal_error:
66 /* SP is not viable */
67 EMSG("Crypto SP error");
68 while (1) {}
69}
70
71void sp_interrupt_handler(uint32_t interrupt_id)
72{
73 (void)interrupt_id;
74}
75
76static int sp_init(uint16_t *own_sp_id)
77{
78 int status = -1;
79 ffa_result ffa_res;
80 sp_result sp_res;
81 static uint8_t tx_buffer[4096] __aligned(4096);
82 static uint8_t rx_buffer[4096] __aligned(4096);
83
84 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
85 if (sp_res == SP_RESULT_OK) {
86 ffa_res = ffa_id_get(own_sp_id);
87 if (ffa_res == FFA_OK) {
88 status = 0;
89 }
90 }
91
92 return status;
93}