Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
| 2 | /* |
Imre Kis | 9757f6b | 2022-07-26 17:19:46 +0200 | [diff] [blame] | 3 | * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved. |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 6 | #include "components/rpc/common/endpoint/rpc_service_interface.h" |
| 7 | #include "components/rpc/ts_rpc/endpoint/sp/ts_rpc_endpoint_sp.h" |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 8 | #include "service/secure_storage/factory/storage_factory.h" |
| 9 | #include "service/crypto/factory/crypto_provider_factory.h" |
| 10 | #include "service/crypto/backend/mbedcrypto/mbedcrypto_backend.h" |
| 11 | #include "protocols/rpc/common/packed-c/status.h" |
| 12 | #include "config/ramstore/config_ramstore.h" |
| 13 | #include "config/loader/sp/sp_config_loader.h" |
| 14 | #include "sp_api.h" |
| 15 | #include "sp_discovery.h" |
| 16 | #include "sp_messaging.h" |
| 17 | #include "sp_rxtx.h" |
| 18 | #include "trace.h" |
julhal01 | 3a4207d | 2021-03-08 13:32:08 +0000 | [diff] [blame] | 19 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 20 | static bool sp_init(uint16_t *own_sp_id); |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 21 | |
Balint Dobszay | 4f9d8e3 | 2023-04-13 13:55:08 +0200 | [diff] [blame] | 22 | void __noreturn sp_main(union ffa_boot_info *boot_info) |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 23 | { |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 24 | struct crypto_provider *crypto_provider = NULL; |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 25 | struct crypto_provider *crypto_protobuf_provider = NULL; |
| 26 | struct ts_rpc_endpoint_sp rpc_endpoint = { 0 }; |
| 27 | struct rpc_service_interface *crypto_iface = NULL; |
| 28 | struct rpc_service_interface *crypto_iface_protobuf = NULL; |
Imre Kis | 76e8a3c | 2021-04-16 16:54:17 +0200 | [diff] [blame] | 29 | struct sp_msg req_msg = { 0 }; |
| 30 | struct sp_msg resp_msg = { 0 }; |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 31 | struct storage_backend *storage_backend = NULL; |
Imre Kis | f656265 | 2022-07-04 15:33:13 +0200 | [diff] [blame] | 32 | uint16_t own_id = 0; |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 33 | psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR; |
| 34 | sp_result result = SP_RESULT_INTERNAL_ERROR; |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 35 | rpc_status_t rpc_status = RPC_ERROR_INTERNAL; |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 36 | |
julhal01 | 3a4207d | 2021-03-08 13:32:08 +0000 | [diff] [blame] | 37 | /* Boot phase */ |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 38 | if (!sp_init(&own_id)) { |
| 39 | EMSG("Failed to init SP"); |
| 40 | goto fatal_error; |
| 41 | } |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 42 | |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame] | 43 | config_ramstore_init(); |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 44 | |
Balint Dobszay | 4f9d8e3 | 2023-04-13 13:55:08 +0200 | [diff] [blame] | 45 | if (!sp_config_load(boot_info)) { |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 46 | EMSG("Failed to load SP config"); |
| 47 | goto fatal_error; |
| 48 | } |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame] | 49 | |
julhal01 | 3a4207d | 2021-03-08 13:32:08 +0000 | [diff] [blame] | 50 | /* Create a storage backend for persistent key storage - prefer ITS */ |
| 51 | storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED); |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 52 | if (!storage_backend) { |
| 53 | EMSG("Failed to create storage factory"); |
| 54 | goto fatal_error; |
| 55 | } |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 56 | |
| 57 | /* Initialize the crypto service */ |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 58 | psa_status = mbedcrypto_backend_init(storage_backend, 0); |
| 59 | if (psa_status != PSA_SUCCESS) { |
| 60 | EMSG("Failed to init Mbed TLS backend: %d", psa_status); |
| 61 | goto fatal_error; |
| 62 | } |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 63 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 64 | crypto_provider = crypto_provider_factory_create(); |
| 65 | if (!crypto_provider) { |
| 66 | EMSG("Failed to create crypto provider factory"); |
| 67 | goto fatal_error; |
| 68 | } |
Julian Hall | 9061e6c | 2021-06-29 14:24:20 +0100 | [diff] [blame] | 69 | |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 70 | crypto_protobuf_provider = crypto_protobuf_provider_factory_create(); |
| 71 | if (!crypto_protobuf_provider) { |
| 72 | EMSG("Failed to create crypto protobuf provider factory"); |
| 73 | goto fatal_error; |
| 74 | } |
| 75 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 76 | crypto_iface = service_provider_get_rpc_interface(&crypto_provider->base_provider); |
| 77 | if (!crypto_iface) { |
| 78 | EMSG("Failed to create service provider RPC interface"); |
| 79 | goto fatal_error; |
Julian Hall | 9061e6c | 2021-06-29 14:24:20 +0100 | [diff] [blame] | 80 | } |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 81 | |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 82 | crypto_iface_protobuf = service_provider_get_rpc_interface( |
| 83 | &crypto_protobuf_provider->base_provider); |
| 84 | if (!crypto_iface_protobuf) { |
| 85 | EMSG("Failed to create service provider RPC interface"); |
| 86 | goto fatal_error; |
| 87 | } |
| 88 | |
| 89 | rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 2, 16); |
| 90 | if (rpc_status != RPC_SUCCESS) { |
| 91 | EMSG("Failed to initialize RPC endpoint: %d", rpc_status); |
| 92 | goto fatal_error; |
| 93 | } |
| 94 | |
| 95 | rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface); |
| 96 | if (rpc_status != RPC_SUCCESS) { |
| 97 | EMSG("Failed to add service to RPC endpoint: %d", rpc_status); |
| 98 | goto fatal_error; |
| 99 | } |
| 100 | |
| 101 | rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, crypto_iface_protobuf); |
| 102 | if (rpc_status != RPC_SUCCESS) { |
| 103 | EMSG("Failed to add service to RPC endpoint: %d", rpc_status); |
| 104 | goto fatal_error; |
| 105 | } |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 106 | |
julhal01 | 1260f10 | 2021-02-15 17:34:08 +0000 | [diff] [blame] | 107 | /* End of boot phase */ |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 108 | result = sp_msg_wait(&req_msg); |
| 109 | if (result != SP_RESULT_OK) { |
| 110 | EMSG("Failed to send message wait %d", result); |
| 111 | goto fatal_error; |
| 112 | } |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 113 | |
| 114 | while (1) { |
Imre Kis | bd8011a | 2023-07-04 18:00:39 +0200 | [diff] [blame] | 115 | ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg); |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 116 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 117 | result = sp_msg_send_direct_resp(&resp_msg, &req_msg); |
| 118 | if (result != SP_RESULT_OK) { |
| 119 | EMSG("Failed to send direct response %d", result); |
| 120 | result = sp_msg_wait(&req_msg); |
| 121 | if (result != SP_RESULT_OK) { |
| 122 | EMSG("Failed to send message wait %d", result); |
| 123 | goto fatal_error; |
| 124 | } |
| 125 | } |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | fatal_error: |
| 129 | /* SP is not viable */ |
| 130 | EMSG("Crypto SP error"); |
| 131 | while (1) {} |
| 132 | } |
| 133 | |
| 134 | void sp_interrupt_handler(uint32_t interrupt_id) |
| 135 | { |
| 136 | (void)interrupt_id; |
| 137 | } |
| 138 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 139 | static bool sp_init(uint16_t *own_id) |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 140 | { |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 141 | sp_result sp_res = SP_RESULT_INTERNAL_ERROR; |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 142 | static uint8_t tx_buffer[4096] __aligned(4096); |
| 143 | static uint8_t rx_buffer[4096] __aligned(4096); |
| 144 | |
| 145 | sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer)); |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 146 | if (sp_res != SP_RESULT_OK) { |
| 147 | EMSG("Failed to map RXTX buffers: %d", sp_res); |
| 148 | return false; |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Imre Kis | 6ef4d0d | 2022-07-05 16:43:17 +0200 | [diff] [blame] | 151 | sp_res = sp_discovery_own_id_get(own_id); |
| 152 | if (sp_res != SP_RESULT_OK) { |
| 153 | EMSG("Failed to query own ID: %d", sp_res); |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | return true; |
Julian Hall | 4061ed6 | 2020-11-23 18:24:06 +0100 | [diff] [blame] | 158 | } |