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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Loading modules, booting the system
35 #include <sys/reboot.h>
39 #include "bootstrap.h"
41 static int autoboot(int timeout, char *prompt);
42 static char *getbootfile(int try);
43 static int loadakernel(int try, int argc, char* argv[]);
45 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
46 static const char *default_bootfiles = "kernel";
48 static int autoboot_tried;
51 * The user wants us to boot.
53 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
56 command_boot(int argc, char *argv[])
58 struct preloaded_file *fp;
61 * See if the user has specified an explicit kernel to boot.
63 if ((argc > 1) && (argv[1][0] != '-')) {
65 /* XXX maybe we should discard everything and start again? */
66 if (file_findfile(NULL, NULL) != NULL) {
67 snprintf(command_errbuf, sizeof(command_errbuf),
68 "can't boot '%s', kernel module already loaded", argv[1]);
72 /* find/load the kernel module */
73 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
75 /* we have consumed all arguments */
80 * See if there is a kernel module already loaded
82 if (file_findfile(NULL, NULL) == NULL)
83 if (loadakernel(0, argc - 1, argv + 1))
84 /* we have consumed all arguments */
88 * Loaded anything yet?
90 if ((fp = file_findfile(NULL, NULL)) == NULL) {
91 command_errmsg = "no bootable kernel";
96 * If we were given arguments, discard any previous.
97 * XXX should we merge arguments? Hard to DWIM.
100 if (fp->f_args != NULL)
102 fp->f_args = unargv(argc - 1, argv + 1);
105 /* Hook for platform-specific autoloading of modules */
106 if (archsw.arch_autoload() != 0)
109 /* Call the exec handler from the loader matching the kernel */
110 file_formats[fp->f_loader]->l_exec(fp);
116 * Autoboot after a delay
119 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
122 command_autoboot(int argc, char *argv[])
134 howlong = strtol(argv[1], &cp, 0);
136 snprintf(command_errbuf, sizeof(command_errbuf),
137 "bad delay '%s'", argv[1]);
142 return(autoboot(howlong, prompt));
145 command_errmsg = "too many arguments";
150 * Called before we go interactive. If we think we can autoboot, and
151 * we haven't tried already, try now.
158 cp = getenv("autoboot_delay");
159 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
160 autoboot(-1, NULL); /* try to boot automatically */
169 for (howto = 0, i = 0; howto_names[i].ev != NULL; i++) {
170 val = getenv(howto_names[i].ev);
171 if (val != NULL && strcasecmp(val, "no") != 0)
172 howto |= howto_names[i].mask;
178 bootenv_set(int howto)
182 for (i = 0; howto_names[i].ev != NULL; i++)
183 if (howto & howto_names[i].mask)
184 setenv(howto_names[i].ev, "YES", 1);
188 autoboot(int timeout, char *prompt)
190 time_t when, otime, ntime;
192 char *argv[2], *cp, *ep;
194 #ifdef BOOT_PROMPT_123
195 const char *seq = "123", *p = seq;
202 /* try to get a delay from the environment */
203 if ((cp = getenv("autoboot_delay"))) {
204 timeout = strtol(cp, &ep, 0);
206 timeout = 10; /* Unparseable? Set default! */
210 kernelname = getenv("kernelname");
211 if (kernelname == NULL) {
213 loadakernel(0, 0, argv);
214 kernelname = getenv("kernelname");
215 if (kernelname == NULL) {
216 command_errmsg = "no valid kernel found";
223 when = otime + timeout; /* when to boot */
227 #ifdef BOOT_PROMPT_123
228 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
229 "1 2 3 sequence for command prompt." : prompt);
231 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
237 #ifdef BOOT_PROMPT_123
238 if ((c == '\r') || (c == '\n')) {
241 } else if (c != *p++)
246 if ((c == '\r') || (c == '\n'))
257 if (ntime != otime) {
258 printf("\rBooting [%s] in %d second%s... ",
259 kernelname, (int)(when - ntime),
260 (when-ntime)==1?"":"s");
269 printf("\rBooting [%s]... ", kernelname);
274 return(command_boot(1, argv));
280 * Scrounge for the name of the (try)'th file we will try to boot.
285 static char *name = NULL;
286 const char *spec, *ep;
289 /* we use dynamic storage */
296 * Try $bootfile, then try our builtin default
298 if ((spec = getenv("bootfile")) == NULL)
299 spec = default_bootfiles;
301 while ((try > 0) && (spec != NULL)) {
302 spec = strchr(spec, ';');
304 spec++; /* skip over the leading ';' */
308 if ((ep = strchr(spec, ';')) != NULL) {
313 name = malloc(len + 1);
314 strncpy(name, spec, len);
317 if (name && name[0] == 0) {
325 * Try to find the /etc/fstab file on the filesystem (rootdev),
326 * which should be be the root filesystem, and parse it to find
327 * out what the kernel ought to think the root filesystem is.
329 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
330 * so that the kernel can tell both which VFS and which node to use
331 * to mount the device. If this variable's already set, don't
335 getrootmount(char *rootdev)
337 char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
340 if (getenv("vfs.root.mountfrom") != NULL)
344 sprintf(lbuf, "%s/etc/fstab", rootdev);
345 if ((fd = open(lbuf, O_RDONLY)) < 0)
348 /* loop reading lines from /etc/fstab What was that about sscanf again? */
351 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
352 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
355 /* skip device name */
356 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
358 if (*cp == 0) /* misformatted */
360 /* delimit and save */
365 /* skip whitespace up to mountpoint */
366 while ((*cp != 0) && isspace(*cp))
368 /* must have /<space> to be root */
369 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
371 /* skip whitespace up to fstype */
373 while ((*cp != 0) && isspace(*cp))
375 if (*cp == 0) /* misformatted */
377 /* skip text to end of fstype and delimit */
379 while ((*cp != 0) && !isspace(*cp))
385 /* skip whitespace up to mount options */
387 while ((*cp != 0) && isspace(*cp))
389 if (*cp == 0) /* misformatted */
391 /* skip text to end of mount options and delimit */
393 while ((*cp != 0) && !isspace(*cp))
396 options = strdup(ep);
397 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
398 sprintf(lbuf, "%s:%s", fstyp, dev);
399 setenv("vfs.root.mountfrom", lbuf, 0);
401 /* Don't override vfs.root.mountfrom.options if it is already set */
402 if (getenv("vfs.root.mountfrom.options") == NULL) {
403 /* save mount options */
404 setenv("vfs.root.mountfrom.options", options, 0);
418 currdev = getenv("currdev");
419 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
420 cp = strdup(currdev);
421 cp[strlen(cp) - 1] = '\0';
422 setenv("vfs.root.mountfrom", cp, 0);
432 loadakernel(int try, int argc, char* argv[])
436 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
437 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
438 printf("can't load '%s'\n", cp);