xref: /freebsd/stand/i386/loader/main.c (revision edb26097cb6416adb89b54c830d36c0ead3ca64c)
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"
48a64f0b83SWarner Losh #include <smbios.h>
49ca987d46SWarner Losh #include "btxv86.h"
50ca987d46SWarner Losh 
51ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
521dc762d4SToomas Soome #include <sys/zfs_bootenv.h>
53007b82d7SWarner Losh #include "libzfs.h"
54ca987d46SWarner Losh #endif
55ca987d46SWarner Losh 
56ca987d46SWarner Losh CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
57ca987d46SWarner Losh CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
58ca987d46SWarner Losh CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
59ca987d46SWarner Losh CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
60ca987d46SWarner Losh 
61ca987d46SWarner Losh /* Arguments passed in from the boot1/boot2 loader */
62ca987d46SWarner Losh static struct bootargs *kargs;
63ca987d46SWarner Losh 
6456e53cb8SWarner Losh static uint32_t		initial_howto;
6556e53cb8SWarner Losh static uint32_t		initial_bootdev;
66ca987d46SWarner Losh static struct bootinfo	*initial_bootinfo;
67ca987d46SWarner Losh 
68ca987d46SWarner Losh struct arch_switch	archsw;		/* MI/MD interface boundary */
69ca987d46SWarner Losh 
70ca987d46SWarner Losh static void		extract_currdev(void);
71ca987d46SWarner Losh static int		isa_inb(int port);
72ca987d46SWarner Losh static void		isa_outb(int port, int value);
73ca987d46SWarner Losh void			exit(int code);
74ca987d46SWarner Losh #ifdef LOADER_GELI_SUPPORT
75ca987d46SWarner Losh #include "geliboot.h"
76ca987d46SWarner Losh struct geli_boot_args	*gargs;
777297dc44SIan Lepore struct geli_boot_data	*gbdata;
78ca987d46SWarner Losh #endif
79ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
80ca987d46SWarner Losh struct zfs_boot_args	*zargs;
81ca987d46SWarner Losh static void		i386_zfs_probe(void);
82ca987d46SWarner Losh #endif
83ca987d46SWarner Losh 
84ca987d46SWarner Losh /* XXX debugging */
85ca987d46SWarner Losh extern char end[];
86ca987d46SWarner Losh 
87ca987d46SWarner Losh static void *heap_top;
88ca987d46SWarner Losh static void *heap_bottom;
89ca987d46SWarner Losh 
90ed2a6576SWarner Losh caddr_t
91ed2a6576SWarner Losh ptov(uintptr_t x)
92ed2a6576SWarner Losh {
93ed2a6576SWarner Losh 	return (PTOV(x));
94ed2a6576SWarner Losh }
95ed2a6576SWarner Losh 
96ca987d46SWarner Losh int
97ca987d46SWarner Losh main(void)
98ca987d46SWarner Losh {
99ca987d46SWarner Losh 	int	i;
100ca987d46SWarner Losh 
101ca987d46SWarner Losh 	/* Pick up arguments */
102ca987d46SWarner Losh 	kargs = (void *)__args;
103ca987d46SWarner Losh 	initial_howto = kargs->howto;
104ca987d46SWarner Losh 	initial_bootdev = kargs->bootdev;
10597dd57e6SToomas Soome 	initial_bootinfo = kargs->bootinfo ?
10697dd57e6SToomas Soome 	    (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
107ca987d46SWarner Losh 
108ca987d46SWarner Losh 	/* Initialize the v86 register set to a known-good state. */
109ca987d46SWarner Losh 	bzero(&v86, sizeof(v86));
110ca987d46SWarner Losh 	v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
111ca987d46SWarner Losh 
112ca987d46SWarner Losh 	/*
11397dd57e6SToomas Soome 	 * Initialise the heap as early as possible.
11497dd57e6SToomas Soome 	 * Once this is done, malloc() is usable.
115ca987d46SWarner Losh 	 */
116ca987d46SWarner Losh 	bios_getmem();
117ca987d46SWarner Losh 
118ca987d46SWarner Losh #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
119ca987d46SWarner Losh     defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
120ca987d46SWarner Losh 	if (high_heap_size > 0) {
121ca987d46SWarner Losh 		heap_top = PTOV(high_heap_base + high_heap_size);
122ca987d46SWarner Losh 		heap_bottom = PTOV(high_heap_base);
123ca987d46SWarner Losh 		if (high_heap_base < memtop_copyin)
124ca987d46SWarner Losh 			memtop_copyin = high_heap_base;
125ca987d46SWarner Losh 	} else
126ca987d46SWarner Losh #endif
127ca987d46SWarner Losh 	{
128ca987d46SWarner Losh 		heap_top = (void *)PTOV(bios_basemem);
129ca987d46SWarner Losh 		heap_bottom = (void *)end;
130ca987d46SWarner Losh 	}
131ca987d46SWarner Losh 	setheap(heap_bottom, heap_top);
132ca987d46SWarner Losh 
133ca987d46SWarner Losh 	/*
134c4b65e95SColin Percival 	 * Now that malloc is usable, allocate a buffer for tslog and start
135c4b65e95SColin Percival 	 * logging timestamps during the boot process.
136c4b65e95SColin Percival 	 */
137c4b65e95SColin Percival 	tslog_init();
138c4b65e95SColin Percival 
139c4b65e95SColin Percival 	/*
1403630506bSToomas Soome 	 * detect ACPI for future reference. This may set console to comconsole
1413630506bSToomas Soome 	 * if we do have ACPI SPCR table.
1423630506bSToomas Soome 	 */
1433630506bSToomas Soome 	biosacpi_detect();
1443630506bSToomas Soome 
1453630506bSToomas Soome 	/*
14697dd57e6SToomas Soome 	 * XXX Chicken-and-egg problem; we want to have console output early,
14797dd57e6SToomas Soome 	 * but some console attributes may depend on reading from eg. the boot
14897dd57e6SToomas Soome 	 * device, which we can't do yet.
149ca987d46SWarner Losh 	 *
150ca987d46SWarner Losh 	 * We can use printf() etc. once this is done.
15197dd57e6SToomas Soome 	 * If the previous boot stage has requested a serial console,
15297dd57e6SToomas Soome 	 * prefer that.
153ca987d46SWarner Losh 	 */
154ca987d46SWarner Losh 	bi_setboothowto(initial_howto);
155ca987d46SWarner Losh 	if (initial_howto & RB_MULTIPLE) {
156ca987d46SWarner Losh 		if (initial_howto & RB_SERIAL)
157ca987d46SWarner Losh 			setenv("console", "comconsole vidconsole", 1);
158ca987d46SWarner Losh 		else
159ca987d46SWarner Losh 			setenv("console", "vidconsole comconsole", 1);
16097dd57e6SToomas Soome 	} else if (initial_howto & RB_SERIAL) {
161ca987d46SWarner Losh 		setenv("console", "comconsole", 1);
16297dd57e6SToomas Soome 	} else if (initial_howto & RB_MUTE) {
163ca987d46SWarner Losh 		setenv("console", "nullconsole", 1);
16497dd57e6SToomas Soome 	}
165ca987d46SWarner Losh 	cons_probe();
166ca987d46SWarner Losh 
167b4cb3fe0SToomas Soome 	/* Set up currdev variable to have hooks in place. */
168b4cb3fe0SToomas Soome 	env_setenv("currdev", EV_VOLATILE | EV_NOHOOK, "",
169b4cb3fe0SToomas Soome 	    i386_setcurrdev, env_nounset);
170b4cb3fe0SToomas Soome 
171ca987d46SWarner Losh 	/*
172ca987d46SWarner Losh 	 * Initialise the block cache. Set the upper limit.
173ca987d46SWarner Losh 	 */
174ca987d46SWarner Losh 	bcache_init(32768, 512);
175ca987d46SWarner Losh 
176ca987d46SWarner Losh 	/*
177ca987d46SWarner Losh 	 * Special handling for PXE and CD booting.
178ca987d46SWarner Losh 	 */
179ca987d46SWarner Losh 	if (kargs->bootinfo == 0) {
180ca987d46SWarner Losh 		/*
181ca987d46SWarner Losh 		 * We only want the PXE disk to try to init itself in the below
182ca987d46SWarner Losh 		 * walk through devsw if we actually booted off of PXE.
183ca987d46SWarner Losh 		 */
184ca987d46SWarner Losh 		if (kargs->bootflags & KARGS_FLAGS_PXE)
18597dd57e6SToomas Soome 			pxe_enable(kargs->pxeinfo ?
18697dd57e6SToomas Soome 			    PTOV(kargs->pxeinfo) : NULL);
187ca987d46SWarner Losh 		else if (kargs->bootflags & KARGS_FLAGS_CD)
188ca987d46SWarner Losh 			bc_add(initial_bootdev);
189ca987d46SWarner Losh 	}
190ca987d46SWarner Losh 
191ca987d46SWarner Losh 	archsw.arch_autoload = i386_autoload;
192ca987d46SWarner Losh 	archsw.arch_getdev = i386_getdev;
193ca987d46SWarner Losh 	archsw.arch_copyin = i386_copyin;
194ca987d46SWarner Losh 	archsw.arch_copyout = i386_copyout;
195ca987d46SWarner Losh 	archsw.arch_readin = i386_readin;
196ca987d46SWarner Losh 	archsw.arch_isainb = isa_inb;
197ca987d46SWarner Losh 	archsw.arch_isaoutb = isa_outb;
198e9b148a3SSimon J. Gerraty 	archsw.arch_hypervisor = x86_hypervisor;
199ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
200ca987d46SWarner Losh 	archsw.arch_zfs_probe = i386_zfs_probe;
201ca987d46SWarner Losh 
2027297dc44SIan Lepore 	/*
20397dd57e6SToomas Soome 	 * zfsboot and gptzfsboot have always passed KARGS_FLAGS_ZFS,
20497dd57e6SToomas Soome 	 * so if that is set along with KARGS_FLAGS_EXTARG we know we
20597dd57e6SToomas Soome 	 * can interpret the extarg data as a struct zfs_boot_args.
2067297dc44SIan Lepore 	 */
2077297dc44SIan Lepore #define	KARGS_EXTARGS_ZFS	(KARGS_FLAGS_EXTARG | KARGS_FLAGS_ZFS)
2087297dc44SIan Lepore 
2097297dc44SIan Lepore 	if ((kargs->bootflags & KARGS_EXTARGS_ZFS) == KARGS_EXTARGS_ZFS) {
210ca987d46SWarner Losh 		zargs = (struct zfs_boot_args *)(kargs + 1);
211ca987d46SWarner Losh 	}
212ca987d46SWarner Losh #endif /* LOADER_ZFS_SUPPORT */
213ca987d46SWarner Losh 
2147297dc44SIan Lepore #ifdef LOADER_GELI_SUPPORT
2157297dc44SIan Lepore 	/*
21697dd57e6SToomas Soome 	 * If we decided earlier that we have zfs_boot_args extarg data,
21797dd57e6SToomas Soome 	 * and it is big enough to contain the embedded geli data
21897dd57e6SToomas Soome 	 * (the early zfs_boot_args structs weren't), then init the gbdata
21997dd57e6SToomas Soome 	 * pointer accordingly. If there is extarg data which isn't
22097dd57e6SToomas Soome 	 * zfs_boot_args data, determine whether it is geli_boot_args data.
22197dd57e6SToomas Soome 	 * Recent versions of gptboot set KARGS_FLAGS_GELI to indicate that.
22297dd57e6SToomas Soome 	 * Earlier versions didn't, but we presume that's what we
22397dd57e6SToomas Soome 	 * have if the extarg size exactly matches the size of the
22497dd57e6SToomas Soome 	 * geli_boot_args struct during that pre-flag era.
2257297dc44SIan Lepore 	 */
2267297dc44SIan Lepore #define	LEGACY_GELI_ARGS_SIZE	260	/* This can never change */
2277297dc44SIan Lepore 
2289631ae82SIan Lepore #ifdef LOADER_ZFS_SUPPORT
2297297dc44SIan Lepore 	if (zargs != NULL) {
2307297dc44SIan Lepore 		if (zargs->size > offsetof(struct zfs_boot_args, gelidata)) {
2317297dc44SIan Lepore 			gbdata = &zargs->gelidata;
2327297dc44SIan Lepore 		}
2339631ae82SIan Lepore 	} else
2349631ae82SIan Lepore #endif /* LOADER_ZFS_SUPPORT */
2359631ae82SIan Lepore 	if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0) {
2367297dc44SIan Lepore 		gargs = (struct geli_boot_args *)(kargs + 1);
2377297dc44SIan Lepore 		if ((kargs->bootflags & KARGS_FLAGS_GELI) ||
2387297dc44SIan Lepore 		    gargs->size == LEGACY_GELI_ARGS_SIZE) {
2397297dc44SIan Lepore 			gbdata = &gargs->gelidata;
2407297dc44SIan Lepore 		}
2417297dc44SIan Lepore 	}
2427297dc44SIan Lepore 
2437297dc44SIan Lepore 	if (gbdata != NULL)
2447297dc44SIan Lepore 		import_geli_boot_data(gbdata);
2457297dc44SIan Lepore #endif /* LOADER_GELI_SUPPORT */
2467297dc44SIan Lepore 
247ca987d46SWarner Losh 	/*
248ca987d46SWarner Losh 	 * March through the device switch probing for things.
249ca987d46SWarner Losh 	 */
250ca987d46SWarner Losh 	for (i = 0; devsw[i] != NULL; i++)
251ca987d46SWarner Losh 		if (devsw[i]->dv_init != NULL)
252ca987d46SWarner Losh 			(devsw[i]->dv_init)();
25397dd57e6SToomas Soome 
25497dd57e6SToomas Soome 	printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024,
25597dd57e6SToomas Soome 	    bios_extmem / 1024);
256ca987d46SWarner Losh 	if (initial_bootinfo != NULL) {
257ca987d46SWarner Losh 		initial_bootinfo->bi_basemem = bios_basemem / 1024;
258ca987d46SWarner Losh 		initial_bootinfo->bi_extmem = bios_extmem / 1024;
259ca987d46SWarner Losh 	}
260ca987d46SWarner Losh 
261ca987d46SWarner Losh 	/* detect SMBIOS for future reference */
262ca987d46SWarner Losh 	smbios_detect(NULL);
263ca987d46SWarner Losh 
264ca987d46SWarner Losh 	/* detect PCI BIOS for future reference */
265ca987d46SWarner Losh 	biospci_detect();
266ca987d46SWarner Losh 
267ca987d46SWarner Losh 	printf("\n%s", bootprog_info);
268ca987d46SWarner Losh 
269ca987d46SWarner Losh 	extract_currdev();		/* set $currdev and $loaddev */
2703630506bSToomas Soome 	autoload_font(true);
271ca987d46SWarner Losh 
272ca987d46SWarner Losh 	bios_getsmap();
273ca987d46SWarner Losh 
2746bc86037SWarner Losh 	interact();
275ca987d46SWarner Losh 
276ca987d46SWarner Losh 	/* if we ever get here, it is an error */
277ca987d46SWarner Losh 	return (1);
278ca987d46SWarner Losh }
279ca987d46SWarner Losh 
280ca987d46SWarner Losh /*
281ca987d46SWarner Losh  * Set the 'current device' by (if possible) recovering the boot device as
282ca987d46SWarner Losh  * supplied by the initial bootstrap.
283ca987d46SWarner Losh  *
284ca987d46SWarner Losh  * XXX should be extended for netbooting.
285ca987d46SWarner Losh  */
286ca987d46SWarner Losh static void
287ca987d46SWarner Losh extract_currdev(void)
288ca987d46SWarner Losh {
289ca987d46SWarner Losh 	struct i386_devdesc	new_currdev;
290ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
291ca987d46SWarner Losh 	char			buf[20];
292e307eb94SToomas Soome 	char			*bootonce;
293ca987d46SWarner Losh #endif
294ca987d46SWarner Losh 	int			biosdev = -1;
295ca987d46SWarner Losh 
296ca987d46SWarner Losh 	/* Assume we are booting from a BIOS disk by default */
297cdff1036SToomas Soome 	new_currdev.dd.d_dev = &bioshd;
298ca987d46SWarner Losh 
299ca987d46SWarner Losh 	/* new-style boot loaders such as pxeldr and cdldr */
300ca987d46SWarner Losh 	if (kargs->bootinfo == 0) {
301ca987d46SWarner Losh 		if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
302ca987d46SWarner Losh 			/* we are booting from a CD with cdboot */
303de04d704SWarner Losh 			new_currdev.dd.d_dev = &bioscd;
304cdff1036SToomas Soome 			new_currdev.dd.d_unit = bd_bios2unit(initial_bootdev);
305ca987d46SWarner Losh 		} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
306ca987d46SWarner Losh 			/* we are booting from pxeldr */
307de04d704SWarner Losh 			new_currdev.dd.d_dev = &pxedisk;
308de04d704SWarner Losh 			new_currdev.dd.d_unit = 0;
309ca987d46SWarner Losh 		} else {
310ca987d46SWarner Losh 			/* we don't know what our boot device is */
311f197c0bfSWarner Losh 			new_currdev.disk.d_slice = -1;
312f197c0bfSWarner Losh 			new_currdev.disk.d_partition = 0;
313ca987d46SWarner Losh 			biosdev = -1;
314ca987d46SWarner Losh 		}
315ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
316ca987d46SWarner Losh 	} else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
31797dd57e6SToomas Soome 		/*
31897dd57e6SToomas Soome 		 * zargs was set in main() if we have new style extended
31997dd57e6SToomas Soome 		 * argument
32097dd57e6SToomas Soome 		 */
321ca987d46SWarner Losh 		if (zargs != NULL &&
32297dd57e6SToomas Soome 		    zargs->size >=
32397dd57e6SToomas Soome 		    offsetof(struct zfs_boot_args, primary_pool)) {
324ca987d46SWarner Losh 			/* sufficient data is provided */
325f197c0bfSWarner Losh 			new_currdev.zfs.pool_guid = zargs->pool;
326f197c0bfSWarner Losh 			new_currdev.zfs.root_guid = zargs->root;
32797dd57e6SToomas Soome 			if (zargs->size >= sizeof(*zargs) &&
32897dd57e6SToomas Soome 			    zargs->primary_vdev != 0) {
329ca987d46SWarner Losh 				sprintf(buf, "%llu", zargs->primary_pool);
330ca987d46SWarner Losh 				setenv("vfs.zfs.boot.primary_pool", buf, 1);
331ca987d46SWarner Losh 				sprintf(buf, "%llu", zargs->primary_vdev);
332ca987d46SWarner Losh 				setenv("vfs.zfs.boot.primary_vdev", buf, 1);
333ca987d46SWarner Losh 			}
334ca987d46SWarner Losh 		} else {
335ca987d46SWarner Losh 			/* old style zfsboot block */
336f197c0bfSWarner Losh 			new_currdev.zfs.pool_guid = kargs->zfspool;
337f197c0bfSWarner Losh 			new_currdev.zfs.root_guid = 0;
338ca987d46SWarner Losh 		}
339de04d704SWarner Losh 		new_currdev.dd.d_dev = &zfs_dev;
340e307eb94SToomas Soome 
341e307eb94SToomas Soome 		if ((bootonce = malloc(VDEV_PAD_SIZE)) != NULL) {
342e307eb94SToomas Soome 			if (zfs_get_bootonce(&new_currdev, OS_BOOTONCE_USED,
343e307eb94SToomas Soome 			    bootonce, VDEV_PAD_SIZE) == 0) {
344e307eb94SToomas Soome 				setenv("zfs-bootonce", bootonce, 1);
345e307eb94SToomas Soome 			}
346e307eb94SToomas Soome 			free(bootonce);
347e307eb94SToomas Soome 			(void) zfs_attach_nvstore(&new_currdev);
348e307eb94SToomas Soome 		}
349e307eb94SToomas Soome 
350ca987d46SWarner Losh #endif
351ca987d46SWarner Losh 	} else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
352ca987d46SWarner Losh 		/* The passed-in boot device is bad */
353f197c0bfSWarner Losh 		new_currdev.disk.d_slice = -1;
354f197c0bfSWarner Losh 		new_currdev.disk.d_partition = 0;
355ca987d46SWarner Losh 		biosdev = -1;
356ca987d46SWarner Losh 	} else {
357f197c0bfSWarner Losh 		new_currdev.disk.d_slice = B_SLICE(initial_bootdev) - 1;
358f197c0bfSWarner Losh 		new_currdev.disk.d_partition = B_PARTITION(initial_bootdev);
359ca987d46SWarner Losh 		biosdev = initial_bootinfo->bi_bios_dev;
360ca987d46SWarner Losh 
361ca987d46SWarner Losh 		/*
36297dd57e6SToomas Soome 		 * If we are booted by an old bootstrap, we have to guess at
36397dd57e6SToomas Soome 		 * the BIOS unit number. We will lose if there is more than
36497dd57e6SToomas Soome 		 * one disk type and we are not booting from the
36597dd57e6SToomas Soome 		 * lowest-numbered disk type (ie. SCSI when IDE also exists).
366ca987d46SWarner Losh 		 */
36797dd57e6SToomas Soome 		if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {
36897dd57e6SToomas Soome 			/*
36997dd57e6SToomas Soome 			 * biosdev doesn't match major, assume harddisk
37097dd57e6SToomas Soome 			 */
37197dd57e6SToomas Soome 			biosdev = 0x80 + B_UNIT(initial_bootdev);
37297dd57e6SToomas Soome 		}
373ca987d46SWarner Losh 	}
374ca987d46SWarner Losh 
375ca987d46SWarner Losh 	/*
37697dd57e6SToomas Soome 	 * If we are booting off of a BIOS disk and we didn't succeed
37797dd57e6SToomas Soome 	 * in determining which one we booted off of, just use disk0:
37897dd57e6SToomas Soome 	 * as a reasonable default.
379ca987d46SWarner Losh 	 */
380cdff1036SToomas Soome 	if ((new_currdev.dd.d_dev->dv_type == bioshd.dv_type) &&
381de04d704SWarner Losh 	    ((new_currdev.dd.d_unit = bd_bios2unit(biosdev)) == -1)) {
38297dd57e6SToomas Soome 		printf("Can't work out which disk we are booting "
38397dd57e6SToomas Soome 		    "from.\nGuessed BIOS device 0x%x not found by "
38497dd57e6SToomas Soome 		    "probes, defaulting to disk0:\n", biosdev);
385de04d704SWarner Losh 		new_currdev.dd.d_unit = 0;
386ca987d46SWarner Losh 	}
387ca987d46SWarner Losh 
388ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
389ad00892fSWarner Losh 	if (new_currdev.dd.d_dev->dv_type == DEVT_ZFS)
390*edb26097SWarner Losh 		init_zfs_boot_options(devformat(&new_currdev.dd));
391ca987d46SWarner Losh #endif
392ca987d46SWarner Losh 
393ca987d46SWarner Losh 	env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
394ca987d46SWarner Losh 	    i386_setcurrdev, env_nounset);
39597dd57e6SToomas Soome 	env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev),
39697dd57e6SToomas Soome 	    env_noset, env_nounset);
397ca987d46SWarner Losh }
398ca987d46SWarner Losh 
399ca987d46SWarner Losh COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
400ca987d46SWarner Losh 
401ca987d46SWarner Losh static int
402ca987d46SWarner Losh command_reboot(int argc, char *argv[])
403ca987d46SWarner Losh {
404ca987d46SWarner Losh 	int i;
405ca987d46SWarner Losh 
406ca987d46SWarner Losh 	for (i = 0; devsw[i] != NULL; ++i)
407ca987d46SWarner Losh 		if (devsw[i]->dv_cleanup != NULL)
408ca987d46SWarner Losh 			(devsw[i]->dv_cleanup)();
409ca987d46SWarner Losh 
410ca987d46SWarner Losh 	printf("Rebooting...\n");
411ca987d46SWarner Losh 	delay(1000000);
412ca987d46SWarner Losh 	__exit(0);
413ca987d46SWarner Losh }
414ca987d46SWarner Losh 
415ca987d46SWarner Losh /* provide this for panic, as it's not in the startup code */
416ca987d46SWarner Losh void
417ca987d46SWarner Losh exit(int code)
418ca987d46SWarner Losh {
419ca987d46SWarner Losh 	__exit(code);
420ca987d46SWarner Losh }
421ca987d46SWarner Losh 
422ca987d46SWarner Losh COMMAND_SET(heap, "heap", "show heap usage", command_heap);
423ca987d46SWarner Losh 
424ca987d46SWarner Losh static int
425ca987d46SWarner Losh command_heap(int argc, char *argv[])
426ca987d46SWarner Losh {
427ca987d46SWarner Losh 	mallocstats();
428ca987d46SWarner Losh 	printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
429ca987d46SWarner Losh 	    sbrk(0), heap_top);
430ca987d46SWarner Losh 	return (CMD_OK);
431ca987d46SWarner Losh }
432ca987d46SWarner Losh 
433ca987d46SWarner Losh /* ISA bus access functions for PnP. */
434ca987d46SWarner Losh static int
435ca987d46SWarner Losh isa_inb(int port)
436ca987d46SWarner Losh {
437ca987d46SWarner Losh 
438ca987d46SWarner Losh 	return (inb(port));
439ca987d46SWarner Losh }
440ca987d46SWarner Losh 
441ca987d46SWarner Losh static void
442ca987d46SWarner Losh isa_outb(int port, int value)
443ca987d46SWarner Losh {
444ca987d46SWarner Losh 
445ca987d46SWarner Losh 	outb(port, value);
446ca987d46SWarner Losh }
447ca987d46SWarner Losh 
448ca987d46SWarner Losh #ifdef LOADER_ZFS_SUPPORT
449ca987d46SWarner Losh static void
450ca987d46SWarner Losh i386_zfs_probe(void)
451ca987d46SWarner Losh {
452ca987d46SWarner Losh 	char devname[32];
453cdff1036SToomas Soome 	struct i386_devdesc dev;
454ca987d46SWarner Losh 
455ca987d46SWarner Losh 	/*
456ca987d46SWarner Losh 	 * Open all the disks we can find and see if we can reconstruct
457ca987d46SWarner Losh 	 * ZFS pools from them.
458ca987d46SWarner Losh 	 */
459cdff1036SToomas Soome 	dev.dd.d_dev = &bioshd;
460cdff1036SToomas Soome 	for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
461cdff1036SToomas Soome 		snprintf(devname, sizeof(devname), "%s%d:", bioshd.dv_name,
462cdff1036SToomas Soome 		    dev.dd.d_unit);
463ca987d46SWarner Losh 		zfs_probe_dev(devname, NULL);
464ca987d46SWarner Losh 	}
465ca987d46SWarner Losh }
466ca987d46SWarner Losh #endif
467