xref: /optee_os/core/arch/arm/kernel/boot.c (revision a499fe129c9103839b25173d69da13a0f5888d8a)
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 	/* External DTB no more reached, reset pointer to invalid */
614 	external_dt.blob = NULL;
615 
616 	return TEE_SUCCESS;
617 }
618 boot_final(release_external_dt);
619 
620 #ifdef CFG_EXTERNAL_DTB_OVERLAY
621 static int add_dt_overlay_fragment(struct dt_descriptor *dt, int ioffs)
622 {
623 	char frag[32];
624 	int offs;
625 	int ret;
626 
627 	snprintf(frag, sizeof(frag), "fragment@%d", dt->frag_id);
628 	offs = fdt_add_subnode(dt->blob, ioffs, frag);
629 	if (offs < 0)
630 		return offs;
631 
632 	dt->frag_id += 1;
633 
634 	ret = fdt_setprop_string(dt->blob, offs, "target-path", "/");
635 	if (ret < 0)
636 		return -1;
637 
638 	return fdt_add_subnode(dt->blob, offs, "__overlay__");
639 }
640 
641 static int init_dt_overlay(struct dt_descriptor *dt, int __maybe_unused dt_size)
642 {
643 	int fragment;
644 	int ret;
645 
646 	ret = fdt_check_header(dt->blob);
647 	if (!ret) {
648 		fdt_for_each_subnode(fragment, dt->blob, 0)
649 			dt->frag_id += 1;
650 		return ret;
651 	}
652 
653 #ifdef CFG_DT_ADDR
654 	return fdt_create_empty_tree(dt->blob, dt_size);
655 #else
656 	return -1;
657 #endif
658 }
659 #else
660 static int add_dt_overlay_fragment(struct dt_descriptor *dt __unused, int offs)
661 {
662 	return offs;
663 }
664 
665 static int init_dt_overlay(struct dt_descriptor *dt __unused,
666 			   int dt_size __unused)
667 {
668 	return 0;
669 }
670 #endif /* CFG_EXTERNAL_DTB_OVERLAY */
671 
672 static int add_dt_path_subnode(struct dt_descriptor *dt, const char *path,
673 			       const char *subnode)
674 {
675 	int offs;
676 
677 	offs = fdt_path_offset(dt->blob, path);
678 	if (offs < 0)
679 		return -1;
680 	offs = add_dt_overlay_fragment(dt, offs);
681 	if (offs < 0)
682 		return -1;
683 	offs = fdt_add_subnode(dt->blob, offs, subnode);
684 	if (offs < 0)
685 		return -1;
686 	return offs;
687 }
688 
689 static int add_optee_dt_node(struct dt_descriptor *dt)
690 {
691 	int offs;
692 	int ret;
693 
694 	if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) {
695 		DMSG("OP-TEE Device Tree node already exists!");
696 		return 0;
697 	}
698 
699 	offs = fdt_path_offset(dt->blob, "/firmware");
700 	if (offs < 0) {
701 		offs = add_dt_path_subnode(dt, "/", "firmware");
702 		if (offs < 0)
703 			return -1;
704 	}
705 
706 	offs = fdt_add_subnode(dt->blob, offs, "optee");
707 	if (offs < 0)
708 		return -1;
709 
710 	ret = fdt_setprop_string(dt->blob, offs, "compatible",
711 				 "linaro,optee-tz");
712 	if (ret < 0)
713 		return -1;
714 	ret = fdt_setprop_string(dt->blob, offs, "method", "smc");
715 	if (ret < 0)
716 		return -1;
717 	return 0;
718 }
719 
720 #ifdef CFG_PSCI_ARM32
721 static int append_psci_compatible(void *fdt, int offs, const char *str)
722 {
723 	return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
724 }
725 
726 static int dt_add_psci_node(struct dt_descriptor *dt)
727 {
728 	int offs;
729 
730 	if (fdt_path_offset(dt->blob, "/psci") >= 0) {
731 		DMSG("PSCI Device Tree node already exists!");
732 		return 0;
733 	}
734 
735 	offs = add_dt_path_subnode(dt, "/", "psci");
736 	if (offs < 0)
737 		return -1;
738 	if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0"))
739 		return -1;
740 	if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2"))
741 		return -1;
742 	if (append_psci_compatible(dt->blob, offs, "arm,psci"))
743 		return -1;
744 	if (fdt_setprop_string(dt->blob, offs, "method", "smc"))
745 		return -1;
746 	if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND))
747 		return -1;
748 	if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF))
749 		return -1;
750 	if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON))
751 		return -1;
752 	if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF))
753 		return -1;
754 	if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET))
755 		return -1;
756 	return 0;
757 }
758 
759 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs,
760 				    const char *prefix)
761 {
762 	const size_t prefix_len = strlen(prefix);
763 	size_t l;
764 	int plen;
765 	const char *prop;
766 
767 	prop = fdt_getprop(dt->blob, offs, "compatible", &plen);
768 	if (!prop)
769 		return -1;
770 
771 	while (plen > 0) {
772 		if (memcmp(prop, prefix, prefix_len) == 0)
773 			return 0; /* match */
774 
775 		l = strlen(prop) + 1;
776 		prop += l;
777 		plen -= l;
778 	}
779 
780 	return -1;
781 }
782 
783 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt)
784 {
785 	int offs = 0;
786 
787 	while (1) {
788 		offs = fdt_next_node(dt->blob, offs, NULL);
789 		if (offs < 0)
790 			break;
791 		if (fdt_getprop(dt->blob, offs, "enable-method", NULL))
792 			continue; /* already set */
793 		if (check_node_compat_prefix(dt, offs, "arm,cortex-a"))
794 			continue; /* no compatible */
795 		if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci"))
796 			return -1;
797 		/* Need to restart scanning as offsets may have changed */
798 		offs = 0;
799 	}
800 	return 0;
801 }
802 
803 static int config_psci(struct dt_descriptor *dt)
804 {
805 	if (dt_add_psci_node(dt))
806 		return -1;
807 	return dt_add_psci_cpu_enable_methods(dt);
808 }
809 #else
810 static int config_psci(struct dt_descriptor *dt __unused)
811 {
812 	return 0;
813 }
814 #endif /*CFG_PSCI_ARM32*/
815 
816 static void set_dt_val(void *data, uint32_t cell_size, uint64_t val)
817 {
818 	if (cell_size == 1) {
819 		fdt32_t v = cpu_to_fdt32((uint32_t)val);
820 
821 		memcpy(data, &v, sizeof(v));
822 	} else {
823 		fdt64_t v = cpu_to_fdt64(val);
824 
825 		memcpy(data, &v, sizeof(v));
826 	}
827 }
828 
829 static int add_res_mem_dt_node(struct dt_descriptor *dt, const char *name,
830 			       paddr_t pa, size_t size)
831 {
832 	int offs = 0;
833 	int ret = 0;
834 	int addr_size = -1;
835 	int len_size = -1;
836 	bool found = true;
837 	char subnode_name[80] = { 0 };
838 
839 	offs = fdt_path_offset(dt->blob, "/reserved-memory");
840 
841 	if (offs < 0) {
842 		found = false;
843 		offs = 0;
844 	}
845 
846 	if (IS_ENABLED(CFG_EXTERNAL_DTB_OVERLAY)) {
847 		len_size = sizeof(paddr_t) / sizeof(uint32_t);
848 		addr_size = sizeof(paddr_t) / sizeof(uint32_t);
849 	} else {
850 		len_size = fdt_size_cells(dt->blob, offs);
851 		if (len_size < 0)
852 			return -1;
853 		addr_size = fdt_address_cells(dt->blob, offs);
854 		if (addr_size < 0)
855 			return -1;
856 	}
857 
858 	if (!found) {
859 		offs = add_dt_path_subnode(dt, "/", "reserved-memory");
860 		if (offs < 0)
861 			return -1;
862 		ret = fdt_setprop_cell(dt->blob, offs, "#address-cells",
863 				       addr_size);
864 		if (ret < 0)
865 			return -1;
866 		ret = fdt_setprop_cell(dt->blob, offs, "#size-cells", len_size);
867 		if (ret < 0)
868 			return -1;
869 		ret = fdt_setprop(dt->blob, offs, "ranges", NULL, 0);
870 		if (ret < 0)
871 			return -1;
872 	}
873 
874 	snprintf(subnode_name, sizeof(subnode_name),
875 		 "%s@0x%" PRIxPA, name, pa);
876 	offs = fdt_add_subnode(dt->blob, offs, subnode_name);
877 	if (offs >= 0) {
878 		uint32_t data[FDT_MAX_NCELLS * 2];
879 
880 		set_dt_val(data, addr_size, pa);
881 		set_dt_val(data + addr_size, len_size, size);
882 		ret = fdt_setprop(dt->blob, offs, "reg", data,
883 				  sizeof(uint32_t) * (addr_size + len_size));
884 		if (ret < 0)
885 			return -1;
886 		ret = fdt_setprop(dt->blob, offs, "no-map", NULL, 0);
887 		if (ret < 0)
888 			return -1;
889 	} else {
890 		return -1;
891 	}
892 	return 0;
893 }
894 
895 #ifdef CFG_CORE_DYN_SHM
896 static uint64_t get_dt_val_and_advance(const void *data, size_t *offs,
897 				       uint32_t cell_size)
898 {
899 	uint64_t rv = 0;
900 
901 	if (cell_size == 1) {
902 		uint32_t v;
903 
904 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
905 		*offs += sizeof(v);
906 		rv = fdt32_to_cpu(v);
907 	} else {
908 		uint64_t v;
909 
910 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
911 		*offs += sizeof(v);
912 		rv = fdt64_to_cpu(v);
913 	}
914 
915 	return rv;
916 }
917 
918 /*
919  * Find all non-secure memory from DT. Memory marked inaccessible by Secure
920  * World is ignored since it could not be mapped to be used as dynamic shared
921  * memory.
922  */
923 static int get_nsec_memory_helper(void *fdt, struct core_mmu_phys_mem *mem)
924 {
925 	const uint8_t *prop = NULL;
926 	uint64_t a = 0;
927 	uint64_t l = 0;
928 	size_t prop_offs = 0;
929 	size_t prop_len = 0;
930 	int elems_total = 0;
931 	int addr_size = 0;
932 	int len_size = 0;
933 	int offs = 0;
934 	size_t n = 0;
935 	int len = 0;
936 
937 	addr_size = fdt_address_cells(fdt, 0);
938 	if (addr_size < 0)
939 		return 0;
940 
941 	len_size = fdt_size_cells(fdt, 0);
942 	if (len_size < 0)
943 		return 0;
944 
945 	while (true) {
946 		offs = fdt_node_offset_by_prop_value(fdt, offs, "device_type",
947 						     "memory",
948 						     sizeof("memory"));
949 		if (offs < 0)
950 			break;
951 
952 		if (_fdt_get_status(fdt, offs) != (DT_STATUS_OK_NSEC |
953 						   DT_STATUS_OK_SEC))
954 			continue;
955 
956 		prop = fdt_getprop(fdt, offs, "reg", &len);
957 		if (!prop)
958 			continue;
959 
960 		prop_len = len;
961 		for (n = 0, prop_offs = 0; prop_offs < prop_len; n++) {
962 			a = get_dt_val_and_advance(prop, &prop_offs, addr_size);
963 			if (prop_offs >= prop_len) {
964 				n--;
965 				break;
966 			}
967 
968 			l = get_dt_val_and_advance(prop, &prop_offs, len_size);
969 			if (mem) {
970 				mem->type = MEM_AREA_DDR_OVERALL;
971 				mem->addr = a;
972 				mem->size = l;
973 				mem++;
974 			}
975 		}
976 
977 		elems_total += n;
978 	}
979 
980 	return elems_total;
981 }
982 
983 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt, size_t *nelems)
984 {
985 	struct core_mmu_phys_mem *mem = NULL;
986 	int elems_total = 0;
987 
988 	elems_total = get_nsec_memory_helper(fdt, NULL);
989 	if (elems_total <= 0)
990 		return NULL;
991 
992 	mem = nex_calloc(elems_total, sizeof(*mem));
993 	if (!mem)
994 		panic();
995 
996 	elems_total = get_nsec_memory_helper(fdt, mem);
997 	assert(elems_total > 0);
998 
999 	*nelems = elems_total;
1000 
1001 	return mem;
1002 }
1003 #endif /*CFG_CORE_DYN_SHM*/
1004 
1005 #ifdef CFG_CORE_RESERVED_SHM
1006 static int mark_static_shm_as_reserved(struct dt_descriptor *dt)
1007 {
1008 	vaddr_t shm_start;
1009 	vaddr_t shm_end;
1010 
1011 	core_mmu_get_mem_by_type(MEM_AREA_NSEC_SHM, &shm_start, &shm_end);
1012 	if (shm_start != shm_end)
1013 		return add_res_mem_dt_node(dt, "optee_shm",
1014 					   virt_to_phys((void *)shm_start),
1015 					   shm_end - shm_start);
1016 
1017 	DMSG("No SHM configured");
1018 	return -1;
1019 }
1020 #endif /*CFG_CORE_RESERVED_SHM*/
1021 
1022 static void init_external_dt(unsigned long phys_dt)
1023 {
1024 	struct dt_descriptor *dt = &external_dt;
1025 	void *fdt;
1026 	int ret;
1027 
1028 	if (!phys_dt) {
1029 		/*
1030 		 * No need to panic as we're not using the DT in OP-TEE
1031 		 * yet, we're only adding some nodes for normal world use.
1032 		 * This makes the switch to using DT easier as we can boot
1033 		 * a newer OP-TEE with older boot loaders. Once we start to
1034 		 * initialize devices based on DT we'll likely panic
1035 		 * instead of returning here.
1036 		 */
1037 		IMSG("No non-secure external DT");
1038 		return;
1039 	}
1040 
1041 	if (!core_mmu_add_mapping(MEM_AREA_EXT_DT, phys_dt, CFG_DTB_MAX_SIZE))
1042 		panic("Failed to map external DTB");
1043 
1044 	fdt = phys_to_virt(phys_dt, MEM_AREA_EXT_DT);
1045 	if (!fdt)
1046 		panic();
1047 
1048 	dt->blob = fdt;
1049 
1050 	ret = init_dt_overlay(dt, CFG_DTB_MAX_SIZE);
1051 	if (ret < 0) {
1052 		EMSG("Device Tree Overlay init fail @ %#lx: error %d", phys_dt,
1053 		     ret);
1054 		panic();
1055 	}
1056 
1057 	ret = fdt_open_into(fdt, fdt, CFG_DTB_MAX_SIZE);
1058 	if (ret < 0) {
1059 		EMSG("Invalid Device Tree at %#lx: error %d", phys_dt, ret);
1060 		panic();
1061 	}
1062 
1063 	IMSG("Non-secure external DT found");
1064 }
1065 
1066 static int mark_tzdram_as_reserved(struct dt_descriptor *dt)
1067 {
1068 	return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START,
1069 				   CFG_TZDRAM_SIZE);
1070 }
1071 
1072 static void update_external_dt(void)
1073 {
1074 	struct dt_descriptor *dt = &external_dt;
1075 
1076 	if (!dt->blob)
1077 		return;
1078 
1079 	if (add_optee_dt_node(dt))
1080 		panic("Failed to add OP-TEE Device Tree node");
1081 
1082 	if (config_psci(dt))
1083 		panic("Failed to config PSCI");
1084 
1085 #ifdef CFG_CORE_RESERVED_SHM
1086 	if (mark_static_shm_as_reserved(dt))
1087 		panic("Failed to config non-secure memory");
1088 #endif
1089 
1090 	if (mark_tzdram_as_reserved(dt))
1091 		panic("Failed to config secure memory");
1092 }
1093 #else /*CFG_DT*/
1094 void *get_external_dt(void)
1095 {
1096 	return NULL;
1097 }
1098 
1099 static void init_external_dt(unsigned long phys_dt __unused)
1100 {
1101 }
1102 
1103 static void update_external_dt(void)
1104 {
1105 }
1106 
1107 #ifdef CFG_CORE_DYN_SHM
1108 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt __unused,
1109 						 size_t *nelems __unused)
1110 {
1111 	return NULL;
1112 }
1113 #endif /*CFG_CORE_DYN_SHM*/
1114 #endif /*!CFG_DT*/
1115 
1116 #ifdef CFG_CORE_DYN_SHM
1117 static void discover_nsec_memory(void)
1118 {
1119 	struct core_mmu_phys_mem *mem;
1120 	const struct core_mmu_phys_mem *mem_begin = NULL;
1121 	const struct core_mmu_phys_mem *mem_end = NULL;
1122 	size_t nelems;
1123 	void *fdt = get_external_dt();
1124 
1125 	if (fdt) {
1126 		mem = get_nsec_memory(fdt, &nelems);
1127 		if (mem) {
1128 			core_mmu_set_discovered_nsec_ddr(mem, nelems);
1129 			return;
1130 		}
1131 
1132 		DMSG("No non-secure memory found in FDT");
1133 	}
1134 
1135 	mem_begin = phys_ddr_overall_begin;
1136 	mem_end = phys_ddr_overall_end;
1137 	nelems = mem_end - mem_begin;
1138 	if (nelems) {
1139 		/*
1140 		 * Platform cannot use both register_ddr() and the now
1141 		 * deprecated register_dynamic_shm().
1142 		 */
1143 		assert(phys_ddr_overall_compat_begin ==
1144 		       phys_ddr_overall_compat_end);
1145 	} else {
1146 		mem_begin = phys_ddr_overall_compat_begin;
1147 		mem_end = phys_ddr_overall_compat_end;
1148 		nelems = mem_end - mem_begin;
1149 		if (!nelems)
1150 			return;
1151 		DMSG("Warning register_dynamic_shm() is deprecated, please use register_ddr() instead");
1152 	}
1153 
1154 	mem = nex_calloc(nelems, sizeof(*mem));
1155 	if (!mem)
1156 		panic();
1157 
1158 	memcpy(mem, phys_ddr_overall_begin, sizeof(*mem) * nelems);
1159 	core_mmu_set_discovered_nsec_ddr(mem, nelems);
1160 }
1161 #else /*CFG_CORE_DYN_SHM*/
1162 static void discover_nsec_memory(void)
1163 {
1164 }
1165 #endif /*!CFG_CORE_DYN_SHM*/
1166 
1167 void init_tee_runtime(void)
1168 {
1169 #ifdef CFG_VIRTUALIZATION
1170 	/* We need to initialize pool for every virtual guest partition */
1171 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
1172 #endif
1173 
1174 #ifndef CFG_WITH_PAGER
1175 	/* Pager initializes TA RAM early */
1176 	core_mmu_init_ta_ram();
1177 #endif
1178 	call_initcalls();
1179 }
1180 
1181 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry)
1182 {
1183 	/*
1184 	 * Mask asynchronous exceptions before switch to the thread vector
1185 	 * as the thread handler requires those to be masked while
1186 	 * executing with the temporary stack. The thread subsystem also
1187 	 * asserts that the foreign interrupts are blocked when using most of
1188 	 * its functions.
1189 	 */
1190 	thread_set_exceptions(THREAD_EXCP_ALL);
1191 	primary_save_cntfrq();
1192 	init_vfp_sec();
1193 	/*
1194 	 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must
1195 	 * set a current thread right now to avoid a chicken-and-egg problem
1196 	 * (thread_init_boot_thread() sets the current thread but needs
1197 	 * things set by init_runtime()).
1198 	 */
1199 	thread_get_core_local()->curr_thread = 0;
1200 	init_runtime(pageable_part);
1201 
1202 	if (IS_ENABLED(CFG_VIRTUALIZATION)) {
1203 		/*
1204 		 * Virtualization: We can't initialize threads right now because
1205 		 * threads belong to "tee" part and will be initialized
1206 		 * separately per each new virtual guest. So, we'll clear
1207 		 * "curr_thread" and call it done.
1208 		 */
1209 		thread_get_core_local()->curr_thread = -1;
1210 	} else {
1211 		thread_init_boot_thread();
1212 	}
1213 	thread_init_primary();
1214 	thread_init_per_cpu();
1215 	init_sec_mon(nsec_entry);
1216 }
1217 
1218 /*
1219  * Note: this function is weak just to make it possible to exclude it from
1220  * the unpaged area.
1221  */
1222 void __weak paged_init_primary(unsigned long fdt)
1223 {
1224 	init_external_dt(fdt);
1225 	tpm_map_log_area(get_external_dt());
1226 	discover_nsec_memory();
1227 	update_external_dt();
1228 	configure_console_from_dt();
1229 
1230 	IMSG("OP-TEE version: %s", core_v_str);
1231 	IMSG("Primary CPU initializing");
1232 #ifdef CFG_CORE_ASLR
1233 	DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA,
1234 	     (unsigned long)boot_mmu_config.load_offset, VCORE_START_VA);
1235 #endif
1236 
1237 	main_init_gic();
1238 	init_vfp_nsec();
1239 #ifndef CFG_VIRTUALIZATION
1240 	init_tee_runtime();
1241 #endif
1242 #ifdef CFG_VIRTUALIZATION
1243 	IMSG("Initializing virtualization support");
1244 	core_mmu_init_virtualization();
1245 #endif
1246 	call_finalcalls();
1247 	IMSG("Primary CPU switching to normal world boot");
1248 }
1249 
1250 static void init_secondary_helper(unsigned long nsec_entry)
1251 {
1252 	IMSG("Secondary CPU %zu initializing", get_core_pos());
1253 
1254 	/*
1255 	 * Mask asynchronous exceptions before switch to the thread vector
1256 	 * as the thread handler requires those to be masked while
1257 	 * executing with the temporary stack. The thread subsystem also
1258 	 * asserts that the foreign interrupts are blocked when using most of
1259 	 * its functions.
1260 	 */
1261 	thread_set_exceptions(THREAD_EXCP_ALL);
1262 
1263 	secondary_init_cntfrq();
1264 	thread_init_per_cpu();
1265 	init_sec_mon(nsec_entry);
1266 	main_secondary_init_gic();
1267 	init_vfp_sec();
1268 	init_vfp_nsec();
1269 
1270 	IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos());
1271 }
1272 
1273 /*
1274  * Note: this function is weak just to make it possible to exclude it from
1275  * the unpaged area so that it lies in the init area.
1276  */
1277 void __weak boot_init_primary(unsigned long pageable_part,
1278 			      unsigned long nsec_entry __maybe_unused,
1279 			      unsigned long fdt)
1280 {
1281 	unsigned long e = PADDR_INVALID;
1282 
1283 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
1284 	e = nsec_entry;
1285 #endif
1286 
1287 	init_primary(pageable_part, e);
1288 	paged_init_primary(fdt);
1289 }
1290 
1291 #if defined(CFG_WITH_ARM_TRUSTED_FW)
1292 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused,
1293 				  unsigned long a1 __unused)
1294 {
1295 	init_secondary_helper(PADDR_INVALID);
1296 	return 0;
1297 }
1298 #else
1299 void boot_init_secondary(unsigned long nsec_entry)
1300 {
1301 	init_secondary_helper(nsec_entry);
1302 }
1303 #endif
1304 
1305 #if defined(CFG_BOOT_SECONDARY_REQUEST)
1306 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry,
1307 			    uintptr_t context_id)
1308 {
1309 	ns_entry_contexts[core_idx].entry_point = entry;
1310 	ns_entry_contexts[core_idx].context_id = context_id;
1311 	dsb_ishst();
1312 }
1313 
1314 int boot_core_release(size_t core_idx, paddr_t entry)
1315 {
1316 	if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE)
1317 		return -1;
1318 
1319 	ns_entry_contexts[core_idx].entry_point = entry;
1320 	dmb();
1321 	spin_table[core_idx] = 1;
1322 	dsb();
1323 	sev();
1324 
1325 	return 0;
1326 }
1327 
1328 /*
1329  * spin until secondary boot request, then returns with
1330  * the secondary core entry address.
1331  */
1332 struct ns_entry_context *boot_core_hpen(void)
1333 {
1334 #ifdef CFG_PSCI_ARM32
1335 	return &ns_entry_contexts[get_core_pos()];
1336 #else
1337 	do {
1338 		wfe();
1339 	} while (!spin_table[get_core_pos()]);
1340 	dmb();
1341 	return &ns_entry_contexts[get_core_pos()];
1342 #endif
1343 }
1344 #endif
1345 
1346 #if defined(CFG_CORE_ASLR)
1347 #if defined(CFG_DT)
1348 unsigned long __weak get_aslr_seed(void *fdt)
1349 {
1350 	int rc = fdt_check_header(fdt);
1351 	const uint64_t *seed = NULL;
1352 	int offs = 0;
1353 	int len = 0;
1354 
1355 	if (rc) {
1356 		DMSG("Bad fdt: %d", rc);
1357 		return 0;
1358 	}
1359 
1360 	offs =  fdt_path_offset(fdt, "/secure-chosen");
1361 	if (offs < 0) {
1362 		DMSG("Cannot find /secure-chosen");
1363 		return 0;
1364 	}
1365 	seed = fdt_getprop(fdt, offs, "kaslr-seed", &len);
1366 	if (!seed || len != sizeof(*seed)) {
1367 		DMSG("Cannot find valid kaslr-seed");
1368 		return 0;
1369 	}
1370 
1371 	return fdt64_to_cpu(*seed);
1372 }
1373 #else /*!CFG_DT*/
1374 unsigned long __weak get_aslr_seed(void *fdt __unused)
1375 {
1376 	DMSG("Warning: no ASLR seed");
1377 	return 0;
1378 }
1379 #endif /*!CFG_DT*/
1380 #endif /*CFG_CORE_ASLR*/
1381