2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998,2000 Doug Rabson <dfr@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
35 #include <sys/zfs_bootenv.h>
37 #include "bootstrap.h"
39 #include "libuserboot.h"
41 #if defined(USERBOOT_ZFS_SUPPORT)
44 static void userboot_zfs_probe(void);
45 static int userboot_zfs_found;
48 /* Minimum version required */
49 #define USERBOOT_VERSION USERBOOT_VERSION_3
51 #define LOADER_PATH "/boot/loader"
52 #define INTERP_MARKER "$Interpreter:"
54 #define MALLOCSZ (64*1024*1024)
56 struct loader_callbacks *callbacks;
61 struct arch_switch archsw; /* MI/MD interface boundary */
63 static void extract_currdev(void);
64 static void check_interpreter(void);
70 CALLBACK(delay, usec);
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
94 check_interpreter(void)
97 size_t marklen, rdsize;
98 const char *guest_interp, *my_interp;
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.
108 if (stat(LOADER_PATH, &st) != 0)
110 if ((fd = open(LOADER_PATH, O_RDONLY)) < 0)
114 buf = malloc(rdsize);
117 if (read(fd, buf, rdsize) < rdsize)
120 marklen = strlen(INTERP_MARKER);
121 my_interp = bootprog_interp + marklen;
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
129 if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER,
131 guest_interp += marklen;
133 guest_interp = "4th";
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
141 if (strcmp(my_interp, guest_interp) != 0)
142 CALLBACK(swap_interpreter, guest_interp);
150 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
152 static char mallocbuf[MALLOCSZ];
156 if (version < USERBOOT_VERSION)
161 userboot_disk_maxunit = ndisks;
164 * initialise the heap as early as possible. Once this is done,
167 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
170 * Hook up the console
174 /* Set up currdev variable to have hooks in place. */
175 env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
177 printf("\n%s", bootprog_info);
179 printf("Memory: %ld k\n", memsize() / 1024);
182 setenv("LINES", "24", 1); /* optional */
185 * Set custom environment variables
189 var = CALLBACK(getenv, i++);
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;
205 * Initialise the block cache. Set the upper limit.
207 bcache_init(32768, 512);
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.
216 if (version >= USERBOOT_VERSION_5)
222 interact(); /* doesn't return */
228 * Set the 'current device' by (if possible) recovering the boot device as
229 * supplied by the initial bootstrap.
232 extract_currdev(void)
234 struct disk_devdesc dev;
236 #if defined(USERBOOT_ZFS_SUPPORT)
237 struct zfs_devdesc zdev;
240 if (userboot_zfs_found) {
242 /* Leave the pool/root guid's unassigned */
243 bzero(&zdev, sizeof(zdev));
244 zdev.dd.d_dev = &zfs_dev;
246 init_zfs_boot_options(devformat(&zdev.dd));
251 if (userboot_disk_maxunit > 0) {
252 dev.dd.d_dev = &userboot_disk;
254 dev.d_slice = D_SLICEWILD;
255 dev.d_partition = D_PARTWILD;
257 * If we cannot auto-detect the partition type then
258 * access the disk as a raw device.
260 if (dev.dd.d_dev->dv_open(NULL, &dev)) {
261 dev.d_slice = D_SLICENONE;
262 dev.d_partition = D_PARTNONE;
266 dev.dd.d_dev = &host_dev;
271 set_currdev(devformat(dd));
273 #if defined(USERBOOT_ZFS_SUPPORT)
274 if (userboot_zfs_found) {
275 buf = malloc(VDEV_PAD_SIZE);
277 if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf,
278 VDEV_PAD_SIZE) == 0) {
279 printf("zfs bootonce: %s\n", buf);
281 setenv("zfs-bootonce", buf, 1);
284 (void) zfs_attach_nvstore(&zdev);
290 #if defined(USERBOOT_ZFS_SUPPORT)
292 userboot_zfs_probe(void)
299 * Open all the disks we can find and see if we can reconstruct
300 * ZFS pools from them. Record if any were found.
302 for (unit = 0; unit < userboot_disk_maxunit; unit++) {
303 sprintf(devname, "disk%d:", unit);
305 zfs_probe_dev(devname, &pool_guid, true);
307 userboot_zfs_found = 1;
312 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
315 command_quit(int argc, char *argv[])
318 exit(USERBOOT_EXIT_QUIT);
322 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
325 command_reboot(int argc, char *argv[])
328 exit(USERBOOT_EXIT_REBOOT);