main.c revision 235154
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: head/sys/boot/i386/loader/main.c 235154 2012-05-09 08:04:29Z avg $"); 29 30/* 31 * MD bootstrap main() and assorted miscellaneous 32 * commands. 33 */ 34 35#include <stand.h> 36#include <string.h> 37#include <machine/bootinfo.h> 38#include <machine/psl.h> 39#include <sys/reboot.h> 40 41#include "bootstrap.h" 42#include "common/bootargs.h" 43#include "libi386/libi386.h" 44#include "btxv86.h" 45 46CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE); 47CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO); 48CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS); 49CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE); 50 51/* Arguments passed in from the boot1/boot2 loader */ 52static struct bootargs *kargs; 53 54static u_int32_t initial_howto; 55static u_int32_t initial_bootdev; 56static struct bootinfo *initial_bootinfo; 57 58struct arch_switch archsw; /* MI/MD interface boundary */ 59 60static void extract_currdev(void); 61static int isa_inb(int port); 62static void isa_outb(int port, int value); 63void exit(int code); 64 65/* from vers.c */ 66extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 67 68/* XXX debugging */ 69extern char end[]; 70 71static void *heap_top; 72static void *heap_bottom; 73 74int 75main(void) 76{ 77 int i; 78 79 /* Pick up arguments */ 80 kargs = (void *)__args; 81 initial_howto = kargs->howto; 82 initial_bootdev = kargs->bootdev; 83 initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL; 84 85 /* Initialize the v86 register set to a known-good state. */ 86 bzero(&v86, sizeof(v86)); 87 v86.efl = PSL_RESERVED_DEFAULT | PSL_I; 88 89 /* 90 * Initialise the heap as early as possible. Once this is done, malloc() is usable. 91 */ 92 bios_getmem(); 93 94#if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \ 95 defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT) 96 if (high_heap_size > 0) { 97 heap_top = PTOV(high_heap_base + high_heap_size); 98 heap_bottom = PTOV(high_heap_base); 99 if (high_heap_base < memtop_copyin) 100 memtop_copyin = high_heap_base; 101 } else 102#endif 103 { 104 heap_top = (void *)PTOV(bios_basemem); 105 heap_bottom = (void *)end; 106 } 107 setheap(heap_bottom, heap_top); 108 109 /* 110 * XXX Chicken-and-egg problem; we want to have console output early, but some 111 * console attributes may depend on reading from eg. the boot device, which we 112 * can't do yet. 113 * 114 * We can use printf() etc. once this is done. 115 * If the previous boot stage has requested a serial console, prefer that. 116 */ 117 bi_setboothowto(initial_howto); 118 if (initial_howto & RB_MULTIPLE) { 119 if (initial_howto & RB_SERIAL) 120 setenv("console", "comconsole vidconsole", 1); 121 else 122 setenv("console", "vidconsole comconsole", 1); 123 } else if (initial_howto & RB_SERIAL) 124 setenv("console", "comconsole", 1); 125 else if (initial_howto & RB_MUTE) 126 setenv("console", "nullconsole", 1); 127 cons_probe(); 128 129 /* 130 * Initialise the block cache 131 */ 132 bcache_init(32, 512); /* 16k cache XXX tune this */ 133 134 /* 135 * Special handling for PXE and CD booting. 136 */ 137 if (kargs->bootinfo == 0) { 138 /* 139 * We only want the PXE disk to try to init itself in the below 140 * walk through devsw if we actually booted off of PXE. 141 */ 142 if (kargs->bootflags & KARGS_FLAGS_PXE) 143 pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL); 144 else if (kargs->bootflags & KARGS_FLAGS_CD) 145 bc_add(initial_bootdev); 146 } 147 148 archsw.arch_autoload = i386_autoload; 149 archsw.arch_getdev = i386_getdev; 150 archsw.arch_copyin = i386_copyin; 151 archsw.arch_copyout = i386_copyout; 152 archsw.arch_readin = i386_readin; 153 archsw.arch_isainb = isa_inb; 154 archsw.arch_isaoutb = isa_outb; 155 156 /* 157 * March through the device switch probing for things. 158 */ 159 for (i = 0; devsw[i] != NULL; i++) 160 if (devsw[i]->dv_init != NULL) 161 (devsw[i]->dv_init)(); 162 printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 163 if (initial_bootinfo != NULL) { 164 initial_bootinfo->bi_basemem = bios_basemem / 1024; 165 initial_bootinfo->bi_extmem = bios_extmem / 1024; 166 } 167 168 /* detect ACPI for future reference */ 169 biosacpi_detect(); 170 171 /* detect SMBIOS for future reference */ 172 smbios_detect(); 173 174 printf("\n"); 175 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 176 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 177 178 extract_currdev(); /* set $currdev and $loaddev */ 179 setenv("LINES", "24", 1); /* optional */ 180 181 bios_getsmap(); 182 183 interact(); /* doesn't return */ 184 185 /* if we ever get here, it is an error */ 186 return (1); 187} 188 189/* 190 * Set the 'current device' by (if possible) recovering the boot device as 191 * supplied by the initial bootstrap. 192 * 193 * XXX should be extended for netbooting. 194 */ 195static void 196extract_currdev(void) 197{ 198 struct i386_devdesc new_currdev; 199 int biosdev = -1; 200 201 /* Assume we are booting from a BIOS disk by default */ 202 new_currdev.d_dev = &biosdisk; 203 204 /* new-style boot loaders such as pxeldr and cdldr */ 205 if (kargs->bootinfo == 0) { 206 if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) { 207 /* we are booting from a CD with cdboot */ 208 new_currdev.d_dev = &bioscd; 209 new_currdev.d_unit = bc_bios2unit(initial_bootdev); 210 } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) { 211 /* we are booting from pxeldr */ 212 new_currdev.d_dev = &pxedisk; 213 new_currdev.d_unit = 0; 214 } else { 215 /* we don't know what our boot device is */ 216 new_currdev.d_kind.biosdisk.slice = -1; 217 new_currdev.d_kind.biosdisk.partition = 0; 218 biosdev = -1; 219 } 220 } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 221 /* The passed-in boot device is bad */ 222 new_currdev.d_kind.biosdisk.slice = -1; 223 new_currdev.d_kind.biosdisk.partition = 0; 224 biosdev = -1; 225 } else { 226 new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1; 227 new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 228 biosdev = initial_bootinfo->bi_bios_dev; 229 230 /* 231 * If we are booted by an old bootstrap, we have to guess at the BIOS 232 * unit number. We will lose if there is more than one disk type 233 * and we are not booting from the lowest-numbered disk type 234 * (ie. SCSI when IDE also exists). 235 */ 236 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 237 biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 238 } 239 new_currdev.d_type = new_currdev.d_dev->dv_type; 240 241 /* 242 * If we are booting off of a BIOS disk and we didn't succeed in determining 243 * which one we booted off of, just use disk0: as a reasonable default. 244 */ 245 if ((new_currdev.d_type == biosdisk.dv_type) && 246 ((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) { 247 printf("Can't work out which disk we are booting from.\n" 248 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 249 new_currdev.d_unit = 0; 250 } 251 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev), 252 i386_setcurrdev, env_nounset); 253 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset, 254 env_nounset); 255 256#ifdef LOADER_ZFS_SUPPORT 257 /* 258 * If we were started from a ZFS-aware boot2, we can work out 259 * which ZFS pool we are booting from. 260 */ 261 if (kargs->bootflags & KARGS_FLAGS_ZFS) { 262 /* 263 * Dig out the pool guid and convert it to a 'unit number' 264 */ 265 uint64_t guid; 266 int unit; 267 char devname[32]; 268 extern int zfs_guid_to_unit(uint64_t); 269 270 guid = kargs->zfspool; 271 unit = zfs_guid_to_unit(guid); 272 if (unit >= 0) { 273 sprintf(devname, "zfs%d", unit); 274 setenv("currdev", devname, 1); 275 } 276 } 277#endif 278} 279 280COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 281 282static int 283command_reboot(int argc, char *argv[]) 284{ 285 int i; 286 287 for (i = 0; devsw[i] != NULL; ++i) 288 if (devsw[i]->dv_cleanup != NULL) 289 (devsw[i]->dv_cleanup)(); 290 291 printf("Rebooting...\n"); 292 delay(1000000); 293 __exit(0); 294} 295 296/* provide this for panic, as it's not in the startup code */ 297void 298exit(int code) 299{ 300 __exit(code); 301} 302 303COMMAND_SET(heap, "heap", "show heap usage", command_heap); 304 305static int 306command_heap(int argc, char *argv[]) 307{ 308 mallocstats(); 309 printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom, 310 sbrk(0), heap_top); 311 return(CMD_OK); 312} 313 314/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 315static int 316isa_inb(int port) 317{ 318 u_char data; 319 320 if (__builtin_constant_p(port) && 321 (((port) & 0xffff) < 0x100) && 322 ((port) < 0x10000)) { 323 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 324 } else { 325 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 326 } 327 return(data); 328} 329 330static void 331isa_outb(int port, int value) 332{ 333 u_char al = value; 334 335 if (__builtin_constant_p(port) && 336 (((port) & 0xffff) < 0x100) && 337 ((port) < 0x10000)) { 338 __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 339 } else { 340 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 341 } 342} 343 344