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