47e158b521cecab6562e46a45d8094f435a58539
[openwrt/staging/stintel.git] /
1 From 5a15437610e8e8c68dc347845a83d0cbad80ca08 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Tue, 19 Jan 2021 10:58:48 +0800
4 Subject: [PATCH 51/71] cmd: bootmenu: add ability to select item by shortkey
5
6 Add ability to use shortkey to select item for bootmenu command
7
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
9 ---
10 cmd/bootmenu.c | 34 ++++++++++++++++++++++++-----
11 common/menu.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--
12 include/menu.h | 12 +++++++----
13 3 files changed, 93 insertions(+), 11 deletions(-)
14
15 --- a/cmd/bootmenu.c
16 +++ b/cmd/bootmenu.c
17 @@ -88,6 +88,7 @@ static char *bootmenu_choice_entry(void
18 struct bootmenu_data *menu = data;
19 struct bootmenu_entry *iter;
20 enum bootmenu_key key = BKEY_NONE;
21 + int choice = -1;
22 int i;
23
24 cli_ch_init(cch);
25 @@ -95,10 +96,10 @@ static char *bootmenu_choice_entry(void
26 while (1) {
27 if (menu->delay >= 0) {
28 /* Autoboot was not stopped */
29 - key = bootmenu_autoboot_loop(menu, cch);
30 + key = bootmenu_autoboot_loop(menu, cch, &choice);
31 } else {
32 /* Some key was pressed, so autoboot was stopped */
33 - key = bootmenu_loop(menu, cch);
34 + key = bootmenu_loop(menu, cch, &choice);
35 }
36
37 switch (key) {
38 @@ -114,6 +115,12 @@ static char *bootmenu_choice_entry(void
39 ++menu->active;
40 /* no menu key selected, regenerate menu */
41 return NULL;
42 + case BKEY_CHOICE:
43 + menu->active = choice;
44 + if (!menu->last_choiced) {
45 + menu->last_choiced = true;
46 + return NULL;
47 + }
48 case BKEY_SELECT:
49 iter = menu->first;
50 for (i = 0; i < menu->active; ++i)
51 @@ -182,6 +189,9 @@ static int prepare_bootmenu_entry(struct
52 unsigned short int i = *index;
53 struct bootmenu_entry *entry = NULL;
54 struct bootmenu_entry *iter = *current;
55 + char *choice_option;
56 + char choice_char;
57 + int len;
58
59 while ((option = bootmenu_getoption(i))) {
60
61 @@ -196,11 +206,24 @@ static int prepare_bootmenu_entry(struct
62 if (!entry)
63 return -ENOMEM;
64
65 - entry->title = strndup(option, sep - option);
66 + /* Add KEY_CHOICE support: '%d. %s\0' : len --> len + 4 */
67 + len = sep - option + 4;
68 + choice_option = malloc(len);
69 + if (!choice_option) {
70 + free(entry->title);
71 + free(entry);
72 + return -ENOMEM;
73 + }
74 + if (!get_choice_char(i, &choice_char))
75 + len = snprintf(choice_option, len, "%c. %s", choice_char, option);
76 + else
77 + len = snprintf(choice_option, len, " %s", option);
78 + entry->title = strndup(choice_option, len);
79 if (!entry->title) {
80 free(entry);
81 return -ENOMEM;
82 }
83 + free(choice_option);
84
85 entry->command = strdup(sep + 1);
86 if (!entry->command) {
87 @@ -347,6 +370,7 @@ static struct bootmenu_data *bootmenu_cr
88 menu->active = 0;
89 menu->last_active = -1;
90 menu->first = NULL;
91 + menu->last_choiced = false;
92
93 default_str = env_get("bootmenu_default");
94 if (default_str)
95 @@ -382,9 +406,9 @@ static struct bootmenu_data *bootmenu_cr
96
97 /* Add Quit entry if exiting bootmenu is disabled */
98 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
99 - entry->title = strdup("Exit");
100 + entry->title = strdup("0. Exit");
101 else
102 - entry->title = strdup("Quit");
103 + entry->title = strdup("0. Quit");
104
105 if (!entry->title) {
106 free(entry);
107 --- a/common/menu.c
108 +++ b/common/menu.c
109 @@ -49,6 +49,33 @@ struct menu {
110 int item_cnt;
111 };
112
113 +const char choice_chars[] = {
114 + '1', '2', '3', '4', '5', '6', '7', '8', '9',
115 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
116 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
117 + 'u', 'v', 'w', 'x', 'y', 'z'
118 +};
119 +
120 +static int find_choice(char choice)
121 +{
122 + int i;
123 +
124 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
125 + if (tolower(choice) == choice_chars[i])
126 + return i;
127 +
128 + return -1;
129 +}
130 +
131 +int get_choice_char(int index, char *result)
132 +{
133 + if (index < ARRAY_SIZE(choice_chars))
134 + *result = choice_chars[index];
135 + else
136 + return -1;
137 + return 0;
138 +}
139 +
140 /*
141 * An iterator function for menu items. callback will be called for each item
142 * in m, with m, a pointer to the item, and extra being passed to callback. If
143 @@ -437,7 +464,7 @@ int menu_destroy(struct menu *m)
144 }
145
146 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
147 - struct cli_ch_state *cch)
148 + struct cli_ch_state *cch, int *choice)
149 {
150 enum bootmenu_key key = BKEY_NONE;
151 int i, c;
152 @@ -472,6 +499,19 @@ enum bootmenu_key bootmenu_autoboot_loop
153 break;
154 default:
155 key = BKEY_NONE;
156 + if (cch->esc_len || !choice)
157 + break;
158 +
159 + *choice = find_choice(c);
160 + if ((*choice >= 0 &&
161 + *choice < menu->count - 1)) {
162 + key = BKEY_CHOICE;
163 + } else if (c == '0') {
164 + *choice = menu->count - 1;
165 + key = BKEY_CHOICE;
166 + } else {
167 + key = BKEY_NONE;
168 + }
169 break;
170 }
171 break;
172 @@ -492,7 +532,8 @@ enum bootmenu_key bootmenu_autoboot_loop
173 return key;
174 }
175
176 -enum bootmenu_key bootmenu_conv_key(int ichar)
177 +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
178 + int *choice)
179 {
180 enum bootmenu_key key;
181
182 @@ -524,6 +565,20 @@ enum bootmenu_key bootmenu_conv_key(int
183 case ' ':
184 key = BKEY_SPACE;
185 break;
186 + case '0' ... '9':
187 + case 'a' ... 'z':
188 + if (choice && menu) {
189 + *choice = find_choice(ichar);
190 + if ((*choice >= 0 && *choice < menu->count - 1)) {
191 + key = BKEY_CHOICE;
192 + break;
193 + } else if (ichar == '0') {
194 + *choice = menu->count - 1;
195 + key = BKEY_CHOICE;
196 + break;
197 + }
198 + }
199 + fallthrough;
200 default:
201 key = BKEY_NONE;
202 break;
203 @@ -533,11 +588,17 @@ enum bootmenu_key bootmenu_conv_key(int
204 }
205
206 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
207 - struct cli_ch_state *cch)
208 + struct cli_ch_state *cch,
209 + int *choice)
210 {
211 enum bootmenu_key key;
212 int c, errchar = 0;
213
214 + if (menu->last_choiced) {
215 + menu->last_choiced = false;
216 + return BKEY_SELECT;
217 + }
218 +
219 c = cli_ch_process(cch, 0);
220 if (!c) {
221 while (!c && !tstc()) {
222 @@ -552,7 +613,7 @@ enum bootmenu_key bootmenu_loop(struct b
223 }
224 }
225
226 - key = bootmenu_conv_key(c);
227 + key = bootmenu_conv_key(menu, c, choice);
228
229 return key;
230 }
231 --- a/include/menu.h
232 +++ b/include/menu.h
233 @@ -6,6 +6,8 @@
234 #ifndef __MENU_H__
235 #define __MENU_H__
236
237 +#include <linux/ctype.h>
238 +
239 struct cli_ch_state;
240 struct menu;
241
242 @@ -20,6 +22,8 @@ int menu_get_choice(struct menu *m, void
243 int menu_item_add(struct menu *m, char *item_key, void *item_data);
244 int menu_destroy(struct menu *m);
245 int menu_default_choice(struct menu *m, void **choice);
246 +/* Add KEY_CHOICE support */
247 +int get_choice_char(int index, char *result);
248
249 /**
250 * menu_show() Show a boot menu
251 @@ -43,6 +47,7 @@ struct bootmenu_data {
252 int last_active; /* last active menu entry */
253 int count; /* total count of menu entries */
254 struct bootmenu_entry *first; /* first menu entry */
255 + bool last_choiced;
256 };
257
258 /** enum bootmenu_key - keys that can be returned by the bootmenu */
259 @@ -53,6 +58,7 @@ enum bootmenu_key {
260 BKEY_SELECT,
261 BKEY_QUIT,
262 BKEY_SAVE,
263 + BKEY_CHOICE,
264
265 /* 'extra' keys, which are used by menus but not cedit */
266 BKEY_PLUS,
267 @@ -83,7 +89,7 @@ enum bootmenu_key {
268 * anything else: KEY_NONE
269 */
270 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
271 - struct cli_ch_state *cch);
272 + struct cli_ch_state *cch, int *choice);
273
274 /**
275 * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled
276 @@ -109,7 +115,7 @@ enum bootmenu_key bootmenu_autoboot_loop
277 * Space: BKEY_SPACE
278 */
279 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
280 - struct cli_ch_state *cch);
281 + struct cli_ch_state *cch, int *choice);
282
283 /**
284 * bootmenu_conv_key() - Convert a U-Boot keypress into a menu key
285 @@ -117,6 +123,7 @@ enum bootmenu_key bootmenu_loop(struct b
286 * @ichar: Keypress to convert (ASCII, including control characters)
287 * Returns: Menu key that corresponds to @ichar, or BKEY_NONE if none
288 */
289 -enum bootmenu_key bootmenu_conv_key(int ichar);
290 +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
291 + int *choice);
292
293 #endif /* __MENU_H__ */
294 --- a/cmd/eficonfig.c
295 +++ b/cmd/eficonfig.c
296 @@ -239,7 +239,7 @@ char *eficonfig_choice_entry(void *data)
297 cli_ch_init(cch);
298
299 while (1) {
300 - key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
301 + key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch, NULL);
302
303 switch (key) {
304 case BKEY_UP:
305 @@ -1881,7 +1881,7 @@ char *eficonfig_choice_change_boot_order
306
307 cli_ch_init(cch);
308 while (1) {
309 - key = bootmenu_loop(NULL, cch);
310 + key = bootmenu_loop(NULL, cch, NULL);
311
312 switch (key) {
313 case BKEY_PLUS:
314 --- a/boot/bootflow_menu.c
315 +++ b/boot/bootflow_menu.c
316 @@ -240,7 +240,7 @@ int bootflow_menu_run(struct bootstd_pri
317
318 key = 0;
319 if (ichar) {
320 - key = bootmenu_conv_key(ichar);
321 + key = bootmenu_conv_key(NULL, ichar, NULL);
322 if (key == BKEY_NONE)
323 key = ichar;
324 }