xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientInterface.c (revision c2ba77d93f696c0ccb8f2b653571104e7b4afb4e)
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 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length)
809 {
810 	TEEC_Result TeecResult;
811 	TEEC_Context TeecContext;
812 	TEEC_Session TeecSession;
813 	uint32_t ErrorOrigin;
814 
815 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
816 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
817 	TEEC_UUID *TeecUuid = &tempuuid;
818 	TEEC_Operation TeecOperation = {0};
819 
820 	OpteeClientApiLibInitialize();
821 
822 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
823 
824 	TeecResult = TEEC_OpenSession(&TeecContext,
825 				&TeecSession,
826 				TeecUuid,
827 				TEEC_LOGIN_PUBLIC,
828 				NULL,
829 				NULL,
830 				&ErrorOrigin);
831 
832 	TEEC_SharedMemory SharedMem0 = {0};
833 
834 	SharedMem0.size = length * sizeof(uint32_t);
835 	SharedMem0.flags = 0;
836 
837 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
838 
839 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
840 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
841 
842 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
843 						TEEC_NONE,
844 						TEEC_NONE,
845 						TEEC_NONE);
846 
847 	TeecResult = TEEC_InvokeCommand(&TeecSession,
848 					0,
849 					&TeecOperation,
850 					&ErrorOrigin);
851 
852 	if (TeecResult == TEEC_SUCCESS)
853 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
854 
855 	TEEC_ReleaseSharedMemory(&SharedMem0);
856 	TEEC_CloseSession(&TeecSession);
857 	TEEC_FinalizeContext(&TeecContext);
858 
859 	return TeecResult;
860 }
861 
862 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length)
863 {
864 	TEEC_Result TeecResult;
865 	TEEC_Context TeecContext;
866 	TEEC_Session TeecSession;
867 	uint32_t ErrorOrigin;
868 
869 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
870 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
871 	TEEC_UUID *TeecUuid = &tempuuid;
872 	TEEC_Operation TeecOperation = {0};
873 
874 	OpteeClientApiLibInitialize();
875 
876 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
877 
878 	TeecResult = TEEC_OpenSession(&TeecContext,
879 				&TeecSession,
880 				TeecUuid,
881 				TEEC_LOGIN_PUBLIC,
882 				NULL,
883 				NULL,
884 				&ErrorOrigin);
885 
886 	TEEC_SharedMemory SharedMem0 = {0};
887 
888 	SharedMem0.size = length * sizeof(uint32_t);
889 	SharedMem0.flags = 0;
890 
891 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
892 
893 	memcpy(SharedMem0.buffer, buf, SharedMem0.size);
894 
895 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
896 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
897 
898 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
899 						TEEC_NONE,
900 						TEEC_NONE,
901 						TEEC_NONE);
902 
903 	TeecResult = TEEC_InvokeCommand(&TeecSession,
904 					1,
905 					&TeecOperation,
906 					&ErrorOrigin);
907 
908 	TEEC_ReleaseSharedMemory(&SharedMem0);
909 	TEEC_CloseSession(&TeecSession);
910 	TEEC_FinalizeContext(&TeecContext);
911 
912 	return TeecResult;
913 }
914 
915 uint32_t notify_optee_rpmb_ta(void)
916 {
917 	TEEC_Result TeecResult;
918 	TEEC_Context TeecContext;
919 	TEEC_Session TeecSession;
920 	uint32_t ErrorOrigin;
921 	TEEC_UUID  tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
922 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
923 	TEEC_UUID *TeecUuid = &tempuuid;
924 	TEEC_Operation TeecOperation = {0};
925 
926 	OpteeClientApiLibInitialize();
927 
928 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
929 
930 	TeecResult = TEEC_OpenSession(&TeecContext,
931 				&TeecSession,
932 				TeecUuid,
933 				TEEC_LOGIN_PUBLIC,
934 				NULL,
935 				NULL,
936 				&ErrorOrigin);
937 
938 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
939 						TEEC_NONE,
940 						TEEC_NONE,
941 						TEEC_NONE);
942 
943 	TeecResult = TEEC_InvokeCommand(&TeecSession,
944 					2,
945 					&TeecOperation,
946 					&ErrorOrigin);
947 
948 	TEEC_CloseSession(&TeecSession);
949 	TEEC_FinalizeContext(&TeecContext);
950 
951 	return TeecResult;
952 }
953 
954 uint32_t notify_optee_efuse_ta(void)
955 {
956 	TEEC_Result TeecResult;
957 	TEEC_Context TeecContext;
958 	TEEC_Session TeecSession;
959 	uint32_t ErrorOrigin;
960 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
961 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
962 
963 	TEEC_UUID *TeecUuid = &tempuuid;
964 	TEEC_Operation TeecOperation = {0};
965 
966 	OpteeClientApiLibInitialize();
967 
968 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
969 
970 	TeecResult = TEEC_OpenSession(&TeecContext,
971 				&TeecSession,
972 				TeecUuid,
973 				TEEC_LOGIN_PUBLIC,
974 				NULL,
975 				NULL,
976 				&ErrorOrigin);
977 
978 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
979 						TEEC_NONE,
980 						TEEC_NONE,
981 						TEEC_NONE);
982 
983 	TeecResult = TEEC_InvokeCommand(&TeecSession,
984 					2,
985 					&TeecOperation,
986 					&ErrorOrigin);
987 
988 	TEEC_CloseSession(&TeecSession);
989 	TEEC_FinalizeContext(&TeecContext);
990 
991 	return TeecResult;
992 }
993 
994 uint32_t trusty_notify_optee_uboot_end(void)
995 {
996 	TEEC_Result res;
997 	res = notify_optee_rpmb_ta();
998 	res |= notify_optee_efuse_ta();
999 	return res;
1000 }
1001 
1002 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length)
1003 {
1004 	TEEC_Result TeecResult;
1005 	TEEC_Context TeecContext;
1006 	TEEC_Session TeecSession;
1007 	uint32_t ErrorOrigin;
1008 
1009 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1010 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1011 	TEEC_UUID *TeecUuid = &tempuuid;
1012 	TEEC_Operation TeecOperation = {0};
1013 
1014 	OpteeClientApiLibInitialize();
1015 
1016 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1017 
1018 	TeecResult = TEEC_OpenSession(&TeecContext,
1019 				&TeecSession,
1020 				TeecUuid,
1021 				TEEC_LOGIN_PUBLIC,
1022 				NULL,
1023 				NULL,
1024 				&ErrorOrigin);
1025 
1026 	TEEC_SharedMemory SharedMem0 = {0};
1027 
1028 	SharedMem0.size = length * sizeof(uint32_t);
1029 	SharedMem0.flags = 0;
1030 
1031 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1032 
1033 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1034 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1035 
1036 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
1037 						TEEC_NONE,
1038 						TEEC_NONE,
1039 						TEEC_NONE);
1040 
1041 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1042 					3,
1043 					&TeecOperation,
1044 					&ErrorOrigin);
1045 
1046 	if (TeecResult == TEEC_SUCCESS)
1047 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
1048 
1049 	TEEC_ReleaseSharedMemory(&SharedMem0);
1050 	TEEC_CloseSession(&TeecSession);
1051 	TEEC_FinalizeContext(&TeecContext);
1052 
1053 	return TeecResult;
1054 }
1055 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length)
1056 {
1057 	TEEC_Result TeecResult;
1058 	TEEC_Context TeecContext;
1059 	TEEC_Session TeecSession;
1060 	uint32_t ErrorOrigin;
1061 
1062 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1063 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1064 	TEEC_UUID *TeecUuid = &tempuuid;
1065 	TEEC_Operation TeecOperation = {0};
1066 
1067 	OpteeClientApiLibInitialize();
1068 
1069 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1070 
1071 	TeecResult = TEEC_OpenSession(&TeecContext,
1072 				&TeecSession,
1073 				TeecUuid,
1074 				TEEC_LOGIN_PUBLIC,
1075 				NULL,
1076 				NULL,
1077 				&ErrorOrigin);
1078 
1079 	TEEC_SharedMemory SharedMem0 = {0};
1080 
1081 	SharedMem0.size = length * sizeof(uint32_t);
1082 	SharedMem0.flags = 0;
1083 
1084 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1085 
1086 	memcpy(SharedMem0.buffer, buf, SharedMem0.size);
1087 
1088 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1089 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1090 
1091 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1092 						TEEC_NONE,
1093 						TEEC_NONE,
1094 						TEEC_NONE);
1095 
1096 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1097 					4,
1098 					&TeecOperation,
1099 					&ErrorOrigin);
1100 
1101 	TEEC_ReleaseSharedMemory(&SharedMem0);
1102 	TEEC_CloseSession(&TeecSession);
1103 	TEEC_FinalizeContext(&TeecContext);
1104 
1105 	return TeecResult;
1106 }
1107 
1108 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag)
1109 {
1110 	TEEC_Result TeecResult;
1111 	TEEC_Context TeecContext;
1112 	TEEC_Session TeecSession;
1113 	uint32_t ErrorOrigin;
1114 	uint32_t bootflag;
1115 
1116 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1117 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1118 	TEEC_UUID *TeecUuid = &tempuuid;
1119 	TEEC_Operation TeecOperation = {0};
1120 
1121 	OpteeClientApiLibInitialize();
1122 
1123 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1124 
1125 	TeecResult = TEEC_OpenSession(&TeecContext,
1126 				&TeecSession,
1127 				TeecUuid,
1128 				TEEC_LOGIN_PUBLIC,
1129 				NULL,
1130 				NULL,
1131 				&ErrorOrigin);
1132 
1133 	TEEC_SharedMemory SharedMem0 = {0};
1134 
1135 	SharedMem0.size = 1 * sizeof(uint32_t);
1136 	SharedMem0.flags = 0;
1137 
1138 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1139 
1140 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1141 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1142 
1143 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
1144 						TEEC_NONE,
1145 						TEEC_NONE,
1146 						TEEC_NONE);
1147 
1148 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1149 					5,
1150 					&TeecOperation,
1151 					&ErrorOrigin);
1152 
1153 	if (TeecResult == TEEC_SUCCESS) {
1154 		memcpy(&bootflag, SharedMem0.buffer, SharedMem0.size);
1155 		if (bootflag == 0x000000FF)
1156 			*flag = 1;
1157 	}
1158 
1159 	TEEC_ReleaseSharedMemory(&SharedMem0);
1160 	TEEC_CloseSession(&TeecSession);
1161 	TEEC_FinalizeContext(&TeecContext);
1162 
1163 	return TeecResult;
1164 }
1165 
1166 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes)
1167 {
1168 	TEEC_Result TeecResult;
1169 	TEEC_Context TeecContext;
1170 	TEEC_Session TeecSession;
1171 	uint32_t ErrorOrigin;
1172 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
1173 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
1174 	TEEC_UUID *TeecUuid = &tempuuid;
1175 	TEEC_Operation TeecOperation = {0};
1176 	struct blk_desc *dev_desc;
1177 	dev_desc = rockchip_get_bootdev();
1178 	if (!dev_desc) {
1179 		printf("%s: dev_desc is NULL!\n", __func__);
1180 		return -TEEC_ERROR_GENERIC;
1181 	}
1182 
1183 	debug("testmm start\n");
1184 	OpteeClientApiLibInitialize();
1185 
1186 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1187 
1188 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1189 						TEEC_NONE,
1190 						TEEC_NONE,
1191 						TEEC_NONE);
1192 	/*0 nand or emmc "security" partition , 1 rpmb*/
1193 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1194 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1195 	TeecOperation.params[0].value.a = 0;
1196 #endif
1197 
1198 	TeecResult = TEEC_OpenSession(&TeecContext,
1199 				&TeecSession,
1200 				TeecUuid,
1201 				TEEC_LOGIN_PUBLIC,
1202 				NULL,
1203 				&TeecOperation,
1204 				&ErrorOrigin);
1205 
1206 	TEEC_SharedMemory SharedMem0 = {0};
1207 
1208 	SharedMem0.size = sizeof("attributes_flag");
1209 	SharedMem0.flags = 0;
1210 
1211 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1212 
1213 	memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size);
1214 
1215 	TEEC_SharedMemory SharedMem1 = {0};
1216 
1217 	SharedMem1.size = 1;
1218 	SharedMem1.flags = 0;
1219 
1220 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1221 
1222 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1223 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1224 
1225 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1226 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1227 
1228 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1229 						TEEC_MEMREF_TEMP_INOUT,
1230 						TEEC_NONE,
1231 						TEEC_NONE);
1232 
1233 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1234 					0,
1235 					&TeecOperation,
1236 					&ErrorOrigin);
1237 	if (TeecResult == TEEC_SUCCESS)
1238 		memcpy(attributes, SharedMem1.buffer, SharedMem1.size);
1239 	TEEC_ReleaseSharedMemory(&SharedMem0);
1240 	TEEC_ReleaseSharedMemory(&SharedMem1);
1241 	TEEC_CloseSession(&TeecSession);
1242 	TEEC_FinalizeContext(&TeecContext);
1243 	debug("testmm end\n");
1244 
1245 	return TeecResult;
1246 }
1247 
1248 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes)
1249 {
1250 	TEEC_Result TeecResult;
1251 	TEEC_Context TeecContext;
1252 	TEEC_Session TeecSession;
1253 	uint32_t ErrorOrigin;
1254 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
1255 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
1256 	TEEC_UUID *TeecUuid = &tempuuid;
1257 	TEEC_Operation TeecOperation = {0};
1258 	struct blk_desc *dev_desc;
1259 	dev_desc = rockchip_get_bootdev();
1260 	if (!dev_desc) {
1261 		printf("%s: dev_desc is NULL!\n", __func__);
1262 		return -TEEC_ERROR_GENERIC;
1263 	}
1264 
1265 	debug("testmm start\n");
1266 	OpteeClientApiLibInitialize();
1267 
1268 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1269 
1270 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1271 						TEEC_NONE,
1272 						TEEC_NONE,
1273 						TEEC_NONE);
1274 	/*0 nand or emmc "security" partition , 1 rpmb*/
1275 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1276 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1277 	TeecOperation.params[0].value.a = 0;
1278 #endif
1279 
1280 	TeecResult = TEEC_OpenSession(&TeecContext,
1281 				&TeecSession,
1282 				TeecUuid,
1283 				TEEC_LOGIN_PUBLIC,
1284 				NULL,
1285 				&TeecOperation,
1286 				&ErrorOrigin);
1287 
1288 	TEEC_SharedMemory SharedMem0 = {0};
1289 
1290 	SharedMem0.size = sizeof("attributes_flag");
1291 	SharedMem0.flags = 0;
1292 
1293 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1294 
1295 	memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size);
1296 
1297 	TEEC_SharedMemory SharedMem1 = {0};
1298 
1299 	SharedMem1.size = 1;
1300 	SharedMem1.flags = 0;
1301 
1302 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1303 
1304 	memcpy(SharedMem1.buffer, (char *)&attributes, SharedMem1.size);
1305 
1306 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1307 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1308 
1309 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1310 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1311 
1312 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1313 						TEEC_MEMREF_TEMP_INOUT,
1314 						TEEC_NONE,
1315 						TEEC_NONE);
1316 
1317 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1318 					1,
1319 					&TeecOperation,
1320 					&ErrorOrigin);
1321 
1322 	TEEC_ReleaseSharedMemory(&SharedMem0);
1323 	TEEC_ReleaseSharedMemory(&SharedMem1);
1324 	TEEC_CloseSession(&TeecSession);
1325 	TEEC_FinalizeContext(&TeecContext);
1326 	debug("testmm end\n");
1327 
1328 	return TeecResult;
1329 }
1330 
1331 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size)
1332 {
1333 	TEEC_Result TeecResult;
1334 	TEEC_Context TeecContext;
1335 	TEEC_Session TeecSession;
1336 	uint32_t ErrorOrigin;
1337 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1338 				{ 0xa8, 0x69, 0x9c, 0xe6,
1339 				  0x88, 0x6c, 0x5d, 0x5d
1340 				}
1341 			     };
1342 	TEEC_UUID *TeecUuid = &tempuuid;
1343 	TEEC_Operation TeecOperation = {0};
1344 	struct blk_desc *dev_desc;
1345 	dev_desc = rockchip_get_bootdev();
1346 	if (!dev_desc) {
1347 		printf("%s: dev_desc is NULL!\n", __func__);
1348 		return -TEEC_ERROR_GENERIC;
1349 	}
1350 
1351 	OpteeClientApiLibInitialize();
1352 
1353 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1354 
1355 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1356 						TEEC_NONE,
1357 						TEEC_NONE,
1358 						TEEC_NONE);
1359 	/*0 nand or emmc "security" partition , 1 rpmb*/
1360 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1361 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1362 	TeecOperation.params[0].value.a = 0;
1363 #endif
1364 
1365 	TeecResult = TEEC_OpenSession(&TeecContext,
1366 				      &TeecSession,
1367 				      TeecUuid,
1368 				      TEEC_LOGIN_PUBLIC,
1369 				      NULL,
1370 					&TeecOperation,
1371 				      &ErrorOrigin);
1372 
1373 	TEEC_SharedMemory SharedMem0 = {0};
1374 
1375 	SharedMem0.size = *dh_size;
1376 	SharedMem0.flags = 0;
1377 
1378 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1379 
1380 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1381 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1382 
1383 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1384 						    TEEC_NONE,
1385 						    TEEC_NONE,
1386 						    TEEC_NONE);
1387 
1388 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1389 					143,
1390 					&TeecOperation,
1391 					&ErrorOrigin);
1392 
1393 	*dh_size = TeecOperation.params[0].tmpref.size;
1394 	memcpy(dh, SharedMem0.buffer, SharedMem0.size);
1395 
1396 	TEEC_ReleaseSharedMemory(&SharedMem0);
1397 
1398 	TEEC_CloseSession(&TeecSession);
1399 	TeecResult = TEEC_FinalizeContext(&TeecContext);
1400 
1401 	return TeecResult;
1402 }
1403 
1404 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size)
1405 {
1406 	TEEC_Result TeecResult;
1407 	TEEC_Context TeecContext;
1408 	TEEC_Session TeecSession;
1409 	uint32_t ErrorOrigin;
1410 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1411 				{ 0xa8, 0x69, 0x9c, 0xe6,
1412 				  0x88, 0x6c, 0x5d, 0x5d
1413 				}
1414 			     };
1415 	TEEC_UUID *TeecUuid = &tempuuid;
1416 	TEEC_Operation TeecOperation = {0};
1417 	struct blk_desc *dev_desc;
1418 	dev_desc = rockchip_get_bootdev();
1419 	if (!dev_desc) {
1420 		printf("%s: dev_desc is NULL!\n", __func__);
1421 		return -TEEC_ERROR_GENERIC;
1422 	}
1423 
1424 	OpteeClientApiLibInitialize();
1425 
1426 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1427 
1428 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1429 						TEEC_NONE,
1430 						TEEC_NONE,
1431 						TEEC_NONE);
1432 	/*0 nand or emmc "security" partition , 1 rpmb*/
1433 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1434 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1435 	TeecOperation.params[0].value.a = 0;
1436 #endif
1437 
1438 	TeecResult = TEEC_OpenSession(&TeecContext,
1439 				      &TeecSession,
1440 				      TeecUuid,
1441 				      TEEC_LOGIN_PUBLIC,
1442 				      NULL,
1443 					&TeecOperation,
1444 				      &ErrorOrigin);
1445 
1446 	TEEC_SharedMemory SharedMem0 = {0};
1447 
1448 	SharedMem0.size = *uuid_size;
1449 	SharedMem0.flags = 0;
1450 
1451 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1452 
1453 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1454 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1455 
1456 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1457 						    TEEC_NONE,
1458 						    TEEC_NONE,
1459 						    TEEC_NONE);
1460 
1461 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1462 					144,
1463 					&TeecOperation,
1464 					&ErrorOrigin);
1465 
1466 	*uuid_size = TeecOperation.params[0].tmpref.size;
1467 	memcpy(uuid, SharedMem0.buffer, SharedMem0.size);
1468 
1469 	TEEC_ReleaseSharedMemory(&SharedMem0);
1470 
1471 	TEEC_CloseSession(&TeecSession);
1472 	TeecResult = TEEC_FinalizeContext(&TeecContext);
1473 
1474 	return TeecResult;
1475 }
1476 
1477 uint32_t trusty_attest_get_ca(uint8_t *operation_start,
1478 			      uint32_t *operation_size,
1479 			      uint8_t *out,
1480 			      uint32_t *out_len)
1481 {
1482 	TEEC_Result TeecResult;
1483 	TEEC_Context TeecContext;
1484 	TEEC_Session TeecSession;
1485 	uint32_t ErrorOrigin;
1486 
1487 	OpteeClientApiLibInitialize();
1488 
1489 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1490 				{ 0xa8, 0x69, 0x9c, 0xe6,
1491 				  0x88, 0x6c, 0x5d, 0x5d
1492 				}
1493 			     };
1494 
1495 	TEEC_UUID *TeecUuid = &tempuuid;
1496 	TEEC_Operation TeecOperation = {0};
1497 	struct blk_desc *dev_desc;
1498 	dev_desc = rockchip_get_bootdev();
1499 	if (!dev_desc) {
1500 		printf("%s: dev_desc is NULL!\n", __func__);
1501 		return -TEEC_ERROR_GENERIC;
1502 	}
1503 
1504 	OpteeClientApiLibInitialize();
1505 
1506 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1507 
1508 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1509 						TEEC_NONE,
1510 						TEEC_NONE,
1511 						TEEC_NONE);
1512 	/*0 nand or emmc "security" partition , 1 rpmb*/
1513 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1514 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1515 	TeecOperation.params[0].value.a = 0;
1516 #endif
1517 
1518 	TeecResult = TEEC_OpenSession(&TeecContext,
1519 				      &TeecSession,
1520 				      TeecUuid,
1521 				      TEEC_LOGIN_PUBLIC,
1522 				      NULL,
1523 					&TeecOperation,
1524 				      &ErrorOrigin);
1525 
1526 	TEEC_SharedMemory SharedMem0 = {0};
1527 
1528 	SharedMem0.size = *operation_size;
1529 	SharedMem0.flags = 0;
1530 
1531 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1532 
1533 	memcpy(SharedMem0.buffer, operation_start, SharedMem0.size);
1534 
1535 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1536 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1537 
1538 	TEEC_SharedMemory SharedMem1 = {0};
1539 
1540 	SharedMem1.size = *out_len;
1541 	SharedMem1.flags = 0;
1542 
1543 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1544 
1545 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1546 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1547 
1548 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1549 						    TEEC_MEMREF_TEMP_INOUT,
1550 						    TEEC_NONE,
1551 						    TEEC_NONE);
1552 
1553 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1554 					145,
1555 					&TeecOperation,
1556 					&ErrorOrigin);
1557 
1558 	*out_len = TeecOperation.params[1].tmpref.size;
1559 	memcpy(out, SharedMem1.buffer, SharedMem1.size);
1560 	TEEC_ReleaseSharedMemory(&SharedMem0);
1561 	TEEC_ReleaseSharedMemory(&SharedMem1);
1562 
1563 	return TeecResult;
1564 }
1565 
1566 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size)
1567 {
1568 	TEEC_Result TeecResult;
1569 	TEEC_Context TeecContext;
1570 	TEEC_Session TeecSession;
1571 	uint32_t ErrorOrigin;
1572 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1573 				{ 0xa8, 0x69, 0x9c, 0xe6,
1574 				  0x88, 0x6c, 0x5d, 0x5d
1575 				}
1576 			     };
1577 	TEEC_UUID *TeecUuid = &tempuuid;
1578 	TEEC_Operation TeecOperation = {0};
1579 	struct blk_desc *dev_desc;
1580 	dev_desc = rockchip_get_bootdev();
1581 	if (!dev_desc) {
1582 		printf("%s: dev_desc is NULL!\n", __func__);
1583 		return -TEEC_ERROR_GENERIC;
1584 	}
1585 
1586 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1587 
1588 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1589 						TEEC_NONE,
1590 						TEEC_NONE,
1591 						TEEC_NONE);
1592 	/*0 nand or emmc "security" partition , 1 rpmb*/
1593 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1594 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1595 	TeecOperation.params[0].value.a = 0;
1596 #endif
1597 
1598 	TeecResult = TEEC_OpenSession(&TeecContext,
1599 					&TeecSession,
1600 					TeecUuid,
1601 					TEEC_LOGIN_PUBLIC,
1602 					NULL,
1603 					&TeecOperation,
1604 					&ErrorOrigin);
1605 
1606 	TEEC_SharedMemory SharedMem0 = {0};
1607 
1608 	SharedMem0.size = *ca_response_size;
1609 	SharedMem0.flags = 0;
1610 
1611 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1612 
1613 	memcpy(SharedMem0.buffer, ca_response, SharedMem0.size);
1614 
1615 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1616 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1617 
1618 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1619 						    TEEC_NONE,
1620 						    TEEC_NONE,
1621 						    TEEC_NONE);
1622 
1623 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1624 					146,
1625 					&TeecOperation,
1626 					&ErrorOrigin);
1627 
1628 	TEEC_ReleaseSharedMemory(&SharedMem0);
1629 
1630 	TEEC_CloseSession(&TeecSession);
1631 	TeecResult = TEEC_FinalizeContext(&TeecContext);
1632 
1633 	return TeecResult;
1634 }
1635