xref: /minix3/sys/arch/i386/stand/lib/bootmenu.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
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
do_bootcfg_command(const char * cmd,char * arg)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
parsebootconf(const char * conf)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
getchoicefrominput(char * input,int def)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
docommandchoice(int choice)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
bootdefault(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 #if defined(__minix)
150 static void
showmenu(void)151 showmenu(void)
152 {
153 	int choice;
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 }
170 #endif /* defined(__minix) */
171 
172 __dead void
doboottypemenu(void)173 doboottypemenu(void)
174 {
175 #if !defined(__minix)
176 	int choice;
177 	char input[80];
178 
179 	printf("\n");
180 	/* Display menu */
181 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
182 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
183 			printf("    %c. %s\n", choice + 'A',
184 			    bootcfg_info.desc[choice]);
185 	} else {
186 		/* Can't use %2d format string with libsa */
187 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
188 			printf("    %s%d. %s\n",
189 			    (choice < 9) ?  " " : "",
190 			    choice + 1,
191 			    bootcfg_info.desc[choice]);
192 	}
193 #else
194 	int choice, editing;
195 	char input[256], *ic, *oc;
196 #endif /* !defined(__minix) */
197 #if defined(__minix)
198 	showmenu();
199 #endif /* defined(__minix) */
200 	choice = -1;
201 #if defined(__minix)
202 	editing = 0;
203 #endif /* defined(__minix) */
204 	for (;;) {
205 		input[0] = '\0';
206 
207 		if (bootcfg_info.timeout < 0) {
208 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
209 #if !defined(__minix)
210 				printf("\nOption: [%c]:",
211 #else
212 				printf("\nOption%s: [%c]:",
213 				    editing ? " (edit)" : "",
214 #endif /* !defined(__minix) */
215 				    bootcfg_info.def + 'A');
216 			else
217 #if !defined(__minix)
218 				printf("\nOption: [%d]:",
219 #else
220 				printf("\nOption%s: [%d]:",
221 				    editing ? " (edit)" : "",
222 #endif /* !defined(__minix) */
223 				    bootcfg_info.def + 1);
224 
225 #if !defined(__minix)
226 			gets(input);
227 #else
228 			editline(input, sizeof(input), NULL);
229 #endif /* !defined(__minix) */
230 			choice = getchoicefrominput(input, bootcfg_info.def);
231 		} else if (bootcfg_info.timeout == 0)
232 			choice = bootcfg_info.def;
233 		else  {
234 			printf("\nChoose an option; RETURN for default; "
235 			       "SPACE to stop countdown.\n");
236 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
237 				printf("Option %c will be chosen in ",
238 				    bootcfg_info.def + 'A');
239 			else
240 				printf("Option %d will be chosen in ",
241 				    bootcfg_info.def + 1);
242 			input[0] = awaitkey(bootcfg_info.timeout, 1);
243 			input[1] = '\0';
244 			choice = getchoicefrominput(input, bootcfg_info.def);
245 			/* If invalid key pressed, drop to menu */
246 			if (choice == -1)
247 				bootcfg_info.timeout = -1;
248 		}
249 		if (choice < 0)
250 			continue;
251 #if !defined(__minix)
252 		if (!strcmp(bootcfg_info.command[choice], "prompt") &&
253 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
254 		    check_password((char *)boot_params.bp_password))) {
255 			printf("type \"?\" or \"help\" for help.\n");
256 			bootmenu(); /* does not return */
257 		} else {
258 			docommandchoice(choice);
259 		}
260 #else
261 		ic = bootcfg_info.command[choice];
262 		if (editing) {
263 			printf("> ");
264 			editline(input, sizeof(input), ic);
265 			ic = input;
266 		}
267 		if (!strcmp(ic, "edit") &&
268 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
269 		    check_password((char *)boot_params.bp_password))) {
270 			editing = 1;
271 			bootcfg_info.timeout = -1;
272 		} else if (!strcmp(ic, "prompt") &&
273 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
274 		    check_password((char *)boot_params.bp_password))) {
275 			printf("type \"?\" or \"help\" for help, "
276 			    "or \"menu\" to return to the menu.\n");
277 			prompt(1);
278 			showmenu();
279 			editing = 0;
280 			bootcfg_info.timeout = -1;
281 		} else {
282 			/* Split command string at ; into separate commands */
283 			do {
284 				/*
285 				 * This must support inline editing, since ic
286 				 * may also point to input.
287 				 */
288 				oc = input;
289 				/* Look for ; separator */
290 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
291 					*oc++ = *ic;
292 				if (*ic == COMMAND_SEPARATOR)
293 					ic++;
294 				if (oc == input)
295 					continue;
296 				/* Strip out any trailing spaces */
297 				oc--;
298 				for (; *oc == ' ' && oc > input; oc--);
299 				*++oc = '\0';
300 				/* Stop silly command strings like ;;; */
301 				if (*input != '\0')
302 					docommand(input);
303 				/* Skip leading spaces */
304 				for (; *ic == ' '; ic++);
305 			} while (*ic);
306 		}
307 #endif /* !defined(__minix) */
308 
309 	}
310 }
311 
312 #endif	/* !SMALL */
313