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