xref: /netbsd-src/sys/arch/i386/stand/lib/bootmenu.c (revision 4f99b703c0363867a33e95c198aece876a227b07)
1 /*	$NetBSD: bootmenu.c,v 1.20 2024/11/27 17:19:37 martin 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
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, "fs") == 0)
63 		fs_add(arg);
64 	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
65 		userconf_add(arg);
66 }
67 
68 int
69 parsebootconf(const char *conf)
70 {
71 	return perform_bootcfg(conf, &do_bootcfg_command, 32768);
72 }
73 
74 /*
75  * doboottypemenu will render the menu and parse any user input
76  */
77 static int
78 getchoicefrominput(char *input, int def)
79 {
80 	int choice, usedef;
81 
82 	choice = -1;
83 	usedef = 0;
84 
85 	if (*input == '\0' || *input == '\r' || *input == '\n') {
86 		choice = def;
87 		usedef = 1;
88 	} else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
89 		choice = (*input) - 'A';
90 	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
91 		choice = (*input) - 'a';
92 	else if (isdigit(*input)) {
93 		choice = atoi(input) - 1;
94 		if (choice < 0 || choice >= bootcfg_info.nummenu)
95 			choice = -1;
96 	}
97 
98 	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
99 	    !isdigit(*input) && !usedef)
100 		choice = -1;
101 
102 	return choice;
103 }
104 
105 static void
106 docommandchoice(int choice)
107 {
108 	char input[80], *ic, *oc;
109 
110 	ic = bootcfg_info.command[choice];
111 	/* Split command string at ; into separate commands */
112 	do {
113 		oc = input;
114 		/* Look for ; separator */
115 		for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
116 			*oc++ = *ic;
117 		if (*input == '\0')
118 			continue;
119 		/* Strip out any trailing spaces */
120 		oc--;
121 		for (; *oc == ' ' && oc > input; oc--);
122 		*++oc = '\0';
123 		if (*ic == COMMAND_SEPARATOR)
124 			ic++;
125 		/* Stop silly command strings like ;;; */
126 		if (*input != '\0')
127 			docommand(input);
128 		/* Skip leading spaces */
129 		for (; *ic == ' '; ic++);
130 	} while (*ic);
131 }
132 
133 __dead void
134 doboottypemenu(void)
135 {
136 	int choice;
137 	char input[80];
138 
139 	/*
140 	 * If we have a single menu entry with empty description and
141 	 * timeout = 0 we do not display any menu.
142 	 */
143 	if ((bootcfg_info.nummenu > 0 &&
144 	     bootcfg_info.desc[0] != bootcfg_info.command[0] &&
145 	     bootcfg_info.desc[0][0] != 0) || bootcfg_info.timeout > 0) {
146 		printf("\n");
147 
148 		/* Display menu */
149 		if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
150 			for (choice = 0; choice < bootcfg_info.nummenu;
151 			    choice++)
152 				printf("    %c. %s\n", choice + 'A',
153 				    bootcfg_info.desc[choice]);
154 		} else {
155 			/* Can't use %2d format string with libsa */
156 			for (choice = 0; choice < bootcfg_info.nummenu;
157 			    choice++)
158 				printf("    %s%d. %s\n",
159 				    (choice < 9) ?  " " : "",
160 				    choice + 1,
161 				    bootcfg_info.desc[choice]);
162 		}
163 	}
164 	choice = -1;
165 	for (;;) {
166 		input[0] = '\0';
167 
168 		if (bootcfg_info.timeout < 0) {
169 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
170 				printf("\nOption: [%c]:",
171 				    bootcfg_info.def + 'A');
172 			else
173 				printf("\nOption: [%d]:",
174 				    bootcfg_info.def + 1);
175 
176 			kgets(input, sizeof(input));
177 			choice = getchoicefrominput(input, bootcfg_info.def);
178 		} else if (bootcfg_info.timeout == 0)
179 			choice = bootcfg_info.def;
180 		else  {
181 			printf("\nChoose an option; RETURN for default; "
182 			       "SPACE to stop countdown.\n");
183 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
184 				printf("Option %c will be chosen in ",
185 				    bootcfg_info.def + 'A');
186 			else
187 				printf("Option %d will be chosen in ",
188 				    bootcfg_info.def + 1);
189 			input[0] = awaitkey(bootcfg_info.timeout, 1);
190 			input[1] = '\0';
191 			choice = getchoicefrominput(input, bootcfg_info.def);
192 			/* If invalid key pressed, drop to menu */
193 			if (choice == -1)
194 				bootcfg_info.timeout = -1;
195 		}
196 		if (choice < 0)
197 			continue;
198 		if (!strcmp(bootcfg_info.command[choice], "prompt") &&
199 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
200 		    check_password((char *)boot_params.bp_password))) {
201 			printf("type \"?\" or \"help\" for help.\n");
202 			bootmenu(); /* does not return */
203 		} else {
204 			docommandchoice(choice);
205 		}
206 
207 	}
208 }
209 
210 #endif	/* !SMALL */
211