xref: /freebsd/stand/userboot/userboot/main.c (revision f126890a)
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 
60 static void	extract_currdev(void);
61 static void	check_interpreter(void);
62 
63 void
64 delay(int usec)
65 {
66 
67 	CALLBACK(delay, usec);
68 }
69 
70 time_t
71 getsecs(void)
72 {
73 
74 	/*
75 	 * userboot can't do netboot, so this implementation isn't strictly
76 	 * required.  Defining it avoids issues with BIND_NOW, and it doesn't
77 	 * hurt to do it.
78 	 */
79 	return (time(NULL));
80 }
81 
82 void
83 exit(int v)
84 {
85 
86 	CALLBACK(exit, v);
87 	longjmp(jb, 1);
88 }
89 
90 static void
91 check_interpreter(void)
92 {
93 	struct stat st;
94 	size_t marklen, rdsize;
95 	const char *guest_interp, *my_interp;
96 	char *buf;
97 	int fd;
98 
99 	/*
100 	 * If we can't stat(2) or open(2) LOADER_PATH, then we'll fail by
101 	 * simply letting us roll on with whatever interpreter we were compiled
102 	 * with.  This is likely not going to be an issue in reality.
103 	 */
104 	buf =  NULL;
105 	if (stat(LOADER_PATH, &st) != 0)
106 		return;
107 	if ((fd = open(LOADER_PATH, O_RDONLY)) < 0)
108 		return;
109 
110 	rdsize = st.st_size;
111 	buf = malloc(rdsize);
112 	if (buf == NULL)
113 		goto out;
114 	if (read(fd, buf, rdsize) < rdsize)
115 		goto out;
116 
117 	marklen = strlen(INTERP_MARKER);
118 	my_interp = bootprog_interp + marklen;
119 
120 	/*
121 	 * Here we make the assumption that a loader binary without the
122 	 * interpreter marker is a 4th one.  All loader binaries going forward
123 	 * should have this properly specified, so our assumption should always
124 	 * be a good one.
125 	 */
126 	if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER,
127 	    marklen)) != NULL)
128 		guest_interp += marklen;
129 	else
130 		guest_interp = "4th";
131 
132 	/*
133 	 * The guest interpreter may not have a version of loader that
134 	 * specifies the interpreter installed.  If that's the case, we'll
135 	 * assume it's legacy (4th) and request a swap to that if we're
136 	 * a Lua-userboot.
137 	 */
138 	if (strcmp(my_interp, guest_interp) != 0)
139 		CALLBACK(swap_interpreter, guest_interp);
140 out:
141 	free(buf);
142 	close(fd);
143 	return;
144 }
145 
146 void
147 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
148 {
149 	static char mallocbuf[MALLOCSZ];
150 	char *var;
151 	int i;
152 
153 	if (version < USERBOOT_VERSION)
154 		abort();
155 
156 	callbacks = cb;
157 	callbacks_arg = arg;
158 	userboot_disk_maxunit = ndisks;
159 
160 	/*
161 	 * initialise the heap as early as possible.  Once this is done,
162 	 * alloc() is usable.
163 	 */
164 	setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
165 
166 	/*
167 	 * Hook up the console
168 	 */
169 	cons_probe();
170 
171 	/* Set up currdev variable to have hooks in place. */
172 	env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
173 
174 	printf("\n%s", bootprog_info);
175 #if 0
176 	printf("Memory: %ld k\n", memsize() / 1024);
177 #endif
178 
179 	setenv("LINES", "24", 1);	/* optional */
180 
181 	/*
182 	 * Set custom environment variables
183 	 */
184 	i = 0;
185 	while (1) {
186 		var = CALLBACK(getenv, i++);
187 		if (var == NULL)
188 			break;
189 		putenv(var);
190 	}
191 
192 	archsw.arch_autoload = userboot_autoload;
193 	archsw.arch_getdev = userboot_getdev;
194 	archsw.arch_copyin = userboot_copyin;
195 	archsw.arch_copyout = userboot_copyout;
196 	archsw.arch_readin = userboot_readin;
197 #if defined(USERBOOT_ZFS_SUPPORT)
198 	archsw.arch_zfs_probe = userboot_zfs_probe;
199 #endif
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