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