xref: /openbsd/sys/stand/boot/boot.c (revision 5af055cd)
1 /*	$OpenBSD: boot.c,v 1.43 2014/02/19 22:02:15 miod Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Dale Rahn
5  * Copyright (c) 1997,1998 Michael Shalayeff
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/stat.h>
34 #include <libsa.h>
35 #include <lib/libsa/loadfile.h>
36 #include <lib/libkern/funcs.h>
37 
38 #include <stand/boot/bootarg.h>
39 
40 #include "cmd.h"
41 
42 #ifndef KERNEL
43 #define KERNEL "/bsd"
44 #endif
45 
46 char prog_ident[40];
47 char *progname = "BOOT";
48 
49 extern	const char version[];
50 struct cmd_state cmd;
51 
52 /* bootprompt can be set by MD code to avoid prompt first time round */
53 int bootprompt = 1;
54 char *kernelfile = KERNEL;		/* can be changed by MD code */
55 int boottimeout = 5;			/* can be changed by MD code */
56 
57 char	rnddata[BOOTRANDOM_MAX];
58 
59 void
60 boot(dev_t bootdev)
61 {
62 	int fd;
63 	int try = 0, st;
64 	u_long marks[MARK_MAX];
65 
66 	machdep();
67 
68 	snprintf(prog_ident, sizeof(prog_ident),
69 	    ">> OpenBSD/" MACHINE " %s %s", progname, version);
70 	printf("%s\n", prog_ident);
71 
72 	devboot(bootdev, cmd.bootdev);
73 	strlcpy(cmd.image, kernelfile, sizeof(cmd.image));
74 	cmd.boothowto = 0;
75 	cmd.conf = "/etc/boot.conf";
76 	cmd.addr = (void *)DEFAULT_KERNEL_ADDRESS;
77 	cmd.timeout = boottimeout;
78 
79 	st = read_conf();
80 	if (!bootprompt)
81 		snprintf(cmd.path, sizeof cmd.path, "%s:%s",
82 		    cmd.bootdev, cmd.image);
83 
84 	while (1) {
85 		/* no boot.conf, or no boot cmd in there */
86 		if (bootprompt && st <= 0) {
87 			do {
88 				printf("boot> ");
89 			} while(!getcmd());
90 		}
91 
92 		loadrandom(BOOTRANDOM, rnddata, sizeof(rnddata));
93 #ifdef MDRANDOM
94 		mdrandom(rnddata, sizeof(rnddata));
95 #endif
96 
97 		st = 0;
98 		bootprompt = 1;	/* allow reselect should we fail */
99 
100 		printf("booting %s: ", cmd.path);
101 		marks[MARK_START] = (u_long)cmd.addr;
102 		if ((fd = loadfile(cmd.path, marks, LOAD_ALL)) != -1) {
103 			close(fd);
104 			break;
105 		}
106 
107 		kernelfile = KERNEL;
108 		try++;
109 		strlcpy(cmd.image, kernelfile, sizeof(cmd.image));
110 		printf(" failed(%d). will try %s\n", errno, kernelfile);
111 
112 		if (try < 2) {
113 			if (cmd.timeout > 0)
114 				cmd.timeout++;
115 		} else {
116 			if (cmd.timeout)
117 				printf("Turning timeout off.\n");
118 			cmd.timeout = 0;
119 		}
120 	}
121 
122 	/* exec */
123 	run_loadfile(marks, cmd.boothowto);
124 }
125 
126 void
127 loadrandom(char *name, char *buf, size_t buflen)
128 {
129 	char path[MAXPATHLEN];
130 	struct stat sb;
131 	int fd, i;
132 
133 #define O_RDONLY	0
134 
135 	/* Extract the device name from the kernel we are loading. */
136 	for (i = 0; i < sizeof(cmd.path); i++) {
137 		if (cmd.path[i] == ':') {
138 			strlcpy(path, cmd.path, i + 1);
139 			snprintf(path + i, sizeof(path) - i, ":%s", name);
140 			break;
141 		} else if (cmd.path[i] == '\0') {
142 			snprintf(path, sizeof path, "%s:%s",
143 			    cmd.bootdev, name);
144 			break;
145 		}
146 	}
147 
148 	fd = open(path, O_RDONLY);
149 	if (fd == -1) {
150 		if (errno != EPERM)
151 			printf("cannot open %s: %s\n", path, strerror(errno));
152 		return;
153 	}
154 	if (fstat(fd, &sb) == -1 ||
155 	    sb.st_uid != 0 ||
156 	    (sb.st_mode & (S_IWOTH|S_IROTH)))
157 		goto fail;
158 	(void) read(fd, buf, buflen);
159 fail:
160 	close(fd);
161 }
162