1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2015-2023, Linaro Limited 4 * Copyright (c) 2023, Arm Limited 5 */ 6 7 #include <arm.h> 8 #include <assert.h> 9 #include <compiler.h> 10 #include <config.h> 11 #include <console.h> 12 #include <crypto/crypto.h> 13 #include <drivers/gic.h> 14 #include <dt-bindings/interrupt-controller/arm-gic.h> 15 #include <ffa.h> 16 #include <initcall.h> 17 #include <inttypes.h> 18 #include <io.h> 19 #include <keep.h> 20 #include <kernel/asan.h> 21 #include <kernel/boot.h> 22 #include <kernel/dt.h> 23 #include <kernel/linker.h> 24 #include <kernel/misc.h> 25 #include <kernel/panic.h> 26 #include <kernel/tee_misc.h> 27 #include <kernel/thread.h> 28 #include <kernel/tpm.h> 29 #include <kernel/transfer_list.h> 30 #include <libfdt.h> 31 #include <malloc.h> 32 #include <memtag.h> 33 #include <mm/core_memprot.h> 34 #include <mm/core_mmu.h> 35 #include <mm/fobj.h> 36 #include <mm/tee_mm.h> 37 #include <mm/tee_pager.h> 38 #include <sm/psci.h> 39 #include <trace.h> 40 #include <utee_defines.h> 41 #include <util.h> 42 43 #include <platform_config.h> 44 45 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 46 #include <sm/sm.h> 47 #endif 48 49 #if defined(CFG_WITH_VFP) 50 #include <kernel/vfp.h> 51 #endif 52 53 /* 54 * In this file we're using unsigned long to represent physical pointers as 55 * they are received in a single register when OP-TEE is initially entered. 56 * This limits 32-bit systems to only use make use of the lower 32 bits 57 * of a physical address for initial parameters. 58 * 59 * 64-bit systems on the other hand can use full 64-bit physical pointers. 60 */ 61 #define PADDR_INVALID ULONG_MAX 62 63 #if defined(CFG_BOOT_SECONDARY_REQUEST) 64 struct ns_entry_context { 65 uintptr_t entry_point; 66 uintptr_t context_id; 67 }; 68 struct ns_entry_context ns_entry_contexts[CFG_TEE_CORE_NB_CORE]; 69 static uint32_t spin_table[CFG_TEE_CORE_NB_CORE]; 70 #endif 71 72 #ifdef CFG_BOOT_SYNC_CPU 73 /* 74 * Array used when booting, to synchronize cpu. 75 * When 0, the cpu has not started. 76 * When 1, it has started 77 */ 78 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE]; 79 DECLARE_KEEP_PAGER(sem_cpu_sync); 80 #endif 81 82 static unsigned long boot_arg_fdt __nex_bss; 83 static unsigned long boot_arg_nsec_entry __nex_bss; 84 static unsigned long boot_arg_pageable_part __nex_bss; 85 static unsigned long boot_arg_transfer_list __nex_bss; 86 static struct transfer_list_header *mapped_tl __nex_bss; 87 88 #ifdef CFG_SECONDARY_INIT_CNTFRQ 89 static uint32_t cntfrq; 90 #endif 91 92 /* May be overridden in plat-$(PLATFORM)/main.c */ 93 __weak void plat_primary_init_early(void) 94 { 95 } 96 DECLARE_KEEP_PAGER(plat_primary_init_early); 97 98 /* May be overridden in plat-$(PLATFORM)/main.c */ 99 __weak void boot_primary_init_intc(void) 100 { 101 } 102 103 /* May be overridden in plat-$(PLATFORM)/main.c */ 104 __weak void boot_secondary_init_intc(void) 105 { 106 } 107 108 /* May be overridden in plat-$(PLATFORM)/main.c */ 109 __weak unsigned long plat_get_aslr_seed(void) 110 { 111 DMSG("Warning: no ASLR seed"); 112 113 return 0; 114 } 115 116 #if defined(_CFG_CORE_STACK_PROTECTOR) || defined(CFG_WITH_STACK_CANARIES) 117 /* Generate random stack canary value on boot up */ 118 __weak void plat_get_random_stack_canaries(void *buf, size_t ncan, size_t size) 119 { 120 TEE_Result ret = TEE_ERROR_GENERIC; 121 size_t i = 0; 122 123 assert(buf && ncan && size); 124 125 /* 126 * With virtualization the RNG is not initialized in Nexus core. 127 * Need to override with platform specific implementation. 128 */ 129 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 130 IMSG("WARNING: Using fixed value for stack canary"); 131 memset(buf, 0xab, ncan * size); 132 goto out; 133 } 134 135 ret = crypto_rng_read(buf, ncan * size); 136 if (ret != TEE_SUCCESS) 137 panic("Failed to generate random stack canary"); 138 139 out: 140 /* Leave null byte in canary to prevent string base exploit */ 141 for (i = 0; i < ncan; i++) 142 *((uint8_t *)buf + size * i) = 0; 143 } 144 #endif /* _CFG_CORE_STACK_PROTECTOR || CFG_WITH_STACK_CANARIES */ 145 146 /* 147 * This function is called as a guard after each smc call which is not 148 * supposed to return. 149 */ 150 void __panic_at_smc_return(void) 151 { 152 panic(); 153 } 154 155 #if defined(CFG_WITH_ARM_TRUSTED_FW) 156 void init_sec_mon(unsigned long nsec_entry __maybe_unused) 157 { 158 assert(nsec_entry == PADDR_INVALID); 159 /* Do nothing as we don't have a secure monitor */ 160 } 161 #else 162 /* May be overridden in plat-$(PLATFORM)/main.c */ 163 __weak void init_sec_mon(unsigned long nsec_entry) 164 { 165 struct sm_nsec_ctx *nsec_ctx; 166 167 assert(nsec_entry != PADDR_INVALID); 168 169 /* Initialize secure monitor */ 170 nsec_ctx = sm_get_nsec_ctx(); 171 nsec_ctx->mon_lr = nsec_entry; 172 nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I; 173 if (nsec_entry & 1) 174 nsec_ctx->mon_spsr |= CPSR_T; 175 } 176 #endif 177 178 #if defined(CFG_WITH_ARM_TRUSTED_FW) 179 static void init_vfp_nsec(void) 180 { 181 } 182 #else 183 static void init_vfp_nsec(void) 184 { 185 /* Normal world can use CP10 and CP11 (SIMD/VFP) */ 186 write_nsacr(read_nsacr() | NSACR_CP10 | NSACR_CP11); 187 } 188 #endif 189 190 static void check_crypto_extensions(void) 191 { 192 bool ce_supported = true; 193 194 if (!feat_aes_implemented() && 195 IS_ENABLED(CFG_CRYPTO_AES_ARM_CE)) { 196 EMSG("AES instructions are not supported"); 197 ce_supported = false; 198 } 199 200 if (!feat_sha1_implemented() && 201 IS_ENABLED(CFG_CRYPTO_SHA1_ARM_CE)) { 202 EMSG("SHA1 instructions are not supported"); 203 ce_supported = false; 204 } 205 206 if (!feat_sha256_implemented() && 207 IS_ENABLED(CFG_CRYPTO_SHA256_ARM_CE)) { 208 EMSG("SHA256 instructions are not supported"); 209 ce_supported = false; 210 } 211 212 /* Check aarch64 specific instructions */ 213 if (IS_ENABLED(CFG_ARM64_core)) { 214 if (!feat_sha512_implemented() && 215 IS_ENABLED(CFG_CRYPTO_SHA512_ARM_CE)) { 216 EMSG("SHA512 instructions are not supported"); 217 ce_supported = false; 218 } 219 220 if (!feat_sha3_implemented() && 221 IS_ENABLED(CFG_CRYPTO_SHA3_ARM_CE)) { 222 EMSG("SHA3 instructions are not supported"); 223 ce_supported = false; 224 } 225 226 if (!feat_sm3_implemented() && 227 IS_ENABLED(CFG_CRYPTO_SM3_ARM_CE)) { 228 EMSG("SM3 instructions are not supported"); 229 ce_supported = false; 230 } 231 232 if (!feat_sm4_implemented() && 233 IS_ENABLED(CFG_CRYPTO_SM4_ARM_CE)) { 234 EMSG("SM4 instructions are not supported"); 235 ce_supported = false; 236 } 237 } 238 239 if (!ce_supported) 240 panic("HW doesn't support CE instructions"); 241 } 242 243 #if defined(CFG_WITH_VFP) 244 245 #ifdef ARM32 246 static void init_vfp_sec(void) 247 { 248 uint32_t cpacr = read_cpacr(); 249 250 /* 251 * Enable Advanced SIMD functionality. 252 * Enable use of D16-D31 of the Floating-point Extension register 253 * file. 254 */ 255 cpacr &= ~(CPACR_ASEDIS | CPACR_D32DIS); 256 /* 257 * Enable usage of CP10 and CP11 (SIMD/VFP) (both kernel and user 258 * mode. 259 */ 260 cpacr |= CPACR_CP(10, CPACR_CP_ACCESS_FULL); 261 cpacr |= CPACR_CP(11, CPACR_CP_ACCESS_FULL); 262 write_cpacr(cpacr); 263 } 264 #endif /* ARM32 */ 265 266 #ifdef ARM64 267 static void init_vfp_sec(void) 268 { 269 /* Not using VFP until thread_kernel_enable_vfp() */ 270 vfp_disable(); 271 } 272 #endif /* ARM64 */ 273 274 #else /* CFG_WITH_VFP */ 275 276 static void init_vfp_sec(void) 277 { 278 /* Not using VFP */ 279 } 280 #endif 281 282 #ifdef CFG_SECONDARY_INIT_CNTFRQ 283 static void primary_save_cntfrq(void) 284 { 285 assert(cntfrq == 0); 286 287 /* 288 * CNTFRQ should be initialized on the primary CPU by a 289 * previous boot stage 290 */ 291 cntfrq = read_cntfrq(); 292 } 293 294 static void secondary_init_cntfrq(void) 295 { 296 assert(cntfrq != 0); 297 write_cntfrq(cntfrq); 298 } 299 #else /* CFG_SECONDARY_INIT_CNTFRQ */ 300 static void primary_save_cntfrq(void) 301 { 302 } 303 304 static void secondary_init_cntfrq(void) 305 { 306 } 307 #endif 308 309 #ifdef CFG_CORE_SANITIZE_KADDRESS 310 static void init_run_constructors(void) 311 { 312 const vaddr_t *ctor; 313 314 for (ctor = &__ctor_list; ctor < &__ctor_end; ctor++) 315 ((void (*)(void))(*ctor))(); 316 } 317 318 static void init_asan(void) 319 { 320 321 /* 322 * CFG_ASAN_SHADOW_OFFSET is also supplied as 323 * -fasan-shadow-offset=$(CFG_ASAN_SHADOW_OFFSET) to the compiler. 324 * Since all the needed values to calculate the value of 325 * CFG_ASAN_SHADOW_OFFSET isn't available in to make we need to 326 * calculate it in advance and hard code it into the platform 327 * conf.mk. Here where we have all the needed values we double 328 * check that the compiler is supplied the correct value. 329 */ 330 331 #define __ASAN_SHADOW_START \ 332 ROUNDUP(TEE_RAM_START + (TEE_RAM_VA_SIZE * 8) / 9 - 8, 8) 333 assert(__ASAN_SHADOW_START == (vaddr_t)&__asan_shadow_start); 334 #define __CFG_ASAN_SHADOW_OFFSET \ 335 (__ASAN_SHADOW_START - (TEE_RAM_START / 8)) 336 COMPILE_TIME_ASSERT(CFG_ASAN_SHADOW_OFFSET == __CFG_ASAN_SHADOW_OFFSET); 337 #undef __ASAN_SHADOW_START 338 #undef __CFG_ASAN_SHADOW_OFFSET 339 340 /* 341 * Assign area covered by the shadow area, everything from start up 342 * to the beginning of the shadow area. 343 */ 344 asan_set_shadowed((void *)TEE_LOAD_ADDR, &__asan_shadow_start); 345 346 /* 347 * Add access to areas that aren't opened automatically by a 348 * constructor. 349 */ 350 asan_tag_access(&__ctor_list, &__ctor_end); 351 asan_tag_access(__rodata_start, __rodata_end); 352 #ifdef CFG_WITH_PAGER 353 asan_tag_access(__pageable_start, __pageable_end); 354 #endif /*CFG_WITH_PAGER*/ 355 asan_tag_access(__nozi_start, __nozi_end); 356 #ifdef ARM32 357 asan_tag_access(__exidx_start, __exidx_end); 358 asan_tag_access(__extab_start, __extab_end); 359 #endif 360 361 init_run_constructors(); 362 363 /* Everything is tagged correctly, let's start address sanitizing. */ 364 asan_start(); 365 } 366 #else /*CFG_CORE_SANITIZE_KADDRESS*/ 367 static void init_asan(void) 368 { 369 } 370 #endif /*CFG_CORE_SANITIZE_KADDRESS*/ 371 372 #if defined(CFG_MEMTAG) 373 /* Called from entry_a64.S only when MEMTAG is configured */ 374 void boot_init_memtag(void) 375 { 376 memtag_init_ops(feat_mte_implemented()); 377 } 378 379 /* Called from entry_a64.S only when MEMTAG is configured */ 380 void boot_clear_memtag(void) 381 { 382 enum teecore_memtypes mtypes[] = { 383 MEM_AREA_TEE_RAM, MEM_AREA_TEE_RAM_RW, MEM_AREA_NEX_RAM_RO, 384 MEM_AREA_NEX_RAM_RW, MEM_AREA_TEE_ASAN, MEM_AREA_TA_RAM 385 }; 386 vaddr_t s = 0; 387 vaddr_t e = 0; 388 size_t n = 0; 389 390 for (n = 0; n < ARRAY_SIZE(mtypes); n++) { 391 core_mmu_get_mem_by_type(mtypes[n], &s, &e); 392 if (e > s) { 393 DMSG("Clearing tags for VA %#"PRIxVA"..%#"PRIxVA, 394 s, e - 1); 395 memtag_set_tags((void *)s, e - s, 0); 396 } 397 } 398 } 399 #endif 400 401 #ifdef CFG_WITH_PAGER 402 403 #ifdef CFG_CORE_SANITIZE_KADDRESS 404 static void carve_out_asan_mem(tee_mm_pool_t *pool) 405 { 406 const size_t s = pool->hi - pool->lo; 407 tee_mm_entry_t *mm; 408 paddr_t apa = ASAN_MAP_PA; 409 size_t asz = ASAN_MAP_SZ; 410 411 if (core_is_buffer_outside(apa, asz, pool->lo, s)) 412 return; 413 414 /* Reserve the shadow area */ 415 if (!core_is_buffer_inside(apa, asz, pool->lo, s)) { 416 if (apa < pool->lo) { 417 /* 418 * ASAN buffer is overlapping with the beginning of 419 * the pool. 420 */ 421 asz -= pool->lo - apa; 422 apa = pool->lo; 423 } else { 424 /* 425 * ASAN buffer is overlapping with the end of the 426 * pool. 427 */ 428 asz = pool->hi - apa; 429 } 430 } 431 mm = tee_mm_alloc2(pool, apa, asz); 432 assert(mm); 433 } 434 #else 435 static void carve_out_asan_mem(tee_mm_pool_t *pool __unused) 436 { 437 } 438 #endif 439 440 static void print_pager_pool_size(void) 441 { 442 struct tee_pager_stats __maybe_unused stats; 443 444 tee_pager_get_stats(&stats); 445 IMSG("Pager pool size: %zukB", 446 stats.npages_all * SMALL_PAGE_SIZE / 1024); 447 } 448 449 static void init_vcore(tee_mm_pool_t *mm_vcore) 450 { 451 const vaddr_t begin = VCORE_START_VA; 452 size_t size = TEE_RAM_VA_SIZE; 453 454 #ifdef CFG_CORE_SANITIZE_KADDRESS 455 /* Carve out asan memory, flat maped after core memory */ 456 if (begin + size > ASAN_SHADOW_PA) 457 size = ASAN_MAP_PA - begin; 458 #endif 459 460 if (!tee_mm_init(mm_vcore, begin, size, SMALL_PAGE_SHIFT, 461 TEE_MM_POOL_NO_FLAGS)) 462 panic("tee_mm_vcore init failed"); 463 } 464 465 /* 466 * With CFG_CORE_ASLR=y the init part is relocated very early during boot. 467 * The init part is also paged just as the rest of the normal paged code, with 468 * the difference that it's preloaded during boot. When the backing store 469 * is configured the entire paged binary is copied in place and then also 470 * the init part. Since the init part has been relocated (references to 471 * addresses updated to compensate for the new load address) this has to be 472 * undone for the hashes of those pages to match with the original binary. 473 * 474 * If CFG_CORE_ASLR=n, nothing needs to be done as the code/ro pages are 475 * unchanged. 476 */ 477 static void undo_init_relocation(uint8_t *paged_store __maybe_unused) 478 { 479 #ifdef CFG_CORE_ASLR 480 unsigned long *ptr = NULL; 481 const uint32_t *reloc = NULL; 482 const uint32_t *reloc_end = NULL; 483 unsigned long offs = boot_mmu_config.map_offset; 484 const struct boot_embdata *embdata = (const void *)__init_end; 485 vaddr_t addr_end = (vaddr_t)__init_end - offs - TEE_LOAD_ADDR; 486 vaddr_t addr_start = (vaddr_t)__init_start - offs - TEE_LOAD_ADDR; 487 488 reloc = (const void *)((vaddr_t)embdata + embdata->reloc_offset); 489 reloc_end = reloc + embdata->reloc_len / sizeof(*reloc); 490 491 for (; reloc < reloc_end; reloc++) { 492 if (*reloc < addr_start) 493 continue; 494 if (*reloc >= addr_end) 495 break; 496 ptr = (void *)(paged_store + *reloc - addr_start); 497 *ptr -= offs; 498 } 499 #endif 500 } 501 502 static struct fobj *ro_paged_alloc(tee_mm_entry_t *mm, void *hashes, 503 void *store) 504 { 505 const unsigned int num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE; 506 #ifdef CFG_CORE_ASLR 507 unsigned int reloc_offs = (vaddr_t)__pageable_start - VCORE_START_VA; 508 const struct boot_embdata *embdata = (const void *)__init_end; 509 const void *reloc = __init_end + embdata->reloc_offset; 510 511 return fobj_ro_reloc_paged_alloc(num_pages, hashes, reloc_offs, 512 reloc, embdata->reloc_len, store); 513 #else 514 return fobj_ro_paged_alloc(num_pages, hashes, store); 515 #endif 516 } 517 518 static void init_runtime(unsigned long pageable_part) 519 { 520 size_t n; 521 size_t init_size = (size_t)(__init_end - __init_start); 522 size_t pageable_start = (size_t)__pageable_start; 523 size_t pageable_end = (size_t)__pageable_end; 524 size_t pageable_size = pageable_end - pageable_start; 525 vaddr_t tzsram_end = TZSRAM_BASE + TZSRAM_SIZE - TEE_LOAD_ADDR + 526 VCORE_START_VA; 527 size_t hash_size = (pageable_size / SMALL_PAGE_SIZE) * 528 TEE_SHA256_HASH_SIZE; 529 const struct boot_embdata *embdata = (const void *)__init_end; 530 const void *tmp_hashes = NULL; 531 tee_mm_entry_t *mm = NULL; 532 struct fobj *fobj = NULL; 533 uint8_t *paged_store = NULL; 534 uint8_t *hashes = NULL; 535 536 assert(pageable_size % SMALL_PAGE_SIZE == 0); 537 assert(embdata->total_len >= embdata->hashes_offset + 538 embdata->hashes_len); 539 assert(hash_size == embdata->hashes_len); 540 541 tmp_hashes = __init_end + embdata->hashes_offset; 542 543 init_asan(); 544 545 /* Add heap2 first as heap1 may be too small as initial bget pool */ 546 malloc_add_pool(__heap2_start, __heap2_end - __heap2_start); 547 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 548 549 /* 550 * This needs to be initialized early to support address lookup 551 * in MEM_AREA_TEE_RAM 552 */ 553 tee_pager_early_init(); 554 555 hashes = malloc(hash_size); 556 IMSG_RAW("\n"); 557 IMSG("Pager is enabled. Hashes: %zu bytes", hash_size); 558 assert(hashes); 559 asan_memcpy_unchecked(hashes, tmp_hashes, hash_size); 560 561 /* 562 * Need tee_mm_sec_ddr initialized to be able to allocate secure 563 * DDR below. 564 */ 565 core_mmu_init_ta_ram(); 566 567 carve_out_asan_mem(&tee_mm_sec_ddr); 568 569 mm = tee_mm_alloc(&tee_mm_sec_ddr, pageable_size); 570 assert(mm); 571 paged_store = phys_to_virt(tee_mm_get_smem(mm), MEM_AREA_TA_RAM, 572 pageable_size); 573 /* 574 * Load pageable part in the dedicated allocated area: 575 * - Move pageable non-init part into pageable area. Note bootloader 576 * may have loaded it anywhere in TA RAM hence use memmove(). 577 * - Copy pageable init part from current location into pageable area. 578 */ 579 memmove(paged_store + init_size, 580 phys_to_virt(pageable_part, 581 core_mmu_get_type_by_pa(pageable_part), 582 __pageable_part_end - __pageable_part_start), 583 __pageable_part_end - __pageable_part_start); 584 asan_memcpy_unchecked(paged_store, __init_start, init_size); 585 /* 586 * Undo eventual relocation for the init part so the hash checks 587 * can pass. 588 */ 589 undo_init_relocation(paged_store); 590 591 /* Check that hashes of what's in pageable area is OK */ 592 DMSG("Checking hashes of pageable area"); 593 for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) { 594 const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE; 595 const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE; 596 TEE_Result res; 597 598 DMSG("hash pg_idx %zu hash %p page %p", n, hash, page); 599 res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE); 600 if (res != TEE_SUCCESS) { 601 EMSG("Hash failed for page %zu at %p: res 0x%x", 602 n, (void *)page, res); 603 panic(); 604 } 605 } 606 607 /* 608 * Assert prepaged init sections are page aligned so that nothing 609 * trails uninited at the end of the premapped init area. 610 */ 611 assert(!(init_size & SMALL_PAGE_MASK)); 612 613 /* 614 * Initialize the virtual memory pool used for main_mmu_l2_ttb which 615 * is supplied to tee_pager_init() below. 616 */ 617 init_vcore(&tee_mm_vcore); 618 619 /* 620 * Assign alias area for pager end of the small page block the rest 621 * of the binary is loaded into. We're taking more than needed, but 622 * we're guaranteed to not need more than the physical amount of 623 * TZSRAM. 624 */ 625 mm = tee_mm_alloc2(&tee_mm_vcore, 626 (vaddr_t)tee_mm_vcore.lo + 627 tee_mm_vcore.size - TZSRAM_SIZE, 628 TZSRAM_SIZE); 629 assert(mm); 630 tee_pager_set_alias_area(mm); 631 632 /* 633 * Claim virtual memory which isn't paged. 634 * Linear memory (flat map core memory) ends there. 635 */ 636 mm = tee_mm_alloc2(&tee_mm_vcore, VCORE_UNPG_RX_PA, 637 (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA)); 638 assert(mm); 639 640 /* 641 * Allocate virtual memory for the pageable area and let the pager 642 * take charge of all the pages already assigned to that memory. 643 */ 644 mm = tee_mm_alloc2(&tee_mm_vcore, (vaddr_t)__pageable_start, 645 pageable_size); 646 assert(mm); 647 fobj = ro_paged_alloc(mm, hashes, paged_store); 648 assert(fobj); 649 tee_pager_add_core_region(tee_mm_get_smem(mm), PAGED_REGION_TYPE_RO, 650 fobj); 651 fobj_put(fobj); 652 653 tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false); 654 tee_pager_add_pages(pageable_start + init_size, 655 (pageable_size - init_size) / SMALL_PAGE_SIZE, 656 true); 657 if (pageable_end < tzsram_end) 658 tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) / 659 SMALL_PAGE_SIZE, true); 660 661 /* 662 * There may be physical pages in TZSRAM before the core load address. 663 * These pages can be added to the physical pages pool of the pager. 664 * This setup may happen when a the secure bootloader runs in TZRAM 665 * and its memory can be reused by OP-TEE once boot stages complete. 666 */ 667 tee_pager_add_pages(tee_mm_vcore.lo, 668 (VCORE_UNPG_RX_PA - tee_mm_vcore.lo) / SMALL_PAGE_SIZE, 669 true); 670 671 print_pager_pool_size(); 672 } 673 #else 674 675 static void init_runtime(unsigned long pageable_part __unused) 676 { 677 init_asan(); 678 679 /* 680 * By default whole OP-TEE uses malloc, so we need to initialize 681 * it early. But, when virtualization is enabled, malloc is used 682 * only by TEE runtime, so malloc should be initialized later, for 683 * every virtual partition separately. Core code uses nex_malloc 684 * instead. 685 */ 686 #ifdef CFG_NS_VIRTUALIZATION 687 nex_malloc_add_pool(__nex_heap_start, __nex_heap_end - 688 __nex_heap_start); 689 #else 690 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 691 #endif 692 693 IMSG_RAW("\n"); 694 } 695 #endif 696 697 #if defined(CFG_DT) 698 static int add_optee_dt_node(struct dt_descriptor *dt) 699 { 700 int offs; 701 int ret; 702 703 if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) { 704 DMSG("OP-TEE Device Tree node already exists!"); 705 return 0; 706 } 707 708 offs = fdt_path_offset(dt->blob, "/firmware"); 709 if (offs < 0) { 710 offs = add_dt_path_subnode(dt, "/", "firmware"); 711 if (offs < 0) 712 return -1; 713 } 714 715 offs = fdt_add_subnode(dt->blob, offs, "optee"); 716 if (offs < 0) 717 return -1; 718 719 ret = fdt_setprop_string(dt->blob, offs, "compatible", 720 "linaro,optee-tz"); 721 if (ret < 0) 722 return -1; 723 ret = fdt_setprop_string(dt->blob, offs, "method", "smc"); 724 if (ret < 0) 725 return -1; 726 727 if (CFG_CORE_ASYNC_NOTIF_GIC_INTID) { 728 /* 729 * The format of the interrupt property is defined by the 730 * binding of the interrupt domain root. In this case it's 731 * one Arm GIC v1, v2 or v3 so we must be compatible with 732 * these. 733 * 734 * An SPI type of interrupt is indicated with a 0 in the 735 * first cell. A PPI type is indicated with value 1. 736 * 737 * The interrupt number goes in the second cell where 738 * SPIs ranges from 0 to 987 and PPI ranges from 0 to 15. 739 * 740 * Flags are passed in the third cells. 741 */ 742 uint32_t itr_trigger = 0; 743 uint32_t itr_type = 0; 744 uint32_t itr_id = 0; 745 uint32_t val[3] = { }; 746 747 /* PPI are visible only in current CPU cluster */ 748 static_assert(IS_ENABLED(CFG_CORE_FFA) || 749 !CFG_CORE_ASYNC_NOTIF_GIC_INTID || 750 (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= 751 GIC_SPI_BASE) || 752 ((CFG_TEE_CORE_NB_CORE <= 8) && 753 (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= 754 GIC_PPI_BASE))); 755 756 if (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= GIC_SPI_BASE) { 757 itr_type = GIC_SPI; 758 itr_id = CFG_CORE_ASYNC_NOTIF_GIC_INTID - GIC_SPI_BASE; 759 itr_trigger = IRQ_TYPE_EDGE_RISING; 760 } else { 761 itr_type = GIC_PPI; 762 itr_id = CFG_CORE_ASYNC_NOTIF_GIC_INTID - GIC_PPI_BASE; 763 itr_trigger = IRQ_TYPE_EDGE_RISING | 764 GIC_CPU_MASK_SIMPLE(CFG_TEE_CORE_NB_CORE); 765 } 766 767 val[0] = TEE_U32_TO_BIG_ENDIAN(itr_type); 768 val[1] = TEE_U32_TO_BIG_ENDIAN(itr_id); 769 val[2] = TEE_U32_TO_BIG_ENDIAN(itr_trigger); 770 771 ret = fdt_setprop(dt->blob, offs, "interrupts", val, 772 sizeof(val)); 773 if (ret < 0) 774 return -1; 775 } 776 return 0; 777 } 778 779 #ifdef CFG_PSCI_ARM32 780 static int append_psci_compatible(void *fdt, int offs, const char *str) 781 { 782 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 783 } 784 785 static int dt_add_psci_node(struct dt_descriptor *dt) 786 { 787 int offs; 788 789 if (fdt_path_offset(dt->blob, "/psci") >= 0) { 790 DMSG("PSCI Device Tree node already exists!"); 791 return 0; 792 } 793 794 offs = add_dt_path_subnode(dt, "/", "psci"); 795 if (offs < 0) 796 return -1; 797 if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0")) 798 return -1; 799 if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2")) 800 return -1; 801 if (append_psci_compatible(dt->blob, offs, "arm,psci")) 802 return -1; 803 if (fdt_setprop_string(dt->blob, offs, "method", "smc")) 804 return -1; 805 if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND)) 806 return -1; 807 if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF)) 808 return -1; 809 if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON)) 810 return -1; 811 if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF)) 812 return -1; 813 if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET)) 814 return -1; 815 return 0; 816 } 817 818 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs, 819 const char *prefix) 820 { 821 const size_t prefix_len = strlen(prefix); 822 size_t l; 823 int plen; 824 const char *prop; 825 826 prop = fdt_getprop(dt->blob, offs, "compatible", &plen); 827 if (!prop) 828 return -1; 829 830 while (plen > 0) { 831 if (memcmp(prop, prefix, prefix_len) == 0) 832 return 0; /* match */ 833 834 l = strlen(prop) + 1; 835 prop += l; 836 plen -= l; 837 } 838 839 return -1; 840 } 841 842 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt) 843 { 844 int offs = 0; 845 846 while (1) { 847 offs = fdt_next_node(dt->blob, offs, NULL); 848 if (offs < 0) 849 break; 850 if (fdt_getprop(dt->blob, offs, "enable-method", NULL)) 851 continue; /* already set */ 852 if (check_node_compat_prefix(dt, offs, "arm,cortex-a")) 853 continue; /* no compatible */ 854 if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci")) 855 return -1; 856 /* Need to restart scanning as offsets may have changed */ 857 offs = 0; 858 } 859 return 0; 860 } 861 862 static int config_psci(struct dt_descriptor *dt) 863 { 864 if (dt_add_psci_node(dt)) 865 return -1; 866 return dt_add_psci_cpu_enable_methods(dt); 867 } 868 #else 869 static int config_psci(struct dt_descriptor *dt __unused) 870 { 871 return 0; 872 } 873 #endif /*CFG_PSCI_ARM32*/ 874 875 static int mark_tzdram_as_reserved(struct dt_descriptor *dt) 876 { 877 return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START, 878 CFG_TZDRAM_SIZE); 879 } 880 881 static void update_external_dt(void) 882 { 883 struct dt_descriptor *dt = get_external_dt_desc(); 884 885 if (!dt || !dt->blob) 886 return; 887 888 if (!IS_ENABLED(CFG_CORE_FFA) && add_optee_dt_node(dt)) 889 panic("Failed to add OP-TEE Device Tree node"); 890 891 if (config_psci(dt)) 892 panic("Failed to config PSCI"); 893 894 #ifdef CFG_CORE_RESERVED_SHM 895 if (mark_static_shm_as_reserved(dt)) 896 panic("Failed to config non-secure memory"); 897 #endif 898 899 if (mark_tzdram_as_reserved(dt)) 900 panic("Failed to config secure memory"); 901 } 902 #else /*CFG_DT*/ 903 static void update_external_dt(void) 904 { 905 } 906 #endif /*!CFG_DT*/ 907 908 #ifdef CFG_NS_VIRTUALIZATION 909 static TEE_Result virt_init_heap(void) 910 { 911 /* We need to initialize pool for every virtual guest partition */ 912 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 913 914 return TEE_SUCCESS; 915 } 916 preinit_early(virt_init_heap); 917 #endif 918 919 void init_tee_runtime(void) 920 { 921 #ifndef CFG_WITH_PAGER 922 /* Pager initializes TA RAM early */ 923 core_mmu_init_ta_ram(); 924 #endif 925 /* 926 * With virtualization we call this function when creating the 927 * OP-TEE partition instead. 928 */ 929 if (!IS_ENABLED(CFG_NS_VIRTUALIZATION)) 930 call_preinitcalls(); 931 call_initcalls(); 932 933 /* 934 * These two functions uses crypto_rng_read() to initialize the 935 * pauth keys. Once call_initcalls() returns we're guaranteed that 936 * crypto_rng_read() is ready to be used. 937 */ 938 thread_init_core_local_pauth_keys(); 939 thread_init_thread_pauth_keys(); 940 941 /* 942 * Reinitialize canaries around the stacks with crypto_rng_read(). 943 * 944 * TODO: Updating canaries when CFG_NS_VIRTUALIZATION is enabled will 945 * require synchronization between thread_check_canaries() and 946 * thread_update_canaries(). 947 */ 948 if (!IS_ENABLED(CFG_NS_VIRTUALIZATION)) 949 thread_update_canaries(); 950 } 951 952 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry) 953 { 954 thread_init_core_local_stacks(); 955 /* 956 * Mask asynchronous exceptions before switch to the thread vector 957 * as the thread handler requires those to be masked while 958 * executing with the temporary stack. The thread subsystem also 959 * asserts that the foreign interrupts are blocked when using most of 960 * its functions. 961 */ 962 thread_set_exceptions(THREAD_EXCP_ALL); 963 primary_save_cntfrq(); 964 init_vfp_sec(); 965 966 if (IS_ENABLED(CFG_CRYPTO_WITH_CE)) 967 check_crypto_extensions(); 968 969 /* 970 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must 971 * set a current thread right now to avoid a chicken-and-egg problem 972 * (thread_init_boot_thread() sets the current thread but needs 973 * things set by init_runtime()). 974 */ 975 thread_get_core_local()->curr_thread = 0; 976 init_runtime(pageable_part); 977 978 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 979 /* 980 * Virtualization: We can't initialize threads right now because 981 * threads belong to "tee" part and will be initialized 982 * separately per each new virtual guest. So, we'll clear 983 * "curr_thread" and call it done. 984 */ 985 thread_get_core_local()->curr_thread = -1; 986 } else { 987 thread_init_boot_thread(); 988 } 989 thread_init_primary(); 990 thread_init_per_cpu(); 991 init_sec_mon(nsec_entry); 992 } 993 994 static bool cpu_nmfi_enabled(void) 995 { 996 #if defined(ARM32) 997 return read_sctlr() & SCTLR_NMFI; 998 #else 999 /* Note: ARM64 does not feature non-maskable FIQ support. */ 1000 return false; 1001 #endif 1002 } 1003 1004 /* 1005 * Note: this function is weak just to make it possible to exclude it from 1006 * the unpaged area. 1007 */ 1008 void __weak boot_init_primary_late(unsigned long fdt __unused, 1009 unsigned long manifest __unused) 1010 { 1011 size_t fdt_size = CFG_DTB_MAX_SIZE; 1012 1013 if (IS_ENABLED(CFG_TRANSFER_LIST) && mapped_tl) { 1014 struct transfer_list_entry *tl_e = NULL; 1015 1016 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1017 if (tl_e) 1018 fdt_size = tl_e->data_size; 1019 } 1020 1021 init_external_dt(boot_arg_fdt, fdt_size); 1022 reinit_manifest_dt(); 1023 #ifdef CFG_CORE_SEL1_SPMC 1024 tpm_map_log_area(get_manifest_dt()); 1025 #else 1026 tpm_map_log_area(get_external_dt()); 1027 #endif 1028 discover_nsec_memory(); 1029 update_external_dt(); 1030 configure_console_from_dt(); 1031 1032 IMSG("OP-TEE version: %s", core_v_str); 1033 if (IS_ENABLED(CFG_INSECURE)) { 1034 IMSG("WARNING: This OP-TEE configuration might be insecure!"); 1035 IMSG("WARNING: Please check https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html"); 1036 } 1037 IMSG("Primary CPU initializing"); 1038 #ifdef CFG_CORE_ASLR 1039 DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA, 1040 (unsigned long)boot_mmu_config.map_offset, VCORE_START_VA); 1041 #endif 1042 if (IS_ENABLED(CFG_MEMTAG)) 1043 DMSG("Memory tagging %s", 1044 memtag_is_enabled() ? "enabled" : "disabled"); 1045 1046 /* Check if platform needs NMFI workaround */ 1047 if (cpu_nmfi_enabled()) { 1048 if (!IS_ENABLED(CFG_CORE_WORKAROUND_ARM_NMFI)) 1049 IMSG("WARNING: This ARM core has NMFI enabled, please apply workaround!"); 1050 } else { 1051 if (IS_ENABLED(CFG_CORE_WORKAROUND_ARM_NMFI)) 1052 IMSG("WARNING: This ARM core does not have NMFI enabled, no need for workaround"); 1053 } 1054 1055 boot_primary_init_intc(); 1056 init_vfp_nsec(); 1057 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 1058 IMSG("Initializing virtualization support"); 1059 core_mmu_init_virtualization(); 1060 } else { 1061 init_tee_runtime(); 1062 } 1063 call_finalcalls(); 1064 IMSG("Primary CPU switching to normal world boot"); 1065 } 1066 1067 static void init_secondary_helper(unsigned long nsec_entry) 1068 { 1069 IMSG("Secondary CPU %zu initializing", get_core_pos()); 1070 1071 /* 1072 * Mask asynchronous exceptions before switch to the thread vector 1073 * as the thread handler requires those to be masked while 1074 * executing with the temporary stack. The thread subsystem also 1075 * asserts that the foreign interrupts are blocked when using most of 1076 * its functions. 1077 */ 1078 thread_set_exceptions(THREAD_EXCP_ALL); 1079 1080 secondary_init_cntfrq(); 1081 thread_init_per_cpu(); 1082 init_sec_mon(nsec_entry); 1083 boot_secondary_init_intc(); 1084 init_vfp_sec(); 1085 init_vfp_nsec(); 1086 1087 IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos()); 1088 } 1089 1090 /* 1091 * Note: this function is weak just to make it possible to exclude it from 1092 * the unpaged area so that it lies in the init area. 1093 */ 1094 void __weak boot_init_primary_early(void) 1095 { 1096 unsigned long pageable_part = 0; 1097 unsigned long e = PADDR_INVALID; 1098 struct transfer_list_entry *tl_e = NULL; 1099 1100 if (!IS_ENABLED(CFG_WITH_ARM_TRUSTED_FW)) 1101 e = boot_arg_nsec_entry; 1102 1103 if (IS_ENABLED(CFG_TRANSFER_LIST) && boot_arg_transfer_list) { 1104 /* map and save the TL */ 1105 mapped_tl = transfer_list_map(boot_arg_transfer_list); 1106 if (!mapped_tl) 1107 panic("Failed to map transfer list"); 1108 1109 transfer_list_dump(mapped_tl); 1110 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1111 if (tl_e) { 1112 /* 1113 * Expand the data size of the DTB entry to the maximum 1114 * allocable mapped memory to reserve sufficient space 1115 * for inserting new nodes, avoid potentially corrupting 1116 * next entries. 1117 */ 1118 uint32_t dtb_max_sz = mapped_tl->max_size - 1119 mapped_tl->size + tl_e->data_size; 1120 1121 if (!transfer_list_set_data_size(mapped_tl, tl_e, 1122 dtb_max_sz)) { 1123 EMSG("Failed to extend DTB size to %#"PRIx32, 1124 dtb_max_sz); 1125 panic(); 1126 } 1127 } 1128 tl_e = transfer_list_find(mapped_tl, TL_TAG_OPTEE_PAGABLE_PART); 1129 } 1130 1131 if (IS_ENABLED(CFG_WITH_PAGER)) { 1132 if (IS_ENABLED(CFG_TRANSFER_LIST) && tl_e) 1133 pageable_part = 1134 get_le64(transfer_list_entry_data(tl_e)); 1135 else 1136 pageable_part = boot_arg_pageable_part; 1137 } 1138 1139 init_primary(pageable_part, e); 1140 } 1141 1142 static void boot_save_transfer_list(unsigned long zero_reg, 1143 unsigned long transfer_list, 1144 unsigned long fdt) 1145 { 1146 struct transfer_list_header *tl = (void *)transfer_list; 1147 struct transfer_list_entry *tl_e = NULL; 1148 1149 if (zero_reg != 0) 1150 panic("Incorrect transfer list register convention"); 1151 1152 if (!IS_ALIGNED_WITH_TYPE(transfer_list, struct transfer_list_header) || 1153 !IS_ALIGNED(transfer_list, TL_ALIGNMENT_FROM_ORDER(tl->alignment))) 1154 panic("Transfer list base address is not aligned"); 1155 1156 if (transfer_list_check_header(tl) == TL_OPS_NONE) 1157 panic("Invalid transfer list"); 1158 1159 tl_e = transfer_list_find(tl, TL_TAG_FDT); 1160 if (fdt != (unsigned long)transfer_list_entry_data(tl_e)) 1161 panic("DT does not match to the DT entry of the TL"); 1162 1163 boot_arg_transfer_list = transfer_list; 1164 } 1165 1166 #if defined(CFG_WITH_ARM_TRUSTED_FW) 1167 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused, 1168 unsigned long a1 __unused) 1169 { 1170 init_secondary_helper(PADDR_INVALID); 1171 return 0; 1172 } 1173 #else 1174 void boot_init_secondary(unsigned long nsec_entry) 1175 { 1176 init_secondary_helper(nsec_entry); 1177 } 1178 #endif 1179 1180 #if defined(CFG_BOOT_SECONDARY_REQUEST) 1181 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry, 1182 uintptr_t context_id) 1183 { 1184 ns_entry_contexts[core_idx].entry_point = entry; 1185 ns_entry_contexts[core_idx].context_id = context_id; 1186 dsb_ishst(); 1187 } 1188 1189 int boot_core_release(size_t core_idx, paddr_t entry) 1190 { 1191 if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE) 1192 return -1; 1193 1194 ns_entry_contexts[core_idx].entry_point = entry; 1195 dmb(); 1196 spin_table[core_idx] = 1; 1197 dsb(); 1198 sev(); 1199 1200 return 0; 1201 } 1202 1203 /* 1204 * spin until secondary boot request, then returns with 1205 * the secondary core entry address. 1206 */ 1207 struct ns_entry_context *boot_core_hpen(void) 1208 { 1209 #ifdef CFG_PSCI_ARM32 1210 return &ns_entry_contexts[get_core_pos()]; 1211 #else 1212 do { 1213 wfe(); 1214 } while (!spin_table[get_core_pos()]); 1215 dmb(); 1216 return &ns_entry_contexts[get_core_pos()]; 1217 #endif 1218 } 1219 #endif 1220 1221 #if defined(CFG_CORE_ASLR) 1222 #if defined(CFG_DT) 1223 unsigned long __weak get_aslr_seed(void) 1224 { 1225 void *fdt = NULL; 1226 int rc = 0; 1227 const uint64_t *seed = NULL; 1228 int offs = 0; 1229 int len = 0; 1230 1231 if (!IS_ENABLED(CFG_CORE_SEL2_SPMC)) 1232 fdt = (void *)boot_arg_fdt; 1233 1234 if (!fdt) { 1235 DMSG("No fdt"); 1236 goto err; 1237 } 1238 1239 rc = fdt_check_header(fdt); 1240 if (rc) { 1241 DMSG("Bad fdt: %d", rc); 1242 goto err; 1243 } 1244 1245 offs = fdt_path_offset(fdt, "/secure-chosen"); 1246 if (offs < 0) { 1247 DMSG("Cannot find /secure-chosen"); 1248 goto err; 1249 } 1250 seed = fdt_getprop(fdt, offs, "kaslr-seed", &len); 1251 if (!seed || len != sizeof(*seed)) { 1252 DMSG("Cannot find valid kaslr-seed"); 1253 goto err; 1254 } 1255 1256 return fdt64_to_cpu(fdt64_ld(seed)); 1257 1258 err: 1259 /* Try platform implementation */ 1260 return plat_get_aslr_seed(); 1261 } 1262 #else /*!CFG_DT*/ 1263 unsigned long __weak get_aslr_seed(void) 1264 { 1265 /* Try platform implementation */ 1266 return plat_get_aslr_seed(); 1267 } 1268 #endif /*!CFG_DT*/ 1269 #endif /*CFG_CORE_ASLR*/ 1270 1271 static void *get_fdt_from_boot_info(struct ffa_boot_info_header_1_1 *hdr) 1272 { 1273 struct ffa_boot_info_1_1 *desc = NULL; 1274 uint8_t content_fmt = 0; 1275 uint8_t name_fmt = 0; 1276 void *fdt = NULL; 1277 int ret = 0; 1278 1279 if (hdr->signature != FFA_BOOT_INFO_SIGNATURE) { 1280 EMSG("Bad boot info signature %#"PRIx32, hdr->signature); 1281 panic(); 1282 } 1283 if (hdr->version != FFA_BOOT_INFO_VERSION) { 1284 EMSG("Bad boot info version %#"PRIx32, hdr->version); 1285 panic(); 1286 } 1287 if (hdr->desc_count != 1) { 1288 EMSG("Bad boot info descriptor count %#"PRIx32, 1289 hdr->desc_count); 1290 panic(); 1291 } 1292 desc = (void *)((vaddr_t)hdr + hdr->desc_offset); 1293 name_fmt = desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK; 1294 if (name_fmt == FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING) 1295 DMSG("Boot info descriptor name \"%16s\"", desc->name); 1296 else if (name_fmt == FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID) 1297 DMSG("Boot info descriptor UUID %pUl", (void *)desc->name); 1298 else 1299 DMSG("Boot info descriptor: unknown name format %"PRIu8, 1300 name_fmt); 1301 1302 content_fmt = (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >> 1303 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT; 1304 if (content_fmt != FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR) { 1305 EMSG("Bad boot info content format %"PRIu8", expected %u (address)", 1306 content_fmt, FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR); 1307 panic(); 1308 } 1309 1310 fdt = (void *)(vaddr_t)desc->contents; 1311 ret = fdt_check_full(fdt, desc->size); 1312 if (ret < 0) { 1313 EMSG("Invalid Device Tree at %p: error %d", fdt, ret); 1314 panic(); 1315 } 1316 return fdt; 1317 } 1318 1319 static void get_sec_mem_from_manifest(void *fdt, paddr_t *base, size_t *size) 1320 { 1321 int ret = 0; 1322 uint64_t num = 0; 1323 1324 ret = fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0"); 1325 if (ret < 0) { 1326 EMSG("Invalid FF-A manifest at %p: error %d", fdt, ret); 1327 panic(); 1328 } 1329 ret = dt_getprop_as_number(fdt, 0, "load-address", &num); 1330 if (ret < 0) { 1331 EMSG("Can't read \"load-address\" from FF-A manifest at %p: error %d", 1332 fdt, ret); 1333 panic(); 1334 } 1335 *base = num; 1336 /* "mem-size" is currently an undocumented extension to the spec. */ 1337 ret = dt_getprop_as_number(fdt, 0, "mem-size", &num); 1338 if (ret < 0) { 1339 EMSG("Can't read \"mem-size\" from FF-A manifest at %p: error %d", 1340 fdt, ret); 1341 panic(); 1342 } 1343 *size = num; 1344 } 1345 1346 void __weak boot_save_args(unsigned long a0, unsigned long a1, 1347 unsigned long a2, unsigned long a3, 1348 unsigned long a4 __maybe_unused) 1349 { 1350 /* 1351 * Register use: 1352 * 1353 * Scenario A: Default arguments 1354 * a0 - CFG_CORE_FFA=y && CFG_CORE_SEL2_SPMC=n: 1355 * if non-NULL holds the TOS FW config [1] address 1356 * - CFG_CORE_FFA=y && 1357 (CFG_CORE_SEL2_SPMC=y || CFG_CORE_EL3_SPMC=y): 1358 * address of FF-A Boot Information Blob 1359 * - CFG_CORE_FFA=n: 1360 * if non-NULL holds the pagable part address 1361 * a1 - CFG_WITH_ARM_TRUSTED_FW=n (Armv7): 1362 * Armv7 standard bootarg #1 (kept track of in entry_a32.S) 1363 * a2 - CFG_CORE_SEL2_SPMC=n: 1364 * if non-NULL holds the system DTB address 1365 * - CFG_WITH_ARM_TRUSTED_FW=n (Armv7): 1366 * Armv7 standard bootarg #2 (system DTB address, kept track 1367 * of in entry_a32.S) 1368 * a3 - Not used 1369 * a4 - CFG_WITH_ARM_TRUSTED_FW=n: 1370 * Non-secure entry address 1371 * 1372 * [1] A TF-A concept: TOS_FW_CONFIG - Trusted OS Firmware 1373 * configuration file. Used by Trusted OS (BL32), that is, OP-TEE 1374 * here. This is also called Manifest DT, related to the Manifest DT 1375 * passed in the FF-A Boot Information Blob, but with a different 1376 * compatible string. 1377 1378 * Scenario B: FW Handoff via Transfer List 1379 * Note: FF-A and non-secure entry are not yet supported with 1380 * Transfer List 1381 * a0 - DTB address or 0 (AArch64) 1382 * - must be 0 (AArch32) 1383 * a1 - TRANSFER_LIST_SIGNATURE | REG_CONVENTION_VER_MASK 1384 * a2 - must be 0 (AArch64) 1385 * - DTB address or 0 (AArch32) 1386 * a3 - Transfer list base address 1387 * a4 - Not used 1388 */ 1389 1390 if (IS_ENABLED(CFG_TRANSFER_LIST) && 1391 a1 == (TRANSFER_LIST_SIGNATURE | REG_CONVENTION_VER_MASK)) { 1392 if (IS_ENABLED(CFG_ARM64_core)) { 1393 boot_save_transfer_list(a2, a3, a0); 1394 boot_arg_fdt = a0; 1395 } else { 1396 boot_save_transfer_list(a0, a3, a2); 1397 boot_arg_fdt = a2; 1398 } 1399 return; 1400 } 1401 1402 if (!IS_ENABLED(CFG_CORE_SEL2_SPMC)) { 1403 #if defined(CFG_DT_ADDR) 1404 boot_arg_fdt = CFG_DT_ADDR; 1405 #else 1406 boot_arg_fdt = a2; 1407 #endif 1408 } 1409 1410 if (IS_ENABLED(CFG_CORE_FFA)) { 1411 if (IS_ENABLED(CFG_CORE_SEL2_SPMC) || 1412 IS_ENABLED(CFG_CORE_EL3_SPMC)) 1413 init_manifest_dt(get_fdt_from_boot_info((void *)a0)); 1414 else 1415 init_manifest_dt((void *)a0); 1416 if (IS_ENABLED(CFG_CORE_SEL2_SPMC) && 1417 IS_ENABLED(CFG_CORE_PHYS_RELOCATABLE)) { 1418 paddr_t base = 0; 1419 size_t size = 0; 1420 1421 get_sec_mem_from_manifest(get_manifest_dt(), 1422 &base, &size); 1423 core_mmu_set_secure_memory(base, size); 1424 } 1425 } else { 1426 if (IS_ENABLED(CFG_WITH_PAGER)) { 1427 #if defined(CFG_PAGEABLE_ADDR) 1428 boot_arg_pageable_part = CFG_PAGEABLE_ADDR; 1429 #else 1430 boot_arg_pageable_part = a0; 1431 #endif 1432 } 1433 if (!IS_ENABLED(CFG_WITH_ARM_TRUSTED_FW)) { 1434 #if defined(CFG_NS_ENTRY_ADDR) 1435 boot_arg_nsec_entry = CFG_NS_ENTRY_ADDR; 1436 #else 1437 boot_arg_nsec_entry = a4; 1438 #endif 1439 } 1440 } 1441 } 1442 1443 #if defined(CFG_TRANSFER_LIST) 1444 static TEE_Result release_transfer_list(void) 1445 { 1446 struct dt_descriptor *dt = get_external_dt_desc(); 1447 1448 if (!mapped_tl) 1449 return TEE_SUCCESS; 1450 1451 if (dt) { 1452 int ret = 0; 1453 struct transfer_list_entry *tl_e = NULL; 1454 1455 /* 1456 * Pack the DTB and update the transfer list before un-mapping 1457 */ 1458 ret = fdt_pack(dt->blob); 1459 if (ret < 0) { 1460 EMSG("Failed to pack Device Tree at 0x%" PRIxPA 1461 ": error %d", virt_to_phys(dt->blob), ret); 1462 panic(); 1463 } 1464 1465 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1466 assert(dt->blob == transfer_list_entry_data(tl_e)); 1467 transfer_list_set_data_size(mapped_tl, tl_e, 1468 fdt_totalsize(dt->blob)); 1469 dt->blob = NULL; 1470 } 1471 1472 transfer_list_unmap_sync(mapped_tl); 1473 mapped_tl = NULL; 1474 1475 return TEE_SUCCESS; 1476 } 1477 1478 boot_final(release_transfer_list); 1479 #endif 1480