main.c revision 54819
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/boot/pc98/loader/main.c 54819 1999-12-19 14:05:46Z nyan $
27 */
28
29/*
30 * MD bootstrap main() and assorted miscellaneous
31 * commands.
32 */
33
34#include <stand.h>
35#include <string.h>
36#include <machine/bootinfo.h>
37#include <sys/reboot.h>
38
39#include "bootstrap.h"
40#include "libi386/libi386.h"
41#include "btxv86.h"
42
43/* Arguments passed in from the boot1/boot2 loader */
44static struct
45{
46    u_int32_t	howto;
47    u_int32_t	bootdev;
48    u_int32_t	res0;
49    u_int32_t	res1;
50    u_int32_t	res2;
51    u_int32_t	bootinfo;
52} *kargs;
53
54static u_int32_t	initial_howto;
55static u_int32_t	initial_bootdev;
56static struct bootinfo	*initial_bootinfo;
57
58struct arch_switch	archsw;		/* MI/MD interface boundary */
59
60static void		extract_currdev(void);
61static int		isa_inb(int port);
62static void		isa_outb(int port, int value);
63
64/* from vers.c */
65extern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
66
67/* XXX debugging */
68extern char end[];
69
70void
71main(void)
72{
73    int			i;
74
75    /* Pick up arguments */
76    kargs = (void *)__args;
77    initial_howto = kargs->howto;
78    initial_bootdev = kargs->bootdev;
79    initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo);
80
81    /*
82     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
83     *
84     * XXX better to locate end of memory and use that
85     */
86    setheap((void *)end, (void *)(end + (384 * 1024)));
87
88    /*
89     * XXX Chicken-and-egg problem; we want to have console output early, but some
90     * console attributes may depend on reading from eg. the boot device, which we
91     * can't do yet.
92     *
93     * We can use printf() etc. once this is done.
94     * If the previous boot stage has requested a serial console, prefer that.
95     */
96    if (initial_howto & RB_SERIAL)
97	setenv("console", "comconsole", 1);
98    cons_probe();
99
100    /*
101     * Initialise the block cache
102     */
103    bcache_init(32, 512);	/* 16k cache XXX tune this */
104
105    /*
106     * March through the device switch probing for things.
107     */
108    for (i = 0; devsw[i] != NULL; i++)
109	if (devsw[i]->dv_init != NULL)
110	    (devsw[i]->dv_init)();
111
112    printf("\n");
113    printf("%s, Revision %s  %d/%dkB\n", bootprog_name, bootprog_rev, getbasemem(), getextmem());
114    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
115
116    extract_currdev();				/* set $currdev and $loaddev */
117    setenv("LINES", "24", 1);			/* optional */
118
119    archsw.arch_autoload = i386_autoload;
120    archsw.arch_getdev = i386_getdev;
121    archsw.arch_copyin = i386_copyin;
122    archsw.arch_copyout = i386_copyout;
123    archsw.arch_readin = i386_readin;
124    archsw.arch_isainb = isa_inb;
125    archsw.arch_isaoutb = isa_outb;
126
127    interact();			/* doesn't return */
128}
129
130/*
131 * Set the 'current device' by (if possible) recovering the boot device as
132 * supplied by the initial bootstrap.
133 *
134 * XXX should be extended for netbooting.
135 */
136static void
137extract_currdev(void)
138{
139    struct i386_devdesc	currdev;
140    int			major, biosdev;
141
142    /* We're booting from a BIOS disk, try to spiff this */
143    currdev.d_dev = devsw[0];				/* XXX presumes that biosdisk is first in devsw */
144    currdev.d_type = currdev.d_dev->dv_type;
145
146    if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
147	/* The passed-in boot device is bad */
148	currdev.d_kind.biosdisk.slice = -1;
149	currdev.d_kind.biosdisk.partition = 0;
150	biosdev = -1;
151    } else {
152	currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + B_CONTROLLER(initial_bootdev) - 1;
153	currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
154	biosdev = initial_bootinfo->bi_bios_dev;
155	major = B_TYPE(initial_bootdev);
156
157	/*
158	 * If we are booted by an old bootstrap, we have to guess at the BIOS
159	 * unit number.  We will loose if there is more than one disk type
160	 * and we are not booting from the lowest-numbered disk type
161	 * (ie. SCSI when IDE also exists).
162	 */
163#ifdef PC98
164	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {	/* biosdev doesn't match major */
165	    if (B_TYPE(initial_bootdev) == 6)
166		biosdev = 0x30 + B_UNIT(initial_bootdev);
167	    else
168		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
169	}
170#else
171	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
172	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
173#endif
174    }
175
176    if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) {
177	printf("Can't work out which disk we are booting from.\n"
178	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
179	currdev.d_kind.biosdisk.unit = 0;
180    }
181    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
182    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset);
183}
184
185COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
186
187static int
188command_reboot(int argc, char *argv[])
189{
190
191    printf("Rebooting...\n");
192    delay(1000000);
193    __exit(0);
194}
195
196/* provide this for panic, as it's not in the startup code */
197void
198exit(int code)
199{
200    __exit(code);
201}
202
203COMMAND_SET(heap, "heap", "show heap usage", command_heap);
204
205static int
206command_heap(int argc, char *argv[])
207{
208    mallocstats();
209    printf("heap base at %p, top at %p\n", end, sbrk(0));
210    return(CMD_OK);
211}
212
213/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
214static int
215isa_inb(int port)
216{
217    u_char	data;
218
219    if (__builtin_constant_p(port) &&
220	(((port) & 0xffff) < 0x100) &&
221	((port) < 0x10000)) {
222	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
223    } else {
224	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
225    }
226    return(data);
227}
228
229static void
230isa_outb(int port, int value)
231{
232    u_char	al = value;
233
234    if (__builtin_constant_p(port) &&
235	(((port) & 0xffff) < 0x100) &&
236	((port) < 0x10000)) {
237	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
238    } else {
239        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
240    }
241}
242
243