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