Home | History | Annotate | Line # | Download | only in lib
bootmenu.c revision 1.4.2.2
      1  1.4.2.2  mjf /*	$NetBSD: bootmenu.c,v 1.4.2.2 2009/01/17 13:28:05 mjf Exp $	*/
      2  1.4.2.2  mjf 
      3  1.4.2.2  mjf /*-
      4  1.4.2.2  mjf  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.4.2.2  mjf  * All rights reserved.
      6  1.4.2.2  mjf  *
      7  1.4.2.2  mjf  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  mjf  * modification, are permitted provided that the following conditions
      9  1.4.2.2  mjf  * are met:
     10  1.4.2.2  mjf  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  mjf  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  mjf  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  mjf  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  mjf  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  mjf  *
     16  1.4.2.2  mjf  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.4.2.2  mjf  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.4.2.2  mjf  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.4.2.2  mjf  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.4.2.2  mjf  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.4.2.2  mjf  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.4.2.2  mjf  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.4.2.2  mjf  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.4.2.2  mjf  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.4.2.2  mjf  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.4.2.2  mjf  * POSSIBILITY OF SUCH DAMAGE.
     27  1.4.2.2  mjf  */
     28  1.4.2.2  mjf 
     29  1.4.2.2  mjf #ifndef SMALL
     30  1.4.2.2  mjf 
     31  1.4.2.2  mjf #include <sys/types.h>
     32  1.4.2.2  mjf #include <sys/reboot.h>
     33  1.4.2.2  mjf #include <sys/bootblock.h>
     34  1.4.2.2  mjf 
     35  1.4.2.2  mjf #include <lib/libsa/stand.h>
     36  1.4.2.2  mjf #include <lib/libsa/ufs.h>
     37  1.4.2.2  mjf #include <lib/libkern/libkern.h>
     38  1.4.2.2  mjf 
     39  1.4.2.2  mjf #include <libi386.h>
     40  1.4.2.2  mjf #include <bootmenu.h>
     41  1.4.2.2  mjf 
     42  1.4.2.2  mjf #define isnum(c) ((c) >= '0' && (c) <= '9')
     43  1.4.2.2  mjf 
     44  1.4.2.2  mjf extern struct x86_boot_params boot_params;
     45  1.4.2.2  mjf extern	const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     46  1.4.2.2  mjf 
     47  1.4.2.2  mjf #define MENUFORMAT_AUTO	  0
     48  1.4.2.2  mjf #define MENUFORMAT_NUMBER 1
     49  1.4.2.2  mjf #define MENUFORMAT_LETTER 2
     50  1.4.2.2  mjf 
     51  1.4.2.2  mjf struct bootconf_def bootconf;
     52  1.4.2.2  mjf 
     53  1.4.2.2  mjf int
     54  1.4.2.2  mjf atoi(const char *in)
     55  1.4.2.2  mjf {
     56  1.4.2.2  mjf 	char *c;
     57  1.4.2.2  mjf 	int ret;
     58  1.4.2.2  mjf 
     59  1.4.2.2  mjf 	ret = 0;
     60  1.4.2.2  mjf 	c = (char *)in;
     61  1.4.2.2  mjf 	if (*c == '-')
     62  1.4.2.2  mjf 		c++;
     63  1.4.2.2  mjf 	for (; isnum(*c); c++)
     64  1.4.2.2  mjf 		ret = (ret * 10) + (*c - '0');
     65  1.4.2.2  mjf 
     66  1.4.2.2  mjf 	return (*in == '-') ? -ret : ret;
     67  1.4.2.2  mjf }
     68  1.4.2.2  mjf 
     69  1.4.2.2  mjf /*
     70  1.4.2.2  mjf  * This function parses a boot.cfg file in the root of the filesystem
     71  1.4.2.2  mjf  * (if present) and populates the global boot configuration.
     72  1.4.2.2  mjf  *
     73  1.4.2.2  mjf  * The file consists of a number of lines each terminated by \n
     74  1.4.2.2  mjf  * The lines are in the format keyword=value. There should not be spaces
     75  1.4.2.2  mjf  * around the = sign.
     76  1.4.2.2  mjf  *
     77  1.4.2.2  mjf  * The recognised keywords are:
     78  1.4.2.2  mjf  * banner: text displayed instead of the normal welcome text
     79  1.4.2.2  mjf  * menu: Descriptive text:command to use
     80  1.4.2.2  mjf  * timeout: Timeout in seconds (overrides that set by installboot)
     81  1.4.2.2  mjf  * default: the default menu option to use if Return is pressed
     82  1.4.2.2  mjf  * consdev: the console device to use
     83  1.4.2.2  mjf  * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
     84  1.4.2.2  mjf  * clear: whether to clear the screen or not
     85  1.4.2.2  mjf  *
     86  1.4.2.2  mjf  * Example boot.cfg file:
     87  1.4.2.2  mjf  * banner=Welcome to NetBSD
     88  1.4.2.2  mjf  * banner=Please choose the boot type from the following menu
     89  1.4.2.2  mjf  * menu=Boot NetBSD:boot netbsd
     90  1.4.2.2  mjf  * menu=Boot into single user mode:boot netbsd -s
     91  1.4.2.2  mjf  * menu=:boot hd1a:netbsd -cs
     92  1.4.2.2  mjf  * menu=Goto boot comand line:prompt
     93  1.4.2.2  mjf  * timeout=10
     94  1.4.2.2  mjf  * consdev=com0
     95  1.4.2.2  mjf  * default=1
     96  1.4.2.2  mjf */
     97  1.4.2.2  mjf void
     98  1.4.2.2  mjf parsebootconf(const char *conf)
     99  1.4.2.2  mjf {
    100  1.4.2.2  mjf 	char *bc, *c;
    101  1.4.2.2  mjf 	int cmenu, cbanner, len;
    102  1.4.2.2  mjf 	int fd, err, off;
    103  1.4.2.2  mjf 	struct stat st;
    104  1.4.2.2  mjf 	char *key, *value, *v2;
    105  1.4.2.2  mjf 
    106  1.4.2.2  mjf 	/* Clear bootconf structure */
    107  1.4.2.2  mjf 	bzero((void *)&bootconf, sizeof(bootconf));
    108  1.4.2.2  mjf 
    109  1.4.2.2  mjf 	/* Set timeout to configured */
    110  1.4.2.2  mjf 	bootconf.timeout = boot_params.bp_timeout;
    111  1.4.2.2  mjf 
    112  1.4.2.2  mjf 	/* automatically switch between letter and numbers on menu */
    113  1.4.2.2  mjf 	bootconf.menuformat = MENUFORMAT_AUTO;
    114  1.4.2.2  mjf 
    115  1.4.2.2  mjf 	fd = open(BOOTCONF, 0);
    116  1.4.2.2  mjf 	if (fd < 0)
    117  1.4.2.2  mjf 		return;
    118  1.4.2.2  mjf 
    119  1.4.2.2  mjf 	err = fstat(fd, &st);
    120  1.4.2.2  mjf 	if (err == -1) {
    121  1.4.2.2  mjf 		close(fd);
    122  1.4.2.2  mjf 		return;
    123  1.4.2.2  mjf 	}
    124  1.4.2.2  mjf 
    125  1.4.2.2  mjf 	bc = alloc(st.st_size + 1);
    126  1.4.2.2  mjf 	if (bc == NULL) {
    127  1.4.2.2  mjf 		printf("Could not allocate memory for boot configuration\n");
    128  1.4.2.2  mjf 		return;
    129  1.4.2.2  mjf 	}
    130  1.4.2.2  mjf 
    131  1.4.2.2  mjf 	off = 0;
    132  1.4.2.2  mjf 	do {
    133  1.4.2.2  mjf 		len = read(fd, bc + off, 1024);
    134  1.4.2.2  mjf 		if (len <= 0)
    135  1.4.2.2  mjf 			break;
    136  1.4.2.2  mjf 		off += len;
    137  1.4.2.2  mjf 	} while (len > 0);
    138  1.4.2.2  mjf 	bc[off] = '\0';
    139  1.4.2.2  mjf 
    140  1.4.2.2  mjf 	close(fd);
    141  1.4.2.2  mjf 	/* bc now contains the whole boot.cfg file */
    142  1.4.2.2  mjf 
    143  1.4.2.2  mjf 	cmenu = 0;
    144  1.4.2.2  mjf 	cbanner = 0;
    145  1.4.2.2  mjf 	for (c = bc; *c; c++) {
    146  1.4.2.2  mjf 		key = c;
    147  1.4.2.2  mjf 		/* Look for = separator between key and value */
    148  1.4.2.2  mjf 		for (; *c && *c != '='; c++)
    149  1.4.2.2  mjf 			continue;
    150  1.4.2.2  mjf 		if (*c == '\0')
    151  1.4.2.2  mjf 			break; /* break if at end of data */
    152  1.4.2.2  mjf 
    153  1.4.2.2  mjf 		/* zero terminate key which points to keyword */
    154  1.4.2.2  mjf 		*c++ = 0;
    155  1.4.2.2  mjf 		value = c;
    156  1.4.2.2  mjf 		/* Look for end of line (or file) and zero terminate value */
    157  1.4.2.2  mjf 		for (; *c && *c != '\n'; c++)
    158  1.4.2.2  mjf 			continue;
    159  1.4.2.2  mjf 		*c = 0;
    160  1.4.2.2  mjf 
    161  1.4.2.2  mjf 		if (!strncmp(key, "menu", 4)) {
    162  1.4.2.2  mjf 			/*
    163  1.4.2.2  mjf 			 * Parse "menu=<description>:<command>".  If the
    164  1.4.2.2  mjf 			 * description is empty ("menu=:<command>)",
    165  1.4.2.2  mjf 			 * then re-use the command as the description.
    166  1.4.2.2  mjf 			 * Note that the command may contain embedded
    167  1.4.2.2  mjf 			 * colons.
    168  1.4.2.2  mjf 			 */
    169  1.4.2.2  mjf 			if (cmenu >= MAXMENU)
    170  1.4.2.2  mjf 				continue;
    171  1.4.2.2  mjf 			bootconf.desc[cmenu] = value;
    172  1.4.2.2  mjf 			for (v2 = value; *v2 && *v2 != ':'; v2++)
    173  1.4.2.2  mjf 				continue;
    174  1.4.2.2  mjf 			if (*v2) {
    175  1.4.2.2  mjf 				*v2++ = 0;
    176  1.4.2.2  mjf 				bootconf.command[cmenu] = v2;
    177  1.4.2.2  mjf 				if (! *value)
    178  1.4.2.2  mjf 					bootconf.desc[cmenu] = v2;
    179  1.4.2.2  mjf 				cmenu++;
    180  1.4.2.2  mjf 			} else {
    181  1.4.2.2  mjf 				/* No delimiter means invalid line */
    182  1.4.2.2  mjf 				bootconf.desc[cmenu] = NULL;
    183  1.4.2.2  mjf 			}
    184  1.4.2.2  mjf 		} else if (!strncmp(key, "banner", 6)) {
    185  1.4.2.2  mjf 			if (cbanner < MAXBANNER)
    186  1.4.2.2  mjf 				bootconf.banner[cbanner++] = value;
    187  1.4.2.2  mjf 		} else if (!strncmp(key, "timeout", 7)) {
    188  1.4.2.2  mjf 			if (!isnum(*value))
    189  1.4.2.2  mjf 				bootconf.timeout = -1;
    190  1.4.2.2  mjf 			else
    191  1.4.2.2  mjf 				bootconf.timeout = atoi(value);
    192  1.4.2.2  mjf 		} else if (!strncmp(key, "default", 7)) {
    193  1.4.2.2  mjf 			bootconf.def = atoi(value) - 1;
    194  1.4.2.2  mjf 		} else if (!strncmp(key, "consdev", 7)) {
    195  1.4.2.2  mjf 			bootconf.consdev = value;
    196  1.4.2.2  mjf 		} else if (!strncmp(key, "load", 4)) {
    197  1.4.2.2  mjf 			module_add(value);
    198  1.4.2.2  mjf 		} else if (!strncmp(key, "format", 6)) {
    199  1.4.2.2  mjf 			printf("value:%c\n", *value);
    200  1.4.2.2  mjf 			switch (*value) {
    201  1.4.2.2  mjf 			case 'a':
    202  1.4.2.2  mjf 			case 'A':
    203  1.4.2.2  mjf 				bootconf.menuformat = MENUFORMAT_AUTO;
    204  1.4.2.2  mjf 				break;
    205  1.4.2.2  mjf 
    206  1.4.2.2  mjf 			case 'n':
    207  1.4.2.2  mjf 			case 'N':
    208  1.4.2.2  mjf 			case 'd':
    209  1.4.2.2  mjf 			case 'D':
    210  1.4.2.2  mjf 				bootconf.menuformat = MENUFORMAT_NUMBER;
    211  1.4.2.2  mjf 				break;
    212  1.4.2.2  mjf 
    213  1.4.2.2  mjf 			case 'l':
    214  1.4.2.2  mjf 			case 'L':
    215  1.4.2.2  mjf 				bootconf.menuformat = MENUFORMAT_LETTER;
    216  1.4.2.2  mjf 				break;
    217  1.4.2.2  mjf 			}
    218  1.4.2.2  mjf 		} else if (!strncmp(key, "clear", 5)) {
    219  1.4.2.2  mjf 			bootconf.clear = !!atoi(value);
    220  1.4.2.2  mjf 		}
    221  1.4.2.2  mjf 	}
    222  1.4.2.2  mjf 	switch (bootconf.menuformat) {
    223  1.4.2.2  mjf 	case MENUFORMAT_AUTO:
    224  1.4.2.2  mjf 		if (cmenu > 9 && bootconf.timeout > 0)
    225  1.4.2.2  mjf 			bootconf.menuformat = MENUFORMAT_LETTER;
    226  1.4.2.2  mjf 		else
    227  1.4.2.2  mjf 			bootconf.menuformat = MENUFORMAT_NUMBER;
    228  1.4.2.2  mjf 		break;
    229  1.4.2.2  mjf 
    230  1.4.2.2  mjf 	case MENUFORMAT_NUMBER:
    231  1.4.2.2  mjf 		if (cmenu > 9 && bootconf.timeout > 0)
    232  1.4.2.2  mjf 			cmenu = 9;
    233  1.4.2.2  mjf 		break;
    234  1.4.2.2  mjf 	}
    235  1.4.2.2  mjf 
    236  1.4.2.2  mjf 	bootconf.nummenu = cmenu;
    237  1.4.2.2  mjf 	if (bootconf.def < 0)
    238  1.4.2.2  mjf 		bootconf.def = 0;
    239  1.4.2.2  mjf 	if (bootconf.def >= cmenu)
    240  1.4.2.2  mjf 		bootconf.def = cmenu - 1;
    241  1.4.2.2  mjf }
    242  1.4.2.2  mjf 
    243  1.4.2.2  mjf /*
    244  1.4.2.2  mjf  * doboottypemenu will render the menu and parse any user input
    245  1.4.2.2  mjf  */
    246  1.4.2.2  mjf static int
    247  1.4.2.2  mjf getchoicefrominput(char *input, int def)
    248  1.4.2.2  mjf {
    249  1.4.2.2  mjf 	int choice;
    250  1.4.2.2  mjf 	choice = -1;
    251  1.4.2.2  mjf 	if (*input == '\0' || *input == '\r' || *input == '\n')
    252  1.4.2.2  mjf 		choice = def;
    253  1.4.2.2  mjf 	else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
    254  1.4.2.2  mjf 		choice = (*input) - 'A';
    255  1.4.2.2  mjf 	else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
    256  1.4.2.2  mjf 		choice = (*input) - 'a';
    257  1.4.2.2  mjf 	else if (isnum(*input)) {
    258  1.4.2.2  mjf 		choice = atoi(input) - 1;
    259  1.4.2.2  mjf 		if (choice < 0 || choice >= bootconf.nummenu)
    260  1.4.2.2  mjf 			choice = -1;
    261  1.4.2.2  mjf 	}
    262  1.4.2.2  mjf 	return choice;
    263  1.4.2.2  mjf }
    264  1.4.2.2  mjf 
    265  1.4.2.2  mjf void
    266  1.4.2.2  mjf doboottypemenu(void)
    267  1.4.2.2  mjf {
    268  1.4.2.2  mjf 	int choice;
    269  1.4.2.2  mjf 	char input[80], *ic, *oc;
    270  1.4.2.2  mjf 
    271  1.4.2.2  mjf 	printf("\n");
    272  1.4.2.2  mjf 	/* Display menu */
    273  1.4.2.2  mjf 	if (bootconf.menuformat == MENUFORMAT_LETTER) {
    274  1.4.2.2  mjf 		for (choice = 0; choice < bootconf.nummenu; choice++)
    275  1.4.2.2  mjf 			printf("    %c. %s\n", choice + 'A',
    276  1.4.2.2  mjf 			    bootconf.desc[choice]);
    277  1.4.2.2  mjf 	} else {
    278  1.4.2.2  mjf 		/* Can't use %2d format string with libsa */
    279  1.4.2.2  mjf 		for (choice = 0; choice < bootconf.nummenu; choice++)
    280  1.4.2.2  mjf 			printf("    %s%d. %s\n",
    281  1.4.2.2  mjf 			    (choice < 9) ?  " " : "",
    282  1.4.2.2  mjf 			    choice + 1,
    283  1.4.2.2  mjf 			    bootconf.desc[choice]);
    284  1.4.2.2  mjf 	}
    285  1.4.2.2  mjf 	choice = -1;
    286  1.4.2.2  mjf 	for (;;) {
    287  1.4.2.2  mjf 		input[0] = '\0';
    288  1.4.2.2  mjf 
    289  1.4.2.2  mjf 		if (bootconf.timeout < 0) {
    290  1.4.2.2  mjf 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    291  1.4.2.2  mjf 				printf("\nOption: [%c]:",
    292  1.4.2.2  mjf 				    bootconf.def + 'A');
    293  1.4.2.2  mjf 			else
    294  1.4.2.2  mjf 				printf("\nOption: [%d]:",
    295  1.4.2.2  mjf 				    bootconf.def + 1);
    296  1.4.2.2  mjf 
    297  1.4.2.2  mjf 			gets(input);
    298  1.4.2.2  mjf 			choice = getchoicefrominput(input, bootconf.def);
    299  1.4.2.2  mjf 		} else if (bootconf.timeout == 0)
    300  1.4.2.2  mjf 			choice = bootconf.def;
    301  1.4.2.2  mjf 		else  {
    302  1.4.2.2  mjf 			printf("\nChoose an option; RETURN for default; "
    303  1.4.2.2  mjf 			       "SPACE to stop countdown.\n");
    304  1.4.2.2  mjf 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    305  1.4.2.2  mjf 				printf("Option %c will be chosen in ",
    306  1.4.2.2  mjf 				    bootconf.def + 'A');
    307  1.4.2.2  mjf 			else
    308  1.4.2.2  mjf 				printf("Option %d will be chosen in ",
    309  1.4.2.2  mjf 				    bootconf.def + 1);
    310  1.4.2.2  mjf 			input[0] = awaitkey(bootconf.timeout, 1);
    311  1.4.2.2  mjf 			input[1] = '\0';
    312  1.4.2.2  mjf 			choice = getchoicefrominput(input, bootconf.def);
    313  1.4.2.2  mjf 			/* If invalid key pressed, drop to menu */
    314  1.4.2.2  mjf 			if (choice == -1)
    315  1.4.2.2  mjf 				bootconf.timeout = -1;
    316  1.4.2.2  mjf 		}
    317  1.4.2.2  mjf 		if (choice < 0)
    318  1.4.2.2  mjf 			continue;
    319  1.4.2.2  mjf 		if (!strcmp(bootconf.command[choice], "prompt") &&
    320  1.4.2.2  mjf 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
    321  1.4.2.2  mjf 		    check_password((char *)boot_params.bp_password))) {
    322  1.4.2.2  mjf 			printf("type \"?\" or \"help\" for help.\n");
    323  1.4.2.2  mjf 			bootmenu(); /* does not return */
    324  1.4.2.2  mjf 		} else {
    325  1.4.2.2  mjf 			ic = bootconf.command[choice];
    326  1.4.2.2  mjf 			/* Split command string at ; into separate commands */
    327  1.4.2.2  mjf 			do {
    328  1.4.2.2  mjf 				oc = input;
    329  1.4.2.2  mjf 				/* Look for ; separator */
    330  1.4.2.2  mjf 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    331  1.4.2.2  mjf 					*oc++ = *ic;
    332  1.4.2.2  mjf 				if (*input == '\0')
    333  1.4.2.2  mjf 					continue;
    334  1.4.2.2  mjf 				/* Strip out any trailing spaces */
    335  1.4.2.2  mjf 				oc--;
    336  1.4.2.2  mjf 				for (; *oc == ' ' && oc > input; oc--);
    337  1.4.2.2  mjf 				*++oc = '\0';
    338  1.4.2.2  mjf 				if (*ic == COMMAND_SEPARATOR)
    339  1.4.2.2  mjf 					ic++;
    340  1.4.2.2  mjf 				/* Stop silly command strings like ;;; */
    341  1.4.2.2  mjf 				if (*input != '\0')
    342  1.4.2.2  mjf 					docommand(input);
    343  1.4.2.2  mjf 				/* Skip leading spaces */
    344  1.4.2.2  mjf 				for (; *ic == ' '; ic++);
    345  1.4.2.2  mjf 			} while (*ic);
    346  1.4.2.2  mjf 		}
    347  1.4.2.2  mjf 
    348  1.4.2.2  mjf 	}
    349  1.4.2.2  mjf }
    350  1.4.2.2  mjf 
    351  1.4.2.2  mjf #endif	/* !SMALL */
    352