xref: /freebsd/stand/userboot/userboot/main.c (revision c07d6445eb89d9dd3950361b065b7bd110e3a043)
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 	devinit();
210 	extract_currdev();
211 
212 	/*
213 	 * Checking the interpreter isn't worth the overhead unless we
214 	 * actually have the swap_interpreter callback, so we actually version
215 	 * check here rather than later on.
216 	 */
217 	if (version >= USERBOOT_VERSION_5)
218 		check_interpreter();
219 
220 	if (setjmp(jb))
221 		return;
222 
223 	interact();			/* doesn't return */
224 
225 	exit(0);
226 }
227 
228 static void
229 set_currdev(const char *devname)
230 {
231 
232 	env_setenv("currdev", EV_VOLATILE, devname,
233 	    userboot_setcurrdev, env_nounset);
234 	env_setenv("loaddev", EV_VOLATILE, devname,
235 	    env_noset, env_nounset);
236 }
237 
238 /*
239  * Set the 'current device' by (if possible) recovering the boot device as
240  * supplied by the initial bootstrap.
241  */
242 static void
243 extract_currdev(void)
244 {
245 	struct disk_devdesc dev;
246 	struct devdesc *dd;
247 #if defined(USERBOOT_ZFS_SUPPORT)
248 	struct zfs_devdesc zdev;
249 	char *buf = NULL;
250 
251 	if (userboot_zfs_found) {
252 
253 		/* Leave the pool/root guid's unassigned */
254 		bzero(&zdev, sizeof(zdev));
255 		zdev.dd.d_dev = &zfs_dev;
256 
257 		init_zfs_boot_options(devformat(&zdev.dd));
258 		dd = &zdev.dd;
259 	} else
260 #endif
261 
262 	if (userboot_disk_maxunit > 0) {
263 		dev.dd.d_dev = &userboot_disk;
264 		dev.dd.d_unit = 0;
265 		dev.d_slice = D_SLICEWILD;
266 		dev.d_partition = D_PARTWILD;
267 		/*
268 		 * If we cannot auto-detect the partition type then
269 		 * access the disk as a raw device.
270 		 */
271 		if (dev.dd.d_dev->dv_open(NULL, &dev)) {
272 			dev.d_slice = D_SLICENONE;
273 			dev.d_partition = D_PARTNONE;
274 		}
275 		dd = &dev.dd;
276 	} else {
277 		dev.dd.d_dev = &host_dev;
278 		dev.dd.d_unit = 0;
279 		dd = &dev.dd;
280 	}
281 
282 	set_currdev(devformat(dd));
283 
284 #if defined(USERBOOT_ZFS_SUPPORT)
285 	if (userboot_zfs_found) {
286 		buf = malloc(VDEV_PAD_SIZE);
287 		if (buf != NULL) {
288 			if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf,
289 			    VDEV_PAD_SIZE) == 0) {
290 				printf("zfs bootonce: %s\n", buf);
291 				set_currdev(buf);
292 				setenv("zfs-bootonce", buf, 1);
293 			}
294 			free(buf);
295 			(void) zfs_attach_nvstore(&zdev);
296 		}
297 	}
298 #endif
299 }
300 
301 #if defined(USERBOOT_ZFS_SUPPORT)
302 static void
303 userboot_zfs_probe(void)
304 {
305 	char devname[32];
306 	uint64_t pool_guid;
307 	int unit;
308 
309 	/*
310 	 * Open all the disks we can find and see if we can reconstruct
311 	 * ZFS pools from them. Record if any were found.
312 	 */
313 	for (unit = 0; unit < userboot_disk_maxunit; unit++) {
314 		sprintf(devname, "disk%d:", unit);
315 		pool_guid = 0;
316 		zfs_probe_dev(devname, &pool_guid, true);
317 		if (pool_guid != 0)
318 			userboot_zfs_found = 1;
319 	}
320 }
321 #endif
322 
323 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
324 
325 static int
326 command_quit(int argc, char *argv[])
327 {
328 
329 	exit(USERBOOT_EXIT_QUIT);
330 	return (CMD_OK);
331 }
332 
333 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
334 
335 static int
336 command_reboot(int argc, char *argv[])
337 {
338 
339 	exit(USERBOOT_EXIT_REBOOT);
340 	return (CMD_OK);
341 }
342