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