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