xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientInterface.c (revision c7de5349c9abcd4e28cc34f9eb02efdc19b877b3)
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 #define STORAGE_CMD_READ_ATTRIBUTE_HASH		0
18 #define STORAGE_CMD_WRITE_ATTRIBUTE_HASH	1
19 #define STORAGE_CMD_UBOOT_END_OTP		2
20 #define STORAGE_CMD_READ_VBOOTKEY_HASH		3
21 #define STORAGE_CMD_WRITE_VBOOTKEY_HASH		4
22 #define STORAGE_CMD_READ_ENABLE_FLAG		5
23 
24 static uint8_t b2hs_add_base(uint8_t in)
25 {
26 	if (in > 9)
27 		return in + 55;
28 	else
29 		return in + 48;
30 }
31 
32 static uint32_t b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen)
33 {
34 	uint32_t i = 0;
35 
36 	if (blen * 2 + 1 > hslen)
37 		return 0;
38 
39 	for (; i < blen; i++) {
40 		hs[i * 2 + 1] = b2hs_add_base(b[i] & 0xf);
41 		hs[i * 2] = b2hs_add_base(b[i] >> 4);
42 	}
43 	hs[blen * 2] = 0;
44 
45 	return blen * 2;
46 }
47 
48 static uint32_t trusty_base_write_security_data(char *filename,
49 						uint32_t filename_size,
50 						uint8_t *data,
51 						uint32_t data_size)
52 {
53 	TEEC_Result TeecResult;
54 	TEEC_Context TeecContext;
55 	TEEC_Session TeecSession;
56 	uint32_t ErrorOrigin;
57 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
58 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
59 	TEEC_UUID *TeecUuid = &tempuuid;
60 	TEEC_Operation TeecOperation = {0};
61 	struct blk_desc *dev_desc;
62 	dev_desc = rockchip_get_bootdev();
63 	if (!dev_desc) {
64 		printf("%s: dev_desc is NULL!\n", __func__);
65 		return -TEEC_ERROR_GENERIC;
66 	}
67 
68 	TeecResult = OpteeClientApiLibInitialize();
69 	if (TeecResult != TEEC_SUCCESS)
70 		return TeecResult;
71 
72 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
73 	if (TeecResult != TEEC_SUCCESS)
74 		return TeecResult;
75 
76 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
77 						    TEEC_NONE,
78 						    TEEC_NONE,
79 						    TEEC_NONE);
80 	/*0 nand or emmc "security" partition , 1 rpmb*/
81 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
82 		TeecOperation.params[0].value.a = 1;
83 	else
84 		TeecOperation.params[0].value.a = 0;
85 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
86 	TeecOperation.params[0].value.a = 0;
87 #endif
88 
89 	TeecResult = TEEC_OpenSession(&TeecContext,
90 				&TeecSession,
91 				TeecUuid,
92 				TEEC_LOGIN_PUBLIC,
93 				NULL,
94 				&TeecOperation,
95 				&ErrorOrigin);
96 	if (TeecResult != TEEC_SUCCESS)
97 		return TeecResult;
98 
99 	TEEC_SharedMemory SharedMem0 = {0};
100 
101 	SharedMem0.size = filename_size;
102 	SharedMem0.flags = 0;
103 
104 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
105 	if (TeecResult != TEEC_SUCCESS)
106 		goto exit;
107 
108 	memcpy(SharedMem0.buffer, filename, SharedMem0.size);
109 
110 	TEEC_SharedMemory SharedMem1 = {0};
111 
112 	SharedMem1.size = data_size;
113 	SharedMem1.flags = 0;
114 
115 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
116 	if (TeecResult != TEEC_SUCCESS)
117 		goto exit;
118 
119 	memcpy(SharedMem1.buffer, data, SharedMem1.size);
120 
121 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
122 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
123 
124 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
125 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
126 
127 
128 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
129 						TEEC_MEMREF_TEMP_INOUT,
130 						TEEC_NONE,
131 						TEEC_NONE);
132 
133 	TeecResult = TEEC_InvokeCommand(&TeecSession,
134 					1,
135 					&TeecOperation,
136 					&ErrorOrigin);
137 	if (TeecResult != TEEC_SUCCESS)
138 		goto exit;
139 exit:
140 	TEEC_ReleaseSharedMemory(&SharedMem0);
141 	TEEC_ReleaseSharedMemory(&SharedMem1);
142 	TEEC_CloseSession(&TeecSession);
143 	TEEC_FinalizeContext(&TeecContext);
144 
145 	return TeecResult;
146 }
147 
148 static uint32_t trusty_base_read_security_data(char *filename,
149 					       uint32_t filename_size,
150 					       uint8_t *data,
151 					       uint32_t data_size)
152 {
153 	TEEC_Result TeecResult;
154 	TEEC_Context TeecContext;
155 	TEEC_Session TeecSession;
156 	uint32_t ErrorOrigin;
157 	TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
158 			{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
159 	TEEC_UUID *TeecUuid = &tempuuid;
160 	TEEC_Operation TeecOperation = {0};
161 
162 	struct blk_desc *dev_desc;
163 	dev_desc = rockchip_get_bootdev();
164 	if (!dev_desc) {
165 		printf("%s: dev_desc is NULL!\n", __func__);
166 		return -TEEC_ERROR_GENERIC;
167 	}
168 
169 	TeecResult = OpteeClientApiLibInitialize();
170 	if (TeecResult != TEEC_SUCCESS)
171 		return TeecResult;
172 
173 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
174 	if (TeecResult != TEEC_SUCCESS)
175 		return TeecResult;
176 
177 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
178 						TEEC_NONE,
179 						TEEC_NONE,
180 						TEEC_NONE);
181 	/*0 nand or emmc "security" partition , 1 rpmb*/
182 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
183 		TeecOperation.params[0].value.a = 1;
184 	else
185 		TeecOperation.params[0].value.a = 0;
186 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
187 	TeecOperation.params[0].value.a = 0;
188 #endif
189 
190 	TeecResult = TEEC_OpenSession(&TeecContext,
191 				&TeecSession,
192 				TeecUuid,
193 				TEEC_LOGIN_PUBLIC,
194 				NULL,
195 				&TeecOperation,
196 				&ErrorOrigin);
197 	if (TeecResult != TEEC_SUCCESS)
198 		return TeecResult;
199 
200 	TEEC_SharedMemory SharedMem0 = {0};
201 
202 	SharedMem0.size = filename_size;
203 	SharedMem0.flags = 0;
204 
205 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
206 	if (TeecResult != TEEC_SUCCESS)
207 		goto exit;
208 
209 	memcpy(SharedMem0.buffer, filename, SharedMem0.size);
210 
211 	TEEC_SharedMemory SharedMem1 = {0};
212 
213 	SharedMem1.size = data_size;
214 	SharedMem1.flags = 0;
215 
216 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
217 	if (TeecResult != TEEC_SUCCESS)
218 		goto exit;
219 
220 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
221 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
222 
223 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
224 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
225 
226 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
227 						TEEC_MEMREF_TEMP_INOUT,
228 						TEEC_NONE,
229 						TEEC_NONE);
230 
231 	TeecResult = TEEC_InvokeCommand(&TeecSession,
232 					0,
233 					&TeecOperation,
234 					&ErrorOrigin);
235 	if (TeecResult == TEEC_SUCCESS)
236 		memcpy(data, SharedMem1.buffer, SharedMem1.size);
237 exit:
238 	TEEC_ReleaseSharedMemory(&SharedMem0);
239 	TEEC_ReleaseSharedMemory(&SharedMem1);
240 	TEEC_CloseSession(&TeecSession);
241 	TEEC_FinalizeContext(&TeecContext);
242 
243 	return TeecResult;
244 }
245 
246 static uint32_t trusty_base_end_security_data(void)
247 {
248 	TEEC_Result TeecResult;
249 	TEEC_Context TeecContext;
250 	TEEC_Session TeecSession;
251 	uint32_t ErrorOrigin;
252 	TEEC_UUID  tempuuid = { 0x1b484ea5, 0x698b, 0x4142,
253 		{ 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } };
254 	TEEC_UUID *TeecUuid = &tempuuid;
255 	TEEC_Operation TeecOperation = {0};
256 
257 	TeecResult = OpteeClientApiLibInitialize();
258 	if (TeecResult != TEEC_SUCCESS)
259 		return TeecResult;
260 
261 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
262 	if (TeecResult != TEEC_SUCCESS)
263 		return TeecResult;
264 
265 	TeecResult = TEEC_OpenSession(&TeecContext,
266 				&TeecSession,
267 				TeecUuid,
268 				TEEC_LOGIN_PUBLIC,
269 				NULL,
270 				NULL,
271 				&ErrorOrigin);
272 	if (TeecResult != TEEC_SUCCESS)
273 		return TeecResult;
274 
275 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
276 						    TEEC_NONE,
277 						    TEEC_NONE,
278 						    TEEC_NONE);
279 
280 	TeecResult = TEEC_InvokeCommand(&TeecSession,
281 					2,
282 					&TeecOperation,
283 					&ErrorOrigin);
284 	if (TeecResult != TEEC_SUCCESS)
285 		goto exit;
286 exit:
287 	TEEC_CloseSession(&TeecSession);
288 	TEEC_FinalizeContext(&TeecContext);
289 
290 	return TeecResult;
291 }
292 
293 uint32_t trusty_read_rollback_index(uint32_t slot, uint64_t *value)
294 {
295 	char hs[9];
296 
297 	b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9);
298 
299 	return trusty_base_read_security_data(hs, 8, (uint8_t *)value, 8);
300 }
301 
302 uint32_t trusty_write_rollback_index(uint32_t slot, uint64_t value)
303 {
304 	char hs[9];
305 
306 	b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9);
307 
308 	return trusty_base_write_security_data(hs, 8, (uint8_t *)&value, 8);
309 }
310 
311 uint32_t trusty_read_permanent_attributes(uint8_t *attributes, uint32_t size)
312 {
313 	return trusty_base_read_security_data("attributes",
314 		sizeof("attributes"), attributes, size);
315 }
316 
317 uint32_t trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size)
318 {
319 	return trusty_base_write_security_data("attributes",
320 		sizeof("attributes"), attributes, size);
321 }
322 
323 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes)
324 {
325 	return trusty_base_read_security_data("attributes_flag",
326 		sizeof("attributes_flag"), attributes, 1);
327 }
328 
329 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes)
330 {
331 	return trusty_base_write_security_data("attributes_flag",
332 		sizeof("attributes_flag"), &attributes, 1);
333 }
334 
335 uint32_t trusty_read_permanent_attributes_cer(uint8_t *attributes,
336 					      uint32_t size)
337 {
338 	return trusty_base_read_security_data("rsacer",
339 		sizeof("rsacer"), attributes, size);
340 }
341 
342 uint32_t trusty_write_permanent_attributes_cer(uint8_t *attributes,
343 					       uint32_t size)
344 {
345 	return trusty_base_write_security_data("rsacer",
346 		sizeof("rsacer"), attributes, size);
347 }
348 
349 uint32_t trusty_read_lock_state(uint8_t *lock_state)
350 {
351 	return trusty_base_read_security_data("lock_state",
352 		sizeof("lock_state"), lock_state, 1);
353 }
354 
355 uint32_t trusty_write_lock_state(uint8_t lock_state)
356 {
357 	return trusty_base_write_security_data("lock_state",
358 		sizeof("lock_state"), &lock_state, 1);
359 }
360 
361 uint32_t trusty_read_flash_lock_state(uint8_t *flash_lock_state)
362 {
363 	return trusty_base_read_security_data("flash_lock_state",
364 		sizeof("flash_lock_state"), flash_lock_state, 1);
365 }
366 
367 uint32_t trusty_write_flash_lock_state(uint8_t flash_lock_state)
368 {
369 	return trusty_base_write_security_data("flash_lock_state",
370 		sizeof("flash_lock_state"), &flash_lock_state, 1);
371 }
372 
373 static uint32_t trusty_base_end_efuse_or_otp(void)
374 {
375 	TEEC_Result TeecResult;
376 	TEEC_Context TeecContext;
377 	TEEC_Session TeecSession;
378 	uint32_t ErrorOrigin;
379 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
380 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
381 
382 	TEEC_UUID *TeecUuid = &tempuuid;
383 	TEEC_Operation TeecOperation = {0};
384 
385 	TeecResult = OpteeClientApiLibInitialize();
386 	if (TeecResult != TEEC_SUCCESS)
387 		return TeecResult;
388 
389 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
390 	if (TeecResult != TEEC_SUCCESS)
391 		return TeecResult;
392 
393 	TeecResult = TEEC_OpenSession(&TeecContext,
394 				      &TeecSession,
395 				      TeecUuid,
396 				      TEEC_LOGIN_PUBLIC,
397 				      NULL,
398 				      NULL,
399 				      &ErrorOrigin);
400 	if (TeecResult != TEEC_SUCCESS)
401 		return TeecResult;
402 
403 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE,
404 						    TEEC_NONE,
405 						    TEEC_NONE,
406 						    TEEC_NONE);
407 
408 	TeecResult = TEEC_InvokeCommand(&TeecSession,
409 					STORAGE_CMD_UBOOT_END_OTP,
410 					&TeecOperation,
411 					&ErrorOrigin);
412 	if (TeecResult != TEEC_SUCCESS)
413 		goto exit;
414 exit:
415 	TEEC_CloseSession(&TeecSession);
416 	TEEC_FinalizeContext(&TeecContext);
417 
418 	return TeecResult;
419 }
420 
421 static uint32_t trusty_base_efuse_or_otp_operation(uint32_t cmd,
422 						   uint8_t is_write,
423 						   uint32_t *buf,
424 						   uint32_t length)
425 {
426 	TEEC_Result TeecResult;
427 	TEEC_Context TeecContext;
428 	TEEC_Session TeecSession;
429 	uint32_t ErrorOrigin;
430 
431 	TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8,
432 			{ 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } };
433 	TEEC_UUID *TeecUuid = &tempuuid;
434 	TEEC_Operation TeecOperation = {0};
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 	TeecResult = TEEC_OpenSession(&TeecContext,
445 				&TeecSession,
446 				TeecUuid,
447 				TEEC_LOGIN_PUBLIC,
448 				NULL,
449 				NULL,
450 				&ErrorOrigin);
451 	if (TeecResult != TEEC_SUCCESS)
452 		return TeecResult;
453 
454 	TEEC_SharedMemory SharedMem0 = {0};
455 
456 	SharedMem0.size = length * sizeof(uint32_t);
457 	SharedMem0.flags = 0;
458 
459 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
460 	if (TeecResult != TEEC_SUCCESS)
461 		goto exit;
462 
463 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
464 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
465 
466 	if (is_write) {
467 		memcpy(SharedMem0.buffer, buf, SharedMem0.size);
468 		TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
469 							    TEEC_NONE,
470 							    TEEC_NONE,
471 							    TEEC_NONE);
472 
473 	} else {
474 		TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
475 							    TEEC_NONE,
476 							    TEEC_NONE,
477 							    TEEC_NONE);
478 	}
479 
480 	TeecResult = TEEC_InvokeCommand(&TeecSession,
481 					cmd,
482 					&TeecOperation,
483 					&ErrorOrigin);
484 	if (TeecResult != TEEC_SUCCESS)
485 		goto exit;
486 
487 	if (!is_write)
488 		memcpy(buf, SharedMem0.buffer, SharedMem0.size);
489 
490 exit:
491 	TEEC_ReleaseSharedMemory(&SharedMem0);
492 	TEEC_CloseSession(&TeecSession);
493 	TEEC_FinalizeContext(&TeecContext);
494 
495 	return TeecResult;
496 }
497 
498 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length)
499 {
500 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ATTRIBUTE_HASH,
501 						  false, buf, length);
502 }
503 
504 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length)
505 {
506 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_ATTRIBUTE_HASH,
507 						  true, buf, length);
508 }
509 
510 uint32_t trusty_notify_optee_uboot_end(void)
511 {
512 	TEEC_Result res;
513 
514 	res = trusty_base_end_security_data();
515 	res |= trusty_base_end_efuse_or_otp();
516 	return res;
517 }
518 
519 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length)
520 {
521 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_VBOOTKEY_HASH,
522 						  false, buf, length);
523 }
524 
525 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length)
526 {
527 	return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_VBOOTKEY_HASH,
528 						  true, buf, length);
529 }
530 
531 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag)
532 {
533 	uint32_t bootflag;
534 	TEEC_Result TeecResult;
535 
536 	TeecResult = trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ENABLE_FLAG,
537 							false, &bootflag, 1);
538 
539 	if (TeecResult == TEEC_SUCCESS) {
540 #if defined(CONFIG_ROCKCHIP_RK3288)
541 		if (bootflag == 0x00000001)
542 			*flag = 1;
543 #else
544 		if (bootflag == 0x000000FF)
545 			*flag = 1;
546 #endif
547 	}
548 	return TeecResult;
549 }
550 
551 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size)
552 {
553 	TEEC_Result TeecResult;
554 	TEEC_Context TeecContext;
555 	TEEC_Session TeecSession;
556 	uint32_t ErrorOrigin;
557 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
558 				{ 0xa8, 0x69, 0x9c, 0xe6,
559 				  0x88, 0x6c, 0x5d, 0x5d
560 				}
561 			     };
562 	TEEC_UUID *TeecUuid = &tempuuid;
563 	TEEC_Operation TeecOperation = {0};
564 	struct blk_desc *dev_desc;
565 	dev_desc = rockchip_get_bootdev();
566 	if (!dev_desc) {
567 		printf("%s: dev_desc is NULL!\n", __func__);
568 		return -TEEC_ERROR_GENERIC;
569 	}
570 
571 	TeecResult = OpteeClientApiLibInitialize();
572 	if (TeecResult != TEEC_SUCCESS)
573 		return TeecResult;
574 
575 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
576 	if (TeecResult != TEEC_SUCCESS)
577 		return TeecResult;
578 
579 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
580 						TEEC_NONE,
581 						TEEC_NONE,
582 						TEEC_NONE);
583 	/*0 nand or emmc "security" partition , 1 rpmb*/
584 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
585 		TeecOperation.params[0].value.a = 1;
586 	else
587 		TeecOperation.params[0].value.a = 0;
588 
589 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
590 	TeecOperation.params[0].value.a = 0;
591 #endif
592 
593 	TeecResult = TEEC_OpenSession(&TeecContext,
594 				      &TeecSession,
595 				      TeecUuid,
596 				      TEEC_LOGIN_PUBLIC,
597 				      NULL,
598 					&TeecOperation,
599 				      &ErrorOrigin);
600 	if (TeecResult != TEEC_SUCCESS)
601 		return TeecResult;
602 
603 	TEEC_SharedMemory SharedMem0 = {0};
604 
605 	SharedMem0.size = *dh_size;
606 	SharedMem0.flags = 0;
607 
608 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
609 	if (TeecResult != TEEC_SUCCESS)
610 		goto exit;
611 
612 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
613 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
614 
615 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
616 						    TEEC_NONE,
617 						    TEEC_NONE,
618 						    TEEC_NONE);
619 
620 	TeecResult = TEEC_InvokeCommand(&TeecSession,
621 					143,
622 					&TeecOperation,
623 					&ErrorOrigin);
624 	if (TeecResult != TEEC_SUCCESS)
625 		goto exit;
626 
627 	*dh_size = TeecOperation.params[0].tmpref.size;
628 	memcpy(dh, SharedMem0.buffer, SharedMem0.size);
629 exit:
630 	TEEC_ReleaseSharedMemory(&SharedMem0);
631 	TEEC_CloseSession(&TeecSession);
632 	TEEC_FinalizeContext(&TeecContext);
633 
634 	return TeecResult;
635 }
636 
637 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size)
638 {
639 	TEEC_Result TeecResult;
640 	TEEC_Context TeecContext;
641 	TEEC_Session TeecSession;
642 	uint32_t ErrorOrigin;
643 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
644 				{ 0xa8, 0x69, 0x9c, 0xe6,
645 				  0x88, 0x6c, 0x5d, 0x5d
646 				}
647 			     };
648 	TEEC_UUID *TeecUuid = &tempuuid;
649 	TEEC_Operation TeecOperation = {0};
650 	struct blk_desc *dev_desc;
651 	dev_desc = rockchip_get_bootdev();
652 	if (!dev_desc) {
653 		printf("%s: dev_desc is NULL!\n", __func__);
654 		return -TEEC_ERROR_GENERIC;
655 	}
656 
657 	TeecResult = OpteeClientApiLibInitialize();
658 	if (TeecResult != TEEC_SUCCESS)
659 		return TeecResult;
660 
661 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
662 	if (TeecResult != TEEC_SUCCESS)
663 		return TeecResult;
664 
665 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
666 						TEEC_NONE,
667 						TEEC_NONE,
668 						TEEC_NONE);
669 	/*0 nand or emmc "security" partition , 1 rpmb*/
670 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
671 		TeecOperation.params[0].value.a = 1;
672 	else
673 		TeecOperation.params[0].value.a = 0;
674 
675 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
676 	TeecOperation.params[0].value.a = 0;
677 #endif
678 
679 	TeecResult = TEEC_OpenSession(&TeecContext,
680 				      &TeecSession,
681 				      TeecUuid,
682 				      TEEC_LOGIN_PUBLIC,
683 				      NULL,
684 					&TeecOperation,
685 				      &ErrorOrigin);
686 	if (TeecResult != TEEC_SUCCESS)
687 		return TeecResult;
688 
689 	TEEC_SharedMemory SharedMem0 = {0};
690 
691 	SharedMem0.size = *uuid_size;
692 	SharedMem0.flags = 0;
693 
694 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
695 	if (TeecResult != TEEC_SUCCESS)
696 		goto exit;
697 
698 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
699 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
700 
701 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
702 						    TEEC_NONE,
703 						    TEEC_NONE,
704 						    TEEC_NONE);
705 
706 	TeecResult = TEEC_InvokeCommand(&TeecSession,
707 					144,
708 					&TeecOperation,
709 					&ErrorOrigin);
710 	if (TeecResult != TEEC_SUCCESS)
711 		goto exit;
712 
713 	*uuid_size = TeecOperation.params[0].tmpref.size;
714 	memcpy(uuid, SharedMem0.buffer, SharedMem0.size);
715 exit:
716 	TEEC_ReleaseSharedMemory(&SharedMem0);
717 	TEEC_CloseSession(&TeecSession);
718 	TEEC_FinalizeContext(&TeecContext);
719 
720 	return TeecResult;
721 }
722 
723 uint32_t trusty_attest_get_ca(uint8_t *operation_start,
724 			      uint32_t *operation_size,
725 			      uint8_t *out,
726 			      uint32_t *out_len)
727 {
728 	TEEC_Result TeecResult;
729 	TEEC_Context TeecContext;
730 	TEEC_Session TeecSession;
731 	uint32_t ErrorOrigin;
732 
733 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
734 				{ 0xa8, 0x69, 0x9c, 0xe6,
735 				  0x88, 0x6c, 0x5d, 0x5d
736 				}
737 			     };
738 
739 	TEEC_UUID *TeecUuid = &tempuuid;
740 	TEEC_Operation TeecOperation = {0};
741 	struct blk_desc *dev_desc;
742 	dev_desc = rockchip_get_bootdev();
743 	if (!dev_desc) {
744 		printf("%s: dev_desc is NULL!\n", __func__);
745 		return -TEEC_ERROR_GENERIC;
746 	}
747 
748 	TeecResult = OpteeClientApiLibInitialize();
749 	if (TeecResult != TEEC_SUCCESS)
750 		return TeecResult;
751 
752 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
753 	if (TeecResult != TEEC_SUCCESS)
754 		return TeecResult;
755 
756 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
757 						TEEC_NONE,
758 						TEEC_NONE,
759 						TEEC_NONE);
760 	/*0 nand or emmc "security" partition , 1 rpmb*/
761 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
762 		TeecOperation.params[0].value.a = 1;
763 	else
764 		TeecOperation.params[0].value.a = 0;
765 
766 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
767 	TeecOperation.params[0].value.a = 0;
768 #endif
769 
770 	TeecResult = TEEC_OpenSession(&TeecContext,
771 				      &TeecSession,
772 				      TeecUuid,
773 				      TEEC_LOGIN_PUBLIC,
774 				      NULL,
775 					&TeecOperation,
776 				      &ErrorOrigin);
777 	if (TeecResult != TEEC_SUCCESS)
778 		return TeecResult;
779 
780 	TEEC_SharedMemory SharedMem0 = {0};
781 
782 	SharedMem0.size = *operation_size;
783 	SharedMem0.flags = 0;
784 
785 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
786 	if (TeecResult != TEEC_SUCCESS)
787 		goto exit;
788 
789 	memcpy(SharedMem0.buffer, operation_start, SharedMem0.size);
790 
791 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
792 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
793 
794 	TEEC_SharedMemory SharedMem1 = {0};
795 
796 	SharedMem1.size = *out_len;
797 	SharedMem1.flags = 0;
798 
799 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1);
800 	if (TeecResult != TEEC_SUCCESS)
801 		goto exit;
802 
803 	TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer;
804 	TeecOperation.params[1].tmpref.size = SharedMem1.size;
805 
806 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
807 						    TEEC_MEMREF_TEMP_INOUT,
808 						    TEEC_NONE,
809 						    TEEC_NONE);
810 
811 	TeecResult = TEEC_InvokeCommand(&TeecSession,
812 					145,
813 					&TeecOperation,
814 					&ErrorOrigin);
815 	if (TeecResult != TEEC_SUCCESS)
816 		goto exit;
817 
818 	*out_len = TeecOperation.params[1].tmpref.size;
819 	memcpy(out, SharedMem1.buffer, SharedMem1.size);
820 exit:
821 	TEEC_ReleaseSharedMemory(&SharedMem0);
822 	TEEC_ReleaseSharedMemory(&SharedMem1);
823 	TEEC_CloseSession(&TeecSession);
824 	TEEC_FinalizeContext(&TeecContext);
825 
826 	return TeecResult;
827 }
828 
829 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size)
830 {
831 	TEEC_Result TeecResult;
832 	TEEC_Context TeecContext;
833 	TEEC_Session TeecSession;
834 	uint32_t ErrorOrigin;
835 	TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6,
836 				{ 0xa8, 0x69, 0x9c, 0xe6,
837 				  0x88, 0x6c, 0x5d, 0x5d
838 				}
839 			     };
840 	TEEC_UUID *TeecUuid = &tempuuid;
841 	TEEC_Operation TeecOperation = {0};
842 	struct blk_desc *dev_desc;
843 	dev_desc = rockchip_get_bootdev();
844 	if (!dev_desc) {
845 		printf("%s: dev_desc is NULL!\n", __func__);
846 		return -TEEC_ERROR_GENERIC;
847 	}
848 	TeecResult = OpteeClientApiLibInitialize();
849 	if (TeecResult != TEEC_SUCCESS)
850 		return TeecResult;
851 
852 	TeecResult = TEEC_InitializeContext(NULL, &TeecContext);
853 	if (TeecResult != TEEC_SUCCESS)
854 		return TeecResult;
855 
856 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
857 						TEEC_NONE,
858 						TEEC_NONE,
859 						TEEC_NONE);
860 	/*0 nand or emmc "security" partition , 1 rpmb*/
861 	if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0)
862 		TeecOperation.params[0].value.a = 1;
863 	else
864 		TeecOperation.params[0].value.a = 0;
865 
866 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION
867 	TeecOperation.params[0].value.a = 0;
868 #endif
869 
870 	TeecResult = TEEC_OpenSession(&TeecContext,
871 					&TeecSession,
872 					TeecUuid,
873 					TEEC_LOGIN_PUBLIC,
874 					NULL,
875 					&TeecOperation,
876 					&ErrorOrigin);
877 	if (TeecResult != TEEC_SUCCESS)
878 		return TeecResult;
879 
880 	TEEC_SharedMemory SharedMem0 = {0};
881 
882 	SharedMem0.size = *ca_response_size;
883 	SharedMem0.flags = 0;
884 
885 	TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0);
886 	if (TeecResult != TEEC_SUCCESS)
887 		goto exit;
888 
889 	memcpy(SharedMem0.buffer, ca_response, SharedMem0.size);
890 
891 	TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer;
892 	TeecOperation.params[0].tmpref.size = SharedMem0.size;
893 
894 	TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
895 						    TEEC_NONE,
896 						    TEEC_NONE,
897 						    TEEC_NONE);
898 
899 	TeecResult = TEEC_InvokeCommand(&TeecSession,
900 					146,
901 					&TeecOperation,
902 					&ErrorOrigin);
903 	if (TeecResult != TEEC_SUCCESS)
904 		goto exit;
905 exit:
906 	TEEC_ReleaseSharedMemory(&SharedMem0);
907 	TEEC_CloseSession(&TeecSession);
908 	TEEC_FinalizeContext(&TeecContext);
909 
910 	return TeecResult;
911 }
912