1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
38  * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
39  */
40 
41 /*
42  * Setup the system to run on the current machine.
43  *
44  * Configure() is called at boot time and initializes the vba
45  * device tables and the memory controller monitoring.  Available
46  * devices are determined (from possibilities mentioned in ioconf.c),
47  * and the drivers are initialized.
48  */
49 #include "opt_bootp.h"
50 #include "opt_ffs.h"
51 #include "opt_cd9660.h"
52 #include "opt_nfs.h"
53 #include "opt_nfsroot.h"
54 #include "opt_bus.h"
55 #include "opt_rootdevname.h"
56 
57 #include "use_isa.h"
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bootmaj.h>
62 #include <sys/bus.h>
63 #include <sys/buf.h>
64 #include <sys/conf.h>
65 #include <sys/diskslice.h>
66 #include <sys/reboot.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mount.h>
70 #include <sys/cons.h>
71 #include <sys/thread.h>
72 #include <sys/device.h>
73 #include <sys/machintr.h>
74 
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_pager.h>
78 
79 #if 0
80 #include <machine/pcb.h>
81 #include <machine/pcb_ext.h>
82 #endif
83 #include <machine/smp.h>
84 #include <machine/globaldata.h>
85 #include <machine/md_var.h>
86 
87 #if NISA > 0
88 #include <bus/isa/isavar.h>
89 
90 device_t isa_bus_device = 0;
91 #endif
92 
93 static void cpu_startup (void *);
94 static void configure_first (void *);
95 static void configure (void *);
96 static void configure_final (void *);
97 
98 #if defined(FFS) && defined(FFS_ROOT)
99 static void	setroot (void);
100 #endif
101 
102 #if defined(NFS) && defined(NFS_ROOT)
103 #if !defined(BOOTP_NFSROOT)
104 static void	pxe_setup_nfsdiskless(void);
105 #endif
106 #endif
107 
108 SYSINIT(cpu, SI_BOOT2_SMP, SI_ORDER_FIRST, cpu_startup, NULL);
109 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
110 /* SI_ORDER_SECOND is hookable */
111 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
112 /* SI_ORDER_MIDDLE is hookable */
113 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
114 
115 cdev_t	rootdev = NULL;
116 cdev_t	dumpdev = NULL;
117 
118 /*
119  *
120  */
121 static void
122 cpu_startup(void *dummy)
123 {
124 	vm_offset_t buffer_sva;
125 	vm_offset_t buffer_eva;
126 	vm_offset_t pager_sva;
127 	vm_offset_t pager_eva;
128 
129 	kprintf("%s", version);
130 	kprintf("real memory = %ju (%juK bytes)\n",
131 	    (uintmax_t)ptoa(Maxmem), (uintmax_t)(ptoa(Maxmem) / 1024));
132 
133 	if (nbuf == 0) {
134 		int factor = 4 * BKVASIZE / 1024;
135 		int kbytes = Maxmem * (PAGE_SIZE / 1024);
136 
137 		nbuf = 50;
138 		if (kbytes > 4096)
139 			nbuf += min((kbytes - 4096) / factor, 65536 / factor);
140 		if (kbytes > 65536)
141 			nbuf += (kbytes - 65536) * 2 / (factor * 5);
142 		if (maxbcache && nbuf > maxbcache / BKVASIZE)
143 			nbuf = maxbcache / BKVASIZE;
144 	}
145 	if (nbuf > (virtual_end - virtual_start) / (BKVASIZE * 2)) {
146 		nbuf = (virtual_end - virtual_start) / (BKVASIZE * 2);
147 		kprintf("Warning: nbufs capped at %d\n", nbuf);
148 	}
149 
150 	nswbuf = max(min(nbuf/4, 256), 16);
151 #ifdef NSWBUF_MIN
152 	if (nswbuf < NSWBUF_MIN)
153 		nswbuf = NSWBUF_MIN;
154 #endif
155 
156 	/*
157 	 * Allocate memory for the buffer cache
158 	 */
159 	buf = (void *)kmem_alloc(&kernel_map, nbuf * sizeof(struct buf));
160 	swbuf = (void *)kmem_alloc(&kernel_map, nswbuf * sizeof(struct buf));
161 
162 
163 #ifdef DIRECTIO
164         ffs_rawread_setup();
165 #endif
166 	kmem_suballoc(&kernel_map, &clean_map, &clean_sva, &clean_eva,
167 		      (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
168 	kmem_suballoc(&clean_map, &buffer_map, &buffer_sva, &buffer_eva,
169 		      (nbuf*BKVASIZE));
170 	buffer_map.system_map = 1;
171 	kmem_suballoc(&clean_map, &pager_map, &pager_sva, &pager_eva,
172 		      (nswbuf*MAXPHYS) + pager_map_size);
173 	pager_map.system_map = 1;
174 #if defined(USERCONFIG)
175         userconfig();
176 	cninit();               /* the preferred console may have changed */
177 #endif
178 	kprintf("avail memory = %lu (%luK bytes)\n", ptoa(vmstats.v_free_count),
179 		ptoa(vmstats.v_free_count) / 1024);
180 	bufinit();
181 	vm_pager_bufferinit();
182 #ifdef SMP
183 	mp_start();
184 	mp_announce();
185 #endif
186 	cpu_setregs();
187 }
188 
189 /*
190  * Determine i/o configuration for a machine.
191  */
192 static void
193 configure_first(void *dummy)
194 {
195 }
196 
197 static void
198 configure(void *dummy)
199 {
200         /*
201 	 * Final interrupt support acviation, then enable hardware interrupts.
202 	 */
203 	MachIntrABI.finalize();
204 	cpu_enable_intr();
205 
206 	/*
207 	 * This will configure all devices, generally starting with the
208 	 * nexus (i386/i386/nexus.c).  The nexus ISA code explicitly
209 	 * dummies up the attach in order to delay legacy initialization
210 	 * until after all other busses/subsystems have had a chance
211 	 * at those resources.
212 	 */
213 	root_bus_configure();
214 
215 #if NISA > 0
216 	/*
217 	 * Explicitly probe and attach ISA last.  The isa bus saves
218 	 * it's device node at attach time for us here.
219 	 */
220 	if (isa_bus_device)
221 		isa_probe_children(isa_bus_device);
222 #endif
223 
224 	/*
225 	 * Allow lowering of the ipl to the lowest kernel level if we
226 	 * panic (or call tsleep() before clearing `cold').  No level is
227 	 * completely safe (since a panic may occur in a critical region
228 	 * at splhigh()), but we want at least bio interrupts to work.
229 	 */
230 	safepri = TDPRI_KERN_USER;
231 }
232 
233 static void
234 configure_final(void *dummy)
235 {
236 	cninit_finish();
237 
238 	if (bootverbose)
239 		kprintf("Device configuration finished.\n");
240 }
241 
242 #ifdef BOOTP
243 void bootpc_init(void);
244 #endif
245 /*
246  * Do legacy root filesystem discovery.
247  */
248 void
249 cpu_rootconf(void)
250 {
251 #ifdef BOOTP
252         bootpc_init();
253 #endif
254 #if defined(NFS) && defined(NFS_ROOT)
255 #if !defined(BOOTP_NFSROOT)
256 	pxe_setup_nfsdiskless();
257 	if (nfs_diskless_valid)
258 #endif
259 		rootdevnames[0] = "nfs:";
260 #endif
261 #if defined(FFS) && defined(FFS_ROOT)
262         if (!rootdevnames[0])
263                 setroot();
264 #endif
265 }
266 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
267 
268 u_long	bootdev = 0;		/* not a cdev_t - encoding is different */
269 
270 #if defined(FFS) && defined(FFS_ROOT)
271 
272 /*
273  * The boot code uses old block device major numbers to pass bootdev to
274  * us.  We have to translate these to character device majors because
275  * we don't have block devices any more.
276  */
277 static int
278 boot_translate_majdev(int bmajor)
279 {
280 	static int conv[] = { BOOTMAJOR_CONVARY };
281 
282 	if (bmajor >= 0 && bmajor < sizeof(conv)/sizeof(conv[0]))
283 		return(conv[bmajor]);
284 	return(-1);
285 }
286 
287 /*
288  * Attempt to find the device from which we were booted.
289  * If we can do so, and not instructed not to do so,
290  * set rootdevs[] and rootdevnames[] to correspond to the
291  * boot device(s).
292  *
293  * This code survives in order to allow the system to be
294  * booted from legacy environments that do not correctly
295  * populate the kernel environment. There are significant
296  * restrictions on the bootability of the system in this
297  * situation; it can only be mounting root from a 'da'
298  * 'wd' or 'fd' device, and the root filesystem must be ufs.
299  */
300 static void
301 setroot(void)
302 {
303 	int majdev, mindev, unit, slice, part;
304 	cdev_t newrootdev, dev;
305 	char partname[2];
306 	char *sname;
307 
308 	if ((bootdev & B_MAGICMASK) != B_DEVMAGIC) {
309 		kprintf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev);
310 		return;
311 	}
312 	majdev = boot_translate_majdev(B_TYPE(bootdev));
313 	if (bootverbose) {
314 		kprintf("bootdev: %08lx type=%ld unit=%ld "
315 			"slice=%ld part=%ld major=%d\n",
316 			bootdev, B_TYPE(bootdev), B_UNIT(bootdev),
317 			B_SLICE(bootdev), B_PARTITION(bootdev), majdev);
318 	}
319 	dev = udev2dev(makeudev(majdev, 0), 0);
320 	if (!dev_is_good(dev))
321 		return;
322 	unit = B_UNIT(bootdev);
323 	slice = B_SLICE(bootdev);
324 	if (slice == WHOLE_DISK_SLICE)
325 		slice = COMPATIBILITY_SLICE;
326 	if (slice < 0 || slice >= MAX_SLICES) {
327 		kprintf("bad slice\n");
328 		return;
329 	}
330 
331 	part = B_PARTITION(bootdev);
332 	mindev = dkmakeminor(unit, slice, part);
333 	newrootdev = udev2dev(makeudev(majdev, mindev), 0);
334 	if (!dev_is_good(newrootdev))
335 		return;
336 	sname = dsname(newrootdev, unit, slice, part, partname);
337 	rootdevnames[0] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
338 	ksprintf(rootdevnames[0], "ufs:%s%s", sname, partname);
339 
340 	/*
341 	 * For properly dangerously dedicated disks (ones with a historical
342 	 * bogus partition table), the boot blocks will give slice = 4, but
343 	 * the kernel will only provide the compatibility slice since it
344 	 * knows that slice 4 is not a real slice.  Arrange to try mounting
345 	 * the compatibility slice as root if mounting the slice passed by
346 	 * the boot blocks fails.  This handles the dangerously dedicated
347 	 * case and perhaps others.
348 	 */
349 	if (slice == COMPATIBILITY_SLICE)
350 		return;
351 	slice = COMPATIBILITY_SLICE;
352 	sname = dsname(newrootdev, unit, slice, part, partname);
353 	rootdevnames[1] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
354 	ksprintf(rootdevnames[1], "ufs:%s%s", sname, partname);
355 }
356 #endif
357 
358 #if defined(NFS) && defined(NFS_ROOT)
359 #if !defined(BOOTP_NFSROOT)
360 
361 #include <sys/socket.h>
362 #include <net/if.h>
363 #include <net/if_dl.h>
364 #include <net/if_types.h>
365 #include <net/if_var.h>
366 #include <net/ethernet.h>
367 #include <netinet/in.h>
368 #include <vfs/nfs/rpcv2.h>
369 #include <vfs/nfs/nfsproto.h>
370 #include <vfs/nfs/nfs.h>
371 #include <vfs/nfs/nfsdiskless.h>
372 
373 extern struct nfs_diskless	nfs_diskless;
374 
375 /*
376  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
377  * exist the sockaddr will remain zerod out (callers typically just check
378  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
379  */
380 static int
381 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
382 {
383 	u_int32_t	a[4];
384 	char		*cp;
385 
386 	bzero(sa, sizeof(*sa));
387 
388 	if ((cp = kgetenv(ev)) == NULL)
389 		return(1);
390 	if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
391 		return(1);
392 	if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
393 		return(1);
394 	/* XXX is this ordering correct? */
395 	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
396 	sa->sin_len = sizeof(*sa);
397 	sa->sin_family = AF_INET;
398 	return(0);
399 }
400 
401 static int
402 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
403 {
404 	char		*cp;
405 	u_int32_t	a[6];
406 
407 	bzero(sa, sizeof(*sa));
408 	sa->sdl_len = sizeof(*sa);
409 	sa->sdl_family = AF_LINK;
410 	sa->sdl_type = IFT_ETHER;
411 	sa->sdl_alen = ETHER_ADDR_LEN;
412 	if ((cp = kgetenv(ev)) == NULL)
413 		return(1);
414 	if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
415 		return(1);
416 	sa->sdl_data[0] = a[0];
417 	sa->sdl_data[1] = a[1];
418 	sa->sdl_data[2] = a[2];
419 	sa->sdl_data[3] = a[3];
420 	sa->sdl_data[4] = a[4];
421 	sa->sdl_data[5] = a[5];
422 	return(0);
423 }
424 
425 static int
426 decode_nfshandle(char *ev, u_char *fh)
427 {
428 	u_char	*cp;
429 	int	len, val;
430 
431 	if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
432 		return(0);
433 	len = 0;
434 	cp++;
435 	for (;;) {
436 		if (*cp == 'X')
437 			return(len);
438 		if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
439 			return(0);
440 		*(fh++) = val;
441 		len++;
442 		cp += 2;
443 		if (len > NFSX_V2FH)
444 		    return(0);
445 	}
446 }
447 
448 /*
449  * Populate the essential fields in the nfsv3_diskless structure.
450  *
451  * The loader is expected to export the following environment variables:
452  *
453  * boot.netif.ip		IP address on boot interface
454  * boot.netif.netmask		netmask on boot interface
455  * boot.netif.gateway		default gateway (optional)
456  * boot.netif.hwaddr		hardware address of boot interface
457  * boot.netif.name		name of boot interface (instead of hw addr)
458  * boot.nfsroot.server		IP address of root filesystem server
459  * boot.nfsroot.path		path of the root filesystem on server
460  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
461  */
462 static void
463 pxe_setup_nfsdiskless(void)
464 {
465 	struct nfs_diskless	*nd = &nfs_diskless;
466 	struct ifnet		*ifp;
467 	struct ifaddr		*ifa;
468 	struct sockaddr_dl	*sdl, ourdl;
469 	struct sockaddr_in	myaddr, netmask;
470 	char			*cp;
471 
472 	/* set up interface */
473 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
474 		return;
475 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
476 		kprintf("PXE: no netmask\n");
477 		return;
478 	}
479 	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
480 	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
481 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
482 		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
483 	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
484 
485 	if ((cp = kgetenv("boot.netif.name")) != NULL) {
486 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
487 			if (strcmp(cp, ifp->if_xname) == 0)
488 				break;
489 		}
490 		if (ifp)
491 			goto match_done;
492 		kprintf("PXE: cannot find interface %s\n", cp);
493 		return;
494 	}
495 
496 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
497 		kprintf("PXE: no hardware address\n");
498 		return;
499 	}
500 	ifa = NULL;
501 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
502 		struct ifaddr_container *ifac;
503 
504 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
505 			ifa = ifac->ifa;
506 
507 			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
508 			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
509 				if ((sdl->sdl_type == ourdl.sdl_type) &&
510 				    (sdl->sdl_alen == ourdl.sdl_alen) &&
511 				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
512 					  ourdl.sdl_data + ourdl.sdl_nlen,
513 					  sdl->sdl_alen))
514 				    goto match_done;
515 			}
516 		}
517 	}
518 	kprintf("PXE: no interface\n");
519 	return;	/* no matching interface */
520 match_done:
521 	strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
522 
523 	/* set up gateway */
524 	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
525 
526 	/* XXX set up swap? */
527 
528 	/* set up root mount */
529 	nd->root_args.rsize = 8192;		/* XXX tunable? */
530 	nd->root_args.wsize = 8192;
531 	nd->root_args.sotype = SOCK_STREAM;
532 	nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT;
533 	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
534 		kprintf("PXE: no server\n");
535 		return;
536 	}
537 	nd->root_saddr.sin_port = htons(NFS_PORT);
538 
539 	/*
540 	 * A tftp-only loader may pass NFS path information without a
541 	 * root handle.  Generate a warning but continue configuring.
542 	 */
543 	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
544 		kprintf("PXE: Warning, no NFS handle passed from loader\n");
545 	}
546 	if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
547 		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
548 
549 	nfs_diskless_valid = 1;
550 }
551 
552 #endif
553 #endif
554