bootmenu.c revision 1.4.4.3 1 1.4.4.3 skrll /* $NetBSD: bootmenu.c,v 1.4.4.3 2009/04/28 07:34:14 skrll Exp $ */
2 1.4.4.2 skrll
3 1.4.4.2 skrll /*-
4 1.4.4.2 skrll * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.4.4.2 skrll * All rights reserved.
6 1.4.4.2 skrll *
7 1.4.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.4.4.2 skrll * modification, are permitted provided that the following conditions
9 1.4.4.2 skrll * are met:
10 1.4.4.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.4.4.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.4.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.4.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.4.4.2 skrll * documentation and/or other materials provided with the distribution.
15 1.4.4.2 skrll *
16 1.4.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.4.4.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.4.4.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.4.4.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.4.4.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.4.4.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.4.4.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.4.4.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.4.4.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.4.4.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.4.4.2 skrll * POSSIBILITY OF SUCH DAMAGE.
27 1.4.4.2 skrll */
28 1.4.4.2 skrll
29 1.4.4.2 skrll #ifndef SMALL
30 1.4.4.2 skrll
31 1.4.4.2 skrll #include <sys/types.h>
32 1.4.4.2 skrll #include <sys/reboot.h>
33 1.4.4.2 skrll #include <sys/bootblock.h>
34 1.4.4.2 skrll
35 1.4.4.2 skrll #include <lib/libsa/stand.h>
36 1.4.4.2 skrll #include <lib/libsa/ufs.h>
37 1.4.4.2 skrll #include <lib/libkern/libkern.h>
38 1.4.4.2 skrll
39 1.4.4.2 skrll #include <libi386.h>
40 1.4.4.2 skrll #include <bootmenu.h>
41 1.4.4.2 skrll
42 1.4.4.2 skrll #define isnum(c) ((c) >= '0' && (c) <= '9')
43 1.4.4.2 skrll
44 1.4.4.2 skrll extern struct x86_boot_params boot_params;
45 1.4.4.2 skrll extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
46 1.4.4.2 skrll
47 1.4.4.2 skrll #define MENUFORMAT_AUTO 0
48 1.4.4.2 skrll #define MENUFORMAT_NUMBER 1
49 1.4.4.2 skrll #define MENUFORMAT_LETTER 2
50 1.4.4.2 skrll
51 1.4.4.2 skrll struct bootconf_def bootconf;
52 1.4.4.2 skrll
53 1.4.4.2 skrll int
54 1.4.4.2 skrll atoi(const char *in)
55 1.4.4.2 skrll {
56 1.4.4.2 skrll char *c;
57 1.4.4.2 skrll int ret;
58 1.4.4.2 skrll
59 1.4.4.2 skrll ret = 0;
60 1.4.4.2 skrll c = (char *)in;
61 1.4.4.2 skrll if (*c == '-')
62 1.4.4.2 skrll c++;
63 1.4.4.2 skrll for (; isnum(*c); c++)
64 1.4.4.2 skrll ret = (ret * 10) + (*c - '0');
65 1.4.4.2 skrll
66 1.4.4.2 skrll return (*in == '-') ? -ret : ret;
67 1.4.4.2 skrll }
68 1.4.4.2 skrll
69 1.4.4.2 skrll /*
70 1.4.4.2 skrll * This function parses a boot.cfg file in the root of the filesystem
71 1.4.4.2 skrll * (if present) and populates the global boot configuration.
72 1.4.4.2 skrll *
73 1.4.4.2 skrll * The file consists of a number of lines each terminated by \n
74 1.4.4.2 skrll * The lines are in the format keyword=value. There should not be spaces
75 1.4.4.2 skrll * around the = sign.
76 1.4.4.2 skrll *
77 1.4.4.2 skrll * The recognised keywords are:
78 1.4.4.2 skrll * banner: text displayed instead of the normal welcome text
79 1.4.4.2 skrll * menu: Descriptive text:command to use
80 1.4.4.2 skrll * timeout: Timeout in seconds (overrides that set by installboot)
81 1.4.4.2 skrll * default: the default menu option to use if Return is pressed
82 1.4.4.2 skrll * consdev: the console device to use
83 1.4.4.2 skrll * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
84 1.4.4.2 skrll * clear: whether to clear the screen or not
85 1.4.4.2 skrll *
86 1.4.4.2 skrll * Example boot.cfg file:
87 1.4.4.2 skrll * banner=Welcome to NetBSD
88 1.4.4.2 skrll * banner=Please choose the boot type from the following menu
89 1.4.4.2 skrll * menu=Boot NetBSD:boot netbsd
90 1.4.4.2 skrll * menu=Boot into single user mode:boot netbsd -s
91 1.4.4.2 skrll * menu=:boot hd1a:netbsd -cs
92 1.4.4.2 skrll * menu=Goto boot comand line:prompt
93 1.4.4.2 skrll * timeout=10
94 1.4.4.2 skrll * consdev=com0
95 1.4.4.2 skrll * default=1
96 1.4.4.2 skrll */
97 1.4.4.2 skrll void
98 1.4.4.2 skrll parsebootconf(const char *conf)
99 1.4.4.2 skrll {
100 1.4.4.2 skrll char *bc, *c;
101 1.4.4.2 skrll int cmenu, cbanner, len;
102 1.4.4.2 skrll int fd, err, off;
103 1.4.4.2 skrll struct stat st;
104 1.4.4.2 skrll char *key, *value, *v2;
105 1.4.4.2 skrll
106 1.4.4.2 skrll /* Clear bootconf structure */
107 1.4.4.3 skrll memset((void *)&bootconf, 0, sizeof(bootconf));
108 1.4.4.2 skrll
109 1.4.4.2 skrll /* Set timeout to configured */
110 1.4.4.2 skrll bootconf.timeout = boot_params.bp_timeout;
111 1.4.4.2 skrll
112 1.4.4.2 skrll /* automatically switch between letter and numbers on menu */
113 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_AUTO;
114 1.4.4.2 skrll
115 1.4.4.2 skrll fd = open(BOOTCONF, 0);
116 1.4.4.2 skrll if (fd < 0)
117 1.4.4.2 skrll return;
118 1.4.4.2 skrll
119 1.4.4.2 skrll err = fstat(fd, &st);
120 1.4.4.2 skrll if (err == -1) {
121 1.4.4.2 skrll close(fd);
122 1.4.4.2 skrll return;
123 1.4.4.2 skrll }
124 1.4.4.2 skrll
125 1.4.4.3 skrll /*
126 1.4.4.3 skrll * Check the size. A bootconf file is normally only a few
127 1.4.4.3 skrll * hundred bytes long. If it is much bigger than expected,
128 1.4.4.3 skrll * don't try to load it. We can't load something big into
129 1.4.4.3 skrll * an 8086 real mode segment anyway, and in pxeboot this is
130 1.4.4.3 skrll * probably a case of the loader getting a filename for the
131 1.4.4.3 skrll * kernel and thinking it is boot.cfg by accident. (The 32k
132 1.4.4.3 skrll * number is arbitrary but 8086 real mode data segments max
133 1.4.4.3 skrll * out at 64k.)
134 1.4.4.3 skrll */
135 1.4.4.3 skrll if (st.st_size > 32768) {
136 1.4.4.3 skrll close(fd);
137 1.4.4.3 skrll return;
138 1.4.4.3 skrll }
139 1.4.4.3 skrll
140 1.4.4.2 skrll bc = alloc(st.st_size + 1);
141 1.4.4.2 skrll if (bc == NULL) {
142 1.4.4.2 skrll printf("Could not allocate memory for boot configuration\n");
143 1.4.4.2 skrll return;
144 1.4.4.2 skrll }
145 1.4.4.2 skrll
146 1.4.4.2 skrll off = 0;
147 1.4.4.2 skrll do {
148 1.4.4.2 skrll len = read(fd, bc + off, 1024);
149 1.4.4.2 skrll if (len <= 0)
150 1.4.4.2 skrll break;
151 1.4.4.2 skrll off += len;
152 1.4.4.2 skrll } while (len > 0);
153 1.4.4.2 skrll bc[off] = '\0';
154 1.4.4.2 skrll
155 1.4.4.2 skrll close(fd);
156 1.4.4.2 skrll /* bc now contains the whole boot.cfg file */
157 1.4.4.2 skrll
158 1.4.4.2 skrll cmenu = 0;
159 1.4.4.2 skrll cbanner = 0;
160 1.4.4.2 skrll for (c = bc; *c; c++) {
161 1.4.4.2 skrll key = c;
162 1.4.4.2 skrll /* Look for = separator between key and value */
163 1.4.4.2 skrll for (; *c && *c != '='; c++)
164 1.4.4.2 skrll continue;
165 1.4.4.2 skrll if (*c == '\0')
166 1.4.4.2 skrll break; /* break if at end of data */
167 1.4.4.2 skrll
168 1.4.4.2 skrll /* zero terminate key which points to keyword */
169 1.4.4.2 skrll *c++ = 0;
170 1.4.4.2 skrll value = c;
171 1.4.4.2 skrll /* Look for end of line (or file) and zero terminate value */
172 1.4.4.2 skrll for (; *c && *c != '\n'; c++)
173 1.4.4.2 skrll continue;
174 1.4.4.2 skrll *c = 0;
175 1.4.4.2 skrll
176 1.4.4.2 skrll if (!strncmp(key, "menu", 4)) {
177 1.4.4.2 skrll /*
178 1.4.4.2 skrll * Parse "menu=<description>:<command>". If the
179 1.4.4.2 skrll * description is empty ("menu=:<command>)",
180 1.4.4.2 skrll * then re-use the command as the description.
181 1.4.4.2 skrll * Note that the command may contain embedded
182 1.4.4.2 skrll * colons.
183 1.4.4.2 skrll */
184 1.4.4.2 skrll if (cmenu >= MAXMENU)
185 1.4.4.2 skrll continue;
186 1.4.4.2 skrll bootconf.desc[cmenu] = value;
187 1.4.4.2 skrll for (v2 = value; *v2 && *v2 != ':'; v2++)
188 1.4.4.2 skrll continue;
189 1.4.4.2 skrll if (*v2) {
190 1.4.4.2 skrll *v2++ = 0;
191 1.4.4.2 skrll bootconf.command[cmenu] = v2;
192 1.4.4.2 skrll if (! *value)
193 1.4.4.2 skrll bootconf.desc[cmenu] = v2;
194 1.4.4.2 skrll cmenu++;
195 1.4.4.2 skrll } else {
196 1.4.4.2 skrll /* No delimiter means invalid line */
197 1.4.4.2 skrll bootconf.desc[cmenu] = NULL;
198 1.4.4.2 skrll }
199 1.4.4.2 skrll } else if (!strncmp(key, "banner", 6)) {
200 1.4.4.2 skrll if (cbanner < MAXBANNER)
201 1.4.4.2 skrll bootconf.banner[cbanner++] = value;
202 1.4.4.2 skrll } else if (!strncmp(key, "timeout", 7)) {
203 1.4.4.2 skrll if (!isnum(*value))
204 1.4.4.2 skrll bootconf.timeout = -1;
205 1.4.4.2 skrll else
206 1.4.4.2 skrll bootconf.timeout = atoi(value);
207 1.4.4.2 skrll } else if (!strncmp(key, "default", 7)) {
208 1.4.4.2 skrll bootconf.def = atoi(value) - 1;
209 1.4.4.2 skrll } else if (!strncmp(key, "consdev", 7)) {
210 1.4.4.2 skrll bootconf.consdev = value;
211 1.4.4.2 skrll } else if (!strncmp(key, "load", 4)) {
212 1.4.4.2 skrll module_add(value);
213 1.4.4.2 skrll } else if (!strncmp(key, "format", 6)) {
214 1.4.4.2 skrll printf("value:%c\n", *value);
215 1.4.4.2 skrll switch (*value) {
216 1.4.4.2 skrll case 'a':
217 1.4.4.2 skrll case 'A':
218 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_AUTO;
219 1.4.4.2 skrll break;
220 1.4.4.2 skrll
221 1.4.4.2 skrll case 'n':
222 1.4.4.2 skrll case 'N':
223 1.4.4.2 skrll case 'd':
224 1.4.4.2 skrll case 'D':
225 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_NUMBER;
226 1.4.4.2 skrll break;
227 1.4.4.2 skrll
228 1.4.4.2 skrll case 'l':
229 1.4.4.2 skrll case 'L':
230 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_LETTER;
231 1.4.4.2 skrll break;
232 1.4.4.2 skrll }
233 1.4.4.2 skrll } else if (!strncmp(key, "clear", 5)) {
234 1.4.4.2 skrll bootconf.clear = !!atoi(value);
235 1.4.4.2 skrll }
236 1.4.4.2 skrll }
237 1.4.4.2 skrll switch (bootconf.menuformat) {
238 1.4.4.2 skrll case MENUFORMAT_AUTO:
239 1.4.4.2 skrll if (cmenu > 9 && bootconf.timeout > 0)
240 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_LETTER;
241 1.4.4.2 skrll else
242 1.4.4.2 skrll bootconf.menuformat = MENUFORMAT_NUMBER;
243 1.4.4.2 skrll break;
244 1.4.4.2 skrll
245 1.4.4.2 skrll case MENUFORMAT_NUMBER:
246 1.4.4.2 skrll if (cmenu > 9 && bootconf.timeout > 0)
247 1.4.4.2 skrll cmenu = 9;
248 1.4.4.2 skrll break;
249 1.4.4.2 skrll }
250 1.4.4.2 skrll
251 1.4.4.2 skrll bootconf.nummenu = cmenu;
252 1.4.4.2 skrll if (bootconf.def < 0)
253 1.4.4.2 skrll bootconf.def = 0;
254 1.4.4.2 skrll if (bootconf.def >= cmenu)
255 1.4.4.2 skrll bootconf.def = cmenu - 1;
256 1.4.4.2 skrll }
257 1.4.4.2 skrll
258 1.4.4.2 skrll /*
259 1.4.4.2 skrll * doboottypemenu will render the menu and parse any user input
260 1.4.4.2 skrll */
261 1.4.4.2 skrll static int
262 1.4.4.2 skrll getchoicefrominput(char *input, int def)
263 1.4.4.2 skrll {
264 1.4.4.2 skrll int choice;
265 1.4.4.2 skrll choice = -1;
266 1.4.4.2 skrll if (*input == '\0' || *input == '\r' || *input == '\n')
267 1.4.4.2 skrll choice = def;
268 1.4.4.2 skrll else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
269 1.4.4.2 skrll choice = (*input) - 'A';
270 1.4.4.2 skrll else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
271 1.4.4.2 skrll choice = (*input) - 'a';
272 1.4.4.2 skrll else if (isnum(*input)) {
273 1.4.4.2 skrll choice = atoi(input) - 1;
274 1.4.4.2 skrll if (choice < 0 || choice >= bootconf.nummenu)
275 1.4.4.2 skrll choice = -1;
276 1.4.4.2 skrll }
277 1.4.4.2 skrll return choice;
278 1.4.4.2 skrll }
279 1.4.4.2 skrll
280 1.4.4.2 skrll void
281 1.4.4.2 skrll doboottypemenu(void)
282 1.4.4.2 skrll {
283 1.4.4.2 skrll int choice;
284 1.4.4.2 skrll char input[80], *ic, *oc;
285 1.4.4.2 skrll
286 1.4.4.2 skrll printf("\n");
287 1.4.4.2 skrll /* Display menu */
288 1.4.4.2 skrll if (bootconf.menuformat == MENUFORMAT_LETTER) {
289 1.4.4.2 skrll for (choice = 0; choice < bootconf.nummenu; choice++)
290 1.4.4.2 skrll printf(" %c. %s\n", choice + 'A',
291 1.4.4.2 skrll bootconf.desc[choice]);
292 1.4.4.2 skrll } else {
293 1.4.4.2 skrll /* Can't use %2d format string with libsa */
294 1.4.4.2 skrll for (choice = 0; choice < bootconf.nummenu; choice++)
295 1.4.4.2 skrll printf(" %s%d. %s\n",
296 1.4.4.2 skrll (choice < 9) ? " " : "",
297 1.4.4.2 skrll choice + 1,
298 1.4.4.2 skrll bootconf.desc[choice]);
299 1.4.4.2 skrll }
300 1.4.4.2 skrll choice = -1;
301 1.4.4.2 skrll for (;;) {
302 1.4.4.2 skrll input[0] = '\0';
303 1.4.4.2 skrll
304 1.4.4.2 skrll if (bootconf.timeout < 0) {
305 1.4.4.2 skrll if (bootconf.menuformat == MENUFORMAT_LETTER)
306 1.4.4.2 skrll printf("\nOption: [%c]:",
307 1.4.4.2 skrll bootconf.def + 'A');
308 1.4.4.2 skrll else
309 1.4.4.2 skrll printf("\nOption: [%d]:",
310 1.4.4.2 skrll bootconf.def + 1);
311 1.4.4.2 skrll
312 1.4.4.2 skrll gets(input);
313 1.4.4.2 skrll choice = getchoicefrominput(input, bootconf.def);
314 1.4.4.2 skrll } else if (bootconf.timeout == 0)
315 1.4.4.2 skrll choice = bootconf.def;
316 1.4.4.2 skrll else {
317 1.4.4.2 skrll printf("\nChoose an option; RETURN for default; "
318 1.4.4.2 skrll "SPACE to stop countdown.\n");
319 1.4.4.2 skrll if (bootconf.menuformat == MENUFORMAT_LETTER)
320 1.4.4.2 skrll printf("Option %c will be chosen in ",
321 1.4.4.2 skrll bootconf.def + 'A');
322 1.4.4.2 skrll else
323 1.4.4.2 skrll printf("Option %d will be chosen in ",
324 1.4.4.2 skrll bootconf.def + 1);
325 1.4.4.2 skrll input[0] = awaitkey(bootconf.timeout, 1);
326 1.4.4.2 skrll input[1] = '\0';
327 1.4.4.2 skrll choice = getchoicefrominput(input, bootconf.def);
328 1.4.4.2 skrll /* If invalid key pressed, drop to menu */
329 1.4.4.2 skrll if (choice == -1)
330 1.4.4.2 skrll bootconf.timeout = -1;
331 1.4.4.2 skrll }
332 1.4.4.2 skrll if (choice < 0)
333 1.4.4.2 skrll continue;
334 1.4.4.2 skrll if (!strcmp(bootconf.command[choice], "prompt") &&
335 1.4.4.2 skrll ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
336 1.4.4.2 skrll check_password((char *)boot_params.bp_password))) {
337 1.4.4.2 skrll printf("type \"?\" or \"help\" for help.\n");
338 1.4.4.2 skrll bootmenu(); /* does not return */
339 1.4.4.2 skrll } else {
340 1.4.4.2 skrll ic = bootconf.command[choice];
341 1.4.4.2 skrll /* Split command string at ; into separate commands */
342 1.4.4.2 skrll do {
343 1.4.4.2 skrll oc = input;
344 1.4.4.2 skrll /* Look for ; separator */
345 1.4.4.2 skrll for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
346 1.4.4.2 skrll *oc++ = *ic;
347 1.4.4.2 skrll if (*input == '\0')
348 1.4.4.2 skrll continue;
349 1.4.4.2 skrll /* Strip out any trailing spaces */
350 1.4.4.2 skrll oc--;
351 1.4.4.2 skrll for (; *oc == ' ' && oc > input; oc--);
352 1.4.4.2 skrll *++oc = '\0';
353 1.4.4.2 skrll if (*ic == COMMAND_SEPARATOR)
354 1.4.4.2 skrll ic++;
355 1.4.4.2 skrll /* Stop silly command strings like ;;; */
356 1.4.4.2 skrll if (*input != '\0')
357 1.4.4.2 skrll docommand(input);
358 1.4.4.2 skrll /* Skip leading spaces */
359 1.4.4.2 skrll for (; *ic == ' '; ic++);
360 1.4.4.2 skrll } while (*ic);
361 1.4.4.2 skrll }
362 1.4.4.2 skrll
363 1.4.4.2 skrll }
364 1.4.4.2 skrll }
365 1.4.4.2 skrll
366 1.4.4.2 skrll #endif /* !SMALL */
367