main.c revision 136891
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 136891 2004-10-24 12:15:07Z 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>
3843561Skato#include <sys/reboot.h>
3943561Skato
4043561Skato#include "bootstrap.h"
4145241Skato#include "libi386/libi386.h"
4243561Skato#include "btxv86.h"
4343561Skato
4458871Skato#define	KARGS_FLAGS_CD		0x1
4558871Skato#define	KARGS_FLAGS_PXE		0x2
4658871Skato
4743561Skato/* Arguments passed in from the boot1/boot2 loader */
4843561Skatostatic struct
4943561Skato{
5043561Skato    u_int32_t	howto;
5143561Skato    u_int32_t	bootdev;
5258871Skato    u_int32_t	bootflags;
5358871Skato    u_int32_t	pxeinfo;
5443561Skato    u_int32_t	res2;
5543561Skato    u_int32_t	bootinfo;
5643561Skato} *kargs;
5743561Skato
5843561Skatostatic u_int32_t	initial_howto;
5943561Skatostatic u_int32_t	initial_bootdev;
6043561Skatostatic struct bootinfo	*initial_bootinfo;
6143561Skato
6243561Skatostruct arch_switch	archsw;		/* MI/MD interface boundary */
6343561Skato
6443561Skatostatic void		extract_currdev(void);
6543561Skatostatic int		isa_inb(int port);
6643561Skatostatic void		isa_outb(int port, int value);
6768358Snyanvoid			exit(int code);
6843561Skato
6943561Skato/* from vers.c */
7043561Skatoextern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
7143561Skato
7243561Skato/* XXX debugging */
7343561Skatoextern char end[];
7443561Skato
7568358Snyanint
7643561Skatomain(void)
7743561Skato{
7843561Skato    int			i;
7943561Skato
8043561Skato    /* Pick up arguments */
8143561Skato    kargs = (void *)__args;
8243561Skato    initial_howto = kargs->howto;
8343561Skato    initial_bootdev = kargs->bootdev;
8458871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
8543561Skato
8643561Skato    /*
8743561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
8843561Skato     */
8955342Snyan    bios_getmem();
9059167Skato
9155342Snyan    setheap((void *)end, (void *)bios_basemem);
9258871Skato
9343561Skato    /*
9443561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
9543561Skato     * console attributes may depend on reading from eg. the boot device, which we
9643561Skato     * can't do yet.
9743561Skato     *
9843561Skato     * We can use printf() etc. once this is done.
9943561Skato     * If the previous boot stage has requested a serial console, prefer that.
10043561Skato     */
10143561Skato    if (initial_howto & RB_SERIAL)
10243561Skato	setenv("console", "comconsole", 1);
10366246Skato    if (initial_howto & RB_MUTE)
10466246Skato	setenv("console", "nullconsole", 1);
105136891Snyan    if (initial_howto & RB_MULTIPLE)
106136891Snyan	setenv("boot_multicons", "YES", 1);
10743561Skato    cons_probe();
10843561Skato
10943561Skato    /*
11043561Skato     * Initialise the block cache
11143561Skato     */
11243561Skato    bcache_init(32, 512);	/* 16k cache XXX tune this */
11343561Skato
11443561Skato    /*
11586131Snyan     * Special handling for PXE and CD booting.
11658871Skato     */
117126970Snyan    if (kargs->bootinfo == 0) {
11886131Snyan	/*
11986131Snyan	 * We only want the PXE disk to try to init itself in the below
12086131Snyan	 * walk through devsw if we actually booted off of PXE.
12186131Snyan	 */
12286131Snyan	if (kargs->bootflags & KARGS_FLAGS_PXE)
12386131Snyan	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
12486131Snyan	else if (kargs->bootflags & KARGS_FLAGS_CD)
12586131Snyan	    bc_add(initial_bootdev);
12658871Skato    }
12758871Skato
12858871Skato    /*
12943561Skato     * March through the device switch probing for things.
13043561Skato     */
13143561Skato    for (i = 0; devsw[i] != NULL; i++)
13243561Skato	if (devsw[i]->dv_init != NULL)
13343561Skato	    (devsw[i]->dv_init)();
13455342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
135136891Snyan    if (initial_bootinfo != NULL) {
136136891Snyan	initial_bootinfo->bi_basemem = bios_basemem / 1024;
137136891Snyan	initial_bootinfo->bi_extmem = bios_extmem / 1024;
138136891Snyan    }
13943561Skato
14083532Snyan#ifndef PC98
14183532Snyan    /* detect ACPI for future reference */
14283532Snyan    biosacpi_detect();
14383532Snyan#endif
14483532Snyan
14543561Skato    printf("\n");
14655342Snyan    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
14743561Skato    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
14843561Skato
14943561Skato    extract_currdev();				/* set $currdev and $loaddev */
15043561Skato    setenv("LINES", "24", 1);			/* optional */
15143561Skato
15243561Skato    archsw.arch_autoload = i386_autoload;
15343561Skato    archsw.arch_getdev = i386_getdev;
15443561Skato    archsw.arch_copyin = i386_copyin;
15543561Skato    archsw.arch_copyout = i386_copyout;
15643561Skato    archsw.arch_readin = i386_readin;
15743561Skato    archsw.arch_isainb = isa_inb;
15843561Skato    archsw.arch_isaoutb = isa_outb;
15943561Skato
16043561Skato    interact();			/* doesn't return */
16168358Snyan
16268358Snyan    /* if we ever get here, it is an error */
16368358Snyan    return (1);
16443561Skato}
16543561Skato
16643561Skato/*
16743561Skato * Set the 'current device' by (if possible) recovering the boot device as
16843561Skato * supplied by the initial bootstrap.
16943561Skato *
17043561Skato * XXX should be extended for netbooting.
17143561Skato */
17243561Skatostatic void
17343561Skatoextract_currdev(void)
17443561Skato{
17568358Snyan    struct i386_devdesc	new_currdev;
17686131Snyan    int			major, biosdev = -1;
17743561Skato
17858871Skato    /* Assume we are booting from a BIOS disk by default */
17968358Snyan    new_currdev.d_dev = &biosdisk;
18043561Skato
18158871Skato    /* new-style boot loaders such as pxeldr and cdldr */
182126970Snyan    if (kargs->bootinfo == 0) {
18358871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
18486131Snyan	    /* we are booting from a CD with cdboot */
18586131Snyan	    new_currdev.d_dev = &bioscd;
18686131Snyan	    new_currdev.d_kind.bioscd.unit = bc_bios2unit(initial_bootdev);
18758871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
18858871Skato	    /* we are booting from pxeldr */
18968358Snyan	    new_currdev.d_dev = &pxedisk;
19068358Snyan	    new_currdev.d_kind.netif.unit = 0;
19158871Skato	} else {
19258871Skato	    /* we don't know what our boot device is */
19368358Snyan	    new_currdev.d_kind.biosdisk.slice = -1;
19468358Snyan	    new_currdev.d_kind.biosdisk.partition = 0;
19558871Skato	    biosdev = -1;
19658871Skato	}
19758871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
19843561Skato	/* The passed-in boot device is bad */
19968358Snyan	new_currdev.d_kind.biosdisk.slice = -1;
20068358Snyan	new_currdev.d_kind.biosdisk.partition = 0;
20143561Skato	biosdev = -1;
20243561Skato    } else {
20368358Snyan	new_currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) +
20468358Snyan					     B_CONTROLLER(initial_bootdev) - 1;
20568358Snyan	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
20643561Skato	biosdev = initial_bootinfo->bi_bios_dev;
20743561Skato	major = B_TYPE(initial_bootdev);
20843561Skato
20943561Skato	/*
21043561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
21143561Skato	 * unit number.  We will loose if there is more than one disk type
21243561Skato	 * and we are not booting from the lowest-numbered disk type
21343561Skato	 * (ie. SCSI when IDE also exists).
21443561Skato	 */
21544463Skato#ifdef PC98
21653218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
21744463Skato	    if (B_TYPE(initial_bootdev) == 6)
21844463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
21944463Skato	    else
22044463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
22153218Snyan	}
22243561Skato#else
22343561Skato	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
22443561Skato	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
22543561Skato#endif
22643561Skato    }
22786131Snyan    new_currdev.d_type = new_currdev.d_dev->dv_type;
22843561Skato
22958871Skato    /*
23058871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
23158871Skato     * which one we booted off of, just use disk0: as a reasonable default.
23258871Skato     */
23386131Snyan    if ((new_currdev.d_type == biosdisk.dv_type) &&
23468358Snyan	((new_currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1)) {
23543561Skato	printf("Can't work out which disk we are booting from.\n"
23643561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
23768358Snyan	new_currdev.d_kind.biosdisk.unit = 0;
23843561Skato    }
23968358Snyan    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
24068358Snyan	       i386_setcurrdev, env_nounset);
24168358Snyan    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
24268358Snyan	       env_nounset);
24343561Skato}
24443561Skato
24543561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
24643561Skato
24743561Skatostatic int
24843561Skatocommand_reboot(int argc, char *argv[])
24943561Skato{
25059535Snyan    int i;
25143561Skato
25259535Snyan    for (i = 0; devsw[i] != NULL; ++i)
25359535Snyan	if (devsw[i]->dv_cleanup != NULL)
25459535Snyan	    (devsw[i]->dv_cleanup)();
25559535Snyan
25643561Skato    printf("Rebooting...\n");
25743561Skato    delay(1000000);
25843561Skato    __exit(0);
25943561Skato}
26043561Skato
26143561Skato/* provide this for panic, as it's not in the startup code */
26243561Skatovoid
26343561Skatoexit(int code)
26443561Skato{
26543561Skato    __exit(code);
26643561Skato}
26743561Skato
26843561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
26943561Skato
27043561Skatostatic int
27143561Skatocommand_heap(int argc, char *argv[])
27243561Skato{
27343561Skato    mallocstats();
27443561Skato    printf("heap base at %p, top at %p\n", end, sbrk(0));
27543561Skato    return(CMD_OK);
27643561Skato}
27743561Skato
27843561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
27943561Skatostatic int
28043561Skatoisa_inb(int port)
28143561Skato{
28243561Skato    u_char	data;
28343561Skato
28443561Skato    if (__builtin_constant_p(port) &&
28543561Skato	(((port) & 0xffff) < 0x100) &&
28643561Skato	((port) < 0x10000)) {
28743561Skato	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
28843561Skato    } else {
28943561Skato	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
29043561Skato    }
29143561Skato    return(data);
29243561Skato}
29343561Skato
29443561Skatostatic void
29543561Skatoisa_outb(int port, int value)
29643561Skato{
29743561Skato    u_char	al = value;
29843561Skato
29943561Skato    if (__builtin_constant_p(port) &&
30043561Skato	(((port) & 0xffff) < 0x100) &&
30143561Skato	((port) < 0x10000)) {
30243561Skato	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
30343561Skato    } else {
30443561Skato        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
30543561Skato    }
30643561Skato}
30743561Skato
308