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