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