2 * Copyright (c) 1990 The Regents of the University of California.
3 * Copyright (c) 2008 The DragonFly Project.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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 $
38 * Setup the system to run on the current machine.
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.
45 #include "opt_bootp.h"
47 #include "opt_cd9660.h"
49 #include "opt_nfsroot.h"
50 #include "opt_rootdevname.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/bootmaj.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>
66 #include <sys/thread.h>
67 #include <sys/device.h>
68 #include <sys/machintr.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_pager.h>
75 #include <machine/pcb.h>
76 #include <machine/pcb_ext.h>
78 #include <machine/smp.h>
79 #include <machine/globaldata.h>
80 #include <machine/md_var.h>
83 #include <bus/isa/isavar.h>
85 device_t isa_bus_device
= NULL
;
88 static void cpu_startup (void *);
89 static void configure_first (void *);
90 static void configure (void *);
91 static void configure_final (void *);
93 #if defined(FFS) && defined(FFS_ROOT)
94 static void setroot (void);
97 #if defined(NFS) && defined(NFS_ROOT)
98 #if !defined(BOOTP_NFSROOT)
99 static void pxe_setup_nfsdiskless(void);
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
);
110 cdev_t rootdev
= NULL
;
111 cdev_t dumpdev
= NULL
;
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.
118 * nfsroot.rahead defaults to something reasonable, can be overridden.
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
);
129 cpu_startup(void *dummy
)
131 vm_offset_t buffer_sva
;
132 vm_offset_t buffer_eva
;
133 vm_offset_t pager_sva
;
134 vm_offset_t pager_eva
;
136 kprintf("%s", version
);
137 kprintf("real memory = %ju (%juK bytes)\n",
138 (uintmax_t)ptoa(Maxmem
), (uintmax_t)(ptoa(Maxmem
) / 1024));
141 int factor
= 4 * NBUFCALCSIZE
/ 1024;
142 int kbytes
= Maxmem
* (PAGE_SIZE
/ 1024);
146 nbuf
+= min((kbytes
- 4096) / factor
, 65536 / factor
);
148 nbuf
+= (kbytes
- 65536) * 2 / (factor
* 5);
149 if (maxbcache
&& nbuf
> maxbcache
/ NBUFCALCSIZE
)
150 nbuf
= maxbcache
/ NBUFCALCSIZE
;
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
);
157 nswbuf_mem
= lmax(lmin(nbuf
/ 32, 32), 4);
159 if (nswbuf_mem
< NSWBUF_MIN
)
160 nswbuf_mem
= NSWBUF_MIN
;
162 nswbuf_kva
= lmax(lmin(nbuf
/ 4, 256), 16);
164 if (nswbuf_kva
< NSWBUF_MIN
)
165 nswbuf_kva
= NSWBUF_MIN
;
169 * Allocate memory for the buffer cache
171 buf
= (void *)kmem_alloc(&kernel_map
,
172 nbuf
* sizeof(struct buf
),
174 swbuf_mem
= (void *)kmem_alloc(&kernel_map
,
175 nswbuf_mem
* sizeof(struct buf
),
177 swbuf_kva
= (void *)kmem_alloc(&kernel_map
,
178 nswbuf_kva
* sizeof(struct buf
),
184 kmem_suballoc(&kernel_map
, &clean_map
, &clean_sva
, &clean_eva
,
185 (nbuf
* MAXBSIZE
* 2) +
186 (nswbuf_mem
+ nswbuf_kva
) *MAXPHYS
+
188 kmem_suballoc(&clean_map
, &buffer_map
, &buffer_sva
, &buffer_eva
,
189 (nbuf
* MAXBSIZE
* 2));
190 buffer_map
.system_map
= 1;
191 kmem_suballoc(&clean_map
, &pager_map
, &pager_sva
, &pager_eva
,
192 (nswbuf_mem
+ nswbuf_kva
) *MAXPHYS
+
194 pager_map
.system_map
= 1;
195 kprintf("avail memory = %lu (%luK bytes)\n", ptoa(vmstats
.v_free_count
),
196 ptoa(vmstats
.v_free_count
) / 1024);
203 * Determine i/o configuration for a machine.
206 configure_first(void *dummy
)
211 configure(void *dummy
)
214 * Final interrupt support acviation, then enable hardware interrupts.
216 MachIntrABI
.finalize();
220 * This will configure all devices, generally starting with the
221 * nexus (i386/i386/nexus.c). The nexus ISA code explicitly
222 * dummies up the attach in order to delay legacy initialization
223 * until after all other busses/subsystems have had a chance
224 * at those resources.
226 root_bus_configure();
230 * Explicitly probe and attach ISA last. The isa bus saves
231 * it's device node at attach time for us here.
234 isa_probe_children(isa_bus_device
);
238 * Allow lowering of the ipl to the lowest kernel level if we
239 * panic (or call tsleep() before clearing `cold'). No level is
240 * completely safe (since a panic may occur in a critical region
241 * at splhigh()), but we want at least bio interrupts to work.
243 safepri
= TDPRI_KERN_USER
;
247 configure_final(void *dummy
)
252 kprintf("Device configuration finished.\n");
256 void bootpc_init(void);
259 * Do legacy root filesystem discovery.
267 #if defined(NFS) && defined(NFS_ROOT)
268 #if !defined(BOOTP_NFSROOT)
269 pxe_setup_nfsdiskless();
270 if (nfs_diskless_valid
)
272 rootdevnames
[0] = "nfs:";
274 #if defined(FFS) && defined(FFS_ROOT)
275 if (!rootdevnames
[0])
279 SYSINIT(cpu_rootconf
, SI_SUB_ROOT_CONF
, SI_ORDER_FIRST
, cpu_rootconf
, NULL
);
281 u_long bootdev
= 0; /* not a cdev_t - encoding is different */
283 #if defined(FFS) && defined(FFS_ROOT)
286 * The boot code uses old block device major numbers to pass bootdev to
287 * us. We have to translate these to character device majors because
288 * we don't have block devices any more.
291 boot_translate_majdev(int bmajor
)
293 static int conv
[] = { BOOTMAJOR_CONVARY
};
295 if (bmajor
>= 0 && bmajor
< NELEM(conv
))
296 return(conv
[bmajor
]);
301 * Attempt to find the device from which we were booted.
302 * If we can do so, and not instructed not to do so,
303 * set rootdevs[] and rootdevnames[] to correspond to the
306 * This code survives in order to allow the system to be
307 * booted from legacy environments that do not correctly
308 * populate the kernel environment. There are significant
309 * restrictions on the bootability of the system in this
310 * situation; it can only be mounting root from a 'da'
311 * 'wd' or 'fd' device, and the root filesystem must be ufs.
316 int majdev
, mindev
, unit
, slice
, part
;
317 cdev_t newrootdev
, dev
;
321 if ((bootdev
& B_MAGICMASK
) != B_DEVMAGIC
) {
322 kprintf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev
);
325 majdev
= boot_translate_majdev(B_TYPE(bootdev
));
327 kprintf("bootdev: %08lx type=%ld unit=%ld "
328 "slice=%ld part=%ld major=%d\n",
329 bootdev
, B_TYPE(bootdev
), B_UNIT(bootdev
),
330 B_SLICE(bootdev
), B_PARTITION(bootdev
), majdev
);
332 dev
= udev2dev(makeudev(majdev
, 0), 0);
333 if (!dev_is_good(dev
))
335 unit
= B_UNIT(bootdev
);
336 slice
= B_SLICE(bootdev
);
337 if (slice
== WHOLE_DISK_SLICE
)
338 slice
= COMPATIBILITY_SLICE
;
339 if (slice
< 0 || slice
>= MAX_SLICES
) {
340 kprintf("bad slice\n");
344 part
= B_PARTITION(bootdev
);
345 mindev
= dkmakeminor(unit
, slice
, part
);
346 newrootdev
= udev2dev(makeudev(majdev
, mindev
), 0);
347 if (!dev_is_good(newrootdev
))
349 sname
= dsname(newrootdev
, unit
, slice
, part
, partname
);
350 rootdevnames
[0] = kmalloc(strlen(sname
) + 6, M_DEVBUF
, M_WAITOK
);
351 ksprintf(rootdevnames
[0], "ufs:%s%s", sname
, partname
);
354 * For properly dangerously dedicated disks (ones with a historical
355 * bogus partition table), the boot blocks will give slice = 4, but
356 * the kernel will only provide the compatibility slice since it
357 * knows that slice 4 is not a real slice. Arrange to try mounting
358 * the compatibility slice as root if mounting the slice passed by
359 * the boot blocks fails. This handles the dangerously dedicated
360 * case and perhaps others.
362 if (slice
== COMPATIBILITY_SLICE
)
364 slice
= COMPATIBILITY_SLICE
;
365 sname
= dsname(newrootdev
, unit
, slice
, part
, partname
);
366 rootdevnames
[1] = kmalloc(strlen(sname
) + 6, M_DEVBUF
, M_WAITOK
);
367 ksprintf(rootdevnames
[1], "ufs:%s%s", sname
, partname
);
371 #if defined(NFS) && defined(NFS_ROOT)
372 #if !defined(BOOTP_NFSROOT)
374 #include <sys/socket.h>
376 #include <net/if_dl.h>
377 #include <net/if_types.h>
378 #include <net/if_var.h>
379 #include <net/ethernet.h>
380 #include <netinet/in.h>
381 #include <vfs/nfs/rpcv2.h>
382 #include <vfs/nfs/nfsproto.h>
383 #include <vfs/nfs/nfs.h>
384 #include <vfs/nfs/nfsdiskless.h>
386 extern struct nfs_diskless nfs_diskless
;
389 * Convert a kenv variable to a sockaddr. If the kenv variable does not
390 * exist the sockaddr will remain zerod out (callers typically just check
391 * sin_len). A network address of 0.0.0.0 is equivalent to failure.
394 inaddr_to_sockaddr(char *ev
, struct sockaddr_in
*sa
)
399 bzero(sa
, sizeof(*sa
));
401 if ((cp
= kgetenv(ev
)) == NULL
)
403 if (ksscanf(cp
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]) != 4)
405 if (a
[0] == 0 && a
[1] == 0 && a
[2] == 0 && a
[3] == 0)
407 /* XXX is this ordering correct? */
408 sa
->sin_addr
.s_addr
= (a
[3] << 24) + (a
[2] << 16) + (a
[1] << 8) + a
[0];
409 sa
->sin_len
= sizeof(*sa
);
410 sa
->sin_family
= AF_INET
;
415 hwaddr_to_sockaddr(char *ev
, struct sockaddr_dl
*sa
)
420 bzero(sa
, sizeof(*sa
));
421 sa
->sdl_len
= sizeof(*sa
);
422 sa
->sdl_family
= AF_LINK
;
423 sa
->sdl_type
= IFT_ETHER
;
424 sa
->sdl_alen
= ETHER_ADDR_LEN
;
425 if ((cp
= kgetenv(ev
)) == NULL
)
427 if (ksscanf(cp
, "%x:%x:%x:%x:%x:%x", &a
[0], &a
[1], &a
[2], &a
[3], &a
[4], &a
[5]) != 6)
429 sa
->sdl_data
[0] = a
[0];
430 sa
->sdl_data
[1] = a
[1];
431 sa
->sdl_data
[2] = a
[2];
432 sa
->sdl_data
[3] = a
[3];
433 sa
->sdl_data
[4] = a
[4];
434 sa
->sdl_data
[5] = a
[5];
439 decode_nfshandle(char *ev
, u_char
*fh
)
444 if (((cp
= kgetenv(ev
)) == NULL
) || (strlen(cp
) < 2) || (*cp
!= 'X'))
451 if ((ksscanf(cp
, "%2x", &val
) != 1) || (val
> 0xff))
462 * Populate the essential fields in the nfsv3_diskless structure.
464 * The loader is expected to export the following environment variables:
466 * boot.netif.ip IP address on boot interface
467 * boot.netif.netmask netmask on boot interface
468 * boot.netif.gateway default gateway (optional)
469 * boot.netif.hwaddr hardware address of boot interface
470 * boot.netif.name name of boot interface (instead of hw addr)
471 * boot.nfsroot.server IP address of root filesystem server
472 * boot.nfsroot.path path of the root filesystem on server
473 * boot.nfsroot.nfshandle NFS handle for root filesystem on server
476 pxe_setup_nfsdiskless(void)
478 struct nfs_diskless
*nd
= &nfs_diskless
;
481 struct sockaddr_dl
*sdl
, ourdl
;
482 struct sockaddr_in myaddr
, netmask
;
485 /* set up interface */
486 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr
))
488 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask
)) {
489 kprintf("PXE: no netmask\n");
492 bcopy(&myaddr
, &nd
->myif
.ifra_addr
, sizeof(myaddr
));
493 bcopy(&myaddr
, &nd
->myif
.ifra_broadaddr
, sizeof(myaddr
));
494 ((struct sockaddr_in
*) &nd
->myif
.ifra_broadaddr
)->sin_addr
.s_addr
=
495 myaddr
.sin_addr
.s_addr
| ~ netmask
.sin_addr
.s_addr
;
496 bcopy(&netmask
, &nd
->myif
.ifra_mask
, sizeof(netmask
));
498 if ((cp
= kgetenv("boot.netif.name")) != NULL
) {
502 strlcpy(nd
->myif
.ifra_name
, ifp
->if_xname
,
503 sizeof(nd
->myif
.ifra_name
));
508 kprintf("PXE: cannot find interface %s\n", cp
);
512 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl
)) {
513 kprintf("PXE: no hardware address\n");
518 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
519 struct ifaddr_container
*ifac
;
521 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
524 if ((ifa
->ifa_addr
->sa_family
== AF_LINK
) &&
525 (sdl
= ((struct sockaddr_dl
*)ifa
->ifa_addr
))) {
526 if ((sdl
->sdl_type
== ourdl
.sdl_type
) &&
527 (sdl
->sdl_alen
== ourdl
.sdl_alen
) &&
528 !bcmp(sdl
->sdl_data
+ sdl
->sdl_nlen
,
529 ourdl
.sdl_data
+ ourdl
.sdl_nlen
,
531 strlcpy(nd
->myif
.ifra_name
,
533 sizeof(nd
->myif
.ifra_name
));
541 kprintf("PXE: no interface\n");
542 return; /* no matching interface */
545 inaddr_to_sockaddr("boot.netif.gateway", &nd
->mygateway
);
547 /* XXX set up swap? */
549 /* set up root mount */
550 nd
->root_args
.rsize
= nfsroot_iosize
;
551 nd
->root_args
.wsize
= nfsroot_iosize
;
552 nd
->root_args
.sotype
= SOCK_STREAM
;
553 nd
->root_args
.readahead
= nfsroot_rahead
;
554 nd
->root_args
.flags
= NFSMNT_WSIZE
| NFSMNT_RSIZE
| NFSMNT_RESVPORT
|
556 if (inaddr_to_sockaddr("boot.nfsroot.server", &nd
->root_saddr
)) {
557 kprintf("PXE: no server\n");
560 nd
->root_saddr
.sin_port
= htons(NFS_PORT
);
563 * A tftp-only loader may pass NFS path information without a
564 * root handle. Generate a warning but continue configuring.
566 if (decode_nfshandle("boot.nfsroot.nfshandle", &nd
->root_fh
[0]) == 0) {
567 kprintf("PXE: Warning, no NFS handle passed from loader\n");
569 if ((cp
= kgetenv("boot.nfsroot.path")) != NULL
)
570 strncpy(nd
->root_hostnam
, cp
, MNAMELEN
- 1);
572 nfs_diskless_valid
= 1;