xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientRPC.c (revision 1f25ada2a8eccef6ea3d9844a40d05d7ce7027d4)
1 /*
2  * Copyright 2017, Rockchip Electronics Co., Ltd
3  * hisping lin, <hisping.lin@rock-chips.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <stdlib.h>
9 #include <command.h>
10 #include <mmc.h>
11 #include <optee_include/OpteeClientMem.h>
12 #include <optee_include/OpteeClientRPC.h>
13 #include <optee_include/teesmc.h>
14 #include <optee_include/teesmc_v2.h>
15 #include <optee_include/teesmc_optee.h>
16 #include <optee_include/tee_rpc_types.h>
17 #include <optee_include/tee_rpc.h>
18 #include <optee_include/258be795-f9ca-40e6-a8699ce6886c5d5d.h>
19 #include <optee_include/OpteeClientRkFs.h>
20 
21 /*
22  * Memory allocation.
23  * Currently treated the same for both arguments & payloads.
24  */
25 TEEC_Result OpteeRpcAlloc(uint32_t Size, uint32_t *Address)
26 {
27 	TEEC_Result TeecResult = TEEC_SUCCESS;
28 	size_t AllocAddress;
29 
30 	*Address = 0;
31 
32 	if (Size != 0) {
33 		AllocAddress = (size_t) OpteeClientMemAlloc(Size);
34 
35 		if (AllocAddress == 0)
36 			TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
37 		else
38 			*Address = AllocAddress;
39 	}
40 	return TeecResult;
41 }
42 
43 /*
44  * Memory free.
45  * Currently treated the same for both arguments & payloads.
46  */
47 TEEC_Result OpteeRpcFree(uint32_t Address)
48 {
49 	OpteeClientMemFree((void *)(size_t)Address);
50 	return TEEC_SUCCESS;
51 }
52 
53 /*
54  * Load a TA from storage into memory and provide it back to OpTEE.
55  * Param[0] = IN: struct tee_rpc_load_ta_cmd
56  * Param[1] = IN: all-zero OUT: TA Image allocated
57  */
58 TEEC_Result OpteeRpcCmdLoadTa(t_teesmc32_arg *TeeSmc32Arg)
59 {
60 	TEEC_Result TeecResult = TEEC_SUCCESS;
61 	t_teesmc32_param *TeeSmc32Param = NULL;
62 	struct tee_rpc_load_ta_cmd *TeeLoadTaCmd = NULL;
63 	uint32_t TeeLoadTaCmdSize = 0;
64 
65 	if (TeeSmc32Arg->num_params != 2) {
66 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
67 		goto Exit;
68 	}
69 
70 	TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
71 	TeeLoadTaCmd = (struct tee_rpc_load_ta_cmd *)
72 					(size_t)TeeSmc32Param[0].u.memref.buf_ptr;
73 	TeeLoadTaCmdSize = TeeSmc32Param[0].u.memref.size;
74 
75 	if ((TeeLoadTaCmd == NULL) ||
76 		(TeeLoadTaCmdSize != sizeof(*TeeLoadTaCmd))) {
77 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
78 		goto Exit;
79 	}
80 
81 	TEEC_Result Status = 0;
82 	void *ImageData = NULL;
83 	uint32_t ImageSize = 0;
84 	size_t AllocAddress = 0;
85 
86 	ImageData = (void *)keymaster_data;
87 	ImageSize = keymaster_size;
88 
89 	if (Status != 0) {
90 		TeecResult = TEEC_ERROR_ITEM_NOT_FOUND;
91 		goto Exit;
92 	}
93 
94 	AllocAddress = (size_t) OpteeClientMemAlloc(ImageSize);
95 
96 	if (AllocAddress == 0) {
97 		TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
98 		goto Exit;
99 	}
100 
101 	memcpy((void *)AllocAddress, ImageData, ImageSize);
102 
103 	debug("...TA loaded at 0x%zu of size 0x%X bytes\n",
104 		AllocAddress, ImageSize);
105 	debug("...AllocAddress[0] 0x%X ; AllocAddress[1] 0x%X bytes\n",
106 		*(char *)AllocAddress, *(char *)(AllocAddress+1));
107 
108 	TeeLoadTaCmd->va = AllocAddress;
109 
110 	TeeSmc32Param[1].u.memref.buf_ptr = AllocAddress;
111 	TeeSmc32Param[1].u.memref.size = ImageSize;
112 
113 Exit:
114 	TeeSmc32Arg->ret = TeecResult;
115 	TeeSmc32Arg->ret_origin = TEEC_ORIGIN_API;
116 
117 	debug("OpteeRpcCmdLoadTa Exit : TeecResult=0x%X\n", TeecResult);
118 
119 	return TeecResult;
120 }
121 
122 #ifdef CONFIG_OPTEE_V2
123 TEEC_Result OpteeRpcCmdLoadV2Ta(t_teesmc32_arg *TeeSmc32Arg)
124 {
125 	TEEC_Result TeecResult = TEEC_SUCCESS;
126 	t_teesmc32_param *TeeSmc32Param = NULL;
127 	uint8_t uuid[16];
128 	int i;
129 
130 	if (TeeSmc32Arg->num_params != 2) {
131 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
132 		goto Exit;
133 	}
134 
135 	TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
136 
137 	memcpy(uuid, (void *)&TeeSmc32Param[0].u.value, 16);
138 	for (i = 0; i < 16; i++)
139 		printf("uuid 0x%x", uuid[i]);
140 
141 	if (TeeSmc32Param[1].u.memref.buf_ptr == 0) {
142 		printf("return size of TA, keymaster_size = 0x%x", keymaster_size);
143 		TeeSmc32Param[1].u.memref.size = keymaster_size;
144 	} else {
145 		memcpy((void *)TeeSmc32Param[1].u.memref.buf_ptr,
146 			(void *)keymaster_data, TeeSmc32Param[1].u.memref.size);
147 		printf("memref.buf_ptr = 0x%llx; memref.size = 0x%llx",
148 			TeeSmc32Param[1].u.memref.buf_ptr,
149 			TeeSmc32Param[1].u.memref.size);
150 	}
151 
152 Exit:
153 	TeeSmc32Arg->ret = TeecResult;
154 	TeeSmc32Arg->ret_origin = TEEC_ORIGIN_API;
155 
156 	debug("OpteeRpcCmdLoadTa Exit : TeecResult=0x%X\n", TeecResult);
157 
158 	return TeecResult;
159 }
160 #endif
161 
162 /*
163  * Free a previously loaded TA and release the memory
164  * Param[0] = IN: TA Image to free
165  *
166  * Um, why is OpTEE holding on to this memory? The OS code suggests that OpTEE
167  * is using the binary in place out of shared memory but I don't understand how
168  * runtime modifications of the binary are being prevented if that's the case?
169  */
170 TEEC_Result OpteeRpcCmdFreeTa(t_teesmc32_arg *TeeSmc32Arg)
171 {
172 	TEEC_Result TeecResult = TEEC_SUCCESS;
173 	t_teesmc32_param *TeeSmc32Param = NULL;
174 	uint32_t ImageSize = 0;
175 	size_t AllocAddress = 0;
176 
177 	if (TeeSmc32Arg->num_params != 1) {
178 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
179 		goto Exit;
180 	}
181 
182 	TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
183 
184 	AllocAddress = TeeSmc32Param[0].u.memref.buf_ptr;
185 	ImageSize = TeeSmc32Param[0].u.memref.size;
186 
187 	debug("OpteeRpcCmdFreeTa Enter: AllocAddress=0x%X, ImageSize=0x%X\n",
188 			(uint32_t) AllocAddress, (uint32_t) ImageSize);
189 
190 	if (AllocAddress == 0) {
191 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
192 		goto Exit;
193 	}
194 
195 	OpteeClientMemFree((void *)AllocAddress);
196 
197 Exit:
198 	TeeSmc32Arg->ret = TeecResult;
199 	TeeSmc32Arg->ret_origin = TEEC_ORIGIN_API;
200 
201 	debug("OpteeRpcCmdFreeTa Exit : TeecResult=0x%X\n", TeecResult);
202 
203 	return TeecResult;
204 }
205 
206 /*
207  * Execute an RPMB storage operation.
208  */
209 
210 uint16_t global_block_count;
211 TEEC_Result OpteeRpcCmdRpmb(t_teesmc32_arg *TeeSmc32Arg)
212 {
213 	struct tee_rpc_rpmb_dev_info *DevInfo;
214 	TEEC_Result EfiStatus;
215 	uint16_t RequestMsgType, i;
216 	EFI_RK_RPMB_DATA_PACKET *RequestPackets;
217 	EFI_RK_RPMB_DATA_PACKET *ResponsePackets;
218 	EFI_RK_RPMB_DATA_PACKET *tempPackets;
219 	EFI_RK_RPMB_DATA_PACKET_BACK *RequestPackets_back;
220 	EFI_RK_RPMB_DATA_PACKET_BACK *tempPackets_back;
221 	struct tee_rpc_rpmb_cmd *RpmbRequest;
222 	TEEC_Result TeecResult = TEEC_SUCCESS;
223 	t_teesmc32_param *TeeSmc32Param;
224 	struct mmc *mmc;
225 
226 	debug("Entered RPMB RPC\n");
227 
228 	if (TeeSmc32Arg->num_params != 2) {
229 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
230 		goto Exit;
231 	}
232 
233 	TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
234 	RpmbRequest = (struct tee_rpc_rpmb_cmd *)(size_t)
235 		TeeSmc32Param[0].u.memref.buf_ptr;
236 	switch (RpmbRequest->cmd) {
237 	case TEE_RPC_RPMB_CMD_DATA_REQ: {
238 		RequestPackets = (EFI_RK_RPMB_DATA_PACKET *)(RpmbRequest + 1);
239 		ResponsePackets = (EFI_RK_RPMB_DATA_PACKET *)(size_t)
240 		TeeSmc32Param[1].u.memref.buf_ptr;
241 
242 		global_block_count =
243 			(RpmbRequest->block_count == 0 ?
244 			1 : RpmbRequest->block_count);
245 		RequestPackets_back =
246 			malloc(sizeof(EFI_RK_RPMB_DATA_PACKET_BACK)
247 			* global_block_count);
248 		memcpy(RequestPackets_back->stuff,
249 			RequestPackets->stuff_bytes,
250 			RPMB_STUFF_DATA_SIZE);
251 		memcpy(RequestPackets_back->mac,
252 			RequestPackets->key_mac,
253 			RPMB_KEY_MAC_SIZE);
254 		memcpy(RequestPackets_back->data,
255 			RequestPackets->data,
256 			RPMB_DATA_SIZE);
257 		memcpy(RequestPackets_back->nonce,
258 			RequestPackets->nonce,
259 			RPMB_NONCE_SIZE);
260 		RequestPackets_back->write_counter =
261 			((RequestPackets->write_counter[3]) << 24) +
262 			((RequestPackets->write_counter[2]) << 16) +
263 			((RequestPackets->write_counter[1]) << 8) +
264 			(RequestPackets->write_counter[0]);
265 		RequestPackets_back->address =
266 			((RequestPackets->address[1]) << 8) +
267 			(RequestPackets->address[0]);
268 		RequestPackets_back->block_count =
269 			((RequestPackets->block_count[1]) << 8) +
270 			(RequestPackets->block_count[0]);
271 		RequestPackets_back->result =
272 			((RequestPackets->op_result[1]) << 8) +
273 			(RequestPackets->op_result[0]);
274 		RequestPackets_back->request =
275 			((RequestPackets->msg_type[1]) << 8) +
276 			(RequestPackets->msg_type[0]);
277 
278 		RequestMsgType = RPMB_PACKET_DATA_TO_UINT16(
279 				RequestPackets->msg_type);
280 
281 		debug("RPMB Data request %d\n", RequestMsgType);
282 
283 		switch (RequestMsgType) {
284 		case TEE_RPC_RPMB_MSG_TYPE_REQ_AUTH_KEY_PROGRAM: {
285 			EfiStatus = init_rpmb();
286 			if (EfiStatus != 0) {
287 				TeecResult = TEEC_ERROR_GENERIC;
288 				break;
289 			}
290 
291 			EfiStatus = do_programkey((struct s_rpmb *)
292 				RequestPackets_back);
293 
294 			if (EfiStatus != 0) {
295 				TeecResult = TEEC_ERROR_GENERIC;
296 				break;
297 			}
298 
299 			EfiStatus = finish_rpmb();
300 			if (EfiStatus != 0) {
301 				TeecResult = TEEC_ERROR_GENERIC;
302 				break;
303 			}
304 
305 			break;
306 		}
307 
308 		case TEE_RPC_RPMB_MSG_TYPE_REQ_WRITE_COUNTER_VAL_READ: {
309 			EfiStatus = init_rpmb();
310 			if (EfiStatus != 0) {
311 				TeecResult = TEEC_ERROR_GENERIC;
312 				break;
313 			}
314 
315 			EfiStatus = do_readcounter((struct s_rpmb *)
316 				RequestPackets_back);
317 			if (EfiStatus != 0) {
318 				TeecResult = TEEC_ERROR_GENERIC;
319 				break;
320 			}
321 
322 			EfiStatus = finish_rpmb();
323 			if (EfiStatus != 0) {
324 				TeecResult = TEEC_ERROR_GENERIC;
325 				break;
326 			}
327 
328 			break;
329 		}
330 
331 		case TEE_RPC_RPMB_MSG_TYPE_REQ_AUTH_DATA_WRITE: {
332 			EfiStatus = init_rpmb();
333 			if (EfiStatus != 0) {
334 				TeecResult = TEEC_ERROR_GENERIC;
335 				break;
336 			}
337 
338 			EfiStatus = do_authenticatedwrite((struct s_rpmb *)
339 				RequestPackets_back);
340 			if (EfiStatus != 0) {
341 				TeecResult = TEEC_ERROR_GENERIC;
342 				break;
343 			}
344 
345 			EfiStatus = finish_rpmb();
346 
347 			if (EfiStatus != 0) {
348 				TeecResult = TEEC_ERROR_GENERIC;
349 				break;
350 			}
351 
352 			break;
353 		}
354 
355 		case TEE_RPC_RPMB_MSG_TYPE_REQ_AUTH_DATA_READ: {
356 			EfiStatus = init_rpmb();
357 			if (EfiStatus != 0) {
358 				TeecResult = TEEC_ERROR_GENERIC;
359 				break;
360 			}
361 
362 			EfiStatus = do_authenticatedread((struct s_rpmb *)
363 				RequestPackets_back, global_block_count);
364 			if (EfiStatus != 0) {
365 				TeecResult = TEEC_ERROR_GENERIC;
366 				break;
367 			}
368 
369 			EfiStatus = finish_rpmb();
370 
371 			if (EfiStatus != 0) {
372 				TeecResult = TEEC_ERROR_GENERIC;
373 				break;
374 			}
375 
376 			break;
377 		}
378 
379 		default:
380 			TeecResult = TEEC_ERROR_BAD_PARAMETERS;
381 			break;
382 		}
383 		debug("RPMB TeecResult %d\n", TeecResult);
384 		break;
385 	}
386 
387 	case TEE_RPC_RPMB_CMD_GET_DEV_INFO: {
388 		mmc = do_returnmmc();
389 
390 		DevInfo = (struct tee_rpc_rpmb_dev_info *)(size_t)
391 		TeeSmc32Param[1].u.memref.buf_ptr;
392 
393 		DevInfo->cid[0] = (mmc->cid[0]) >> 24 & 0xff;
394 		DevInfo->cid[1] = (mmc->cid[0]) >> 16 & 0xff;
395 		DevInfo->cid[2] = (mmc->cid[0]) >> 8 & 0xff;
396 		DevInfo->cid[3] = (mmc->cid[0]) & 0xff;
397 		DevInfo->cid[4] = (mmc->cid[1]) >> 24 & 0xff;
398 		DevInfo->cid[5] = (mmc->cid[1]) >> 16 & 0xff;
399 		DevInfo->cid[6] = (mmc->cid[1]) >> 8 & 0xff;
400 		DevInfo->cid[7] = (mmc->cid[1]) & 0xff;
401 		DevInfo->cid[8] = (mmc->cid[2]) >> 24 & 0xff;
402 		DevInfo->cid[9] = (mmc->cid[2]) >> 16 & 0xff;
403 		DevInfo->cid[10] = (mmc->cid[2]) >> 8 & 0xff;
404 		DevInfo->cid[11] = (mmc->cid[2]) & 0xff;
405 		DevInfo->cid[12] = (mmc->cid[3]) >> 24 & 0xff;
406 		DevInfo->cid[13] = (mmc->cid[3]) >> 16 & 0xff;
407 		DevInfo->cid[14] = (mmc->cid[3]) >> 8 & 0xff;
408 		DevInfo->cid[15] = (mmc->cid[3]) & 0xff;
409 		DevInfo->rel_wr_sec_c = 1;
410 		DevInfo->rpmb_size_mult =
411 			(uint8_t)(mmc->capacity_rpmb / (128 * 1024));
412 		DevInfo->ret_code = 0;
413 
414 		goto Exit;
415 	}
416 
417 	default:
418 		TeecResult = TEEC_ERROR_BAD_PARAMETERS;
419 
420 		goto Exit;
421 	}
422 
423 	tempPackets = ResponsePackets;
424 	tempPackets_back = RequestPackets_back;
425 
426 	for (i = 0; i < global_block_count; i++) {
427 		memcpy(tempPackets->stuff_bytes,
428 			tempPackets_back->stuff,
429 			RPMB_STUFF_DATA_SIZE);
430 		memcpy(tempPackets->key_mac,
431 			tempPackets_back->mac,
432 			RPMB_KEY_MAC_SIZE);
433 		memcpy(tempPackets->data,
434 			tempPackets_back->data,
435 			RPMB_DATA_SIZE);
436 		memcpy(tempPackets->nonce,
437 			tempPackets_back->nonce,
438 			RPMB_NONCE_SIZE);
439 		tempPackets->write_counter[3] =
440 			((tempPackets_back->write_counter) >> 24) & 0xFF;
441 		tempPackets->write_counter[2] =
442 			((tempPackets_back->write_counter) >> 16) & 0xFF;
443 		tempPackets->write_counter[1] =
444 			((tempPackets_back->write_counter) >> 8) & 0xFF;
445 		tempPackets->write_counter[0] =
446 			(tempPackets_back->write_counter) & 0xFF;
447 		tempPackets->address[1] =
448 			((tempPackets_back->address) >> 8) & 0xFF;
449 		tempPackets->address[0] =
450 			(tempPackets_back->address) & 0xFF;
451 		tempPackets->block_count[1] =
452 			((tempPackets_back->block_count) >> 8) & 0xFF;
453 		tempPackets->block_count[0] =
454 			(tempPackets_back->block_count) & 0xFF;
455 		tempPackets->op_result[1] =
456 			((tempPackets_back->result) >> 8) & 0xFF;
457 		tempPackets->op_result[0] =
458 			(tempPackets_back->result) & 0xFF;
459 		tempPackets->msg_type[1] =
460 			((tempPackets_back->request) >> 8) & 0xFF;
461 		tempPackets->msg_type[0] =
462 			(tempPackets_back->request) & 0xFF;
463 		tempPackets++;
464 		tempPackets_back++;
465 	}
466 
467 	free(RequestPackets_back);
468 
469 Exit:
470 	TeeSmc32Arg->ret = TeecResult;
471 	TeeSmc32Arg->ret_origin = TEEC_ORIGIN_API;
472 
473 	return TeecResult;
474 }
475 
476 /*
477  * Execute a normal world local file system operation.
478  */
479 TEEC_Result OpteeRpcCmdFs(t_teesmc32_arg *TeeSmc32Arg)
480 {
481 	TEEC_Result TeecResult = TEEC_SUCCESS;
482 	t_teesmc32_param *TeeSmc32Param;
483 
484 	TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
485 	TeecResult = tee_supp_rk_fs_process((void *)(size_t)TeeSmc32Param[0].u.memref.buf_ptr,
486 							TeeSmc32Param[0].u.memref.size);
487 
488 	return TeecResult;
489 }
490 
491 /*
492  * TBD.
493  */
494 TEEC_Result OpteeRpcCmdGetTime(t_teesmc32_arg *TeeSmc32Arg)
495 {
496 	return TEEC_ERROR_NOT_IMPLEMENTED;
497 }
498 
499 /*
500  * TBD.
501  */
502 TEEC_Result OpteeRpcCmdWaitMutex(t_teesmc32_arg *TeeSmc32Arg)
503 {
504 	return TEEC_ERROR_NOT_IMPLEMENTED;
505 }
506 
507 /*
508  * Handle the callback from secure world.
509  */
510 TEEC_Result OpteeRpcCallback(ARM_SMC_ARGS *ArmSmcArgs)
511 {
512 	TEEC_Result TeecResult = TEEC_SUCCESS;
513 
514 	//printf("OpteeRpcCallback Enter: Arg0=0x%X, Arg1=0x%X, Arg2=0x%X\n",
515 		//ArmSmcArgs->Arg0, ArmSmcArgs->Arg1, ArmSmcArgs->Arg2);
516 
517 	switch (TEESMC_RETURN_GET_RPC_FUNC(ArmSmcArgs->Arg0)) {
518 	case TEESMC_RPC_FUNC_ALLOC_ARG: {
519 #ifdef CONFIG_OPTEE_V1
520 		TeecResult = OpteeRpcAlloc(ArmSmcArgs->Arg1, &ArmSmcArgs->Arg1);
521 #endif
522 #ifdef CONFIG_OPTEE_V2
523 		printf("ArmSmcArgs->Arg1 = 0x%x", ArmSmcArgs->Arg1);
524 		TeecResult = OpteeRpcAlloc(ArmSmcArgs->Arg1, &ArmSmcArgs->Arg2);
525 		ArmSmcArgs->Arg5 = ArmSmcArgs->Arg2;
526 		ArmSmcArgs->Arg1 = 0;
527 		ArmSmcArgs->Arg4 = 0;
528 #endif
529 		break;
530 	}
531 
532 	case TEESMC_RPC_FUNC_ALLOC_PAYLOAD: {
533 		TeecResult = OpteeRpcAlloc(ArmSmcArgs->Arg1, &ArmSmcArgs->Arg1);
534 		break;
535 	}
536 
537 	case TEESMC_RPC_FUNC_FREE_ARG: {
538 #ifdef CONFIG_OPTEE_V1
539 		TeecResult = OpteeRpcFree(ArmSmcArgs->Arg1);
540 #endif
541 #ifdef CONFIG_OPTEE_V2
542 		TeecResult = OpteeRpcFree(ArmSmcArgs->Arg2);
543 #endif
544 		break;
545 	}
546 
547 	case TEESMC_RPC_FUNC_FREE_PAYLOAD: {
548 		TeecResult = OpteeRpcFree(ArmSmcArgs->Arg1);
549 		break;
550 	}
551 
552 	case TEESMC_RPC_FUNC_IRQ: {
553 		break;
554 	}
555 
556 	case TEESMC_RPC_FUNC_CMD: {
557 #ifdef CONFIG_OPTEE_V1
558 		t_teesmc32_arg *TeeSmc32Arg =
559 			(t_teesmc32_arg *)(size_t)ArmSmcArgs->Arg1;
560 #endif
561 #ifdef CONFIG_OPTEE_V2
562 		t_teesmc32_arg *TeeSmc32Arg =
563 			(t_teesmc32_arg *)((size_t)ArmSmcArgs->Arg1 << 32 | ArmSmcArgs->Arg2);
564 		printf("TeeSmc32Arg->cmd = 0x%x", TeeSmc32Arg->cmd);
565 #endif
566 		switch (TeeSmc32Arg->cmd) {
567 #ifdef CONFIG_OPTEE_V1
568 		case TEE_RPC_LOAD_TA: {
569 			TeecResult = OpteeRpcCmdLoadTa(TeeSmc32Arg);
570 			break;
571 		}
572 
573 		case TEE_RPC_FREE_TA: {
574 			TeecResult = OpteeRpcCmdFreeTa(TeeSmc32Arg);
575 			break;
576 		}
577 
578 		case TEE_RPC_RPMB_CMD: {
579 			TeecResult = OpteeRpcCmdRpmb(TeeSmc32Arg);
580 			break;
581 		}
582 
583 		case TEE_RPC_FS: {
584 			TeecResult = OpteeRpcCmdFs(TeeSmc32Arg);
585 			TeeSmc32Arg->ret = TEEC_SUCCESS;
586 			break;
587 		}
588 
589 		case TEE_RPC_GET_TIME: {
590 			TeecResult = OpteeRpcCmdGetTime(TeeSmc32Arg);
591 			break;
592 		}
593 
594 		case TEE_RPC_WAIT_MUTEX: {
595 			TeecResult = OpteeRpcCmdWaitMutex(TeeSmc32Arg);
596 			break;
597 		}
598 #endif
599 #ifdef CONFIG_OPTEE_V2
600 		case OPTEE_MSG_RPC_CMD_SHM_ALLOC_V2: {
601 			uint32_t tempaddr;
602 			uint32_t allocsize = TeeSmc32Arg->params[0].u.value.b;
603 			TeecResult = OpteeRpcAlloc(allocsize, &tempaddr);
604 			printf("allocsize = 0x%x tempaddr = 0x%x", allocsize, tempaddr);
605 			TeeSmc32Arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT_V2;
606 			TeeSmc32Arg->params[0].u.memref.buf_ptr = tempaddr;
607 			TeeSmc32Arg->params[0].u.memref.size = allocsize;
608 			TeeSmc32Arg->params[0].u.memref.shm_ref = tempaddr;
609 			TeeSmc32Arg->ret = TEE_SUCCESS;
610 			break;
611 		}
612 		case OPTEE_MSG_RPC_CMD_SHM_FREE_V2: {
613 			uint32_t tempaddr = TeeSmc32Arg->params[0].u.value.b;
614 			TeecResult = OpteeRpcFree(tempaddr);
615 			break;
616 
617 		}
618 		case OPTEE_MSG_RPC_CMD_RPMB_V2: {
619 			TeecResult = OpteeRpcCmdRpmb(TeeSmc32Arg);
620 			break;
621 		}
622 		case OPTEE_MSG_RPC_CMD_LOAD_TA_V2: {
623 			TeecResult = OpteeRpcCmdLoadV2Ta(TeeSmc32Arg);
624 			break;
625 		}
626 #endif
627 
628 		default: {
629 			printf("...unsupported RPC CMD: cmd=0x%X\n",
630 				TeeSmc32Arg->cmd);
631 			TeecResult = TEEC_ERROR_NOT_IMPLEMENTED;
632 			break;
633 		}
634 	}
635 
636 		break;
637 	}
638 
639 	case TEESMC_OPTEE_RPC_FUNC_ALLOC_PAYLOAD: {
640 		TeecResult = OpteeRpcAlloc(ArmSmcArgs->Arg1, &ArmSmcArgs->Arg1);
641 		ArmSmcArgs->Arg2 = ArmSmcArgs->Arg1;
642 		break;
643 	}
644 
645 	case TEESMC_OPTEE_RPC_FUNC_FREE_PAYLOAD: {
646 		TeecResult = OpteeRpcFree(ArmSmcArgs->Arg1);
647 		break;
648 	}
649 
650 	default: {
651 		printf("...unsupported RPC : Arg0=0x%X\n", ArmSmcArgs->Arg0);
652 		TeecResult = TEEC_ERROR_NOT_IMPLEMENTED;
653 		break;
654 	}
655 	}
656 
657 	ArmSmcArgs->Arg0 = TEESMC32_CALL_RETURN_FROM_RPC;
658 	debug("OpteeRpcCallback Exit : TeecResult=0x%X\n", TeecResult);
659 
660 	return TeecResult;
661 }
662