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