1998-08-21 03:17:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-08-25 23:30:41 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/*
|
|
|
|
* Loading modules, booting the system
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stand.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "bootstrap.h"
|
|
|
|
|
|
|
|
static char *getbootfile(int try);
|
2000-09-08 16:47:05 +00:00
|
|
|
static int loadakernel(int try, int argc, char* argv[]);
|
1998-08-21 03:17:42 +00:00
|
|
|
|
|
|
|
/* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
|
2000-11-17 04:43:56 +00:00
|
|
|
static const char *default_bootfiles = "kernel";
|
1998-08-21 03:17:42 +00:00
|
|
|
|
1998-10-07 02:38:26 +00:00
|
|
|
static int autoboot_tried;
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/*
|
|
|
|
* The user wants us to boot.
|
|
|
|
*/
|
|
|
|
COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_boot(int argc, char *argv[])
|
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/*
|
|
|
|
* See if the user has specified an explicit kernel to boot.
|
|
|
|
*/
|
|
|
|
if ((argc > 1) && (argv[1][0] != '-')) {
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/* XXX maybe we should discard everything and start again? */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (file_findfile(NULL, NULL) != NULL) {
|
1999-05-30 10:45:03 +00:00
|
|
|
sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]);
|
1998-08-21 03:17:42 +00:00
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/* find/load the kernel module */
|
2001-09-11 01:09:24 +00:00
|
|
|
if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
|
1998-08-21 03:17:42 +00:00
|
|
|
return(CMD_ERROR);
|
|
|
|
/* we have consumed all arguments */
|
|
|
|
argc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if there is a kernel module already loaded
|
|
|
|
*/
|
2000-09-08 16:47:05 +00:00
|
|
|
if (file_findfile(NULL, NULL) == NULL)
|
|
|
|
if (loadakernel(0, argc - 1, argv + 1))
|
|
|
|
/* we have consumed all arguments */
|
|
|
|
argc = 1;
|
1998-08-21 03:17:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loaded anything yet?
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
if ((fp = file_findfile(NULL, NULL)) == NULL) {
|
1998-08-21 03:17:42 +00:00
|
|
|
command_errmsg = "no bootable kernel";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we were given arguments, discard any previous.
|
|
|
|
* XXX should we merge arguments? Hard to DWIM.
|
|
|
|
*/
|
|
|
|
if (argc > 1) {
|
2005-05-19 05:15:08 +00:00
|
|
|
if (fp->f_args != NULL)
|
2000-05-01 17:41:25 +00:00
|
|
|
free(fp->f_args);
|
|
|
|
fp->f_args = unargv(argc - 1, argv + 1);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Hook for platform-specific autoloading of modules */
|
|
|
|
if (archsw.arch_autoload() != 0)
|
|
|
|
return(CMD_ERROR);
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
/* Call the exec handler from the loader matching the kernel */
|
2000-05-01 17:41:25 +00:00
|
|
|
file_formats[fp->f_loader]->l_exec(fp);
|
1998-08-21 03:17:42 +00:00
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Autoboot after a delay
|
|
|
|
*/
|
|
|
|
|
|
|
|
COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_autoboot(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int howlong;
|
|
|
|
char *cp, *prompt;
|
|
|
|
|
|
|
|
prompt = NULL;
|
|
|
|
howlong = -1;
|
|
|
|
switch(argc) {
|
|
|
|
case 3:
|
|
|
|
prompt = argv[2];
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2:
|
|
|
|
howlong = strtol(argv[1], &cp, 0);
|
|
|
|
if (*cp != 0) {
|
|
|
|
sprintf(command_errbuf, "bad delay '%s'", argv[1]);
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 1:
|
|
|
|
return(autoboot(howlong, prompt));
|
|
|
|
}
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
command_errmsg = "too many arguments";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
1998-10-07 02:38:26 +00:00
|
|
|
/*
|
|
|
|
* Called before we go interactive. If we think we can autoboot, and
|
|
|
|
* we haven't tried already, try now.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
autoboot_maybe()
|
|
|
|
{
|
|
|
|
char *cp;
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-10-07 02:38:26 +00:00
|
|
|
cp = getenv("autoboot_delay");
|
|
|
|
if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
|
|
|
|
autoboot(-1, NULL); /* try to boot automatically */
|
|
|
|
}
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
int
|
2000-08-03 09:14:02 +00:00
|
|
|
autoboot(int timeout, char *prompt)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
|
|
|
time_t when, otime, ntime;
|
1999-05-28 08:01:52 +00:00
|
|
|
int c, yes;
|
1998-10-07 02:38:26 +00:00
|
|
|
char *argv[2], *cp, *ep;
|
2000-09-08 16:47:05 +00:00
|
|
|
char *kernelname;
|
2009-11-12 01:30:17 +00:00
|
|
|
#ifdef BOOT_PROMPT_123
|
|
|
|
const char *seq = "123", *p = seq;
|
|
|
|
#endif
|
1998-08-21 03:17:42 +00:00
|
|
|
|
1998-10-07 02:38:26 +00:00
|
|
|
autoboot_tried = 1;
|
|
|
|
|
2000-08-03 09:14:02 +00:00
|
|
|
if (timeout == -1) {
|
2005-05-19 23:03:02 +00:00
|
|
|
timeout = 10;
|
1998-10-07 02:38:26 +00:00
|
|
|
/* try to get a delay from the environment */
|
|
|
|
if ((cp = getenv("autoboot_delay"))) {
|
2000-08-03 09:14:02 +00:00
|
|
|
timeout = strtol(cp, &ep, 0);
|
1998-10-07 02:38:26 +00:00
|
|
|
if (cp == ep)
|
2005-05-19 23:03:02 +00:00
|
|
|
timeout = 10; /* Unparseable? Set default! */
|
1998-10-07 02:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2000-09-08 16:47:05 +00:00
|
|
|
kernelname = getenv("kernelname");
|
|
|
|
if (kernelname == NULL) {
|
|
|
|
argv[0] = NULL;
|
|
|
|
loadakernel(0, 0, argv);
|
|
|
|
kernelname = getenv("kernelname");
|
|
|
|
if (kernelname == NULL) {
|
|
|
|
command_errmsg = "no valid kernel found";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-19 23:03:02 +00:00
|
|
|
if (timeout >= 0) {
|
|
|
|
otime = time(NULL);
|
|
|
|
when = otime + timeout; /* when to boot */
|
|
|
|
|
|
|
|
yes = 0;
|
|
|
|
|
2009-11-12 01:30:17 +00:00
|
|
|
#ifdef BOOT_PROMPT_123
|
|
|
|
printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
|
|
|
|
"1 2 3 sequence for command prompt." : prompt);
|
|
|
|
#else
|
2005-05-19 23:03:02 +00:00
|
|
|
printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
|
2009-11-12 01:30:17 +00:00
|
|
|
#endif
|
2005-05-19 23:03:02 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (ischar()) {
|
|
|
|
c = getchar();
|
2009-11-12 01:30:17 +00:00
|
|
|
#ifdef BOOT_PROMPT_123
|
|
|
|
if ((c == '\r') || (c == '\n')) {
|
|
|
|
yes = 1;
|
|
|
|
break;
|
|
|
|
} else if (c != *p++)
|
|
|
|
p = seq;
|
|
|
|
if (*p == 0)
|
|
|
|
break;
|
|
|
|
#else
|
2005-05-19 23:03:02 +00:00
|
|
|
if ((c == '\r') || (c == '\n'))
|
|
|
|
yes = 1;
|
|
|
|
break;
|
2009-11-12 01:30:17 +00:00
|
|
|
#endif
|
2005-05-19 23:03:02 +00:00
|
|
|
}
|
|
|
|
ntime = time(NULL);
|
|
|
|
if (ntime >= when) {
|
|
|
|
yes = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ntime != otime) {
|
|
|
|
printf("\rBooting [%s] in %d second%s... ",
|
|
|
|
kernelname, (int)(when - ntime),
|
|
|
|
(when-ntime)==1?"":"s");
|
|
|
|
otime = ntime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yes = 1;
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
2005-05-19 23:03:02 +00:00
|
|
|
|
1998-10-31 02:53:12 +00:00
|
|
|
if (yes)
|
2000-09-08 16:47:05 +00:00
|
|
|
printf("\rBooting [%s]... ", kernelname);
|
1999-05-28 08:01:52 +00:00
|
|
|
putchar('\n');
|
1998-08-21 03:17:42 +00:00
|
|
|
if (yes) {
|
|
|
|
argv[0] = "boot";
|
|
|
|
argv[1] = NULL;
|
|
|
|
return(command_boot(1, argv));
|
|
|
|
}
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scrounge for the name of the (try)'th file we will try to boot.
|
|
|
|
*/
|
|
|
|
static char *
|
2005-05-19 05:15:08 +00:00
|
|
|
getbootfile(int try)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
|
|
|
static char *name = NULL;
|
2000-09-08 16:47:05 +00:00
|
|
|
const char *spec, *ep;
|
2000-08-03 09:14:02 +00:00
|
|
|
size_t len;
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/* we use dynamic storage */
|
|
|
|
if (name != NULL) {
|
|
|
|
free(name);
|
|
|
|
name = NULL;
|
|
|
|
}
|
2005-05-19 05:15:08 +00:00
|
|
|
|
|
|
|
/*
|
1998-08-21 03:17:42 +00:00
|
|
|
* Try $bootfile, then try our builtin default
|
|
|
|
*/
|
|
|
|
if ((spec = getenv("bootfile")) == NULL)
|
|
|
|
spec = default_bootfiles;
|
|
|
|
|
1998-10-02 16:22:26 +00:00
|
|
|
while ((try > 0) && (spec != NULL)) {
|
1998-11-02 16:55:57 +00:00
|
|
|
spec = strchr(spec, ';');
|
2000-05-13 02:45:11 +00:00
|
|
|
if (spec)
|
|
|
|
spec++; /* skip over the leading ';' */
|
1998-10-02 16:22:26 +00:00
|
|
|
try--;
|
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
if (spec != NULL) {
|
1998-11-02 16:55:57 +00:00
|
|
|
if ((ep = strchr(spec, ';')) != NULL) {
|
1998-08-21 03:17:42 +00:00
|
|
|
len = ep - spec;
|
|
|
|
} else {
|
|
|
|
len = strlen(spec);
|
|
|
|
}
|
|
|
|
name = malloc(len + 1);
|
|
|
|
strncpy(name, spec, len);
|
|
|
|
name[len] = 0;
|
|
|
|
}
|
2000-10-25 23:28:19 +00:00
|
|
|
if (name && name[0] == 0) {
|
1998-10-02 16:22:26 +00:00
|
|
|
free(name);
|
|
|
|
name = NULL;
|
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
return(name);
|
|
|
|
}
|
|
|
|
|
1999-07-21 00:08:54 +00:00
|
|
|
/*
|
|
|
|
* Try to find the /etc/fstab file on the filesystem (rootdev),
|
2005-05-19 05:15:08 +00:00
|
|
|
* which should be be the root filesystem, and parse it to find
|
1999-07-21 00:08:54 +00:00
|
|
|
* out what the kernel ought to think the root filesystem is.
|
|
|
|
*
|
|
|
|
* If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
|
|
|
|
* so that the kernel can tell both which VFS and which node to use
|
|
|
|
* to mount the device. If this variable's already set, don't
|
|
|
|
* overwrite it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getrootmount(char *rootdev)
|
|
|
|
{
|
2009-06-01 01:02:30 +00:00
|
|
|
char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
|
1999-07-21 00:08:54 +00:00
|
|
|
int fd, error;
|
|
|
|
|
|
|
|
if (getenv("vfs.root.mountfrom") != NULL)
|
|
|
|
return(0);
|
|
|
|
|
2012-05-12 09:07:41 +00:00
|
|
|
error = 1;
|
1999-07-21 00:08:54 +00:00
|
|
|
sprintf(lbuf, "%s/etc/fstab", rootdev);
|
|
|
|
if ((fd = open(lbuf, O_RDONLY)) < 0)
|
2012-05-12 09:07:41 +00:00
|
|
|
goto notfound;
|
1999-07-21 00:08:54 +00:00
|
|
|
|
|
|
|
/* loop reading lines from /etc/fstab What was that about sscanf again? */
|
|
|
|
while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
|
|
|
|
if ((lbuf[0] == 0) || (lbuf[0] == '#'))
|
|
|
|
continue;
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1999-07-21 00:08:54 +00:00
|
|
|
/* skip device name */
|
|
|
|
for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
|
|
|
|
;
|
|
|
|
if (*cp == 0) /* misformatted */
|
|
|
|
continue;
|
|
|
|
/* delimit and save */
|
|
|
|
*cp++ = 0;
|
|
|
|
dev = strdup(lbuf);
|
2005-05-19 05:15:08 +00:00
|
|
|
|
1999-07-21 00:08:54 +00:00
|
|
|
/* skip whitespace up to mountpoint */
|
|
|
|
while ((*cp != 0) && isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
/* must have /<space> to be root */
|
|
|
|
if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
|
|
|
|
continue;
|
|
|
|
/* skip whitespace up to fstype */
|
|
|
|
cp += 2;
|
|
|
|
while ((*cp != 0) && isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == 0) /* misformatted */
|
|
|
|
continue;
|
|
|
|
/* skip text to end of fstype and delimit */
|
|
|
|
ep = cp;
|
|
|
|
while ((*cp != 0) && !isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
*cp = 0;
|
|
|
|
fstyp = strdup(ep);
|
|
|
|
|
2009-06-01 01:02:30 +00:00
|
|
|
/* skip whitespace up to mount options */
|
|
|
|
cp += 1;
|
|
|
|
while ((*cp != 0) && isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == 0) /* misformatted */
|
|
|
|
continue;
|
|
|
|
/* skip text to end of mount options and delimit */
|
|
|
|
ep = cp;
|
|
|
|
while ((*cp != 0) && !isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
*cp = 0;
|
|
|
|
options = strdup(ep);
|
|
|
|
/* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
|
1999-07-21 00:08:54 +00:00
|
|
|
sprintf(lbuf, "%s:%s", fstyp, dev);
|
|
|
|
free(dev);
|
|
|
|
free(fstyp);
|
|
|
|
setenv("vfs.root.mountfrom", lbuf, 0);
|
2009-06-01 01:02:30 +00:00
|
|
|
|
|
|
|
/* Don't override vfs.root.mountfrom.options if it is already set */
|
|
|
|
if (getenv("vfs.root.mountfrom.options") == NULL) {
|
|
|
|
/* save mount options */
|
|
|
|
setenv("vfs.root.mountfrom.options", options, 0);
|
|
|
|
}
|
|
|
|
free(options);
|
1999-07-21 00:08:54 +00:00
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
2012-05-12 09:07:41 +00:00
|
|
|
|
|
|
|
notfound:
|
|
|
|
if (error) {
|
|
|
|
const char *currdev;
|
|
|
|
|
|
|
|
currdev = getenv("currdev");
|
|
|
|
if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
|
|
|
|
cp = strdup(currdev);
|
|
|
|
cp[strlen(cp) - 1] = '\0';
|
|
|
|
setenv("vfs.root.mountfrom", cp, 0);
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-21 00:08:54 +00:00
|
|
|
return(error);
|
|
|
|
}
|
2000-09-08 16:47:05 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
loadakernel(int try, int argc, char* argv[])
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
for (try = 0; (cp = getbootfile(try)) != NULL; try++)
|
2001-09-11 01:09:24 +00:00
|
|
|
if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
|
2000-09-08 16:47:05 +00:00
|
|
|
printf("can't load '%s'\n", cp);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|