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