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$");
36 #include "bootstrap.h"
38 #include "libuserboot.h"
40 #if defined(USERBOOT_ZFS_SUPPORT)
43 static void userboot_zfs_probe(void);
44 static int userboot_zfs_found;
47 /* Minimum version required */
48 #define USERBOOT_VERSION USERBOOT_VERSION_3
50 #define LOADER_PATH "/boot/loader"
51 #define INTERP_MARKER "$Interpreter:"
53 #define MALLOCSZ (64*1024*1024)
55 struct loader_callbacks *callbacks;
60 struct arch_switch archsw; /* MI/MD interface boundary */
62 static void extract_currdev(void);
63 static void check_interpreter(void);
69 CALLBACK(delay, usec);
81 check_interpreter(void)
84 size_t marklen, rdsize;
85 const char *guest_interp, *my_interp;
90 * If we can't stat(2) or open(2) LOADER_PATH, then we'll fail by
91 * simply letting us roll on with whatever interpreter we were compiled
92 * with. This is likely not going to be an issue in reality.
95 if (stat(LOADER_PATH, &st) != 0)
97 if ((fd = open(LOADER_PATH, O_RDONLY)) < 0)
101 buf = malloc(rdsize);
104 if (read(fd, buf, rdsize) < rdsize)
107 marklen = strlen(INTERP_MARKER);
108 my_interp = bootprog_interp + marklen;
111 * Here we make the assumption that a loader binary without the
112 * interpreter marker is a 4th one. All loader binaries going forward
113 * should have this properly specified, so our assumption should always
116 if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER,
118 guest_interp += marklen;
120 guest_interp = "4th";
123 * The guest interpreter may not have a version of loader that
124 * specifies the interpreter installed. If that's the case, we'll
125 * assume it's legacy (4th) and request a swap to that if we're
128 if (strcmp(my_interp, guest_interp) != 0)
129 CALLBACK(swap_interpreter, guest_interp);
137 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
139 static char mallocbuf[MALLOCSZ];
143 if (version < USERBOOT_VERSION)
148 userboot_disk_maxunit = ndisks;
151 * initialise the heap as early as possible. Once this is done,
154 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
157 * Hook up the console
161 printf("\n%s", bootprog_info);
163 printf("Memory: %ld k\n", memsize() / 1024);
166 setenv("LINES", "24", 1); /* optional */
169 * Set custom environment variables
173 var = CALLBACK(getenv, i++);
179 archsw.arch_autoload = userboot_autoload;
180 archsw.arch_getdev = userboot_getdev;
181 archsw.arch_copyin = userboot_copyin;
182 archsw.arch_copyout = userboot_copyout;
183 archsw.arch_readin = userboot_readin;
184 #if defined(USERBOOT_ZFS_SUPPORT)
185 archsw.arch_zfs_probe = userboot_zfs_probe;
189 * Initialise the block cache. Set the upper limit.
191 bcache_init(32768, 512);
193 * March through the device switch probing for things.
195 for (i = 0; devsw[i] != NULL; i++)
196 if (devsw[i]->dv_init != NULL)
197 (devsw[i]->dv_init)();
202 * Checking the interpreter isn't worth the overhead unless we
203 * actually have the swap_interpreter callback, so we actually version
204 * check here rather than later on.
206 if (version >= USERBOOT_VERSION_5)
212 interact(); /* doesn't return */
218 * Set the 'current device' by (if possible) recovering the boot device as
219 * supplied by the initial bootstrap.
222 extract_currdev(void)
224 struct disk_devdesc dev;
226 #if defined(USERBOOT_ZFS_SUPPORT)
227 struct zfs_devdesc zdev;
229 if (userboot_zfs_found) {
231 /* Leave the pool/root guid's unassigned */
232 bzero(&zdev, sizeof(zdev));
233 zdev.dd.d_dev = &zfs_dev;
235 init_zfs_bootenv(zfs_fmtdev(&zdev));
240 if (userboot_disk_maxunit > 0) {
241 dev.dd.d_dev = &userboot_disk;
243 dev.d_slice = D_SLICEWILD;
244 dev.d_partition = D_PARTWILD;
246 * If we cannot auto-detect the partition type then
247 * access the disk as a raw device.
249 if (dev.dd.d_dev->dv_open(NULL, &dev)) {
250 dev.d_slice = D_SLICENONE;
251 dev.d_partition = D_PARTNONE;
255 dev.dd.d_dev = &host_dev;
260 env_setenv("currdev", EV_VOLATILE, userboot_fmtdev(dd),
261 userboot_setcurrdev, env_nounset);
262 env_setenv("loaddev", EV_VOLATILE, userboot_fmtdev(dd),
263 env_noset, env_nounset);
266 #if defined(USERBOOT_ZFS_SUPPORT)
268 userboot_zfs_probe(void)
275 * Open all the disks we can find and see if we can reconstruct
276 * ZFS pools from them. Record if any were found.
278 for (unit = 0; unit < userboot_disk_maxunit; unit++) {
279 sprintf(devname, "disk%d:", unit);
281 zfs_probe_dev(devname, &pool_guid);
283 userboot_zfs_found = 1;
288 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
291 command_quit(int argc, char *argv[])
294 exit(USERBOOT_EXIT_QUIT);
298 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
301 command_reboot(int argc, char *argv[])
304 exit(USERBOOT_EXIT_REBOOT);