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