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