Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.47
      1 /* $NetBSD: fdt_machdep.c,v 1.47 2018/10/29 21:05:58 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.47 2018/10/29 21:05:58 jmcneill Exp $");
     31 
     32 #include "opt_machdep.h"
     33 #include "opt_bootconfig.h"
     34 #include "opt_ddb.h"
     35 #include "opt_md.h"
     36 #include "opt_arm_debug.h"
     37 #include "opt_multiprocessor.h"
     38 #include "opt_cpuoptions.h"
     39 #include "opt_efi.h"
     40 
     41 #include "ukbd.h"
     42 #include "wsdisplay.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/bus.h>
     47 #include <sys/atomic.h>
     48 #include <sys/cpu.h>
     49 #include <sys/device.h>
     50 #include <sys/exec.h>
     51 #include <sys/kernel.h>
     52 #include <sys/kmem.h>
     53 #include <sys/ksyms.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/proc.h>
     56 #include <sys/reboot.h>
     57 #include <sys/termios.h>
     58 #include <sys/extent.h>
     59 #include <sys/bootblock.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/vnode.h>
     62 #include <sys/kauth.h>
     63 #include <sys/fcntl.h>
     64 #include <sys/md5.h>
     65 
     66 #include <dev/cons.h>
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <sys/conf.h>
     70 
     71 #include <machine/db_machdep.h>
     72 #include <ddb/db_sym.h>
     73 #include <ddb/db_extern.h>
     74 
     75 #include <machine/bootconfig.h>
     76 #include <arm/armreg.h>
     77 
     78 #include <arm/cpufunc.h>
     79 
     80 #include <evbarm/include/autoconf.h>
     81 #include <evbarm/fdt/machdep.h>
     82 #include <evbarm/fdt/platform.h>
     83 
     84 #include <arm/fdt/arm_fdtvar.h>
     85 
     86 #ifdef EFI_RUNTIME
     87 #include <arm/arm/efi_runtime.h>
     88 #endif
     89 
     90 #if NUKBD > 0
     91 #include <dev/usb/ukbdvar.h>
     92 #endif
     93 #if NWSDISPLAY > 0
     94 #include <dev/wscons/wsdisplayvar.h>
     95 #endif
     96 
     97 #ifdef MEMORY_DISK_DYNAMIC
     98 #include <dev/md.h>
     99 #endif
    100 
    101 #ifndef FDT_MAX_BOOT_STRING
    102 #define FDT_MAX_BOOT_STRING 1024
    103 #endif
    104 
    105 BootConfig bootconfig;
    106 char bootargs[FDT_MAX_BOOT_STRING] = "";
    107 char *boot_args = NULL;
    108 
    109 /* filled in before cleaning bss. keep in .data */
    110 u_long uboot_args[4] __attribute__((__section__(".data")));
    111 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
    112 
    113 static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)];
    114 static struct extent *fdt_memory_ext;
    115 
    116 static uint64_t initrd_start, initrd_end;
    117 
    118 #include <libfdt.h>
    119 #include <dev/fdt/fdtvar.h>
    120 #define FDT_BUF_SIZE	(512*1024)
    121 static uint8_t fdt_data[FDT_BUF_SIZE];
    122 
    123 extern char KERNEL_BASE_phys[];
    124 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    125 
    126 static void fdt_update_stdout_path(void);
    127 static void fdt_device_register(device_t, void *);
    128 static void fdt_device_register_post_config(device_t, void *);
    129 static void fdt_cpu_rootconf(void);
    130 static void fdt_reset(void);
    131 static void fdt_powerdown(void);
    132 
    133 static dev_type_cnputc(earlyconsputc);
    134 static dev_type_cngetc(earlyconsgetc);
    135 
    136 static struct consdev earlycons = {
    137 	.cn_putc = earlyconsputc,
    138 	.cn_getc = earlyconsgetc,
    139 	.cn_pollc = nullcnpollc,
    140 };
    141 
    142 static void
    143 fdt_putchar(char c)
    144 {
    145 #ifdef EARLYCONS
    146 	const struct arm_platform *plat = arm_fdt_platform();
    147 	if (plat && plat->ap_early_putchar) {
    148 		plat->ap_early_putchar(c);
    149 	} else {
    150 		uartputc(c);
    151 	}
    152 #endif
    153 }
    154 
    155 static void
    156 earlyconsputc(dev_t dev, int c)
    157 {
    158 	fdt_putchar(c);
    159 }
    160 
    161 static int
    162 earlyconsgetc(dev_t dev)
    163 {
    164 	return 0;	/* XXX */
    165 }
    166 
    167 #ifdef VERBOSE_INIT_ARM
    168 #define VPRINTF(...)	printf(__VA_ARGS__)
    169 #else
    170 #define VPRINTF(...)	__nothing
    171 #endif
    172 
    173 /*
    174  * ARM: Get the first physically contiguous region of memory.
    175  * ARM64: Get all of physical memory, including holes.
    176  */
    177 static void
    178 fdt_get_memory(uint64_t *pstart, uint64_t *pend)
    179 {
    180 	const int memory = OF_finddevice("/memory");
    181 	uint64_t cur_addr, cur_size;
    182 	int index;
    183 
    184 	/* Assume the first entry is the start of memory */
    185 	if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
    186 		panic("Cannot determine memory size");
    187 
    188 	*pstart = cur_addr;
    189 	*pend = cur_addr + cur_size;
    190 
    191 	VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    192 	    0, *pstart, *pend - *pstart);
    193 
    194 	for (index = 1;
    195 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    196 	     index++) {
    197 		VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    198 		    index, cur_addr, cur_size);
    199 
    200 #ifdef __aarch64__
    201 		if (cur_addr + cur_size > *pend)
    202 			*pend = cur_addr + cur_size;
    203 #else
    204 		/* If subsequent entries follow the previous, append them. */
    205 		if (*pend == cur_addr)
    206 			*pend = cur_addr + cur_size;
    207 #endif
    208 	}
    209 }
    210 
    211 void
    212 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
    213 {
    214 	uint64_t start = addr;
    215 	uint64_t end = addr + size;
    216 
    217 	int error = extent_free(fdt_memory_ext, start,
    218 	     end - start, EX_NOWAIT);
    219 	if (error != 0)
    220 		printf("MEM ERROR: res %" PRIx64 "-%" PRIx64 " failed: %d\n",
    221 		    start, end, error);
    222 	else
    223 		VPRINTF("MEM: res %" PRIx64 "-%" PRIx64 "\n", start, end);
    224 }
    225 
    226 /*
    227  * Exclude memory ranges from memory config from the device tree
    228  */
    229 static void
    230 fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
    231 {
    232 	uint64_t lstart = 0, lend = 0;
    233 	uint64_t addr, size;
    234 	int index, error;
    235 
    236 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
    237 	for (index = 0; index <= num; index++) {
    238 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
    239 		    &addr, &size);
    240 		if (error != 0)
    241 			continue;
    242 		if (lstart <= addr && addr <= lend) {
    243 			size -= (lend - addr);
    244 			addr = lend;
    245 		}
    246 		if (size == 0)
    247 			continue;
    248 		if (addr + size <= min_addr)
    249 			continue;
    250 		if (addr >= max_addr)
    251 			continue;
    252 		if (addr < min_addr) {
    253 			size -= (min_addr - addr);
    254 			addr = min_addr;
    255 		}
    256 		if (addr + size > max_addr)
    257 			size = max_addr - addr;
    258 		fdt_add_reserved_memory_range(addr, size);
    259 		lstart = addr;
    260 		lend = addr + size;
    261 	}
    262 }
    263 
    264 /*
    265  * Define usable memory regions.
    266  */
    267 static void
    268 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
    269 {
    270 	const int memory = OF_finddevice("/memory");
    271 	BootConfig *bc = &bootconfig;
    272 	struct extent_region *er;
    273 	uint64_t addr, size;
    274 	int index, error;
    275 
    276 	fdt_memory_ext = extent_create("FDT Memory", mem_start, mem_end,
    277 	    fdt_memory_ext_storage, sizeof(fdt_memory_ext_storage), EX_EARLY);
    278 
    279 	for (index = 0;
    280 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
    281 	     index++) {
    282 		if (addr >= mem_end || size == 0)
    283 			continue;
    284 		if (addr + size > mem_end)
    285 			size = mem_end - addr;
    286 
    287 		error = extent_alloc_region(fdt_memory_ext, addr, size,
    288 		    EX_NOWAIT);
    289 		if (error != 0)
    290 			printf("MEM ERROR: add %" PRIx64 "-%" PRIx64 " failed: %d\n",
    291 			    addr, addr + size, error);
    292 		VPRINTF("MEM: add %" PRIx64 "-%" PRIx64 "\n", addr, addr + size);
    293 	}
    294 
    295 	fdt_add_reserved_memory(mem_start, mem_end);
    296 
    297 	const uint64_t initrd_size = initrd_end - initrd_start;
    298 	if (initrd_size > 0)
    299 		fdt_add_reserved_memory_range(initrd_start, initrd_size);
    300 
    301 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
    302 	if (framebuffer >= 0) {
    303 		for (index = 0;
    304 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
    305 		     index++) {
    306 			fdt_add_reserved_memory_range(addr, size);
    307 		}
    308 	}
    309 
    310 	VPRINTF("Usable memory:\n");
    311 	bc->dramblocks = 0;
    312 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    313 		VPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    314 		bc->dram[bc->dramblocks].address = er->er_start;
    315 		bc->dram[bc->dramblocks].pages =
    316 		    (er->er_end - er->er_start) / PAGE_SIZE;
    317 		bc->dramblocks++;
    318 	}
    319 }
    320 
    321 static void
    322 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    323 {
    324 	*pstart = *pend = 0;
    325 
    326 #ifdef MEMORY_DISK_DYNAMIC
    327 	const int chosen = OF_finddevice("/chosen");
    328 	if (chosen < 0)
    329 		return;
    330 
    331 	int len;
    332 	const void *start_data = fdtbus_get_prop(chosen,
    333 	    "linux,initrd-start", &len);
    334 	const void *end_data = fdtbus_get_prop(chosen,
    335 	    "linux,initrd-end", NULL);
    336 	if (start_data == NULL || end_data == NULL)
    337 		return;
    338 
    339 	switch (len) {
    340 	case 4:
    341 		*pstart = be32dec(start_data);
    342 		*pend = be32dec(end_data);
    343 		break;
    344 	case 8:
    345 		*pstart = be64dec(start_data);
    346 		*pend = be64dec(end_data);
    347 		break;
    348 	default:
    349 		printf("Unsupported len %d for /chosen/initrd-start\n", len);
    350 		return;
    351 	}
    352 #endif
    353 }
    354 
    355 static void
    356 fdt_setup_initrd(void)
    357 {
    358 #ifdef MEMORY_DISK_DYNAMIC
    359 	const uint64_t initrd_size = initrd_end - initrd_start;
    360 	paddr_t startpa = trunc_page(initrd_start);
    361 	paddr_t endpa = round_page(initrd_end);
    362 	paddr_t pa;
    363 	vaddr_t va;
    364 	void *md_start;
    365 
    366 	if (initrd_size == 0)
    367 		return;
    368 
    369 	va = uvm_km_alloc(kernel_map, initrd_size, 0,
    370 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    371 	if (va == 0) {
    372 		printf("Failed to allocate VA for initrd\n");
    373 		return;
    374 	}
    375 
    376 	md_start = (void *)va;
    377 
    378 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    379 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
    380 	pmap_update(pmap_kernel());
    381 
    382 	md_root_setconf(md_start, initrd_size);
    383 #endif
    384 }
    385 
    386 #ifdef EFI_RUNTIME
    387 static void
    388 fdt_map_efi_runtime(const char *prop, bool exec)
    389 {
    390 	int len;
    391 
    392 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    393 	if (chosen_off < 0)
    394 		return;
    395 
    396 	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
    397 	if (map == NULL)
    398 		return;
    399 
    400 	while (len >= 24) {
    401 		const paddr_t pa = be64toh(map[0]);
    402 		const vaddr_t va = be64toh(map[1]);
    403 		const uint64_t sz = be64toh(map[2]);
    404 		arm_efirt_md_map_range(va, pa, sz, exec);
    405 		map += 3;
    406 		len -= 24;
    407 	}
    408 }
    409 #endif
    410 
    411 u_int initarm(void *arg);
    412 
    413 u_int
    414 initarm(void *arg)
    415 {
    416 	const struct arm_platform *plat;
    417 	uint64_t memory_start, memory_end;
    418 
    419 	/* set temporally to work printf()/panic() even before consinit() */
    420 	cn_tab = &earlycons;
    421 
    422 	/* Load FDT */
    423 	int error = fdt_check_header(fdt_addr_r);
    424 	if (error == 0) {
    425 		/* If the DTB is too big, try to pack it in place first. */
    426 		if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
    427 			(void)fdt_pack(__UNCONST(fdt_addr_r));
    428 		error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
    429 		if (error != 0)
    430 			panic("fdt_move failed: %s", fdt_strerror(error));
    431 		fdtbus_set_data(fdt_data);
    432 	} else {
    433 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    434 	}
    435 
    436 	/* Lookup platform specific backend */
    437 	plat = arm_fdt_platform();
    438 	if (plat == NULL)
    439 		panic("Kernel does not support this device");
    440 
    441 	/* Early console may be available, announce ourselves. */
    442 	VPRINTF("FDT<%p>\n", fdt_addr_r);
    443 
    444 	const int chosen = OF_finddevice("/chosen");
    445 	if (chosen >= 0)
    446 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    447 	boot_args = bootargs;
    448 
    449 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    450 	VPRINTF("cpufunc\n");
    451 	if (set_cpufuncs())
    452 		panic("cpu not recognized!");
    453 
    454 	/*
    455 	 * Memory is still identity/flat mapped this point so using ttbr for
    456 	 * l1pt VA is fine
    457 	 */
    458 
    459 	VPRINTF("devmap\n");
    460 	extern char ARM_BOOTSTRAP_LxPT[];
    461 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
    462 
    463 	VPRINTF("bootstrap\n");
    464 	plat->ap_bootstrap();
    465 
    466 	/*
    467 	 * If stdout-path is specified on the command line, override the
    468 	 * value in /chosen/stdout-path before initializing console.
    469 	 */
    470 	VPRINTF("stdout\n");
    471 	fdt_update_stdout_path();
    472 
    473 	/*
    474 	 * Done making changes to the FDT.
    475 	 */
    476 	fdt_pack(fdt_data);
    477 
    478 	VPRINTF("consinit ");
    479 	consinit();
    480 	VPRINTF("ok\n");
    481 
    482 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
    483 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    484 
    485 	cpu_reset_address = fdt_reset;
    486 	cpu_powerdown_address = fdt_powerdown;
    487 	evbarm_device_register = fdt_device_register;
    488 	evbarm_device_register_post_config = fdt_device_register_post_config;
    489 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
    490 
    491 	/* Talk to the user */
    492 	printf("NetBSD/evbarm (fdt) booting ...\n");
    493 
    494 #ifdef BOOT_ARGS
    495 	char mi_bootargs[] = BOOT_ARGS;
    496 	parse_mi_bootargs(mi_bootargs);
    497 #endif
    498 
    499 	fdt_get_memory(&memory_start, &memory_end);
    500 
    501 #if !defined(_LP64)
    502 	/* Cannot map memory above 4GB */
    503 	if (memory_end >= 0x100000000ULL)
    504 		memory_end = 0x100000000ULL - PAGE_SIZE;
    505 
    506 #endif
    507 	uint64_t memory_size = memory_end - memory_start;
    508 
    509 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    510 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    511 
    512 	/* Parse ramdisk info */
    513 	fdt_probe_initrd(&initrd_start, &initrd_end);
    514 
    515 	/*
    516 	 * Populate bootconfig structure for the benefit of
    517 	 * dodumpsys
    518 	 */
    519 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
    520 	fdt_build_bootconfig(memory_start, memory_end);
    521 
    522 #ifdef EFI_RUNTIME
    523 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", true);
    524 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", false);
    525 #endif
    526 
    527 	/* Perform PT build and VM init */
    528 	cpu_kernel_vm_init(memory_start, memory_size);
    529 
    530 	VPRINTF("bootargs: %s\n", bootargs);
    531 
    532 	parse_mi_bootargs(boot_args);
    533 
    534 	#define MAX_PHYSMEM 64
    535 	static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
    536 	int nfdt_physmem = 0;
    537 	struct extent_region *er;
    538 
    539 	VPRINTF("Memory regions :\n");
    540 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    541 		VPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    542 		struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    543 
    544 		KASSERT(nfdt_physmem <= MAX_PHYSMEM);
    545 
    546 		bp->bp_start = atop(round_page(er->er_start));
    547 		bp->bp_pages = atop(trunc_page(er->er_end + 1)) - bp->bp_start;
    548 		bp->bp_freelist = VM_FREELIST_DEFAULT;
    549 
    550 #ifdef _LP64
    551 		if (er->er_end > 0x100000000)
    552 			bp->bp_freelist = VM_FREELIST_HIGHMEM;
    553 #endif
    554 
    555 #ifdef PMAP_NEED_ALLOC_POOLPAGE
    556 		if (atop(memory_size) > bp->bp_pages) {
    557 			arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    558 			bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    559 		}
    560 #endif
    561 	}
    562 
    563 	u_int sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    564 	     nfdt_physmem);
    565 
    566 	VPRINTF("mpstart\n");
    567 	if (plat->ap_mpstart)
    568 		plat->ap_mpstart();
    569 
    570 	/*
    571 	 * Now we have APs started the pages used for stacks and L1PT can
    572 	 * be given to uvm
    573 	 */
    574 	extern char __start__init_memory[], __stop__init_memory[];
    575 	if (__start__init_memory != __stop__init_memory) {
    576 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    577 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    578 		const paddr_t spg = atop(spa);
    579 		const paddr_t epg = atop(epa);
    580 
    581 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
    582 
    583 		VPRINTF("           start %08lx  end %08lx", ptoa(spa), ptoa(epa));
    584 	}
    585 
    586 	return sp;
    587 }
    588 
    589 static void
    590 fdt_update_stdout_path(void)
    591 {
    592 	char *stdout_path, *ep;
    593 	int stdout_path_len;
    594 	char buf[256];
    595 
    596 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    597 	if (chosen_off == -1)
    598 		return;
    599 
    600 	if (get_bootconf_option(boot_args, "stdout-path",
    601 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    602 		return;
    603 
    604 	ep = strchr(stdout_path, ' ');
    605 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    606 	if (stdout_path_len >= sizeof(buf))
    607 		return;
    608 
    609 	strncpy(buf, stdout_path, stdout_path_len);
    610 	buf[stdout_path_len] = '\0';
    611 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    612 	    buf, stdout_path_len + 1);
    613 }
    614 
    615 void
    616 consinit(void)
    617 {
    618 	static bool initialized = false;
    619 	const struct arm_platform *plat = arm_fdt_platform();
    620 	const struct fdt_console *cons = fdtbus_get_console();
    621 	struct fdt_attach_args faa;
    622 	u_int uart_freq = 0;
    623 
    624 	if (initialized || cons == NULL)
    625 		return;
    626 
    627 	plat->ap_init_attach_args(&faa);
    628 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    629 
    630 	if (plat->ap_uart_freq != NULL)
    631 		uart_freq = plat->ap_uart_freq();
    632 
    633 	cons->consinit(&faa, uart_freq);
    634 
    635 	initialized = true;
    636 }
    637 
    638 void
    639 delay(u_int us)
    640 {
    641 	const struct arm_platform *plat = arm_fdt_platform();
    642 
    643 	plat->ap_delay(us);
    644 }
    645 
    646 static void
    647 fdt_detect_root_device(device_t dev)
    648 {
    649 	struct mbr_sector mbr;
    650 	uint8_t buf[DEV_BSIZE];
    651 	uint8_t hash[16];
    652 	const uint8_t *rhash;
    653 	char rootarg[32];
    654 	struct vnode *vp;
    655 	MD5_CTX md5ctx;
    656 	int error, len;
    657 	size_t resid;
    658 	u_int part;
    659 
    660 	const int chosen = OF_finddevice("/chosen");
    661 	if (chosen < 0)
    662 		return;
    663 
    664 	if (of_hasprop(chosen, "netbsd,mbr") &&
    665 	    of_hasprop(chosen, "netbsd,partition")) {
    666 
    667 		/*
    668 		 * The bootloader has passed in a partition index and MD5 hash
    669 		 * of the MBR sector. Read the MBR of this device, calculate the
    670 		 * hash, and compare it with the value passed in.
    671 		 */
    672 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
    673 		if (rhash == NULL || len != 16)
    674 			return;
    675 		of_getprop_uint32(chosen, "netbsd,partition", &part);
    676 		if (part >= MAXPARTITIONS)
    677 			return;
    678 
    679 		vp = opendisk(dev);
    680 		if (!vp)
    681 			return;
    682 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
    683 		    0, NOCRED, &resid, NULL);
    684 		VOP_CLOSE(vp, FREAD, NOCRED);
    685 		vput(vp);
    686 
    687 		if (error != 0)
    688 			return;
    689 
    690 		memcpy(&mbr, buf, sizeof(mbr));
    691 		MD5Init(&md5ctx);
    692 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
    693 		MD5Final(hash, &md5ctx);
    694 
    695 		if (memcmp(rhash, hash, 16) != 0)
    696 			return;
    697 
    698 		snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
    699 		strcat(boot_args, rootarg);
    700 	}
    701 }
    702 
    703 static void
    704 fdt_device_register(device_t self, void *aux)
    705 {
    706 	const struct arm_platform *plat = arm_fdt_platform();
    707 
    708 	if (device_is_a(self, "armfdt"))
    709 		fdt_setup_initrd();
    710 
    711 	if (plat && plat->ap_device_register)
    712 		plat->ap_device_register(self, aux);
    713 }
    714 
    715 static void
    716 fdt_device_register_post_config(device_t self, void *aux)
    717 {
    718 #if NUKBD > 0 && NWSDISPLAY > 0
    719 	if (device_is_a(self, "wsdisplay")) {
    720 		struct wsdisplay_softc *sc = device_private(self);
    721 		if (wsdisplay_isconsole(sc))
    722 			ukbd_cnattach();
    723 	}
    724 #endif
    725 }
    726 
    727 static void
    728 fdt_cpu_rootconf(void)
    729 {
    730 	device_t dev;
    731 	deviter_t di;
    732 	char *ptr;
    733 
    734 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
    735 		if (device_class(dev) != DV_DISK)
    736 			continue;
    737 
    738 		if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
    739 			break;
    740 
    741 		if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
    742 			fdt_detect_root_device(dev);
    743 	}
    744 	deviter_release(&di);
    745 }
    746 
    747 static void
    748 fdt_reset(void)
    749 {
    750 	const struct arm_platform *plat = arm_fdt_platform();
    751 
    752 	fdtbus_power_reset();
    753 
    754 	if (plat && plat->ap_reset)
    755 		plat->ap_reset();
    756 }
    757 
    758 static void
    759 fdt_powerdown(void)
    760 {
    761 	fdtbus_power_poweroff();
    762 }
    763