autoconf.c revision 83651
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
37 * $FreeBSD: head/sys/i386/i386/autoconf.c 83651 2001-09-18 23:32:09Z peter $
38 */
39
40/*
41 * Setup the system to run on the current machine.
42 *
43 * Configure() is called at boot time and initializes the vba
44 * device tables and the memory controller monitoring.  Available
45 * devices are determined (from possibilities mentioned in ioconf.c),
46 * and the drivers are initialized.
47 */
48#include "opt_bootp.h"
49#include "opt_isa.h"
50#include "opt_nfs.h"
51#include "opt_nfsroot.h"
52#include "opt_bus.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/bus.h>
57#include <sys/conf.h>
58#include <sys/disklabel.h>
59#include <sys/diskslice.h>
60#include <sys/reboot.h>
61#include <sys/kernel.h>
62#include <sys/malloc.h>
63#include <sys/mount.h>
64#include <sys/cons.h>
65
66#if defined(NFSCLIENT) && defined(NFS_ROOT)
67#include <sys/socket.h>
68#include <net/if.h>
69#include <net/if_dl.h>
70#include <net/if_types.h>
71#include <net/if_var.h>
72#include <net/ethernet.h>
73#include <netinet/in.h>
74#include <nfs/rpcv2.h>
75#include <nfs/nfsproto.h>
76#include <nfsclient/nfs.h>
77#include <nfsclient/nfsdiskless.h>
78#endif
79
80#include <machine/bootinfo.h>
81#include <machine/md_var.h>
82#ifdef APIC_IO
83#include <machine/smp.h>
84#else
85#include <i386/isa/icu.h>
86#endif /* APIC_IO */
87
88#ifdef DEV_ISA
89#include <isa/isavar.h>
90
91device_t isa_bus_device = 0;
92#endif
93
94static void	configure_first __P((void *));
95static void	configure __P((void *));
96static void	configure_final __P((void *));
97
98#if defined(NFSCLIENT) && defined(NFS_ROOT)
99static void	pxe_setup_nfsdiskless(void);
100#endif
101
102SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
103/* SI_ORDER_SECOND is hookable */
104SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
105/* SI_ORDER_MIDDLE is hookable */
106SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
107
108dev_t	rootdev = NODEV;
109dev_t	dumpdev = NODEV;
110
111device_t nexus_dev;
112
113/*
114 * Determine i/o configuration for a machine.
115 */
116static void
117configure_first(dummy)
118	void *dummy;
119{
120}
121
122static void
123configure(dummy)
124	void *dummy;
125{
126
127	/*
128	 * Activate the ICU's.  Note that we are explicitly at splhigh()
129	 * at present as we have no way to disable stray PCI level triggered
130	 * interrupts until the devices have had a driver attached.  This
131	 * is particularly a problem when the interrupts are shared.  For
132	 * example, if IRQ 10 is shared between a disk and network device
133	 * and the disk device generates an interrupt, if we "activate"
134	 * IRQ 10 when the network driver is set up, then we will get
135	 * recursive interrupt 10's as nothing will know how to turn off
136	 * the disk device's interrupt.
137	 *
138	 * Having the ICU's active means we can probe interrupt routing to
139	 * see if a device causes the corresponding pending bit to be set.
140	 *
141	 * This is all rather inconvenient.
142	 */
143#ifdef APIC_IO
144	bsp_apic_configure();
145	enable_intr();
146#else
147	enable_intr();
148	INTREN(IRQ_SLAVE);
149#endif /* APIC_IO */
150
151	/* nexus0 is the top of the i386 device tree */
152	device_add_child(root_bus, "nexus", 0);
153
154	/* initialize new bus architecture */
155	root_bus_configure();
156
157#ifdef DEV_ISA
158	/*
159	 * Explicitly probe and attach ISA last.  The isa bus saves
160	 * it's device node at attach time for us here.
161	 */
162	if (isa_bus_device)
163		isa_probe_children(isa_bus_device);
164#endif
165
166	/*
167	 * Now we're ready to handle (pending) interrupts.
168	 * XXX this is slightly misplaced.
169	 */
170	spl0();
171}
172
173static void
174configure_final(dummy)
175	void *dummy;
176{
177	int i;
178
179	cninit_finish();
180
181	if (bootverbose) {
182
183#ifdef APIC_IO
184		imen_dump();
185#endif /* APIC_IO */
186
187		/*
188		 * Print out the BIOS's idea of the disk geometries.
189		 */
190		printf("BIOS Geometries:\n");
191		for (i = 0; i < N_BIOS_GEOM; i++) {
192			unsigned long bios_geom;
193			int max_cylinder, max_head, max_sector;
194
195			bios_geom = bootinfo.bi_bios_geom[i];
196
197			/*
198			 * XXX the bootstrap punts a 1200K floppy geometry
199			 * when the get-disk-geometry interrupt fails.  Skip
200			 * drives that have this geometry.
201			 */
202			if (bios_geom == 0x4f010f)
203				continue;
204
205			printf(" %x:%08lx ", i, bios_geom);
206			max_cylinder = bios_geom >> 16;
207			max_head = (bios_geom >> 8) & 0xff;
208			max_sector = bios_geom & 0xff;
209			printf(
210		"0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
211			       max_cylinder, max_cylinder + 1,
212			       max_head, max_head + 1,
213			       max_sector, max_sector);
214		}
215		printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
216
217		printf("Device configuration finished.\n");
218	}
219	cold = 0;
220}
221
222#ifdef BOOTP
223extern void bootpc_init(void);
224#endif
225/*
226 * Do legacy root filesystem discovery.
227 */
228void
229cpu_rootconf()
230{
231#ifdef BOOTP
232        bootpc_init();
233#endif
234#if defined(NFSCLIENT) && defined(NFS_ROOT)
235#if !defined(BOOTP_NFSROOT)
236	pxe_setup_nfsdiskless();
237	if (nfs_diskless_valid)
238#endif
239		rootdevnames[0] = "nfs:";
240#endif
241}
242SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
243
244u_long	bootdev = 0;		/* not a dev_t - encoding is different */
245
246#if defined(NFSCLIENT) && defined(NFS_ROOT)
247
248static int
249inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
250{
251	u_int32_t	a[4];
252	char		*cp;
253
254	bzero(sa, sizeof(*sa));
255	sa->sin_len = sizeof(*sa);
256	sa->sin_family = AF_INET;
257
258	if ((cp = getenv(ev)) == NULL)
259		return(1);
260	if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
261		return(1);
262	/* XXX is this ordering correct? */
263	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
264	return(0);
265}
266
267static int
268hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
269{
270	char		*cp;
271	u_int32_t	a[6];
272
273	bzero(sa, sizeof(*sa));
274	sa->sdl_len = sizeof(*sa);
275	sa->sdl_family = AF_LINK;
276	sa->sdl_type = IFT_ETHER;
277	sa->sdl_alen = ETHER_ADDR_LEN;
278	if ((cp = getenv(ev)) == NULL)
279		return(1);
280	if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
281		return(1);
282	sa->sdl_data[0] = a[0];
283	sa->sdl_data[1] = a[1];
284	sa->sdl_data[2] = a[2];
285	sa->sdl_data[3] = a[3];
286	sa->sdl_data[4] = a[4];
287	sa->sdl_data[5] = a[5];
288	return(0);
289}
290
291static int
292decode_nfshandle(char *ev, u_char *fh)
293{
294	u_char	*cp;
295	int	len, val;
296
297	if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
298		return(0);
299	len = 0;
300	cp++;
301	for (;;) {
302		if (*cp == 'X')
303			return(len);
304		if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
305			return(0);
306		*(fh++) = val;
307		len++;
308		cp += 2;
309		if (len > NFSX_V2FH)
310		    return(0);
311	}
312}
313
314/*
315 * Populate the essential fields in the nfsv3_diskless structure.
316 *
317 * The loader is expected to export the following environment variables:
318 *
319 * boot.netif.ip		IP address on boot interface
320 * boot.netif.netmask		netmask on boot interface
321 * boot.netif.gateway		default gateway (optional)
322 * boot.netif.hwaddr		hardware address of boot interface
323 * boot.nfsroot.server		IP address of root filesystem server
324 * boot.nfsroot.path		path of the root filesystem on server
325 * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
326 */
327static void
328pxe_setup_nfsdiskless(void)
329{
330	struct nfs_diskless	*nd = &nfs_diskless;
331	struct ifnet		*ifp;
332	struct ifaddr		*ifa;
333	struct sockaddr_dl	*sdl, ourdl;
334	struct sockaddr_in	myaddr, netmask;
335	char			*cp;
336
337	/* set up interface */
338	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
339		return;
340	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
341		printf("PXE: no netmask\n");
342		return;
343	}
344	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
345	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
346	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
347		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
348	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
349
350	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
351		printf("PXE: no hardware address\n");
352		return;
353	}
354	ifa = NULL;
355	TAILQ_FOREACH(ifp, &ifnet, if_link) {
356		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
357			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
358			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
359				if ((sdl->sdl_type == ourdl.sdl_type) &&
360				    (sdl->sdl_alen == ourdl.sdl_alen) &&
361				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
362					  ourdl.sdl_data + ourdl.sdl_nlen,
363					  sdl->sdl_alen))
364				    goto match_done;
365			}
366		}
367	}
368	printf("PXE: no interface\n");
369	return;	/* no matching interface */
370match_done:
371	sprintf(nd->myif.ifra_name, "%s%d", ifp->if_name, ifp->if_unit);
372
373
374	/* set up gateway */
375	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
376
377	/* XXX set up swap? */
378
379	/* set up root mount */
380	nd->root_args.rsize = 8192;		/* XXX tunable? */
381	nd->root_args.wsize = 8192;
382	nd->root_args.sotype = SOCK_DGRAM;
383	nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
384	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
385		printf("PXE: no server\n");
386		return;
387	}
388	nd->root_saddr.sin_port = htons(NFS_PORT);
389	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
390		printf("PXE: no NFS handle\n");
391		return;
392	}
393	if ((cp = getenv("boot.nfsroot.path")) != NULL)
394		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
395
396	nfs_diskless_valid = 1;
397}
398#endif
399