Rui Miguel Silva | 78a2d01 | 2021-12-03 19:05:18 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef SERVICE_PSA_IPC_H |
| 8 | #define SERVICE_PSA_IPC_H |
| 9 | |
| 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #include <rpc_caller.h> |
| 14 | #include <psa/error.h> |
| 15 | |
| 16 | #ifdef __cplusplus |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | #ifndef IOVEC_LEN |
| 21 | #define IOVEC_LEN(arr) ((uint32_t)(sizeof(arr)/sizeof(arr[0]))) |
| 22 | #endif |
| 23 | |
| 24 | /*********************** PSA Client Macros and Types *************************/ |
| 25 | |
| 26 | typedef int32_t psa_handle_t; |
| 27 | |
| 28 | /** |
| 29 | * The version of the PSA Framework API that is being used to build the calling |
| 30 | * firmware. Only part of features of FF-M v1.1 have been implemented. FF-M v1.1 |
| 31 | * is compatible with v1.0. |
| 32 | */ |
| 33 | #define PSA_FRAMEWORK_VERSION (0x0101u) |
| 34 | |
| 35 | /** |
| 36 | * Return value from psa_version() if the requested RoT Service is not present |
| 37 | * in the system. |
| 38 | */ |
| 39 | #define PSA_VERSION_NONE (0u) |
| 40 | |
| 41 | /** |
| 42 | * The zero-value null handle can be assigned to variables used in clients and |
| 43 | * RoT Services, indicating that there is no current connection or message. |
| 44 | */ |
| 45 | #define PSA_NULL_HANDLE ((psa_handle_t)0) |
| 46 | |
| 47 | /** |
| 48 | * Tests whether a handle value returned by psa_connect() is valid. |
| 49 | */ |
| 50 | #define PSA_HANDLE_IS_VALID(handle) ((psa_handle_t)(handle) > 0) |
| 51 | |
| 52 | /** |
| 53 | * Converts the handle value returned from a failed call psa_connect() into |
| 54 | * an error code. |
| 55 | */ |
| 56 | #define PSA_HANDLE_TO_ERROR(handle) ((psa_status_t)(handle)) |
| 57 | |
| 58 | /** |
| 59 | * Maximum number of input and output vectors for a request to psa_call(). |
| 60 | */ |
| 61 | #define PSA_MAX_IOVEC (4u) |
| 62 | |
| 63 | /** |
| 64 | * An IPC message type that indicates a generic client request. |
| 65 | */ |
| 66 | #define PSA_IPC_CALL (0) |
| 67 | |
| 68 | /** |
| 69 | * A read-only input memory region provided to an RoT Service. |
| 70 | */ |
| 71 | struct __attribute__ ((__packed__)) psa_invec { |
| 72 | uint32_t base; /*!< the start address of the memory buffer */ |
| 73 | uint32_t len; /*!< the size in bytes */ |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * A writable output memory region provided to an RoT Service. |
| 78 | */ |
| 79 | struct __attribute__ ((__packed__)) psa_outvec { |
| 80 | uint32_t base; /*!< the start address of the memory buffer */ |
| 81 | uint32_t len; /*!< the size in bytes */ |
| 82 | }; |
| 83 | |
| 84 | /*************************** PSA Client API **********************************/ |
| 85 | |
| 86 | /** |
| 87 | * \brief Retrieve the version of the PSA Framework API that is implemented. |
| 88 | * |
| 89 | * \param[in] rpc_caller RPC caller to use |
| 90 | * \return version The version of the PSA Framework implementation |
| 91 | * that is providing the runtime services to the |
| 92 | * caller. The major and minor version are encoded |
| 93 | * as follows: |
| 94 | * \arg version[15:8] -- major version number. |
| 95 | * \arg version[7:0] -- minor version number. |
| 96 | */ |
| 97 | uint32_t psa_framework_version(struct rpc_caller *caller); |
| 98 | |
| 99 | /** |
| 100 | * \brief Retrieve the version of an RoT Service or indicate that it is not |
| 101 | * present on this system. |
| 102 | * |
| 103 | * \param[in] rpc_caller RPC caller to use |
| 104 | * \param[in] sid ID of the RoT Service to query. |
| 105 | * |
| 106 | * \retval PSA_VERSION_NONE The RoT Service is not implemented, or the |
| 107 | * caller is not permitted to access the service. |
| 108 | * \retval > 0 The version of the implemented RoT Service. |
| 109 | */ |
| 110 | uint32_t psa_version(struct rpc_caller *caller, uint32_t sid); |
| 111 | |
| 112 | /** |
| 113 | * \brief Connect to an RoT Service by its SID. |
| 114 | * |
| 115 | * \param[in] rpc_caller RPC caller to use |
| 116 | * \param[in] sid ID of the RoT Service to connect to. |
| 117 | * \param[in] version Requested version of the RoT Service. |
| 118 | * |
| 119 | * \retval > 0 A handle for the connection. |
| 120 | * \retval PSA_ERROR_CONNECTION_REFUSED The SPM or RoT Service has refused the |
| 121 | * connection. |
| 122 | * \retval PSA_ERROR_CONNECTION_BUSY The SPM or RoT Service cannot make the |
| 123 | * connection at the moment. |
| 124 | * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more |
| 125 | * of the following are true: |
| 126 | * \arg The RoT Service ID is not present. |
| 127 | * \arg The RoT Service version is not supported. |
| 128 | * \arg The caller is not allowed to access the RoT |
| 129 | * service. |
| 130 | */ |
| 131 | psa_handle_t psa_connect(struct rpc_caller *caller, uint32_t sid, |
| 132 | uint32_t version); |
| 133 | |
| 134 | /** |
| 135 | * \brief Call an RoT Service on an established connection. |
| 136 | * |
| 137 | * \note FF-M 1.0 proposes 6 parameters for psa_call but the secure gateway ABI |
| 138 | * support at most 4 parameters. TF-M chooses to encode 'in_len', |
| 139 | * 'out_len', and 'type' into a 32-bit integer to improve efficiency. |
| 140 | * Compared with struct-based encoding, this method saves extra memory |
| 141 | * check and memory copy operation. The disadvantage is that the 'type' |
| 142 | * range has to be reduced into a 16-bit integer. So with this encoding, |
| 143 | * the valid range for 'type' is 0-32767. |
| 144 | * |
| 145 | * \param[in] rpc_caller RPC caller to use |
| 146 | * \param[in] handle A handle to an established connection. |
| 147 | * \param[in] type The request type. |
| 148 | * Must be zero( \ref PSA_IPC_CALL) or positive. |
| 149 | * \param[in] in_vec Array of input \ref psa_invec structures. |
| 150 | * \param[in] in_len Number of input \ref psa_invec structures. |
| 151 | * \param[in,out] out_vec Array of output \ref psa_outvec structures. |
| 152 | * \param[in] out_len Number of output \ref psa_outvec structures. |
| 153 | * |
| 154 | * \retval >=0 RoT Service-specific status value. |
| 155 | * \retval <0 RoT Service-specific error code. |
| 156 | * \retval PSA_ERROR_PROGRAMMER_ERROR The connection has been terminated by the |
| 157 | * RoT Service. The call is a PROGRAMMER ERROR if |
| 158 | * one or more of the following are true: |
| 159 | * \arg An invalid handle was passed. |
| 160 | * \arg The connection is already handling a request. |
| 161 | * \arg type < 0. |
| 162 | * \arg An invalid memory reference was provided. |
| 163 | * \arg in_len + out_len > PSA_MAX_IOVEC. |
| 164 | * \arg The message is unrecognized by the RoT |
| 165 | * Service or incorrectly formatted. |
| 166 | */ |
| 167 | psa_status_t psa_call(struct rpc_caller *caller, psa_handle_t handle, |
| 168 | int32_t type, const struct psa_invec *in_vec, |
| 169 | size_t in_len, struct psa_outvec *out_vec, size_t out_len); |
| 170 | |
| 171 | /** |
| 172 | * \brief Close a connection to an RoT Service. |
| 173 | * |
| 174 | * \param[in] rpc_caller RPC caller to use |
| 175 | * \param[in] handle A handle to an established connection, or the |
| 176 | * null handle. |
| 177 | * |
| 178 | * \retval void Success. |
| 179 | * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more |
| 180 | * of the following are true: |
| 181 | * \arg An invalid handle was provided that is not |
| 182 | * the null handle. |
| 183 | * \arg The connection is currently handling a |
| 184 | * request. |
| 185 | */ |
| 186 | void psa_close(struct rpc_caller *caller, psa_handle_t handle); |
| 187 | |
| 188 | #ifdef __cplusplus |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | #endif /* SERVICE_PSA_IPC_H */ |
| 193 | |
| 194 | |