xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientInterface.c (revision b77c257eaed4fb83d4f17aeb4ad71b3a8a7e38e6)
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 
8 #include <common.h>
9 #include <optee_include/OpteeClientInterface.h>
10 #include <optee_include/OpteeClientApiLib.h>
11 #include <optee_include/tee_client_api.h>
12 #include <optee_include/tee_api_defines.h>
13 #include <boot_rkimg.h>
14 #include <stdlib.h>
15 #include <attestation_key.h>
16 
17 #define	BOOT_FROM_EMMC	(1 << 1)
18 #define STORAGE_CMD_READ_ATTRIBUTE_HASH		0
19 #define STORAGE_CMD_WRITE_ATTRIBUTE_HASH	1
20 #define STORAGE_CMD_UBOOT_END_OTP		2
21 #define STORAGE_CMD_READ_VBOOTKEY_HASH		3
22 #define STORAGE_CMD_WRITE_VBOOTKEY_HASH		4
23 #define STORAGE_CMD_READ_ENABLE_FLAG		5
24 #define STORAGE_CMD_WRITE_TA_ENCRYPTION_KEY	9
25 #define STORAGE_CMD_CHECK_SECURITY_LEVEL_FLAG	10
26 #define STORAGE_CMD_WRITE_OEM_HUK		11
27 #define STORAGE_CMD_WRITE_OEM_NS_OTP		12
28 #define STORAGE_CMD_READ_OEM_NS_OTP		13
29 #define STORAGE_CMD_WRITE_OEM_OTP_KEY		14
30 #define STORAGE_CMD_SET_OEM_HR_OTP_READ_LOCK	15
31 #define STORAGE_CMD_OEM_OTP_KEY_IS_WRITTEN	16
32 #define STORAGE_CMD_TA_ENCRYPTION_KEY_IS_WRITTEN	20
33 #define STORAGE_CMD_WRITE_OEM_HDCP_KEY	21
34 #define STORAGE_CMD_OEM_HDCP_KEY_IS_WRITTEN	22
35 #define STORAGE_CMD_SET_OEM_HDCP_KEY_MASK	23
36 
37 #define CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER	0x00000002
38 
39 #define RK_CRYPTO_SERVICE_UUID	{ 0x0cacdb5d, 0x4fea, 0x466c, \
40 		{ 0x97, 0x16, 0x3d, 0x54, 0x16, 0x52, 0x83, 0x0f } }
41 
42 static uint8_t b2hs_add_base(uint8_t in)
43 {
44 	if (in > 9)
45 		return in + 55;
46 	else
47 		return in + 48;
48 }
49 
50 static uint32_t b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen)
51 {
52 	uint32_t i = 0;
53 
54 	if (blen * 2 + 1 > hslen)
55 		return 0;
56 
57 	for (; i < blen; i++) {
58 		hs[i * 2 + 1] = b2hs_add_base(b[i] & 0xf);
59 		hs[i * 2] = b2hs_add_base(b[i] >> 4);
60 	}
61 	hs[blen * 2] = 0;
62 
63 	return blen * 2;
64 }
65 
66 static void crypto_flush_cacheline(uint32_t addr, uint32_t size)
67 {
68 	ulong alignment = CONFIG_SYS_CACHELINE_SIZE;
69 	ulong aligned_input, aligned_len;
70 
71 	if (!addr || !size)
72 		return;
73 
74 	/* Must flush dcache before crypto DMA fetch data region */
75 	aligned_input = round_down(addr, alignment);
76 	aligned_len = round_up(size + (addr - aligned_input), alignment);
77 	flush_cache(aligned_input, aligned_len);
78 }
79 
80 static void crypto_invalidate_cacheline(uint32_t addr, uint32_t size)
81 {
82 	ulong alignment = CONFIG_SYS_CACHELINE_SIZE;
83 	ulong aligned_input, aligned_len;
84 
85 	if (!addr || !size)
86 		return;
87 
88 	/* Must invalidate dcache after crypto DMA write data region */
89 	aligned_input = round_down(addr, alignment);
90 	aligned_len = round_up(size + (addr - aligned_input), alignment);
91 	invalidate_dcache_range(aligned_input, aligned_input + aligned_len);
92 }
93 
94 static uint32_t trusty_base_write_security_data(char *filename,
95 						uint32_t filename_size,
96 						uint8_t *data,
97 						uint32_t data_size)
98 {
99 	TEEC_Result TeecResult;
100 	TEEC_Context TeecContext;
101 	TEEC_Session TeecSession;
102 	uint32_t ErrorOrigin;
103 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
104 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
105 	TEEC_UUID *TeecUuid = &tempuuid;
106 	TEEC_Operation TeecOperation = {0};
107 	struct blk_desc *dev_desc;
108 	dev_desc = rockchip_get_bootdev();
109 	if (!dev_desc) {
110 		printf("%s: dev_desc is NULL!\n", __func__);
111 		return -TEEC_ERROR_GENERIC;
112 	}
113 
114 	TeecResult = OpteeClientApiLibInitialize();
115 	if (TeecResult != TEEC_SUCCESS)
116 		return TeecResult;
117 
118 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
119 	if (TeecResult != TEEC_SUCCESS)
120 		return TeecResult;
121 
122 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
123 						    TEEC_NONE,
124 						    TEEC_NONE,
125 						    TEEC_NONE);
126 	/*0 nand or emmc "security" partition , 1 rpmb*/
127 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
128 		TeecOperation.params[0].value.a = 1;
129 	else
130 		TeecOperation.params[0].value.a = 0;
131 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
132 	TeecOperation.params[0].value.a = 0;
133 #endif
134 
135 	TeecResult = TEEC_OpenSession(&TeecContext,
136 				&TeecSession,
137 				TeecUuid,
138 				TEEC_LOGIN_PUBLIC,
139 				NULL,
140 				&TeecOperation,
141 				&ErrorOrigin);
142 	if (TeecResult != TEEC_SUCCESS)
143 		return TeecResult;
144 
145 	TEEC_SharedMemory SharedMem0 = {0};
146 
147 	SharedMem0.size = filename_size;
148 	SharedMem0.flags = 0;
149 
150 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
151 	if (TeecResult != TEEC_SUCCESS)
152 		goto exit;
153 
154 	memcpy(SharedMem0.buffer, filename, SharedMem0.size);
155 
156 	TEEC_SharedMemory SharedMem1 = {0};
157 
158 	SharedMem1.size = data_size;
159 	SharedMem1.flags = 0;
160 
161 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
162 	if (TeecResult != TEEC_SUCCESS)
163 		goto exit;
164 
165 	memcpy(SharedMem1.buffer, data, SharedMem1.size);
166 
167 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
168 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
169 
170 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
171 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
172 
173 
174 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
175 						TEEC_MEMREF_TEMP_INOUT,
176 						TEEC_NONE,
177 						TEEC_NONE);
178 
179 	TeecResult = TEEC_InvokeCommand(&TeecSession,
180 					1,
181 					&TeecOperation,
182 					&ErrorOrigin);
183 	if (TeecResult != TEEC_SUCCESS)
184 		goto exit;
185 exit:
186 	TEEC_ReleaseSharedMemory(&SharedMem0);
187 	TEEC_ReleaseSharedMemory(&SharedMem1);
188 	TEEC_CloseSession(&TeecSession);
189 	TEEC_FinalizeContext(&TeecContext);
190 
191 	return TeecResult;
192 }
193 
194 static uint32_t trusty_base_read_security_data(char *filename,
195 					       uint32_t filename_size,
196 					       uint8_t *data,
197 					       uint32_t data_size)
198 {
199 	TEEC_Result TeecResult;
200 	TEEC_Context TeecContext;
201 	TEEC_Session TeecSession;
202 	uint32_t ErrorOrigin;
203 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
204 			{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
205 	TEEC_UUID *TeecUuid = &tempuuid;
206 	TEEC_Operation TeecOperation = {0};
207 
208 	struct blk_desc *dev_desc;
209 	dev_desc = rockchip_get_bootdev();
210 	if (!dev_desc) {
211 		printf("%s: dev_desc is NULL!\n", __func__);
212 		return -TEEC_ERROR_GENERIC;
213 	}
214 
215 	TeecResult = OpteeClientApiLibInitialize();
216 	if (TeecResult != TEEC_SUCCESS)
217 		return TeecResult;
218 
219 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
220 	if (TeecResult != TEEC_SUCCESS)
221 		return TeecResult;
222 
223 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
224 						TEEC_NONE,
225 						TEEC_NONE,
226 						TEEC_NONE);
227 	/*0 nand or emmc "security" partition , 1 rpmb*/
228 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
229 		TeecOperation.params[0].value.a = 1;
230 	else
231 		TeecOperation.params[0].value.a = 0;
232 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
233 	TeecOperation.params[0].value.a = 0;
234 #endif
235 
236 	TeecResult = TEEC_OpenSession(&TeecContext,
237 				&TeecSession,
238 				TeecUuid,
239 				TEEC_LOGIN_PUBLIC,
240 				NULL,
241 				&TeecOperation,
242 				&ErrorOrigin);
243 	if (TeecResult != TEEC_SUCCESS)
244 		return TeecResult;
245 
246 	TEEC_SharedMemory SharedMem0 = {0};
247 
248 	SharedMem0.size = filename_size;
249 	SharedMem0.flags = 0;
250 
251 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
252 	if (TeecResult != TEEC_SUCCESS)
253 		goto exit;
254 
255 	memcpy(SharedMem0.buffer, filename, SharedMem0.size);
256 
257 	TEEC_SharedMemory SharedMem1 = {0};
258 
259 	SharedMem1.size = data_size;
260 	SharedMem1.flags = 0;
261 
262 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
263 	if (TeecResult != TEEC_SUCCESS)
264 		goto exit;
265 
266 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
267 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
268 
269 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
270 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
271 
272 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
273 						TEEC_MEMREF_TEMP_INOUT,
274 						TEEC_NONE,
275 						TEEC_NONE);
276 
277 	TeecResult = TEEC_InvokeCommand(&TeecSession,
278 					0,
279 					&TeecOperation,
280 					&ErrorOrigin);
281 	if (TeecResult == TEEC_SUCCESS)
282 		memcpy(data, SharedMem1.buffer, SharedMem1.size);
283 exit:
284 	TEEC_ReleaseSharedMemory(&SharedMem0);
285 	TEEC_ReleaseSharedMemory(&SharedMem1);
286 	TEEC_CloseSession(&TeecSession);
287 	TEEC_FinalizeContext(&TeecContext);
288 
289 	return TeecResult;
290 }
291 
292 static uint32_t trusty_base_end_security_data(void)
293 {
294 	TEEC_Result TeecResult;
295 	TEEC_Context TeecContext;
296 	TEEC_Session TeecSession;
297 	uint32_t ErrorOrigin;
298 	TEEC_UUID  tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
299 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
300 	TEEC_UUID *TeecUuid = &tempuuid;
301 	TEEC_Operation TeecOperation = {0};
302 
303 	TeecResult = OpteeClientApiLibInitialize();
304 	if (TeecResult != TEEC_SUCCESS)
305 		return TeecResult;
306 
307 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
308 	if (TeecResult != TEEC_SUCCESS)
309 		return TeecResult;
310 
311 	TeecResult = TEEC_OpenSession(&TeecContext,
312 				&TeecSession,
313 				TeecUuid,
314 				TEEC_LOGIN_PUBLIC,
315 				NULL,
316 				NULL,
317 				&ErrorOrigin);
318 	if (TeecResult != TEEC_SUCCESS)
319 		return TeecResult;
320 
321 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
322 						    TEEC_NONE,
323 						    TEEC_NONE,
324 						    TEEC_NONE);
325 
326 	TeecResult = TEEC_InvokeCommand(&TeecSession,
327 					2,
328 					&TeecOperation,
329 					&ErrorOrigin);
330 	if (TeecResult != TEEC_SUCCESS)
331 		goto exit;
332 exit:
333 	TEEC_CloseSession(&TeecSession);
334 	TEEC_FinalizeContext(&TeecContext);
335 
336 	return TeecResult;
337 }
338 
339 static void trusty_notify_always_use_security(void)
340 {
341 #if defined(CONFIG_OPTEE_V2) && defined(CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION)
342 	TEEC_Result TeecResult;
343 	TEEC_Context TeecContext;
344 	TEEC_Session TeecSession;
345 	uint32_t ErrorOrigin;
346 	TEEC_UUID  tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
347 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
348 	TEEC_UUID *TeecUuid = &tempuuid;
349 	TEEC_Operation TeecOperation = {0};
350 
351 	TeecResult = OpteeClientApiLibInitialize();
352 	if (TeecResult != TEEC_SUCCESS)
353 		return;
354 
355 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
356 	if (TeecResult != TEEC_SUCCESS)
357 		return;
358 
359 	TeecResult = TEEC_OpenSession(&TeecContext,
360 				&TeecSession,
361 				TeecUuid,
362 				TEEC_LOGIN_PUBLIC,
363 				NULL,
364 				NULL,
365 				&ErrorOrigin);
366 	if (TeecResult != TEEC_SUCCESS)
367 		return;
368 
369 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
370 						    TEEC_NONE,
371 						    TEEC_NONE,
372 						    TEEC_NONE);
373 
374 	TeecResult = TEEC_InvokeCommand(&TeecSession,
375 					9,
376 					&TeecOperation,
377 					&ErrorOrigin);
378 	if (TeecResult != TEEC_SUCCESS)
379 		debug("notify always use security fail! please update optee!");
380 
381 	TEEC_CloseSession(&TeecSession);
382 	TEEC_FinalizeContext(&TeecContext);
383 
384 	return;
385 #endif
386 }
387 
388 uint32_t trusty_read_rollback_index(uint32_t slot, uint64_t *value)
389 {
390 	char hs[9];
391 
392 	b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9);
393 
394 	return trusty_base_read_security_data(hs, 8, (uint8_t *)value, 8);
395 }
396 
397 uint32_t trusty_write_rollback_index(uint32_t slot, uint64_t value)
398 {
399 	char hs[9];
400 
401 	b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9);
402 
403 	return trusty_base_write_security_data(hs, 8, (uint8_t *)&value, 8);
404 }
405 
406 uint32_t trusty_read_permanent_attributes(uint8_t *attributes, uint32_t size)
407 {
408 	return trusty_base_read_security_data("attributes",
409 		sizeof("attributes"), attributes, size);
410 }
411 
412 uint32_t trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size)
413 {
414 	return trusty_base_write_security_data("attributes",
415 		sizeof("attributes"), attributes, size);
416 }
417 
418 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes)
419 {
420 	return trusty_base_read_security_data("attributes_flag",
421 		sizeof("attributes_flag"), attributes, 1);
422 }
423 
424 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes)
425 {
426 	return trusty_base_write_security_data("attributes_flag",
427 		sizeof("attributes_flag"), &attributes, 1);
428 }
429 
430 uint32_t trusty_read_permanent_attributes_cer(uint8_t *attributes,
431 					      uint32_t size)
432 {
433 	return trusty_base_read_security_data("rsacer",
434 		sizeof("rsacer"), attributes, size);
435 }
436 
437 uint32_t trusty_write_permanent_attributes_cer(uint8_t *attributes,
438 					       uint32_t size)
439 {
440 	return trusty_base_write_security_data("rsacer",
441 		sizeof("rsacer"), attributes, size);
442 }
443 
444 uint32_t trusty_read_lock_state(uint8_t *lock_state)
445 {
446 	return trusty_base_read_security_data("lock_state",
447 		sizeof("lock_state"), lock_state, 1);
448 }
449 
450 uint32_t trusty_write_lock_state(uint8_t lock_state)
451 {
452 	return trusty_base_write_security_data("lock_state",
453 		sizeof("lock_state"), &lock_state, 1);
454 }
455 
456 uint32_t trusty_read_flash_lock_state(uint8_t *flash_lock_state)
457 {
458 	return trusty_base_read_security_data("flash_lock_state",
459 		sizeof("flash_lock_state"), flash_lock_state, 1);
460 }
461 
462 uint32_t trusty_write_flash_lock_state(uint8_t flash_lock_state)
463 {
464 	return trusty_base_write_security_data("flash_lock_state",
465 		sizeof("flash_lock_state"), &flash_lock_state, 1);
466 }
467 
468 static uint32_t trusty_base_end_efuse_or_otp(void)
469 {
470 	TEEC_Result TeecResult;
471 	TEEC_Context TeecContext;
472 	TEEC_Session TeecSession;
473 	uint32_t ErrorOrigin;
474 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
475 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
476 
477 	TEEC_UUID *TeecUuid = &tempuuid;
478 	TEEC_Operation TeecOperation = {0};
479 
480 	TeecResult = OpteeClientApiLibInitialize();
481 	if (TeecResult != TEEC_SUCCESS)
482 		return TeecResult;
483 
484 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
485 	if (TeecResult != TEEC_SUCCESS)
486 		return TeecResult;
487 
488 	TeecResult = TEEC_OpenSession(&TeecContext,
489 				      &TeecSession,
490 				      TeecUuid,
491 				      TEEC_LOGIN_PUBLIC,
492 				      NULL,
493 				      NULL,
494 				      &ErrorOrigin);
495 	if (TeecResult != TEEC_SUCCESS)
496 		return TeecResult;
497 
498 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
499 						    TEEC_NONE,
500 						    TEEC_NONE,
501 						    TEEC_NONE);
502 
503 	TeecResult = TEEC_InvokeCommand(&TeecSession,
504 					STORAGE_CMD_UBOOT_END_OTP,
505 					&TeecOperation,
506 					&ErrorOrigin);
507 	if (TeecResult != TEEC_SUCCESS)
508 		goto exit;
509 exit:
510 	TEEC_CloseSession(&TeecSession);
511 	TEEC_FinalizeContext(&TeecContext);
512 
513 	return TeecResult;
514 }
515 
516 static uint32_t trusty_base_efuse_or_otp_operation(uint32_t cmd,
517 						   uint8_t is_write,
518 						   uint32_t *buf,
519 						   uint32_t length)
520 {
521 	TEEC_Result TeecResult;
522 	TEEC_Context TeecContext;
523 	TEEC_Session TeecSession;
524 	uint32_t ErrorOrigin;
525 
526 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
527 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
528 	TEEC_UUID *TeecUuid = &tempuuid;
529 	TEEC_Operation TeecOperation = {0};
530 
531 	TeecResult = OpteeClientApiLibInitialize();
532 	if (TeecResult != TEEC_SUCCESS)
533 		return TeecResult;
534 
535 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
536 	if (TeecResult != TEEC_SUCCESS)
537 		return TeecResult;
538 
539 	TeecResult = TEEC_OpenSession(&TeecContext,
540 				&TeecSession,
541 				TeecUuid,
542 				TEEC_LOGIN_PUBLIC,
543 				NULL,
544 				NULL,
545 				&ErrorOrigin);
546 	if (TeecResult != TEEC_SUCCESS)
547 		return TeecResult;
548 
549 	TEEC_SharedMemory SharedMem0 = {0};
550 
551 	SharedMem0.size = length * sizeof(uint32_t);
552 	SharedMem0.flags = 0;
553 
554 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
555 	if (TeecResult != TEEC_SUCCESS)
556 		goto exit;
557 
558 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
559 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
560 
561 	if (is_write) {
562 		memcpy(SharedMem0.buffer, buf, SharedMem0.size);
563 		TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
564 							    TEEC_NONE,
565 							    TEEC_NONE,
566 							    TEEC_NONE);
567 
568 	} else {
569 		TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
570 							    TEEC_NONE,
571 							    TEEC_NONE,
572 							    TEEC_NONE);
573 	}
574 
575 	TeecResult = TEEC_InvokeCommand(&TeecSession,
576 					cmd,
577 					&TeecOperation,
578 					&ErrorOrigin);
579 	if (TeecResult != TEEC_SUCCESS)
580 		goto exit;
581 
582 	if (!is_write)
583 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
584 
585 exit:
586 	TEEC_ReleaseSharedMemory(&SharedMem0);
587 	TEEC_CloseSession(&TeecSession);
588 	TEEC_FinalizeContext(&TeecContext);
589 
590 	return TeecResult;
591 }
592 
593 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length)
594 {
595 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ATTRIBUTE_HASH,
596 						  false, buf, length);
597 }
598 
599 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length)
600 {
601 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_ATTRIBUTE_HASH,
602 						  true, buf, length);
603 }
604 
605 uint32_t trusty_notify_optee_uboot_end(void)
606 {
607 	TEEC_Result res;
608 
609 	res = trusty_base_end_security_data();
610 	res |= trusty_base_end_efuse_or_otp();
611 	return res;
612 }
613 
614 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length)
615 {
616 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_VBOOTKEY_HASH,
617 						  false, buf, length);
618 }
619 
620 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length)
621 {
622 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_VBOOTKEY_HASH,
623 						  true, buf, length);
624 }
625 
626 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag)
627 {
628 	uint32_t bootflag;
629 	TEEC_Result TeecResult;
630 
631 	*flag = 0;
632 
633 	TeecResult = trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ENABLE_FLAG,
634 							false, &bootflag, 1);
635 
636 	if (TeecResult == TEEC_SUCCESS) {
637 #if defined(CONFIG_ROCKCHIP_RK3288)
638 		if (bootflag == 0x00000001)
639 			*flag = 1;
640 #else
641 		if (bootflag == 0x000000FF)
642 			*flag = 1;
643 #endif
644 	}
645 	return TeecResult;
646 }
647 
648 uint32_t trusty_write_ta_encryption_key(uint32_t *buf, uint32_t length)
649 {
650 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_TA_ENCRYPTION_KEY,
651 						  true, buf, length);
652 }
653 
654 uint32_t trusty_ta_encryption_key_is_written(uint8_t *value)
655 {
656 	TEEC_Result TeecResult;
657 	TEEC_Context TeecContext;
658 	TEEC_Session TeecSession;
659 	uint32_t ErrorOrigin;
660 
661 	*value = 0;
662 
663 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
664 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
665 	TEEC_UUID *TeecUuid = &tempuuid;
666 	TEEC_Operation TeecOperation = {0};
667 
668 	TeecResult = OpteeClientApiLibInitialize();
669 	if (TeecResult != TEEC_SUCCESS)
670 		return TeecResult;
671 
672 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
673 	if (TeecResult != TEEC_SUCCESS)
674 		return TeecResult;
675 
676 	TeecResult = TEEC_OpenSession(&TeecContext,
677 				&TeecSession,
678 				TeecUuid,
679 				TEEC_LOGIN_PUBLIC,
680 				NULL,
681 				NULL,
682 				&ErrorOrigin);
683 	if (TeecResult != TEEC_SUCCESS)
684 		return TeecResult;
685 
686 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
687 						    TEEC_NONE,
688 						    TEEC_NONE,
689 						    TEEC_NONE);
690 
691 	TeecResult = TEEC_InvokeCommand(&TeecSession,
692 					STORAGE_CMD_TA_ENCRYPTION_KEY_IS_WRITTEN,
693 					&TeecOperation,
694 					&ErrorOrigin);
695 	if (TeecResult == TEEC_SUCCESS)
696 		*value = TeecOperation.params[0].value.a;
697 
698 	TEEC_CloseSession(&TeecSession);
699 	TEEC_FinalizeContext(&TeecContext);
700 
701 	return TeecResult;
702 }
703 
704 uint32_t trusty_check_security_level_flag(uint8_t flag)
705 {
706 	uint32_t levelflag;
707 
708 	levelflag = flag;
709 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_CHECK_SECURITY_LEVEL_FLAG,
710 						  true, &levelflag, 1);
711 }
712 
713 uint32_t trusty_write_oem_huk(uint32_t *buf, uint32_t length)
714 {
715 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_OEM_HUK,
716 						  true, buf, length);
717 }
718 
719 static void trusty_select_security_level(void)
720 {
721 #if (CONFIG_OPTEE_SECURITY_LEVEL > 0)
722 	TEEC_Result TeecResult;
723 
724 	TeecResult = trusty_check_security_level_flag(CONFIG_OPTEE_SECURITY_LEVEL);
725 	if (TeecResult == TEE_ERROR_CANCEL) {
726 		run_command("download", 0);
727 		return;
728 	}
729 
730 	if (TeecResult == TEEC_SUCCESS)
731 		debug("optee select security level success!");
732 	else
733 		panic("optee select security level fail!");
734 
735 	return;
736 #endif
737 }
738 
739 void optee_client_init(void)
740 {
741 	trusty_select_security_level();
742 	trusty_notify_always_use_security();
743 }
744 
745 uint32_t trusty_write_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len)
746 {
747 	TEEC_Result TeecResult;
748 	TEEC_Context TeecContext;
749 	TEEC_Session TeecSession;
750 	uint32_t ErrorOrigin;
751 
752 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
753 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
754 	TEEC_UUID *TeecUuid = &tempuuid;
755 	TEEC_Operation TeecOperation = {0};
756 
757 	TeecResult = OpteeClientApiLibInitialize();
758 	if (TeecResult != TEEC_SUCCESS)
759 		return TeecResult;
760 
761 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
762 	if (TeecResult != TEEC_SUCCESS)
763 		return TeecResult;
764 
765 	TeecResult = TEEC_OpenSession(&TeecContext,
766 				&TeecSession,
767 				TeecUuid,
768 				TEEC_LOGIN_PUBLIC,
769 				NULL,
770 				NULL,
771 				&ErrorOrigin);
772 	if (TeecResult != TEEC_SUCCESS)
773 		return TeecResult;
774 
775 	TeecOperation.params[0].value.a = byte_off;
776 
777 	TEEC_SharedMemory SharedMem = {0};
778 
779 	SharedMem.size = byte_len;
780 	SharedMem.flags = 0;
781 
782 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem);
783 	if (TeecResult != TEEC_SUCCESS)
784 		goto exit;
785 
786 	TeecOperation.params[1].tmpref.buffer = SharedMem.buffer;
787 	TeecOperation.params[1].tmpref.size = SharedMem.size;
788 
789 	memcpy(SharedMem.buffer, byte_buf, SharedMem.size);
790 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
791 						    TEEC_MEMREF_TEMP_INPUT,
792 						    TEEC_NONE,
793 						    TEEC_NONE);
794 
795 	TeecResult = TEEC_InvokeCommand(&TeecSession,
796 					STORAGE_CMD_WRITE_OEM_NS_OTP,
797 					&TeecOperation,
798 					&ErrorOrigin);
799 	if (TeecResult != TEEC_SUCCESS)
800 		goto exit;
801 
802 exit:
803 	TEEC_ReleaseSharedMemory(&SharedMem);
804 	TEEC_CloseSession(&TeecSession);
805 	TEEC_FinalizeContext(&TeecContext);
806 
807 	return TeecResult;
808 }
809 
810 uint32_t trusty_read_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len)
811 {
812 	TEEC_Result TeecResult;
813 	TEEC_Context TeecContext;
814 	TEEC_Session TeecSession;
815 	uint32_t ErrorOrigin;
816 
817 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
818 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
819 	TEEC_UUID *TeecUuid = &tempuuid;
820 	TEEC_Operation TeecOperation = {0};
821 
822 	TeecResult = OpteeClientApiLibInitialize();
823 	if (TeecResult != TEEC_SUCCESS)
824 		return TeecResult;
825 
826 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
827 	if (TeecResult != TEEC_SUCCESS)
828 		return TeecResult;
829 
830 	TeecResult = TEEC_OpenSession(&TeecContext,
831 				&TeecSession,
832 				TeecUuid,
833 				TEEC_LOGIN_PUBLIC,
834 				NULL,
835 				NULL,
836 				&ErrorOrigin);
837 	if (TeecResult != TEEC_SUCCESS)
838 		return TeecResult;
839 
840 	TeecOperation.params[0].value.a = byte_off;
841 
842 	TEEC_SharedMemory SharedMem = {0};
843 
844 	SharedMem.size = byte_len;
845 	SharedMem.flags = 0;
846 
847 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem);
848 	if (TeecResult != TEEC_SUCCESS)
849 		goto exit;
850 
851 	TeecOperation.params[1].tmpref.buffer = SharedMem.buffer;
852 	TeecOperation.params[1].tmpref.size = SharedMem.size;
853 
854 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
855 						    TEEC_MEMREF_TEMP_OUTPUT,
856 						    TEEC_NONE,
857 						    TEEC_NONE);
858 
859 	TeecResult = TEEC_InvokeCommand(&TeecSession,
860 					STORAGE_CMD_READ_OEM_NS_OTP,
861 					&TeecOperation,
862 					&ErrorOrigin);
863 	if (TeecResult != TEEC_SUCCESS)
864 		goto exit;
865 
866 	memcpy(byte_buf, SharedMem.buffer, SharedMem.size);
867 
868 exit:
869 	TEEC_ReleaseSharedMemory(&SharedMem);
870 	TEEC_CloseSession(&TeecSession);
871 	TEEC_FinalizeContext(&TeecContext);
872 
873 	return TeecResult;
874 }
875 
876 uint32_t trusty_write_oem_otp_key(enum RK_OEM_OTP_KEYID key_id,
877 				  uint8_t *byte_buf, uint32_t byte_len)
878 {
879 	TEEC_Result TeecResult;
880 	TEEC_Context TeecContext;
881 	TEEC_Session TeecSession;
882 	uint32_t ErrorOrigin;
883 
884 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
885 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
886 	TEEC_UUID *TeecUuid = &tempuuid;
887 	TEEC_Operation TeecOperation = {0};
888 
889 	TeecResult = OpteeClientApiLibInitialize();
890 	if (TeecResult != TEEC_SUCCESS)
891 		return TeecResult;
892 
893 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
894 	if (TeecResult != TEEC_SUCCESS)
895 		return TeecResult;
896 
897 	TeecResult = TEEC_OpenSession(&TeecContext,
898 				&TeecSession,
899 				TeecUuid,
900 				TEEC_LOGIN_PUBLIC,
901 				NULL,
902 				NULL,
903 				&ErrorOrigin);
904 	if (TeecResult != TEEC_SUCCESS)
905 		return TeecResult;
906 
907 	TeecOperation.params[0].value.a = key_id;
908 
909 	TEEC_SharedMemory SharedMem = {0};
910 
911 	SharedMem.size = byte_len;
912 	SharedMem.flags = 0;
913 
914 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem);
915 	if (TeecResult != TEEC_SUCCESS)
916 		goto exit;
917 
918 	TeecOperation.params[1].tmpref.buffer = SharedMem.buffer;
919 	TeecOperation.params[1].tmpref.size = SharedMem.size;
920 
921 	memcpy(SharedMem.buffer, byte_buf, SharedMem.size);
922 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
923 						    TEEC_MEMREF_TEMP_INPUT,
924 						    TEEC_NONE,
925 						    TEEC_NONE);
926 
927 	TeecResult = TEEC_InvokeCommand(&TeecSession,
928 					STORAGE_CMD_WRITE_OEM_OTP_KEY,
929 					&TeecOperation,
930 					&ErrorOrigin);
931 	if (TeecResult != TEEC_SUCCESS)
932 		goto exit;
933 
934 exit:
935 	TEEC_ReleaseSharedMemory(&SharedMem);
936 	TEEC_CloseSession(&TeecSession);
937 	TEEC_FinalizeContext(&TeecContext);
938 
939 	return TeecResult;
940 }
941 
942 uint32_t trusty_oem_otp_key_is_written(enum RK_OEM_OTP_KEYID key_id, uint8_t *value)
943 {
944 	TEEC_Result TeecResult;
945 	TEEC_Context TeecContext;
946 	TEEC_Session TeecSession;
947 	uint32_t ErrorOrigin;
948 
949 	*value = 0xFF;
950 
951 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
952 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
953 	TEEC_UUID *TeecUuid = &tempuuid;
954 	TEEC_Operation TeecOperation = {0};
955 
956 	TeecResult = OpteeClientApiLibInitialize();
957 	if (TeecResult != TEEC_SUCCESS)
958 		return TeecResult;
959 
960 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
961 	if (TeecResult != TEEC_SUCCESS)
962 		return TeecResult;
963 
964 	TeecResult = TEEC_OpenSession(&TeecContext,
965 				&TeecSession,
966 				TeecUuid,
967 				TEEC_LOGIN_PUBLIC,
968 				NULL,
969 				NULL,
970 				&ErrorOrigin);
971 	if (TeecResult != TEEC_SUCCESS)
972 		return TeecResult;
973 
974 	TeecOperation.params[0].value.a = key_id;
975 
976 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
977 						    TEEC_NONE,
978 						    TEEC_NONE,
979 						    TEEC_NONE);
980 
981 	TeecResult = TEEC_InvokeCommand(&TeecSession,
982 					STORAGE_CMD_OEM_OTP_KEY_IS_WRITTEN,
983 					&TeecOperation,
984 					&ErrorOrigin);
985 	if (TeecResult == TEEC_SUCCESS)
986 		*value = TeecOperation.params[0].value.b;
987 
988 	TEEC_CloseSession(&TeecSession);
989 	TEEC_FinalizeContext(&TeecContext);
990 
991 	return TeecResult;
992 }
993 
994 uint32_t trusty_set_oem_hr_otp_read_lock(enum RK_OEM_OTP_KEYID key_id)
995 {
996 	TEEC_Result TeecResult;
997 	TEEC_Context TeecContext;
998 	TEEC_Session TeecSession;
999 	uint32_t ErrorOrigin;
1000 
1001 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
1002 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1003 	TEEC_UUID *TeecUuid = &tempuuid;
1004 	TEEC_Operation TeecOperation = {0};
1005 
1006 	TeecResult = OpteeClientApiLibInitialize();
1007 	if (TeecResult != TEEC_SUCCESS)
1008 		return TeecResult;
1009 
1010 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1011 	if (TeecResult != TEEC_SUCCESS)
1012 		return TeecResult;
1013 
1014 	TeecResult = TEEC_OpenSession(&TeecContext,
1015 				&TeecSession,
1016 				TeecUuid,
1017 				TEEC_LOGIN_PUBLIC,
1018 				NULL,
1019 				NULL,
1020 				&ErrorOrigin);
1021 	if (TeecResult != TEEC_SUCCESS)
1022 		return TeecResult;
1023 
1024 	TeecOperation.params[0].value.a = key_id;
1025 
1026 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1027 						    TEEC_NONE,
1028 						    TEEC_NONE,
1029 						    TEEC_NONE);
1030 
1031 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1032 					STORAGE_CMD_SET_OEM_HR_OTP_READ_LOCK,
1033 					&TeecOperation,
1034 					&ErrorOrigin);
1035 	if (TeecResult != TEEC_SUCCESS)
1036 		goto exit;
1037 
1038 exit:
1039 	TEEC_CloseSession(&TeecSession);
1040 	TEEC_FinalizeContext(&TeecContext);
1041 
1042 	return TeecResult;
1043 }
1044 
1045 uint32_t trusty_oem_otp_key_cipher(enum RK_OEM_OTP_KEYID key_id, rk_cipher_config *config,
1046 				   uint32_t src_phys_addr, uint32_t dst_phys_addr,
1047 				   uint32_t len)
1048 {
1049 	TEEC_Result TeecResult;
1050 	TEEC_Context TeecContext;
1051 	TEEC_Session TeecSession;
1052 	TEEC_Operation TeecOperation = {0};
1053 	uint32_t ErrorOrigin;
1054 	TEEC_UUID uuid = RK_CRYPTO_SERVICE_UUID;
1055 	TEEC_SharedMemory SharedMem_config = {0};
1056 
1057 	if (key_id != RK_OEM_OTP_KEY0 &&
1058 	    key_id != RK_OEM_OTP_KEY1 &&
1059 	    key_id != RK_OEM_OTP_KEY2 &&
1060 	    key_id != RK_OEM_OTP_KEY3 &&
1061 	    key_id != RK_OEM_OTP_KEY_FW)
1062 		return TEEC_ERROR_BAD_PARAMETERS;
1063 
1064 	if (!config)
1065 		return TEEC_ERROR_BAD_PARAMETERS;
1066 
1067 	if (config->algo != RK_ALGO_AES && config->algo != RK_ALGO_SM4)
1068 		return TEEC_ERROR_BAD_PARAMETERS;
1069 
1070 	if (config->mode >= RK_CIPHER_MODE_XTS)
1071 		return TEEC_ERROR_BAD_PARAMETERS;
1072 
1073 	if (config->operation != RK_MODE_ENCRYPT &&
1074 	    config->operation != RK_MODE_DECRYPT)
1075 		return TEEC_ERROR_BAD_PARAMETERS;
1076 
1077 	if (config->key_len != 16 &&
1078 	    config->key_len != 24 &&
1079 	    config->key_len != 32)
1080 		return TEEC_ERROR_BAD_PARAMETERS;
1081 
1082 	if (key_id == RK_OEM_OTP_KEY_FW && config->key_len != 16)
1083 		return TEEC_ERROR_BAD_PARAMETERS;
1084 
1085 #if defined(CONFIG_ROCKCHIP_RV1126)
1086 	if (config->key_len == 24)
1087 		return TEEC_ERROR_BAD_PARAMETERS;
1088 #endif
1089 
1090 	if (len % AES_BLOCK_SIZE ||
1091 	    len == 0)
1092 		return TEEC_ERROR_BAD_PARAMETERS;
1093 
1094 	if (!src_phys_addr || !dst_phys_addr)
1095 		return TEEC_ERROR_BAD_PARAMETERS;
1096 
1097 	TeecResult = OpteeClientApiLibInitialize();
1098 	if (TeecResult != TEEC_SUCCESS)
1099 		return TeecResult;
1100 
1101 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1102 	if (TeecResult != TEEC_SUCCESS)
1103 		return TeecResult;
1104 
1105 	TeecResult = TEEC_OpenSession(&TeecContext,
1106 				      &TeecSession,
1107 				      &uuid,
1108 				      TEEC_LOGIN_PUBLIC,
1109 				      NULL,
1110 				      NULL,
1111 				      &ErrorOrigin);
1112 	if (TeecResult != TEEC_SUCCESS)
1113 		goto exit;
1114 
1115 	SharedMem_config.size = sizeof(rk_cipher_config);
1116 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem_config);
1117 	if (TeecResult != TEEC_SUCCESS)
1118 		goto exit;
1119 
1120 	memcpy(SharedMem_config.buffer, config, sizeof(rk_cipher_config));
1121 	TeecOperation.params[0].value.a       = key_id;
1122 	TeecOperation.params[1].tmpref.buffer = SharedMem_config.buffer;
1123 	TeecOperation.params[1].tmpref.size   = SharedMem_config.size;
1124 	TeecOperation.params[2].value.a       = src_phys_addr;
1125 	TeecOperation.params[2].value.b       = len;
1126 	TeecOperation.params[3].value.a       = dst_phys_addr;
1127 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1128 						    TEEC_MEMREF_TEMP_INPUT,
1129 						    TEEC_VALUE_INPUT,
1130 						    TEEC_VALUE_INPUT);
1131 
1132 	crypto_flush_cacheline(src_phys_addr, len);
1133 	crypto_flush_cacheline(dst_phys_addr, len);
1134 
1135 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1136 					CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER,
1137 					&TeecOperation,
1138 					&ErrorOrigin);
1139 
1140 	crypto_invalidate_cacheline(dst_phys_addr, len);
1141 
1142 exit:
1143 	TEEC_ReleaseSharedMemory(&SharedMem_config);
1144 	TEEC_CloseSession(&TeecSession);
1145 	TEEC_FinalizeContext(&TeecContext);
1146 	return TeecResult;
1147 }
1148 
1149 uint32_t trusty_write_oem_hdcp_key(enum RK_HDCP_KEYID key_id,
1150 				  uint8_t *byte_buf, uint32_t byte_len)
1151 {
1152 	TEEC_Result TeecResult;
1153 	TEEC_Context TeecContext;
1154 	TEEC_Session TeecSession;
1155 	uint32_t ErrorOrigin;
1156 
1157 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
1158 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1159 	TEEC_UUID *TeecUuid = &tempuuid;
1160 	TEEC_Operation TeecOperation = {0};
1161 
1162 	TeecResult = OpteeClientApiLibInitialize();
1163 	if (TeecResult != TEEC_SUCCESS)
1164 		return TeecResult;
1165 
1166 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1167 	if (TeecResult != TEEC_SUCCESS)
1168 		return TeecResult;
1169 
1170 	TeecResult = TEEC_OpenSession(&TeecContext,
1171 				&TeecSession,
1172 				TeecUuid,
1173 				TEEC_LOGIN_PUBLIC,
1174 				NULL,
1175 				NULL,
1176 				&ErrorOrigin);
1177 	if (TeecResult != TEEC_SUCCESS)
1178 		return TeecResult;
1179 
1180 	TeecOperation.params[0].value.a = key_id;
1181 
1182 	TEEC_SharedMemory SharedMem = {0};
1183 
1184 	SharedMem.size = byte_len;
1185 	SharedMem.flags = 0;
1186 
1187 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem);
1188 	if (TeecResult != TEEC_SUCCESS)
1189 		goto exit;
1190 
1191 	TeecOperation.params[1].tmpref.buffer = SharedMem.buffer;
1192 	TeecOperation.params[1].tmpref.size = SharedMem.size;
1193 
1194 	memcpy(SharedMem.buffer, byte_buf, SharedMem.size);
1195 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1196 						    TEEC_MEMREF_TEMP_INPUT,
1197 						    TEEC_NONE,
1198 						    TEEC_NONE);
1199 
1200 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1201 					STORAGE_CMD_WRITE_OEM_HDCP_KEY,
1202 					&TeecOperation,
1203 					&ErrorOrigin);
1204 	if (TeecResult != TEEC_SUCCESS)
1205 		goto exit;
1206 
1207 exit:
1208 	TEEC_ReleaseSharedMemory(&SharedMem);
1209 	TEEC_CloseSession(&TeecSession);
1210 	TEEC_FinalizeContext(&TeecContext);
1211 
1212 	return TeecResult;
1213 }
1214 
1215 uint32_t trusty_oem_hdcp_key_is_written(enum RK_HDCP_KEYID key_id, uint8_t *value)
1216 {
1217 	TEEC_Result TeecResult;
1218 	TEEC_Context TeecContext;
1219 	TEEC_Session TeecSession;
1220 	uint32_t ErrorOrigin;
1221 
1222 	*value = 0xFF;
1223 
1224 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
1225 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1226 	TEEC_UUID *TeecUuid = &tempuuid;
1227 	TEEC_Operation TeecOperation = {0};
1228 
1229 	TeecResult = OpteeClientApiLibInitialize();
1230 	if (TeecResult != TEEC_SUCCESS)
1231 		return TeecResult;
1232 
1233 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1234 	if (TeecResult != TEEC_SUCCESS)
1235 		return TeecResult;
1236 
1237 	TeecResult = TEEC_OpenSession(&TeecContext,
1238 				&TeecSession,
1239 				TeecUuid,
1240 				TEEC_LOGIN_PUBLIC,
1241 				NULL,
1242 				NULL,
1243 				&ErrorOrigin);
1244 	if (TeecResult != TEEC_SUCCESS)
1245 		return TeecResult;
1246 
1247 	TeecOperation.params[0].value.a = key_id;
1248 
1249 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
1250 						    TEEC_NONE,
1251 						    TEEC_NONE,
1252 						    TEEC_NONE);
1253 
1254 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1255 					STORAGE_CMD_OEM_HDCP_KEY_IS_WRITTEN,
1256 					&TeecOperation,
1257 					&ErrorOrigin);
1258 	if (TeecResult == TEEC_SUCCESS)
1259 		*value = TeecOperation.params[0].value.b;
1260 
1261 	TEEC_CloseSession(&TeecSession);
1262 	TEEC_FinalizeContext(&TeecContext);
1263 
1264 	return TeecResult;
1265 }
1266 
1267 uint32_t trusty_set_oem_hdcp_key_mask(enum RK_HDCP_KEYID key_id)
1268 {
1269 	TEEC_Result TeecResult;
1270 	TEEC_Context TeecContext;
1271 	TEEC_Session TeecSession;
1272 	uint32_t ErrorOrigin;
1273 
1274 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
1275 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1276 	TEEC_UUID *TeecUuid = &tempuuid;
1277 	TEEC_Operation TeecOperation = {0};
1278 
1279 	TeecResult = OpteeClientApiLibInitialize();
1280 	if (TeecResult != TEEC_SUCCESS)
1281 		return TeecResult;
1282 
1283 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1284 	if (TeecResult != TEEC_SUCCESS)
1285 		return TeecResult;
1286 
1287 	TeecResult = TEEC_OpenSession(&TeecContext,
1288 				&TeecSession,
1289 				TeecUuid,
1290 				TEEC_LOGIN_PUBLIC,
1291 				NULL,
1292 				NULL,
1293 				&ErrorOrigin);
1294 	if (TeecResult != TEEC_SUCCESS)
1295 		return TeecResult;
1296 
1297 	TeecOperation.params[0].value.a = key_id;
1298 
1299 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1300 						    TEEC_NONE,
1301 						    TEEC_NONE,
1302 						    TEEC_NONE);
1303 
1304 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1305 					STORAGE_CMD_SET_OEM_HDCP_KEY_MASK,
1306 					&TeecOperation,
1307 					&ErrorOrigin);
1308 	if (TeecResult != TEEC_SUCCESS)
1309 		goto exit;
1310 
1311 exit:
1312 	TEEC_CloseSession(&TeecSession);
1313 	TEEC_FinalizeContext(&TeecContext);
1314 
1315 	return TeecResult;
1316 }
1317 
1318 uint32_t trusty_oem_user_ta_transfer(void)
1319 {
1320 	TEEC_Result TeecResult;
1321 	TEEC_Context TeecContext;
1322 	TEEC_Session TeecSession;
1323 	uint32_t ErrorOrigin;
1324 	TEEC_UUID tempuuid = { 0x1db57234, 0xdacd, 0x462d,
1325 		{ 0x9b, 0xb1, 0xae, 0x79, 0xde, 0x44, 0xe2, 0xa5} };
1326 	TEEC_UUID *TeecUuid = &tempuuid;
1327 	TEEC_Operation TeecOperation = {0};
1328 	const uint8_t transfer_inout[] = "Transfer data test.";
1329 
1330 	TeecResult = OpteeClientApiLibInitialize();
1331 	if (TeecResult != TEEC_SUCCESS)
1332 		return TeecResult;
1333 
1334 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1335 	if (TeecResult != TEEC_SUCCESS)
1336 		return TeecResult;
1337 
1338 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
1339 						TEEC_NONE,
1340 						TEEC_NONE,
1341 						TEEC_NONE);
1342 
1343 	TeecResult = TEEC_OpenSession(&TeecContext,
1344 				&TeecSession,
1345 				TeecUuid,
1346 				TEEC_LOGIN_PUBLIC,
1347 				NULL,
1348 				&TeecOperation,
1349 				&ErrorOrigin);
1350 	if (TeecResult != TEEC_SUCCESS)
1351 		return TeecResult;
1352 
1353 	TEEC_SharedMemory SharedMem0 = {0};
1354 
1355 	SharedMem0.size = sizeof(transfer_inout);
1356 	SharedMem0.flags = 0;
1357 
1358 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1359 	if (TeecResult != TEEC_SUCCESS)
1360 		goto exit;
1361 
1362 	memcpy(SharedMem0.buffer, transfer_inout, SharedMem0.size);
1363 
1364 	TEEC_SharedMemory SharedMem1 = {0};
1365 
1366 	SharedMem1.size = sizeof(transfer_inout);
1367 	SharedMem1.flags = 0;
1368 
1369 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1370 	if (TeecResult != TEEC_SUCCESS)
1371 		goto exit;
1372 
1373 	TeecOperation.params[0].value.a = 66;
1374 	TeecOperation.params[1].tmpref.buffer = SharedMem0.buffer;
1375 	TeecOperation.params[1].tmpref.size = SharedMem0.size;
1376 	TeecOperation.params[2].tmpref.buffer = SharedMem1.buffer;
1377 	TeecOperation.params[2].tmpref.size = SharedMem1.size;
1378 
1379 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
1380 						TEEC_MEMREF_TEMP_INPUT,
1381 						TEEC_MEMREF_TEMP_OUTPUT,
1382 						TEEC_NONE);
1383 
1384 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1385 					102,
1386 					&TeecOperation,
1387 					&ErrorOrigin);
1388 	if (TeecResult != TEEC_SUCCESS)
1389 		goto exit;
1390 
1391 	//Check the result
1392 	if (TeecOperation.params[0].value.a == 66 + 1 &&
1393 	    TeecOperation.params[0].value.b == TeecOperation.params[0].value.a)
1394 		printf("test value : Pass!\n");
1395 	else
1396 		printf("test value : Fail! (mismatch values)\n");
1397 
1398 	if (memcmp(SharedMem1.buffer, transfer_inout, sizeof(transfer_inout)) == 0)
1399 		printf("test buffer : Pass!\n");
1400 	else
1401 		printf("test buffer : Fail! (mismatch buffer)\n");
1402 
1403 exit:
1404 	TEEC_ReleaseSharedMemory(&SharedMem0);
1405 	TEEC_ReleaseSharedMemory(&SharedMem1);
1406 	TEEC_CloseSession(&TeecSession);
1407 	TEEC_FinalizeContext(&TeecContext);
1408 
1409 	return TeecResult;
1410 }
1411 
1412 uint32_t trusty_oem_user_ta_storage(void)
1413 {
1414 	TEEC_Result TeecResult;
1415 	TEEC_Context TeecContext;
1416 	TEEC_Session TeecSession;
1417 	uint32_t ErrorOrigin;
1418 	TEEC_UUID tempuuid = { 0x1db57234, 0xdacd, 0x462d,
1419 		{ 0x9b, 0xb1, 0xae, 0x79, 0xde, 0x44, 0xe2, 0xa5} };
1420 	TEEC_UUID *TeecUuid = &tempuuid;
1421 	TEEC_Operation TeecOperation = {0};
1422 
1423 	TeecResult = OpteeClientApiLibInitialize();
1424 	if (TeecResult != TEEC_SUCCESS)
1425 		return TeecResult;
1426 
1427 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1428 	if (TeecResult != TEEC_SUCCESS)
1429 		return TeecResult;
1430 
1431 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
1432 						TEEC_NONE,
1433 						TEEC_NONE,
1434 						TEEC_NONE);
1435 
1436 	TeecResult = TEEC_OpenSession(&TeecContext,
1437 				&TeecSession,
1438 				TeecUuid,
1439 				TEEC_LOGIN_PUBLIC,
1440 				NULL,
1441 				&TeecOperation,
1442 				&ErrorOrigin);
1443 	if (TeecResult != TEEC_SUCCESS)
1444 		return TeecResult;
1445 
1446 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
1447 						TEEC_NONE,
1448 						TEEC_NONE,
1449 						TEEC_NONE);
1450 
1451 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1452 					103,
1453 					&TeecOperation,
1454 					&ErrorOrigin);
1455 	if (TeecResult != TEEC_SUCCESS)
1456 		goto exit;
1457 
1458 exit:
1459 	TEEC_CloseSession(&TeecSession);
1460 	TEEC_FinalizeContext(&TeecContext);
1461 
1462 	return TeecResult;
1463 }
1464 
1465 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size)
1466 {
1467 	TEEC_Result TeecResult;
1468 	TEEC_Context TeecContext;
1469 	TEEC_Session TeecSession;
1470 	uint32_t ErrorOrigin;
1471 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1472 				{ 0xa8, 0x69, 0x9c, 0xe6,
1473 				  0x88, 0x6c, 0x5d, 0x5d
1474 				}
1475 			     };
1476 	TEEC_UUID *TeecUuid = &tempuuid;
1477 	TEEC_Operation TeecOperation = {0};
1478 	struct blk_desc *dev_desc;
1479 	dev_desc = rockchip_get_bootdev();
1480 	if (!dev_desc) {
1481 		printf("%s: dev_desc is NULL!\n", __func__);
1482 		return -TEEC_ERROR_GENERIC;
1483 	}
1484 
1485 	TeecResult = OpteeClientApiLibInitialize();
1486 	if (TeecResult != TEEC_SUCCESS)
1487 		return TeecResult;
1488 
1489 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1490 	if (TeecResult != TEEC_SUCCESS)
1491 		return TeecResult;
1492 
1493 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1494 						TEEC_NONE,
1495 						TEEC_NONE,
1496 						TEEC_NONE);
1497 	/*0 nand or emmc "security" partition , 1 rpmb*/
1498 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
1499 		TeecOperation.params[0].value.a = 1;
1500 	else
1501 		TeecOperation.params[0].value.a = 0;
1502 
1503 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1504 	TeecOperation.params[0].value.a = 0;
1505 #endif
1506 
1507 	TeecResult = TEEC_OpenSession(&TeecContext,
1508 				      &TeecSession,
1509 				      TeecUuid,
1510 				      TEEC_LOGIN_PUBLIC,
1511 				      NULL,
1512 					&TeecOperation,
1513 				      &ErrorOrigin);
1514 	if (TeecResult != TEEC_SUCCESS)
1515 		return TeecResult;
1516 
1517 	TEEC_SharedMemory SharedMem0 = {0};
1518 
1519 	SharedMem0.size = *dh_size;
1520 	SharedMem0.flags = 0;
1521 
1522 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1523 	if (TeecResult != TEEC_SUCCESS)
1524 		goto exit;
1525 
1526 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1527 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1528 
1529 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1530 						    TEEC_NONE,
1531 						    TEEC_NONE,
1532 						    TEEC_NONE);
1533 
1534 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1535 					143,
1536 					&TeecOperation,
1537 					&ErrorOrigin);
1538 	if (TeecResult != TEEC_SUCCESS)
1539 		goto exit;
1540 
1541 	*dh_size = TeecOperation.params[0].tmpref.size;
1542 	memcpy(dh, SharedMem0.buffer, SharedMem0.size);
1543 exit:
1544 	TEEC_ReleaseSharedMemory(&SharedMem0);
1545 	TEEC_CloseSession(&TeecSession);
1546 	TEEC_FinalizeContext(&TeecContext);
1547 
1548 	return TeecResult;
1549 }
1550 
1551 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size)
1552 {
1553 	TEEC_Result TeecResult;
1554 	TEEC_Context TeecContext;
1555 	TEEC_Session TeecSession;
1556 	uint32_t ErrorOrigin;
1557 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1558 				{ 0xa8, 0x69, 0x9c, 0xe6,
1559 				  0x88, 0x6c, 0x5d, 0x5d
1560 				}
1561 			     };
1562 	TEEC_UUID *TeecUuid = &tempuuid;
1563 	TEEC_Operation TeecOperation = {0};
1564 	struct blk_desc *dev_desc;
1565 	dev_desc = rockchip_get_bootdev();
1566 	if (!dev_desc) {
1567 		printf("%s: dev_desc is NULL!\n", __func__);
1568 		return -TEEC_ERROR_GENERIC;
1569 	}
1570 
1571 	TeecResult = OpteeClientApiLibInitialize();
1572 	if (TeecResult != TEEC_SUCCESS)
1573 		return TeecResult;
1574 
1575 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1576 	if (TeecResult != TEEC_SUCCESS)
1577 		return TeecResult;
1578 
1579 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1580 						TEEC_NONE,
1581 						TEEC_NONE,
1582 						TEEC_NONE);
1583 	/*0 nand or emmc "security" partition , 1 rpmb*/
1584 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
1585 		TeecOperation.params[0].value.a = 1;
1586 	else
1587 		TeecOperation.params[0].value.a = 0;
1588 
1589 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1590 	TeecOperation.params[0].value.a = 0;
1591 #endif
1592 
1593 	TeecResult = TEEC_OpenSession(&TeecContext,
1594 				      &TeecSession,
1595 				      TeecUuid,
1596 				      TEEC_LOGIN_PUBLIC,
1597 				      NULL,
1598 					&TeecOperation,
1599 				      &ErrorOrigin);
1600 	if (TeecResult != TEEC_SUCCESS)
1601 		return TeecResult;
1602 
1603 	TEEC_SharedMemory SharedMem0 = {0};
1604 
1605 	SharedMem0.size = *uuid_size;
1606 	SharedMem0.flags = 0;
1607 
1608 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1609 	if (TeecResult != TEEC_SUCCESS)
1610 		goto exit;
1611 
1612 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1613 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1614 
1615 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1616 						    TEEC_NONE,
1617 						    TEEC_NONE,
1618 						    TEEC_NONE);
1619 
1620 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1621 					144,
1622 					&TeecOperation,
1623 					&ErrorOrigin);
1624 	if (TeecResult != TEEC_SUCCESS)
1625 		goto exit;
1626 
1627 	*uuid_size = TeecOperation.params[0].tmpref.size;
1628 	memcpy(uuid, SharedMem0.buffer, SharedMem0.size);
1629 exit:
1630 	TEEC_ReleaseSharedMemory(&SharedMem0);
1631 	TEEC_CloseSession(&TeecSession);
1632 	TEEC_FinalizeContext(&TeecContext);
1633 
1634 	return TeecResult;
1635 }
1636 
1637 uint32_t trusty_attest_get_ca(uint8_t *operation_start,
1638 			      uint32_t *operation_size,
1639 			      uint8_t *out,
1640 			      uint32_t *out_len)
1641 {
1642 	TEEC_Result TeecResult;
1643 	TEEC_Context TeecContext;
1644 	TEEC_Session TeecSession;
1645 	uint32_t ErrorOrigin;
1646 
1647 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1648 				{ 0xa8, 0x69, 0x9c, 0xe6,
1649 				  0x88, 0x6c, 0x5d, 0x5d
1650 				}
1651 			     };
1652 
1653 	TEEC_UUID *TeecUuid = &tempuuid;
1654 	TEEC_Operation TeecOperation = {0};
1655 	struct blk_desc *dev_desc;
1656 	dev_desc = rockchip_get_bootdev();
1657 	if (!dev_desc) {
1658 		printf("%s: dev_desc is NULL!\n", __func__);
1659 		return -TEEC_ERROR_GENERIC;
1660 	}
1661 
1662 	TeecResult = OpteeClientApiLibInitialize();
1663 	if (TeecResult != TEEC_SUCCESS)
1664 		return TeecResult;
1665 
1666 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1667 	if (TeecResult != TEEC_SUCCESS)
1668 		return TeecResult;
1669 
1670 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1671 						TEEC_NONE,
1672 						TEEC_NONE,
1673 						TEEC_NONE);
1674 	/*0 nand or emmc "security" partition , 1 rpmb*/
1675 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
1676 		TeecOperation.params[0].value.a = 1;
1677 	else
1678 		TeecOperation.params[0].value.a = 0;
1679 
1680 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1681 	TeecOperation.params[0].value.a = 0;
1682 #endif
1683 
1684 	TeecResult = TEEC_OpenSession(&TeecContext,
1685 				      &TeecSession,
1686 				      TeecUuid,
1687 				      TEEC_LOGIN_PUBLIC,
1688 				      NULL,
1689 					&TeecOperation,
1690 				      &ErrorOrigin);
1691 	if (TeecResult != TEEC_SUCCESS)
1692 		return TeecResult;
1693 
1694 	TEEC_SharedMemory SharedMem0 = {0};
1695 
1696 	SharedMem0.size = *operation_size;
1697 	SharedMem0.flags = 0;
1698 
1699 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1700 	if (TeecResult != TEEC_SUCCESS)
1701 		goto exit;
1702 
1703 	memcpy(SharedMem0.buffer, operation_start, SharedMem0.size);
1704 
1705 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1706 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1707 
1708 	TEEC_SharedMemory SharedMem1 = {0};
1709 
1710 	SharedMem1.size = *out_len;
1711 	SharedMem1.flags = 0;
1712 
1713 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1714 	if (TeecResult != TEEC_SUCCESS)
1715 		goto exit;
1716 
1717 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1718 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1719 
1720 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1721 						    TEEC_MEMREF_TEMP_INOUT,
1722 						    TEEC_NONE,
1723 						    TEEC_NONE);
1724 
1725 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1726 					145,
1727 					&TeecOperation,
1728 					&ErrorOrigin);
1729 	if (TeecResult != TEEC_SUCCESS)
1730 		goto exit;
1731 
1732 	*out_len = TeecOperation.params[1].tmpref.size;
1733 	memcpy(out, SharedMem1.buffer, SharedMem1.size);
1734 exit:
1735 	TEEC_ReleaseSharedMemory(&SharedMem0);
1736 	TEEC_ReleaseSharedMemory(&SharedMem1);
1737 	TEEC_CloseSession(&TeecSession);
1738 	TEEC_FinalizeContext(&TeecContext);
1739 
1740 	return TeecResult;
1741 }
1742 
1743 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size)
1744 {
1745 	TEEC_Result TeecResult;
1746 	TEEC_Context TeecContext;
1747 	TEEC_Session TeecSession;
1748 	uint32_t ErrorOrigin;
1749 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1750 				{ 0xa8, 0x69, 0x9c, 0xe6,
1751 				  0x88, 0x6c, 0x5d, 0x5d
1752 				}
1753 			     };
1754 	TEEC_UUID *TeecUuid = &tempuuid;
1755 	TEEC_Operation TeecOperation = {0};
1756 	struct blk_desc *dev_desc;
1757 	dev_desc = rockchip_get_bootdev();
1758 	if (!dev_desc) {
1759 		printf("%s: dev_desc is NULL!\n", __func__);
1760 		return -TEEC_ERROR_GENERIC;
1761 	}
1762 	TeecResult = OpteeClientApiLibInitialize();
1763 	if (TeecResult != TEEC_SUCCESS)
1764 		return TeecResult;
1765 
1766 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1767 	if (TeecResult != TEEC_SUCCESS)
1768 		return TeecResult;
1769 
1770 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1771 						TEEC_NONE,
1772 						TEEC_NONE,
1773 						TEEC_NONE);
1774 	/*0 nand or emmc "security" partition , 1 rpmb*/
1775 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
1776 		TeecOperation.params[0].value.a = 1;
1777 	else
1778 		TeecOperation.params[0].value.a = 0;
1779 
1780 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1781 	TeecOperation.params[0].value.a = 0;
1782 #endif
1783 
1784 	TeecResult = TEEC_OpenSession(&TeecContext,
1785 					&TeecSession,
1786 					TeecUuid,
1787 					TEEC_LOGIN_PUBLIC,
1788 					NULL,
1789 					&TeecOperation,
1790 					&ErrorOrigin);
1791 	if (TeecResult != TEEC_SUCCESS)
1792 		return TeecResult;
1793 
1794 	TEEC_SharedMemory SharedMem0 = {0};
1795 
1796 	SharedMem0.size = *ca_response_size;
1797 	SharedMem0.flags = 0;
1798 
1799 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1800 	if (TeecResult != TEEC_SUCCESS)
1801 		goto exit;
1802 
1803 	memcpy(SharedMem0.buffer, ca_response, SharedMem0.size);
1804 
1805 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1806 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1807 
1808 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1809 						    TEEC_NONE,
1810 						    TEEC_NONE,
1811 						    TEEC_NONE);
1812 
1813 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1814 					146,
1815 					&TeecOperation,
1816 					&ErrorOrigin);
1817 	if (TeecResult != TEEC_SUCCESS)
1818 		goto exit;
1819 exit:
1820 	TEEC_ReleaseSharedMemory(&SharedMem0);
1821 	TEEC_CloseSession(&TeecSession);
1822 	TEEC_FinalizeContext(&TeecContext);
1823 
1824 	return TeecResult;
1825 }
1826