Home | History | Annotate | Line # | Download | only in zboot
bootmenu.c revision 1.1
      1 /*	$NetBSD: bootmenu.c,v 1.1 2009/03/02 09:33:02 nonaka 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 #include <sys/types.h>
     30 #include <sys/reboot.h>
     31 #include <sys/bootblock.h>
     32 
     33 #include "boot.h"
     34 #include "unixdev.h"
     35 #include "bootmenu.h"
     36 #include "pathnames.h"
     37 
     38 #define isnum(c) ((c) >= '0' && (c) <= '9')
     39 
     40 #define MENUFORMAT_AUTO	  0
     41 #define MENUFORMAT_NUMBER 1
     42 #define MENUFORMAT_LETTER 2
     43 
     44 struct bootconf_def bootconf;
     45 
     46 int
     47 atoi(const char *in)
     48 {
     49 	char *c;
     50 	int ret;
     51 
     52 	ret = 0;
     53 	c = (char *)in;
     54 	if (*c == '-')
     55 		c++;
     56 	for (; isnum(*c); c++)
     57 		ret = (ret * 10) + (*c - '0');
     58 
     59 	return (*in == '-') ? -ret : ret;
     60 }
     61 
     62 /*
     63  * This function parses a boot.cfg file in the root of the filesystem
     64  * (if present) and populates the global boot configuration.
     65  *
     66  * The file consists of a number of lines each terminated by \n
     67  * The lines are in the format keyword=value. There should not be spaces
     68  * around the = sign.
     69  *
     70  * The recognised keywords are:
     71  * banner: text displayed instead of the normal welcome text
     72  * menu: Descriptive text:command to use
     73  * timeout: Timeout in seconds (overrides that set by installboot)
     74  * default: the default menu option to use if Return is pressed
     75  * consdev: the console device to use
     76  * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
     77  * clear: whether to clear the screen or not
     78  *
     79  * Example boot.cfg file:
     80  * banner=Welcome to NetBSD
     81  * banner=Please choose the boot type from the following menu
     82  * menu=Boot NetBSD:boot netbsd
     83  * menu=Boot into single user mode:boot netbsd -s
     84  * menu=:boot hd1a:netbsd -cs
     85  * menu=Goto boot comand line:prompt
     86  * timeout=10
     87  * consdev=com0
     88  * default=1
     89 */
     90 void
     91 parsebootconf(const char *conf)
     92 {
     93 	char *bc, *c;
     94 	int cmenu, cbanner, len;
     95 	int fd, err, off;
     96 	struct stat st;
     97 	char *key, *value, *v2;
     98 
     99 	/* Clear bootconf structure */
    100 	memset(&bootconf, 0, sizeof(bootconf));
    101 
    102 	/* Set timeout to configured */
    103 	bootconf.timeout = default_timeout;
    104 
    105 	/* automatically switch between letter and numbers on menu */
    106 	bootconf.menuformat = MENUFORMAT_AUTO;
    107 
    108 	fd = open(_PATH_BOOTCONF, 0);
    109 	if (fd < 0)
    110 		return;
    111 
    112 	err = fstat(fd, &st);
    113 	if (err == -1) {
    114 		close(fd);
    115 		return;
    116 	}
    117 
    118 	bc = alloc(st.st_size + 1);
    119 	if (bc == NULL) {
    120 		printf("Could not allocate memory for boot configuration\n");
    121 		return;
    122 	}
    123 
    124 	off = 0;
    125 	do {
    126 		len = read(fd, bc + off, 1024);
    127 		if (len <= 0)
    128 			break;
    129 		off += len;
    130 	} while (len > 0);
    131 	bc[off] = '\0';
    132 
    133 	close(fd);
    134 	/* bc now contains the whole boot.cfg file */
    135 
    136 	cmenu = 0;
    137 	cbanner = 0;
    138 	for (c = bc; *c; c++) {
    139 		key = c;
    140 		/* Look for = separator between key and value */
    141 		for (; *c && *c != '='; c++)
    142 			continue;
    143 		if (*c == '\0')
    144 			break; /* break if at end of data */
    145 
    146 		/* zero terminate key which points to keyword */
    147 		*c++ = 0;
    148 		value = c;
    149 		/* Look for end of line (or file) and zero terminate value */
    150 		for (; *c && *c != '\n'; c++)
    151 			continue;
    152 		*c = 0;
    153 
    154 		if (!strncmp(key, "menu", 4)) {
    155 			/*
    156 			 * Parse "menu=<description>:<command>".  If the
    157 			 * description is empty ("menu=:<command>)",
    158 			 * then re-use the command as the description.
    159 			 * Note that the command may contain embedded
    160 			 * colons.
    161 			 */
    162 			if (cmenu >= MAXMENU)
    163 				continue;
    164 			bootconf.desc[cmenu] = value;
    165 			for (v2 = value; *v2 && *v2 != ':'; v2++)
    166 				continue;
    167 			if (*v2) {
    168 				*v2++ = 0;
    169 				bootconf.command[cmenu] = v2;
    170 				if (! *value)
    171 					bootconf.desc[cmenu] = v2;
    172 				cmenu++;
    173 			} else {
    174 				/* No delimiter means invalid line */
    175 				bootconf.desc[cmenu] = NULL;
    176 			}
    177 		} else if (!strncmp(key, "banner", 6)) {
    178 			if (cbanner < MAXBANNER)
    179 				bootconf.banner[cbanner++] = value;
    180 		} else if (!strncmp(key, "timeout", 7)) {
    181 			if (!isnum(*value))
    182 				bootconf.timeout = -1;
    183 			else
    184 				bootconf.timeout = atoi(value);
    185 		} else if (!strncmp(key, "default", 7)) {
    186 			bootconf.def = atoi(value) - 1;
    187 		} else if (!strncmp(key, "consdev", 7)) {
    188 			bootconf.consdev = value;
    189 		} else if (!strncmp(key, "format", 6)) {
    190 			printf("value:%c\n", *value);
    191 			switch (*value) {
    192 			case 'a':
    193 			case 'A':
    194 				bootconf.menuformat = MENUFORMAT_AUTO;
    195 				break;
    196 
    197 			case 'n':
    198 			case 'N':
    199 			case 'd':
    200 			case 'D':
    201 				bootconf.menuformat = MENUFORMAT_NUMBER;
    202 				break;
    203 
    204 			case 'l':
    205 			case 'L':
    206 				bootconf.menuformat = MENUFORMAT_LETTER;
    207 				break;
    208 			}
    209 		} else if (!strncmp(key, "clear", 5)) {
    210 			bootconf.clear = !!atoi(value);
    211 		}
    212 	}
    213 	switch (bootconf.menuformat) {
    214 	case MENUFORMAT_AUTO:
    215 		if (cmenu > 9 && bootconf.timeout > 0)
    216 			bootconf.menuformat = MENUFORMAT_LETTER;
    217 		else
    218 			bootconf.menuformat = MENUFORMAT_NUMBER;
    219 		break;
    220 
    221 	case MENUFORMAT_NUMBER:
    222 		if (cmenu > 9 && bootconf.timeout > 0)
    223 			cmenu = 9;
    224 		break;
    225 	}
    226 
    227 	bootconf.nummenu = cmenu;
    228 	if (bootconf.def < 0)
    229 		bootconf.def = 0;
    230 	if (bootconf.def >= cmenu)
    231 		bootconf.def = cmenu - 1;
    232 }
    233 
    234 /*
    235  * doboottypemenu will render the menu and parse any user input
    236  */
    237 static int
    238 getchoicefrominput(char *input, int def)
    239 {
    240 	int choice = -1;
    241 
    242 	if (*input == '\0' || *input == '\r' || *input == '\n')
    243 		choice = def;
    244 	else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
    245 		choice = (*input) - 'A';
    246 	else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
    247 		choice = (*input) - 'a';
    248 	else if (isnum(*input)) {
    249 		choice = atoi(input) - 1;
    250 		if (choice < 0 || choice >= bootconf.nummenu)
    251 			choice = -1;
    252 	}
    253 	return choice;
    254 }
    255 
    256 void
    257 doboottypemenu(void)
    258 {
    259 	char input[80], *ic, *oc;
    260 	int choice;
    261 
    262 	printf("\n");
    263 	/* Display menu */
    264 	if (bootconf.menuformat == MENUFORMAT_LETTER) {
    265 		for (choice = 0; choice < bootconf.nummenu; choice++)
    266 			printf("    %c. %s\n", choice + 'A',
    267 			    bootconf.desc[choice]);
    268 	} else {
    269 		/* Can't use %2d format string with libsa */
    270 		for (choice = 0; choice < bootconf.nummenu; choice++)
    271 			printf("    %s%d. %s\n",
    272 			    (choice < 9) ?  " " : "",
    273 			    choice + 1,
    274 			    bootconf.desc[choice]);
    275 	}
    276 	choice = -1;
    277 	for (;;) {
    278 		input[0] = '\0';
    279 
    280 		if (bootconf.timeout < 0) {
    281 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    282 				printf("\nOption: [%c]:",
    283 				    bootconf.def + 'A');
    284 			else
    285 				printf("\nOption: [%d]:",
    286 				    bootconf.def + 1);
    287 
    288 			gets(input);
    289 			choice = getchoicefrominput(input, bootconf.def);
    290 		} else if (bootconf.timeout == 0)
    291 			choice = bootconf.def;
    292 		else  {
    293 			printf("\nChoose an option; RETURN for default; "
    294 			       "SPACE to stop countdown.\n");
    295 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    296 				printf("Option %c will be chosen in ",
    297 				    bootconf.def + 'A');
    298 			else
    299 				printf("Option %d will be chosen in ",
    300 				    bootconf.def + 1);
    301 			input[0] = awaitkey(bootconf.timeout, 1);
    302 			input[1] = '\0';
    303 			choice = getchoicefrominput(input, bootconf.def);
    304 			/* If invalid key pressed, drop to menu */
    305 			if (choice == -1)
    306 				bootconf.timeout = -1;
    307 		}
    308 		if (choice < 0)
    309 			continue;
    310 		if (!strcmp(bootconf.command[choice], "prompt")) {
    311 			printf("type \"?\" or \"help\" for help.\n");
    312 			bootmenu(); /* does not return */
    313 		} else {
    314 			ic = bootconf.command[choice];
    315 			/* Split command string at ; into separate commands */
    316 			do {
    317 				oc = input;
    318 				/* Look for ; separator */
    319 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    320 					*oc++ = *ic;
    321 				if (*input == '\0')
    322 					continue;
    323 				/* Strip out any trailing spaces */
    324 				oc--;
    325 				for (; *oc == ' ' && oc > input; oc--);
    326 				*++oc = '\0';
    327 				if (*ic == COMMAND_SEPARATOR)
    328 					ic++;
    329 				/* Stop silly command strings like ;;; */
    330 				if (*input != '\0')
    331 					docommand(input);
    332 				/* Skip leading spaces */
    333 				for (; *ic == ' '; ic++);
    334 			} while (*ic);
    335 		}
    336 	}
    337 }
    338