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 *
| 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 * $FreeBSD: head/sys/boot/common/boot.c 61659 2000-06-14 10:34:29Z ps $
| 26 * $FreeBSD: head/sys/boot/common/boot.c 64187 2000-08-03 09:14:02Z jhb $
|
27 */ 28 29/* 30 * Loading modules, booting the system 31 */ 32 33#include <stand.h> 34#include <string.h> 35 36#include "bootstrap.h" 37 38static char *getbootfile(int try); 39 40/* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
| 27 */ 28 29/* 30 * Loading modules, booting the system 31 */ 32 33#include <stand.h> 34#include <string.h> 35 36#include "bootstrap.h" 37 38static char *getbootfile(int try); 39 40/* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
|
41static char *default_bootfiles = "kernel;kernel.old";
| 41static const char *default_bootfiles = "kernel;kernel.old";
|
42 43static int autoboot_tried; 44 45/* 46 * The user wants us to boot. 47 */ 48COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot); 49 50static int 51command_boot(int argc, char *argv[]) 52{ 53 struct preloaded_file *fp; 54 char *cp; 55 int try;
| 42 43static int autoboot_tried; 44 45/* 46 * The user wants us to boot. 47 */ 48COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot); 49 50static int 51command_boot(int argc, char *argv[]) 52{ 53 struct preloaded_file *fp; 54 char *cp; 55 int try;
|
56 int i;
| |
57 58 /* 59 * See if the user has specified an explicit kernel to boot. 60 */ 61 if ((argc > 1) && (argv[1][0] != '-')) { 62 63 /* XXX maybe we should discard everything and start again? */ 64 if (file_findfile(NULL, NULL) != NULL) { 65 sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]); 66 return(CMD_ERROR); 67 } 68 69 /* find/load the kernel module */ 70 if (mod_load(argv[1], argc - 2, argv + 2) != 0) 71 return(CMD_ERROR); 72 /* we have consumed all arguments */ 73 argc = 1; 74 } 75 76 /* 77 * See if there is a kernel module already loaded 78 */ 79 if (file_findfile(NULL, NULL) == NULL) { 80 for (try = 0; (cp = getbootfile(try)) != NULL; try++) { 81 if (mod_load(cp, argc - 1, argv + 1) != 0) { 82 printf("can't load '%s'\n", cp); 83 } else { 84 /* we have consumed all arguments */ 85 argc = 1; 86 break; 87 } 88 } 89 } 90 91 /* 92 * Loaded anything yet? 93 */ 94 if ((fp = file_findfile(NULL, NULL)) == NULL) { 95 command_errmsg = "no bootable kernel"; 96 return(CMD_ERROR); 97 } 98 99 /* 100 * If we were given arguments, discard any previous. 101 * XXX should we merge arguments? Hard to DWIM. 102 */ 103 if (argc > 1) { 104 if (fp->f_args != NULL) 105 free(fp->f_args); 106 fp->f_args = unargv(argc - 1, argv + 1); 107 } 108 109 /* Hook for platform-specific autoloading of modules */ 110 if (archsw.arch_autoload() != 0) 111 return(CMD_ERROR); 112 113 /* Call the exec handler from the loader matching the kernel */ 114 file_formats[fp->f_loader]->l_exec(fp); 115 return(CMD_ERROR); 116} 117 118 119/* 120 * Autoboot after a delay 121 */ 122 123COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot); 124 125static int 126command_autoboot(int argc, char *argv[]) 127{ 128 int howlong; 129 char *cp, *prompt; 130 131 prompt = NULL; 132 howlong = -1; 133 switch(argc) { 134 case 3: 135 prompt = argv[2]; 136 /* FALLTHROUGH */ 137 case 2: 138 howlong = strtol(argv[1], &cp, 0); 139 if (*cp != 0) { 140 sprintf(command_errbuf, "bad delay '%s'", argv[1]); 141 return(CMD_ERROR); 142 } 143 /* FALLTHROUGH */ 144 case 1: 145 return(autoboot(howlong, prompt)); 146 } 147 148 command_errmsg = "too many arguments"; 149 return(CMD_ERROR); 150} 151 152/* 153 * Called before we go interactive. If we think we can autoboot, and 154 * we haven't tried already, try now. 155 */ 156void 157autoboot_maybe() 158{ 159 char *cp; 160 161 cp = getenv("autoboot_delay"); 162 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO"))) 163 autoboot(-1, NULL); /* try to boot automatically */ 164} 165 166int
| 56 57 /* 58 * See if the user has specified an explicit kernel to boot. 59 */ 60 if ((argc > 1) && (argv[1][0] != '-')) { 61 62 /* XXX maybe we should discard everything and start again? */ 63 if (file_findfile(NULL, NULL) != NULL) { 64 sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]); 65 return(CMD_ERROR); 66 } 67 68 /* find/load the kernel module */ 69 if (mod_load(argv[1], argc - 2, argv + 2) != 0) 70 return(CMD_ERROR); 71 /* we have consumed all arguments */ 72 argc = 1; 73 } 74 75 /* 76 * See if there is a kernel module already loaded 77 */ 78 if (file_findfile(NULL, NULL) == NULL) { 79 for (try = 0; (cp = getbootfile(try)) != NULL; try++) { 80 if (mod_load(cp, argc - 1, argv + 1) != 0) { 81 printf("can't load '%s'\n", cp); 82 } else { 83 /* we have consumed all arguments */ 84 argc = 1; 85 break; 86 } 87 } 88 } 89 90 /* 91 * Loaded anything yet? 92 */ 93 if ((fp = file_findfile(NULL, NULL)) == NULL) { 94 command_errmsg = "no bootable kernel"; 95 return(CMD_ERROR); 96 } 97 98 /* 99 * If we were given arguments, discard any previous. 100 * XXX should we merge arguments? Hard to DWIM. 101 */ 102 if (argc > 1) { 103 if (fp->f_args != NULL) 104 free(fp->f_args); 105 fp->f_args = unargv(argc - 1, argv + 1); 106 } 107 108 /* Hook for platform-specific autoloading of modules */ 109 if (archsw.arch_autoload() != 0) 110 return(CMD_ERROR); 111 112 /* Call the exec handler from the loader matching the kernel */ 113 file_formats[fp->f_loader]->l_exec(fp); 114 return(CMD_ERROR); 115} 116 117 118/* 119 * Autoboot after a delay 120 */ 121 122COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot); 123 124static int 125command_autoboot(int argc, char *argv[]) 126{ 127 int howlong; 128 char *cp, *prompt; 129 130 prompt = NULL; 131 howlong = -1; 132 switch(argc) { 133 case 3: 134 prompt = argv[2]; 135 /* FALLTHROUGH */ 136 case 2: 137 howlong = strtol(argv[1], &cp, 0); 138 if (*cp != 0) { 139 sprintf(command_errbuf, "bad delay '%s'", argv[1]); 140 return(CMD_ERROR); 141 } 142 /* FALLTHROUGH */ 143 case 1: 144 return(autoboot(howlong, prompt)); 145 } 146 147 command_errmsg = "too many arguments"; 148 return(CMD_ERROR); 149} 150 151/* 152 * Called before we go interactive. If we think we can autoboot, and 153 * we haven't tried already, try now. 154 */ 155void 156autoboot_maybe() 157{ 158 char *cp; 159 160 cp = getenv("autoboot_delay"); 161 if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO"))) 162 autoboot(-1, NULL); /* try to boot automatically */ 163} 164 165int
|
167autoboot(int delay, char *prompt)
| 166autoboot(int timeout, char *prompt)
|
168{ 169 time_t when, otime, ntime; 170 int c, yes; 171 char *argv[2], *cp, *ep; 172 173 autoboot_tried = 1; 174
| 167{ 168 time_t when, otime, ntime; 169 int c, yes; 170 char *argv[2], *cp, *ep; 171 172 autoboot_tried = 1; 173
|
175 if (delay == -1) {
| 174 if (timeout == -1) {
|
176 /* try to get a delay from the environment */ 177 if ((cp = getenv("autoboot_delay"))) {
| 175 /* try to get a delay from the environment */ 176 if ((cp = getenv("autoboot_delay"))) {
|
178 delay = strtol(cp, &ep, 0);
| 177 timeout = strtol(cp, &ep, 0);
|
179 if (cp == ep)
| 178 if (cp == ep)
|
180 delay = -1;
| 179 timeout = -1;
|
181 } 182 }
| 180 } 181 }
|
183 if (delay == -1) /* all else fails */ 184 delay = 10;
| 182 if (timeout == -1) /* all else fails */ 183 timeout = 10;
|
185 186 otime = time(NULL);
| 184 185 otime = time(NULL);
|
187 when = otime + delay; /* when to boot */
| 186 when = otime + timeout; /* when to boot */
|
188 yes = 0; 189 190 /* XXX could try to work out what we might boot */ 191 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt); 192 193 for (;;) { 194 if (ischar()) { 195 c = getchar(); 196 if ((c == '\r') || (c == '\n')) 197 yes = 1; 198 break; 199 } 200 ntime = time(NULL); 201 if (ntime >= when) { 202 yes = 1; 203 break; 204 } 205 if (ntime != otime) { 206 printf("\rBooting [%s] in %d second%s... ", 207 getbootfile(0), (int)(when - ntime), 208 (when-ntime)==1?"":"s"); 209 otime = ntime; 210 } 211 } 212 if (yes) 213 printf("\rBooting [%s]... ", getbootfile(0)); 214 putchar('\n'); 215 if (yes) { 216 argv[0] = "boot"; 217 argv[1] = NULL; 218 return(command_boot(1, argv)); 219 } 220 return(CMD_OK); 221} 222 223/* 224 * Scrounge for the name of the (try)'th file we will try to boot. 225 */ 226static char * 227getbootfile(int try) 228{ 229 static char *name = NULL; 230 char *spec, *ep;
| 187 yes = 0; 188 189 /* XXX could try to work out what we might boot */ 190 printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt); 191 192 for (;;) { 193 if (ischar()) { 194 c = getchar(); 195 if ((c == '\r') || (c == '\n')) 196 yes = 1; 197 break; 198 } 199 ntime = time(NULL); 200 if (ntime >= when) { 201 yes = 1; 202 break; 203 } 204 if (ntime != otime) { 205 printf("\rBooting [%s] in %d second%s... ", 206 getbootfile(0), (int)(when - ntime), 207 (when-ntime)==1?"":"s"); 208 otime = ntime; 209 } 210 } 211 if (yes) 212 printf("\rBooting [%s]... ", getbootfile(0)); 213 putchar('\n'); 214 if (yes) { 215 argv[0] = "boot"; 216 argv[1] = NULL; 217 return(command_boot(1, argv)); 218 } 219 return(CMD_OK); 220} 221 222/* 223 * Scrounge for the name of the (try)'th file we will try to boot. 224 */ 225static char * 226getbootfile(int try) 227{ 228 static char *name = NULL; 229 char *spec, *ep;
|
231 int len;
| 230 size_t len;
|
232 233 /* we use dynamic storage */ 234 if (name != NULL) { 235 free(name); 236 name = NULL; 237 } 238 239 /* 240 * Try $bootfile, then try our builtin default 241 */ 242 if ((spec = getenv("bootfile")) == NULL) 243 spec = default_bootfiles; 244 245 while ((try > 0) && (spec != NULL)) { 246 spec = strchr(spec, ';'); 247 if (spec) 248 spec++; /* skip over the leading ';' */ 249 try--; 250 } 251 if (spec != NULL) { 252 if ((ep = strchr(spec, ';')) != NULL) { 253 len = ep - spec; 254 } else { 255 len = strlen(spec); 256 } 257 name = malloc(len + 1); 258 strncpy(name, spec, len); 259 name[len] = 0; 260 } 261 if (name[0] == 0) { 262 free(name); 263 name = NULL; 264 } 265 return(name); 266} 267 268/* 269 * Try to find the /etc/fstab file on the filesystem (rootdev), 270 * which should be be the root filesystem, and parse it to find 271 * out what the kernel ought to think the root filesystem is. 272 * 273 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path> 274 * so that the kernel can tell both which VFS and which node to use 275 * to mount the device. If this variable's already set, don't 276 * overwrite it. 277 */ 278int 279getrootmount(char *rootdev) 280{ 281 char lbuf[128], *cp, *ep, *dev, *fstyp; 282 int fd, error; 283 284 if (getenv("vfs.root.mountfrom") != NULL) 285 return(0); 286 287 sprintf(lbuf, "%s/etc/fstab", rootdev); 288 if ((fd = open(lbuf, O_RDONLY)) < 0) 289 return(1); 290 291 /* loop reading lines from /etc/fstab What was that about sscanf again? */ 292 error = 1; 293 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) { 294 if ((lbuf[0] == 0) || (lbuf[0] == '#')) 295 continue; 296 297 /* skip device name */ 298 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++) 299 ; 300 if (*cp == 0) /* misformatted */ 301 continue; 302 /* delimit and save */ 303 *cp++ = 0; 304 dev = strdup(lbuf); 305 306 /* skip whitespace up to mountpoint */ 307 while ((*cp != 0) && isspace(*cp)) 308 cp++; 309 /* must have /<space> to be root */ 310 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1))) 311 continue; 312 /* skip whitespace up to fstype */ 313 cp += 2; 314 while ((*cp != 0) && isspace(*cp)) 315 cp++; 316 if (*cp == 0) /* misformatted */ 317 continue; 318 /* skip text to end of fstype and delimit */ 319 ep = cp; 320 while ((*cp != 0) && !isspace(*cp)) 321 cp++; 322 *cp = 0; 323 fstyp = strdup(ep); 324 325 /* build the final result and save it */ 326 sprintf(lbuf, "%s:%s", fstyp, dev); 327 free(dev); 328 free(fstyp); 329 setenv("vfs.root.mountfrom", lbuf, 0); 330 error = 0; 331 break; 332 } 333 close(fd); 334 return(error); 335}
| 231 232 /* we use dynamic storage */ 233 if (name != NULL) { 234 free(name); 235 name = NULL; 236 } 237 238 /* 239 * Try $bootfile, then try our builtin default 240 */ 241 if ((spec = getenv("bootfile")) == NULL) 242 spec = default_bootfiles; 243 244 while ((try > 0) && (spec != NULL)) { 245 spec = strchr(spec, ';'); 246 if (spec) 247 spec++; /* skip over the leading ';' */ 248 try--; 249 } 250 if (spec != NULL) { 251 if ((ep = strchr(spec, ';')) != NULL) { 252 len = ep - spec; 253 } else { 254 len = strlen(spec); 255 } 256 name = malloc(len + 1); 257 strncpy(name, spec, len); 258 name[len] = 0; 259 } 260 if (name[0] == 0) { 261 free(name); 262 name = NULL; 263 } 264 return(name); 265} 266 267/* 268 * Try to find the /etc/fstab file on the filesystem (rootdev), 269 * which should be be the root filesystem, and parse it to find 270 * out what the kernel ought to think the root filesystem is. 271 * 272 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path> 273 * so that the kernel can tell both which VFS and which node to use 274 * to mount the device. If this variable's already set, don't 275 * overwrite it. 276 */ 277int 278getrootmount(char *rootdev) 279{ 280 char lbuf[128], *cp, *ep, *dev, *fstyp; 281 int fd, error; 282 283 if (getenv("vfs.root.mountfrom") != NULL) 284 return(0); 285 286 sprintf(lbuf, "%s/etc/fstab", rootdev); 287 if ((fd = open(lbuf, O_RDONLY)) < 0) 288 return(1); 289 290 /* loop reading lines from /etc/fstab What was that about sscanf again? */ 291 error = 1; 292 while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) { 293 if ((lbuf[0] == 0) || (lbuf[0] == '#')) 294 continue; 295 296 /* skip device name */ 297 for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++) 298 ; 299 if (*cp == 0) /* misformatted */ 300 continue; 301 /* delimit and save */ 302 *cp++ = 0; 303 dev = strdup(lbuf); 304 305 /* skip whitespace up to mountpoint */ 306 while ((*cp != 0) && isspace(*cp)) 307 cp++; 308 /* must have /<space> to be root */ 309 if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1))) 310 continue; 311 /* skip whitespace up to fstype */ 312 cp += 2; 313 while ((*cp != 0) && isspace(*cp)) 314 cp++; 315 if (*cp == 0) /* misformatted */ 316 continue; 317 /* skip text to end of fstype and delimit */ 318 ep = cp; 319 while ((*cp != 0) && !isspace(*cp)) 320 cp++; 321 *cp = 0; 322 fstyp = strdup(ep); 323 324 /* build the final result and save it */ 325 sprintf(lbuf, "%s:%s", fstyp, dev); 326 free(dev); 327 free(fstyp); 328 setenv("vfs.root.mountfrom", lbuf, 0); 329 error = 0; 330 break; 331 } 332 close(fd); 333 return(error); 334}
|