xref: /OK3568_Linux_fs/u-boot/include/optee_include/tee_client_api.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2017, Rockchip Electronics Co., Ltd
3*4882a593Smuzhiyun  * hisping lin, <hisping.lin@rock-chips.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #ifndef TEE_CLIENT_API_H
8*4882a593Smuzhiyun #define TEE_CLIENT_API_H
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <optee_include/tee_base_types.h>
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * Defines the number of available memory references in an open session or
13*4882a593Smuzhiyun  * invoke command operation payload.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun #define TEEC_CONFIG_PAYLOAD_REF_COUNT 4
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * Defines the maximum size of a single shared memory block, in bytes, of both
19*4882a593Smuzhiyun  * API allocated and API registered memory. The size is currently set to
20*4882a593Smuzhiyun  * 512 * kB (512 * 1024).
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun #define TEEC_CONFIG_SHAREDMEM_MAX_SIZE 0x8000
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun  * Flag constants indicating the type of parameters encoded inside the
26*4882a593Smuzhiyun  * operation payload (TEEC_Operation), Type is uint32_t.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * TEEC_NONE                   The Parameter is not used
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * TEEC_VALUE_INPUT            The Parameter is a TEEC_Value tagged as input.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * TEEC_VALUE_OUTPUT           The Parameter is a TEEC_Value tagged as output.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * TEEC_VALUE_INOUT            The Parameter is a TEEC_Value tagged as both as
35*4882a593Smuzhiyun  *                             input and output, i.e., for which both the
36*4882a593Smuzhiyun  *                             behaviors of TEEC_VALUE_INPUT and
37*4882a593Smuzhiyun  *                             TEEC_VALUE_OUTPUT apply.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * TEEC_MEMREF_TEMP_INPUT      The Parameter is a TEEC_TempMemoryReference
40*4882a593Smuzhiyun  *                             describing a region of memory which needs to be
41*4882a593Smuzhiyun  *                             temporarily registered for the duration of the
42*4882a593Smuzhiyun  *                             Operation and is tagged as input.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * TEEC_MEMREF_TEMP_OUTPUT     Same as TEEC_MEMREF_TEMP_INPUT, but the Memory
45*4882a593Smuzhiyun  *                             Reference is tagged as output. The
46*4882a593Smuzhiyun  *                             Implementation may update the size field to
47*4882a593Smuzhiyun  *                             reflect the required output size in some use
48*4882a593Smuzhiyun  *                             cases.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * TEEC_MEMREF_TEMP_INOUT      A Temporary Memory Reference tagged as both
51*4882a593Smuzhiyun  *                             input and output, i.e., for which both the
52*4882a593Smuzhiyun  *                             behaviors of TEEC_MEMREF_TEMP_INPUT and
53*4882a593Smuzhiyun  *                             TEEC_MEMREF_TEMP_OUTPUT apply.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * TEEC_MEMREF_WHOLE           The Parameter is a Registered Memory Reference
56*4882a593Smuzhiyun  *                             that refers to the entirety of its parent Shared
57*4882a593Smuzhiyun  *                             Memory block. The parameter structure is a
58*4882a593Smuzhiyun  *                             TEEC_MemoryReference. In this structure, the
59*4882a593Smuzhiyun  *                             Implementation MUST read only the parent field
60*4882a593Smuzhiyun  *                             and MAY update the size field when the operation
61*4882a593Smuzhiyun  *                             completes.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * TEEC_MEMREF_PARTIAL_INPUT   A Registered Memory Reference structure that
64*4882a593Smuzhiyun  *                             refers to a partial region of its parent Shared
65*4882a593Smuzhiyun  *                             Memory block and is tagged as input.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * TEEC_MEMREF_PARTIAL_OUTPUT  Registered Memory Reference structure that
68*4882a593Smuzhiyun  *                             refers to a partial region of its parent Shared
69*4882a593Smuzhiyun  *                             Memory block and is tagged as output.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * TEEC_MEMREF_PARTIAL_INOUT   The Registered Memory Reference structure that
72*4882a593Smuzhiyun  *                             refers to a partial region of its parent Shared
73*4882a593Smuzhiyun  *                             Memory block and is tagged as both input and
74*4882a593Smuzhiyun  *                             output, i.e., for which both the behaviors of
75*4882a593Smuzhiyun  *                             TEEC_MEMREF_PARTIAL_INPUT and
76*4882a593Smuzhiyun  *                             TEEC_MEMREF_PARTIAL_OUTPUT apply.
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun #define TEEC_NONE                   0x00000000
79*4882a593Smuzhiyun #define TEEC_VALUE_INPUT            0x00000001
80*4882a593Smuzhiyun #define TEEC_VALUE_OUTPUT           0x00000002
81*4882a593Smuzhiyun #define TEEC_VALUE_INOUT            0x00000003
82*4882a593Smuzhiyun #define TEEC_MEMREF_TEMP_INPUT      0x00000005
83*4882a593Smuzhiyun #define TEEC_MEMREF_TEMP_OUTPUT     0x00000006
84*4882a593Smuzhiyun #define TEEC_MEMREF_TEMP_INOUT      0x00000007
85*4882a593Smuzhiyun #define TEEC_MEMREF_WHOLE           0x0000000C
86*4882a593Smuzhiyun #define TEEC_MEMREF_PARTIAL_INPUT   0x0000000D
87*4882a593Smuzhiyun #define TEEC_MEMREF_PARTIAL_OUTPUT  0x0000000E
88*4882a593Smuzhiyun #define TEEC_MEMREF_PARTIAL_INOUT   0x0000000F
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /**
91*4882a593Smuzhiyun  * Flag constants indicating the data transfer direction of memory in
92*4882a593Smuzhiyun  * TEEC_Parameter. TEEC_MEM_INPUT signifies data transfer direction from the
93*4882a593Smuzhiyun  * client application to the TEE. TEEC_MEM_OUTPUT signifies data transfer
94*4882a593Smuzhiyun  * direction from the TEE to the client application. Type is uint32_t.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * TEEC_MEM_INPUT   The Shared Memory can carry data from the client
97*4882a593Smuzhiyun  *                  application to the Trusted Application.
98*4882a593Smuzhiyun  * TEEC_MEM_OUTPUT  The Shared Memory can carry data from the Trusted
99*4882a593Smuzhiyun  *                  Application to the client application.
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun #define TEEC_MEM_INPUT   0x00000001
102*4882a593Smuzhiyun #define TEEC_MEM_OUTPUT  0x00000002
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun  * Return values. Type is TEEC_Result
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * TEEC_SUCCESS                 The operation was successful.
108*4882a593Smuzhiyun  * TEEC_ERROR_GENERIC           Non-specific cause.
109*4882a593Smuzhiyun  * TEEC_ERROR_ACCESS_DENIED     Access privileges are not sufficient.
110*4882a593Smuzhiyun  * TEEC_ERROR_CANCEL            The operation was canceled.
111*4882a593Smuzhiyun  * TEEC_ERROR_ACCESS_CONFLICT   Concurrent accesses caused conflict.
112*4882a593Smuzhiyun  * TEEC_ERROR_EXCESS_DATA       Too much data for the requested operation was
113*4882a593Smuzhiyun  *                              passed.
114*4882a593Smuzhiyun  * TEEC_ERROR_BAD_FORMAT        Input data was of invalid format.
115*4882a593Smuzhiyun  * TEEC_ERROR_BAD_PARAMETERS    Input parameters were invalid.
116*4882a593Smuzhiyun  * TEEC_ERROR_BAD_STATE         Operation is not valid in the current state.
117*4882a593Smuzhiyun  * TEEC_ERROR_ITEM_NOT_FOUND    The requested data item is not found.
118*4882a593Smuzhiyun  * TEEC_ERROR_NOT_IMPLEMENTED   The requested operation should exist but is not
119*4882a593Smuzhiyun  *                              yet implemented.
120*4882a593Smuzhiyun  * TEEC_ERROR_NOT_SUPPORTED     The requested operation is valid but is not
121*4882a593Smuzhiyun  *                              supported in this implementation.
122*4882a593Smuzhiyun  * TEEC_ERROR_NO_DATA           Expected data was missing.
123*4882a593Smuzhiyun  * TEEC_ERROR_OUT_OF_MEMORY     System ran out of resources.
124*4882a593Smuzhiyun  * TEEC_ERROR_BUSY              The system is busy working on something else.
125*4882a593Smuzhiyun  * TEEC_ERROR_COMMUNICATION     Communication with a remote party failed.
126*4882a593Smuzhiyun  * TEEC_ERROR_SECURITY          A security fault was detected.
127*4882a593Smuzhiyun  * TEEC_ERROR_SHORT_BUFFER      The supplied buffer is too short for the
128*4882a593Smuzhiyun  *                              generated output.
129*4882a593Smuzhiyun  * TEEC_ERROR_TARGET_DEAD       Trusted Application has panicked
130*4882a593Smuzhiyun  *                              during the operation.
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun  *  Standard defined error codes.
135*4882a593Smuzhiyun  */
136*4882a593Smuzhiyun #define TEEC_SUCCESS                0x00000000
137*4882a593Smuzhiyun #define TEEC_ERROR_GENERIC          0xFFFF0000
138*4882a593Smuzhiyun #define TEEC_ERROR_ACCESS_DENIED    0xFFFF0001
139*4882a593Smuzhiyun #define TEEC_ERROR_CANCEL           0xFFFF0002
140*4882a593Smuzhiyun #define TEEC_ERROR_ACCESS_CONFLICT  0xFFFF0003
141*4882a593Smuzhiyun #define TEEC_ERROR_EXCESS_DATA      0xFFFF0004
142*4882a593Smuzhiyun #define TEEC_ERROR_BAD_FORMAT       0xFFFF0005
143*4882a593Smuzhiyun #define TEEC_ERROR_BAD_PARAMETERS   0xFFFF0006
144*4882a593Smuzhiyun #define TEEC_ERROR_BAD_STATE        0xFFFF0007
145*4882a593Smuzhiyun #define TEEC_ERROR_ITEM_NOT_FOUND   0xFFFF0008
146*4882a593Smuzhiyun #define TEEC_ERROR_NOT_IMPLEMENTED  0xFFFF0009
147*4882a593Smuzhiyun #define TEEC_ERROR_NOT_SUPPORTED    0xFFFF000A
148*4882a593Smuzhiyun #define TEEC_ERROR_NO_DATA          0xFFFF000B
149*4882a593Smuzhiyun #define TEEC_ERROR_OUT_OF_MEMORY    0xFFFF000C
150*4882a593Smuzhiyun #define TEEC_ERROR_BUSY             0xFFFF000D
151*4882a593Smuzhiyun #define TEEC_ERROR_COMMUNICATION    0xFFFF000E
152*4882a593Smuzhiyun #define TEEC_ERROR_SECURITY         0xFFFF000F
153*4882a593Smuzhiyun #define TEEC_ERROR_SHORT_BUFFER     0xFFFF0010
154*4882a593Smuzhiyun #define TEEC_ERROR_TARGET_DEAD      0xFFFF3024
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /**
157*4882a593Smuzhiyun  * Function error origins, of type TEEC_ErrorOrigin. These indicate where in
158*4882a593Smuzhiyun  * the software stack a particular return value originates from.
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * TEEC_ORIGIN_API          The error originated within the TEE Client API
161*4882a593Smuzhiyun  *                          implementation.
162*4882a593Smuzhiyun  * TEEC_ORIGIN_COMMS        The error originated within the underlying
163*4882a593Smuzhiyun  *                          communications stack linking the rich OS with
164*4882a593Smuzhiyun  *                          the TEE.
165*4882a593Smuzhiyun  * TEEC_ORIGIN_TEE          The error originated within the common TEE code.
166*4882a593Smuzhiyun  * TEEC_ORIGIN_TRUSTED_APP  The error originated within the Trusted Application
167*4882a593Smuzhiyun  *                          code.
168*4882a593Smuzhiyun  */
169*4882a593Smuzhiyun #define TEEC_ORIGIN_API          0x00000001
170*4882a593Smuzhiyun #define TEEC_ORIGIN_COMMS        0x00000002
171*4882a593Smuzhiyun #define TEEC_ORIGIN_TEE          0x00000003
172*4882a593Smuzhiyun #define TEEC_ORIGIN_TRUSTED_APP  0x00000004
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * Session login methods, for use in TEEC_OpenSession() as parameter
176*4882a593Smuzhiyun  * connectionMethod. Type is uint32_t.
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * TEEC_LOGIN_PUBLIC       No login data is provided.
179*4882a593Smuzhiyun  * TEEC_LOGIN_USER         Login data about the user running the Client
180*4882a593Smuzhiyun  *                         Application process is provided.
181*4882a593Smuzhiyun  * TEEC_LOGIN_GROUP        Login data about the group running the Client
182*4882a593Smuzhiyun  *                         Application process is provided.
183*4882a593Smuzhiyun  * TEEC_LOGIN_APPLICATION  Login data about the running Client Application
184*4882a593Smuzhiyun  *                         itself is provided.
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun #define TEEC_LOGIN_PUBLIC       0x00000000
187*4882a593Smuzhiyun #define TEEC_LOGIN_USER         0x00000001
188*4882a593Smuzhiyun #define TEEC_LOGIN_GROUP        0x00000002
189*4882a593Smuzhiyun #define TEEC_LOGIN_APPLICATION  0x00000004
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /**
192*4882a593Smuzhiyun  * Encode the paramTypes according to the supplied types.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * @param p0 The first param type.
195*4882a593Smuzhiyun  * @param p1 The second param type.
196*4882a593Smuzhiyun  * @param p2 The third param type.
197*4882a593Smuzhiyun  * @param p3 The fourth param type.
198*4882a593Smuzhiyun  */
199*4882a593Smuzhiyun #define TEEC_PARAM_TYPES(p0, p1, p2, p3) \
200*4882a593Smuzhiyun 	((p0) | ((p1) << 4) | ((p2) << 8) | ((p3) << 12))
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun  * Get the i_th param type from the paramType.
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * @param p The paramType.
206*4882a593Smuzhiyun  * @param i The i-th parameter to get the type for.
207*4882a593Smuzhiyun  */
208*4882a593Smuzhiyun #define TEEC_PARAM_TYPE_GET(p, i) (((p) >> (i * 4)) & 0xF)
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun typedef uint32_t TEEC_Result;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /**
213*4882a593Smuzhiyun  * struct TEEC_Context - Represents a connection between a client application
214*4882a593Smuzhiyun  * and a TEE.
215*4882a593Smuzhiyun  */
216*4882a593Smuzhiyun typedef struct {
217*4882a593Smuzhiyun 	char devname[256];
218*4882a593Smuzhiyun 	int fd;
219*4882a593Smuzhiyun } TEEC_Context;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun  * This type contains a Universally Unique Resource Identifier (UUID) type as
223*4882a593Smuzhiyun  * defined in RFC4122. These UUID values are used to identify Trusted
224*4882a593Smuzhiyun  * Applications.
225*4882a593Smuzhiyun  */
226*4882a593Smuzhiyun typedef struct {
227*4882a593Smuzhiyun 	uint32_t timeLow;
228*4882a593Smuzhiyun 	uint16_t timeMid;
229*4882a593Smuzhiyun 	uint16_t timeHiAndVersion;
230*4882a593Smuzhiyun 	uint8_t clockSeqAndNode[8];
231*4882a593Smuzhiyun } TEEC_UUID;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /**
234*4882a593Smuzhiyun  * struct TEEC_SharedMemory - Memory to transfer data between a client
235*4882a593Smuzhiyun  * application and trusted code.
236*4882a593Smuzhiyun  *
237*4882a593Smuzhiyun  * @param buffer      The memory buffer which is to be, or has been, shared
238*4882a593Smuzhiyun  *                    with the TEE.
239*4882a593Smuzhiyun  * @param size        The size, in bytes, of the memory buffer.
240*4882a593Smuzhiyun  * @param flags       Bit-vector which holds properties of buffer.
241*4882a593Smuzhiyun  *                    The bit-vector can contain either or both of the
242*4882a593Smuzhiyun  *                    TEEC_MEM_INPUT and TEEC_MEM_OUTPUT flags.
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * A shared memory block is a region of memory allocated in the context of the
245*4882a593Smuzhiyun  * client application memory space that can be used to transfer data between
246*4882a593Smuzhiyun  * that client application and a trusted application. The user of this struct
247*4882a593Smuzhiyun  * is responsible to populate the buffer pointer.
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun typedef struct {
250*4882a593Smuzhiyun 	void *buffer;
251*4882a593Smuzhiyun 	size_t size;
252*4882a593Smuzhiyun 	uint32_t flags;
253*4882a593Smuzhiyun 	/*
254*4882a593Smuzhiyun 	 * Implementation-Defined
255*4882a593Smuzhiyun 	 *
256*4882a593Smuzhiyun 	 * These represent a pointer to an allocated buffer.
257*4882a593Smuzhiyun 	 * This can be different from the
258*4882a593Smuzhiyun 	 * "buffer" above if the caller tried to register
259*4882a593Smuzhiyun 	 * a buffer rather than allocate one.
260*4882a593Smuzhiyun 	 */
261*4882a593Smuzhiyun 	void   *alloc_buffer;
262*4882a593Smuzhiyun } TEEC_SharedMemory;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun /**
265*4882a593Smuzhiyun  * struct TEEC_TempMemoryReference - Temporary memory to transfer data between
266*4882a593Smuzhiyun  * a client application and trusted code, only used for the duration of the
267*4882a593Smuzhiyun  * operation.
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  * @param buffer  The memory buffer which is to be, or has been shared with
270*4882a593Smuzhiyun  *                the TEE.
271*4882a593Smuzhiyun  * @param size    The size, in bytes, of the memory buffer.
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * A memory buffer that is registered temporarily for the duration of the
274*4882a593Smuzhiyun  * operation to be called.
275*4882a593Smuzhiyun  */
276*4882a593Smuzhiyun typedef struct {
277*4882a593Smuzhiyun 	void *buffer;
278*4882a593Smuzhiyun 	size_t size;
279*4882a593Smuzhiyun } TEEC_TempMemoryReference;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun  * struct TEEC_RegisteredMemoryReference - use a pre-registered or
283*4882a593Smuzhiyun  * pre-allocated shared memory block of memory to transfer data between
284*4882a593Smuzhiyun  * a client application and trusted code.
285*4882a593Smuzhiyun  *
286*4882a593Smuzhiyun  * @param parent  Points to a shared memory structure. The memory reference
287*4882a593Smuzhiyun  *                may utilize the whole shared memory or only a part of it.
288*4882a593Smuzhiyun  *                Must not be NULL
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * @param size    The size, in bytes, of the memory buffer.
291*4882a593Smuzhiyun  *
292*4882a593Smuzhiyun  * @param offset  The offset, in bytes, of the referenced memory region from
293*4882a593Smuzhiyun  *                the start of the shared memory block.
294*4882a593Smuzhiyun  *
295*4882a593Smuzhiyun  */
296*4882a593Smuzhiyun typedef struct {
297*4882a593Smuzhiyun 	TEEC_SharedMemory *parent;
298*4882a593Smuzhiyun 	size_t size;
299*4882a593Smuzhiyun 	size_t offset;
300*4882a593Smuzhiyun } TEEC_RegisteredMemoryReference;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun  * struct TEEC_Value - Small raw data container
304*4882a593Smuzhiyun  *
305*4882a593Smuzhiyun  * Instead of allocating a shared memory buffer this structure can be used
306*4882a593Smuzhiyun  * to pass small raw data between a client application and trusted code.
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * @param a  The first integer value.
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * @param b  The second second value.
311*4882a593Smuzhiyun  */
312*4882a593Smuzhiyun typedef struct {
313*4882a593Smuzhiyun 	uint32_t a;
314*4882a593Smuzhiyun 	uint32_t b;
315*4882a593Smuzhiyun } TEEC_Value;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun /**
318*4882a593Smuzhiyun  * union TEEC_Parameter - Memory container to be used when passing data between
319*4882a593Smuzhiyun  *                        client application and trusted code.
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  * Either the client uses a shared memory reference, parts of it or a small raw
322*4882a593Smuzhiyun  * data container.
323*4882a593Smuzhiyun  *
324*4882a593Smuzhiyun  * @param tmpref  A temporary memory reference only valid for the duration
325*4882a593Smuzhiyun  *                of the operation.
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * @param memref  The entire shared memory or parts of it.
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * @param value   The small raw data container to use
330*4882a593Smuzhiyun  */
331*4882a593Smuzhiyun typedef union {
332*4882a593Smuzhiyun 	TEEC_TempMemoryReference tmpref;
333*4882a593Smuzhiyun 	TEEC_RegisteredMemoryReference memref;
334*4882a593Smuzhiyun 	TEEC_Value value;
335*4882a593Smuzhiyun } TEEC_Parameter;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /**
338*4882a593Smuzhiyun  * struct TEEC_Session - Represents a connection between a client application
339*4882a593Smuzhiyun  * and a trusted application.
340*4882a593Smuzhiyun  */
341*4882a593Smuzhiyun typedef struct {
342*4882a593Smuzhiyun 	uint32_t id;
343*4882a593Smuzhiyun } TEEC_Session;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun /**
346*4882a593Smuzhiyun  * struct TEEC_Operation - Holds information and memory references used in
347*4882a593Smuzhiyun  * TEEC_InvokeCommand().
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * @param   started     Client must initialize to zero if it needs to cancel
350*4882a593Smuzhiyun  *                      an operation about to be performed.
351*4882a593Smuzhiyun  * @param   paramTypes  Type of data passed. Use TEEC_PARAMS_TYPE macro to
352*4882a593Smuzhiyun  *                      create the correct flags.
353*4882a593Smuzhiyun  *                      0 means TEEC_NONE is passed for all params.
354*4882a593Smuzhiyun  * @param   params      Array of parameters of type TEEC_Parameter.
355*4882a593Smuzhiyun  * @param   session     Internal pointer to the last session used by
356*4882a593Smuzhiyun  *                      TEEC_InvokeCommand with this operation.
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  */
359*4882a593Smuzhiyun typedef struct {
360*4882a593Smuzhiyun 	uint32_t started;
361*4882a593Smuzhiyun 	uint32_t paramTypes;
362*4882a593Smuzhiyun 	TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
363*4882a593Smuzhiyun 	/* Implementation-Defined */
364*4882a593Smuzhiyun 	TEEC_Session *session;
365*4882a593Smuzhiyun 	TEEC_SharedMemory memRefs[TEEC_CONFIG_PAYLOAD_REF_COUNT];
366*4882a593Smuzhiyun 	uint32_t flags;
367*4882a593Smuzhiyun } TEEC_Operation;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun  * TEEC_InitializeContext() - Initializes a context holding connection
371*4882a593Smuzhiyun  * information on the specific TEE, designated by the name string.
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun  * @param name    A zero-terminated string identifying the TEE to connect to.
374*4882a593Smuzhiyun  *                If name is set to NULL, the default TEE is connected to. NULL
375*4882a593Smuzhiyun  *                is the only supported value in this version of the API
376*4882a593Smuzhiyun  *                implementation.
377*4882a593Smuzhiyun  *
378*4882a593Smuzhiyun  * @param context The context structure which is to be initialized.
379*4882a593Smuzhiyun  *
380*4882a593Smuzhiyun  * @return TEEC_SUCCESS  The initialization was successful.
381*4882a593Smuzhiyun  * @return TEEC_Result   Something failed.
382*4882a593Smuzhiyun  */
383*4882a593Smuzhiyun TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun  * TEEC_FinalizeContext() - Destroys a context holding connection information
387*4882a593Smuzhiyun  * on the specific TEE.
388*4882a593Smuzhiyun  *
389*4882a593Smuzhiyun  * This function destroys an initialized TEE context, closing the connection
390*4882a593Smuzhiyun  * between the client application and the TEE. This function must only be
391*4882a593Smuzhiyun  * called when all sessions related to this TEE context have been closed and
392*4882a593Smuzhiyun  * all shared memory blocks have been released, otherwise an error will be
393*4882a593Smuzhiyun  * returned.
394*4882a593Smuzhiyun  *
395*4882a593Smuzhiyun  * @param context       The context to be destroyed.
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * @return TEEC_SUCCESS The function call was successful.
398*4882a593Smuzhiyun  * @return TEEC_Result  Something failed.
399*4882a593Smuzhiyun  */
400*4882a593Smuzhiyun TEEC_Result TEEC_FinalizeContext(TEEC_Context *context);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun /**
403*4882a593Smuzhiyun  * TEEC_OpenSession() - Opens a new session with the specified trusted
404*4882a593Smuzhiyun  *                      application.
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * @param context            The initialized TEE context structure in which
407*4882a593Smuzhiyun  *                           scope to open the session.
408*4882a593Smuzhiyun  * @param session            The session to initialize.
409*4882a593Smuzhiyun  * @param destination        A structure identifying the trusted application
410*4882a593Smuzhiyun  *                           with which to open a session.
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * @param connectionMethod   The connection method to use.
413*4882a593Smuzhiyun  * @param connectionData     Any data necessary to connect with the chosen
414*4882a593Smuzhiyun  *                           connection method. Not supported, should be set to
415*4882a593Smuzhiyun  *                           NULL.
416*4882a593Smuzhiyun  * @param operation          An operation structure to use in the session. May
417*4882a593Smuzhiyun  *                           be set to NULL to signify no operation structure
418*4882a593Smuzhiyun  *                           needed.
419*4882a593Smuzhiyun  *
420*4882a593Smuzhiyun  * @param returnOrigin       A parameter which will hold the error origin if
421*4882a593Smuzhiyun  *                           this function returns any value other than
422*4882a593Smuzhiyun  *                           TEEC_SUCCESS.
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  * @return TEEC_SUCCESS      OpenSession successfully opened a new session.
425*4882a593Smuzhiyun  * @return TEEC_Result       Something failed.
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  */
428*4882a593Smuzhiyun TEEC_Result TEEC_OpenSession(TEEC_Context *context,
429*4882a593Smuzhiyun 			     TEEC_Session *session,
430*4882a593Smuzhiyun 			     const TEEC_UUID *destination,
431*4882a593Smuzhiyun 			     uint32_t connectionMethod,
432*4882a593Smuzhiyun 			     const void *connectionData,
433*4882a593Smuzhiyun 			     TEEC_Operation *operation,
434*4882a593Smuzhiyun 			     uint32_t *returnOrigin);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun /**
437*4882a593Smuzhiyun  * TEEC_CloseSession() - Closes the session which has been opened with the
438*4882a593Smuzhiyun  * specific trusted application.
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * @param session The opened session to close.
441*4882a593Smuzhiyun  */
442*4882a593Smuzhiyun void TEEC_CloseSession(TEEC_Session *session);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun /**
445*4882a593Smuzhiyun  * TEEC_InvokeCommand() - Executes a command in the specified trusted
446*4882a593Smuzhiyun  * application.
447*4882a593Smuzhiyun  *
448*4882a593Smuzhiyun  * @param session        A handle to an open connection to the trusted
449*4882a593Smuzhiyun  *                       application.
450*4882a593Smuzhiyun  * @param commandID      Identifier of the command in the trusted application
451*4882a593Smuzhiyun  *                       to invoke.
452*4882a593Smuzhiyun  * @param operation      An operation structure to use in the invoke command.
453*4882a593Smuzhiyun  *                       May be set to NULL to signify no operation structure
454*4882a593Smuzhiyun  *                       needed.
455*4882a593Smuzhiyun  * @param returnOrigin   A parameter which will hold the error origin if this
456*4882a593Smuzhiyun  *                       function returns any value other than TEEC_SUCCESS.
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * @return TEEC_SUCCESS  OpenSession successfully opened a new session.
459*4882a593Smuzhiyun  * @return TEEC_Result   Something failed.
460*4882a593Smuzhiyun  */
461*4882a593Smuzhiyun TEEC_Result TEEC_InvokeCommand(TEEC_Session *session,
462*4882a593Smuzhiyun 			       uint32_t commandID,
463*4882a593Smuzhiyun 			       TEEC_Operation *operation,
464*4882a593Smuzhiyun 			       uint32_t *returnOrigin);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun  * TEEC_RegisterSharedMemory() - Register a block of existing memory as a
468*4882a593Smuzhiyun  * shared block within the scope of the specified context.
469*4882a593Smuzhiyun  *
470*4882a593Smuzhiyun  * @param context    The initialized TEE context structure in which scope to
471*4882a593Smuzhiyun  *                   open the session.
472*4882a593Smuzhiyun  * @param sharedMem  pointer to the shared memory structure to register.
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * @return TEEC_SUCCESS              The registration was successful.
475*4882a593Smuzhiyun  * @return TEEC_ERROR_OUT_OF_MEMORY  Memory exhaustion.
476*4882a593Smuzhiyun  * @return TEEC_Result               Something failed.
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
479*4882a593Smuzhiyun 				      TEEC_SharedMemory *sharedMem);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun /**
482*4882a593Smuzhiyun  * TEEC_AllocateSharedMemory() - Allocate shared memory for TEE.
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * @param context     The initialized TEE context structure in which scope to
485*4882a593Smuzhiyun  *                    open the session.
486*4882a593Smuzhiyun  * @param sharedMem   Pointer to the allocated shared memory.
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * @return TEEC_SUCCESS              The registration was successful.
489*4882a593Smuzhiyun  * @return TEEC_ERROR_OUT_OF_MEMORY  Memory exhaustion.
490*4882a593Smuzhiyun  * @return TEEC_Result               Something failed.
491*4882a593Smuzhiyun  */
492*4882a593Smuzhiyun TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
493*4882a593Smuzhiyun 				      TEEC_SharedMemory *sharedMem);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /**
496*4882a593Smuzhiyun  * TEEC_ReleaseSharedMemory() - Free or deregister the shared memory.
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  * @param sharedMem  Pointer to the shared memory to be freed.
499*4882a593Smuzhiyun  */
500*4882a593Smuzhiyun void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMemory);
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun /**
503*4882a593Smuzhiyun  * TEEC_RequestCancellation() - Request the cancellation of a pending open
504*4882a593Smuzhiyun  *                              session or command invocation.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * @param operation Pointer to an operation previously passed to open session
507*4882a593Smuzhiyun  *                  or invoke.
508*4882a593Smuzhiyun  */
509*4882a593Smuzhiyun void TEEC_RequestCancellation(TEEC_Operation *operation);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun  * Register a pre-allocated Trusted Application This is mainly intended for
513*4882a593Smuzhiyun  * OS-FREE contexts or when a filesystem is not available.
514*4882a593Smuzhiyun  *
515*4882a593Smuzhiyun  * @param ta   Pointer to the trusted application binary
516*4882a593Smuzhiyun  * @param size The size of the TA binary
517*4882a593Smuzhiyun  *
518*4882a593Smuzhiyun  * @return TEEC_SUCCESS if successful.
519*4882a593Smuzhiyun  * @return TEEC_Result something failed.
520*4882a593Smuzhiyun  */
521*4882a593Smuzhiyun TEEC_Result TEEC_RegisterTA(const void *ta, const size_t size);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun /**
524*4882a593Smuzhiyun  * Unregister a pre-allocated Trusted Application This is mainly intended for
525*4882a593Smuzhiyun  * OS-FREE contexts or when a filesystem is not available.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * @param ta Pointer to the trusted application binary
528*4882a593Smuzhiyun  */
529*4882a593Smuzhiyun void TEEC_UnregisterTA(const void *ta);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun #endif
532