1 From afea25576fc92d562b248b783cf03564eb4521da 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 12/12] 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 | 77 +++++++++++++++++++++++++++++++++++++++++++++-----
11 1 file changed, 70 insertions(+), 7 deletions(-)
19 +#include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/string.h>
23 @@ -87,16 +88,17 @@ static char *bootmenu_choice_entry(void
24 struct bootmenu_data *menu = data;
25 struct bootmenu_entry *iter;
26 enum bootmenu_key key = KEY_NONE;
32 if (menu->delay >= 0) {
33 /* Autoboot was not stopped */
34 - bootmenu_autoboot_loop(menu, &key, &esc);
35 + bootmenu_autoboot_loop(menu, &key, &esc, &choice);
37 /* Some key was pressed, so autoboot was stopped */
38 - bootmenu_loop(menu, &key, &esc);
39 + bootmenu_loop(menu, &key, &esc, &choice);
43 @@ -110,6 +112,12 @@ static char *bootmenu_choice_entry(void
45 /* no menu key selected, regenerate menu */
48 + menu->active = choice;
49 + if (!menu->last_choiced) {
50 + menu->last_choiced = true;
55 for (i = 0; i < menu->active; ++i)
56 @@ -181,12 +189,19 @@ static int prepare_bootmenu_entry(struct
60 - entry->title = strndup(option, sep - option);
61 + entry->title = malloc((sep - option) + 4);
67 + if (i < ARRAY_SIZE(choice_chars)) {
68 + sprintf(entry->title, "%c. %.*s", choice_chars[i],
69 + (int)(sep - option), option);
71 + sprintf(entry->title, " %.*s", (int)(sep - option), option);
74 entry->command = strdup(sep + 1);
75 if (!entry->command) {
77 @@ -331,6 +346,7 @@ static struct bootmenu_data *bootmenu_cr
81 + menu->last_choiced = false;
83 default_str = env_get("bootmenu_default");
85 @@ -356,9 +372,9 @@ static struct bootmenu_data *bootmenu_cr
87 /* Add Quit entry if entering U-Boot console is disabled */
88 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
89 - entry->title = strdup("U-Boot console");
90 + entry->title = strdup("0. U-Boot console");
92 - entry->title = strdup("Quit");
93 + entry->title = strdup("0. Quit");
103 +#include <linux/ctype.h>
104 #include <linux/delay.h>
105 #include <linux/list.h>
106 #include <watchdog.h>
107 @@ -47,6 +48,17 @@ struct menu {
111 +static int find_choice(char choice)
115 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
116 + if (tolower(choice) == choice_chars[i])
123 * An iterator function for menu items. callback will be called for each item
124 * in m, with m, a pointer to the item, and extra being passed to callback. If
125 @@ -426,7 +445,7 @@ int menu_destroy(struct menu *m)
128 void bootmenu_autoboot_loop(struct bootmenu_data *menu,
129 - enum bootmenu_key *key, int *esc)
130 + enum bootmenu_key *key, int *esc, int *choice)
134 @@ -456,6 +475,19 @@ void bootmenu_autoboot_loop(struct bootm
141 + *choice = find_choice(c);
142 + if ((*choice >= 0 &&
143 + *choice < menu->count - 1)) {
145 + } else if (c == '0') {
146 + *choice = menu->count - 1;
154 @@ -475,10 +507,16 @@ void bootmenu_autoboot_loop(struct bootm
157 void bootmenu_loop(struct bootmenu_data *menu,
158 - enum bootmenu_key *key, int *esc)
159 + enum bootmenu_key *key, int *esc, int *choice)
163 + if (menu->last_choiced) {
164 + menu->last_choiced = false;
172 @@ -504,6 +542,14 @@ void bootmenu_loop(struct bootmenu_data
177 + *choice = find_choice(c);
178 + if ((*choice >= 0 && *choice < menu->count - 1)) {
180 + } else if (c == '0') {
181 + *choice = menu->count - 1;
189 @@ -40,6 +40,7 @@ struct bootmenu_data {
190 int active; /* active menu entry */
191 int count; /* total count of menu entries */
192 struct bootmenu_entry *first; /* first menu entry */
197 @@ -48,11 +49,19 @@ enum bootmenu_key {
204 void bootmenu_autoboot_loop(struct bootmenu_data *menu,
205 - enum bootmenu_key *key, int *esc);
206 + enum bootmenu_key *key, int *esc, int *choice);
207 void bootmenu_loop(struct bootmenu_data *menu,
208 - enum bootmenu_key *key, int *esc);
209 + enum bootmenu_key *key, int *esc, int *choice);
211 +static const char choice_chars[] = {
212 + '1', '2', '3', '4', '5', '6', '7', '8', '9',
213 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
214 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
215 + 'u', 'v', 'w', 'x', 'y', 'z'
218 #endif /* __MENU_H__ */