blob: 136c4a38814e506e3c735501b923c8c4c7eb0212 [file] [log] [blame]
Julian Hall4061ed62020-11-23 18:24:06 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
4 */
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>
11#include <protocols/rpc/common/packed-c/status.h>
12#include <ffa_api.h>
13#include <sp_api.h>
14#include <sp_rxtx.h>
15#include <trace.h>
16
17
18#define SP_STORAGE_UUID_BYTES \
19 { 0x48, 0xef, 0x1e, 0xdc, 0x7a, 0xb1, 0xcf, 0x4c, \
20 0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, }
21
22uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
23static const uint8_t storage_uuid[] = SP_STORAGE_UUID_BYTES;
24
25
26static int sp_init(uint16_t *own_sp_id);
27
28void __noreturn sp_main(struct ffa_init_info *init_info)
29{
30 struct mbed_crypto_provider crypto_provider;
31 struct ffa_call_ep ffarpc_call_ep;
32 struct call_ep *crypto_ep;
33 struct ffarpc_caller ffarpc_caller;
34 struct dummy_caller dummy_caller;
35 struct rpc_caller *storage_caller;
36 struct ffa_direct_msg req_msg;
37 uint16_t storage_sp_ids[1];
38
39 /* Boot */
40 (void) init_info;
41
42 if (sp_init(&own_id) != 0) goto fatal_error;
43
44 /* Establish RPC session with secure storage SP */
45 storage_caller = ffarpc_caller_init(&ffarpc_caller);
46
47 if (!ffarpc_caller_discover(storage_uuid, storage_sp_ids, sizeof(storage_sp_ids)/sizeof(uint16_t)) ||
48 ffarpc_caller_open(&ffarpc_caller, storage_sp_ids[0])) {
49 /*
50 * Failed to establish session. To allow the crypto service
51 * to still be initialized, albeit with no persistent storage,
52 * initialise a dummy_caller that will safely
53 * handle rpc requests but will report an error.
54 */
55 storage_caller = dummy_caller_init(&dummy_caller,
56 TS_RPC_CALL_ACCEPTED, PSA_ERROR_STORAGE_FAILURE);
57 }
58
59 /* Initialize the crypto service */
60 crypto_ep = mbed_crypto_provider_init(&crypto_provider, storage_caller);
61 ffa_call_ep_init(&ffarpc_call_ep, crypto_ep);
62
63 /* End of boot phase */
64 ffa_msg_wait(&req_msg);
65
66 while (1) {
67 if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
68
69 struct ffa_direct_msg resp_msg;
70
71 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
72
73 ffa_msg_send_direct_resp(req_msg.destination_id,
74 req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
75 resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
76 &req_msg);
77 }
78 }
79
80fatal_error:
81 /* SP is not viable */
82 EMSG("Crypto SP error");
83 while (1) {}
84}
85
86void sp_interrupt_handler(uint32_t interrupt_id)
87{
88 (void)interrupt_id;
89}
90
91static int sp_init(uint16_t *own_sp_id)
92{
93 int status = -1;
94 ffa_result ffa_res;
95 sp_result sp_res;
96 static uint8_t tx_buffer[4096] __aligned(4096);
97 static uint8_t rx_buffer[4096] __aligned(4096);
98
99 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
100 if (sp_res == SP_RESULT_OK) {
101 ffa_res = ffa_id_get(own_sp_id);
102 if (ffa_res == FFA_OK) {
103 status = 0;
104 }
105 }
106
107 return status;
108}