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
37 #include "bootstrap.h"
39 static int autoboot(int timeout, char *prompt);
40 static char *getbootfile(int try);
41 static int loadakernel(int try, int argc, char* argv[]);
43 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
44 static const char *default_bootfiles = "kernel";
46 static int autoboot_tried;
49 * The user wants us to boot.
51 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
54 command_boot(int argc, char *argv[])
56 struct preloaded_file *fp;
59 * See if the user has specified an explicit kernel to boot.
61 if ((argc > 1) && (argv[1][0] != '-')) {
63 /* XXX maybe we should discard everything and start again? */
64 if (file_findfile(NULL, NULL) != NULL) {
65 snprintf(command_errbuf, sizeof(command_errbuf),
66 "can't boot '%s', kernel module already loaded", argv[1]);
70 /* find/load the kernel module */
71 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
73 /* we have consumed all arguments */
78 * See if there is a kernel module already loaded
80 if (file_findfile(NULL, NULL) == NULL)
81 if (loadakernel(0, argc - 1, argv + 1))
82 /* we have consumed all arguments */
86 * Loaded anything yet?
88 if ((fp = file_findfile(NULL, NULL)) == NULL) {
89 command_errmsg = "no bootable kernel";
94 * If we were given arguments, discard any previous.
95 * XXX should we merge arguments? Hard to DWIM.
98 if (fp->f_args != NULL)
100 fp->f_args = unargv(argc - 1, argv + 1);
103 /* Hook for platform-specific autoloading of modules */
104 if (archsw.arch_autoload() != 0)
107 /* Call the exec handler from the loader matching the kernel */
108 file_formats[fp->f_loader]->l_exec(fp);
114 * Autoboot after a delay
117 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
120 command_autoboot(int argc, char *argv[])
132 howlong = strtol(argv[1], &cp, 0);
134 snprintf(command_errbuf, sizeof(command_errbuf),
135 "bad delay '%s'", argv[1]);
140 return(autoboot(howlong, prompt));
143 command_errmsg = "too many arguments";
148 * Called before we go interactive. If we think we can autoboot, and
149 * we haven't tried already, try now.
156 cp = getenv("autoboot_delay");
157 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
158 autoboot(-1, NULL); /* try to boot automatically */
162 autoboot(int timeout, char *prompt)
164 time_t when, otime, ntime;
166 char *argv[2], *cp, *ep;
168 #ifdef BOOT_PROMPT_123
169 const char *seq = "123", *p = seq;
176 /* try to get a delay from the environment */
177 if ((cp = getenv("autoboot_delay"))) {
178 timeout = strtol(cp, &ep, 0);
180 timeout = 10; /* Unparseable? Set default! */
184 kernelname = getenv("kernelname");
185 if (kernelname == NULL) {
187 loadakernel(0, 0, argv);
188 kernelname = getenv("kernelname");
189 if (kernelname == NULL) {
190 command_errmsg = "no valid kernel found";
197 when = otime + timeout; /* when to boot */
201 #ifdef BOOT_PROMPT_123
202 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
203 "1 2 3 sequence for command prompt." : prompt);
205 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
211 #ifdef BOOT_PROMPT_123
212 if ((c == '\r') || (c == '\n')) {
215 } else if (c != *p++)
220 if ((c == '\r') || (c == '\n'))
231 if (ntime != otime) {
232 printf("\rBooting [%s] in %d second%s... ",
233 kernelname, (int)(when - ntime),
234 (when-ntime)==1?"":"s");
243 printf("\rBooting [%s]... ", kernelname);
248 return(command_boot(1, argv));
254 * Scrounge for the name of the (try)'th file we will try to boot.
259 static char *name = NULL;
260 const char *spec, *ep;
263 /* we use dynamic storage */
270 * Try $bootfile, then try our builtin default
272 if ((spec = getenv("bootfile")) == NULL)
273 spec = default_bootfiles;
275 while ((try > 0) && (spec != NULL)) {
276 spec = strchr(spec, ';');
278 spec++; /* skip over the leading ';' */
282 if ((ep = strchr(spec, ';')) != NULL) {
287 name = malloc(len + 1);
288 strncpy(name, spec, len);
291 if (name && name[0] == 0) {
299 * Try to find the /etc/fstab file on the filesystem (rootdev),
300 * which should be be the root filesystem, and parse it to find
301 * out what the kernel ought to think the root filesystem is.
303 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
304 * so that the kernel can tell both which VFS and which node to use
305 * to mount the device. If this variable's already set, don't
309 getrootmount(char *rootdev)
311 char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
314 if (getenv("vfs.root.mountfrom") != NULL)
318 sprintf(lbuf, "%s/etc/fstab", rootdev);
319 if ((fd = open(lbuf, O_RDONLY)) < 0)
322 /* loop reading lines from /etc/fstab What was that about sscanf again? */
325 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
326 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
329 /* skip device name */
330 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
332 if (*cp == 0) /* misformatted */
334 /* delimit and save */
339 /* skip whitespace up to mountpoint */
340 while ((*cp != 0) && isspace(*cp))
342 /* must have /<space> to be root */
343 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
345 /* skip whitespace up to fstype */
347 while ((*cp != 0) && isspace(*cp))
349 if (*cp == 0) /* misformatted */
351 /* skip text to end of fstype and delimit */
353 while ((*cp != 0) && !isspace(*cp))
359 /* skip whitespace up to mount options */
361 while ((*cp != 0) && isspace(*cp))
363 if (*cp == 0) /* misformatted */
365 /* skip text to end of mount options and delimit */
367 while ((*cp != 0) && !isspace(*cp))
370 options = strdup(ep);
371 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
372 sprintf(lbuf, "%s:%s", fstyp, dev);
373 setenv("vfs.root.mountfrom", lbuf, 0);
375 /* Don't override vfs.root.mountfrom.options if it is already set */
376 if (getenv("vfs.root.mountfrom.options") == NULL) {
377 /* save mount options */
378 setenv("vfs.root.mountfrom.options", options, 0);
392 currdev = getenv("currdev");
393 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
394 cp = strdup(currdev);
395 cp[strlen(cp) - 1] = '\0';
396 setenv("vfs.root.mountfrom", cp, 0);
406 loadakernel(int try, int argc, char* argv[])
410 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
411 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
412 printf("can't load '%s'\n", cp);