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