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