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