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/OpteeClientApiLib.h> 10 #include <optee_include/tee_client_api.h> 11 #include <optee_include/tee_api_defines.h> 12 #include <boot_rkimg.h> 13 #include <stdlib.h> 14 #include <attestation_key.h> 15 16 #define BOOT_FROM_EMMC (1 << 1) 17 #define STORAGE_CMD_READ_ATTRIBUTE_HASH 0 18 #define STORAGE_CMD_WRITE_ATTRIBUTE_HASH 1 19 #define STORAGE_CMD_UBOOT_END_OTP 2 20 #define STORAGE_CMD_READ_VBOOTKEY_HASH 3 21 #define STORAGE_CMD_WRITE_VBOOTKEY_HASH 4 22 #define STORAGE_CMD_READ_ENABLE_FLAG 5 23 #define STORAGE_CMD_WRITE_TA_ENCRYPTION_KEY 9 24 #define STORAGE_CMD_CHECK_SECURITY_LEVEL_FLAG 10 25 #define STORAGE_CMD_WRITE_OEM_HUK 11 26 27 static uint8_t b2hs_add_base(uint8_t in) 28 { 29 if (in > 9) 30 return in + 55; 31 else 32 return in + 48; 33 } 34 35 static uint32_t b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen) 36 { 37 uint32_t i = 0; 38 39 if (blen * 2 + 1 > hslen) 40 return 0; 41 42 for (; i < blen; i++) { 43 hs[i * 2 + 1] = b2hs_add_base(b[i] & 0xf); 44 hs[i * 2] = b2hs_add_base(b[i] >> 4); 45 } 46 hs[blen * 2] = 0; 47 48 return blen * 2; 49 } 50 51 static uint32_t trusty_base_write_security_data(char *filename, 52 uint32_t filename_size, 53 uint8_t *data, 54 uint32_t data_size) 55 { 56 TEEC_Result TeecResult; 57 TEEC_Context TeecContext; 58 TEEC_Session TeecSession; 59 uint32_t ErrorOrigin; 60 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 61 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 62 TEEC_UUID *TeecUuid = &tempuuid; 63 TEEC_Operation TeecOperation = {0}; 64 struct blk_desc *dev_desc; 65 dev_desc = rockchip_get_bootdev(); 66 if (!dev_desc) { 67 printf("%s: dev_desc is NULL!\n", __func__); 68 return -TEEC_ERROR_GENERIC; 69 } 70 71 TeecResult = OpteeClientApiLibInitialize(); 72 if (TeecResult != TEEC_SUCCESS) 73 return TeecResult; 74 75 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 76 if (TeecResult != TEEC_SUCCESS) 77 return TeecResult; 78 79 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 80 TEEC_NONE, 81 TEEC_NONE, 82 TEEC_NONE); 83 /*0 nand or emmc "security" partition , 1 rpmb*/ 84 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 85 TeecOperation.params[0].value.a = 1; 86 else 87 TeecOperation.params[0].value.a = 0; 88 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 89 TeecOperation.params[0].value.a = 0; 90 #endif 91 92 TeecResult = TEEC_OpenSession(&TeecContext, 93 &TeecSession, 94 TeecUuid, 95 TEEC_LOGIN_PUBLIC, 96 NULL, 97 &TeecOperation, 98 &ErrorOrigin); 99 if (TeecResult != TEEC_SUCCESS) 100 return TeecResult; 101 102 TEEC_SharedMemory SharedMem0 = {0}; 103 104 SharedMem0.size = filename_size; 105 SharedMem0.flags = 0; 106 107 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 108 if (TeecResult != TEEC_SUCCESS) 109 goto exit; 110 111 memcpy(SharedMem0.buffer, filename, SharedMem0.size); 112 113 TEEC_SharedMemory SharedMem1 = {0}; 114 115 SharedMem1.size = data_size; 116 SharedMem1.flags = 0; 117 118 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 119 if (TeecResult != TEEC_SUCCESS) 120 goto exit; 121 122 memcpy(SharedMem1.buffer, data, SharedMem1.size); 123 124 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 125 TeecOperation.params[0].tmpref.size = SharedMem0.size; 126 127 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 128 TeecOperation.params[1].tmpref.size = SharedMem1.size; 129 130 131 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 132 TEEC_MEMREF_TEMP_INOUT, 133 TEEC_NONE, 134 TEEC_NONE); 135 136 TeecResult = TEEC_InvokeCommand(&TeecSession, 137 1, 138 &TeecOperation, 139 &ErrorOrigin); 140 if (TeecResult != TEEC_SUCCESS) 141 goto exit; 142 exit: 143 TEEC_ReleaseSharedMemory(&SharedMem0); 144 TEEC_ReleaseSharedMemory(&SharedMem1); 145 TEEC_CloseSession(&TeecSession); 146 TEEC_FinalizeContext(&TeecContext); 147 148 return TeecResult; 149 } 150 151 static uint32_t trusty_base_read_security_data(char *filename, 152 uint32_t filename_size, 153 uint8_t *data, 154 uint32_t data_size) 155 { 156 TEEC_Result TeecResult; 157 TEEC_Context TeecContext; 158 TEEC_Session TeecSession; 159 uint32_t ErrorOrigin; 160 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 161 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 162 TEEC_UUID *TeecUuid = &tempuuid; 163 TEEC_Operation TeecOperation = {0}; 164 165 struct blk_desc *dev_desc; 166 dev_desc = rockchip_get_bootdev(); 167 if (!dev_desc) { 168 printf("%s: dev_desc is NULL!\n", __func__); 169 return -TEEC_ERROR_GENERIC; 170 } 171 172 TeecResult = OpteeClientApiLibInitialize(); 173 if (TeecResult != TEEC_SUCCESS) 174 return TeecResult; 175 176 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 177 if (TeecResult != TEEC_SUCCESS) 178 return TeecResult; 179 180 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 181 TEEC_NONE, 182 TEEC_NONE, 183 TEEC_NONE); 184 /*0 nand or emmc "security" partition , 1 rpmb*/ 185 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 186 TeecOperation.params[0].value.a = 1; 187 else 188 TeecOperation.params[0].value.a = 0; 189 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 190 TeecOperation.params[0].value.a = 0; 191 #endif 192 193 TeecResult = TEEC_OpenSession(&TeecContext, 194 &TeecSession, 195 TeecUuid, 196 TEEC_LOGIN_PUBLIC, 197 NULL, 198 &TeecOperation, 199 &ErrorOrigin); 200 if (TeecResult != TEEC_SUCCESS) 201 return TeecResult; 202 203 TEEC_SharedMemory SharedMem0 = {0}; 204 205 SharedMem0.size = filename_size; 206 SharedMem0.flags = 0; 207 208 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 209 if (TeecResult != TEEC_SUCCESS) 210 goto exit; 211 212 memcpy(SharedMem0.buffer, filename, SharedMem0.size); 213 214 TEEC_SharedMemory SharedMem1 = {0}; 215 216 SharedMem1.size = data_size; 217 SharedMem1.flags = 0; 218 219 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 220 if (TeecResult != TEEC_SUCCESS) 221 goto exit; 222 223 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 224 TeecOperation.params[0].tmpref.size = SharedMem0.size; 225 226 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 227 TeecOperation.params[1].tmpref.size = SharedMem1.size; 228 229 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 230 TEEC_MEMREF_TEMP_INOUT, 231 TEEC_NONE, 232 TEEC_NONE); 233 234 TeecResult = TEEC_InvokeCommand(&TeecSession, 235 0, 236 &TeecOperation, 237 &ErrorOrigin); 238 if (TeecResult == TEEC_SUCCESS) 239 memcpy(data, SharedMem1.buffer, SharedMem1.size); 240 exit: 241 TEEC_ReleaseSharedMemory(&SharedMem0); 242 TEEC_ReleaseSharedMemory(&SharedMem1); 243 TEEC_CloseSession(&TeecSession); 244 TEEC_FinalizeContext(&TeecContext); 245 246 return TeecResult; 247 } 248 249 static uint32_t trusty_base_end_security_data(void) 250 { 251 TEEC_Result TeecResult; 252 TEEC_Context TeecContext; 253 TEEC_Session TeecSession; 254 uint32_t ErrorOrigin; 255 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 256 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 257 TEEC_UUID *TeecUuid = &tempuuid; 258 TEEC_Operation TeecOperation = {0}; 259 260 TeecResult = OpteeClientApiLibInitialize(); 261 if (TeecResult != TEEC_SUCCESS) 262 return TeecResult; 263 264 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 265 if (TeecResult != TEEC_SUCCESS) 266 return TeecResult; 267 268 TeecResult = TEEC_OpenSession(&TeecContext, 269 &TeecSession, 270 TeecUuid, 271 TEEC_LOGIN_PUBLIC, 272 NULL, 273 NULL, 274 &ErrorOrigin); 275 if (TeecResult != TEEC_SUCCESS) 276 return TeecResult; 277 278 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 279 TEEC_NONE, 280 TEEC_NONE, 281 TEEC_NONE); 282 283 TeecResult = TEEC_InvokeCommand(&TeecSession, 284 2, 285 &TeecOperation, 286 &ErrorOrigin); 287 if (TeecResult != TEEC_SUCCESS) 288 goto exit; 289 exit: 290 TEEC_CloseSession(&TeecSession); 291 TEEC_FinalizeContext(&TeecContext); 292 293 return TeecResult; 294 } 295 296 uint32_t trusty_read_rollback_index(uint32_t slot, uint64_t *value) 297 { 298 char hs[9]; 299 300 b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9); 301 302 return trusty_base_read_security_data(hs, 8, (uint8_t *)value, 8); 303 } 304 305 uint32_t trusty_write_rollback_index(uint32_t slot, uint64_t value) 306 { 307 char hs[9]; 308 309 b2hs((uint8_t *)&slot, (uint8_t *)hs, 4, 9); 310 311 return trusty_base_write_security_data(hs, 8, (uint8_t *)&value, 8); 312 } 313 314 uint32_t trusty_read_permanent_attributes(uint8_t *attributes, uint32_t size) 315 { 316 return trusty_base_read_security_data("attributes", 317 sizeof("attributes"), attributes, size); 318 } 319 320 uint32_t trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size) 321 { 322 return trusty_base_write_security_data("attributes", 323 sizeof("attributes"), attributes, size); 324 } 325 326 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes) 327 { 328 return trusty_base_read_security_data("attributes_flag", 329 sizeof("attributes_flag"), attributes, 1); 330 } 331 332 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes) 333 { 334 return trusty_base_write_security_data("attributes_flag", 335 sizeof("attributes_flag"), &attributes, 1); 336 } 337 338 uint32_t trusty_read_permanent_attributes_cer(uint8_t *attributes, 339 uint32_t size) 340 { 341 return trusty_base_read_security_data("rsacer", 342 sizeof("rsacer"), attributes, size); 343 } 344 345 uint32_t trusty_write_permanent_attributes_cer(uint8_t *attributes, 346 uint32_t size) 347 { 348 return trusty_base_write_security_data("rsacer", 349 sizeof("rsacer"), attributes, size); 350 } 351 352 uint32_t trusty_read_lock_state(uint8_t *lock_state) 353 { 354 return trusty_base_read_security_data("lock_state", 355 sizeof("lock_state"), lock_state, 1); 356 } 357 358 uint32_t trusty_write_lock_state(uint8_t lock_state) 359 { 360 return trusty_base_write_security_data("lock_state", 361 sizeof("lock_state"), &lock_state, 1); 362 } 363 364 uint32_t trusty_read_flash_lock_state(uint8_t *flash_lock_state) 365 { 366 return trusty_base_read_security_data("flash_lock_state", 367 sizeof("flash_lock_state"), flash_lock_state, 1); 368 } 369 370 uint32_t trusty_write_flash_lock_state(uint8_t flash_lock_state) 371 { 372 return trusty_base_write_security_data("flash_lock_state", 373 sizeof("flash_lock_state"), &flash_lock_state, 1); 374 } 375 376 static uint32_t trusty_base_end_efuse_or_otp(void) 377 { 378 TEEC_Result TeecResult; 379 TEEC_Context TeecContext; 380 TEEC_Session TeecSession; 381 uint32_t ErrorOrigin; 382 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 383 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 384 385 TEEC_UUID *TeecUuid = &tempuuid; 386 TEEC_Operation TeecOperation = {0}; 387 388 TeecResult = OpteeClientApiLibInitialize(); 389 if (TeecResult != TEEC_SUCCESS) 390 return TeecResult; 391 392 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 393 if (TeecResult != TEEC_SUCCESS) 394 return TeecResult; 395 396 TeecResult = TEEC_OpenSession(&TeecContext, 397 &TeecSession, 398 TeecUuid, 399 TEEC_LOGIN_PUBLIC, 400 NULL, 401 NULL, 402 &ErrorOrigin); 403 if (TeecResult != TEEC_SUCCESS) 404 return TeecResult; 405 406 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 407 TEEC_NONE, 408 TEEC_NONE, 409 TEEC_NONE); 410 411 TeecResult = TEEC_InvokeCommand(&TeecSession, 412 STORAGE_CMD_UBOOT_END_OTP, 413 &TeecOperation, 414 &ErrorOrigin); 415 if (TeecResult != TEEC_SUCCESS) 416 goto exit; 417 exit: 418 TEEC_CloseSession(&TeecSession); 419 TEEC_FinalizeContext(&TeecContext); 420 421 return TeecResult; 422 } 423 424 static uint32_t trusty_base_efuse_or_otp_operation(uint32_t cmd, 425 uint8_t is_write, 426 uint32_t *buf, 427 uint32_t length) 428 { 429 TEEC_Result TeecResult; 430 TEEC_Context TeecContext; 431 TEEC_Session TeecSession; 432 uint32_t ErrorOrigin; 433 434 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, 435 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 436 TEEC_UUID *TeecUuid = &tempuuid; 437 TEEC_Operation TeecOperation = {0}; 438 439 TeecResult = OpteeClientApiLibInitialize(); 440 if (TeecResult != TEEC_SUCCESS) 441 return TeecResult; 442 443 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 444 if (TeecResult != TEEC_SUCCESS) 445 return TeecResult; 446 447 TeecResult = TEEC_OpenSession(&TeecContext, 448 &TeecSession, 449 TeecUuid, 450 TEEC_LOGIN_PUBLIC, 451 NULL, 452 NULL, 453 &ErrorOrigin); 454 if (TeecResult != TEEC_SUCCESS) 455 return TeecResult; 456 457 TEEC_SharedMemory SharedMem0 = {0}; 458 459 SharedMem0.size = length * sizeof(uint32_t); 460 SharedMem0.flags = 0; 461 462 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 463 if (TeecResult != TEEC_SUCCESS) 464 goto exit; 465 466 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 467 TeecOperation.params[0].tmpref.size = SharedMem0.size; 468 469 if (is_write) { 470 memcpy(SharedMem0.buffer, buf, SharedMem0.size); 471 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 472 TEEC_NONE, 473 TEEC_NONE, 474 TEEC_NONE); 475 476 } else { 477 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, 478 TEEC_NONE, 479 TEEC_NONE, 480 TEEC_NONE); 481 } 482 483 TeecResult = TEEC_InvokeCommand(&TeecSession, 484 cmd, 485 &TeecOperation, 486 &ErrorOrigin); 487 if (TeecResult != TEEC_SUCCESS) 488 goto exit; 489 490 if (!is_write) 491 memcpy(buf, SharedMem0.buffer, SharedMem0.size); 492 493 exit: 494 TEEC_ReleaseSharedMemory(&SharedMem0); 495 TEEC_CloseSession(&TeecSession); 496 TEEC_FinalizeContext(&TeecContext); 497 498 return TeecResult; 499 } 500 501 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length) 502 { 503 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ATTRIBUTE_HASH, 504 false, buf, length); 505 } 506 507 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length) 508 { 509 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_ATTRIBUTE_HASH, 510 true, buf, length); 511 } 512 513 uint32_t trusty_notify_optee_uboot_end(void) 514 { 515 TEEC_Result res; 516 517 res = trusty_base_end_security_data(); 518 res |= trusty_base_end_efuse_or_otp(); 519 return res; 520 } 521 522 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length) 523 { 524 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_VBOOTKEY_HASH, 525 false, buf, length); 526 } 527 528 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length) 529 { 530 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_VBOOTKEY_HASH, 531 true, buf, length); 532 } 533 534 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag) 535 { 536 uint32_t bootflag; 537 TEEC_Result TeecResult; 538 539 TeecResult = trusty_base_efuse_or_otp_operation(STORAGE_CMD_READ_ENABLE_FLAG, 540 false, &bootflag, 1); 541 542 if (TeecResult == TEEC_SUCCESS) { 543 #if defined(CONFIG_ROCKCHIP_RK3288) 544 if (bootflag == 0x00000001) 545 *flag = 1; 546 #else 547 if (bootflag == 0x000000FF) 548 *flag = 1; 549 #endif 550 } 551 return TeecResult; 552 } 553 554 uint32_t trusty_write_ta_encryption_key(uint32_t *buf, uint32_t length) 555 { 556 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_TA_ENCRYPTION_KEY, 557 true, buf, length); 558 } 559 560 uint32_t trusty_check_security_level_flag(uint8_t flag) 561 { 562 uint32_t levelflag; 563 564 levelflag = flag; 565 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_CHECK_SECURITY_LEVEL_FLAG, 566 true, &levelflag, 1); 567 } 568 569 uint32_t trusty_write_oem_huk(uint32_t *buf, uint32_t length) 570 { 571 return trusty_base_efuse_or_otp_operation(STORAGE_CMD_WRITE_OEM_HUK, 572 true, buf, length); 573 } 574 575 void trusty_select_security_level(void) 576 { 577 #if (CONFIG_OPTEE_SECURITY_LEVEL > 0) 578 TEEC_Result TeecResult; 579 580 TeecResult = trusty_check_security_level_flag(CONFIG_OPTEE_SECURITY_LEVEL); 581 if (TeecResult == TEE_ERROR_CANCEL) { 582 run_command("download", 0); 583 return; 584 } 585 586 if (TeecResult == TEEC_SUCCESS) 587 debug("optee select security level success!"); 588 else 589 panic("optee select security level fail!"); 590 591 return; 592 #endif 593 } 594 595 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size) 596 { 597 TEEC_Result TeecResult; 598 TEEC_Context TeecContext; 599 TEEC_Session TeecSession; 600 uint32_t ErrorOrigin; 601 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 602 { 0xa8, 0x69, 0x9c, 0xe6, 603 0x88, 0x6c, 0x5d, 0x5d 604 } 605 }; 606 TEEC_UUID *TeecUuid = &tempuuid; 607 TEEC_Operation TeecOperation = {0}; 608 struct blk_desc *dev_desc; 609 dev_desc = rockchip_get_bootdev(); 610 if (!dev_desc) { 611 printf("%s: dev_desc is NULL!\n", __func__); 612 return -TEEC_ERROR_GENERIC; 613 } 614 615 TeecResult = OpteeClientApiLibInitialize(); 616 if (TeecResult != TEEC_SUCCESS) 617 return TeecResult; 618 619 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 620 if (TeecResult != TEEC_SUCCESS) 621 return TeecResult; 622 623 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 624 TEEC_NONE, 625 TEEC_NONE, 626 TEEC_NONE); 627 /*0 nand or emmc "security" partition , 1 rpmb*/ 628 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 629 TeecOperation.params[0].value.a = 1; 630 else 631 TeecOperation.params[0].value.a = 0; 632 633 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 634 TeecOperation.params[0].value.a = 0; 635 #endif 636 637 TeecResult = TEEC_OpenSession(&TeecContext, 638 &TeecSession, 639 TeecUuid, 640 TEEC_LOGIN_PUBLIC, 641 NULL, 642 &TeecOperation, 643 &ErrorOrigin); 644 if (TeecResult != TEEC_SUCCESS) 645 return TeecResult; 646 647 TEEC_SharedMemory SharedMem0 = {0}; 648 649 SharedMem0.size = *dh_size; 650 SharedMem0.flags = 0; 651 652 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 653 if (TeecResult != TEEC_SUCCESS) 654 goto exit; 655 656 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 657 TeecOperation.params[0].tmpref.size = SharedMem0.size; 658 659 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 660 TEEC_NONE, 661 TEEC_NONE, 662 TEEC_NONE); 663 664 TeecResult = TEEC_InvokeCommand(&TeecSession, 665 143, 666 &TeecOperation, 667 &ErrorOrigin); 668 if (TeecResult != TEEC_SUCCESS) 669 goto exit; 670 671 *dh_size = TeecOperation.params[0].tmpref.size; 672 memcpy(dh, SharedMem0.buffer, SharedMem0.size); 673 exit: 674 TEEC_ReleaseSharedMemory(&SharedMem0); 675 TEEC_CloseSession(&TeecSession); 676 TEEC_FinalizeContext(&TeecContext); 677 678 return TeecResult; 679 } 680 681 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size) 682 { 683 TEEC_Result TeecResult; 684 TEEC_Context TeecContext; 685 TEEC_Session TeecSession; 686 uint32_t ErrorOrigin; 687 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 688 { 0xa8, 0x69, 0x9c, 0xe6, 689 0x88, 0x6c, 0x5d, 0x5d 690 } 691 }; 692 TEEC_UUID *TeecUuid = &tempuuid; 693 TEEC_Operation TeecOperation = {0}; 694 struct blk_desc *dev_desc; 695 dev_desc = rockchip_get_bootdev(); 696 if (!dev_desc) { 697 printf("%s: dev_desc is NULL!\n", __func__); 698 return -TEEC_ERROR_GENERIC; 699 } 700 701 TeecResult = OpteeClientApiLibInitialize(); 702 if (TeecResult != TEEC_SUCCESS) 703 return TeecResult; 704 705 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 706 if (TeecResult != TEEC_SUCCESS) 707 return TeecResult; 708 709 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 710 TEEC_NONE, 711 TEEC_NONE, 712 TEEC_NONE); 713 /*0 nand or emmc "security" partition , 1 rpmb*/ 714 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 715 TeecOperation.params[0].value.a = 1; 716 else 717 TeecOperation.params[0].value.a = 0; 718 719 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 720 TeecOperation.params[0].value.a = 0; 721 #endif 722 723 TeecResult = TEEC_OpenSession(&TeecContext, 724 &TeecSession, 725 TeecUuid, 726 TEEC_LOGIN_PUBLIC, 727 NULL, 728 &TeecOperation, 729 &ErrorOrigin); 730 if (TeecResult != TEEC_SUCCESS) 731 return TeecResult; 732 733 TEEC_SharedMemory SharedMem0 = {0}; 734 735 SharedMem0.size = *uuid_size; 736 SharedMem0.flags = 0; 737 738 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 739 if (TeecResult != TEEC_SUCCESS) 740 goto exit; 741 742 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 743 TeecOperation.params[0].tmpref.size = SharedMem0.size; 744 745 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 746 TEEC_NONE, 747 TEEC_NONE, 748 TEEC_NONE); 749 750 TeecResult = TEEC_InvokeCommand(&TeecSession, 751 144, 752 &TeecOperation, 753 &ErrorOrigin); 754 if (TeecResult != TEEC_SUCCESS) 755 goto exit; 756 757 *uuid_size = TeecOperation.params[0].tmpref.size; 758 memcpy(uuid, SharedMem0.buffer, SharedMem0.size); 759 exit: 760 TEEC_ReleaseSharedMemory(&SharedMem0); 761 TEEC_CloseSession(&TeecSession); 762 TEEC_FinalizeContext(&TeecContext); 763 764 return TeecResult; 765 } 766 767 uint32_t trusty_attest_get_ca(uint8_t *operation_start, 768 uint32_t *operation_size, 769 uint8_t *out, 770 uint32_t *out_len) 771 { 772 TEEC_Result TeecResult; 773 TEEC_Context TeecContext; 774 TEEC_Session TeecSession; 775 uint32_t ErrorOrigin; 776 777 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 778 { 0xa8, 0x69, 0x9c, 0xe6, 779 0x88, 0x6c, 0x5d, 0x5d 780 } 781 }; 782 783 TEEC_UUID *TeecUuid = &tempuuid; 784 TEEC_Operation TeecOperation = {0}; 785 struct blk_desc *dev_desc; 786 dev_desc = rockchip_get_bootdev(); 787 if (!dev_desc) { 788 printf("%s: dev_desc is NULL!\n", __func__); 789 return -TEEC_ERROR_GENERIC; 790 } 791 792 TeecResult = OpteeClientApiLibInitialize(); 793 if (TeecResult != TEEC_SUCCESS) 794 return TeecResult; 795 796 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 797 if (TeecResult != TEEC_SUCCESS) 798 return TeecResult; 799 800 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 801 TEEC_NONE, 802 TEEC_NONE, 803 TEEC_NONE); 804 /*0 nand or emmc "security" partition , 1 rpmb*/ 805 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 806 TeecOperation.params[0].value.a = 1; 807 else 808 TeecOperation.params[0].value.a = 0; 809 810 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 811 TeecOperation.params[0].value.a = 0; 812 #endif 813 814 TeecResult = TEEC_OpenSession(&TeecContext, 815 &TeecSession, 816 TeecUuid, 817 TEEC_LOGIN_PUBLIC, 818 NULL, 819 &TeecOperation, 820 &ErrorOrigin); 821 if (TeecResult != TEEC_SUCCESS) 822 return TeecResult; 823 824 TEEC_SharedMemory SharedMem0 = {0}; 825 826 SharedMem0.size = *operation_size; 827 SharedMem0.flags = 0; 828 829 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 830 if (TeecResult != TEEC_SUCCESS) 831 goto exit; 832 833 memcpy(SharedMem0.buffer, operation_start, SharedMem0.size); 834 835 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 836 TeecOperation.params[0].tmpref.size = SharedMem0.size; 837 838 TEEC_SharedMemory SharedMem1 = {0}; 839 840 SharedMem1.size = *out_len; 841 SharedMem1.flags = 0; 842 843 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 844 if (TeecResult != TEEC_SUCCESS) 845 goto exit; 846 847 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 848 TeecOperation.params[1].tmpref.size = SharedMem1.size; 849 850 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 851 TEEC_MEMREF_TEMP_INOUT, 852 TEEC_NONE, 853 TEEC_NONE); 854 855 TeecResult = TEEC_InvokeCommand(&TeecSession, 856 145, 857 &TeecOperation, 858 &ErrorOrigin); 859 if (TeecResult != TEEC_SUCCESS) 860 goto exit; 861 862 *out_len = TeecOperation.params[1].tmpref.size; 863 memcpy(out, SharedMem1.buffer, SharedMem1.size); 864 exit: 865 TEEC_ReleaseSharedMemory(&SharedMem0); 866 TEEC_ReleaseSharedMemory(&SharedMem1); 867 TEEC_CloseSession(&TeecSession); 868 TEEC_FinalizeContext(&TeecContext); 869 870 return TeecResult; 871 } 872 873 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size) 874 { 875 TEEC_Result TeecResult; 876 TEEC_Context TeecContext; 877 TEEC_Session TeecSession; 878 uint32_t ErrorOrigin; 879 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 880 { 0xa8, 0x69, 0x9c, 0xe6, 881 0x88, 0x6c, 0x5d, 0x5d 882 } 883 }; 884 TEEC_UUID *TeecUuid = &tempuuid; 885 TEEC_Operation TeecOperation = {0}; 886 struct blk_desc *dev_desc; 887 dev_desc = rockchip_get_bootdev(); 888 if (!dev_desc) { 889 printf("%s: dev_desc is NULL!\n", __func__); 890 return -TEEC_ERROR_GENERIC; 891 } 892 TeecResult = OpteeClientApiLibInitialize(); 893 if (TeecResult != TEEC_SUCCESS) 894 return TeecResult; 895 896 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 897 if (TeecResult != TEEC_SUCCESS) 898 return TeecResult; 899 900 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 901 TEEC_NONE, 902 TEEC_NONE, 903 TEEC_NONE); 904 /*0 nand or emmc "security" partition , 1 rpmb*/ 905 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 906 TeecOperation.params[0].value.a = 1; 907 else 908 TeecOperation.params[0].value.a = 0; 909 910 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 911 TeecOperation.params[0].value.a = 0; 912 #endif 913 914 TeecResult = TEEC_OpenSession(&TeecContext, 915 &TeecSession, 916 TeecUuid, 917 TEEC_LOGIN_PUBLIC, 918 NULL, 919 &TeecOperation, 920 &ErrorOrigin); 921 if (TeecResult != TEEC_SUCCESS) 922 return TeecResult; 923 924 TEEC_SharedMemory SharedMem0 = {0}; 925 926 SharedMem0.size = *ca_response_size; 927 SharedMem0.flags = 0; 928 929 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 930 if (TeecResult != TEEC_SUCCESS) 931 goto exit; 932 933 memcpy(SharedMem0.buffer, ca_response, SharedMem0.size); 934 935 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 936 TeecOperation.params[0].tmpref.size = SharedMem0.size; 937 938 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 939 TEEC_NONE, 940 TEEC_NONE, 941 TEEC_NONE); 942 943 TeecResult = TEEC_InvokeCommand(&TeecSession, 944 146, 945 &TeecOperation, 946 &ErrorOrigin); 947 if (TeecResult != TEEC_SUCCESS) 948 goto exit; 949 exit: 950 TEEC_ReleaseSharedMemory(&SharedMem0); 951 TEEC_CloseSession(&TeecSession); 952 TEEC_FinalizeContext(&TeecContext); 953 954 return TeecResult; 955 } 956