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/cpufunc.h>
40 #include <machine/psl.h>
42 #include <sys/reboot.h>
43 #include <common/drv.h>
45 #include "bootstrap.h"
46 #include "common/bootargs.h"
47 #include "libi386/libi386.h"
51 #ifdef LOADER_ZFS_SUPPORT
52 #include <sys/zfs_bootenv.h>
56 CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
57 CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
58 CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
59 CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
61 /* Arguments passed in from the boot1/boot2 loader */
62 static struct bootargs *kargs;
64 static uint32_t initial_howto;
65 static uint32_t initial_bootdev;
66 static struct bootinfo *initial_bootinfo;
68 struct arch_switch archsw; /* MI/MD interface boundary */
70 static void extract_currdev(void);
71 static int isa_inb(int port);
72 static void isa_outb(int port, int value);
74 #ifdef LOADER_GELI_SUPPORT
76 struct geli_boot_args *gargs;
77 struct geli_boot_data *gbdata;
79 #ifdef LOADER_ZFS_SUPPORT
80 struct zfs_boot_args *zargs;
81 static void i386_zfs_probe(void);
87 static void *heap_top;
88 static void *heap_bottom;
101 /* Pick up arguments */
102 kargs = (void *)__args;
103 initial_howto = kargs->howto;
104 initial_bootdev = kargs->bootdev;
105 initial_bootinfo = kargs->bootinfo ?
106 (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
108 /* Initialize the v86 register set to a known-good state. */
109 bzero(&v86, sizeof(v86));
110 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
113 * Initialise the heap as early as possible.
114 * Once this is done, malloc() is usable.
118 #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
119 defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
120 if (high_heap_size > 0) {
121 heap_top = PTOV(high_heap_base + high_heap_size);
122 heap_bottom = PTOV(high_heap_base);
123 if (high_heap_base < memtop_copyin)
124 memtop_copyin = high_heap_base;
128 heap_top = (void *)PTOV(bios_basemem);
129 heap_bottom = (void *)end;
131 setheap(heap_bottom, heap_top);
134 * XXX Chicken-and-egg problem; we want to have console output early,
135 * but some console attributes may depend on reading from eg. the boot
136 * device, which we can't do yet.
138 * We can use printf() etc. once this is done.
139 * If the previous boot stage has requested a serial console,
142 bi_setboothowto(initial_howto);
143 if (initial_howto & RB_MULTIPLE) {
144 if (initial_howto & RB_SERIAL)
145 setenv("console", "comconsole vidconsole", 1);
147 setenv("console", "vidconsole comconsole", 1);
148 } else if (initial_howto & RB_SERIAL) {
149 setenv("console", "comconsole", 1);
150 } else if (initial_howto & RB_MUTE) {
151 setenv("console", "nullconsole", 1);
156 * Initialise the block cache. Set the upper limit.
158 bcache_init(32768, 512);
161 * Special handling for PXE and CD booting.
163 if (kargs->bootinfo == 0) {
165 * We only want the PXE disk to try to init itself in the below
166 * walk through devsw if we actually booted off of PXE.
168 if (kargs->bootflags & KARGS_FLAGS_PXE)
169 pxe_enable(kargs->pxeinfo ?
170 PTOV(kargs->pxeinfo) : NULL);
171 else if (kargs->bootflags & KARGS_FLAGS_CD)
172 bc_add(initial_bootdev);
175 archsw.arch_autoload = i386_autoload;
176 archsw.arch_getdev = i386_getdev;
177 archsw.arch_copyin = i386_copyin;
178 archsw.arch_copyout = i386_copyout;
179 archsw.arch_readin = i386_readin;
180 archsw.arch_isainb = isa_inb;
181 archsw.arch_isaoutb = isa_outb;
182 archsw.arch_hypervisor = x86_hypervisor;
183 #ifdef LOADER_ZFS_SUPPORT
184 archsw.arch_zfs_probe = i386_zfs_probe;
187 * zfsboot and gptzfsboot have always passed KARGS_FLAGS_ZFS,
188 * so if that is set along with KARGS_FLAGS_EXTARG we know we
189 * can interpret the extarg data as a struct zfs_boot_args.
191 #define KARGS_EXTARGS_ZFS (KARGS_FLAGS_EXTARG | KARGS_FLAGS_ZFS)
193 if ((kargs->bootflags & KARGS_EXTARGS_ZFS) == KARGS_EXTARGS_ZFS) {
194 zargs = (struct zfs_boot_args *)(kargs + 1);
196 #endif /* LOADER_ZFS_SUPPORT */
198 #ifdef LOADER_GELI_SUPPORT
200 * If we decided earlier that we have zfs_boot_args extarg data,
201 * and it is big enough to contain the embedded geli data
202 * (the early zfs_boot_args structs weren't), then init the gbdata
203 * pointer accordingly. If there is extarg data which isn't
204 * zfs_boot_args data, determine whether it is geli_boot_args data.
205 * Recent versions of gptboot set KARGS_FLAGS_GELI to indicate that.
206 * Earlier versions didn't, but we presume that's what we
207 * have if the extarg size exactly matches the size of the
208 * geli_boot_args struct during that pre-flag era.
210 #define LEGACY_GELI_ARGS_SIZE 260 /* This can never change */
212 #ifdef LOADER_ZFS_SUPPORT
214 if (zargs->size > offsetof(struct zfs_boot_args, gelidata)) {
215 gbdata = &zargs->gelidata;
218 #endif /* LOADER_ZFS_SUPPORT */
219 if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0) {
220 gargs = (struct geli_boot_args *)(kargs + 1);
221 if ((kargs->bootflags & KARGS_FLAGS_GELI) ||
222 gargs->size == LEGACY_GELI_ARGS_SIZE) {
223 gbdata = &gargs->gelidata;
228 import_geli_boot_data(gbdata);
229 #endif /* LOADER_GELI_SUPPORT */
232 * March through the device switch probing for things.
234 for (i = 0; devsw[i] != NULL; i++)
235 if (devsw[i]->dv_init != NULL)
236 (devsw[i]->dv_init)();
238 printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024,
240 if (initial_bootinfo != NULL) {
241 initial_bootinfo->bi_basemem = bios_basemem / 1024;
242 initial_bootinfo->bi_extmem = bios_extmem / 1024;
245 /* detect ACPI for future reference */
248 /* detect SMBIOS for future reference */
251 /* detect PCI BIOS for future reference */
254 printf("\n%s", bootprog_info);
256 extract_currdev(); /* set $currdev and $loaddev */
262 /* if we ever get here, it is an error */
267 * Set the 'current device' by (if possible) recovering the boot device as
268 * supplied by the initial bootstrap.
270 * XXX should be extended for netbooting.
273 extract_currdev(void)
275 struct i386_devdesc new_currdev;
276 #ifdef LOADER_ZFS_SUPPORT
282 /* Assume we are booting from a BIOS disk by default */
283 new_currdev.dd.d_dev = &bioshd;
285 /* new-style boot loaders such as pxeldr and cdldr */
286 if (kargs->bootinfo == 0) {
287 if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
288 /* we are booting from a CD with cdboot */
289 new_currdev.dd.d_dev = &bioscd;
290 new_currdev.dd.d_unit = bd_bios2unit(initial_bootdev);
291 } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
292 /* we are booting from pxeldr */
293 new_currdev.dd.d_dev = &pxedisk;
294 new_currdev.dd.d_unit = 0;
296 /* we don't know what our boot device is */
297 new_currdev.d_kind.biosdisk.slice = -1;
298 new_currdev.d_kind.biosdisk.partition = 0;
301 #ifdef LOADER_ZFS_SUPPORT
302 } else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
304 * zargs was set in main() if we have new style extended
309 offsetof(struct zfs_boot_args, primary_pool)) {
310 /* sufficient data is provided */
311 new_currdev.d_kind.zfs.pool_guid = zargs->pool;
312 new_currdev.d_kind.zfs.root_guid = zargs->root;
313 if (zargs->size >= sizeof(*zargs) &&
314 zargs->primary_vdev != 0) {
315 sprintf(buf, "%llu", zargs->primary_pool);
316 setenv("vfs.zfs.boot.primary_pool", buf, 1);
317 sprintf(buf, "%llu", zargs->primary_vdev);
318 setenv("vfs.zfs.boot.primary_vdev", buf, 1);
321 /* old style zfsboot block */
322 new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
323 new_currdev.d_kind.zfs.root_guid = 0;
325 new_currdev.dd.d_dev = &zfs_dev;
327 if ((bootonce = malloc(VDEV_PAD_SIZE)) != NULL) {
328 if (zfs_get_bootonce(&new_currdev, OS_BOOTONCE_USED,
329 bootonce, VDEV_PAD_SIZE) == 0) {
330 setenv("zfs-bootonce", bootonce, 1);
333 (void) zfs_attach_nvstore(&new_currdev);
337 } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
338 /* The passed-in boot device is bad */
339 new_currdev.d_kind.biosdisk.slice = -1;
340 new_currdev.d_kind.biosdisk.partition = 0;
343 new_currdev.d_kind.biosdisk.slice =
344 B_SLICE(initial_bootdev) - 1;
345 new_currdev.d_kind.biosdisk.partition =
346 B_PARTITION(initial_bootdev);
347 biosdev = initial_bootinfo->bi_bios_dev;
350 * If we are booted by an old bootstrap, we have to guess at
351 * the BIOS unit number. We will lose if there is more than
352 * one disk type and we are not booting from the
353 * lowest-numbered disk type (ie. SCSI when IDE also exists).
355 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {
357 * biosdev doesn't match major, assume harddisk
359 biosdev = 0x80 + B_UNIT(initial_bootdev);
364 * If we are booting off of a BIOS disk and we didn't succeed
365 * in determining which one we booted off of, just use disk0:
366 * as a reasonable default.
368 if ((new_currdev.dd.d_dev->dv_type == bioshd.dv_type) &&
369 ((new_currdev.dd.d_unit = bd_bios2unit(biosdev)) == -1)) {
370 printf("Can't work out which disk we are booting "
371 "from.\nGuessed BIOS device 0x%x not found by "
372 "probes, defaulting to disk0:\n", biosdev);
373 new_currdev.dd.d_unit = 0;
376 #ifdef LOADER_ZFS_SUPPORT
377 if (new_currdev.dd.d_dev->dv_type == DEVT_ZFS)
378 init_zfs_boot_options(zfs_fmtdev(&new_currdev));
381 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
382 i386_setcurrdev, env_nounset);
383 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev),
384 env_noset, env_nounset);
387 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
390 command_reboot(int argc, char *argv[])
394 for (i = 0; devsw[i] != NULL; ++i)
395 if (devsw[i]->dv_cleanup != NULL)
396 (devsw[i]->dv_cleanup)();
398 printf("Rebooting...\n");
403 /* provide this for panic, as it's not in the startup code */
410 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
413 command_heap(int argc, char *argv[])
416 printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
421 /* ISA bus access functions for PnP. */
430 isa_outb(int port, int value)
436 #ifdef LOADER_ZFS_SUPPORT
441 struct i386_devdesc dev;
444 * Open all the disks we can find and see if we can reconstruct
445 * ZFS pools from them.
447 dev.dd.d_dev = &bioshd;
448 for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
449 snprintf(devname, sizeof(devname), "%s%d:", bioshd.dv_name,
451 zfs_probe_dev(devname, NULL);