main.c revision 330311
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: stable/11/stand/pc98/loader/main.c 330311 2018-03-03 06:37:53Z kevans $");
29119880Sobrien
3043561Skato/*
3143561Skato * MD bootstrap main() and assorted miscellaneous
3243561Skato * commands.
3343561Skato */
3443561Skato
3543561Skato#include <stand.h>
36235264Savg#include <stddef.h>
3743561Skato#include <string.h>
3843561Skato#include <machine/bootinfo.h>
39240854Snyan#include <machine/cpufunc.h>
40220311Smarcel#include <sys/param.h>
4143561Skato#include <sys/reboot.h>
4243561Skato
4343561Skato#include "bootstrap.h"
44235264Savg#include "common/bootargs.h"
4545241Skato#include "libi386/libi386.h"
46201339Snyan#include "libpc98/libpc98.h"
4743561Skato#include "btxv86.h"
4843561Skato
49235264SavgCTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
50235264SavgCTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
51235264SavgCTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
52235264SavgCTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
5358871Skato
5443561Skato/* Arguments passed in from the boot1/boot2 loader */
55235264Savgstatic struct bootargs *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);
6668358Snyanvoid			exit(int code);
6743561Skato
6843561Skato/* from vers.c */
69312318Semasteextern	char bootprog_info[];
7043561Skato
7143561Skato/* XXX debugging */
7243561Skatoextern char end[];
7343561Skato
74153600Snyanstatic void *heap_top;
75153600Snyanstatic void *heap_bottom;
76153600Snyan
77220311Smarcelstatic uint64_t
78220311Smarcelpc98_loadaddr(u_int type, void *data, uint64_t addr)
79220311Smarcel{
80220311Smarcel	struct stat st;
81220311Smarcel
82220311Smarcel	if (type == LOAD_ELF)
83220311Smarcel		return (roundup(addr, PAGE_SIZE));
84220311Smarcel
85220311Smarcel	/* We cannot use 15M-16M area on pc98. */
86220311Smarcel	if (type == LOAD_RAW && addr < 0x1000000 && stat(data, &st) == 0 &&
87220311Smarcel	    (st.st_size == -1 || addr + st.st_size > 0xf00000))
88220311Smarcel		addr = 0x1000000;
89220311Smarcel	return (addr);
90220311Smarcel}
91220311Smarcel
9268358Snyanint
9343561Skatomain(void)
9443561Skato{
9543561Skato    int			i;
9643561Skato
97201339Snyan    /* Set machine type to PC98_SYSTEM_PARAMETER. */
98201339Snyan    set_machine_type();
99201339Snyan
10043561Skato    /* Pick up arguments */
10143561Skato    kargs = (void *)__args;
10243561Skato    initial_howto = kargs->howto;
10343561Skato    initial_bootdev = kargs->bootdev;
10458871Skato    initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
10543561Skato
106181436Sjhb    /* Initialize the v86 register set to a known-good state. */
107181436Sjhb    bzero(&v86, sizeof(v86));
108181436Sjhb    v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
109181436Sjhb
11043561Skato    /*
11143561Skato     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
11243561Skato     */
11355342Snyan    bios_getmem();
11459167Skato
115200253Snyan#if defined(LOADER_BZIP2_SUPPORT)
116200255Snyan    if (high_heap_size > 0) {
117200255Snyan	heap_top = PTOV(high_heap_base + high_heap_size);
118200255Snyan	heap_bottom = PTOV(high_heap_base);
119200255Snyan	if (high_heap_base < memtop_copyin)
120200255Snyan	    memtop_copyin = high_heap_base;
121200255Snyan    } else
122153600Snyan#endif
123200255Snyan    {
124200255Snyan	heap_top = (void *)PTOV(bios_basemem);
125200255Snyan	heap_bottom = (void *)end;
126200255Snyan    }
127153600Snyan    setheap(heap_bottom, heap_top);
12858871Skato
12943561Skato    /*
13043561Skato     * XXX Chicken-and-egg problem; we want to have console output early, but some
13143561Skato     * console attributes may depend on reading from eg. the boot device, which we
13243561Skato     * can't do yet.
13343561Skato     *
13443561Skato     * We can use printf() etc. once this is done.
13543561Skato     * If the previous boot stage has requested a serial console, prefer that.
13643561Skato     */
137150751Snyan    bi_setboothowto(initial_howto);
138146698Sjhb    if (initial_howto & RB_MULTIPLE) {
139146698Sjhb	if (initial_howto & RB_SERIAL)
140146698Sjhb	    setenv("console", "comconsole vidconsole", 1);
141146698Sjhb	else
142146698Sjhb	    setenv("console", "vidconsole comconsole", 1);
143146698Sjhb    } else if (initial_howto & RB_SERIAL)
14443561Skato	setenv("console", "comconsole", 1);
145146698Sjhb    else if (initial_howto & RB_MUTE)
14666246Skato	setenv("console", "nullconsole", 1);
14743561Skato    cons_probe();
14843561Skato
14943561Skato    /*
150298230Sallanjude     * Initialise the block cache. Set the upper limit.
15143561Skato     */
152298230Sallanjude    bcache_init(32768, 512);
15343561Skato
15443561Skato    /*
15586131Snyan     * Special handling for PXE and CD booting.
15658871Skato     */
157126970Snyan    if (kargs->bootinfo == 0) {
15886131Snyan	/*
15986131Snyan	 * We only want the PXE disk to try to init itself in the below
16086131Snyan	 * walk through devsw if we actually booted off of PXE.
16186131Snyan	 */
16286131Snyan	if (kargs->bootflags & KARGS_FLAGS_PXE)
16386131Snyan	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
16486131Snyan	else if (kargs->bootflags & KARGS_FLAGS_CD)
16586131Snyan	    bc_add(initial_bootdev);
16658871Skato    }
16758871Skato
168190046Snyan    archsw.arch_autoload = i386_autoload;
169190046Snyan    archsw.arch_getdev = i386_getdev;
170190046Snyan    archsw.arch_copyin = i386_copyin;
171190046Snyan    archsw.arch_copyout = i386_copyout;
172190046Snyan    archsw.arch_readin = i386_readin;
173190046Snyan    archsw.arch_isainb = isa_inb;
174190046Snyan    archsw.arch_isaoutb = isa_outb;
175220311Smarcel    archsw.arch_loadaddr = pc98_loadaddr;
176190046Snyan
17758871Skato    /*
17843561Skato     * March through the device switch probing for things.
17943561Skato     */
18043561Skato    for (i = 0; devsw[i] != NULL; i++)
18143561Skato	if (devsw[i]->dv_init != NULL)
18243561Skato	    (devsw[i]->dv_init)();
18355342Snyan    printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
184136891Snyan    if (initial_bootinfo != NULL) {
185136891Snyan	initial_bootinfo->bi_basemem = bios_basemem / 1024;
186136891Snyan	initial_bootinfo->bi_extmem = bios_extmem / 1024;
187136891Snyan    }
18843561Skato
189312318Semaste    printf("\n%s", bootprog_info);
19043561Skato
19143561Skato    extract_currdev();				/* set $currdev and $loaddev */
19243561Skato    setenv("LINES", "24", 1);			/* optional */
19343561Skato
194330311Skevans    interact();			/* doesn't return */
19568358Snyan
19668358Snyan    /* if we ever get here, it is an error */
19768358Snyan    return (1);
19843561Skato}
19943561Skato
20043561Skato/*
20143561Skato * Set the 'current device' by (if possible) recovering the boot device as
20243561Skato * supplied by the initial bootstrap.
20343561Skato *
20443561Skato * XXX should be extended for netbooting.
20543561Skato */
20643561Skatostatic void
20743561Skatoextract_currdev(void)
20843561Skato{
209240852Snyan    struct i386_devdesc		new_currdev;
210240852Snyan    int				major;
211240852Snyan    int				biosdev = -1;
21243561Skato
21358871Skato    /* Assume we are booting from a BIOS disk by default */
21468358Snyan    new_currdev.d_dev = &biosdisk;
21543561Skato
21658871Skato    /* new-style boot loaders such as pxeldr and cdldr */
217126970Snyan    if (kargs->bootinfo == 0) {
21858871Skato        if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
21986131Snyan	    /* we are booting from a CD with cdboot */
22086131Snyan	    new_currdev.d_dev = &bioscd;
221163897Smarcel	    new_currdev.d_unit = bc_bios2unit(initial_bootdev);
22258871Skato	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
22358871Skato	    /* we are booting from pxeldr */
22468358Snyan	    new_currdev.d_dev = &pxedisk;
225163897Smarcel	    new_currdev.d_unit = 0;
22658871Skato	} else {
22758871Skato	    /* we don't know what our boot device is */
22868358Snyan	    new_currdev.d_kind.biosdisk.slice = -1;
22968358Snyan	    new_currdev.d_kind.biosdisk.partition = 0;
23058871Skato	    biosdev = -1;
23158871Skato	}
23258871Skato    } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
23343561Skato	/* The passed-in boot device is bad */
23468358Snyan	new_currdev.d_kind.biosdisk.slice = -1;
23568358Snyan	new_currdev.d_kind.biosdisk.partition = 0;
23643561Skato	biosdev = -1;
23743561Skato    } else {
238172924Snyan	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
23968358Snyan	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
24043561Skato	biosdev = initial_bootinfo->bi_bios_dev;
24143561Skato	major = B_TYPE(initial_bootdev);
24243561Skato
24343561Skato	/*
24443561Skato	 * If we are booted by an old bootstrap, we have to guess at the BIOS
245160964Syar	 * unit number.  We will lose if there is more than one disk type
24643561Skato	 * and we are not booting from the lowest-numbered disk type
24743561Skato	 * (ie. SCSI when IDE also exists).
24843561Skato	 */
24953218Snyan	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
25044463Skato	    if (B_TYPE(initial_bootdev) == 6)
25144463Skato		biosdev = 0x30 + B_UNIT(initial_bootdev);
25244463Skato	    else
25344463Skato		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
25453218Snyan	}
25543561Skato    }
25686131Snyan    new_currdev.d_type = new_currdev.d_dev->dv_type;
257240852Snyan
25858871Skato    /*
25958871Skato     * If we are booting off of a BIOS disk and we didn't succeed in determining
26058871Skato     * which one we booted off of, just use disk0: as a reasonable default.
26158871Skato     */
26286131Snyan    if ((new_currdev.d_type == biosdisk.dv_type) &&
263163897Smarcel	((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
26443561Skato	printf("Can't work out which disk we are booting from.\n"
26543561Skato	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
266163897Smarcel	new_currdev.d_unit = 0;
26743561Skato    }
268240852Snyan
26968358Snyan    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
27068358Snyan	       i386_setcurrdev, env_nounset);
27168358Snyan    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
27268358Snyan	       env_nounset);
27343561Skato}
27443561Skato
27543561SkatoCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
27643561Skato
27743561Skatostatic int
27843561Skatocommand_reboot(int argc, char *argv[])
27943561Skato{
28059535Snyan    int i;
28143561Skato
28259535Snyan    for (i = 0; devsw[i] != NULL; ++i)
28359535Snyan	if (devsw[i]->dv_cleanup != NULL)
28459535Snyan	    (devsw[i]->dv_cleanup)();
28559535Snyan
28643561Skato    printf("Rebooting...\n");
28743561Skato    delay(1000000);
28843561Skato    __exit(0);
28943561Skato}
29043561Skato
29143561Skato/* provide this for panic, as it's not in the startup code */
29243561Skatovoid
29343561Skatoexit(int code)
29443561Skato{
29543561Skato    __exit(code);
29643561Skato}
29743561Skato
29843561SkatoCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
29943561Skato
30043561Skatostatic int
30143561Skatocommand_heap(int argc, char *argv[])
30243561Skato{
30343561Skato    mallocstats();
304153600Snyan    printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
305153600Snyan      sbrk(0), heap_top);
30643561Skato    return(CMD_OK);
30743561Skato}
30843561Skato
309240854Snyan/* ISA bus access functions for PnP. */
310240852Snyanstatic int
31143561Skatoisa_inb(int port)
31243561Skato{
313240854Snyan
314240854Snyan    return (inb(port));
31543561Skato}
31643561Skato
31743561Skatostatic void
31843561Skatoisa_outb(int port, int value)
31943561Skato{
320240854Snyan
321240854Snyan    outb(port, value);
32243561Skato}
323