xref: /netbsd-src/sys/arch/i386/stand/lib/bootmenu.c (revision 32d1c65c71fbdb65a012e8392a62a757dd6853e9)
1 /*	$NetBSD: bootmenu.c,v 1.19 2024/11/09 12:43:52 mlelstv 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 	printf("\n");
140 	/* Display menu */
141 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
142 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
143 			printf("    %c. %s\n", choice + 'A',
144 			    bootcfg_info.desc[choice]);
145 	} else {
146 		/* Can't use %2d format string with libsa */
147 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
148 			printf("    %s%d. %s\n",
149 			    (choice < 9) ?  " " : "",
150 			    choice + 1,
151 			    bootcfg_info.desc[choice]);
152 	}
153 	choice = -1;
154 	for (;;) {
155 		input[0] = '\0';
156 
157 		if (bootcfg_info.timeout < 0) {
158 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
159 				printf("\nOption: [%c]:",
160 				    bootcfg_info.def + 'A');
161 			else
162 				printf("\nOption: [%d]:",
163 				    bootcfg_info.def + 1);
164 
165 			kgets(input, sizeof(input));
166 			choice = getchoicefrominput(input, bootcfg_info.def);
167 		} else if (bootcfg_info.timeout == 0)
168 			choice = bootcfg_info.def;
169 		else  {
170 			printf("\nChoose an option; RETURN for default; "
171 			       "SPACE to stop countdown.\n");
172 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
173 				printf("Option %c will be chosen in ",
174 				    bootcfg_info.def + 'A');
175 			else
176 				printf("Option %d will be chosen in ",
177 				    bootcfg_info.def + 1);
178 			input[0] = awaitkey(bootcfg_info.timeout, 1);
179 			input[1] = '\0';
180 			choice = getchoicefrominput(input, bootcfg_info.def);
181 			/* If invalid key pressed, drop to menu */
182 			if (choice == -1)
183 				bootcfg_info.timeout = -1;
184 		}
185 		if (choice < 0)
186 			continue;
187 		if (!strcmp(bootcfg_info.command[choice], "prompt") &&
188 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
189 		    check_password((char *)boot_params.bp_password))) {
190 			printf("type \"?\" or \"help\" for help.\n");
191 			bootmenu(); /* does not return */
192 		} else {
193 			docommandchoice(choice);
194 		}
195 
196 	}
197 }
198 
199 #endif	/* !SMALL */
200