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