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