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