#include <sys/cdefs.h>
#include <stand.h>
#include <string.h>
#include "bootstrap.h"
static char *getbootfile(int try);
static int loadakernel(int try, int argc, char *argv[]);
static const char *default_bootfiles = "kernel";
static int autoboot_tried;
COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);
static int
command_boot(int argc, char *argv[])
{
struct preloaded_file *fp;
if ((argc > 1) && (argv[1][0] == '/')) {
if (file_findfile(NULL, NULL) != NULL) {
snprintf(command_errbuf, sizeof (command_errbuf),
"can't boot '%s', kernel module already loaded",
argv[1]);
return (CMD_ERROR);
}
if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
return (CMD_ERROR);
argc = 1;
}
if (file_findfile(NULL, NULL) == NULL)
if (loadakernel(0, argc - 1, argv + 1)) {
argc = 1;
}
if ((fp = file_findfile(NULL, NULL)) == NULL) {
command_errmsg = "no bootable kernel";
return (CMD_ERROR);
}
if (argc > 1) {
free(fp->f_args);
fp->f_args = unargv(argc - 1, argv + 1);
}
if (archsw.arch_autoload() != 0)
return (CMD_ERROR);
file_formats[fp->f_loader]->l_exec(fp);
return (CMD_ERROR);
}
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];
case 2:
howlong = strtol(argv[1], &cp, 0);
if (*cp != 0) {
snprintf(command_errbuf, sizeof (command_errbuf),
"bad delay '%s'", argv[1]);
return (CMD_ERROR);
}
case 1:
return (autoboot(howlong, prompt));
}
command_errmsg = "too many arguments";
return (CMD_ERROR);
}
void
autoboot_maybe(void)
{
char *cp;
cp = getenv("autoboot?");
if (cp != NULL && strcasecmp(cp, "true") != 0)
return;
cp = getenv("autoboot_delay");
if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
autoboot(-1, NULL);
}
int
autoboot(int timeout, char *prompt)
{
time_t when, otime, ntime;
int c, yes;
char *argv[2], *cp, *ep;
char *kernelname;
struct preloaded_file *fp;
autoboot_tried = 1;
if (timeout == -1) {
timeout = 10;
if ((cp = getenv("autoboot_delay"))) {
timeout = strtol(cp, &ep, 0);
if (cp == ep)
timeout = 10;
}
}
fp = file_findfile(NULL, NULL);
if (fp == NULL) {
bf_run("start");
fp = file_findfile(NULL, NULL);
if (fp == NULL) {
command_errmsg = "no valid kernel found";
return (CMD_ERROR);
}
}
kernelname = fp->f_name;
if (timeout >= 0) {
otime = time(NULL);
when = otime + timeout;
yes = 0;
printf("%s\n", (prompt == NULL) ?
"Hit [Enter] to boot immediately, or any other key "
"for command prompt." : prompt);
for (;;) {
if (ischar()) {
c = getchar();
if ((c == '\r') || (c == '\n'))
yes = 1;
break;
}
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;
}
if (yes)
printf("\rBooting [%s]... ", kernelname);
putchar('\n');
if (yes) {
argv[0] = "boot";
argv[1] = NULL;
return (command_boot(1, argv));
}
return (CMD_OK);
}
static char *
getbootfile(int try)
{
static char *name = NULL;
const char *spec, *ep;
size_t len;
free(name);
name = NULL;
if ((spec = getenv("bootfile")) == NULL)
spec = default_bootfiles;
while ((try > 0) && (spec != NULL)) {
spec = strchr(spec, ';');
if (spec)
spec++;
try--;
}
if (spec != NULL) {
if ((ep = strchr(spec, ';')) != NULL) {
len = ep - spec;
} else {
len = strlen(spec);
}
name = malloc(len + 1);
strncpy(name, spec, len);
name[len] = 0;
}
if (name && name[0] == 0) {
free(name);
name = NULL;
}
return (name);
}
int
getrootmount(char *rootdev)
{
char lbuf[128], *cp, *ep, *dev, *fstyp, *options;
int fd, error;
if (getenv("vfs.root.mountfrom") != NULL)
return (0);
error = 1;
sprintf(lbuf, "%s/etc/fstab", rootdev);
if ((fd = open(lbuf, O_RDONLY)) < 0)
goto notfound;
fstyp = NULL;
dev = NULL;
while (fgetstr(lbuf, sizeof (lbuf), fd) >= 0) {
if ((lbuf[0] == 0) || (lbuf[0] == '#'))
continue;
for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
;
if (*cp == 0)
continue;
*cp++ = 0;
free(dev);
dev = strdup(lbuf);
while ((*cp != 0) && isspace(*cp))
cp++;
if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
continue;
cp += 2;
while ((*cp != 0) && isspace(*cp))
cp++;
if (*cp == 0)
continue;
ep = cp;
while ((*cp != 0) && !isspace(*cp))
cp++;
*cp = 0;
free(fstyp);
fstyp = strdup(ep);
cp += 1;
while ((*cp != 0) && isspace(*cp))
cp++;
if (*cp == 0)
continue;
ep = cp;
while ((*cp != 0) && !isspace(*cp))
cp++;
*cp = 0;
options = strdup(ep);
sprintf(lbuf, "%s:%s", fstyp, dev);
setenv("vfs.root.mountfrom", lbuf, 0);
if (getenv("vfs.root.mountfrom.options") == NULL) {
setenv("vfs.root.mountfrom.options", options, 0);
}
free(options);
error = 0;
break;
}
close(fd);
free(dev);
free(fstyp);
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;
free(cp);
}
}
return (error);
}
static int
loadakernel(int try, int argc, char *argv[])
{
char *cp;
for (try = 0; (cp = getbootfile(try)) != NULL; try++)
if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
printf("can't load '%s'\n", cp);
else
return (1);
return (0);
}