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, nbuf * sizeof(struct buf));
172 	swbuf_mem = (void *)kmem_alloc(&kernel_map, nswbuf_mem * sizeof(struct buf));
173 	swbuf_kva = (void *)kmem_alloc(&kernel_map, nswbuf_kva * sizeof(struct buf));
174 
175 #ifdef DIRECTIO
176         ffs_rawread_setup();
177 #endif
178 	kmem_suballoc(&kernel_map, &clean_map, &clean_sva, &clean_eva,
179 		      (nbuf * MAXBSIZE * 2) +
180 		      (nswbuf_mem + nswbuf_kva) *MAXPHYS +
181 		      pager_map_size);
182 	kmem_suballoc(&clean_map, &buffer_map, &buffer_sva, &buffer_eva,
183 		      (nbuf * MAXBSIZE * 2));
184 	buffer_map.system_map = 1;
185 	kmem_suballoc(&clean_map, &pager_map, &pager_sva, &pager_eva,
186 		      (nswbuf_mem + nswbuf_kva) *MAXPHYS +
187 		      pager_map_size);
188 	pager_map.system_map = 1;
189 	kprintf("avail memory = %lu (%luK bytes)\n", ptoa(vmstats.v_free_count),
190 		ptoa(vmstats.v_free_count) / 1024);
191 	mp_start();
192 	mp_announce();
193 	cpu_setregs();
194 }
195 
196 /*
197  * Determine i/o configuration for a machine.
198  */
199 static void
200 configure_first(void *dummy)
201 {
202 }
203 
204 static void
205 configure(void *dummy)
206 {
207         /*
208 	 * Final interrupt support acviation, then enable hardware interrupts.
209 	 */
210 	MachIntrABI.finalize();
211 	cpu_enable_intr();
212 
213 	/*
214 	 * This will configure all devices, generally starting with the
215 	 * nexus (i386/i386/nexus.c).  The nexus ISA code explicitly
216 	 * dummies up the attach in order to delay legacy initialization
217 	 * until after all other busses/subsystems have had a chance
218 	 * at those resources.
219 	 */
220 	root_bus_configure();
221 
222 #if NISA > 0
223 	/*
224 	 * Explicitly probe and attach ISA last.  The isa bus saves
225 	 * it's device node at attach time for us here.
226 	 */
227 	if (isa_bus_device)
228 		isa_probe_children(isa_bus_device);
229 #endif
230 
231 	/*
232 	 * Allow lowering of the ipl to the lowest kernel level if we
233 	 * panic (or call tsleep() before clearing `cold').  No level is
234 	 * completely safe (since a panic may occur in a critical region
235 	 * at splhigh()), but we want at least bio interrupts to work.
236 	 */
237 	safepri = TDPRI_KERN_USER;
238 }
239 
240 static void
241 configure_final(void *dummy)
242 {
243 	cninit_finish();
244 
245 	if (bootverbose)
246 		kprintf("Device configuration finished.\n");
247 }
248 
249 #ifdef BOOTP
250 void bootpc_init(void);
251 #endif
252 /*
253  * Do legacy root filesystem discovery.
254  */
255 void
256 cpu_rootconf(void)
257 {
258 #ifdef BOOTP
259         bootpc_init();
260 #endif
261 #if defined(NFS) && defined(NFS_ROOT)
262 #if !defined(BOOTP_NFSROOT)
263 	pxe_setup_nfsdiskless();
264 	if (nfs_diskless_valid)
265 #endif
266 		rootdevnames[0] = "nfs:";
267 #endif
268 #if defined(FFS) && defined(FFS_ROOT)
269         if (!rootdevnames[0])
270                 setroot();
271 #endif
272 }
273 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL);
274 
275 u_long	bootdev = 0;		/* not a cdev_t - encoding is different */
276 
277 #if defined(FFS) && defined(FFS_ROOT)
278 
279 /*
280  * The boot code uses old block device major numbers to pass bootdev to
281  * us.  We have to translate these to character device majors because
282  * we don't have block devices any more.
283  */
284 static int
285 boot_translate_majdev(int bmajor)
286 {
287 	static int conv[] = { BOOTMAJOR_CONVARY };
288 
289 	if (bmajor >= 0 && bmajor < NELEM(conv))
290 		return(conv[bmajor]);
291 	return(-1);
292 }
293 
294 /*
295  * Attempt to find the device from which we were booted.
296  * If we can do so, and not instructed not to do so,
297  * set rootdevs[] and rootdevnames[] to correspond to the
298  * boot device(s).
299  *
300  * This code survives in order to allow the system to be
301  * booted from legacy environments that do not correctly
302  * populate the kernel environment. There are significant
303  * restrictions on the bootability of the system in this
304  * situation; it can only be mounting root from a 'da'
305  * 'wd' or 'fd' device, and the root filesystem must be ufs.
306  */
307 static void
308 setroot(void)
309 {
310 	int majdev, mindev, unit, slice, part;
311 	cdev_t newrootdev, dev;
312 	char partname[2];
313 	char *sname;
314 
315 	if ((bootdev & B_MAGICMASK) != B_DEVMAGIC) {
316 		kprintf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev);
317 		return;
318 	}
319 	majdev = boot_translate_majdev(B_TYPE(bootdev));
320 	if (bootverbose) {
321 		kprintf("bootdev: %08lx type=%ld unit=%ld "
322 			"slice=%ld part=%ld major=%d\n",
323 			bootdev, B_TYPE(bootdev), B_UNIT(bootdev),
324 			B_SLICE(bootdev), B_PARTITION(bootdev), majdev);
325 	}
326 	dev = udev2dev(makeudev(majdev, 0), 0);
327 	if (!dev_is_good(dev))
328 		return;
329 	unit = B_UNIT(bootdev);
330 	slice = B_SLICE(bootdev);
331 	if (slice == WHOLE_DISK_SLICE)
332 		slice = COMPATIBILITY_SLICE;
333 	if (slice < 0 || slice >= MAX_SLICES) {
334 		kprintf("bad slice\n");
335 		return;
336 	}
337 
338 	part = B_PARTITION(bootdev);
339 	mindev = dkmakeminor(unit, slice, part);
340 	newrootdev = udev2dev(makeudev(majdev, mindev), 0);
341 	if (!dev_is_good(newrootdev))
342 		return;
343 	sname = dsname(newrootdev, unit, slice, part, partname);
344 	rootdevnames[0] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
345 	ksprintf(rootdevnames[0], "ufs:%s%s", sname, partname);
346 
347 	/*
348 	 * For properly dangerously dedicated disks (ones with a historical
349 	 * bogus partition table), the boot blocks will give slice = 4, but
350 	 * the kernel will only provide the compatibility slice since it
351 	 * knows that slice 4 is not a real slice.  Arrange to try mounting
352 	 * the compatibility slice as root if mounting the slice passed by
353 	 * the boot blocks fails.  This handles the dangerously dedicated
354 	 * case and perhaps others.
355 	 */
356 	if (slice == COMPATIBILITY_SLICE)
357 		return;
358 	slice = COMPATIBILITY_SLICE;
359 	sname = dsname(newrootdev, unit, slice, part, partname);
360 	rootdevnames[1] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
361 	ksprintf(rootdevnames[1], "ufs:%s%s", sname, partname);
362 }
363 #endif
364 
365 #if defined(NFS) && defined(NFS_ROOT)
366 #if !defined(BOOTP_NFSROOT)
367 
368 #include <sys/socket.h>
369 #include <net/if.h>
370 #include <net/if_dl.h>
371 #include <net/if_types.h>
372 #include <net/if_var.h>
373 #include <net/ethernet.h>
374 #include <netinet/in.h>
375 #include <vfs/nfs/rpcv2.h>
376 #include <vfs/nfs/nfsproto.h>
377 #include <vfs/nfs/nfs.h>
378 #include <vfs/nfs/nfsdiskless.h>
379 
380 extern struct nfs_diskless	nfs_diskless;
381 
382 /*
383  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
384  * exist the sockaddr will remain zerod out (callers typically just check
385  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
386  */
387 static int
388 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
389 {
390 	u_int32_t	a[4];
391 	char		*cp;
392 
393 	bzero(sa, sizeof(*sa));
394 
395 	if ((cp = kgetenv(ev)) == NULL)
396 		return(1);
397 	if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
398 		return(1);
399 	if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
400 		return(1);
401 	/* XXX is this ordering correct? */
402 	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
403 	sa->sin_len = sizeof(*sa);
404 	sa->sin_family = AF_INET;
405 	return(0);
406 }
407 
408 static int
409 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
410 {
411 	char		*cp;
412 	u_int32_t	a[6];
413 
414 	bzero(sa, sizeof(*sa));
415 	sa->sdl_len = sizeof(*sa);
416 	sa->sdl_family = AF_LINK;
417 	sa->sdl_type = IFT_ETHER;
418 	sa->sdl_alen = ETHER_ADDR_LEN;
419 	if ((cp = kgetenv(ev)) == NULL)
420 		return(1);
421 	if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
422 		return(1);
423 	sa->sdl_data[0] = a[0];
424 	sa->sdl_data[1] = a[1];
425 	sa->sdl_data[2] = a[2];
426 	sa->sdl_data[3] = a[3];
427 	sa->sdl_data[4] = a[4];
428 	sa->sdl_data[5] = a[5];
429 	return(0);
430 }
431 
432 static int
433 decode_nfshandle(char *ev, u_char *fh)
434 {
435 	u_char	*cp;
436 	int	len, val;
437 
438 	if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
439 		return(0);
440 	len = 0;
441 	cp++;
442 	for (;;) {
443 		if (*cp == 'X')
444 			return(len);
445 		if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
446 			return(0);
447 		*(fh++) = val;
448 		len++;
449 		cp += 2;
450 		if (len > NFSX_V2FH)
451 		    return(0);
452 	}
453 }
454 
455 /*
456  * Populate the essential fields in the nfsv3_diskless structure.
457  *
458  * The loader is expected to export the following environment variables:
459  *
460  * boot.netif.ip		IP address on boot interface
461  * boot.netif.netmask		netmask on boot interface
462  * boot.netif.gateway		default gateway (optional)
463  * boot.netif.hwaddr		hardware address of boot interface
464  * boot.netif.name		name of boot interface (instead of hw addr)
465  * boot.nfsroot.server		IP address of root filesystem server
466  * boot.nfsroot.path		path of the root filesystem on server
467  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
468  */
469 static void
470 pxe_setup_nfsdiskless(void)
471 {
472 	struct nfs_diskless	*nd = &nfs_diskless;
473 	struct ifnet		*ifp;
474 	struct ifaddr		*ifa;
475 	struct sockaddr_dl	*sdl, ourdl;
476 	struct sockaddr_in	myaddr, netmask;
477 	char			*cp;
478 
479 	/* set up interface */
480 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
481 		return;
482 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
483 		kprintf("PXE: no netmask\n");
484 		return;
485 	}
486 	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
487 	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
488 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
489 		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
490 	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
491 
492 	if ((cp = kgetenv("boot.netif.name")) != NULL) {
493 		ifnet_lock();
494 		ifp = ifunit(cp);
495 		if (ifp) {
496 			strlcpy(nd->myif.ifra_name, ifp->if_xname,
497 			    sizeof(nd->myif.ifra_name));
498 			ifnet_unlock();
499 			goto match_done;
500 		}
501 		ifnet_unlock();
502 		kprintf("PXE: cannot find interface %s\n", cp);
503 		return;
504 	}
505 
506 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
507 		kprintf("PXE: no hardware address\n");
508 		return;
509 	}
510 	ifa = NULL;
511 	ifnet_lock();
512 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
513 		struct ifaddr_container *ifac;
514 
515 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
516 			ifa = ifac->ifa;
517 
518 			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
519 			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
520 				if ((sdl->sdl_type == ourdl.sdl_type) &&
521 				    (sdl->sdl_alen == ourdl.sdl_alen) &&
522 				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
523 					  ourdl.sdl_data + ourdl.sdl_nlen,
524 					  sdl->sdl_alen)) {
525 					strlcpy(nd->myif.ifra_name,
526 					    ifp->if_xname,
527 					    sizeof(nd->myif.ifra_name));
528 					ifnet_unlock();
529 					goto match_done;
530 				}
531 			}
532 		}
533 	}
534 	ifnet_unlock();
535 	kprintf("PXE: no interface\n");
536 	return;	/* no matching interface */
537 match_done:
538 	/* set up gateway */
539 	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
540 
541 	/* XXX set up swap? */
542 
543 	/* set up root mount */
544 	nd->root_args.rsize = nfsroot_iosize;
545 	nd->root_args.wsize = nfsroot_iosize;
546 	nd->root_args.sotype = SOCK_STREAM;
547 	nd->root_args.readahead = nfsroot_rahead;
548 	nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT |
549 			      NFSMNT_READAHEAD;
550 	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
551 		kprintf("PXE: no server\n");
552 		return;
553 	}
554 	nd->root_saddr.sin_port = htons(NFS_PORT);
555 
556 	/*
557 	 * A tftp-only loader may pass NFS path information without a
558 	 * root handle.  Generate a warning but continue configuring.
559 	 */
560 	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
561 		kprintf("PXE: Warning, no NFS handle passed from loader\n");
562 	}
563 	if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
564 		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
565 
566 	nfs_diskless_valid = 1;
567 }
568 
569 #endif
570 #endif
571