main.c revision 44463
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 *	$Id: main.c,v 1.1 1999/02/03 08:39:09 kato Exp $
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 "../../i386/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;
56#ifdef PC98
57struct bootinfo	*initial_bootinfo;
58#else
59static struct bootinfo	*initial_bootinfo;
60#endif
61
62struct arch_switch	archsw;		/* MI/MD interface boundary */
63
64static void		extract_currdev(void);
65static int		isa_inb(int port);
66static void		isa_outb(int port, int value);
67
68/* from vers.c */
69extern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
70
71/* XXX debugging */
72extern char end[];
73
74void
75main(void)
76{
77    int			i;
78
79    /* Pick up arguments */
80    kargs = (void *)__args;
81    initial_howto = kargs->howto;
82    initial_bootdev = kargs->bootdev;
83    initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo);
84
85    /*
86     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
87     *
88     * XXX better to locate end of memory and use that
89     */
90    setheap((void *)end, (void *)(end + (384 * 1024)));
91
92    /*
93     * XXX Chicken-and-egg problem; we want to have console output early, but some
94     * console attributes may depend on reading from eg. the boot device, which we
95     * can't do yet.
96     *
97     * We can use printf() etc. once this is done.
98     * If the previous boot stage has requested a serial console, prefer that.
99     */
100    if (initial_howto & RB_SERIAL)
101	setenv("console", "comconsole", 1);
102    cons_probe();
103
104    /*
105     * Initialise the block cache
106     */
107    bcache_init(32, 512);	/* 16k cache XXX tune this */
108
109    /*
110     * March through the device switch probing for things.
111     */
112    for (i = 0; devsw[i] != NULL; i++)
113	if (devsw[i]->dv_init != NULL)
114	    (devsw[i]->dv_init)();
115
116    printf("\n");
117    printf("%s, Revision %s  %d/%dkB\n", bootprog_name, bootprog_rev, getbasemem(), getextmem());
118    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
119
120    extract_currdev();				/* set $currdev and $loaddev */
121    setenv("LINES", "24", 1);			/* optional */
122
123    archsw.arch_autoload = i386_autoload;
124    archsw.arch_getdev = i386_getdev;
125    archsw.arch_copyin = i386_copyin;
126    archsw.arch_copyout = i386_copyout;
127    archsw.arch_readin = i386_readin;
128    archsw.arch_isainb = isa_inb;
129    archsw.arch_isaoutb = isa_outb;
130
131    interact();			/* doesn't return */
132}
133
134/*
135 * Set the 'current device' by (if possible) recovering the boot device as
136 * supplied by the initial bootstrap.
137 *
138 * XXX should be extended for netbooting.
139 */
140static void
141extract_currdev(void)
142{
143    struct i386_devdesc	currdev;
144    int			major, biosdev;
145
146    /* We're booting from a BIOS disk, try to spiff this */
147    currdev.d_dev = devsw[0];				/* XXX presumes that biosdisk is first in devsw */
148    currdev.d_type = currdev.d_dev->dv_type;
149
150    if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
151	/* The passed-in boot device is bad */
152	currdev.d_kind.biosdisk.slice = -1;
153	currdev.d_kind.biosdisk.partition = 0;
154	biosdev = -1;
155    } else {
156	currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) + B_CONTROLLER(initial_bootdev) - 1;
157	currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
158	biosdev = initial_bootinfo->bi_bios_dev;
159	major = B_TYPE(initial_bootdev);
160
161	/*
162	 * If we are booted by an old bootstrap, we have to guess at the BIOS
163	 * unit number.  We will loose if there is more than one disk type
164	 * and we are not booting from the lowest-numbered disk type
165	 * (ie. SCSI when IDE also exists).
166	 */
167#ifdef PC98
168	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
169	    if (B_TYPE(initial_bootdev) == 6)
170		biosdev = 0x30 + B_UNIT(initial_bootdev);
171	    else
172		biosdev = (major << 3) + 0x80 + B_UNIT(initial_bootdev);
173#else
174	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
175	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
176#endif
177    }
178
179    if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) {
180	printf("Can't work out which disk we are booting from.\n"
181	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
182	currdev.d_kind.biosdisk.unit = 0;
183    }
184    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
185    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset);
186}
187
188COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
189
190static int
191command_reboot(int argc, char *argv[])
192{
193
194    printf("Rebooting...\n");
195    delay(1000000);
196    __exit(0);
197}
198
199/* provide this for panic, as it's not in the startup code */
200void
201exit(int code)
202{
203    __exit(code);
204}
205
206COMMAND_SET(heap, "heap", "show heap usage", command_heap);
207
208static int
209command_heap(int argc, char *argv[])
210{
211    mallocstats();
212    printf("heap base at %p, top at %p\n", end, sbrk(0));
213    return(CMD_OK);
214}
215
216/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
217static int
218isa_inb(int port)
219{
220    u_char	data;
221
222    if (__builtin_constant_p(port) &&
223	(((port) & 0xffff) < 0x100) &&
224	((port) < 0x10000)) {
225	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
226    } else {
227	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
228    }
229    return(data);
230}
231
232static void
233isa_outb(int port, int value)
234{
235    u_char	al = value;
236
237    if (__builtin_constant_p(port) &&
238	(((port) & 0xffff) < 0x100) &&
239	((port) < 0x10000)) {
240	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
241    } else {
242        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
243    }
244}
245
246