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