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