xref: /openbsd/sys/arch/powerpc64/stand/rdboot/rdboot.c (revision 952f302f)
1 /*	$OpenBSD: rdboot.c,v 1.3 2020/12/09 18:10:19 krw Exp $	*/
2 
3 /*
4  * Copyright (c) 2019-2020 Visa Hankala
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include <sys/reboot.h>
24 #include <sys/select.h>
25 #include <sys/stat.h>
26 
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <paths.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <termios.h>
35 #include <unistd.h>
36 #include <util.h>
37 
38 #include <machine/kexec.h>
39 #include <machine/param.h>
40 
41 #include "cmd.h"
42 #include "disk.h"
43 
44 #define DEVRANDOM	"/dev/random"
45 #define BOOTRANDOM	"/etc/random.seed"
46 #define BOOTRANDOM_MAX	256	/* no point being greater than RC4STATE */
47 #define KERNEL		"/bsd"
48 
49 int	loadrandom(void);
50 void	kexec(void);
51 
52 struct cmd_state cmd;
53 int kexecfd = -1;
54 const char version[] = "0.2";
55 
56 int
57 main(void)
58 {
59 	u_char bootduid[8];
60 	int fd, hasboot;
61 
62 	fd = open(_PATH_CONSOLE, O_RDWR);
63 	login_tty(fd);
64 
65 	/* Keep stdout unbuffered to mimic ordinary bootblocks. */
66 	setvbuf(stdout, NULL, _IONBF, 0);
67 
68 	printf(">> OpenBSD/" MACHINE " BOOT %s\n", version);
69 
70 	kexecfd = open("/dev/kexec", O_WRONLY);
71 	if (kexecfd == -1)
72 		err(1, "cannot open boot control device");
73 
74 	memset(&cmd, 0, sizeof(cmd));
75 	cmd.boothowto = 0;
76 	cmd.conf = "/etc/boot.conf";
77 	strlcpy(cmd.image, KERNEL, sizeof(cmd.image));
78 	cmd.timeout = 5;
79 
80 	if (ioctl(kexecfd, KIOC_GETBOOTDUID, bootduid) == -1) {
81 		fprintf(stderr, "cannot get bootduid from kernel: %s\n",
82 		    strerror(errno));
83 	} else {
84 		memcpy(cmd.bootduid, bootduid, sizeof(cmd.bootduid));
85 	}
86 
87 	disk_init();
88 
89 	if (upgrade()) {
90 		strlcpy(cmd.image, "/bsd.upgrade", sizeof(cmd.image));
91 		printf("upgrade detected: switching to %s\n", cmd.image);
92 	}
93 
94 	hasboot = read_conf();
95 
96 	for (;;) {
97 		if (hasboot <= 0) {
98 			do {
99 				printf("boot> ");
100 			} while (!getcmd());
101 		}
102 
103 		if (loadrandom() == 0)
104 			cmd.boothowto |= RB_GOODRANDOM;
105 
106 		kexec();
107 
108 		hasboot = 0;
109 		strlcpy(cmd.image, KERNEL, sizeof(cmd.image));
110 		printf("will try %s\n", cmd.image);
111 	}
112 
113 	return 0;
114 }
115 
116 int
117 loadrandom(void)
118 {
119 	char buf[BOOTRANDOM_MAX];
120 	struct stat sb;
121 	int fd, ret = 0;
122 
123 	/* Read the file from the device specified by the kernel path. */
124 	if (disk_open(cmd.path) == NULL)
125 		return -1;
126 	fd = open(BOOTRANDOM, O_RDONLY);
127 	if (fd == -1) {
128 		fprintf(stderr, "%s: cannot open %s: %s", __func__, BOOTRANDOM,
129 		    strerror(errno));
130 		disk_close();
131 		return -1;
132 	}
133 	if (fstat(fd, &sb) == 0) {
134 		if (sb.st_mode & S_ISTXT) {
135 			printf("NOTE: random seed is being reused.\n");
136 			ret = -1;
137 		}
138 		if (read(fd, buf, sizeof(buf)) != sizeof(buf))
139 			ret = -1;
140 		fchmod(fd, sb.st_mode | S_ISTXT);
141 	} else {
142 		ret = -1;
143 	}
144 	close(fd);
145 	disk_close();
146 
147 	/*
148 	 * Push the whole buffer to the entropy pool.
149 	 * The kernel will use the entropy on kexec().
150 	 * It does not matter if some of the buffer content is uninitialized.
151 	 */
152 	fd = open(DEVRANDOM, O_WRONLY);
153 	if (fd == -1) {
154 		fprintf(stderr, "%s: cannot open %s: %s", __func__,
155 		    DEVRANDOM, strerror(errno));
156 		return -1;
157 	}
158 	write(fd, buf, sizeof(buf));
159 	close(fd);
160 	return ret;
161 }
162 
163 void
164 kexec(void)
165 {
166 	struct kexec_args kargs;
167 	struct stat sb;
168 	char *kimg = NULL;
169 	const char *path;
170 	ssize_t n;
171 	off_t pos;
172 	int fd = -1, ret;
173 
174 	path = disk_open(cmd.path);
175 	if (path == NULL)
176 		return;
177 
178 	fd = open(path, O_RDONLY);
179 	if (fd == -1)
180 		goto load_failed;
181 	if (fstat(fd, &sb) == -1)
182 		goto load_failed;
183 	if (!S_ISREG(sb.st_mode) || sb.st_size == 0) {
184 		errno = ENOEXEC;
185 		goto load_failed;
186 	}
187 
188 	kimg = malloc(sb.st_size);
189 	if (kimg == NULL)
190 		goto load_failed;
191 
192 	pos = 0;
193 	while (pos < sb.st_size) {
194 		n = read(fd, kimg + pos, sb.st_size - pos);
195 		if (n == -1)
196 			goto load_failed;
197 		pos += n;
198 	}
199 
200 	close(fd);
201 	disk_close();
202 
203 	memset(&kargs, 0, sizeof(kargs));
204 	kargs.kimg = kimg;
205 	kargs.klen = sb.st_size;
206 	kargs.boothowto = cmd.boothowto;
207 	memcpy(kargs.bootduid, cmd.bootduid, sizeof(kargs.bootduid));
208 
209 	printf("booting %s\n", cmd.path);
210 	ret = ioctl(kexecfd, KIOC_KEXEC, &kargs);
211 	if (ret == -1)
212 		fprintf(stderr, "failed to execute kernel %s: %s\n",
213 		    cmd.path, strerror(errno));
214 	else
215 		fprintf(stderr, "kexec() returned unexpectedly\n");
216 	free(kimg);
217 	return;
218 
219 load_failed:
220 	fprintf(stderr, "failed to load kernel %s: %s\n",
221 	    cmd.path, strerror(errno));
222 	if (fd != -1)
223 		close(fd);
224 	disk_close();
225 	free(kimg);
226 }
227