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 | |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame] | 6 | #include <config/ramstore/config_ramstore.h> |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 7 | #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> |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 12 | #include <service/crypto/provider/serializer/protobuf/pb_crypto_provider_serializer.h> |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 13 | #include <service/crypto/provider/serializer/packed-c/packedc_crypto_provider_serializer.h> |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 14 | #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 Dobszay | 1b631eb | 2021-01-15 11:09:36 +0100 | [diff] [blame] | 22 | { 0xdc, 0x1e, 0xef, 0x48, 0xb1, 0x7a, 0x4c, 0xcf, \ |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 23 | 0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, } |
| 24 | |
| 25 | uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */ |
| 26 | static const uint8_t storage_uuid[] = SP_STORAGE_UUID_BYTES; |
| 27 | |
| 28 | |
| 29 | static int sp_init(uint16_t *own_sp_id); |
| 30 | |
| 31 | void __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; |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 35 | struct rpc_interface *crypto_iface; |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 36 | 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 | |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame] | 47 | /* Read config data */ |
| 48 | config_ramstore_init(); |
| 49 | // ~ read here |
| 50 | |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 51 | /* Establish RPC session with secure storage SP */ |
| 52 | storage_caller = ffarpc_caller_init(&ffarpc_caller); |
| 53 | |
julhal01 | ffa98d8 | 2021-01-20 13:51:58 +0000 | [diff] [blame] | 54 | if (!ffarpc_caller_discover(storage_uuid, storage_sp_ids, |
| 55 | sizeof(storage_sp_ids)/sizeof(uint16_t)) || |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 56 | ffarpc_caller_open(&ffarpc_caller, storage_sp_ids[0], 0)) { |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 57 | /* |
| 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 */ |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame] | 68 | crypto_iface = mbed_crypto_provider_init(&crypto_provider, storage_caller, 0); |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 69 | |
| 70 | mbed_crypto_provider_register_serializer(&crypto_provider, |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 71 | TS_RPC_ENCODING_PROTOBUF, pb_crypto_provider_serializer_instance()); |
| 72 | |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 73 | mbed_crypto_provider_register_serializer(&crypto_provider, |
| 74 | TS_RPC_ENCODING_PACKED_C, packedc_crypto_provider_serializer_instance()); |
| 75 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 76 | ffa_call_ep_init(&ffarpc_call_ep, crypto_iface); |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 77 | |
| 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 | |
| 95 | fatal_error: |
| 96 | /* SP is not viable */ |
| 97 | EMSG("Crypto SP error"); |
| 98 | while (1) {} |
| 99 | } |
| 100 | |
| 101 | void sp_interrupt_handler(uint32_t interrupt_id) |
| 102 | { |
| 103 | (void)interrupt_id; |
| 104 | } |
| 105 | |
| 106 | static 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 | } |