xref: /freebsd/stand/i386/loader/main.c (revision 9631ae826787274753b7f8eb33adef9665078c16)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
27ca987d46SWarner Losh #include <sys/cdefs.h>
28ca987d46SWarner Losh __FBSDID("$FreeBSD$");
29ca987d46SWarner Losh 
30ca987d46SWarner Losh /*
31ca987d46SWarner Losh  * MD bootstrap main() and assorted miscellaneous
32ca987d46SWarner Losh  * commands.
33ca987d46SWarner Losh  */
34ca987d46SWarner Losh 
35ca987d46SWarner Losh #include <stand.h>
36ca987d46SWarner Losh #include <stddef.h>
37ca987d46SWarner Losh #include <string.h>
38ca987d46SWarner Losh #include <machine/bootinfo.h>
39ca987d46SWarner Losh #include <machine/cpufunc.h>
40ca987d46SWarner Losh #include <machine/psl.h>
41ca987d46SWarner Losh #include <sys/disk.h>
42ca987d46SWarner Losh #include <sys/reboot.h>
43ca987d46SWarner Losh #include <common/drv.h>
44ca987d46SWarner Losh 
45ca987d46SWarner Losh #include "bootstrap.h"
46ca987d46SWarner Losh #include "common/bootargs.h"
47ca987d46SWarner Losh #include "libi386/libi386.h"
48ca987d46SWarner Losh #include "libi386/smbios.h"
49ca987d46SWarner Losh #include "btxv86.h"
50ca987d46SWarner Losh 
51ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
52007b82d7SWarner Losh #include "libzfs.h"
53ca987d46SWarner Losh #endif
54ca987d46SWarner Losh 
55ca987d46SWarner Losh CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
56ca987d46SWarner Losh CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
57ca987d46SWarner Losh CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
58ca987d46SWarner Losh CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
59ca987d46SWarner Losh 
60ca987d46SWarner Losh /* Arguments passed in from the boot1/boot2 loader */
61ca987d46SWarner Losh static struct bootargs *kargs;
62ca987d46SWarner Losh 
6356e53cb8SWarner Losh static uint32_t		initial_howto;
6456e53cb8SWarner Losh static uint32_t		initial_bootdev;
65ca987d46SWarner Losh static struct bootinfo	*initial_bootinfo;
66ca987d46SWarner Losh 
67ca987d46SWarner Losh struct arch_switch	archsw;		/* MI/MD interface boundary */
68ca987d46SWarner Losh 
69ca987d46SWarner Losh static void		extract_currdev(void);
70ca987d46SWarner Losh static int		isa_inb(int port);
71ca987d46SWarner Losh static void		isa_outb(int port, int value);
72ca987d46SWarner Losh void			exit(int code);
73ca987d46SWarner Losh #ifdef LOADER_GELI_SUPPORT
74ca987d46SWarner Losh #include "geliboot.h"
75ca987d46SWarner Losh struct geli_boot_args	*gargs;
767297dc44SIan Lepore struct geli_boot_data	*gbdata;
77ca987d46SWarner Losh #endif
78ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
79ca987d46SWarner Losh struct zfs_boot_args	*zargs;
80ca987d46SWarner Losh static void		i386_zfs_probe(void);
81ca987d46SWarner Losh #endif
82ca987d46SWarner Losh 
83ca987d46SWarner Losh /* XXX debugging */
84ca987d46SWarner Losh extern char end[];
85ca987d46SWarner Losh 
86ca987d46SWarner Losh static void *heap_top;
87ca987d46SWarner Losh static void *heap_bottom;
88ca987d46SWarner Losh 
89ca987d46SWarner Losh int
90ca987d46SWarner Losh main(void)
91ca987d46SWarner Losh {
92ca987d46SWarner Losh     int			i;
93ca987d46SWarner Losh 
94ca987d46SWarner Losh     /* Pick up arguments */
95ca987d46SWarner Losh     kargs = (void *)__args;
96ca987d46SWarner Losh     initial_howto = kargs->howto;
97ca987d46SWarner Losh     initial_bootdev = kargs->bootdev;
98ca987d46SWarner Losh     initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
99ca987d46SWarner Losh 
100ca987d46SWarner Losh     /* Initialize the v86 register set to a known-good state. */
101ca987d46SWarner Losh     bzero(&v86, sizeof(v86));
102ca987d46SWarner Losh     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
103ca987d46SWarner Losh 
104ca987d46SWarner Losh     /*
105ca987d46SWarner Losh      * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
106ca987d46SWarner Losh      */
107ca987d46SWarner Losh     bios_getmem();
108ca987d46SWarner Losh 
109ca987d46SWarner Losh #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
110ca987d46SWarner Losh     defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
111ca987d46SWarner Losh     if (high_heap_size > 0) {
112ca987d46SWarner Losh 	heap_top = PTOV(high_heap_base + high_heap_size);
113ca987d46SWarner Losh 	heap_bottom = PTOV(high_heap_base);
114ca987d46SWarner Losh 	if (high_heap_base < memtop_copyin)
115ca987d46SWarner Losh 	    memtop_copyin = high_heap_base;
116ca987d46SWarner Losh     } else
117ca987d46SWarner Losh #endif
118ca987d46SWarner Losh     {
119ca987d46SWarner Losh 	heap_top = (void *)PTOV(bios_basemem);
120ca987d46SWarner Losh 	heap_bottom = (void *)end;
121ca987d46SWarner Losh     }
122ca987d46SWarner Losh     setheap(heap_bottom, heap_top);
123ca987d46SWarner Losh 
124ca987d46SWarner Losh     /*
125ca987d46SWarner Losh      * XXX Chicken-and-egg problem; we want to have console output early, but some
126ca987d46SWarner Losh      * console attributes may depend on reading from eg. the boot device, which we
127ca987d46SWarner Losh      * can't do yet.
128ca987d46SWarner Losh      *
129ca987d46SWarner Losh      * We can use printf() etc. once this is done.
130ca987d46SWarner Losh      * If the previous boot stage has requested a serial console, prefer that.
131ca987d46SWarner Losh      */
132ca987d46SWarner Losh     bi_setboothowto(initial_howto);
133ca987d46SWarner Losh     if (initial_howto & RB_MULTIPLE) {
134ca987d46SWarner Losh 	if (initial_howto & RB_SERIAL)
135ca987d46SWarner Losh 	    setenv("console", "comconsole vidconsole", 1);
136ca987d46SWarner Losh 	else
137ca987d46SWarner Losh 	    setenv("console", "vidconsole comconsole", 1);
138ca987d46SWarner Losh     } else if (initial_howto & RB_SERIAL)
139ca987d46SWarner Losh 	setenv("console", "comconsole", 1);
140ca987d46SWarner Losh     else if (initial_howto & RB_MUTE)
141ca987d46SWarner Losh 	setenv("console", "nullconsole", 1);
142ca987d46SWarner Losh     cons_probe();
143ca987d46SWarner Losh 
144ca987d46SWarner Losh     /*
145ca987d46SWarner Losh      * Initialise the block cache. Set the upper limit.
146ca987d46SWarner Losh      */
147ca987d46SWarner Losh     bcache_init(32768, 512);
148ca987d46SWarner Losh 
149ca987d46SWarner Losh     /*
150ca987d46SWarner Losh      * Special handling for PXE and CD booting.
151ca987d46SWarner Losh      */
152ca987d46SWarner Losh     if (kargs->bootinfo == 0) {
153ca987d46SWarner Losh 	/*
154ca987d46SWarner Losh 	 * We only want the PXE disk to try to init itself in the below
155ca987d46SWarner Losh 	 * walk through devsw if we actually booted off of PXE.
156ca987d46SWarner Losh 	 */
157ca987d46SWarner Losh 	if (kargs->bootflags & KARGS_FLAGS_PXE)
158ca987d46SWarner Losh 	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
159ca987d46SWarner Losh 	else if (kargs->bootflags & KARGS_FLAGS_CD)
160ca987d46SWarner Losh 	    bc_add(initial_bootdev);
161ca987d46SWarner Losh     }
162ca987d46SWarner Losh 
163ca987d46SWarner Losh     archsw.arch_autoload = i386_autoload;
164ca987d46SWarner Losh     archsw.arch_getdev = i386_getdev;
165ca987d46SWarner Losh     archsw.arch_copyin = i386_copyin;
166ca987d46SWarner Losh     archsw.arch_copyout = i386_copyout;
167ca987d46SWarner Losh     archsw.arch_readin = i386_readin;
168ca987d46SWarner Losh     archsw.arch_isainb = isa_inb;
169ca987d46SWarner Losh     archsw.arch_isaoutb = isa_outb;
170ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
171ca987d46SWarner Losh     archsw.arch_zfs_probe = i386_zfs_probe;
172ca987d46SWarner Losh 
1737297dc44SIan Lepore     /*
1747297dc44SIan Lepore      * zfsboot and gptzfsboot have always passed KARGS_FLAGS_ZFS, so if that is
1757297dc44SIan Lepore      * set along with KARGS_FLAGS_EXTARG we know we can interpret the extarg
1767297dc44SIan Lepore      * data as a struct zfs_boot_args.
1777297dc44SIan Lepore      */
1787297dc44SIan Lepore #define	KARGS_EXTARGS_ZFS	(KARGS_FLAGS_EXTARG | KARGS_FLAGS_ZFS)
1797297dc44SIan Lepore 
1807297dc44SIan Lepore     if ((kargs->bootflags & KARGS_EXTARGS_ZFS) == KARGS_EXTARGS_ZFS) {
181ca987d46SWarner Losh 	zargs = (struct zfs_boot_args *)(kargs + 1);
182ca987d46SWarner Losh     }
183ca987d46SWarner Losh #endif /* LOADER_ZFS_SUPPORT */
184ca987d46SWarner Losh 
1857297dc44SIan Lepore #ifdef LOADER_GELI_SUPPORT
1867297dc44SIan Lepore     /*
1877297dc44SIan Lepore      * If we decided earlier that we have zfs_boot_args extarg data, and it is
1887297dc44SIan Lepore      * big enough to contain the embedded geli data (the early zfs_boot_args
1897297dc44SIan Lepore      * structs weren't), then init the gbdata pointer accordingly. If there is
1907297dc44SIan Lepore      * extarg data which isn't zfs_boot_args data, determine whether it is
1917297dc44SIan Lepore      * geli_boot_args data.  Recent versions of gptboot set KARGS_FLAGS_GELI to
1927297dc44SIan Lepore      * indicate that.  Earlier versions didn't, but we presume that's what we
1937297dc44SIan Lepore      * have if the extarg size exactly matches the size of the geli_boot_args
1947297dc44SIan Lepore      * struct during that pre-flag era.
1957297dc44SIan Lepore      */
1967297dc44SIan Lepore #define	LEGACY_GELI_ARGS_SIZE	260	/* This can never change */
1977297dc44SIan Lepore 
198*9631ae82SIan Lepore #ifdef LOADER_ZFS_SUPPORT
1997297dc44SIan Lepore     if (zargs != NULL) {
2007297dc44SIan Lepore 	if (zargs->size > offsetof(struct zfs_boot_args, gelidata)) {
2017297dc44SIan Lepore 	    gbdata = &zargs->gelidata;
2027297dc44SIan Lepore 	}
203*9631ae82SIan Lepore     } else
204*9631ae82SIan Lepore #endif /* LOADER_ZFS_SUPPORT */
205*9631ae82SIan Lepore 	   if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0) {
2067297dc44SIan Lepore 	gargs = (struct geli_boot_args *)(kargs + 1);
2077297dc44SIan Lepore 	if ((kargs->bootflags & KARGS_FLAGS_GELI) ||
2087297dc44SIan Lepore 	    gargs->size == LEGACY_GELI_ARGS_SIZE) {
2097297dc44SIan Lepore 	    gbdata = &gargs->gelidata;
2107297dc44SIan Lepore 	}
2117297dc44SIan Lepore     }
2127297dc44SIan Lepore 
2137297dc44SIan Lepore     if (gbdata != NULL)
2147297dc44SIan Lepore 	import_geli_boot_data(gbdata);
2157297dc44SIan Lepore #endif /* LOADER_GELI_SUPPORT */
2167297dc44SIan Lepore 
217ca987d46SWarner Losh     /*
218ca987d46SWarner Losh      * March through the device switch probing for things.
219ca987d46SWarner Losh      */
220ca987d46SWarner Losh     for (i = 0; devsw[i] != NULL; i++)
221ca987d46SWarner Losh 	if (devsw[i]->dv_init != NULL)
222ca987d46SWarner Losh 	    (devsw[i]->dv_init)();
223ca987d46SWarner Losh     printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
224ca987d46SWarner Losh     if (initial_bootinfo != NULL) {
225ca987d46SWarner Losh 	initial_bootinfo->bi_basemem = bios_basemem / 1024;
226ca987d46SWarner Losh 	initial_bootinfo->bi_extmem = bios_extmem / 1024;
227ca987d46SWarner Losh     }
228ca987d46SWarner Losh 
229ca987d46SWarner Losh     /* detect ACPI for future reference */
230ca987d46SWarner Losh     biosacpi_detect();
231ca987d46SWarner Losh 
232ca987d46SWarner Losh     /* detect SMBIOS for future reference */
233ca987d46SWarner Losh     smbios_detect(NULL);
234ca987d46SWarner Losh 
235ca987d46SWarner Losh     /* detect PCI BIOS for future reference */
236ca987d46SWarner Losh     biospci_detect();
237ca987d46SWarner Losh 
238ca987d46SWarner Losh     printf("\n%s", bootprog_info);
239ca987d46SWarner Losh 
240ca987d46SWarner Losh     extract_currdev();				/* set $currdev and $loaddev */
241ca987d46SWarner Losh     setenv("LINES", "24", 1);			/* optional */
242ca987d46SWarner Losh 
243ca987d46SWarner Losh     bios_getsmap();
244ca987d46SWarner Losh 
2456bc86037SWarner Losh     interact();
246ca987d46SWarner Losh 
247ca987d46SWarner Losh     /* if we ever get here, it is an error */
248ca987d46SWarner Losh     return (1);
249ca987d46SWarner Losh }
250ca987d46SWarner Losh 
251ca987d46SWarner Losh /*
252ca987d46SWarner Losh  * Set the 'current device' by (if possible) recovering the boot device as
253ca987d46SWarner Losh  * supplied by the initial bootstrap.
254ca987d46SWarner Losh  *
255ca987d46SWarner Losh  * XXX should be extended for netbooting.
256ca987d46SWarner Losh  */
257ca987d46SWarner Losh static void
258ca987d46SWarner Losh extract_currdev(void)
259ca987d46SWarner Losh {
260ca987d46SWarner Losh     struct i386_devdesc		new_currdev;
261ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
262ca987d46SWarner Losh     char			buf[20];
263ca987d46SWarner Losh #endif
264ca987d46SWarner Losh     int				biosdev = -1;
265ca987d46SWarner Losh 
266ca987d46SWarner Losh     /* Assume we are booting from a BIOS disk by default */
267cdff1036SToomas Soome     new_currdev.dd.d_dev = &bioshd;
268ca987d46SWarner Losh 
269ca987d46SWarner Losh     /* new-style boot loaders such as pxeldr and cdldr */
270ca987d46SWarner Losh     if (kargs->bootinfo == 0) {
271ca987d46SWarner Losh         if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
272ca987d46SWarner Losh 	    /* we are booting from a CD with cdboot */
273de04d704SWarner Losh 	    new_currdev.dd.d_dev = &bioscd;
274cdff1036SToomas Soome 	    new_currdev.dd.d_unit = bd_bios2unit(initial_bootdev);
275ca987d46SWarner Losh 	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
276ca987d46SWarner Losh 	    /* we are booting from pxeldr */
277de04d704SWarner Losh 	    new_currdev.dd.d_dev = &pxedisk;
278de04d704SWarner Losh 	    new_currdev.dd.d_unit = 0;
279ca987d46SWarner Losh 	} else {
280ca987d46SWarner Losh 	    /* we don't know what our boot device is */
281ca987d46SWarner Losh 	    new_currdev.d_kind.biosdisk.slice = -1;
282ca987d46SWarner Losh 	    new_currdev.d_kind.biosdisk.partition = 0;
283ca987d46SWarner Losh 	    biosdev = -1;
284ca987d46SWarner Losh 	}
285ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
286ca987d46SWarner Losh     } else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
2877297dc44SIan Lepore 	/* zargs was set in main() if we have new style extended argument */
288ca987d46SWarner Losh 	if (zargs != NULL &&
289ca987d46SWarner Losh 	    zargs->size >= offsetof(struct zfs_boot_args, primary_pool)) {
290ca987d46SWarner Losh 	    /* sufficient data is provided */
291ca987d46SWarner Losh 	    new_currdev.d_kind.zfs.pool_guid = zargs->pool;
292ca987d46SWarner Losh 	    new_currdev.d_kind.zfs.root_guid = zargs->root;
293ca987d46SWarner Losh 	    if (zargs->size >= sizeof(*zargs) && zargs->primary_vdev != 0) {
294ca987d46SWarner Losh 		sprintf(buf, "%llu", zargs->primary_pool);
295ca987d46SWarner Losh 		setenv("vfs.zfs.boot.primary_pool", buf, 1);
296ca987d46SWarner Losh 		sprintf(buf, "%llu", zargs->primary_vdev);
297ca987d46SWarner Losh 		setenv("vfs.zfs.boot.primary_vdev", buf, 1);
298ca987d46SWarner Losh 	    }
299ca987d46SWarner Losh 	} else {
300ca987d46SWarner Losh 	    /* old style zfsboot block */
301ca987d46SWarner Losh 	    new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
302ca987d46SWarner Losh 	    new_currdev.d_kind.zfs.root_guid = 0;
303ca987d46SWarner Losh 	}
304de04d704SWarner Losh 	new_currdev.dd.d_dev = &zfs_dev;
305ca987d46SWarner Losh #endif
306ca987d46SWarner Losh     } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
307ca987d46SWarner Losh 	/* The passed-in boot device is bad */
308ca987d46SWarner Losh 	new_currdev.d_kind.biosdisk.slice = -1;
309ca987d46SWarner Losh 	new_currdev.d_kind.biosdisk.partition = 0;
310ca987d46SWarner Losh 	biosdev = -1;
311ca987d46SWarner Losh     } else {
312ca987d46SWarner Losh 	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
313ca987d46SWarner Losh 	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
314ca987d46SWarner Losh 	biosdev = initial_bootinfo->bi_bios_dev;
315ca987d46SWarner Losh 
316ca987d46SWarner Losh 	/*
317ca987d46SWarner Losh 	 * If we are booted by an old bootstrap, we have to guess at the BIOS
318ca987d46SWarner Losh 	 * unit number.  We will lose if there is more than one disk type
319ca987d46SWarner Losh 	 * and we are not booting from the lowest-numbered disk type
320ca987d46SWarner Losh 	 * (ie. SCSI when IDE also exists).
321ca987d46SWarner Losh 	 */
322ca987d46SWarner Losh 	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
323ca987d46SWarner Losh 	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
324ca987d46SWarner Losh     }
325ca987d46SWarner Losh 
326ca987d46SWarner Losh     /*
327ca987d46SWarner Losh      * If we are booting off of a BIOS disk and we didn't succeed in determining
328ca987d46SWarner Losh      * which one we booted off of, just use disk0: as a reasonable default.
329ca987d46SWarner Losh      */
330cdff1036SToomas Soome     if ((new_currdev.dd.d_dev->dv_type == bioshd.dv_type) &&
331de04d704SWarner Losh 	((new_currdev.dd.d_unit = bd_bios2unit(biosdev)) == -1)) {
332ca987d46SWarner Losh 	printf("Can't work out which disk we are booting from.\n"
333ca987d46SWarner Losh 	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
334de04d704SWarner Losh 	new_currdev.dd.d_unit = 0;
335ca987d46SWarner Losh     }
336ca987d46SWarner Losh 
337ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
338ad00892fSWarner Losh     if (new_currdev.dd.d_dev->dv_type == DEVT_ZFS)
339ca987d46SWarner Losh 	init_zfs_bootenv(zfs_fmtdev(&new_currdev));
340ca987d46SWarner Losh #endif
341ca987d46SWarner Losh 
342ca987d46SWarner Losh     env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
343ca987d46SWarner Losh 	       i386_setcurrdev, env_nounset);
344ca987d46SWarner Losh     env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
345ca987d46SWarner Losh 	       env_nounset);
346ca987d46SWarner Losh }
347ca987d46SWarner Losh 
348ca987d46SWarner Losh COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
349ca987d46SWarner Losh 
350ca987d46SWarner Losh static int
351ca987d46SWarner Losh command_reboot(int argc, char *argv[])
352ca987d46SWarner Losh {
353ca987d46SWarner Losh     int i;
354ca987d46SWarner Losh 
355ca987d46SWarner Losh     for (i = 0; devsw[i] != NULL; ++i)
356ca987d46SWarner Losh 	if (devsw[i]->dv_cleanup != NULL)
357ca987d46SWarner Losh 	    (devsw[i]->dv_cleanup)();
358ca987d46SWarner Losh 
359ca987d46SWarner Losh     printf("Rebooting...\n");
360ca987d46SWarner Losh     delay(1000000);
361ca987d46SWarner Losh     __exit(0);
362ca987d46SWarner Losh }
363ca987d46SWarner Losh 
364ca987d46SWarner Losh /* provide this for panic, as it's not in the startup code */
365ca987d46SWarner Losh void
366ca987d46SWarner Losh exit(int code)
367ca987d46SWarner Losh {
368ca987d46SWarner Losh     __exit(code);
369ca987d46SWarner Losh }
370ca987d46SWarner Losh 
371ca987d46SWarner Losh COMMAND_SET(heap, "heap", "show heap usage", command_heap);
372ca987d46SWarner Losh 
373ca987d46SWarner Losh static int
374ca987d46SWarner Losh command_heap(int argc, char *argv[])
375ca987d46SWarner Losh {
376ca987d46SWarner Losh     mallocstats();
377ca987d46SWarner Losh     printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
378ca987d46SWarner Losh       sbrk(0), heap_top);
379ca987d46SWarner Losh     return(CMD_OK);
380ca987d46SWarner Losh }
381ca987d46SWarner Losh 
382ca987d46SWarner Losh /* ISA bus access functions for PnP. */
383ca987d46SWarner Losh static int
384ca987d46SWarner Losh isa_inb(int port)
385ca987d46SWarner Losh {
386ca987d46SWarner Losh 
387ca987d46SWarner Losh     return (inb(port));
388ca987d46SWarner Losh }
389ca987d46SWarner Losh 
390ca987d46SWarner Losh static void
391ca987d46SWarner Losh isa_outb(int port, int value)
392ca987d46SWarner Losh {
393ca987d46SWarner Losh 
394ca987d46SWarner Losh     outb(port, value);
395ca987d46SWarner Losh }
396ca987d46SWarner Losh 
397ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
398ca987d46SWarner Losh static void
399ca987d46SWarner Losh i386_zfs_probe(void)
400ca987d46SWarner Losh {
401ca987d46SWarner Losh     char devname[32];
402cdff1036SToomas Soome     struct i386_devdesc dev;
403ca987d46SWarner Losh 
404ca987d46SWarner Losh     /*
405ca987d46SWarner Losh      * Open all the disks we can find and see if we can reconstruct
406ca987d46SWarner Losh      * ZFS pools from them.
407ca987d46SWarner Losh      */
408cdff1036SToomas Soome     dev.dd.d_dev = &bioshd;
409cdff1036SToomas Soome     for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
410cdff1036SToomas Soome 	snprintf(devname, sizeof(devname), "%s%d:", bioshd.dv_name,
411cdff1036SToomas Soome 	    dev.dd.d_unit);
412ca987d46SWarner Losh 	zfs_probe_dev(devname, NULL);
413ca987d46SWarner Losh     }
414ca987d46SWarner Losh }
415ca987d46SWarner Losh #endif
416