main.c revision 44463
143561Skato/*- 243561Skato * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 343561Skato * All rights reserved. 443561Skato * 543561Skato * Redistribution and use in source and binary forms, with or without 643561Skato * modification, are permitted provided that the following conditions 743561Skato * are met: 843561Skato * 1. Redistributions of source code must retain the above copyright 943561Skato * notice, this list of conditions and the following disclaimer. 1043561Skato * 2. Redistributions in binary form must reproduce the above copyright 1143561Skato * notice, this list of conditions and the following disclaimer in the 1243561Skato * documentation and/or other materials provided with the distribution. 1343561Skato * 1443561Skato * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1543561Skato * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1643561Skato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1743561Skato * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1843561Skato * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1943561Skato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2043561Skato * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2143561Skato * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2243561Skato * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2343561Skato * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2443561Skato * SUCH DAMAGE. 2543561Skato * 2644463Skato * $Id: main.c,v 1.1 1999/02/03 08:39:09 kato Exp $ 2743561Skato */ 2843561Skato 2943561Skato/* 3043561Skato * MD bootstrap main() and assorted miscellaneous 3143561Skato * commands. 3243561Skato */ 3343561Skato 3443561Skato#include <stand.h> 3543561Skato#include <string.h> 3643561Skato#include <machine/bootinfo.h> 3743561Skato#include <sys/reboot.h> 3843561Skato 3943561Skato#include "bootstrap.h" 4043561Skato#include "../../i386/libi386/libi386.h" 4143561Skato#include "btxv86.h" 4243561Skato 4343561Skato/* Arguments passed in from the boot1/boot2 loader */ 4443561Skatostatic struct 4543561Skato{ 4643561Skato u_int32_t howto; 4743561Skato u_int32_t bootdev; 4843561Skato u_int32_t res0; 4943561Skato u_int32_t res1; 5043561Skato u_int32_t res2; 5143561Skato u_int32_t bootinfo; 5243561Skato} *kargs; 5343561Skato 5443561Skatostatic u_int32_t initial_howto; 5543561Skatostatic u_int32_t initial_bootdev; 5643561Skato#ifdef PC98 5743561Skatostruct bootinfo *initial_bootinfo; 5843561Skato#else 5943561Skatostatic struct bootinfo *initial_bootinfo; 6043561Skato#endif 6143561Skato 6243561Skatostruct arch_switch archsw; /* MI/MD interface boundary */ 6343561Skato 6443561Skatostatic void extract_currdev(void); 6543561Skatostatic int isa_inb(int port); 6643561Skatostatic void isa_outb(int port, int value); 6743561Skato 6843561Skato/* from vers.c */ 6943561Skatoextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 7043561Skato 7143561Skato/* XXX debugging */ 7243561Skatoextern char end[]; 7343561Skato 7443561Skatovoid 7543561Skatomain(void) 7643561Skato{ 7743561Skato int i; 7843561Skato 7943561Skato /* Pick up arguments */ 8043561Skato kargs = (void *)__args; 8143561Skato initial_howto = kargs->howto; 8243561Skato initial_bootdev = kargs->bootdev; 8343561Skato initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo); 8443561Skato 8543561Skato /* 8643561Skato * Initialise the heap as early as possible. Once this is done, malloc() is usable. 8743561Skato * 8843561Skato * XXX better to locate end of memory and use that 8943561Skato */ 9043561Skato setheap((void *)end, (void *)(end + (384 * 1024))); 9143561Skato 9243561Skato /* 9343561Skato * XXX Chicken-and-egg problem; we want to have console output early, but some 9443561Skato * console attributes may depend on reading from eg. the boot device, which we 9543561Skato * can't do yet. 9643561Skato * 9743561Skato * We can use printf() etc. once this is done. 9843561Skato * If the previous boot stage has requested a serial console, prefer that. 9943561Skato */ 10043561Skato if (initial_howto & RB_SERIAL) 10143561Skato setenv("console", "comconsole", 1); 10243561Skato cons_probe(); 10343561Skato 10443561Skato /* 10543561Skato * Initialise the block cache 10643561Skato */ 10743561Skato bcache_init(32, 512); /* 16k cache XXX tune this */ 10843561Skato 10943561Skato /* 11043561Skato * March through the device switch probing for things. 11143561Skato */ 11243561Skato for (i = 0; devsw[i] != NULL; i++) 11343561Skato if (devsw[i]->dv_init != NULL) 11443561Skato (devsw[i]->dv_init)(); 11543561Skato 11643561Skato printf("\n"); 11743561Skato printf("%s, Revision %s %d/%dkB\n", bootprog_name, bootprog_rev, getbasemem(), getextmem()); 11843561Skato printf("(%s, %s)\n", bootprog_maker, bootprog_date); 11943561Skato 12043561Skato extract_currdev(); /* set $currdev and $loaddev */ 12143561Skato setenv("LINES", "24", 1); /* optional */ 12243561Skato 12343561Skato archsw.arch_autoload = i386_autoload; 12443561Skato archsw.arch_getdev = i386_getdev; 12543561Skato archsw.arch_copyin = i386_copyin; 12643561Skato archsw.arch_copyout = i386_copyout; 12743561Skato archsw.arch_readin = i386_readin; 12843561Skato archsw.arch_isainb = isa_inb; 12943561Skato archsw.arch_isaoutb = isa_outb; 13043561Skato 13143561Skato interact(); /* doesn't return */ 13243561Skato} 13343561Skato 13443561Skato/* 13543561Skato * Set the 'current device' by (if possible) recovering the boot device as 13643561Skato * supplied by the initial bootstrap. 13743561Skato * 13843561Skato * XXX should be extended for netbooting. 13943561Skato */ 14043561Skatostatic void 14143561Skatoextract_currdev(void) 14243561Skato{ 14343561Skato struct i386_devdesc currdev; 14443561Skato int major, biosdev; 14543561Skato 14643561Skato /* We're booting from a BIOS disk, try to spiff this */ 14743561Skato currdev.d_dev = devsw[0]; /* XXX presumes that biosdisk is first in devsw */ 14843561Skato currdev.d_type = currdev.d_dev->dv_type; 14943561Skato 15043561Skato if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 15143561Skato /* The passed-in boot device is bad */ 15243561Skato currdev.d_kind.biosdisk.slice = -1; 15343561Skato currdev.d_kind.biosdisk.partition = 0; 15443561Skato biosdev = -1; 15543561Skato } else { 15643561Skato currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + B_CONTROLLER(initial_bootdev) - 1; 15743561Skato currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 15843561Skato biosdev = initial_bootinfo->bi_bios_dev; 15943561Skato major = B_TYPE(initial_bootdev); 16043561Skato 16143561Skato /* 16243561Skato * If we are booted by an old bootstrap, we have to guess at the BIOS 16343561Skato * unit number. We will loose if there is more than one disk type 16443561Skato * and we are not booting from the lowest-numbered disk type 16543561Skato * (ie. SCSI when IDE also exists). 16643561Skato */ 16744463Skato#ifdef PC98 16844463Skato if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 16944463Skato if (B_TYPE(initial_bootdev) == 6) 17044463Skato biosdev = 0x30 + B_UNIT(initial_bootdev); 17144463Skato else 17244463Skato biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev); 17343561Skato#else 17443561Skato if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 17543561Skato biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 17643561Skato#endif 17743561Skato } 17843561Skato 17943561Skato if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) { 18043561Skato printf("Can't work out which disk we are booting from.\n" 18143561Skato "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 18243561Skato currdev.d_kind.biosdisk.unit = 0; 18343561Skato } 18443561Skato env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset); 18543561Skato env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset); 18643561Skato} 18743561Skato 18843561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 18943561Skato 19043561Skatostatic int 19143561Skatocommand_reboot(int argc, char *argv[]) 19243561Skato{ 19343561Skato 19443561Skato printf("Rebooting...\n"); 19543561Skato delay(1000000); 19643561Skato __exit(0); 19743561Skato} 19843561Skato 19943561Skato/* provide this for panic, as it's not in the startup code */ 20043561Skatovoid 20143561Skatoexit(int code) 20243561Skato{ 20343561Skato __exit(code); 20443561Skato} 20543561Skato 20643561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap); 20743561Skato 20843561Skatostatic int 20943561Skatocommand_heap(int argc, char *argv[]) 21043561Skato{ 21143561Skato mallocstats(); 21243561Skato printf("heap base at %p, top at %p\n", end, sbrk(0)); 21343561Skato return(CMD_OK); 21443561Skato} 21543561Skato 21643561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 21743561Skatostatic int 21843561Skatoisa_inb(int port) 21943561Skato{ 22043561Skato u_char data; 22143561Skato 22243561Skato if (__builtin_constant_p(port) && 22343561Skato (((port) & 0xffff) < 0x100) && 22443561Skato ((port) < 0x10000)) { 22543561Skato __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 22643561Skato } else { 22743561Skato __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 22843561Skato } 22943561Skato return(data); 23043561Skato} 23143561Skato 23243561Skatostatic void 23343561Skatoisa_outb(int port, int value) 23443561Skato{ 23543561Skato u_char al = value; 23643561Skato 23743561Skato if (__builtin_constant_p(port) && 23843561Skato (((port) & 0xffff) < 0x100) && 23943561Skato ((port) < 0x10000)) { 24043561Skato __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 24143561Skato } else { 24243561Skato __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 24343561Skato } 24443561Skato} 24543561Skato 246