xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientInterface.c (revision cb0376d1acea586b26080b72f6becda2fd6f6e63)
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 
1058 	return TeecResult;
1059 }
1060 
1061 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length)
1062 {
1063 	TEEC_Result TeecResult;
1064 	TEEC_Context TeecContext;
1065 	TEEC_Session TeecSession;
1066 	uint32_t ErrorOrigin;
1067 
1068 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1069 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1070 	TEEC_UUID *TeecUuid = &tempuuid;
1071 	TEEC_Operation TeecOperation = {0};
1072 
1073 	TeecResult = OpteeClientApiLibInitialize();
1074 	if (TeecResult != TEEC_SUCCESS)
1075 		return TeecResult;
1076 
1077 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1078 	if (TeecResult != TEEC_SUCCESS)
1079 		return TeecResult;
1080 
1081 	TeecResult = TEEC_OpenSession(&TeecContext,
1082 				&TeecSession,
1083 				TeecUuid,
1084 				TEEC_LOGIN_PUBLIC,
1085 				NULL,
1086 				NULL,
1087 				&ErrorOrigin);
1088 	if (TeecResult != TEEC_SUCCESS)
1089 		return TeecResult;
1090 
1091 	TEEC_SharedMemory SharedMem0 = {0};
1092 
1093 	SharedMem0.size = length * sizeof(uint32_t);
1094 	SharedMem0.flags = 0;
1095 
1096 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1097 	if (TeecResult != TEEC_SUCCESS)
1098 		goto exit;
1099 
1100 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1101 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1102 
1103 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
1104 						TEEC_NONE,
1105 						TEEC_NONE,
1106 						TEEC_NONE);
1107 
1108 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1109 					0,
1110 					&TeecOperation,
1111 					&ErrorOrigin);
1112 
1113 	if (TeecResult == TEEC_SUCCESS)
1114 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
1115 exit:
1116 	TEEC_ReleaseSharedMemory(&SharedMem0);
1117 	TEEC_CloseSession(&TeecSession);
1118 	TEEC_FinalizeContext(&TeecContext);
1119 
1120 	return TeecResult;
1121 }
1122 
1123 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length)
1124 {
1125 	TEEC_Result TeecResult;
1126 	TEEC_Context TeecContext;
1127 	TEEC_Session TeecSession;
1128 	uint32_t ErrorOrigin;
1129 
1130 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1131 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1132 	TEEC_UUID *TeecUuid = &tempuuid;
1133 	TEEC_Operation TeecOperation = {0};
1134 
1135 	TeecResult = OpteeClientApiLibInitialize();
1136 	if (TeecResult != TEEC_SUCCESS)
1137 		return TeecResult;
1138 
1139 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1140 	if (TeecResult != TEEC_SUCCESS)
1141 		return TeecResult;
1142 
1143 	TeecResult = TEEC_OpenSession(&TeecContext,
1144 				&TeecSession,
1145 				TeecUuid,
1146 				TEEC_LOGIN_PUBLIC,
1147 				NULL,
1148 				NULL,
1149 				&ErrorOrigin);
1150 	if (TeecResult != TEEC_SUCCESS)
1151 		return TeecResult;
1152 
1153 	TEEC_SharedMemory SharedMem0 = {0};
1154 
1155 	SharedMem0.size = length * sizeof(uint32_t);
1156 	SharedMem0.flags = 0;
1157 
1158 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1159 	if (TeecResult != TEEC_SUCCESS)
1160 		goto exit;
1161 
1162 	memcpy(SharedMem0.buffer, buf, SharedMem0.size);
1163 
1164 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1165 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1166 
1167 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1168 						TEEC_NONE,
1169 						TEEC_NONE,
1170 						TEEC_NONE);
1171 
1172 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1173 					1,
1174 					&TeecOperation,
1175 					&ErrorOrigin);
1176 	if (TeecResult != TEEC_SUCCESS)
1177 		goto exit;
1178 exit:
1179 	TEEC_ReleaseSharedMemory(&SharedMem0);
1180 	TEEC_CloseSession(&TeecSession);
1181 	TEEC_FinalizeContext(&TeecContext);
1182 
1183 	return TeecResult;
1184 }
1185 
1186 uint32_t notify_optee_rpmb_ta(void)
1187 {
1188 	TEEC_Result TeecResult;
1189 	TEEC_Context TeecContext;
1190 	TEEC_Session TeecSession;
1191 	uint32_t ErrorOrigin;
1192 	TEEC_UUID  tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
1193 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
1194 	TEEC_UUID *TeecUuid = &tempuuid;
1195 	TEEC_Operation TeecOperation = {0};
1196 
1197 	TeecResult = OpteeClientApiLibInitialize();
1198 	if (TeecResult != TEEC_SUCCESS)
1199 		return TeecResult;
1200 
1201 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1202 	if (TeecResult != TEEC_SUCCESS)
1203 		return TeecResult;
1204 
1205 	TeecResult = TEEC_OpenSession(&TeecContext,
1206 				&TeecSession,
1207 				TeecUuid,
1208 				TEEC_LOGIN_PUBLIC,
1209 				NULL,
1210 				NULL,
1211 				&ErrorOrigin);
1212 	if (TeecResult != TEEC_SUCCESS)
1213 		return TeecResult;
1214 
1215 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
1216 						TEEC_NONE,
1217 						TEEC_NONE,
1218 						TEEC_NONE);
1219 
1220 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1221 					2,
1222 					&TeecOperation,
1223 					&ErrorOrigin);
1224 	if (TeecResult != TEEC_SUCCESS)
1225 		goto exit;
1226 exit:
1227 	TEEC_CloseSession(&TeecSession);
1228 	TEEC_FinalizeContext(&TeecContext);
1229 
1230 	return TeecResult;
1231 }
1232 
1233 uint32_t notify_optee_efuse_ta(void)
1234 {
1235 	TEEC_Result TeecResult;
1236 	TEEC_Context TeecContext;
1237 	TEEC_Session TeecSession;
1238 	uint32_t ErrorOrigin;
1239 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1240 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1241 
1242 	TEEC_UUID *TeecUuid = &tempuuid;
1243 	TEEC_Operation TeecOperation = {0};
1244 
1245 	TeecResult = OpteeClientApiLibInitialize();
1246 	if (TeecResult != TEEC_SUCCESS)
1247 		return TeecResult;
1248 
1249 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1250 	if (TeecResult != TEEC_SUCCESS)
1251 		return TeecResult;
1252 
1253 	TeecResult = TEEC_OpenSession(&TeecContext,
1254 				&TeecSession,
1255 				TeecUuid,
1256 				TEEC_LOGIN_PUBLIC,
1257 				NULL,
1258 				NULL,
1259 				&ErrorOrigin);
1260 	if (TeecResult != TEEC_SUCCESS)
1261 		return TeecResult;
1262 
1263 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
1264 						TEEC_NONE,
1265 						TEEC_NONE,
1266 						TEEC_NONE);
1267 
1268 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1269 					2,
1270 					&TeecOperation,
1271 					&ErrorOrigin);
1272 	if (TeecResult != TEEC_SUCCESS)
1273 		goto exit;
1274 exit:
1275 	TEEC_CloseSession(&TeecSession);
1276 	TEEC_FinalizeContext(&TeecContext);
1277 
1278 	return TeecResult;
1279 }
1280 
1281 uint32_t trusty_notify_optee_uboot_end(void)
1282 {
1283 	TEEC_Result res;
1284 	res = notify_optee_rpmb_ta();
1285 	res |= notify_optee_efuse_ta();
1286 	return res;
1287 }
1288 
1289 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length)
1290 {
1291 	TEEC_Result TeecResult;
1292 	TEEC_Context TeecContext;
1293 	TEEC_Session TeecSession;
1294 	uint32_t ErrorOrigin;
1295 
1296 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1297 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1298 	TEEC_UUID *TeecUuid = &tempuuid;
1299 	TEEC_Operation TeecOperation = {0};
1300 
1301 	TeecResult = OpteeClientApiLibInitialize();
1302 	if (TeecResult != TEEC_SUCCESS)
1303 		return TeecResult;
1304 
1305 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1306 	if (TeecResult != TEEC_SUCCESS)
1307 		return TeecResult;
1308 
1309 	TeecResult = TEEC_OpenSession(&TeecContext,
1310 				&TeecSession,
1311 				TeecUuid,
1312 				TEEC_LOGIN_PUBLIC,
1313 				NULL,
1314 				NULL,
1315 				&ErrorOrigin);
1316 	if (TeecResult != TEEC_SUCCESS)
1317 		return TeecResult;
1318 
1319 	TEEC_SharedMemory SharedMem0 = {0};
1320 
1321 	SharedMem0.size = length * sizeof(uint32_t);
1322 	SharedMem0.flags = 0;
1323 
1324 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1325 	if (TeecResult != TEEC_SUCCESS)
1326 		goto exit;
1327 
1328 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1329 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1330 
1331 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
1332 						TEEC_NONE,
1333 						TEEC_NONE,
1334 						TEEC_NONE);
1335 
1336 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1337 					3,
1338 					&TeecOperation,
1339 					&ErrorOrigin);
1340 
1341 	if (TeecResult == TEEC_SUCCESS)
1342 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
1343 exit:
1344 	TEEC_ReleaseSharedMemory(&SharedMem0);
1345 	TEEC_CloseSession(&TeecSession);
1346 	TEEC_FinalizeContext(&TeecContext);
1347 
1348 	return TeecResult;
1349 }
1350 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length)
1351 {
1352 	TEEC_Result TeecResult;
1353 	TEEC_Context TeecContext;
1354 	TEEC_Session TeecSession;
1355 	uint32_t ErrorOrigin;
1356 
1357 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1358 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1359 	TEEC_UUID *TeecUuid = &tempuuid;
1360 	TEEC_Operation TeecOperation = {0};
1361 
1362 	TeecResult = OpteeClientApiLibInitialize();
1363 	if (TeecResult != TEEC_SUCCESS)
1364 		return TeecResult;
1365 
1366 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1367 	if (TeecResult != TEEC_SUCCESS)
1368 		return TeecResult;
1369 
1370 	TeecResult = TEEC_OpenSession(&TeecContext,
1371 				&TeecSession,
1372 				TeecUuid,
1373 				TEEC_LOGIN_PUBLIC,
1374 				NULL,
1375 				NULL,
1376 				&ErrorOrigin);
1377 	if (TeecResult != TEEC_SUCCESS)
1378 		return TeecResult;
1379 
1380 	TEEC_SharedMemory SharedMem0 = {0};
1381 
1382 	SharedMem0.size = length * sizeof(uint32_t);
1383 	SharedMem0.flags = 0;
1384 
1385 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1386 	if (TeecResult != TEEC_SUCCESS)
1387 		goto exit;
1388 
1389 	memcpy(SharedMem0.buffer, buf, SharedMem0.size);
1390 
1391 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1392 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1393 
1394 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1395 						TEEC_NONE,
1396 						TEEC_NONE,
1397 						TEEC_NONE);
1398 
1399 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1400 					4,
1401 					&TeecOperation,
1402 					&ErrorOrigin);
1403 	if (TeecResult != TEEC_SUCCESS)
1404 		goto exit;
1405 exit:
1406 	TEEC_ReleaseSharedMemory(&SharedMem0);
1407 	TEEC_CloseSession(&TeecSession);
1408 	TEEC_FinalizeContext(&TeecContext);
1409 
1410 	return TeecResult;
1411 }
1412 
1413 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag)
1414 {
1415 	TEEC_Result TeecResult;
1416 	TEEC_Context TeecContext;
1417 	TEEC_Session TeecSession;
1418 	uint32_t ErrorOrigin;
1419 	uint32_t bootflag;
1420 
1421 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \
1422 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
1423 	TEEC_UUID *TeecUuid = &tempuuid;
1424 	TEEC_Operation TeecOperation = {0};
1425 
1426 	TeecResult = OpteeClientApiLibInitialize();
1427 	if (TeecResult != TEEC_SUCCESS)
1428 		return TeecResult;
1429 
1430 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1431 	if (TeecResult != TEEC_SUCCESS)
1432 		return TeecResult;
1433 
1434 	TeecResult = TEEC_OpenSession(&TeecContext,
1435 				&TeecSession,
1436 				TeecUuid,
1437 				TEEC_LOGIN_PUBLIC,
1438 				NULL,
1439 				NULL,
1440 				&ErrorOrigin);
1441 	if (TeecResult != TEEC_SUCCESS)
1442 		return TeecResult;
1443 
1444 	TEEC_SharedMemory SharedMem0 = {0};
1445 
1446 	SharedMem0.size = 1 * sizeof(uint32_t);
1447 	SharedMem0.flags = 0;
1448 
1449 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1450 	if (TeecResult != TEEC_SUCCESS)
1451 		goto exit;
1452 
1453 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1454 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1455 
1456 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
1457 						TEEC_NONE,
1458 						TEEC_NONE,
1459 						TEEC_NONE);
1460 
1461 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1462 					5,
1463 					&TeecOperation,
1464 					&ErrorOrigin);
1465 
1466 	if (TeecResult == TEEC_SUCCESS) {
1467 		memcpy(&bootflag, SharedMem0.buffer, SharedMem0.size);
1468 		if (bootflag == 0x000000FF)
1469 			*flag = 1;
1470 	}
1471 exit:
1472 	TEEC_ReleaseSharedMemory(&SharedMem0);
1473 	TEEC_CloseSession(&TeecSession);
1474 	TEEC_FinalizeContext(&TeecContext);
1475 
1476 	return TeecResult;
1477 }
1478 
1479 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes)
1480 {
1481 	TEEC_Result TeecResult;
1482 	TEEC_Context TeecContext;
1483 	TEEC_Session TeecSession;
1484 	uint32_t ErrorOrigin;
1485 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
1486 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
1487 	TEEC_UUID *TeecUuid = &tempuuid;
1488 	TEEC_Operation TeecOperation = {0};
1489 	struct blk_desc *dev_desc;
1490 	dev_desc = rockchip_get_bootdev();
1491 	if (!dev_desc) {
1492 		printf("%s: dev_desc is NULL!\n", __func__);
1493 		return -TEEC_ERROR_GENERIC;
1494 	}
1495 
1496 	TeecResult = OpteeClientApiLibInitialize();
1497 	if (TeecResult != TEEC_SUCCESS)
1498 		return TeecResult;
1499 
1500 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1501 	if (TeecResult != TEEC_SUCCESS)
1502 		return TeecResult;
1503 
1504 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1505 						TEEC_NONE,
1506 						TEEC_NONE,
1507 						TEEC_NONE);
1508 	/*0 nand or emmc "security" partition , 1 rpmb*/
1509 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1510 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1511 	TeecOperation.params[0].value.a = 0;
1512 #endif
1513 
1514 	TeecResult = TEEC_OpenSession(&TeecContext,
1515 				&TeecSession,
1516 				TeecUuid,
1517 				TEEC_LOGIN_PUBLIC,
1518 				NULL,
1519 				&TeecOperation,
1520 				&ErrorOrigin);
1521 	if (TeecResult != TEEC_SUCCESS)
1522 		return TeecResult;
1523 
1524 	TEEC_SharedMemory SharedMem0 = {0};
1525 
1526 	SharedMem0.size = sizeof("attributes_flag");
1527 	SharedMem0.flags = 0;
1528 
1529 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1530 	if (TeecResult != TEEC_SUCCESS)
1531 		goto exit;
1532 
1533 	memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size);
1534 
1535 	TEEC_SharedMemory SharedMem1 = {0};
1536 
1537 	SharedMem1.size = 1;
1538 	SharedMem1.flags = 0;
1539 
1540 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1541 	if (TeecResult != TEEC_SUCCESS)
1542 		goto exit;
1543 
1544 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1545 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1546 
1547 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1548 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1549 
1550 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1551 						TEEC_MEMREF_TEMP_INOUT,
1552 						TEEC_NONE,
1553 						TEEC_NONE);
1554 
1555 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1556 					0,
1557 					&TeecOperation,
1558 					&ErrorOrigin);
1559 	if (TeecResult == TEEC_SUCCESS)
1560 		memcpy(attributes, SharedMem1.buffer, SharedMem1.size);
1561 exit:
1562 	TEEC_ReleaseSharedMemory(&SharedMem0);
1563 	TEEC_ReleaseSharedMemory(&SharedMem1);
1564 	TEEC_CloseSession(&TeecSession);
1565 	TEEC_FinalizeContext(&TeecContext);
1566 
1567 	return TeecResult;
1568 }
1569 
1570 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes)
1571 {
1572 	TEEC_Result TeecResult;
1573 	TEEC_Context TeecContext;
1574 	TEEC_Session TeecSession;
1575 	uint32_t ErrorOrigin;
1576 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
1577 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
1578 	TEEC_UUID *TeecUuid = &tempuuid;
1579 	TEEC_Operation TeecOperation = {0};
1580 	struct blk_desc *dev_desc;
1581 	dev_desc = rockchip_get_bootdev();
1582 	if (!dev_desc) {
1583 		printf("%s: dev_desc is NULL!\n", __func__);
1584 		return -TEEC_ERROR_GENERIC;
1585 	}
1586 
1587 	TeecResult = OpteeClientApiLibInitialize();
1588 	if (TeecResult != TEEC_SUCCESS)
1589 		return TeecResult;
1590 
1591 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1592 	if (TeecResult != TEEC_SUCCESS)
1593 		return TeecResult;
1594 
1595 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1596 						TEEC_NONE,
1597 						TEEC_NONE,
1598 						TEEC_NONE);
1599 	/*0 nand or emmc "security" partition , 1 rpmb*/
1600 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1601 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1602 	TeecOperation.params[0].value.a = 0;
1603 #endif
1604 
1605 	TeecResult = TEEC_OpenSession(&TeecContext,
1606 				&TeecSession,
1607 				TeecUuid,
1608 				TEEC_LOGIN_PUBLIC,
1609 				NULL,
1610 				&TeecOperation,
1611 				&ErrorOrigin);
1612 	if (TeecResult != TEEC_SUCCESS)
1613 		return TeecResult;
1614 
1615 	TEEC_SharedMemory SharedMem0 = {0};
1616 
1617 	SharedMem0.size = sizeof("attributes_flag");
1618 	SharedMem0.flags = 0;
1619 
1620 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1621 	if (TeecResult != TEEC_SUCCESS)
1622 		goto exit;
1623 
1624 	memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size);
1625 
1626 	TEEC_SharedMemory SharedMem1 = {0};
1627 
1628 	SharedMem1.size = 1;
1629 	SharedMem1.flags = 0;
1630 
1631 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1632 	if (TeecResult != TEEC_SUCCESS)
1633 		goto exit;
1634 
1635 	memcpy(SharedMem1.buffer, (char *)&attributes, SharedMem1.size);
1636 
1637 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1638 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1639 
1640 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1641 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1642 
1643 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1644 						TEEC_MEMREF_TEMP_INOUT,
1645 						TEEC_NONE,
1646 						TEEC_NONE);
1647 
1648 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1649 					1,
1650 					&TeecOperation,
1651 					&ErrorOrigin);
1652 	if (TeecResult != TEEC_SUCCESS)
1653 		goto exit;
1654 exit:
1655 	TEEC_ReleaseSharedMemory(&SharedMem0);
1656 	TEEC_ReleaseSharedMemory(&SharedMem1);
1657 	TEEC_CloseSession(&TeecSession);
1658 	TEEC_FinalizeContext(&TeecContext);
1659 
1660 	return TeecResult;
1661 }
1662 
1663 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size)
1664 {
1665 	TEEC_Result TeecResult;
1666 	TEEC_Context TeecContext;
1667 	TEEC_Session TeecSession;
1668 	uint32_t ErrorOrigin;
1669 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1670 				{ 0xa8, 0x69, 0x9c, 0xe6,
1671 				  0x88, 0x6c, 0x5d, 0x5d
1672 				}
1673 			     };
1674 	TEEC_UUID *TeecUuid = &tempuuid;
1675 	TEEC_Operation TeecOperation = {0};
1676 	struct blk_desc *dev_desc;
1677 	dev_desc = rockchip_get_bootdev();
1678 	if (!dev_desc) {
1679 		printf("%s: dev_desc is NULL!\n", __func__);
1680 		return -TEEC_ERROR_GENERIC;
1681 	}
1682 
1683 	TeecResult = OpteeClientApiLibInitialize();
1684 	if (TeecResult != TEEC_SUCCESS)
1685 		return TeecResult;
1686 
1687 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1688 	if (TeecResult != TEEC_SUCCESS)
1689 		return TeecResult;
1690 
1691 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1692 						TEEC_NONE,
1693 						TEEC_NONE,
1694 						TEEC_NONE);
1695 	/*0 nand or emmc "security" partition , 1 rpmb*/
1696 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1697 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1698 	TeecOperation.params[0].value.a = 0;
1699 #endif
1700 
1701 	TeecResult = TEEC_OpenSession(&TeecContext,
1702 				      &TeecSession,
1703 				      TeecUuid,
1704 				      TEEC_LOGIN_PUBLIC,
1705 				      NULL,
1706 					&TeecOperation,
1707 				      &ErrorOrigin);
1708 	if (TeecResult != TEEC_SUCCESS)
1709 		return TeecResult;
1710 
1711 	TEEC_SharedMemory SharedMem0 = {0};
1712 
1713 	SharedMem0.size = *dh_size;
1714 	SharedMem0.flags = 0;
1715 
1716 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1717 	if (TeecResult != TEEC_SUCCESS)
1718 		goto exit;
1719 
1720 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1721 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1722 
1723 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1724 						    TEEC_NONE,
1725 						    TEEC_NONE,
1726 						    TEEC_NONE);
1727 
1728 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1729 					143,
1730 					&TeecOperation,
1731 					&ErrorOrigin);
1732 	if (TeecResult != TEEC_SUCCESS)
1733 		goto exit;
1734 
1735 	*dh_size = TeecOperation.params[0].tmpref.size;
1736 	memcpy(dh, SharedMem0.buffer, SharedMem0.size);
1737 exit:
1738 	TEEC_ReleaseSharedMemory(&SharedMem0);
1739 	TEEC_CloseSession(&TeecSession);
1740 	TEEC_FinalizeContext(&TeecContext);
1741 
1742 	return TeecResult;
1743 }
1744 
1745 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size)
1746 {
1747 	TEEC_Result TeecResult;
1748 	TEEC_Context TeecContext;
1749 	TEEC_Session TeecSession;
1750 	uint32_t ErrorOrigin;
1751 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1752 				{ 0xa8, 0x69, 0x9c, 0xe6,
1753 				  0x88, 0x6c, 0x5d, 0x5d
1754 				}
1755 			     };
1756 	TEEC_UUID *TeecUuid = &tempuuid;
1757 	TEEC_Operation TeecOperation = {0};
1758 	struct blk_desc *dev_desc;
1759 	dev_desc = rockchip_get_bootdev();
1760 	if (!dev_desc) {
1761 		printf("%s: dev_desc is NULL!\n", __func__);
1762 		return -TEEC_ERROR_GENERIC;
1763 	}
1764 
1765 	TeecResult = OpteeClientApiLibInitialize();
1766 	if (TeecResult != TEEC_SUCCESS)
1767 		return TeecResult;
1768 
1769 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1770 	if (TeecResult != TEEC_SUCCESS)
1771 		return TeecResult;
1772 
1773 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1774 						TEEC_NONE,
1775 						TEEC_NONE,
1776 						TEEC_NONE);
1777 	/*0 nand or emmc "security" partition , 1 rpmb*/
1778 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1779 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1780 	TeecOperation.params[0].value.a = 0;
1781 #endif
1782 
1783 	TeecResult = TEEC_OpenSession(&TeecContext,
1784 				      &TeecSession,
1785 				      TeecUuid,
1786 				      TEEC_LOGIN_PUBLIC,
1787 				      NULL,
1788 					&TeecOperation,
1789 				      &ErrorOrigin);
1790 	if (TeecResult != TEEC_SUCCESS)
1791 		return TeecResult;
1792 
1793 	TEEC_SharedMemory SharedMem0 = {0};
1794 
1795 	SharedMem0.size = *uuid_size;
1796 	SharedMem0.flags = 0;
1797 
1798 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1799 	if (TeecResult != TEEC_SUCCESS)
1800 		goto exit;
1801 
1802 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1803 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1804 
1805 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1806 						    TEEC_NONE,
1807 						    TEEC_NONE,
1808 						    TEEC_NONE);
1809 
1810 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1811 					144,
1812 					&TeecOperation,
1813 					&ErrorOrigin);
1814 	if (TeecResult != TEEC_SUCCESS)
1815 		goto exit;
1816 
1817 	*uuid_size = TeecOperation.params[0].tmpref.size;
1818 	memcpy(uuid, SharedMem0.buffer, SharedMem0.size);
1819 exit:
1820 	TEEC_ReleaseSharedMemory(&SharedMem0);
1821 	TEEC_CloseSession(&TeecSession);
1822 	TEEC_FinalizeContext(&TeecContext);
1823 
1824 	return TeecResult;
1825 }
1826 
1827 uint32_t trusty_attest_get_ca(uint8_t *operation_start,
1828 			      uint32_t *operation_size,
1829 			      uint8_t *out,
1830 			      uint32_t *out_len)
1831 {
1832 	TEEC_Result TeecResult;
1833 	TEEC_Context TeecContext;
1834 	TEEC_Session TeecSession;
1835 	uint32_t ErrorOrigin;
1836 
1837 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1838 				{ 0xa8, 0x69, 0x9c, 0xe6,
1839 				  0x88, 0x6c, 0x5d, 0x5d
1840 				}
1841 			     };
1842 
1843 	TEEC_UUID *TeecUuid = &tempuuid;
1844 	TEEC_Operation TeecOperation = {0};
1845 	struct blk_desc *dev_desc;
1846 	dev_desc = rockchip_get_bootdev();
1847 	if (!dev_desc) {
1848 		printf("%s: dev_desc is NULL!\n", __func__);
1849 		return -TEEC_ERROR_GENERIC;
1850 	}
1851 
1852 	TeecResult = OpteeClientApiLibInitialize();
1853 	if (TeecResult != TEEC_SUCCESS)
1854 		return TeecResult;
1855 
1856 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1857 	if (TeecResult != TEEC_SUCCESS)
1858 		return TeecResult;
1859 
1860 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1861 						TEEC_NONE,
1862 						TEEC_NONE,
1863 						TEEC_NONE);
1864 	/*0 nand or emmc "security" partition , 1 rpmb*/
1865 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1866 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1867 	TeecOperation.params[0].value.a = 0;
1868 #endif
1869 
1870 	TeecResult = TEEC_OpenSession(&TeecContext,
1871 				      &TeecSession,
1872 				      TeecUuid,
1873 				      TEEC_LOGIN_PUBLIC,
1874 				      NULL,
1875 					&TeecOperation,
1876 				      &ErrorOrigin);
1877 	if (TeecResult != TEEC_SUCCESS)
1878 		return TeecResult;
1879 
1880 	TEEC_SharedMemory SharedMem0 = {0};
1881 
1882 	SharedMem0.size = *operation_size;
1883 	SharedMem0.flags = 0;
1884 
1885 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1886 	if (TeecResult != TEEC_SUCCESS)
1887 		goto exit;
1888 
1889 	memcpy(SharedMem0.buffer, operation_start, SharedMem0.size);
1890 
1891 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1892 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1893 
1894 	TEEC_SharedMemory SharedMem1 = {0};
1895 
1896 	SharedMem1.size = *out_len;
1897 	SharedMem1.flags = 0;
1898 
1899 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
1900 	if (TeecResult != TEEC_SUCCESS)
1901 		goto exit;
1902 
1903 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
1904 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
1905 
1906 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1907 						    TEEC_MEMREF_TEMP_INOUT,
1908 						    TEEC_NONE,
1909 						    TEEC_NONE);
1910 
1911 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1912 					145,
1913 					&TeecOperation,
1914 					&ErrorOrigin);
1915 	if (TeecResult != TEEC_SUCCESS)
1916 		goto exit;
1917 
1918 	*out_len = TeecOperation.params[1].tmpref.size;
1919 	memcpy(out, SharedMem1.buffer, SharedMem1.size);
1920 exit:
1921 	TEEC_ReleaseSharedMemory(&SharedMem0);
1922 	TEEC_ReleaseSharedMemory(&SharedMem1);
1923 	TEEC_CloseSession(&TeecSession);
1924 	TEEC_FinalizeContext(&TeecContext);
1925 
1926 	return TeecResult;
1927 }
1928 
1929 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size)
1930 {
1931 	TEEC_Result TeecResult;
1932 	TEEC_Context TeecContext;
1933 	TEEC_Session TeecSession;
1934 	uint32_t ErrorOrigin;
1935 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
1936 				{ 0xa8, 0x69, 0x9c, 0xe6,
1937 				  0x88, 0x6c, 0x5d, 0x5d
1938 				}
1939 			     };
1940 	TEEC_UUID *TeecUuid = &tempuuid;
1941 	TEEC_Operation TeecOperation = {0};
1942 	struct blk_desc *dev_desc;
1943 	dev_desc = rockchip_get_bootdev();
1944 	if (!dev_desc) {
1945 		printf("%s: dev_desc is NULL!\n", __func__);
1946 		return -TEEC_ERROR_GENERIC;
1947 	}
1948 	TeecResult = OpteeClientApiLibInitialize();
1949 	if (TeecResult != TEEC_SUCCESS)
1950 		return TeecResult;
1951 
1952 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
1953 	if (TeecResult != TEEC_SUCCESS)
1954 		return TeecResult;
1955 
1956 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1957 						TEEC_NONE,
1958 						TEEC_NONE,
1959 						TEEC_NONE);
1960 	/*0 nand or emmc "security" partition , 1 rpmb*/
1961 	TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0;
1962 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
1963 	TeecOperation.params[0].value.a = 0;
1964 #endif
1965 
1966 	TeecResult = TEEC_OpenSession(&TeecContext,
1967 					&TeecSession,
1968 					TeecUuid,
1969 					TEEC_LOGIN_PUBLIC,
1970 					NULL,
1971 					&TeecOperation,
1972 					&ErrorOrigin);
1973 	if (TeecResult != TEEC_SUCCESS)
1974 		return TeecResult;
1975 
1976 	TEEC_SharedMemory SharedMem0 = {0};
1977 
1978 	SharedMem0.size = *ca_response_size;
1979 	SharedMem0.flags = 0;
1980 
1981 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
1982 	if (TeecResult != TEEC_SUCCESS)
1983 		goto exit;
1984 
1985 	memcpy(SharedMem0.buffer, ca_response, SharedMem0.size);
1986 
1987 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
1988 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
1989 
1990 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
1991 						    TEEC_NONE,
1992 						    TEEC_NONE,
1993 						    TEEC_NONE);
1994 
1995 	TeecResult = TEEC_InvokeCommand(&TeecSession,
1996 					146,
1997 					&TeecOperation,
1998 					&ErrorOrigin);
1999 	if (TeecResult != TEEC_SUCCESS)
2000 		goto exit;
2001 exit:
2002 	TEEC_ReleaseSharedMemory(&SharedMem0);
2003 	TEEC_CloseSession(&TeecSession);
2004 	TEEC_FinalizeContext(&TeecContext);
2005 
2006 	return TeecResult;
2007 }
2008