xref: /freebsd/stand/userboot/userboot/main.c (revision 38a52bd3)
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 #include <sys/zfs_bootenv.h>
36 
37 #include "bootstrap.h"
38 #include "disk.h"
39 #include "libuserboot.h"
40 
41 #if defined(USERBOOT_ZFS_SUPPORT)
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, "",
176 	    userboot_setcurrdev, env_nounset);
177 
178 	printf("\n%s", bootprog_info);
179 #if 0
180 	printf("Memory: %ld k\n", memsize() / 1024);
181 #endif
182 
183 	setenv("LINES", "24", 1);	/* optional */
184 
185 	/*
186 	 * Set custom environment variables
187 	 */
188 	i = 0;
189 	while (1) {
190 		var = CALLBACK(getenv, i++);
191 		if (var == NULL)
192 			break;
193 		putenv(var);
194 	}
195 
196 	archsw.arch_autoload = userboot_autoload;
197 	archsw.arch_getdev = userboot_getdev;
198 	archsw.arch_copyin = userboot_copyin;
199 	archsw.arch_copyout = userboot_copyout;
200 	archsw.arch_readin = userboot_readin;
201 #if defined(USERBOOT_ZFS_SUPPORT)
202 	archsw.arch_zfs_probe = userboot_zfs_probe;
203 #endif
204 
205 	/*
206 	 * Initialise the block cache. Set the upper limit.
207 	 */
208 	bcache_init(32768, 512);
209 	/*
210 	 * March through the device switch probing for things.
211 	 */
212 	for (i = 0; devsw[i] != NULL; i++)
213 		if (devsw[i]->dv_init != NULL)
214 			(devsw[i]->dv_init)();
215 
216 	extract_currdev();
217 
218 	/*
219 	 * Checking the interpreter isn't worth the overhead unless we
220 	 * actually have the swap_interpreter callback, so we actually version
221 	 * check here rather than later on.
222 	 */
223 	if (version >= USERBOOT_VERSION_5)
224 		check_interpreter();
225 
226 	if (setjmp(jb))
227 		return;
228 
229 	interact();			/* doesn't return */
230 
231 	exit(0);
232 }
233 
234 static void
235 set_currdev(const char *devname)
236 {
237 
238 	env_setenv("currdev", EV_VOLATILE, devname,
239 	    userboot_setcurrdev, env_nounset);
240 	env_setenv("loaddev", EV_VOLATILE, devname,
241 	    env_noset, env_nounset);
242 }
243 
244 /*
245  * Set the 'current device' by (if possible) recovering the boot device as
246  * supplied by the initial bootstrap.
247  */
248 static void
249 extract_currdev(void)
250 {
251 	struct disk_devdesc dev;
252 	struct devdesc *dd;
253 #if defined(USERBOOT_ZFS_SUPPORT)
254 	struct zfs_devdesc zdev;
255 	char *buf = NULL;
256 
257 	if (userboot_zfs_found) {
258 
259 		/* Leave the pool/root guid's unassigned */
260 		bzero(&zdev, sizeof(zdev));
261 		zdev.dd.d_dev = &zfs_dev;
262 
263 		init_zfs_boot_options(devformat(&zdev.dd));
264 		dd = &zdev.dd;
265 	} else
266 #endif
267 
268 	if (userboot_disk_maxunit > 0) {
269 		dev.dd.d_dev = &userboot_disk;
270 		dev.dd.d_unit = 0;
271 		dev.d_slice = D_SLICEWILD;
272 		dev.d_partition = D_PARTWILD;
273 		/*
274 		 * If we cannot auto-detect the partition type then
275 		 * access the disk as a raw device.
276 		 */
277 		if (dev.dd.d_dev->dv_open(NULL, &dev)) {
278 			dev.d_slice = D_SLICENONE;
279 			dev.d_partition = D_PARTNONE;
280 		}
281 		dd = &dev.dd;
282 	} else {
283 		dev.dd.d_dev = &host_dev;
284 		dev.dd.d_unit = 0;
285 		dd = &dev.dd;
286 	}
287 
288 	set_currdev(devformat(dd));
289 
290 #if defined(USERBOOT_ZFS_SUPPORT)
291 	if (userboot_zfs_found) {
292 		buf = malloc(VDEV_PAD_SIZE);
293 		if (buf != NULL) {
294 			if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf,
295 			    VDEV_PAD_SIZE) == 0) {
296 				printf("zfs bootonce: %s\n", buf);
297 				set_currdev(buf);
298 				setenv("zfs-bootonce", buf, 1);
299 			}
300 			free(buf);
301 			(void) zfs_attach_nvstore(&zdev);
302 		}
303 	}
304 #endif
305 }
306 
307 #if defined(USERBOOT_ZFS_SUPPORT)
308 static void
309 userboot_zfs_probe(void)
310 {
311 	char devname[32];
312 	uint64_t pool_guid;
313 	int unit;
314 
315 	/*
316 	 * Open all the disks we can find and see if we can reconstruct
317 	 * ZFS pools from them. Record if any were found.
318 	 */
319 	for (unit = 0; unit < userboot_disk_maxunit; unit++) {
320 		sprintf(devname, "disk%d:", unit);
321 		pool_guid = 0;
322 		zfs_probe_dev(devname, &pool_guid);
323 		if (pool_guid != 0)
324 			userboot_zfs_found = 1;
325 	}
326 }
327 #endif
328 
329 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
330 
331 static int
332 command_quit(int argc, char *argv[])
333 {
334 
335 	exit(USERBOOT_EXIT_QUIT);
336 	return (CMD_OK);
337 }
338 
339 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
340 
341 static int
342 command_reboot(int argc, char *argv[])
343 {
344 
345 	exit(USERBOOT_EXIT_REBOOT);
346 	return (CMD_OK);
347 }
348