Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.102
      1  1.102     skrll /* $NetBSD: fdt_machdep.c,v 1.102 2023/04/07 08:55:31 skrll Exp $ */
      2    1.1  jmcneill 
      3    1.1  jmcneill /*-
      4    1.1  jmcneill  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5    1.1  jmcneill  * All rights reserved.
      6    1.1  jmcneill  *
      7    1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8    1.1  jmcneill  * modification, are permitted provided that the following conditions
      9    1.1  jmcneill  * are met:
     10    1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11    1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12    1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14    1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15    1.1  jmcneill  *
     16    1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17    1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18    1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19    1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20    1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21    1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22    1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23    1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24    1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25    1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26    1.1  jmcneill  * SUCH DAMAGE.
     27    1.1  jmcneill  */
     28    1.1  jmcneill 
     29    1.1  jmcneill #include <sys/cdefs.h>
     30  1.102     skrll __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.102 2023/04/07 08:55:31 skrll Exp $");
     31    1.1  jmcneill 
     32   1.84     skrll #include "opt_arm_debug.h"
     33   1.21       ryo #include "opt_bootconfig.h"
     34   1.84     skrll #include "opt_cpuoptions.h"
     35    1.1  jmcneill #include "opt_ddb.h"
     36   1.84     skrll #include "opt_efi.h"
     37   1.84     skrll #include "opt_machdep.h"
     38    1.1  jmcneill #include "opt_md.h"
     39    1.1  jmcneill #include "opt_multiprocessor.h"
     40    1.1  jmcneill 
     41   1.75  jmcneill #include "genfb.h"
     42   1.14  jmcneill #include "ukbd.h"
     43   1.39    bouyer #include "wsdisplay.h"
     44   1.14  jmcneill 
     45    1.1  jmcneill #include <sys/param.h>
     46   1.93     skrll #include <sys/types.h>
     47   1.93     skrll 
     48   1.93     skrll #include <sys/atomic.h>
     49   1.93     skrll #include <sys/bootblock.h>
     50    1.1  jmcneill #include <sys/bus.h>
     51   1.93     skrll #include <sys/conf.h>
     52    1.1  jmcneill #include <sys/cpu.h>
     53    1.1  jmcneill #include <sys/device.h>
     54   1.93     skrll #include <sys/disk.h>
     55   1.93     skrll #include <sys/disklabel.h>
     56   1.76       rin #include <sys/endian.h>
     57    1.1  jmcneill #include <sys/exec.h>
     58   1.93     skrll #include <sys/fcntl.h>
     59   1.93     skrll #include <sys/kauth.h>
     60    1.1  jmcneill #include <sys/kernel.h>
     61    1.1  jmcneill #include <sys/kmem.h>
     62    1.1  jmcneill #include <sys/ksyms.h>
     63   1.93     skrll #include <sys/md5.h>
     64    1.1  jmcneill #include <sys/msgbuf.h>
     65    1.1  jmcneill #include <sys/proc.h>
     66   1.93     skrll #include <sys/pserialize.h>
     67    1.1  jmcneill #include <sys/reboot.h>
     68   1.93     skrll #include <sys/rnd.h>
     69   1.93     skrll #include <sys/rndsource.h>
     70   1.93     skrll #include <sys/systm.h>
     71    1.1  jmcneill #include <sys/termios.h>
     72   1.34  jmcneill #include <sys/vnode.h>
     73   1.53  jmcneill #include <sys/uuid.h>
     74   1.55  jmcneill 
     75   1.55  jmcneill #include <net/if.h>
     76   1.55  jmcneill #include <net/if_dl.h>
     77    1.1  jmcneill 
     78   1.23       ryo #include <dev/cons.h>
     79    1.1  jmcneill #include <uvm/uvm_extern.h>
     80    1.1  jmcneill 
     81    1.1  jmcneill #include <machine/db_machdep.h>
     82    1.1  jmcneill #include <ddb/db_sym.h>
     83    1.1  jmcneill #include <ddb/db_extern.h>
     84    1.1  jmcneill 
     85    1.1  jmcneill #include <machine/bootconfig.h>
     86    1.1  jmcneill #include <arm/armreg.h>
     87    1.1  jmcneill 
     88   1.21       ryo #include <arm/cpufunc.h>
     89    1.1  jmcneill 
     90    1.1  jmcneill #include <evbarm/include/autoconf.h>
     91   1.30     skrll #include <evbarm/fdt/machdep.h>
     92    1.1  jmcneill #include <evbarm/fdt/platform.h>
     93    1.1  jmcneill 
     94    1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     95  1.102     skrll 
     96  1.102     skrll #include <dev/fdt/fdtvar.h>
     97   1.68     skrll #include <dev/fdt/fdt_private.h>
     98   1.83     skrll #include <dev/fdt/fdt_memory.h>
     99    1.1  jmcneill 
    100   1.46  jmcneill #ifdef EFI_RUNTIME
    101   1.46  jmcneill #include <arm/arm/efi_runtime.h>
    102   1.46  jmcneill #endif
    103   1.46  jmcneill 
    104   1.75  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    105   1.75  jmcneill #include <arm/fdt/arm_simplefb.h>
    106   1.75  jmcneill #endif
    107   1.75  jmcneill 
    108   1.14  jmcneill #if NUKBD > 0
    109   1.14  jmcneill #include <dev/usb/ukbdvar.h>
    110   1.14  jmcneill #endif
    111   1.39    bouyer #if NWSDISPLAY > 0
    112   1.39    bouyer #include <dev/wscons/wsdisplayvar.h>
    113   1.39    bouyer #endif
    114   1.14  jmcneill 
    115    1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    116    1.8  jmcneill #include <dev/md.h>
    117    1.8  jmcneill #endif
    118    1.8  jmcneill 
    119    1.1  jmcneill #ifndef FDT_MAX_BOOT_STRING
    120    1.1  jmcneill #define FDT_MAX_BOOT_STRING 1024
    121    1.1  jmcneill #endif
    122    1.1  jmcneill 
    123    1.1  jmcneill BootConfig bootconfig;
    124    1.1  jmcneill char bootargs[FDT_MAX_BOOT_STRING] = "";
    125    1.1  jmcneill char *boot_args = NULL;
    126   1.26  christos 
    127   1.26  christos /* filled in before cleaning bss. keep in .data */
    128   1.27  christos u_long uboot_args[4] __attribute__((__section__(".data")));
    129   1.28     skrll const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
    130    1.1  jmcneill 
    131    1.8  jmcneill static uint64_t initrd_start, initrd_end;
    132   1.69  riastrad static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
    133   1.69  riastrad static uint64_t efirng_start, efirng_end;   /* firmware's EFI RNG output */
    134    1.8  jmcneill 
    135    1.1  jmcneill #include <libfdt.h>
    136    1.1  jmcneill #include <dev/fdt/fdtvar.h>
    137   1.40  jmcneill #define FDT_BUF_SIZE	(512*1024)
    138    1.1  jmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
    139    1.1  jmcneill 
    140    1.1  jmcneill extern char KERNEL_BASE_phys[];
    141    1.1  jmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    142    1.1  jmcneill 
    143    1.5  jmcneill static void fdt_update_stdout_path(void);
    144    1.1  jmcneill static void fdt_device_register(device_t, void *);
    145   1.39    bouyer static void fdt_device_register_post_config(device_t, void *);
    146   1.34  jmcneill static void fdt_cpu_rootconf(void);
    147    1.1  jmcneill static void fdt_reset(void);
    148    1.1  jmcneill static void fdt_powerdown(void);
    149    1.1  jmcneill 
    150   1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    151   1.76       rin static void fdt_update_fb_format(void);
    152   1.76       rin #endif
    153   1.76       rin 
    154    1.1  jmcneill static void
    155   1.23       ryo earlyconsputc(dev_t dev, int c)
    156    1.1  jmcneill {
    157   1.48     skrll 	uartputc(c);
    158    1.1  jmcneill }
    159    1.1  jmcneill 
    160   1.23       ryo static int
    161   1.23       ryo earlyconsgetc(dev_t dev)
    162    1.1  jmcneill {
    163   1.87  jmcneill 	return -1;
    164   1.23       ryo }
    165    1.1  jmcneill 
    166   1.54     skrll static struct consdev earlycons = {
    167   1.54     skrll 	.cn_putc = earlyconsputc,
    168   1.54     skrll 	.cn_getc = earlyconsgetc,
    169   1.54     skrll 	.cn_pollc = nullcnpollc,
    170   1.54     skrll };
    171   1.54     skrll 
    172   1.23       ryo #ifdef VERBOSE_INIT_ARM
    173   1.29     skrll #define VPRINTF(...)	printf(__VA_ARGS__)
    174    1.1  jmcneill #else
    175   1.43     skrll #define VPRINTF(...)	__nothing
    176    1.1  jmcneill #endif
    177    1.1  jmcneill 
    178   1.49  jmcneill static void
    179   1.49  jmcneill fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
    180   1.49  jmcneill {
    181   1.49  jmcneill 	BootConfig *bc = arg;
    182   1.49  jmcneill 
    183   1.52  jmcneill 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
    184   1.49  jmcneill 	bc->dram[bc->dramblocks].address = m->start;
    185   1.49  jmcneill 	bc->dram[bc->dramblocks].pages =
    186   1.49  jmcneill 	    (m->end - m->start) / PAGE_SIZE;
    187   1.49  jmcneill 	bc->dramblocks++;
    188   1.49  jmcneill }
    189   1.49  jmcneill 
    190   1.49  jmcneill static int nfdt_physmem = 0;
    191   1.99  jmcneill static struct boot_physmem fdt_physmem[FDT_MEMORY_RANGES];
    192   1.49  jmcneill 
    193   1.49  jmcneill static void
    194   1.49  jmcneill fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
    195   1.49  jmcneill {
    196   1.62     skrll 	const paddr_t saddr = round_page(m->start);
    197   1.62     skrll 	const paddr_t eaddr = trunc_page(m->end);
    198   1.62     skrll 
    199   1.62     skrll 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
    200   1.62     skrll 	if (saddr >= eaddr) {
    201   1.62     skrll 		VPRINTF(" skipped\n");
    202   1.62     skrll 		return;
    203   1.62     skrll 	}
    204   1.62     skrll 	VPRINTF("\n");
    205   1.62     skrll 
    206   1.49  jmcneill 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    207   1.49  jmcneill 
    208   1.99  jmcneill 	KASSERT(nfdt_physmem <= FDT_MEMORY_RANGES);
    209   1.49  jmcneill 
    210   1.62     skrll 	bp->bp_start = atop(saddr);
    211   1.62     skrll 	bp->bp_pages = atop(eaddr) - bp->bp_start;
    212   1.49  jmcneill 	bp->bp_freelist = VM_FREELIST_DEFAULT;
    213   1.49  jmcneill 
    214   1.49  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
    215   1.49  jmcneill 	const uint64_t memory_size = *(uint64_t *)arg;
    216   1.49  jmcneill 	if (atop(memory_size) > bp->bp_pages) {
    217   1.49  jmcneill 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    218   1.49  jmcneill 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    219   1.49  jmcneill 	}
    220   1.49  jmcneill #endif
    221   1.49  jmcneill }
    222   1.49  jmcneill 
    223   1.83     skrll 
    224   1.83     skrll static void
    225   1.83     skrll fdt_print_memory(const struct fdt_memory *m, void *arg)
    226   1.83     skrll {
    227   1.83     skrll 
    228   1.83     skrll 	VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    229   1.83     skrll 	    m->start, m->end - m->start);
    230   1.83     skrll }
    231   1.83     skrll 
    232   1.83     skrll 
    233    1.8  jmcneill /*
    234    1.8  jmcneill  * Define usable memory regions.
    235    1.8  jmcneill  */
    236    1.8  jmcneill static void
    237   1.21       ryo fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
    238    1.8  jmcneill {
    239    1.8  jmcneill 	BootConfig *bc = &bootconfig;
    240   1.83     skrll 
    241    1.8  jmcneill 	uint64_t addr, size;
    242   1.49  jmcneill 	int index;
    243    1.8  jmcneill 
    244   1.73  jmcneill 	const uint64_t initrd_size =
    245   1.73  jmcneill 	    round_page(initrd_end) - trunc_page(initrd_start);
    246    1.8  jmcneill 	if (initrd_size > 0)
    247   1.73  jmcneill 		fdt_memory_remove_range(trunc_page(initrd_start), initrd_size);
    248    1.8  jmcneill 
    249   1.73  jmcneill 	const uint64_t rndseed_size =
    250   1.73  jmcneill 	    round_page(rndseed_end) - trunc_page(rndseed_start);
    251   1.65  riastrad 	if (rndseed_size > 0)
    252   1.73  jmcneill 		fdt_memory_remove_range(trunc_page(rndseed_start),
    253   1.73  jmcneill 		    rndseed_size);
    254   1.65  riastrad 
    255   1.73  jmcneill 	const uint64_t efirng_size =
    256   1.73  jmcneill 	    round_page(efirng_end) - trunc_page(efirng_start);
    257   1.69  riastrad 	if (efirng_size > 0)
    258   1.73  jmcneill 		fdt_memory_remove_range(trunc_page(efirng_start), efirng_size);
    259   1.69  riastrad 
    260   1.47  jmcneill 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
    261   1.47  jmcneill 	if (framebuffer >= 0) {
    262   1.47  jmcneill 		for (index = 0;
    263   1.47  jmcneill 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
    264   1.47  jmcneill 		     index++) {
    265   1.83     skrll 			fdt_memory_remove_range(addr, size);
    266   1.47  jmcneill 		}
    267   1.47  jmcneill 	}
    268   1.47  jmcneill 
    269   1.29     skrll 	VPRINTF("Usable memory:\n");
    270    1.8  jmcneill 	bc->dramblocks = 0;
    271   1.49  jmcneill 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
    272    1.8  jmcneill }
    273    1.8  jmcneill 
    274    1.8  jmcneill static void
    275   1.70  riastrad fdt_probe_range(const char *startname, const char *endname,
    276   1.70  riastrad     uint64_t *pstart, uint64_t *pend)
    277    1.8  jmcneill {
    278   1.70  riastrad 	int chosen, len;
    279   1.70  riastrad 	const void *start_data, *end_data;
    280   1.70  riastrad 
    281    1.8  jmcneill 	*pstart = *pend = 0;
    282    1.8  jmcneill 
    283   1.70  riastrad 	chosen = OF_finddevice("/chosen");
    284    1.8  jmcneill 	if (chosen < 0)
    285    1.8  jmcneill 		return;
    286    1.8  jmcneill 
    287   1.70  riastrad 	start_data = fdtbus_get_prop(chosen, startname, &len);
    288   1.70  riastrad 	end_data = fdtbus_get_prop(chosen, endname, NULL);
    289    1.8  jmcneill 	if (start_data == NULL || end_data == NULL)
    290    1.8  jmcneill 		return;
    291    1.8  jmcneill 
    292    1.8  jmcneill 	switch (len) {
    293    1.8  jmcneill 	case 4:
    294    1.8  jmcneill 		*pstart = be32dec(start_data);
    295    1.8  jmcneill 		*pend = be32dec(end_data);
    296    1.8  jmcneill 		break;
    297    1.8  jmcneill 	case 8:
    298    1.8  jmcneill 		*pstart = be64dec(start_data);
    299    1.8  jmcneill 		*pend = be64dec(end_data);
    300    1.8  jmcneill 		break;
    301    1.8  jmcneill 	default:
    302   1.70  riastrad 		printf("Unsupported len %d for /chosen `%s'\n",
    303   1.70  riastrad 		    len, startname);
    304    1.8  jmcneill 		return;
    305    1.8  jmcneill 	}
    306    1.8  jmcneill }
    307    1.8  jmcneill 
    308   1.70  riastrad static void *
    309   1.70  riastrad fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
    310   1.70  riastrad     const char *purpose)
    311    1.8  jmcneill {
    312   1.70  riastrad 	const paddr_t startpa = trunc_page(start);
    313   1.70  riastrad 	const paddr_t endpa = round_page(end);
    314    1.8  jmcneill 	paddr_t pa;
    315    1.8  jmcneill 	vaddr_t va;
    316   1.70  riastrad 	void *ptr;
    317    1.8  jmcneill 
    318   1.70  riastrad 	*psize = end - start;
    319   1.70  riastrad 	if (*psize == 0)
    320   1.70  riastrad 		return NULL;
    321    1.8  jmcneill 
    322   1.72     skrll 	const vaddr_t voff = start & PAGE_MASK;
    323   1.72     skrll 
    324   1.94     skrll 	va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    325    1.8  jmcneill 	if (va == 0) {
    326   1.70  riastrad 		printf("Failed to allocate VA for %s\n", purpose);
    327   1.70  riastrad 		return NULL;
    328    1.8  jmcneill 	}
    329   1.72     skrll 	ptr = (void *)(va + voff);
    330    1.8  jmcneill 
    331    1.8  jmcneill 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    332   1.94     skrll 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
    333    1.8  jmcneill 	pmap_update(pmap_kernel());
    334    1.8  jmcneill 
    335   1.70  riastrad 	return ptr;
    336   1.70  riastrad }
    337   1.70  riastrad 
    338   1.70  riastrad static void
    339   1.71  riastrad fdt_unmap_range(void *ptr, uint64_t size)
    340   1.71  riastrad {
    341   1.71  riastrad 	const char *start = ptr, *end = start + size;
    342   1.71  riastrad 	const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start);
    343   1.71  riastrad 	const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end);
    344   1.95     skrll 	const vsize_t sz = endva - startva;
    345   1.71  riastrad 
    346   1.95     skrll 	pmap_kremove(startva, sz);
    347   1.71  riastrad 	pmap_update(pmap_kernel());
    348   1.95     skrll 
    349   1.95     skrll 	uvm_km_free(kernel_map, startva, sz, UVM_KMF_VAONLY);
    350   1.71  riastrad }
    351   1.71  riastrad 
    352   1.71  riastrad static void
    353   1.70  riastrad fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    354   1.70  riastrad {
    355   1.70  riastrad 	*pstart = *pend = 0;
    356   1.70  riastrad 
    357   1.70  riastrad #ifdef MEMORY_DISK_DYNAMIC
    358   1.70  riastrad 	fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
    359    1.8  jmcneill #endif
    360    1.8  jmcneill }
    361    1.8  jmcneill 
    362   1.65  riastrad static void
    363   1.70  riastrad fdt_setup_initrd(void)
    364   1.65  riastrad {
    365   1.70  riastrad #ifdef MEMORY_DISK_DYNAMIC
    366   1.70  riastrad 	void *md_start;
    367   1.70  riastrad 	uint64_t initrd_size;
    368   1.65  riastrad 
    369   1.70  riastrad 	md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
    370   1.70  riastrad 	    "initrd");
    371   1.70  riastrad 	if (md_start == NULL)
    372   1.65  riastrad 		return;
    373   1.70  riastrad 	md_root_setconf(md_start, initrd_size);
    374   1.70  riastrad #endif
    375   1.70  riastrad }
    376   1.65  riastrad 
    377   1.70  riastrad static void
    378   1.70  riastrad fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
    379   1.70  riastrad {
    380   1.65  riastrad 
    381   1.70  riastrad 	fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
    382   1.70  riastrad 	    pstart, pend);
    383   1.65  riastrad }
    384   1.65  riastrad 
    385   1.65  riastrad static void
    386   1.65  riastrad fdt_setup_rndseed(void)
    387   1.65  riastrad {
    388   1.70  riastrad 	uint64_t rndseed_size;
    389   1.65  riastrad 	void *rndseed;
    390   1.65  riastrad 
    391   1.70  riastrad 	rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
    392   1.70  riastrad 	    "rndseed");
    393   1.70  riastrad 	if (rndseed == NULL)
    394   1.65  riastrad 		return;
    395   1.65  riastrad 	rnd_seed(rndseed, rndseed_size);
    396   1.71  riastrad 	fdt_unmap_range(rndseed, rndseed_size);
    397   1.65  riastrad }
    398   1.65  riastrad 
    399   1.69  riastrad static void
    400   1.69  riastrad fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
    401   1.69  riastrad {
    402   1.69  riastrad 
    403   1.70  riastrad 	fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
    404   1.70  riastrad 	    pstart, pend);
    405   1.69  riastrad }
    406   1.69  riastrad 
    407   1.69  riastrad static struct krndsource efirng_source;
    408   1.69  riastrad 
    409   1.69  riastrad static void
    410   1.69  riastrad fdt_setup_efirng(void)
    411   1.69  riastrad {
    412   1.70  riastrad 	uint64_t efirng_size;
    413   1.69  riastrad 	void *efirng;
    414   1.69  riastrad 
    415   1.70  riastrad 	efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
    416   1.70  riastrad 	    "efirng");
    417   1.70  riastrad 	if (efirng == NULL)
    418   1.69  riastrad 		return;
    419   1.69  riastrad 
    420   1.69  riastrad 	rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
    421   1.69  riastrad 	    RND_FLAG_DEFAULT);
    422   1.82  riastrad 
    423   1.82  riastrad 	/*
    424   1.82  riastrad 	 * We don't really have specific information about the physical
    425   1.82  riastrad 	 * process underlying the data provided by the firmware via the
    426   1.82  riastrad 	 * EFI RNG API, so the entropy estimate here is heuristic.
    427   1.82  riastrad 	 * What efiboot provides us is up to 4096 bytes of data from
    428   1.82  riastrad 	 * the EFI RNG API, although in principle it may return short.
    429   1.82  riastrad 	 *
    430   1.82  riastrad 	 * The UEFI Specification (2.8 Errata A, February 2020[1]) says
    431   1.82  riastrad 	 *
    432   1.82  riastrad 	 *	When a Deterministic Random Bit Generator (DRBG) is
    433   1.82  riastrad 	 *	used on the output of a (raw) entropy source, its
    434   1.82  riastrad 	 *	security level must be at least 256 bits.
    435   1.82  riastrad 	 *
    436   1.82  riastrad 	 * It's not entirely clear whether `it' refers to the DRBG or
    437   1.82  riastrad 	 * the entropy source; if it refers to the DRBG, it's not
    438   1.82  riastrad 	 * entirely clear how ANSI X9.31 3DES, one of the options for
    439   1.82  riastrad 	 * DRBG in the UEFI spec, can provide a `256-bit security
    440   1.82  riastrad 	 * level' because it has only 232 bits of inputs (three 56-bit
    441   1.82  riastrad 	 * keys and one 64-bit block).  That said, even if it provides
    442   1.82  riastrad 	 * only 232 bits of entropy, that's enough to prevent all
    443   1.82  riastrad 	 * attacks and we probably get a few more bits from sampling
    444   1.82  riastrad 	 * the clock anyway.
    445   1.82  riastrad 	 *
    446   1.82  riastrad 	 * In the event we get raw samples, e.g. the bits sampled by a
    447   1.82  riastrad 	 * ring oscillator, we hope that the samples have at least half
    448   1.82  riastrad 	 * a bit of entropy per bit of data -- and efiboot tries to
    449   1.82  riastrad 	 * draw 4096 bytes to provide plenty of slop.  Hence we divide
    450   1.82  riastrad 	 * the total number of bits by two and clamp at 256.  There are
    451   1.82  riastrad 	 * ways this could go wrong, but on most machines it should
    452   1.82  riastrad 	 * behave reasonably.
    453   1.82  riastrad 	 *
    454   1.82  riastrad 	 * [1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_A_Feb14.pdf
    455   1.82  riastrad 	 */
    456   1.82  riastrad 	rnd_add_data(&efirng_source, efirng, efirng_size,
    457   1.82  riastrad 	    MIN(256, efirng_size*NBBY/2));
    458   1.82  riastrad 
    459   1.69  riastrad 	explicit_memset(efirng, 0, efirng_size);
    460   1.71  riastrad 	fdt_unmap_range(efirng, efirng_size);
    461   1.69  riastrad }
    462   1.69  riastrad 
    463   1.46  jmcneill #ifdef EFI_RUNTIME
    464   1.46  jmcneill static void
    465   1.51  jmcneill fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
    466   1.46  jmcneill {
    467   1.46  jmcneill 	int len;
    468   1.46  jmcneill 
    469   1.46  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    470   1.46  jmcneill 	if (chosen_off < 0)
    471   1.46  jmcneill 		return;
    472   1.46  jmcneill 
    473   1.46  jmcneill 	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
    474   1.46  jmcneill 	if (map == NULL)
    475   1.46  jmcneill 		return;
    476   1.46  jmcneill 
    477   1.46  jmcneill 	while (len >= 24) {
    478   1.46  jmcneill 		const paddr_t pa = be64toh(map[0]);
    479   1.46  jmcneill 		const vaddr_t va = be64toh(map[1]);
    480   1.85     skrll 		const size_t sz = be64toh(map[2]);
    481   1.89     skrll 		VPRINTF("%s: %s %#" PRIxPADDR "-%#" PRIxVADDR " (%#" PRIxVADDR
    482   1.89     skrll 		    "-%#" PRIxVSIZE ")\n", __func__, prop, pa, pa + sz - 1,
    483   1.85     skrll 		    va, va + sz - 1);
    484   1.51  jmcneill 		arm_efirt_md_map_range(va, pa, sz, type);
    485   1.46  jmcneill 		map += 3;
    486   1.46  jmcneill 		len -= 24;
    487   1.46  jmcneill 	}
    488   1.46  jmcneill }
    489   1.46  jmcneill #endif
    490   1.46  jmcneill 
    491   1.64     skrll vaddr_t
    492    1.1  jmcneill initarm(void *arg)
    493    1.1  jmcneill {
    494  1.102     skrll 	const struct fdt_platform *plat;
    495   1.21       ryo 	uint64_t memory_start, memory_end;
    496    1.1  jmcneill 
    497   1.23       ryo 	/* set temporally to work printf()/panic() even before consinit() */
    498   1.23       ryo 	cn_tab = &earlycons;
    499   1.23       ryo 
    500    1.1  jmcneill 	/* Load FDT */
    501    1.1  jmcneill 	int error = fdt_check_header(fdt_addr_r);
    502   1.79     skrll 	if (error != 0)
    503    1.1  jmcneill 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    504   1.79     skrll 
    505   1.79     skrll 	/* If the DTB is too big, try to pack it in place first. */
    506   1.79     skrll 	if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
    507   1.79     skrll 		(void)fdt_pack(__UNCONST(fdt_addr_r));
    508   1.83     skrll 
    509   1.79     skrll 	error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
    510   1.79     skrll 	if (error != 0)
    511   1.79     skrll 		panic("fdt_move failed: %s", fdt_strerror(error));
    512   1.79     skrll 
    513   1.79     skrll 	fdtbus_init(fdt_data);
    514    1.1  jmcneill 
    515    1.1  jmcneill 	/* Lookup platform specific backend */
    516  1.102     skrll 	plat = fdt_platform_find();
    517    1.1  jmcneill 	if (plat == NULL)
    518    1.1  jmcneill 		panic("Kernel does not support this device");
    519    1.1  jmcneill 
    520    1.1  jmcneill 	/* Early console may be available, announce ourselves. */
    521   1.29     skrll 	VPRINTF("FDT<%p>\n", fdt_addr_r);
    522    1.1  jmcneill 
    523    1.6  jmcneill 	const int chosen = OF_finddevice("/chosen");
    524    1.6  jmcneill 	if (chosen >= 0)
    525    1.6  jmcneill 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    526    1.6  jmcneill 	boot_args = bootargs;
    527    1.6  jmcneill 
    528    1.1  jmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    529   1.29     skrll 	VPRINTF("cpufunc\n");
    530    1.1  jmcneill 	if (set_cpufuncs())
    531    1.1  jmcneill 		panic("cpu not recognized!");
    532    1.1  jmcneill 
    533   1.44     skrll 	/*
    534   1.44     skrll 	 * Memory is still identity/flat mapped this point so using ttbr for
    535   1.44     skrll 	 * l1pt VA is fine
    536   1.44     skrll 	 */
    537   1.44     skrll 
    538  1.102     skrll 	VPRINTF("devmap %p\n", plat->fp_devmap());
    539   1.44     skrll 	extern char ARM_BOOTSTRAP_LxPT[];
    540  1.102     skrll 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->fp_devmap());
    541   1.44     skrll 
    542   1.29     skrll 	VPRINTF("bootstrap\n");
    543  1.102     skrll 	plat->fp_bootstrap();
    544   1.16     skrll 
    545    1.5  jmcneill 	/*
    546    1.5  jmcneill 	 * If stdout-path is specified on the command line, override the
    547    1.5  jmcneill 	 * value in /chosen/stdout-path before initializing console.
    548    1.5  jmcneill 	 */
    549   1.44     skrll 	VPRINTF("stdout\n");
    550    1.5  jmcneill 	fdt_update_stdout_path();
    551    1.5  jmcneill 
    552   1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    553   1.76       rin 	/*
    554   1.92     skrll 	 * Most boards are configured to little-endian mode initially, and
    555   1.76       rin 	 * switched to big-endian mode after kernel is loaded. In this case,
    556   1.76       rin 	 * framebuffer seems byte-swapped to CPU. Override FDT to let
    557   1.76       rin 	 * drivers know.
    558   1.76       rin 	 */
    559   1.76       rin 	VPRINTF("fb_format\n");
    560   1.76       rin 	fdt_update_fb_format();
    561   1.76       rin #endif
    562   1.76       rin 
    563   1.38  jmcneill 	/*
    564   1.38  jmcneill 	 * Done making changes to the FDT.
    565   1.38  jmcneill 	 */
    566   1.38  jmcneill 	fdt_pack(fdt_data);
    567   1.38  jmcneill 
    568   1.29     skrll 	VPRINTF("consinit ");
    569    1.1  jmcneill 	consinit();
    570   1.29     skrll 	VPRINTF("ok\n");
    571    1.1  jmcneill 
    572   1.29     skrll 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
    573    1.1  jmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    574    1.1  jmcneill 
    575    1.1  jmcneill 	cpu_reset_address = fdt_reset;
    576    1.1  jmcneill 	cpu_powerdown_address = fdt_powerdown;
    577    1.1  jmcneill 	evbarm_device_register = fdt_device_register;
    578   1.39    bouyer 	evbarm_device_register_post_config = fdt_device_register_post_config;
    579   1.34  jmcneill 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
    580    1.1  jmcneill 
    581    1.1  jmcneill 	/* Talk to the user */
    582   1.45     skrll 	printf("NetBSD/evbarm (fdt) booting ...\n");
    583    1.1  jmcneill 
    584    1.1  jmcneill #ifdef BOOT_ARGS
    585    1.1  jmcneill 	char mi_bootargs[] = BOOT_ARGS;
    586    1.1  jmcneill 	parse_mi_bootargs(mi_bootargs);
    587    1.1  jmcneill #endif
    588    1.1  jmcneill 
    589   1.83     skrll 	fdt_memory_get(&memory_start, &memory_end);
    590   1.83     skrll 
    591   1.83     skrll 	fdt_memory_foreach(fdt_print_memory, NULL);
    592    1.1  jmcneill 
    593    1.1  jmcneill #if !defined(_LP64)
    594   1.81     skrll 	/* Cannot map memory above 4GB (remove last page as well) */
    595   1.81     skrll 	const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE;
    596   1.83     skrll 	if (memory_end > memory_limit) {
    597   1.83     skrll 		fdt_memory_remove_range(memory_limit , memory_end);
    598   1.81     skrll 		memory_end = memory_limit;
    599   1.83     skrll 	}
    600   1.31     skrll #endif
    601   1.21       ryo 	uint64_t memory_size = memory_end - memory_start;
    602    1.1  jmcneill 
    603   1.44     skrll 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    604   1.44     skrll 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    605   1.44     skrll 
    606    1.8  jmcneill 	/* Parse ramdisk info */
    607    1.8  jmcneill 	fdt_probe_initrd(&initrd_start, &initrd_end);
    608    1.8  jmcneill 
    609   1.69  riastrad 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
    610   1.65  riastrad 	fdt_probe_rndseed(&rndseed_start, &rndseed_end);
    611   1.69  riastrad 	fdt_probe_efirng(&efirng_start, &efirng_end);
    612   1.65  riastrad 
    613   1.98     skrll 	fdt_memory_remove_reserved(memory_start, memory_end);
    614   1.96     skrll 
    615   1.16     skrll 	/*
    616   1.80     skrll 	 * Populate bootconfig structure for the benefit of dodumpsys
    617   1.16     skrll 	 */
    618   1.44     skrll 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
    619   1.21       ryo 	fdt_build_bootconfig(memory_start, memory_end);
    620   1.21       ryo 
    621   1.31     skrll 	/* Perform PT build and VM init */
    622   1.31     skrll 	cpu_kernel_vm_init(memory_start, memory_size);
    623    1.1  jmcneill 
    624   1.29     skrll 	VPRINTF("bootargs: %s\n", bootargs);
    625    1.1  jmcneill 
    626    1.1  jmcneill 	parse_mi_bootargs(boot_args);
    627    1.1  jmcneill 
    628   1.49  jmcneill 	VPRINTF("Memory regions:\n");
    629   1.86     skrll 
    630   1.86     skrll 	/* Populate fdt_physmem / nfdt_physmem for initarm_common */
    631   1.49  jmcneill 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
    632    1.1  jmcneill 
    633   1.64     skrll 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    634   1.16     skrll 	     nfdt_physmem);
    635   1.44     skrll 
    636   1.59     skrll 	/*
    637   1.60     skrll 	 * initarm_common flushes cache if required before AP start
    638   1.59     skrll 	 */
    639   1.58     skrll 	error = 0;
    640   1.56       ryo 	if ((boothowto & RB_MD1) == 0) {
    641   1.56       ryo 		VPRINTF("mpstart\n");
    642  1.102     skrll 		if (plat->fp_mpstart)
    643  1.102     skrll 			error = plat->fp_mpstart();
    644   1.56       ryo 	}
    645   1.44     skrll 
    646   1.58     skrll 	if (error)
    647   1.58     skrll 		return sp;
    648   1.74     skrll 
    649   1.44     skrll 	/*
    650   1.44     skrll 	 * Now we have APs started the pages used for stacks and L1PT can
    651   1.44     skrll 	 * be given to uvm
    652   1.44     skrll 	 */
    653   1.57     skrll 	extern char const __start__init_memory[];
    654   1.57     skrll 	extern char const __stop__init_memory[] __weak;
    655   1.57     skrll 
    656   1.44     skrll 	if (__start__init_memory != __stop__init_memory) {
    657   1.44     skrll 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    658   1.44     skrll 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    659   1.44     skrll 		const paddr_t spg = atop(spa);
    660   1.44     skrll 		const paddr_t epg = atop(epa);
    661   1.44     skrll 
    662   1.63     skrll 		VPRINTF("         start %08lx  end %08lx... "
    663   1.63     skrll 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
    664   1.63     skrll 
    665   1.44     skrll 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
    666   1.44     skrll 	}
    667   1.44     skrll 
    668   1.44     skrll 	return sp;
    669    1.1  jmcneill }
    670    1.1  jmcneill 
    671    1.5  jmcneill static void
    672    1.5  jmcneill fdt_update_stdout_path(void)
    673    1.5  jmcneill {
    674    1.5  jmcneill 	char *stdout_path, *ep;
    675    1.5  jmcneill 	int stdout_path_len;
    676    1.5  jmcneill 	char buf[256];
    677    1.5  jmcneill 
    678    1.5  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    679    1.5  jmcneill 	if (chosen_off == -1)
    680    1.5  jmcneill 		return;
    681    1.5  jmcneill 
    682    1.5  jmcneill 	if (get_bootconf_option(boot_args, "stdout-path",
    683    1.5  jmcneill 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    684    1.5  jmcneill 		return;
    685    1.5  jmcneill 
    686    1.5  jmcneill 	ep = strchr(stdout_path, ' ');
    687    1.5  jmcneill 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    688    1.5  jmcneill 	if (stdout_path_len >= sizeof(buf))
    689    1.5  jmcneill 		return;
    690    1.5  jmcneill 
    691    1.5  jmcneill 	strncpy(buf, stdout_path, stdout_path_len);
    692    1.5  jmcneill 	buf[stdout_path_len] = '\0';
    693    1.5  jmcneill 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    694    1.5  jmcneill 	    buf, stdout_path_len + 1);
    695    1.5  jmcneill }
    696    1.5  jmcneill 
    697    1.1  jmcneill void
    698    1.1  jmcneill consinit(void)
    699    1.1  jmcneill {
    700    1.1  jmcneill 	static bool initialized = false;
    701  1.102     skrll 	const struct fdt_platform *plat = fdt_platform_find();
    702    1.1  jmcneill 	const struct fdt_console *cons = fdtbus_get_console();
    703    1.1  jmcneill 	struct fdt_attach_args faa;
    704    1.4  jmcneill 	u_int uart_freq = 0;
    705    1.1  jmcneill 
    706    1.1  jmcneill 	if (initialized || cons == NULL)
    707    1.1  jmcneill 		return;
    708    1.1  jmcneill 
    709  1.102     skrll 	plat->fp_init_attach_args(&faa);
    710    1.1  jmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    711    1.1  jmcneill 
    712  1.102     skrll 	if (plat->fp_uart_freq != NULL)
    713  1.102     skrll 		uart_freq = plat->fp_uart_freq();
    714    1.4  jmcneill 
    715    1.4  jmcneill 	cons->consinit(&faa, uart_freq);
    716    1.1  jmcneill 
    717    1.1  jmcneill 	initialized = true;
    718    1.1  jmcneill }
    719    1.1  jmcneill 
    720    1.3  jmcneill void
    721   1.65  riastrad cpu_startup_hook(void)
    722   1.65  riastrad {
    723   1.91     skrll #ifdef EFI_RUNTIME
    724   1.91     skrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
    725   1.91     skrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
    726   1.91     skrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
    727   1.91     skrll #endif
    728   1.65  riastrad 
    729   1.68     skrll 	fdtbus_intr_init();
    730   1.68     skrll 
    731   1.65  riastrad 	fdt_setup_rndseed();
    732   1.69  riastrad 	fdt_setup_efirng();
    733   1.65  riastrad }
    734   1.65  riastrad 
    735   1.65  riastrad void
    736    1.3  jmcneill delay(u_int us)
    737    1.3  jmcneill {
    738  1.102     skrll 	const struct fdt_platform *plat = fdt_platform_find();
    739    1.3  jmcneill 
    740  1.102     skrll 	plat->fp_delay(us);
    741    1.3  jmcneill }
    742    1.3  jmcneill 
    743    1.1  jmcneill static void
    744   1.34  jmcneill fdt_detect_root_device(device_t dev)
    745   1.34  jmcneill {
    746   1.34  jmcneill 	int error, len;
    747  1.100       mrg 
    748   1.34  jmcneill 	const int chosen = OF_finddevice("/chosen");
    749   1.34  jmcneill 	if (chosen < 0)
    750   1.34  jmcneill 		return;
    751   1.34  jmcneill 
    752   1.34  jmcneill 	if (of_hasprop(chosen, "netbsd,mbr") &&
    753   1.34  jmcneill 	    of_hasprop(chosen, "netbsd,partition")) {
    754  1.100       mrg 		struct mbr_sector mbr;
    755  1.100       mrg 		uint8_t buf[DEV_BSIZE];
    756  1.100       mrg 		uint8_t hash[16];
    757  1.100       mrg 		const uint8_t *rhash;
    758  1.100       mrg 		struct vnode *vp;
    759  1.100       mrg 		MD5_CTX md5ctx;
    760  1.100       mrg 		size_t resid;
    761  1.100       mrg 		u_int part;
    762   1.34  jmcneill 
    763   1.34  jmcneill 		/*
    764   1.34  jmcneill 		 * The bootloader has passed in a partition index and MD5 hash
    765   1.34  jmcneill 		 * of the MBR sector. Read the MBR of this device, calculate the
    766   1.34  jmcneill 		 * hash, and compare it with the value passed in.
    767   1.34  jmcneill 		 */
    768   1.34  jmcneill 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
    769   1.34  jmcneill 		if (rhash == NULL || len != 16)
    770   1.34  jmcneill 			return;
    771   1.34  jmcneill 		of_getprop_uint32(chosen, "netbsd,partition", &part);
    772   1.34  jmcneill 		if (part >= MAXPARTITIONS)
    773   1.34  jmcneill 			return;
    774   1.34  jmcneill 
    775   1.34  jmcneill 		vp = opendisk(dev);
    776   1.34  jmcneill 		if (!vp)
    777   1.34  jmcneill 			return;
    778   1.34  jmcneill 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
    779   1.90   hannken 		    IO_NODELOCKED, NOCRED, &resid, NULL);
    780   1.34  jmcneill 		VOP_CLOSE(vp, FREAD, NOCRED);
    781   1.34  jmcneill 		vput(vp);
    782   1.34  jmcneill 
    783   1.34  jmcneill 		if (error != 0)
    784   1.34  jmcneill 			return;
    785   1.34  jmcneill 
    786   1.34  jmcneill 		memcpy(&mbr, buf, sizeof(mbr));
    787   1.34  jmcneill 		MD5Init(&md5ctx);
    788   1.34  jmcneill 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
    789   1.34  jmcneill 		MD5Final(hash, &md5ctx);
    790   1.34  jmcneill 
    791  1.100       mrg 		if (memcmp(rhash, hash, 16) == 0) {
    792  1.100       mrg 			booted_device = dev;
    793  1.100       mrg 			booted_partition = part;
    794  1.100       mrg 		}
    795   1.34  jmcneill 
    796  1.100       mrg 		return;
    797   1.34  jmcneill 	}
    798   1.53  jmcneill 
    799   1.53  jmcneill 	if (of_hasprop(chosen, "netbsd,gpt-guid")) {
    800  1.100       mrg 		const struct uuid *guid =
    801  1.100       mrg 		    fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
    802  1.100       mrg 
    803  1.101   mlelstv 		if (guid == NULL || len != 16)
    804  1.101   mlelstv 			return;
    805  1.101   mlelstv 
    806  1.101   mlelstv 		char guidstr[UUID_STR_LEN];
    807  1.101   mlelstv 		uuid_snprintf(guidstr, sizeof(guidstr), guid);
    808  1.101   mlelstv 
    809  1.101   mlelstv 		device_t dv = dkwedge_find_by_wname(guidstr);
    810  1.101   mlelstv 		if (dv != NULL)
    811  1.101   mlelstv 			booted_device = dv;
    812   1.53  jmcneill 
    813  1.100       mrg 		return;
    814   1.53  jmcneill 	}
    815   1.53  jmcneill 
    816   1.53  jmcneill 	if (of_hasprop(chosen, "netbsd,gpt-label")) {
    817   1.53  jmcneill 		const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
    818   1.53  jmcneill 		if (label == NULL || *label == '\0')
    819   1.53  jmcneill 			return;
    820   1.53  jmcneill 
    821   1.53  jmcneill 		device_t dv = dkwedge_find_by_wname(label);
    822   1.53  jmcneill 		if (dv != NULL)
    823   1.53  jmcneill 			booted_device = dv;
    824  1.100       mrg 
    825  1.100       mrg 		return;
    826   1.53  jmcneill 	}
    827   1.55  jmcneill 
    828   1.55  jmcneill 	if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
    829  1.100       mrg 		const uint8_t *macaddr =
    830  1.100       mrg 		    fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
    831  1.100       mrg 		struct ifnet *ifp;
    832  1.100       mrg 
    833   1.55  jmcneill 		if (macaddr == NULL || len != 6)
    834   1.55  jmcneill 			return;
    835  1.100       mrg 
    836   1.55  jmcneill 		int s = pserialize_read_enter();
    837   1.55  jmcneill 		IFNET_READER_FOREACH(ifp) {
    838   1.55  jmcneill 			if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
    839   1.55  jmcneill 				device_t dv = device_find_by_xname(ifp->if_xname);
    840   1.55  jmcneill 				if (dv != NULL)
    841   1.55  jmcneill 					booted_device = dv;
    842   1.55  jmcneill 				break;
    843   1.55  jmcneill 			}
    844   1.55  jmcneill 		}
    845   1.55  jmcneill 		pserialize_read_exit(s);
    846  1.100       mrg 
    847  1.100       mrg 		return;
    848   1.55  jmcneill 	}
    849   1.34  jmcneill }
    850   1.34  jmcneill 
    851   1.34  jmcneill static void
    852    1.1  jmcneill fdt_device_register(device_t self, void *aux)
    853    1.1  jmcneill {
    854  1.102     skrll 	const struct fdt_platform *plat = fdt_platform_find();
    855    1.1  jmcneill 
    856   1.75  jmcneill 	if (device_is_a(self, "armfdt")) {
    857    1.8  jmcneill 		fdt_setup_initrd();
    858    1.8  jmcneill 
    859   1.75  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    860   1.75  jmcneill 		/*
    861   1.75  jmcneill 		 * Setup framebuffer console, if present.
    862   1.75  jmcneill 		 */
    863   1.75  jmcneill 		arm_simplefb_preattach();
    864   1.75  jmcneill #endif
    865   1.75  jmcneill 	}
    866   1.75  jmcneill 
    867   1.77  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    868   1.77  jmcneill 	if (device_is_a(self, "genfb")) {
    869   1.77  jmcneill 		prop_dictionary_t dict = device_properties(self);
    870   1.77  jmcneill 		prop_dictionary_set_uint64(dict,
    871   1.77  jmcneill 		    "simplefb-physaddr", arm_simplefb_physaddr());
    872   1.77  jmcneill 	}
    873   1.77  jmcneill #endif
    874   1.77  jmcneill 
    875  1.102     skrll 	if (plat && plat->fp_device_register)
    876  1.102     skrll 		plat->fp_device_register(self, aux);
    877    1.1  jmcneill }
    878    1.1  jmcneill 
    879    1.1  jmcneill static void
    880   1.39    bouyer fdt_device_register_post_config(device_t self, void *aux)
    881   1.39    bouyer {
    882   1.39    bouyer #if NUKBD > 0 && NWSDISPLAY > 0
    883   1.39    bouyer 	if (device_is_a(self, "wsdisplay")) {
    884   1.39    bouyer 		struct wsdisplay_softc *sc = device_private(self);
    885   1.39    bouyer 		if (wsdisplay_isconsole(sc))
    886   1.39    bouyer 			ukbd_cnattach();
    887   1.39    bouyer 	}
    888   1.39    bouyer #endif
    889   1.39    bouyer }
    890   1.39    bouyer 
    891   1.39    bouyer static void
    892   1.34  jmcneill fdt_cpu_rootconf(void)
    893   1.34  jmcneill {
    894   1.34  jmcneill 	device_t dev;
    895   1.34  jmcneill 	deviter_t di;
    896   1.34  jmcneill 
    897   1.88       mrg 	if (booted_device != NULL)
    898   1.88       mrg 		return;
    899   1.88       mrg 
    900   1.34  jmcneill 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
    901   1.34  jmcneill 		if (device_class(dev) != DV_DISK)
    902   1.34  jmcneill 			continue;
    903   1.34  jmcneill 
    904  1.101   mlelstv 		fdt_detect_root_device(dev);
    905  1.100       mrg 
    906  1.100       mrg 		if (booted_device != NULL)
    907  1.100       mrg 			break;
    908   1.34  jmcneill 	}
    909   1.34  jmcneill 	deviter_release(&di);
    910   1.34  jmcneill }
    911   1.34  jmcneill 
    912   1.34  jmcneill static void
    913    1.1  jmcneill fdt_reset(void)
    914    1.1  jmcneill {
    915  1.102     skrll 	const struct fdt_platform *plat = fdt_platform_find();
    916    1.1  jmcneill 
    917    1.1  jmcneill 	fdtbus_power_reset();
    918    1.1  jmcneill 
    919  1.102     skrll 	if (plat && plat->fp_reset)
    920  1.102     skrll 		plat->fp_reset();
    921    1.1  jmcneill }
    922    1.1  jmcneill 
    923    1.1  jmcneill static void
    924    1.1  jmcneill fdt_powerdown(void)
    925    1.1  jmcneill {
    926    1.1  jmcneill 	fdtbus_power_poweroff();
    927    1.1  jmcneill }
    928   1.76       rin 
    929   1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    930   1.76       rin static void
    931   1.76       rin fdt_update_fb_format(void)
    932   1.76       rin {
    933   1.76       rin 	int off, len;
    934   1.76       rin 	const char *format, *replace;
    935   1.76       rin 
    936   1.76       rin 	off = fdt_path_offset(fdt_data, "/chosen");
    937   1.76       rin 	if (off < 0)
    938   1.76       rin 		return;
    939   1.76       rin 
    940   1.76       rin 	for (;;) {
    941   1.76       rin 		off = fdt_node_offset_by_compatible(fdt_data, off,
    942   1.76       rin 		    "simple-framebuffer");
    943   1.76       rin 		if (off < 0)
    944   1.76       rin 			return;
    945   1.76       rin 
    946   1.76       rin 		format = fdt_getprop(fdt_data, off, "format", &len);
    947   1.76       rin 		if (format == NULL)
    948   1.76       rin 			continue;
    949   1.76       rin 
    950   1.76       rin 		replace = NULL;
    951   1.76       rin 		if (strcmp(format, "a8b8g8r8") == 0)
    952   1.76       rin 			replace = "r8g8b8a8";
    953   1.76       rin 		else if (strcmp(format, "x8r8g8b8") == 0)
    954   1.76       rin 			replace = "b8g8r8x8";
    955   1.76       rin 		if (replace != NULL)
    956   1.76       rin 			fdt_setprop(fdt_data, off, "format", replace,
    957   1.76       rin 			    strlen(replace) + 1);
    958   1.76       rin 	}
    959   1.76       rin }
    960   1.76       rin #endif
    961