main.c revision 58871
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 58871 2000-03-31 16:03:02Z kato $
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);
6643561Skato
6743561Skato/* from vers.c */
6843561Skatoextern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
6943561Skato
7043561Skato/* XXX debugging */
7143561Skatoextern char end[];
7243561Skato
7343561Skatovoid
7443561Skatomain(void)
7543561Skato{
7643561Skato    int			i;
7743561Skato
7843561Skato    /* Pick up arguments */
7943561Skato    kargs = (void *)__args;
8043561Skato    initial_howto = kargs->howto;
8143561Skato    initial_bootdev = kargs->bootdev;
8258871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
8343561Skato
8443561Skato    /*
8543561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
8643561Skato     */
8755342Snyan    bios_getmem();
8855342Snyan    setheap((void *)end, (void *)bios_basemem);
8958871Skato
9043561Skato    /*
9143561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
9243561Skato     * console attributes may depend on reading from eg. the boot device, which we
9343561Skato     * can't do yet.
9443561Skato     *
9543561Skato     * We can use printf() etc. once this is done.
9643561Skato     * If the previous boot stage has requested a serial console, prefer that.
9743561Skato     */
9843561Skato    if (initial_howto & RB_SERIAL)
9943561Skato	setenv("console", "comconsole", 1);
10043561Skato    cons_probe();
10143561Skato
10243561Skato    /*
10343561Skato     * Initialise the block cache
10443561Skato     */
10543561Skato    bcache_init(32, 512);	/* 16k cache XXX tune this */
10643561Skato
10743561Skato    /*
10858871Skato     * We only want the PXE disk to try to init itself in the below walk through
10958871Skato     * devsw if we actually booted off of PXE.
11058871Skato     */
11158871Skato    if((kargs->bootinfo == NULL) &&
11258871Skato       ((kargs->bootflags & KARGS_FLAGS_PXE) != 0)) {
11358871Skato      pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
11458871Skato    }
11558871Skato
11658871Skato    /*
11743561Skato     * March through the device switch probing for things.
11843561Skato     */
11943561Skato    for (i = 0; devsw[i] != NULL; i++)
12043561Skato	if (devsw[i]->dv_init != NULL)
12143561Skato	    (devsw[i]->dv_init)();
12255342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
12343561Skato
12443561Skato    printf("\n");
12555342Snyan    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
12643561Skato    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
12743561Skato
12843561Skato    extract_currdev();				/* set $currdev and $loaddev */
12943561Skato    setenv("LINES", "24", 1);			/* optional */
13043561Skato
13143561Skato    archsw.arch_autoload = i386_autoload;
13243561Skato    archsw.arch_getdev = i386_getdev;
13343561Skato    archsw.arch_copyin = i386_copyin;
13443561Skato    archsw.arch_copyout = i386_copyout;
13543561Skato    archsw.arch_readin = i386_readin;
13643561Skato    archsw.arch_isainb = isa_inb;
13743561Skato    archsw.arch_isaoutb = isa_outb;
13843561Skato
13943561Skato    interact();			/* doesn't return */
14043561Skato}
14143561Skato
14243561Skato/*
14343561Skato * Set the 'current device' by (if possible) recovering the boot device as
14443561Skato * supplied by the initial bootstrap.
14543561Skato *
14643561Skato * XXX should be extended for netbooting.
14743561Skato */
14843561Skatostatic void
14943561Skatoextract_currdev(void)
15043561Skato{
15143561Skato    struct i386_devdesc	currdev;
15243561Skato    int			major, biosdev;
15343561Skato
15458871Skato    /* Assume we are booting from a BIOS disk by default */
15558871Skato    currdev.d_dev = &biosdisk;
15643561Skato    currdev.d_type = currdev.d_dev->dv_type;
15743561Skato
15858871Skato    /* new-style boot loaders such as pxeldr and cdldr */
15958871Skato    if (kargs->bootinfo == NULL) {
16058871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
16158871Skato	    /* we are booting from a CD with cdldr */
16258871Skato	    currdev.d_kind.biosdisk.slice = -1;
16358871Skato	    currdev.d_kind.biosdisk.partition = 0;
16458871Skato	    biosdev = initial_bootdev;
16558871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
16658871Skato	    /* we are booting from pxeldr */
16758871Skato	    currdev.d_dev = &pxedisk;
16858871Skato	    currdev.d_type = currdev.d_dev->dv_type;
16958871Skato	    currdev.d_kind.netif.unit = 0;
17058871Skato	} else {
17158871Skato	    /* we don't know what our boot device is */
17258871Skato	    currdev.d_kind.biosdisk.slice = -1;
17358871Skato	    currdev.d_kind.biosdisk.partition = 0;
17458871Skato	    biosdev = -1;
17558871Skato	}
17658871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
17743561Skato	/* The passed-in boot device is bad */
17843561Skato	currdev.d_kind.biosdisk.slice = -1;
17943561Skato	currdev.d_kind.biosdisk.partition = 0;
18043561Skato	biosdev = -1;
18143561Skato    } else {
18258871Skato	currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) +
18358871Skato					 B_CONTROLLER(initial_bootdev) - 1;
18443561Skato	currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
18543561Skato	biosdev = initial_bootinfo->bi_bios_dev;
18643561Skato	major = B_TYPE(initial_bootdev);
18743561Skato
18843561Skato	/*
18943561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
19043561Skato	 * unit number.  We will loose if there is more than one disk type
19143561Skato	 * and we are not booting from the lowest-numbered disk type
19243561Skato	 * (ie. SCSI when IDE also exists).
19343561Skato	 */
19444463Skato#ifdef PC98
19553218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
19644463Skato	    if (B_TYPE(initial_bootdev) == 6)
19744463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
19844463Skato	    else
19944463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
20053218Snyan	}
20143561Skato#else
20243561Skato	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
20343561Skato	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
20443561Skato#endif
20543561Skato    }
20643561Skato
20758871Skato    /*
20858871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
20958871Skato     * which one we booted off of, just use disk0: as a reasonable default.
21058871Skato     */
21158871Skato    if ((currdev.d_type == devsw[0]->dv_type) &&
21258871Skato	((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1)) {
21343561Skato	printf("Can't work out which disk we are booting from.\n"
21443561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
21543561Skato	currdev.d_kind.biosdisk.unit = 0;
21643561Skato    }
21743561Skato    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
21843561Skato    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset);
21943561Skato}
22043561Skato
22143561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
22243561Skato
22343561Skatostatic int
22443561Skatocommand_reboot(int argc, char *argv[])
22543561Skato{
22643561Skato
22743561Skato    printf("Rebooting...\n");
22843561Skato    delay(1000000);
22943561Skato    __exit(0);
23043561Skato}
23143561Skato
23243561Skato/* provide this for panic, as it's not in the startup code */
23343561Skatovoid
23443561Skatoexit(int code)
23543561Skato{
23643561Skato    __exit(code);
23743561Skato}
23843561Skato
23943561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
24043561Skato
24143561Skatostatic int
24243561Skatocommand_heap(int argc, char *argv[])
24343561Skato{
24443561Skato    mallocstats();
24543561Skato    printf("heap base at %p, top at %p\n", end, sbrk(0));
24643561Skato    return(CMD_OK);
24743561Skato}
24843561Skato
24943561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
25043561Skatostatic int
25143561Skatoisa_inb(int port)
25243561Skato{
25343561Skato    u_char	data;
25443561Skato
25543561Skato    if (__builtin_constant_p(port) &&
25643561Skato	(((port) & 0xffff) < 0x100) &&
25743561Skato	((port) < 0x10000)) {
25843561Skato	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
25943561Skato    } else {
26043561Skato	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
26143561Skato    }
26243561Skato    return(data);
26343561Skato}
26443561Skato
26543561Skatostatic void
26643561Skatoisa_outb(int port, int value)
26743561Skato{
26843561Skato    u_char	al = value;
26943561Skato
27043561Skato    if (__builtin_constant_p(port) &&
27143561Skato	(((port) & 0xffff) < 0x100) &&
27243561Skato	((port) < 0x10000)) {
27343561Skato	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
27443561Skato    } else {
27543561Skato        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
27643561Skato    }
27743561Skato}
27843561Skato
279