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