1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2020-2024, Arm Limited. 4 */ 5 #include <crypto/crypto.h> 6 #include <initcall.h> 7 #include <kernel/boot.h> 8 #include <kernel/embedded_ts.h> 9 #include <kernel/ldelf_loader.h> 10 #include <kernel/secure_partition.h> 11 #include <kernel/spinlock.h> 12 #include <kernel/spmc_sp_handler.h> 13 #include <kernel/thread_private.h> 14 #include <kernel/thread_spmc.h> 15 #include <kernel/tpm.h> 16 #include <kernel/ts_store.h> 17 #include <ldelf.h> 18 #include <libfdt.h> 19 #include <mm/core_mmu.h> 20 #include <mm/fobj.h> 21 #include <mm/mobj.h> 22 #include <mm/vm.h> 23 #include <optee_ffa.h> 24 #include <stdio.h> 25 #include <string.h> 26 #include <tee_api_types.h> 27 #include <tee/uuid.h> 28 #include <trace.h> 29 #include <types_ext.h> 30 #include <utee_defines.h> 31 #include <util.h> 32 #include <zlib.h> 33 34 #define BOUNCE_BUFFER_SIZE 4096 35 36 #define SP_MANIFEST_ATTR_READ BIT(0) 37 #define SP_MANIFEST_ATTR_WRITE BIT(1) 38 #define SP_MANIFEST_ATTR_EXEC BIT(2) 39 #define SP_MANIFEST_ATTR_NSEC BIT(3) 40 41 #define SP_MANIFEST_ATTR_RO (SP_MANIFEST_ATTR_READ) 42 #define SP_MANIFEST_ATTR_RW (SP_MANIFEST_ATTR_READ | \ 43 SP_MANIFEST_ATTR_WRITE) 44 #define SP_MANIFEST_ATTR_RX (SP_MANIFEST_ATTR_READ | \ 45 SP_MANIFEST_ATTR_EXEC) 46 #define SP_MANIFEST_ATTR_RWX (SP_MANIFEST_ATTR_READ | \ 47 SP_MANIFEST_ATTR_WRITE | \ 48 SP_MANIFEST_ATTR_EXEC) 49 50 #define SP_MANIFEST_FLAG_NOBITS BIT(0) 51 52 #define SP_MANIFEST_NS_INT_QUEUED (0x0) 53 #define SP_MANIFEST_NS_INT_MANAGED_EXIT (0x1) 54 #define SP_MANIFEST_NS_INT_SIGNALED (0x2) 55 56 #define SP_MANIFEST_EXEC_STATE_AARCH64 (0x0) 57 #define SP_MANIFEST_EXEC_STATE_AARCH32 (0x1) 58 59 #define SP_MANIFEST_DIRECT_REQ_RECEIVE BIT(0) 60 #define SP_MANIFEST_DIRECT_REQ_SEND BIT(1) 61 #define SP_MANIFEST_INDIRECT_REQ BIT(2) 62 63 #define SP_MANIFEST_VM_CREATED_MSG BIT(0) 64 #define SP_MANIFEST_VM_DESTROYED_MSG BIT(1) 65 66 #define SP_PKG_HEADER_MAGIC (0x474b5053) 67 #define SP_PKG_HEADER_VERSION_V1 (0x1) 68 #define SP_PKG_HEADER_VERSION_V2 (0x2) 69 70 struct sp_pkg_header { 71 uint32_t magic; 72 uint32_t version; 73 uint32_t pm_offset; 74 uint32_t pm_size; 75 uint32_t img_offset; 76 uint32_t img_size; 77 }; 78 79 struct fip_sp_head fip_sp_list = STAILQ_HEAD_INITIALIZER(fip_sp_list); 80 81 static const struct ts_ops sp_ops; 82 83 /* List that holds all of the loaded SP's */ 84 static struct sp_sessions_head open_sp_sessions = 85 TAILQ_HEAD_INITIALIZER(open_sp_sessions); 86 87 static const struct embedded_ts *find_secure_partition(const TEE_UUID *uuid) 88 { 89 const struct sp_image *sp = NULL; 90 const struct fip_sp *fip_sp = NULL; 91 92 for_each_secure_partition(sp) { 93 if (!memcmp(&sp->image.uuid, uuid, sizeof(*uuid))) 94 return &sp->image; 95 } 96 97 for_each_fip_sp(fip_sp) { 98 if (!memcmp(&fip_sp->sp_img.image.uuid, uuid, sizeof(*uuid))) 99 return &fip_sp->sp_img.image; 100 } 101 102 return NULL; 103 } 104 105 bool is_sp_ctx(struct ts_ctx *ctx) 106 { 107 return ctx && (ctx->ops == &sp_ops); 108 } 109 110 static void set_sp_ctx_ops(struct ts_ctx *ctx) 111 { 112 ctx->ops = &sp_ops; 113 } 114 115 struct sp_session *sp_get_session(uint32_t session_id) 116 { 117 struct sp_session *s = NULL; 118 119 TAILQ_FOREACH(s, &open_sp_sessions, link) { 120 if (s->endpoint_id == session_id) 121 return s; 122 } 123 124 return NULL; 125 } 126 127 TEE_Result sp_partition_info_get(uint32_t ffa_vers, void *buf, size_t buf_size, 128 const TEE_UUID *ffa_uuid, size_t *elem_count, 129 bool count_only) 130 { 131 TEE_Result res = TEE_SUCCESS; 132 struct sp_session *s = NULL; 133 134 TAILQ_FOREACH(s, &open_sp_sessions, link) { 135 if (ffa_uuid && 136 memcmp(&s->ffa_uuid, ffa_uuid, sizeof(*ffa_uuid))) 137 continue; 138 139 if (s->state == sp_dead) 140 continue; 141 if (!count_only && !res) { 142 uint32_t uuid_words[4] = { 0 }; 143 144 tee_uuid_to_octets((uint8_t *)uuid_words, &s->ffa_uuid); 145 res = spmc_fill_partition_entry(ffa_vers, buf, buf_size, 146 *elem_count, 147 s->endpoint_id, 1, 148 s->props, uuid_words); 149 } 150 *elem_count += 1; 151 } 152 153 return res; 154 } 155 156 bool sp_has_exclusive_access(struct sp_mem_map_region *mem, 157 struct user_mode_ctx *uctx) 158 { 159 /* 160 * Check that we have access to the region if it is supposed to be 161 * mapped to the current context. 162 */ 163 if (uctx) { 164 struct vm_region *region = NULL; 165 166 /* Make sure that each mobj belongs to the SP */ 167 TAILQ_FOREACH(region, &uctx->vm_info.regions, link) { 168 if (region->mobj == mem->mobj) 169 break; 170 } 171 172 if (!region) 173 return false; 174 } 175 176 /* Check that it is not shared with another SP */ 177 return !sp_mem_is_shared(mem); 178 } 179 180 static bool endpoint_id_is_valid(uint32_t id) 181 { 182 /* 183 * These IDs are assigned at the SPMC init so already have valid values 184 * by the time this function gets first called 185 */ 186 return id != spmd_id && id != spmc_id && id != optee_endpoint_id && 187 id >= FFA_SWD_ID_MIN && id <= FFA_SWD_ID_MAX; 188 } 189 190 static TEE_Result new_session_id(uint16_t *endpoint_id) 191 { 192 uint32_t id = 0; 193 194 /* Find the first available endpoint id */ 195 for (id = FFA_SWD_ID_MIN; id <= FFA_SWD_ID_MAX; id++) { 196 if (endpoint_id_is_valid(id) && !sp_get_session(id)) { 197 *endpoint_id = id; 198 return TEE_SUCCESS; 199 } 200 } 201 202 return TEE_ERROR_BAD_FORMAT; 203 } 204 205 static TEE_Result sp_create_ctx(const TEE_UUID *bin_uuid, struct sp_session *s) 206 { 207 TEE_Result res = TEE_SUCCESS; 208 struct sp_ctx *spc = NULL; 209 210 /* Register context */ 211 spc = calloc(1, sizeof(struct sp_ctx)); 212 if (!spc) 213 return TEE_ERROR_OUT_OF_MEMORY; 214 215 spc->open_session = s; 216 s->ts_sess.ctx = &spc->ts_ctx; 217 spc->ts_ctx.uuid = *bin_uuid; 218 219 res = vm_info_init(&spc->uctx, &spc->ts_ctx); 220 if (res) 221 goto err; 222 223 set_sp_ctx_ops(&spc->ts_ctx); 224 225 return TEE_SUCCESS; 226 227 err: 228 free(spc); 229 return res; 230 } 231 232 /* 233 * Insert a new sp_session to the sessions list, so that it is ordered 234 * by boot_order. 235 */ 236 static void insert_session_ordered(struct sp_sessions_head *open_sessions, 237 struct sp_session *session) 238 { 239 struct sp_session *s = NULL; 240 241 if (!open_sessions || !session) 242 return; 243 244 TAILQ_FOREACH(s, &open_sp_sessions, link) { 245 if (s->boot_order > session->boot_order) 246 break; 247 } 248 249 if (!s) 250 TAILQ_INSERT_TAIL(open_sessions, session, link); 251 else 252 TAILQ_INSERT_BEFORE(s, session, link); 253 } 254 255 static TEE_Result sp_create_session(struct sp_sessions_head *open_sessions, 256 const TEE_UUID *bin_uuid, 257 const uint32_t boot_order, 258 struct sp_session **sess) 259 { 260 TEE_Result res = TEE_SUCCESS; 261 struct sp_session *s = calloc(1, sizeof(struct sp_session)); 262 263 if (!s) 264 return TEE_ERROR_OUT_OF_MEMORY; 265 266 s->boot_order = boot_order; 267 268 /* Other properties are filled later, based on the SP's manifest */ 269 s->props = FFA_PART_PROP_IS_PE_ID; 270 271 res = new_session_id(&s->endpoint_id); 272 if (res) 273 goto err; 274 275 DMSG("Loading Secure Partition %pUl", (void *)bin_uuid); 276 res = sp_create_ctx(bin_uuid, s); 277 if (res) 278 goto err; 279 280 insert_session_ordered(open_sessions, s); 281 *sess = s; 282 return TEE_SUCCESS; 283 284 err: 285 free(s); 286 return res; 287 } 288 289 static TEE_Result sp_init_set_registers(struct sp_ctx *ctx) 290 { 291 struct thread_ctx_regs *sp_regs = &ctx->sp_regs; 292 293 memset(sp_regs, 0, sizeof(*sp_regs)); 294 sp_regs->sp = ctx->uctx.stack_ptr; 295 sp_regs->pc = ctx->uctx.entry_func; 296 297 return TEE_SUCCESS; 298 } 299 300 TEE_Result sp_map_shared(struct sp_session *s, 301 struct sp_mem_receiver *receiver, 302 struct sp_mem *smem, 303 uint64_t *va) 304 { 305 TEE_Result res = TEE_SUCCESS; 306 struct sp_ctx *ctx = NULL; 307 uint32_t perm = TEE_MATTR_UR; 308 struct sp_mem_map_region *reg = NULL; 309 310 ctx = to_sp_ctx(s->ts_sess.ctx); 311 312 /* Get the permission */ 313 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 314 perm |= TEE_MATTR_UX; 315 316 if (receiver->perm.perm & FFA_MEM_ACC_RW) { 317 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 318 return TEE_ERROR_ACCESS_CONFLICT; 319 320 perm |= TEE_MATTR_UW; 321 } 322 /* 323 * Currently we don't support passing a va. We can't guarantee that the 324 * full region will be mapped in a contiguous region. A smem->region can 325 * have multiple mobj for one share. Currently there doesn't seem to be 326 * an option to guarantee that these will be mapped in a contiguous va 327 * space. 328 */ 329 if (*va) 330 return TEE_ERROR_NOT_SUPPORTED; 331 332 SLIST_FOREACH(reg, &smem->regions, link) { 333 res = vm_map(&ctx->uctx, va, reg->page_count * SMALL_PAGE_SIZE, 334 perm, 0, reg->mobj, reg->page_offset); 335 336 if (res != TEE_SUCCESS) { 337 EMSG("Failed to map memory region %#"PRIx32, res); 338 return res; 339 } 340 } 341 return TEE_SUCCESS; 342 } 343 344 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem) 345 { 346 TEE_Result res = TEE_SUCCESS; 347 vaddr_t vaddr = 0; 348 size_t len = 0; 349 struct sp_ctx *ctx = to_sp_ctx(s->ts_sess.ctx); 350 struct sp_mem_map_region *reg = NULL; 351 352 SLIST_FOREACH(reg, &smem->regions, link) { 353 vaddr = (vaddr_t)sp_mem_get_va(&ctx->uctx, reg->page_offset, 354 reg->mobj); 355 len = reg->page_count * SMALL_PAGE_SIZE; 356 357 res = vm_unmap(&ctx->uctx, vaddr, len); 358 if (res != TEE_SUCCESS) 359 return res; 360 } 361 362 return TEE_SUCCESS; 363 } 364 365 static TEE_Result sp_dt_get_u64(const void *fdt, int node, const char *property, 366 uint64_t *value) 367 { 368 const fdt64_t *p = NULL; 369 int len = 0; 370 371 p = fdt_getprop(fdt, node, property, &len); 372 if (!p) 373 return TEE_ERROR_ITEM_NOT_FOUND; 374 375 if (len != sizeof(*p)) 376 return TEE_ERROR_BAD_FORMAT; 377 378 *value = fdt64_ld(p); 379 380 return TEE_SUCCESS; 381 } 382 383 static TEE_Result sp_dt_get_u32(const void *fdt, int node, const char *property, 384 uint32_t *value) 385 { 386 const fdt32_t *p = NULL; 387 int len = 0; 388 389 p = fdt_getprop(fdt, node, property, &len); 390 if (!p) 391 return TEE_ERROR_ITEM_NOT_FOUND; 392 393 if (len != sizeof(*p)) 394 return TEE_ERROR_BAD_FORMAT; 395 396 *value = fdt32_to_cpu(*p); 397 398 return TEE_SUCCESS; 399 } 400 401 static TEE_Result sp_dt_get_u16(const void *fdt, int node, const char *property, 402 uint16_t *value) 403 { 404 const fdt16_t *p = NULL; 405 int len = 0; 406 407 p = fdt_getprop(fdt, node, property, &len); 408 if (!p) 409 return TEE_ERROR_ITEM_NOT_FOUND; 410 411 if (len != sizeof(*p)) 412 return TEE_ERROR_BAD_FORMAT; 413 414 *value = fdt16_to_cpu(*p); 415 416 return TEE_SUCCESS; 417 } 418 419 static TEE_Result sp_dt_get_uuid(const void *fdt, int node, 420 const char *property, TEE_UUID *uuid) 421 { 422 uint32_t uuid_array[4] = { 0 }; 423 const fdt32_t *p = NULL; 424 int len = 0; 425 int i = 0; 426 427 p = fdt_getprop(fdt, node, property, &len); 428 if (!p) 429 return TEE_ERROR_ITEM_NOT_FOUND; 430 431 if (len != sizeof(TEE_UUID)) 432 return TEE_ERROR_BAD_FORMAT; 433 434 for (i = 0; i < 4; i++) 435 uuid_array[i] = fdt32_to_cpu(p[i]); 436 437 tee_uuid_from_octets(uuid, (uint8_t *)uuid_array); 438 439 return TEE_SUCCESS; 440 } 441 442 static TEE_Result sp_is_elf_format(const void *fdt, int sp_node, 443 bool *is_elf_format) 444 { 445 TEE_Result res = TEE_SUCCESS; 446 uint32_t elf_format = 0; 447 448 res = sp_dt_get_u32(fdt, sp_node, "elf-format", &elf_format); 449 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) 450 return res; 451 452 *is_elf_format = (elf_format != 0); 453 454 return TEE_SUCCESS; 455 } 456 457 static TEE_Result sp_binary_open(const TEE_UUID *uuid, 458 const struct ts_store_ops **ops, 459 struct ts_store_handle **handle) 460 { 461 TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; 462 463 SCATTERED_ARRAY_FOREACH(*ops, sp_stores, struct ts_store_ops) { 464 res = (*ops)->open(uuid, handle); 465 if (res != TEE_ERROR_ITEM_NOT_FOUND && 466 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 467 break; 468 } 469 470 return res; 471 } 472 473 static TEE_Result load_binary_sp(struct ts_session *s, 474 struct user_mode_ctx *uctx) 475 { 476 size_t bin_size = 0, bin_size_rounded = 0, bin_page_count = 0; 477 size_t bb_size = ROUNDUP(BOUNCE_BUFFER_SIZE, SMALL_PAGE_SIZE); 478 size_t bb_num_pages = bb_size / SMALL_PAGE_SIZE; 479 const struct ts_store_ops *store_ops = NULL; 480 struct ts_store_handle *handle = NULL; 481 TEE_Result res = TEE_SUCCESS; 482 tee_mm_entry_t *mm = NULL; 483 struct fobj *fobj = NULL; 484 struct mobj *mobj = NULL; 485 uaddr_t base_addr = 0; 486 uint32_t vm_flags = 0; 487 unsigned int idx = 0; 488 vaddr_t va = 0; 489 490 if (!s || !uctx) 491 return TEE_ERROR_BAD_PARAMETERS; 492 493 DMSG("Loading raw binary format SP %pUl", &uctx->ts_ctx->uuid); 494 495 /* Initialize the bounce buffer */ 496 fobj = fobj_sec_mem_alloc(bb_num_pages); 497 mobj = mobj_with_fobj_alloc(fobj, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 498 fobj_put(fobj); 499 if (!mobj) 500 return TEE_ERROR_OUT_OF_MEMORY; 501 502 res = vm_map(uctx, &va, bb_size, TEE_MATTR_PRW, 0, mobj, 0); 503 mobj_put(mobj); 504 if (res) 505 return res; 506 507 uctx->bbuf = (uint8_t *)va; 508 uctx->bbuf_size = BOUNCE_BUFFER_SIZE; 509 510 vm_set_ctx(uctx->ts_ctx); 511 512 /* Find TS store and open SP binary */ 513 res = sp_binary_open(&uctx->ts_ctx->uuid, &store_ops, &handle); 514 if (res != TEE_SUCCESS) { 515 EMSG("Failed to open SP binary"); 516 return res; 517 } 518 519 /* Query binary size and calculate page count */ 520 res = store_ops->get_size(handle, &bin_size); 521 if (res != TEE_SUCCESS) 522 goto err; 523 524 if (ROUNDUP_OVERFLOW(bin_size, SMALL_PAGE_SIZE, &bin_size_rounded)) { 525 res = TEE_ERROR_OVERFLOW; 526 goto err; 527 } 528 529 bin_page_count = bin_size_rounded / SMALL_PAGE_SIZE; 530 531 /* Allocate memory */ 532 mm = tee_mm_alloc(&tee_mm_sec_ddr, bin_size_rounded); 533 if (!mm) { 534 res = TEE_ERROR_OUT_OF_MEMORY; 535 goto err; 536 } 537 538 base_addr = tee_mm_get_smem(mm); 539 540 /* Create mobj */ 541 mobj = sp_mem_new_mobj(bin_page_count, TEE_MATTR_MEM_TYPE_CACHED, true); 542 if (!mobj) { 543 res = TEE_ERROR_OUT_OF_MEMORY; 544 goto err_free_tee_mm; 545 } 546 547 res = sp_mem_add_pages(mobj, &idx, base_addr, bin_page_count); 548 if (res) 549 goto err_free_mobj; 550 551 /* Map memory area for the SP binary */ 552 va = 0; 553 res = vm_map(uctx, &va, bin_size_rounded, TEE_MATTR_URWX, 554 vm_flags, mobj, 0); 555 if (res) 556 goto err_free_mobj; 557 558 /* Read SP binary into the previously mapped memory area */ 559 res = store_ops->read(handle, NULL, (void *)va, bin_size); 560 if (res) 561 goto err_unmap; 562 563 /* Set memory protection to allow execution */ 564 res = vm_set_prot(uctx, va, bin_size_rounded, TEE_MATTR_UX); 565 if (res) 566 goto err_unmap; 567 568 mobj_put(mobj); 569 store_ops->close(handle); 570 571 /* The entry point must be at the beginning of the SP binary. */ 572 uctx->entry_func = va; 573 uctx->load_addr = va; 574 uctx->is_32bit = false; 575 576 s->handle_scall = s->ctx->ops->handle_scall; 577 578 return TEE_SUCCESS; 579 580 err_unmap: 581 vm_unmap(uctx, va, bin_size_rounded); 582 583 err_free_mobj: 584 mobj_put(mobj); 585 586 err_free_tee_mm: 587 tee_mm_free(mm); 588 589 err: 590 store_ops->close(handle); 591 592 return res; 593 } 594 595 static TEE_Result sp_open_session(struct sp_session **sess, 596 struct sp_sessions_head *open_sessions, 597 const TEE_UUID *ffa_uuid, 598 const TEE_UUID *bin_uuid, 599 const uint32_t boot_order, 600 const void *fdt) 601 { 602 TEE_Result res = TEE_SUCCESS; 603 struct sp_session *s = NULL; 604 struct sp_ctx *ctx = NULL; 605 bool is_elf_format = false; 606 607 if (!find_secure_partition(bin_uuid)) 608 return TEE_ERROR_ITEM_NOT_FOUND; 609 610 res = sp_create_session(open_sessions, bin_uuid, boot_order, &s); 611 if (res != TEE_SUCCESS) { 612 DMSG("sp_create_session failed %#"PRIx32, res); 613 return res; 614 } 615 616 ctx = to_sp_ctx(s->ts_sess.ctx); 617 assert(ctx); 618 if (!ctx) 619 return TEE_ERROR_TARGET_DEAD; 620 *sess = s; 621 622 ts_push_current_session(&s->ts_sess); 623 624 res = sp_is_elf_format(fdt, 0, &is_elf_format); 625 if (res == TEE_SUCCESS) { 626 if (is_elf_format) { 627 /* Load the SP using ldelf. */ 628 ldelf_load_ldelf(&ctx->uctx); 629 res = ldelf_init_with_ldelf(&s->ts_sess, &ctx->uctx); 630 } else { 631 /* Raw binary format SP */ 632 res = load_binary_sp(&s->ts_sess, &ctx->uctx); 633 } 634 } else { 635 EMSG("Failed to detect SP format"); 636 } 637 638 if (res != TEE_SUCCESS) { 639 EMSG("Failed loading SP %#"PRIx32, res); 640 ts_pop_current_session(); 641 return TEE_ERROR_TARGET_DEAD; 642 } 643 644 /* 645 * Make the SP ready for its first run. 646 * Set state to busy to prevent other endpoints from sending messages to 647 * the SP before its boot phase is done. 648 */ 649 s->state = sp_busy; 650 s->caller_id = 0; 651 sp_init_set_registers(ctx); 652 memcpy(&s->ffa_uuid, ffa_uuid, sizeof(*ffa_uuid)); 653 ts_pop_current_session(); 654 655 return TEE_SUCCESS; 656 } 657 658 static TEE_Result fdt_get_uuid(const void * const fdt, TEE_UUID *uuid) 659 { 660 const struct fdt_property *description = NULL; 661 int description_name_len = 0; 662 663 if (fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0")) { 664 EMSG("Failed loading SP, manifest not found"); 665 return TEE_ERROR_BAD_PARAMETERS; 666 } 667 668 description = fdt_get_property(fdt, 0, "description", 669 &description_name_len); 670 if (description) 671 DMSG("Loading SP: %s", description->data); 672 673 if (sp_dt_get_uuid(fdt, 0, "uuid", uuid)) { 674 EMSG("Missing or invalid UUID in SP manifest"); 675 return TEE_ERROR_BAD_FORMAT; 676 } 677 678 return TEE_SUCCESS; 679 } 680 681 static TEE_Result copy_and_map_fdt(struct sp_ctx *ctx, const void * const fdt, 682 void **fdt_copy, size_t *mapped_size) 683 { 684 size_t total_size = ROUNDUP(fdt_totalsize(fdt), SMALL_PAGE_SIZE); 685 size_t num_pages = total_size / SMALL_PAGE_SIZE; 686 uint32_t perm = TEE_MATTR_UR | TEE_MATTR_PRW; 687 TEE_Result res = TEE_SUCCESS; 688 struct mobj *m = NULL; 689 struct fobj *f = NULL; 690 vaddr_t va = 0; 691 692 f = fobj_sec_mem_alloc(num_pages); 693 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 694 fobj_put(f); 695 if (!m) 696 return TEE_ERROR_OUT_OF_MEMORY; 697 698 res = vm_map(&ctx->uctx, &va, total_size, perm, 0, m, 0); 699 mobj_put(m); 700 if (res) 701 return res; 702 703 if (fdt_open_into(fdt, (void *)va, total_size)) 704 return TEE_ERROR_GENERIC; 705 706 *fdt_copy = (void *)va; 707 *mapped_size = total_size; 708 709 return res; 710 } 711 712 static void fill_boot_info_1_0(vaddr_t buf, const void *fdt) 713 { 714 struct ffa_boot_info_1_0 *info = (struct ffa_boot_info_1_0 *)buf; 715 static const char fdt_name[16] = "TYPE_DT\0\0\0\0\0\0\0\0"; 716 717 memcpy(&info->magic, "FF-A", 4); 718 info->count = 1; 719 720 COMPILE_TIME_ASSERT(sizeof(info->nvp[0].name) == sizeof(fdt_name)); 721 memcpy(info->nvp[0].name, fdt_name, sizeof(fdt_name)); 722 info->nvp[0].value = (uintptr_t)fdt; 723 info->nvp[0].size = fdt_totalsize(fdt); 724 } 725 726 static void fill_boot_info_1_1(vaddr_t buf, const void *fdt) 727 { 728 size_t desc_offs = ROUNDUP(sizeof(struct ffa_boot_info_header_1_1), 8); 729 struct ffa_boot_info_header_1_1 *header = 730 (struct ffa_boot_info_header_1_1 *)buf; 731 struct ffa_boot_info_1_1 *desc = 732 (struct ffa_boot_info_1_1 *)(buf + desc_offs); 733 734 header->signature = FFA_BOOT_INFO_SIGNATURE; 735 header->version = FFA_BOOT_INFO_VERSION; 736 header->blob_size = desc_offs + sizeof(struct ffa_boot_info_1_1); 737 header->desc_size = sizeof(struct ffa_boot_info_1_1); 738 header->desc_count = 1; 739 header->desc_offset = desc_offs; 740 741 memset(&desc[0].name, 0, sizeof(desc[0].name)); 742 /* Type: Standard boot info (bit[7] == 0), FDT type */ 743 desc[0].type = FFA_BOOT_INFO_TYPE_ID_FDT; 744 /* Flags: Contents field contains an address */ 745 desc[0].flags = FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR << 746 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT; 747 desc[0].size = fdt_totalsize(fdt); 748 desc[0].contents = (uintptr_t)fdt; 749 } 750 751 static TEE_Result create_and_map_boot_info(struct sp_ctx *ctx, const void *fdt, 752 struct thread_smc_args *args, 753 vaddr_t *va, size_t *mapped_size, 754 uint32_t sp_ffa_version) 755 { 756 size_t total_size = ROUNDUP(CFG_SP_INIT_INFO_MAX_SIZE, SMALL_PAGE_SIZE); 757 size_t num_pages = total_size / SMALL_PAGE_SIZE; 758 uint32_t perm = TEE_MATTR_UR | TEE_MATTR_PRW; 759 TEE_Result res = TEE_SUCCESS; 760 struct fobj *f = NULL; 761 struct mobj *m = NULL; 762 uint32_t info_reg = 0; 763 764 f = fobj_sec_mem_alloc(num_pages); 765 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 766 fobj_put(f); 767 if (!m) 768 return TEE_ERROR_OUT_OF_MEMORY; 769 770 res = vm_map(&ctx->uctx, va, total_size, perm, 0, m, 0); 771 mobj_put(m); 772 if (res) 773 return res; 774 775 *mapped_size = total_size; 776 777 switch (sp_ffa_version) { 778 case MAKE_FFA_VERSION(1, 0): 779 fill_boot_info_1_0(*va, fdt); 780 break; 781 case MAKE_FFA_VERSION(1, 1): 782 fill_boot_info_1_1(*va, fdt); 783 break; 784 default: 785 EMSG("Unknown FF-A version: %#"PRIx32, sp_ffa_version); 786 return TEE_ERROR_NOT_SUPPORTED; 787 } 788 789 res = sp_dt_get_u32(fdt, 0, "gp-register-num", &info_reg); 790 if (res) { 791 if (res == TEE_ERROR_ITEM_NOT_FOUND) { 792 /* If the property is not present, set default to x0 */ 793 info_reg = 0; 794 } else { 795 return TEE_ERROR_BAD_FORMAT; 796 } 797 } 798 799 switch (info_reg) { 800 case 0: 801 args->a0 = *va; 802 break; 803 case 1: 804 args->a1 = *va; 805 break; 806 case 2: 807 args->a2 = *va; 808 break; 809 case 3: 810 args->a3 = *va; 811 break; 812 default: 813 EMSG("Invalid register selected for passing boot info"); 814 return TEE_ERROR_BAD_FORMAT; 815 } 816 817 return TEE_SUCCESS; 818 } 819 820 static TEE_Result handle_fdt_load_relative_mem_regions(struct sp_ctx *ctx, 821 const void *fdt) 822 { 823 int node = 0; 824 int subnode = 0; 825 tee_mm_entry_t *mm = NULL; 826 TEE_Result res = TEE_SUCCESS; 827 828 /* 829 * Memory regions are optional in the SP manifest, it's not an error if 830 * we don't find any. 831 */ 832 node = fdt_node_offset_by_compatible(fdt, 0, 833 "arm,ffa-manifest-memory-regions"); 834 if (node < 0) 835 return TEE_SUCCESS; 836 837 fdt_for_each_subnode(subnode, fdt, node) { 838 uint64_t load_rel_offset = 0; 839 uint32_t attributes = 0; 840 uint64_t base_addr = 0; 841 uint32_t pages_cnt = 0; 842 uint32_t flags = 0; 843 uint32_t perm = 0; 844 size_t size = 0; 845 vaddr_t va = 0; 846 847 mm = NULL; 848 849 /* Load address relative offset of a memory region */ 850 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 851 &load_rel_offset)) { 852 va = ctx->uctx.load_addr + load_rel_offset; 853 } else { 854 /* Skip non load address relative memory regions */ 855 continue; 856 } 857 858 if (!sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 859 EMSG("Both base-address and load-address-relative-offset fields are set"); 860 return TEE_ERROR_BAD_FORMAT; 861 } 862 863 /* Size of memory region as count of 4K pages */ 864 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 865 EMSG("Mandatory field is missing: pages-count"); 866 return TEE_ERROR_BAD_FORMAT; 867 } 868 869 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 870 return TEE_ERROR_OVERFLOW; 871 872 /* Memory region attributes */ 873 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 874 EMSG("Mandatory field is missing: attributes"); 875 return TEE_ERROR_BAD_FORMAT; 876 } 877 878 /* Check instruction and data access permissions */ 879 switch (attributes & SP_MANIFEST_ATTR_RWX) { 880 case SP_MANIFEST_ATTR_RO: 881 perm = TEE_MATTR_UR; 882 break; 883 case SP_MANIFEST_ATTR_RW: 884 perm = TEE_MATTR_URW; 885 break; 886 case SP_MANIFEST_ATTR_RX: 887 perm = TEE_MATTR_URX; 888 break; 889 default: 890 EMSG("Invalid memory access permissions"); 891 return TEE_ERROR_BAD_FORMAT; 892 } 893 894 res = sp_dt_get_u32(fdt, subnode, "load-flags", &flags); 895 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) { 896 EMSG("Optional field with invalid value: flags"); 897 return TEE_ERROR_BAD_FORMAT; 898 } 899 900 /* Load relative regions must be secure */ 901 if (attributes & SP_MANIFEST_ATTR_NSEC) { 902 EMSG("Invalid memory security attribute"); 903 return TEE_ERROR_BAD_FORMAT; 904 } 905 906 if (flags & SP_MANIFEST_FLAG_NOBITS) { 907 /* 908 * NOBITS flag is set, which means that loaded binary 909 * doesn't contain this area, so it's need to be 910 * allocated. 911 */ 912 struct mobj *m = NULL; 913 unsigned int idx = 0; 914 915 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 916 if (!mm) 917 return TEE_ERROR_OUT_OF_MEMORY; 918 919 base_addr = tee_mm_get_smem(mm); 920 921 m = sp_mem_new_mobj(pages_cnt, 922 TEE_MATTR_MEM_TYPE_CACHED, true); 923 if (!m) { 924 res = TEE_ERROR_OUT_OF_MEMORY; 925 goto err_mm_free; 926 } 927 928 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 929 if (res) { 930 mobj_put(m); 931 goto err_mm_free; 932 } 933 934 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 935 mobj_put(m); 936 if (res) 937 goto err_mm_free; 938 } else { 939 /* 940 * If NOBITS is not present the memory area is already 941 * mapped and only need to set the correct permissions. 942 */ 943 res = vm_set_prot(&ctx->uctx, va, size, perm); 944 if (res) 945 return res; 946 } 947 } 948 949 return TEE_SUCCESS; 950 951 err_mm_free: 952 tee_mm_free(mm); 953 return res; 954 } 955 956 static TEE_Result handle_fdt_dev_regions(struct sp_ctx *ctx, void *fdt) 957 { 958 int node = 0; 959 int subnode = 0; 960 TEE_Result res = TEE_SUCCESS; 961 const char *dt_device_match_table = { 962 "arm,ffa-manifest-device-regions", 963 }; 964 965 /* 966 * Device regions are optional in the SP manifest, it's not an error if 967 * we don't find any 968 */ 969 node = fdt_node_offset_by_compatible(fdt, 0, dt_device_match_table); 970 if (node < 0) 971 return TEE_SUCCESS; 972 973 fdt_for_each_subnode(subnode, fdt, node) { 974 uint64_t base_addr = 0; 975 uint32_t pages_cnt = 0; 976 uint32_t attributes = 0; 977 struct mobj *m = NULL; 978 bool is_secure = true; 979 uint32_t perm = 0; 980 vaddr_t va = 0; 981 unsigned int idx = 0; 982 983 /* 984 * Physical base address of a device MMIO region. 985 * Currently only physically contiguous region is supported. 986 */ 987 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 988 EMSG("Mandatory field is missing: base-address"); 989 return TEE_ERROR_BAD_FORMAT; 990 } 991 992 /* Total size of MMIO region as count of 4K pages */ 993 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 994 EMSG("Mandatory field is missing: pages-count"); 995 return TEE_ERROR_BAD_FORMAT; 996 } 997 998 /* Data access, instruction access and security attributes */ 999 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 1000 EMSG("Mandatory field is missing: attributes"); 1001 return TEE_ERROR_BAD_FORMAT; 1002 } 1003 1004 /* Check instruction and data access permissions */ 1005 switch (attributes & SP_MANIFEST_ATTR_RWX) { 1006 case SP_MANIFEST_ATTR_RO: 1007 perm = TEE_MATTR_UR; 1008 break; 1009 case SP_MANIFEST_ATTR_RW: 1010 perm = TEE_MATTR_URW; 1011 break; 1012 default: 1013 EMSG("Invalid memory access permissions"); 1014 return TEE_ERROR_BAD_FORMAT; 1015 } 1016 1017 /* 1018 * The SP is a secure endpoint, security attribute can be 1019 * secure or non-secure 1020 */ 1021 if (attributes & SP_MANIFEST_ATTR_NSEC) 1022 is_secure = false; 1023 1024 /* Memory attributes must be Device-nGnRnE */ 1025 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_STRONGLY_O, 1026 is_secure); 1027 if (!m) 1028 return TEE_ERROR_OUT_OF_MEMORY; 1029 1030 res = sp_mem_add_pages(m, &idx, (paddr_t)base_addr, pages_cnt); 1031 if (res) { 1032 mobj_put(m); 1033 return res; 1034 } 1035 1036 res = vm_map(&ctx->uctx, &va, pages_cnt * SMALL_PAGE_SIZE, 1037 perm, 0, m, 0); 1038 mobj_put(m); 1039 if (res) 1040 return res; 1041 1042 /* 1043 * Overwrite the device region's PA in the fdt with the VA. This 1044 * fdt will be passed to the SP. 1045 */ 1046 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1047 1048 /* 1049 * Unmap the region if the overwrite failed since the SP won't 1050 * be able to access it without knowing the VA. 1051 */ 1052 if (res) { 1053 vm_unmap(&ctx->uctx, va, pages_cnt * SMALL_PAGE_SIZE); 1054 return res; 1055 } 1056 } 1057 1058 return TEE_SUCCESS; 1059 } 1060 1061 static TEE_Result swap_sp_endpoints(uint32_t endpoint_id, 1062 uint32_t new_endpoint_id) 1063 { 1064 struct sp_session *session = sp_get_session(endpoint_id); 1065 uint32_t manifest_endpoint_id = 0; 1066 1067 /* 1068 * We don't know in which order the SPs are loaded. The endpoint ID 1069 * defined in the manifest could already be generated by 1070 * new_session_id() and used by another SP. If this is the case, we swap 1071 * the ID's of the two SPs. We also have to make sure that the ID's are 1072 * not defined twice in the manifest. 1073 */ 1074 1075 /* The endpoint ID was not assigned yet */ 1076 if (!session) 1077 return TEE_SUCCESS; 1078 1079 /* 1080 * Read the manifest file from the SP who originally had the endpoint. 1081 * We can safely swap the endpoint ID's if the manifest file doesn't 1082 * have an endpoint ID defined. 1083 */ 1084 if (!sp_dt_get_u32(session->fdt, 0, "id", &manifest_endpoint_id)) { 1085 assert(manifest_endpoint_id == endpoint_id); 1086 EMSG("SP: Found duplicated endpoint ID %#"PRIx32, endpoint_id); 1087 return TEE_ERROR_ACCESS_CONFLICT; 1088 } 1089 1090 session->endpoint_id = new_endpoint_id; 1091 1092 return TEE_SUCCESS; 1093 } 1094 1095 static TEE_Result read_manifest_endpoint_id(struct sp_session *s) 1096 { 1097 uint32_t endpoint_id = 0; 1098 1099 /* 1100 * The endpoint ID can be optionally defined in the manifest file. We 1101 * have to map the ID inside the manifest to the SP if it's defined. 1102 * If not, the endpoint ID generated inside new_session_id() will be 1103 * used. 1104 */ 1105 if (!sp_dt_get_u32(s->fdt, 0, "id", &endpoint_id)) { 1106 TEE_Result res = TEE_ERROR_GENERIC; 1107 1108 if (!endpoint_id_is_valid(endpoint_id)) { 1109 EMSG("Invalid endpoint ID 0x%"PRIx32, endpoint_id); 1110 return TEE_ERROR_BAD_FORMAT; 1111 } 1112 1113 res = swap_sp_endpoints(endpoint_id, s->endpoint_id); 1114 if (res) 1115 return res; 1116 1117 DMSG("SP: endpoint ID (0x%"PRIx32") found in manifest", 1118 endpoint_id); 1119 /* Assign the endpoint ID to the current SP */ 1120 s->endpoint_id = endpoint_id; 1121 } 1122 return TEE_SUCCESS; 1123 } 1124 1125 static TEE_Result handle_fdt_mem_regions(struct sp_ctx *ctx, void *fdt) 1126 { 1127 int node = 0; 1128 int subnode = 0; 1129 tee_mm_entry_t *mm = NULL; 1130 TEE_Result res = TEE_SUCCESS; 1131 1132 /* 1133 * Memory regions are optional in the SP manifest, it's not an error if 1134 * we don't find any. 1135 */ 1136 node = fdt_node_offset_by_compatible(fdt, 0, 1137 "arm,ffa-manifest-memory-regions"); 1138 if (node < 0) 1139 return TEE_SUCCESS; 1140 1141 fdt_for_each_subnode(subnode, fdt, node) { 1142 uint64_t load_rel_offset = 0; 1143 bool alloc_needed = false; 1144 uint32_t attributes = 0; 1145 uint64_t base_addr = 0; 1146 uint32_t pages_cnt = 0; 1147 bool is_secure = true; 1148 struct mobj *m = NULL; 1149 unsigned int idx = 0; 1150 uint32_t perm = 0; 1151 size_t size = 0; 1152 vaddr_t va = 0; 1153 1154 mm = NULL; 1155 1156 /* Load address relative offset of a memory region */ 1157 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 1158 &load_rel_offset)) { 1159 /* 1160 * At this point the memory region is already mapped by 1161 * handle_fdt_load_relative_mem_regions. 1162 * Only need to set the base-address in the manifest and 1163 * then skip the rest of the mapping process. 1164 */ 1165 va = ctx->uctx.load_addr + load_rel_offset; 1166 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1167 if (res) 1168 return res; 1169 1170 continue; 1171 } 1172 1173 /* 1174 * Base address of a memory region. 1175 * If not present, we have to allocate the specified memory. 1176 * If present, this field could specify a PA or VA. Currently 1177 * only a PA is supported. 1178 */ 1179 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) 1180 alloc_needed = true; 1181 1182 /* Size of memory region as count of 4K pages */ 1183 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 1184 EMSG("Mandatory field is missing: pages-count"); 1185 return TEE_ERROR_BAD_FORMAT; 1186 } 1187 1188 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 1189 return TEE_ERROR_OVERFLOW; 1190 1191 /* 1192 * Memory region attributes: 1193 * - Instruction/data access permissions 1194 * - Cacheability/shareability attributes 1195 * - Security attributes 1196 * 1197 * Cacheability/shareability attributes can be ignored for now. 1198 * OP-TEE only supports a single type for normal cached memory 1199 * and currently there is no use case that would require to 1200 * change this. 1201 */ 1202 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 1203 EMSG("Mandatory field is missing: attributes"); 1204 return TEE_ERROR_BAD_FORMAT; 1205 } 1206 1207 /* Check instruction and data access permissions */ 1208 switch (attributes & SP_MANIFEST_ATTR_RWX) { 1209 case SP_MANIFEST_ATTR_RO: 1210 perm = TEE_MATTR_UR; 1211 break; 1212 case SP_MANIFEST_ATTR_RW: 1213 perm = TEE_MATTR_URW; 1214 break; 1215 case SP_MANIFEST_ATTR_RX: 1216 perm = TEE_MATTR_URX; 1217 break; 1218 default: 1219 EMSG("Invalid memory access permissions"); 1220 return TEE_ERROR_BAD_FORMAT; 1221 } 1222 1223 /* 1224 * The SP is a secure endpoint, security attribute can be 1225 * secure or non-secure. 1226 * The SPMC cannot allocate non-secure memory, i.e. if the base 1227 * address is missing this attribute must be secure. 1228 */ 1229 if (attributes & SP_MANIFEST_ATTR_NSEC) { 1230 if (alloc_needed) { 1231 EMSG("Invalid memory security attribute"); 1232 return TEE_ERROR_BAD_FORMAT; 1233 } 1234 is_secure = false; 1235 } 1236 1237 if (alloc_needed) { 1238 /* Base address is missing, we have to allocate */ 1239 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 1240 if (!mm) 1241 return TEE_ERROR_OUT_OF_MEMORY; 1242 1243 base_addr = tee_mm_get_smem(mm); 1244 } 1245 1246 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_CACHED, 1247 is_secure); 1248 if (!m) { 1249 res = TEE_ERROR_OUT_OF_MEMORY; 1250 goto err_mm_free; 1251 } 1252 1253 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 1254 if (res) { 1255 mobj_put(m); 1256 goto err_mm_free; 1257 } 1258 1259 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 1260 mobj_put(m); 1261 if (res) 1262 goto err_mm_free; 1263 1264 /* 1265 * Overwrite the memory region's base address in the fdt with 1266 * the VA. This fdt will be passed to the SP. 1267 * If the base-address field was not present in the original 1268 * fdt, this function will create it. This doesn't cause issues 1269 * since the necessary extra space has been allocated when 1270 * opening the fdt. 1271 */ 1272 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1273 1274 /* 1275 * Unmap the region if the overwrite failed since the SP won't 1276 * be able to access it without knowing the VA. 1277 */ 1278 if (res) { 1279 vm_unmap(&ctx->uctx, va, size); 1280 goto err_mm_free; 1281 } 1282 } 1283 1284 return TEE_SUCCESS; 1285 1286 err_mm_free: 1287 tee_mm_free(mm); 1288 return res; 1289 } 1290 1291 static TEE_Result handle_tpm_event_log(struct sp_ctx *ctx, void *fdt) 1292 { 1293 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 1294 uint32_t dummy_size __maybe_unused = 0; 1295 TEE_Result res = TEE_SUCCESS; 1296 size_t page_count = 0; 1297 struct fobj *f = NULL; 1298 struct mobj *m = NULL; 1299 vaddr_t log_addr = 0; 1300 size_t log_size = 0; 1301 int node = 0; 1302 1303 node = fdt_node_offset_by_compatible(fdt, 0, "arm,tpm_event_log"); 1304 if (node < 0) 1305 return TEE_SUCCESS; 1306 1307 /* Checking the existence and size of the event log properties */ 1308 if (sp_dt_get_u64(fdt, node, "tpm_event_log_addr", &log_addr)) { 1309 EMSG("tpm_event_log_addr not found or has invalid size"); 1310 return TEE_ERROR_BAD_FORMAT; 1311 } 1312 1313 if (sp_dt_get_u32(fdt, node, "tpm_event_log_size", &dummy_size)) { 1314 EMSG("tpm_event_log_size not found or has invalid size"); 1315 return TEE_ERROR_BAD_FORMAT; 1316 } 1317 1318 /* Validating event log */ 1319 res = tpm_get_event_log_size(&log_size); 1320 if (res) 1321 return res; 1322 1323 if (!log_size) { 1324 EMSG("Empty TPM event log was provided"); 1325 return TEE_ERROR_ITEM_NOT_FOUND; 1326 } 1327 1328 /* Allocating memory area for the event log to share with the SP */ 1329 page_count = ROUNDUP_DIV(log_size, SMALL_PAGE_SIZE); 1330 1331 f = fobj_sec_mem_alloc(page_count); 1332 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 1333 fobj_put(f); 1334 if (!m) 1335 return TEE_ERROR_OUT_OF_MEMORY; 1336 1337 res = vm_map(&ctx->uctx, &log_addr, log_size, perm, 0, m, 0); 1338 mobj_put(m); 1339 if (res) 1340 return res; 1341 1342 /* Copy event log */ 1343 res = tpm_get_event_log((void *)log_addr, &log_size); 1344 if (res) 1345 goto err_unmap; 1346 1347 /* Setting event log details in the manifest */ 1348 res = fdt_setprop_u64(fdt, node, "tpm_event_log_addr", log_addr); 1349 if (res) 1350 goto err_unmap; 1351 1352 res = fdt_setprop_u32(fdt, node, "tpm_event_log_size", log_size); 1353 if (res) 1354 goto err_unmap; 1355 1356 return TEE_SUCCESS; 1357 1358 err_unmap: 1359 vm_unmap(&ctx->uctx, log_addr, log_size); 1360 1361 return res; 1362 } 1363 1364 /* 1365 * Note: this function is called only on the primary CPU. It assumes that the 1366 * features present on the primary CPU are available on all of the secondary 1367 * CPUs as well. 1368 */ 1369 static TEE_Result handle_hw_features(void *fdt) 1370 { 1371 uint32_t val __maybe_unused = 0; 1372 TEE_Result res = TEE_SUCCESS; 1373 int node = 0; 1374 1375 /* 1376 * HW feature descriptions are optional in the SP manifest, it's not an 1377 * error if we don't find any. 1378 */ 1379 node = fdt_node_offset_by_compatible(fdt, 0, "arm,hw-features"); 1380 if (node < 0) 1381 return TEE_SUCCESS; 1382 1383 /* Modify the crc32 property only if it's already present */ 1384 if (!sp_dt_get_u32(fdt, node, "crc32", &val)) { 1385 res = fdt_setprop_u32(fdt, node, "crc32", 1386 feat_crc32_implemented()); 1387 if (res) 1388 return res; 1389 } 1390 1391 return TEE_SUCCESS; 1392 } 1393 1394 static TEE_Result read_ns_interrupts_action(const void *fdt, 1395 struct sp_session *s) 1396 { 1397 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1398 1399 res = sp_dt_get_u32(fdt, 0, "ns-interrupts-action", &s->ns_int_mode); 1400 1401 if (res) { 1402 EMSG("Mandatory property is missing: ns-interrupts-action"); 1403 return res; 1404 } 1405 1406 switch (s->ns_int_mode) { 1407 case SP_MANIFEST_NS_INT_QUEUED: 1408 case SP_MANIFEST_NS_INT_SIGNALED: 1409 /* OK */ 1410 break; 1411 1412 case SP_MANIFEST_NS_INT_MANAGED_EXIT: 1413 EMSG("Managed exit is not implemented"); 1414 return TEE_ERROR_NOT_IMPLEMENTED; 1415 1416 default: 1417 EMSG("Invalid ns-interrupts-action value: %"PRIu32, 1418 s->ns_int_mode); 1419 return TEE_ERROR_BAD_PARAMETERS; 1420 } 1421 1422 return TEE_SUCCESS; 1423 } 1424 1425 static TEE_Result read_ffa_version(const void *fdt, struct sp_session *s) 1426 { 1427 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1428 uint32_t ffa_version = 0; 1429 1430 res = sp_dt_get_u32(fdt, 0, "ffa-version", &ffa_version); 1431 if (res) { 1432 EMSG("Mandatory property is missing: ffa-version"); 1433 return res; 1434 } 1435 1436 if (ffa_version != FFA_VERSION_1_0 && ffa_version != FFA_VERSION_1_1) { 1437 EMSG("Invalid FF-A version value: 0x%08"PRIx32, ffa_version); 1438 return TEE_ERROR_BAD_PARAMETERS; 1439 } 1440 1441 s->rxtx.ffa_vers = ffa_version; 1442 1443 return TEE_SUCCESS; 1444 } 1445 1446 static TEE_Result read_sp_exec_state(const void *fdt, struct sp_session *s) 1447 { 1448 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1449 uint32_t exec_state = 0; 1450 1451 res = sp_dt_get_u32(fdt, 0, "execution-state", &exec_state); 1452 if (res) { 1453 EMSG("Mandatory property is missing: execution-state"); 1454 return res; 1455 } 1456 1457 /* Currently only AArch64 SPs are supported */ 1458 if (exec_state == SP_MANIFEST_EXEC_STATE_AARCH64) { 1459 s->props |= FFA_PART_PROP_AARCH64_STATE; 1460 } else { 1461 EMSG("Invalid execution-state value: %"PRIu32, exec_state); 1462 return TEE_ERROR_BAD_PARAMETERS; 1463 } 1464 1465 return TEE_SUCCESS; 1466 } 1467 1468 static TEE_Result read_sp_msg_types(const void *fdt, struct sp_session *s) 1469 { 1470 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1471 uint32_t msg_method = 0; 1472 1473 res = sp_dt_get_u32(fdt, 0, "messaging-method", &msg_method); 1474 if (res) { 1475 EMSG("Mandatory property is missing: messaging-method"); 1476 return res; 1477 } 1478 1479 if (msg_method & SP_MANIFEST_DIRECT_REQ_RECEIVE) 1480 s->props |= FFA_PART_PROP_DIRECT_REQ_RECV; 1481 1482 if (msg_method & SP_MANIFEST_DIRECT_REQ_SEND) 1483 s->props |= FFA_PART_PROP_DIRECT_REQ_SEND; 1484 1485 if (msg_method & SP_MANIFEST_INDIRECT_REQ) 1486 IMSG("Indirect messaging is not supported"); 1487 1488 return TEE_SUCCESS; 1489 } 1490 1491 static TEE_Result read_vm_availability_msg(const void *fdt, 1492 struct sp_session *s) 1493 { 1494 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1495 uint32_t v = 0; 1496 1497 res = sp_dt_get_u32(fdt, 0, "vm-availability-messages", &v); 1498 1499 /* This field in the manifest is optional */ 1500 if (res == TEE_ERROR_ITEM_NOT_FOUND) 1501 return TEE_SUCCESS; 1502 1503 if (res) 1504 return res; 1505 1506 if (v & ~(SP_MANIFEST_VM_CREATED_MSG | SP_MANIFEST_VM_DESTROYED_MSG)) { 1507 EMSG("Invalid vm-availability-messages value: %"PRIu32, v); 1508 return TEE_ERROR_BAD_PARAMETERS; 1509 } 1510 1511 if (v & SP_MANIFEST_VM_CREATED_MSG) 1512 s->props |= FFA_PART_PROP_NOTIF_CREATED; 1513 1514 if (v & SP_MANIFEST_VM_DESTROYED_MSG) 1515 s->props |= FFA_PART_PROP_NOTIF_DESTROYED; 1516 1517 return TEE_SUCCESS; 1518 } 1519 1520 static TEE_Result sp_init_uuid(const TEE_UUID *bin_uuid, const void * const fdt) 1521 { 1522 TEE_Result res = TEE_SUCCESS; 1523 struct sp_session *sess = NULL; 1524 TEE_UUID ffa_uuid = {}; 1525 uint16_t boot_order = 0; 1526 uint32_t boot_order_arg = 0; 1527 1528 res = fdt_get_uuid(fdt, &ffa_uuid); 1529 if (res) 1530 return res; 1531 1532 res = sp_dt_get_u16(fdt, 0, "boot-order", &boot_order); 1533 if (res == TEE_SUCCESS) { 1534 boot_order_arg = boot_order; 1535 } else if (res == TEE_ERROR_ITEM_NOT_FOUND) { 1536 boot_order_arg = UINT32_MAX; 1537 } else { 1538 EMSG("Failed reading boot-order property err:%#"PRIx32, res); 1539 return res; 1540 } 1541 1542 res = sp_open_session(&sess, 1543 &open_sp_sessions, 1544 &ffa_uuid, bin_uuid, boot_order_arg, fdt); 1545 if (res) 1546 return res; 1547 1548 sess->fdt = fdt; 1549 1550 res = read_manifest_endpoint_id(sess); 1551 if (res) 1552 return res; 1553 DMSG("endpoint is 0x%"PRIx16, sess->endpoint_id); 1554 1555 res = read_ns_interrupts_action(fdt, sess); 1556 if (res) 1557 return res; 1558 1559 res = read_ffa_version(fdt, sess); 1560 if (res) 1561 return res; 1562 1563 res = read_sp_exec_state(fdt, sess); 1564 if (res) 1565 return res; 1566 1567 res = read_sp_msg_types(fdt, sess); 1568 if (res) 1569 return res; 1570 1571 res = read_vm_availability_msg(fdt, sess); 1572 if (res) 1573 return res; 1574 1575 return TEE_SUCCESS; 1576 } 1577 1578 static TEE_Result sp_first_run(struct sp_session *sess) 1579 { 1580 TEE_Result res = TEE_SUCCESS; 1581 struct thread_smc_args args = { }; 1582 struct sp_ctx *ctx = NULL; 1583 vaddr_t boot_info_va = 0; 1584 size_t boot_info_size = 0; 1585 void *fdt_copy = NULL; 1586 size_t fdt_size = 0; 1587 1588 ctx = to_sp_ctx(sess->ts_sess.ctx); 1589 ts_push_current_session(&sess->ts_sess); 1590 sess->is_initialized = false; 1591 1592 /* 1593 * Load relative memory regions must be handled before doing any other 1594 * mapping to prevent conflicts in the VA space. 1595 */ 1596 res = handle_fdt_load_relative_mem_regions(ctx, sess->fdt); 1597 if (res) { 1598 ts_pop_current_session(); 1599 return res; 1600 } 1601 1602 res = copy_and_map_fdt(ctx, sess->fdt, &fdt_copy, &fdt_size); 1603 if (res) 1604 goto out; 1605 1606 res = handle_fdt_dev_regions(ctx, fdt_copy); 1607 if (res) 1608 goto out; 1609 1610 res = handle_fdt_mem_regions(ctx, fdt_copy); 1611 if (res) 1612 goto out; 1613 1614 if (IS_ENABLED(CFG_CORE_TPM_EVENT_LOG)) { 1615 res = handle_tpm_event_log(ctx, fdt_copy); 1616 if (res) 1617 goto out; 1618 } 1619 1620 res = handle_hw_features(fdt_copy); 1621 if (res) 1622 goto out; 1623 1624 res = create_and_map_boot_info(ctx, fdt_copy, &args, &boot_info_va, 1625 &boot_info_size, sess->rxtx.ffa_vers); 1626 if (res) 1627 goto out; 1628 1629 ts_pop_current_session(); 1630 1631 res = sp_enter(&args, sess); 1632 if (res) { 1633 ts_push_current_session(&sess->ts_sess); 1634 goto out; 1635 } 1636 1637 spmc_sp_msg_handler(&args, sess); 1638 1639 ts_push_current_session(&sess->ts_sess); 1640 sess->is_initialized = true; 1641 1642 out: 1643 /* Free the boot info page from the SP memory */ 1644 vm_unmap(&ctx->uctx, boot_info_va, boot_info_size); 1645 vm_unmap(&ctx->uctx, (vaddr_t)fdt_copy, fdt_size); 1646 ts_pop_current_session(); 1647 1648 return res; 1649 } 1650 1651 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp) 1652 { 1653 TEE_Result res = TEE_SUCCESS; 1654 struct sp_ctx *ctx = to_sp_ctx(sp->ts_sess.ctx); 1655 1656 ctx->sp_regs.x[0] = args->a0; 1657 ctx->sp_regs.x[1] = args->a1; 1658 ctx->sp_regs.x[2] = args->a2; 1659 ctx->sp_regs.x[3] = args->a3; 1660 ctx->sp_regs.x[4] = args->a4; 1661 ctx->sp_regs.x[5] = args->a5; 1662 ctx->sp_regs.x[6] = args->a6; 1663 ctx->sp_regs.x[7] = args->a7; 1664 1665 res = sp->ts_sess.ctx->ops->enter_invoke_cmd(&sp->ts_sess, 0); 1666 1667 args->a0 = ctx->sp_regs.x[0]; 1668 args->a1 = ctx->sp_regs.x[1]; 1669 args->a2 = ctx->sp_regs.x[2]; 1670 args->a3 = ctx->sp_regs.x[3]; 1671 args->a4 = ctx->sp_regs.x[4]; 1672 args->a5 = ctx->sp_regs.x[5]; 1673 args->a6 = ctx->sp_regs.x[6]; 1674 args->a7 = ctx->sp_regs.x[7]; 1675 1676 return res; 1677 } 1678 1679 /* 1680 * According to FF-A v1.1 section 8.3.1.4 if a caller requires less permissive 1681 * active on NS interrupt than the callee, the callee must inherit the caller's 1682 * configuration. 1683 * Each SP's own NS action setting is stored in ns_int_mode. The effective 1684 * action will be MIN([self action], [caller's action]) which is stored in the 1685 * ns_int_mode_inherited field. 1686 */ 1687 static void sp_cpsr_configure_foreign_interrupts(struct sp_session *s, 1688 struct ts_session *caller, 1689 uint64_t *cpsr) 1690 { 1691 if (caller) { 1692 struct sp_session *caller_sp = to_sp_session(caller); 1693 1694 s->ns_int_mode_inherited = MIN(caller_sp->ns_int_mode_inherited, 1695 s->ns_int_mode); 1696 } else { 1697 s->ns_int_mode_inherited = s->ns_int_mode; 1698 } 1699 1700 if (s->ns_int_mode_inherited == SP_MANIFEST_NS_INT_QUEUED) 1701 *cpsr |= SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1702 ARM32_CPSR_F_SHIFT); 1703 else 1704 *cpsr &= ~SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1705 ARM32_CPSR_F_SHIFT); 1706 } 1707 1708 static TEE_Result sp_enter_invoke_cmd(struct ts_session *s, 1709 uint32_t cmd __unused) 1710 { 1711 struct sp_ctx *ctx = to_sp_ctx(s->ctx); 1712 TEE_Result res = TEE_SUCCESS; 1713 uint32_t exceptions = 0; 1714 struct sp_session *sp_s = to_sp_session(s); 1715 struct ts_session *sess = NULL; 1716 struct thread_ctx_regs *sp_regs = NULL; 1717 uint32_t thread_id = THREAD_ID_INVALID; 1718 struct ts_session *caller = NULL; 1719 uint32_t rpc_target_info = 0; 1720 uint32_t panicked = false; 1721 uint32_t panic_code = 0; 1722 1723 sp_regs = &ctx->sp_regs; 1724 ts_push_current_session(s); 1725 1726 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 1727 1728 /* Enable/disable foreign interrupts in CPSR/SPSR */ 1729 caller = ts_get_calling_session(); 1730 sp_cpsr_configure_foreign_interrupts(sp_s, caller, &sp_regs->cpsr); 1731 1732 /* 1733 * Store endpoint ID and thread ID in rpc_target_info. This will be used 1734 * as w1 in FFA_INTERRUPT in case of a foreign interrupt. 1735 */ 1736 rpc_target_info = thread_get_tsd()->rpc_target_info; 1737 thread_id = thread_get_id(); 1738 assert(thread_id <= UINT16_MAX); 1739 thread_get_tsd()->rpc_target_info = 1740 FFA_TARGET_INFO_SET(sp_s->endpoint_id, thread_id); 1741 1742 __thread_enter_user_mode(sp_regs, &panicked, &panic_code); 1743 1744 /* Restore rpc_target_info */ 1745 thread_get_tsd()->rpc_target_info = rpc_target_info; 1746 1747 thread_unmask_exceptions(exceptions); 1748 1749 thread_user_clear_vfp(&ctx->uctx); 1750 1751 if (panicked) { 1752 DMSG("SP panicked with code %#"PRIx32, panic_code); 1753 abort_print_current_ts(); 1754 1755 sess = ts_pop_current_session(); 1756 cpu_spin_lock(&sp_s->spinlock); 1757 sp_s->state = sp_dead; 1758 cpu_spin_unlock(&sp_s->spinlock); 1759 1760 return TEE_ERROR_TARGET_DEAD; 1761 } 1762 1763 sess = ts_pop_current_session(); 1764 assert(sess == s); 1765 1766 return res; 1767 } 1768 1769 /* We currently don't support 32 bits */ 1770 #ifdef ARM64 1771 static void sp_svc_store_registers(struct thread_scall_regs *regs, 1772 struct thread_ctx_regs *sp_regs) 1773 { 1774 COMPILE_TIME_ASSERT(sizeof(sp_regs->x[0]) == sizeof(regs->x0)); 1775 memcpy(sp_regs->x, ®s->x0, 31 * sizeof(regs->x0)); 1776 sp_regs->pc = regs->elr; 1777 sp_regs->sp = regs->sp_el0; 1778 } 1779 #endif 1780 1781 static bool sp_handle_scall(struct thread_scall_regs *regs) 1782 { 1783 struct ts_session *ts = ts_get_current_session(); 1784 struct sp_ctx *uctx = to_sp_ctx(ts->ctx); 1785 struct sp_session *s = uctx->open_session; 1786 1787 assert(s); 1788 1789 sp_svc_store_registers(regs, &uctx->sp_regs); 1790 1791 regs->x0 = 0; 1792 regs->x1 = 0; /* panic */ 1793 regs->x2 = 0; /* panic code */ 1794 1795 /* 1796 * All the registers of the SP are saved in the SP session by the SVC 1797 * handler. 1798 * We always return to S-El1 after handling the SVC. We will continue 1799 * in sp_enter_invoke_cmd() (return from __thread_enter_user_mode). 1800 * The sp_enter() function copies the FF-A parameters (a0-a7) from the 1801 * saved registers to the thread_smc_args. The thread_smc_args object is 1802 * afterward used by the spmc_sp_msg_handler() to handle the 1803 * FF-A message send by the SP. 1804 */ 1805 return false; 1806 } 1807 1808 static void sp_dump_state(struct ts_ctx *ctx) 1809 { 1810 struct sp_ctx *utc = to_sp_ctx(ctx); 1811 1812 if (utc->uctx.dump_entry_func) { 1813 TEE_Result res = ldelf_dump_state(&utc->uctx); 1814 1815 if (!res || res == TEE_ERROR_TARGET_DEAD) 1816 return; 1817 } 1818 1819 user_mode_ctx_print_mappings(&utc->uctx); 1820 } 1821 1822 static const struct ts_ops sp_ops = { 1823 .enter_invoke_cmd = sp_enter_invoke_cmd, 1824 .handle_scall = sp_handle_scall, 1825 .dump_state = sp_dump_state, 1826 }; 1827 1828 static TEE_Result process_sp_pkg(uint64_t sp_pkg_pa, TEE_UUID *sp_uuid) 1829 { 1830 enum teecore_memtypes mtype = MEM_AREA_TA_RAM; 1831 struct sp_pkg_header *sp_pkg_hdr = NULL; 1832 struct fip_sp *sp = NULL; 1833 uint64_t sp_fdt_end = 0; 1834 size_t sp_pkg_size = 0; 1835 vaddr_t sp_pkg_va = 0; 1836 1837 /* Process the first page which contains the SP package header */ 1838 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, SMALL_PAGE_SIZE); 1839 if (!sp_pkg_va) { 1840 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1841 return TEE_ERROR_GENERIC; 1842 } 1843 1844 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1845 1846 if (sp_pkg_hdr->magic != SP_PKG_HEADER_MAGIC) { 1847 EMSG("Invalid SP package magic"); 1848 return TEE_ERROR_BAD_FORMAT; 1849 } 1850 1851 if (sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V1 && 1852 sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V2) { 1853 EMSG("Invalid SP header version"); 1854 return TEE_ERROR_BAD_FORMAT; 1855 } 1856 1857 if (ADD_OVERFLOW(sp_pkg_hdr->img_offset, sp_pkg_hdr->img_size, 1858 &sp_pkg_size)) { 1859 EMSG("Invalid SP package size"); 1860 return TEE_ERROR_BAD_FORMAT; 1861 } 1862 1863 if (ADD_OVERFLOW(sp_pkg_hdr->pm_offset, sp_pkg_hdr->pm_size, 1864 &sp_fdt_end) || sp_fdt_end > sp_pkg_hdr->img_offset) { 1865 EMSG("Invalid SP manifest size"); 1866 return TEE_ERROR_BAD_FORMAT; 1867 } 1868 1869 /* Process the whole SP package now that the size is known */ 1870 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, sp_pkg_size); 1871 if (!sp_pkg_va) { 1872 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1873 return TEE_ERROR_GENERIC; 1874 } 1875 1876 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1877 1878 sp = calloc(1, sizeof(struct fip_sp)); 1879 if (!sp) 1880 return TEE_ERROR_OUT_OF_MEMORY; 1881 1882 memcpy(&sp->sp_img.image.uuid, sp_uuid, sizeof(*sp_uuid)); 1883 sp->sp_img.image.ts = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->img_offset); 1884 sp->sp_img.image.size = sp_pkg_hdr->img_size; 1885 sp->sp_img.image.flags = 0; 1886 sp->sp_img.fdt = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->pm_offset); 1887 1888 STAILQ_INSERT_TAIL(&fip_sp_list, sp, link); 1889 1890 return TEE_SUCCESS; 1891 } 1892 1893 static TEE_Result fip_sp_init_all(void) 1894 { 1895 TEE_Result res = TEE_SUCCESS; 1896 uint64_t sp_pkg_addr = 0; 1897 const void *fdt = NULL; 1898 TEE_UUID sp_uuid = { }; 1899 int sp_pkgs_node = 0; 1900 int subnode = 0; 1901 int root = 0; 1902 1903 fdt = get_manifest_dt(); 1904 if (!fdt) { 1905 EMSG("No SPMC manifest found"); 1906 return TEE_ERROR_GENERIC; 1907 } 1908 1909 root = fdt_path_offset(fdt, "/"); 1910 if (root < 0) 1911 return TEE_ERROR_BAD_FORMAT; 1912 1913 if (fdt_node_check_compatible(fdt, root, "arm,ffa-core-manifest-1.0")) 1914 return TEE_ERROR_BAD_FORMAT; 1915 1916 /* SP packages are optional, it's not an error if we don't find any */ 1917 sp_pkgs_node = fdt_node_offset_by_compatible(fdt, root, "arm,sp_pkg"); 1918 if (sp_pkgs_node < 0) 1919 return TEE_SUCCESS; 1920 1921 fdt_for_each_subnode(subnode, fdt, sp_pkgs_node) { 1922 res = sp_dt_get_u64(fdt, subnode, "load-address", &sp_pkg_addr); 1923 if (res) { 1924 EMSG("Invalid FIP SP load address"); 1925 return res; 1926 } 1927 1928 res = sp_dt_get_uuid(fdt, subnode, "uuid", &sp_uuid); 1929 if (res) { 1930 EMSG("Invalid FIP SP uuid"); 1931 return res; 1932 } 1933 1934 res = process_sp_pkg(sp_pkg_addr, &sp_uuid); 1935 if (res) { 1936 EMSG("Invalid FIP SP package"); 1937 return res; 1938 } 1939 } 1940 1941 return TEE_SUCCESS; 1942 } 1943 1944 static void fip_sp_deinit_all(void) 1945 { 1946 while (!STAILQ_EMPTY(&fip_sp_list)) { 1947 struct fip_sp *sp = STAILQ_FIRST(&fip_sp_list); 1948 1949 STAILQ_REMOVE_HEAD(&fip_sp_list, link); 1950 free(sp); 1951 } 1952 } 1953 1954 static TEE_Result sp_init_all(void) 1955 { 1956 TEE_Result res = TEE_SUCCESS; 1957 const struct sp_image *sp = NULL; 1958 const struct fip_sp *fip_sp = NULL; 1959 char __maybe_unused msg[60] = { '\0', }; 1960 struct sp_session *s = NULL; 1961 struct sp_session *prev_sp = NULL; 1962 1963 for_each_secure_partition(sp) { 1964 if (sp->image.uncompressed_size) 1965 snprintf(msg, sizeof(msg), 1966 " (compressed, uncompressed %u)", 1967 sp->image.uncompressed_size); 1968 else 1969 msg[0] = '\0'; 1970 DMSG("SP %pUl size %u%s", (void *)&sp->image.uuid, 1971 sp->image.size, msg); 1972 1973 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1974 1975 if (res != TEE_SUCCESS) { 1976 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1977 &sp->image.uuid, res); 1978 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1979 panic(); 1980 } 1981 } 1982 1983 res = fip_sp_init_all(); 1984 if (res) 1985 panic("Failed initializing FIP SPs"); 1986 1987 for_each_fip_sp(fip_sp) { 1988 sp = &fip_sp->sp_img; 1989 1990 DMSG("SP %pUl size %u", (void *)&sp->image.uuid, 1991 sp->image.size); 1992 1993 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1994 1995 if (res != TEE_SUCCESS) { 1996 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1997 &sp->image.uuid, res); 1998 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1999 panic(); 2000 } 2001 } 2002 2003 /* 2004 * At this point all FIP SPs are loaded by ldelf or by the raw binary SP 2005 * loader, so the original images (loaded by BL2) are not needed anymore 2006 */ 2007 fip_sp_deinit_all(); 2008 2009 /* 2010 * Now that all SPs are loaded, check through the boot order values, 2011 * and warn in case there is a non-unique value. 2012 */ 2013 TAILQ_FOREACH(s, &open_sp_sessions, link) { 2014 /* User specified boot-order values are uint16 */ 2015 if (s->boot_order > UINT16_MAX) 2016 break; 2017 2018 if (prev_sp && prev_sp->boot_order == s->boot_order) 2019 IMSG("WARNING: duplicated boot-order (%pUl vs %pUl)", 2020 &prev_sp->ts_sess.ctx->uuid, 2021 &s->ts_sess.ctx->uuid); 2022 2023 prev_sp = s; 2024 } 2025 2026 /* Continue the initialization and run the SP */ 2027 TAILQ_FOREACH(s, &open_sp_sessions, link) { 2028 DMSG("Starting SP: 0x%"PRIx16, s->endpoint_id); 2029 2030 res = sp_first_run(s); 2031 if (res != TEE_SUCCESS) { 2032 EMSG("Failed starting SP(0x%"PRIx16") err:%#"PRIx32, 2033 s->endpoint_id, res); 2034 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 2035 panic(); 2036 } 2037 } 2038 2039 return TEE_SUCCESS; 2040 } 2041 2042 boot_final(sp_init_all); 2043 2044 static TEE_Result secure_partition_open(const TEE_UUID *uuid, 2045 struct ts_store_handle **h) 2046 { 2047 return emb_ts_open(uuid, h, find_secure_partition); 2048 } 2049 2050 REGISTER_SP_STORE(2) = { 2051 .description = "SP store", 2052 .open = secure_partition_open, 2053 .get_size = emb_ts_get_size, 2054 .get_tag = emb_ts_get_tag, 2055 .read = emb_ts_read, 2056 .close = emb_ts_close, 2057 }; 2058