main.c revision 39897
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.8 1998/09/28 20:17:05 peter 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 "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
54struct bootinfo	*initial_bootinfo;
55
56struct arch_switch	archsw;		/* MI/MD interface boundary */
57
58static void		extract_currdev(void);
59
60/* from vers.c */
61extern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
62
63/* XXX debugging */
64extern char end[];
65
66void
67main(void)
68{
69    int			i;
70
71    /* Pick up arguments */
72    kargs = (void *)__args;
73    initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo);
74
75    /*
76     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
77     *
78     * XXX better to locate end of memory and use that
79     */
80    setheap((void *)end, (void *)(end + (384 * 1024)));
81
82    /*
83     * XXX Chicken-and-egg problem; we want to have console output early, but some
84     * console attributes may depend on reading from eg. the boot device, which we
85     * can't do yet.
86     *
87     * We can use printf() etc. once this is done.
88     * If the previous boot stage has requested a serial console, prefer that.
89     */
90    if (kargs->howto & RB_SERIAL)
91	setenv("console", "comconsole", 1);
92    cons_probe();
93
94    /*
95     * March through the device switch probing for things.
96     */
97    for (i = 0; devsw[i] != NULL; i++)
98	if (devsw[i]->dv_init != NULL)
99	    (devsw[i]->dv_init)();
100
101    printf("\n");
102    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
103    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
104    printf("memory: %d/%dkB\n", getbasemem(), getextmem());
105#if 0
106    printf("diskbuf at %p, %d sectors\n", &diskbuf, diskbuf_size);
107    printf("using %d bytes of stack at %p\n",  (&stacktop - &stackbase), &stacktop);
108#endif
109
110    extract_currdev();				/* set $currdev and $loaddev */
111    setenv("LINES", "24", 1);			/* optional */
112
113    archsw.arch_autoload = i386_autoload;
114    archsw.arch_getdev = i386_getdev;
115    archsw.arch_copyin = i386_copyin;
116    archsw.arch_copyout = i386_copyout;
117    archsw.arch_readin = i386_readin;
118    /*
119     * XXX should these be in the MI source?
120     */
121#if 0
122    legacy_config();		/* read old /boot.config file */
123#endif
124    interact();			/* doesn't return */
125}
126
127/*
128 * Set the 'current device' by (if possible) recovering the boot device as
129 * supplied by the initial bootstrap.
130 *
131 * XXX should be extended for netbooting.
132 */
133static void
134extract_currdev(void)
135{
136    struct i386_devdesc	currdev;
137    int			major, biosdev, i;
138
139    /* We're booting from a BIOS disk, try to spiff this */
140    currdev.d_dev = devsw[0];				/* XXX presumes that biosdisk is first in devsw */
141    currdev.d_type = currdev.d_dev->dv_type;
142    currdev.d_kind.biosdisk.slice = (B_ADAPTOR(kargs->bootdev) << 4) + B_CONTROLLER(kargs->bootdev) - 1;
143    currdev.d_kind.biosdisk.partition = B_PARTITION(kargs->bootdev);
144
145    biosdev = initial_bootinfo->bi_bios_dev;
146    major = B_TYPE(kargs->bootdev);
147
148    /*
149     * If we are booted by an old bootstrap, we have to guess at the BIOS
150     * unit number.  We will loose if there is more than one disk type
151     * and we are not booting from the lowest-numbered disk type.
152     */
153    if ((biosdev == 0) && (major != 2))			/* biosdev doesn't match major */
154	biosdev = 0x80 + B_UNIT(kargs->bootdev);	/* assume harddisk */
155
156    if ((currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1) {
157	printf("Can't work out which disk we are booting from, defaulting to disk0:\n");
158	currdev.d_kind.biosdisk.unit;
159    }
160    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
161    env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset, env_nounset);
162}
163
164COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
165
166static int
167command_reboot(int argc, char *argv[])
168{
169
170    printf("Rebooting...\n");
171    delay(1000000);
172    __exit(0);
173}
174
175/* provide this for panic, as it's not in the startup code */
176void
177exit(int code)
178{
179    __exit(code);
180}
181
182COMMAND_SET(heap, "heap", "show heap usage", command_heap);
183
184static int
185command_heap(int argc, char *argv[])
186{
187    mallocstats();
188    printf("heap base at %p, top at %p", end, sbrk(0));
189    return(CMD_OK);
190}
191