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)
41 #include "../zfs/libzfs.h"
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 MALLOCSZ (64*1024*1024)
52 struct loader_callbacks *callbacks;
55 extern char bootprog_info[];
58 struct arch_switch archsw; /* MI/MD interface boundary */
60 static void extract_currdev(void);
66 CALLBACK(delay, usec);
78 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
80 static char mallocbuf[MALLOCSZ];
84 if (version < USERBOOT_VERSION)
89 userboot_disk_maxunit = ndisks;
92 * initialise the heap as early as possible. Once this is done,
95 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
102 printf("\n%s", bootprog_info);
104 printf("Memory: %ld k\n", memsize() / 1024);
107 setenv("LINES", "24", 1); /* optional */
110 * Set custom environment variables
114 var = CALLBACK(getenv, i++);
120 archsw.arch_autoload = userboot_autoload;
121 archsw.arch_getdev = userboot_getdev;
122 archsw.arch_copyin = userboot_copyin;
123 archsw.arch_copyout = userboot_copyout;
124 archsw.arch_readin = userboot_readin;
125 #if defined(USERBOOT_ZFS_SUPPORT)
126 archsw.arch_zfs_probe = userboot_zfs_probe;
130 * Initialise the block cache. Set the upper limit.
132 bcache_init(32768, 512);
134 * March through the device switch probing for things.
136 for (i = 0; devsw[i] != NULL; i++)
137 if (devsw[i]->dv_init != NULL)
138 (devsw[i]->dv_init)();
145 interact(NULL); /* doesn't return */
151 * Set the 'current device' by (if possible) recovering the boot device as
152 * supplied by the initial bootstrap.
155 extract_currdev(void)
157 struct disk_devdesc dev;
159 //bzero(&dev, sizeof(dev));
161 #if defined(USERBOOT_ZFS_SUPPORT)
162 if (userboot_zfs_found) {
163 struct zfs_devdesc zdev;
165 /* Leave the pool/root guid's unassigned */
166 bzero(&zdev, sizeof(zdev));
167 zdev.d_dev = &zfs_dev;
168 zdev.d_type = zdev.d_dev->dv_type;
170 dev = *(struct disk_devdesc *)&zdev;
171 init_zfs_bootenv(zfs_fmtdev(&dev));
175 if (userboot_disk_maxunit > 0) {
176 dev.d_dev = &userboot_disk;
177 dev.d_type = dev.d_dev->dv_type;
182 * If we cannot auto-detect the partition type then
183 * access the disk as a raw device.
185 if (dev.d_dev->dv_open(NULL, &dev)) {
187 dev.d_partition = -1;
190 dev.d_dev = &host_dev;
191 dev.d_type = dev.d_dev->dv_type;
195 env_setenv("currdev", EV_VOLATILE, userboot_fmtdev(&dev),
196 userboot_setcurrdev, env_nounset);
197 env_setenv("loaddev", EV_VOLATILE, userboot_fmtdev(&dev),
198 env_noset, env_nounset);
201 #if defined(USERBOOT_ZFS_SUPPORT)
203 userboot_zfs_probe(void)
210 * Open all the disks we can find and see if we can reconstruct
211 * ZFS pools from them. Record if any were found.
213 for (unit = 0; unit < userboot_disk_maxunit; unit++) {
214 sprintf(devname, "disk%d:", unit);
216 zfs_probe_dev(devname, &pool_guid);
218 userboot_zfs_found = 1;
222 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
226 command_lszfs(int argc, char *argv[])
231 command_errmsg = "a single dataset must be supplied";
235 err = zfs_list(argv[1]);
237 command_errmsg = strerror(err);
243 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
247 command_reloadbe(int argc, char *argv[])
253 command_errmsg = "wrong number of arguments";
258 err = zfs_bootenv(argv[1]);
260 root = getenv("zfs_be_root");
264 err = zfs_bootenv(root);
268 command_errmsg = strerror(err);
276 ldi_get_size(void *priv)
278 int fd = (uintptr_t) priv;
281 ioctl(fd, DIOCGMEDIASIZE, &size);
284 #endif /* USERBOOT_ZFS_SUPPORT */
286 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
289 command_quit(int argc, char *argv[])
292 exit(USERBOOT_EXIT_QUIT);
296 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
299 command_reboot(int argc, char *argv[])
302 exit(USERBOOT_EXIT_REBOOT);