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
6 Add ability to use shortkey to select item for bootmenu command
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
10 cmd/bootmenu.c | 34 ++++++++++++++++++++++++-----
11 common/menu.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--
12 include/menu.h | 12 +++++++----
13 3 files changed, 93 insertions(+), 11 deletions(-)
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;
25 @@ -95,10 +96,10 @@ static char *bootmenu_choice_entry(void
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);
32 /* Some key was pressed, so autoboot was stopped */
33 - key = bootmenu_loop(menu, cch);
34 + key = bootmenu_loop(menu, cch, &choice);
38 @@ -112,6 +113,12 @@ static char *bootmenu_choice_entry(void
40 /* no menu key selected, regenerate menu */
43 + menu->active = choice;
44 + if (!menu->last_choiced) {
45 + menu->last_choiced = true;
50 for (i = 0; i < menu->active; ++i)
51 @@ -169,6 +176,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;
59 while ((option = bootmenu_getoption(i))) {
61 @@ -183,11 +193,24 @@ static int prepare_bootmenu_entry(struct
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) {
74 + if (!get_choice_char(i, &choice_char))
75 + len = snprintf(choice_option, len, "%c. %s", choice_char, option);
77 + len = snprintf(choice_option, len, " %s", option);
78 + entry->title = strndup(choice_option, len);
83 + free(choice_option);
85 entry->command = strdup(sep + 1);
86 if (!entry->command) {
87 @@ -333,6 +356,7 @@ static struct bootmenu_data *bootmenu_cr
91 + menu->last_choiced = false;
93 default_str = env_get("bootmenu_default");
95 @@ -368,9 +392,9 @@ static struct bootmenu_data *bootmenu_cr
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");
102 - entry->title = strdup("Quit");
103 + entry->title = strdup("0. Quit");
109 @@ -48,6 +48,33 @@ struct menu {
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'
120 +static int find_choice(char choice)
124 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
125 + if (tolower(choice) == choice_chars[i])
131 +int get_choice_char(int index, char *result)
133 + if (index < ARRAY_SIZE(choice_chars))
134 + *result = choice_chars[index];
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 @@ -426,7 +453,7 @@ int menu_destroy(struct menu *m)
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)
150 enum bootmenu_key key = BKEY_NONE;
152 @@ -461,6 +488,19 @@ enum bootmenu_key bootmenu_autoboot_loop
156 + if (cch->esc_len || !choice)
159 + *choice = find_choice(c);
160 + if ((*choice >= 0 &&
161 + *choice < menu->count - 1)) {
163 + } else if (c == '0') {
164 + *choice = menu->count - 1;
172 @@ -481,7 +521,8 @@ enum bootmenu_key bootmenu_autoboot_loop
176 -enum bootmenu_key bootmenu_conv_key(int ichar)
177 +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
180 enum bootmenu_key key;
182 @@ -513,6 +554,20 @@ enum bootmenu_key bootmenu_conv_key(int
188 + if (choice && menu) {
189 + *choice = find_choice(ichar);
190 + if ((*choice >= 0 && *choice < menu->count - 1)) {
193 + } else if (ichar == '0') {
194 + *choice = menu->count - 1;
203 @@ -522,11 +577,16 @@ enum bootmenu_key bootmenu_conv_key(int
206 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
207 - struct cli_ch_state *cch)
208 + struct cli_ch_state *cch, int *choice)
210 enum bootmenu_key key;
213 + if (menu->last_choiced) {
214 + menu->last_choiced = false;
215 + return BKEY_SELECT;
218 c = cli_ch_process(cch, 0);
220 while (!c && !tstc()) {
221 @@ -540,7 +600,7 @@ enum bootmenu_key bootmenu_loop(struct b
225 - key = bootmenu_conv_key(c);
226 + key = bootmenu_conv_key(menu, c, choice);
236 +#include <linux/ctype.h>
241 @@ -19,6 +21,8 @@ int menu_get_choice(struct menu *m, void
242 int menu_item_add(struct menu *m, char *item_key, void *item_data);
243 int menu_destroy(struct menu *m);
244 int menu_default_choice(struct menu *m, void **choice);
245 +/* Add KEY_CHOICE support */
246 +int get_choice_char(int index, char *result);
249 * menu_show() Show a boot menu
250 @@ -41,6 +45,7 @@ struct bootmenu_data {
251 int active; /* active menu entry */
252 int count; /* total count of menu entries */
253 struct bootmenu_entry *first; /* first menu entry */
257 /** enum bootmenu_key - keys that can be returned by the bootmenu */
258 @@ -51,6 +56,7 @@ enum bootmenu_key {
264 /* 'extra' keys, which are used by menus but not cedit */
266 @@ -81,7 +87,7 @@ enum bootmenu_key {
267 * anything else: KEY_NONE
269 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
270 - struct cli_ch_state *cch);
271 + struct cli_ch_state *cch, int *choice);
274 * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled
275 @@ -107,7 +113,7 @@ enum bootmenu_key bootmenu_autoboot_loop
278 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
279 - struct cli_ch_state *cch);
280 + struct cli_ch_state *cch, int *choice);
283 * bootmenu_conv_key() - Convert a U-Boot keypress into a menu key
284 @@ -115,6 +121,7 @@ enum bootmenu_key bootmenu_loop(struct b
285 * @ichar: Keypress to convert (ASCII, including control characters)
286 * Returns: Menu key that corresponds to @ichar, or BKEY_NONE if none
288 -enum bootmenu_key bootmenu_conv_key(int ichar);
289 +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
292 #endif /* __MENU_H__ */
293 --- a/cmd/eficonfig.c
294 +++ b/cmd/eficonfig.c
295 @@ -239,7 +239,7 @@ char *eficonfig_choice_entry(void *data)
299 - key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
300 + key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch, NULL);
304 @@ -1881,7 +1881,7 @@ char *eficonfig_choice_change_boot_order
308 - key = bootmenu_loop(NULL, cch);
309 + key = bootmenu_loop(NULL, cch, NULL);
313 --- a/boot/bootflow_menu.c
314 +++ b/boot/bootflow_menu.c
315 @@ -240,7 +240,7 @@ int bootflow_menu_run(struct bootstd_pri
319 - key = bootmenu_conv_key(ichar);
320 + key = bootmenu_conv_key(NULL, ichar, NULL);
321 if (key == BKEY_NONE)