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