1 /* $NetBSD: bootmenu.c,v 1.14 2014/08/10 07:40:49 isaki 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.
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/bootcfg.h>
37 #include <lib/libsa/ufs.h>
38 #include <lib/libkern/libkern.h>
43 static void docommandchoice(int);
45 extern struct x86_boot_params boot_params
;
46 extern const char bootprog_name
[], bootprog_rev
[], bootprog_kernrev
[];
48 #define MENUFORMAT_AUTO 0
49 #define MENUFORMAT_NUMBER 1
50 #define MENUFORMAT_LETTER 2
54 * if module_add, userconf_add are strictly mi they can be folded back
55 * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
58 do_bootcfg_command(const char *cmd
, char *arg
)
60 if (strcmp(cmd
, BOOTCFG_CMD_LOAD
) == 0)
62 else if (strcmp(cmd
, BOOTCFG_CMD_USERCONF
) == 0)
67 parsebootconf(const char *conf
)
69 perform_bootcfg(conf
, &do_bootcfg_command
, 32768);
73 * doboottypemenu will render the menu and parse any user input
76 getchoicefrominput(char *input
, int def
)
83 if (*input
== '\0' || *input
== '\r' || *input
== '\n') {
86 } else if (*input
>= 'A' && *input
< bootcfg_info
.nummenu
+ 'A')
87 choice
= (*input
) - 'A';
88 else if (*input
>= 'a' && *input
< bootcfg_info
.nummenu
+ 'a')
89 choice
= (*input
) - 'a';
90 else if (isdigit(*input
)) {
91 choice
= atoi(input
) - 1;
92 if (choice
< 0 || choice
>= bootcfg_info
.nummenu
)
96 if (bootcfg_info
.menuformat
!= MENUFORMAT_LETTER
&&
97 !isdigit(*input
) && !usedef
)
104 docommandchoice(int choice
)
106 char input
[80], *ic
, *oc
;
108 ic
= bootcfg_info
.command
[choice
];
109 /* Split command string at ; into separate commands */
112 /* Look for ; separator */
113 for (; *ic
&& *ic
!= COMMAND_SEPARATOR
; ic
++)
117 /* Strip out any trailing spaces */
119 for (; *oc
== ' ' && oc
> input
; oc
--);
121 if (*ic
== COMMAND_SEPARATOR
)
123 /* Stop silly command strings like ;;; */
126 /* Skip leading spaces */
127 for (; *ic
== ' '; ic
++);
137 if (bootcfg_info
.nummenu
> 0) {
139 printf("default boot twice, skipping...\n");
143 choice
= bootcfg_info
.def
;
144 printf("command(s): %s\n", bootcfg_info
.command
[choice
]);
145 docommandchoice(choice
);
157 if (bootcfg_info
.menuformat
== MENUFORMAT_LETTER
) {
158 for (choice
= 0; choice
< bootcfg_info
.nummenu
; choice
++)
159 printf(" %c. %s\n", choice
+ 'A',
160 bootcfg_info
.desc
[choice
]);
162 /* Can't use %2d format string with libsa */
163 for (choice
= 0; choice
< bootcfg_info
.nummenu
; choice
++)
164 printf(" %s%d. %s\n",
165 (choice
< 9) ? " " : "",
167 bootcfg_info
.desc
[choice
]);
170 #endif /* defined(__minix) */
175 #if !defined(__minix)
181 if (bootcfg_info
.menuformat
== MENUFORMAT_LETTER
) {
182 for (choice
= 0; choice
< bootcfg_info
.nummenu
; choice
++)
183 printf(" %c. %s\n", choice
+ 'A',
184 bootcfg_info
.desc
[choice
]);
186 /* Can't use %2d format string with libsa */
187 for (choice
= 0; choice
< bootcfg_info
.nummenu
; choice
++)
188 printf(" %s%d. %s\n",
189 (choice
< 9) ? " " : "",
191 bootcfg_info
.desc
[choice
]);
195 char input
[256], *ic
, *oc
;
196 #endif /* !defined(__minix) */
199 #endif /* defined(__minix) */
203 #endif /* defined(__minix) */
207 if (bootcfg_info
.timeout
< 0) {
208 if (bootcfg_info
.menuformat
== MENUFORMAT_LETTER
)
209 #if !defined(__minix)
210 printf("\nOption: [%c]:",
212 printf("\nOption%s: [%c]:",
213 editing
? " (edit)" : "",
214 #endif /* !defined(__minix) */
215 bootcfg_info
.def
+ 'A');
217 #if !defined(__minix)
218 printf("\nOption: [%d]:",
220 printf("\nOption%s: [%d]:",
221 editing
? " (edit)" : "",
222 #endif /* !defined(__minix) */
223 bootcfg_info
.def
+ 1);
225 #if !defined(__minix)
228 editline(input
, sizeof(input
), NULL
);
229 #endif /* !defined(__minix) */
230 choice
= getchoicefrominput(input
, bootcfg_info
.def
);
231 } else if (bootcfg_info
.timeout
== 0)
232 choice
= bootcfg_info
.def
;
234 printf("\nChoose an option; RETURN for default; "
235 "SPACE to stop countdown.\n");
236 if (bootcfg_info
.menuformat
== MENUFORMAT_LETTER
)
237 printf("Option %c will be chosen in ",
238 bootcfg_info
.def
+ 'A');
240 printf("Option %d will be chosen in ",
241 bootcfg_info
.def
+ 1);
242 input
[0] = awaitkey(bootcfg_info
.timeout
, 1);
244 choice
= getchoicefrominput(input
, bootcfg_info
.def
);
245 /* If invalid key pressed, drop to menu */
247 bootcfg_info
.timeout
= -1;
251 #if !defined(__minix)
252 if (!strcmp(bootcfg_info
.command
[choice
], "prompt") &&
253 ((boot_params
.bp_flags
& X86_BP_FLAGS_PASSWORD
) == 0 ||
254 check_password((char *)boot_params
.bp_password
))) {
255 printf("type \"?\" or \"help\" for help.\n");
256 bootmenu(); /* does not return */
258 docommandchoice(choice
);
261 ic
= bootcfg_info
.command
[choice
];
264 editline(input
, sizeof(input
), ic
);
267 if (!strcmp(ic
, "edit") &&
268 ((boot_params
.bp_flags
& X86_BP_FLAGS_PASSWORD
) == 0 ||
269 check_password((char *)boot_params
.bp_password
))) {
271 bootcfg_info
.timeout
= -1;
272 } else if (!strcmp(ic
, "prompt") &&
273 ((boot_params
.bp_flags
& X86_BP_FLAGS_PASSWORD
) == 0 ||
274 check_password((char *)boot_params
.bp_password
))) {
275 printf("type \"?\" or \"help\" for help, "
276 "or \"menu\" to return to the menu.\n");
280 bootcfg_info
.timeout
= -1;
282 /* Split command string at ; into separate commands */
285 * This must support inline editing, since ic
286 * may also point to input.
289 /* Look for ; separator */
290 for (; *ic
&& *ic
!= COMMAND_SEPARATOR
; ic
++)
292 if (*ic
== COMMAND_SEPARATOR
)
296 /* Strip out any trailing spaces */
298 for (; *oc
== ' ' && oc
> input
; oc
--);
300 /* Stop silly command strings like ;;; */
303 /* Skip leading spaces */
304 for (; *ic
== ' '; ic
++);
307 #endif /* !defined(__minix) */