unstack - fix ipcvecs
[minix.git] / sys / arch / i386 / stand / lib / bootmenu.c
blobf2e0a4dae947ccd1bba4c37d72bfbe12dc425b26
1 /* $NetBSD: bootmenu.c,v 1.10 2011/08/18 13:20:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
29 #ifndef SMALL
31 #include <sys/types.h>
32 #include <sys/reboot.h>
33 #include <sys/bootblock.h>
35 #include <lib/libsa/stand.h>
36 #include <lib/libsa/ufs.h>
37 #include <lib/libkern/libkern.h>
39 #include <libi386.h>
40 #include <bootmenu.h>
42 #define isnum(c) ((c) >= '0' && (c) <= '9')
44 extern struct x86_boot_params boot_params;
45 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
47 #define MENUFORMAT_AUTO 0
48 #define MENUFORMAT_NUMBER 1
49 #define MENUFORMAT_LETTER 2
51 struct bootconf_def bootconf;
53 int
54 atoi(const char *in)
56 char *c;
57 int ret;
59 ret = 0;
60 c = (char *)in;
61 if (*c == '-')
62 c++;
63 for (; isnum(*c); c++)
64 ret = (ret * 10) + (*c - '0');
66 return (*in == '-') ? -ret : ret;
70 * This function parses a boot.cfg file in the root of the filesystem
71 * (if present) and populates the global boot configuration.
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.
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
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
97 void
98 parsebootconf(const char *conf)
100 char *bc, *c;
101 int cmenu, cbanner, len;
102 int fd, err, off;
103 struct stat st;
104 char *next, *key, *value, *v2;
106 /* Clear bootconf structure */
107 memset((void *)&bootconf, 0, sizeof(bootconf));
109 /* Set timeout to configured */
110 bootconf.timeout = boot_params.bp_timeout;
112 /* automatically switch between letter and numbers on menu */
113 bootconf.menuformat = MENUFORMAT_AUTO;
115 fd = open(BOOTCONF, 0);
116 if (fd < 0)
117 return;
119 err = fstat(fd, &st);
120 if (err == -1) {
121 close(fd);
122 return;
126 * Check the size. A bootconf file is normally only a few
127 * hundred bytes long. If it is much bigger than expected,
128 * don't try to load it. We can't load something big into
129 * an 8086 real mode segment anyway, and in pxeboot this is
130 * probably a case of the loader getting a filename for the
131 * kernel and thinking it is boot.cfg by accident. (The 32k
132 * number is arbitrary but 8086 real mode data segments max
133 * out at 64k.)
135 if (st.st_size > 32768) {
136 close(fd);
137 return;
140 bc = alloc(st.st_size + 1);
141 if (bc == NULL) {
142 printf("Could not allocate memory for boot configuration\n");
143 return;
146 off = 0;
147 do {
148 len = read(fd, bc + off, 1024);
149 if (len <= 0)
150 break;
151 off += len;
152 } while (len > 0);
153 bc[off] = '\0';
155 close(fd);
156 /* bc now contains the whole boot.cfg file */
158 cmenu = 0;
159 cbanner = 0;
160 for (c = bc; *c; c = next) {
161 key = c;
162 /* find end of line */
163 for (; *c && *c != '\n'; c++)
164 /* zero terminate line on start of comment */
165 if (*c == '#')
166 *c = 0;
167 /* zero terminate line */
168 if (*(next = c))
169 *next++ = 0;
170 /* Look for = separator between key and value */
171 for (c = key; *c && *c != '='; c++)
172 continue;
173 /* Ignore lines with no key=value pair */
174 if (*c == '\0')
175 continue;
177 /* zero terminate key which points to keyword */
178 *c++ = 0;
179 value = c;
180 /* Look for end of line (or file) and zero terminate value */
181 for (; *c && *c != '\n'; c++)
182 continue;
183 *c = 0;
185 if (!strncmp(key, "menu", 4)) {
187 * Parse "menu=<description>:<command>". If the
188 * description is empty ("menu=:<command>)",
189 * then re-use the command as the description.
190 * Note that the command may contain embedded
191 * colons.
193 if (cmenu >= MAXMENU)
194 continue;
195 bootconf.desc[cmenu] = value;
196 for (v2 = value; *v2 && *v2 != ':'; v2++)
197 continue;
198 if (*v2) {
199 *v2++ = 0;
200 bootconf.command[cmenu] = v2;
201 if (! *value)
202 bootconf.desc[cmenu] = v2;
203 cmenu++;
204 } else {
205 /* No delimiter means invalid line */
206 bootconf.desc[cmenu] = NULL;
208 } else if (!strncmp(key, "banner", 6)) {
209 if (cbanner < MAXBANNER)
210 bootconf.banner[cbanner++] = value;
211 } else if (!strncmp(key, "timeout", 7)) {
212 if (!isnum(*value))
213 bootconf.timeout = -1;
214 else
215 bootconf.timeout = atoi(value);
216 } else if (!strncmp(key, "default", 7)) {
217 bootconf.def = atoi(value) - 1;
218 } else if (!strncmp(key, "consdev", 7)) {
219 bootconf.consdev = value;
220 } else if (!strncmp(key, "load", 4)) {
221 module_add(value);
222 } else if (!strncmp(key, "format", 6)) {
223 printf("value:%c\n", *value);
224 switch (*value) {
225 case 'a':
226 case 'A':
227 bootconf.menuformat = MENUFORMAT_AUTO;
228 break;
230 case 'n':
231 case 'N':
232 case 'd':
233 case 'D':
234 bootconf.menuformat = MENUFORMAT_NUMBER;
235 break;
237 case 'l':
238 case 'L':
239 bootconf.menuformat = MENUFORMAT_LETTER;
240 break;
242 } else if (!strncmp(key, "clear", 5)) {
243 bootconf.clear = !!atoi(value);
244 } else if (!strncmp(key, "userconf", 8)) {
245 userconf_add(value);
248 switch (bootconf.menuformat) {
249 case MENUFORMAT_AUTO:
250 if (cmenu > 9 && bootconf.timeout > 0)
251 bootconf.menuformat = MENUFORMAT_LETTER;
252 else
253 bootconf.menuformat = MENUFORMAT_NUMBER;
254 break;
256 case MENUFORMAT_NUMBER:
257 if (cmenu > 9 && bootconf.timeout > 0)
258 cmenu = 9;
259 break;
262 bootconf.nummenu = cmenu;
263 if (bootconf.def < 0)
264 bootconf.def = 0;
265 if (bootconf.def >= cmenu)
266 bootconf.def = cmenu - 1;
270 * doboottypemenu will render the menu and parse any user input
272 static int
273 getchoicefrominput(char *input, int def)
275 int choice, usedef;
277 choice = -1;
278 usedef = 0;
280 if (*input == '\0' || *input == '\r' || *input == '\n') {
281 choice = def;
282 usedef = 1;
283 } else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
284 choice = (*input) - 'A';
285 else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
286 choice = (*input) - 'a';
287 else if (isnum(*input)) {
288 choice = atoi(input) - 1;
289 if (choice < 0 || choice >= bootconf.nummenu)
290 choice = -1;
293 if (bootconf.menuformat != MENUFORMAT_LETTER &&
294 !isnum(*input) && !usedef)
295 choice = -1;
297 return choice;
300 static void
301 showmenu(void)
303 int choice;
305 printf("\n");
306 /* Display menu */
307 if (bootconf.menuformat == MENUFORMAT_LETTER) {
308 for (choice = 0; choice < bootconf.nummenu; choice++)
309 printf(" %c. %s\n", choice + 'A',
310 bootconf.desc[choice]);
311 } else {
312 /* Can't use %2d format string with libsa */
313 for (choice = 0; choice < bootconf.nummenu; choice++)
314 printf(" %s%d. %s\n",
315 (choice < 9) ? " " : "",
316 choice + 1,
317 bootconf.desc[choice]);
321 void
322 doboottypemenu(void)
324 int choice, editing;
325 char input[256], *ic, *oc;
327 showmenu();
328 choice = -1;
329 editing = 0;
330 for (;;) {
331 input[0] = '\0';
333 if (bootconf.timeout < 0) {
334 if (bootconf.menuformat == MENUFORMAT_LETTER)
335 printf("\nOption%s: [%c]:",
336 editing ? " (edit)" : "",
337 bootconf.def + 'A');
338 else
339 printf("\nOption%s: [%d]:",
340 editing ? " (edit)" : "",
341 bootconf.def + 1);
343 editline(input, sizeof(input), NULL);
344 choice = getchoicefrominput(input, bootconf.def);
345 } else if (bootconf.timeout == 0)
346 choice = bootconf.def;
347 else {
348 printf("\nChoose an option; RETURN for default; "
349 "SPACE to stop countdown.\n");
350 if (bootconf.menuformat == MENUFORMAT_LETTER)
351 printf("Option %c will be chosen in ",
352 bootconf.def + 'A');
353 else
354 printf("Option %d will be chosen in ",
355 bootconf.def + 1);
356 input[0] = awaitkey(bootconf.timeout, 1);
357 input[1] = '\0';
358 choice = getchoicefrominput(input, bootconf.def);
359 /* If invalid key pressed, drop to menu */
360 if (choice == -1)
361 bootconf.timeout = -1;
363 if (choice < 0)
364 continue;
365 ic = bootconf.command[choice];
366 if (editing) {
367 printf("> ");
368 editline(input, sizeof(input), ic);
369 ic = input;
371 if (!strcmp(ic, "edit") &&
372 ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
373 check_password((char *)boot_params.bp_password))) {
374 editing = 1;
375 bootconf.timeout = -1;
376 } else if (!strcmp(ic, "prompt") &&
377 ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
378 check_password((char *)boot_params.bp_password))) {
379 printf("type \"?\" or \"help\" for help, "
380 "or \"menu\" to return to the menu.\n");
381 prompt(1);
382 showmenu();
383 editing = 0;
384 bootconf.timeout = -1;
385 } else {
386 /* Split command string at ; into separate commands */
387 do {
389 * This must support inline editing, since ic
390 * may also point to input.
392 oc = input;
393 /* Look for ; separator */
394 for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
395 *oc++ = *ic;
396 if (*ic == COMMAND_SEPARATOR)
397 ic++;
398 if (oc == input)
399 continue;
400 /* Strip out any trailing spaces */
401 oc--;
402 for (; *oc == ' ' && oc > input; oc--);
403 *++oc = '\0';
404 /* Stop silly command strings like ;;; */
405 if (*input != '\0')
406 docommand(input);
407 /* Skip leading spaces */
408 for (; *ic == ' '; ic++);
409 } while (*ic);
415 #endif /* !SMALL */