Home | History | Annotate | Line # | Download | only in zboot
bootmenu.c revision 1.1.18.1
      1  1.1.18.1    yamt /*	$NetBSD: bootmenu.c,v 1.1.18.1 2012/04/17 00:07:13 yamt 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.18.1    yamt 	fd = open(conf, 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.18.1    yamt 		close(fd);
    122       1.1  nonaka 		return;
    123       1.1  nonaka 	}
    124       1.1  nonaka 
    125       1.1  nonaka 	off = 0;
    126       1.1  nonaka 	do {
    127       1.1  nonaka 		len = read(fd, bc + off, 1024);
    128       1.1  nonaka 		if (len <= 0)
    129       1.1  nonaka 			break;
    130       1.1  nonaka 		off += len;
    131       1.1  nonaka 	} while (len > 0);
    132       1.1  nonaka 	bc[off] = '\0';
    133       1.1  nonaka 
    134       1.1  nonaka 	close(fd);
    135       1.1  nonaka 	/* bc now contains the whole boot.cfg file */
    136       1.1  nonaka 
    137       1.1  nonaka 	cmenu = 0;
    138       1.1  nonaka 	cbanner = 0;
    139       1.1  nonaka 	for (c = bc; *c; c++) {
    140       1.1  nonaka 		key = c;
    141       1.1  nonaka 		/* Look for = separator between key and value */
    142       1.1  nonaka 		for (; *c && *c != '='; c++)
    143       1.1  nonaka 			continue;
    144       1.1  nonaka 		if (*c == '\0')
    145       1.1  nonaka 			break; /* break if at end of data */
    146       1.1  nonaka 
    147       1.1  nonaka 		/* zero terminate key which points to keyword */
    148       1.1  nonaka 		*c++ = 0;
    149       1.1  nonaka 		value = c;
    150       1.1  nonaka 		/* Look for end of line (or file) and zero terminate value */
    151       1.1  nonaka 		for (; *c && *c != '\n'; c++)
    152       1.1  nonaka 			continue;
    153       1.1  nonaka 		*c = 0;
    154       1.1  nonaka 
    155       1.1  nonaka 		if (!strncmp(key, "menu", 4)) {
    156       1.1  nonaka 			/*
    157       1.1  nonaka 			 * Parse "menu=<description>:<command>".  If the
    158       1.1  nonaka 			 * description is empty ("menu=:<command>)",
    159       1.1  nonaka 			 * then re-use the command as the description.
    160       1.1  nonaka 			 * Note that the command may contain embedded
    161       1.1  nonaka 			 * colons.
    162       1.1  nonaka 			 */
    163       1.1  nonaka 			if (cmenu >= MAXMENU)
    164       1.1  nonaka 				continue;
    165       1.1  nonaka 			bootconf.desc[cmenu] = value;
    166       1.1  nonaka 			for (v2 = value; *v2 && *v2 != ':'; v2++)
    167       1.1  nonaka 				continue;
    168       1.1  nonaka 			if (*v2) {
    169       1.1  nonaka 				*v2++ = 0;
    170       1.1  nonaka 				bootconf.command[cmenu] = v2;
    171       1.1  nonaka 				if (! *value)
    172       1.1  nonaka 					bootconf.desc[cmenu] = v2;
    173       1.1  nonaka 				cmenu++;
    174       1.1  nonaka 			} else {
    175       1.1  nonaka 				/* No delimiter means invalid line */
    176       1.1  nonaka 				bootconf.desc[cmenu] = NULL;
    177       1.1  nonaka 			}
    178       1.1  nonaka 		} else if (!strncmp(key, "banner", 6)) {
    179       1.1  nonaka 			if (cbanner < MAXBANNER)
    180       1.1  nonaka 				bootconf.banner[cbanner++] = value;
    181       1.1  nonaka 		} else if (!strncmp(key, "timeout", 7)) {
    182       1.1  nonaka 			if (!isnum(*value))
    183       1.1  nonaka 				bootconf.timeout = -1;
    184       1.1  nonaka 			else
    185       1.1  nonaka 				bootconf.timeout = atoi(value);
    186       1.1  nonaka 		} else if (!strncmp(key, "default", 7)) {
    187       1.1  nonaka 			bootconf.def = atoi(value) - 1;
    188       1.1  nonaka 		} else if (!strncmp(key, "consdev", 7)) {
    189       1.1  nonaka 			bootconf.consdev = value;
    190       1.1  nonaka 		} else if (!strncmp(key, "format", 6)) {
    191       1.1  nonaka 			printf("value:%c\n", *value);
    192       1.1  nonaka 			switch (*value) {
    193       1.1  nonaka 			case 'a':
    194       1.1  nonaka 			case 'A':
    195       1.1  nonaka 				bootconf.menuformat = MENUFORMAT_AUTO;
    196       1.1  nonaka 				break;
    197       1.1  nonaka 
    198       1.1  nonaka 			case 'n':
    199       1.1  nonaka 			case 'N':
    200       1.1  nonaka 			case 'd':
    201       1.1  nonaka 			case 'D':
    202       1.1  nonaka 				bootconf.menuformat = MENUFORMAT_NUMBER;
    203       1.1  nonaka 				break;
    204       1.1  nonaka 
    205       1.1  nonaka 			case 'l':
    206       1.1  nonaka 			case 'L':
    207       1.1  nonaka 				bootconf.menuformat = MENUFORMAT_LETTER;
    208       1.1  nonaka 				break;
    209       1.1  nonaka 			}
    210       1.1  nonaka 		} else if (!strncmp(key, "clear", 5)) {
    211       1.1  nonaka 			bootconf.clear = !!atoi(value);
    212       1.1  nonaka 		}
    213       1.1  nonaka 	}
    214       1.1  nonaka 	switch (bootconf.menuformat) {
    215       1.1  nonaka 	case MENUFORMAT_AUTO:
    216       1.1  nonaka 		if (cmenu > 9 && bootconf.timeout > 0)
    217       1.1  nonaka 			bootconf.menuformat = MENUFORMAT_LETTER;
    218       1.1  nonaka 		else
    219       1.1  nonaka 			bootconf.menuformat = MENUFORMAT_NUMBER;
    220       1.1  nonaka 		break;
    221       1.1  nonaka 
    222       1.1  nonaka 	case MENUFORMAT_NUMBER:
    223       1.1  nonaka 		if (cmenu > 9 && bootconf.timeout > 0)
    224       1.1  nonaka 			cmenu = 9;
    225       1.1  nonaka 		break;
    226       1.1  nonaka 	}
    227       1.1  nonaka 
    228       1.1  nonaka 	bootconf.nummenu = cmenu;
    229       1.1  nonaka 	if (bootconf.def < 0)
    230       1.1  nonaka 		bootconf.def = 0;
    231       1.1  nonaka 	if (bootconf.def >= cmenu)
    232       1.1  nonaka 		bootconf.def = cmenu - 1;
    233       1.1  nonaka }
    234       1.1  nonaka 
    235       1.1  nonaka /*
    236       1.1  nonaka  * doboottypemenu will render the menu and parse any user input
    237       1.1  nonaka  */
    238       1.1  nonaka static int
    239       1.1  nonaka getchoicefrominput(char *input, int def)
    240       1.1  nonaka {
    241       1.1  nonaka 	int choice = -1;
    242       1.1  nonaka 
    243       1.1  nonaka 	if (*input == '\0' || *input == '\r' || *input == '\n')
    244       1.1  nonaka 		choice = def;
    245       1.1  nonaka 	else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
    246       1.1  nonaka 		choice = (*input) - 'A';
    247       1.1  nonaka 	else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
    248       1.1  nonaka 		choice = (*input) - 'a';
    249       1.1  nonaka 	else if (isnum(*input)) {
    250       1.1  nonaka 		choice = atoi(input) - 1;
    251       1.1  nonaka 		if (choice < 0 || choice >= bootconf.nummenu)
    252       1.1  nonaka 			choice = -1;
    253       1.1  nonaka 	}
    254       1.1  nonaka 	return choice;
    255       1.1  nonaka }
    256       1.1  nonaka 
    257       1.1  nonaka void
    258       1.1  nonaka doboottypemenu(void)
    259       1.1  nonaka {
    260       1.1  nonaka 	char input[80], *ic, *oc;
    261       1.1  nonaka 	int choice;
    262       1.1  nonaka 
    263       1.1  nonaka 	printf("\n");
    264       1.1  nonaka 	/* Display menu */
    265       1.1  nonaka 	if (bootconf.menuformat == MENUFORMAT_LETTER) {
    266       1.1  nonaka 		for (choice = 0; choice < bootconf.nummenu; choice++)
    267       1.1  nonaka 			printf("    %c. %s\n", choice + 'A',
    268       1.1  nonaka 			    bootconf.desc[choice]);
    269       1.1  nonaka 	} else {
    270       1.1  nonaka 		/* Can't use %2d format string with libsa */
    271       1.1  nonaka 		for (choice = 0; choice < bootconf.nummenu; choice++)
    272       1.1  nonaka 			printf("    %s%d. %s\n",
    273       1.1  nonaka 			    (choice < 9) ?  " " : "",
    274       1.1  nonaka 			    choice + 1,
    275       1.1  nonaka 			    bootconf.desc[choice]);
    276       1.1  nonaka 	}
    277       1.1  nonaka 	choice = -1;
    278       1.1  nonaka 	for (;;) {
    279       1.1  nonaka 		input[0] = '\0';
    280       1.1  nonaka 
    281       1.1  nonaka 		if (bootconf.timeout < 0) {
    282       1.1  nonaka 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    283       1.1  nonaka 				printf("\nOption: [%c]:",
    284       1.1  nonaka 				    bootconf.def + 'A');
    285       1.1  nonaka 			else
    286       1.1  nonaka 				printf("\nOption: [%d]:",
    287       1.1  nonaka 				    bootconf.def + 1);
    288       1.1  nonaka 
    289       1.1  nonaka 			gets(input);
    290       1.1  nonaka 			choice = getchoicefrominput(input, bootconf.def);
    291       1.1  nonaka 		} else if (bootconf.timeout == 0)
    292       1.1  nonaka 			choice = bootconf.def;
    293       1.1  nonaka 		else  {
    294       1.1  nonaka 			printf("\nChoose an option; RETURN for default; "
    295       1.1  nonaka 			       "SPACE to stop countdown.\n");
    296       1.1  nonaka 			if (bootconf.menuformat == MENUFORMAT_LETTER)
    297       1.1  nonaka 				printf("Option %c will be chosen in ",
    298       1.1  nonaka 				    bootconf.def + 'A');
    299       1.1  nonaka 			else
    300       1.1  nonaka 				printf("Option %d will be chosen in ",
    301       1.1  nonaka 				    bootconf.def + 1);
    302       1.1  nonaka 			input[0] = awaitkey(bootconf.timeout, 1);
    303       1.1  nonaka 			input[1] = '\0';
    304       1.1  nonaka 			choice = getchoicefrominput(input, bootconf.def);
    305       1.1  nonaka 			/* If invalid key pressed, drop to menu */
    306       1.1  nonaka 			if (choice == -1)
    307       1.1  nonaka 				bootconf.timeout = -1;
    308       1.1  nonaka 		}
    309       1.1  nonaka 		if (choice < 0)
    310       1.1  nonaka 			continue;
    311       1.1  nonaka 		if (!strcmp(bootconf.command[choice], "prompt")) {
    312       1.1  nonaka 			printf("type \"?\" or \"help\" for help.\n");
    313       1.1  nonaka 			bootmenu(); /* does not return */
    314       1.1  nonaka 		} else {
    315       1.1  nonaka 			ic = bootconf.command[choice];
    316       1.1  nonaka 			/* Split command string at ; into separate commands */
    317       1.1  nonaka 			do {
    318       1.1  nonaka 				oc = input;
    319       1.1  nonaka 				/* Look for ; separator */
    320       1.1  nonaka 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    321       1.1  nonaka 					*oc++ = *ic;
    322       1.1  nonaka 				if (*input == '\0')
    323       1.1  nonaka 					continue;
    324       1.1  nonaka 				/* Strip out any trailing spaces */
    325       1.1  nonaka 				oc--;
    326       1.1  nonaka 				for (; *oc == ' ' && oc > input; oc--);
    327       1.1  nonaka 				*++oc = '\0';
    328       1.1  nonaka 				if (*ic == COMMAND_SEPARATOR)
    329       1.1  nonaka 					ic++;
    330       1.1  nonaka 				/* Stop silly command strings like ;;; */
    331       1.1  nonaka 				if (*input != '\0')
    332       1.1  nonaka 					docommand(input);
    333       1.1  nonaka 				/* Skip leading spaces */
    334       1.1  nonaka 				for (; *ic == ' '; ic++);
    335       1.1  nonaka 			} while (*ic);
    336       1.1  nonaka 		}
    337       1.1  nonaka 	}
    338       1.1  nonaka }
    339