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