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>
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
[]);
42 * List of kernel names to try (may be overwritten by boot.config)
43 * 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",
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 */
89 * Loaded anything yet?
91 if ((fp
= file_findfile(NULL
, NULL
)) == NULL
) {
92 command_errmsg
= "no bootable kernel";
97 * If we were given arguments, discard any previous.
98 * XXX should we merge arguments? Hard to DWIM.
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",
123 command_autoboot(int argc
, char *argv
[])
135 howlong
= strtol(argv
[1], &cp
, 0);
137 snprintf(command_errbuf
, sizeof (command_errbuf
),
138 "bad delay '%s'", argv
[1]);
143 return (autoboot(howlong
, prompt
));
146 command_errmsg
= "too many arguments";
151 * Called before we go interactive. If we think we can autoboot, and
152 * we haven't tried already, try now.
159 /* compatibility with sparc prom, check for autoboot? */
160 cp
= getenv("autoboot?");
161 if (cp
!= NULL
&& strcasecmp(cp
, "true") != 0)
163 cp
= getenv("autoboot_delay");
164 if ((autoboot_tried
== 0) && ((cp
== NULL
) || strcasecmp(cp
, "NO")))
165 autoboot(-1, NULL
); /* try to boot automatically */
169 autoboot(int timeout
, char *prompt
)
171 time_t when
, otime
, ntime
;
173 char *argv
[2], *cp
, *ep
;
175 struct preloaded_file
*fp
;
181 /* try to get a delay from the environment */
182 if ((cp
= getenv("autoboot_delay"))) {
183 timeout
= strtol(cp
, &ep
, 0);
185 timeout
= 10; /* Unparseable? Set default! */
189 fp
= file_findfile(NULL
, NULL
);
191 /* no preloaded files, run command start to load all */
193 fp
= file_findfile(NULL
, NULL
);
194 if (fp
== NULL
) { /* still nothing? can't boot */
195 command_errmsg
= "no valid kernel found";
200 kernelname
= fp
->f_name
;
204 when
= otime
+ timeout
; /* when to boot */
208 printf("%s\n", (prompt
== NULL
) ?
209 "Hit [Enter] to boot immediately, or any other key "
210 "for command prompt." : prompt
);
215 if ((c
== '\r') || (c
== '\n'))
225 if (ntime
!= otime
) {
226 printf("\rBooting [%s] in %d second%s... ",
227 kernelname
, (int)(when
- ntime
),
228 (when
- ntime
) == 1? "":"s");
237 printf("\rBooting [%s]... ", kernelname
);
242 return (command_boot(1, argv
));
248 * Scrounge for the name of the (try)'th file we will try to boot.
253 static char *name
= NULL
;
254 const char *spec
, *ep
;
257 /* we use dynamic storage */
262 * Try $bootfile, then try our builtin default
264 if ((spec
= getenv("bootfile")) == NULL
)
265 spec
= default_bootfiles
;
267 while ((try > 0) && (spec
!= NULL
)) {
268 spec
= strchr(spec
, ';');
270 spec
++; /* skip over the leading ';' */
274 if ((ep
= strchr(spec
, ';')) != NULL
) {
279 name
= malloc(len
+ 1);
280 strncpy(name
, spec
, len
);
283 if (name
&& name
[0] == 0) {
291 * Try to find the /etc/fstab file on the filesystem (rootdev),
292 * which should be be the root filesystem, and parse it to find
293 * out what the kernel ought to think the root filesystem is.
295 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
296 * so that the kernel can tell both which VFS and which node to use
297 * to mount the device. If this variable's already set, don't
301 getrootmount(char *rootdev
)
303 char lbuf
[128], *cp
, *ep
, *dev
, *fstyp
, *options
;
306 if (getenv("vfs.root.mountfrom") != NULL
)
310 sprintf(lbuf
, "%s/etc/fstab", rootdev
);
311 if ((fd
= open(lbuf
, O_RDONLY
)) < 0)
315 * loop reading lines from /etc/fstab
316 * What was that about sscanf again?
320 while (fgetstr(lbuf
, sizeof (lbuf
), fd
) >= 0) {
321 if ((lbuf
[0] == 0) || (lbuf
[0] == '#'))
324 /* skip device name */
325 for (cp
= lbuf
; (*cp
!= 0) && !isspace(*cp
); cp
++)
327 if (*cp
== 0) /* misformatted */
329 /* delimit and save */
334 /* skip whitespace up to mountpoint */
335 while ((*cp
!= 0) && isspace(*cp
))
337 /* must have /<space> to be root */
338 if ((*cp
== 0) || (*cp
!= '/') || !isspace(*(cp
+ 1)))
340 /* skip whitespace up to fstype */
342 while ((*cp
!= 0) && isspace(*cp
))
344 if (*cp
== 0) /* misformatted */
346 /* skip text to end of fstype and delimit */
348 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
);
367 * Build the <fstype>:<device> and save it in
370 sprintf(lbuf
, "%s:%s", fstyp
, dev
);
371 setenv("vfs.root.mountfrom", lbuf
, 0);
374 * Don't override vfs.root.mountfrom.options if it is
377 if (getenv("vfs.root.mountfrom.options") == NULL
) {
378 /* save mount options */
379 setenv("vfs.root.mountfrom.options", options
, 0);
393 currdev
= getenv("currdev");
394 if (currdev
!= NULL
&& strncmp("zfs:", currdev
, 4) == 0) {
395 cp
= strdup(currdev
);
396 cp
[strlen(cp
) - 1] = '\0';
397 setenv("vfs.root.mountfrom", cp
, 0);
407 loadakernel(int try, int argc
, char *argv
[])
411 for (try = 0; (cp
= getbootfile(try)) != NULL
; try++)
412 if (mod_loadkld(cp
, argc
- 1, argv
+ 1) != 0)
413 printf("can't load '%s'\n", cp
);