main.c revision 56693
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 * $FreeBSD: head/sys/boot/i386/loader/main.c 56693 2000-01-27 21:21:01Z jhb $ 27 */ 28 29/* 30 * MD bootstrap main() and assorted miscellaneous 31 * commands. 32 */ 33 34#include <stand.h> 35#include <string.h> 36#include <machine/bootinfo.h> 37#include <sys/reboot.h> 38 39#include "bootstrap.h" 40#include "libi386/libi386.h" 41#include "btxv86.h" 42 43#define KARGS_FLAGS_CD 0x1 44 45/* Arguments passed in from the boot1/boot2 loader */ 46static struct 47{ 48 u_int32_t howto; 49 u_int32_t bootdev; 50 u_int32_t bootflags; 51 u_int32_t res1; 52 u_int32_t res2; 53 u_int32_t bootinfo; 54} *kargs; 55 56static u_int32_t initial_howto; 57static u_int32_t initial_bootdev; 58static struct bootinfo *initial_bootinfo; 59 60struct arch_switch archsw; /* MI/MD interface boundary */ 61 62static void extract_currdev(void); 63static int isa_inb(int port); 64static void isa_outb(int port, int value); 65 66/* from vers.c */ 67extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 68 69/* XXX debugging */ 70extern char end[]; 71 72void 73main(void) 74{ 75 int i; 76 77 /* Pick up arguments */ 78 kargs = (void *)__args; 79 initial_howto = kargs->howto; 80 initial_bootdev = kargs->bootdev; 81 initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo); 82 83 /* 84 * Initialise the heap as early as possible. Once this is done, malloc() is usable. 85 */ 86 bios_getmem(); 87 setheap((void *)end, (void *)bios_basemem); 88 89 /* 90 * XXX Chicken-and-egg problem; we want to have console output early, but some 91 * console attributes may depend on reading from eg. the boot device, which we 92 * can't do yet. 93 * 94 * We can use printf() etc. once this is done. 95 * If the previous boot stage has requested a serial console, prefer that. 96 */ 97 if (initial_howto & RB_SERIAL) 98 setenv("console", "comconsole", 1); 99 cons_probe(); 100 101 /* 102 * Initialise the block cache 103 */ 104 bcache_init(32, 512); /* 16k cache XXX tune this */ 105 106 /* 107 * March through the device switch probing for things. 108 */ 109 for (i = 0; devsw[i] != NULL; i++) 110 if (devsw[i]->dv_init != NULL) 111 (devsw[i]->dv_init)(); 112 printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 113 114 printf("\n"); 115 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 116 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 117 118 extract_currdev(); /* set $currdev and $loaddev */ 119 setenv("LINES", "24", 1); /* optional */ 120 121 archsw.arch_autoload = i386_autoload; 122 archsw.arch_getdev = i386_getdev; 123 archsw.arch_copyin = i386_copyin; 124 archsw.arch_copyout = i386_copyout; 125 archsw.arch_readin = i386_readin; 126 archsw.arch_isainb = isa_inb; 127 archsw.arch_isaoutb = isa_outb; 128 129 interact(); /* doesn't return */ 130} 131 132/* 133 * Set the 'current device' by (if possible) recovering the boot device as 134 * supplied by the initial bootstrap. 135 * 136 * XXX should be extended for netbooting. 137 */ 138static void 139extract_currdev(void) 140{ 141 struct i386_devdesc currdev; 142 int major, biosdev; 143 144 /* We're booting from a BIOS disk, try to spiff this */ 145 currdev.d_dev = devsw[0]; /* XXX presumes that biosdisk is first in devsw */ 146 currdev.d_type = currdev.d_dev->dv_type; 147 148 if ((kargs->bootinfo == NULL) && ((kargs->bootflags & KARGS_FLAGS_CD) != 0)) { 149 /* we are booting from a CD with cdldr */ 150 currdev.d_kind.biosdisk.slice = -1; 151 currdev.d_kind.biosdisk.partition = 0; 152 biosdev = initial_bootdev; 153 } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 154 /* The passed-in boot device is bad */ 155 currdev.d_kind.biosdisk.slice = -1; 156 currdev.d_kind.biosdisk.partition = 0; 157 biosdev = -1; 158 } else { 159 currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + B_CONTROLLER(initial_bootdev) - 1; 160 currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 161 biosdev = initial_bootinfo->bi_bios_dev; 162 major = B_TYPE(initial_bootdev); 163 164 /* 165 * If we are booted by an old bootstrap, we have to guess at the BIOS 166 * unit number. We will loose if there is more than one disk type 167 * and we are not booting from the lowest-numbered disk type 168 * (ie. SCSI when IDE also exists). 169 */ 170 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 171 biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 172 } 173 174 if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) { 175 printf("Can't work out which disk we are booting from.\n" 176 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 177 currdev.d_kind.biosdisk.unit = 0; 178 } 179 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset); 180 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset); 181} 182 183COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 184 185static int 186command_reboot(int argc, char *argv[]) 187{ 188 189 printf("Rebooting...\n"); 190 delay(1000000); 191 __exit(0); 192} 193 194/* provide this for panic, as it's not in the startup code */ 195void 196exit(int code) 197{ 198 __exit(code); 199} 200 201COMMAND_SET(heap, "heap", "show heap usage", command_heap); 202 203static int 204command_heap(int argc, char *argv[]) 205{ 206 mallocstats(); 207 printf("heap base at %p, top at %p\n", end, sbrk(0)); 208 return(CMD_OK); 209} 210 211/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 212static int 213isa_inb(int port) 214{ 215 u_char data; 216 217 if (__builtin_constant_p(port) && 218 (((port) & 0xffff) < 0x100) && 219 ((port) < 0x10000)) { 220 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 221 } else { 222 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 223 } 224 return(data); 225} 226 227static void 228isa_outb(int port, int value) 229{ 230 u_char al = value; 231 232 if (__builtin_constant_p(port) && 233 (((port) & 0xffff) < 0x100) && 234 ((port) < 0x10000)) { 235 __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 236 } else { 237 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 238 } 239} 240 241