1 /* $NetBSD: autoconf.c,v 1.23 2019/05/24 14:28:48 nonaka Exp $ */ 2 /* NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp */ 3 4 /*- 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)autoconf.c 7.1 (Berkeley) 5/9/91 36 */ 37 38 /* 39 * Setup the system to run on the current machine. 40 * 41 * Configure() is called at boot time and initializes the vba 42 * device tables and the memory controller monitoring. Available 43 * devices are determined (from possibilities mentioned in ioconf.c), 44 * and the drivers are initialized. 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.23 2019/05/24 14:28:48 nonaka Exp $"); 49 50 #include "opt_xen.h" 51 #include "opt_multiprocessor.h" 52 #include "opt_nfs_boot.h" 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/buf.h> 57 #include <sys/disklabel.h> 58 #include <sys/disk.h> 59 #include <sys/conf.h> 60 #include <sys/device.h> 61 #include <sys/vnode.h> 62 #include <sys/fcntl.h> 63 #include <sys/dkio.h> 64 #include <sys/proc.h> 65 #include <sys/kauth.h> 66 67 #ifdef NFS_BOOT_BOOTSTATIC 68 #include <net/if.h> 69 #include <net/if_ether.h> 70 #include <netinet/in.h> 71 #include <nfs/rpcv2.h> 72 #include <nfs/nfsproto.h> 73 #include <nfs/nfs.h> 74 #include <nfs/nfsmount.h> 75 #include <nfs/nfsdiskless.h> 76 #include <xen/if_xennetvar.h> 77 #endif 78 79 #include <machine/pte.h> 80 #include <machine/cpu.h> 81 #include <machine/gdt.h> 82 #include <machine/pcb.h> 83 #include <machine/bootinfo.h> 84 85 static int is_valid_disk(device_t); 86 87 struct disklist *x86_alldisks; 88 int x86_ndisks; 89 int x86_found_console; 90 91 #include "bios32.h" 92 #if NBIOS32 > 0 93 #include <machine/bios32.h> 94 /* XXX */ 95 extern void platform_init(void); 96 #endif 97 98 #include "opt_pcibios.h" 99 #ifdef PCIBIOS 100 #include <dev/pci/pcireg.h> 101 #include <dev/pci/pcivar.h> 102 #include <i386/pci/pcibios.h> 103 #endif 104 105 #ifdef DEBUG_GEOM 106 #define DPRINTF(a) printf a 107 #else 108 #define DPRINTF(a) 109 #endif 110 111 /* 112 * Determine i/o configuration for a machine. 113 */ 114 void 115 cpu_configure(void) 116 { 117 struct pcb *pcb; 118 119 startrtclock(); 120 121 #if defined(DOM0OPS) 122 if (xendomain_is_dom0()) { 123 #if NBIOS32 > 0 124 bios32_init(); 125 platform_init(); 126 /* identify hypervisor type from SMBIOS */ 127 identify_hypervisor(); 128 #endif /* NBIOS32 > 0 */ 129 } else 130 #endif /* DOM0OPS */ 131 vm_guest = VM_GUEST_XEN; 132 #ifdef PCIBIOS 133 pcibios_init(); 134 #endif 135 136 if (config_rootfound("mainbus", NULL) == NULL) 137 panic("configure: mainbus not configured"); 138 139 #ifdef INTRDEBUG 140 intr_printconfig(); 141 #endif 142 143 #if NIOAPIC > 0 144 ioapic_enable(); 145 #endif 146 /* resync cr0 after FPU configuration */ 147 pcb = lwp_getpcb(&lwp0); 148 pcb->pcb_cr0 = rcr0(); 149 #ifdef MULTIPROCESSOR 150 /* propagate this to the idle pcb's. */ 151 cpu_init_idle_lwps(); 152 #endif 153 154 spl0(); 155 } 156 157 void 158 cpu_rootconf(void) 159 { 160 cpu_bootconf(); 161 162 printf("boot device: %s\n", 163 booted_device ? device_xname(booted_device) : 164 bootspec ? bootspec : "<unknown>"); 165 rootconf(); 166 } 167 168 169 /* 170 * Attempt to find the device from which we were booted. 171 */ 172 void 173 cpu_bootconf(void) 174 { 175 device_t dv; 176 deviter_t di; 177 union xen_cmdline_parseinfo xcp; 178 static char bootspecbuf[sizeof(xcp.xcp_bootdev)]; 179 180 if (booted_device) { 181 DPRINTF(("%s: preset booted_device: %s\n", __func__, device_xname(booted_device))); 182 return; 183 } 184 185 xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp); 186 187 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); 188 dv != NULL; 189 dv = deviter_next(&di)) { 190 bool is_ifnet, is_disk; 191 const char *devname; 192 193 is_ifnet = (device_class(dv) == DV_IFNET); 194 is_disk = is_valid_disk(dv); 195 devname = device_xname(dv); 196 197 if (!is_ifnet && !is_disk) 198 continue; 199 200 if (is_disk && xcp.xcp_bootdev[0] == 0) { 201 booted_device = dv; 202 break; 203 } 204 205 if (strncmp(xcp.xcp_bootdev, devname, strlen(devname))) 206 continue; 207 208 if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) { 209 /* XXX check device_cfdata as in x86_autoconf.c? */ 210 booted_partition = toupper( 211 xcp.xcp_bootdev[strlen(devname)]) - 'A'; 212 DPRINTF(("%s: booted_partition: %d\n", __func__, booted_partition)); 213 } 214 215 booted_device = dv; 216 booted_method = "bootinfo/bootdev"; 217 break; 218 } 219 deviter_release(&di); 220 221 if (booted_device) { 222 DPRINTF(("%s: booted_device: %s\n", __func__, device_xname(booted_device))); 223 return; 224 } 225 226 /* 227 * not a boot device name, pass through to MI code 228 */ 229 if (xcp.xcp_bootdev[0] != '\0') { 230 strlcpy(bootspecbuf, xcp.xcp_bootdev, sizeof(bootspecbuf)); 231 bootspec = bootspecbuf; 232 booted_method = "bootinfo/bootspec"; 233 DPRINTF(("%s: bootspec: %s\n", __func__, bootspec)); 234 return; 235 } 236 } 237 238 #include "pci.h" 239 240 #include <dev/isa/isavar.h> 241 #if NPCI > 0 242 #include <dev/pci/pcivar.h> 243 #endif 244 245 246 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS) 247 static int 248 dom0_bootstatic_callback(struct nfs_diskless *nd) 249 { 250 #if 0 251 struct ifnet *ifp = nd->nd_ifp; 252 #endif 253 int flags = 0; 254 union xen_cmdline_parseinfo xcp; 255 struct sockaddr_in *sin; 256 257 memset(&xcp, 0, sizeof(xcp.xcp_netinfo)); 258 xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */ 259 xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host; 260 xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp); 261 262 if (xcp.xcp_netinfo.xi_root[0] != '\0') { 263 flags |= NFS_BOOT_HAS_SERVER; 264 if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL) 265 flags |= NFS_BOOT_HAS_ROOTPATH; 266 } 267 268 nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]); 269 nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]); 270 nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]); 271 272 sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr; 273 memset((void *)sin, 0, sizeof(*sin)); 274 sin->sin_len = sizeof(*sin); 275 sin->sin_family = AF_INET; 276 sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]); 277 278 if (nd->nd_myip.s_addr) 279 flags |= NFS_BOOT_HAS_MYIP; 280 if (nd->nd_gwip.s_addr) 281 flags |= NFS_BOOT_HAS_GWIP; 282 if (nd->nd_mask.s_addr) 283 flags |= NFS_BOOT_HAS_MASK; 284 if (sin->sin_addr.s_addr) 285 flags |= NFS_BOOT_HAS_SERVADDR; 286 287 return flags; 288 } 289 #endif 290 291 void 292 device_register(device_t dev, void *aux) 293 { 294 /* 295 * Handle network interfaces here, the attachment information is 296 * not available driver independently later. 297 * For disks, there is nothing useful available at attach time. 298 */ 299 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS) 300 if (device_class(dev) == DV_IFNET) { 301 union xen_cmdline_parseinfo xcp; 302 303 #ifdef NFS_BOOT_BOOTSTATIC 304 #ifdef DOM0OPS 305 if (xendomain_is_privileged()) { 306 nfs_bootstatic_callback = dom0_bootstatic_callback; 307 } else 308 #endif 309 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 310 nfs_bootstatic_callback = xennet_bootstatic_callback; 311 #endif 312 #endif 313 xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp); 314 if (strncmp(xcp.xcp_bootdev, device_xname(dev), 315 sizeof(xcp.xcp_bootdev)) == 0) 316 { 317 goto found; 318 } 319 } 320 #endif 321 if (device_class(dev) == DV_IFNET) { 322 struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF); 323 if (bin == NULL) 324 return; 325 326 /* 327 * We don't check the driver name against the device name 328 * passed by the boot ROM. The ROM should stay usable 329 * if the driver gets obsoleted. 330 * The physical attachment information (checked below) 331 * must be sufficient to identify the device. 332 */ 333 334 if (bin->bus == BI_BUS_ISA && 335 device_is_a(device_parent(dev), "isa")) { 336 struct isa_attach_args *iaa = aux; 337 338 /* compare IO base address */ 339 /* XXXJRT what about multiple I/O addrs? */ 340 if (iaa->ia_nio > 0 && 341 bin->addr.iobase == iaa->ia_io[0].ir_addr) 342 goto found; 343 } 344 #if NPCI > 0 345 if (bin->bus == BI_BUS_PCI && 346 device_is_a(device_parent(dev), "pci")) { 347 struct pci_attach_args *paa = aux; 348 int b, d, f; 349 350 /* 351 * Calculate BIOS representation of: 352 * 353 * <bus,device,function> 354 * 355 * and compare. 356 */ 357 pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f); 358 if (bin->addr.tag == ((b << 8) | (d << 3) | f)) 359 goto found; 360 } 361 #endif 362 } 363 return; 364 365 found: 366 if (booted_device) { 367 /* XXX should be a "panic()" */ 368 printf("warning: double match for boot device (%s, %s)\n", 369 device_xname(booted_device), device_xname(dev)); 370 return; 371 } 372 booted_device = dev; 373 } 374 375 static int 376 is_valid_disk(device_t dv) 377 { 378 if (device_class(dv) != DV_DISK) 379 return (0); 380 381 return (device_is_a(dv, "dk") || 382 device_is_a(dv, "sd") || 383 device_is_a(dv, "wd") || 384 device_is_a(dv, "ld") || 385 device_is_a(dv, "ed") || 386 device_is_a(dv, "xbd")); 387 } 388