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 void optee_client_init(void) 637 { 638 trusty_select_security_level(); 639 } 640 641 uint32_t trusty_write_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len) 642 { 643 TEEC_Result TeecResult; 644 TEEC_Context TeecContext; 645 TEEC_Session TeecSession; 646 uint32_t ErrorOrigin; 647 648 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 649 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 650 TEEC_UUID *TeecUuid = &tempuuid; 651 TEEC_Operation TeecOperation = {0}; 652 653 TeecResult = OpteeClientApiLibInitialize(); 654 if (TeecResult != TEEC_SUCCESS) 655 return TeecResult; 656 657 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 658 if (TeecResult != TEEC_SUCCESS) 659 return TeecResult; 660 661 TeecResult = TEEC_OpenSession(&TeecContext, 662 &TeecSession, 663 TeecUuid, 664 TEEC_LOGIN_PUBLIC, 665 NULL, 666 NULL, 667 &ErrorOrigin); 668 if (TeecResult != TEEC_SUCCESS) 669 return TeecResult; 670 671 TeecOperation.params[0].value.a = byte_off; 672 673 TEEC_SharedMemory SharedMem = {0}; 674 675 SharedMem.size = byte_len; 676 SharedMem.flags = 0; 677 678 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 679 if (TeecResult != TEEC_SUCCESS) 680 goto exit; 681 682 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 683 TeecOperation.params[1].tmpref.size = SharedMem.size; 684 685 memcpy(SharedMem.buffer, byte_buf, SharedMem.size); 686 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 687 TEEC_MEMREF_TEMP_INPUT, 688 TEEC_NONE, 689 TEEC_NONE); 690 691 TeecResult = TEEC_InvokeCommand(&TeecSession, 692 STORAGE_CMD_WRITE_OEM_NS_OTP, 693 &TeecOperation, 694 &ErrorOrigin); 695 if (TeecResult != TEEC_SUCCESS) 696 goto exit; 697 698 exit: 699 TEEC_ReleaseSharedMemory(&SharedMem); 700 TEEC_CloseSession(&TeecSession); 701 TEEC_FinalizeContext(&TeecContext); 702 703 return TeecResult; 704 } 705 706 uint32_t trusty_read_oem_ns_otp(uint32_t byte_off, uint8_t *byte_buf, uint32_t byte_len) 707 { 708 TEEC_Result TeecResult; 709 TEEC_Context TeecContext; 710 TEEC_Session TeecSession; 711 uint32_t ErrorOrigin; 712 713 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 714 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 715 TEEC_UUID *TeecUuid = &tempuuid; 716 TEEC_Operation TeecOperation = {0}; 717 718 TeecResult = OpteeClientApiLibInitialize(); 719 if (TeecResult != TEEC_SUCCESS) 720 return TeecResult; 721 722 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 723 if (TeecResult != TEEC_SUCCESS) 724 return TeecResult; 725 726 TeecResult = TEEC_OpenSession(&TeecContext, 727 &TeecSession, 728 TeecUuid, 729 TEEC_LOGIN_PUBLIC, 730 NULL, 731 NULL, 732 &ErrorOrigin); 733 if (TeecResult != TEEC_SUCCESS) 734 return TeecResult; 735 736 TeecOperation.params[0].value.a = byte_off; 737 738 TEEC_SharedMemory SharedMem = {0}; 739 740 SharedMem.size = byte_len; 741 SharedMem.flags = 0; 742 743 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 744 if (TeecResult != TEEC_SUCCESS) 745 goto exit; 746 747 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 748 TeecOperation.params[1].tmpref.size = SharedMem.size; 749 750 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 751 TEEC_MEMREF_TEMP_OUTPUT, 752 TEEC_NONE, 753 TEEC_NONE); 754 755 TeecResult = TEEC_InvokeCommand(&TeecSession, 756 STORAGE_CMD_READ_OEM_NS_OTP, 757 &TeecOperation, 758 &ErrorOrigin); 759 if (TeecResult != TEEC_SUCCESS) 760 goto exit; 761 762 memcpy(byte_buf, SharedMem.buffer, SharedMem.size); 763 764 exit: 765 TEEC_ReleaseSharedMemory(&SharedMem); 766 TEEC_CloseSession(&TeecSession); 767 TEEC_FinalizeContext(&TeecContext); 768 769 return TeecResult; 770 } 771 772 uint32_t trusty_write_oem_otp_key(enum RK_OEM_OTP_KEYID key_id, 773 uint8_t *byte_buf, uint32_t byte_len) 774 { 775 TEEC_Result TeecResult; 776 TEEC_Context TeecContext; 777 TEEC_Session TeecSession; 778 uint32_t ErrorOrigin; 779 780 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 781 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 782 TEEC_UUID *TeecUuid = &tempuuid; 783 TEEC_Operation TeecOperation = {0}; 784 785 TeecResult = OpteeClientApiLibInitialize(); 786 if (TeecResult != TEEC_SUCCESS) 787 return TeecResult; 788 789 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 790 if (TeecResult != TEEC_SUCCESS) 791 return TeecResult; 792 793 TeecResult = TEEC_OpenSession(&TeecContext, 794 &TeecSession, 795 TeecUuid, 796 TEEC_LOGIN_PUBLIC, 797 NULL, 798 NULL, 799 &ErrorOrigin); 800 if (TeecResult != TEEC_SUCCESS) 801 return TeecResult; 802 803 TeecOperation.params[0].value.a = key_id; 804 805 TEEC_SharedMemory SharedMem = {0}; 806 807 SharedMem.size = byte_len; 808 SharedMem.flags = 0; 809 810 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem); 811 if (TeecResult != TEEC_SUCCESS) 812 goto exit; 813 814 TeecOperation.params[1].tmpref.buffer = SharedMem.buffer; 815 TeecOperation.params[1].tmpref.size = SharedMem.size; 816 817 memcpy(SharedMem.buffer, byte_buf, SharedMem.size); 818 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 819 TEEC_MEMREF_TEMP_INPUT, 820 TEEC_NONE, 821 TEEC_NONE); 822 823 TeecResult = TEEC_InvokeCommand(&TeecSession, 824 STORAGE_CMD_WRITE_OEM_OTP_KEY, 825 &TeecOperation, 826 &ErrorOrigin); 827 if (TeecResult != TEEC_SUCCESS) 828 goto exit; 829 830 exit: 831 TEEC_ReleaseSharedMemory(&SharedMem); 832 TEEC_CloseSession(&TeecSession); 833 TEEC_FinalizeContext(&TeecContext); 834 835 return TeecResult; 836 } 837 838 uint32_t trusty_oem_otp_key_is_written(enum RK_OEM_OTP_KEYID key_id, uint8_t *value) 839 { 840 TEEC_Result TeecResult; 841 TEEC_Context TeecContext; 842 TEEC_Session TeecSession; 843 uint32_t ErrorOrigin; 844 845 *value = 0xFF; 846 847 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 848 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 849 TEEC_UUID *TeecUuid = &tempuuid; 850 TEEC_Operation TeecOperation = {0}; 851 852 TeecResult = OpteeClientApiLibInitialize(); 853 if (TeecResult != TEEC_SUCCESS) 854 return TeecResult; 855 856 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 857 if (TeecResult != TEEC_SUCCESS) 858 return TeecResult; 859 860 TeecResult = TEEC_OpenSession(&TeecContext, 861 &TeecSession, 862 TeecUuid, 863 TEEC_LOGIN_PUBLIC, 864 NULL, 865 NULL, 866 &ErrorOrigin); 867 if (TeecResult != TEEC_SUCCESS) 868 return TeecResult; 869 870 TeecOperation.params[0].value.a = key_id; 871 872 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, 873 TEEC_NONE, 874 TEEC_NONE, 875 TEEC_NONE); 876 877 TeecResult = TEEC_InvokeCommand(&TeecSession, 878 STORAGE_CMD_OEM_OTP_KEY_IS_WRITTEN, 879 &TeecOperation, 880 &ErrorOrigin); 881 if (TeecResult == TEEC_SUCCESS) 882 *value = TeecOperation.params[0].value.b; 883 884 TEEC_CloseSession(&TeecSession); 885 TEEC_FinalizeContext(&TeecContext); 886 887 return TeecResult; 888 } 889 890 uint32_t trusty_set_oem_hr_otp_read_lock(enum RK_OEM_OTP_KEYID key_id) 891 { 892 TEEC_Result TeecResult; 893 TEEC_Context TeecContext; 894 TEEC_Session TeecSession; 895 uint32_t ErrorOrigin; 896 897 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 898 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 899 TEEC_UUID *TeecUuid = &tempuuid; 900 TEEC_Operation TeecOperation = {0}; 901 902 TeecResult = OpteeClientApiLibInitialize(); 903 if (TeecResult != TEEC_SUCCESS) 904 return TeecResult; 905 906 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 907 if (TeecResult != TEEC_SUCCESS) 908 return TeecResult; 909 910 TeecResult = TEEC_OpenSession(&TeecContext, 911 &TeecSession, 912 TeecUuid, 913 TEEC_LOGIN_PUBLIC, 914 NULL, 915 NULL, 916 &ErrorOrigin); 917 if (TeecResult != TEEC_SUCCESS) 918 return TeecResult; 919 920 TeecOperation.params[0].value.a = key_id; 921 922 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 923 TEEC_NONE, 924 TEEC_NONE, 925 TEEC_NONE); 926 927 TeecResult = TEEC_InvokeCommand(&TeecSession, 928 STORAGE_CMD_SET_OEM_HR_OTP_READ_LOCK, 929 &TeecOperation, 930 &ErrorOrigin); 931 if (TeecResult != TEEC_SUCCESS) 932 goto exit; 933 934 exit: 935 TEEC_CloseSession(&TeecSession); 936 TEEC_FinalizeContext(&TeecContext); 937 938 return TeecResult; 939 } 940 941 uint32_t trusty_oem_otp_key_cipher(enum RK_OEM_OTP_KEYID key_id, rk_cipher_config *config, 942 uint32_t src_phys_addr, uint32_t dst_phys_addr, 943 uint32_t len) 944 { 945 TEEC_Result TeecResult; 946 TEEC_Context TeecContext; 947 TEEC_Session TeecSession; 948 TEEC_Operation TeecOperation = {0}; 949 uint32_t ErrorOrigin; 950 TEEC_UUID uuid = RK_CRYPTO_SERVICE_UUID; 951 TEEC_SharedMemory SharedMem_config = {0}; 952 953 if (key_id != RK_OEM_OTP_KEY0 && 954 key_id != RK_OEM_OTP_KEY1 && 955 key_id != RK_OEM_OTP_KEY2 && 956 key_id != RK_OEM_OTP_KEY3 && 957 key_id != RK_OEM_OTP_KEY_FW) 958 return TEEC_ERROR_BAD_PARAMETERS; 959 960 if (!config) 961 return TEEC_ERROR_BAD_PARAMETERS; 962 963 if (config->algo != RK_ALGO_AES && config->algo != RK_ALGO_SM4) 964 return TEEC_ERROR_BAD_PARAMETERS; 965 966 if (config->mode >= RK_CIPHER_MODE_XTS) 967 return TEEC_ERROR_BAD_PARAMETERS; 968 969 if (config->operation != RK_MODE_ENCRYPT && 970 config->operation != RK_MODE_DECRYPT) 971 return TEEC_ERROR_BAD_PARAMETERS; 972 973 if (config->key_len != 16 && 974 config->key_len != 24 && 975 config->key_len != 32) 976 return TEEC_ERROR_BAD_PARAMETERS; 977 978 if (key_id == RK_OEM_OTP_KEY_FW && config->key_len != 16) 979 return TEEC_ERROR_BAD_PARAMETERS; 980 981 #if defined(CONFIG_ROCKCHIP_RV1126) 982 if (config->key_len == 24) 983 return TEEC_ERROR_BAD_PARAMETERS; 984 #endif 985 986 if (len % AES_BLOCK_SIZE || 987 len == 0) 988 return TEEC_ERROR_BAD_PARAMETERS; 989 990 if (!src_phys_addr || !dst_phys_addr) 991 return TEEC_ERROR_BAD_PARAMETERS; 992 993 TeecResult = OpteeClientApiLibInitialize(); 994 if (TeecResult != TEEC_SUCCESS) 995 return TeecResult; 996 997 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 998 if (TeecResult != TEEC_SUCCESS) 999 return TeecResult; 1000 1001 TeecResult = TEEC_OpenSession(&TeecContext, 1002 &TeecSession, 1003 &uuid, 1004 TEEC_LOGIN_PUBLIC, 1005 NULL, 1006 NULL, 1007 &ErrorOrigin); 1008 if (TeecResult != TEEC_SUCCESS) 1009 goto exit; 1010 1011 SharedMem_config.size = sizeof(rk_cipher_config); 1012 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem_config); 1013 if (TeecResult != TEEC_SUCCESS) 1014 goto exit; 1015 1016 memcpy(SharedMem_config.buffer, config, sizeof(rk_cipher_config)); 1017 TeecOperation.params[0].value.a = key_id; 1018 TeecOperation.params[1].tmpref.buffer = SharedMem_config.buffer; 1019 TeecOperation.params[1].tmpref.size = SharedMem_config.size; 1020 TeecOperation.params[2].value.a = src_phys_addr; 1021 TeecOperation.params[2].value.b = len; 1022 TeecOperation.params[3].value.a = dst_phys_addr; 1023 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1024 TEEC_MEMREF_TEMP_INPUT, 1025 TEEC_VALUE_INPUT, 1026 TEEC_VALUE_INPUT); 1027 1028 crypto_flush_cacheline(src_phys_addr, len); 1029 crypto_flush_cacheline(dst_phys_addr, len); 1030 1031 TeecResult = TEEC_InvokeCommand(&TeecSession, 1032 CRYPTO_SERVICE_CMD_OEM_OTP_KEY_PHYS_CIPHER, 1033 &TeecOperation, 1034 &ErrorOrigin); 1035 1036 crypto_invalidate_cacheline(dst_phys_addr, len); 1037 1038 exit: 1039 TEEC_ReleaseSharedMemory(&SharedMem_config); 1040 TEEC_CloseSession(&TeecSession); 1041 TEEC_FinalizeContext(&TeecContext); 1042 return TeecResult; 1043 } 1044 1045 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size) 1046 { 1047 TEEC_Result TeecResult; 1048 TEEC_Context TeecContext; 1049 TEEC_Session TeecSession; 1050 uint32_t ErrorOrigin; 1051 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1052 { 0xa8, 0x69, 0x9c, 0xe6, 1053 0x88, 0x6c, 0x5d, 0x5d 1054 } 1055 }; 1056 TEEC_UUID *TeecUuid = &tempuuid; 1057 TEEC_Operation TeecOperation = {0}; 1058 struct blk_desc *dev_desc; 1059 dev_desc = rockchip_get_bootdev(); 1060 if (!dev_desc) { 1061 printf("%s: dev_desc is NULL!\n", __func__); 1062 return -TEEC_ERROR_GENERIC; 1063 } 1064 1065 TeecResult = OpteeClientApiLibInitialize(); 1066 if (TeecResult != TEEC_SUCCESS) 1067 return TeecResult; 1068 1069 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1070 if (TeecResult != TEEC_SUCCESS) 1071 return TeecResult; 1072 1073 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1074 TEEC_NONE, 1075 TEEC_NONE, 1076 TEEC_NONE); 1077 /*0 nand or emmc "security" partition , 1 rpmb*/ 1078 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1079 TeecOperation.params[0].value.a = 1; 1080 else 1081 TeecOperation.params[0].value.a = 0; 1082 1083 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1084 TeecOperation.params[0].value.a = 0; 1085 #endif 1086 1087 TeecResult = TEEC_OpenSession(&TeecContext, 1088 &TeecSession, 1089 TeecUuid, 1090 TEEC_LOGIN_PUBLIC, 1091 NULL, 1092 &TeecOperation, 1093 &ErrorOrigin); 1094 if (TeecResult != TEEC_SUCCESS) 1095 return TeecResult; 1096 1097 TEEC_SharedMemory SharedMem0 = {0}; 1098 1099 SharedMem0.size = *dh_size; 1100 SharedMem0.flags = 0; 1101 1102 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1103 if (TeecResult != TEEC_SUCCESS) 1104 goto exit; 1105 1106 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1107 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1108 1109 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1110 TEEC_NONE, 1111 TEEC_NONE, 1112 TEEC_NONE); 1113 1114 TeecResult = TEEC_InvokeCommand(&TeecSession, 1115 143, 1116 &TeecOperation, 1117 &ErrorOrigin); 1118 if (TeecResult != TEEC_SUCCESS) 1119 goto exit; 1120 1121 *dh_size = TeecOperation.params[0].tmpref.size; 1122 memcpy(dh, SharedMem0.buffer, SharedMem0.size); 1123 exit: 1124 TEEC_ReleaseSharedMemory(&SharedMem0); 1125 TEEC_CloseSession(&TeecSession); 1126 TEEC_FinalizeContext(&TeecContext); 1127 1128 return TeecResult; 1129 } 1130 1131 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size) 1132 { 1133 TEEC_Result TeecResult; 1134 TEEC_Context TeecContext; 1135 TEEC_Session TeecSession; 1136 uint32_t ErrorOrigin; 1137 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1138 { 0xa8, 0x69, 0x9c, 0xe6, 1139 0x88, 0x6c, 0x5d, 0x5d 1140 } 1141 }; 1142 TEEC_UUID *TeecUuid = &tempuuid; 1143 TEEC_Operation TeecOperation = {0}; 1144 struct blk_desc *dev_desc; 1145 dev_desc = rockchip_get_bootdev(); 1146 if (!dev_desc) { 1147 printf("%s: dev_desc is NULL!\n", __func__); 1148 return -TEEC_ERROR_GENERIC; 1149 } 1150 1151 TeecResult = OpteeClientApiLibInitialize(); 1152 if (TeecResult != TEEC_SUCCESS) 1153 return TeecResult; 1154 1155 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1156 if (TeecResult != TEEC_SUCCESS) 1157 return TeecResult; 1158 1159 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1160 TEEC_NONE, 1161 TEEC_NONE, 1162 TEEC_NONE); 1163 /*0 nand or emmc "security" partition , 1 rpmb*/ 1164 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1165 TeecOperation.params[0].value.a = 1; 1166 else 1167 TeecOperation.params[0].value.a = 0; 1168 1169 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1170 TeecOperation.params[0].value.a = 0; 1171 #endif 1172 1173 TeecResult = TEEC_OpenSession(&TeecContext, 1174 &TeecSession, 1175 TeecUuid, 1176 TEEC_LOGIN_PUBLIC, 1177 NULL, 1178 &TeecOperation, 1179 &ErrorOrigin); 1180 if (TeecResult != TEEC_SUCCESS) 1181 return TeecResult; 1182 1183 TEEC_SharedMemory SharedMem0 = {0}; 1184 1185 SharedMem0.size = *uuid_size; 1186 SharedMem0.flags = 0; 1187 1188 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1189 if (TeecResult != TEEC_SUCCESS) 1190 goto exit; 1191 1192 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1193 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1194 1195 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1196 TEEC_NONE, 1197 TEEC_NONE, 1198 TEEC_NONE); 1199 1200 TeecResult = TEEC_InvokeCommand(&TeecSession, 1201 144, 1202 &TeecOperation, 1203 &ErrorOrigin); 1204 if (TeecResult != TEEC_SUCCESS) 1205 goto exit; 1206 1207 *uuid_size = TeecOperation.params[0].tmpref.size; 1208 memcpy(uuid, SharedMem0.buffer, SharedMem0.size); 1209 exit: 1210 TEEC_ReleaseSharedMemory(&SharedMem0); 1211 TEEC_CloseSession(&TeecSession); 1212 TEEC_FinalizeContext(&TeecContext); 1213 1214 return TeecResult; 1215 } 1216 1217 uint32_t trusty_attest_get_ca(uint8_t *operation_start, 1218 uint32_t *operation_size, 1219 uint8_t *out, 1220 uint32_t *out_len) 1221 { 1222 TEEC_Result TeecResult; 1223 TEEC_Context TeecContext; 1224 TEEC_Session TeecSession; 1225 uint32_t ErrorOrigin; 1226 1227 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1228 { 0xa8, 0x69, 0x9c, 0xe6, 1229 0x88, 0x6c, 0x5d, 0x5d 1230 } 1231 }; 1232 1233 TEEC_UUID *TeecUuid = &tempuuid; 1234 TEEC_Operation TeecOperation = {0}; 1235 struct blk_desc *dev_desc; 1236 dev_desc = rockchip_get_bootdev(); 1237 if (!dev_desc) { 1238 printf("%s: dev_desc is NULL!\n", __func__); 1239 return -TEEC_ERROR_GENERIC; 1240 } 1241 1242 TeecResult = OpteeClientApiLibInitialize(); 1243 if (TeecResult != TEEC_SUCCESS) 1244 return TeecResult; 1245 1246 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1247 if (TeecResult != TEEC_SUCCESS) 1248 return TeecResult; 1249 1250 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1251 TEEC_NONE, 1252 TEEC_NONE, 1253 TEEC_NONE); 1254 /*0 nand or emmc "security" partition , 1 rpmb*/ 1255 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1256 TeecOperation.params[0].value.a = 1; 1257 else 1258 TeecOperation.params[0].value.a = 0; 1259 1260 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1261 TeecOperation.params[0].value.a = 0; 1262 #endif 1263 1264 TeecResult = TEEC_OpenSession(&TeecContext, 1265 &TeecSession, 1266 TeecUuid, 1267 TEEC_LOGIN_PUBLIC, 1268 NULL, 1269 &TeecOperation, 1270 &ErrorOrigin); 1271 if (TeecResult != TEEC_SUCCESS) 1272 return TeecResult; 1273 1274 TEEC_SharedMemory SharedMem0 = {0}; 1275 1276 SharedMem0.size = *operation_size; 1277 SharedMem0.flags = 0; 1278 1279 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1280 if (TeecResult != TEEC_SUCCESS) 1281 goto exit; 1282 1283 memcpy(SharedMem0.buffer, operation_start, SharedMem0.size); 1284 1285 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1286 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1287 1288 TEEC_SharedMemory SharedMem1 = {0}; 1289 1290 SharedMem1.size = *out_len; 1291 SharedMem1.flags = 0; 1292 1293 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 1294 if (TeecResult != TEEC_SUCCESS) 1295 goto exit; 1296 1297 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 1298 TeecOperation.params[1].tmpref.size = SharedMem1.size; 1299 1300 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1301 TEEC_MEMREF_TEMP_INOUT, 1302 TEEC_NONE, 1303 TEEC_NONE); 1304 1305 TeecResult = TEEC_InvokeCommand(&TeecSession, 1306 145, 1307 &TeecOperation, 1308 &ErrorOrigin); 1309 if (TeecResult != TEEC_SUCCESS) 1310 goto exit; 1311 1312 *out_len = TeecOperation.params[1].tmpref.size; 1313 memcpy(out, SharedMem1.buffer, SharedMem1.size); 1314 exit: 1315 TEEC_ReleaseSharedMemory(&SharedMem0); 1316 TEEC_ReleaseSharedMemory(&SharedMem1); 1317 TEEC_CloseSession(&TeecSession); 1318 TEEC_FinalizeContext(&TeecContext); 1319 1320 return TeecResult; 1321 } 1322 1323 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size) 1324 { 1325 TEEC_Result TeecResult; 1326 TEEC_Context TeecContext; 1327 TEEC_Session TeecSession; 1328 uint32_t ErrorOrigin; 1329 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1330 { 0xa8, 0x69, 0x9c, 0xe6, 1331 0x88, 0x6c, 0x5d, 0x5d 1332 } 1333 }; 1334 TEEC_UUID *TeecUuid = &tempuuid; 1335 TEEC_Operation TeecOperation = {0}; 1336 struct blk_desc *dev_desc; 1337 dev_desc = rockchip_get_bootdev(); 1338 if (!dev_desc) { 1339 printf("%s: dev_desc is NULL!\n", __func__); 1340 return -TEEC_ERROR_GENERIC; 1341 } 1342 TeecResult = OpteeClientApiLibInitialize(); 1343 if (TeecResult != TEEC_SUCCESS) 1344 return TeecResult; 1345 1346 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1347 if (TeecResult != TEEC_SUCCESS) 1348 return TeecResult; 1349 1350 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1351 TEEC_NONE, 1352 TEEC_NONE, 1353 TEEC_NONE); 1354 /*0 nand or emmc "security" partition , 1 rpmb*/ 1355 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 1356 TeecOperation.params[0].value.a = 1; 1357 else 1358 TeecOperation.params[0].value.a = 0; 1359 1360 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1361 TeecOperation.params[0].value.a = 0; 1362 #endif 1363 1364 TeecResult = TEEC_OpenSession(&TeecContext, 1365 &TeecSession, 1366 TeecUuid, 1367 TEEC_LOGIN_PUBLIC, 1368 NULL, 1369 &TeecOperation, 1370 &ErrorOrigin); 1371 if (TeecResult != TEEC_SUCCESS) 1372 return TeecResult; 1373 1374 TEEC_SharedMemory SharedMem0 = {0}; 1375 1376 SharedMem0.size = *ca_response_size; 1377 SharedMem0.flags = 0; 1378 1379 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1380 if (TeecResult != TEEC_SUCCESS) 1381 goto exit; 1382 1383 memcpy(SharedMem0.buffer, ca_response, SharedMem0.size); 1384 1385 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1386 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1387 1388 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1389 TEEC_NONE, 1390 TEEC_NONE, 1391 TEEC_NONE); 1392 1393 TeecResult = TEEC_InvokeCommand(&TeecSession, 1394 146, 1395 &TeecOperation, 1396 &ErrorOrigin); 1397 if (TeecResult != TEEC_SUCCESS) 1398 goto exit; 1399 exit: 1400 TEEC_ReleaseSharedMemory(&SharedMem0); 1401 TEEC_CloseSession(&TeecSession); 1402 TEEC_FinalizeContext(&TeecContext); 1403 1404 return TeecResult; 1405 } 1406