boot - Fix CD booting
[dragonfly.git] / sys / boot / common / boot.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $FreeBSD: src/sys/boot/common/boot.c,v 1.29 2003/08/25 23:30:41 obrien Exp $
27  * $DragonFly: src/sys/boot/common/boot.c,v 1.4 2005/12/10 00:39:48 swildner Exp $
28  */
29
30 /*
31  * Loading modules, booting the system
32  */
33
34 #include <stand.h>
35 #include <string.h>
36
37 #include "bootstrap.h"
38
39 static char     *getbootfile(int try);
40 static int      loadakernel(int try, int argc, char* argv[]);
41
42 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
43 static const char *default_bootfiles = "kernel";
44
45 static int autoboot_tried;
46
47 /*
48  * The user wants us to boot.
49  */
50 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
51
52 static int
53 command_boot(int argc, char *argv[])
54 {
55     struct preloaded_file       *fp;
56     char *local_module_path;
57     char *exported_module_path;
58     
59     /*
60      * See if the user has specified an explicit kernel to boot.
61      */
62     if ((argc > 1) && (argv[1][0] != '-')) {
63         
64         /* XXX maybe we should discard everything and start again? */
65         if (file_findfile(NULL, NULL) != NULL) {
66             sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]);
67             return(CMD_ERROR);
68         }
69         
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;
75     }
76
77     /*
78      * See if there is a kernel module already loaded
79      */
80     if (file_findfile(NULL, NULL) == NULL)
81         if (loadakernel(0, argc - 1, argv + 1))
82             /* we have consumed all arguments */
83             argc = 1;
84
85     /*
86      * Loaded anything yet?
87      */
88     if ((fp = file_findfile(NULL, NULL)) == NULL) {
89         command_errmsg = "no bootable kernel";
90         return(CMD_ERROR);
91     }
92
93     /*
94      * If we were given arguments, discard any previous.
95      * XXX should we merge arguments?  Hard to DWIM.
96      */
97     if (argc > 1) {
98         if (fp->f_args != NULL) 
99             free(fp->f_args);
100         fp->f_args = unargv(argc - 1, argv + 1);
101     }
102
103     /* Hook for platform-specific autoloading of modules */
104     if (archsw.arch_autoload() != 0)
105         return(CMD_ERROR);
106
107     /*
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.
111      */
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");
120     }
121
122     /* Call the exec handler from the loader matching the kernel */
123     file_formats[fp->f_loader]->l_exec(fp);
124
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");
131             }
132             setenv("exported_module_path", exported_module_path, 1);
133             free(exported_module_path);
134     }
135     return(CMD_ERROR);
136 }
137
138
139 /*
140  * Autoboot after a delay
141  */
142
143 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
144
145 static int
146 command_autoboot(int argc, char *argv[])
147 {
148     int         howlong;
149     char        *cp, *prompt;
150
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             sprintf(command_errbuf, "bad delay '%s'", argv[1]);
161             return(CMD_ERROR);
162         }
163         /* FALLTHROUGH */
164     case 1:
165         return(autoboot(howlong, prompt));
166     }
167         
168     command_errmsg = "too many arguments";
169     return(CMD_ERROR);
170 }
171
172 /*
173  * Called before we go interactive.  If we think we can autoboot, and
174  * we haven't tried already, try now.
175  */
176 void
177 autoboot_maybe(void)
178 {
179     char        *cp;
180     
181     cp = getenv("autoboot_delay");
182     if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
183         autoboot(-1, NULL);             /* try to boot automatically */
184 }
185
186 int
187 autoboot(int timeout, char *prompt)
188 {
189     time_t      when, otime, ntime;
190     int         c, yes;
191     char        *argv[2], *cp, *ep;
192     char        *kernelname;
193
194     autoboot_tried = 1;
195
196     if (timeout == -1) {
197         /* try to get a delay from the environment */
198         if ((cp = getenv("autoboot_delay"))) {
199             timeout = strtol(cp, &ep, 0);
200             if (cp == ep)
201                 timeout = -1;
202         }
203     }
204     if (timeout == -1)          /* all else fails */
205         timeout = 10;
206
207     kernelname = getenv("kernelname");
208     if (kernelname == NULL) {
209         argv[0] = NULL;
210         loadakernel(0, 0, argv);
211         kernelname = getenv("kernelname");
212         if (kernelname == NULL) {
213             command_errmsg = "no valid kernel found";
214             return(CMD_ERROR);
215         }
216     }
217
218     otime = time(NULL);
219     when = otime + timeout;     /* when to boot */
220     yes = 0;
221
222     printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
223
224     for (;;) {
225         if (ischar()) {
226             c = getchar();
227             if ((c == '\r') || (c == '\n'))
228                 yes = 1;
229             break;
230         }
231         ntime = time(NULL);
232         if (ntime >= when) {
233             yes = 1;
234             break;
235         }
236         
237         if (ntime != otime) {
238             printf("\rBooting [%s] in %d second%s... ",
239                         kernelname, (int)(when - ntime),
240                         (when-ntime)==1?"":"s");
241             otime = ntime;
242         }
243     }
244     if (yes)
245         printf("\rBooting [%s]...               ", kernelname);
246     putchar('\n');
247     if (yes) {
248         argv[0] = "boot";
249         argv[1] = NULL;
250         return(command_boot(1, argv));
251     }
252     return(CMD_OK);
253 }
254
255 /*
256  * Scrounge for the name of the (try)'th file we will try to boot.
257  */
258 static char *
259 getbootfile(int try) 
260 {
261     static char *name = NULL;
262     const char  *spec, *ep;
263     size_t      len;
264     
265     /* we use dynamic storage */
266     if (name != NULL) {
267         free(name);
268         name = NULL;
269     }
270     
271     /* 
272      * Try $bootfile, then try our builtin default
273      */
274     if ((spec = getenv("bootfile")) == NULL)
275         spec = default_bootfiles;
276
277     while ((try > 0) && (spec != NULL)) {
278         spec = strchr(spec, ';');
279         if (spec)
280             spec++;     /* skip over the leading ';' */
281         try--;
282     }
283     if (spec != NULL) {
284         if ((ep = strchr(spec, ';')) != NULL) {
285             len = ep - spec;
286         } else {
287             len = strlen(spec);
288         }
289         name = malloc(len + 1);
290         strncpy(name, spec, len);
291         name[len] = 0;
292     }
293     if (name && name[0] == 0) {
294         free(name);
295         name = NULL;
296     }
297     return(name);
298 }
299
300 /*
301  * Try to find the /etc/fstab file on the filesystem (rootdev),
302  * which should be be the root filesystem, and parse it to find 
303  * out what the kernel ought to think the root filesystem is.
304  *
305  * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
306  * so that the kernel can tell both which VFS and which node to use
307  * to mount the device.  If this variable's already set, don't
308  * overwrite it.
309  */
310 int
311 getrootmount(char *rootdev)
312 {
313     char        lbuf[128], *cp, *ep, *dev, *fstyp;
314     int         fd, error;
315
316     if (getenv("vfs.root.mountfrom") != NULL)
317         return(0);
318
319     sprintf(lbuf, "%s/etc/fstab", rootdev);
320     if ((fd = open(lbuf, O_RDONLY)) < 0)
321         return(1);
322
323     /* loop reading lines from /etc/fstab    What was that about sscanf again? */
324     error = 1;
325     while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
326         if ((lbuf[0] == 0) || (lbuf[0] == '#'))
327             continue;
328         
329         /* skip device name */
330         for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
331             ;
332         if (*cp == 0)           /* misformatted */
333             continue;
334         /* delimit and save */
335         *cp++ = 0;
336         dev = strdup(lbuf);
337     
338         /* skip whitespace up to mountpoint */
339         while ((*cp != 0) && isspace(*cp))
340             cp++;
341         /* must have /<space> to be root */
342         if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
343             continue;
344         /* skip whitespace up to fstype */
345         cp += 2;
346         while ((*cp != 0) && isspace(*cp))
347             cp++;
348         if (*cp == 0)           /* misformatted */
349             continue;
350         /* skip text to end of fstype and delimit */
351         ep = cp;
352         while ((*cp != 0) && !isspace(*cp))
353             cp++;
354         *cp = 0;
355         fstyp = strdup(ep);
356
357         /* build the final result and save it */
358         sprintf(lbuf, "%s:%s", fstyp, dev);
359         free(dev);
360         free(fstyp);
361         setenv("vfs.root.mountfrom", lbuf, 0);
362         error = 0;
363         break;
364     }
365     close(fd);
366     return(error);
367 }
368
369 static int
370 loadakernel(int try, int argc, char* argv[])
371 {
372     char *cp;
373
374         for (try = 0; (cp = getbootfile(try)) != NULL; try++)
375             if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
376                 printf("can't load '%s'\n", cp);
377             else
378                 return 1;
379         return 0;
380 }
381