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 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 42 | uint32_t tfm_rpc_psa_version(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 43 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 44 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 45 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 46 | return tfm_spm_client_psa_version(params->sid); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 47 | } |
| 48 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 49 | psa_status_t tfm_rpc_psa_connect(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 50 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 51 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 52 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 53 | return tfm_spm_client_psa_connect(params->sid, params->version); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 56 | psa_status_t tfm_rpc_psa_call(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 57 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 58 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 59 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 60 | return tfm_spm_client_psa_call(params->handle, params->type, |
| 61 | params->in_vec, params->in_len, |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 62 | params->out_vec, params->out_len, |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 63 | TFM_PARTITION_UNPRIVILEGED_MODE); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 66 | void tfm_rpc_psa_close(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 67 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 68 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 69 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame^] | 70 | tfm_spm_client_psa_close(params->handle); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int32_t tfm_rpc_register_ops(const struct tfm_rpc_ops_t *ops_ptr) |
| 74 | { |
| 75 | if (!ops_ptr) { |
| 76 | return TFM_RPC_INVAL_PARAM; |
| 77 | } |
| 78 | |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 79 | 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] | 80 | return TFM_RPC_INVAL_PARAM; |
| 81 | } |
| 82 | |
| 83 | /* Currently, one and only one mailbox implementation is supported. */ |
| 84 | if ((rpc_ops.handle_req != default_handle_req) || |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 85 | (rpc_ops.reply != default_mailbox_reply) || |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 86 | (rpc_ops.get_caller_data != default_get_caller_data)) { |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 87 | return TFM_RPC_CONFLICT_CALLBACK; |
| 88 | } |
| 89 | |
| 90 | rpc_ops.handle_req = ops_ptr->handle_req; |
| 91 | rpc_ops.reply = ops_ptr->reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 92 | rpc_ops.get_caller_data = ops_ptr->get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 93 | |
| 94 | return TFM_RPC_SUCCESS; |
| 95 | } |
| 96 | |
| 97 | void tfm_rpc_unregister_ops(void) |
| 98 | { |
| 99 | rpc_ops.handle_req = default_handle_req; |
| 100 | rpc_ops.reply = default_mailbox_reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 101 | rpc_ops.get_caller_data = default_get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void tfm_rpc_client_call_handler(void) |
| 105 | { |
| 106 | rpc_ops.handle_req(); |
| 107 | } |
| 108 | |
| 109 | void tfm_rpc_client_call_reply(const void *owner, int32_t ret) |
| 110 | { |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 111 | const struct tfm_msg_body_t *msg = (const struct tfm_msg_body_t *)owner; |
| 112 | |
| 113 | rpc_ops.reply(msg->caller_data, ret); |
| 114 | } |
| 115 | |
| 116 | void tfm_rpc_set_caller_data(struct tfm_msg_body_t *msg, int32_t client_id) |
| 117 | { |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 118 | msg->caller_data = rpc_ops.get_caller_data(client_id); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 119 | } |