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