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