main.c revision 201339
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 201339 2009-12-31 12:05:48Z nyan $");
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>
38181436Sjhb#include <machine/psl.h>
3943561Skato#include <sys/reboot.h>
4043561Skato
4143561Skato#include "bootstrap.h"
4245241Skato#include "libi386/libi386.h"
43201339Snyan#include "libpc98/libpc98.h"
4443561Skato#include "btxv86.h"
4543561Skato
4658871Skato#define	KARGS_FLAGS_CD		0x1
4758871Skato#define	KARGS_FLAGS_PXE		0x2
4858871Skato
4943561Skato/* Arguments passed in from the boot1/boot2 loader */
5043561Skatostatic struct
5143561Skato{
5243561Skato    u_int32_t	howto;
5343561Skato    u_int32_t	bootdev;
5458871Skato    u_int32_t	bootflags;
5558871Skato    u_int32_t	pxeinfo;
5643561Skato    u_int32_t	res2;
5743561Skato    u_int32_t	bootinfo;
5843561Skato} *kargs;
5943561Skato
6043561Skatostatic u_int32_t	initial_howto;
6143561Skatostatic u_int32_t	initial_bootdev;
6243561Skatostatic struct bootinfo	*initial_bootinfo;
6343561Skato
6443561Skatostruct arch_switch	archsw;		/* MI/MD interface boundary */
6543561Skato
6643561Skatostatic void		extract_currdev(void);
6743561Skatostatic int		isa_inb(int port);
6843561Skatostatic void		isa_outb(int port, int value);
6968358Snyanvoid			exit(int code);
7043561Skato
7143561Skato/* from vers.c */
7243561Skatoextern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
7343561Skato
7443561Skato/* XXX debugging */
7543561Skatoextern char end[];
7643561Skato
77153600Snyanstatic void *heap_top;
78153600Snyanstatic void *heap_bottom;
79153600Snyan
8068358Snyanint
8143561Skatomain(void)
8243561Skato{
8343561Skato    int			i;
8443561Skato
85201339Snyan    /* Set machine type to PC98_SYSTEM_PARAMETER. */
86201339Snyan    set_machine_type();
87201339Snyan
8843561Skato    /* Pick up arguments */
8943561Skato    kargs = (void *)__args;
9043561Skato    initial_howto = kargs->howto;
9143561Skato    initial_bootdev = kargs->bootdev;
9258871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
9343561Skato
94181436Sjhb    /* Initialize the v86 register set to a known-good state. */
95181436Sjhb    bzero(&v86, sizeof(v86));
96181436Sjhb    v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
97181436Sjhb
9843561Skato    /*
9943561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
10043561Skato     */
10155342Snyan    bios_getmem();
10259167Skato
103200253Snyan#if defined(LOADER_BZIP2_SUPPORT)
104200255Snyan    if (high_heap_size > 0) {
105200255Snyan	heap_top = PTOV(high_heap_base + high_heap_size);
106200255Snyan	heap_bottom = PTOV(high_heap_base);
107200255Snyan	if (high_heap_base < memtop_copyin)
108200255Snyan	    memtop_copyin = high_heap_base;
109200255Snyan    } else
110153600Snyan#endif
111200255Snyan    {
112200255Snyan	heap_top = (void *)PTOV(bios_basemem);
113200255Snyan	heap_bottom = (void *)end;
114200255Snyan    }
115153600Snyan    setheap(heap_bottom, heap_top);
11658871Skato
11743561Skato    /*
11843561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
11943561Skato     * console attributes may depend on reading from eg. the boot device, which we
12043561Skato     * can't do yet.
12143561Skato     *
12243561Skato     * We can use printf() etc. once this is done.
12343561Skato     * If the previous boot stage has requested a serial console, prefer that.
12443561Skato     */
125150751Snyan    bi_setboothowto(initial_howto);
126146698Sjhb    if (initial_howto & RB_MULTIPLE) {
127146698Sjhb	if (initial_howto & RB_SERIAL)
128146698Sjhb	    setenv("console", "comconsole vidconsole", 1);
129146698Sjhb	else
130146698Sjhb	    setenv("console", "vidconsole comconsole", 1);
131146698Sjhb    } else if (initial_howto & RB_SERIAL)
13243561Skato	setenv("console", "comconsole", 1);
133146698Sjhb    else if (initial_howto & RB_MUTE)
13466246Skato	setenv("console", "nullconsole", 1);
13543561Skato    cons_probe();
13643561Skato
13743561Skato    /*
13843561Skato     * Initialise the block cache
13943561Skato     */
14043561Skato    bcache_init(32, 512);	/* 16k cache XXX tune this */
14143561Skato
14243561Skato    /*
14386131Snyan     * Special handling for PXE and CD booting.
14458871Skato     */
145126970Snyan    if (kargs->bootinfo == 0) {
14686131Snyan	/*
14786131Snyan	 * We only want the PXE disk to try to init itself in the below
14886131Snyan	 * walk through devsw if we actually booted off of PXE.
14986131Snyan	 */
15086131Snyan	if (kargs->bootflags & KARGS_FLAGS_PXE)
15186131Snyan	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
15286131Snyan	else if (kargs->bootflags & KARGS_FLAGS_CD)
15386131Snyan	    bc_add(initial_bootdev);
15458871Skato    }
15558871Skato
156190046Snyan    archsw.arch_autoload = i386_autoload;
157190046Snyan    archsw.arch_getdev = i386_getdev;
158190046Snyan    archsw.arch_copyin = i386_copyin;
159190046Snyan    archsw.arch_copyout = i386_copyout;
160190046Snyan    archsw.arch_readin = i386_readin;
161190046Snyan    archsw.arch_isainb = isa_inb;
162190046Snyan    archsw.arch_isaoutb = isa_outb;
163190046Snyan
16458871Skato    /*
16543561Skato     * March through the device switch probing for things.
16643561Skato     */
16743561Skato    for (i = 0; devsw[i] != NULL; i++)
16843561Skato	if (devsw[i]->dv_init != NULL)
16943561Skato	    (devsw[i]->dv_init)();
17055342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
171136891Snyan    if (initial_bootinfo != NULL) {
172136891Snyan	initial_bootinfo->bi_basemem = bios_basemem / 1024;
173136891Snyan	initial_bootinfo->bi_extmem = bios_extmem / 1024;
174136891Snyan    }
17543561Skato
17643561Skato    printf("\n");
17755342Snyan    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
17843561Skato    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
17943561Skato
18043561Skato    extract_currdev();				/* set $currdev and $loaddev */
18143561Skato    setenv("LINES", "24", 1);			/* optional */
18243561Skato
18343561Skato    interact();			/* doesn't return */
18468358Snyan
18568358Snyan    /* if we ever get here, it is an error */
18668358Snyan    return (1);
18743561Skato}
18843561Skato
18943561Skato/*
19043561Skato * Set the 'current device' by (if possible) recovering the boot device as
19143561Skato * supplied by the initial bootstrap.
19243561Skato *
19343561Skato * XXX should be extended for netbooting.
19443561Skato */
19543561Skatostatic void
19643561Skatoextract_currdev(void)
19743561Skato{
19868358Snyan    struct i386_devdesc	new_currdev;
199190046Snyan    int			major;
200190046Snyan    int			biosdev = -1;
20143561Skato
20258871Skato    /* Assume we are booting from a BIOS disk by default */
20368358Snyan    new_currdev.d_dev = &biosdisk;
20443561Skato
20558871Skato    /* new-style boot loaders such as pxeldr and cdldr */
206126970Snyan    if (kargs->bootinfo == 0) {
20758871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
20886131Snyan	    /* we are booting from a CD with cdboot */
20986131Snyan	    new_currdev.d_dev = &bioscd;
210163897Smarcel	    new_currdev.d_unit = bc_bios2unit(initial_bootdev);
21158871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
21258871Skato	    /* we are booting from pxeldr */
21368358Snyan	    new_currdev.d_dev = &pxedisk;
214163897Smarcel	    new_currdev.d_unit = 0;
21558871Skato	} else {
21658871Skato	    /* we don't know what our boot device is */
21768358Snyan	    new_currdev.d_kind.biosdisk.slice = -1;
21868358Snyan	    new_currdev.d_kind.biosdisk.partition = 0;
21958871Skato	    biosdev = -1;
22058871Skato	}
22158871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
22243561Skato	/* The passed-in boot device is bad */
22368358Snyan	new_currdev.d_kind.biosdisk.slice = -1;
22468358Snyan	new_currdev.d_kind.biosdisk.partition = 0;
22543561Skato	biosdev = -1;
22643561Skato    } else {
227172924Snyan	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
22868358Snyan	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
22943561Skato	biosdev = initial_bootinfo->bi_bios_dev;
23043561Skato	major = B_TYPE(initial_bootdev);
23143561Skato
23243561Skato	/*
23343561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
234160964Syar	 * unit number.  We will lose if there is more than one disk type
23543561Skato	 * and we are not booting from the lowest-numbered disk type
23643561Skato	 * (ie. SCSI when IDE also exists).
23743561Skato	 */
23853218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
23944463Skato	    if (B_TYPE(initial_bootdev) == 6)
24044463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
24144463Skato	    else
24244463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
24353218Snyan	}
24443561Skato    }
24586131Snyan    new_currdev.d_type = new_currdev.d_dev->dv_type;
24643561Skato
24758871Skato    /*
24858871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
24958871Skato     * which one we booted off of, just use disk0: as a reasonable default.
25058871Skato     */
25186131Snyan    if ((new_currdev.d_type == biosdisk.dv_type) &&
252163897Smarcel	((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
25343561Skato	printf("Can't work out which disk we are booting from.\n"
25443561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
255163897Smarcel	new_currdev.d_unit = 0;
25643561Skato    }
25768358Snyan    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
25868358Snyan	       i386_setcurrdev, env_nounset);
25968358Snyan    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
26068358Snyan	       env_nounset);
26143561Skato}
26243561Skato
26343561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
26443561Skato
26543561Skatostatic int
26643561Skatocommand_reboot(int argc, char *argv[])
26743561Skato{
26859535Snyan    int i;
26943561Skato
27059535Snyan    for (i = 0; devsw[i] != NULL; ++i)
27159535Snyan	if (devsw[i]->dv_cleanup != NULL)
27259535Snyan	    (devsw[i]->dv_cleanup)();
27359535Snyan
27443561Skato    printf("Rebooting...\n");
27543561Skato    delay(1000000);
27643561Skato    __exit(0);
27743561Skato}
27843561Skato
27943561Skato/* provide this for panic, as it's not in the startup code */
28043561Skatovoid
28143561Skatoexit(int code)
28243561Skato{
28343561Skato    __exit(code);
28443561Skato}
28543561Skato
28643561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
28743561Skato
28843561Skatostatic int
28943561Skatocommand_heap(int argc, char *argv[])
29043561Skato{
29143561Skato    mallocstats();
292153600Snyan    printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
293153600Snyan      sbrk(0), heap_top);
29443561Skato    return(CMD_OK);
29543561Skato}
29643561Skato
29743561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
29843561Skatostatic int
29943561Skatoisa_inb(int port)
30043561Skato{
30143561Skato    u_char	data;
30243561Skato
30343561Skato    if (__builtin_constant_p(port) &&
30443561Skato	(((port) & 0xffff) < 0x100) &&
30543561Skato	((port) < 0x10000)) {
30643561Skato	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
30743561Skato    } else {
30843561Skato	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
30943561Skato    }
31043561Skato    return(data);
31143561Skato}
31243561Skato
31343561Skatostatic void
31443561Skatoisa_outb(int port, int value)
31543561Skato{
31643561Skato    u_char	al = value;
31743561Skato
31843561Skato    if (__builtin_constant_p(port) &&
31943561Skato	(((port) & 0xffff) < 0x100) &&
32043561Skato	((port) < 0x10000)) {
32143561Skato	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
32243561Skato    } else {
32343561Skato        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
32443561Skato    }
32543561Skato}
32643561Skato
327