main.c revision 53218
161454Sdfr/*-
261454Sdfr * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
370711Sobrien * All rights reserved.
461454Sdfr *
570711Sobrien * Redistribution and use in source and binary forms, with or without
6110844Sanholt * modification, are permitted provided that the following conditions
7110844Sanholt * are met:
8119368Smdodd * 1. Redistributions of source code must retain the above copyright
9119368Smdodd *    notice, this list of conditions and the following disclaimer.
10110844Sanholt * 2. Redistributions in binary form must reproduce the above copyright
11110844Sanholt *    notice, this list of conditions and the following disclaimer in the
12110844Sanholt *    documentation and/or other materials provided with the distribution.
13110844Sanholt *
1470711Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15106861Smux * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1670711Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1770711Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1889244Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1989244Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2089244Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2189244Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2289244Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2389244Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2489244Smsmith * SUCH DAMAGE.
2589244Smsmith *
2689244Smsmith * $FreeBSD: head/sys/boot/pc98/loader/main.c 53218 1999-11-16 11:59:19Z nyan $
2789244Smsmith */
2889244Smsmith
2963510Speter/*
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;
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	}
174#else
175	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
176	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
177#endif
178    }
179
180    if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) {
181	printf("Can't work out which disk we are booting from.\n"
182	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
183	currdev.d_kind.biosdisk.unit = 0;
184    }
185    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
186    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset);
187}
188
189COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
190
191static int
192command_reboot(int argc, char *argv[])
193{
194
195    printf("Rebooting...\n");
196    delay(1000000);
197    __exit(0);
198}
199
200/* provide this for panic, as it's not in the startup code */
201void
202exit(int code)
203{
204    __exit(code);
205}
206
207COMMAND_SET(heap, "heap", "show heap usage", command_heap);
208
209static int
210command_heap(int argc, char *argv[])
211{
212    mallocstats();
213    printf("heap base at %p, top at %p\n", end, sbrk(0));
214    return(CMD_OK);
215}
216
217/* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
218static int
219isa_inb(int port)
220{
221    u_char	data;
222
223    if (__builtin_constant_p(port) &&
224	(((port) & 0xffff) < 0x100) &&
225	((port) < 0x10000)) {
226	__asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
227    } else {
228	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
229    }
230    return(data);
231}
232
233static void
234isa_outb(int port, int value)
235{
236    u_char	al = value;
237
238    if (__builtin_constant_p(port) &&
239	(((port) & 0xffff) < 0x100) &&
240	((port) < 0x10000)) {
241	__asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
242    } else {
243        __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
244    }
245}
246
247