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