uboot-mediatek: update to 2022.07 release
[openwrt/staging/wigyori.git] / package / boot / uboot-mediatek / patches / 210-cmd-bootmenu-add-ability-to-select-item-by-shortkey.patch
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
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 | 77 +++++++++++++++++++++++++++++++++++++++++++++-----
11 1 file changed, 70 insertions(+), 7 deletions(-)
12
13 --- a/cmd/bootmenu.c
14 +++ b/cmd/bootmenu.c
15 @@ -14,6 +14,7 @@
16 #include <menu.h>
17 #include <watchdog.h>
18 #include <malloc.h>
19 +#include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/string.h>
22
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;
27 + int choice = -1;
28 int esc = 0;
29 int i;
30
31 while (1) {
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);
36 } else {
37 /* Some key was pressed, so autoboot was stopped */
38 - bootmenu_loop(menu, &key, &esc);
39 + bootmenu_loop(menu, &key, &esc, &choice);
40 }
41
42 switch (key) {
43 @@ -110,6 +112,12 @@ static char *bootmenu_choice_entry(void
44 ++menu->active;
45 /* no menu key selected, regenerate menu */
46 return NULL;
47 + case KEY_CHOICE:
48 + menu->active = choice;
49 + if (!menu->last_choiced) {
50 + menu->last_choiced = true;
51 + return NULL;
52 + }
53 case KEY_SELECT:
54 iter = menu->first;
55 for (i = 0; i < menu->active; ++i)
56 @@ -181,12 +189,19 @@ static int prepare_bootmenu_entry(struct
57 if (!entry)
58 return -ENOMEM;
59
60 - entry->title = strndup(option, sep - option);
61 + entry->title = malloc((sep - option) + 4);
62 if (!entry->title) {
63 free(entry);
64 return -ENOMEM;
65 }
66
67 + if (i < ARRAY_SIZE(choice_chars)) {
68 + sprintf(entry->title, "%c. %.*s", choice_chars[i],
69 + (int)(sep - option), option);
70 + } else {
71 + sprintf(entry->title, " %.*s", (int)(sep - option), option);
72 + }
73 +
74 entry->command = strdup(sep + 1);
75 if (!entry->command) {
76 free(entry->title);
77 @@ -331,6 +346,7 @@ static struct bootmenu_data *bootmenu_cr
78 menu->delay = delay;
79 menu->active = 0;
80 menu->first = NULL;
81 + menu->last_choiced = false;
82
83 default_str = env_get("bootmenu_default");
84 if (default_str)
85 @@ -356,9 +372,9 @@ static struct bootmenu_data *bootmenu_cr
86
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");
91 else
92 - entry->title = strdup("Quit");
93 + entry->title = strdup("0. Quit");
94
95 if (!entry->title) {
96 free(entry);
97 --- a/common/menu.c
98 +++ b/common/menu.c
99 @@ -9,6 +9,7 @@
100 #include <cli.h>
101 #include <malloc.h>
102 #include <errno.h>
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 {
108 int item_cnt;
109 };
110
111 +static int find_choice(char choice)
112 +{
113 + int i;
114 +
115 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
116 + if (tolower(choice) == choice_chars[i])
117 + return i;
118 +
119 + return -1;
120 +}
121 +
122 /*
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)
126 }
127
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)
131 {
132 int i, c;
133
134 @@ -456,6 +475,19 @@ void bootmenu_autoboot_loop(struct bootm
135 break;
136 default:
137 *key = KEY_NONE;
138 + if (*esc)
139 + break;
140 +
141 + *choice = find_choice(c);
142 + if ((*choice >= 0 &&
143 + *choice < menu->count - 1)) {
144 + *key = KEY_CHOICE;
145 + } else if (c == '0') {
146 + *choice = menu->count - 1;
147 + *key = KEY_CHOICE;
148 + } else {
149 + *key = KEY_NONE;
150 + }
151 break;
152 }
153
154 @@ -475,10 +507,16 @@ void bootmenu_autoboot_loop(struct bootm
155 }
156
157 void bootmenu_loop(struct bootmenu_data *menu,
158 - enum bootmenu_key *key, int *esc)
159 + enum bootmenu_key *key, int *esc, int *choice)
160 {
161 int c;
162
163 + if (menu->last_choiced) {
164 + menu->last_choiced = false;
165 + *key = KEY_SELECT;
166 + return;
167 + }
168 +
169 if (*esc == 1) {
170 if (tstc()) {
171 c = getchar();
172 @@ -504,6 +542,14 @@ void bootmenu_loop(struct bootmenu_data
173 if (c == '\e') {
174 *esc = 1;
175 *key = KEY_NONE;
176 + } else {
177 + *choice = find_choice(c);
178 + if ((*choice >= 0 && *choice < menu->count - 1)) {
179 + *key = KEY_CHOICE;
180 + } else if (c == '0') {
181 + *choice = menu->count - 1;
182 + *key = KEY_CHOICE;
183 + }
184 }
185 break;
186 case 1:
187 --- a/include/menu.h
188 +++ b/include/menu.h
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 */
193 + bool last_choiced;
194 };
195
196 enum bootmenu_key {
197 @@ -48,11 +49,19 @@ enum bootmenu_key {
198 KEY_DOWN,
199 KEY_SELECT,
200 KEY_QUIT,
201 + KEY_CHOICE,
202 };
203
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);
210 +
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'
216 +};
217
218 #endif /* __MENU_H__ */