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 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size) 576 { 577 TEEC_Result TeecResult; 578 TEEC_Context TeecContext; 579 TEEC_Session TeecSession; 580 uint32_t ErrorOrigin; 581 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 582 { 0xa8, 0x69, 0x9c, 0xe6, 583 0x88, 0x6c, 0x5d, 0x5d 584 } 585 }; 586 TEEC_UUID *TeecUuid = &tempuuid; 587 TEEC_Operation TeecOperation = {0}; 588 struct blk_desc *dev_desc; 589 dev_desc = rockchip_get_bootdev(); 590 if (!dev_desc) { 591 printf("%s: dev_desc is NULL!\n", __func__); 592 return -TEEC_ERROR_GENERIC; 593 } 594 595 TeecResult = OpteeClientApiLibInitialize(); 596 if (TeecResult != TEEC_SUCCESS) 597 return TeecResult; 598 599 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 600 if (TeecResult != TEEC_SUCCESS) 601 return TeecResult; 602 603 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 604 TEEC_NONE, 605 TEEC_NONE, 606 TEEC_NONE); 607 /*0 nand or emmc "security" partition , 1 rpmb*/ 608 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 609 TeecOperation.params[0].value.a = 1; 610 else 611 TeecOperation.params[0].value.a = 0; 612 613 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 614 TeecOperation.params[0].value.a = 0; 615 #endif 616 617 TeecResult = TEEC_OpenSession(&TeecContext, 618 &TeecSession, 619 TeecUuid, 620 TEEC_LOGIN_PUBLIC, 621 NULL, 622 &TeecOperation, 623 &ErrorOrigin); 624 if (TeecResult != TEEC_SUCCESS) 625 return TeecResult; 626 627 TEEC_SharedMemory SharedMem0 = {0}; 628 629 SharedMem0.size = *dh_size; 630 SharedMem0.flags = 0; 631 632 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 633 if (TeecResult != TEEC_SUCCESS) 634 goto exit; 635 636 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 637 TeecOperation.params[0].tmpref.size = SharedMem0.size; 638 639 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 640 TEEC_NONE, 641 TEEC_NONE, 642 TEEC_NONE); 643 644 TeecResult = TEEC_InvokeCommand(&TeecSession, 645 143, 646 &TeecOperation, 647 &ErrorOrigin); 648 if (TeecResult != TEEC_SUCCESS) 649 goto exit; 650 651 *dh_size = TeecOperation.params[0].tmpref.size; 652 memcpy(dh, SharedMem0.buffer, SharedMem0.size); 653 exit: 654 TEEC_ReleaseSharedMemory(&SharedMem0); 655 TEEC_CloseSession(&TeecSession); 656 TEEC_FinalizeContext(&TeecContext); 657 658 return TeecResult; 659 } 660 661 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size) 662 { 663 TEEC_Result TeecResult; 664 TEEC_Context TeecContext; 665 TEEC_Session TeecSession; 666 uint32_t ErrorOrigin; 667 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 668 { 0xa8, 0x69, 0x9c, 0xe6, 669 0x88, 0x6c, 0x5d, 0x5d 670 } 671 }; 672 TEEC_UUID *TeecUuid = &tempuuid; 673 TEEC_Operation TeecOperation = {0}; 674 struct blk_desc *dev_desc; 675 dev_desc = rockchip_get_bootdev(); 676 if (!dev_desc) { 677 printf("%s: dev_desc is NULL!\n", __func__); 678 return -TEEC_ERROR_GENERIC; 679 } 680 681 TeecResult = OpteeClientApiLibInitialize(); 682 if (TeecResult != TEEC_SUCCESS) 683 return TeecResult; 684 685 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 686 if (TeecResult != TEEC_SUCCESS) 687 return TeecResult; 688 689 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 690 TEEC_NONE, 691 TEEC_NONE, 692 TEEC_NONE); 693 /*0 nand or emmc "security" partition , 1 rpmb*/ 694 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 695 TeecOperation.params[0].value.a = 1; 696 else 697 TeecOperation.params[0].value.a = 0; 698 699 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 700 TeecOperation.params[0].value.a = 0; 701 #endif 702 703 TeecResult = TEEC_OpenSession(&TeecContext, 704 &TeecSession, 705 TeecUuid, 706 TEEC_LOGIN_PUBLIC, 707 NULL, 708 &TeecOperation, 709 &ErrorOrigin); 710 if (TeecResult != TEEC_SUCCESS) 711 return TeecResult; 712 713 TEEC_SharedMemory SharedMem0 = {0}; 714 715 SharedMem0.size = *uuid_size; 716 SharedMem0.flags = 0; 717 718 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 719 if (TeecResult != TEEC_SUCCESS) 720 goto exit; 721 722 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 723 TeecOperation.params[0].tmpref.size = SharedMem0.size; 724 725 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 726 TEEC_NONE, 727 TEEC_NONE, 728 TEEC_NONE); 729 730 TeecResult = TEEC_InvokeCommand(&TeecSession, 731 144, 732 &TeecOperation, 733 &ErrorOrigin); 734 if (TeecResult != TEEC_SUCCESS) 735 goto exit; 736 737 *uuid_size = TeecOperation.params[0].tmpref.size; 738 memcpy(uuid, SharedMem0.buffer, SharedMem0.size); 739 exit: 740 TEEC_ReleaseSharedMemory(&SharedMem0); 741 TEEC_CloseSession(&TeecSession); 742 TEEC_FinalizeContext(&TeecContext); 743 744 return TeecResult; 745 } 746 747 uint32_t trusty_attest_get_ca(uint8_t *operation_start, 748 uint32_t *operation_size, 749 uint8_t *out, 750 uint32_t *out_len) 751 { 752 TEEC_Result TeecResult; 753 TEEC_Context TeecContext; 754 TEEC_Session TeecSession; 755 uint32_t ErrorOrigin; 756 757 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 758 { 0xa8, 0x69, 0x9c, 0xe6, 759 0x88, 0x6c, 0x5d, 0x5d 760 } 761 }; 762 763 TEEC_UUID *TeecUuid = &tempuuid; 764 TEEC_Operation TeecOperation = {0}; 765 struct blk_desc *dev_desc; 766 dev_desc = rockchip_get_bootdev(); 767 if (!dev_desc) { 768 printf("%s: dev_desc is NULL!\n", __func__); 769 return -TEEC_ERROR_GENERIC; 770 } 771 772 TeecResult = OpteeClientApiLibInitialize(); 773 if (TeecResult != TEEC_SUCCESS) 774 return TeecResult; 775 776 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 777 if (TeecResult != TEEC_SUCCESS) 778 return TeecResult; 779 780 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 781 TEEC_NONE, 782 TEEC_NONE, 783 TEEC_NONE); 784 /*0 nand or emmc "security" partition , 1 rpmb*/ 785 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 786 TeecOperation.params[0].value.a = 1; 787 else 788 TeecOperation.params[0].value.a = 0; 789 790 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 791 TeecOperation.params[0].value.a = 0; 792 #endif 793 794 TeecResult = TEEC_OpenSession(&TeecContext, 795 &TeecSession, 796 TeecUuid, 797 TEEC_LOGIN_PUBLIC, 798 NULL, 799 &TeecOperation, 800 &ErrorOrigin); 801 if (TeecResult != TEEC_SUCCESS) 802 return TeecResult; 803 804 TEEC_SharedMemory SharedMem0 = {0}; 805 806 SharedMem0.size = *operation_size; 807 SharedMem0.flags = 0; 808 809 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 810 if (TeecResult != TEEC_SUCCESS) 811 goto exit; 812 813 memcpy(SharedMem0.buffer, operation_start, SharedMem0.size); 814 815 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 816 TeecOperation.params[0].tmpref.size = SharedMem0.size; 817 818 TEEC_SharedMemory SharedMem1 = {0}; 819 820 SharedMem1.size = *out_len; 821 SharedMem1.flags = 0; 822 823 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 824 if (TeecResult != TEEC_SUCCESS) 825 goto exit; 826 827 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 828 TeecOperation.params[1].tmpref.size = SharedMem1.size; 829 830 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 831 TEEC_MEMREF_TEMP_INOUT, 832 TEEC_NONE, 833 TEEC_NONE); 834 835 TeecResult = TEEC_InvokeCommand(&TeecSession, 836 145, 837 &TeecOperation, 838 &ErrorOrigin); 839 if (TeecResult != TEEC_SUCCESS) 840 goto exit; 841 842 *out_len = TeecOperation.params[1].tmpref.size; 843 memcpy(out, SharedMem1.buffer, SharedMem1.size); 844 exit: 845 TEEC_ReleaseSharedMemory(&SharedMem0); 846 TEEC_ReleaseSharedMemory(&SharedMem1); 847 TEEC_CloseSession(&TeecSession); 848 TEEC_FinalizeContext(&TeecContext); 849 850 return TeecResult; 851 } 852 853 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size) 854 { 855 TEEC_Result TeecResult; 856 TEEC_Context TeecContext; 857 TEEC_Session TeecSession; 858 uint32_t ErrorOrigin; 859 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 860 { 0xa8, 0x69, 0x9c, 0xe6, 861 0x88, 0x6c, 0x5d, 0x5d 862 } 863 }; 864 TEEC_UUID *TeecUuid = &tempuuid; 865 TEEC_Operation TeecOperation = {0}; 866 struct blk_desc *dev_desc; 867 dev_desc = rockchip_get_bootdev(); 868 if (!dev_desc) { 869 printf("%s: dev_desc is NULL!\n", __func__); 870 return -TEEC_ERROR_GENERIC; 871 } 872 TeecResult = OpteeClientApiLibInitialize(); 873 if (TeecResult != TEEC_SUCCESS) 874 return TeecResult; 875 876 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 877 if (TeecResult != TEEC_SUCCESS) 878 return TeecResult; 879 880 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 881 TEEC_NONE, 882 TEEC_NONE, 883 TEEC_NONE); 884 /*0 nand or emmc "security" partition , 1 rpmb*/ 885 if (dev_desc->if_type == IF_TYPE_MMC && dev_desc->devnum == 0) 886 TeecOperation.params[0].value.a = 1; 887 else 888 TeecOperation.params[0].value.a = 0; 889 890 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 891 TeecOperation.params[0].value.a = 0; 892 #endif 893 894 TeecResult = TEEC_OpenSession(&TeecContext, 895 &TeecSession, 896 TeecUuid, 897 TEEC_LOGIN_PUBLIC, 898 NULL, 899 &TeecOperation, 900 &ErrorOrigin); 901 if (TeecResult != TEEC_SUCCESS) 902 return TeecResult; 903 904 TEEC_SharedMemory SharedMem0 = {0}; 905 906 SharedMem0.size = *ca_response_size; 907 SharedMem0.flags = 0; 908 909 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 910 if (TeecResult != TEEC_SUCCESS) 911 goto exit; 912 913 memcpy(SharedMem0.buffer, ca_response, SharedMem0.size); 914 915 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 916 TeecOperation.params[0].tmpref.size = SharedMem0.size; 917 918 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 919 TEEC_NONE, 920 TEEC_NONE, 921 TEEC_NONE); 922 923 TeecResult = TEEC_InvokeCommand(&TeecSession, 924 146, 925 &TeecOperation, 926 &ErrorOrigin); 927 if (TeecResult != TEEC_SUCCESS) 928 goto exit; 929 exit: 930 TEEC_ReleaseSharedMemory(&SharedMem0); 931 TEEC_CloseSession(&TeecSession); 932 TEEC_FinalizeContext(&TeecContext); 933 934 return TeecResult; 935 } 936