main.c revision 39725
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.7 1998/09/26 01:31:10 msmith 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
58/* from vers.c */
59extern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
60
61/* XXX debugging */
62extern char end[];
63
64void
65main(void)
66{
67    struct i386_devdesc	currdev;
68    int			i;
69
70    /* Pick up arguments */
71    kargs = (void *)__args;
72    initial_bootinfo = (struct bootinfo *)PTOV(kargs->bootinfo);
73
74    /*
75     * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
76     *
77     * XXX better to locate end of memory and use that
78     */
79    setheap((void *)end, (void *)(end + (384 * 1024)));
80
81    /*
82     * XXX Chicken-and-egg problem; we want to have console output early, but some
83     * console attributes may depend on reading from eg. the boot device, which we
84     * can't do yet.
85     *
86     * We can use printf() etc. once this is done.
87     * If the previous boot stage has requested a serial console, prefer that.
88     */
89    if (kargs->howto & RB_SERIAL)
90	setenv("console", "comconsole", 1);
91    cons_probe();
92
93    /*
94     * March through the device switch probing for things.
95     */
96    for (i = 0; devsw[i] != NULL; i++)
97	if (devsw[i]->dv_init != NULL)
98	    (devsw[i]->dv_init)();
99
100    printf("\n");
101    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
102    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
103    printf("memory: %d/%dkB\n", getbasemem(), getextmem());
104#if 0
105    printf("diskbuf at %p, %d sectors\n", &diskbuf, diskbuf_size);
106    printf("using %d bytes of stack at %p\n",  (&stacktop - &stackbase), &stacktop);
107#endif
108
109    /* We're booting from a BIOS disk, try to spiff this */
110    currdev.d_dev = devsw[0];				/* XXX presumes that biosdisk is first in devsw */
111    currdev.d_type = currdev.d_dev->dv_type;
112    currdev.d_kind.biosdisk.unit = 0;			/* XXX wrong, need to get from bootinfo etc. */
113    currdev.d_kind.biosdisk.slice = -1;			/* XXX should be able to detect this, default to autoprobe */
114    currdev.d_kind.biosdisk.partition = 0;		/* default to 'a' */
115
116    /* Create i386-specific variables */
117
118    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
119    env_setenv("loaddev", EV_VOLATILE,  i386_fmtdev(&currdev), env_noset, env_nounset);
120    setenv("LINES", "24", 1);				/* optional */
121
122    archsw.arch_autoload = i386_autoload;
123    archsw.arch_getdev = i386_getdev;
124    archsw.arch_copyin = i386_copyin;
125    archsw.arch_copyout = i386_copyout;
126    archsw.arch_readin = i386_readin;
127    /*
128     * XXX should these be in the MI source?
129     */
130#if 0
131    legacy_config();		/* read old /boot.config file */
132#endif
133    interact();			/* doesn't return */
134}
135
136COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
137
138static int
139command_reboot(int argc, char *argv[])
140{
141
142    printf("Rebooting...\n");
143    delay(1000000);
144    __exit(0);
145}
146
147/* provide this for panic, as it's not in the startup code */
148void
149exit(int code)
150{
151    __exit(code);
152}
153
154#if 0 /* XXX learn to ask BTX */
155
156COMMAND_SET(stack, "stack", "show stack usage", command_stack);
157
158extern char stackbase, stacktop;
159
160static int
161command_stack(int argc, char *argv[])
162{
163    char	*cp;
164
165    for (cp = &stackbase; cp < &stacktop; cp++)
166	if (*cp != 0)
167	    break;
168
169    printf("%d bytes of stack used\n", &stacktop - cp);
170    return(CMD_OK);
171}
172#endif
173
174COMMAND_SET(heap, "heap", "show heap usage", command_heap);
175
176static int
177command_heap(int argc, char *argv[])
178{
179    mallocstats();
180    printf("heap base at %p, top at %p", end, sbrk(0));
181    return(CMD_OK);
182}
183