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