blob: 6b376b792029271c7ace573b06d24cf865c383af [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
julhal012c18fbf2021-02-01 08:29:28 +00006#include <config/ramstore/config_ramstore.h>
Julian Hall4061ed62020-11-23 18:24:06 +01007#include <rpc/ffarpc/caller/sp/ffarpc_caller.h>
8#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
9#include <rpc/dummy/dummy_caller.h>
10#include <service/secure_storage/client/psa/its/its_client.h>
11#include <service/crypto/provider/mbedcrypto/crypto_provider.h>
julhal01c3f4e9a2020-12-15 13:39:01 +000012#include <service/crypto/provider/serializer/protobuf/pb_crypto_provider_serializer.h>
julhal01734dbad2020-12-21 10:27:41 +000013#include <service/crypto/provider/serializer/packed-c/packedc_crypto_provider_serializer.h>
Julian Hall4061ed62020-11-23 18:24:06 +010014#include <protocols/rpc/common/packed-c/status.h>
15#include <ffa_api.h>
16#include <sp_api.h>
17#include <sp_rxtx.h>
18#include <trace.h>
19
20
21#define SP_STORAGE_UUID_BYTES \
Balint Dobszay1b631eb2021-01-15 11:09:36 +010022 { 0xdc, 0x1e, 0xef, 0x48, 0xb1, 0x7a, 0x4c, 0xcf, \
Julian Hall4061ed62020-11-23 18:24:06 +010023 0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, }
24
25uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
26static const uint8_t storage_uuid[] = SP_STORAGE_UUID_BYTES;
27
28
29static int sp_init(uint16_t *own_sp_id);
30
31void __noreturn sp_main(struct ffa_init_info *init_info)
32{
33 struct mbed_crypto_provider crypto_provider;
34 struct ffa_call_ep ffarpc_call_ep;
julhal01c3f4e9a2020-12-15 13:39:01 +000035 struct rpc_interface *crypto_iface;
Julian Hall4061ed62020-11-23 18:24:06 +010036 struct ffarpc_caller ffarpc_caller;
37 struct dummy_caller dummy_caller;
38 struct rpc_caller *storage_caller;
39 struct ffa_direct_msg req_msg;
40 uint16_t storage_sp_ids[1];
41
42 /* Boot */
43 (void) init_info;
44
45 if (sp_init(&own_id) != 0) goto fatal_error;
46
julhal012c18fbf2021-02-01 08:29:28 +000047 /* Read config data */
48 config_ramstore_init();
49 // ~ read here
50
Julian Hall4061ed62020-11-23 18:24:06 +010051 /* Establish RPC session with secure storage SP */
52 storage_caller = ffarpc_caller_init(&ffarpc_caller);
53
julhal01ffa98d82021-01-20 13:51:58 +000054 if (!ffarpc_caller_discover(storage_uuid, storage_sp_ids,
55 sizeof(storage_sp_ids)/sizeof(uint16_t)) ||
julhal01c3f4e9a2020-12-15 13:39:01 +000056 ffarpc_caller_open(&ffarpc_caller, storage_sp_ids[0], 0)) {
Julian Hall4061ed62020-11-23 18:24:06 +010057 /*
58 * Failed to establish session. To allow the crypto service
59 * to still be initialized, albeit with no persistent storage,
60 * initialise a dummy_caller that will safely
61 * handle rpc requests but will report an error.
62 */
63 storage_caller = dummy_caller_init(&dummy_caller,
64 TS_RPC_CALL_ACCEPTED, PSA_ERROR_STORAGE_FAILURE);
65 }
66
67 /* Initialize the crypto service */
julhal012c18fbf2021-02-01 08:29:28 +000068 crypto_iface = mbed_crypto_provider_init(&crypto_provider, storage_caller, 0);
julhal01734dbad2020-12-21 10:27:41 +000069
70 mbed_crypto_provider_register_serializer(&crypto_provider,
julhal01c3f4e9a2020-12-15 13:39:01 +000071 TS_RPC_ENCODING_PROTOBUF, pb_crypto_provider_serializer_instance());
72
julhal01734dbad2020-12-21 10:27:41 +000073 mbed_crypto_provider_register_serializer(&crypto_provider,
74 TS_RPC_ENCODING_PACKED_C, packedc_crypto_provider_serializer_instance());
75
julhal01c3f4e9a2020-12-15 13:39:01 +000076 ffa_call_ep_init(&ffarpc_call_ep, crypto_iface);
Julian Hall4061ed62020-11-23 18:24:06 +010077
78 /* End of boot phase */
79 ffa_msg_wait(&req_msg);
80
81 while (1) {
82 if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
83
84 struct ffa_direct_msg resp_msg;
85
86 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
87
88 ffa_msg_send_direct_resp(req_msg.destination_id,
89 req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
90 resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
91 &req_msg);
92 }
93 }
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
106static int sp_init(uint16_t *own_sp_id)
107{
108 int status = -1;
109 ffa_result ffa_res;
110 sp_result sp_res;
111 static uint8_t tx_buffer[4096] __aligned(4096);
112 static uint8_t rx_buffer[4096] __aligned(4096);
113
114 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
115 if (sp_res == SP_RESULT_OK) {
116 ffa_res = ffa_id_get(own_sp_id);
117 if (ffa_res == FFA_OK) {
118 status = 0;
119 }
120 }
121
122 return status;
123}