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 char *getbootfile(int try);
40 static int loadakernel(int try, int argc, char* argv[]);
42 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
43 static const char *default_bootfiles = "kernel";
45 static int autoboot_tried;
48 * The user wants us to boot.
50 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
53 command_boot(int argc, char *argv[])
55 struct preloaded_file *fp;
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 (file_findfile(NULL, NULL) != NULL) {
64 snprintf(command_errbuf, sizeof(command_errbuf),
65 "can't boot '%s', kernel module already loaded", argv[1]);
69 /* find/load the kernel module */
70 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
72 /* we have consumed all arguments */
77 * See if there is a kernel module already loaded
79 if (file_findfile(NULL, NULL) == NULL)
80 if (loadakernel(0, argc - 1, argv + 1))
81 /* we have consumed all arguments */
85 * Loaded anything yet?
87 if ((fp = file_findfile(NULL, NULL)) == NULL) {
88 command_errmsg = "no bootable kernel";
93 * If we were given arguments, discard any previous.
94 * XXX should we merge arguments? Hard to DWIM.
97 if (fp->f_args != NULL)
99 fp->f_args = unargv(argc - 1, argv + 1);
102 /* Hook for platform-specific autoloading of modules */
103 if (archsw.arch_autoload() != 0)
106 /* Call the exec handler from the loader matching the kernel */
107 file_formats[fp->f_loader]->l_exec(fp);
113 * Autoboot after a delay
116 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
119 command_autoboot(int argc, char *argv[])
131 howlong = strtol(argv[1], &cp, 0);
133 snprintf(command_errbuf, sizeof(command_errbuf),
134 "bad delay '%s'", argv[1]);
139 return(autoboot(howlong, prompt));
142 command_errmsg = "too many arguments";
147 * Called before we go interactive. If we think we can autoboot, and
148 * we haven't tried already, try now.
155 cp = getenv("autoboot_delay");
156 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
157 autoboot(-1, NULL); /* try to boot automatically */
161 autoboot(int timeout, char *prompt)
163 time_t when, otime, ntime;
165 char *argv[2], *cp, *ep;
167 #ifdef BOOT_PROMPT_123
168 const char *seq = "123", *p = seq;
175 /* try to get a delay from the environment */
176 if ((cp = getenv("autoboot_delay"))) {
177 timeout = strtol(cp, &ep, 0);
179 timeout = 10; /* Unparseable? Set default! */
183 kernelname = getenv("kernelname");
184 if (kernelname == NULL) {
186 loadakernel(0, 0, argv);
187 kernelname = getenv("kernelname");
188 if (kernelname == NULL) {
189 command_errmsg = "no valid kernel found";
196 when = otime + timeout; /* when to boot */
200 #ifdef BOOT_PROMPT_123
201 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
202 "1 2 3 sequence for command prompt." : prompt);
204 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
210 #ifdef BOOT_PROMPT_123
211 if ((c == '\r') || (c == '\n')) {
214 } else if (c != *p++)
219 if ((c == '\r') || (c == '\n'))
230 if (ntime != otime) {
231 printf("\rBooting [%s] in %d second%s... ",
232 kernelname, (int)(when - ntime),
233 (when-ntime)==1?"":"s");
242 printf("\rBooting [%s]... ", kernelname);
247 return(command_boot(1, argv));
253 * Scrounge for the name of the (try)'th file we will try to boot.
258 static char *name = NULL;
259 const char *spec, *ep;
262 /* we use dynamic storage */
269 * Try $bootfile, then try our builtin default
271 if ((spec = getenv("bootfile")) == NULL)
272 spec = default_bootfiles;
274 while ((try > 0) && (spec != NULL)) {
275 spec = strchr(spec, ';');
277 spec++; /* skip over the leading ';' */
281 if ((ep = strchr(spec, ';')) != NULL) {
286 name = malloc(len + 1);
287 strncpy(name, spec, len);
290 if (name && name[0] == 0) {
298 * Try to find the /etc/fstab file on the filesystem (rootdev),
299 * which should be be the root filesystem, and parse it to find
300 * out what the kernel ought to think the root filesystem is.
302 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
303 * so that the kernel can tell both which VFS and which node to use
304 * to mount the device. If this variable's already set, don't
308 getrootmount(char *rootdev)
310 char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
313 if (getenv("vfs.root.mountfrom") != NULL)
317 sprintf(lbuf, "%s/etc/fstab", rootdev);
318 if ((fd = open(lbuf, O_RDONLY)) < 0)
321 /* loop reading lines from /etc/fstab What was that about sscanf again? */
322 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
323 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
326 /* skip device name */
327 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
329 if (*cp == 0) /* misformatted */
331 /* delimit and save */
335 /* skip whitespace up to mountpoint */
336 while ((*cp != 0) && isspace(*cp))
338 /* must have /<space> to be root */
339 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
341 /* skip whitespace up to fstype */
343 while ((*cp != 0) && isspace(*cp))
345 if (*cp == 0) /* misformatted */
347 /* skip text to end of fstype and delimit */
349 while ((*cp != 0) && !isspace(*cp))
354 /* skip whitespace up to mount options */
356 while ((*cp != 0) && isspace(*cp))
358 if (*cp == 0) /* misformatted */
360 /* skip text to end of mount options and delimit */
362 while ((*cp != 0) && !isspace(*cp))
365 options = strdup(ep);
366 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
367 sprintf(lbuf, "%s:%s", fstyp, dev);
370 setenv("vfs.root.mountfrom", lbuf, 0);
372 /* Don't override vfs.root.mountfrom.options if it is already set */
373 if (getenv("vfs.root.mountfrom.options") == NULL) {
374 /* save mount options */
375 setenv("vfs.root.mountfrom.options", options, 0);
387 currdev = getenv("currdev");
388 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
389 cp = strdup(currdev);
390 cp[strlen(cp) - 1] = '\0';
391 setenv("vfs.root.mountfrom", cp, 0);
400 loadakernel(int try, int argc, char* argv[])
404 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
405 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
406 printf("can't load '%s'\n", cp);