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 | 28 +++++++++++++++++++++++---
11 common/menu.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
13 include/menu.h | 3 +++
14 4 files changed, 84 insertions(+), 3 deletions(-)
18 @@ -114,6 +114,8 @@ static char *bootmenu_choice_entry(void
20 /* no menu key selected, regenerate menu */
23 + menu->active = cch->choice;
26 for (i = 0; i < menu->active; ++i)
27 @@ -182,6 +184,9 @@ static int prepare_bootmenu_entry(struct
28 unsigned short int i = *index;
29 struct bootmenu_entry *entry = NULL;
30 struct bootmenu_entry *iter = *current;
31 + char *choice_option;
35 while ((option = bootmenu_getoption(i))) {
37 @@ -196,11 +201,28 @@ static int prepare_bootmenu_entry(struct
41 - entry->title = strndup(option, sep - option);
42 + /* Add BKEY_CHOICE support: '%c. %s\0' : len --> len + 4 */
43 + len = sep - option + 4;
45 + choice_option = malloc(len);
46 + if (!choice_option) {
52 + if (!get_choice_char(i, &choice_char))
53 + len = snprintf(choice_option, len, "%c. %s", choice_char, option);
55 + len = snprintf(choice_option, len, " %s", option);
57 + entry->title = strndup(choice_option, len);
63 + free(choice_option);
65 entry->command = strdup(sep + 1);
66 if (!entry->command) {
67 @@ -382,9 +404,9 @@ static struct bootmenu_data *bootmenu_cr
69 /* Add Quit entry if exiting bootmenu is disabled */
70 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
71 - entry->title = strdup("Exit");
72 + entry->title = strdup("0. Exit");
74 - entry->title = strdup("Quit");
75 + entry->title = strdup("0. Quit");
85 +#include <linux/ctype.h>
86 #include <linux/delay.h>
87 #include <linux/list.h>
89 @@ -49,6 +50,33 @@ struct menu {
93 +const char choice_chars[] = {
94 + '1', '2', '3', '4', '5', '6', '7', '8', '9',
95 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
96 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
97 + 'u', 'v', 'w', 'x', 'y', 'z'
100 +static int find_choice(char choice)
104 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
105 + if (tolower(choice) == choice_chars[i])
111 +int get_choice_char(int index, char *result)
113 + if (index < ARRAY_SIZE(choice_chars))
114 + *result = choice_chars[index];
121 * An iterator function for menu items. callback will be called for each item
122 * in m, with m, a pointer to the item, and extra being passed to callback. If
123 @@ -441,6 +469,7 @@ enum bootmenu_key bootmenu_autoboot_loop
125 enum bootmenu_key key = BKEY_NONE;
129 while (menu->delay > 0) {
131 @@ -458,6 +487,18 @@ enum bootmenu_key bootmenu_autoboot_loop
135 + choice = find_choice(c);
136 + if ((choice >= 0 &&
137 + choice < menu->count - 1)) {
138 + cch->choice = choice;
141 + } else if (c == '0') {
142 + cch->choice = menu->count - 1;
147 ichar = cli_ch_process(cch, c);
150 @@ -537,6 +578,7 @@ enum bootmenu_key bootmenu_loop(struct b
152 enum bootmenu_key key;
156 c = cli_ch_process(cch, 0);
158 @@ -548,6 +590,18 @@ enum bootmenu_key bootmenu_loop(struct b
163 + choice = find_choice(c);
164 + if ((choice >= 0 &&
165 + choice < menu->count - 1)) {
166 + cch->choice = choice;
167 + return BKEY_CHOICE;
169 + } else if (c == '0') {
170 + cch->choice = menu->count - 1;
171 + return BKEY_CHOICE;
174 c = cli_ch_process(cch, c);
179 @@ -23,6 +23,8 @@ struct cli_ch_state {
183 + /* mediatek bootmenu choice feature */
190 @@ -37,6 +37,8 @@ int menu_default_choice(struct menu *m,
192 int menu_show(int bootdelay);
194 +int get_choice_char(int index, char *result);
196 struct bootmenu_data {
197 int delay; /* delay for autoboot */
198 int active; /* active menu entry */
199 @@ -51,6 +53,7 @@ enum bootmenu_key {