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 <boot_rkimg.h> 12 13 void test_optee(void) 14 { 15 TEEC_Result TeecResult; 16 TEEC_Context TeecContext; 17 TEEC_Session TeecSession; 18 uint32_t ErrorOrigin; 19 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 20 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 21 TEEC_UUID *TeecUuid = &tempuuid; 22 TEEC_Operation TeecOperation = {0}; 23 struct blk_desc *dev_desc; 24 dev_desc = rockchip_get_bootdev(); 25 26 debug("testmm start\n"); 27 OpteeClientApiLibInitialize(); 28 29 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 30 31 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 32 TEEC_NONE, 33 TEEC_NONE, 34 TEEC_NONE); 35 /*0 nand or emmc "security" partition , 1 rpmb*/ 36 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 37 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 38 TeecOperation.params[0].value.a = 0; 39 #endif 40 41 TeecResult = TEEC_OpenSession(&TeecContext, 42 &TeecSession, 43 TeecUuid, 44 TEEC_LOGIN_PUBLIC, 45 NULL, 46 #ifdef CONFIG_OPTEE_V1 47 NULL, 48 #endif 49 #ifdef CONFIG_OPTEE_V2 50 &TeecOperation, 51 #endif 52 &ErrorOrigin); 53 54 TEEC_SharedMemory SharedMem0 = {0}; 55 56 SharedMem0.size = sizeof("filename_test"); 57 SharedMem0.flags = 0; 58 59 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 60 61 memcpy(SharedMem0.buffer, "filename_test", SharedMem0.size); 62 63 TEEC_SharedMemory SharedMem1 = {0}; 64 65 SharedMem1.size = 32; 66 SharedMem1.flags = 0; 67 68 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 69 70 memset(SharedMem1.buffer, 'a', SharedMem1.size); 71 72 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 73 TeecOperation.params[0].tmpref.size = SharedMem0.size; 74 75 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 76 TeecOperation.params[1].tmpref.size = SharedMem1.size; 77 78 79 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 80 TEEC_MEMREF_TEMP_INOUT, 81 TEEC_NONE, 82 TEEC_NONE); 83 84 TeecResult = TEEC_InvokeCommand(&TeecSession, 85 1, 86 &TeecOperation, 87 &ErrorOrigin); 88 89 TEEC_ReleaseSharedMemory(&SharedMem0); 90 TEEC_ReleaseSharedMemory(&SharedMem1); 91 92 TEEC_CloseSession(&TeecSession); 93 94 TEEC_FinalizeContext(&TeecContext); 95 96 debug("testmm end\n"); 97 debug("TeecResult %x\n", TeecResult); 98 } 99 100 static uint8_t b2hs_add_base(uint8_t in) 101 { 102 if (in > 9) 103 return in + 55; 104 else 105 return in + 48; 106 } 107 108 uint32_t b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen) 109 { 110 uint32_t i = 0; 111 112 if (blen * 2 + 1 > hslen) 113 return 0; 114 115 for (; i < blen; i++) { 116 hs[i * 2 + 1] = b2hs_add_base(b[i] & 0xf); 117 hs[i * 2] = b2hs_add_base(b[i] >> 4); 118 } 119 hs[blen * 2] = 0; 120 121 return blen * 2; 122 } 123 124 125 uint32_t trusty_read_rollback_index(uint32_t slot, uint64_t *value) 126 { 127 TEEC_Result TeecResult; 128 TEEC_Context TeecContext; 129 TEEC_Session TeecSession; 130 uint32_t ErrorOrigin; 131 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 132 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 133 TEEC_UUID *TeecUuid = &tempuuid; 134 TEEC_Operation TeecOperation = {0}; 135 uint8_t hs[9]; 136 137 struct blk_desc *dev_desc; 138 dev_desc = rockchip_get_bootdev(); 139 140 b2hs((uint8_t *)&slot, hs, 4, 9); 141 debug("testmm start\n"); 142 OpteeClientApiLibInitialize(); 143 144 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 145 146 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 147 TEEC_NONE, 148 TEEC_NONE, 149 TEEC_NONE); 150 /*0 nand or emmc "security" partition , 1 rpmb*/ 151 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 152 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 153 TeecOperation.params[0].value.a = 0; 154 #endif 155 156 TeecResult = TEEC_OpenSession(&TeecContext, 157 &TeecSession, 158 TeecUuid, 159 TEEC_LOGIN_PUBLIC, 160 NULL, 161 #ifdef CONFIG_OPTEE_V1 162 NULL, 163 #endif 164 #ifdef CONFIG_OPTEE_V2 165 &TeecOperation, 166 #endif 167 &ErrorOrigin); 168 169 TEEC_SharedMemory SharedMem0 = {0}; 170 171 SharedMem0.size = 8; 172 SharedMem0.flags = 0; 173 174 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 175 176 memcpy(SharedMem0.buffer, hs, SharedMem0.size); 177 178 TEEC_SharedMemory SharedMem1 = {0}; 179 180 SharedMem1.size = 8; 181 SharedMem1.flags = 0; 182 183 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 184 185 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 186 TeecOperation.params[0].tmpref.size = SharedMem0.size; 187 188 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 189 TeecOperation.params[1].tmpref.size = SharedMem1.size; 190 191 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 192 TEEC_MEMREF_TEMP_INOUT, 193 TEEC_NONE, 194 TEEC_NONE); 195 196 TeecResult = TEEC_InvokeCommand(&TeecSession, 197 0, 198 &TeecOperation, 199 &ErrorOrigin); 200 if (TeecResult == TEEC_SUCCESS) 201 memcpy((char *)value, SharedMem1.buffer, SharedMem1.size); 202 203 TEEC_ReleaseSharedMemory(&SharedMem0); 204 TEEC_ReleaseSharedMemory(&SharedMem1); 205 206 TEEC_CloseSession(&TeecSession); 207 208 TEEC_FinalizeContext(&TeecContext); 209 210 debug("testmm end\n"); 211 return TeecResult; 212 } 213 214 uint32_t trusty_write_rollback_index(uint32_t slot, uint64_t value) 215 { 216 TEEC_Result TeecResult; 217 TEEC_Context TeecContext; 218 TEEC_Session TeecSession; 219 uint32_t ErrorOrigin; 220 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 221 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 222 TEEC_UUID *TeecUuid = &tempuuid; 223 TEEC_Operation TeecOperation = {0}; 224 uint8_t hs[9]; 225 struct blk_desc *dev_desc; 226 dev_desc = rockchip_get_bootdev(); 227 228 b2hs((uint8_t *)&slot, hs, 4, 9); 229 OpteeClientApiLibInitialize(); 230 231 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 232 233 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 234 TEEC_NONE, 235 TEEC_NONE, 236 TEEC_NONE); 237 /*0 nand or emmc "security" partition , 1 rpmb*/ 238 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 239 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 240 TeecOperation.params[0].value.a = 0; 241 #endif 242 243 TeecResult = TEEC_OpenSession(&TeecContext, 244 &TeecSession, 245 TeecUuid, 246 TEEC_LOGIN_PUBLIC, 247 NULL, 248 #ifdef CONFIG_OPTEE_V1 249 NULL, 250 #endif 251 #ifdef CONFIG_OPTEE_V2 252 &TeecOperation, 253 #endif 254 &ErrorOrigin); 255 256 TEEC_SharedMemory SharedMem0 = {0}; 257 258 SharedMem0.size = 8; 259 SharedMem0.flags = 0; 260 261 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 262 263 memcpy(SharedMem0.buffer, hs, SharedMem0.size); 264 265 TEEC_SharedMemory SharedMem1 = {0}; 266 267 SharedMem1.size = 8; 268 SharedMem1.flags = 0; 269 270 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 271 272 memcpy(SharedMem1.buffer, (char *)&value, SharedMem1.size); 273 274 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 275 TeecOperation.params[0].tmpref.size = SharedMem0.size; 276 277 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 278 TeecOperation.params[1].tmpref.size = SharedMem1.size; 279 280 281 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 282 TEEC_MEMREF_TEMP_INOUT, 283 TEEC_NONE, 284 TEEC_NONE); 285 286 TeecResult = TEEC_InvokeCommand(&TeecSession, 287 1, 288 &TeecOperation, 289 &ErrorOrigin); 290 291 TEEC_ReleaseSharedMemory(&SharedMem0); 292 TEEC_ReleaseSharedMemory(&SharedMem1); 293 294 TEEC_CloseSession(&TeecSession); 295 296 TEEC_FinalizeContext(&TeecContext); 297 298 debug("testmm end\n"); 299 300 return TeecResult; 301 } 302 303 uint32_t trusty_read_permanent_attributes(uint8_t *attributes, uint32_t size) 304 { 305 TEEC_Result TeecResult; 306 TEEC_Context TeecContext; 307 TEEC_Session TeecSession; 308 uint32_t ErrorOrigin; 309 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 310 { 0xa8, 0x69, 0x9c, 0xe6, 0x88, 0x6c, 0x5d, 0x5d } }; 311 TEEC_UUID *TeecUuid = &tempuuid; 312 TEEC_Operation TeecOperation = {0}; 313 struct blk_desc *dev_desc; 314 dev_desc = rockchip_get_bootdev(); 315 316 debug("testmm start\n"); 317 OpteeClientApiLibInitialize(); 318 319 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 320 321 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 322 TEEC_NONE, 323 TEEC_NONE, 324 TEEC_NONE); 325 /*0 nand or emmc "security" partition , 1 rpmb*/ 326 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 327 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 328 TeecOperation.params[0].value.a = 0; 329 #endif 330 331 TeecResult = TEEC_OpenSession(&TeecContext, 332 &TeecSession, 333 TeecUuid, 334 TEEC_LOGIN_PUBLIC, 335 NULL, 336 #ifdef CONFIG_OPTEE_V1 337 NULL, 338 #endif 339 #ifdef CONFIG_OPTEE_V2 340 &TeecOperation, 341 #endif 342 &ErrorOrigin); 343 344 TEEC_SharedMemory SharedMem0 = {0}; 345 346 SharedMem0.size = sizeof("attributes"); 347 SharedMem0.flags = 0; 348 349 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 350 351 memcpy(SharedMem0.buffer, "attributes", SharedMem0.size); 352 353 TEEC_SharedMemory SharedMem1 = {0}; 354 355 SharedMem1.size = size; 356 SharedMem1.flags = 0; 357 358 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 359 360 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 361 TeecOperation.params[0].tmpref.size = SharedMem0.size; 362 363 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 364 TeecOperation.params[1].tmpref.size = SharedMem1.size; 365 366 367 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 368 TEEC_MEMREF_TEMP_INOUT, 369 TEEC_NONE, 370 TEEC_NONE); 371 372 TeecResult = TEEC_InvokeCommand(&TeecSession, 373 142, 374 &TeecOperation, 375 &ErrorOrigin); 376 if (TeecResult == TEEC_SUCCESS) 377 memcpy(attributes, SharedMem1.buffer, SharedMem1.size); 378 TEEC_ReleaseSharedMemory(&SharedMem0); 379 TEEC_ReleaseSharedMemory(&SharedMem1); 380 TEEC_CloseSession(&TeecSession); 381 TEEC_FinalizeContext(&TeecContext); 382 debug("testmm end\n"); 383 384 return TeecResult; 385 } 386 387 uint32_t trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size) 388 { 389 TEEC_Result TeecResult; 390 TEEC_Context TeecContext; 391 TEEC_Session TeecSession; 392 uint32_t ErrorOrigin; 393 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 394 { 0xa8, 0x69, 0x9c, 0xe6, 0x88, 0x6c, 0x5d, 0x5d } }; 395 TEEC_UUID *TeecUuid = &tempuuid; 396 TEEC_Operation TeecOperation = {0}; 397 struct blk_desc *dev_desc; 398 dev_desc = rockchip_get_bootdev(); 399 400 debug("testmm start\n"); 401 OpteeClientApiLibInitialize(); 402 403 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 404 405 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 406 TEEC_NONE, 407 TEEC_NONE, 408 TEEC_NONE); 409 /*0 nand or emmc "security" partition , 1 rpmb*/ 410 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 411 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 412 TeecOperation.params[0].value.a = 0; 413 #endif 414 415 TeecResult = TEEC_OpenSession(&TeecContext, 416 &TeecSession, 417 TeecUuid, 418 TEEC_LOGIN_PUBLIC, 419 NULL, 420 #ifdef CONFIG_OPTEE_V1 421 NULL, 422 #endif 423 #ifdef CONFIG_OPTEE_V2 424 &TeecOperation, 425 #endif 426 &ErrorOrigin); 427 428 TEEC_SharedMemory SharedMem0 = {0}; 429 430 SharedMem0.size = sizeof("attributes"); 431 SharedMem0.flags = 0; 432 433 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 434 435 memcpy(SharedMem0.buffer, "attributes", SharedMem0.size); 436 437 TEEC_SharedMemory SharedMem1 = {0}; 438 439 SharedMem1.size = size; 440 SharedMem1.flags = 0; 441 442 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 443 444 memcpy(SharedMem1.buffer, attributes, SharedMem1.size); 445 446 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 447 TeecOperation.params[0].tmpref.size = SharedMem0.size; 448 449 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 450 TeecOperation.params[1].tmpref.size = SharedMem1.size; 451 452 453 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 454 TEEC_MEMREF_TEMP_INOUT, 455 TEEC_NONE, 456 TEEC_NONE); 457 458 TeecResult = TEEC_InvokeCommand(&TeecSession, 459 141, 460 &TeecOperation, 461 &ErrorOrigin); 462 463 TEEC_ReleaseSharedMemory(&SharedMem0); 464 TEEC_ReleaseSharedMemory(&SharedMem1); 465 TEEC_CloseSession(&TeecSession); 466 TEEC_FinalizeContext(&TeecContext); 467 debug("testmm end\n"); 468 469 return TeecResult; 470 } 471 472 uint32_t trusty_read_lock_state(uint8_t *lock_state) 473 { 474 TEEC_Result TeecResult; 475 TEEC_Context TeecContext; 476 TEEC_Session TeecSession; 477 uint32_t ErrorOrigin; 478 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 479 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 480 TEEC_UUID *TeecUuid = &tempuuid; 481 TEEC_Operation TeecOperation = {0}; 482 struct blk_desc *dev_desc; 483 dev_desc = rockchip_get_bootdev(); 484 485 debug("testmm start\n"); 486 OpteeClientApiLibInitialize(); 487 488 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 489 490 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 491 TEEC_NONE, 492 TEEC_NONE, 493 TEEC_NONE); 494 /*0 nand or emmc "security" partition , 1 rpmb*/ 495 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 496 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 497 TeecOperation.params[0].value.a = 0; 498 #endif 499 500 TeecResult = TEEC_OpenSession(&TeecContext, 501 &TeecSession, 502 TeecUuid, 503 TEEC_LOGIN_PUBLIC, 504 NULL, 505 #ifdef CONFIG_OPTEE_V1 506 NULL, 507 #endif 508 #ifdef CONFIG_OPTEE_V2 509 &TeecOperation, 510 #endif 511 &ErrorOrigin); 512 513 TEEC_SharedMemory SharedMem0 = {0}; 514 515 SharedMem0.size = sizeof("lock_state"); 516 SharedMem0.flags = 0; 517 518 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 519 520 memcpy(SharedMem0.buffer, "lock_state", SharedMem0.size); 521 522 TEEC_SharedMemory SharedMem1 = {0}; 523 524 SharedMem1.size = 1; 525 SharedMem1.flags = 0; 526 527 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 528 529 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 530 TeecOperation.params[0].tmpref.size = SharedMem0.size; 531 532 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 533 TeecOperation.params[1].tmpref.size = SharedMem1.size; 534 535 536 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 537 TEEC_MEMREF_TEMP_INOUT, 538 TEEC_NONE, 539 TEEC_NONE); 540 541 TeecResult = TEEC_InvokeCommand(&TeecSession, 542 0, 543 &TeecOperation, 544 &ErrorOrigin); 545 if (TeecResult == TEEC_SUCCESS) 546 memcpy(lock_state, SharedMem1.buffer, SharedMem1.size); 547 TEEC_ReleaseSharedMemory(&SharedMem0); 548 TEEC_ReleaseSharedMemory(&SharedMem1); 549 TEEC_CloseSession(&TeecSession); 550 TEEC_FinalizeContext(&TeecContext); 551 debug("testmm end\n"); 552 553 return TeecResult; 554 } 555 556 uint32_t trusty_write_lock_state(uint8_t lock_state) 557 { 558 TEEC_Result TeecResult; 559 TEEC_Context TeecContext; 560 TEEC_Session TeecSession; 561 uint32_t ErrorOrigin; 562 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 563 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 564 TEEC_UUID *TeecUuid = &tempuuid; 565 TEEC_Operation TeecOperation = {0}; 566 struct blk_desc *dev_desc; 567 dev_desc = rockchip_get_bootdev(); 568 569 debug("testmm start\n"); 570 OpteeClientApiLibInitialize(); 571 572 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 573 574 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 575 TEEC_NONE, 576 TEEC_NONE, 577 TEEC_NONE); 578 /*0 nand or emmc "security" partition , 1 rpmb*/ 579 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 580 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 581 TeecOperation.params[0].value.a = 0; 582 #endif 583 584 TeecResult = TEEC_OpenSession(&TeecContext, 585 &TeecSession, 586 TeecUuid, 587 TEEC_LOGIN_PUBLIC, 588 NULL, 589 #ifdef CONFIG_OPTEE_V1 590 NULL, 591 #endif 592 #ifdef CONFIG_OPTEE_V2 593 &TeecOperation, 594 #endif 595 &ErrorOrigin); 596 597 TEEC_SharedMemory SharedMem0 = {0}; 598 599 SharedMem0.size = sizeof("lock_state"); 600 SharedMem0.flags = 0; 601 602 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 603 604 memcpy(SharedMem0.buffer, "lock_state", SharedMem0.size); 605 606 TEEC_SharedMemory SharedMem1 = {0}; 607 608 SharedMem1.size = 1; 609 SharedMem1.flags = 0; 610 611 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 612 613 memcpy(SharedMem1.buffer, &lock_state, SharedMem1.size); 614 615 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 616 TeecOperation.params[0].tmpref.size = SharedMem0.size; 617 618 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 619 TeecOperation.params[1].tmpref.size = SharedMem1.size; 620 621 622 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 623 TEEC_MEMREF_TEMP_INOUT, 624 TEEC_NONE, 625 TEEC_NONE); 626 627 TeecResult = TEEC_InvokeCommand(&TeecSession, 628 1, 629 &TeecOperation, 630 &ErrorOrigin); 631 632 TEEC_ReleaseSharedMemory(&SharedMem0); 633 TEEC_ReleaseSharedMemory(&SharedMem1); 634 TEEC_CloseSession(&TeecSession); 635 TEEC_FinalizeContext(&TeecContext); 636 debug("testmm end\n"); 637 638 return TeecResult; 639 } 640 641 uint32_t trusty_read_flash_lock_state(uint8_t *flash_lock_state) 642 { 643 TEEC_Result TeecResult; 644 TEEC_Context TeecContext; 645 TEEC_Session TeecSession; 646 uint32_t ErrorOrigin; 647 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 648 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 649 TEEC_UUID *TeecUuid = &tempuuid; 650 TEEC_Operation TeecOperation = {0}; 651 struct blk_desc *dev_desc; 652 dev_desc = rockchip_get_bootdev(); 653 654 debug("testmm start\n"); 655 OpteeClientApiLibInitialize(); 656 657 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 658 659 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 660 TEEC_NONE, 661 TEEC_NONE, 662 TEEC_NONE); 663 /*0 nand or emmc "security" partition , 1 rpmb*/ 664 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 665 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 666 TeecOperation.params[0].value.a = 0; 667 #endif 668 669 TeecResult = TEEC_OpenSession(&TeecContext, 670 &TeecSession, 671 TeecUuid, 672 TEEC_LOGIN_PUBLIC, 673 NULL, 674 #ifdef CONFIG_OPTEE_V1 675 NULL, 676 #endif 677 #ifdef CONFIG_OPTEE_V2 678 &TeecOperation, 679 #endif 680 &ErrorOrigin); 681 682 TEEC_SharedMemory SharedMem0 = {0}; 683 684 SharedMem0.size = sizeof("flash_lock_state"); 685 SharedMem0.flags = 0; 686 687 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 688 689 memcpy(SharedMem0.buffer, "flash_lock_state", SharedMem0.size); 690 691 TEEC_SharedMemory SharedMem1 = {0}; 692 693 SharedMem1.size = 1; 694 SharedMem1.flags = 0; 695 696 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 697 698 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 699 TeecOperation.params[0].tmpref.size = SharedMem0.size; 700 701 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 702 TeecOperation.params[1].tmpref.size = SharedMem1.size; 703 704 705 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 706 TEEC_MEMREF_TEMP_INOUT, 707 TEEC_NONE, 708 TEEC_NONE); 709 710 TeecResult = TEEC_InvokeCommand(&TeecSession, 711 0, 712 &TeecOperation, 713 &ErrorOrigin); 714 if (TeecResult == TEEC_SUCCESS) 715 memcpy(flash_lock_state, SharedMem1.buffer, SharedMem1.size); 716 TEEC_ReleaseSharedMemory(&SharedMem0); 717 TEEC_ReleaseSharedMemory(&SharedMem1); 718 TEEC_CloseSession(&TeecSession); 719 TEEC_FinalizeContext(&TeecContext); 720 debug("testmm end\n"); 721 722 return TeecResult; 723 } 724 725 726 uint32_t trusty_write_flash_lock_state(uint8_t flash_lock_state) 727 { 728 TEEC_Result TeecResult; 729 TEEC_Context TeecContext; 730 TEEC_Session TeecSession; 731 uint32_t ErrorOrigin; 732 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 733 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 734 TEEC_UUID *TeecUuid = &tempuuid; 735 TEEC_Operation TeecOperation = {0}; 736 struct blk_desc *dev_desc; 737 dev_desc = rockchip_get_bootdev(); 738 739 debug("testmm start\n"); 740 OpteeClientApiLibInitialize(); 741 742 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 743 744 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 745 TEEC_NONE, 746 TEEC_NONE, 747 TEEC_NONE); 748 /*0 nand or emmc "security" partition , 1 rpmb*/ 749 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 750 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 751 TeecOperation.params[0].value.a = 0; 752 #endif 753 754 TeecResult = TEEC_OpenSession(&TeecContext, 755 &TeecSession, 756 TeecUuid, 757 TEEC_LOGIN_PUBLIC, 758 NULL, 759 #ifdef CONFIG_OPTEE_V1 760 NULL, 761 #endif 762 #ifdef CONFIG_OPTEE_V2 763 &TeecOperation, 764 #endif 765 &ErrorOrigin); 766 767 TEEC_SharedMemory SharedMem0 = {0}; 768 769 SharedMem0.size = sizeof("flash_lock_state"); 770 SharedMem0.flags = 0; 771 772 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 773 774 memcpy(SharedMem0.buffer, "flash_lock_state", SharedMem0.size); 775 776 TEEC_SharedMemory SharedMem1 = {0}; 777 778 SharedMem1.size = 1; 779 SharedMem1.flags = 0; 780 781 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 782 783 memcpy(SharedMem1.buffer, &flash_lock_state, SharedMem1.size); 784 785 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 786 TeecOperation.params[0].tmpref.size = SharedMem0.size; 787 788 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 789 TeecOperation.params[1].tmpref.size = SharedMem1.size; 790 791 792 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 793 TEEC_MEMREF_TEMP_INOUT, 794 TEEC_NONE, 795 TEEC_NONE); 796 797 TeecResult = TEEC_InvokeCommand(&TeecSession, 798 1, 799 &TeecOperation, 800 &ErrorOrigin); 801 802 TEEC_ReleaseSharedMemory(&SharedMem0); 803 TEEC_ReleaseSharedMemory(&SharedMem1); 804 TEEC_CloseSession(&TeecSession); 805 TEEC_FinalizeContext(&TeecContext); 806 debug("testmm end\n"); 807 808 return TeecResult; 809 } 810 811 uint32_t write_to_keymaster(uint8_t *filename, 812 uint32_t filename_size, 813 uint8_t *data, 814 uint32_t data_size) 815 { 816 TEEC_Result TeecResult; 817 TEEC_Context TeecContext; 818 TEEC_Session TeecSession; 819 uint32_t ErrorOrigin; 820 821 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 822 { 0xa8, 0x69, 0x9c, 0xe6, 0x88, 0x6c, 0x5d, 0x5d } }; 823 TEEC_UUID *TeecUuid = &tempuuid; 824 TEEC_Operation TeecOperation = {0}; 825 struct blk_desc *dev_desc; 826 dev_desc = rockchip_get_bootdev(); 827 828 debug("write_to_keymaster\n"); 829 OpteeClientApiLibInitialize(); 830 831 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 832 833 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 834 TEEC_NONE, 835 TEEC_NONE, 836 TEEC_NONE); 837 /*0 nand or emmc "security" partition , 1 rpmb*/ 838 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 839 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 840 TeecOperation.params[0].value.a = 0; 841 #endif 842 843 TeecResult = TEEC_OpenSession(&TeecContext, 844 &TeecSession, 845 TeecUuid, 846 TEEC_LOGIN_PUBLIC, 847 NULL, 848 #ifdef CONFIG_OPTEE_V1 849 NULL, 850 #endif 851 #ifdef CONFIG_OPTEE_V2 852 &TeecOperation, 853 #endif 854 &ErrorOrigin); 855 856 TEEC_SharedMemory SharedMem0 = {0}; 857 858 SharedMem0.size = filename_size; 859 SharedMem0.flags = 0; 860 861 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 862 863 memcpy(SharedMem0.buffer, filename, SharedMem0.size); 864 865 TEEC_SharedMemory SharedMem1 = {0}; 866 867 SharedMem1.size = data_size; 868 SharedMem1.flags = 0; 869 870 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 871 872 memcpy(SharedMem1.buffer, data, SharedMem1.size); 873 874 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 875 TeecOperation.params[0].tmpref.size = SharedMem0.size; 876 877 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 878 TeecOperation.params[1].tmpref.size = SharedMem1.size; 879 880 881 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 882 TEEC_MEMREF_TEMP_INOUT, 883 TEEC_NONE, 884 TEEC_NONE); 885 886 TeecResult = TEEC_InvokeCommand(&TeecSession, 887 141, 888 &TeecOperation, 889 &ErrorOrigin); 890 891 TEEC_ReleaseSharedMemory(&SharedMem0); 892 TEEC_ReleaseSharedMemory(&SharedMem1); 893 TEEC_CloseSession(&TeecSession); 894 TEEC_FinalizeContext(&TeecContext); 895 debug("testmm end\n"); 896 debug("TeecResult %x\n", TeecResult); 897 898 return TeecResult; 899 } 900 901 uint32_t trusty_read_attribute_hash(uint32_t *buf, uint32_t length) 902 { 903 TEEC_Result TeecResult; 904 TEEC_Context TeecContext; 905 TEEC_Session TeecSession; 906 uint32_t ErrorOrigin; 907 908 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 909 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 910 TEEC_UUID *TeecUuid = &tempuuid; 911 TEEC_Operation TeecOperation = {0}; 912 913 OpteeClientApiLibInitialize(); 914 915 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 916 917 TeecResult = TEEC_OpenSession(&TeecContext, 918 &TeecSession, 919 TeecUuid, 920 TEEC_LOGIN_PUBLIC, 921 NULL, 922 NULL, 923 &ErrorOrigin); 924 925 TEEC_SharedMemory SharedMem0 = {0}; 926 927 SharedMem0.size = length * sizeof(uint32_t); 928 SharedMem0.flags = 0; 929 930 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 931 932 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 933 TeecOperation.params[0].tmpref.size = SharedMem0.size; 934 935 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, 936 TEEC_NONE, 937 TEEC_NONE, 938 TEEC_NONE); 939 940 TeecResult = TEEC_InvokeCommand(&TeecSession, 941 0, 942 &TeecOperation, 943 &ErrorOrigin); 944 945 if (TeecResult == TEEC_SUCCESS) 946 memcpy(buf, SharedMem0.buffer, SharedMem0.size); 947 948 TEEC_ReleaseSharedMemory(&SharedMem0); 949 TEEC_CloseSession(&TeecSession); 950 TEEC_FinalizeContext(&TeecContext); 951 952 return TeecResult; 953 } 954 955 uint32_t trusty_write_attribute_hash(uint32_t *buf, uint32_t length) 956 { 957 TEEC_Result TeecResult; 958 TEEC_Context TeecContext; 959 TEEC_Session TeecSession; 960 uint32_t ErrorOrigin; 961 962 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 963 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 964 TEEC_UUID *TeecUuid = &tempuuid; 965 TEEC_Operation TeecOperation = {0}; 966 967 OpteeClientApiLibInitialize(); 968 969 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 970 971 TeecResult = TEEC_OpenSession(&TeecContext, 972 &TeecSession, 973 TeecUuid, 974 TEEC_LOGIN_PUBLIC, 975 NULL, 976 NULL, 977 &ErrorOrigin); 978 979 TEEC_SharedMemory SharedMem0 = {0}; 980 981 SharedMem0.size = length * sizeof(uint32_t); 982 SharedMem0.flags = 0; 983 984 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 985 986 memcpy(SharedMem0.buffer, buf, SharedMem0.size); 987 988 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 989 TeecOperation.params[0].tmpref.size = SharedMem0.size; 990 991 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 992 TEEC_NONE, 993 TEEC_NONE, 994 TEEC_NONE); 995 996 TeecResult = TEEC_InvokeCommand(&TeecSession, 997 1, 998 &TeecOperation, 999 &ErrorOrigin); 1000 1001 TEEC_ReleaseSharedMemory(&SharedMem0); 1002 TEEC_CloseSession(&TeecSession); 1003 TEEC_FinalizeContext(&TeecContext); 1004 1005 return TeecResult; 1006 } 1007 1008 uint32_t notify_optee_rpmb_ta(void) 1009 { 1010 TEEC_Result TeecResult; 1011 TEEC_Context TeecContext; 1012 TEEC_Session TeecSession; 1013 uint32_t ErrorOrigin; 1014 TEEC_UUID tempuuid = { 0x1b484ea5, 0x698b, 0x4142, 1015 { 0x82, 0xb8, 0x3a, 0xcf, 0x16, 0xe9, 0x9e, 0x2a } }; 1016 TEEC_UUID *TeecUuid = &tempuuid; 1017 TEEC_Operation TeecOperation = {0}; 1018 1019 OpteeClientApiLibInitialize(); 1020 1021 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1022 1023 TeecResult = TEEC_OpenSession(&TeecContext, 1024 &TeecSession, 1025 TeecUuid, 1026 TEEC_LOGIN_PUBLIC, 1027 NULL, 1028 NULL, 1029 &ErrorOrigin); 1030 1031 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 1032 TEEC_NONE, 1033 TEEC_NONE, 1034 TEEC_NONE); 1035 1036 TeecResult = TEEC_InvokeCommand(&TeecSession, 1037 2, 1038 &TeecOperation, 1039 &ErrorOrigin); 1040 1041 TEEC_CloseSession(&TeecSession); 1042 TEEC_FinalizeContext(&TeecContext); 1043 1044 return TeecResult; 1045 } 1046 1047 uint32_t notify_optee_efuse_ta(void) 1048 { 1049 TEEC_Result TeecResult; 1050 TEEC_Context TeecContext; 1051 TEEC_Session TeecSession; 1052 uint32_t ErrorOrigin; 1053 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 1054 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 1055 1056 TEEC_UUID *TeecUuid = &tempuuid; 1057 TEEC_Operation TeecOperation = {0}; 1058 1059 OpteeClientApiLibInitialize(); 1060 1061 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1062 1063 TeecResult = TEEC_OpenSession(&TeecContext, 1064 &TeecSession, 1065 TeecUuid, 1066 TEEC_LOGIN_PUBLIC, 1067 NULL, 1068 NULL, 1069 &ErrorOrigin); 1070 1071 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, 1072 TEEC_NONE, 1073 TEEC_NONE, 1074 TEEC_NONE); 1075 1076 TeecResult = TEEC_InvokeCommand(&TeecSession, 1077 2, 1078 &TeecOperation, 1079 &ErrorOrigin); 1080 1081 TEEC_CloseSession(&TeecSession); 1082 TEEC_FinalizeContext(&TeecContext); 1083 1084 return TeecResult; 1085 } 1086 1087 uint32_t trusty_notify_optee_uboot_end(void) 1088 { 1089 TEEC_Result res; 1090 res = notify_optee_rpmb_ta(); 1091 res |= notify_optee_efuse_ta(); 1092 return res; 1093 } 1094 1095 uint32_t trusty_read_vbootkey_hash(uint32_t *buf, uint32_t length) 1096 { 1097 TEEC_Result TeecResult; 1098 TEEC_Context TeecContext; 1099 TEEC_Session TeecSession; 1100 uint32_t ErrorOrigin; 1101 1102 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 1103 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 1104 TEEC_UUID *TeecUuid = &tempuuid; 1105 TEEC_Operation TeecOperation = {0}; 1106 1107 OpteeClientApiLibInitialize(); 1108 1109 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1110 1111 TeecResult = TEEC_OpenSession(&TeecContext, 1112 &TeecSession, 1113 TeecUuid, 1114 TEEC_LOGIN_PUBLIC, 1115 NULL, 1116 NULL, 1117 &ErrorOrigin); 1118 1119 TEEC_SharedMemory SharedMem0 = {0}; 1120 1121 SharedMem0.size = length * sizeof(uint32_t); 1122 SharedMem0.flags = 0; 1123 1124 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1125 1126 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1127 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1128 1129 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, 1130 TEEC_NONE, 1131 TEEC_NONE, 1132 TEEC_NONE); 1133 1134 TeecResult = TEEC_InvokeCommand(&TeecSession, 1135 3, 1136 &TeecOperation, 1137 &ErrorOrigin); 1138 1139 if (TeecResult == TEEC_SUCCESS) 1140 memcpy(buf, SharedMem0.buffer, SharedMem0.size); 1141 1142 TEEC_ReleaseSharedMemory(&SharedMem0); 1143 TEEC_CloseSession(&TeecSession); 1144 TEEC_FinalizeContext(&TeecContext); 1145 1146 return TeecResult; 1147 } 1148 uint32_t trusty_write_vbootkey_hash(uint32_t *buf, uint32_t length) 1149 { 1150 TEEC_Result TeecResult; 1151 TEEC_Context TeecContext; 1152 TEEC_Session TeecSession; 1153 uint32_t ErrorOrigin; 1154 1155 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 1156 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 1157 TEEC_UUID *TeecUuid = &tempuuid; 1158 TEEC_Operation TeecOperation = {0}; 1159 1160 OpteeClientApiLibInitialize(); 1161 1162 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1163 1164 TeecResult = TEEC_OpenSession(&TeecContext, 1165 &TeecSession, 1166 TeecUuid, 1167 TEEC_LOGIN_PUBLIC, 1168 NULL, 1169 NULL, 1170 &ErrorOrigin); 1171 1172 TEEC_SharedMemory SharedMem0 = {0}; 1173 1174 SharedMem0.size = length * sizeof(uint32_t); 1175 SharedMem0.flags = 0; 1176 1177 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1178 1179 memcpy(SharedMem0.buffer, buf, SharedMem0.size); 1180 1181 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1182 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1183 1184 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 1185 TEEC_NONE, 1186 TEEC_NONE, 1187 TEEC_NONE); 1188 1189 TeecResult = TEEC_InvokeCommand(&TeecSession, 1190 4, 1191 &TeecOperation, 1192 &ErrorOrigin); 1193 1194 TEEC_ReleaseSharedMemory(&SharedMem0); 1195 TEEC_CloseSession(&TeecSession); 1196 TEEC_FinalizeContext(&TeecContext); 1197 1198 return TeecResult; 1199 } 1200 1201 uint32_t trusty_read_vbootkey_enable_flag(uint8_t *flag) 1202 { 1203 TEEC_Result TeecResult; 1204 TEEC_Context TeecContext; 1205 TEEC_Session TeecSession; 1206 uint32_t ErrorOrigin; 1207 uint32_t bootflag; 1208 1209 TEEC_UUID tempuuid = { 0x2d26d8a8, 0x5134, 0x4dd8, \ 1210 { 0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71 } }; 1211 TEEC_UUID *TeecUuid = &tempuuid; 1212 TEEC_Operation TeecOperation = {0}; 1213 1214 OpteeClientApiLibInitialize(); 1215 1216 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1217 1218 TeecResult = TEEC_OpenSession(&TeecContext, 1219 &TeecSession, 1220 TeecUuid, 1221 TEEC_LOGIN_PUBLIC, 1222 NULL, 1223 NULL, 1224 &ErrorOrigin); 1225 1226 TEEC_SharedMemory SharedMem0 = {0}; 1227 1228 SharedMem0.size = 1 * sizeof(uint32_t); 1229 SharedMem0.flags = 0; 1230 1231 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1232 1233 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1234 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1235 1236 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, 1237 TEEC_NONE, 1238 TEEC_NONE, 1239 TEEC_NONE); 1240 1241 TeecResult = TEEC_InvokeCommand(&TeecSession, 1242 5, 1243 &TeecOperation, 1244 &ErrorOrigin); 1245 1246 if (TeecResult == TEEC_SUCCESS) { 1247 memcpy(&bootflag, SharedMem0.buffer, SharedMem0.size); 1248 if (bootflag == 0x000000FF) 1249 *flag = 1; 1250 } 1251 1252 TEEC_ReleaseSharedMemory(&SharedMem0); 1253 TEEC_CloseSession(&TeecSession); 1254 TEEC_FinalizeContext(&TeecContext); 1255 1256 return TeecResult; 1257 } 1258 1259 uint32_t trusty_read_permanent_attributes_flag(uint8_t *attributes) 1260 { 1261 TEEC_Result TeecResult; 1262 TEEC_Context TeecContext; 1263 TEEC_Session TeecSession; 1264 uint32_t ErrorOrigin; 1265 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1266 { 0xa8, 0x69, 0x9c, 0xe6, 0x88, 0x6c, 0x5d, 0x5d } }; 1267 TEEC_UUID *TeecUuid = &tempuuid; 1268 TEEC_Operation TeecOperation = {0}; 1269 struct blk_desc *dev_desc; 1270 dev_desc = rockchip_get_bootdev(); 1271 1272 debug("testmm start\n"); 1273 OpteeClientApiLibInitialize(); 1274 1275 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1276 1277 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1278 TEEC_NONE, 1279 TEEC_NONE, 1280 TEEC_NONE); 1281 /*0 nand or emmc "security" partition , 1 rpmb*/ 1282 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1283 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1284 TeecOperation.params[0].value.a = 0; 1285 #endif 1286 1287 TeecResult = TEEC_OpenSession(&TeecContext, 1288 &TeecSession, 1289 TeecUuid, 1290 TEEC_LOGIN_PUBLIC, 1291 NULL, 1292 #ifdef CONFIG_OPTEE_V1 1293 NULL, 1294 #endif 1295 #ifdef CONFIG_OPTEE_V2 1296 &TeecOperation, 1297 #endif 1298 &ErrorOrigin); 1299 1300 TEEC_SharedMemory SharedMem0 = {0}; 1301 1302 SharedMem0.size = sizeof("attributes_flag"); 1303 SharedMem0.flags = 0; 1304 1305 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1306 1307 memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size); 1308 1309 TEEC_SharedMemory SharedMem1 = {0}; 1310 1311 SharedMem1.size = 1; 1312 SharedMem1.flags = 0; 1313 1314 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 1315 1316 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1317 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1318 1319 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 1320 TeecOperation.params[1].tmpref.size = SharedMem1.size; 1321 1322 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 1323 TEEC_MEMREF_TEMP_INOUT, 1324 TEEC_NONE, 1325 TEEC_NONE); 1326 1327 TeecResult = TEEC_InvokeCommand(&TeecSession, 1328 142, 1329 &TeecOperation, 1330 &ErrorOrigin); 1331 if (TeecResult == TEEC_SUCCESS) 1332 memcpy(attributes, SharedMem1.buffer, SharedMem1.size); 1333 TEEC_ReleaseSharedMemory(&SharedMem0); 1334 TEEC_ReleaseSharedMemory(&SharedMem1); 1335 TEEC_CloseSession(&TeecSession); 1336 TEEC_FinalizeContext(&TeecContext); 1337 debug("testmm end\n"); 1338 1339 return TeecResult; 1340 } 1341 1342 uint32_t trusty_write_permanent_attributes_flag(uint8_t attributes) 1343 { 1344 TEEC_Result TeecResult; 1345 TEEC_Context TeecContext; 1346 TEEC_Session TeecSession; 1347 uint32_t ErrorOrigin; 1348 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1349 { 0xa8, 0x69, 0x9c, 0xe6, 0x88, 0x6c, 0x5d, 0x5d } }; 1350 TEEC_UUID *TeecUuid = &tempuuid; 1351 TEEC_Operation TeecOperation = {0}; 1352 struct blk_desc *dev_desc; 1353 dev_desc = rockchip_get_bootdev(); 1354 1355 debug("testmm start\n"); 1356 OpteeClientApiLibInitialize(); 1357 1358 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1359 1360 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1361 TEEC_NONE, 1362 TEEC_NONE, 1363 TEEC_NONE); 1364 /*0 nand or emmc "security" partition , 1 rpmb*/ 1365 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1366 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1367 TeecOperation.params[0].value.a = 0; 1368 #endif 1369 1370 TeecResult = TEEC_OpenSession(&TeecContext, 1371 &TeecSession, 1372 TeecUuid, 1373 TEEC_LOGIN_PUBLIC, 1374 NULL, 1375 #ifdef CONFIG_OPTEE_V1 1376 NULL, 1377 #endif 1378 #ifdef CONFIG_OPTEE_V2 1379 &TeecOperation, 1380 #endif 1381 &ErrorOrigin); 1382 1383 TEEC_SharedMemory SharedMem0 = {0}; 1384 1385 SharedMem0.size = sizeof("attributes_flag"); 1386 SharedMem0.flags = 0; 1387 1388 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1389 1390 memcpy(SharedMem0.buffer, "attributes_flag", SharedMem0.size); 1391 1392 TEEC_SharedMemory SharedMem1 = {0}; 1393 1394 SharedMem1.size = 1; 1395 SharedMem1.flags = 0; 1396 1397 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 1398 1399 memcpy(SharedMem1.buffer, (char *)&attributes, SharedMem1.size); 1400 1401 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1402 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1403 1404 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 1405 TeecOperation.params[1].tmpref.size = SharedMem1.size; 1406 1407 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, 1408 TEEC_MEMREF_TEMP_INOUT, 1409 TEEC_NONE, 1410 TEEC_NONE); 1411 1412 TeecResult = TEEC_InvokeCommand(&TeecSession, 1413 141, 1414 &TeecOperation, 1415 &ErrorOrigin); 1416 1417 TEEC_ReleaseSharedMemory(&SharedMem0); 1418 TEEC_ReleaseSharedMemory(&SharedMem1); 1419 TEEC_CloseSession(&TeecSession); 1420 TEEC_FinalizeContext(&TeecContext); 1421 debug("testmm end\n"); 1422 1423 return TeecResult; 1424 } 1425 1426 uint32_t trusty_attest_dh(uint8_t *dh, uint32_t *dh_size) 1427 { 1428 TEEC_Result TeecResult; 1429 TEEC_Context TeecContext; 1430 TEEC_Session TeecSession; 1431 uint32_t ErrorOrigin; 1432 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1433 { 0xa8, 0x69, 0x9c, 0xe6, 1434 0x88, 0x6c, 0x5d, 0x5d 1435 } 1436 }; 1437 TEEC_UUID *TeecUuid = &tempuuid; 1438 TEEC_Operation TeecOperation = {0}; 1439 struct blk_desc *dev_desc; 1440 dev_desc = rockchip_get_bootdev(); 1441 1442 OpteeClientApiLibInitialize(); 1443 1444 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1445 1446 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1447 TEEC_NONE, 1448 TEEC_NONE, 1449 TEEC_NONE); 1450 /*0 nand or emmc "security" partition , 1 rpmb*/ 1451 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1452 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1453 TeecOperation.params[0].value.a = 0; 1454 #endif 1455 1456 TeecResult = TEEC_OpenSession(&TeecContext, 1457 &TeecSession, 1458 TeecUuid, 1459 TEEC_LOGIN_PUBLIC, 1460 NULL, 1461 #ifdef CONFIG_OPTEE_V1 1462 NULL, 1463 #endif 1464 #ifdef CONFIG_OPTEE_V2 1465 &TeecOperation, 1466 #endif 1467 &ErrorOrigin); 1468 1469 TEEC_SharedMemory SharedMem0 = {0}; 1470 1471 SharedMem0.size = *dh_size; 1472 SharedMem0.flags = 0; 1473 1474 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1475 1476 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1477 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1478 1479 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1480 TEEC_NONE, 1481 TEEC_NONE, 1482 TEEC_NONE); 1483 1484 TeecResult = TEEC_InvokeCommand(&TeecSession, 1485 143, 1486 &TeecOperation, 1487 &ErrorOrigin); 1488 1489 *dh_size = TeecOperation.params[0].tmpref.size; 1490 memcpy(dh, SharedMem0.buffer, SharedMem0.size); 1491 1492 TEEC_ReleaseSharedMemory(&SharedMem0); 1493 1494 TEEC_CloseSession(&TeecSession); 1495 TeecResult = TEEC_FinalizeContext(&TeecContext); 1496 1497 return TeecResult; 1498 } 1499 1500 uint32_t trusty_attest_uuid(uint8_t *uuid, uint32_t *uuid_size) 1501 { 1502 TEEC_Result TeecResult; 1503 TEEC_Context TeecContext; 1504 TEEC_Session TeecSession; 1505 uint32_t ErrorOrigin; 1506 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1507 { 0xa8, 0x69, 0x9c, 0xe6, 1508 0x88, 0x6c, 0x5d, 0x5d 1509 } 1510 }; 1511 TEEC_UUID *TeecUuid = &tempuuid; 1512 TEEC_Operation TeecOperation = {0}; 1513 struct blk_desc *dev_desc; 1514 dev_desc = rockchip_get_bootdev(); 1515 1516 OpteeClientApiLibInitialize(); 1517 1518 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1519 1520 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1521 TEEC_NONE, 1522 TEEC_NONE, 1523 TEEC_NONE); 1524 /*0 nand or emmc "security" partition , 1 rpmb*/ 1525 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1526 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1527 TeecOperation.params[0].value.a = 0; 1528 #endif 1529 1530 TeecResult = TEEC_OpenSession(&TeecContext, 1531 &TeecSession, 1532 TeecUuid, 1533 TEEC_LOGIN_PUBLIC, 1534 NULL, 1535 #ifdef CONFIG_OPTEE_V1 1536 NULL, 1537 #endif 1538 #ifdef CONFIG_OPTEE_V2 1539 &TeecOperation, 1540 #endif 1541 &ErrorOrigin); 1542 1543 TEEC_SharedMemory SharedMem0 = {0}; 1544 1545 SharedMem0.size = *uuid_size; 1546 SharedMem0.flags = 0; 1547 1548 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1549 1550 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1551 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1552 1553 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1554 TEEC_NONE, 1555 TEEC_NONE, 1556 TEEC_NONE); 1557 1558 TeecResult = TEEC_InvokeCommand(&TeecSession, 1559 144, 1560 &TeecOperation, 1561 &ErrorOrigin); 1562 1563 *uuid_size = TeecOperation.params[0].tmpref.size; 1564 memcpy(uuid, SharedMem0.buffer, SharedMem0.size); 1565 1566 TEEC_ReleaseSharedMemory(&SharedMem0); 1567 1568 TEEC_CloseSession(&TeecSession); 1569 TeecResult = TEEC_FinalizeContext(&TeecContext); 1570 1571 return TeecResult; 1572 } 1573 1574 uint32_t trusty_attest_get_ca(uint8_t *operation_start, 1575 uint32_t *operation_size, 1576 uint8_t *out, 1577 uint32_t *out_len) 1578 { 1579 TEEC_Result TeecResult; 1580 TEEC_Context TeecContext; 1581 TEEC_Session TeecSession; 1582 uint32_t ErrorOrigin; 1583 1584 OpteeClientApiLibInitialize(); 1585 1586 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1587 { 0xa8, 0x69, 0x9c, 0xe6, 1588 0x88, 0x6c, 0x5d, 0x5d 1589 } 1590 }; 1591 1592 TEEC_UUID *TeecUuid = &tempuuid; 1593 TEEC_Operation TeecOperation = {0}; 1594 struct blk_desc *dev_desc; 1595 dev_desc = rockchip_get_bootdev(); 1596 1597 OpteeClientApiLibInitialize(); 1598 1599 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1600 1601 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1602 TEEC_NONE, 1603 TEEC_NONE, 1604 TEEC_NONE); 1605 /*0 nand or emmc "security" partition , 1 rpmb*/ 1606 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1607 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1608 TeecOperation.params[0].value.a = 0; 1609 #endif 1610 1611 TeecResult = TEEC_OpenSession(&TeecContext, 1612 &TeecSession, 1613 TeecUuid, 1614 TEEC_LOGIN_PUBLIC, 1615 NULL, 1616 #ifdef CONFIG_OPTEE_V1 1617 NULL, 1618 #endif 1619 #ifdef CONFIG_OPTEE_V2 1620 &TeecOperation, 1621 #endif 1622 &ErrorOrigin); 1623 1624 TEEC_SharedMemory SharedMem0 = {0}; 1625 1626 SharedMem0.size = *operation_size; 1627 SharedMem0.flags = 0; 1628 1629 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1630 1631 memcpy(SharedMem0.buffer, operation_start, SharedMem0.size); 1632 1633 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1634 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1635 1636 TEEC_SharedMemory SharedMem1 = {0}; 1637 1638 SharedMem1.size = *out_len; 1639 SharedMem1.flags = 0; 1640 1641 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem1); 1642 1643 TeecOperation.params[1].tmpref.buffer = SharedMem1.buffer; 1644 TeecOperation.params[1].tmpref.size = SharedMem1.size; 1645 1646 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1647 TEEC_MEMREF_TEMP_INOUT, 1648 TEEC_NONE, 1649 TEEC_NONE); 1650 1651 TeecResult = TEEC_InvokeCommand(&TeecSession, 1652 145, 1653 &TeecOperation, 1654 &ErrorOrigin); 1655 1656 *out_len = TeecOperation.params[1].tmpref.size; 1657 memcpy(out, SharedMem1.buffer, SharedMem1.size); 1658 TEEC_ReleaseSharedMemory(&SharedMem0); 1659 TEEC_ReleaseSharedMemory(&SharedMem1); 1660 1661 return TeecResult; 1662 } 1663 1664 uint32_t trusty_attest_set_ca(uint8_t *ca_response, uint32_t *ca_response_size) 1665 { 1666 TEEC_Result TeecResult; 1667 TEEC_Context TeecContext; 1668 TEEC_Session TeecSession; 1669 uint32_t ErrorOrigin; 1670 TEEC_UUID tempuuid = { 0x258be795, 0xf9ca, 0x40e6, 1671 { 0xa8, 0x69, 0x9c, 0xe6, 1672 0x88, 0x6c, 0x5d, 0x5d 1673 } 1674 }; 1675 TEEC_UUID *TeecUuid = &tempuuid; 1676 TEEC_Operation TeecOperation = {0}; 1677 struct blk_desc *dev_desc; 1678 dev_desc = rockchip_get_bootdev(); 1679 1680 TeecResult = TEEC_InitializeContext(NULL, &TeecContext); 1681 1682 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, 1683 TEEC_NONE, 1684 TEEC_NONE, 1685 TEEC_NONE); 1686 /*0 nand or emmc "security" partition , 1 rpmb*/ 1687 TeecOperation.params[0].value.a = (dev_desc->if_type == IF_TYPE_MMC) ? 1 : 0; 1688 #ifdef CONFIG_OPTEE_ALWAYS_USE_SECURITY_PARTITION 1689 TeecOperation.params[0].value.a = 0; 1690 #endif 1691 1692 TeecResult = TEEC_OpenSession(&TeecContext, 1693 &TeecSession, 1694 TeecUuid, 1695 TEEC_LOGIN_PUBLIC, 1696 NULL, 1697 #ifdef CONFIG_OPTEE_V1 1698 NULL, 1699 #endif 1700 #ifdef CONFIG_OPTEE_V2 1701 &TeecOperation, 1702 #endif 1703 &ErrorOrigin); 1704 1705 TEEC_SharedMemory SharedMem0 = {0}; 1706 1707 SharedMem0.size = *ca_response_size; 1708 SharedMem0.flags = 0; 1709 1710 TeecResult = TEEC_AllocateSharedMemory(&TeecContext, &SharedMem0); 1711 1712 memcpy(SharedMem0.buffer, ca_response, SharedMem0.size); 1713 1714 TeecOperation.params[0].tmpref.buffer = SharedMem0.buffer; 1715 TeecOperation.params[0].tmpref.size = SharedMem0.size; 1716 1717 TeecOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, 1718 TEEC_NONE, 1719 TEEC_NONE, 1720 TEEC_NONE); 1721 1722 TeecResult = TEEC_InvokeCommand(&TeecSession, 1723 146, 1724 &TeecOperation, 1725 &ErrorOrigin); 1726 1727 TEEC_ReleaseSharedMemory(&SharedMem0); 1728 1729 TEEC_CloseSession(&TeecSession); 1730 TeecResult = TEEC_FinalizeContext(&TeecContext); 1731 1732 return TeecResult; 1733 } 1734