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