2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * MD bootstrap main() and assorted miscellaneous
38 #include <machine/bootinfo.h>
39 #include <machine/psl.h>
40 #include <sys/reboot.h>
42 #include "bootstrap.h"
43 #include "common/bootargs.h"
44 #include "libi386/libi386.h"
47 #ifdef LOADER_ZFS_SUPPORT
48 #include "../zfs/libzfs.h"
51 CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
52 CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
53 CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
54 CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
56 /* Arguments passed in from the boot1/boot2 loader */
57 static struct bootargs *kargs;
59 static u_int32_t initial_howto;
60 static u_int32_t initial_bootdev;
61 static struct bootinfo *initial_bootinfo;
63 struct arch_switch archsw; /* MI/MD interface boundary */
65 static void extract_currdev(void);
66 static int isa_inb(int port);
67 static void isa_outb(int port, int value);
69 #ifdef LOADER_ZFS_SUPPORT
70 static void i386_zfs_probe(void);
74 extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
79 static void *heap_top;
80 static void *heap_bottom;
87 /* Pick up arguments */
88 kargs = (void *)__args;
89 initial_howto = kargs->howto;
90 initial_bootdev = kargs->bootdev;
91 initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
93 /* Initialize the v86 register set to a known-good state. */
94 bzero(&v86, sizeof(v86));
95 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
98 * Initialise the heap as early as possible. Once this is done, malloc() is usable.
102 #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
103 defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
104 if (high_heap_size > 0) {
105 heap_top = PTOV(high_heap_base + high_heap_size);
106 heap_bottom = PTOV(high_heap_base);
107 if (high_heap_base < memtop_copyin)
108 memtop_copyin = high_heap_base;
112 heap_top = (void *)PTOV(bios_basemem);
113 heap_bottom = (void *)end;
115 setheap(heap_bottom, heap_top);
118 * XXX Chicken-and-egg problem; we want to have console output early, but some
119 * console attributes may depend on reading from eg. the boot device, which we
122 * We can use printf() etc. once this is done.
123 * If the previous boot stage has requested a serial console, prefer that.
125 bi_setboothowto(initial_howto);
126 if (initial_howto & RB_MULTIPLE) {
127 if (initial_howto & RB_SERIAL)
128 setenv("console", "comconsole vidconsole", 1);
130 setenv("console", "vidconsole comconsole", 1);
131 } else if (initial_howto & RB_SERIAL)
132 setenv("console", "comconsole", 1);
133 else if (initial_howto & RB_MUTE)
134 setenv("console", "nullconsole", 1);
138 * Initialise the block cache
140 bcache_init(32, 512); /* 16k cache XXX tune this */
143 * Special handling for PXE and CD booting.
145 if (kargs->bootinfo == 0) {
147 * We only want the PXE disk to try to init itself in the below
148 * walk through devsw if we actually booted off of PXE.
150 if (kargs->bootflags & KARGS_FLAGS_PXE)
151 pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
152 else if (kargs->bootflags & KARGS_FLAGS_CD)
153 bc_add(initial_bootdev);
156 archsw.arch_autoload = i386_autoload;
157 archsw.arch_getdev = i386_getdev;
158 archsw.arch_copyin = i386_copyin;
159 archsw.arch_copyout = i386_copyout;
160 archsw.arch_readin = i386_readin;
161 archsw.arch_isainb = isa_inb;
162 archsw.arch_isaoutb = isa_outb;
163 #ifdef LOADER_ZFS_SUPPORT
164 archsw.arch_zfs_probe = i386_zfs_probe;
168 * March through the device switch probing for things.
170 for (i = 0; devsw[i] != NULL; i++)
171 if (devsw[i]->dv_init != NULL)
172 (devsw[i]->dv_init)();
173 printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
174 if (initial_bootinfo != NULL) {
175 initial_bootinfo->bi_basemem = bios_basemem / 1024;
176 initial_bootinfo->bi_extmem = bios_extmem / 1024;
179 /* detect ACPI for future reference */
182 /* detect SMBIOS for future reference */
186 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
187 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
189 extract_currdev(); /* set $currdev and $loaddev */
190 setenv("LINES", "24", 1); /* optional */
194 interact(); /* doesn't return */
196 /* if we ever get here, it is an error */
201 * Set the 'current device' by (if possible) recovering the boot device as
202 * supplied by the initial bootstrap.
204 * XXX should be extended for netbooting.
207 extract_currdev(void)
209 struct i386_devdesc new_currdev;
210 #ifdef LOADER_ZFS_SUPPORT
211 struct zfs_boot_args *zargs;
215 /* Assume we are booting from a BIOS disk by default */
216 new_currdev.d_dev = &biosdisk;
218 /* new-style boot loaders such as pxeldr and cdldr */
219 if (kargs->bootinfo == 0) {
220 if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
221 /* we are booting from a CD with cdboot */
222 new_currdev.d_dev = &bioscd;
223 new_currdev.d_unit = bc_bios2unit(initial_bootdev);
224 } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
225 /* we are booting from pxeldr */
226 new_currdev.d_dev = &pxedisk;
227 new_currdev.d_unit = 0;
229 /* we don't know what our boot device is */
230 new_currdev.d_kind.biosdisk.slice = -1;
231 new_currdev.d_kind.biosdisk.partition = 0;
234 #ifdef LOADER_ZFS_SUPPORT
235 } else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
237 /* check for new style extended argument */
238 if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0)
239 zargs = (struct zfs_boot_args *)(kargs + 1);
241 if (zargs != NULL && zargs->size >= sizeof(*zargs)) {
242 /* sufficient data is provided */
243 new_currdev.d_kind.zfs.pool_guid = zargs->pool;
244 new_currdev.d_kind.zfs.root_guid = zargs->root;
246 /* old style zfsboot block */
247 new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
248 new_currdev.d_kind.zfs.root_guid = 0;
250 new_currdev.d_dev = &zfs_dev;
252 } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
253 /* The passed-in boot device is bad */
254 new_currdev.d_kind.biosdisk.slice = -1;
255 new_currdev.d_kind.biosdisk.partition = 0;
258 new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
259 new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
260 biosdev = initial_bootinfo->bi_bios_dev;
263 * If we are booted by an old bootstrap, we have to guess at the BIOS
264 * unit number. We will lose if there is more than one disk type
265 * and we are not booting from the lowest-numbered disk type
266 * (ie. SCSI when IDE also exists).
268 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */
269 biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */
271 new_currdev.d_type = new_currdev.d_dev->dv_type;
274 * If we are booting off of a BIOS disk and we didn't succeed in determining
275 * which one we booted off of, just use disk0: as a reasonable default.
277 if ((new_currdev.d_type == biosdisk.dv_type) &&
278 ((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
279 printf("Can't work out which disk we are booting from.\n"
280 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
281 new_currdev.d_unit = 0;
284 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
285 i386_setcurrdev, env_nounset);
286 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
290 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
293 command_reboot(int argc, char *argv[])
297 for (i = 0; devsw[i] != NULL; ++i)
298 if (devsw[i]->dv_cleanup != NULL)
299 (devsw[i]->dv_cleanup)();
301 printf("Rebooting...\n");
306 /* provide this for panic, as it's not in the startup code */
313 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
316 command_heap(int argc, char *argv[])
319 printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
324 /* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
330 if (__builtin_constant_p(port) &&
331 (((port) & 0xffff) < 0x100) &&
332 ((port) < 0x10000)) {
333 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
335 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
341 isa_outb(int port, int value)
345 if (__builtin_constant_p(port) &&
346 (((port) & 0xffff) < 0x100) &&
347 ((port) < 0x10000)) {
348 __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
350 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
354 #ifdef LOADER_ZFS_SUPPORT
362 * Open all the disks we can find and see if we can reconstruct
363 * ZFS pools from them. Bogusly assumes that the disks are named
364 * diskN, diskNpM or diskNsM.
366 for (unit = 0; unit < MAXBDDEV; unit++) {
367 sprintf(devname, "disk%d:", unit);
368 if (zfs_probe_dev(devname, NULL) == ENXIO)
370 for (slice = 1; slice <= 128; slice++) {
371 sprintf(devname, "disk%dp%d:", unit, slice);
372 zfs_probe_dev(devname, NULL);
374 for (slice = 1; slice <= 4; slice++) {
375 sprintf(devname, "disk%ds%d:", unit, slice);
376 zfs_probe_dev(devname, NULL);