/*
* Copyright (c) 2003
* David Laight. All rights reserved
* Copyright (c) 1996, 1997, 1999
* Matthias Drochner. All rights reserved.
* Copyright (c) 1996, 1997
* Perry E. Metzger. All rights reserved.
* Copyright (c) 1997
* Jason R. Thorpe. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgements:
* This product includes software developed for the NetBSD Project
* by Matthias Drochner.
* This product includes software developed for the NetBSD Project
* by Perry E. Metzger.
* 4. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
*/
#ifndef SMALL
struct bootconf_def {
char *banner[MAXBANNER]; /* Banner text */
char *command[MAXMENU]; /* Menu commands per entry*/
char *consdev; /* Console device */
int def; /* Default menu option */
char *desc[MAXMENU]; /* Menu text per entry */
int nummenu; /* Number of menu items */
int timeout; /* Timeout in seconds */
} bootconf;
#endif /* ifndef SMALL */
int
parsebootfile(const char *fname, char **fsname, char **devname,
int *unit, int *partition, const char **file)
{
const char *col;
if ((col = strchr(fname, ':')) != NULL) { /* device given */
static char savedevname[MAXDEVNAME+1];
int devlen;
int u = 0, p = 0;
int i = 0;
devlen = col - fname;
if (devlen > MAXDEVNAME)
return EINVAL;
#define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
if (!isvalidname(fname[i]))
return EINVAL;
do {
savedevname[i] = fname[i];
i++;
} while (isvalidname(fname[i]));
savedevname[i] = '\0';
#define isnum(c) ((c) >= '0' && (c) <= '9')
if (i < devlen) {
if (!isnum(fname[i]))
return EUNIT;
do {
u *= 10;
u += fname[i++] - '0';
} while (isnum(fname[i]));
}
#define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
if (i < devlen) {
if (!isvalidpart(fname[i]))
return EPART;
p = fname[i++] - 'a';
}
void
print_banner(void)
{
#ifndef SMALL
int n;
if (bootconf.banner[0]) {
for (n = 0; bootconf.banner[n]; n++)
printf("%s\n", bootconf.banner[n]);
printf("\n");
} else {
#endif /* ifndef SMALL */
printf("\n");
printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
printf(">> Memory: %d/%d k\n", getbasemem(), getextmem());
#ifndef SMALL
}
#endif /* ifndef SMALL */
}
#ifndef SMALL
int
atoi(const char *in)
{
char *c;
int ret;
ret = 0;
c = (char *)in;
if (*c == '-')
c++;
for (; isnum(*c); c++)
ret = (ret * 10) + (*c - '0');
return (*in == '-') ? -ret : ret;
}
/*
* This function parses a boot.cnf file in the root of the filesystem
* (if present) and populates the global boot configuration.
*
* The file consists of a number of lines each terminated by \n
* The lines are in the format keyword=value. There should be spaces
* around the = sign.
*
* The recognised keywords are:
* banner: text displayed instead of the normal welcome text
* menu: Descriptive text:command to use
* timeout: Timeout in seconds (overrides that set by installboot)
* default: the default menu option to use if Return is pressed
* consdev: the console device to use
*
* Example boot.cnf file:
* banner=Welcome to NetBSD
* banner=Please choose the boot type from the following menu
* menu=Boot NetBSD:boot netbsd
* menu=Boot into single user mode:boot netbsd -s
* menu=Goto boot comand line:prompt
* timeout=10
* consdev=com0
* default=1
*/
void
parsebootconf(const char *conf)
{
char *bc, *c;
int cmenu, cbanner, len;
int fd, err, off;
struct stat st;
char *value, *key;
/* Set timeout to configured */
bootconf.timeout = boot_params.bp_timeout;
err = stat(BOOTCONF, &st);
if (err == -1)
return;
fd = open(BOOTCONF, 0);
if (fd < 0)
return;
bc = alloc(st.st_size + 1);
if (bc == NULL) {
printf("Could not allocate memory for boot configuration\n");
return;
}
off = 0;
do {
len = read(fd, bc + off, 1024);
if (len <= 0)
break;
off += len;
} while (len > 0);
bc[off] = '\0';
close(fd);
/* bc now contains the whole boot.cnf file */
cmenu = 0;
cbanner = 0;
for(c = bc; *c; c++) {
key = c;
/* Look for = separator between key and value */
for (; *c && *c != '='; c++)
continue;
if (*c == '\0')
break; /* break if at end of data */
/* zero terminate key which points to keyword */
*c++ = 0;
value = c;
/* Look for end of line (or file) and zero terminate value */
for (; *c && *c != '\n'; c++)
continue;
*c = 0;
if (!strncmp(key, "menu", 4)) {
if (cmenu >= MAXMENU)
continue;
bootconf.desc[cmenu] = value;
/* Look for : between description and command */
for (; *value && *value != ':'; value++)
continue;
if(*value) {
*value++ = 0;
bootconf.command[cmenu] = value;
cmenu++;
} else {
/* No delimiter means invalid line */
bootconf.desc[cmenu] = NULL;
}
} else if (!strncmp(key, "banner", 6)) {
if (cbanner < MAXBANNER)
bootconf.banner[cbanner++] = value;
} else if (!strncmp(key, "timeout", 7)) {
if (!isnum(*value))
bootconf.timeout = -1;
else
bootconf.timeout = atoi(value);
} else if (!strncmp(key, "default", 7)) {
bootconf.def = atoi(value) - 1;
} else if (!strncmp(key, "consdev", 7)) {
bootconf.consdev = value;
}
}
bootconf.nummenu = cmenu;
if (bootconf.def < 0)
bootconf.def = 0;
if (bootconf.def >= cmenu)
bootconf.def = cmenu - 1;
}
/*
* doboottypemenu will render the menu and parse any user input
*/
void
doboottypemenu(void)
{
int choice;
char input[80], c;
/* Display menu */
for (choice = 0; bootconf.desc[choice]; choice++)
printf(" %d. %s\n", choice+1, bootconf.desc[choice]);
choice = -1;
for(;;) {
input[0] = '\0';
if (bootconf.timeout < 0) {
printf("\nOption: [%d]:", bootconf.def + 1);
gets(input);
if (input[0] == '\0') choice = bootconf.def;
if (input[0] >= '1' &&
input[0] <= bootconf.nummenu + '0')
choice = input[0] - '1';
} else if (bootconf.timeout == 0)
choice = bootconf.def;
else {
printf("\nPress the key for your chosen option or ");
printf("Return to choose the default (%d)\n",
bootconf.def + 1);
printf("Option %d will be chosen in ",
bootconf.def + 1);
c = awaitkey(bootconf.timeout, 1);
if (c >= '1' && c <= bootconf.nummenu + '0')
choice = c - '1';
else if (c == '\r' || c == '\n' || c == '\0')
/* default if timed out or Return pressed */
choice = bootconf.def;
else {
/* If any other key pressed, drop to menu */
bootconf.timeout = -1;
choice = -1;
}
}
if (choice < 0)
continue;
if (!strcmp(bootconf.command[choice], "prompt") &&
((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
check_password(boot_params.bp_password))) {
printf("type \"?\" or \"help\" for help.\n");
bootmenu(); /* does not return */
} else
docommand(bootconf.command[choice]);
}
}
#endif /* ifndef SMALL */
/*
* Called from the initial entry point boot_start in biosboot.S
*
* biosdev: BIOS drive number the system booted from
* biossector: Sector number of the NetBSD partition
*/
void
boot2(int biosdev, u_int biossector)
{
int currname;
char c;
initio(boot_params.bp_consdev);
#ifdef SUPPORT_PS2
biosmca();
#endif
gateA20();
if (boot_params.bp_flags & X86_BP_FLAGS_RESET_VIDEO)
biosvideomode();
/* need to remember these */
boot_biosdev = biosdev;
boot_biossector = biossector;
/* try to set default device to what BIOS tells us */
bios2dev(biosdev, biossector, &default_devname, &default_unit,
&default_partition);
/* if the user types "boot" without filename */
default_filename = DEFFILENAME;
#ifndef SMALL
parsebootconf(BOOTCONF);
/*
* If console set in boot.cnf, switch to it.
* This will print the banner, so we don't need to explicitly do it
*/
if (bootconf.consdev)
command_consdev(bootconf.consdev);
else
print_banner();
/* Display the menu, if applicable */
if (bootconf.nummenu > 0) {
/* Does not return */
doboottypemenu();
}
#else
print_banner();
#endif /* ifndef SMALL */
printf("Press return to boot now, any other key for boot menu\n");
for (currname = 0; currname < NUMNAMES; currname++) {
printf("booting %s - starting in ",
sprint_bootsel(names[currname][0]));
#ifdef SMALL
c = awaitkey(boot_params.bp_timeout, 1);
#else
c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
#endif /* ifdef SMALL */
if ((c != '\r') && (c != '\n') && (c != '\0') &&
((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
|| check_password(boot_params.bp_password))) {
printf("type \"?\" or \"help\" for help.\n");
bootmenu(); /* does not return */
}
/*
* try pairs of names[] entries, foo and foo.gz
*/
/* don't print "booting..." again */
bootit(names[currname][0], 0, 0);
/* since it failed, try compressed bootfile. */
bootit(names[currname][1], 0, 1);
}