xref: /optee_os/lib/libutee/tee_api.c (revision c15e5835925664519a7fbd735657df2684334314)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <tee_api.h>
31 #include <utee_syscalls.h>
32 #include <user_ta_header.h>
33 #include "tee_user_mem.h"
34 
35 static void *tee_api_instance_data;
36 
37 /* System API - Misc */
38 
39 void __noreturn TEE_Panic(TEE_Result panicCode)
40 {
41 	utee_panic(panicCode);
42 }
43 
44 /* System API - Internal Client API */
45 
46 TEE_Result TEE_OpenTASession(const TEE_UUID *destination,
47 			     uint32_t cancellationRequestTimeout,
48 			     uint32_t paramTypes, TEE_Param params[4],
49 			     TEE_TASessionHandle *session,
50 			     uint32_t *returnOrigin)
51 {
52 	TEE_Result res;
53 
54 	res = utee_open_ta_session(destination, cancellationRequestTimeout,
55 				   paramTypes, params, session, returnOrigin);
56 	/*
57 	 * Specification says that *session must hold TEE_HANDLE_NULL is
58 	 * TEE_SUCCESS isn't returned. Set it here explicitly in case
59 	 * the syscall fails before out parameters has been updated.
60 	 */
61 	if (res != TEE_SUCCESS)
62 		*session = TEE_HANDLE_NULL;
63 
64 	return res;
65 }
66 
67 void TEE_CloseTASession(TEE_TASessionHandle session)
68 {
69 	if (session != TEE_HANDLE_NULL) {
70 		TEE_Result res = utee_close_ta_session(session);
71 		if (res != TEE_SUCCESS)
72 			TEE_Panic(res);
73 	}
74 }
75 
76 TEE_Result TEE_InvokeTACommand(TEE_TASessionHandle session,
77 			       uint32_t cancellationRequestTimeout,
78 			       uint32_t commandID, uint32_t paramTypes,
79 			       TEE_Param params[4], uint32_t *returnOrigin)
80 {
81 	TEE_Result res;
82 
83 	res = utee_invoke_ta_command(session, cancellationRequestTimeout,
84 				      commandID, paramTypes, params,
85 				      returnOrigin);
86 	if (res != TEE_SUCCESS &&
87 	    res != TEE_ERROR_OUT_OF_MEMORY &&
88 	    res != TEE_ERROR_TARGET_DEAD)
89 		TEE_Panic(res);
90 
91 	return res;
92 }
93 
94 /* System API - Cancellations */
95 
96 bool TEE_GetCancellationFlag(void)
97 {
98 	bool c;
99 	TEE_Result res = utee_get_cancellation_flag(&c);
100 	if (res != TEE_SUCCESS)
101 		c = false;
102 	return c;
103 }
104 
105 bool TEE_UnmaskCancellation(void)
106 {
107 	bool old_mask;
108 	TEE_Result res = utee_unmask_cancellation(&old_mask);
109 
110 	if (res != TEE_SUCCESS)
111 		TEE_Panic(res);
112 	return old_mask;
113 }
114 
115 bool TEE_MaskCancellation(void)
116 {
117 	bool old_mask;
118 	TEE_Result res = utee_mask_cancellation(&old_mask);
119 
120 	if (res != TEE_SUCCESS)
121 		TEE_Panic(res);
122 	return old_mask;
123 }
124 
125 /* System API - Memory Management */
126 
127 TEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, void *buffer,
128 				       uint32_t size)
129 {
130 	TEE_Result res;
131 
132 	if (size == 0)
133 		return TEE_SUCCESS;
134 
135 	/* Check access rights against memory mapping */
136 	res = utee_check_access_rights(accessFlags, buffer, size);
137 	if (res != TEE_SUCCESS)
138 		goto out;
139 
140 	/*
141 	* Check access rights against input parameters
142 	* Previous legacy code was removed and will need to be restored
143 	*/
144 
145 	res = TEE_SUCCESS;
146 out:
147 	return res;
148 }
149 
150 void TEE_SetInstanceData(void *instanceData)
151 {
152 	tee_api_instance_data = instanceData;
153 }
154 
155 void *TEE_GetInstanceData(void)
156 {
157 	return tee_api_instance_data;
158 }
159 
160 void *TEE_MemMove(void *dest, const void *src, uint32_t size)
161 {
162 	return memmove(dest, src, size);
163 }
164 
165 int32_t TEE_MemCompare(const void *buffer1, const void *buffer2, uint32_t size)
166 {
167 	return memcmp(buffer1, buffer2, size);
168 }
169 
170 void *TEE_MemFill(void *buff, uint32_t x, uint32_t size)
171 {
172 	return memset(buff, x, size);
173 }
174 
175 /* Date & Time API */
176 
177 void TEE_GetSystemTime(TEE_Time *time)
178 {
179 	TEE_Result res = utee_get_time(UTEE_TIME_CAT_SYSTEM, time);
180 
181 	if (res != TEE_SUCCESS)
182 		TEE_Panic(0);
183 }
184 
185 TEE_Result TEE_Wait(uint32_t timeout)
186 {
187 	TEE_Result res = utee_wait(timeout);
188 
189 	if (res != TEE_SUCCESS && res != TEE_ERROR_CANCEL)
190 		TEE_Panic(res);
191 
192 	return res;
193 }
194 
195 TEE_Result TEE_GetTAPersistentTime(TEE_Time *time)
196 {
197 	return utee_get_time(UTEE_TIME_CAT_TA_PERSISTENT, time);
198 }
199 
200 TEE_Result TEE_SetTAPersistentTime(const TEE_Time *time)
201 {
202 	return utee_set_ta_time(time);
203 }
204 
205 void TEE_GetREETime(TEE_Time *time)
206 {
207 	TEE_Result res = utee_get_time(UTEE_TIME_CAT_REE, time);
208 
209 	if (res != TEE_SUCCESS)
210 		TEE_Panic(0);
211 }
212 
213 void *TEE_Malloc(uint32_t len, uint32_t hint)
214 {
215 	return tee_user_mem_alloc(len, hint);
216 }
217 
218 void *TEE_Realloc(void *buffer, uint32_t newSize)
219 {
220 	/*
221 	 * GP TEE Internal API specifies newSize as 'uint32_t'.
222 	 * use unsigned 'size_t' type. it is at least 32bit!
223 	 */
224 	return tee_user_mem_realloc(buffer, (size_t) newSize);
225 }
226 
227 void TEE_Free(void *buffer)
228 {
229 	tee_user_mem_free(buffer);
230 }
231 
232 /* Cache maintenance support (TA requires the CACHE_MAINTENANCE property) */
233 TEE_Result TEE_CacheClean(char *buf, size_t len)
234 {
235 	return utee_cache_operation(buf, len, TEE_CACHECLEAN);
236 }
237 TEE_Result TEE_CacheFlush(char *buf, size_t len)
238 {
239 	return utee_cache_operation(buf, len, TEE_CACHEFLUSH);
240 }
241 
242 TEE_Result TEE_CacheInvalidate(char *buf, size_t len)
243 {
244 	return utee_cache_operation(buf, len, TEE_CACHEINVALIDATE);
245 }
246