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