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