1 /* $NetBSD: bootmenu.c,v 1.4 2008/12/14 18:46:33 christos Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 #include <sys/types.h>
30 #include <sys/reboot.h>
31 #include <sys/bootblock.h>
36 #include "pathnames.h"
38 #define isnum(c) ((c) >= '0' && (c) <= '9')
40 #define MENUFORMAT_AUTO 0
41 #define MENUFORMAT_NUMBER 1
42 #define MENUFORMAT_LETTER 2
44 struct bootconf_def bootconf
;
56 for (; isnum(*c
); c
++)
57 ret
= (ret
* 10) + (*c
- '0');
59 return (*in
== '-') ? -ret
: ret
;
63 * This function parses a boot.cfg file in the root of the filesystem
64 * (if present) and populates the global boot configuration.
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
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
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
91 parsebootconf(const char *conf
)
94 int cmenu
, cbanner
, len
;
97 char *key
, *value
, *v2
;
99 /* Clear bootconf structure */
100 memset(&bootconf
, 0, sizeof(bootconf
));
102 /* Set timeout to configured */
103 bootconf
.timeout
= default_timeout
;
105 /* automatically switch between letter and numbers on menu */
106 bootconf
.menuformat
= MENUFORMAT_AUTO
;
108 fd
= open(_PATH_BOOTCONF
, 0);
112 err
= fstat(fd
, &st
);
118 bc
= alloc(st
.st_size
+ 1);
120 printf("Could not allocate memory for boot configuration\n");
126 len
= read(fd
, bc
+ off
, 1024);
134 /* bc now contains the whole boot.cfg file */
138 for (c
= bc
; *c
; c
++) {
140 /* Look for = separator between key and value */
141 for (; *c
&& *c
!= '='; c
++)
144 break; /* break if at end of data */
146 /* zero terminate key which points to keyword */
149 /* Look for end of line (or file) and zero terminate value */
150 for (; *c
&& *c
!= '\n'; c
++)
154 if (!strncmp(key
, "menu", 4)) {
156 * Parse "menu=<description>:<command>". If the
157 * description is empty ("menu=:<command>)",
158 * then re-use the command as the description.
159 * Note that the command may contain embedded
162 if (cmenu
>= MAXMENU
)
164 bootconf
.desc
[cmenu
] = value
;
165 for (v2
= value
; *v2
&& *v2
!= ':'; v2
++)
169 bootconf
.command
[cmenu
] = v2
;
171 bootconf
.desc
[cmenu
] = v2
;
174 /* No delimiter means invalid line */
175 bootconf
.desc
[cmenu
] = NULL
;
177 } else if (!strncmp(key
, "banner", 6)) {
178 if (cbanner
< MAXBANNER
)
179 bootconf
.banner
[cbanner
++] = value
;
180 } else if (!strncmp(key
, "timeout", 7)) {
182 bootconf
.timeout
= -1;
184 bootconf
.timeout
= atoi(value
);
185 } else if (!strncmp(key
, "default", 7)) {
186 bootconf
.def
= atoi(value
) - 1;
187 } else if (!strncmp(key
, "consdev", 7)) {
188 bootconf
.consdev
= value
;
189 } else if (!strncmp(key
, "format", 6)) {
190 printf("value:%c\n", *value
);
194 bootconf
.menuformat
= MENUFORMAT_AUTO
;
201 bootconf
.menuformat
= MENUFORMAT_NUMBER
;
206 bootconf
.menuformat
= MENUFORMAT_LETTER
;
209 } else if (!strncmp(key
, "clear", 5)) {
210 bootconf
.clear
= !!atoi(value
);
213 switch (bootconf
.menuformat
) {
214 case MENUFORMAT_AUTO
:
215 if (cmenu
> 9 && bootconf
.timeout
> 0)
216 bootconf
.menuformat
= MENUFORMAT_LETTER
;
218 bootconf
.menuformat
= MENUFORMAT_NUMBER
;
221 case MENUFORMAT_NUMBER
:
222 if (cmenu
> 9 && bootconf
.timeout
> 0)
227 bootconf
.nummenu
= cmenu
;
228 if (bootconf
.def
< 0)
230 if (bootconf
.def
>= cmenu
)
231 bootconf
.def
= cmenu
- 1;
235 * doboottypemenu will render the menu and parse any user input
238 getchoicefrominput(char *input
, int def
)
242 if (*input
== '\0' || *input
== '\r' || *input
== '\n')
244 else if (*input
>= 'A' && *input
< bootconf
.nummenu
+ 'A')
245 choice
= (*input
) - 'A';
246 else if (*input
>= 'a' && *input
< bootconf
.nummenu
+ 'a')
247 choice
= (*input
) - 'a';
248 else if (isnum(*input
)) {
249 choice
= atoi(input
) - 1;
250 if (choice
< 0 || choice
>= bootconf
.nummenu
)
259 char input
[80], *ic
, *oc
;
264 if (bootconf
.menuformat
== MENUFORMAT_LETTER
) {
265 for (choice
= 0; choice
< bootconf
.nummenu
; choice
++)
266 printf(" %c. %s\n", choice
+ 'A',
267 bootconf
.desc
[choice
]);
269 /* Can't use %2d format string with libsa */
270 for (choice
= 0; choice
< bootconf
.nummenu
; choice
++)
271 printf(" %s%d. %s\n",
272 (choice
< 9) ? " " : "",
274 bootconf
.desc
[choice
]);
280 if (bootconf
.timeout
< 0) {
281 if (bootconf
.menuformat
== MENUFORMAT_LETTER
)
282 printf("\nOption: [%c]:",
285 printf("\nOption: [%d]:",
289 choice
= getchoicefrominput(input
, bootconf
.def
);
290 } else if (bootconf
.timeout
== 0)
291 choice
= bootconf
.def
;
293 printf("\nChoose an option; RETURN for default; "
294 "SPACE to stop countdown.\n");
295 if (bootconf
.menuformat
== MENUFORMAT_LETTER
)
296 printf("Option %c will be chosen in ",
299 printf("Option %d will be chosen in ",
301 input
[0] = awaitkey(bootconf
.timeout
, 1);
303 choice
= getchoicefrominput(input
, bootconf
.def
);
304 /* If invalid key pressed, drop to menu */
306 bootconf
.timeout
= -1;
310 if (!strcmp(bootconf
.command
[choice
], "prompt")) {
311 printf("type \"?\" or \"help\" for help.\n");
312 bootmenu(); /* does not return */
314 ic
= bootconf
.command
[choice
];
315 /* Split command string at ; into separate commands */
318 /* Look for ; separator */
319 for (; *ic
&& *ic
!= COMMAND_SEPARATOR
; ic
++)
323 /* Strip out any trailing spaces */
325 for (; *oc
== ' ' && oc
> input
; oc
--);
327 if (*ic
== COMMAND_SEPARATOR
)
329 /* Stop silly command strings like ;;; */
332 /* Skip leading spaces */
333 for (; *ic
== ' '; ic
++);