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