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