main.c revision 83532
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 * 2650477Speter * $FreeBSD: head/sys/boot/pc98/loader/main.c 83532 2001-09-16 05:22:51Z nyan $ 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" 4045241Skato#include "libi386/libi386.h" 4143561Skato#include "btxv86.h" 4243561Skato 4358871Skato#define KARGS_FLAGS_CD 0x1 4458871Skato#define KARGS_FLAGS_PXE 0x2 4558871Skato 4643561Skato/* Arguments passed in from the boot1/boot2 loader */ 4743561Skatostatic struct 4843561Skato{ 4943561Skato u_int32_t howto; 5043561Skato u_int32_t bootdev; 5158871Skato u_int32_t bootflags; 5258871Skato u_int32_t pxeinfo; 5343561Skato u_int32_t res2; 5443561Skato u_int32_t bootinfo; 5543561Skato} *kargs; 5643561Skato 5743561Skatostatic u_int32_t initial_howto; 5843561Skatostatic u_int32_t initial_bootdev; 5943561Skatostatic struct bootinfo *initial_bootinfo; 6043561Skato 6143561Skatostruct arch_switch archsw; /* MI/MD interface boundary */ 6243561Skato 6343561Skatostatic void extract_currdev(void); 6443561Skatostatic int isa_inb(int port); 6543561Skatostatic void isa_outb(int port, int value); 6668358Snyanvoid exit(int code); 6743561Skato 6843561Skato/* from vers.c */ 6943561Skatoextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 7043561Skato 7143561Skato/* XXX debugging */ 7243561Skatoextern char end[]; 7343561Skato 7468358Snyanint 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; 8358871Skato initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL; 8443561Skato 8543561Skato /* 8643561Skato * Initialise the heap as early as possible. Once this is done, malloc() is usable. 8743561Skato */ 8855342Snyan bios_getmem(); 8959167Skato 9055342Snyan setheap((void *)end, (void *)bios_basemem); 9158871Skato 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); 10266246Skato if (initial_howto & RB_MUTE) 10366246Skato setenv("console", "nullconsole", 1); 10443561Skato cons_probe(); 10543561Skato 10643561Skato /* 10743561Skato * Initialise the block cache 10843561Skato */ 10943561Skato bcache_init(32, 512); /* 16k cache XXX tune this */ 11043561Skato 11143561Skato /* 11258871Skato * We only want the PXE disk to try to init itself in the below walk through 11358871Skato * devsw if we actually booted off of PXE. 11458871Skato */ 11558871Skato if((kargs->bootinfo == NULL) && 11658871Skato ((kargs->bootflags & KARGS_FLAGS_PXE) != 0)) { 11758871Skato pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL); 11858871Skato } 11958871Skato 12058871Skato /* 12143561Skato * March through the device switch probing for things. 12243561Skato */ 12343561Skato for (i = 0; devsw[i] != NULL; i++) 12443561Skato if (devsw[i]->dv_init != NULL) 12543561Skato (devsw[i]->dv_init)(); 12655342Snyan printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 12743561Skato 12883532Snyan#ifndef PC98 12983532Snyan /* detect ACPI for future reference */ 13083532Snyan biosacpi_detect(); 13183532Snyan#endif 13283532Snyan 13343561Skato printf("\n"); 13455342Snyan printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 13543561Skato printf("(%s, %s)\n", bootprog_maker, bootprog_date); 13643561Skato 13743561Skato extract_currdev(); /* set $currdev and $loaddev */ 13843561Skato setenv("LINES", "24", 1); /* optional */ 13943561Skato 14043561Skato archsw.arch_autoload = i386_autoload; 14143561Skato archsw.arch_getdev = i386_getdev; 14243561Skato archsw.arch_copyin = i386_copyin; 14343561Skato archsw.arch_copyout = i386_copyout; 14443561Skato archsw.arch_readin = i386_readin; 14543561Skato archsw.arch_isainb = isa_inb; 14643561Skato archsw.arch_isaoutb = isa_outb; 14743561Skato 14843561Skato interact(); /* doesn't return */ 14968358Snyan 15068358Snyan /* if we ever get here, it is an error */ 15168358Snyan return (1); 15243561Skato} 15343561Skato 15443561Skato/* 15543561Skato * Set the 'current device' by (if possible) recovering the boot device as 15643561Skato * supplied by the initial bootstrap. 15743561Skato * 15843561Skato * XXX should be extended for netbooting. 15943561Skato */ 16043561Skatostatic void 16143561Skatoextract_currdev(void) 16243561Skato{ 16368358Snyan struct i386_devdesc new_currdev; 16443561Skato int major, biosdev; 16543561Skato 16658871Skato /* Assume we are booting from a BIOS disk by default */ 16768358Snyan new_currdev.d_dev = &biosdisk; 16868358Snyan new_currdev.d_type = new_currdev.d_dev->dv_type; 16943561Skato 17058871Skato /* new-style boot loaders such as pxeldr and cdldr */ 17158871Skato if (kargs->bootinfo == NULL) { 17258871Skato if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) { 17358871Skato /* we are booting from a CD with cdldr */ 17468358Snyan new_currdev.d_kind.biosdisk.slice = -1; 17568358Snyan new_currdev.d_kind.biosdisk.partition = 0; 17658871Skato biosdev = initial_bootdev; 17758871Skato } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) { 17858871Skato /* we are booting from pxeldr */ 17968358Snyan new_currdev.d_dev = &pxedisk; 18068358Snyan new_currdev.d_type = new_currdev.d_dev->dv_type; 18168358Snyan new_currdev.d_kind.netif.unit = 0; 18268358Snyan biosdev = -1; 18358871Skato } else { 18458871Skato /* we don't know what our boot device is */ 18568358Snyan new_currdev.d_kind.biosdisk.slice = -1; 18668358Snyan new_currdev.d_kind.biosdisk.partition = 0; 18758871Skato biosdev = -1; 18858871Skato } 18958871Skato } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 19043561Skato /* The passed-in boot device is bad */ 19168358Snyan new_currdev.d_kind.biosdisk.slice = -1; 19268358Snyan new_currdev.d_kind.biosdisk.partition = 0; 19343561Skato biosdev = -1; 19443561Skato } else { 19568358Snyan new_currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + 19668358Snyan B_CONTROLLER(initial_bootdev) - 1; 19768358Snyan new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 19843561Skato biosdev = initial_bootinfo->bi_bios_dev; 19943561Skato major = B_TYPE(initial_bootdev); 20043561Skato 20143561Skato /* 20243561Skato * If we are booted by an old bootstrap, we have to guess at the BIOS 20343561Skato * unit number. We will loose if there is more than one disk type 20443561Skato * and we are not booting from the lowest-numbered disk type 20543561Skato * (ie. SCSI when IDE also exists). 20643561Skato */ 20744463Skato#ifdef PC98 20853218Snyan if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) { /* biosdev doesn't match major */ 20944463Skato if (B_TYPE(initial_bootdev) == 6) 21044463Skato biosdev = 0x30 + B_UNIT(initial_bootdev); 21144463Skato else 21244463Skato biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev); 21353218Snyan } 21443561Skato#else 21543561Skato if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) /* biosdev doesn't match major */ 21643561Skato biosdev = 0x80 + B_UNIT(initial_bootdev); /* assume harddisk */ 21743561Skato#endif 21843561Skato } 21943561Skato 22058871Skato /* 22158871Skato * If we are booting off of a BIOS disk and we didn't succeed in determining 22258871Skato * which one we booted off of, just use disk0: as a reasonable default. 22358871Skato */ 22468358Snyan if ((new_currdev.d_type == devsw[0]->dv_type) && 22568358Snyan ((new_currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1)) { 22643561Skato printf("Can't work out which disk we are booting from.\n" 22743561Skato "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 22868358Snyan new_currdev.d_kind.biosdisk.unit = 0; 22943561Skato } 23068358Snyan env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev), 23168358Snyan i386_setcurrdev, env_nounset); 23268358Snyan env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset, 23368358Snyan env_nounset); 23443561Skato} 23543561Skato 23643561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 23743561Skato 23843561Skatostatic int 23943561Skatocommand_reboot(int argc, char *argv[]) 24043561Skato{ 24159535Snyan int i; 24243561Skato 24359535Snyan for (i = 0; devsw[i] != NULL; ++i) 24459535Snyan if (devsw[i]->dv_cleanup != NULL) 24559535Snyan (devsw[i]->dv_cleanup)(); 24659535Snyan 24743561Skato printf("Rebooting...\n"); 24843561Skato delay(1000000); 24943561Skato __exit(0); 25043561Skato} 25143561Skato 25243561Skato/* provide this for panic, as it's not in the startup code */ 25343561Skatovoid 25443561Skatoexit(int code) 25543561Skato{ 25643561Skato __exit(code); 25743561Skato} 25843561Skato 25943561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap); 26043561Skato 26143561Skatostatic int 26243561Skatocommand_heap(int argc, char *argv[]) 26343561Skato{ 26443561Skato mallocstats(); 26543561Skato printf("heap base at %p, top at %p\n", end, sbrk(0)); 26643561Skato return(CMD_OK); 26743561Skato} 26843561Skato 26943561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 27043561Skatostatic int 27143561Skatoisa_inb(int port) 27243561Skato{ 27343561Skato u_char data; 27443561Skato 27543561Skato if (__builtin_constant_p(port) && 27643561Skato (((port) & 0xffff) < 0x100) && 27743561Skato ((port) < 0x10000)) { 27843561Skato __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 27943561Skato } else { 28043561Skato __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 28143561Skato } 28243561Skato return(data); 28343561Skato} 28443561Skato 28543561Skatostatic void 28643561Skatoisa_outb(int port, int value) 28743561Skato{ 28843561Skato u_char al = value; 28943561Skato 29043561Skato if (__builtin_constant_p(port) && 29143561Skato (((port) & 0xffff) < 0x100) && 29243561Skato ((port) < 0x10000)) { 29343561Skato __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 29443561Skato } else { 29543561Skato __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 29643561Skato } 29743561Skato} 29843561Skato 299