315f7f92a3d794562afd1fe1d54fe0887f613b47
[openwrt/staging/blocktrron.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 @@ -89,6 +89,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 @@ -96,10 +97,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 @@ -113,6 +114,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 @@ -170,6 +177,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 @@ -184,11 +194,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 @@ -334,6 +357,7 @@ static struct bootmenu_data *bootmenu_cr
88 menu->delay = delay;
89 menu->active = 0;
90 menu->first = NULL;
91 + menu->last_choiced = false;
92
93 default_str = env_get("bootmenu_default");
94 if (default_str)
95 @@ -369,9 +393,9 @@ static struct bootmenu_data *bootmenu_cr
96
97 /* Add Quit entry if entering U-Boot console is disabled */
98 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
99 - entry->title = strdup("U-Boot console");
100 + entry->title = strdup("0. U-Boot console");
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 @@ -428,7 +455,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 @@ -463,6 +490,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 @@ -483,7 +523,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 @@ -515,6 +556,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 @@ -524,11 +579,16 @@ 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, int *choice)
209 {
210 enum bootmenu_key key;
211 int c;
212
213 + if (menu->last_choiced) {
214 + menu->last_choiced = false;
215 + return BKEY_SELECT;
216 + }
217 +
218 c = cli_ch_process(cch, 0);
219 if (!c) {
220 while (!c && !tstc()) {
221 @@ -542,7 +602,7 @@ enum bootmenu_key bootmenu_loop(struct b
222 }
223 }
224
225 - key = bootmenu_conv_key(c);
226 + key = bootmenu_conv_key(menu, c, choice);
227
228 return key;
229 }
230 --- a/include/menu.h
231 +++ b/include/menu.h
232 @@ -6,6 +6,8 @@
233 #ifndef __MENU_H__
234 #define __MENU_H__
235
236 +#include <linux/ctype.h>
237 +
238 struct cli_ch_state;
239 struct menu;
240
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);
247
248 /**
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 */
254 + bool last_choiced;
255 };
256
257 /** enum bootmenu_key - keys that can be returned by the bootmenu */
258 @@ -54,6 +59,7 @@ enum bootmenu_key {
259 BKEY_MINUS,
260 BKEY_SPACE,
261 BKEY_SAVE,
262 + BKEY_CHOICE,
263
264 BKEY_COUNT,
265 };
266 @@ -76,7 +82,7 @@ enum bootmenu_key {
267 * anything else: KEY_NONE
268 */
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);
272
273 /**
274 * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled
275 @@ -102,7 +108,7 @@ enum bootmenu_key bootmenu_autoboot_loop
276 * Space: BKEY_SPACE
277 */
278 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
279 - struct cli_ch_state *cch);
280 + struct cli_ch_state *cch, int *choice);
281
282 /**
283 * bootmenu_conv_key() - Convert a U-Boot keypress into a menu key
284 @@ -110,6 +116,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
287 */
288 -enum bootmenu_key bootmenu_conv_key(int ichar);
289 +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
290 + int *choice);
291
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)
296 cli_ch_init(cch);
297
298 while (1) {
299 - key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
300 + key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch, NULL);
301
302 switch (key) {
303 case BKEY_UP:
304 @@ -1937,7 +1937,7 @@ char *eficonfig_choice_change_boot_order
305
306 cli_ch_init(cch);
307 while (1) {
308 - key = bootmenu_loop(NULL, cch);
309 + key = bootmenu_loop(NULL, cch, NULL);
310
311 switch (key) {
312 case BKEY_PLUS:
313 --- a/boot/bootflow_menu.c
314 +++ b/boot/bootflow_menu.c
315 @@ -231,7 +231,7 @@ int bootflow_menu_run(struct bootstd_pri
316
317 key = 0;
318 if (ichar) {
319 - key = bootmenu_conv_key(ichar);
320 + key = bootmenu_conv_key(NULL, ichar, NULL);
321 if (key == BKEY_NONE)
322 key = ichar;
323 }