blob: ea60d1c7073d1a02cd4ef38347a85c9cbc7066f5 [file] [log] [blame]
Julian Hall4061ed62020-11-23 18:24:06 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
Balint Dobszay1b631eb2021-01-15 11:09:36 +01003 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
Julian Hall4061ed62020-11-23 18:24:06 +01004 */
5
6#include <rpc/ffarpc/caller/sp/ffarpc_caller.h>
7#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
8#include <rpc/dummy/dummy_caller.h>
9#include <service/secure_storage/client/psa/its/its_client.h>
10#include <service/crypto/provider/mbedcrypto/crypto_provider.h>
julhal01c3f4e9a2020-12-15 13:39:01 +000011#include <service/crypto/provider/serializer/protobuf/pb_crypto_provider_serializer.h>
julhal01734dbad2020-12-21 10:27:41 +000012#include <service/crypto/provider/serializer/packed-c/packedc_crypto_provider_serializer.h>
Julian Hall4061ed62020-11-23 18:24:06 +010013#include <protocols/rpc/common/packed-c/status.h>
14#include <ffa_api.h>
15#include <sp_api.h>
16#include <sp_rxtx.h>
17#include <trace.h>
18
19
20#define SP_STORAGE_UUID_BYTES \
Balint Dobszay1b631eb2021-01-15 11:09:36 +010021 { 0xdc, 0x1e, 0xef, 0x48, 0xb1, 0x7a, 0x4c, 0xcf, \
Julian Hall4061ed62020-11-23 18:24:06 +010022 0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, }
23
24uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
25static const uint8_t storage_uuid[] = SP_STORAGE_UUID_BYTES;
26
27
28static int sp_init(uint16_t *own_sp_id);
29
30void __noreturn sp_main(struct ffa_init_info *init_info)
31{
32 struct mbed_crypto_provider crypto_provider;
33 struct ffa_call_ep ffarpc_call_ep;
julhal01c3f4e9a2020-12-15 13:39:01 +000034 struct rpc_interface *crypto_iface;
Julian Hall4061ed62020-11-23 18:24:06 +010035 struct ffarpc_caller ffarpc_caller;
36 struct dummy_caller dummy_caller;
37 struct rpc_caller *storage_caller;
38 struct ffa_direct_msg req_msg;
39 uint16_t storage_sp_ids[1];
40
41 /* Boot */
42 (void) init_info;
43
44 if (sp_init(&own_id) != 0) goto fatal_error;
45
46 /* Establish RPC session with secure storage SP */
47 storage_caller = ffarpc_caller_init(&ffarpc_caller);
48
49 if (!ffarpc_caller_discover(storage_uuid, storage_sp_ids, sizeof(storage_sp_ids)/sizeof(uint16_t)) ||
julhal01c3f4e9a2020-12-15 13:39:01 +000050 ffarpc_caller_open(&ffarpc_caller, storage_sp_ids[0], 0)) {
Julian Hall4061ed62020-11-23 18:24:06 +010051 /*
52 * Failed to establish session. To allow the crypto service
53 * to still be initialized, albeit with no persistent storage,
54 * initialise a dummy_caller that will safely
55 * handle rpc requests but will report an error.
56 */
57 storage_caller = dummy_caller_init(&dummy_caller,
58 TS_RPC_CALL_ACCEPTED, PSA_ERROR_STORAGE_FAILURE);
59 }
60
61 /* Initialize the crypto service */
julhal01c3f4e9a2020-12-15 13:39:01 +000062 crypto_iface = mbed_crypto_provider_init(&crypto_provider, storage_caller);
julhal01734dbad2020-12-21 10:27:41 +000063
64 mbed_crypto_provider_register_serializer(&crypto_provider,
julhal01c3f4e9a2020-12-15 13:39:01 +000065 TS_RPC_ENCODING_PROTOBUF, pb_crypto_provider_serializer_instance());
66
julhal01734dbad2020-12-21 10:27:41 +000067 mbed_crypto_provider_register_serializer(&crypto_provider,
68 TS_RPC_ENCODING_PACKED_C, packedc_crypto_provider_serializer_instance());
69
julhal01c3f4e9a2020-12-15 13:39:01 +000070 ffa_call_ep_init(&ffarpc_call_ep, crypto_iface);
Julian Hall4061ed62020-11-23 18:24:06 +010071
72 /* End of boot phase */
73 ffa_msg_wait(&req_msg);
74
75 while (1) {
76 if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
77
78 struct ffa_direct_msg resp_msg;
79
80 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
81
82 ffa_msg_send_direct_resp(req_msg.destination_id,
83 req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
84 resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
85 &req_msg);
86 }
87 }
88
89fatal_error:
90 /* SP is not viable */
91 EMSG("Crypto SP error");
92 while (1) {}
93}
94
95void sp_interrupt_handler(uint32_t interrupt_id)
96{
97 (void)interrupt_id;
98}
99
100static int sp_init(uint16_t *own_sp_id)
101{
102 int status = -1;
103 ffa_result ffa_res;
104 sp_result sp_res;
105 static uint8_t tx_buffer[4096] __aligned(4096);
106 static uint8_t rx_buffer[4096] __aligned(4096);
107
108 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
109 if (sp_res == SP_RESULT_OK) {
110 ffa_res = ffa_id_get(own_sp_id);
111 if (ffa_res == FFA_OK) {
112 status = 0;
113 }
114 }
115
116 return status;
117}