1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30/*
31 * MD bootstrap main() and assorted miscellaneous
32 * commands.
33 */
34
35#include <stand.h>
36#include <stddef.h>
37#include <string.h>
38#include <machine/bootinfo.h>
39#include <machine/cpufunc.h>
40#include <machine/psl.h>
41#include <sys/disk.h>
42#include <sys/reboot.h>
43#include <common/drv.h>
44
45#include "bootstrap.h"
46#include "common/bootargs.h"
47#include "libi386/libi386.h"
48#include <smbios.h>
49#include "btxv86.h"
50
51#ifdef LOADER_ZFS_SUPPORT
52#include <sys/zfs_bootenv.h>
53#include "libzfs.h"
54#endif
55
56CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
57CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
58CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
59CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
60
61/* Arguments passed in from the boot1/boot2 loader */
62static struct bootargs *kargs;
63
64static uint32_t		initial_howto;
65static uint32_t		initial_bootdev;
66static struct bootinfo	*initial_bootinfo;
67
68struct arch_switch	archsw;		/* MI/MD interface boundary */
69
70static void		extract_currdev(void);
71static int		isa_inb(int port);
72static void		isa_outb(int port, int value);
73void			exit(int code);
74#ifdef LOADER_GELI_SUPPORT
75#include "geliboot.h"
76struct geli_boot_args	*gargs;
77struct geli_boot_data	*gbdata;
78#endif
79#ifdef LOADER_ZFS_SUPPORT
80struct zfs_boot_args	*zargs;
81static void		i386_zfs_probe(void);
82#endif
83
84/* XXX debugging */
85extern char end[];
86
87static void *heap_top;
88static void *heap_bottom;
89
90caddr_t
91ptov(uintptr_t x)
92{
93	return (PTOV(x));
94}
95
96int
97main(void)
98{
99	int	i;
100
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;
107
108	/* Initialize the v86 register set to a known-good state. */
109	bzero(&v86, sizeof(v86));
110	v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
111
112	/*
113	 * Initialise the heap as early as possible.
114	 * Once this is done, malloc() is usable.
115	 */
116	bios_getmem();
117
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;
125	} else
126#endif
127	{
128		heap_top = (void *)PTOV(bios_basemem);
129		heap_bottom = (void *)end;
130	}
131	setheap(heap_bottom, heap_top);
132
133	/*
134	 * detect ACPI for future reference. This may set console to comconsole
135	 * if we do have ACPI SPCR table.
136	 */
137	biosacpi_detect();
138
139	/*
140	 * XXX Chicken-and-egg problem; we want to have console output early,
141	 * but some console attributes may depend on reading from eg. the boot
142	 * device, which we can't do yet.
143	 *
144	 * We can use printf() etc. once this is done.
145	 * If the previous boot stage has requested a serial console,
146	 * prefer that.
147	 */
148	bi_setboothowto(initial_howto);
149	if (initial_howto & RB_MULTIPLE) {
150		if (initial_howto & RB_SERIAL)
151			setenv("console", "comconsole vidconsole", 1);
152		else
153			setenv("console", "vidconsole comconsole", 1);
154	} else if (initial_howto & RB_SERIAL) {
155		setenv("console", "comconsole", 1);
156	} else if (initial_howto & RB_MUTE) {
157		setenv("console", "nullconsole", 1);
158	}
159	cons_probe();
160
161	/*
162	 * Initialise the block cache. Set the upper limit.
163	 */
164	bcache_init(32768, 512);
165
166	/*
167	 * Special handling for PXE and CD booting.
168	 */
169	if (kargs->bootinfo == 0) {
170		/*
171		 * We only want the PXE disk to try to init itself in the below
172		 * walk through devsw if we actually booted off of PXE.
173		 */
174		if (kargs->bootflags & KARGS_FLAGS_PXE)
175			pxe_enable(kargs->pxeinfo ?
176			    PTOV(kargs->pxeinfo) : NULL);
177		else if (kargs->bootflags & KARGS_FLAGS_CD)
178			bc_add(initial_bootdev);
179	}
180
181	archsw.arch_autoload = i386_autoload;
182	archsw.arch_getdev = i386_getdev;
183	archsw.arch_copyin = i386_copyin;
184	archsw.arch_copyout = i386_copyout;
185	archsw.arch_readin = i386_readin;
186	archsw.arch_isainb = isa_inb;
187	archsw.arch_isaoutb = isa_outb;
188	archsw.arch_hypervisor = x86_hypervisor;
189#ifdef LOADER_ZFS_SUPPORT
190	archsw.arch_zfs_probe = i386_zfs_probe;
191
192	/*
193	 * zfsboot and gptzfsboot have always passed KARGS_FLAGS_ZFS,
194	 * so if that is set along with KARGS_FLAGS_EXTARG we know we
195	 * can interpret the extarg data as a struct zfs_boot_args.
196	 */
197#define	KARGS_EXTARGS_ZFS	(KARGS_FLAGS_EXTARG | KARGS_FLAGS_ZFS)
198
199	if ((kargs->bootflags & KARGS_EXTARGS_ZFS) == KARGS_EXTARGS_ZFS) {
200		zargs = (struct zfs_boot_args *)(kargs + 1);
201	}
202#endif /* LOADER_ZFS_SUPPORT */
203
204#ifdef LOADER_GELI_SUPPORT
205	/*
206	 * If we decided earlier that we have zfs_boot_args extarg data,
207	 * and it is big enough to contain the embedded geli data
208	 * (the early zfs_boot_args structs weren't), then init the gbdata
209	 * pointer accordingly. If there is extarg data which isn't
210	 * zfs_boot_args data, determine whether it is geli_boot_args data.
211	 * Recent versions of gptboot set KARGS_FLAGS_GELI to indicate that.
212	 * Earlier versions didn't, but we presume that's what we
213	 * have if the extarg size exactly matches the size of the
214	 * geli_boot_args struct during that pre-flag era.
215	 */
216#define	LEGACY_GELI_ARGS_SIZE	260	/* This can never change */
217
218#ifdef LOADER_ZFS_SUPPORT
219	if (zargs != NULL) {
220		if (zargs->size > offsetof(struct zfs_boot_args, gelidata)) {
221			gbdata = &zargs->gelidata;
222		}
223	} else
224#endif /* LOADER_ZFS_SUPPORT */
225	if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0) {
226		gargs = (struct geli_boot_args *)(kargs + 1);
227		if ((kargs->bootflags & KARGS_FLAGS_GELI) ||
228		    gargs->size == LEGACY_GELI_ARGS_SIZE) {
229			gbdata = &gargs->gelidata;
230		}
231	}
232
233	if (gbdata != NULL)
234		import_geli_boot_data(gbdata);
235#endif /* LOADER_GELI_SUPPORT */
236
237	/*
238	 * March through the device switch probing for things.
239	 */
240	for (i = 0; devsw[i] != NULL; i++)
241		if (devsw[i]->dv_init != NULL)
242			(devsw[i]->dv_init)();
243
244	printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024,
245	    bios_extmem / 1024);
246	if (initial_bootinfo != NULL) {
247		initial_bootinfo->bi_basemem = bios_basemem / 1024;
248		initial_bootinfo->bi_extmem = bios_extmem / 1024;
249	}
250
251	/* detect SMBIOS for future reference */
252	smbios_detect(NULL);
253
254	/* detect PCI BIOS for future reference */
255	biospci_detect();
256
257	printf("\n%s", bootprog_info);
258
259	extract_currdev();		/* set $currdev and $loaddev */
260	autoload_font(true);
261
262	bios_getsmap();
263
264	interact();
265
266	/* if we ever get here, it is an error */
267	return (1);
268}
269
270/*
271 * Set the 'current device' by (if possible) recovering the boot device as
272 * supplied by the initial bootstrap.
273 *
274 * XXX should be extended for netbooting.
275 */
276static void
277extract_currdev(void)
278{
279	struct i386_devdesc	new_currdev;
280#ifdef LOADER_ZFS_SUPPORT
281	char			buf[20];
282	char			*bootonce;
283#endif
284	int			biosdev = -1;
285
286	/* Assume we are booting from a BIOS disk by default */
287	new_currdev.dd.d_dev = &bioshd;
288
289	/* new-style boot loaders such as pxeldr and cdldr */
290	if (kargs->bootinfo == 0) {
291		if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
292			/* we are booting from a CD with cdboot */
293			new_currdev.dd.d_dev = &bioscd;
294			new_currdev.dd.d_unit = bd_bios2unit(initial_bootdev);
295		} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
296			/* we are booting from pxeldr */
297			new_currdev.dd.d_dev = &pxedisk;
298			new_currdev.dd.d_unit = 0;
299		} else {
300			/* we don't know what our boot device is */
301			new_currdev.d_kind.biosdisk.slice = -1;
302			new_currdev.d_kind.biosdisk.partition = 0;
303			biosdev = -1;
304		}
305#ifdef LOADER_ZFS_SUPPORT
306	} else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
307		/*
308		 * zargs was set in main() if we have new style extended
309		 * argument
310		 */
311		if (zargs != NULL &&
312		    zargs->size >=
313		    offsetof(struct zfs_boot_args, primary_pool)) {
314			/* sufficient data is provided */
315			new_currdev.d_kind.zfs.pool_guid = zargs->pool;
316			new_currdev.d_kind.zfs.root_guid = zargs->root;
317			if (zargs->size >= sizeof(*zargs) &&
318			    zargs->primary_vdev != 0) {
319				sprintf(buf, "%llu", zargs->primary_pool);
320				setenv("vfs.zfs.boot.primary_pool", buf, 1);
321				sprintf(buf, "%llu", zargs->primary_vdev);
322				setenv("vfs.zfs.boot.primary_vdev", buf, 1);
323			}
324		} else {
325			/* old style zfsboot block */
326			new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
327			new_currdev.d_kind.zfs.root_guid = 0;
328		}
329		new_currdev.dd.d_dev = &zfs_dev;
330
331		if ((bootonce = malloc(VDEV_PAD_SIZE)) != NULL) {
332			if (zfs_get_bootonce(&new_currdev, OS_BOOTONCE_USED,
333			    bootonce, VDEV_PAD_SIZE) == 0) {
334				setenv("zfs-bootonce", bootonce, 1);
335			}
336			free(bootonce);
337			(void) zfs_attach_nvstore(&new_currdev);
338		}
339
340#endif
341	} else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
342		/* The passed-in boot device is bad */
343		new_currdev.d_kind.biosdisk.slice = -1;
344		new_currdev.d_kind.biosdisk.partition = 0;
345		biosdev = -1;
346	} else {
347		new_currdev.d_kind.biosdisk.slice =
348		    B_SLICE(initial_bootdev) - 1;
349		new_currdev.d_kind.biosdisk.partition =
350		    B_PARTITION(initial_bootdev);
351		biosdev = initial_bootinfo->bi_bios_dev;
352
353		/*
354		 * If we are booted by an old bootstrap, we have to guess at
355		 * the BIOS unit number. We will lose if there is more than
356		 * one disk type and we are not booting from the
357		 * lowest-numbered disk type (ie. SCSI when IDE also exists).
358		 */
359		if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {
360			/*
361			 * biosdev doesn't match major, assume harddisk
362			 */
363			biosdev = 0x80 + B_UNIT(initial_bootdev);
364		}
365	}
366
367	/*
368	 * If we are booting off of a BIOS disk and we didn't succeed
369	 * in determining which one we booted off of, just use disk0:
370	 * as a reasonable default.
371	 */
372	if ((new_currdev.dd.d_dev->dv_type == bioshd.dv_type) &&
373	    ((new_currdev.dd.d_unit = bd_bios2unit(biosdev)) == -1)) {
374		printf("Can't work out which disk we are booting "
375		    "from.\nGuessed BIOS device 0x%x not found by "
376		    "probes, defaulting to disk0:\n", biosdev);
377		new_currdev.dd.d_unit = 0;
378	}
379
380#ifdef LOADER_ZFS_SUPPORT
381	if (new_currdev.dd.d_dev->dv_type == DEVT_ZFS)
382		init_zfs_boot_options(zfs_fmtdev(&new_currdev));
383#endif
384
385	env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
386	    i386_setcurrdev, env_nounset);
387	env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev),
388	    env_noset, env_nounset);
389}
390
391COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
392
393static int
394command_reboot(int argc, char *argv[])
395{
396	int i;
397
398	for (i = 0; devsw[i] != NULL; ++i)
399		if (devsw[i]->dv_cleanup != NULL)
400			(devsw[i]->dv_cleanup)();
401
402	printf("Rebooting...\n");
403	delay(1000000);
404	__exit(0);
405}
406
407/* provide this for panic, as it's not in the startup code */
408void
409exit(int code)
410{
411	__exit(code);
412}
413
414COMMAND_SET(heap, "heap", "show heap usage", command_heap);
415
416static int
417command_heap(int argc, char *argv[])
418{
419	mallocstats();
420	printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
421	    sbrk(0), heap_top);
422	return (CMD_OK);
423}
424
425/* ISA bus access functions for PnP. */
426static int
427isa_inb(int port)
428{
429
430	return (inb(port));
431}
432
433static void
434isa_outb(int port, int value)
435{
436
437	outb(port, value);
438}
439
440#ifdef LOADER_ZFS_SUPPORT
441static void
442i386_zfs_probe(void)
443{
444	char devname[32];
445	struct i386_devdesc dev;
446
447	/*
448	 * Open all the disks we can find and see if we can reconstruct
449	 * ZFS pools from them.
450	 */
451	dev.dd.d_dev = &bioshd;
452	for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
453		snprintf(devname, sizeof(devname), "%s%d:", bioshd.dv_name,
454		    dev.dd.d_unit);
455		zfs_probe_dev(devname, NULL);
456	}
457}
458#endif
459