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