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 __FBSDID("$FreeBSD$"); 30 31 #include <stand.h> 32 #include <string.h> 33 #include <setjmp.h> 34 35 #include "bootstrap.h" 36 #include "disk.h" 37 #include "libuserboot.h" 38 39 #if defined(USERBOOT_ZFS_SUPPORT) 40 #include "../zfs/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 MALLOCSZ (10*1024*1024) 50 51 struct loader_callbacks *callbacks; 52 void *callbacks_arg; 53 54 extern char bootprog_info[]; 55 static jmp_buf jb; 56 57 struct arch_switch archsw; /* MI/MD interface boundary */ 58 59 static void extract_currdev(void); 60 61 void 62 delay(int usec) 63 { 64 65 CALLBACK(delay, usec); 66 } 67 68 void 69 exit(int v) 70 { 71 72 CALLBACK(exit, v); 73 longjmp(jb, 1); 74 } 75 76 void 77 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks) 78 { 79 static char mallocbuf[MALLOCSZ]; 80 const char *var; 81 int i; 82 83 if (version < USERBOOT_VERSION) 84 abort(); 85 86 callbacks = cb; 87 callbacks_arg = arg; 88 userboot_disk_maxunit = ndisks; 89 90 /* 91 * initialise the heap as early as possible. Once this is done, 92 * alloc() is usable. 93 */ 94 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf))); 95 96 /* 97 * Hook up the console 98 */ 99 cons_probe(); 100 101 printf("\n%s", bootprog_info); 102 #if 0 103 printf("Memory: %ld k\n", memsize() / 1024); 104 #endif 105 106 setenv("LINES", "24", 1); /* optional */ 107 108 /* 109 * Set custom environment variables 110 */ 111 i = 0; 112 while (1) { 113 var = CALLBACK(getenv, i++); 114 if (var == NULL) 115 break; 116 putenv(var); 117 } 118 119 archsw.arch_autoload = userboot_autoload; 120 archsw.arch_getdev = userboot_getdev; 121 archsw.arch_copyin = userboot_copyin; 122 archsw.arch_copyout = userboot_copyout; 123 archsw.arch_readin = userboot_readin; 124 #if defined(USERBOOT_ZFS_SUPPORT) 125 archsw.arch_zfs_probe = userboot_zfs_probe; 126 #endif 127 128 /* 129 * March through the device switch probing for things. 130 */ 131 for (i = 0; devsw[i] != NULL; i++) 132 if (devsw[i]->dv_init != NULL) 133 (devsw[i]->dv_init)(); 134 135 extract_currdev(); 136 137 if (setjmp(jb)) 138 return; 139 140 interact(NULL); /* doesn't return */ 141 142 exit(0); 143 } 144 145 /* 146 * Set the 'current device' by (if possible) recovering the boot device as 147 * supplied by the initial bootstrap. 148 */ 149 static void 150 extract_currdev(void) 151 { 152 struct disk_devdesc dev; 153 154 //bzero(&dev, sizeof(dev)); 155 156 #if defined(USERBOOT_ZFS_SUPPORT) 157 if (userboot_zfs_found) { 158 struct zfs_devdesc zdev; 159 160 /* Leave the pool/root guid's unassigned */ 161 bzero(&zdev, sizeof(zdev)); 162 zdev.d_dev = &zfs_dev; 163 zdev.d_type = zdev.d_dev->dv_type; 164 165 dev = *(struct disk_devdesc *)&zdev; 166 init_zfs_bootenv(zfs_fmtdev(&dev)); 167 } else 168 #endif 169 170 if (userboot_disk_maxunit > 0) { 171 dev.d_dev = &userboot_disk; 172 dev.d_type = dev.d_dev->dv_type; 173 dev.d_unit = 0; 174 dev.d_slice = 0; 175 dev.d_partition = 0; 176 /* 177 * If we cannot auto-detect the partition type then 178 * access the disk as a raw device. 179 */ 180 if (dev.d_dev->dv_open(NULL, &dev)) { 181 dev.d_slice = -1; 182 dev.d_partition = -1; 183 } 184 } else { 185 dev.d_dev = &host_dev; 186 dev.d_type = dev.d_dev->dv_type; 187 dev.d_unit = 0; 188 } 189 190 env_setenv("currdev", EV_VOLATILE, userboot_fmtdev(&dev), 191 userboot_setcurrdev, env_nounset); 192 env_setenv("loaddev", EV_VOLATILE, userboot_fmtdev(&dev), 193 env_noset, env_nounset); 194 } 195 196 #if defined(USERBOOT_ZFS_SUPPORT) 197 static void 198 userboot_zfs_probe(void) 199 { 200 char devname[32]; 201 uint64_t pool_guid; 202 int unit; 203 204 /* 205 * Open all the disks we can find and see if we can reconstruct 206 * ZFS pools from them. Record if any were found. 207 */ 208 for (unit = 0; unit < userboot_disk_maxunit; unit++) { 209 sprintf(devname, "disk%d:", unit); 210 pool_guid = 0; 211 zfs_probe_dev(devname, &pool_guid); 212 if (pool_guid != 0) 213 userboot_zfs_found = 1; 214 } 215 } 216 217 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset", 218 command_lszfs); 219 220 static int 221 command_lszfs(int argc, char *argv[]) 222 { 223 int err; 224 225 if (argc != 2) { 226 command_errmsg = "a single dataset must be supplied"; 227 return (CMD_ERROR); 228 } 229 230 err = zfs_list(argv[1]); 231 if (err != 0) { 232 command_errmsg = strerror(err); 233 return (CMD_ERROR); 234 } 235 return (CMD_OK); 236 } 237 238 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments", 239 command_reloadbe); 240 241 static int 242 command_reloadbe(int argc, char *argv[]) 243 { 244 int err; 245 char *root; 246 247 if (argc > 2) { 248 command_errmsg = "wrong number of arguments"; 249 return (CMD_ERROR); 250 } 251 252 if (argc == 2) { 253 err = zfs_bootenv(argv[1]); 254 } else { 255 root = getenv("zfs_be_root"); 256 if (root == NULL) { 257 return (CMD_OK); 258 } 259 err = zfs_bootenv(root); 260 } 261 262 if (err != 0) { 263 command_errmsg = strerror(err); 264 return (CMD_ERROR); 265 } 266 267 return (CMD_OK); 268 } 269 #endif /* USERBOOT_ZFS_SUPPORT */ 270 271 COMMAND_SET(quit, "quit", "exit the loader", command_quit); 272 273 static int 274 command_quit(int argc, char *argv[]) 275 { 276 277 exit(USERBOOT_EXIT_QUIT); 278 return (CMD_OK); 279 } 280 281 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 282 283 static int 284 command_reboot(int argc, char *argv[]) 285 { 286 287 exit(USERBOOT_EXIT_REBOOT); 288 return (CMD_OK); 289 } 290