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