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