main.c revision 43561
190075Sobrien/*- 290075Sobrien * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 390075Sobrien * All rights reserved. 490075Sobrien * 590075Sobrien * Redistribution and use in source and binary forms, with or without 690075Sobrien * modification, are permitted provided that the following conditions 790075Sobrien * are met: 890075Sobrien * 1. Redistributions of source code must retain the above copyright 990075Sobrien * notice, this list of conditions and the following disclaimer. 1090075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1190075Sobrien * notice, this list of conditions and the following disclaimer in the 1290075Sobrien * documentation and/or other materials provided with the distribution. 1390075Sobrien * 1490075Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1590075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1690075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1790075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1890075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1990075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2090075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2190075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2290075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2390075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2490075Sobrien * SUCH DAMAGE. 2590075Sobrien * 2690075Sobrien * $Id: main.c,v 1.14 1998/11/02 23:28:11 msmith Exp $ 2790075Sobrien */ 2890075Sobrien 2990075Sobrien/* 3090075Sobrien * MD bootstrap main() and assorted miscellaneous 3190075Sobrien * commands. 3290075Sobrien */ 3390075Sobrien 3490075Sobrien#include <stand.h> 3590075Sobrien#include <string.h> 3690075Sobrien#include <machine/bootinfo.h> 3790075Sobrien#include <sys/reboot.h> 3890075Sobrien 3990075Sobrien#include "bootstrap.h" 4090075Sobrien#include "../../i386/libi386/libi386.h" 4190075Sobrien#include "btxv86.h" 4290075Sobrien 4390075Sobrien/* Arguments passed in from the boot1/boot2 loader */ 4490075Sobrienstatic struct 4590075Sobrien{ 4690075Sobrien u_int32_t howto; 4790075Sobrien u_int32_t bootdev; 4890075Sobrien u_int32_t res0; 4990075Sobrien u_int32_t res1; 5090075Sobrien u_int32_t res2; 5190075Sobrien u_int32_t bootinfo; 5290075Sobrien} *kargs; 5390075Sobrien 5490075Sobrienstatic u_int32_t initial_howto; 5590075Sobrienstatic u_int32_t initial_bootdev; 5690075Sobrien#ifdef PC98 5790075Sobrienstruct bootinfo *initial_bootinfo; 5890075Sobrien#else 5990075Sobrienstatic struct bootinfo *initial_bootinfo; 6090075Sobrien#endif 6190075Sobrien 6290075Sobrienstruct arch_switch archsw; /* MI/MD interface boundary */ 6390075Sobrien 6490075Sobrienstatic void extract_currdev(void); 6590075Sobrienstatic int isa_inb(int port); 6690075Sobrienstatic void isa_outb(int port, int value); 6790075Sobrien 6890075Sobrien/* from vers.c */ 6990075Sobrienextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 7090075Sobrien 7190075Sobrien/* XXX debugging */ 7290075Sobrienextern char end[]; 7390075Sobrien 7490075Sobrienvoid 7590075Sobrienmain(void) 7690075Sobrien{ 7790075Sobrien int i; 7890075Sobrien 7990075Sobrien /* Pick up arguments */ 8090075Sobrien kargs = (void *)__args; 8190075Sobrien initial_howto = kargs->howto; 8290075Sobrien initial_bootdev = kargs->bootdev; 8390075Sobrien initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo); 8490075Sobrien 8590075Sobrien /* 8690075Sobrien * Initialise the heap as early as possible. Once this is done, malloc() is usable. 8790075Sobrien * 8890075Sobrien * XXX better to locate end of memory and use that 8990075Sobrien */ 9090075Sobrien setheap((void *)end, (void *)(end + (384 * 1024))); 9190075Sobrien 9290075Sobrien /* 93117395Skan * XXX Chicken-and-egg problem; we want to have console output early, but some 9490075Sobrien * console attributes may depend on reading from eg. the boot device, which we 9590075Sobrien * can't do yet. 9690075Sobrien * 9790075Sobrien * We can use printf() etc. once this is done. 9890075Sobrien * If the previous boot stage has requested a serial console, prefer that. 9990075Sobrien */ 10090075Sobrien if (initial_howto & RB_SERIAL) 10190075Sobrien setenv("console", "comconsole", 1); 10290075Sobrien cons_probe(); 10390075Sobrien 10490075Sobrien /* 10590075Sobrien * Initialise the block cache 10690075Sobrien */ 10790075Sobrien bcache_init(32, 512); /* 16k cache XXX tune this */ 10890075Sobrien 10990075Sobrien /* 11090075Sobrien * March through the device switch probing for things. 11190075Sobrien */ 11290075Sobrien for (i = 0; devsw[i] != NULL; i++) 11390075Sobrien if (devsw[i]->dv_init != NULL) 11490075Sobrien (devsw[i]->dv_init)(); 11590075Sobrien 11690075Sobrien printf("\n"); 11790075Sobrien printf("%s, Revision %s %d/%dkB\n", bootprog_name, bootprog_rev, getbasemem(), getextmem()); 11890075Sobrien printf("(%s, %s)\n", bootprog_maker, bootprog_date); 11990075Sobrien 12090075Sobrien extract_currdev(); /* set $currdev and $loaddev */ 12190075Sobrien setenv("LINES", "24", 1); /* optional */ 12290075Sobrien 12390075Sobrien archsw.arch_autoload = i386_autoload; 12490075Sobrien archsw.arch_getdev = i386_getdev; 12590075Sobrien archsw.arch_copyin = i386_copyin; 12690075Sobrien archsw.arch_copyout = i386_copyout; 12790075Sobrien archsw.arch_readin = i386_readin; 12890075Sobrien archsw.arch_isainb = isa_inb; 12990075Sobrien archsw.arch_isaoutb = isa_outb; 13090075Sobrien 13190075Sobrien interact(); /* doesn't return */ 13290075Sobrien} 13390075Sobrien 13490075Sobrien/* 13590075Sobrien * Set the 'current device' by (if possible) recovering the boot device as 13690075Sobrien * supplied by the initial bootstrap. 13790075Sobrien * 13890075Sobrien * XXX should be extended for netbooting. 13990075Sobrien */ 14090075Sobrienstatic void 14190075Sobrienextract_currdev(void) 14290075Sobrien{ 14390075Sobrien struct i386_devdesc currdev; 14490075Sobrien int major, biosdev; 14590075Sobrien 14690075Sobrien /* We're booting from a BIOS disk, try to spiff this */ 14790075Sobrien currdev.d_dev = devsw[0]; /* XXX presumes that biosdisk is first in devsw */ 14890075Sobrien currdev.d_type = currdev.d_dev->dv_type; 14990075Sobrien 15090075Sobrien if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 15190075Sobrien /* The passed-in boot device is bad */ 15290075Sobrien currdev.d_kind.biosdisk.slice = -1; 153 currdev.d_kind.biosdisk.partition = 0; 154 biosdev = -1; 155 } else { 156 currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + B_CONTROLLER(initial_bootdev) - 1; 157 currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 158 biosdev = initial_bootinfo->bi_bios_dev; 159 major = B_TYPE(initial_bootdev); 160 161 /* 162 * If we are booted by an old bootstrap, we have to guess at the BIOS 163 * unit number. We will loose if there is more than one disk type 164 * and we are not booting from the lowest-numbered disk type 165 * (ie. SCSI when IDE also exists). 166 */ 167#ifdef PC98 168 if (major == 6) 169 biosdev = 0x30 + B_UNIT(initial_bootdev); 170 else 171 biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev); 172#else 173 if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 174 biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 175#endif 176 } 177 178 if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) { 179 printf("Can't work out which disk we are booting from.\n" 180 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 181 currdev.d_kind.biosdisk.unit = 0; 182 } 183 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset); 184 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset); 185} 186 187COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 188 189static int 190command_reboot(int argc, char *argv[]) 191{ 192 193 printf("Rebooting...\n"); 194 delay(1000000); 195 __exit(0); 196} 197 198/* provide this for panic, as it's not in the startup code */ 199void 200exit(int code) 201{ 202 __exit(code); 203} 204 205COMMAND_SET(heap, "heap", "show heap usage", command_heap); 206 207static int 208command_heap(int argc, char *argv[]) 209{ 210 mallocstats(); 211 printf("heap base at %p, top at %p\n", end, sbrk(0)); 212 return(CMD_OK); 213} 214 215/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 216static int 217isa_inb(int port) 218{ 219 u_char data; 220 221 if (__builtin_constant_p(port) && 222 (((port) & 0xffff) < 0x100) && 223 ((port) < 0x10000)) { 224 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 225 } else { 226 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 227 } 228 return(data); 229} 230 231static void 232isa_outb(int port, int value) 233{ 234 u_char al = value; 235 236 if (__builtin_constant_p(port) && 237 (((port) & 0xffff) < 0x100) && 238 ((port) < 0x10000)) { 239 __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 240 } else { 241 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 242 } 243} 244 245