xref: /netbsd-src/sys/stand/efiboot/bootmenu.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: bootmenu.c,v 1.2 2020/06/26 03:23:04 thorpej 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 "bootmenu.h"
41 #include "efiboot.h"
42 #include "module.h"
43 #include "overlay.h"
44 
45 static void docommandchoice(int);
46 
47 extern	const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
48 
49 #define MENUFORMAT_AUTO	  0
50 #define MENUFORMAT_NUMBER 1
51 #define MENUFORMAT_LETTER 2
52 
53 /*
54  * XXX
55  * if module_add, userconf_add are strictly mi they can be folded back
56  * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
57  */
58 static void
59 do_bootcfg_command(const char *cmd, char *arg)
60 {
61 	if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
62 		module_add(arg);
63 #if notyet
64 	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
65 		userconf_add(arg);
66 #endif
67 	else if (strcmp(cmd, "dtoverlay") == 0)
68 		dtoverlay_add(arg);
69 }
70 
71 int
72 parsebootconf(const char *conf)
73 {
74 	return perform_bootcfg(conf, &do_bootcfg_command, 32768);
75 }
76 
77 /*
78  * doboottypemenu will render the menu and parse any user input
79  */
80 static int
81 getchoicefrominput(char *input, int def)
82 {
83 	int choice, usedef;
84 
85 	choice = -1;
86 	usedef = 0;
87 
88 	if (*input == '\0' || *input == '\r' || *input == '\n') {
89 		choice = def;
90 		usedef = 1;
91 	} else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
92 		choice = (*input) - 'A';
93 	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
94 		choice = (*input) - 'a';
95 	else if (isdigit(*input)) {
96 		choice = atoi(input) - 1;
97 		if (choice < 0 || choice >= bootcfg_info.nummenu)
98 			choice = -1;
99 	}
100 
101 	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
102 	    !isdigit(*input) && !usedef)
103 		choice = -1;
104 
105 	return choice;
106 }
107 
108 static void
109 docommandchoice(int choice)
110 {
111 	char input[80], *ic, *oc;
112 
113 	ic = bootcfg_info.command[choice];
114 	/* Split command string at ; into separate commands */
115 	do {
116 		oc = input;
117 		/* Look for ; separator */
118 		for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
119 			*oc++ = *ic;
120 		if (*input == '\0')
121 			continue;
122 		/* Strip out any trailing spaces */
123 		oc--;
124 		for (; *oc == ' ' && oc > input; oc--);
125 		*++oc = '\0';
126 		if (*ic == COMMAND_SEPARATOR)
127 			ic++;
128 		/* Stop silly command strings like ;;; */
129 		if (*input != '\0')
130 			docommand(input);
131 		/* Skip leading spaces */
132 		for (; *ic == ' '; ic++);
133 	} while (*ic);
134 }
135 
136 void
137 bootdefault(void)
138 {
139 	int choice;
140 	static int entered;
141 
142 	if (bootcfg_info.nummenu > 0) {
143 		if (entered) {
144 			printf("default boot twice, skipping...\n");
145 			return;
146 		}
147 		entered = 1;
148 		choice = bootcfg_info.def;
149 		printf("command(s): %s\n", bootcfg_info.command[choice]);
150 		docommandchoice(choice);
151 	}
152 }
153 
154 __dead void
155 doboottypemenu(void)
156 {
157 	int choice;
158 	char input[80];
159 
160 	printf("\n");
161 	/* Display menu */
162 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
163 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
164 			printf("    %c. %s\n", choice + 'A',
165 			    bootcfg_info.desc[choice]);
166 	} else {
167 		/* Can't use %2d format string with libsa */
168 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
169 			printf("    %s%d. %s\n",
170 			    (choice < 9) ?  " " : "",
171 			    choice + 1,
172 			    bootcfg_info.desc[choice]);
173 	}
174 	choice = -1;
175 	for (;;) {
176 		input[0] = '\0';
177 
178 		if (bootcfg_info.timeout < 0) {
179 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
180 				printf("\nOption: [%c]:",
181 				    bootcfg_info.def + 'A');
182 			else
183 				printf("\nOption: [%d]:",
184 				    bootcfg_info.def + 1);
185 
186 			kgets(input, sizeof(input));
187 			choice = getchoicefrominput(input, bootcfg_info.def);
188 		} else if (bootcfg_info.timeout == 0)
189 			choice = bootcfg_info.def;
190 		else  {
191 			printf("\nChoose an option; RETURN for default; "
192 			       "SPACE to stop countdown.\n");
193 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
194 				printf("Option %c will be chosen in ",
195 				    bootcfg_info.def + 'A');
196 			else
197 				printf("Option %d will be chosen in ",
198 				    bootcfg_info.def + 1);
199 			input[0] = awaitkey(bootcfg_info.timeout, 1);
200 			input[1] = '\0';
201 			choice = getchoicefrominput(input, bootcfg_info.def);
202 			/* If invalid key pressed, drop to menu */
203 			if (choice == -1)
204 				bootcfg_info.timeout = -1;
205 		}
206 		if (choice < 0)
207 			continue;
208 		if (!strcmp(bootcfg_info.command[choice], "prompt")) {
209 			printf("type \"?\" or \"help\" for help.\n");
210 			bootprompt(); /* does not return */
211 		} else {
212 			docommandchoice(choice);
213 		}
214 
215 	}
216 }
217 
218 #endif	/* !SMALL */
219