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