blob: 7aee1e5e6a3b9732047d3e89fb0ef95acac9af75 [file] [log] [blame]
Miklos Balint9ecb24c2018-03-29 15:30:28 +02001/*
Shawn Shan7ef79ec2021-01-21 10:28:18 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
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
Edison Aib3e56962018-09-04 19:12:31 +080024/*********************** PSA Client Macros and Types *************************/
Miklos Balint9ecb24c2018-03-29 15:30:28 +020025
Summer Qin4b1d03b2019-07-02 14:56:08 +080026/**
27 * The version of the PSA Framework API that is being used to build the calling
Mingyang Sun9ac44d32021-03-12 14:47:46 +080028 * firmware. Only part of features of FF-M v1.1 have been implemented. FF-M v1.1
29 * is compatible with v1.0.
Summer Qin4b1d03b2019-07-02 14:56:08 +080030 */
Mingyang Sun9ac44d32021-03-12 14:47:46 +080031#define PSA_FRAMEWORK_VERSION (0x0101u)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020032
Summer Qin4b1d03b2019-07-02 14:56:08 +080033/**
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 Balint9ecb24c2018-03-29 15:30:28 +020038
Summer Qin4b1d03b2019-07-02 14:56:08 +080039/**
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 Balint9ecb24c2018-03-29 15:30:28 +020044
Summer Qin4b1d03b2019-07-02 14:56:08 +080045/**
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
61/**
62 * An IPC message type that indicates a generic client request.
63 */
64#define PSA_IPC_CALL (0)
Miklos Balint9ecb24c2018-03-29 15:30:28 +020065
Miklos Balint9ecb24c2018-03-29 15:30:28 +020066typedef int32_t psa_handle_t;
67
68/**
Edison Aib3e56962018-09-04 19:12:31 +080069 * A read-only input memory region provided to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +020070 */
71typedef struct psa_invec {
Edison Aib3e56962018-09-04 19:12:31 +080072 const void *base; /*!< the start address of the memory buffer */
73 size_t len; /*!< the size in bytes */
Miklos Balint9ecb24c2018-03-29 15:30:28 +020074} psa_invec;
75
76/**
Edison Aib3e56962018-09-04 19:12:31 +080077 * A writable output memory region provided to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +020078 */
79typedef struct psa_outvec {
Edison Aib3e56962018-09-04 19:12:31 +080080 void *base; /*!< the start address of the memory buffer */
81 size_t len; /*!< the size in bytes */
Miklos Balint9ecb24c2018-03-29 15:30:28 +020082} psa_outvec;
83
Edison Aib3e56962018-09-04 19:12:31 +080084/*************************** PSA Client API **********************************/
85
Miklos Balint9ecb24c2018-03-29 15:30:28 +020086/**
87 * \brief Retrieve the version of the PSA Framework API that is implemented.
88 *
Edison Aib3e56962018-09-04 19:12:31 +080089 * \return version The version of the PSA Framework implementation
90 * that is providing the runtime services to the
91 * caller. The major and minor version are encoded
92 * as follows:
93 * \arg version[15:8] -- major version number.
94 * \arg version[7:0] -- minor version number.
Miklos Balint9ecb24c2018-03-29 15:30:28 +020095 */
96uint32_t psa_framework_version(void);
97
Miklos Balint9ecb24c2018-03-29 15:30:28 +020098/**
Summer Qin4b1d03b2019-07-02 14:56:08 +080099 * \brief Retrieve the version of an RoT Service or indicate that it is not
100 * present on this system.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200101 *
Edison Aib3e56962018-09-04 19:12:31 +0800102 * \param[in] sid ID of the RoT Service to query.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200103 *
Edison Aib3e56962018-09-04 19:12:31 +0800104 * \retval PSA_VERSION_NONE The RoT Service is not implemented, or the
105 * caller is not permitted to access the service.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800106 * \retval > 0 The version of the implemented RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200107 */
108uint32_t psa_version(uint32_t sid);
109
110/**
Edison Aib3e56962018-09-04 19:12:31 +0800111 * \brief Connect to an RoT Service by its SID.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200112 *
Edison Aib3e56962018-09-04 19:12:31 +0800113 * \param[in] sid ID of the RoT Service to connect to.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800114 * \param[in] version Requested version of the RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200115 *
Edison Aib3e56962018-09-04 19:12:31 +0800116 * \retval > 0 A handle for the connection.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800117 * \retval PSA_ERROR_CONNECTION_REFUSED The SPM or RoT Service has refused the
Edison Aib3e56962018-09-04 19:12:31 +0800118 * connection.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800119 * \retval PSA_ERROR_CONNECTION_BUSY The SPM or RoT Service cannot make the
Edison Aib3e56962018-09-04 19:12:31 +0800120 * connection at the moment.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800121 * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more
122 * of the following are true:
123 * \arg The RoT Service ID is not present.
124 * \arg The RoT Service version is not supported.
125 * \arg The caller is not allowed to access the RoT
126 * service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200127 */
Summer Qin4b1d03b2019-07-02 14:56:08 +0800128psa_handle_t psa_connect(uint32_t sid, uint32_t version);
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200129
130/**
Edison Aib3e56962018-09-04 19:12:31 +0800131 * \brief Call an RoT Service on an established connection.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200132 *
Shawn Shan9c7b9be2021-05-17 15:28:02 +0800133 * \note FF-M 1.0 proposes 6 parameters for psa_call but the secure gateway ABI
134 * support at most 4 parameters. TF-M chooses to encode 'in_len',
135 * 'out_len', and 'type' into a 32-bit integer to improve efficiency.
136 * Compared with struct-based encoding, this method saves extra memory
137 * check and memory copy operation. The disadvantage is that the 'type'
138 * range has to be reduced into a 16-bit integer. So with this encoding,
139 * the valid range for 'type' is 0-32767.
140 *
Edison Aib3e56962018-09-04 19:12:31 +0800141 * \param[in] handle A handle to an established connection.
Edison Aib6d91ad2020-07-16 17:42:40 +0800142 * \param[in] type The request type.
Summer Qin632b3e02019-07-29 15:34:38 +0800143 * Must be zero( \ref PSA_IPC_CALL) or positive.
Edison Aib3e56962018-09-04 19:12:31 +0800144 * \param[in] in_vec Array of input \ref psa_invec structures.
145 * \param[in] in_len Number of input \ref psa_invec structures.
Shawn Shan7ef79ec2021-01-21 10:28:18 +0800146 * \param[in,out] out_vec Array of output \ref psa_outvec structures.
Edison Aib3e56962018-09-04 19:12:31 +0800147 * \param[in] out_len Number of output \ref psa_outvec structures.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200148 *
Edison Aib3e56962018-09-04 19:12:31 +0800149 * \retval >=0 RoT Service-specific status value.
150 * \retval <0 RoT Service-specific error code.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800151 * \retval PSA_ERROR_PROGRAMMER_ERROR The connection has been terminated by the
152 * RoT Service. The call is a PROGRAMMER ERROR if
153 * one or more of the following are true:
Edison Aib3e56962018-09-04 19:12:31 +0800154 * \arg An invalid handle was passed.
155 * \arg The connection is already handling a request.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800156 * \arg type < 0.
Edison Aib3e56962018-09-04 19:12:31 +0800157 * \arg An invalid memory reference was provided.
158 * \arg in_len + out_len > PSA_MAX_IOVEC.
159 * \arg The message is unrecognized by the RoT
160 * Service or incorrectly formatted.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200161 */
Summer Qin4b1d03b2019-07-02 14:56:08 +0800162psa_status_t psa_call(psa_handle_t handle, int32_t type,
Edison Aib3e56962018-09-04 19:12:31 +0800163 const psa_invec *in_vec,
164 size_t in_len,
165 psa_outvec *out_vec,
166 size_t out_len);
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200167
168/**
Edison Aib3e56962018-09-04 19:12:31 +0800169 * \brief Close a connection to an RoT Service.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200170 *
Edison Aib3e56962018-09-04 19:12:31 +0800171 * \param[in] handle A handle to an established connection, or the
172 * null handle.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200173 *
Edison Aib3e56962018-09-04 19:12:31 +0800174 * \retval void Success.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800175 * \retval "PROGRAMMER ERROR" The call is a PROGRAMMER ERROR if one or more
176 * of the following are true:
Edison Aib3e56962018-09-04 19:12:31 +0800177 * \arg An invalid handle was provided that is not
178 * the null handle.
Summer Qin4b1d03b2019-07-02 14:56:08 +0800179 * \arg The connection is currently handling a
180 * request.
Miklos Balint9ecb24c2018-03-29 15:30:28 +0200181 */
182void psa_close(psa_handle_t handle);
183
184#ifdef __cplusplus
185}
186#endif
187
188#endif /* __PSA_CLIENT_H__ */