David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 1 | /* |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 8 | #include "spm_ipc.h" |
Mingyang Sun | 133a792 | 2021-07-08 16:01:26 +0800 | [diff] [blame^] | 9 | #include "ffm/psa_api.h" |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 10 | #include "tfm_rpc.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 11 | #include "utilities.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 12 | #include "load/partition_defs.h" |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 13 | |
| 14 | static void default_handle_req(void) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | static void default_mailbox_reply(const void *owner, int32_t ret) |
| 19 | { |
| 20 | (void)owner; |
| 21 | (void)ret; |
| 22 | } |
| 23 | |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 24 | static const void *default_get_caller_data(int32_t client_id) |
| 25 | { |
| 26 | (void)client_id; |
| 27 | |
| 28 | return NULL; |
| 29 | } |
| 30 | |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 31 | static struct tfm_rpc_ops_t rpc_ops = { |
| 32 | .handle_req = default_handle_req, |
| 33 | .reply = default_mailbox_reply, |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 34 | .get_caller_data = default_get_caller_data, |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | uint32_t tfm_rpc_psa_framework_version(void) |
| 38 | { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 39 | return tfm_spm_client_psa_framework_version(); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | uint32_t tfm_rpc_psa_version(const struct client_call_params_t *params, |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 43 | bool ns_caller) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 44 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 45 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 46 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 47 | return tfm_spm_client_psa_version(params->sid, ns_caller); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | psa_status_t tfm_rpc_psa_connect(const struct client_call_params_t *params, |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 51 | bool ns_caller) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 52 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 53 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 54 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 55 | return tfm_spm_client_psa_connect(params->sid, params->version, ns_caller); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | psa_status_t tfm_rpc_psa_call(const struct client_call_params_t *params, |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 59 | bool ns_caller) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 60 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 61 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 62 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 63 | return tfm_spm_client_psa_call(params->handle, params->type, |
| 64 | params->in_vec, params->in_len, |
| 65 | params->out_vec, params->out_len, ns_caller, |
| 66 | TFM_PARTITION_UNPRIVILEGED_MODE); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void tfm_rpc_psa_close(const struct client_call_params_t *params, |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 70 | bool ns_caller) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 71 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 72 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 73 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 74 | tfm_spm_client_psa_close(params->handle, ns_caller); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int32_t tfm_rpc_register_ops(const struct tfm_rpc_ops_t *ops_ptr) |
| 78 | { |
| 79 | if (!ops_ptr) { |
| 80 | return TFM_RPC_INVAL_PARAM; |
| 81 | } |
| 82 | |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 83 | if (!ops_ptr->handle_req || !ops_ptr->reply || !ops_ptr->get_caller_data) { |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 84 | return TFM_RPC_INVAL_PARAM; |
| 85 | } |
| 86 | |
| 87 | /* Currently, one and only one mailbox implementation is supported. */ |
| 88 | if ((rpc_ops.handle_req != default_handle_req) || |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 89 | (rpc_ops.reply != default_mailbox_reply) || |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 90 | (rpc_ops.get_caller_data != default_get_caller_data)) { |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 91 | return TFM_RPC_CONFLICT_CALLBACK; |
| 92 | } |
| 93 | |
| 94 | rpc_ops.handle_req = ops_ptr->handle_req; |
| 95 | rpc_ops.reply = ops_ptr->reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 96 | rpc_ops.get_caller_data = ops_ptr->get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 97 | |
| 98 | return TFM_RPC_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | void tfm_rpc_unregister_ops(void) |
| 102 | { |
| 103 | rpc_ops.handle_req = default_handle_req; |
| 104 | rpc_ops.reply = default_mailbox_reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 105 | rpc_ops.get_caller_data = default_get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void tfm_rpc_client_call_handler(void) |
| 109 | { |
| 110 | rpc_ops.handle_req(); |
| 111 | } |
| 112 | |
| 113 | void tfm_rpc_client_call_reply(const void *owner, int32_t ret) |
| 114 | { |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 115 | const struct tfm_msg_body_t *msg = (const struct tfm_msg_body_t *)owner; |
| 116 | |
| 117 | rpc_ops.reply(msg->caller_data, ret); |
| 118 | } |
| 119 | |
| 120 | void tfm_rpc_set_caller_data(struct tfm_msg_body_t *msg, int32_t client_id) |
| 121 | { |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 122 | msg->caller_data = rpc_ops.get_caller_data(client_id); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 123 | } |