main.c revision 86094
1235537Sgber/*- 2235537Sgber * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3235537Sgber * All rights reserved. 4235537Sgber * 5235537Sgber * Redistribution and use in source and binary forms, with or without 6235537Sgber * modification, are permitted provided that the following conditions 7235537Sgber * are met: 8235537Sgber * 1. Redistributions of source code must retain the above copyright 9235537Sgber * notice, this list of conditions and the following disclaimer. 10235537Sgber * 2. Redistributions in binary form must reproduce the above copyright 11235537Sgber * notice, this list of conditions and the following disclaimer in the 12235537Sgber * documentation and/or other materials provided with the distribution. 13235537Sgber * 14235537Sgber * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15235537Sgber * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16235537Sgber * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17235537Sgber * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18235537Sgber * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19235537Sgber * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20235537Sgber * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21235537Sgber * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22235537Sgber * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23235537Sgber * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24235537Sgber * SUCH DAMAGE. 25235537Sgber * 26235537Sgber * $FreeBSD: head/sys/boot/i386/loader/main.c 86094 2001-11-05 19:03:01Z jhb $ 27235537Sgber */ 28235537Sgber 29235537Sgber/* 30235537Sgber * MD bootstrap main() and assorted miscellaneous 31235537Sgber * commands. 32235537Sgber */ 33235537Sgber 34235537Sgber#include <stand.h> 35235537Sgber#include <string.h> 36235537Sgber#include <machine/bootinfo.h> 37235537Sgber#include <sys/reboot.h> 38235537Sgber 39235537Sgber#include "bootstrap.h" 40235537Sgber#include "libi386/libi386.h" 41235537Sgber#include "btxv86.h" 42235537Sgber 43235537Sgber#define KARGS_FLAGS_CD 0x1 44235537Sgber#define KARGS_FLAGS_PXE 0x2 45235537Sgber 46235537Sgber/* Arguments passed in from the boot1/boot2 loader */ 47235537Sgberstatic struct 48235537Sgber{ 49235537Sgber u_int32_t howto; 50235537Sgber u_int32_t bootdev; 51235537Sgber u_int32_t bootflags; 52235537Sgber u_int32_t pxeinfo; 53235537Sgber u_int32_t res2; 54235537Sgber u_int32_t bootinfo; 55235537Sgber} *kargs; 56235537Sgber 57235537Sgberstatic u_int32_t initial_howto; 58235537Sgberstatic u_int32_t initial_bootdev; 59235537Sgberstatic struct bootinfo *initial_bootinfo; 60235537Sgber 61235537Sgberstruct arch_switch archsw; /* MI/MD interface boundary */ 62235537Sgber 63235537Sgberstatic void extract_currdev(void); 64235537Sgberstatic int isa_inb(int port); 65235537Sgberstatic void isa_outb(int port, int value); 66235537Sgbervoid exit(int code); 67235537Sgber 68235537Sgber/* from vers.c */ 69235537Sgberextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 70235537Sgber 71235537Sgber/* XXX debugging */ 72235537Sgberextern char end[]; 73235537Sgber 74235537Sgberint 75235537Sgbermain(void) 76235537Sgber{ 77235537Sgber int i; 78235537Sgber 79235537Sgber /* Pick up arguments */ 80235537Sgber kargs = (void *)__args; 81235537Sgber initial_howto = kargs->howto; 82235537Sgber initial_bootdev = kargs->bootdev; 83235537Sgber initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL; 84235537Sgber 85235537Sgber /* 86235537Sgber * Initialise the heap as early as possible. Once this is done, malloc() is usable. 87235537Sgber */ 88235537Sgber bios_getmem(); 89235537Sgber 90235537Sgber setheap((void *)end, (void *)bios_basemem); 91235537Sgber 92235537Sgber /* 93235537Sgber * XXX Chicken-and-egg problem; we want to have console output early, but some 94235537Sgber * console attributes may depend on reading from eg. the boot device, which we 95235537Sgber * can't do yet. 96235537Sgber * 97235537Sgber * We can use printf() etc. once this is done. 98235537Sgber * If the previous boot stage has requested a serial console, prefer that. 99235537Sgber */ 100235537Sgber if (initial_howto & RB_SERIAL) 101235537Sgber setenv("console", "comconsole", 1); 102235537Sgber if (initial_howto & RB_MUTE) 103235537Sgber setenv("console", "nullconsole", 1); 104235537Sgber cons_probe(); 105235537Sgber 106235537Sgber /* 107235537Sgber * Initialise the block cache 108235537Sgber */ 109235537Sgber bcache_init(32, 512); /* 16k cache XXX tune this */ 110235537Sgber 111235537Sgber /* 112235537Sgber * Special handling for PXE and CD booting. 113 */ 114 if (kargs->bootinfo == NULL) { 115 /* 116 * We only want the PXE disk to try to init itself in the below 117 * walk through devsw if we actually booted off of PXE. 118 */ 119 if (kargs->bootflags & KARGS_FLAGS_PXE) 120 pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL); 121 else if (kargs->bootflags & KARGS_FLAGS_CD) 122 bc_add(initial_bootdev); 123 } 124 125 /* 126 * March through the device switch probing for things. 127 */ 128 for (i = 0; devsw[i] != NULL; i++) 129 if (devsw[i]->dv_init != NULL) 130 (devsw[i]->dv_init)(); 131 printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 132 133 /* detect ACPI for future reference */ 134 biosacpi_detect(); 135 136 printf("\n"); 137 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 138 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 139 140 extract_currdev(); /* set $currdev and $loaddev */ 141 setenv("LINES", "24", 1); /* optional */ 142 143 archsw.arch_autoload = i386_autoload; 144 archsw.arch_getdev = i386_getdev; 145 archsw.arch_copyin = i386_copyin; 146 archsw.arch_copyout = i386_copyout; 147 archsw.arch_readin = i386_readin; 148 archsw.arch_isainb = isa_inb; 149 archsw.arch_isaoutb = isa_outb; 150 151 interact(); /* doesn't return */ 152 153 /* if we ever get here, it is an error */ 154 return (1); 155} 156 157/* 158 * Set the 'current device' by (if possible) recovering the boot device as 159 * supplied by the initial bootstrap. 160 * 161 * XXX should be extended for netbooting. 162 */ 163static void 164extract_currdev(void) 165{ 166 struct i386_devdesc new_currdev; 167 int major, biosdev = -1; 168 169 /* Assume we are booting from a BIOS disk by default */ 170 new_currdev.d_dev = &biosdisk; 171 172 /* new-style boot loaders such as pxeldr and cdldr */ 173 if (kargs->bootinfo == NULL) { 174 if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) { 175 /* we are booting from a CD with cdboot */ 176 new_currdev.d_dev = &bioscd; 177 new_currdev.d_kind.bioscd.unit = bc_bios2unit(initial_bootdev); 178 } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) { 179 /* we are booting from pxeldr */ 180 new_currdev.d_dev = &pxedisk; 181 new_currdev.d_kind.netif.unit = 0; 182 } else { 183 /* we don't know what our boot device is */ 184 new_currdev.d_kind.biosdisk.slice = -1; 185 new_currdev.d_kind.biosdisk.partition = 0; 186 biosdev = -1; 187 } 188 } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 189 /* The passed-in boot device is bad */ 190 new_currdev.d_kind.biosdisk.slice = -1; 191 new_currdev.d_kind.biosdisk.partition = 0; 192 biosdev = -1; 193 } else { 194 new_currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + 195 B_CONTROLLER(initial_bootdev) - 1; 196 new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 197 biosdev = initial_bootinfo->bi_bios_dev; 198 major = B_TYPE(initial_bootdev); 199 200 /* 201 * If we are booted by an old bootstrap, we have to guess at the BIOS 202 * unit number. We will loose if there is more than one disk type 203 * and we are not booting from the lowest-numbered disk type 204 * (ie. SCSI when IDE also exists). 205 */ 206 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 207 biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 208 } 209 new_currdev.d_type = new_currdev.d_dev->dv_type; 210 211 /* 212 * If we are booting off of a BIOS disk and we didn't succeed in determining 213 * which one we booted off of, just use disk0: as a reasonable default. 214 */ 215 if ((new_currdev.d_type == biosdisk.dv_type) && 216 ((new_currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1)) { 217 printf("Can't work out which disk we are booting from.\n" 218 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 219 new_currdev.d_kind.biosdisk.unit = 0; 220 } 221 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev), 222 i386_setcurrdev, env_nounset); 223 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset, 224 env_nounset); 225} 226 227COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 228 229static int 230command_reboot(int argc, char *argv[]) 231{ 232 int i; 233 234 for (i = 0; devsw[i] != NULL; ++i) 235 if (devsw[i]->dv_cleanup != NULL) 236 (devsw[i]->dv_cleanup)(); 237 238 printf("Rebooting...\n"); 239 delay(1000000); 240 __exit(0); 241} 242 243/* provide this for panic, as it's not in the startup code */ 244void 245exit(int code) 246{ 247 __exit(code); 248} 249 250COMMAND_SET(heap, "heap", "show heap usage", command_heap); 251 252static int 253command_heap(int argc, char *argv[]) 254{ 255 mallocstats(); 256 printf("heap base at %p, top at %p\n", end, sbrk(0)); 257 return(CMD_OK); 258} 259 260/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 261static int 262isa_inb(int port) 263{ 264 u_char data; 265 266 if (__builtin_constant_p(port) && 267 (((port) & 0xffff) < 0x100) && 268 ((port) < 0x10000)) { 269 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 270 } else { 271 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 272 } 273 return(data); 274} 275 276static void 277isa_outb(int port, int value) 278{ 279 u_char al = value; 280 281 if (__builtin_constant_p(port) && 282 (((port) & 0xffff) < 0x100) && 283 ((port) < 0x10000)) { 284 __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 285 } else { 286 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 287 } 288} 289 290