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