xref: /freebsd/stand/userboot/userboot/main.c (revision 0d48d1ffe0446cd2f87ce02555e3d17772ae7284)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998,2000 Doug Rabson <dfr@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <stand.h>
29 #include <string.h>
30 #include <setjmp.h>
31 #include <sys/disk.h>
32 
33 #include "bootstrap.h"
34 #include "disk.h"
35 #include "libuserboot.h"
36 
37 #if defined(USERBOOT_ZFS_SUPPORT)
38 #include <sys/zfs_bootenv.h>
39 #include "libzfs.h"
40 
41 static void userboot_zfs_probe(void);
42 static int userboot_zfs_found;
43 #endif
44 
45 /* Minimum version required */
46 #define	USERBOOT_VERSION	USERBOOT_VERSION_3
47 
48 #define	LOADER_PATH		"/boot/loader"
49 #define	INTERP_MARKER		"$Interpreter:"
50 
51 #define	MALLOCSZ		(64*1024*1024)
52 
53 struct loader_callbacks *callbacks;
54 void *callbacks_arg;
55 
56 static jmp_buf jb;
57 
58 struct arch_switch archsw = {	/* MI/MD interface boundary */
59 	.arch_autoload = userboot_autoload,
60 	.arch_getdev = userboot_getdev,
61 	.arch_copyin = userboot_copyin,
62 	.arch_copyout = userboot_copyout,
63 	.arch_readin = userboot_readin,
64 #if defined(USERBOOT_ZFS_SUPPORT)
65 	.arch_zfs_probe = userboot_zfs_probe,
66 #endif
67 };
68 
69 static void	extract_currdev(void);
70 static void	check_interpreter(void);
71 
72 void
73 delay(int usec)
74 {
75 
76 	CALLBACK(delay, usec);
77 }
78 
79 time_t
80 getsecs(void)
81 {
82 
83 	/*
84 	 * userboot can't do netboot, so this implementation isn't strictly
85 	 * required.  Defining it avoids issues with BIND_NOW, and it doesn't
86 	 * hurt to do it.
87 	 */
88 	return (time(NULL));
89 }
90 
91 void
92 exit(int v)
93 {
94 
95 	CALLBACK(exit, v);
96 	longjmp(jb, 1);
97 }
98 
99 static void
100 check_interpreter(void)
101 {
102 	struct stat st;
103 	size_t marklen, rdsize;
104 	const char *guest_interp, *my_interp;
105 	char *buf;
106 	int fd;
107 
108 	/*
109 	 * If we can't stat(2) or open(2) LOADER_PATH, then we'll fail by
110 	 * simply letting us roll on with whatever interpreter we were compiled
111 	 * with.  This is likely not going to be an issue in reality.
112 	 */
113 	buf =  NULL;
114 	if (stat(LOADER_PATH, &st) != 0)
115 		return;
116 	if ((fd = open(LOADER_PATH, O_RDONLY)) < 0)
117 		return;
118 
119 	rdsize = st.st_size;
120 	buf = malloc(rdsize);
121 	if (buf == NULL)
122 		goto out;
123 	if (read(fd, buf, rdsize) < rdsize)
124 		goto out;
125 
126 	marklen = strlen(INTERP_MARKER);
127 	my_interp = bootprog_interp + marklen;
128 
129 	/*
130 	 * Here we make the assumption that a loader binary without the
131 	 * interpreter marker is a 4th one.  All loader binaries going forward
132 	 * should have this properly specified, so our assumption should always
133 	 * be a good one.
134 	 */
135 	if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER,
136 	    marklen)) != NULL)
137 		guest_interp += marklen;
138 	else
139 		guest_interp = "4th";
140 
141 	/*
142 	 * The guest interpreter may not have a version of loader that
143 	 * specifies the interpreter installed.  If that's the case, we'll
144 	 * assume it's legacy (4th) and request a swap to that if we're
145 	 * a Lua-userboot.
146 	 */
147 	if (strcmp(my_interp, guest_interp) != 0)
148 		CALLBACK(swap_interpreter, guest_interp);
149 out:
150 	free(buf);
151 	close(fd);
152 	return;
153 }
154 
155 void
156 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
157 {
158 	static char mallocbuf[MALLOCSZ];
159 	char *var;
160 	int i;
161 
162 	if (version < USERBOOT_VERSION)
163 		abort();
164 
165 	callbacks = cb;
166 	callbacks_arg = arg;
167 	userboot_disk_maxunit = ndisks;
168 
169 	/*
170 	 * initialise the heap as early as possible.  Once this is done,
171 	 * alloc() is usable.
172 	 */
173 	setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
174 
175 	/*
176 	 * Hook up the console
177 	 */
178 	cons_probe();
179 
180 	/* Set up currdev variable to have hooks in place. */
181 	env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
182 
183 	printf("\n%s", bootprog_info);
184 #if 0
185 	printf("Memory: %ld k\n", memsize() / 1024);
186 #endif
187 
188 	setenv("LINES", "24", 1);	/* optional */
189 
190 	/*
191 	 * Set custom environment variables
192 	 */
193 	i = 0;
194 	while (1) {
195 		var = CALLBACK(getenv, i++);
196 		if (var == NULL)
197 			break;
198 		putenv(var);
199 	}
200 
201 	/*
202 	 * Initialise the block cache. Set the upper limit.
203 	 */
204 	bcache_init(32768, 512);
205 	devinit();
206 	extract_currdev();
207 
208 	/*
209 	 * Checking the interpreter isn't worth the overhead unless we
210 	 * actually have the swap_interpreter callback, so we actually version
211 	 * check here rather than later on.
212 	 */
213 	if (version >= USERBOOT_VERSION_5)
214 		check_interpreter();
215 
216 	if (setjmp(jb))
217 		return;
218 
219 	interact();			/* doesn't return */
220 
221 	exit(0);
222 }
223 
224 /*
225  * Set the 'current device' by (if possible) recovering the boot device as
226  * supplied by the initial bootstrap.
227  */
228 static void
229 extract_currdev(void)
230 {
231 	struct disk_devdesc dev;
232 	struct devdesc *dd;
233 #if defined(USERBOOT_ZFS_SUPPORT)
234 	struct zfs_devdesc zdev;
235 
236 	if (userboot_zfs_found) {
237 
238 		/* Leave the pool/root guid's unassigned */
239 		bzero(&zdev, sizeof(zdev));
240 		zdev.dd.d_dev = &zfs_dev;
241 
242 		init_zfs_boot_options(devformat(&zdev.dd));
243 		dd = &zdev.dd;
244 	} else
245 #endif
246 
247 	if (userboot_disk_maxunit > 0) {
248 		dev.dd.d_dev = &userboot_disk;
249 		dev.dd.d_unit = 0;
250 		dev.d_slice = D_SLICEWILD;
251 		dev.d_partition = D_PARTWILD;
252 		/*
253 		 * If we cannot auto-detect the partition type then
254 		 * access the disk as a raw device.
255 		 */
256 		if (dev.dd.d_dev->dv_open(NULL, &dev)) {
257 			dev.d_slice = D_SLICENONE;
258 			dev.d_partition = D_PARTNONE;
259 		}
260 		dd = &dev.dd;
261 	} else {
262 		dev.dd.d_dev = &host_dev;
263 		dev.dd.d_unit = 0;
264 		dd = &dev.dd;
265 	}
266 
267 	set_currdev(devformat(dd));
268 
269 #if defined(USERBOOT_ZFS_SUPPORT)
270 	if (userboot_zfs_found) {
271 		char buf[VDEV_PAD_SIZE];
272 
273 		if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) {
274 			printf("zfs bootonce: %s\n", buf);
275 			set_currdev(buf);
276 			setenv("zfs-bootonce", buf, 1);
277 		}
278 		(void)zfs_attach_nvstore(&zdev);
279 	}
280 #endif
281 }
282 
283 #if defined(USERBOOT_ZFS_SUPPORT)
284 static void
285 userboot_zfs_probe(void)
286 {
287 	char devname[32];
288 	uint64_t pool_guid;
289 	int unit;
290 
291 	/*
292 	 * Open all the disks we can find and see if we can reconstruct
293 	 * ZFS pools from them. Record if any were found.
294 	 */
295 	for (unit = 0; unit < userboot_disk_maxunit; unit++) {
296 		sprintf(devname, "disk%d:", unit);
297 		pool_guid = 0;
298 		zfs_probe_dev(devname, &pool_guid, true);
299 		if (pool_guid != 0)
300 			userboot_zfs_found = 1;
301 	}
302 }
303 #endif
304 
305 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
306 
307 static int
308 command_quit(int argc, char *argv[])
309 {
310 
311 	exit(USERBOOT_EXIT_QUIT);
312 	return (CMD_OK);
313 }
314 
315 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
316 
317 static int
318 command_reboot(int argc, char *argv[])
319 {
320 
321 	exit(USERBOOT_EXIT_REBOOT);
322 	return (CMD_OK);
323 }
324