blob: 967db72dd5179cb75ae7710eb0934ece4c35033e [file] [log] [blame]
Miklos Balint9ecb24c2018-03-29 15:30:28 +02001/*
David Vincze6ec7c652025-03-07 17:46:28 +00002 * SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
Miklos Balint9ecb24c2018-03-29 15:30:28 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __PSA_CLIENT_H__
9#define __PSA_CLIENT_H__
10
Jamie Fox520fb4d2019-06-13 14:27:21 +010011#include <stddef.h>
12#include <stdint.h>
13
14#include "psa/error.h"
15
Miklos Balint9ecb24c2018-03-29 15:30:28 +020016#ifdef __cplusplus
17extern "C" {
18#endif
19
Xinyu Zhangade2e0a2021-03-18 16:20:54 +080020#ifndef IOVEC_LEN
21#define IOVEC_LEN(arr) ((uint32_t)(sizeof(arr)/sizeof(arr[0])))
22#endif
23
David Vincze6ec7c652025-03-07 17:46:28 +000024/**
25 * Type definitions equivalent to size_t as defined in the RoT Service
26 * environment.
27 */
28typedef uint32_t rot_size_t;
29#define ROT_SIZE_MAX UINT32_MAX
30
Edison Aib3e56962018-09-04 19:12:31 +080031/*********************** PSA Client Macros and Types *************************/
Miklos Balint9ecb24c2018-03-29 15:30:28 +020032
Summer Qin4b1d03b2019-07-02 14:56:08 +080033/**
34 * The version of the PSA Framework API that is being used to build the calling
Mingyang Sun9ac44d32021-03-12 14:47:46 +080035 * firmware. Only part of features of FF-M v1.1 have been implemented. FF-M v1.1
36 * is compatible with v1.0.
Summer Qin4b1d03b2019-07-02 14:56:08 +080037 */
Mingyang Sun9ac44d32021-03-12 14:47:46 +080038#define PSA_FRAMEWORK_VERSION (0x0101u)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020039
Summer Qin4b1d03b2019-07-02 14:56:08 +080040/**
41 * Return value from psa_version() if the requested RoT Service is not present
42 * in the system.
43 */
44#define PSA_VERSION_NONE (0u)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020045
Summer Qin4b1d03b2019-07-02 14:56:08 +080046/**
47 * The zero-value null handle can be assigned to variables used in clients and
48 * RoT Services, indicating that there is no current connection or message.
49 */
50#define PSA_NULL_HANDLE ((psa_handle_t)0)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020051
Summer Qin4b1d03b2019-07-02 14:56:08 +080052/**
53 * Tests whether a handle value returned by psa_connect() is valid.
54 */
55#define PSA_HANDLE_IS_VALID(handle) ((psa_handle_t)(handle) > 0)
56
57/**
58 * Converts the handle value returned from a failed call psa_connect() into
59 * an error code.
60 */
61#define PSA_HANDLE_TO_ERROR(handle) ((psa_status_t)(handle))
62
63/**
64 * Maximum number of input and output vectors for a request to psa_call().
65 */
66#define PSA_MAX_IOVEC (4u)
67
Ken Liu40b09ba2023-08-10 10:01:40 +080068
69/**
70 * The minimum and maximum value in THIS implementation that can be passed
71 * as the type parameter in a call to psa_call().
72 */
73
74#define PSA_CALL_TYPE_MIN (0)
75#define PSA_CALL_TYPE_MAX (INT16_MAX)
76
Summer Qin4b1d03b2019-07-02 14:56:08 +080077/**
78 * An IPC message type that indicates a generic client request.
79 */
80#define PSA_IPC_CALL (0)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020081
Miklos Balint9ecb24c2018-03-29 15:30:28 +020082typedef int32_t psa_handle_t;
83
84/**
Edison Aib3e56962018-09-04 19:12:31 +080085 * A read-only input memory region provided to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +020086 */
87typedef struct psa_invec {
Edison Aib3e56962018-09-04 19:12:31 +080088 const void *base; /*!< the start address of the memory buffer */
89 size_t len; /*!< the size in bytes */
Miklos Balint9ecb24c2018-03-29 15:30:28 +020090} psa_invec;
91
92/**
Edison Aib3e56962018-09-04 19:12:31 +080093 * A writable output memory region provided to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +020094 */
95typedef struct psa_outvec {
Edison Aib3e56962018-09-04 19:12:31 +080096 void *base; /*!< the start address of the memory buffer */
97 size_t len; /*!< the size in bytes */
Miklos Balint9ecb24c2018-03-29 15:30:28 +020098} psa_outvec;
99
Edison Aib3e56962018-09-04 19:12:31 +0800100/*************************** PSA Client API **********************************/
101
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200102/**
103 * \brief Retrieve the version of the PSA Framework API that is implemented.
104 *
Edison Aib3e56962018-09-04 19:12:31 +0800105 * \return version The version of the PSA Framework implementation
106 * that is providing the runtime services to the
107 * caller. The major and minor version are encoded
108 * as follows:
109 * \arg version[15:8] -- major version number.
110 * \arg version[7:0] -- minor version number.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200111 */
112uint32_t psa_framework_version(void);
113
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200114/**
Summer Qin4b1d03b2019-07-02 14:56:08 +0800115 * \brief Retrieve the version of an RoT Service or indicate that it is not
116 * present on this system.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200117 *
Edison Aib3e56962018-09-04 19:12:31 +0800118 * \param[in] sid ID of the RoT Service to query.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200119 *
Edison Aib3e56962018-09-04 19:12:31 +0800120 * \retval PSA_VERSION_NONE The RoT Service is not implemented, or the
121 * caller is not permitted to access the service.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800122 * \retval > 0 The version of the implemented RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200123 */
124uint32_t psa_version(uint32_t sid);
125
126/**
Edison Aib3e56962018-09-04 19:12:31 +0800127 * \brief Connect to an RoT Service by its SID.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200128 *
Edison Aib3e56962018-09-04 19:12:31 +0800129 * \param[in] sid ID of the RoT Service to connect to.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800130 * \param[in] version Requested version of the RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200131 *
Edison Aib3e56962018-09-04 19:12:31 +0800132 * \retval > 0 A handle for the connection.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800133 * \retval PSA_ERROR_CONNECTION_REFUSED The SPM or RoT Service has refused the
Edison Aib3e56962018-09-04 19:12:31 +0800134 * connection.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800135 * \retval PSA_ERROR_CONNECTION_BUSY The SPM or RoT Service cannot make the
Edison Aib3e56962018-09-04 19:12:31 +0800136 * connection at the moment.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800137 * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more
138 * of the following are true:
139 * \arg The RoT Service ID is not present.
140 * \arg The RoT Service version is not supported.
141 * \arg The caller is not allowed to access the RoT
142 * service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200143 */
Summer Qin4b1d03b2019-07-02 14:56:08 +0800144psa_handle_t psa_connect(uint32_t sid, uint32_t version);
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200145
146/**
Edison Aib3e56962018-09-04 19:12:31 +0800147 * \brief Call an RoT Service on an established connection.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200148 *
Shawn Shan9c7b9be2021-05-17 15:28:02 +0800149 * \note FF-M 1.0 proposes 6 parameters for psa_call but the secure gateway ABI
150 * support at most 4 parameters. TF-M chooses to encode 'in_len',
151 * 'out_len', and 'type' into a 32-bit integer to improve efficiency.
152 * Compared with struct-based encoding, this method saves extra memory
153 * check and memory copy operation. The disadvantage is that the 'type'
154 * range has to be reduced into a 16-bit integer. So with this encoding,
155 * the valid range for 'type' is 0-32767.
156 *
Edison Aib3e56962018-09-04 19:12:31 +0800157 * \param[in] handle A handle to an established connection.
Edison Aib6d91ad2020-07-16 17:42:40 +0800158 * \param[in] type The request type.
Summer Qin632b3e02019-07-29 15:34:38 +0800159 * Must be zero( \ref PSA_IPC_CALL) or positive.
Edison Aib3e56962018-09-04 19:12:31 +0800160 * \param[in] in_vec Array of input \ref psa_invec structures.
161 * \param[in] in_len Number of input \ref psa_invec structures.
Shawn Shan7ef79ec2021-01-21 10:28:18 +0800162 * \param[in,out] out_vec Array of output \ref psa_outvec structures.
Edison Aib3e56962018-09-04 19:12:31 +0800163 * \param[in] out_len Number of output \ref psa_outvec structures.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200164 *
Edison Aib3e56962018-09-04 19:12:31 +0800165 * \retval >=0 RoT Service-specific status value.
166 * \retval <0 RoT Service-specific error code.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800167 * \retval PSA_ERROR_PROGRAMMER_ERROR The connection has been terminated by the
168 * RoT Service. The call is a PROGRAMMER ERROR if
169 * one or more of the following are true:
Edison Aib3e56962018-09-04 19:12:31 +0800170 * \arg An invalid handle was passed.
171 * \arg The connection is already handling a request.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800172 * \arg type < 0.
Edison Aib3e56962018-09-04 19:12:31 +0800173 * \arg An invalid memory reference was provided.
174 * \arg in_len + out_len > PSA_MAX_IOVEC.
175 * \arg The message is unrecognized by the RoT
176 * Service or incorrectly formatted.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200177 */
Summer Qin4b1d03b2019-07-02 14:56:08 +0800178psa_status_t psa_call(psa_handle_t handle, int32_t type,
Edison Aib3e56962018-09-04 19:12:31 +0800179 const psa_invec *in_vec,
180 size_t in_len,
181 psa_outvec *out_vec,
182 size_t out_len);
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200183
184/**
Edison Aib3e56962018-09-04 19:12:31 +0800185 * \brief Close a connection to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200186 *
Edison Aib3e56962018-09-04 19:12:31 +0800187 * \param[in] handle A handle to an established connection, or the
188 * null handle.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200189 *
Edison Aib3e56962018-09-04 19:12:31 +0800190 * \retval void Success.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800191 * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more
192 * of the following are true:
Edison Aib3e56962018-09-04 19:12:31 +0800193 * \arg An invalid handle was provided that is not
194 * the null handle.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800195 * \arg The connection is currently handling a
196 * request.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200197 */
198void psa_close(psa_handle_t handle);
199
200#ifdef __cplusplus
201}
202#endif
203
204#endif /* __PSA_CLIENT_H__ */