xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientApiLib.c (revision 74eb6027432600de60ed1c8bf892f1f8243c2c8a)
1abdd2437Shisping /*
2abdd2437Shisping  * Copyright 2017, Rockchip Electronics Co., Ltd
3abdd2437Shisping  * hisping lin, <hisping.lin@rock-chips.com>
4abdd2437Shisping  *
5abdd2437Shisping  * SPDX-License-Identifier:	GPL-2.0+
6abdd2437Shisping  */
7abdd2437Shisping #include <common.h>
8abdd2437Shisping #include <optee_include/OpteeClientApiLib.h>
9abdd2437Shisping #include <optee_include/OpteeClientMem.h>
10*74eb6027SHisping Lin #include <optee_include/OpteeClientRPC.h>
11abdd2437Shisping #include <optee_include/OpteeClientSMC.h>
121f25ada2SHisping Lin #include <optee_include/OpteeClientRkFs.h>
13*74eb6027SHisping Lin #include <optee_include/teesmc.h>
14*74eb6027SHisping Lin #include <optee_include/teesmc_optee.h>
15*74eb6027SHisping Lin #include <optee_include/teesmc_v2.h>
16*74eb6027SHisping Lin 
17*74eb6027SHisping Lin #define OPTEE_MSG_REVISION_MAJOR        2
18*74eb6027SHisping Lin #define OPTEE_MSG_REVISION_MINOR        0
19*74eb6027SHisping Lin 
20*74eb6027SHisping Lin static bool optee_api_revision_is_compatible(void)
21*74eb6027SHisping Lin {
22*74eb6027SHisping Lin 	ARM_SMC_ARGS ArmSmcArgs = {0};
23*74eb6027SHisping Lin 
24*74eb6027SHisping Lin 	ArmSmcArgs.Arg0 = OPTEE_SMC_CALLS_REVISION;
25*74eb6027SHisping Lin 
26*74eb6027SHisping Lin 	tee_smc_call(&ArmSmcArgs);
27*74eb6027SHisping Lin 
28*74eb6027SHisping Lin 	if (ArmSmcArgs.Arg0 == OPTEE_MSG_REVISION_MAJOR &&
29*74eb6027SHisping Lin 	    ArmSmcArgs.Arg1 >= OPTEE_MSG_REVISION_MINOR) {
30*74eb6027SHisping Lin 		printf("optee api revision: %d.%d\n",
31*74eb6027SHisping Lin 		       ArmSmcArgs.Arg0, ArmSmcArgs.Arg1);
32*74eb6027SHisping Lin 		return true;
33*74eb6027SHisping Lin 	} else {
34*74eb6027SHisping Lin 		printf("optee check api revision fail: %d.%d\n",
35*74eb6027SHisping Lin 		       ArmSmcArgs.Arg0, ArmSmcArgs.Arg1);
36*74eb6027SHisping Lin 		return false;
37*74eb6027SHisping Lin 	}
38*74eb6027SHisping Lin }
39abdd2437Shisping 
40abdd2437Shisping /*
41abdd2437Shisping  * Initlialize the library
42abdd2437Shisping  */
43abdd2437Shisping TEEC_Result OpteeClientApiLibInitialize(void)
44abdd2437Shisping {
45abdd2437Shisping 	TEEC_Result status = TEEC_SUCCESS;
46abdd2437Shisping 
47*74eb6027SHisping Lin 	/* check api revision compatibility */
48*74eb6027SHisping Lin 	if (!optee_api_revision_is_compatible())
49*74eb6027SHisping Lin 		panic("optee api revision is too low");
50*74eb6027SHisping Lin 
51f4e1db95SHisping Lin 	status = OpteeClientMemInit();
52f4e1db95SHisping Lin 	if (status != TEEC_SUCCESS) {
53efb93541SHisping Lin 		printf("TEEC: OpteeClientMemInit fail!\n");
54abdd2437Shisping 		return status;
55abdd2437Shisping 	}
56f4e1db95SHisping Lin 	status = OpteeClientRkFsInit();
57f4e1db95SHisping Lin 	if (status != TEEC_SUCCESS) {
58efb93541SHisping Lin 		printf("TEEC: OpteeClientRkFsInit fail!\n");
59f4e1db95SHisping Lin 		return status;
60f4e1db95SHisping Lin 	}
61f4e1db95SHisping Lin 
62f4e1db95SHisping Lin 	return TEEC_SUCCESS;
63f4e1db95SHisping Lin }
64abdd2437Shisping 
65abdd2437Shisping /*
66abdd2437Shisping  * This function initializes a new TEE Context, connecting this Client
67abdd2437Shisping  * application to the TEE indentified by the name name.
68abdd2437Shisping  *
69abdd2437Shisping  * name == NULL will give the default TEE.
70abdd2437Shisping  *
71abdd2437Shisping  * In this implementation only the default name is supported.
72abdd2437Shisping  * If name != NULL then TEEC_ERROR_ITEM_NOT_FOUND is returned.
73abdd2437Shisping  */
74abdd2437Shisping TEEC_Result TEEC_InitializeContext(const char *name,
75abdd2437Shisping 				TEEC_Context *context)
76abdd2437Shisping {
77abdd2437Shisping 	TEEC_Result teecresult = TEEC_SUCCESS;
78abdd2437Shisping 
79abdd2437Shisping 	debug("TEEC_InitializeContext Enter: name=%s  context=%s  0x%X\n",
80abdd2437Shisping 			name, context->devname, context->fd);
81abdd2437Shisping 
82abdd2437Shisping 	if (context == NULL) {
83abdd2437Shisping 		teecresult = TEEC_ERROR_BAD_PARAMETERS;
84abdd2437Shisping 		goto exit;
85abdd2437Shisping 	}
86abdd2437Shisping 
87abdd2437Shisping 	if (name != NULL) {
88abdd2437Shisping 		teecresult = TEEC_ERROR_ITEM_NOT_FOUND;
89abdd2437Shisping 		goto exit;
90abdd2437Shisping 	}
91abdd2437Shisping 
92abdd2437Shisping 	memset(context, 0, sizeof(*context));
93abdd2437Shisping 
94abdd2437Shisping exit:
95efb93541SHisping Lin 	debug("TEEC_InitializeContext Exit : teecresult=0x%X\n", teecresult);
96abdd2437Shisping 	return teecresult;
97abdd2437Shisping }
98abdd2437Shisping 
99abdd2437Shisping /*
100abdd2437Shisping  * This function destroys an initialized TEE Context, closing the connection
101abdd2437Shisping  * between the Client and the TEE.
102abdd2437Shisping  * The function implementation MUST do nothing if context is NULL
103abdd2437Shisping  *
104abdd2437Shisping  * There is nothing to do here since there is no context state.
105abdd2437Shisping  */
106abdd2437Shisping TEEC_Result TEEC_FinalizeContext(TEEC_Context *context)
107abdd2437Shisping {
1081f25ada2SHisping Lin 	debug("TEEC_FinalizeContext Enter-Exit: context=0x%zu\n",
1091f25ada2SHisping Lin 		(size_t)context);
110abdd2437Shisping 	return TEEC_SUCCESS;
111abdd2437Shisping }
112abdd2437Shisping 
113abdd2437Shisping /*
114abdd2437Shisping  * Allocates or registers shared memory.
115abdd2437Shisping  *
116abdd2437Shisping  * Since EDK2 is configured flat with virtual memory == physical memory
117abdd2437Shisping  * then we don't need to perform any special operations to get physical
118abdd2437Shisping  * contiguous memory.
119abdd2437Shisping  */
120abdd2437Shisping TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
121abdd2437Shisping 			TEEC_SharedMemory *shared_memory)
122abdd2437Shisping {
123abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
124abdd2437Shisping 
1251f25ada2SHisping Lin 	debug("TEEC_AllocateSharedMemory Enter: context=%s 0x%X, shared_memory=0x%zu\n",
126abdd2437Shisping 		context->devname, context->fd, shared_memory->size);
127abdd2437Shisping 
128abdd2437Shisping 	if ((context == NULL) || (shared_memory == NULL)) {
129abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
130abdd2437Shisping 		goto Exit;
131abdd2437Shisping 	}
132abdd2437Shisping 
133abdd2437Shisping 	if (shared_memory->flags != 0) {
134abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
135abdd2437Shisping 		goto Exit;
136abdd2437Shisping 	}
137abdd2437Shisping 
138abdd2437Shisping 	shared_memory->buffer = NULL;
139abdd2437Shisping 	shared_memory->alloc_buffer = 0;
140abdd2437Shisping 
1411f25ada2SHisping Lin 	debug("TEEC_AllocateSharedMemory: size=0x%zu, flags=0x%X\n",
142abdd2437Shisping 			shared_memory->size, shared_memory->flags);
143abdd2437Shisping 
144abdd2437Shisping 	shared_memory->buffer = OpteeClientMemAlloc(shared_memory->size);
145abdd2437Shisping 	if (shared_memory->buffer == NULL) {
146abdd2437Shisping 		TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
147abdd2437Shisping 		goto Exit;
148abdd2437Shisping 	}
149abdd2437Shisping 
150abdd2437Shisping 	shared_memory->alloc_buffer = shared_memory->buffer;
151abdd2437Shisping 
152abdd2437Shisping Exit:
153abdd2437Shisping 	debug("TEEC_AllocateSharedMemory Exit : TeecResult=0x%X\n", TeecResult);
154abdd2437Shisping 	return TeecResult;
155abdd2437Shisping }
156abdd2437Shisping 
157abdd2437Shisping /*
158abdd2437Shisping  * Releases shared memory.
159abdd2437Shisping  *
160abdd2437Shisping  * The optee_client implementation allows this to be called with a null pointer
161abdd2437Shisping  * and null buffer but we'll assert this is not the case for better debugging.
162abdd2437Shisping  */
163abdd2437Shisping void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *shared_memory)
164abdd2437Shisping {
1651f25ada2SHisping Lin 	debug("TEEC_ReleaseSharedMemory Enter: shared_memory=0x%zu\n",
166abdd2437Shisping 				shared_memory->size);
167abdd2437Shisping 
168abdd2437Shisping 	if (shared_memory == NULL)
169abdd2437Shisping 		goto Exit;
170abdd2437Shisping 
171abdd2437Shisping 	if (shared_memory->buffer == NULL)
172abdd2437Shisping 		goto Exit;
173abdd2437Shisping 
174abdd2437Shisping 	if (shared_memory->alloc_buffer != 0) {
175abdd2437Shisping 		OpteeClientMemFree(shared_memory->alloc_buffer);
176abdd2437Shisping 		shared_memory->alloc_buffer = 0;
177abdd2437Shisping 	}
178abdd2437Shisping 
179abdd2437Shisping 	shared_memory->buffer = NULL;
180abdd2437Shisping 	shared_memory->size = 0;
181abdd2437Shisping 
182abdd2437Shisping Exit:
183abdd2437Shisping 	return;
184abdd2437Shisping }
185abdd2437Shisping 
186abdd2437Shisping /*
187abdd2437Shisping  * Register shared memory
188abdd2437Shisping  *
189abdd2437Shisping  * If the supplied buffer is compatible we can use it as supplied otherwise
190abdd2437Shisping  * we'll need to allocate a copy buffer for the transfer instead.
191abdd2437Shisping  */
192abdd2437Shisping TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
193abdd2437Shisping 			TEEC_SharedMemory *shared_memory)
194abdd2437Shisping {
195abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
196abdd2437Shisping 
197abdd2437Shisping 	if ((context == NULL) || (shared_memory == NULL)) {
198abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
199abdd2437Shisping 		goto Exit;
200abdd2437Shisping 	}
201abdd2437Shisping 
202abdd2437Shisping 	if (shared_memory->buffer == NULL) {
203abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
204abdd2437Shisping 		goto Exit;
205abdd2437Shisping 	}
206abdd2437Shisping 
207abdd2437Shisping 	shared_memory->alloc_buffer = 0;
208abdd2437Shisping 
209abdd2437Shisping 	phys_addr_t start = (phys_addr_t) shared_memory->buffer;
210abdd2437Shisping 
211abdd2437Shisping 	if ((start % 4096) != 0) {
212abdd2437Shisping 		TEEC_SharedMemory TempSharedMemory;
213abdd2437Shisping 		TempSharedMemory.size  = shared_memory->size;
214abdd2437Shisping 		TempSharedMemory.flags = shared_memory->flags;
215abdd2437Shisping 
216abdd2437Shisping 		TeecResult = TEEC_AllocateSharedMemory
217abdd2437Shisping 			(context, &TempSharedMemory);
218abdd2437Shisping 
219abdd2437Shisping 		if (TeecResult != TEEC_SUCCESS)
220abdd2437Shisping 			goto Exit;
221abdd2437Shisping 
222abdd2437Shisping 		shared_memory->alloc_buffer = TempSharedMemory.alloc_buffer;
223abdd2437Shisping 	}
224abdd2437Shisping 
225abdd2437Shisping Exit:
226abdd2437Shisping 	debug("TEEC_RegisterSharedMemory Exit : TeecResult=0x%X\n", TeecResult);
227abdd2437Shisping 	return TeecResult;
228abdd2437Shisping }
229abdd2437Shisping 
230abdd2437Shisping /*
231abdd2437Shisping  * This function opens a new Session between the Client application and the
232abdd2437Shisping  * specified TEE application.
233abdd2437Shisping  *
234abdd2437Shisping  * Only connection_method == TEEC_LOGIN_PUBLIC is supported connection_data and
235abdd2437Shisping  * operation shall be set to NULL.
236abdd2437Shisping  */
237abdd2437Shisping TEEC_Result TEEC_OpenSession(TEEC_Context *context,
238abdd2437Shisping 			TEEC_Session *session,
239abdd2437Shisping 			const TEEC_UUID *destination,
240abdd2437Shisping 			uint32_t connection_method,
241abdd2437Shisping 			const void *connection_data,
242abdd2437Shisping 			TEEC_Operation *operation,
243abdd2437Shisping 			uint32_t *error_origin)
244abdd2437Shisping {
245abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
246abdd2437Shisping 	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;
247abdd2437Shisping 
248abdd2437Shisping 	debug("TEEC_OpenSession: session=0x%X, ...\n", session->id);
249abdd2437Shisping 
250abdd2437Shisping 	if ((context == NULL) || (session == NULL) || (destination == NULL)) {
251abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
252abdd2437Shisping 		goto Exit;
253abdd2437Shisping 	}
254abdd2437Shisping 
255abdd2437Shisping 	if (connection_method != TEEC_LOGIN_PUBLIC) {
256abdd2437Shisping 		TeecResult = TEEC_ERROR_NOT_SUPPORTED;
257abdd2437Shisping 		goto Exit;
258abdd2437Shisping 	}
259abdd2437Shisping 
260abdd2437Shisping 	TEEC_Operation TeecNullOperation = {0};
261abdd2437Shisping 	TEEC_Operation *TeecOperation;
262abdd2437Shisping 
263abdd2437Shisping 	if (operation == NULL) {
264abdd2437Shisping 		memset(&TeecNullOperation, 0, sizeof(TEEC_Operation));
265abdd2437Shisping 		TeecOperation = &TeecNullOperation;
266abdd2437Shisping 	} else {
267abdd2437Shisping 		TeecOperation = operation;
268abdd2437Shisping 	}
269abdd2437Shisping 
270abdd2437Shisping 	TeecResult = TEEC_SMC_OpenSession(context, session, destination,
271abdd2437Shisping 				TeecOperation, &TeecErrorOrigin);
272abdd2437Shisping 
273abdd2437Shisping Exit:
274abdd2437Shisping 	if (error_origin != NULL)
275abdd2437Shisping 		*error_origin = TeecErrorOrigin;
276abdd2437Shisping 
277efb93541SHisping Lin 	debug("TEEC_OpenSession Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n",
278abdd2437Shisping 				TeecResult, TeecErrorOrigin);
279abdd2437Shisping 	return TeecResult;
280abdd2437Shisping }
281abdd2437Shisping 
282abdd2437Shisping /*
283abdd2437Shisping  * This function closes a session which has been opened with a TEE
284abdd2437Shisping  * application.
285abdd2437Shisping  */
286abdd2437Shisping void TEEC_CloseSession(TEEC_Session *session)
287abdd2437Shisping {
288abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
289abdd2437Shisping 	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;
290abdd2437Shisping 
291abdd2437Shisping 	debug("TEEC_CloseSession Enter: session=0x%X\n", session->id);
292abdd2437Shisping 
293abdd2437Shisping 	if (session == NULL)
294abdd2437Shisping 		goto Exit;
295abdd2437Shisping 
296abdd2437Shisping 	TeecResult = TEEC_SMC_CloseSession(session, &TeecErrorOrigin);
297abdd2437Shisping 
298abdd2437Shisping Exit:
299efb93541SHisping Lin 	debug("TEEC_CloseSession Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n",
300abdd2437Shisping 			TeecResult, TeecErrorOrigin);
301abdd2437Shisping 	return;
302abdd2437Shisping }
303abdd2437Shisping 
304abdd2437Shisping /*
305abdd2437Shisping  * Invokes a TEE command (secure service, sub-PA or whatever).
306abdd2437Shisping  */
307abdd2437Shisping TEEC_Result TEEC_InvokeCommand(TEEC_Session *session,
308abdd2437Shisping 				uint32_t cmd_id,
309abdd2437Shisping 				TEEC_Operation *operation,
310abdd2437Shisping 				uint32_t *error_origin)
311abdd2437Shisping {
312abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
313abdd2437Shisping 	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;
314abdd2437Shisping 
315abdd2437Shisping 	debug("TEEC_InvokeCommand Enter: session=0x%X, cmd_id=0x%X\n",
316abdd2437Shisping 			session->id, cmd_id);
317abdd2437Shisping 
318abdd2437Shisping 	if (session == NULL) {
319abdd2437Shisping 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
320abdd2437Shisping 		goto Exit;
321abdd2437Shisping 	}
322abdd2437Shisping 
323abdd2437Shisping 	TEEC_Operation TeecNullOperation = {0};
324abdd2437Shisping 	TEEC_Operation *TeecOperation;
325abdd2437Shisping 
326abdd2437Shisping 	if (operation == NULL)
327abdd2437Shisping 		TeecOperation = &TeecNullOperation;
328abdd2437Shisping 	else
329abdd2437Shisping 		TeecOperation = operation;
330abdd2437Shisping 
331abdd2437Shisping 	TeecResult = TEEC_SMC_InvokeCommand(session, cmd_id,
332abdd2437Shisping 			TeecOperation, &TeecErrorOrigin);
333abdd2437Shisping 
334abdd2437Shisping Exit:
335abdd2437Shisping 	if (error_origin != NULL)
336abdd2437Shisping 		*error_origin = TeecErrorOrigin;
337abdd2437Shisping 
338efb93541SHisping Lin 	debug("TEEC_InvokeCommand Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n",
339abdd2437Shisping 				TeecResult, TeecErrorOrigin);
340abdd2437Shisping 
341abdd2437Shisping 	return TeecResult;
342abdd2437Shisping }
343abdd2437Shisping 
344abdd2437Shisping /*
345abdd2437Shisping  * Request a cancellation of a in-progress operation (best effort)
346abdd2437Shisping  */
347abdd2437Shisping void TEEC_RequestCancellation(TEEC_Operation *operation)
348abdd2437Shisping {
349abdd2437Shisping 	TEEC_Result TeecResult = TEEC_SUCCESS;
350abdd2437Shisping 	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;
351abdd2437Shisping 
352abdd2437Shisping 	if (operation == NULL)
353abdd2437Shisping 		goto Exit;
354abdd2437Shisping 
355abdd2437Shisping 	TeecResult = TEEC_SMC_RequestCancellation(operation, &TeecErrorOrigin);
356abdd2437Shisping 
357abdd2437Shisping Exit:
358abdd2437Shisping 	debug("TEEC_RequestCancellation Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n",
359abdd2437Shisping 			TeecResult, TeecErrorOrigin);
360abdd2437Shisping 
361abdd2437Shisping 	return;
362abdd2437Shisping }
363