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