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