bootmenu.c revision 1.1 1 1.1 jmcneill /* $NetBSD: bootmenu.c,v 1.1 2020/06/21 23:53:26 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #ifndef SMALL
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/types.h>
32 1.1 jmcneill #include <sys/reboot.h>
33 1.1 jmcneill #include <sys/bootblock.h>
34 1.1 jmcneill
35 1.1 jmcneill #include <lib/libsa/stand.h>
36 1.1 jmcneill #include <lib/libsa/bootcfg.h>
37 1.1 jmcneill #include <lib/libsa/ufs.h>
38 1.1 jmcneill #include <lib/libkern/libkern.h>
39 1.1 jmcneill
40 1.1 jmcneill #include "bootmenu.h"
41 1.1 jmcneill #include "efiboot.h"
42 1.1 jmcneill #include "module.h"
43 1.1 jmcneill
44 1.1 jmcneill static void docommandchoice(int);
45 1.1 jmcneill
46 1.1 jmcneill extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
47 1.1 jmcneill
48 1.1 jmcneill #define MENUFORMAT_AUTO 0
49 1.1 jmcneill #define MENUFORMAT_NUMBER 1
50 1.1 jmcneill #define MENUFORMAT_LETTER 2
51 1.1 jmcneill
52 1.1 jmcneill /*
53 1.1 jmcneill * XXX
54 1.1 jmcneill * if module_add, userconf_add are strictly mi they can be folded back
55 1.1 jmcneill * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
56 1.1 jmcneill */
57 1.1 jmcneill static void
58 1.1 jmcneill do_bootcfg_command(const char *cmd, char *arg)
59 1.1 jmcneill {
60 1.1 jmcneill if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
61 1.1 jmcneill module_add(arg);
62 1.1 jmcneill #if notyet
63 1.1 jmcneill else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
64 1.1 jmcneill userconf_add(arg);
65 1.1 jmcneill #endif
66 1.1 jmcneill }
67 1.1 jmcneill
68 1.1 jmcneill int
69 1.1 jmcneill parsebootconf(const char *conf)
70 1.1 jmcneill {
71 1.1 jmcneill return perform_bootcfg(conf, &do_bootcfg_command, 32768);
72 1.1 jmcneill }
73 1.1 jmcneill
74 1.1 jmcneill /*
75 1.1 jmcneill * doboottypemenu will render the menu and parse any user input
76 1.1 jmcneill */
77 1.1 jmcneill static int
78 1.1 jmcneill getchoicefrominput(char *input, int def)
79 1.1 jmcneill {
80 1.1 jmcneill int choice, usedef;
81 1.1 jmcneill
82 1.1 jmcneill choice = -1;
83 1.1 jmcneill usedef = 0;
84 1.1 jmcneill
85 1.1 jmcneill if (*input == '\0' || *input == '\r' || *input == '\n') {
86 1.1 jmcneill choice = def;
87 1.1 jmcneill usedef = 1;
88 1.1 jmcneill } else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
89 1.1 jmcneill choice = (*input) - 'A';
90 1.1 jmcneill else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
91 1.1 jmcneill choice = (*input) - 'a';
92 1.1 jmcneill else if (isdigit(*input)) {
93 1.1 jmcneill choice = atoi(input) - 1;
94 1.1 jmcneill if (choice < 0 || choice >= bootcfg_info.nummenu)
95 1.1 jmcneill choice = -1;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
99 1.1 jmcneill !isdigit(*input) && !usedef)
100 1.1 jmcneill choice = -1;
101 1.1 jmcneill
102 1.1 jmcneill return choice;
103 1.1 jmcneill }
104 1.1 jmcneill
105 1.1 jmcneill static void
106 1.1 jmcneill docommandchoice(int choice)
107 1.1 jmcneill {
108 1.1 jmcneill char input[80], *ic, *oc;
109 1.1 jmcneill
110 1.1 jmcneill ic = bootcfg_info.command[choice];
111 1.1 jmcneill /* Split command string at ; into separate commands */
112 1.1 jmcneill do {
113 1.1 jmcneill oc = input;
114 1.1 jmcneill /* Look for ; separator */
115 1.1 jmcneill for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
116 1.1 jmcneill *oc++ = *ic;
117 1.1 jmcneill if (*input == '\0')
118 1.1 jmcneill continue;
119 1.1 jmcneill /* Strip out any trailing spaces */
120 1.1 jmcneill oc--;
121 1.1 jmcneill for (; *oc == ' ' && oc > input; oc--);
122 1.1 jmcneill *++oc = '\0';
123 1.1 jmcneill if (*ic == COMMAND_SEPARATOR)
124 1.1 jmcneill ic++;
125 1.1 jmcneill /* Stop silly command strings like ;;; */
126 1.1 jmcneill if (*input != '\0')
127 1.1 jmcneill docommand(input);
128 1.1 jmcneill /* Skip leading spaces */
129 1.1 jmcneill for (; *ic == ' '; ic++);
130 1.1 jmcneill } while (*ic);
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.1 jmcneill void
134 1.1 jmcneill bootdefault(void)
135 1.1 jmcneill {
136 1.1 jmcneill int choice;
137 1.1 jmcneill static int entered;
138 1.1 jmcneill
139 1.1 jmcneill if (bootcfg_info.nummenu > 0) {
140 1.1 jmcneill if (entered) {
141 1.1 jmcneill printf("default boot twice, skipping...\n");
142 1.1 jmcneill return;
143 1.1 jmcneill }
144 1.1 jmcneill entered = 1;
145 1.1 jmcneill choice = bootcfg_info.def;
146 1.1 jmcneill printf("command(s): %s\n", bootcfg_info.command[choice]);
147 1.1 jmcneill docommandchoice(choice);
148 1.1 jmcneill }
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill __dead void
152 1.1 jmcneill doboottypemenu(void)
153 1.1 jmcneill {
154 1.1 jmcneill int choice;
155 1.1 jmcneill char input[80];
156 1.1 jmcneill
157 1.1 jmcneill printf("\n");
158 1.1 jmcneill /* Display menu */
159 1.1 jmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
160 1.1 jmcneill for (choice = 0; choice < bootcfg_info.nummenu; choice++)
161 1.1 jmcneill printf(" %c. %s\n", choice + 'A',
162 1.1 jmcneill bootcfg_info.desc[choice]);
163 1.1 jmcneill } else {
164 1.1 jmcneill /* Can't use %2d format string with libsa */
165 1.1 jmcneill for (choice = 0; choice < bootcfg_info.nummenu; choice++)
166 1.1 jmcneill printf(" %s%d. %s\n",
167 1.1 jmcneill (choice < 9) ? " " : "",
168 1.1 jmcneill choice + 1,
169 1.1 jmcneill bootcfg_info.desc[choice]);
170 1.1 jmcneill }
171 1.1 jmcneill choice = -1;
172 1.1 jmcneill for (;;) {
173 1.1 jmcneill input[0] = '\0';
174 1.1 jmcneill
175 1.1 jmcneill if (bootcfg_info.timeout < 0) {
176 1.1 jmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
177 1.1 jmcneill printf("\nOption: [%c]:",
178 1.1 jmcneill bootcfg_info.def + 'A');
179 1.1 jmcneill else
180 1.1 jmcneill printf("\nOption: [%d]:",
181 1.1 jmcneill bootcfg_info.def + 1);
182 1.1 jmcneill
183 1.1 jmcneill kgets(input, sizeof(input));
184 1.1 jmcneill choice = getchoicefrominput(input, bootcfg_info.def);
185 1.1 jmcneill } else if (bootcfg_info.timeout == 0)
186 1.1 jmcneill choice = bootcfg_info.def;
187 1.1 jmcneill else {
188 1.1 jmcneill printf("\nChoose an option; RETURN for default; "
189 1.1 jmcneill "SPACE to stop countdown.\n");
190 1.1 jmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
191 1.1 jmcneill printf("Option %c will be chosen in ",
192 1.1 jmcneill bootcfg_info.def + 'A');
193 1.1 jmcneill else
194 1.1 jmcneill printf("Option %d will be chosen in ",
195 1.1 jmcneill bootcfg_info.def + 1);
196 1.1 jmcneill input[0] = awaitkey(bootcfg_info.timeout, 1);
197 1.1 jmcneill input[1] = '\0';
198 1.1 jmcneill choice = getchoicefrominput(input, bootcfg_info.def);
199 1.1 jmcneill /* If invalid key pressed, drop to menu */
200 1.1 jmcneill if (choice == -1)
201 1.1 jmcneill bootcfg_info.timeout = -1;
202 1.1 jmcneill }
203 1.1 jmcneill if (choice < 0)
204 1.1 jmcneill continue;
205 1.1 jmcneill if (!strcmp(bootcfg_info.command[choice], "prompt")) {
206 1.1 jmcneill printf("type \"?\" or \"help\" for help.\n");
207 1.1 jmcneill bootprompt(); /* does not return */
208 1.1 jmcneill } else {
209 1.1 jmcneill docommandchoice(choice);
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill }
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill #endif /* !SMALL */
216