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