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