boot: Use snprintf() when filling command_errbuf[] w/ dynamic content.
[dragonfly.git] / sys / boot / common / boot.c
blob48cde2b33dfd7abeed03df554dea3ce513cd4b36
1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/boot/common/boot.c,v 1.29 2003/08/25 23:30:41 obrien Exp $
30 * Loading modules, booting the system
33 #include <stand.h>
34 #include <string.h>
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);
51 static int
52 command_boot(int argc, char *argv[])
54 struct preloaded_file *fp;
55 char *local_module_path;
56 char *exported_module_path;
59 * See if the user has specified an explicit kernel to boot.
61 if ((argc > 1) && (argv[1][0] != '-')) {
63 /* XXX maybe we should discard everything and start again? */
64 if (file_findfile(NULL, NULL) != NULL) {
65 snprintf(command_errbuf, sizeof(command_errbuf),
66 "can't boot '%s', kernel module already loaded", argv[1]);
67 return(CMD_ERROR);
70 /* find/load the kernel module */
71 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
72 return(CMD_ERROR);
73 /* we have consumed all arguments */
74 argc = 1;
78 * See if there is a kernel module already loaded
80 if (file_findfile(NULL, NULL) == NULL)
81 if (loadakernel(0, argc - 1, argv + 1))
82 /* we have consumed all arguments */
83 argc = 1;
86 * Loaded anything yet?
88 if ((fp = file_findfile(NULL, NULL)) == NULL) {
89 command_errmsg = "no bootable kernel";
90 return(CMD_ERROR);
94 * If we were given arguments, discard any previous.
95 * XXX should we merge arguments? Hard to DWIM.
97 if (argc > 1) {
98 if (fp->f_args != NULL)
99 free(fp->f_args);
100 fp->f_args = unargv(argc - 1, argv + 1);
103 /* Hook for platform-specific autoloading of modules */
104 if (archsw.arch_autoload() != 0)
105 return(CMD_ERROR);
108 * Exec the kernel. We have to shift our exported_module_path
109 * (which has the correct /boot prefix for the kernel) over to
110 * module_path. If the exec fails we switch it back.
112 exported_module_path = getenv("exported_module_path");
113 if (exported_module_path) {
114 exported_module_path = strdup(exported_module_path);
115 local_module_path = getenv("module_path");
116 if (local_module_path)
117 local_module_path = strdup(local_module_path);
118 setenv("module_path", exported_module_path, 1);
119 unsetenv("exported_module_path");
122 /* Call the exec handler from the loader matching the kernel */
123 file_formats[fp->f_loader]->l_exec(fp);
125 if (exported_module_path) {
126 if (local_module_path) {
127 setenv("module_path", local_module_path, 1);
128 free(local_module_path);
129 } else {
130 unsetenv("module_path");
132 setenv("exported_module_path", exported_module_path, 1);
133 free(exported_module_path);
135 return(CMD_ERROR);
140 * Autoboot after a delay
143 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
145 static int
146 command_autoboot(int argc, char *argv[])
148 int howlong;
149 char *cp, *prompt;
151 prompt = NULL;
152 howlong = -1;
153 switch(argc) {
154 case 3:
155 prompt = argv[2];
156 /* FALLTHROUGH */
157 case 2:
158 howlong = strtol(argv[1], &cp, 0);
159 if (*cp != 0) {
160 snprintf(command_errbuf, sizeof(command_errbuf),
161 "bad delay '%s'", argv[1]);
162 return(CMD_ERROR);
164 /* FALLTHROUGH */
165 case 1:
166 return(autoboot(howlong, prompt));
169 command_errmsg = "too many arguments";
170 return(CMD_ERROR);
174 * Called before we go interactive. If we think we can autoboot, and
175 * we haven't tried already, try now.
177 void
178 autoboot_maybe(void)
180 char *cp;
182 cp = getenv("autoboot_delay");
183 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
184 autoboot(-1, NULL); /* try to boot automatically */
188 autoboot(int timeout, char *prompt)
190 time_t when, otime, ntime;
191 int c, yes;
192 char *argv[2], *cp, *ep;
193 char *kernelname;
195 autoboot_tried = 1;
197 if (timeout == -1) {
198 /* try to get a delay from the environment */
199 if ((cp = getenv("autoboot_delay"))) {
200 timeout = strtol(cp, &ep, 0);
201 if (cp == ep)
202 timeout = -1;
205 if (timeout == -1) /* all else fails */
206 timeout = 10;
208 kernelname = getenv("kernelname");
209 if (kernelname == NULL) {
210 argv[0] = NULL;
211 loadakernel(0, 0, argv);
212 kernelname = getenv("kernelname");
213 if (kernelname == NULL) {
214 command_errmsg = "no valid kernel found";
215 return(CMD_ERROR);
219 otime = time(NULL);
220 when = otime + timeout; /* when to boot */
221 yes = 0;
223 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
225 for (;;) {
226 if (ischar()) {
227 c = getchar();
228 if ((c == '\r') || (c == '\n'))
229 yes = 1;
230 break;
232 ntime = time(NULL);
233 if (ntime >= when) {
234 yes = 1;
235 break;
238 if (ntime != otime) {
239 printf("\rBooting [%s] in %d second%s... ",
240 kernelname, (int)(when - ntime),
241 (when-ntime)==1?"":"s");
242 otime = ntime;
245 if (yes)
246 printf("\rBooting [%s]... ", kernelname);
247 putchar('\n');
248 if (yes) {
249 argv[0] = "boot";
250 argv[1] = NULL;
251 return(command_boot(1, argv));
253 return(CMD_OK);
257 * Scrounge for the name of the (try)'th file we will try to boot.
259 static char *
260 getbootfile(int try)
262 static char *name = NULL;
263 const char *spec, *ep;
264 size_t len;
266 /* we use dynamic storage */
267 if (name != NULL) {
268 free(name);
269 name = NULL;
273 * Try $bootfile, then try our builtin default
275 if ((spec = getenv("bootfile")) == NULL)
276 spec = default_bootfiles;
278 while ((try > 0) && (spec != NULL)) {
279 spec = strchr(spec, ';');
280 if (spec)
281 spec++; /* skip over the leading ';' */
282 try--;
284 if (spec != NULL) {
285 if ((ep = strchr(spec, ';')) != NULL) {
286 len = ep - spec;
287 } else {
288 len = strlen(spec);
290 name = malloc(len + 1);
291 strncpy(name, spec, len);
292 name[len] = 0;
294 if (name && name[0] == 0) {
295 free(name);
296 name = NULL;
298 return(name);
302 * Try to find the /etc/fstab file on the filesystem (rootdev),
303 * which should be be the root filesystem, and parse it to find
304 * out what the kernel ought to think the root filesystem is.
306 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
307 * so that the kernel can tell both which VFS and which node to use
308 * to mount the device. If this variable's already set, don't
309 * overwrite it.
312 getrootmount(char *rootdev)
314 char lbuf[128], *cp, *ep, *dev, *fstyp;
315 int fd, error;
317 if (getenv("vfs.root.mountfrom") != NULL)
318 return(0);
320 sprintf(lbuf, "%s/etc/fstab", rootdev);
321 if ((fd = open(lbuf, O_RDONLY)) < 0)
322 return(1);
324 /* loop reading lines from /etc/fstab What was that about sscanf again? */
325 error = 1;
326 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
327 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
328 continue;
330 /* skip device name */
331 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
333 if (*cp == 0) /* misformatted */
334 continue;
335 /* delimit and save */
336 *cp++ = 0;
337 dev = strdup(lbuf);
339 /* skip whitespace up to mountpoint */
340 while ((*cp != 0) && isspace(*cp))
341 cp++;
342 /* must have /<space> to be root */
343 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
344 continue;
345 /* skip whitespace up to fstype */
346 cp += 2;
347 while ((*cp != 0) && isspace(*cp))
348 cp++;
349 if (*cp == 0) /* misformatted */
350 continue;
351 /* skip text to end of fstype and delimit */
352 ep = cp;
353 while ((*cp != 0) && !isspace(*cp))
354 cp++;
355 *cp = 0;
356 fstyp = strdup(ep);
358 /* build the final result and save it */
359 sprintf(lbuf, "%s:%s", fstyp, dev);
360 free(dev);
361 free(fstyp);
362 setenv("vfs.root.mountfrom", lbuf, 0);
363 error = 0;
364 break;
366 close(fd);
367 return(error);
370 static int
371 loadakernel(int try, int argc, char* argv[])
373 char *cp;
375 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
376 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
377 printf("can't load '%s'\n", cp);
378 else
379 return 1;
380 return 0;