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