main.c revision 163897
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 */ 2643561Skato 27119880Sobrien#include <sys/cdefs.h> 28119880Sobrien__FBSDID("$FreeBSD: head/sys/boot/pc98/loader/main.c 163897 2006-11-02 01:23:18Z marcel $"); 29119880Sobrien 3043561Skato/* 3143561Skato * MD bootstrap main() and assorted miscellaneous 3243561Skato * commands. 3343561Skato */ 3443561Skato 3543561Skato#include <stand.h> 3643561Skato#include <string.h> 3743561Skato#include <machine/bootinfo.h> 3843561Skato#include <sys/reboot.h> 3943561Skato 4043561Skato#include "bootstrap.h" 4145241Skato#include "libi386/libi386.h" 4243561Skato#include "btxv86.h" 4343561Skato 4458871Skato#define KARGS_FLAGS_CD 0x1 4558871Skato#define KARGS_FLAGS_PXE 0x2 4658871Skato 4743561Skato/* Arguments passed in from the boot1/boot2 loader */ 4843561Skatostatic struct 4943561Skato{ 5043561Skato u_int32_t howto; 5143561Skato u_int32_t bootdev; 5258871Skato u_int32_t bootflags; 5358871Skato u_int32_t pxeinfo; 5443561Skato u_int32_t res2; 5543561Skato u_int32_t bootinfo; 5643561Skato} *kargs; 5743561Skato 5843561Skatostatic u_int32_t initial_howto; 5943561Skatostatic u_int32_t initial_bootdev; 6043561Skatostatic struct bootinfo *initial_bootinfo; 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); 6768358Snyanvoid exit(int code); 6843561Skato 6943561Skato/* from vers.c */ 7043561Skatoextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 7143561Skato 7243561Skato/* XXX debugging */ 7343561Skatoextern char end[]; 7443561Skato 75153600Snyanstatic void *heap_top; 76153600Snyanstatic void *heap_bottom; 77153600Snyan 7868358Snyanint 7943561Skatomain(void) 8043561Skato{ 8143561Skato int i; 8243561Skato 8343561Skato /* Pick up arguments */ 8443561Skato kargs = (void *)__args; 8543561Skato initial_howto = kargs->howto; 8643561Skato initial_bootdev = kargs->bootdev; 8758871Skato initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL; 8843561Skato 8943561Skato /* 9043561Skato * Initialise the heap as early as possible. Once this is done, malloc() is usable. 9143561Skato */ 9255342Snyan bios_getmem(); 9359167Skato 94153600Snyan#ifdef LOADER_BZIP2_SUPPORT 95153600Snyan heap_top = PTOV(memtop_copyin); 96153600Snyan memtop_copyin -= 0x300000; 97153600Snyan heap_bottom = PTOV(memtop_copyin); 98153600Snyan#else 99153600Snyan heap_top = (void *)bios_basemem; 100153600Snyan heap_bottom = (void *)end; 101153600Snyan#endif 102153600Snyan setheap(heap_bottom, heap_top); 10358871Skato 10443561Skato /* 10543561Skato * XXX Chicken-and-egg problem; we want to have console output early, but some 10643561Skato * console attributes may depend on reading from eg. the boot device, which we 10743561Skato * can't do yet. 10843561Skato * 10943561Skato * We can use printf() etc. once this is done. 11043561Skato * If the previous boot stage has requested a serial console, prefer that. 11143561Skato */ 112150751Snyan bi_setboothowto(initial_howto); 113146698Sjhb if (initial_howto & RB_MULTIPLE) { 114146698Sjhb if (initial_howto & RB_SERIAL) 115146698Sjhb setenv("console", "comconsole vidconsole", 1); 116146698Sjhb else 117146698Sjhb setenv("console", "vidconsole comconsole", 1); 118146698Sjhb } else if (initial_howto & RB_SERIAL) 11943561Skato setenv("console", "comconsole", 1); 120146698Sjhb else if (initial_howto & RB_MUTE) 12166246Skato setenv("console", "nullconsole", 1); 12243561Skato cons_probe(); 12343561Skato 12443561Skato /* 12543561Skato * Initialise the block cache 12643561Skato */ 12743561Skato bcache_init(32, 512); /* 16k cache XXX tune this */ 12843561Skato 12943561Skato /* 13086131Snyan * Special handling for PXE and CD booting. 13158871Skato */ 132126970Snyan if (kargs->bootinfo == 0) { 13386131Snyan /* 13486131Snyan * We only want the PXE disk to try to init itself in the below 13586131Snyan * walk through devsw if we actually booted off of PXE. 13686131Snyan */ 13786131Snyan if (kargs->bootflags & KARGS_FLAGS_PXE) 13886131Snyan pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL); 13986131Snyan else if (kargs->bootflags & KARGS_FLAGS_CD) 14086131Snyan bc_add(initial_bootdev); 14158871Skato } 14258871Skato 14358871Skato /* 14443561Skato * March through the device switch probing for things. 14543561Skato */ 14643561Skato for (i = 0; devsw[i] != NULL; i++) 14743561Skato if (devsw[i]->dv_init != NULL) 14843561Skato (devsw[i]->dv_init)(); 14955342Snyan printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 150136891Snyan if (initial_bootinfo != NULL) { 151136891Snyan initial_bootinfo->bi_basemem = bios_basemem / 1024; 152136891Snyan initial_bootinfo->bi_extmem = bios_extmem / 1024; 153136891Snyan } 15443561Skato 15543561Skato printf("\n"); 15655342Snyan printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 15743561Skato printf("(%s, %s)\n", bootprog_maker, bootprog_date); 15843561Skato 15943561Skato extract_currdev(); /* set $currdev and $loaddev */ 16043561Skato setenv("LINES", "24", 1); /* optional */ 16143561Skato 16243561Skato archsw.arch_autoload = i386_autoload; 16343561Skato archsw.arch_getdev = i386_getdev; 16443561Skato archsw.arch_copyin = i386_copyin; 16543561Skato archsw.arch_copyout = i386_copyout; 16643561Skato archsw.arch_readin = i386_readin; 16743561Skato archsw.arch_isainb = isa_inb; 16843561Skato archsw.arch_isaoutb = isa_outb; 16943561Skato 17043561Skato interact(); /* doesn't return */ 17168358Snyan 17268358Snyan /* if we ever get here, it is an error */ 17368358Snyan return (1); 17443561Skato} 17543561Skato 17643561Skato/* 17743561Skato * Set the 'current device' by (if possible) recovering the boot device as 17843561Skato * supplied by the initial bootstrap. 17943561Skato * 18043561Skato * XXX should be extended for netbooting. 18143561Skato */ 18243561Skatostatic void 18343561Skatoextract_currdev(void) 18443561Skato{ 18568358Snyan struct i386_devdesc new_currdev; 18686131Snyan int major, biosdev = -1; 18743561Skato 18858871Skato /* Assume we are booting from a BIOS disk by default */ 18968358Snyan new_currdev.d_dev = &biosdisk; 19043561Skato 19158871Skato /* new-style boot loaders such as pxeldr and cdldr */ 192126970Snyan if (kargs->bootinfo == 0) { 19358871Skato if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) { 19486131Snyan /* we are booting from a CD with cdboot */ 19586131Snyan new_currdev.d_dev = &bioscd; 196163897Smarcel new_currdev.d_unit = bc_bios2unit(initial_bootdev); 19758871Skato } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) { 19858871Skato /* we are booting from pxeldr */ 19968358Snyan new_currdev.d_dev = &pxedisk; 200163897Smarcel new_currdev.d_unit = 0; 20158871Skato } else { 20258871Skato /* we don't know what our boot device is */ 20368358Snyan new_currdev.d_kind.biosdisk.slice = -1; 20468358Snyan new_currdev.d_kind.biosdisk.partition = 0; 20558871Skato biosdev = -1; 20658871Skato } 20758871Skato } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 20843561Skato /* The passed-in boot device is bad */ 20968358Snyan new_currdev.d_kind.biosdisk.slice = -1; 21068358Snyan new_currdev.d_kind.biosdisk.partition = 0; 21143561Skato biosdev = -1; 21243561Skato } else { 21368358Snyan new_currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + 21468358Snyan B_CONTROLLER(initial_bootdev) - 1; 21568358Snyan new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 21643561Skato biosdev = initial_bootinfo->bi_bios_dev; 21743561Skato major = B_TYPE(initial_bootdev); 21843561Skato 21943561Skato /* 22043561Skato * If we are booted by an old bootstrap, we have to guess at the BIOS 221160964Syar * unit number. We will lose if there is more than one disk type 22243561Skato * and we are not booting from the lowest-numbered disk type 22343561Skato * (ie. SCSI when IDE also exists). 22443561Skato */ 22553218Snyan if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) { /* biosdev doesn't match major */ 22644463Skato if (B_TYPE(initial_bootdev) == 6) 22744463Skato biosdev = 0x30 + B_UNIT(initial_bootdev); 22844463Skato else 22944463Skato biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev); 23053218Snyan } 23143561Skato } 23286131Snyan new_currdev.d_type = new_currdev.d_dev->dv_type; 23343561Skato 23458871Skato /* 23558871Skato * If we are booting off of a BIOS disk and we didn't succeed in determining 23658871Skato * which one we booted off of, just use disk0: as a reasonable default. 23758871Skato */ 23886131Snyan if ((new_currdev.d_type == biosdisk.dv_type) && 239163897Smarcel ((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) { 24043561Skato printf("Can't work out which disk we are booting from.\n" 24143561Skato "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 242163897Smarcel new_currdev.d_unit = 0; 24343561Skato } 24468358Snyan env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev), 24568358Snyan i386_setcurrdev, env_nounset); 24668358Snyan env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset, 24768358Snyan env_nounset); 24843561Skato} 24943561Skato 25043561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 25143561Skato 25243561Skatostatic int 25343561Skatocommand_reboot(int argc, char *argv[]) 25443561Skato{ 25559535Snyan int i; 25643561Skato 25759535Snyan for (i = 0; devsw[i] != NULL; ++i) 25859535Snyan if (devsw[i]->dv_cleanup != NULL) 25959535Snyan (devsw[i]->dv_cleanup)(); 26059535Snyan 26143561Skato printf("Rebooting...\n"); 26243561Skato delay(1000000); 26343561Skato __exit(0); 26443561Skato} 26543561Skato 26643561Skato/* provide this for panic, as it's not in the startup code */ 26743561Skatovoid 26843561Skatoexit(int code) 26943561Skato{ 27043561Skato __exit(code); 27143561Skato} 27243561Skato 27343561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap); 27443561Skato 27543561Skatostatic int 27643561Skatocommand_heap(int argc, char *argv[]) 27743561Skato{ 27843561Skato mallocstats(); 279153600Snyan printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom, 280153600Snyan sbrk(0), heap_top); 28143561Skato return(CMD_OK); 28243561Skato} 28343561Skato 28443561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 28543561Skatostatic int 28643561Skatoisa_inb(int port) 28743561Skato{ 28843561Skato u_char data; 28943561Skato 29043561Skato if (__builtin_constant_p(port) && 29143561Skato (((port) & 0xffff) < 0x100) && 29243561Skato ((port) < 0x10000)) { 29343561Skato __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 29443561Skato } else { 29543561Skato __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 29643561Skato } 29743561Skato return(data); 29843561Skato} 29943561Skato 30043561Skatostatic void 30143561Skatoisa_outb(int port, int value) 30243561Skato{ 30343561Skato u_char al = value; 30443561Skato 30543561Skato if (__builtin_constant_p(port) && 30643561Skato (((port) & 0xffff) < 0x100) && 30743561Skato ((port) < 0x10000)) { 30843561Skato __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 30943561Skato } else { 31043561Skato __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 31143561Skato } 31243561Skato} 31343561Skato 314