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