bootmenu.c revision 1.14 1 /* $NetBSD: bootmenu.c,v 1.14 2014/08/10 07:40:49 isaki 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/bootcfg.h>
37 #include <lib/libsa/ufs.h>
38 #include <lib/libkern/libkern.h>
39
40 #include <libi386.h>
41 #include <bootmenu.h>
42
43 static void docommandchoice(int);
44
45 extern struct x86_boot_params boot_params;
46 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
47
48 #define MENUFORMAT_AUTO 0
49 #define MENUFORMAT_NUMBER 1
50 #define MENUFORMAT_LETTER 2
51
52 /*
53 * XXX
54 * if module_add, userconf_add are strictly mi they can be folded back
55 * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
56 */
57 static void
58 do_bootcfg_command(const char *cmd, char *arg)
59 {
60 if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
61 module_add(arg);
62 else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
63 userconf_add(arg);
64 }
65
66 void
67 parsebootconf(const char *conf)
68 {
69 perform_bootcfg(conf, &do_bootcfg_command, 32768);
70 }
71
72 /*
73 * doboottypemenu will render the menu and parse any user input
74 */
75 static int
76 getchoicefrominput(char *input, int def)
77 {
78 int choice, usedef;
79
80 choice = -1;
81 usedef = 0;
82
83 if (*input == '\0' || *input == '\r' || *input == '\n') {
84 choice = def;
85 usedef = 1;
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)
93 choice = -1;
94 }
95
96 if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
97 !isdigit(*input) && !usedef)
98 choice = -1;
99
100 return choice;
101 }
102
103 static void
104 docommandchoice(int choice)
105 {
106 char input[80], *ic, *oc;
107
108 ic = bootcfg_info.command[choice];
109 /* Split command string at ; into separate commands */
110 do {
111 oc = input;
112 /* Look for ; separator */
113 for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
114 *oc++ = *ic;
115 if (*input == '\0')
116 continue;
117 /* Strip out any trailing spaces */
118 oc--;
119 for (; *oc == ' ' && oc > input; oc--);
120 *++oc = '\0';
121 if (*ic == COMMAND_SEPARATOR)
122 ic++;
123 /* Stop silly command strings like ;;; */
124 if (*input != '\0')
125 docommand(input);
126 /* Skip leading spaces */
127 for (; *ic == ' '; ic++);
128 } while (*ic);
129 }
130
131 void
132 bootdefault(void)
133 {
134 int choice;
135 static int entered;
136
137 if (bootcfg_info.nummenu > 0) {
138 if (entered) {
139 printf("default boot twice, skipping...\n");
140 return;
141 }
142 entered = 1;
143 choice = bootcfg_info.def;
144 printf("command(s): %s\n", bootcfg_info.command[choice]);
145 docommandchoice(choice);
146 }
147 }
148
149 __dead void
150 doboottypemenu(void)
151 {
152 int choice;
153 char input[80];
154
155 printf("\n");
156 /* Display menu */
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]);
161 } else {
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) ? " " : "",
166 choice + 1,
167 bootcfg_info.desc[choice]);
168 }
169 choice = -1;
170 for (;;) {
171 input[0] = '\0';
172
173 if (bootcfg_info.timeout < 0) {
174 if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
175 printf("\nOption: [%c]:",
176 bootcfg_info.def + 'A');
177 else
178 printf("\nOption: [%d]:",
179 bootcfg_info.def + 1);
180
181 gets(input);
182 choice = getchoicefrominput(input, bootcfg_info.def);
183 } else if (bootcfg_info.timeout == 0)
184 choice = bootcfg_info.def;
185 else {
186 printf("\nChoose an option; RETURN for default; "
187 "SPACE to stop countdown.\n");
188 if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
189 printf("Option %c will be chosen in ",
190 bootcfg_info.def + 'A');
191 else
192 printf("Option %d will be chosen in ",
193 bootcfg_info.def + 1);
194 input[0] = awaitkey(bootcfg_info.timeout, 1);
195 input[1] = '\0';
196 choice = getchoicefrominput(input, bootcfg_info.def);
197 /* If invalid key pressed, drop to menu */
198 if (choice == -1)
199 bootcfg_info.timeout = -1;
200 }
201 if (choice < 0)
202 continue;
203 if (!strcmp(bootcfg_info.command[choice], "prompt") &&
204 ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
205 check_password((char *)boot_params.bp_password))) {
206 printf("type \"?\" or \"help\" for help.\n");
207 bootmenu(); /* does not return */
208 } else {
209 docommandchoice(choice);
210 }
211
212 }
213 }
214
215 #endif /* !SMALL */
216