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);
82 check_interpreter(void)
85 size_t marklen, rdsize;
86 const char *guest_interp, *my_interp;
91 * If we can't stat(2) or open(2) LOADER_PATH, then we'll fail by
92 * simply letting us roll on with whatever interpreter we were compiled
93 * with. This is likely not going to be an issue in reality.
96 if (stat(LOADER_PATH, &st) != 0)
98 if ((fd = open(LOADER_PATH, O_RDONLY)) < 0)
102 buf = malloc(rdsize);
105 if (read(fd, buf, rdsize) < rdsize)
108 marklen = strlen(INTERP_MARKER);
109 my_interp = bootprog_interp + marklen;
112 * Here we make the assumption that a loader binary without the
113 * interpreter marker is a 4th one. All loader binaries going forward
114 * should have this properly specified, so our assumption should always
117 if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER,
119 guest_interp += marklen;
121 guest_interp = "4th";
124 * The guest interpreter may not have a version of loader that
125 * specifies the interpreter installed. If that's the case, we'll
126 * assume it's legacy (4th) and request a swap to that if we're
129 if (strcmp(my_interp, guest_interp) != 0)
130 CALLBACK(swap_interpreter, guest_interp);
138 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
140 static char mallocbuf[MALLOCSZ];
144 if (version < USERBOOT_VERSION)
149 userboot_disk_maxunit = ndisks;
152 * initialise the heap as early as possible. Once this is done,
155 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
158 * Hook up the console
162 printf("\n%s", bootprog_info);
164 printf("Memory: %ld k\n", memsize() / 1024);
167 setenv("LINES", "24", 1); /* optional */
170 * Set custom environment variables
174 var = CALLBACK(getenv, i++);
180 archsw.arch_autoload = userboot_autoload;
181 archsw.arch_getdev = userboot_getdev;
182 archsw.arch_copyin = userboot_copyin;
183 archsw.arch_copyout = userboot_copyout;
184 archsw.arch_readin = userboot_readin;
185 #if defined(USERBOOT_ZFS_SUPPORT)
186 archsw.arch_zfs_probe = userboot_zfs_probe;
190 * Initialise the block cache. Set the upper limit.
192 bcache_init(32768, 512);
194 * March through the device switch probing for things.
196 for (i = 0; devsw[i] != NULL; i++)
197 if (devsw[i]->dv_init != NULL)
198 (devsw[i]->dv_init)();
203 * Checking the interpreter isn't worth the overhead unless we
204 * actually have the swap_interpreter callback, so we actually version
205 * check here rather than later on.
207 if (version >= USERBOOT_VERSION_5)
213 interact(); /* doesn't return */
219 set_currdev(const char *devname)
222 env_setenv("currdev", EV_VOLATILE, devname,
223 userboot_setcurrdev, env_nounset);
224 env_setenv("loaddev", EV_VOLATILE, devname,
225 env_noset, env_nounset);
229 * Set the 'current device' by (if possible) recovering the boot device as
230 * supplied by the initial bootstrap.
233 extract_currdev(void)
235 struct disk_devdesc dev;
237 #if defined(USERBOOT_ZFS_SUPPORT)
238 struct zfs_devdesc zdev;
241 if (userboot_zfs_found) {
243 /* Leave the pool/root guid's unassigned */
244 bzero(&zdev, sizeof(zdev));
245 zdev.dd.d_dev = &zfs_dev;
247 init_zfs_boot_options(zfs_fmtdev(&zdev));
252 if (userboot_disk_maxunit > 0) {
253 dev.dd.d_dev = &userboot_disk;
255 dev.d_slice = D_SLICEWILD;
256 dev.d_partition = D_PARTWILD;
258 * If we cannot auto-detect the partition type then
259 * access the disk as a raw device.
261 if (dev.dd.d_dev->dv_open(NULL, &dev)) {
262 dev.d_slice = D_SLICENONE;
263 dev.d_partition = D_PARTNONE;
267 dev.dd.d_dev = &host_dev;
272 set_currdev(userboot_fmtdev(dd));
274 #if defined(USERBOOT_ZFS_SUPPORT)
275 if (userboot_zfs_found) {
276 buf = malloc(VDEV_PAD_SIZE);
278 if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf,
279 VDEV_PAD_SIZE) == 0) {
280 printf("zfs bootonce: %s\n", buf);
282 setenv("zfs-bootonce", buf, 1);
285 (void) zfs_attach_nvstore(&zdev);
291 #if defined(USERBOOT_ZFS_SUPPORT)
293 userboot_zfs_probe(void)
300 * Open all the disks we can find and see if we can reconstruct
301 * ZFS pools from them. Record if any were found.
303 for (unit = 0; unit < userboot_disk_maxunit; unit++) {
304 sprintf(devname, "disk%d:", unit);
306 zfs_probe_dev(devname, &pool_guid);
308 userboot_zfs_found = 1;
313 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
316 command_quit(int argc, char *argv[])
319 exit(USERBOOT_EXIT_QUIT);
323 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
326 command_reboot(int argc, char *argv[])
329 exit(USERBOOT_EXIT_REBOOT);