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