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