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