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