blob: b9c1fb236769e440bd735596dcd26750f454adec [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
julhal013a4207d2021-03-08 13:32:08 +00006
Julian Hall4061ed62020-11-23 18:24:06 +01007#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
julhal013a4207d2021-03-08 13:32:08 +00008#include <service/secure_storage/factory/storage_factory.h>
Julian Hall4061ed62020-11-23 18:24:06 +01009#include <service/crypto/provider/mbedcrypto/crypto_provider.h>
julhal01c3f4e9a2020-12-15 13:39:01 +000010#include <service/crypto/provider/serializer/protobuf/pb_crypto_provider_serializer.h>
julhal01734dbad2020-12-21 10:27:41 +000011#include <service/crypto/provider/serializer/packed-c/packedc_crypto_provider_serializer.h>
Julian Hall4061ed62020-11-23 18:24:06 +010012#include <protocols/rpc/common/packed-c/status.h>
julhal0137e1aea2021-02-09 15:22:20 +000013#include <config/ramstore/config_ramstore.h>
14#include <config/loader/sp/sp_config_loader.h>
Julian Hall4061ed62020-11-23 18:24:06 +010015#include <ffa_api.h>
16#include <sp_api.h>
17#include <sp_rxtx.h>
18#include <trace.h>
19
20
Julian Hall4061ed62020-11-23 18:24:06 +010021uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
Julian Hall4061ed62020-11-23 18:24:06 +010022
23
24static int sp_init(uint16_t *own_sp_id);
25
26void __noreturn sp_main(struct ffa_init_info *init_info)
27{
28 struct mbed_crypto_provider crypto_provider;
29 struct ffa_call_ep ffarpc_call_ep;
julhal01c3f4e9a2020-12-15 13:39:01 +000030 struct rpc_interface *crypto_iface;
Julian Hall4061ed62020-11-23 18:24:06 +010031 struct ffa_direct_msg req_msg;
julhal013a4207d2021-03-08 13:32:08 +000032 struct storage_backend *storage_backend;
Julian Hall4061ed62020-11-23 18:24:06 +010033
julhal013a4207d2021-03-08 13:32:08 +000034 /* Boot phase */
Julian Hall4061ed62020-11-23 18:24:06 +010035 if (sp_init(&own_id) != 0) goto fatal_error;
36
julhal012c18fbf2021-02-01 08:29:28 +000037 config_ramstore_init();
julhal0137e1aea2021-02-09 15:22:20 +000038 sp_config_load(init_info);
julhal012c18fbf2021-02-01 08:29:28 +000039
julhal013a4207d2021-03-08 13:32:08 +000040 /* Create a storage backend for persistent key storage - prefer ITS */
41 storage_backend = storage_factory_create(storage_factory_security_class_INTERNAL_TRUSTED);
42 if (!storage_backend) goto fatal_error;
Julian Hall4061ed62020-11-23 18:24:06 +010043
44 /* Initialize the crypto service */
julhal013a4207d2021-03-08 13:32:08 +000045 crypto_iface = mbed_crypto_provider_init(&crypto_provider, storage_backend, 0);
julhal01734dbad2020-12-21 10:27:41 +000046
47 mbed_crypto_provider_register_serializer(&crypto_provider,
julhal01c3f4e9a2020-12-15 13:39:01 +000048 TS_RPC_ENCODING_PROTOBUF, pb_crypto_provider_serializer_instance());
49
julhal01734dbad2020-12-21 10:27:41 +000050 mbed_crypto_provider_register_serializer(&crypto_provider,
51 TS_RPC_ENCODING_PACKED_C, packedc_crypto_provider_serializer_instance());
52
julhal01c3f4e9a2020-12-15 13:39:01 +000053 ffa_call_ep_init(&ffarpc_call_ep, crypto_iface);
Julian Hall4061ed62020-11-23 18:24:06 +010054
julhal011260f102021-02-15 17:34:08 +000055 /* End of boot phase */
Julian Hall4061ed62020-11-23 18:24:06 +010056 ffa_msg_wait(&req_msg);
57
58 while (1) {
59 if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
60
61 struct ffa_direct_msg resp_msg;
62
63 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
64
65 ffa_msg_send_direct_resp(req_msg.destination_id,
66 req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
67 resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
68 &req_msg);
69 }
70 }
71
72fatal_error:
73 /* SP is not viable */
74 EMSG("Crypto SP error");
75 while (1) {}
76}
77
78void sp_interrupt_handler(uint32_t interrupt_id)
79{
80 (void)interrupt_id;
81}
82
83static int sp_init(uint16_t *own_sp_id)
84{
85 int status = -1;
86 ffa_result ffa_res;
87 sp_result sp_res;
88 static uint8_t tx_buffer[4096] __aligned(4096);
89 static uint8_t rx_buffer[4096] __aligned(4096);
90
91 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
92 if (sp_res == SP_RESULT_OK) {
93 ffa_res = ffa_id_get(own_sp_id);
94 if (ffa_res == FFA_OK) {
95 status = 0;
96 }
97 }
98
99 return status;
100}