autoconf.c revision 87620
14Srgrimes/*-
24Srgrimes * Copyright (c) 1990 The Regents of the University of California.
34Srgrimes * All rights reserved.
44Srgrimes *
54Srgrimes * This code is derived from software contributed to Berkeley by
64Srgrimes * William Jolitz.
74Srgrimes *
84Srgrimes * Redistribution and use in source and binary forms, with or without
94Srgrimes * modification, are permitted provided that the following conditions
104Srgrimes * are met:
114Srgrimes * 1. Redistributions of source code must retain the above copyright
124Srgrimes *    notice, this list of conditions and the following disclaimer.
134Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
144Srgrimes *    notice, this list of conditions and the following disclaimer in the
154Srgrimes *    documentation and/or other materials provided with the distribution.
164Srgrimes * 3. All advertising materials mentioning features or use of this software
174Srgrimes *    must display the following acknowledgement:
184Srgrimes *	This product includes software developed by the University of
194Srgrimes *	California, Berkeley and its contributors.
204Srgrimes * 4. Neither the name of the University nor the names of its contributors
214Srgrimes *    may be used to endorse or promote products derived from this software
224Srgrimes *    without specific prior written permission.
234Srgrimes *
244Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344Srgrimes * SUCH DAMAGE.
354Srgrimes *
36620Srgrimes *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
3750477Speter * $FreeBSD: head/sys/i386/i386/autoconf.c 87620 2001-12-10 20:02:22Z guido $
384Srgrimes */
394Srgrimes
404Srgrimes/*
414Srgrimes * Setup the system to run on the current machine.
424Srgrimes *
438876Srgrimes * Configure() is called at boot time and initializes the vba
444Srgrimes * device tables and the memory controller monitoring.  Available
454Srgrimes * devices are determined (from possibilities mentioned in ioconf.c),
464Srgrimes * and the drivers are initialized.
474Srgrimes */
4832358Seivind#include "opt_bootp.h"
4971785Speter#include "opt_isa.h"
5053888Sdillon#include "opt_nfs.h"
5137272Sjmg#include "opt_nfsroot.h"
5245720Speter#include "opt_bus.h"
5325164Speter
542056Swollman#include <sys/param.h>
552056Swollman#include <sys/systm.h>
5645720Speter#include <sys/bus.h>
572056Swollman#include <sys/conf.h>
5831328Speter#include <sys/disklabel.h>
5936809Sbde#include <sys/diskslice.h>
602056Swollman#include <sys/reboot.h>
612056Swollman#include <sys/kernel.h>
6236809Sbde#include <sys/malloc.h>
6312604Sbde#include <sys/mount.h>
6449558Sphk#include <sys/cons.h>
654Srgrimes
6683651Speter#if defined(NFSCLIENT) && defined(NFS_ROOT)
6783651Speter#include <sys/socket.h>
6883651Speter#include <net/if.h>
6983651Speter#include <net/if_dl.h>
7083651Speter#include <net/if_types.h>
7183651Speter#include <net/if_var.h>
7283651Speter#include <net/ethernet.h>
7383651Speter#include <netinet/in.h>
7483651Speter#include <nfs/rpcv2.h>
7583651Speter#include <nfs/nfsproto.h>
7683651Speter#include <nfsclient/nfs.h>
7783651Speter#include <nfsclient/nfsdiskless.h>
7883651Speter#endif
7983651Speter
8020641Sbde#include <machine/bootinfo.h>
817090Sbde#include <machine/md_var.h>
8227288Sfsmp#ifdef APIC_IO
8325164Speter#include <machine/smp.h>
8450197Speter#else
8550197Speter#include <i386/isa/icu.h>
8625164Speter#endif /* APIC_IO */
8727288Sfsmp
8871785Speter#ifdef DEV_ISA
8950769Sdfr#include <isa/isavar.h>
9055117Sbde
9146915Speterdevice_t isa_bus_device = 0;
9255117Sbde#endif
9346915Speter
9442817Speterstatic void	configure_first __P((void *));
9512604Sbdestatic void	configure __P((void *));
9642817Speterstatic void	configure_final __P((void *));
9712604Sbde
9883651Speter#if defined(NFSCLIENT) && defined(NFS_ROOT)
9965500Smsmithstatic void	pxe_setup_nfsdiskless(void);
10065500Smsmith#endif
10165500Smsmith
10242817SpeterSYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
10342817Speter/* SI_ORDER_SECOND is hookable */
10442817SpeterSYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
10542817Speter/* SI_ORDER_MIDDLE is hookable */
10642817SpeterSYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
10742817Speter
10846806Sphkdev_t	rootdev = NODEV;
10946806Sphkdev_t	dumpdev = NODEV;
11042817Speter
11145720Speterdevice_t nexus_dev;
11245720Speter
1134Srgrimes/*
1144Srgrimes * Determine i/o configuration for a machine.
1154Srgrimes */
11610665Sbdestatic void
11742817Speterconfigure_first(dummy)
11842817Speter	void *dummy;
11942817Speter{
12042817Speter}
12142817Speter
12242817Speterstatic void
12310665Sbdeconfigure(dummy)
12410653Sdg	void *dummy;
1254Srgrimes{
1264Srgrimes
12729675Sgibbs	/*
12850184Speter	 * Activate the ICU's.  Note that we are explicitly at splhigh()
12950184Speter	 * at present as we have no way to disable stray PCI level triggered
13050184Speter	 * interrupts until the devices have had a driver attached.  This
13150184Speter	 * is particularly a problem when the interrupts are shared.  For
13250184Speter	 * example, if IRQ 10 is shared between a disk and network device
13350184Speter	 * and the disk device generates an interrupt, if we "activate"
13450184Speter	 * IRQ 10 when the network driver is set up, then we will get
13550184Speter	 * recursive interrupt 10's as nothing will know how to turn off
13650184Speter	 * the disk device's interrupt.
13750184Speter	 *
13850184Speter	 * Having the ICU's active means we can probe interrupt routing to
13950184Speter	 * see if a device causes the corresponding pending bit to be set.
14050184Speter	 *
14150184Speter	 * This is all rather inconvenient.
14229675Sgibbs	 */
14327288Sfsmp#ifdef APIC_IO
14427288Sfsmp	bsp_apic_configure();
14525172Speter	enable_intr();
14627288Sfsmp#else
14727288Sfsmp	enable_intr();
14825172Speter	INTREN(IRQ_SLAVE);
14927288Sfsmp#endif /* APIC_IO */
15012791Sgibbs
15145720Speter	/* nexus0 is the top of the i386 device tree */
15254073Smdodd	device_add_child(root_bus, "nexus", 0);
15312791Sgibbs
15438779Snsouch	/* initialize new bus architecture */
15538779Snsouch	root_bus_configure();
15638779Snsouch
15771785Speter#ifdef DEV_ISA
15850184Speter	/*
15950184Speter	 * Explicitly probe and attach ISA last.  The isa bus saves
16050184Speter	 * it's device node at attach time for us here.
16150184Speter	 */
16245720Speter	if (isa_bus_device)
16350769Sdfr		isa_probe_children(isa_bus_device);
16450769Sdfr#endif
16545720Speter
16631336Sbde	/*
16731336Sbde	 * Now we're ready to handle (pending) interrupts.
16831336Sbde	 * XXX this is slightly misplaced.
16931336Sbde	 */
17031336Sbde	spl0();
17142817Speter}
17231337Sbde
17342817Speterstatic void
17442817Speterconfigure_final(dummy)
17542817Speter	void *dummy;
17642817Speter{
17742817Speter	int i;
17822564Sbde
17987620Sguido	cninit_finish();
18010665Sbde
18120641Sbde	if (bootverbose) {
18227561Sfsmp
18327561Sfsmp#ifdef APIC_IO
18427615Sfsmp		imen_dump();
18527561Sfsmp#endif /* APIC_IO */
18627561Sfsmp
18720641Sbde		/*
18820641Sbde		 * Print out the BIOS's idea of the disk geometries.
18920641Sbde		 */
19020641Sbde		printf("BIOS Geometries:\n");
19120641Sbde		for (i = 0; i < N_BIOS_GEOM; i++) {
19220641Sbde			unsigned long bios_geom;
19320641Sbde			int max_cylinder, max_head, max_sector;
19420641Sbde
19520641Sbde			bios_geom = bootinfo.bi_bios_geom[i];
19620641Sbde
19720641Sbde			/*
19820641Sbde			 * XXX the bootstrap punts a 1200K floppy geometry
19920641Sbde			 * when the get-disk-geometry interrupt fails.  Skip
20020641Sbde			 * drives that have this geometry.
20120641Sbde			 */
20220641Sbde			if (bios_geom == 0x4f010f)
20320641Sbde				continue;
20420641Sbde
20520641Sbde			printf(" %x:%08lx ", i, bios_geom);
20620641Sbde			max_cylinder = bios_geom >> 16;
20720641Sbde			max_head = (bios_geom >> 8) & 0xff;
20820641Sbde			max_sector = bios_geom & 0xff;
20920641Sbde			printf(
21020641Sbde		"0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
21120641Sbde			       max_cylinder, max_cylinder + 1,
21220641Sbde			       max_head, max_head + 1,
21320641Sbde			       max_sector, max_sector);
21420641Sbde		}
21520641Sbde		printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
21620641Sbde
21716075Sjoerg		printf("Device configuration finished.\n");
21820641Sbde	}
21929675Sgibbs	cold = 0;
22029675Sgibbs}
22116075Sjoerg
22253888Sdillon#ifdef BOOTP
22353888Sdillonextern void bootpc_init(void);
22453888Sdillon#endif
22552778Smsmith/*
22652778Smsmith * Do legacy root filesystem discovery.
22752778Smsmith */
22829675Sgibbsvoid
22929675Sgibbscpu_rootconf()
23029675Sgibbs{
23153888Sdillon#ifdef BOOTP
23253888Sdillon        bootpc_init();
23353888Sdillon#endif
23483651Speter#if defined(NFSCLIENT) && defined(NFS_ROOT)
23552778Smsmith#if !defined(BOOTP_NFSROOT)
23665500Smsmith	pxe_setup_nfsdiskless();
23752778Smsmith	if (nfs_diskless_valid)
2387731Sphk#endif
23952778Smsmith		rootdevnames[0] = "nfs:";
2404Srgrimes#endif
24129675Sgibbs}
24252778SmsmithSYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
24317355Sbde
2448833Sdgu_long	bootdev = 0;		/* not a dev_t - encoding is different */
2454Srgrimes
24683651Speter#if defined(NFSCLIENT) && defined(NFS_ROOT)
24765500Smsmith
24865500Smsmithstatic int
24965500Smsmithinaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
25065500Smsmith{
25165500Smsmith	u_int32_t	a[4];
25265500Smsmith	char		*cp;
25365500Smsmith
25465500Smsmith	bzero(sa, sizeof(*sa));
25565500Smsmith	sa->sin_len = sizeof(*sa);
25665500Smsmith	sa->sin_family = AF_INET;
25765500Smsmith
25865500Smsmith	if ((cp = getenv(ev)) == NULL)
25965500Smsmith		return(1);
26065500Smsmith	if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
26165500Smsmith		return(1);
26265500Smsmith	/* XXX is this ordering correct? */
26365500Smsmith	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
26465500Smsmith	return(0);
26565500Smsmith}
26665500Smsmith
26765500Smsmithstatic int
26865500Smsmithhwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
26965500Smsmith{
27065500Smsmith	char		*cp;
27165500Smsmith	u_int32_t	a[6];
27265500Smsmith
27365500Smsmith	bzero(sa, sizeof(*sa));
27465500Smsmith	sa->sdl_len = sizeof(*sa);
27565500Smsmith	sa->sdl_family = AF_LINK;
27665500Smsmith	sa->sdl_type = IFT_ETHER;
27765500Smsmith	sa->sdl_alen = ETHER_ADDR_LEN;
27865500Smsmith	if ((cp = getenv(ev)) == NULL)
27965500Smsmith		return(1);
28065500Smsmith	if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
28165500Smsmith		return(1);
28265500Smsmith	sa->sdl_data[0] = a[0];
28365500Smsmith	sa->sdl_data[1] = a[1];
28465500Smsmith	sa->sdl_data[2] = a[2];
28565500Smsmith	sa->sdl_data[3] = a[3];
28665500Smsmith	sa->sdl_data[4] = a[4];
28765500Smsmith	sa->sdl_data[5] = a[5];
28865500Smsmith	return(0);
28965500Smsmith}
29065500Smsmith
29165500Smsmithstatic int
29265500Smsmithdecode_nfshandle(char *ev, u_char *fh)
29365500Smsmith{
29465500Smsmith	u_char	*cp;
29565500Smsmith	int	len, val;
29665500Smsmith
29765500Smsmith	if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
29865500Smsmith		return(0);
29965500Smsmith	len = 0;
30065500Smsmith	cp++;
30165500Smsmith	for (;;) {
30265500Smsmith		if (*cp == 'X')
30365500Smsmith			return(len);
30465500Smsmith		if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
30565500Smsmith			return(0);
30665500Smsmith		*(fh++) = val;
30765500Smsmith		len++;
30865500Smsmith		cp += 2;
30965500Smsmith		if (len > NFSX_V2FH)
31065500Smsmith		    return(0);
31165500Smsmith	}
31265500Smsmith}
31365500Smsmith
31465500Smsmith/*
31565500Smsmith * Populate the essential fields in the nfsv3_diskless structure.
31665500Smsmith *
31765500Smsmith * The loader is expected to export the following environment variables:
31865500Smsmith *
31965500Smsmith * boot.netif.ip		IP address on boot interface
32065500Smsmith * boot.netif.netmask		netmask on boot interface
32165500Smsmith * boot.netif.gateway		default gateway (optional)
32265500Smsmith * boot.netif.hwaddr		hardware address of boot interface
32365500Smsmith * boot.nfsroot.server		IP address of root filesystem server
32465500Smsmith * boot.nfsroot.path		path of the root filesystem on server
32565500Smsmith * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
32665500Smsmith */
32765500Smsmithstatic void
32883651Speterpxe_setup_nfsdiskless(void)
32965500Smsmith{
33065500Smsmith	struct nfs_diskless	*nd = &nfs_diskless;
33165500Smsmith	struct ifnet		*ifp;
33265500Smsmith	struct ifaddr		*ifa;
33365500Smsmith	struct sockaddr_dl	*sdl, ourdl;
33465500Smsmith	struct sockaddr_in	myaddr, netmask;
33565500Smsmith	char			*cp;
33665500Smsmith
33765500Smsmith	/* set up interface */
33865500Smsmith	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
33965500Smsmith		return;
34065500Smsmith	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
34165500Smsmith		printf("PXE: no netmask\n");
34265500Smsmith		return;
34365500Smsmith	}
34465500Smsmith	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
34565500Smsmith	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
34665500Smsmith	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
34765500Smsmith		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
34865500Smsmith	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
34965500Smsmith
35065500Smsmith	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
35165500Smsmith		printf("PXE: no hardware address\n");
35265500Smsmith		return;
35365500Smsmith	}
35465500Smsmith	ifa = NULL;
35565500Smsmith	TAILQ_FOREACH(ifp, &ifnet, if_link) {
35665500Smsmith		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
35765500Smsmith			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
35865500Smsmith			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
35965500Smsmith				if ((sdl->sdl_type == ourdl.sdl_type) &&
36065500Smsmith				    (sdl->sdl_alen == ourdl.sdl_alen) &&
36165500Smsmith				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
36265500Smsmith					  ourdl.sdl_data + ourdl.sdl_nlen,
36365500Smsmith					  sdl->sdl_alen))
36465500Smsmith				    goto match_done;
36565500Smsmith			}
36665500Smsmith		}
36765500Smsmith	}
36865500Smsmith	printf("PXE: no interface\n");
36965500Smsmith	return;	/* no matching interface */
37065500Smsmithmatch_done:
37165500Smsmith	sprintf(nd->myif.ifra_name, "%s%d", ifp->if_name, ifp->if_unit);
37265500Smsmith
37365500Smsmith
37465500Smsmith	/* set up gateway */
37565500Smsmith	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
37665500Smsmith
37765500Smsmith	/* XXX set up swap? */
37865500Smsmith
37965500Smsmith	/* set up root mount */
38065500Smsmith	nd->root_args.rsize = 8192;		/* XXX tunable? */
38165500Smsmith	nd->root_args.wsize = 8192;
38265500Smsmith	nd->root_args.sotype = SOCK_DGRAM;
38365500Smsmith	nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
38465500Smsmith	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
38565500Smsmith		printf("PXE: no server\n");
38665500Smsmith		return;
38765500Smsmith	}
38865500Smsmith	nd->root_saddr.sin_port = htons(NFS_PORT);
38965500Smsmith	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
39065500Smsmith		printf("PXE: no NFS handle\n");
39165500Smsmith		return;
39265500Smsmith	}
39365500Smsmith	if ((cp = getenv("boot.nfsroot.path")) != NULL)
39465500Smsmith		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
39565500Smsmith
39665500Smsmith	nfs_diskless_valid = 1;
39765500Smsmith}
39865500Smsmith#endif
399