1 From 26d4e2e58bf0007db74b47c783785c3305ea1fa0 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 17/23] 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 @@ -38,6 +39,7 @@ struct bootmenu_data {
24 int active; /* active menu entry */
25 int count; /* total count of menu entries */
26 struct bootmenu_entry *first; /* first menu entry */
31 @@ -46,8 +48,27 @@ enum bootmenu_key {
38 +static const char choice_chars[] = {
39 + '1', '2', '3', '4', '5', '6', '7', '8', '9',
40 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
41 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
42 + 'u', 'v', 'w', 'x', 'y', 'z'
45 +static int find_choice(char choice)
49 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
50 + if (tolower(choice) == choice_chars[i])
56 static char *bootmenu_getoption(unsigned short int n)
58 char name[MAX_ENV_SIZE];
59 @@ -82,7 +103,7 @@ static void bootmenu_print_entry(void *d
62 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
63 - enum bootmenu_key *key, int *esc)
64 + enum bootmenu_key *key, int *esc, int *choice)
68 @@ -115,6 +136,19 @@ static void bootmenu_autoboot_loop(struc
75 + *choice = find_choice(c);
76 + if ((*choice >= 0 &&
77 + *choice < menu->count - 1)) {
79 + } else if (c == '0') {
80 + *choice = menu->count - 1;
88 @@ -136,10 +170,16 @@ static void bootmenu_autoboot_loop(struc
91 static void bootmenu_loop(struct bootmenu_data *menu,
92 - enum bootmenu_key *key, int *esc)
93 + enum bootmenu_key *key, int *esc, int *choice)
97 + if (menu->last_choiced) {
98 + menu->last_choiced = false;
106 @@ -165,6 +205,14 @@ static void bootmenu_loop(struct bootmen
111 + *choice = find_choice(c);
112 + if ((*choice >= 0 && *choice < menu->count - 1)) {
114 + } else if (c == '0') {
115 + *choice = menu->count - 1;
121 @@ -216,16 +264,17 @@ static char *bootmenu_choice_entry(void
122 struct bootmenu_data *menu = data;
123 struct bootmenu_entry *iter;
124 enum bootmenu_key key = KEY_NONE;
130 if (menu->delay >= 0) {
131 /* Autoboot was not stopped */
132 - bootmenu_autoboot_loop(menu, &key, &esc);
133 + bootmenu_autoboot_loop(menu, &key, &esc, &choice);
135 /* Some key was pressed, so autoboot was stopped */
136 - bootmenu_loop(menu, &key, &esc);
137 + bootmenu_loop(menu, &key, &esc, &choice);
141 @@ -239,6 +288,12 @@ static char *bootmenu_choice_entry(void
143 /* no menu key selected, regenerate menu */
146 + menu->active = choice;
147 + if (!menu->last_choiced) {
148 + menu->last_choiced = true;
153 for (i = 0; i < menu->active; ++i)
154 @@ -294,6 +349,7 @@ static struct bootmenu_data *bootmenu_cr
158 + menu->last_choiced = false;
160 default_str = env_get("bootmenu_default");
162 @@ -311,12 +367,19 @@ static struct bootmenu_data *bootmenu_cr
166 - entry->title = malloc(len + 1);
167 + entry->title = malloc(len + 4);
172 - memcpy(entry->title, option, len);
174 + if (i < ARRAY_SIZE(choice_chars)) {
175 + len = sprintf(entry->title, "%c. %.*s", choice_chars[i],
178 + len = sprintf(entry->title, " %.*s", len, option);
181 entry->title[len] = 0;
183 len = strlen(sep + 1);
184 @@ -353,7 +416,7 @@ static struct bootmenu_data *bootmenu_cr
188 - entry->title = strdup("U-Boot console");
189 + entry->title = strdup("0. U-Boot console");