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
30 * Loading modules, booting the system
36 #include "bootstrap.h"
38 static char *getbootfile(int try);
39 static int loadakernel(int try, int argc, char* argv[]);
41 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
42 static const char *default_bootfiles = "kernel";
44 static int autoboot_tried;
47 * The user wants us to boot.
49 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
52 command_boot(int argc, char *argv[])
54 struct preloaded_file *fp;
57 * See if the user has specified an explicit kernel to boot.
59 if ((argc > 1) && (argv[1][0] != '-')) {
61 /* XXX maybe we should discard everything and start again? */
62 if (file_findfile(NULL, NULL) != NULL) {
63 sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]);
67 /* find/load the kernel module */
68 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
70 /* we have consumed all arguments */
75 * See if there is a kernel module already loaded
77 if (file_findfile(NULL, NULL) == NULL)
78 if (loadakernel(0, argc - 1, argv + 1))
79 /* we have consumed all arguments */
83 * Loaded anything yet?
85 if ((fp = file_findfile(NULL, NULL)) == NULL) {
86 command_errmsg = "no bootable kernel";
91 * If we were given arguments, discard any previous.
92 * XXX should we merge arguments? Hard to DWIM.
95 if (fp->f_args != NULL)
97 fp->f_args = unargv(argc - 1, argv + 1);
100 /* Hook for platform-specific autoloading of modules */
101 if (archsw.arch_autoload() != 0)
104 /* Call the exec handler from the loader matching the kernel */
105 file_formats[fp->f_loader]->l_exec(fp);
111 * Autoboot after a delay
114 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
117 command_autoboot(int argc, char *argv[])
129 howlong = strtol(argv[1], &cp, 0);
131 sprintf(command_errbuf, "bad delay '%s'", argv[1]);
136 return(autoboot(howlong, prompt));
139 command_errmsg = "too many arguments";
144 * Called before we go interactive. If we think we can autoboot, and
145 * we haven't tried already, try now.
152 cp = getenv("autoboot_delay");
153 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
154 autoboot(-1, NULL); /* try to boot automatically */
158 autoboot(int timeout, char *prompt)
160 time_t when, otime, ntime;
162 char *argv[2], *cp, *ep;
168 /* try to get a delay from the environment */
169 if ((cp = getenv("autoboot_delay"))) {
170 timeout = strtol(cp, &ep, 0);
175 if (timeout == -1) /* all else fails */
178 kernelname = getenv("kernelname");
179 if (kernelname == NULL) {
181 loadakernel(0, 0, argv);
182 kernelname = getenv("kernelname");
183 if (kernelname == NULL) {
184 command_errmsg = "no valid kernel found";
190 when = otime + timeout; /* when to boot */
193 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
198 if ((c == '\r') || (c == '\n'))
208 if (ntime != otime) {
209 printf("\rBooting [%s] in %d second%s... ",
210 kernelname, (int)(when - ntime),
211 (when-ntime)==1?"":"s");
216 printf("\rBooting [%s]... ", kernelname);
221 return(command_boot(1, argv));
227 * Scrounge for the name of the (try)'th file we will try to boot.
232 static char *name = NULL;
233 const char *spec, *ep;
236 /* we use dynamic storage */
243 * Try $bootfile, then try our builtin default
245 if ((spec = getenv("bootfile")) == NULL)
246 spec = default_bootfiles;
248 while ((try > 0) && (spec != NULL)) {
249 spec = strchr(spec, ';');
251 spec++; /* skip over the leading ';' */
255 if ((ep = strchr(spec, ';')) != NULL) {
260 name = malloc(len + 1);
261 strncpy(name, spec, len);
264 if (name && name[0] == 0) {
272 * Try to find the /etc/fstab file on the filesystem (rootdev),
273 * which should be be the root filesystem, and parse it to find
274 * out what the kernel ought to think the root filesystem is.
276 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
277 * so that the kernel can tell both which VFS and which node to use
278 * to mount the device. If this variable's already set, don't
282 getrootmount(char *rootdev)
284 char lbuf[128], *cp, *ep, *dev, *fstyp;
287 if (getenv("vfs.root.mountfrom") != NULL)
290 sprintf(lbuf, "%s/etc/fstab", rootdev);
291 if ((fd = open(lbuf, O_RDONLY)) < 0)
294 /* loop reading lines from /etc/fstab What was that about sscanf again? */
296 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
297 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
300 /* skip device name */
301 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
303 if (*cp == 0) /* misformatted */
305 /* delimit and save */
309 /* skip whitespace up to mountpoint */
310 while ((*cp != 0) && isspace(*cp))
312 /* must have /<space> to be root */
313 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
315 /* skip whitespace up to fstype */
317 while ((*cp != 0) && isspace(*cp))
319 if (*cp == 0) /* misformatted */
321 /* skip text to end of fstype and delimit */
323 while ((*cp != 0) && !isspace(*cp))
328 /* build the final result and save it */
329 sprintf(lbuf, "%s:%s", fstyp, dev);
332 setenv("vfs.root.mountfrom", lbuf, 0);
341 loadakernel(int try, int argc, char* argv[])
345 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
346 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
347 printf("can't load '%s'\n", cp);