1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Loading modules, booting the system 32 */ 33 34 #include <stand.h> 35 #include <sys/reboot.h> 36 #include <sys/boot.h> 37 #include <string.h> 38 39 #include "bootstrap.h" 40 41 static int autoboot(int timeout, char *prompt); 42 static char *getbootfile(int try); 43 static int loadakernel(int try, int argc, char* argv[]); 44 45 /* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */ 46 static const char *default_bootfiles = "kernel"; 47 48 static int autoboot_tried; 49 50 /* 51 * The user wants us to boot. 52 */ 53 COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot); 54 55 static int 56 command_boot(int argc, char *argv[]) 57 { 58 struct preloaded_file *fp; 59 60 /* 61 * See if the user has specified an explicit kernel to boot. 62 */ 63 if ((argc > 1) && (argv[1][0] != '-')) { 64 65 /* XXX maybe we should discard everything and start again? */ 66 if (file_findfile(NULL, NULL) != NULL) { 67 snprintf(command_errbuf, sizeof(command_errbuf), 68 "can't boot '%s', kernel module already loaded", argv[1]); 69 return(CMD_ERROR); 70 } 71 72 /* find/load the kernel module */ 73 if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0) 74 return(CMD_ERROR); 75 /* we have consumed all arguments */ 76 argc = 1; 77 } 78 79 /* 80 * See if there is a kernel module already loaded 81 */ 82 if (file_findfile(NULL, NULL) == NULL) 83 if (loadakernel(0, argc - 1, argv + 1)) 84 /* we have consumed all arguments */ 85 argc = 1; 86 87 /* 88 * Loaded anything yet? 89 */ 90 if ((fp = file_findfile(NULL, NULL)) == NULL) { 91 command_errmsg = "no bootable kernel"; 92 return(CMD_ERROR); 93 } 94 95 /* 96 * If we were given arguments, discard any previous. 97 * XXX should we merge arguments? Hard to DWIM. 98 */ 99 if (argc > 1) { 100 if (fp->f_args != NULL) 101 free(fp->f_args); 102 fp->f_args = unargv(argc - 1, argv + 1); 103 } 104 105 /* Hook for platform-specific autoloading of modules */ 106 if (archsw.arch_autoload() != 0) 107 return(CMD_ERROR); 108 109 /* Call the exec handler from the loader matching the kernel */ 110 file_formats[fp->f_loader]->l_exec(fp); 111 return(CMD_ERROR); 112 } 113 114 115 /* 116 * Autoboot after a delay 117 */ 118 119 COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot); 120 121 static int 122 command_autoboot(int argc, char *argv[]) 123 { 124 int howlong; 125 char *cp, *prompt; 126 127 prompt = NULL; 128 howlong = -1; 129 switch(argc) { 130 case 3: 131 prompt = argv[2]; 132 /* FALLTHROUGH */ 133 case 2: 134 howlong = strtol(argv[1], &cp, 0); 135 if (*cp != 0) { 136 snprintf(command_errbuf, sizeof(command_errbuf), 137 "bad delay '%s'", argv[1]); 138 return(CMD_ERROR); 139 } 140 /* FALLTHROUGH */ 141 case 1: 142 return(autoboot(howlong, prompt)); 143 } 144 145 command_errmsg = "too many arguments"; 146 return(CMD_ERROR); 147 } 148 149 /* 150 * Called before we go interactive. If we think we can autoboot, and 151 * we haven't tried already, try now. 152 */ 153 void 154 autoboot_maybe() 155 { 156 char *cp; 157 158 cp = getenv("autoboot_delay"); 159 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO"))) 160 autoboot(-1, NULL); /* try to boot automatically */ 161 } 162 163 int 164 bootenv_flags() 165 { 166 int i, howto; 167 char *val; 168 169 for (howto = 0, i = 0; howto_names[i].ev != NULL; i++) { 170 val = getenv(howto_names[i].ev); 171 if (val != NULL && strcasecmp(val, "no") != 0) 172 howto |= howto_names[i].mask; 173 } 174 return (howto); 175 } 176 177 static int 178 autoboot(int timeout, char *prompt) 179 { 180 time_t when, otime, ntime; 181 int c, yes; 182 char *argv[2], *cp, *ep; 183 char *kernelname; 184 #ifdef BOOT_PROMPT_123 185 const char *seq = "123", *p = seq; 186 #endif 187 188 autoboot_tried = 1; 189 190 if (timeout == -1) { 191 timeout = 10; 192 /* try to get a delay from the environment */ 193 if ((cp = getenv("autoboot_delay"))) { 194 timeout = strtol(cp, &ep, 0); 195 if (cp == ep) 196 timeout = 10; /* Unparseable? Set default! */ 197 } 198 } 199 200 kernelname = getenv("kernelname"); 201 if (kernelname == NULL) { 202 argv[0] = NULL; 203 loadakernel(0, 0, argv); 204 kernelname = getenv("kernelname"); 205 if (kernelname == NULL) { 206 command_errmsg = "no valid kernel found"; 207 return(CMD_ERROR); 208 } 209 } 210 211 if (timeout >= 0) { 212 otime = time(NULL); 213 when = otime + timeout; /* when to boot */ 214 215 yes = 0; 216 217 #ifdef BOOT_PROMPT_123 218 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or " 219 "1 2 3 sequence for command prompt." : prompt); 220 #else 221 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt); 222 #endif 223 224 for (;;) { 225 if (ischar()) { 226 c = getchar(); 227 #ifdef BOOT_PROMPT_123 228 if ((c == '\r') || (c == '\n')) { 229 yes = 1; 230 break; 231 } else if (c != *p++) 232 p = seq; 233 if (*p == 0) 234 break; 235 #else 236 if ((c == '\r') || (c == '\n')) 237 yes = 1; 238 break; 239 #endif 240 } 241 ntime = time(NULL); 242 if (ntime >= when) { 243 yes = 1; 244 break; 245 } 246 247 if (ntime != otime) { 248 printf("\rBooting [%s] in %d second%s... ", 249 kernelname, (int)(when - ntime), 250 (when-ntime)==1?"":"s"); 251 otime = ntime; 252 } 253 } 254 } else { 255 yes = 1; 256 } 257 258 if (yes) 259 printf("\rBooting [%s]... ", kernelname); 260 putchar('\n'); 261 if (yes) { 262 argv[0] = "boot"; 263 argv[1] = NULL; 264 return(command_boot(1, argv)); 265 } 266 return(CMD_OK); 267 } 268 269 /* 270 * Scrounge for the name of the (try)'th file we will try to boot. 271 */ 272 static char * 273 getbootfile(int try) 274 { 275 static char *name = NULL; 276 const char *spec, *ep; 277 size_t len; 278 279 /* we use dynamic storage */ 280 if (name != NULL) { 281 free(name); 282 name = NULL; 283 } 284 285 /* 286 * Try $bootfile, then try our builtin default 287 */ 288 if ((spec = getenv("bootfile")) == NULL) 289 spec = default_bootfiles; 290 291 while ((try > 0) && (spec != NULL)) { 292 spec = strchr(spec, ';'); 293 if (spec) 294 spec++; /* skip over the leading ';' */ 295 try--; 296 } 297 if (spec != NULL) { 298 if ((ep = strchr(spec, ';')) != NULL) { 299 len = ep - spec; 300 } else { 301 len = strlen(spec); 302 } 303 name = malloc(len + 1); 304 strncpy(name, spec, len); 305 name[len] = 0; 306 } 307 if (name && name[0] == 0) { 308 free(name); 309 name = NULL; 310 } 311 return(name); 312 } 313 314 /* 315 * Try to find the /etc/fstab file on the filesystem (rootdev), 316 * which should be be the root filesystem, and parse it to find 317 * out what the kernel ought to think the root filesystem is. 318 * 319 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path> 320 * so that the kernel can tell both which VFS and which node to use 321 * to mount the device. If this variable's already set, don't 322 * overwrite it. 323 */ 324 int 325 getrootmount(char *rootdev) 326 { 327 char lbuf[128], *cp, *ep, *dev, *fstyp, *options; 328 int fd, error; 329 330 if (getenv("vfs.root.mountfrom") != NULL) 331 return(0); 332 333 error = 1; 334 sprintf(lbuf, "%s/etc/fstab", rootdev); 335 if ((fd = open(lbuf, O_RDONLY)) < 0) 336 goto notfound; 337 338 /* loop reading lines from /etc/fstab What was that about sscanf again? */ 339 fstyp = NULL; 340 dev = NULL; 341 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) { 342 if ((lbuf[0] == 0) || (lbuf[0] == '#')) 343 continue; 344 345 /* skip device name */ 346 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++) 347 ; 348 if (*cp == 0) /* misformatted */ 349 continue; 350 /* delimit and save */ 351 *cp++ = 0; 352 free(dev); 353 dev = strdup(lbuf); 354 355 /* skip whitespace up to mountpoint */ 356 while ((*cp != 0) && isspace(*cp)) 357 cp++; 358 /* must have /<space> to be root */ 359 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1))) 360 continue; 361 /* skip whitespace up to fstype */ 362 cp += 2; 363 while ((*cp != 0) && isspace(*cp)) 364 cp++; 365 if (*cp == 0) /* misformatted */ 366 continue; 367 /* skip text to end of fstype and delimit */ 368 ep = cp; 369 while ((*cp != 0) && !isspace(*cp)) 370 cp++; 371 *cp = 0; 372 free(fstyp); 373 fstyp = strdup(ep); 374 375 /* skip whitespace up to mount options */ 376 cp += 1; 377 while ((*cp != 0) && isspace(*cp)) 378 cp++; 379 if (*cp == 0) /* misformatted */ 380 continue; 381 /* skip text to end of mount options and delimit */ 382 ep = cp; 383 while ((*cp != 0) && !isspace(*cp)) 384 cp++; 385 *cp = 0; 386 options = strdup(ep); 387 /* Build the <fstype>:<device> and save it in vfs.root.mountfrom */ 388 sprintf(lbuf, "%s:%s", fstyp, dev); 389 setenv("vfs.root.mountfrom", lbuf, 0); 390 391 /* Don't override vfs.root.mountfrom.options if it is already set */ 392 if (getenv("vfs.root.mountfrom.options") == NULL) { 393 /* save mount options */ 394 setenv("vfs.root.mountfrom.options", options, 0); 395 } 396 free(options); 397 error = 0; 398 break; 399 } 400 close(fd); 401 free(dev); 402 free(fstyp); 403 404 notfound: 405 if (error) { 406 const char *currdev; 407 408 currdev = getenv("currdev"); 409 if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) { 410 cp = strdup(currdev); 411 cp[strlen(cp) - 1] = '\0'; 412 setenv("vfs.root.mountfrom", cp, 0); 413 error = 0; 414 free(cp); 415 } 416 } 417 418 return(error); 419 } 420 421 static int 422 loadakernel(int try, int argc, char* argv[]) 423 { 424 char *cp; 425 426 for (try = 0; (cp = getbootfile(try)) != NULL; try++) 427 if (mod_loadkld(cp, argc - 1, argv + 1) != 0) 428 printf("can't load '%s'\n", cp); 429 else 430 return 1; 431 return 0; 432 } 433