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