1 /* $NetBSD: fdt_machdep.c,v 1.73 2020/06/27 18:44:02 jmcneill 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.73 2020/06/27 18:44:02 jmcneill Exp $"); 31 32 #include "opt_machdep.h" 33 #include "opt_bootconfig.h" 34 #include "opt_ddb.h" 35 #include "opt_md.h" 36 #include "opt_arm_debug.h" 37 #include "opt_multiprocessor.h" 38 #include "opt_cpuoptions.h" 39 #include "opt_efi.h" 40 41 #include "ukbd.h" 42 #include "wsdisplay.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/bus.h> 47 #include <sys/atomic.h> 48 #include <sys/cpu.h> 49 #include <sys/device.h> 50 #include <sys/exec.h> 51 #include <sys/kernel.h> 52 #include <sys/kmem.h> 53 #include <sys/ksyms.h> 54 #include <sys/msgbuf.h> 55 #include <sys/proc.h> 56 #include <sys/reboot.h> 57 #include <sys/termios.h> 58 #include <sys/bootblock.h> 59 #include <sys/disklabel.h> 60 #include <sys/vnode.h> 61 #include <sys/kauth.h> 62 #include <sys/fcntl.h> 63 #include <sys/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 = 311 round_page(initrd_end) - trunc_page(initrd_start); 312 if (initrd_size > 0) 313 fdt_memory_remove_range(trunc_page(initrd_start), initrd_size); 314 315 const uint64_t rndseed_size = 316 round_page(rndseed_end) - trunc_page(rndseed_start); 317 if (rndseed_size > 0) 318 fdt_memory_remove_range(trunc_page(rndseed_start), 319 rndseed_size); 320 321 const uint64_t efirng_size = 322 round_page(efirng_end) - trunc_page(efirng_start); 323 if (efirng_size > 0) 324 fdt_memory_remove_range(trunc_page(efirng_start), efirng_size); 325 326 const int framebuffer = OF_finddevice("/chosen/framebuffer"); 327 if (framebuffer >= 0) { 328 for (index = 0; 329 fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0; 330 index++) { 331 fdt_add_reserved_memory_range(addr, size); 332 } 333 } 334 335 VPRINTF("Usable memory:\n"); 336 bc->dramblocks = 0; 337 fdt_memory_foreach(fdt_add_dram_blocks, bc); 338 } 339 340 static void 341 fdt_probe_range(const char *startname, const char *endname, 342 uint64_t *pstart, uint64_t *pend) 343 { 344 int chosen, len; 345 const void *start_data, *end_data; 346 347 *pstart = *pend = 0; 348 349 chosen = OF_finddevice("/chosen"); 350 if (chosen < 0) 351 return; 352 353 start_data = fdtbus_get_prop(chosen, startname, &len); 354 end_data = fdtbus_get_prop(chosen, endname, NULL); 355 if (start_data == NULL || end_data == NULL) 356 return; 357 358 switch (len) { 359 case 4: 360 *pstart = be32dec(start_data); 361 *pend = be32dec(end_data); 362 break; 363 case 8: 364 *pstart = be64dec(start_data); 365 *pend = be64dec(end_data); 366 break; 367 default: 368 printf("Unsupported len %d for /chosen `%s'\n", 369 len, startname); 370 return; 371 } 372 } 373 374 static void * 375 fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize, 376 const char *purpose) 377 { 378 const paddr_t startpa = trunc_page(start); 379 const paddr_t endpa = round_page(end); 380 paddr_t pa; 381 vaddr_t va; 382 void *ptr; 383 384 *psize = end - start; 385 if (*psize == 0) 386 return NULL; 387 388 const vaddr_t voff = start & PAGE_MASK; 389 390 va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT); 391 if (va == 0) { 392 printf("Failed to allocate VA for %s\n", purpose); 393 return NULL; 394 } 395 ptr = (void *)(va + voff); 396 397 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) 398 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0); 399 pmap_update(pmap_kernel()); 400 401 return ptr; 402 } 403 404 static void 405 fdt_unmap_range(void *ptr, uint64_t size) 406 { 407 const char *start = ptr, *end = start + size; 408 const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start); 409 const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end); 410 411 pmap_kremove(startva, endva - startva); 412 pmap_update(pmap_kernel()); 413 } 414 415 static void 416 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend) 417 { 418 *pstart = *pend = 0; 419 420 #ifdef MEMORY_DISK_DYNAMIC 421 fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend); 422 #endif 423 } 424 425 static void 426 fdt_setup_initrd(void) 427 { 428 #ifdef MEMORY_DISK_DYNAMIC 429 void *md_start; 430 uint64_t initrd_size; 431 432 md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size, 433 "initrd"); 434 if (md_start == NULL) 435 return; 436 md_root_setconf(md_start, initrd_size); 437 #endif 438 } 439 440 static void 441 fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend) 442 { 443 444 fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end", 445 pstart, pend); 446 } 447 448 static void 449 fdt_setup_rndseed(void) 450 { 451 uint64_t rndseed_size; 452 void *rndseed; 453 454 rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size, 455 "rndseed"); 456 if (rndseed == NULL) 457 return; 458 rnd_seed(rndseed, rndseed_size); 459 fdt_unmap_range(rndseed, rndseed_size); 460 } 461 462 static void 463 fdt_probe_efirng(uint64_t *pstart, uint64_t *pend) 464 { 465 466 fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end", 467 pstart, pend); 468 } 469 470 static struct krndsource efirng_source; 471 472 static void 473 fdt_setup_efirng(void) 474 { 475 uint64_t efirng_size; 476 void *efirng; 477 478 efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size, 479 "efirng"); 480 if (efirng == NULL) 481 return; 482 483 rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG, 484 RND_FLAG_DEFAULT); 485 rnd_add_data(&efirng_source, efirng, efirng_size, 0); 486 explicit_memset(efirng, 0, efirng_size); 487 fdt_unmap_range(efirng, efirng_size); 488 } 489 490 #ifdef EFI_RUNTIME 491 static void 492 fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type) 493 { 494 int len; 495 496 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 497 if (chosen_off < 0) 498 return; 499 500 const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len); 501 if (map == NULL) 502 return; 503 504 while (len >= 24) { 505 const paddr_t pa = be64toh(map[0]); 506 const vaddr_t va = be64toh(map[1]); 507 const uint64_t sz = be64toh(map[2]); 508 VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1); 509 arm_efirt_md_map_range(va, pa, sz, type); 510 map += 3; 511 len -= 24; 512 } 513 } 514 #endif 515 516 vaddr_t 517 initarm(void *arg) 518 { 519 const struct arm_platform *plat; 520 uint64_t memory_start, memory_end; 521 522 /* set temporally to work printf()/panic() even before consinit() */ 523 cn_tab = &earlycons; 524 525 /* Load FDT */ 526 int error = fdt_check_header(fdt_addr_r); 527 if (error == 0) { 528 /* If the DTB is too big, try to pack it in place first. */ 529 if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data)) 530 (void)fdt_pack(__UNCONST(fdt_addr_r)); 531 error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data)); 532 if (error != 0) 533 panic("fdt_move failed: %s", fdt_strerror(error)); 534 fdtbus_init(fdt_data); 535 } else { 536 panic("fdt_check_header failed: %s", fdt_strerror(error)); 537 } 538 539 /* Lookup platform specific backend */ 540 plat = arm_fdt_platform(); 541 if (plat == NULL) 542 panic("Kernel does not support this device"); 543 544 /* Early console may be available, announce ourselves. */ 545 VPRINTF("FDT<%p>\n", fdt_addr_r); 546 547 const int chosen = OF_finddevice("/chosen"); 548 if (chosen >= 0) 549 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs)); 550 boot_args = bootargs; 551 552 /* Heads up ... Setup the CPU / MMU / TLB functions. */ 553 VPRINTF("cpufunc\n"); 554 if (set_cpufuncs()) 555 panic("cpu not recognized!"); 556 557 /* 558 * Memory is still identity/flat mapped this point so using ttbr for 559 * l1pt VA is fine 560 */ 561 562 VPRINTF("devmap\n"); 563 extern char ARM_BOOTSTRAP_LxPT[]; 564 pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap()); 565 566 VPRINTF("bootstrap\n"); 567 plat->ap_bootstrap(); 568 569 /* 570 * If stdout-path is specified on the command line, override the 571 * value in /chosen/stdout-path before initializing console. 572 */ 573 VPRINTF("stdout\n"); 574 fdt_update_stdout_path(); 575 576 /* 577 * Done making changes to the FDT. 578 */ 579 fdt_pack(fdt_data); 580 581 VPRINTF("consinit "); 582 consinit(); 583 VPRINTF("ok\n"); 584 585 VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n", 586 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]); 587 588 cpu_reset_address = fdt_reset; 589 cpu_powerdown_address = fdt_powerdown; 590 evbarm_device_register = fdt_device_register; 591 evbarm_device_register_post_config = fdt_device_register_post_config; 592 evbarm_cpu_rootconf = fdt_cpu_rootconf; 593 594 /* Talk to the user */ 595 printf("NetBSD/evbarm (fdt) booting ...\n"); 596 597 #ifdef BOOT_ARGS 598 char mi_bootargs[] = BOOT_ARGS; 599 parse_mi_bootargs(mi_bootargs); 600 #endif 601 602 fdt_get_memory(&memory_start, &memory_end); 603 604 #if !defined(_LP64) 605 /* Cannot map memory above 4GB */ 606 if (memory_end >= 0x100000000ULL) 607 memory_end = 0x100000000ULL - PAGE_SIZE; 608 609 #endif 610 uint64_t memory_size = memory_end - memory_start; 611 612 VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %" 613 PRIx64 ")\n", __func__, memory_start, memory_end, memory_size); 614 615 /* Parse ramdisk info */ 616 fdt_probe_initrd(&initrd_start, &initrd_end); 617 618 /* Parse our on-disk rndseed and the firmware's RNG from EFI */ 619 fdt_probe_rndseed(&rndseed_start, &rndseed_end); 620 fdt_probe_efirng(&efirng_start, &efirng_end); 621 622 /* 623 * Populate bootconfig structure for the benefit of 624 * dodumpsys 625 */ 626 VPRINTF("%s: fdt_build_bootconfig\n", __func__); 627 fdt_build_bootconfig(memory_start, memory_end); 628 629 #ifdef EFI_RUNTIME 630 fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE); 631 fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA); 632 fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO); 633 #endif 634 635 /* Perform PT build and VM init */ 636 cpu_kernel_vm_init(memory_start, memory_size); 637 638 VPRINTF("bootargs: %s\n", bootargs); 639 640 parse_mi_bootargs(boot_args); 641 642 VPRINTF("Memory regions:\n"); 643 fdt_memory_foreach(fdt_add_boot_physmem, &memory_size); 644 645 vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem, 646 nfdt_physmem); 647 648 /* 649 * initarm_common flushes cache if required before AP start 650 */ 651 error = 0; 652 if ((boothowto & RB_MD1) == 0) { 653 VPRINTF("mpstart\n"); 654 if (plat->ap_mpstart) 655 error = plat->ap_mpstart(); 656 } 657 658 if (error) 659 return sp; 660 /* 661 * Now we have APs started the pages used for stacks and L1PT can 662 * be given to uvm 663 */ 664 extern char const __start__init_memory[]; 665 extern char const __stop__init_memory[] __weak; 666 667 if (__start__init_memory != __stop__init_memory) { 668 const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory); 669 const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory); 670 const paddr_t spg = atop(spa); 671 const paddr_t epg = atop(epa); 672 673 VPRINTF(" start %08lx end %08lx... " 674 "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT); 675 676 uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT); 677 678 } 679 680 return sp; 681 } 682 683 static void 684 fdt_update_stdout_path(void) 685 { 686 char *stdout_path, *ep; 687 int stdout_path_len; 688 char buf[256]; 689 690 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 691 if (chosen_off == -1) 692 return; 693 694 if (get_bootconf_option(boot_args, "stdout-path", 695 BOOTOPT_TYPE_STRING, &stdout_path) == 0) 696 return; 697 698 ep = strchr(stdout_path, ' '); 699 stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path); 700 if (stdout_path_len >= sizeof(buf)) 701 return; 702 703 strncpy(buf, stdout_path, stdout_path_len); 704 buf[stdout_path_len] = '\0'; 705 fdt_setprop(fdt_data, chosen_off, "stdout-path", 706 buf, stdout_path_len + 1); 707 } 708 709 void 710 consinit(void) 711 { 712 static bool initialized = false; 713 const struct arm_platform *plat = arm_fdt_platform(); 714 const struct fdt_console *cons = fdtbus_get_console(); 715 struct fdt_attach_args faa; 716 u_int uart_freq = 0; 717 718 if (initialized || cons == NULL) 719 return; 720 721 plat->ap_init_attach_args(&faa); 722 faa.faa_phandle = fdtbus_get_stdout_phandle(); 723 724 if (plat->ap_uart_freq != NULL) 725 uart_freq = plat->ap_uart_freq(); 726 727 cons->consinit(&faa, uart_freq); 728 729 initialized = true; 730 } 731 732 void 733 cpu_startup_hook(void) 734 { 735 736 fdtbus_intr_init(); 737 738 fdt_setup_rndseed(); 739 fdt_setup_efirng(); 740 } 741 742 void 743 delay(u_int us) 744 { 745 const struct arm_platform *plat = arm_fdt_platform(); 746 747 plat->ap_delay(us); 748 } 749 750 static void 751 fdt_detect_root_device(device_t dev) 752 { 753 struct mbr_sector mbr; 754 uint8_t buf[DEV_BSIZE]; 755 uint8_t hash[16]; 756 const uint8_t *rhash; 757 char rootarg[64]; 758 struct vnode *vp; 759 MD5_CTX md5ctx; 760 int error, len; 761 size_t resid; 762 u_int part; 763 764 const int chosen = OF_finddevice("/chosen"); 765 if (chosen < 0) 766 return; 767 768 if (of_hasprop(chosen, "netbsd,mbr") && 769 of_hasprop(chosen, "netbsd,partition")) { 770 771 /* 772 * The bootloader has passed in a partition index and MD5 hash 773 * of the MBR sector. Read the MBR of this device, calculate the 774 * hash, and compare it with the value passed in. 775 */ 776 rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len); 777 if (rhash == NULL || len != 16) 778 return; 779 of_getprop_uint32(chosen, "netbsd,partition", &part); 780 if (part >= MAXPARTITIONS) 781 return; 782 783 vp = opendisk(dev); 784 if (!vp) 785 return; 786 error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE, 787 0, NOCRED, &resid, NULL); 788 VOP_CLOSE(vp, FREAD, NOCRED); 789 vput(vp); 790 791 if (error != 0) 792 return; 793 794 memcpy(&mbr, buf, sizeof(mbr)); 795 MD5Init(&md5ctx); 796 MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr)); 797 MD5Final(hash, &md5ctx); 798 799 if (memcmp(rhash, hash, 16) != 0) 800 return; 801 802 snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a'); 803 strcat(boot_args, rootarg); 804 } 805 806 if (of_hasprop(chosen, "netbsd,gpt-guid")) { 807 char guidbuf[UUID_STR_LEN]; 808 const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len); 809 if (guid == NULL || len != 16) 810 return; 811 812 uuid_snprintf(guidbuf, sizeof(guidbuf), guid); 813 snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf); 814 strcat(boot_args, rootarg); 815 } 816 817 if (of_hasprop(chosen, "netbsd,gpt-label")) { 818 const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label"); 819 if (label == NULL || *label == '\0') 820 return; 821 822 device_t dv = dkwedge_find_by_wname(label); 823 if (dv != NULL) 824 booted_device = dv; 825 } 826 827 if (of_hasprop(chosen, "netbsd,booted-mac-address")) { 828 const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len); 829 if (macaddr == NULL || len != 6) 830 return; 831 int s = pserialize_read_enter(); 832 struct ifnet *ifp; 833 IFNET_READER_FOREACH(ifp) { 834 if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) { 835 device_t dv = device_find_by_xname(ifp->if_xname); 836 if (dv != NULL) 837 booted_device = dv; 838 break; 839 } 840 } 841 pserialize_read_exit(s); 842 } 843 } 844 845 static void 846 fdt_device_register(device_t self, void *aux) 847 { 848 const struct arm_platform *plat = arm_fdt_platform(); 849 850 if (device_is_a(self, "armfdt")) 851 fdt_setup_initrd(); 852 853 if (plat && plat->ap_device_register) 854 plat->ap_device_register(self, aux); 855 } 856 857 static void 858 fdt_device_register_post_config(device_t self, void *aux) 859 { 860 #if NUKBD > 0 && NWSDISPLAY > 0 861 if (device_is_a(self, "wsdisplay")) { 862 struct wsdisplay_softc *sc = device_private(self); 863 if (wsdisplay_isconsole(sc)) 864 ukbd_cnattach(); 865 } 866 #endif 867 } 868 869 static void 870 fdt_cpu_rootconf(void) 871 { 872 device_t dev; 873 deviter_t di; 874 char *ptr; 875 876 for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) { 877 if (device_class(dev) != DV_DISK) 878 continue; 879 880 if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0) 881 break; 882 883 if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd")) 884 fdt_detect_root_device(dev); 885 } 886 deviter_release(&di); 887 } 888 889 static void 890 fdt_reset(void) 891 { 892 const struct arm_platform *plat = arm_fdt_platform(); 893 894 fdtbus_power_reset(); 895 896 if (plat && plat->ap_reset) 897 plat->ap_reset(); 898 } 899 900 static void 901 fdt_powerdown(void) 902 { 903 fdtbus_power_poweroff(); 904 } 905