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