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