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