xref: /freebsd/stand/common/boot.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
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 <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
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 #endif
112 
113 	/* Call the exec handler from the loader matching the kernel */
114 	file_formats[fp->f_loader]->l_exec(fp);
115 	return(CMD_ERROR);
116 }
117 
118 
119 /*
120  * Autoboot after a delay
121  */
122 
123 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
124 
125 static int
126 command_autoboot(int argc, char *argv[])
127 {
128 	int		howlong;
129 	char	*cp, *prompt;
130 
131 	prompt = NULL;
132 	howlong = -1;
133 	switch(argc) {
134 	case 3:
135 		prompt = argv[2];
136 		/* FALLTHROUGH */
137 	case 2:
138 		howlong = strtol(argv[1], &cp, 0);
139 		if (*cp != 0) {
140 			snprintf(command_errbuf, sizeof(command_errbuf),
141 			    "bad delay '%s'", argv[1]);
142 			return(CMD_ERROR);
143 		}
144 		/* FALLTHROUGH */
145 	case 1:
146 		return(autoboot(howlong, prompt));
147 	}
148 
149 	command_errmsg = "too many arguments";
150 	return(CMD_ERROR);
151 }
152 
153 /*
154  * Called before we go interactive.  If we think we can autoboot, and
155  * we haven't tried already, try now.
156  */
157 void
158 autoboot_maybe()
159 {
160 	char	*cp;
161 
162 	cp = getenv("autoboot_delay");
163 	if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
164 		autoboot(-1, NULL);		/* try to boot automatically */
165 }
166 
167 static int
168 autoboot(int timeout, char *prompt)
169 {
170 	time_t	when, otime, ntime;
171 	int		c, yes;
172 	char	*argv[2], *cp, *ep;
173 	char	*kernelname;
174 #ifdef BOOT_PROMPT_123
175 	const char	*seq = "123", *p = seq;
176 #endif
177 
178 	autoboot_tried = 1;
179 
180 	if (timeout == -1) {
181 		timeout = 10;
182 		/* try to get a delay from the environment */
183 		if ((cp = getenv("autoboot_delay"))) {
184 			timeout = strtol(cp, &ep, 0);
185 			if (cp == ep)
186 				timeout = 10;		/* Unparseable? Set default! */
187 		}
188 	}
189 
190 	kernelname = getenv("kernelname");
191 	if (kernelname == NULL) {
192 		argv[0] = NULL;
193 		loadakernel(0, 0, argv);
194 		kernelname = getenv("kernelname");
195 		if (kernelname == NULL) {
196 			command_errmsg = "no valid kernel found";
197 			return(CMD_ERROR);
198 		}
199 	}
200 
201 	if (timeout >= 0) {
202 		otime = time(NULL);
203 		when = otime + timeout;	/* when to boot */
204 
205 		yes = 0;
206 
207 #ifdef BOOT_PROMPT_123
208 		printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
209 		    "1 2 3 sequence for command prompt." : prompt);
210 #else
211 		printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
212 #endif
213 
214 		for (;;) {
215 			if (ischar()) {
216 				c = getchar();
217 #ifdef BOOT_PROMPT_123
218 				if ((c == '\r') || (c == '\n')) {
219 					yes = 1;
220 					break;
221 				} else if (c != *p++)
222 					p = seq;
223 				if (*p == 0)
224 					break;
225 #else
226 				if ((c == '\r') || (c == '\n'))
227 					yes = 1;
228 				break;
229 #endif
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 	} else {
245 		yes = 1;
246 	}
247 
248 	if (yes)
249 		printf("\rBooting [%s]...               ", kernelname);
250 	putchar('\n');
251 	if (yes) {
252 		argv[0] = "boot";
253 		argv[1] = NULL;
254 		return(command_boot(1, argv));
255 	}
256 	return(CMD_OK);
257 }
258 
259 /*
260  * Scrounge for the name of the (try)'th file we will try to boot.
261  */
262 static char *
263 getbootfile(int try)
264 {
265 	static char *name = NULL;
266 	const char	*spec, *ep;
267 	size_t	len;
268 
269 	/* we use dynamic storage */
270 	if (name != NULL) {
271 		free(name);
272 		name = NULL;
273 	}
274 
275 	/*
276 	 * Try $bootfile, then try our builtin default
277 	 */
278 	if ((spec = getenv("bootfile")) == NULL)
279 		spec = default_bootfiles;
280 
281 	while ((try > 0) && (spec != NULL)) {
282 		spec = strchr(spec, ';');
283 		if (spec)
284 			spec++;	/* skip over the leading ';' */
285 		try--;
286 	}
287 	if (spec != NULL) {
288 		if ((ep = strchr(spec, ';')) != NULL) {
289 			len = ep - spec;
290 		} else {
291 			len = strlen(spec);
292 		}
293 		name = malloc(len + 1);
294 		strncpy(name, spec, len);
295 		name[len] = 0;
296 	}
297 	if (name && name[0] == 0) {
298 		free(name);
299 		name = NULL;
300 	}
301 	return(name);
302 }
303 
304 /*
305  * Try to find the /etc/fstab file on the filesystem (rootdev),
306  * which should be be the root filesystem, and parse it to find
307  * out what the kernel ought to think the root filesystem is.
308  *
309  * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
310  * so that the kernel can tell both which VFS and which node to use
311  * to mount the device.  If this variable's already set, don't
312  * overwrite it.
313  */
314 int
315 getrootmount(char *rootdev)
316 {
317 	char	lbuf[128], *cp, *ep, *dev, *fstyp, *options;
318 	int		fd, error;
319 
320 	if (getenv("vfs.root.mountfrom") != NULL)
321 		return(0);
322 
323 	error = 1;
324 	sprintf(lbuf, "%s/etc/fstab", rootdev);
325 	if ((fd = open(lbuf, O_RDONLY)) < 0)
326 		goto notfound;
327 
328 	/* loop reading lines from /etc/fstab    What was that about sscanf again? */
329 	fstyp = NULL;
330 	dev = NULL;
331 	while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
332 		if ((lbuf[0] == 0) || (lbuf[0] == '#'))
333 			continue;
334 
335 		/* skip device name */
336 		for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
337 			;
338 		if (*cp == 0)		/* misformatted */
339 			continue;
340 		/* delimit and save */
341 		*cp++ = 0;
342 		free(dev);
343 		dev = strdup(lbuf);
344 
345 		/* skip whitespace up to mountpoint */
346 		while ((*cp != 0) && isspace(*cp))
347 			cp++;
348 		/* must have /<space> to be root */
349 		if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
350 			continue;
351 		/* skip whitespace up to fstype */
352 		cp += 2;
353 		while ((*cp != 0) && isspace(*cp))
354 			cp++;
355 		if (*cp == 0)		/* misformatted */
356 			continue;
357 		/* skip text to end of fstype and delimit */
358 		ep = cp;
359 		while ((*cp != 0) && !isspace(*cp))
360 			cp++;
361 		*cp = 0;
362 		free(fstyp);
363 		fstyp = strdup(ep);
364 
365 		/* skip whitespace up to mount options */
366 		cp += 1;
367 		while ((*cp != 0) && isspace(*cp))
368 			cp++;
369 		if (*cp == 0)           /* misformatted */
370 			continue;
371 		/* skip text to end of mount options and delimit */
372 		ep = cp;
373 		while ((*cp != 0) && !isspace(*cp))
374 			cp++;
375 		*cp = 0;
376 		options = strdup(ep);
377 		/* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
378 		sprintf(lbuf, "%s:%s", fstyp, dev);
379 		setenv("vfs.root.mountfrom", lbuf, 0);
380 
381 		/* Don't override vfs.root.mountfrom.options if it is already set */
382 		if (getenv("vfs.root.mountfrom.options") == NULL) {
383 			/* save mount options */
384 			setenv("vfs.root.mountfrom.options", options, 0);
385 		}
386 		free(options);
387 		error = 0;
388 		break;
389 	}
390 	close(fd);
391 	free(dev);
392 	free(fstyp);
393 
394 notfound:
395 	if (error) {
396 		const char *currdev;
397 
398 		currdev = getenv("currdev");
399 		if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
400 			cp = strdup(currdev);
401 			cp[strlen(cp) - 1] = '\0';
402 			setenv("vfs.root.mountfrom", cp, 0);
403 			error = 0;
404 			free(cp);
405 		}
406 	}
407 
408 	return(error);
409 }
410 
411 static int
412 loadakernel(int try, int argc, char* argv[])
413 {
414 	char *cp;
415 
416 	for (try = 0; (cp = getbootfile(try)) != NULL; try++)
417 		if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
418 			printf("can't load '%s'\n", cp);
419 		else
420 			return 1;
421 	return 0;
422 }
423