main.c revision 240852
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 240852 2012-09-23 08:46:44Z nyan $"); 29119880Sobrien 3043561Skato/* 3143561Skato * MD bootstrap main() and assorted miscellaneous 3243561Skato * commands. 3343561Skato */ 3443561Skato 3543561Skato#include <stand.h> 36235264Savg#include <stddef.h> 3743561Skato#include <string.h> 3843561Skato#include <machine/bootinfo.h> 39220311Smarcel#include <sys/param.h> 4043561Skato#include <sys/reboot.h> 4143561Skato 4243561Skato#include "bootstrap.h" 43235264Savg#include "common/bootargs.h" 4445241Skato#include "libi386/libi386.h" 45201339Snyan#include "libpc98/libpc98.h" 4643561Skato#include "btxv86.h" 4743561Skato 48235264SavgCTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE); 49235264SavgCTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO); 50235264SavgCTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS); 51235264SavgCTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE); 5258871Skato 5343561Skato/* Arguments passed in from the boot1/boot2 loader */ 54235264Savgstatic struct bootargs *kargs; 5543561Skato 5643561Skatostatic u_int32_t initial_howto; 5743561Skatostatic u_int32_t initial_bootdev; 5843561Skatostatic struct bootinfo *initial_bootinfo; 5943561Skato 6043561Skatostruct arch_switch archsw; /* MI/MD interface boundary */ 6143561Skato 6243561Skatostatic void extract_currdev(void); 6343561Skatostatic int isa_inb(int port); 6443561Skatostatic void isa_outb(int port, int value); 6568358Snyanvoid exit(int code); 6643561Skato 6743561Skato/* from vers.c */ 6843561Skatoextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; 6943561Skato 7043561Skato/* XXX debugging */ 7143561Skatoextern char end[]; 7243561Skato 73153600Snyanstatic void *heap_top; 74153600Snyanstatic void *heap_bottom; 75153600Snyan 76220311Smarcelstatic uint64_t 77220311Smarcelpc98_loadaddr(u_int type, void *data, uint64_t addr) 78220311Smarcel{ 79220311Smarcel struct stat st; 80220311Smarcel 81220311Smarcel if (type == LOAD_ELF) 82220311Smarcel return (roundup(addr, PAGE_SIZE)); 83220311Smarcel 84220311Smarcel /* We cannot use 15M-16M area on pc98. */ 85220311Smarcel if (type == LOAD_RAW && addr < 0x1000000 && stat(data, &st) == 0 && 86220311Smarcel (st.st_size == -1 || addr + st.st_size > 0xf00000)) 87220311Smarcel addr = 0x1000000; 88220311Smarcel return (addr); 89220311Smarcel} 90220311Smarcel 9168358Snyanint 9243561Skatomain(void) 9343561Skato{ 9443561Skato int i; 9543561Skato 96201339Snyan /* Set machine type to PC98_SYSTEM_PARAMETER. */ 97201339Snyan set_machine_type(); 98201339Snyan 9943561Skato /* Pick up arguments */ 10043561Skato kargs = (void *)__args; 10143561Skato initial_howto = kargs->howto; 10243561Skato initial_bootdev = kargs->bootdev; 10358871Skato initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL; 10443561Skato 105181436Sjhb /* Initialize the v86 register set to a known-good state. */ 106181436Sjhb bzero(&v86, sizeof(v86)); 107181436Sjhb v86.efl = PSL_RESERVED_DEFAULT | PSL_I; 108181436Sjhb 10943561Skato /* 11043561Skato * Initialise the heap as early as possible. Once this is done, malloc() is usable. 11143561Skato */ 11255342Snyan bios_getmem(); 11359167Skato 114200253Snyan#if defined(LOADER_BZIP2_SUPPORT) 115200255Snyan if (high_heap_size > 0) { 116200255Snyan heap_top = PTOV(high_heap_base + high_heap_size); 117200255Snyan heap_bottom = PTOV(high_heap_base); 118200255Snyan if (high_heap_base < memtop_copyin) 119200255Snyan memtop_copyin = high_heap_base; 120200255Snyan } else 121153600Snyan#endif 122200255Snyan { 123200255Snyan heap_top = (void *)PTOV(bios_basemem); 124200255Snyan heap_bottom = (void *)end; 125200255Snyan } 126153600Snyan setheap(heap_bottom, heap_top); 12758871Skato 12843561Skato /* 12943561Skato * XXX Chicken-and-egg problem; we want to have console output early, but some 13043561Skato * console attributes may depend on reading from eg. the boot device, which we 13143561Skato * can't do yet. 13243561Skato * 13343561Skato * We can use printf() etc. once this is done. 13443561Skato * If the previous boot stage has requested a serial console, prefer that. 13543561Skato */ 136150751Snyan bi_setboothowto(initial_howto); 137146698Sjhb if (initial_howto & RB_MULTIPLE) { 138146698Sjhb if (initial_howto & RB_SERIAL) 139146698Sjhb setenv("console", "comconsole vidconsole", 1); 140146698Sjhb else 141146698Sjhb setenv("console", "vidconsole comconsole", 1); 142146698Sjhb } else if (initial_howto & RB_SERIAL) 14343561Skato setenv("console", "comconsole", 1); 144146698Sjhb else if (initial_howto & RB_MUTE) 14566246Skato setenv("console", "nullconsole", 1); 14643561Skato cons_probe(); 14743561Skato 14843561Skato /* 14943561Skato * Initialise the block cache 15043561Skato */ 15143561Skato bcache_init(32, 512); /* 16k cache XXX tune this */ 15243561Skato 15343561Skato /* 15486131Snyan * Special handling for PXE and CD booting. 15558871Skato */ 156126970Snyan if (kargs->bootinfo == 0) { 15786131Snyan /* 15886131Snyan * We only want the PXE disk to try to init itself in the below 15986131Snyan * walk through devsw if we actually booted off of PXE. 16086131Snyan */ 16186131Snyan if (kargs->bootflags & KARGS_FLAGS_PXE) 16286131Snyan pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL); 16386131Snyan else if (kargs->bootflags & KARGS_FLAGS_CD) 16486131Snyan bc_add(initial_bootdev); 16558871Skato } 16658871Skato 167190046Snyan archsw.arch_autoload = i386_autoload; 168190046Snyan archsw.arch_getdev = i386_getdev; 169190046Snyan archsw.arch_copyin = i386_copyin; 170190046Snyan archsw.arch_copyout = i386_copyout; 171190046Snyan archsw.arch_readin = i386_readin; 172190046Snyan archsw.arch_isainb = isa_inb; 173190046Snyan archsw.arch_isaoutb = isa_outb; 174220311Smarcel archsw.arch_loadaddr = pc98_loadaddr; 175190046Snyan 17658871Skato /* 17743561Skato * March through the device switch probing for things. 17843561Skato */ 17943561Skato for (i = 0; devsw[i] != NULL; i++) 18043561Skato if (devsw[i]->dv_init != NULL) 18143561Skato (devsw[i]->dv_init)(); 18255342Snyan printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024); 183136891Snyan if (initial_bootinfo != NULL) { 184136891Snyan initial_bootinfo->bi_basemem = bios_basemem / 1024; 185136891Snyan initial_bootinfo->bi_extmem = bios_extmem / 1024; 186136891Snyan } 18743561Skato 18843561Skato printf("\n"); 18955342Snyan printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 19043561Skato printf("(%s, %s)\n", bootprog_maker, bootprog_date); 19143561Skato 19243561Skato extract_currdev(); /* set $currdev and $loaddev */ 19343561Skato setenv("LINES", "24", 1); /* optional */ 19443561Skato 19543561Skato interact(); /* doesn't return */ 19668358Snyan 19768358Snyan /* if we ever get here, it is an error */ 19868358Snyan return (1); 19943561Skato} 20043561Skato 20143561Skato/* 20243561Skato * Set the 'current device' by (if possible) recovering the boot device as 20343561Skato * supplied by the initial bootstrap. 20443561Skato * 20543561Skato * XXX should be extended for netbooting. 20643561Skato */ 20743561Skatostatic void 20843561Skatoextract_currdev(void) 20943561Skato{ 210240852Snyan struct i386_devdesc new_currdev; 211240852Snyan int major; 212240852Snyan int biosdev = -1; 21343561Skato 21458871Skato /* Assume we are booting from a BIOS disk by default */ 21568358Snyan new_currdev.d_dev = &biosdisk; 21643561Skato 21758871Skato /* new-style boot loaders such as pxeldr and cdldr */ 218126970Snyan if (kargs->bootinfo == 0) { 21958871Skato if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) { 22086131Snyan /* we are booting from a CD with cdboot */ 22186131Snyan new_currdev.d_dev = &bioscd; 222163897Smarcel new_currdev.d_unit = bc_bios2unit(initial_bootdev); 22358871Skato } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) { 22458871Skato /* we are booting from pxeldr */ 22568358Snyan new_currdev.d_dev = &pxedisk; 226163897Smarcel new_currdev.d_unit = 0; 22758871Skato } else { 22858871Skato /* we don't know what our boot device is */ 22968358Snyan new_currdev.d_kind.biosdisk.slice = -1; 23068358Snyan new_currdev.d_kind.biosdisk.partition = 0; 23158871Skato biosdev = -1; 23258871Skato } 23358871Skato } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) { 23443561Skato /* The passed-in boot device is bad */ 23568358Snyan new_currdev.d_kind.biosdisk.slice = -1; 23668358Snyan new_currdev.d_kind.biosdisk.partition = 0; 23743561Skato biosdev = -1; 23843561Skato } else { 239172924Snyan new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1; 24068358Snyan new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev); 24143561Skato biosdev = initial_bootinfo->bi_bios_dev; 24243561Skato major = B_TYPE(initial_bootdev); 24343561Skato 24443561Skato /* 24543561Skato * If we are booted by an old bootstrap, we have to guess at the BIOS 246160964Syar * unit number. We will lose if there is more than one disk type 24743561Skato * and we are not booting from the lowest-numbered disk type 24843561Skato * (ie. SCSI when IDE also exists). 24943561Skato */ 25053218Snyan if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) { /* biosdev doesn't match major */ 25144463Skato if (B_TYPE(initial_bootdev) == 6) 25244463Skato biosdev = 0x30 + B_UNIT(initial_bootdev); 25344463Skato else 25444463Skato biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev); 25553218Snyan } 25643561Skato } 25786131Snyan new_currdev.d_type = new_currdev.d_dev->dv_type; 258240852Snyan 25958871Skato /* 26058871Skato * If we are booting off of a BIOS disk and we didn't succeed in determining 26158871Skato * which one we booted off of, just use disk0: as a reasonable default. 26258871Skato */ 26386131Snyan if ((new_currdev.d_type == biosdisk.dv_type) && 264163897Smarcel ((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) { 26543561Skato printf("Can't work out which disk we are booting from.\n" 26643561Skato "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev); 267163897Smarcel new_currdev.d_unit = 0; 26843561Skato } 269240852Snyan 27068358Snyan env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev), 27168358Snyan i386_setcurrdev, env_nounset); 27268358Snyan env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset, 27368358Snyan env_nounset); 27443561Skato} 27543561Skato 27643561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 27743561Skato 27843561Skatostatic int 27943561Skatocommand_reboot(int argc, char *argv[]) 28043561Skato{ 28159535Snyan int i; 28243561Skato 28359535Snyan for (i = 0; devsw[i] != NULL; ++i) 28459535Snyan if (devsw[i]->dv_cleanup != NULL) 28559535Snyan (devsw[i]->dv_cleanup)(); 28659535Snyan 28743561Skato printf("Rebooting...\n"); 28843561Skato delay(1000000); 28943561Skato __exit(0); 29043561Skato} 29143561Skato 29243561Skato/* provide this for panic, as it's not in the startup code */ 29343561Skatovoid 29443561Skatoexit(int code) 29543561Skato{ 29643561Skato __exit(code); 29743561Skato} 29843561Skato 29943561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap); 30043561Skato 30143561Skatostatic int 30243561Skatocommand_heap(int argc, char *argv[]) 30343561Skato{ 30443561Skato mallocstats(); 305153600Snyan printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom, 306153600Snyan sbrk(0), heap_top); 30743561Skato return(CMD_OK); 30843561Skato} 30943561Skato 31043561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */ 311240852Snyanstatic int 31243561Skatoisa_inb(int port) 31343561Skato{ 31443561Skato u_char data; 31543561Skato 31643561Skato if (__builtin_constant_p(port) && 31743561Skato (((port) & 0xffff) < 0x100) && 31843561Skato ((port) < 0x10000)) { 31943561Skato __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 32043561Skato } else { 32143561Skato __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 32243561Skato } 32343561Skato return(data); 32443561Skato} 32543561Skato 32643561Skatostatic void 32743561Skatoisa_outb(int port, int value) 32843561Skato{ 32943561Skato u_char al = value; 33043561Skato 33143561Skato if (__builtin_constant_p(port) && 33243561Skato (((port) & 0xffff) < 0x100) && 33343561Skato ((port) < 0x10000)) { 33443561Skato __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port))); 33543561Skato } else { 33643561Skato __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 33743561Skato } 33843561Skato} 339