2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/boot/common/boot.c,v 1.15.2.5 2000/12/28 13:12:33 ps Exp $
30 * Loading modules, booting the system
36 #include "bootstrap.h"
38 static char *getbootfile(int try);
40 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
41 static const char *default_bootfiles = "kernel;kernel.old";
43 static int autoboot_tried;
46 * The user wants us to boot.
48 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
51 command_boot(int argc, char *argv[])
53 struct loaded_module *km;
58 * See if the user has specified an explicit kernel to boot.
60 if ((argc > 1) && (argv[1][0] != '-')) {
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[1]);
68 /* find/load the kernel module */
69 if (mod_load(argv[1], argc - 2, argv + 2) != 0)
71 /* we have consumed all arguments */
76 * See if there is a kernel module already loaded
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);
83 /* we have consumed all arguments */
91 * Loaded anything yet?
93 if ((km = mod_findmodule(NULL, NULL)) == NULL) {
94 command_errmsg = "no bootable kernel";
99 * If we were given arguments, discard any previous.
100 * XXX should we merge arguments? Hard to DWIM.
103 if (km->m_args != NULL)
105 km->m_args = unargv(argc - 1, argv + 1);
108 /* Hook for platform-specific autoloading of modules */
109 if (archsw.arch_autoload() != 0)
112 /* Call the exec handler from the loader matching the kernel */
113 module_formats[km->m_loader]->l_exec(km);
119 * Autoboot after a delay
122 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
125 command_autoboot(int argc, char *argv[])
137 howlong = strtol(argv[1], &cp, 0);
139 sprintf(command_errbuf, "bad delay '%s'", argv[1]);
144 return(autoboot(howlong, prompt));
147 command_errmsg = "too many arguments";
152 * Called before we go interactive. If we think we can autoboot, and
153 * we haven't tried already, try now.
160 cp = getenv("autoboot_delay");
161 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
162 autoboot(-1, NULL); /* try to boot automatically */
166 autoboot(int timeout, char *prompt)
168 time_t when, otime, ntime;
170 char *argv[2], *cp, *ep;
175 /* try to get a delay from the environment */
176 if ((cp = getenv("autoboot_delay"))) {
177 timeout = strtol(cp, &ep, 0);
182 if (timeout == -1) /* all else fails */
186 when = otime + timeout; /* when to boot */
189 /* XXX could try to work out what we might boot */
190 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
195 if ((c == '\r') || (c == '\n'))
204 if (ntime != otime) {
205 printf("\rBooting [%s] in %d second%s... ",
206 getbootfile(0), (int)(when - ntime),
207 (when-ntime)==1?"":"s");
212 printf("\rBooting [%s]... ", getbootfile(0));
217 return(command_boot(1, argv));
223 * Scrounge for the name of the (try)'th file we will try to boot.
228 static char *name = NULL;
232 /* we use dynamic storage */
239 * Try $bootfile, then try our builtin default
241 if ((spec = getenv("bootfile")) == NULL)
242 spec = default_bootfiles;
244 while ((try > 0) && (spec != NULL)) {
245 spec = strchr(spec, ';');
247 spec++; /* skip over the leading ';' */
251 if ((ep = strchr(spec, ';')) != NULL) {
256 name = malloc(len + 1);
257 strncpy(name, spec, len);
260 if (name && name[0] == 0) {
268 * Try to find the /etc/fstab file on the filesystem (rootdev),
269 * which should be be the root filesystem, and parse it to find
270 * out what the kernel ought to think the root filesystem is.
272 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
273 * so that the kernel can tell both which VFS and which node to use
274 * to mount the device. If this variable's already set, don't
278 getrootmount(char *rootdev)
280 char lbuf[128], *cp, *ep, *dev, *fstyp;
283 if (getenv("vfs.root.mountfrom") != NULL)
286 sprintf(lbuf, "%s/etc/fstab", rootdev);
287 if ((fd = open(lbuf, O_RDONLY)) < 0)
290 /* loop reading lines from /etc/fstab What was that about sscanf again? */
292 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
293 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
296 /* skip device name */
297 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
299 if (*cp == 0) /* misformatted */
301 /* delimit and save */
305 /* skip whitespace up to mountpoint */
306 while ((*cp != 0) && isspace(*cp))
308 /* must have /<space> to be root */
309 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
311 /* skip whitespace up to fstype */
313 while ((*cp != 0) && isspace(*cp))
315 if (*cp == 0) /* misformatted */
317 /* skip text to end of fstype and delimit */
319 while ((*cp != 0) && !isspace(*cp))
324 /* build the final result and save it */
325 sprintf(lbuf, "%s:%s", fstyp, dev);
328 setenv("vfs.root.mountfrom", lbuf, 0);