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