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 <stand.h> 29 #include <string.h> 30 #include <setjmp.h> 31 #include <sys/disk.h> 32 33 #include "bootstrap.h" 34 #include "disk.h" 35 #include "libuserboot.h" 36 37 #if defined(USERBOOT_ZFS_SUPPORT) 38 #include <sys/zfs_bootenv.h> 39 #include "libzfs.h" 40 41 static void userboot_zfs_probe(void); 42 static int userboot_zfs_found; 43 #endif 44 45 /* Minimum version required */ 46 #define USERBOOT_VERSION USERBOOT_VERSION_3 47 48 #define LOADER_PATH "/boot/loader" 49 #define INTERP_MARKER "$Interpreter:" 50 51 #define MALLOCSZ (64*1024*1024) 52 53 struct loader_callbacks *callbacks; 54 void *callbacks_arg; 55 56 static jmp_buf jb; 57 58 struct arch_switch archsw = { /* MI/MD interface boundary */ 59 .arch_autoload = userboot_autoload, 60 .arch_getdev = userboot_getdev, 61 .arch_copyin = userboot_copyin, 62 .arch_copyout = userboot_copyout, 63 .arch_readin = userboot_readin, 64 #if defined(USERBOOT_ZFS_SUPPORT) 65 .arch_zfs_probe = userboot_zfs_probe, 66 #endif 67 }; 68 69 static void extract_currdev(void); 70 static void check_interpreter(void); 71 72 void 73 delay(int usec) 74 { 75 76 CALLBACK(delay, usec); 77 } 78 79 time_t 80 getsecs(void) 81 { 82 83 /* 84 * userboot can't do netboot, so this implementation isn't strictly 85 * required. Defining it avoids issues with BIND_NOW, and it doesn't 86 * hurt to do it. 87 */ 88 return (time(NULL)); 89 } 90 91 void 92 exit(int v) 93 { 94 95 CALLBACK(exit, v); 96 longjmp(jb, 1); 97 } 98 99 static void 100 check_interpreter(void) 101 { 102 struct stat st; 103 size_t marklen, rdsize; 104 const char *guest_interp, *my_interp; 105 char *buf; 106 int fd; 107 108 /* 109 * If we can't stat(2) or open(2) LOADER_PATH, then we'll fail by 110 * simply letting us roll on with whatever interpreter we were compiled 111 * with. This is likely not going to be an issue in reality. 112 */ 113 buf = NULL; 114 if (stat(LOADER_PATH, &st) != 0) 115 return; 116 if ((fd = open(LOADER_PATH, O_RDONLY)) < 0) 117 return; 118 119 rdsize = st.st_size; 120 buf = malloc(rdsize); 121 if (buf == NULL) 122 goto out; 123 if (read(fd, buf, rdsize) < rdsize) 124 goto out; 125 126 marklen = strlen(INTERP_MARKER); 127 my_interp = bootprog_interp + marklen; 128 129 /* 130 * Here we make the assumption that a loader binary without the 131 * interpreter marker is a 4th one. All loader binaries going forward 132 * should have this properly specified, so our assumption should always 133 * be a good one. 134 */ 135 if ((guest_interp = memmem(buf, rdsize, INTERP_MARKER, 136 marklen)) != NULL) 137 guest_interp += marklen; 138 else 139 guest_interp = "4th"; 140 141 /* 142 * The guest interpreter may not have a version of loader that 143 * specifies the interpreter installed. If that's the case, we'll 144 * assume it's legacy (4th) and request a swap to that if we're 145 * a Lua-userboot. 146 */ 147 if (strcmp(my_interp, guest_interp) != 0) 148 CALLBACK(swap_interpreter, guest_interp); 149 out: 150 free(buf); 151 close(fd); 152 return; 153 } 154 155 void 156 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks) 157 { 158 static char mallocbuf[MALLOCSZ]; 159 char *var; 160 int i; 161 162 if (version < USERBOOT_VERSION) 163 abort(); 164 165 callbacks = cb; 166 callbacks_arg = arg; 167 userboot_disk_maxunit = ndisks; 168 169 /* 170 * initialise the heap as early as possible. Once this is done, 171 * alloc() is usable. 172 */ 173 setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf))); 174 175 /* 176 * Hook up the console 177 */ 178 cons_probe(); 179 180 /* Set up currdev variable to have hooks in place. */ 181 env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset); 182 183 printf("\n%s", bootprog_info); 184 #if 0 185 printf("Memory: %ld k\n", memsize() / 1024); 186 #endif 187 188 setenv("LINES", "24", 1); /* optional */ 189 190 /* 191 * Set custom environment variables 192 */ 193 i = 0; 194 while (1) { 195 var = CALLBACK(getenv, i++); 196 if (var == NULL) 197 break; 198 putenv(var); 199 } 200 201 /* 202 * Initialise the block cache. Set the upper limit. 203 */ 204 bcache_init(32768, 512); 205 devinit(); 206 extract_currdev(); 207 208 #if !defined(USERBOOT_KERNEL_SUPPORT) 209 printf("WARNING: This userboot does not support loading a kernel\n"); 210 delay(1500000); 211 #endif 212 213 /* 214 * Checking the interpreter isn't worth the overhead unless we 215 * actually have the swap_interpreter callback, so we actually version 216 * check here rather than later on. 217 */ 218 if (version >= USERBOOT_VERSION_5) 219 check_interpreter(); 220 221 if (setjmp(jb)) 222 return; 223 224 interact(); /* doesn't return */ 225 226 exit(0); 227 } 228 229 /* 230 * Set the 'current device' by (if possible) recovering the boot device as 231 * supplied by the initial bootstrap. 232 */ 233 static void 234 extract_currdev(void) 235 { 236 struct disk_devdesc dev; 237 struct devdesc *dd; 238 #if defined(USERBOOT_ZFS_SUPPORT) 239 struct zfs_devdesc zdev; 240 241 if (userboot_zfs_found) { 242 243 /* Leave the pool/root guid's unassigned */ 244 bzero(&zdev, sizeof(zdev)); 245 zdev.dd.d_dev = &zfs_dev; 246 247 init_zfs_boot_options(devformat(&zdev.dd)); 248 dd = &zdev.dd; 249 } else 250 #endif 251 252 if (userboot_disk_maxunit > 0) { 253 dev.dd.d_dev = &userboot_disk; 254 dev.dd.d_unit = 0; 255 dev.d_slice = D_SLICEWILD; 256 dev.d_partition = D_PARTWILD; 257 /* 258 * If we cannot auto-detect the partition type then 259 * access the disk as a raw device. 260 */ 261 if (dev.dd.d_dev->dv_open(NULL, &dev)) { 262 dev.d_slice = D_SLICENONE; 263 dev.d_partition = D_PARTNONE; 264 } 265 dd = &dev.dd; 266 } else { 267 dev.dd.d_dev = &host_dev; 268 dev.dd.d_unit = 0; 269 dd = &dev.dd; 270 } 271 272 set_currdev(devformat(dd)); 273 274 #if defined(USERBOOT_ZFS_SUPPORT) 275 if (userboot_zfs_found) { 276 char buf[VDEV_PAD_SIZE]; 277 278 if (zfs_get_bootonce(&zdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) { 279 printf("zfs bootonce: %s\n", buf); 280 set_currdev(buf); 281 setenv("zfs-bootonce", buf, 1); 282 } 283 (void)zfs_attach_nvstore(&zdev); 284 } 285 #endif 286 } 287 288 #if defined(USERBOOT_ZFS_SUPPORT) 289 static void 290 userboot_zfs_probe(void) 291 { 292 char devname[32]; 293 uint64_t pool_guid; 294 int unit; 295 296 /* 297 * Open all the disks we can find and see if we can reconstruct 298 * ZFS pools from them. Record if any were found. 299 */ 300 for (unit = 0; unit < userboot_disk_maxunit; unit++) { 301 sprintf(devname, "disk%d:", unit); 302 pool_guid = 0; 303 zfs_probe_dev(devname, &pool_guid, true); 304 if (pool_guid != 0) 305 userboot_zfs_found = 1; 306 } 307 } 308 #endif 309 310 COMMAND_SET(quit, "quit", "exit the loader", command_quit); 311 312 static int 313 command_quit(int argc, char *argv[]) 314 { 315 316 exit(USERBOOT_EXIT_QUIT); 317 return (CMD_OK); 318 } 319 320 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 321 322 static int 323 command_reboot(int argc, char *argv[]) 324 { 325 326 exit(USERBOOT_EXIT_REBOOT); 327 return (CMD_OK); 328 } 329