main.c revision 181436
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 181436 2008-08-08 19:41:20Z jhb $");
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"
4343561Skato#include "btxv86.h"
4443561Skato
4558871Skato#define	KARGS_FLAGS_CD		0x1
4658871Skato#define	KARGS_FLAGS_PXE		0x2
4758871Skato
4843561Skato/* Arguments passed in from the boot1/boot2 loader */
4943561Skatostatic struct
5043561Skato{
5143561Skato    u_int32_t	howto;
5243561Skato    u_int32_t	bootdev;
5358871Skato    u_int32_t	bootflags;
5458871Skato    u_int32_t	pxeinfo;
5543561Skato    u_int32_t	res2;
5643561Skato    u_int32_t	bootinfo;
5743561Skato} *kargs;
5843561Skato
5943561Skatostatic u_int32_t	initial_howto;
6043561Skatostatic u_int32_t	initial_bootdev;
6143561Skatostatic struct bootinfo	*initial_bootinfo;
6243561Skato
6343561Skatostruct arch_switch	archsw;		/* MI/MD interface boundary */
6443561Skato
6543561Skatostatic void		extract_currdev(void);
6643561Skatostatic int		isa_inb(int port);
6743561Skatostatic void		isa_outb(int port, int value);
6868358Snyanvoid			exit(int code);
6943561Skato
7043561Skato/* from vers.c */
7143561Skatoextern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
7243561Skato
7343561Skato/* XXX debugging */
7443561Skatoextern char end[];
7543561Skato
76153600Snyanstatic void *heap_top;
77153600Snyanstatic void *heap_bottom;
78153600Snyan
7968358Snyanint
8043561Skatomain(void)
8143561Skato{
8243561Skato    int			i;
8343561Skato
8443561Skato    /* Pick up arguments */
8543561Skato    kargs = (void *)__args;
8643561Skato    initial_howto = kargs->howto;
8743561Skato    initial_bootdev = kargs->bootdev;
8858871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
8943561Skato
90181436Sjhb    /* Initialize the v86 register set to a known-good state. */
91181436Sjhb    bzero(&v86, sizeof(v86));
92181436Sjhb    v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
93181436Sjhb
9443561Skato    /*
9543561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
9643561Skato     */
9755342Snyan    bios_getmem();
9859167Skato
99153600Snyan#ifdef LOADER_BZIP2_SUPPORT
100153600Snyan    heap_top = PTOV(memtop_copyin);
101153600Snyan    memtop_copyin -= 0x300000;
102153600Snyan    heap_bottom = PTOV(memtop_copyin);
103153600Snyan#else
104153600Snyan    heap_top = (void *)bios_basemem;
105153600Snyan    heap_bottom = (void *)end;
106153600Snyan#endif
107153600Snyan    setheap(heap_bottom, heap_top);
10858871Skato
10943561Skato    /*
11043561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
11143561Skato     * console attributes may depend on reading from eg. the boot device, which we
11243561Skato     * can't do yet.
11343561Skato     *
11443561Skato     * We can use printf() etc. once this is done.
11543561Skato     * If the previous boot stage has requested a serial console, prefer that.
11643561Skato     */
117150751Snyan    bi_setboothowto(initial_howto);
118146698Sjhb    if (initial_howto & RB_MULTIPLE) {
119146698Sjhb	if (initial_howto & RB_SERIAL)
120146698Sjhb	    setenv("console", "comconsole vidconsole", 1);
121146698Sjhb	else
122146698Sjhb	    setenv("console", "vidconsole comconsole", 1);
123146698Sjhb    } else if (initial_howto & RB_SERIAL)
12443561Skato	setenv("console", "comconsole", 1);
125146698Sjhb    else if (initial_howto & RB_MUTE)
12666246Skato	setenv("console", "nullconsole", 1);
12743561Skato    cons_probe();
12843561Skato
12943561Skato    /*
13043561Skato     * Initialise the block cache
13143561Skato     */
13243561Skato    bcache_init(32, 512);	/* 16k cache XXX tune this */
13343561Skato
13443561Skato    /*
13586131Snyan     * Special handling for PXE and CD booting.
13658871Skato     */
137126970Snyan    if (kargs->bootinfo == 0) {
13886131Snyan	/*
13986131Snyan	 * We only want the PXE disk to try to init itself in the below
14086131Snyan	 * walk through devsw if we actually booted off of PXE.
14186131Snyan	 */
14286131Snyan	if (kargs->bootflags & KARGS_FLAGS_PXE)
14386131Snyan	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
14486131Snyan	else if (kargs->bootflags & KARGS_FLAGS_CD)
14586131Snyan	    bc_add(initial_bootdev);
14658871Skato    }
14758871Skato
14858871Skato    /*
14943561Skato     * March through the device switch probing for things.
15043561Skato     */
15143561Skato    for (i = 0; devsw[i] != NULL; i++)
15243561Skato	if (devsw[i]->dv_init != NULL)
15343561Skato	    (devsw[i]->dv_init)();
15455342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
155136891Snyan    if (initial_bootinfo != NULL) {
156136891Snyan	initial_bootinfo->bi_basemem = bios_basemem / 1024;
157136891Snyan	initial_bootinfo->bi_extmem = bios_extmem / 1024;
158136891Snyan    }
15943561Skato
16043561Skato    printf("\n");
16155342Snyan    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
16243561Skato    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
16343561Skato
16443561Skato    extract_currdev();				/* set $currdev and $loaddev */
16543561Skato    setenv("LINES", "24", 1);			/* optional */
16643561Skato
16743561Skato    archsw.arch_autoload = i386_autoload;
16843561Skato    archsw.arch_getdev = i386_getdev;
16943561Skato    archsw.arch_copyin = i386_copyin;
17043561Skato    archsw.arch_copyout = i386_copyout;
17143561Skato    archsw.arch_readin = i386_readin;
17243561Skato    archsw.arch_isainb = isa_inb;
17343561Skato    archsw.arch_isaoutb = isa_outb;
17443561Skato
17543561Skato    interact();			/* doesn't return */
17668358Snyan
17768358Snyan    /* if we ever get here, it is an error */
17868358Snyan    return (1);
17943561Skato}
18043561Skato
18143561Skato/*
18243561Skato * Set the 'current device' by (if possible) recovering the boot device as
18343561Skato * supplied by the initial bootstrap.
18443561Skato *
18543561Skato * XXX should be extended for netbooting.
18643561Skato */
18743561Skatostatic void
18843561Skatoextract_currdev(void)
18943561Skato{
19068358Snyan    struct i386_devdesc	new_currdev;
19186131Snyan    int			major, biosdev = -1;
19243561Skato
19358871Skato    /* Assume we are booting from a BIOS disk by default */
19468358Snyan    new_currdev.d_dev = &biosdisk;
19543561Skato
19658871Skato    /* new-style boot loaders such as pxeldr and cdldr */
197126970Snyan    if (kargs->bootinfo == 0) {
19858871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
19986131Snyan	    /* we are booting from a CD with cdboot */
20086131Snyan	    new_currdev.d_dev = &bioscd;
201163897Smarcel	    new_currdev.d_unit = bc_bios2unit(initial_bootdev);
20258871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
20358871Skato	    /* we are booting from pxeldr */
20468358Snyan	    new_currdev.d_dev = &pxedisk;
205163897Smarcel	    new_currdev.d_unit = 0;
20658871Skato	} else {
20758871Skato	    /* we don't know what our boot device is */
20868358Snyan	    new_currdev.d_kind.biosdisk.slice = -1;
20968358Snyan	    new_currdev.d_kind.biosdisk.partition = 0;
21058871Skato	    biosdev = -1;
21158871Skato	}
21258871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
21343561Skato	/* The passed-in boot device is bad */
21468358Snyan	new_currdev.d_kind.biosdisk.slice = -1;
21568358Snyan	new_currdev.d_kind.biosdisk.partition = 0;
21643561Skato	biosdev = -1;
21743561Skato    } else {
218172924Snyan	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
21968358Snyan	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
22043561Skato	biosdev = initial_bootinfo->bi_bios_dev;
22143561Skato	major = B_TYPE(initial_bootdev);
22243561Skato
22343561Skato	/*
22443561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
225160964Syar	 * unit number.  We will lose if there is more than one disk type
22643561Skato	 * and we are not booting from the lowest-numbered disk type
22743561Skato	 * (ie. SCSI when IDE also exists).
22843561Skato	 */
22953218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
23044463Skato	    if (B_TYPE(initial_bootdev) == 6)
23144463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
23244463Skato	    else
23344463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
23453218Snyan	}
23543561Skato    }
23686131Snyan    new_currdev.d_type = new_currdev.d_dev->dv_type;
23743561Skato
23858871Skato    /*
23958871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
24058871Skato     * which one we booted off of, just use disk0: as a reasonable default.
24158871Skato     */
24286131Snyan    if ((new_currdev.d_type == biosdisk.dv_type) &&
243163897Smarcel	((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
24443561Skato	printf("Can't work out which disk we are booting from.\n"
24543561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
246163897Smarcel	new_currdev.d_unit = 0;
24743561Skato    }
24868358Snyan    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
24968358Snyan	       i386_setcurrdev, env_nounset);
25068358Snyan    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
25168358Snyan	       env_nounset);
25243561Skato}
25343561Skato
25443561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
25543561Skato
25643561Skatostatic int
25743561Skatocommand_reboot(int argc, char *argv[])
25843561Skato{
25959535Snyan    int i;
26043561Skato
26159535Snyan    for (i = 0; devsw[i] != NULL; ++i)
26259535Snyan	if (devsw[i]->dv_cleanup != NULL)
26359535Snyan	    (devsw[i]->dv_cleanup)();
26459535Snyan
26543561Skato    printf("Rebooting...\n");
26643561Skato    delay(1000000);
26743561Skato    __exit(0);
26843561Skato}
26943561Skato
27043561Skato/* provide this for panic, as it's not in the startup code */
27143561Skatovoid
27243561Skatoexit(int code)
27343561Skato{
27443561Skato    __exit(code);
27543561Skato}
27643561Skato
27743561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
27843561Skato
27943561Skatostatic int
28043561Skatocommand_heap(int argc, char *argv[])
28143561Skato{
28243561Skato    mallocstats();
283153600Snyan    printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
284153600Snyan      sbrk(0), heap_top);
28543561Skato    return(CMD_OK);
28643561Skato}
28743561Skato
28843561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
28943561Skatostatic int
29043561Skatoisa_inb(int port)
29143561Skato{
29243561Skato    u_char	data;
29343561Skato
29443561Skato    if (__builtin_constant_p(port) &&
29543561Skato	(((port) & 0xffff) < 0x100) &&
29643561Skato	((port) < 0x10000)) {
29743561Skato	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
29843561Skato    } else {
29943561Skato	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
30043561Skato    }
30143561Skato    return(data);
30243561Skato}
30343561Skato
30443561Skatostatic void
30543561Skatoisa_outb(int port, int value)
30643561Skato{
30743561Skato    u_char	al = value;
30843561Skato
30943561Skato    if (__builtin_constant_p(port) &&
31043561Skato	(((port) & 0xffff) < 0x100) &&
31143561Skato	((port) < 0x10000)) {
31243561Skato	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
31343561Skato    } else {
31443561Skato        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
31543561Skato    }
31643561Skato}
31743561Skato
318