autoconf.c revision 71785
1251881Speter/*-
2251881Speter * Copyright (c) 1990 The Regents of the University of California.
3251881Speter * All rights reserved.
4251881Speter *
5251881Speter * This code is derived from software contributed to Berkeley by
6251881Speter * William Jolitz.
7251881Speter *
8251881Speter * Redistribution and use in source and binary forms, with or without
9251881Speter * modification, are permitted provided that the following conditions
10251881Speter * are met:
11251881Speter * 1. Redistributions of source code must retain the above copyright
12251881Speter *    notice, this list of conditions and the following disclaimer.
13251881Speter * 2. Redistributions in binary form must reproduce the above copyright
14251881Speter *    notice, this list of conditions and the following disclaimer in the
15251881Speter *    documentation and/or other materials provided with the distribution.
16251881Speter * 3. All advertising materials mentioning features or use of this software
17251881Speter *    must display the following acknowledgement:
18251881Speter *	This product includes software developed by the University of
19251881Speter *	California, Berkeley and its contributors.
20251881Speter * 4. Neither the name of the University nor the names of its contributors
21251881Speter *    may be used to endorse or promote products derived from this software
22251881Speter *    without specific prior written permission.
23251881Speter *
24251881Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25251881Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26251881Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27251881Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28251881Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29251881Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30251881Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31251881Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32251881Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33251881Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34251881Speter * SUCH DAMAGE.
35251881Speter *
36251881Speter *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
37251881Speter * $FreeBSD: head/sys/i386/i386/autoconf.c 71785 2001-01-29 09:38:39Z peter $
38251881Speter */
39251881Speter
40251881Speter/*
41251881Speter * Setup the system to run on the current machine.
42251881Speter *
43251881Speter * Configure() is called at boot time and initializes the vba
44251881Speter * device tables and the memory controller monitoring.  Available
45251881Speter * devices are determined (from possibilities mentioned in ioconf.c),
46251881Speter * and the drivers are initialized.
47251881Speter */
48251881Speter#include "opt_bootp.h"
49251881Speter#include "opt_ffs.h"
50251881Speter#include "opt_cd9660.h"
51251881Speter#include "opt_isa.h"
52251881Speter#include "opt_nfs.h"
53251881Speter#include "opt_nfsroot.h"
54362181Sdim#include "opt_bus.h"
55251881Speter#include "opt_rootdevname.h"
56251881Speter
57251881Speter#include <sys/param.h>
58251881Speter#include <sys/systm.h>
59251881Speter#include <sys/bus.h>
60251881Speter#include <sys/conf.h>
61251881Speter#include <sys/disklabel.h>
62251881Speter#include <sys/diskslice.h>
63251881Speter#include <sys/ipl.h>
64251881Speter#include <sys/reboot.h>
65362181Sdim#include <sys/kernel.h>
66362181Sdim#include <sys/malloc.h>
67251881Speter#include <sys/mount.h>
68251881Speter#include <sys/cons.h>
69362181Sdim
70251881Speter#include <machine/bootinfo.h>
71362181Sdim#include <machine/md_var.h>
72251881Speter#ifdef APIC_IO
73251881Speter#include <machine/smp.h>
74251881Speter#else
75251881Speter#include <i386/isa/icu.h>
76251881Speter#endif /* APIC_IO */
77251881Speter
78251881Speter#ifdef DEV_ISA
79251881Speter#include <isa/isavar.h>
80251881Speter
81251881Speterdevice_t isa_bus_device = 0;
82251881Speter#endif
83251881Speter
84251881Speterstatic void	configure_first __P((void *));
85251881Speterstatic void	configure __P((void *));
86251881Speterstatic void	configure_final __P((void *));
87251881Speter
88251881Speter#if defined(NFS) && defined(NFS_ROOT)
89251881Speterstatic void	pxe_setup_nfsdiskless(void);
90362181Sdim#endif
91251881Speter
92251881SpeterSYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
93251881Speter/* SI_ORDER_SECOND is hookable */
94251881SpeterSYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
95251881Speter/* SI_ORDER_MIDDLE is hookable */
96251881SpeterSYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
97251881Speter
98251881Speterdev_t	rootdev = NODEV;
99251881Speterdev_t	dumpdev = NODEV;
100251881Speter
101251881Speterdevice_t nexus_dev;
102251881Speter
103251881Speter/*
104251881Speter * Determine i/o configuration for a machine.
105251881Speter */
106251881Speterstatic void
107251881Speterconfigure_first(dummy)
108251881Speter	void *dummy;
109251881Speter{
110251881Speter}
111251881Speter
112251881Speterstatic void
113251881Speterconfigure(dummy)
114251881Speter	void *dummy;
115251881Speter{
116251881Speter
117251881Speter	/*
118251881Speter	 * Activate the ICU's.  Note that we are explicitly at splhigh()
119251881Speter	 * at present as we have no way to disable stray PCI level triggered
120251881Speter	 * interrupts until the devices have had a driver attached.  This
121251881Speter	 * is particularly a problem when the interrupts are shared.  For
122251881Speter	 * example, if IRQ 10 is shared between a disk and network device
123362181Sdim	 * and the disk device generates an interrupt, if we "activate"
124251881Speter	 * IRQ 10 when the network driver is set up, then we will get
125251881Speter	 * recursive interrupt 10's as nothing will know how to turn off
126251881Speter	 * the disk device's interrupt.
127251881Speter	 *
128251881Speter	 * Having the ICU's active means we can probe interrupt routing to
129251881Speter	 * see if a device causes the corresponding pending bit to be set.
130251881Speter	 *
131251881Speter	 * This is all rather inconvenient.
132251881Speter	 */
133251881Speter#ifdef APIC_IO
134251881Speter	bsp_apic_configure();
135251881Speter	enable_intr();
136251881Speter#else
137251881Speter	enable_intr();
138251881Speter	INTREN(IRQ_SLAVE);
139251881Speter#endif /* APIC_IO */
140251881Speter
141251881Speter	/* nexus0 is the top of the i386 device tree */
142251881Speter	device_add_child(root_bus, "nexus", 0);
143251881Speter
144251881Speter	/* initialize new bus architecture */
145251881Speter	root_bus_configure();
146251881Speter
147251881Speter#ifdef DEV_ISA
148251881Speter	/*
149251881Speter	 * Explicitly probe and attach ISA last.  The isa bus saves
150251881Speter	 * it's device node at attach time for us here.
151251881Speter	 */
152251881Speter	if (isa_bus_device)
153251881Speter		isa_probe_children(isa_bus_device);
154362181Sdim#endif
155251881Speter
156251881Speter	/*
157251881Speter	 * Now we're ready to handle (pending) interrupts.
158251881Speter	 * XXX this is slightly misplaced.
159251881Speter	 */
160251881Speter	spl0();
161251881Speter}
162251881Speter
163251881Speterstatic void
164251881Speterconfigure_final(dummy)
165251881Speter	void *dummy;
166251881Speter{
167251881Speter	int i;
168251881Speter
169251881Speter	cninit_finish();
170251881Speter
171251881Speter	if (bootverbose) {
172251881Speter
173251881Speter#ifdef APIC_IO
174251881Speter		imen_dump();
175251881Speter#endif /* APIC_IO */
176251881Speter
177362181Sdim		/*
178251881Speter		 * Print out the BIOS's idea of the disk geometries.
179251881Speter		 */
180251881Speter		printf("BIOS Geometries:\n");
181362181Sdim		for (i = 0; i < N_BIOS_GEOM; i++) {
182251881Speter			unsigned long bios_geom;
183251881Speter			int max_cylinder, max_head, max_sector;
184251881Speter
185251881Speter			bios_geom = bootinfo.bi_bios_geom[i];
186251881Speter
187362181Sdim			/*
188251881Speter			 * XXX the bootstrap punts a 1200K floppy geometry
189251881Speter			 * when the get-disk-geometry interrupt fails.  Skip
190251881Speter			 * drives that have this geometry.
191251881Speter			 */
192			if (bios_geom == 0x4f010f)
193				continue;
194
195			printf(" %x:%08lx ", i, bios_geom);
196			max_cylinder = bios_geom >> 16;
197			max_head = (bios_geom >> 8) & 0xff;
198			max_sector = bios_geom & 0xff;
199			printf(
200		"0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
201			       max_cylinder, max_cylinder + 1,
202			       max_head, max_head + 1,
203			       max_sector, max_sector);
204		}
205		printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
206
207		printf("Device configuration finished.\n");
208	}
209	cold = 0;
210}
211
212#ifdef BOOTP
213extern void bootpc_init(void);
214#endif
215/*
216 * Do legacy root filesystem discovery.
217 */
218void
219cpu_rootconf()
220{
221#ifdef BOOTP
222        bootpc_init();
223#endif
224#if defined(NFS) && defined(NFS_ROOT)
225#if !defined(BOOTP_NFSROOT)
226	pxe_setup_nfsdiskless();
227	if (nfs_diskless_valid)
228#endif
229		rootdevnames[0] = "nfs:";
230#endif
231}
232SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
233
234u_long	bootdev = 0;		/* not a dev_t - encoding is different */
235
236#if defined(NFS) && defined(NFS_ROOT)
237
238#include <sys/socket.h>
239#include <net/if.h>
240#include <net/if_dl.h>
241#include <net/if_types.h>
242#include <net/if_var.h>
243#include <net/ethernet.h>
244#include <netinet/in.h>
245#include <nfs/rpcv2.h>
246#include <nfs/nfsproto.h>
247#include <nfs/nfs.h>
248#include <nfs/nfsdiskless.h>
249
250extern struct nfs_diskless	nfs_diskless;
251
252static int
253inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
254{
255	u_int32_t	a[4];
256	char		*cp;
257
258	bzero(sa, sizeof(*sa));
259	sa->sin_len = sizeof(*sa);
260	sa->sin_family = AF_INET;
261
262	if ((cp = getenv(ev)) == NULL)
263		return(1);
264	if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
265		return(1);
266	/* XXX is this ordering correct? */
267	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
268	return(0);
269}
270
271static int
272hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
273{
274	char		*cp;
275	u_int32_t	a[6];
276
277	bzero(sa, sizeof(*sa));
278	sa->sdl_len = sizeof(*sa);
279	sa->sdl_family = AF_LINK;
280	sa->sdl_type = IFT_ETHER;
281	sa->sdl_alen = ETHER_ADDR_LEN;
282	if ((cp = getenv(ev)) == NULL)
283		return(1);
284	if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
285		return(1);
286	sa->sdl_data[0] = a[0];
287	sa->sdl_data[1] = a[1];
288	sa->sdl_data[2] = a[2];
289	sa->sdl_data[3] = a[3];
290	sa->sdl_data[4] = a[4];
291	sa->sdl_data[5] = a[5];
292	return(0);
293}
294
295static int
296decode_nfshandle(char *ev, u_char *fh)
297{
298	u_char	*cp;
299	int	len, val;
300
301	if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
302		return(0);
303	len = 0;
304	cp++;
305	for (;;) {
306		if (*cp == 'X')
307			return(len);
308		if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
309			return(0);
310		*(fh++) = val;
311		len++;
312		cp += 2;
313		if (len > NFSX_V2FH)
314		    return(0);
315	}
316}
317
318/*
319 * Populate the essential fields in the nfsv3_diskless structure.
320 *
321 * The loader is expected to export the following environment variables:
322 *
323 * boot.netif.ip		IP address on boot interface
324 * boot.netif.netmask		netmask on boot interface
325 * boot.netif.gateway		default gateway (optional)
326 * boot.netif.hwaddr		hardware address of boot interface
327 * boot.nfsroot.server		IP address of root filesystem server
328 * boot.nfsroot.path		path of the root filesystem on server
329 * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
330 */
331static void
332pxe_setup_nfsdiskless()
333{
334	struct nfs_diskless	*nd = &nfs_diskless;
335	struct ifnet		*ifp;
336	struct ifaddr		*ifa;
337	struct sockaddr_dl	*sdl, ourdl;
338	struct sockaddr_in	myaddr, netmask;
339	char			*cp;
340
341	/* set up interface */
342	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
343		return;
344	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
345		printf("PXE: no netmask\n");
346		return;
347	}
348	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
349	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
350	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
351		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
352	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
353
354	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
355		printf("PXE: no hardware address\n");
356		return;
357	}
358	ifa = NULL;
359	ifp = TAILQ_FIRST(&ifnet);
360	TAILQ_FOREACH(ifp, &ifnet, if_link) {
361		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
362			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
363			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
364				if ((sdl->sdl_type == ourdl.sdl_type) &&
365				    (sdl->sdl_alen == ourdl.sdl_alen) &&
366				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
367					  ourdl.sdl_data + ourdl.sdl_nlen,
368					  sdl->sdl_alen))
369				    goto match_done;
370			}
371		}
372	}
373	printf("PXE: no interface\n");
374	return;	/* no matching interface */
375match_done:
376	sprintf(nd->myif.ifra_name, "%s%d", ifp->if_name, ifp->if_unit);
377
378
379	/* set up gateway */
380	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
381
382	/* XXX set up swap? */
383
384	/* set up root mount */
385	nd->root_args.rsize = 8192;		/* XXX tunable? */
386	nd->root_args.wsize = 8192;
387	nd->root_args.sotype = SOCK_DGRAM;
388	nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
389	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
390		printf("PXE: no server\n");
391		return;
392	}
393	nd->root_saddr.sin_port = htons(NFS_PORT);
394	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
395		printf("PXE: no NFS handle\n");
396		return;
397	}
398	if ((cp = getenv("boot.nfsroot.path")) != NULL)
399		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
400
401	nfs_diskless_valid = 1;
402}
403#endif
404