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