1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #ifndef TEE_CLIENT_TYPE_H
14 #define TEE_CLIENT_TYPE_H
15 /**
16  * @addtogroup TeeClient
17  * @{
18  *
19  * @brief Provides APIs for the client applications (CAs) in the Rich Execution Environment (normal mode) to
20  * access the trusted applications (TAs) in a Trusted Execution Environment (TEE).
21  *
22  * @syscap SystemCapability.Tee.TeeClient
23  * @since 9
24  * @version 1.0
25  */
26 
27 /**
28  * @file tee_client_type.h
29  *
30  * @brief Defines basic data types and data structures.
31  *
32  * @since 9
33  * @version 1.0
34  */
35 
36 #include <semaphore.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include "tee_client_constants.h"
42 
43 /**
44  * @brief Defines the linked list type.
45  *
46  * @since 9
47  * @version 1.0
48  */
49 struct ListNode {
50     struct ListNode *next;
51     struct ListNode *prev;
52 };
53 
54 /**
55  * @brief Defines the return values.
56  *
57  * @since 9
58  * @version 1.0
59  */
60 typedef enum TEEC_ReturnCode TEEC_Result;
61 
62 /**
63  * @brief Defines the universally unique identifier (UUID) as defined in RFC4122 [2]. The UUIDs are used to identify TAs.
64  *
65  * @since 9
66  * @version 1.0
67  */
68 typedef struct {
69     uint32_t timeLow;
70     uint16_t timeMid;
71     uint16_t timeHiAndVersion;
72     uint8_t clockSeqAndNode[8];
73 } TEEC_UUID;
74 
75 /**
76  * @brief Defines the context, a logical connection between a CA and a TEE.
77  *
78  * @since 9
79  * @version 1.0
80  */
81 typedef struct {
82     int32_t fd;
83     uint8_t *ta_path;
84     struct ListNode session_list;
85     struct ListNode shrd_mem_list;
86     union {
87         struct {
88             void *buffer;
89             sem_t buffer_barrier;
90         } share_buffer;
91         uint64_t imp;
92     };
93 } TEEC_Context;
94 
95 /**
96  * @brief Defines the session between a CA and a TA.
97  *
98  * @since 9
99  * @version 1.0
100  */
101 typedef struct {
102     uint32_t session_id;
103     TEEC_UUID service_id;
104     uint32_t ops_cnt;
105     union {
106         struct ListNode head;
107         uint64_t imp;
108     };
109     TEEC_Context *context;
110 } TEEC_Session;
111 
112 /**
113  * @brief Defines a shared memory block, which can be registered or allocated.
114  *
115  * @since 9
116  * @version 1.0
117  */
118 typedef struct {
119     void *buffer;
120     uint32_t size;
121     uint32_t flags;
122     uint32_t ops_cnt;
123     bool is_allocated;
124     union {
125         struct ListNode head;
126         void* imp;
127     };
128     TEEC_Context *context;
129 } TEEC_SharedMemory;
130 
131 /**
132  * @brief Defines a pointer to a temporary buffer.
133  *
134  * @since 9
135  * @version 1.0
136  */
137 typedef struct {
138     void *buffer;
139     uint32_t size;
140 } TEEC_TempMemoryReference;
141 
142 /**
143  * @brief Defines a pointer to the shared memory that is registered or allocated.
144  *
145  * @since 9
146  * @version 1.0
147  */
148 typedef struct {
149     TEEC_SharedMemory *parent;
150     uint32_t size;
151     uint32_t offset;
152 } TEEC_RegisteredMemoryReference;
153 
154 /**
155  * @brief Describes a parameter that carries small raw data passed by <b>value</b>.
156  *
157  * @since 9
158  * @version 1.0
159  */
160 typedef struct {
161     uint32_t a;
162     uint32_t b;
163 } TEEC_Value;
164 
165 /**
166  * @brief Describes the size and handle of the ION memory.
167  *
168  * @since 9
169  * @version 1.0
170  */
171 typedef struct {
172     int ionShareFd;
173     uint32_t ionSize;
174 } TEEC_IonReference;
175 
176 /**
177  * @brief Defines a parameter of {@code TEEC_Operation}.
178  *
179  * @since 9
180  * @version 1.0
181  */
182 typedef union {
183     TEEC_TempMemoryReference tmpref;
184     TEEC_RegisteredMemoryReference memref;
185     TEEC_Value value;
186     TEEC_IonReference ionref;
187 } TEEC_Parameter;
188 
189 /**
190  * @brief Defines the parameters for opening a session or sending a command.
191  *
192  * @since 9
193  * @version 1.0
194  */
195 typedef struct {
196     /** The value 0 means to cancel the command, and other values mean to execute the command. */
197     uint32_t started;
198     /** Use {@code TEEC_PARAM_TYPES} to create this parameter. */
199     uint32_t paramTypes;
200     TEEC_Parameter params[TEEC_PARAM_NUM];
201     TEEC_Session *session;
202     bool cancel_flag;
203 } TEEC_Operation;
204 
205 /** @} */
206 #endif
207