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