xref: /optee_os/core/arch/arm/kernel/boot.c (revision 9fc2442cc66c279cb962c90c4375746fc9b28bb9)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015-2020, Linaro Limited
4  */
5 
6 #include <arm.h>
7 #include <assert.h>
8 #include <compiler.h>
9 #include <config.h>
10 #include <console.h>
11 #include <crypto/crypto.h>
12 #include <initcall.h>
13 #include <inttypes.h>
14 #include <keep.h>
15 #include <kernel/asan.h>
16 #include <kernel/boot.h>
17 #include <kernel/linker.h>
18 #include <kernel/misc.h>
19 #include <kernel/panic.h>
20 #include <kernel/tee_misc.h>
21 #include <kernel/thread.h>
22 #include <kernel/tpm.h>
23 #include <libfdt.h>
24 #include <malloc.h>
25 #include <mm/core_memprot.h>
26 #include <mm/core_mmu.h>
27 #include <mm/fobj.h>
28 #include <mm/tee_mm.h>
29 #include <mm/tee_pager.h>
30 #include <sm/psci.h>
31 #include <stdio.h>
32 #include <trace.h>
33 #include <utee_defines.h>
34 #include <util.h>
35 
36 #include <platform_config.h>
37 
38 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
39 #include <sm/sm.h>
40 #endif
41 
42 #if defined(CFG_WITH_VFP)
43 #include <kernel/vfp.h>
44 #endif
45 
46 /*
47  * In this file we're using unsigned long to represent physical pointers as
48  * they are received in a single register when OP-TEE is initially entered.
49  * This limits 32-bit systems to only use make use of the lower 32 bits
50  * of a physical address for initial parameters.
51  *
52  * 64-bit systems on the other hand can use full 64-bit physical pointers.
53  */
54 #define PADDR_INVALID		ULONG_MAX
55 
56 #if defined(CFG_BOOT_SECONDARY_REQUEST)
57 struct ns_entry_context {
58 	uintptr_t entry_point;
59 	uintptr_t context_id;
60 };
61 struct ns_entry_context ns_entry_contexts[CFG_TEE_CORE_NB_CORE];
62 static uint32_t spin_table[CFG_TEE_CORE_NB_CORE];
63 #endif
64 
65 #ifdef CFG_BOOT_SYNC_CPU
66 /*
67  * Array used when booting, to synchronize cpu.
68  * When 0, the cpu has not started.
69  * When 1, it has started
70  */
71 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE];
72 DECLARE_KEEP_PAGER(sem_cpu_sync);
73 #endif
74 
75 #ifdef CFG_DT
76 struct dt_descriptor {
77 	void *blob;
78 #ifdef CFG_EXTERNAL_DTB_OVERLAY
79 	int frag_id;
80 #endif
81 };
82 
83 static struct dt_descriptor external_dt __nex_bss;
84 #endif
85 
86 #ifdef CFG_SECONDARY_INIT_CNTFRQ
87 static uint32_t cntfrq;
88 #endif
89 
90 /* May be overridden in plat-$(PLATFORM)/main.c */
91 __weak void plat_primary_init_early(void)
92 {
93 }
94 DECLARE_KEEP_PAGER(plat_primary_init_early);
95 
96 /* May be overridden in plat-$(PLATFORM)/main.c */
97 __weak void main_init_gic(void)
98 {
99 }
100 
101 /* May be overridden in plat-$(PLATFORM)/main.c */
102 __weak void main_secondary_init_gic(void)
103 {
104 }
105 
106 #if defined(CFG_WITH_ARM_TRUSTED_FW)
107 void init_sec_mon(unsigned long nsec_entry __maybe_unused)
108 {
109 	assert(nsec_entry == PADDR_INVALID);
110 	/* Do nothing as we don't have a secure monitor */
111 }
112 #else
113 /* May be overridden in plat-$(PLATFORM)/main.c */
114 __weak void init_sec_mon(unsigned long nsec_entry)
115 {
116 	struct sm_nsec_ctx *nsec_ctx;
117 
118 	assert(nsec_entry != PADDR_INVALID);
119 
120 	/* Initialize secure monitor */
121 	nsec_ctx = sm_get_nsec_ctx();
122 	nsec_ctx->mon_lr = nsec_entry;
123 	nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I;
124 	if (nsec_entry & 1)
125 		nsec_ctx->mon_spsr |= CPSR_T;
126 }
127 #endif
128 
129 #if defined(CFG_WITH_ARM_TRUSTED_FW)
130 static void init_vfp_nsec(void)
131 {
132 }
133 #else
134 static void init_vfp_nsec(void)
135 {
136 	/* Normal world can use CP10 and CP11 (SIMD/VFP) */
137 	write_nsacr(read_nsacr() | NSACR_CP10 | NSACR_CP11);
138 }
139 #endif
140 
141 #if defined(CFG_WITH_VFP)
142 
143 #ifdef ARM32
144 static void init_vfp_sec(void)
145 {
146 	uint32_t cpacr = read_cpacr();
147 
148 	/*
149 	 * Enable Advanced SIMD functionality.
150 	 * Enable use of D16-D31 of the Floating-point Extension register
151 	 * file.
152 	 */
153 	cpacr &= ~(CPACR_ASEDIS | CPACR_D32DIS);
154 	/*
155 	 * Enable usage of CP10 and CP11 (SIMD/VFP) (both kernel and user
156 	 * mode.
157 	 */
158 	cpacr |= CPACR_CP(10, CPACR_CP_ACCESS_FULL);
159 	cpacr |= CPACR_CP(11, CPACR_CP_ACCESS_FULL);
160 	write_cpacr(cpacr);
161 }
162 #endif /* ARM32 */
163 
164 #ifdef ARM64
165 static void init_vfp_sec(void)
166 {
167 	/* Not using VFP until thread_kernel_enable_vfp() */
168 	vfp_disable();
169 }
170 #endif /* ARM64 */
171 
172 #else /* CFG_WITH_VFP */
173 
174 static void init_vfp_sec(void)
175 {
176 	/* Not using VFP */
177 }
178 #endif
179 
180 #ifdef CFG_SECONDARY_INIT_CNTFRQ
181 static void primary_save_cntfrq(void)
182 {
183 	assert(cntfrq == 0);
184 
185 	/*
186 	 * CNTFRQ should be initialized on the primary CPU by a
187 	 * previous boot stage
188 	 */
189 	cntfrq = read_cntfrq();
190 }
191 
192 static void secondary_init_cntfrq(void)
193 {
194 	assert(cntfrq != 0);
195 	write_cntfrq(cntfrq);
196 }
197 #else /* CFG_SECONDARY_INIT_CNTFRQ */
198 static void primary_save_cntfrq(void)
199 {
200 }
201 
202 static void secondary_init_cntfrq(void)
203 {
204 }
205 #endif
206 
207 #ifdef CFG_CORE_SANITIZE_KADDRESS
208 static void init_run_constructors(void)
209 {
210 	const vaddr_t *ctor;
211 
212 	for (ctor = &__ctor_list; ctor < &__ctor_end; ctor++)
213 		((void (*)(void))(*ctor))();
214 }
215 
216 static void init_asan(void)
217 {
218 
219 	/*
220 	 * CFG_ASAN_SHADOW_OFFSET is also supplied as
221 	 * -fasan-shadow-offset=$(CFG_ASAN_SHADOW_OFFSET) to the compiler.
222 	 * Since all the needed values to calculate the value of
223 	 * CFG_ASAN_SHADOW_OFFSET isn't available in to make we need to
224 	 * calculate it in advance and hard code it into the platform
225 	 * conf.mk. Here where we have all the needed values we double
226 	 * check that the compiler is supplied the correct value.
227 	 */
228 
229 #define __ASAN_SHADOW_START \
230 	ROUNDUP(TEE_RAM_VA_START + (TEE_RAM_VA_SIZE * 8) / 9 - 8, 8)
231 	assert(__ASAN_SHADOW_START == (vaddr_t)&__asan_shadow_start);
232 #define __CFG_ASAN_SHADOW_OFFSET \
233 	(__ASAN_SHADOW_START - (TEE_RAM_VA_START / 8))
234 	COMPILE_TIME_ASSERT(CFG_ASAN_SHADOW_OFFSET == __CFG_ASAN_SHADOW_OFFSET);
235 #undef __ASAN_SHADOW_START
236 #undef __CFG_ASAN_SHADOW_OFFSET
237 
238 	/*
239 	 * Assign area covered by the shadow area, everything from start up
240 	 * to the beginning of the shadow area.
241 	 */
242 	asan_set_shadowed((void *)TEE_TEXT_VA_START, &__asan_shadow_start);
243 
244 	/*
245 	 * Add access to areas that aren't opened automatically by a
246 	 * constructor.
247 	 */
248 	asan_tag_access(&__ctor_list, &__ctor_end);
249 	asan_tag_access(__rodata_start, __rodata_end);
250 #ifdef CFG_WITH_PAGER
251 	asan_tag_access(__pageable_start, __pageable_end);
252 #endif /*CFG_WITH_PAGER*/
253 	asan_tag_access(__nozi_start, __nozi_end);
254 	asan_tag_access(__exidx_start, __exidx_end);
255 	asan_tag_access(__extab_start, __extab_end);
256 
257 	init_run_constructors();
258 
259 	/* Everything is tagged correctly, let's start address sanitizing. */
260 	asan_start();
261 }
262 #else /*CFG_CORE_SANITIZE_KADDRESS*/
263 static void init_asan(void)
264 {
265 }
266 #endif /*CFG_CORE_SANITIZE_KADDRESS*/
267 
268 #ifdef CFG_WITH_PAGER
269 
270 #ifdef CFG_CORE_SANITIZE_KADDRESS
271 static void carve_out_asan_mem(tee_mm_pool_t *pool)
272 {
273 	const size_t s = pool->hi - pool->lo;
274 	tee_mm_entry_t *mm;
275 	paddr_t apa = ASAN_MAP_PA;
276 	size_t asz = ASAN_MAP_SZ;
277 
278 	if (core_is_buffer_outside(apa, asz, pool->lo, s))
279 		return;
280 
281 	/* Reserve the shadow area */
282 	if (!core_is_buffer_inside(apa, asz, pool->lo, s)) {
283 		if (apa < pool->lo) {
284 			/*
285 			 * ASAN buffer is overlapping with the beginning of
286 			 * the pool.
287 			 */
288 			asz -= pool->lo - apa;
289 			apa = pool->lo;
290 		} else {
291 			/*
292 			 * ASAN buffer is overlapping with the end of the
293 			 * pool.
294 			 */
295 			asz = pool->hi - apa;
296 		}
297 	}
298 	mm = tee_mm_alloc2(pool, apa, asz);
299 	assert(mm);
300 }
301 #else
302 static void carve_out_asan_mem(tee_mm_pool_t *pool __unused)
303 {
304 }
305 #endif
306 
307 static void print_pager_pool_size(void)
308 {
309 	struct tee_pager_stats __maybe_unused stats;
310 
311 	tee_pager_get_stats(&stats);
312 	IMSG("Pager pool size: %zukB",
313 		stats.npages_all * SMALL_PAGE_SIZE / 1024);
314 }
315 
316 static void init_vcore(tee_mm_pool_t *mm_vcore)
317 {
318 	const vaddr_t begin = VCORE_START_VA;
319 	vaddr_t end = begin + TEE_RAM_VA_SIZE;
320 
321 #ifdef CFG_CORE_SANITIZE_KADDRESS
322 	/* Carve out asan memory, flat maped after core memory */
323 	if (end > ASAN_SHADOW_PA)
324 		end = ASAN_MAP_PA;
325 #endif
326 
327 	if (!tee_mm_init(mm_vcore, begin, end, SMALL_PAGE_SHIFT,
328 			 TEE_MM_POOL_NO_FLAGS))
329 		panic("tee_mm_vcore init failed");
330 }
331 
332 /*
333  * With CFG_CORE_ASLR=y the init part is relocated very early during boot.
334  * The init part is also paged just as the rest of the normal paged code, with
335  * the difference that it's preloaded during boot. When the backing store
336  * is configured the entire paged binary is copied in place and then also
337  * the init part. Since the init part has been relocated (references to
338  * addresses updated to compensate for the new load address) this has to be
339  * undone for the hashes of those pages to match with the original binary.
340  *
341  * If CFG_CORE_ASLR=n, nothing needs to be done as the code/ro pages are
342  * unchanged.
343  */
344 static void undo_init_relocation(uint8_t *paged_store __maybe_unused)
345 {
346 #ifdef CFG_CORE_ASLR
347 	unsigned long *ptr = NULL;
348 	const uint32_t *reloc = NULL;
349 	const uint32_t *reloc_end = NULL;
350 	unsigned long offs = boot_mmu_config.load_offset;
351 	const struct boot_embdata *embdata = (const void *)__init_end;
352 	vaddr_t addr_end = (vaddr_t)__init_end - offs - TEE_RAM_START;
353 	vaddr_t addr_start = (vaddr_t)__init_start - offs - TEE_RAM_START;
354 
355 	reloc = (const void *)((vaddr_t)embdata + embdata->reloc_offset);
356 	reloc_end = reloc + embdata->reloc_len / sizeof(*reloc);
357 
358 	for (; reloc < reloc_end; reloc++) {
359 		if (*reloc < addr_start)
360 			continue;
361 		if (*reloc >= addr_end)
362 			break;
363 		ptr = (void *)(paged_store + *reloc - addr_start);
364 		*ptr -= offs;
365 	}
366 #endif
367 }
368 
369 static struct fobj *ro_paged_alloc(tee_mm_entry_t *mm, void *hashes,
370 				   void *store)
371 {
372 	const unsigned int num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE;
373 #ifdef CFG_CORE_ASLR
374 	unsigned int reloc_offs = (vaddr_t)__pageable_start - VCORE_START_VA;
375 	const struct boot_embdata *embdata = (const void *)__init_end;
376 	const void *reloc = __init_end + embdata->reloc_offset;
377 
378 	return fobj_ro_reloc_paged_alloc(num_pages, hashes, reloc_offs,
379 					 reloc, embdata->reloc_len, store);
380 #else
381 	return fobj_ro_paged_alloc(num_pages, hashes, store);
382 #endif
383 }
384 
385 static void init_runtime(unsigned long pageable_part)
386 {
387 	size_t n;
388 	size_t init_size = (size_t)(__init_end - __init_start);
389 	size_t pageable_start = (size_t)__pageable_start;
390 	size_t pageable_end = (size_t)__pageable_end;
391 	size_t pageable_size = pageable_end - pageable_start;
392 	size_t tzsram_end = TZSRAM_BASE + TZSRAM_SIZE;
393 	size_t hash_size = (pageable_size / SMALL_PAGE_SIZE) *
394 			   TEE_SHA256_HASH_SIZE;
395 	const struct boot_embdata *embdata = (const void *)__init_end;
396 	const void *tmp_hashes = NULL;
397 	tee_mm_entry_t *mm = NULL;
398 	struct fobj *fobj = NULL;
399 	uint8_t *paged_store = NULL;
400 	uint8_t *hashes = NULL;
401 
402 	assert(pageable_size % SMALL_PAGE_SIZE == 0);
403 	assert(embdata->total_len >= embdata->hashes_offset +
404 				     embdata->hashes_len);
405 	assert(hash_size == embdata->hashes_len);
406 
407 	tmp_hashes = __init_end + embdata->hashes_offset;
408 
409 	init_asan();
410 
411 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
412 	malloc_add_pool(__heap2_start, __heap2_end - __heap2_start);
413 
414 	/*
415 	 * This needs to be initialized early to support address lookup
416 	 * in MEM_AREA_TEE_RAM
417 	 */
418 	tee_pager_early_init();
419 
420 	hashes = malloc(hash_size);
421 	IMSG_RAW("\n");
422 	IMSG("Pager is enabled. Hashes: %zu bytes", hash_size);
423 	assert(hashes);
424 	asan_memcpy_unchecked(hashes, tmp_hashes, hash_size);
425 
426 	/*
427 	 * Need tee_mm_sec_ddr initialized to be able to allocate secure
428 	 * DDR below.
429 	 */
430 	core_mmu_init_ta_ram();
431 
432 	carve_out_asan_mem(&tee_mm_sec_ddr);
433 
434 	mm = tee_mm_alloc(&tee_mm_sec_ddr, pageable_size);
435 	assert(mm);
436 	paged_store = phys_to_virt(tee_mm_get_smem(mm), MEM_AREA_TA_RAM);
437 	/*
438 	 * Load pageable part in the dedicated allocated area:
439 	 * - Move pageable non-init part into pageable area. Note bootloader
440 	 *   may have loaded it anywhere in TA RAM hence use memmove().
441 	 * - Copy pageable init part from current location into pageable area.
442 	 */
443 	memmove(paged_store + init_size,
444 		phys_to_virt(pageable_part,
445 			     core_mmu_get_type_by_pa(pageable_part)),
446 		__pageable_part_end - __pageable_part_start);
447 	asan_memcpy_unchecked(paged_store, __init_start, init_size);
448 	/*
449 	 * Undo eventual relocation for the init part so the hash checks
450 	 * can pass.
451 	 */
452 	undo_init_relocation(paged_store);
453 
454 	/* Check that hashes of what's in pageable area is OK */
455 	DMSG("Checking hashes of pageable area");
456 	for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) {
457 		const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE;
458 		const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE;
459 		TEE_Result res;
460 
461 		DMSG("hash pg_idx %zu hash %p page %p", n, hash, page);
462 		res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE);
463 		if (res != TEE_SUCCESS) {
464 			EMSG("Hash failed for page %zu at %p: res 0x%x",
465 			     n, (void *)page, res);
466 			panic();
467 		}
468 	}
469 
470 	/*
471 	 * Assert prepaged init sections are page aligned so that nothing
472 	 * trails uninited at the end of the premapped init area.
473 	 */
474 	assert(!(init_size & SMALL_PAGE_MASK));
475 
476 	/*
477 	 * Initialize the virtual memory pool used for main_mmu_l2_ttb which
478 	 * is supplied to tee_pager_init() below.
479 	 */
480 	init_vcore(&tee_mm_vcore);
481 
482 	/*
483 	 * Assign alias area for pager end of the small page block the rest
484 	 * of the binary is loaded into. We're taking more than needed, but
485 	 * we're guaranteed to not need more than the physical amount of
486 	 * TZSRAM.
487 	 */
488 	mm = tee_mm_alloc2(&tee_mm_vcore,
489 		(vaddr_t)tee_mm_vcore.hi - TZSRAM_SIZE, TZSRAM_SIZE);
490 	assert(mm);
491 	tee_pager_set_alias_area(mm);
492 
493 	/*
494 	 * Claim virtual memory which isn't paged.
495 	 * Linear memory (flat map core memory) ends there.
496 	 */
497 	mm = tee_mm_alloc2(&tee_mm_vcore, VCORE_UNPG_RX_PA,
498 			   (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA));
499 	assert(mm);
500 
501 	/*
502 	 * Allocate virtual memory for the pageable area and let the pager
503 	 * take charge of all the pages already assigned to that memory.
504 	 */
505 	mm = tee_mm_alloc2(&tee_mm_vcore, (vaddr_t)__pageable_start,
506 			   pageable_size);
507 	assert(mm);
508 	fobj = ro_paged_alloc(mm, hashes, paged_store);
509 	assert(fobj);
510 	tee_pager_add_core_area(tee_mm_get_smem(mm), PAGER_AREA_TYPE_RO, fobj);
511 	fobj_put(fobj);
512 
513 	tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false);
514 	tee_pager_add_pages(pageable_start + init_size,
515 			    (pageable_size - init_size) / SMALL_PAGE_SIZE,
516 			    true);
517 	if (pageable_end < tzsram_end)
518 		tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) /
519 						   SMALL_PAGE_SIZE, true);
520 
521 	/*
522 	 * There may be physical pages in TZSRAM before the core load address.
523 	 * These pages can be added to the physical pages pool of the pager.
524 	 * This setup may happen when a the secure bootloader runs in TZRAM
525 	 * and its memory can be reused by OP-TEE once boot stages complete.
526 	 */
527 	tee_pager_add_pages(tee_mm_vcore.lo,
528 			(VCORE_UNPG_RX_PA - tee_mm_vcore.lo) / SMALL_PAGE_SIZE,
529 			true);
530 
531 	print_pager_pool_size();
532 }
533 #else
534 
535 static void init_runtime(unsigned long pageable_part __unused)
536 {
537 	init_asan();
538 
539 	/*
540 	 * By default whole OP-TEE uses malloc, so we need to initialize
541 	 * it early. But, when virtualization is enabled, malloc is used
542 	 * only by TEE runtime, so malloc should be initialized later, for
543 	 * every virtual partition separately. Core code uses nex_malloc
544 	 * instead.
545 	 */
546 #ifdef CFG_VIRTUALIZATION
547 	nex_malloc_add_pool(__nex_heap_start, __nex_heap_end -
548 					      __nex_heap_start);
549 #else
550 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
551 #endif
552 
553 	IMSG_RAW("\n");
554 }
555 #endif
556 
557 void *get_dt(void)
558 {
559 	void *fdt = get_embedded_dt();
560 
561 	if (!fdt)
562 		fdt = get_external_dt();
563 
564 	return fdt;
565 }
566 
567 #if defined(CFG_EMBED_DTB)
568 void *get_embedded_dt(void)
569 {
570 	static bool checked;
571 
572 	assert(cpu_mmu_enabled());
573 
574 	if (!checked) {
575 		IMSG("Embedded DTB found");
576 
577 		if (fdt_check_header(embedded_secure_dtb))
578 			panic("Invalid embedded DTB");
579 
580 		checked = true;
581 	}
582 
583 	return embedded_secure_dtb;
584 }
585 #else
586 void *get_embedded_dt(void)
587 {
588 	return NULL;
589 }
590 #endif /*CFG_EMBED_DTB*/
591 
592 #if defined(CFG_DT)
593 void *get_external_dt(void)
594 {
595 	assert(cpu_mmu_enabled());
596 	return external_dt.blob;
597 }
598 
599 static TEE_Result release_external_dt(void)
600 {
601 	int ret = 0;
602 
603 	if (!external_dt.blob)
604 		return TEE_SUCCESS;
605 
606 	ret = fdt_pack(external_dt.blob);
607 	if (ret < 0) {
608 		EMSG("Failed to pack Device Tree at 0x%" PRIxPA ": error %d",
609 		     virt_to_phys(external_dt.blob), ret);
610 		panic();
611 	}
612 
613 	if (core_mmu_remove_mapping(MEM_AREA_EXT_DT, external_dt.blob,
614 				    CFG_DTB_MAX_SIZE))
615 		panic("Failed to remove temporary Device Tree mapping");
616 
617 	/* External DTB no more reached, reset pointer to invalid */
618 	external_dt.blob = NULL;
619 
620 	return TEE_SUCCESS;
621 }
622 boot_final(release_external_dt);
623 
624 #ifdef CFG_EXTERNAL_DTB_OVERLAY
625 static int add_dt_overlay_fragment(struct dt_descriptor *dt, int ioffs)
626 {
627 	char frag[32];
628 	int offs;
629 	int ret;
630 
631 	snprintf(frag, sizeof(frag), "fragment@%d", dt->frag_id);
632 	offs = fdt_add_subnode(dt->blob, ioffs, frag);
633 	if (offs < 0)
634 		return offs;
635 
636 	dt->frag_id += 1;
637 
638 	ret = fdt_setprop_string(dt->blob, offs, "target-path", "/");
639 	if (ret < 0)
640 		return -1;
641 
642 	return fdt_add_subnode(dt->blob, offs, "__overlay__");
643 }
644 
645 static int init_dt_overlay(struct dt_descriptor *dt, int __maybe_unused dt_size)
646 {
647 	int fragment;
648 	int ret;
649 
650 	ret = fdt_check_header(dt->blob);
651 	if (!ret) {
652 		fdt_for_each_subnode(fragment, dt->blob, 0)
653 			dt->frag_id += 1;
654 		return ret;
655 	}
656 
657 #ifdef CFG_DT_ADDR
658 	return fdt_create_empty_tree(dt->blob, dt_size);
659 #else
660 	return -1;
661 #endif
662 }
663 #else
664 static int add_dt_overlay_fragment(struct dt_descriptor *dt __unused, int offs)
665 {
666 	return offs;
667 }
668 
669 static int init_dt_overlay(struct dt_descriptor *dt __unused,
670 			   int dt_size __unused)
671 {
672 	return 0;
673 }
674 #endif /* CFG_EXTERNAL_DTB_OVERLAY */
675 
676 static int add_dt_path_subnode(struct dt_descriptor *dt, const char *path,
677 			       const char *subnode)
678 {
679 	int offs;
680 
681 	offs = fdt_path_offset(dt->blob, path);
682 	if (offs < 0)
683 		return -1;
684 	offs = add_dt_overlay_fragment(dt, offs);
685 	if (offs < 0)
686 		return -1;
687 	offs = fdt_add_subnode(dt->blob, offs, subnode);
688 	if (offs < 0)
689 		return -1;
690 	return offs;
691 }
692 
693 static int add_optee_dt_node(struct dt_descriptor *dt)
694 {
695 	int offs;
696 	int ret;
697 
698 	if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) {
699 		DMSG("OP-TEE Device Tree node already exists!");
700 		return 0;
701 	}
702 
703 	offs = fdt_path_offset(dt->blob, "/firmware");
704 	if (offs < 0) {
705 		offs = add_dt_path_subnode(dt, "/", "firmware");
706 		if (offs < 0)
707 			return -1;
708 	}
709 
710 	offs = fdt_add_subnode(dt->blob, offs, "optee");
711 	if (offs < 0)
712 		return -1;
713 
714 	ret = fdt_setprop_string(dt->blob, offs, "compatible",
715 				 "linaro,optee-tz");
716 	if (ret < 0)
717 		return -1;
718 	ret = fdt_setprop_string(dt->blob, offs, "method", "smc");
719 	if (ret < 0)
720 		return -1;
721 	return 0;
722 }
723 
724 #ifdef CFG_PSCI_ARM32
725 static int append_psci_compatible(void *fdt, int offs, const char *str)
726 {
727 	return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
728 }
729 
730 static int dt_add_psci_node(struct dt_descriptor *dt)
731 {
732 	int offs;
733 
734 	if (fdt_path_offset(dt->blob, "/psci") >= 0) {
735 		DMSG("PSCI Device Tree node already exists!");
736 		return 0;
737 	}
738 
739 	offs = add_dt_path_subnode(dt, "/", "psci");
740 	if (offs < 0)
741 		return -1;
742 	if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0"))
743 		return -1;
744 	if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2"))
745 		return -1;
746 	if (append_psci_compatible(dt->blob, offs, "arm,psci"))
747 		return -1;
748 	if (fdt_setprop_string(dt->blob, offs, "method", "smc"))
749 		return -1;
750 	if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND))
751 		return -1;
752 	if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF))
753 		return -1;
754 	if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON))
755 		return -1;
756 	if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF))
757 		return -1;
758 	if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET))
759 		return -1;
760 	return 0;
761 }
762 
763 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs,
764 				    const char *prefix)
765 {
766 	const size_t prefix_len = strlen(prefix);
767 	size_t l;
768 	int plen;
769 	const char *prop;
770 
771 	prop = fdt_getprop(dt->blob, offs, "compatible", &plen);
772 	if (!prop)
773 		return -1;
774 
775 	while (plen > 0) {
776 		if (memcmp(prop, prefix, prefix_len) == 0)
777 			return 0; /* match */
778 
779 		l = strlen(prop) + 1;
780 		prop += l;
781 		plen -= l;
782 	}
783 
784 	return -1;
785 }
786 
787 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt)
788 {
789 	int offs = 0;
790 
791 	while (1) {
792 		offs = fdt_next_node(dt->blob, offs, NULL);
793 		if (offs < 0)
794 			break;
795 		if (fdt_getprop(dt->blob, offs, "enable-method", NULL))
796 			continue; /* already set */
797 		if (check_node_compat_prefix(dt, offs, "arm,cortex-a"))
798 			continue; /* no compatible */
799 		if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci"))
800 			return -1;
801 		/* Need to restart scanning as offsets may have changed */
802 		offs = 0;
803 	}
804 	return 0;
805 }
806 
807 static int config_psci(struct dt_descriptor *dt)
808 {
809 	if (dt_add_psci_node(dt))
810 		return -1;
811 	return dt_add_psci_cpu_enable_methods(dt);
812 }
813 #else
814 static int config_psci(struct dt_descriptor *dt __unused)
815 {
816 	return 0;
817 }
818 #endif /*CFG_PSCI_ARM32*/
819 
820 static void set_dt_val(void *data, uint32_t cell_size, uint64_t val)
821 {
822 	if (cell_size == 1) {
823 		fdt32_t v = cpu_to_fdt32((uint32_t)val);
824 
825 		memcpy(data, &v, sizeof(v));
826 	} else {
827 		fdt64_t v = cpu_to_fdt64(val);
828 
829 		memcpy(data, &v, sizeof(v));
830 	}
831 }
832 
833 static int add_res_mem_dt_node(struct dt_descriptor *dt, const char *name,
834 			       paddr_t pa, size_t size)
835 {
836 	int offs = 0;
837 	int ret = 0;
838 	int addr_size = -1;
839 	int len_size = -1;
840 	bool found = true;
841 	char subnode_name[80] = { 0 };
842 
843 	offs = fdt_path_offset(dt->blob, "/reserved-memory");
844 
845 	if (offs < 0) {
846 		found = false;
847 		offs = 0;
848 	}
849 
850 	if (IS_ENABLED(CFG_EXTERNAL_DTB_OVERLAY)) {
851 		len_size = sizeof(paddr_t) / sizeof(uint32_t);
852 		addr_size = sizeof(paddr_t) / sizeof(uint32_t);
853 	} else {
854 		len_size = fdt_size_cells(dt->blob, offs);
855 		if (len_size < 0)
856 			return -1;
857 		addr_size = fdt_address_cells(dt->blob, offs);
858 		if (addr_size < 0)
859 			return -1;
860 	}
861 
862 	if (!found) {
863 		offs = add_dt_path_subnode(dt, "/", "reserved-memory");
864 		if (offs < 0)
865 			return -1;
866 		ret = fdt_setprop_cell(dt->blob, offs, "#address-cells",
867 				       addr_size);
868 		if (ret < 0)
869 			return -1;
870 		ret = fdt_setprop_cell(dt->blob, offs, "#size-cells", len_size);
871 		if (ret < 0)
872 			return -1;
873 		ret = fdt_setprop(dt->blob, offs, "ranges", NULL, 0);
874 		if (ret < 0)
875 			return -1;
876 	}
877 
878 	snprintf(subnode_name, sizeof(subnode_name),
879 		 "%s@0x%" PRIxPA, name, pa);
880 	offs = fdt_add_subnode(dt->blob, offs, subnode_name);
881 	if (offs >= 0) {
882 		uint32_t data[FDT_MAX_NCELLS * 2];
883 
884 		set_dt_val(data, addr_size, pa);
885 		set_dt_val(data + addr_size, len_size, size);
886 		ret = fdt_setprop(dt->blob, offs, "reg", data,
887 				  sizeof(uint32_t) * (addr_size + len_size));
888 		if (ret < 0)
889 			return -1;
890 		ret = fdt_setprop(dt->blob, offs, "no-map", NULL, 0);
891 		if (ret < 0)
892 			return -1;
893 	} else {
894 		return -1;
895 	}
896 	return 0;
897 }
898 
899 #ifdef CFG_CORE_DYN_SHM
900 static uint64_t get_dt_val_and_advance(const void *data, size_t *offs,
901 				       uint32_t cell_size)
902 {
903 	uint64_t rv = 0;
904 
905 	if (cell_size == 1) {
906 		uint32_t v;
907 
908 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
909 		*offs += sizeof(v);
910 		rv = fdt32_to_cpu(v);
911 	} else {
912 		uint64_t v;
913 
914 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
915 		*offs += sizeof(v);
916 		rv = fdt64_to_cpu(v);
917 	}
918 
919 	return rv;
920 }
921 
922 /*
923  * Find all non-secure memory from DT. Memory marked inaccessible by Secure
924  * World is ignored since it could not be mapped to be used as dynamic shared
925  * memory.
926  */
927 static int get_nsec_memory_helper(void *fdt, struct core_mmu_phys_mem *mem)
928 {
929 	const uint8_t *prop = NULL;
930 	uint64_t a = 0;
931 	uint64_t l = 0;
932 	size_t prop_offs = 0;
933 	size_t prop_len = 0;
934 	int elems_total = 0;
935 	int addr_size = 0;
936 	int len_size = 0;
937 	int offs = 0;
938 	size_t n = 0;
939 	int len = 0;
940 
941 	addr_size = fdt_address_cells(fdt, 0);
942 	if (addr_size < 0)
943 		return 0;
944 
945 	len_size = fdt_size_cells(fdt, 0);
946 	if (len_size < 0)
947 		return 0;
948 
949 	while (true) {
950 		offs = fdt_node_offset_by_prop_value(fdt, offs, "device_type",
951 						     "memory",
952 						     sizeof("memory"));
953 		if (offs < 0)
954 			break;
955 
956 		if (_fdt_get_status(fdt, offs) != (DT_STATUS_OK_NSEC |
957 						   DT_STATUS_OK_SEC))
958 			continue;
959 
960 		prop = fdt_getprop(fdt, offs, "reg", &len);
961 		if (!prop)
962 			continue;
963 
964 		prop_len = len;
965 		for (n = 0, prop_offs = 0; prop_offs < prop_len; n++) {
966 			a = get_dt_val_and_advance(prop, &prop_offs, addr_size);
967 			if (prop_offs >= prop_len) {
968 				n--;
969 				break;
970 			}
971 
972 			l = get_dt_val_and_advance(prop, &prop_offs, len_size);
973 			if (mem) {
974 				mem->type = MEM_AREA_DDR_OVERALL;
975 				mem->addr = a;
976 				mem->size = l;
977 				mem++;
978 			}
979 		}
980 
981 		elems_total += n;
982 	}
983 
984 	return elems_total;
985 }
986 
987 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt, size_t *nelems)
988 {
989 	struct core_mmu_phys_mem *mem = NULL;
990 	int elems_total = 0;
991 
992 	elems_total = get_nsec_memory_helper(fdt, NULL);
993 	if (elems_total <= 0)
994 		return NULL;
995 
996 	mem = nex_calloc(elems_total, sizeof(*mem));
997 	if (!mem)
998 		panic();
999 
1000 	elems_total = get_nsec_memory_helper(fdt, mem);
1001 	assert(elems_total > 0);
1002 
1003 	*nelems = elems_total;
1004 
1005 	return mem;
1006 }
1007 #endif /*CFG_CORE_DYN_SHM*/
1008 
1009 #ifdef CFG_CORE_RESERVED_SHM
1010 static int mark_static_shm_as_reserved(struct dt_descriptor *dt)
1011 {
1012 	vaddr_t shm_start;
1013 	vaddr_t shm_end;
1014 
1015 	core_mmu_get_mem_by_type(MEM_AREA_NSEC_SHM, &shm_start, &shm_end);
1016 	if (shm_start != shm_end)
1017 		return add_res_mem_dt_node(dt, "optee_shm",
1018 					   virt_to_phys((void *)shm_start),
1019 					   shm_end - shm_start);
1020 
1021 	DMSG("No SHM configured");
1022 	return -1;
1023 }
1024 #endif /*CFG_CORE_RESERVED_SHM*/
1025 
1026 static void init_external_dt(unsigned long phys_dt)
1027 {
1028 	struct dt_descriptor *dt = &external_dt;
1029 	void *fdt;
1030 	int ret;
1031 
1032 	if (!phys_dt) {
1033 		/*
1034 		 * No need to panic as we're not using the DT in OP-TEE
1035 		 * yet, we're only adding some nodes for normal world use.
1036 		 * This makes the switch to using DT easier as we can boot
1037 		 * a newer OP-TEE with older boot loaders. Once we start to
1038 		 * initialize devices based on DT we'll likely panic
1039 		 * instead of returning here.
1040 		 */
1041 		IMSG("No non-secure external DT");
1042 		return;
1043 	}
1044 
1045 	if (!core_mmu_add_mapping(MEM_AREA_EXT_DT, phys_dt, CFG_DTB_MAX_SIZE))
1046 		panic("Failed to map external DTB");
1047 
1048 	fdt = phys_to_virt(phys_dt, MEM_AREA_EXT_DT);
1049 	if (!fdt)
1050 		panic();
1051 
1052 	dt->blob = fdt;
1053 
1054 	ret = init_dt_overlay(dt, CFG_DTB_MAX_SIZE);
1055 	if (ret < 0) {
1056 		EMSG("Device Tree Overlay init fail @ %#lx: error %d", phys_dt,
1057 		     ret);
1058 		panic();
1059 	}
1060 
1061 	ret = fdt_open_into(fdt, fdt, CFG_DTB_MAX_SIZE);
1062 	if (ret < 0) {
1063 		EMSG("Invalid Device Tree at %#lx: error %d", phys_dt, ret);
1064 		panic();
1065 	}
1066 
1067 	IMSG("Non-secure external DT found");
1068 }
1069 
1070 static int mark_tzdram_as_reserved(struct dt_descriptor *dt)
1071 {
1072 	return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START,
1073 				   CFG_TZDRAM_SIZE);
1074 }
1075 
1076 static void update_external_dt(void)
1077 {
1078 	struct dt_descriptor *dt = &external_dt;
1079 
1080 	if (!dt->blob)
1081 		return;
1082 
1083 	if (add_optee_dt_node(dt))
1084 		panic("Failed to add OP-TEE Device Tree node");
1085 
1086 	if (config_psci(dt))
1087 		panic("Failed to config PSCI");
1088 
1089 #ifdef CFG_CORE_RESERVED_SHM
1090 	if (mark_static_shm_as_reserved(dt))
1091 		panic("Failed to config non-secure memory");
1092 #endif
1093 
1094 	if (mark_tzdram_as_reserved(dt))
1095 		panic("Failed to config secure memory");
1096 }
1097 #else /*CFG_DT*/
1098 void *get_external_dt(void)
1099 {
1100 	return NULL;
1101 }
1102 
1103 static void init_external_dt(unsigned long phys_dt __unused)
1104 {
1105 }
1106 
1107 static void update_external_dt(void)
1108 {
1109 }
1110 
1111 #ifdef CFG_CORE_DYN_SHM
1112 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt __unused,
1113 						 size_t *nelems __unused)
1114 {
1115 	return NULL;
1116 }
1117 #endif /*CFG_CORE_DYN_SHM*/
1118 #endif /*!CFG_DT*/
1119 
1120 #ifdef CFG_CORE_DYN_SHM
1121 static void discover_nsec_memory(void)
1122 {
1123 	struct core_mmu_phys_mem *mem;
1124 	const struct core_mmu_phys_mem *mem_begin = NULL;
1125 	const struct core_mmu_phys_mem *mem_end = NULL;
1126 	size_t nelems;
1127 	void *fdt = get_external_dt();
1128 
1129 	if (fdt) {
1130 		mem = get_nsec_memory(fdt, &nelems);
1131 		if (mem) {
1132 			core_mmu_set_discovered_nsec_ddr(mem, nelems);
1133 			return;
1134 		}
1135 
1136 		DMSG("No non-secure memory found in FDT");
1137 	}
1138 
1139 	mem_begin = phys_ddr_overall_begin;
1140 	mem_end = phys_ddr_overall_end;
1141 	nelems = mem_end - mem_begin;
1142 	if (nelems) {
1143 		/*
1144 		 * Platform cannot use both register_ddr() and the now
1145 		 * deprecated register_dynamic_shm().
1146 		 */
1147 		assert(phys_ddr_overall_compat_begin ==
1148 		       phys_ddr_overall_compat_end);
1149 	} else {
1150 		mem_begin = phys_ddr_overall_compat_begin;
1151 		mem_end = phys_ddr_overall_compat_end;
1152 		nelems = mem_end - mem_begin;
1153 		if (!nelems)
1154 			return;
1155 		DMSG("Warning register_dynamic_shm() is deprecated, please use register_ddr() instead");
1156 	}
1157 
1158 	mem = nex_calloc(nelems, sizeof(*mem));
1159 	if (!mem)
1160 		panic();
1161 
1162 	memcpy(mem, phys_ddr_overall_begin, sizeof(*mem) * nelems);
1163 	core_mmu_set_discovered_nsec_ddr(mem, nelems);
1164 }
1165 #else /*CFG_CORE_DYN_SHM*/
1166 static void discover_nsec_memory(void)
1167 {
1168 }
1169 #endif /*!CFG_CORE_DYN_SHM*/
1170 
1171 void init_tee_runtime(void)
1172 {
1173 #ifdef CFG_VIRTUALIZATION
1174 	/* We need to initialize pool for every virtual guest partition */
1175 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
1176 #endif
1177 
1178 #ifndef CFG_WITH_PAGER
1179 	/* Pager initializes TA RAM early */
1180 	core_mmu_init_ta_ram();
1181 #endif
1182 	call_initcalls();
1183 }
1184 
1185 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry)
1186 {
1187 	/*
1188 	 * Mask asynchronous exceptions before switch to the thread vector
1189 	 * as the thread handler requires those to be masked while
1190 	 * executing with the temporary stack. The thread subsystem also
1191 	 * asserts that the foreign interrupts are blocked when using most of
1192 	 * its functions.
1193 	 */
1194 	thread_set_exceptions(THREAD_EXCP_ALL);
1195 	primary_save_cntfrq();
1196 	init_vfp_sec();
1197 	/*
1198 	 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must
1199 	 * set a current thread right now to avoid a chicken-and-egg problem
1200 	 * (thread_init_boot_thread() sets the current thread but needs
1201 	 * things set by init_runtime()).
1202 	 */
1203 	thread_get_core_local()->curr_thread = 0;
1204 	init_runtime(pageable_part);
1205 
1206 	if (IS_ENABLED(CFG_VIRTUALIZATION)) {
1207 		/*
1208 		 * Virtualization: We can't initialize threads right now because
1209 		 * threads belong to "tee" part and will be initialized
1210 		 * separately per each new virtual guest. So, we'll clear
1211 		 * "curr_thread" and call it done.
1212 		 */
1213 		thread_get_core_local()->curr_thread = -1;
1214 	} else {
1215 		thread_init_boot_thread();
1216 	}
1217 	thread_init_primary();
1218 	thread_init_per_cpu();
1219 	init_sec_mon(nsec_entry);
1220 }
1221 
1222 /*
1223  * Note: this function is weak just to make it possible to exclude it from
1224  * the unpaged area.
1225  */
1226 void __weak boot_init_primary_late(unsigned long fdt)
1227 {
1228 	init_external_dt(fdt);
1229 	tpm_map_log_area(get_external_dt());
1230 	discover_nsec_memory();
1231 	update_external_dt();
1232 	configure_console_from_dt();
1233 
1234 	IMSG("OP-TEE version: %s", core_v_str);
1235 	IMSG("Primary CPU initializing");
1236 #ifdef CFG_CORE_ASLR
1237 	DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA,
1238 	     (unsigned long)boot_mmu_config.load_offset, VCORE_START_VA);
1239 #endif
1240 
1241 	main_init_gic();
1242 	init_vfp_nsec();
1243 #ifndef CFG_VIRTUALIZATION
1244 	init_tee_runtime();
1245 #endif
1246 #ifdef CFG_VIRTUALIZATION
1247 	IMSG("Initializing virtualization support");
1248 	core_mmu_init_virtualization();
1249 #endif
1250 	call_finalcalls();
1251 	IMSG("Primary CPU switching to normal world boot");
1252 }
1253 
1254 static void init_secondary_helper(unsigned long nsec_entry)
1255 {
1256 	IMSG("Secondary CPU %zu initializing", get_core_pos());
1257 
1258 	/*
1259 	 * Mask asynchronous exceptions before switch to the thread vector
1260 	 * as the thread handler requires those to be masked while
1261 	 * executing with the temporary stack. The thread subsystem also
1262 	 * asserts that the foreign interrupts are blocked when using most of
1263 	 * its functions.
1264 	 */
1265 	thread_set_exceptions(THREAD_EXCP_ALL);
1266 
1267 	secondary_init_cntfrq();
1268 	thread_init_per_cpu();
1269 	init_sec_mon(nsec_entry);
1270 	main_secondary_init_gic();
1271 	init_vfp_sec();
1272 	init_vfp_nsec();
1273 
1274 	IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos());
1275 }
1276 
1277 /*
1278  * Note: this function is weak just to make it possible to exclude it from
1279  * the unpaged area so that it lies in the init area.
1280  */
1281 void __weak boot_init_primary_early(unsigned long pageable_part,
1282 				    unsigned long nsec_entry __maybe_unused)
1283 {
1284 	unsigned long e = PADDR_INVALID;
1285 
1286 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
1287 	e = nsec_entry;
1288 #endif
1289 
1290 	init_primary(pageable_part, e);
1291 }
1292 
1293 #if defined(CFG_WITH_ARM_TRUSTED_FW)
1294 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused,
1295 				  unsigned long a1 __unused)
1296 {
1297 	init_secondary_helper(PADDR_INVALID);
1298 	return 0;
1299 }
1300 #else
1301 void boot_init_secondary(unsigned long nsec_entry)
1302 {
1303 	init_secondary_helper(nsec_entry);
1304 }
1305 #endif
1306 
1307 #if defined(CFG_BOOT_SECONDARY_REQUEST)
1308 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry,
1309 			    uintptr_t context_id)
1310 {
1311 	ns_entry_contexts[core_idx].entry_point = entry;
1312 	ns_entry_contexts[core_idx].context_id = context_id;
1313 	dsb_ishst();
1314 }
1315 
1316 int boot_core_release(size_t core_idx, paddr_t entry)
1317 {
1318 	if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE)
1319 		return -1;
1320 
1321 	ns_entry_contexts[core_idx].entry_point = entry;
1322 	dmb();
1323 	spin_table[core_idx] = 1;
1324 	dsb();
1325 	sev();
1326 
1327 	return 0;
1328 }
1329 
1330 /*
1331  * spin until secondary boot request, then returns with
1332  * the secondary core entry address.
1333  */
1334 struct ns_entry_context *boot_core_hpen(void)
1335 {
1336 #ifdef CFG_PSCI_ARM32
1337 	return &ns_entry_contexts[get_core_pos()];
1338 #else
1339 	do {
1340 		wfe();
1341 	} while (!spin_table[get_core_pos()]);
1342 	dmb();
1343 	return &ns_entry_contexts[get_core_pos()];
1344 #endif
1345 }
1346 #endif
1347 
1348 #if defined(CFG_CORE_ASLR)
1349 #if defined(CFG_DT)
1350 unsigned long __weak get_aslr_seed(void *fdt)
1351 {
1352 	int rc = fdt_check_header(fdt);
1353 	const uint64_t *seed = NULL;
1354 	int offs = 0;
1355 	int len = 0;
1356 
1357 	if (rc) {
1358 		DMSG("Bad fdt: %d", rc);
1359 		return 0;
1360 	}
1361 
1362 	offs =  fdt_path_offset(fdt, "/secure-chosen");
1363 	if (offs < 0) {
1364 		DMSG("Cannot find /secure-chosen");
1365 		return 0;
1366 	}
1367 	seed = fdt_getprop(fdt, offs, "kaslr-seed", &len);
1368 	if (!seed || len != sizeof(*seed)) {
1369 		DMSG("Cannot find valid kaslr-seed");
1370 		return 0;
1371 	}
1372 
1373 	return fdt64_to_cpu(*seed);
1374 }
1375 #else /*!CFG_DT*/
1376 unsigned long __weak get_aslr_seed(void *fdt __unused)
1377 {
1378 	DMSG("Warning: no ASLR seed");
1379 	return 0;
1380 }
1381 #endif /*!CFG_DT*/
1382 #endif /*CFG_CORE_ASLR*/
1383