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>
29 * Loading modules, booting the system
35 #include <sys/reboot.h>
38 #include "bootstrap.h"
40 static int autoboot(int timeout, char *prompt);
41 static char *getbootfile(int try);
42 static int loadakernel(int try, int argc, char* argv[]);
44 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
45 static const char *default_bootfiles = "kernel";
47 static int autoboot_tried;
50 * The user wants us to boot.
52 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
55 command_boot(int argc, char *argv[])
57 struct preloaded_file *fp;
60 * See if the user has specified an explicit kernel to boot.
62 if ((argc > 1) && (argv[1][0] != '-')) {
64 /* XXX maybe we should discard everything and start again? */
65 if (file_findfile(NULL, NULL) != NULL) {
66 snprintf(command_errbuf, sizeof(command_errbuf),
67 "can't boot '%s', kernel module already loaded", argv[1]);
71 /* find/load the kernel module */
72 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
74 /* we have consumed all arguments */
79 * See if there is a kernel module already loaded
81 if (file_findfile(NULL, NULL) == NULL)
82 if (loadakernel(0, argc - 1, argv + 1))
83 /* we have consumed all arguments */
87 * Loaded anything yet?
89 if ((fp = file_findfile(NULL, NULL)) == NULL) {
90 command_errmsg = "no bootable kernel";
95 * If we were given arguments, discard any previous.
96 * XXX should we merge arguments? Hard to DWIM.
99 if (fp->f_args != NULL)
101 fp->f_args = unargv(argc - 1, argv + 1);
104 /* Hook for platform-specific autoloading of modules */
105 if (archsw.arch_autoload() != 0)
108 #ifdef LOADER_VERIEXEC
109 verify_pcr_export(); /* for measured boot */
110 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
111 pass_manifest_export_envs();
115 /* Pass the tslog buffer to the kernel as a preloaded module. */
118 /* Call the exec handler from the loader matching the kernel */
119 file_formats[fp->f_loader]->l_exec(fp);
125 * Autoboot after a delay
128 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
131 command_autoboot(int argc, char *argv[])
143 howlong = strtol(argv[1], &cp, 0);
145 snprintf(command_errbuf, sizeof(command_errbuf),
146 "bad delay '%s'", argv[1]);
151 return(autoboot(howlong, prompt));
154 command_errmsg = "too many arguments";
159 * Called before we go interactive. If we think we can autoboot, and
160 * we haven't tried already, try now.
167 cp = getenv("autoboot_delay");
168 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
169 autoboot(-1, NULL); /* try to boot automatically */
173 autoboot(int timeout, char *prompt)
175 time_t when, otime, ntime;
177 char *argv[2], *cp, *ep;
179 #ifdef BOOT_PROMPT_123
180 const char *seq = "123", *p = seq;
187 /* try to get a delay from the environment */
188 if ((cp = getenv("autoboot_delay"))) {
189 timeout = strtol(cp, &ep, 0);
191 timeout = 10; /* Unparseable? Set default! */
195 kernelname = getenv("kernelname");
196 if (kernelname == NULL) {
198 loadakernel(0, 0, argv);
199 kernelname = getenv("kernelname");
200 if (kernelname == NULL) {
201 command_errmsg = "no valid kernel found";
209 when = ntime + timeout; /* when to boot */
213 #ifdef BOOT_PROMPT_123
214 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
215 "1 2 3 sequence for command prompt." : prompt);
217 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
223 #ifdef BOOT_PROMPT_123
224 if ((c == '\r') || (c == '\n')) {
227 } else if (c != *p++)
232 if ((c == '\r') || (c == '\n'))
243 if (ntime != otime) {
244 printf("\rBooting [%s] in %d second%s... ",
245 kernelname, (int)(when - ntime),
246 (when-ntime)==1?"":"s");
255 printf("\rBooting [%s]... ", kernelname);
260 return(command_boot(1, argv));
266 * Scrounge for the name of the (try)'th file we will try to boot.
271 static char *name = NULL;
272 const char *spec, *ep;
275 /* we use dynamic storage */
282 * Try $bootfile, then try our builtin default
284 if ((spec = getenv("bootfile")) == NULL)
285 spec = default_bootfiles;
287 while ((try > 0) && (spec != NULL)) {
288 spec = strchr(spec, ';');
290 spec++; /* skip over the leading ';' */
294 if ((ep = strchr(spec, ';')) != NULL) {
299 name = malloc(len + 1);
300 strncpy(name, spec, len);
303 if (name && name[0] == 0) {
311 * Try to find the /etc/fstab file on the filesystem (rootdev),
312 * which should be the root filesystem, and parse it to find
313 * out what the kernel ought to think the root filesystem is.
315 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
316 * so that the kernel can tell both which VFS and which node to use
317 * to mount the device. If this variable's already set, don't
321 getrootmount(char *rootdev)
323 char lbuf[KENV_MVALLEN], *cp, *ep, *dev, *fstyp, *options;
326 if (getenv("vfs.root.mountfrom") != NULL)
330 snprintf(lbuf, sizeof(lbuf), "%s/etc/fstab", rootdev);
331 if ((fd = open(lbuf, O_RDONLY)) < 0)
334 /* loop reading lines from /etc/fstab What was that about sscanf again? */
337 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
338 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
341 /* skip device name */
342 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
344 if (*cp == 0) /* misformatted */
346 /* delimit and save */
351 /* skip whitespace up to mountpoint */
352 while ((*cp != 0) && isspace(*cp))
354 /* must have /<space> to be root */
355 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
357 /* skip whitespace up to fstype */
359 while ((*cp != 0) && isspace(*cp))
361 if (*cp == 0) /* misformatted */
363 /* skip text to end of fstype and delimit */
365 while ((*cp != 0) && !isspace(*cp))
371 /* skip whitespace up to mount options */
373 while ((*cp != 0) && isspace(*cp))
375 if (*cp == 0) /* misformatted */
377 /* skip text to end of mount options and delimit */
379 while ((*cp != 0) && !isspace(*cp))
382 options = strdup(ep);
383 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
384 snprintf(lbuf, sizeof(lbuf), "%s:%s", fstyp, dev);
385 setenv("vfs.root.mountfrom", lbuf, 0);
387 /* Don't override vfs.root.mountfrom.options if it is already set */
388 if (getenv("vfs.root.mountfrom.options") == NULL) {
389 /* save mount options */
390 setenv("vfs.root.mountfrom.options", options, 0);
404 currdev = getenv("currdev");
405 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
406 cp = strdup(currdev);
407 cp[strlen(cp) - 1] = '\0';
408 setenv("vfs.root.mountfrom", cp, 0);
418 loadakernel(int try, int argc, char* argv[])
422 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
423 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
424 printf("can't load '%s'\n", cp);