summaryrefslogtreecommitdiff
path: root/lib/optee_clientApi/OpteeClientApiLib.c
blob: a2f8c12ab434df1c0785887ab4d770c81cad061b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright 2017, Rockchip Electronics Co., Ltd
 * hisping lin, <hisping.lin@rock-chips.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */
#include <common.h>
#include <optee_include/OpteeClientApiLib.h>
#include <optee_include/OpteeClientMem.h>
#include <optee_include/OpteeClientSMC.h>
#include <optee_include/OpteeClientRkFs.h>

/*
 * Initlialize the library
 */
TEEC_Result OpteeClientApiLibInitialize(void)
{
	TEEC_Result status = TEEC_SUCCESS;

	OpteeClientMemInit();

	OpteeClientRkFsInit();

	return status;
}

/*
 * This function initializes a new TEE Context, connecting this Client
 * application to the TEE indentified by the name name.
 *
 * name == NULL will give the default TEE.
 *
 * In this implementation only the default name is supported.
 * If name != NULL then TEEC_ERROR_ITEM_NOT_FOUND is returned.
 */
TEEC_Result TEEC_InitializeContext(const char *name,
				TEEC_Context *context)
{
	TEEC_Result teecresult = TEEC_SUCCESS;

	debug("TEEC_InitializeContext Enter: name=%s  context=%s  0x%X\n",
			name, context->devname, context->fd);

	if (context == NULL) {
		teecresult = TEEC_ERROR_BAD_PARAMETERS;
		goto exit;
	}

	if (name != NULL) {
		teecresult = TEEC_ERROR_ITEM_NOT_FOUND;
		goto exit;
	}

	memset(context, 0, sizeof(*context));

exit:
	debug("TEEC_InitializeContext Exit : teecresult=0x%X\n\n", teecresult);
	return teecresult;
}

/*
 * This function destroys an initialized TEE Context, closing the connection
 * between the Client and the TEE.
 * The function implementation MUST do nothing if context is NULL
 *
 * There is nothing to do here since there is no context state.
 */
TEEC_Result TEEC_FinalizeContext(TEEC_Context *context)
{
	debug("TEEC_FinalizeContext Enter-Exit: context=0x%zu\n",
		(size_t)context);
	return TEEC_SUCCESS;
}

/*
 * Allocates or registers shared memory.
 *
 * Since EDK2 is configured flat with virtual memory == physical memory
 * then we don't need to perform any special operations to get physical
 * contiguous memory.
 */
TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
			TEEC_SharedMemory *shared_memory)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;

	debug("TEEC_AllocateSharedMemory Enter: context=%s 0x%X, shared_memory=0x%zu\n",
		context->devname, context->fd, shared_memory->size);

	if ((context == NULL) || (shared_memory == NULL)) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	if (shared_memory->flags != 0) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	shared_memory->buffer = NULL;
	shared_memory->alloc_buffer = 0;

	debug("TEEC_AllocateSharedMemory: size=0x%zu, flags=0x%X\n",
			shared_memory->size, shared_memory->flags);

	shared_memory->buffer = OpteeClientMemAlloc(shared_memory->size);
	if (shared_memory->buffer == NULL) {
		TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
		goto Exit;
	}

	shared_memory->alloc_buffer = shared_memory->buffer;

Exit:
	debug("TEEC_AllocateSharedMemory Exit : TeecResult=0x%X\n", TeecResult);
	return TeecResult;
}

/*
 * Releases shared memory.
 *
 * The optee_client implementation allows this to be called with a null pointer
 * and null buffer but we'll assert this is not the case for better debugging.
 */
void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *shared_memory)
{
	debug("TEEC_ReleaseSharedMemory Enter: shared_memory=0x%zu\n",
				shared_memory->size);

	if (shared_memory == NULL)
		goto Exit;

	if (shared_memory->buffer == NULL)
		goto Exit;

	if (shared_memory->alloc_buffer != 0) {
		OpteeClientMemFree(shared_memory->alloc_buffer);
		shared_memory->alloc_buffer = 0;
	}

	shared_memory->buffer = NULL;
	shared_memory->size = 0;

Exit:
	return;
}

/*
 * Register shared memory
 *
 * If the supplied buffer is compatible we can use it as supplied otherwise
 * we'll need to allocate a copy buffer for the transfer instead.
 */
TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
			TEEC_SharedMemory *shared_memory)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;

	if ((context == NULL) || (shared_memory == NULL)) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	if (shared_memory->buffer == NULL) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	shared_memory->alloc_buffer = 0;

	phys_addr_t start = (phys_addr_t) shared_memory->buffer;

	if ((start % 4096) != 0) {
		TEEC_SharedMemory TempSharedMemory;
		TempSharedMemory.size  = shared_memory->size;
		TempSharedMemory.flags = shared_memory->flags;

		TeecResult = TEEC_AllocateSharedMemory
			(context, &TempSharedMemory);

		if (TeecResult != TEEC_SUCCESS)
			goto Exit;

		shared_memory->alloc_buffer = TempSharedMemory.alloc_buffer;
	}

Exit:
	debug("TEEC_RegisterSharedMemory Exit : TeecResult=0x%X\n", TeecResult);
	return TeecResult;
}

/*
 * This function opens a new Session between the Client application and the
 * specified TEE application.
 *
 * Only connection_method == TEEC_LOGIN_PUBLIC is supported connection_data and
 * operation shall be set to NULL.
 */
TEEC_Result TEEC_OpenSession(TEEC_Context *context,
			TEEC_Session *session,
			const TEEC_UUID *destination,
			uint32_t connection_method,
			const void *connection_data,
			TEEC_Operation *operation,
			uint32_t *error_origin)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;
	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;

	debug("TEEC_OpenSession: session=0x%X, ...\n", session->id);

	if ((context == NULL) || (session == NULL) || (destination == NULL)) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	if (connection_method != TEEC_LOGIN_PUBLIC) {
		TeecResult = TEEC_ERROR_NOT_SUPPORTED;
		goto Exit;
	}

	TEEC_Operation TeecNullOperation = {0};
	TEEC_Operation *TeecOperation;

	if (operation == NULL) {
		memset(&TeecNullOperation, 0, sizeof(TEEC_Operation));
		TeecOperation = &TeecNullOperation;
	} else {
		TeecOperation = operation;
	}

	TeecResult = TEEC_SMC_OpenSession(context, session, destination,
				TeecOperation, &TeecErrorOrigin);

Exit:
	if (error_origin != NULL)
		*error_origin = TeecErrorOrigin;

	debug("TEEC_OpenSession Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n\n",
				TeecResult, TeecErrorOrigin);
	return TeecResult;
}

/*
 * This function closes a session which has been opened with a TEE
 * application.
 */
void TEEC_CloseSession(TEEC_Session *session)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;
	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;

	debug("TEEC_CloseSession Enter: session=0x%X\n", session->id);

	if (session == NULL)
		goto Exit;

	TeecResult = TEEC_SMC_CloseSession(session, &TeecErrorOrigin);

Exit:
	debug("TEEC_CloseSession Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n\n",
			TeecResult, TeecErrorOrigin);
	return;
}

/*
 * Invokes a TEE command (secure service, sub-PA or whatever).
 */
TEEC_Result TEEC_InvokeCommand(TEEC_Session *session,
				uint32_t cmd_id,
				TEEC_Operation *operation,
				uint32_t *error_origin)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;
	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;

	debug("TEEC_InvokeCommand Enter: session=0x%X, cmd_id=0x%X\n",
			session->id, cmd_id);

	if (session == NULL) {
		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
		goto Exit;
	}

	TEEC_Operation TeecNullOperation = {0};
	TEEC_Operation *TeecOperation;

	if (operation == NULL)
		TeecOperation = &TeecNullOperation;
	else
		TeecOperation = operation;

	TeecResult = TEEC_SMC_InvokeCommand(session, cmd_id,
			TeecOperation, &TeecErrorOrigin);

Exit:
	if (error_origin != NULL)
		*error_origin = TeecErrorOrigin;

	debug("TEEC_InvokeCommand Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n\n",
				TeecResult, TeecErrorOrigin);

	return TeecResult;
}

/*
 * Request a cancellation of a in-progress operation (best effort)
 */
void TEEC_RequestCancellation(TEEC_Operation *operation)
{
	TEEC_Result TeecResult = TEEC_SUCCESS;
	uint32_t TeecErrorOrigin = TEEC_ORIGIN_API;

	if (operation == NULL)
		goto Exit;

	TeecResult = TEEC_SMC_RequestCancellation(operation, &TeecErrorOrigin);

Exit:
	debug("TEEC_RequestCancellation Exit : TeecResult=0x%X, TeecErrorOrigin=0x%X\n",
			TeecResult, TeecErrorOrigin);

	return;
}