1 /* $NetBSD: boot.c,v 1.4 2009/03/18 10:22:31 cegger Exp $ */
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/sys/boot/common/boot.c,v 1.29 2003/08/25 23:30:41 obrien Exp $"); */
34 * Loading modules, booting the system
37 #include <lib/libsa/stand.h>
38 #include <lib/libsa/loadfile.h>
39 #include <lib/libkern/libkern.h>
41 #include "bootstrap.h"
43 static char *getbootfile(int try);
44 static int loadakernel(int try, int argc
, char* argv
[]);
46 /* List of kernel names to try */
47 static const char *default_bootfiles
= "netbsd";
49 static int autoboot_tried
;
52 * The user wants us to boot.
56 command_boot(int argc
, char *argv
[])
58 struct preloaded_file
*fp
;
61 * See if the user has specified an explicit kernel to boot.
63 if ((argc
> 1) && (argv
[1][0] != '-')) {
65 /* XXX maybe we should discard everything and start again? */
66 if (file_findfile(NULL
, NULL
) != NULL
) {
67 sprintf(command_errbuf
, "can't boot '%s', kernel module already loaded", argv
[1]);
71 /* find/load the kernel module */
72 if (file_loadkernel(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 */
88 * Loaded anything yet?
90 if ((fp
= file_findfile(NULL
, NULL
)) == NULL
) {
91 command_errmsg
= "no bootable kernel";
96 * If we were given arguments, discard any previous.
97 * XXX should we merge arguments? Hard to DWIM.
100 if (fp
->f_args
!= NULL
)
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 command_errmsg
= strerror(file_formats
[fp
->f_loader
]->l_exec(fp
));
116 * Autoboot after a delay
120 command_autoboot(int argc
, char *argv
[])
132 howlong
= strtol(argv
[1], &cp
, 0);
134 sprintf(command_errbuf
, "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
;
171 /* try to get a delay from the environment */
172 if ((cp
= getenv("autoboot_delay"))) {
173 timeout
= strtol(cp
, &ep
, 0);
178 if (timeout
== -1) /* all else fails */
181 kernelname
= getenv("kernelname");
182 if (kernelname
== NULL
) {
184 loadakernel(0, 0, argv
);
185 kernelname
= getenv("kernelname");
186 if (kernelname
== NULL
) {
187 command_errmsg
= "no valid kernel found";
193 when
= otime
+ timeout
; /* when to boot */
196 printf("%s\n", (prompt
== NULL
) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt
);
201 if ((c
== '\r') || (c
== '\n'))
211 if (ntime
!= otime
) {
212 printf("\rBooting [%s] in %d second%s... ",
213 kernelname
, (int)(when
- ntime
),
214 (when
-ntime
)==1?"":"s");
219 printf("\rBooting [%s]... ", kernelname
);
224 return(command_boot(1, argv
));
230 * Scrounge for the name of the (try)'th file we will try to boot.
235 static char *name
= NULL
;
236 const char *spec
, *ep
;
239 /* we use dynamic storage */
246 * Try $bootfile, then try our builtin default
248 if ((spec
= getenv("bootfile")) == NULL
)
249 spec
= default_bootfiles
;
251 while ((try > 0) && (spec
!= NULL
)) {
252 spec
= strchr(spec
, ';');
254 spec
++; /* skip over the leading ';' */
258 if ((ep
= strchr(spec
, ';')) != NULL
) {
263 name
= alloc(len
+ 1);
264 strncpy(name
, spec
, len
);
267 if (name
&& name
[0] == 0) {
272 setenv("kernelname", name
, 1);
279 * Try to find the /etc/fstab file on the filesystem (rootdev),
280 * which should be be the root filesystem, and parse it to find
281 * out what the kernel ought to think the root filesystem is.
283 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
284 * so that the kernel can tell both which VFS and which node to use
285 * to mount the device. If this variable's already set, don't
289 getrootmount(char *rootdev
)
291 char lbuf
[128], *cp
, *ep
, *dev
, *fstyp
;
294 if (getenv("vfs.root.mountfrom") != NULL
)
297 sprintf(lbuf
, "%s/etc/fstab", rootdev
);
298 if ((fd
= open(lbuf
, O_RDONLY
)) < 0)
301 /* loop reading lines from /etc/fstab What was that about sscanf again? */
303 while (fgetstr(lbuf
, sizeof(lbuf
), fd
) >= 0) {
304 if ((lbuf
[0] == 0) || (lbuf
[0] == '#'))
307 /* skip device name */
308 for (cp
= lbuf
; (*cp
!= 0) && !isspace(*cp
); cp
++)
310 if (*cp
== 0) /* misformatted */
312 /* delimit and save */
316 /* skip whitespace up to mountpoint */
317 while ((*cp
!= 0) && isspace(*cp
))
319 /* must have /<space> to be root */
320 if ((*cp
== 0) || (*cp
!= '/') || !isspace(*(cp
+ 1)))
322 /* skip whitespace up to fstype */
324 while ((*cp
!= 0) && isspace(*cp
))
326 if (*cp
== 0) /* misformatted */
328 /* skip text to end of fstype and delimit */
330 while ((*cp
!= 0) && !isspace(*cp
))
335 /* build the final result and save it */
336 sprintf(lbuf
, "%s:%s", fstyp
, dev
);
339 setenv("vfs.root.mountfrom", lbuf
, 0);
348 loadakernel(int try, int argc
, char* argv
[])
352 for (try = 0; (cp
= getbootfile(try)) != NULL
; try++)
353 if (file_loadkernel(cp
, argc
- 1, argv
+ 1) != 0)
354 printf("can't load '%s'\n", cp
);