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