boot.c revision 40327
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: boot.c,v 1.6 1998/10/11 10:10:41 peter Exp $
27 */
28
29/*
30 * Loading modules, booting the system
31 */
32
33#include <stand.h>
34#include <string.h>
35
36#include "bootstrap.h"
37
38static char	*getbootfile(int try);
39
40/* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
41static char *default_bootfiles = "kernel,kernel.old";
42
43static int autoboot_tried;
44
45/*
46 * The user wants us to boot.
47 */
48COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
49
50static int
51command_boot(int argc, char *argv[])
52{
53    struct loaded_module	*km;
54    char			*cp;
55    int				try;
56
57    /*
58     * See if the user has specified an explicit kernel to boot.
59     */
60    if ((argc > 1) && (argv[1][0] != '-')) {
61
62	/* XXX maybe we should discard everything and start again? */
63	if (mod_findmodule(NULL, NULL) != NULL) {
64	    sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[0]);
65	    return(CMD_ERROR);
66	}
67
68	/* find/load the kernel module */
69	if (mod_load(argv[1], argc - 2, argv + 2) != 0)
70	    return(CMD_ERROR);
71	/* we have consumed all arguments */
72	argc = 1;
73    }
74
75    /*
76     * See if there is a kernel module already loaded
77     */
78    if (mod_findmodule(NULL, NULL) == NULL) {
79	for (try = 0; (cp = getbootfile(try)) != NULL; try++) {
80	    if (mod_load(cp, argc - 1, argv + 1) != 0) {
81		printf("can't load '%s'\n", cp);
82	    } else {
83		/* we have consumed all arguments */
84		argc = 1;
85		break;
86	    }
87	}
88    }
89
90    /*
91     * Loaded anything yet?
92     */
93    if ((km = mod_findmodule(NULL, NULL)) == NULL) {
94	command_errmsg = "no bootable kernel";
95	return(CMD_ERROR);
96    }
97
98    /*
99     * If we were given arguments, discard any previous.
100     * XXX should we merge arguments?  Hard to DWIM.
101     */
102    if (argc > 1) {
103	if (km->m_args != NULL)
104	    free(km->m_args);
105	km->m_args = unargv(argc - 1, argv + 1);
106    }
107
108    /* Hook for platform-specific autoloading of modules */
109    if (archsw.arch_autoload() != 0)
110	return(CMD_ERROR);
111
112    /* Call the exec handler from the loader matching the kernel */
113    module_formats[km->m_loader]->l_exec(km);
114    return(CMD_ERROR);
115}
116
117
118/*
119 * Autoboot after a delay
120 */
121
122COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
123
124static int
125command_autoboot(int argc, char *argv[])
126{
127    int		howlong;
128    char	*cp, *prompt;
129
130    prompt = NULL;
131    howlong = -1;
132    switch(argc) {
133    case 3:
134	prompt = argv[2];
135	/* FALLTHROUGH */
136    case 2:
137	howlong = strtol(argv[1], &cp, 0);
138	if (*cp != 0) {
139	    sprintf(command_errbuf, "bad delay '%s'", argv[1]);
140	    return(CMD_ERROR);
141	}
142	/* FALLTHROUGH */
143    case 1:
144	return(autoboot(howlong, prompt));
145    }
146
147    command_errmsg = "too many arguments";
148    return(CMD_ERROR);
149}
150
151/*
152 * Called before we go interactive.  If we think we can autoboot, and
153 * we haven't tried already, try now.
154 */
155void
156autoboot_maybe()
157{
158    char	*cp;
159
160    cp = getenv("autoboot_delay");
161    if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
162	autoboot(-1, NULL);		/* try to boot automatically */
163}
164
165int
166autoboot(int delay, char *prompt)
167{
168    time_t	when, otime, ntime;
169    int		c, yes, cr;
170    char	*argv[2], *cp, *ep;
171
172    autoboot_tried = 1;
173
174    if (delay == -1) {
175	/* try to get a delay from the environment */
176	if ((cp = getenv("autoboot_delay"))) {
177	    delay = strtol(cp, &ep, 0);
178	    if (cp == ep)
179		delay = -1;
180	}
181    }
182    if (delay == -1)		/* all else fails */
183	delay = 10;
184
185    otime = time(NULL);
186    when = otime + delay;	/* when to boot */
187    yes = 0;
188    cr = 0;
189
190    /* XXX could try to work out what we might boot */
191    printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
192
193    for (;;) {
194	if (ischar()) {
195	    c = getchar();
196	    if ((c == '\r') || (c == '\n'))
197		yes = 1;
198	    break;
199	}
200	ntime = time(NULL);
201	if (ntime >= when) {
202	    yes = 1;
203	    break;
204	}
205	if (ntime != otime) {
206	    printf("\rBooting [%s] in %d seconds...", getbootfile(0), (int)(when - ntime));
207	    otime = ntime;
208	    cr = 1;
209	}
210    }
211    if (cr)
212	putchar('\n');
213    if (yes) {
214	argv[0] = "boot";
215	argv[1] = NULL;
216	return(command_boot(1, argv));
217    }
218    return(CMD_OK);
219}
220
221/*
222 * Scrounge for the name of the (try)'th file we will try to boot.
223 */
224static char *
225getbootfile(int try)
226{
227    static char *name = NULL;
228    char	*spec, *ep;
229    int		len;
230
231    /* we use dynamic storage */
232    if (name != NULL) {
233	free(name);
234	name = NULL;
235    }
236
237    /*
238     * Try $bootfile, then try our builtin default
239     */
240    if ((spec = getenv("bootfile")) == NULL)
241	spec = default_bootfiles;
242
243    while ((try > 0) && (spec != NULL)) {
244	spec = strchr(spec, ',');
245	try--;
246    }
247    if (spec != NULL) {
248	if ((ep = strchr(spec, ',')) != NULL) {
249	    len = ep - spec;
250	} else {
251	    len = strlen(spec);
252	}
253	name = malloc(len + 1);
254	strncpy(name, spec, len);
255	name[len] = 0;
256    }
257    if (name[0] == 0) {
258	free(name);
259	name = NULL;
260    }
261    return(name);
262}
263
264