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 33 #define CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER 0x00000002 34 35 #define RK_CRYPTO_SERVICE_UUID { 0x0cacdb5d, 0x4fea, 0x466c, \ 36 { 0x97, 0x16, 0x3d, 0x54, 0x16, 0x52, 0x83, 0x0f } } 37 38 static uint8_t b2hs_add_base(uint8_t in) 39 { 40 if (in > 9) 41 return in + 55; 42 else 43 return in + 48; 44 } 45 46 static uint32_t b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen) 47 { 48 uint32_t i = 0; 49 50 if (blen * 2 + 1 > hslen) 51 return 0; 52 53 for (; i < blen; i++) { 54 hs[i * 2 + 1] = b2hs_add_base(b[i] & 0xf); 55 hs[i * 2] = b2hs_add_base(b[i] >> 4); 56 } 57 hs[blen * 2] = 0; 58 59 return blen * 2; 60 } 61 62 static void crypto_flush_cacheline(uint32_t addr, uint32_t size) 63 { 64 ulong alignment = CONFIG_SYS_CACHELINE_SIZE; 65 ulong aligned_input, aligned_len; 66 67 if (!addr || !size) 68 return; 69 70 /* Must flush dcache before crypto DMA fetch data region */ 71 aligned_input = round_down(addr, alignment); 72 aligned_len = round_up(size + (addr - aligned_input), alignment); 73 flush_cache(aligned_input, aligned_len); 74 } 75 76 static void crypto_invalidate_cacheline(uint32_t addr, uint32_t size) 77 { 78 ulong alignment = CONFIG_SYS_CACHELINE_SIZE; 79 ulong aligned_input, aligned_len; 80 81 if (!addr || !size) 82 return; 83 84 /* Must invalidate dcache after crypto DMA write data region */ 85 aligned_input = round_down(addr, alignment); 86 aligned_len = round_up(size + (addr - aligned_input), alignment); 87 invalidate_dcache_range(aligned_input, aligned_input + aligned_len); 88 } 89 90 static uint32_t trusty_base_write_security_data(char *filename, 91 uint32_t filename_size, 92 uint8_t *data, 93 uint32_t data_size) 94 { 95 TEEC_Result TeecResult; 96 TEEC_Context TeecContext; 97 TEEC_Session TeecSession; 98 uint32_t ErrorOrigin; 99 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 100 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 101 TEEC_UUID *TeecUuid = &tempuuid; 102 TEEC_Operation TeecOperation = {0}; 103 struct blk_desc *dev_desc; 104 dev_desc = rockchip_get_bootdev(); 105 if (!dev_desc) { 106 printf("%s: dev_desc is NULL!\n", __func__); 107 return -TEEC_ERROR_GENERIC; 108 } 109 110 TeecResult = OpteeClientApiLibInitialize(); 111 if (TeecResult != TEEC_SUCCESS) 112 return TeecResult; 113 114 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 115 if (TeecResult != TEEC_SUCCESS) 116 return TeecResult; 117 118 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 119 TEEC_NONE, 120 TEEC_NONE, 121 TEEC_NONE); 122 /*0 nand or emmc "security" partition , 1 rpmb*/ 123 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 124 TeecOperation.params[0].value.a = 1; 125 else 126 TeecOperation.params[0].value.a = 0; 127 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 128 TeecOperation.params[0].value.a = 0; 129 #endif 130 131 TeecResult = TEEC_OpenSession(&TeecContext, 132 &TeecSession, 133 TeecUuid, 134 TEEC_LOGIN_PUBLIC, 135 NULL, 136 &TeecOperation, 137 &ErrorOrigin); 138 if (TeecResult != TEEC_SUCCESS) 139 return TeecResult; 140 141 TEEC_SharedMemory SharedMem0 = {0}; 142 143 SharedMem0.size = filename_size; 144 SharedMem0.flags = 0; 145 146 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 147 if (TeecResult != TEEC_SUCCESS) 148 goto exit; 149 150 memcpy(SharedMem0.buffer, filename, SharedMem0.size); 151 152 TEEC_SharedMemory SharedMem1 = {0}; 153 154 SharedMem1.size = data_size; 155 SharedMem1.flags = 0; 156 157 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 158 if (TeecResult != TEEC_SUCCESS) 159 goto exit; 160 161 memcpy(SharedMem1.buffer, data, SharedMem1.size); 162 163 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 164 TeecOperation.params[0].tmpref.size = SharedMem0.size; 165 166 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 167 TeecOperation.params[1].tmpref.size = SharedMem1.size; 168 169 170 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 171 TEEC_MEMREF_TEMP_INOUT, 172 TEEC_NONE, 173 TEEC_NONE); 174 175 TeecResult = TEEC_InvokeCommand(&TeecSession, 176 1, 177 &TeecOperation, 178 &ErrorOrigin); 179 if (TeecResult != TEEC_SUCCESS) 180 goto exit; 181 exit: 182 TEEC_ReleaseSharedMemory(&SharedMem0); 183 TEEC_ReleaseSharedMemory(&SharedMem1); 184 TEEC_CloseSession(&TeecSession); 185 TEEC_FinalizeContext(&TeecContext); 186 187 return TeecResult; 188 } 189 190 static uint32_t trusty_base_read_security_data(char *filename, 191 uint32_t filename_size, 192 uint8_t *data, 193 uint32_t data_size) 194 { 195 TEEC_Result TeecResult; 196 TEEC_Context TeecContext; 197 TEEC_Session TeecSession; 198 uint32_t ErrorOrigin; 199 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 200 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 201 TEEC_UUID *TeecUuid = &tempuuid; 202 TEEC_Operation TeecOperation = {0}; 203 204 struct blk_desc *dev_desc; 205 dev_desc = rockchip_get_bootdev(); 206 if (!dev_desc) { 207 printf("%s: dev_desc is NULL!\n", __func__); 208 return -TEEC_ERROR_GENERIC; 209 } 210 211 TeecResult = OpteeClientApiLibInitialize(); 212 if (TeecResult != TEEC_SUCCESS) 213 return TeecResult; 214 215 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 216 if (TeecResult != TEEC_SUCCESS) 217 return TeecResult; 218 219 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 220 TEEC_NONE, 221 TEEC_NONE, 222 TEEC_NONE); 223 /*0 nand or emmc "security" partition , 1 rpmb*/ 224 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 225 TeecOperation.params[0].value.a = 1; 226 else 227 TeecOperation.params[0].value.a = 0; 228 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 229 TeecOperation.params[0].value.a = 0; 230 #endif 231 232 TeecResult = TEEC_OpenSession(&TeecContext, 233 &TeecSession, 234 TeecUuid, 235 TEEC_LOGIN_PUBLIC, 236 NULL, 237 &TeecOperation, 238 &ErrorOrigin); 239 if (TeecResult != TEEC_SUCCESS) 240 return TeecResult; 241 242 TEEC_SharedMemory SharedMem0 = {0}; 243 244 SharedMem0.size = filename_size; 245 SharedMem0.flags = 0; 246 247 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 248 if (TeecResult != TEEC_SUCCESS) 249 goto exit; 250 251 memcpy(SharedMem0.buffer, filename, SharedMem0.size); 252 253 TEEC_SharedMemory SharedMem1 = {0}; 254 255 SharedMem1.size = data_size; 256 SharedMem1.flags = 0; 257 258 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 259 if (TeecResult != TEEC_SUCCESS) 260 goto exit; 261 262 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 263 TeecOperation.params[0].tmpref.size = SharedMem0.size; 264 265 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 266 TeecOperation.params[1].tmpref.size = SharedMem1.size; 267 268 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 269 TEEC_MEMREF_TEMP_INOUT, 270 TEEC_NONE, 271 TEEC_NONE); 272 273 TeecResult = TEEC_InvokeCommand(&TeecSession, 274 0, 275 &TeecOperation, 276 &ErrorOrigin); 277 if (TeecResult == TEEC_SUCCESS) 278 memcpy(data, SharedMem1.buffer, SharedMem1.size); 279 exit: 280 TEEC_ReleaseSharedMemory(&SharedMem0); 281 TEEC_ReleaseSharedMemory(&SharedMem1); 282 TEEC_CloseSession(&TeecSession); 283 TEEC_FinalizeContext(&TeecContext); 284 285 return TeecResult; 286 } 287 288 static uint32_t trusty_base_end_security_data(void) 289 { 290 TEEC_Result TeecResult; 291 TEEC_Context TeecContext; 292 TEEC_Session TeecSession; 293 uint32_t ErrorOrigin; 294 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 295 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 296 TEEC_UUID *TeecUuid = &tempuuid; 297 TEEC_Operation TeecOperation = {0}; 298 299 TeecResult = OpteeClientApiLibInitialize(); 300 if (TeecResult != TEEC_SUCCESS) 301 return TeecResult; 302 303 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 304 if (TeecResult != TEEC_SUCCESS) 305 return TeecResult; 306 307 TeecResult = TEEC_OpenSession(&TeecContext, 308 &TeecSession, 309 TeecUuid, 310 TEEC_LOGIN_PUBLIC, 311 NULL, 312 NULL, 313 &ErrorOrigin); 314 if (TeecResult != TEEC_SUCCESS) 315 return TeecResult; 316 317 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 318 TEEC_NONE, 319 TEEC_NONE, 320 TEEC_NONE); 321 322 TeecResult = TEEC_InvokeCommand(&TeecSession, 323 2, 324 &TeecOperation, 325 &ErrorOrigin); 326 if (TeecResult != TEEC_SUCCESS) 327 goto exit; 328 exit: 329 TEEC_CloseSession(&TeecSession); 330 TEEC_FinalizeContext(&TeecContext); 331 332 return TeecResult; 333 } 334 335 uint32_t trusty_read_rollback_index(uint32_t slot, uint64_t *value) 336 { 337 char hs[9]; 338 339 b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9); 340 341 return trusty_base_read_security_data(hs, 8, (uint8_t *)value, 8); 342 } 343 344 uint32_t trusty_write_rollback_index(uint32_t slot, uint64_t value) 345 { 346 char hs[9]; 347 348 b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9); 349 350 return trusty_base_write_security_data(hs, 8, (uint8_t *)&value, 8); 351 } 352 353 uint32_t trusty_read_permanent_attributes(uint8_t *attributes, uint32_t size) 354 { 355 return trusty_base_read_security_data("attributes", 356 sizeof("attributes"), attributes, size); 357 } 358 359 uint32_t trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size) 360 { 361 return trusty_base_write_security_data("attributes", 362 sizeof("attributes"), attributes, size); 363 } 364 365 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes) 366 { 367 return trusty_base_read_security_data("attributes_flag", 368 sizeof("attributes_flag"), attributes, 1); 369 } 370 371 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes) 372 { 373 return trusty_base_write_security_data("attributes_flag", 374 sizeof("attributes_flag"), &attributes, 1); 375 } 376 377 uint32_t trusty_read_permanent_attributes_cer(uint8_t *attributes, 378 uint32_t size) 379 { 380 return trusty_base_read_security_data("rsacer", 381 sizeof("rsacer"), attributes, size); 382 } 383 384 uint32_t trusty_write_permanent_attributes_cer(uint8_t *attributes, 385 uint32_t size) 386 { 387 return trusty_base_write_security_data("rsacer", 388 sizeof("rsacer"), attributes, size); 389 } 390 391 uint32_t trusty_read_lock_state(uint8_t *lock_state) 392 { 393 return trusty_base_read_security_data("lock_state", 394 sizeof("lock_state"), lock_state, 1); 395 } 396 397 uint32_t trusty_write_lock_state(uint8_t lock_state) 398 { 399 return trusty_base_write_security_data("lock_state", 400 sizeof("lock_state"), &lock_state, 1); 401 } 402 403 uint32_t trusty_read_flash_lock_state(uint8_t *flash_lock_state) 404 { 405 return trusty_base_read_security_data("flash_lock_state", 406 sizeof("flash_lock_state"), flash_lock_state, 1); 407 } 408 409 uint32_t trusty_write_flash_lock_state(uint8_t flash_lock_state) 410 { 411 return trusty_base_write_security_data("flash_lock_state", 412 sizeof("flash_lock_state"), &flash_lock_state, 1); 413 } 414 415 static uint32_t trusty_base_end_efuse_or_otp(void) 416 { 417 TEEC_Result TeecResult; 418 TEEC_Context TeecContext; 419 TEEC_Session TeecSession; 420 uint32_t ErrorOrigin; 421 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 422 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 423 424 TEEC_UUID *TeecUuid = &tempuuid; 425 TEEC_Operation TeecOperation = {0}; 426 427 TeecResult = OpteeClientApiLibInitialize(); 428 if (TeecResult != TEEC_SUCCESS) 429 return TeecResult; 430 431 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 432 if (TeecResult != TEEC_SUCCESS) 433 return TeecResult; 434 435 TeecResult = TEEC_OpenSession(&TeecContext, 436 &TeecSession, 437 TeecUuid, 438 TEEC_LOGIN_PUBLIC, 439 NULL, 440 NULL, 441 &ErrorOrigin); 442 if (TeecResult != TEEC_SUCCESS) 443 return TeecResult; 444 445 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 446 TEEC_NONE, 447 TEEC_NONE, 448 TEEC_NONE); 449 450 TeecResult = TEEC_InvokeCommand(&TeecSession, 451 STORAGE_CMD_UBOOT_END_OTP, 452 &TeecOperation, 453 &ErrorOrigin); 454 if (TeecResult != TEEC_SUCCESS) 455 goto exit; 456 exit: 457 TEEC_CloseSession(&TeecSession); 458 TEEC_FinalizeContext(&TeecContext); 459 460 return TeecResult; 461 } 462 463 static uint32_t trusty_base_efuse_or_otp_operation(uint32_t cmd, 464 uint8_t is_write, 465 uint32_t *buf, 466 uint32_t length) 467 { 468 TEEC_Result TeecResult; 469 TEEC_Context TeecContext; 470 TEEC_Session TeecSession; 471 uint32_t ErrorOrigin; 472 473 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 474 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 475 TEEC_UUID *TeecUuid = &tempuuid; 476 TEEC_Operation TeecOperation = {0}; 477 478 TeecResult = OpteeClientApiLibInitialize(); 479 if (TeecResult != TEEC_SUCCESS) 480 return TeecResult; 481 482 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 483 if (TeecResult != TEEC_SUCCESS) 484 return TeecResult; 485 486 TeecResult = TEEC_OpenSession(&TeecContext, 487 &TeecSession, 488 TeecUuid, 489 TEEC_LOGIN_PUBLIC, 490 NULL, 491 NULL, 492 &ErrorOrigin); 493 if (TeecResult != TEEC_SUCCESS) 494 return TeecResult; 495 496 TEEC_SharedMemory SharedMem0 = {0}; 497 498 SharedMem0.size = length * sizeof(uint32_t); 499 SharedMem0.flags = 0; 500 501 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 502 if (TeecResult != TEEC_SUCCESS) 503 goto exit; 504 505 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 506 TeecOperation.params[0].tmpref.size = SharedMem0.size; 507 508 if (is_write) { 509 memcpy(SharedMem0.buffer, buf, SharedMem0.size); 510 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 511 TEEC_NONE, 512 TEEC_NONE, 513 TEEC_NONE); 514 515 } else { 516 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, 517 TEEC_NONE, 518 TEEC_NONE, 519 TEEC_NONE); 520 } 521 522 TeecResult = TEEC_InvokeCommand(&TeecSession, 523 cmd, 524 &TeecOperation, 525 &ErrorOrigin); 526 if (TeecResult != TEEC_SUCCESS) 527 goto exit; 528 529 if (!is_write) 530 memcpy(buf, SharedMem0.buffer, SharedMem0.size); 531 532 exit: 533 TEEC_ReleaseSharedMemory(&SharedMem0); 534 TEEC_CloseSession(&TeecSession); 535 TEEC_FinalizeContext(&TeecContext); 536 537 return TeecResult; 538 } 539 540 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length) 541 { 542 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ATTRIBUTE_HASH, 543 false, buf, length); 544 } 545 546 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length) 547 { 548 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_ATTRIBUTE_HASH, 549 true, buf, length); 550 } 551 552 uint32_t trusty_notify_optee_uboot_end(void) 553 { 554 TEEC_Result res; 555 556 res = trusty_base_end_security_data(); 557 res |= trusty_base_end_efuse_or_otp(); 558 return res; 559 } 560 561 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length) 562 { 563 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_VBOOTKEY_HASH, 564 false, buf, length); 565 } 566 567 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length) 568 { 569 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_VBOOTKEY_HASH, 570 true, buf, length); 571 } 572 573 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag) 574 { 575 uint32_t bootflag; 576 TEEC_Result TeecResult; 577 578 *flag = 0; 579 580 TeecResult = trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ENABLE_FLAG, 581 false, &bootflag, 1); 582 583 if (TeecResult == TEEC_SUCCESS) { 584 #if defined(CONFIG_ROCKCHIP_RK3288) 585 if (bootflag == 0x00000001) 586 *flag = 1; 587 #else 588 if (bootflag == 0x000000FF) 589 *flag = 1; 590 #endif 591 } 592 return TeecResult; 593 } 594 595 uint32_t trusty_write_ta_encryption_key(uint32_t *buf, uint32_t length) 596 { 597 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_TA_ENCRYPTION_KEY, 598 true, buf, length); 599 } 600 601 uint32_t trusty_check_security_level_flag(uint8_t flag) 602 { 603 uint32_t levelflag; 604 605 levelflag = flag; 606 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_CHECK_SECURITY_LEVEL_FLAG, 607 true, &levelflag, 1); 608 } 609 610 uint32_t trusty_write_oem_huk(uint32_t *buf, uint32_t length) 611 { 612 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_OEM_HUK, 613 true, buf, length); 614 } 615 616 void trusty_select_security_level(void) 617 { 618 #if (CONFIG_OPTEE_SECURITY_LEVEL > 0) 619 TEEC_Result TeecResult; 620 621 TeecResult = trusty_check_security_level_flag(CONFIG_OPTEE_SECURITY_LEVEL); 622 if (TeecResult == TEE_ERROR_CANCEL) { 623 run_command("download", 0); 624 return; 625 } 626 627 if (TeecResult == TEEC_SUCCESS) 628 debug("optee select security level success!"); 629 else 630 panic("optee select security level fail!"); 631 632 return; 633 #endif 634 } 635 636 uint32_t trusty_write_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len) 637 { 638 TEEC_Result TeecResult; 639 TEEC_Context TeecContext; 640 TEEC_Session TeecSession; 641 uint32_t ErrorOrigin; 642 643 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 644 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 645 TEEC_UUID *TeecUuid = &tempuuid; 646 TEEC_Operation TeecOperation = {0}; 647 648 TeecResult = OpteeClientApiLibInitialize(); 649 if (TeecResult != TEEC_SUCCESS) 650 return TeecResult; 651 652 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 653 if (TeecResult != TEEC_SUCCESS) 654 return TeecResult; 655 656 TeecResult = TEEC_OpenSession(&TeecContext, 657 &TeecSession, 658 TeecUuid, 659 TEEC_LOGIN_PUBLIC, 660 NULL, 661 NULL, 662 &ErrorOrigin); 663 if (TeecResult != TEEC_SUCCESS) 664 return TeecResult; 665 666 TeecOperation.params[0].value.a = byte_off; 667 668 TEEC_SharedMemory SharedMem = {0}; 669 670 SharedMem.size = byte_len; 671 SharedMem.flags = 0; 672 673 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 674 if (TeecResult != TEEC_SUCCESS) 675 goto exit; 676 677 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 678 TeecOperation.params[1].tmpref.size = SharedMem.size; 679 680 memcpy(SharedMem.buffer, byte_buf, SharedMem.size); 681 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 682 TEEC_MEMREF_TEMP_INPUT, 683 TEEC_NONE, 684 TEEC_NONE); 685 686 TeecResult = TEEC_InvokeCommand(&TeecSession, 687 STORAGE_CMD_WRITE_OEM_NS_OTP, 688 &TeecOperation, 689 &ErrorOrigin); 690 if (TeecResult != TEEC_SUCCESS) 691 goto exit; 692 693 exit: 694 TEEC_ReleaseSharedMemory(&SharedMem); 695 TEEC_CloseSession(&TeecSession); 696 TEEC_FinalizeContext(&TeecContext); 697 698 return TeecResult; 699 } 700 701 uint32_t trusty_read_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len) 702 { 703 TEEC_Result TeecResult; 704 TEEC_Context TeecContext; 705 TEEC_Session TeecSession; 706 uint32_t ErrorOrigin; 707 708 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 709 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 710 TEEC_UUID *TeecUuid = &tempuuid; 711 TEEC_Operation TeecOperation = {0}; 712 713 TeecResult = OpteeClientApiLibInitialize(); 714 if (TeecResult != TEEC_SUCCESS) 715 return TeecResult; 716 717 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 718 if (TeecResult != TEEC_SUCCESS) 719 return TeecResult; 720 721 TeecResult = TEEC_OpenSession(&TeecContext, 722 &TeecSession, 723 TeecUuid, 724 TEEC_LOGIN_PUBLIC, 725 NULL, 726 NULL, 727 &ErrorOrigin); 728 if (TeecResult != TEEC_SUCCESS) 729 return TeecResult; 730 731 TeecOperation.params[0].value.a = byte_off; 732 733 TEEC_SharedMemory SharedMem = {0}; 734 735 SharedMem.size = byte_len; 736 SharedMem.flags = 0; 737 738 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 739 if (TeecResult != TEEC_SUCCESS) 740 goto exit; 741 742 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 743 TeecOperation.params[1].tmpref.size = SharedMem.size; 744 745 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 746 TEEC_MEMREF_TEMP_OUTPUT, 747 TEEC_NONE, 748 TEEC_NONE); 749 750 TeecResult = TEEC_InvokeCommand(&TeecSession, 751 STORAGE_CMD_READ_OEM_NS_OTP, 752 &TeecOperation, 753 &ErrorOrigin); 754 if (TeecResult != TEEC_SUCCESS) 755 goto exit; 756 757 memcpy(byte_buf, SharedMem.buffer, SharedMem.size); 758 759 exit: 760 TEEC_ReleaseSharedMemory(&SharedMem); 761 TEEC_CloseSession(&TeecSession); 762 TEEC_FinalizeContext(&TeecContext); 763 764 return TeecResult; 765 } 766 767 uint32_t trusty_write_oem_otp_key(enum RK_OEM_OTP_KEYID key_id, 768 uint8_t *byte_buf, uint32_t byte_len) 769 { 770 TEEC_Result TeecResult; 771 TEEC_Context TeecContext; 772 TEEC_Session TeecSession; 773 uint32_t ErrorOrigin; 774 775 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 776 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 777 TEEC_UUID *TeecUuid = &tempuuid; 778 TEEC_Operation TeecOperation = {0}; 779 780 TeecResult = OpteeClientApiLibInitialize(); 781 if (TeecResult != TEEC_SUCCESS) 782 return TeecResult; 783 784 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 785 if (TeecResult != TEEC_SUCCESS) 786 return TeecResult; 787 788 TeecResult = TEEC_OpenSession(&TeecContext, 789 &TeecSession, 790 TeecUuid, 791 TEEC_LOGIN_PUBLIC, 792 NULL, 793 NULL, 794 &ErrorOrigin); 795 if (TeecResult != TEEC_SUCCESS) 796 return TeecResult; 797 798 TeecOperation.params[0].value.a = key_id; 799 800 TEEC_SharedMemory SharedMem = {0}; 801 802 SharedMem.size = byte_len; 803 SharedMem.flags = 0; 804 805 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 806 if (TeecResult != TEEC_SUCCESS) 807 goto exit; 808 809 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 810 TeecOperation.params[1].tmpref.size = SharedMem.size; 811 812 memcpy(SharedMem.buffer, byte_buf, SharedMem.size); 813 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 814 TEEC_MEMREF_TEMP_INPUT, 815 TEEC_NONE, 816 TEEC_NONE); 817 818 TeecResult = TEEC_InvokeCommand(&TeecSession, 819 STORAGE_CMD_WRITE_OEM_OTP_KEY, 820 &TeecOperation, 821 &ErrorOrigin); 822 if (TeecResult != TEEC_SUCCESS) 823 goto exit; 824 825 exit: 826 TEEC_ReleaseSharedMemory(&SharedMem); 827 TEEC_CloseSession(&TeecSession); 828 TEEC_FinalizeContext(&TeecContext); 829 830 return TeecResult; 831 } 832 833 uint32_t trusty_oem_otp_key_is_written(enum RK_OEM_OTP_KEYID key_id, uint8_t *value) 834 { 835 TEEC_Result TeecResult; 836 TEEC_Context TeecContext; 837 TEEC_Session TeecSession; 838 uint32_t ErrorOrigin; 839 840 *value = 0xFF; 841 842 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 843 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 844 TEEC_UUID *TeecUuid = &tempuuid; 845 TEEC_Operation TeecOperation = {0}; 846 847 TeecResult = OpteeClientApiLibInitialize(); 848 if (TeecResult != TEEC_SUCCESS) 849 return TeecResult; 850 851 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 852 if (TeecResult != TEEC_SUCCESS) 853 return TeecResult; 854 855 TeecResult = TEEC_OpenSession(&TeecContext, 856 &TeecSession, 857 TeecUuid, 858 TEEC_LOGIN_PUBLIC, 859 NULL, 860 NULL, 861 &ErrorOrigin); 862 if (TeecResult != TEEC_SUCCESS) 863 return TeecResult; 864 865 TeecOperation.params[0].value.a = key_id; 866 867 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, 868 TEEC_NONE, 869 TEEC_NONE, 870 TEEC_NONE); 871 872 TeecResult = TEEC_InvokeCommand(&TeecSession, 873 STORAGE_CMD_OEM_OTP_KEY_IS_WRITTEN, 874 &TeecOperation, 875 &ErrorOrigin); 876 if (TeecResult == TEEC_SUCCESS) 877 *value = TeecOperation.params[0].value.b; 878 879 TEEC_CloseSession(&TeecSession); 880 TEEC_FinalizeContext(&TeecContext); 881 882 return TeecResult; 883 } 884 885 uint32_t trusty_set_oem_hr_otp_read_lock(enum RK_OEM_OTP_KEYID key_id) 886 { 887 TEEC_Result TeecResult; 888 TEEC_Context TeecContext; 889 TEEC_Session TeecSession; 890 uint32_t ErrorOrigin; 891 892 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 893 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 894 TEEC_UUID *TeecUuid = &tempuuid; 895 TEEC_Operation TeecOperation = {0}; 896 897 TeecResult = OpteeClientApiLibInitialize(); 898 if (TeecResult != TEEC_SUCCESS) 899 return TeecResult; 900 901 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 902 if (TeecResult != TEEC_SUCCESS) 903 return TeecResult; 904 905 TeecResult = TEEC_OpenSession(&TeecContext, 906 &TeecSession, 907 TeecUuid, 908 TEEC_LOGIN_PUBLIC, 909 NULL, 910 NULL, 911 &ErrorOrigin); 912 if (TeecResult != TEEC_SUCCESS) 913 return TeecResult; 914 915 TeecOperation.params[0].value.a = key_id; 916 917 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 918 TEEC_NONE, 919 TEEC_NONE, 920 TEEC_NONE); 921 922 TeecResult = TEEC_InvokeCommand(&TeecSession, 923 STORAGE_CMD_SET_OEM_HR_OTP_READ_LOCK, 924 &TeecOperation, 925 &ErrorOrigin); 926 if (TeecResult != TEEC_SUCCESS) 927 goto exit; 928 929 exit: 930 TEEC_CloseSession(&TeecSession); 931 TEEC_FinalizeContext(&TeecContext); 932 933 return TeecResult; 934 } 935 936 uint32_t trusty_oem_otp_key_cipher(enum RK_OEM_OTP_KEYID key_id, rk_cipher_config *config, 937 uint32_t src_phys_addr, uint32_t dst_phys_addr, 938 uint32_t len) 939 { 940 TEEC_Result TeecResult; 941 TEEC_Context TeecContext; 942 TEEC_Session TeecSession; 943 TEEC_Operation TeecOperation = {0}; 944 uint32_t ErrorOrigin; 945 TEEC_UUID uuid = RK_CRYPTO_SERVICE_UUID; 946 TEEC_SharedMemory SharedMem_config = {0}; 947 948 if (key_id != RK_OEM_OTP_KEY0 && 949 key_id != RK_OEM_OTP_KEY1 && 950 key_id != RK_OEM_OTP_KEY2 && 951 key_id != RK_OEM_OTP_KEY3 && 952 key_id != RK_OEM_OTP_KEY_FW) 953 return TEEC_ERROR_BAD_PARAMETERS; 954 955 if (!config) 956 return TEEC_ERROR_BAD_PARAMETERS; 957 958 if (config->algo != RK_ALGO_AES && config->algo != RK_ALGO_SM4) 959 return TEEC_ERROR_BAD_PARAMETERS; 960 961 if (config->mode >= RK_CIPHER_MODE_XTS) 962 return TEEC_ERROR_BAD_PARAMETERS; 963 964 if (config->operation != RK_MODE_ENCRYPT && 965 config->operation != RK_MODE_DECRYPT) 966 return TEEC_ERROR_BAD_PARAMETERS; 967 968 if (config->key_len != 16 && 969 config->key_len != 24 && 970 config->key_len != 32) 971 return TEEC_ERROR_BAD_PARAMETERS; 972 973 if (key_id == RK_OEM_OTP_KEY_FW && config->key_len != 16) 974 return TEEC_ERROR_BAD_PARAMETERS; 975 976 #if defined(CONFIG_ROCKCHIP_RV1126) 977 if (config->key_len == 24) 978 return TEEC_ERROR_BAD_PARAMETERS; 979 #endif 980 981 if (len % AES_BLOCK_SIZE || 982 len == 0) 983 return TEEC_ERROR_BAD_PARAMETERS; 984 985 if (!src_phys_addr || !dst_phys_addr) 986 return TEEC_ERROR_BAD_PARAMETERS; 987 988 TeecResult = OpteeClientApiLibInitialize(); 989 if (TeecResult != TEEC_SUCCESS) 990 return TeecResult; 991 992 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 993 if (TeecResult != TEEC_SUCCESS) 994 return TeecResult; 995 996 TeecResult = TEEC_OpenSession(&TeecContext, 997 &TeecSession, 998 &uuid, 999 TEEC_LOGIN_PUBLIC, 1000 NULL, 1001 NULL, 1002 &ErrorOrigin); 1003 if (TeecResult != TEEC_SUCCESS) 1004 goto exit; 1005 1006 SharedMem_config.size = sizeof(rk_cipher_config); 1007 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem_config); 1008 if (TeecResult != TEEC_SUCCESS) 1009 goto exit; 1010 1011 memcpy(SharedMem_config.buffer, config, sizeof(rk_cipher_config)); 1012 TeecOperation.params[0].value.a = key_id; 1013 TeecOperation.params[1].tmpref.buffer = SharedMem_config.buffer; 1014 TeecOperation.params[1].tmpref.size = SharedMem_config.size; 1015 TeecOperation.params[2].value.a = src_phys_addr; 1016 TeecOperation.params[2].value.b = len; 1017 TeecOperation.params[3].value.a = dst_phys_addr; 1018 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1019 TEEC_MEMREF_TEMP_INPUT, 1020 TEEC_VALUE_INPUT, 1021 TEEC_VALUE_INPUT); 1022 1023 crypto_flush_cacheline(src_phys_addr, len); 1024 crypto_flush_cacheline(dst_phys_addr, len); 1025 1026 TeecResult = TEEC_InvokeCommand(&TeecSession, 1027 CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER, 1028 &TeecOperation, 1029 &ErrorOrigin); 1030 1031 crypto_invalidate_cacheline(dst_phys_addr, len); 1032 1033 exit: 1034 TEEC_ReleaseSharedMemory(&SharedMem_config); 1035 TEEC_CloseSession(&TeecSession); 1036 TEEC_FinalizeContext(&TeecContext); 1037 return TeecResult; 1038 } 1039 1040 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size) 1041 { 1042 TEEC_Result TeecResult; 1043 TEEC_Context TeecContext; 1044 TEEC_Session TeecSession; 1045 uint32_t ErrorOrigin; 1046 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1047 { 0xa8, 0x69, 0x9c, 0xe6, 1048 0x88, 0x6c, 0x5d, 0x5d 1049 } 1050 }; 1051 TEEC_UUID *TeecUuid = &tempuuid; 1052 TEEC_Operation TeecOperation = {0}; 1053 struct blk_desc *dev_desc; 1054 dev_desc = rockchip_get_bootdev(); 1055 if (!dev_desc) { 1056 printf("%s: dev_desc is NULL!\n", __func__); 1057 return -TEEC_ERROR_GENERIC; 1058 } 1059 1060 TeecResult = OpteeClientApiLibInitialize(); 1061 if (TeecResult != TEEC_SUCCESS) 1062 return TeecResult; 1063 1064 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1065 if (TeecResult != TEEC_SUCCESS) 1066 return TeecResult; 1067 1068 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1069 TEEC_NONE, 1070 TEEC_NONE, 1071 TEEC_NONE); 1072 /*0 nand or emmc "security" partition , 1 rpmb*/ 1073 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1074 TeecOperation.params[0].value.a = 1; 1075 else 1076 TeecOperation.params[0].value.a = 0; 1077 1078 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1079 TeecOperation.params[0].value.a = 0; 1080 #endif 1081 1082 TeecResult = TEEC_OpenSession(&TeecContext, 1083 &TeecSession, 1084 TeecUuid, 1085 TEEC_LOGIN_PUBLIC, 1086 NULL, 1087 &TeecOperation, 1088 &ErrorOrigin); 1089 if (TeecResult != TEEC_SUCCESS) 1090 return TeecResult; 1091 1092 TEEC_SharedMemory SharedMem0 = {0}; 1093 1094 SharedMem0.size = *dh_size; 1095 SharedMem0.flags = 0; 1096 1097 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1098 if (TeecResult != TEEC_SUCCESS) 1099 goto exit; 1100 1101 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1102 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1103 1104 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1105 TEEC_NONE, 1106 TEEC_NONE, 1107 TEEC_NONE); 1108 1109 TeecResult = TEEC_InvokeCommand(&TeecSession, 1110 143, 1111 &TeecOperation, 1112 &ErrorOrigin); 1113 if (TeecResult != TEEC_SUCCESS) 1114 goto exit; 1115 1116 *dh_size = TeecOperation.params[0].tmpref.size; 1117 memcpy(dh, SharedMem0.buffer, SharedMem0.size); 1118 exit: 1119 TEEC_ReleaseSharedMemory(&SharedMem0); 1120 TEEC_CloseSession(&TeecSession); 1121 TEEC_FinalizeContext(&TeecContext); 1122 1123 return TeecResult; 1124 } 1125 1126 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size) 1127 { 1128 TEEC_Result TeecResult; 1129 TEEC_Context TeecContext; 1130 TEEC_Session TeecSession; 1131 uint32_t ErrorOrigin; 1132 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1133 { 0xa8, 0x69, 0x9c, 0xe6, 1134 0x88, 0x6c, 0x5d, 0x5d 1135 } 1136 }; 1137 TEEC_UUID *TeecUuid = &tempuuid; 1138 TEEC_Operation TeecOperation = {0}; 1139 struct blk_desc *dev_desc; 1140 dev_desc = rockchip_get_bootdev(); 1141 if (!dev_desc) { 1142 printf("%s: dev_desc is NULL!\n", __func__); 1143 return -TEEC_ERROR_GENERIC; 1144 } 1145 1146 TeecResult = OpteeClientApiLibInitialize(); 1147 if (TeecResult != TEEC_SUCCESS) 1148 return TeecResult; 1149 1150 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1151 if (TeecResult != TEEC_SUCCESS) 1152 return TeecResult; 1153 1154 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1155 TEEC_NONE, 1156 TEEC_NONE, 1157 TEEC_NONE); 1158 /*0 nand or emmc "security" partition , 1 rpmb*/ 1159 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1160 TeecOperation.params[0].value.a = 1; 1161 else 1162 TeecOperation.params[0].value.a = 0; 1163 1164 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1165 TeecOperation.params[0].value.a = 0; 1166 #endif 1167 1168 TeecResult = TEEC_OpenSession(&TeecContext, 1169 &TeecSession, 1170 TeecUuid, 1171 TEEC_LOGIN_PUBLIC, 1172 NULL, 1173 &TeecOperation, 1174 &ErrorOrigin); 1175 if (TeecResult != TEEC_SUCCESS) 1176 return TeecResult; 1177 1178 TEEC_SharedMemory SharedMem0 = {0}; 1179 1180 SharedMem0.size = *uuid_size; 1181 SharedMem0.flags = 0; 1182 1183 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1184 if (TeecResult != TEEC_SUCCESS) 1185 goto exit; 1186 1187 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1188 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1189 1190 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1191 TEEC_NONE, 1192 TEEC_NONE, 1193 TEEC_NONE); 1194 1195 TeecResult = TEEC_InvokeCommand(&TeecSession, 1196 144, 1197 &TeecOperation, 1198 &ErrorOrigin); 1199 if (TeecResult != TEEC_SUCCESS) 1200 goto exit; 1201 1202 *uuid_size = TeecOperation.params[0].tmpref.size; 1203 memcpy(uuid, SharedMem0.buffer, SharedMem0.size); 1204 exit: 1205 TEEC_ReleaseSharedMemory(&SharedMem0); 1206 TEEC_CloseSession(&TeecSession); 1207 TEEC_FinalizeContext(&TeecContext); 1208 1209 return TeecResult; 1210 } 1211 1212 uint32_t trusty_attest_get_ca(uint8_t *operation_start, 1213 uint32_t *operation_size, 1214 uint8_t *out, 1215 uint32_t *out_len) 1216 { 1217 TEEC_Result TeecResult; 1218 TEEC_Context TeecContext; 1219 TEEC_Session TeecSession; 1220 uint32_t ErrorOrigin; 1221 1222 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1223 { 0xa8, 0x69, 0x9c, 0xe6, 1224 0x88, 0x6c, 0x5d, 0x5d 1225 } 1226 }; 1227 1228 TEEC_UUID *TeecUuid = &tempuuid; 1229 TEEC_Operation TeecOperation = {0}; 1230 struct blk_desc *dev_desc; 1231 dev_desc = rockchip_get_bootdev(); 1232 if (!dev_desc) { 1233 printf("%s: dev_desc is NULL!\n", __func__); 1234 return -TEEC_ERROR_GENERIC; 1235 } 1236 1237 TeecResult = OpteeClientApiLibInitialize(); 1238 if (TeecResult != TEEC_SUCCESS) 1239 return TeecResult; 1240 1241 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1242 if (TeecResult != TEEC_SUCCESS) 1243 return TeecResult; 1244 1245 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1246 TEEC_NONE, 1247 TEEC_NONE, 1248 TEEC_NONE); 1249 /*0 nand or emmc "security" partition , 1 rpmb*/ 1250 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1251 TeecOperation.params[0].value.a = 1; 1252 else 1253 TeecOperation.params[0].value.a = 0; 1254 1255 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1256 TeecOperation.params[0].value.a = 0; 1257 #endif 1258 1259 TeecResult = TEEC_OpenSession(&TeecContext, 1260 &TeecSession, 1261 TeecUuid, 1262 TEEC_LOGIN_PUBLIC, 1263 NULL, 1264 &TeecOperation, 1265 &ErrorOrigin); 1266 if (TeecResult != TEEC_SUCCESS) 1267 return TeecResult; 1268 1269 TEEC_SharedMemory SharedMem0 = {0}; 1270 1271 SharedMem0.size = *operation_size; 1272 SharedMem0.flags = 0; 1273 1274 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1275 if (TeecResult != TEEC_SUCCESS) 1276 goto exit; 1277 1278 memcpy(SharedMem0.buffer, operation_start, SharedMem0.size); 1279 1280 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1281 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1282 1283 TEEC_SharedMemory SharedMem1 = {0}; 1284 1285 SharedMem1.size = *out_len; 1286 SharedMem1.flags = 0; 1287 1288 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 1289 if (TeecResult != TEEC_SUCCESS) 1290 goto exit; 1291 1292 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 1293 TeecOperation.params[1].tmpref.size = SharedMem1.size; 1294 1295 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1296 TEEC_MEMREF_TEMP_INOUT, 1297 TEEC_NONE, 1298 TEEC_NONE); 1299 1300 TeecResult = TEEC_InvokeCommand(&TeecSession, 1301 145, 1302 &TeecOperation, 1303 &ErrorOrigin); 1304 if (TeecResult != TEEC_SUCCESS) 1305 goto exit; 1306 1307 *out_len = TeecOperation.params[1].tmpref.size; 1308 memcpy(out, SharedMem1.buffer, SharedMem1.size); 1309 exit: 1310 TEEC_ReleaseSharedMemory(&SharedMem0); 1311 TEEC_ReleaseSharedMemory(&SharedMem1); 1312 TEEC_CloseSession(&TeecSession); 1313 TEEC_FinalizeContext(&TeecContext); 1314 1315 return TeecResult; 1316 } 1317 1318 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size) 1319 { 1320 TEEC_Result TeecResult; 1321 TEEC_Context TeecContext; 1322 TEEC_Session TeecSession; 1323 uint32_t ErrorOrigin; 1324 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1325 { 0xa8, 0x69, 0x9c, 0xe6, 1326 0x88, 0x6c, 0x5d, 0x5d 1327 } 1328 }; 1329 TEEC_UUID *TeecUuid = &tempuuid; 1330 TEEC_Operation TeecOperation = {0}; 1331 struct blk_desc *dev_desc; 1332 dev_desc = rockchip_get_bootdev(); 1333 if (!dev_desc) { 1334 printf("%s: dev_desc is NULL!\n", __func__); 1335 return -TEEC_ERROR_GENERIC; 1336 } 1337 TeecResult = OpteeClientApiLibInitialize(); 1338 if (TeecResult != TEEC_SUCCESS) 1339 return TeecResult; 1340 1341 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1342 if (TeecResult != TEEC_SUCCESS) 1343 return TeecResult; 1344 1345 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1346 TEEC_NONE, 1347 TEEC_NONE, 1348 TEEC_NONE); 1349 /*0 nand or emmc "security" partition , 1 rpmb*/ 1350 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1351 TeecOperation.params[0].value.a = 1; 1352 else 1353 TeecOperation.params[0].value.a = 0; 1354 1355 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1356 TeecOperation.params[0].value.a = 0; 1357 #endif 1358 1359 TeecResult = TEEC_OpenSession(&TeecContext, 1360 &TeecSession, 1361 TeecUuid, 1362 TEEC_LOGIN_PUBLIC, 1363 NULL, 1364 &TeecOperation, 1365 &ErrorOrigin); 1366 if (TeecResult != TEEC_SUCCESS) 1367 return TeecResult; 1368 1369 TEEC_SharedMemory SharedMem0 = {0}; 1370 1371 SharedMem0.size = *ca_response_size; 1372 SharedMem0.flags = 0; 1373 1374 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1375 if (TeecResult != TEEC_SUCCESS) 1376 goto exit; 1377 1378 memcpy(SharedMem0.buffer, ca_response, SharedMem0.size); 1379 1380 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1381 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1382 1383 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1384 TEEC_NONE, 1385 TEEC_NONE, 1386 TEEC_NONE); 1387 1388 TeecResult = TEEC_InvokeCommand(&TeecSession, 1389 146, 1390 &TeecOperation, 1391 &ErrorOrigin); 1392 if (TeecResult != TEEC_SUCCESS) 1393 goto exit; 1394 exit: 1395 TEEC_ReleaseSharedMemory(&SharedMem0); 1396 TEEC_CloseSession(&TeecSession); 1397 TEEC_FinalizeContext(&TeecContext); 1398 1399 return TeecResult; 1400 } 1401