main.c revision 38764
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.2 1998/08/31 21:10:43 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
37#include "bootstrap.h"
38#include "libi386/libi386.h"
39
40extern int		boot_biosdev;	/* from runtime startup */
41
42struct arch_switch	archsw;		/* MI/MD interface boundary */
43
44/* from vers.c */
45extern	char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
46
47/* XXX debugging */
48#include "libi386/crt/diskbuf.h"
49extern char stackbase, stacktop;
50extern char end[];
51
52void
53main(void)
54{
55    struct i386_devdesc	currdev;
56    int			i;
57
58    /*
59     * Initialise the heap as early as possible.  Once this is done, alloc() is usable.
60     * The stack is buried inside us, so this is safe
61     */
62    setheap((void *)end, (void *)(end + 0x80000));
63
64    /*
65     * XXX Chicken-and-egg problem; we want to have console output early, but some
66     * console attributes may depend on reading from eg. the boot device, which we
67     * can't do yet.
68     *
69     * We can use printf() etc. once this is done.
70     */
71    cons_probe();
72
73    /*
74     * March through the device switch probing for things.
75     */
76    for (i = 0; devsw[i] != NULL; i++)
77	if (devsw[i]->dv_init != NULL)
78	    (devsw[i]->dv_init)();
79
80    printf("\n");
81    printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
82    printf("(%s, %s)\n", bootprog_maker, bootprog_date);
83    printf("memory: %d/%dkB\n", getbasemem(), getextmem());
84#if 0
85    printf("diskbuf at %p, %d sectors\n", &diskbuf, diskbuf_size);
86    printf("using %d bytes of stack at %p\n",  (&stacktop - &stackbase), &stacktop);
87#endif
88
89    /* We're booting from a BIOS disk, try to spiff this */
90    currdev.d_dev = devsw[0];				/* XXX presumes that biosdisk is first in devsw */
91    currdev.d_type = currdev.d_dev->dv_type;
92    currdev.d_kind.biosdisk.unit = boot_biosdev;
93    currdev.d_kind.biosdisk.slice = -1;			/* XXX should be able to detect this, default to autoprobe */
94    currdev.d_kind.biosdisk.partition = 0;		/* default to 'a' */
95
96    /* Create i386-specific variables */
97
98    env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev), i386_setcurrdev, env_nounset);
99    env_setenv("loaddev", EV_VOLATILE,  i386_fmtdev(&currdev), env_noset, env_nounset);
100    setenv("LINES", "24", 1);				/* optional */
101
102    archsw.arch_autoload = i386_autoload;
103    archsw.arch_getdev = i386_getdev;
104    archsw.arch_copyin = i386_copyin;
105    archsw.arch_copyout = i386_copyout;
106    archsw.arch_readin = i386_readin;
107    /*
108     * XXX should these be in the MI source?
109     */
110#if 0
111    legacy_config();		/* read old /boot.config file */
112#endif
113    interact();			/* doesn't return */
114}
115
116COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
117
118static int
119command_reboot(int argc, char *argv[])
120{
121
122    printf("Rebooting...\n");
123    delay(1000000);
124    reboot();
125    /* Note: we shouldn't get to this point! */
126    panic("Reboot failed!");
127    exit(0);
128}
129
130COMMAND_SET(stack, "stack", "show stack usage", command_stack);
131
132static int
133command_stack(int argc, char *argv[])
134{
135    char	*cp;
136
137    for (cp = &stackbase; cp < &stacktop; cp++)
138	if (*cp != 0)
139	    break;
140
141    printf("%d bytes of stack used\n", &stacktop - cp);
142    return(CMD_OK);
143}
144
145COMMAND_SET(heap, "heap", "show heap usage", command_heap);
146
147static int
148command_heap(int argc, char *argv[])
149{
150    printf("heap base at %p, top at %p, used %d\n", end, sbrk(0), sbrk(0) - end);
151    return(CMD_OK);
152}
153