main.c revision 220311
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 220311 2011-04-03 22:31:51Z marcel $");
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>
39220311Smarcel#include <sys/param.h>
4043561Skato#include <sys/reboot.h>
4143561Skato
4243561Skato#include "bootstrap.h"
4345241Skato#include "libi386/libi386.h"
44201339Snyan#include "libpc98/libpc98.h"
4543561Skato#include "btxv86.h"
4643561Skato
4758871Skato#define	KARGS_FLAGS_CD		0x1
4858871Skato#define	KARGS_FLAGS_PXE		0x2
4958871Skato
5043561Skato/* Arguments passed in from the boot1/boot2 loader */
5143561Skatostatic struct
5243561Skato{
5343561Skato    u_int32_t	howto;
5443561Skato    u_int32_t	bootdev;
5558871Skato    u_int32_t	bootflags;
5658871Skato    u_int32_t	pxeinfo;
5743561Skato    u_int32_t	res2;
5843561Skato    u_int32_t	bootinfo;
5943561Skato} *kargs;
6043561Skato
6143561Skatostatic u_int32_t	initial_howto;
6243561Skatostatic u_int32_t	initial_bootdev;
6343561Skatostatic struct bootinfo	*initial_bootinfo;
6443561Skato
6543561Skatostruct arch_switch	archsw;		/* MI/MD interface boundary */
6643561Skato
6743561Skatostatic void		extract_currdev(void);
6843561Skatostatic int		isa_inb(int port);
6943561Skatostatic void		isa_outb(int port, int value);
7068358Snyanvoid			exit(int code);
7143561Skato
7243561Skato/* from vers.c */
7343561Skatoextern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
7443561Skato
7543561Skato/* XXX debugging */
7643561Skatoextern char end[];
7743561Skato
78153600Snyanstatic void *heap_top;
79153600Snyanstatic void *heap_bottom;
80153600Snyan
81220311Smarcelstatic uint64_t
82220311Smarcelpc98_loadaddr(u_int type, void *data, uint64_t addr)
83220311Smarcel{
84220311Smarcel	struct stat st;
85220311Smarcel
86220311Smarcel	if (type == LOAD_ELF)
87220311Smarcel		return (roundup(addr, PAGE_SIZE));
88220311Smarcel
89220311Smarcel	/* We cannot use 15M-16M area on pc98. */
90220311Smarcel	if (type == LOAD_RAW && addr < 0x1000000 && stat(data, &st) == 0 &&
91220311Smarcel	    (st.st_size == -1 || addr + st.st_size > 0xf00000))
92220311Smarcel		addr = 0x1000000;
93220311Smarcel	return (addr);
94220311Smarcel}
95220311Smarcel
9668358Snyanint
9743561Skatomain(void)
9843561Skato{
9943561Skato    int			i;
10043561Skato
101201339Snyan    /* Set machine type to PC98_SYSTEM_PARAMETER. */
102201339Snyan    set_machine_type();
103201339Snyan
10443561Skato    /* Pick up arguments */
10543561Skato    kargs = (void *)__args;
10643561Skato    initial_howto = kargs->howto;
10743561Skato    initial_bootdev = kargs->bootdev;
10858871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
10943561Skato
110181436Sjhb    /* Initialize the v86 register set to a known-good state. */
111181436Sjhb    bzero(&v86, sizeof(v86));
112181436Sjhb    v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
113181436Sjhb
11443561Skato    /*
11543561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
11643561Skato     */
11755342Snyan    bios_getmem();
11859167Skato
119200253Snyan#if defined(LOADER_BZIP2_SUPPORT)
120200255Snyan    if (high_heap_size > 0) {
121200255Snyan	heap_top = PTOV(high_heap_base + high_heap_size);
122200255Snyan	heap_bottom = PTOV(high_heap_base);
123200255Snyan	if (high_heap_base < memtop_copyin)
124200255Snyan	    memtop_copyin = high_heap_base;
125200255Snyan    } else
126153600Snyan#endif
127200255Snyan    {
128200255Snyan	heap_top = (void *)PTOV(bios_basemem);
129200255Snyan	heap_bottom = (void *)end;
130200255Snyan    }
131153600Snyan    setheap(heap_bottom, heap_top);
13258871Skato
13343561Skato    /*
13443561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
13543561Skato     * console attributes may depend on reading from eg. the boot device, which we
13643561Skato     * can't do yet.
13743561Skato     *
13843561Skato     * We can use printf() etc. once this is done.
13943561Skato     * If the previous boot stage has requested a serial console, prefer that.
14043561Skato     */
141150751Snyan    bi_setboothowto(initial_howto);
142146698Sjhb    if (initial_howto & RB_MULTIPLE) {
143146698Sjhb	if (initial_howto & RB_SERIAL)
144146698Sjhb	    setenv("console", "comconsole vidconsole", 1);
145146698Sjhb	else
146146698Sjhb	    setenv("console", "vidconsole comconsole", 1);
147146698Sjhb    } else if (initial_howto & RB_SERIAL)
14843561Skato	setenv("console", "comconsole", 1);
149146698Sjhb    else if (initial_howto & RB_MUTE)
15066246Skato	setenv("console", "nullconsole", 1);
15143561Skato    cons_probe();
15243561Skato
15343561Skato    /*
15443561Skato     * Initialise the block cache
15543561Skato     */
15643561Skato    bcache_init(32, 512);	/* 16k cache XXX tune this */
15743561Skato
15843561Skato    /*
15986131Snyan     * Special handling for PXE and CD booting.
16058871Skato     */
161126970Snyan    if (kargs->bootinfo == 0) {
16286131Snyan	/*
16386131Snyan	 * We only want the PXE disk to try to init itself in the below
16486131Snyan	 * walk through devsw if we actually booted off of PXE.
16586131Snyan	 */
16686131Snyan	if (kargs->bootflags & KARGS_FLAGS_PXE)
16786131Snyan	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
16886131Snyan	else if (kargs->bootflags & KARGS_FLAGS_CD)
16986131Snyan	    bc_add(initial_bootdev);
17058871Skato    }
17158871Skato
172190046Snyan    archsw.arch_autoload = i386_autoload;
173190046Snyan    archsw.arch_getdev = i386_getdev;
174190046Snyan    archsw.arch_copyin = i386_copyin;
175190046Snyan    archsw.arch_copyout = i386_copyout;
176190046Snyan    archsw.arch_readin = i386_readin;
177190046Snyan    archsw.arch_isainb = isa_inb;
178190046Snyan    archsw.arch_isaoutb = isa_outb;
179220311Smarcel    archsw.arch_loadaddr = pc98_loadaddr;
180190046Snyan
18158871Skato    /*
18243561Skato     * March through the device switch probing for things.
18343561Skato     */
18443561Skato    for (i = 0; devsw[i] != NULL; i++)
18543561Skato	if (devsw[i]->dv_init != NULL)
18643561Skato	    (devsw[i]->dv_init)();
18755342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
188136891Snyan    if (initial_bootinfo != NULL) {
189136891Snyan	initial_bootinfo->bi_basemem = bios_basemem / 1024;
190136891Snyan	initial_bootinfo->bi_extmem = bios_extmem / 1024;
191136891Snyan    }
19243561Skato
19343561Skato    printf("\n");
19455342Snyan    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
19543561Skato    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
19643561Skato
19743561Skato    extract_currdev();				/* set $currdev and $loaddev */
19843561Skato    setenv("LINES", "24", 1);			/* optional */
19943561Skato
20043561Skato    interact();			/* doesn't return */
20168358Snyan
20268358Snyan    /* if we ever get here, it is an error */
20368358Snyan    return (1);
20443561Skato}
20543561Skato
20643561Skato/*
20743561Skato * Set the 'current device' by (if possible) recovering the boot device as
20843561Skato * supplied by the initial bootstrap.
20943561Skato *
21043561Skato * XXX should be extended for netbooting.
21143561Skato */
21243561Skatostatic void
21343561Skatoextract_currdev(void)
21443561Skato{
21568358Snyan    struct i386_devdesc	new_currdev;
216190046Snyan    int			major;
217190046Snyan    int			biosdev = -1;
21843561Skato
21958871Skato    /* Assume we are booting from a BIOS disk by default */
22068358Snyan    new_currdev.d_dev = &biosdisk;
22143561Skato
22258871Skato    /* new-style boot loaders such as pxeldr and cdldr */
223126970Snyan    if (kargs->bootinfo == 0) {
22458871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
22586131Snyan	    /* we are booting from a CD with cdboot */
22686131Snyan	    new_currdev.d_dev = &bioscd;
227163897Smarcel	    new_currdev.d_unit = bc_bios2unit(initial_bootdev);
22858871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
22958871Skato	    /* we are booting from pxeldr */
23068358Snyan	    new_currdev.d_dev = &pxedisk;
231163897Smarcel	    new_currdev.d_unit = 0;
23258871Skato	} else {
23358871Skato	    /* we don't know what our boot device is */
23468358Snyan	    new_currdev.d_kind.biosdisk.slice = -1;
23568358Snyan	    new_currdev.d_kind.biosdisk.partition = 0;
23658871Skato	    biosdev = -1;
23758871Skato	}
23858871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
23943561Skato	/* The passed-in boot device is bad */
24068358Snyan	new_currdev.d_kind.biosdisk.slice = -1;
24168358Snyan	new_currdev.d_kind.biosdisk.partition = 0;
24243561Skato	biosdev = -1;
24343561Skato    } else {
244172924Snyan	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
24568358Snyan	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
24643561Skato	biosdev = initial_bootinfo->bi_bios_dev;
24743561Skato	major = B_TYPE(initial_bootdev);
24843561Skato
24943561Skato	/*
25043561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
251160964Syar	 * unit number.  We will lose if there is more than one disk type
25243561Skato	 * and we are not booting from the lowest-numbered disk type
25343561Skato	 * (ie. SCSI when IDE also exists).
25443561Skato	 */
25553218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
25644463Skato	    if (B_TYPE(initial_bootdev) == 6)
25744463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
25844463Skato	    else
25944463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
26053218Snyan	}
26143561Skato    }
26286131Snyan    new_currdev.d_type = new_currdev.d_dev->dv_type;
26343561Skato
26458871Skato    /*
26558871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
26658871Skato     * which one we booted off of, just use disk0: as a reasonable default.
26758871Skato     */
26886131Snyan    if ((new_currdev.d_type == biosdisk.dv_type) &&
269163897Smarcel	((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
27043561Skato	printf("Can't work out which disk we are booting from.\n"
27143561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
272163897Smarcel	new_currdev.d_unit = 0;
27343561Skato    }
27468358Snyan    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
27568358Snyan	       i386_setcurrdev, env_nounset);
27668358Snyan    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
27768358Snyan	       env_nounset);
27843561Skato}
27943561Skato
28043561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
28143561Skato
28243561Skatostatic int
28343561Skatocommand_reboot(int argc, char *argv[])
28443561Skato{
28559535Snyan    int i;
28643561Skato
28759535Snyan    for (i = 0; devsw[i] != NULL; ++i)
28859535Snyan	if (devsw[i]->dv_cleanup != NULL)
28959535Snyan	    (devsw[i]->dv_cleanup)();
29059535Snyan
29143561Skato    printf("Rebooting...\n");
29243561Skato    delay(1000000);
29343561Skato    __exit(0);
29443561Skato}
29543561Skato
29643561Skato/* provide this for panic, as it's not in the startup code */
29743561Skatovoid
29843561Skatoexit(int code)
29943561Skato{
30043561Skato    __exit(code);
30143561Skato}
30243561Skato
30343561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
30443561Skato
30543561Skatostatic int
30643561Skatocommand_heap(int argc, char *argv[])
30743561Skato{
30843561Skato    mallocstats();
309153600Snyan    printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
310153600Snyan      sbrk(0), heap_top);
31143561Skato    return(CMD_OK);
31243561Skato}
31343561Skato
31443561Skato/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
31543561Skatostatic int
31643561Skatoisa_inb(int port)
31743561Skato{
31843561Skato    u_char	data;
31943561Skato
32043561Skato    if (__builtin_constant_p(port) &&
32143561Skato	(((port) & 0xffff) < 0x100) &&
32243561Skato	((port) < 0x10000)) {
32343561Skato	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
32443561Skato    } else {
32543561Skato	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
32643561Skato    }
32743561Skato    return(data);
32843561Skato}
32943561Skato
33043561Skatostatic void
33143561Skatoisa_outb(int port, int value)
33243561Skato{
33343561Skato    u_char	al = value;
33443561Skato
33543561Skato    if (__builtin_constant_p(port) &&
33643561Skato	(((port) & 0xffff) < 0x100) &&
33743561Skato	((port) < 0x10000)) {
33843561Skato	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
33943561Skato    } else {
34043561Skato        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
34143561Skato    }
34243561Skato}
34343561Skato
344