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