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