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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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
command_boot(int argc,char * argv[])53 command_boot(int argc, char *argv[])
54 {
55 struct preloaded_file *fp;
56
57 /*
58 * See if the user has specified an explicit kernel to boot.
59 */
60 if ((argc > 1) && (argv[1][0] == '/')) {
61
62 /* XXX maybe we should discard everything and start again? */
63 if (file_findfile(NULL, NULL) != NULL) {
64 snprintf(command_errbuf, sizeof (command_errbuf),
65 "can't boot '%s', kernel module already loaded", argv[1]);
66 return(CMD_ERROR);
67 }
68
69 /* find/load the kernel module */
70 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
71 return(CMD_ERROR);
72 /* we have consumed all arguments */
73 argc = 1;
74 }
75
76 /*
77 * See if there is a kernel module already loaded
78 */
79 if (file_findfile(NULL, NULL) == NULL)
80 if (loadakernel(0, argc - 1, argv + 1))
81 /* we have consumed all arguments */
82 argc = 1;
83
84 /*
85 * Loaded anything yet?
86 */
87 if ((fp = file_findfile(NULL, NULL)) == NULL) {
88 command_errmsg = "no bootable kernel";
89 return(CMD_ERROR);
90 }
91
92 /*
93 * If we were given arguments, discard any previous.
94 * XXX should we merge arguments? Hard to DWIM.
95 */
96 if (argc > 1) {
97 if (fp->f_args != NULL)
98 free(fp->f_args);
99 fp->f_args = unargv(argc - 1, argv + 1);
100 }
101
102 /* Hook for platform-specific autoloading of modules */
103 if (archsw.arch_autoload() != 0)
104 return(CMD_ERROR);
105
106 /* Call the exec handler from the loader matching the kernel */
107 file_formats[fp->f_loader]->l_exec(fp);
108 return(CMD_ERROR);
109 }
110
111
112 /*
113 * Autoboot after a delay
114 */
115
116 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
117
118 static int
command_autoboot(int argc,char * argv[])119 command_autoboot(int argc, char *argv[])
120 {
121 int howlong;
122 char *cp, *prompt;
123
124 prompt = NULL;
125 howlong = -1;
126 switch(argc) {
127 case 3:
128 prompt = argv[2];
129 /* FALLTHROUGH */
130 case 2:
131 howlong = strtol(argv[1], &cp, 0);
132 if (*cp != 0) {
133 snprintf(command_errbuf, sizeof (command_errbuf),
134 "bad delay '%s'", argv[1]);
135 return(CMD_ERROR);
136 }
137 /* FALLTHROUGH */
138 case 1:
139 return(autoboot(howlong, prompt));
140 }
141
142 command_errmsg = "too many arguments";
143 return(CMD_ERROR);
144 }
145
146 /*
147 * Called before we go interactive. If we think we can autoboot, and
148 * we haven't tried already, try now.
149 */
150 void
autoboot_maybe(void)151 autoboot_maybe(void)
152 {
153 char *cp;
154
155 /* compatibility with sparc prom, check for autoboot? */
156 cp = getenv("autoboot?");
157 if (cp != NULL && strcasecmp(cp, "true") != 0)
158 return;
159 cp = getenv("autoboot_delay");
160 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
161 autoboot(-1, NULL); /* try to boot automatically */
162 }
163
164 int
autoboot(int timeout,char * prompt)165 autoboot(int timeout, char *prompt)
166 {
167 time_t when, otime, ntime;
168 int c, yes;
169 char *argv[2], *cp, *ep;
170 char *kernelname;
171 struct preloaded_file *fp;
172
173 autoboot_tried = 1;
174
175 if (timeout == -1) {
176 timeout = 10;
177 /* try to get a delay from the environment */
178 if ((cp = getenv("autoboot_delay"))) {
179 timeout = strtol(cp, &ep, 0);
180 if (cp == ep)
181 timeout = 10; /* Unparseable? Set default! */
182 }
183 }
184
185 fp = file_findfile(NULL, NULL);
186 if (fp == NULL) { /* no preloaded files, run command start to load all */
187 bf_run("start");
188 fp = file_findfile(NULL, NULL);
189 if (fp == NULL) { /* still nothing? can't boot */
190 command_errmsg = "no valid kernel found";
191 return(CMD_ERROR);
192 }
193 }
194
195 kernelname = fp->f_name;
196
197 if (timeout >= 0) {
198 otime = time(NULL);
199 when = otime + timeout; /* when to boot */
200
201 yes = 0;
202
203 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
204
205 for (;;) {
206 if (ischar()) {
207 c = getchar();
208 if ((c == '\r') || (c == '\n'))
209 yes = 1;
210 break;
211 }
212 ntime = time(NULL);
213 if (ntime >= when) {
214 yes = 1;
215 break;
216 }
217
218 if (ntime != otime) {
219 printf("\rBooting [%s] in %d second%s... ",
220 kernelname, (int)(when - ntime),
221 (when-ntime)==1?"":"s");
222 otime = ntime;
223 }
224 }
225 } else {
226 yes = 1;
227 }
228
229 if (yes)
230 printf("\rBooting [%s]... ", kernelname);
231 putchar('\n');
232 if (yes) {
233 argv[0] = "boot";
234 argv[1] = NULL;
235 return(command_boot(1, argv));
236 }
237 return(CMD_OK);
238 }
239
240 /*
241 * Scrounge for the name of the (try)'th file we will try to boot.
242 */
243 static char *
getbootfile(int try)244 getbootfile(int try)
245 {
246 static char *name = NULL;
247 const char *spec, *ep;
248 size_t len;
249
250 /* we use dynamic storage */
251 if (name != NULL) {
252 free(name);
253 name = NULL;
254 }
255
256 /*
257 * Try $bootfile, then try our builtin default
258 */
259 if ((spec = getenv("bootfile")) == NULL)
260 spec = default_bootfiles;
261
262 while ((try > 0) && (spec != NULL)) {
263 spec = strchr(spec, ';');
264 if (spec)
265 spec++; /* skip over the leading ';' */
266 try--;
267 }
268 if (spec != NULL) {
269 if ((ep = strchr(spec, ';')) != NULL) {
270 len = ep - spec;
271 } else {
272 len = strlen(spec);
273 }
274 name = malloc(len + 1);
275 strncpy(name, spec, len);
276 name[len] = 0;
277 }
278 if (name && name[0] == 0) {
279 free(name);
280 name = NULL;
281 }
282 return(name);
283 }
284
285 /*
286 * Try to find the /etc/fstab file on the filesystem (rootdev),
287 * which should be be the root filesystem, and parse it to find
288 * out what the kernel ought to think the root filesystem is.
289 *
290 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
291 * so that the kernel can tell both which VFS and which node to use
292 * to mount the device. If this variable's already set, don't
293 * overwrite it.
294 */
295 int
getrootmount(char * rootdev)296 getrootmount(char *rootdev)
297 {
298 char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
299 int fd, error;
300
301 if (getenv("vfs.root.mountfrom") != NULL)
302 return(0);
303
304 error = 1;
305 sprintf(lbuf, "%s/etc/fstab", rootdev);
306 if ((fd = open(lbuf, O_RDONLY)) < 0)
307 goto notfound;
308
309 /* loop reading lines from /etc/fstab What was that about sscanf again? */
310 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
311 if ((lbuf[0] == 0) || (lbuf[0] == '#'))
312 continue;
313
314 /* skip device name */
315 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
316 ;
317 if (*cp == 0) /* misformatted */
318 continue;
319 /* delimit and save */
320 *cp++ = 0;
321 dev = strdup(lbuf);
322
323 /* skip whitespace up to mountpoint */
324 while ((*cp != 0) && isspace(*cp))
325 cp++;
326 /* must have /<space> to be root */
327 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
328 continue;
329 /* skip whitespace up to fstype */
330 cp += 2;
331 while ((*cp != 0) && isspace(*cp))
332 cp++;
333 if (*cp == 0) /* misformatted */
334 continue;
335 /* skip text to end of fstype and delimit */
336 ep = cp;
337 while ((*cp != 0) && !isspace(*cp))
338 cp++;
339 *cp = 0;
340 fstyp = strdup(ep);
341
342 /* skip whitespace up to mount options */
343 cp += 1;
344 while ((*cp != 0) && isspace(*cp))
345 cp++;
346 if (*cp == 0) /* misformatted */
347 continue;
348 /* skip text to end of mount options and delimit */
349 ep = cp;
350 while ((*cp != 0) && !isspace(*cp))
351 cp++;
352 *cp = 0;
353 options = strdup(ep);
354 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
355 sprintf(lbuf, "%s:%s", fstyp, dev);
356 free(dev);
357 free(fstyp);
358 setenv("vfs.root.mountfrom", lbuf, 0);
359
360 /* Don't override vfs.root.mountfrom.options if it is already set */
361 if (getenv("vfs.root.mountfrom.options") == NULL) {
362 /* save mount options */
363 setenv("vfs.root.mountfrom.options", options, 0);
364 }
365 free(options);
366 error = 0;
367 break;
368 }
369 close(fd);
370
371 notfound:
372 if (error) {
373 const char *currdev;
374
375 currdev = getenv("currdev");
376 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
377 cp = strdup(currdev);
378 cp[strlen(cp) - 1] = '\0';
379 setenv("vfs.root.mountfrom", cp, 0);
380 error = 0;
381 }
382 }
383
384 return(error);
385 }
386
387 static int
loadakernel(int try,int argc,char * argv[])388 loadakernel(int try, int argc, char* argv[])
389 {
390 char *cp;
391
392 for (try = 0; (cp = getbootfile(try)) != NULL; try++)
393 if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
394 printf("can't load '%s'\n", cp);
395 else
396 return 1;
397 return 0;
398 }
399