2022-08-07 10:06:56 +00:00
|
|
|
From 5a15437610e8e8c68dc347845a83d0cbad80ca08 Mon Sep 17 00:00:00 2001
|
2021-02-20 13:04:38 +00:00
|
|
|
From: Weijie Gao <weijie.gao@mediatek.com>
|
|
|
|
Date: Tue, 19 Jan 2021 10:58:48 +0800
|
2022-08-07 10:06:56 +00:00
|
|
|
Subject: [PATCH 51/71] cmd: bootmenu: add ability to select item by shortkey
|
2021-02-20 13:04:38 +00:00
|
|
|
|
|
|
|
Add ability to use shortkey to select item for bootmenu command
|
|
|
|
|
|
|
|
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
|
|
|
|
---
|
2022-08-07 10:06:56 +00:00
|
|
|
cmd/bootmenu.c | 34 ++++++++++++++++++++++++-----
|
|
|
|
common/menu.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--
|
|
|
|
include/menu.h | 12 +++++++----
|
|
|
|
3 files changed, 93 insertions(+), 11 deletions(-)
|
2021-02-20 13:04:38 +00:00
|
|
|
|
|
|
|
--- a/cmd/bootmenu.c
|
|
|
|
+++ b/cmd/bootmenu.c
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -87,16 +87,17 @@ static char *bootmenu_choice_entry(void
|
2022-07-11 10:02:49 +00:00
|
|
|
struct bootmenu_data *menu = data;
|
|
|
|
struct bootmenu_entry *iter;
|
|
|
|
enum bootmenu_key key = KEY_NONE;
|
|
|
|
+ int choice = -1;
|
|
|
|
int esc = 0;
|
|
|
|
int i;
|
2021-02-20 13:04:38 +00:00
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
while (1) {
|
|
|
|
if (menu->delay >= 0) {
|
|
|
|
/* Autoboot was not stopped */
|
|
|
|
- bootmenu_autoboot_loop(menu, &key, &esc);
|
|
|
|
+ bootmenu_autoboot_loop(menu, &key, &esc, &choice);
|
|
|
|
} else {
|
|
|
|
/* Some key was pressed, so autoboot was stopped */
|
|
|
|
- bootmenu_loop(menu, &key, &esc);
|
|
|
|
+ bootmenu_loop(menu, &key, &esc, &choice);
|
|
|
|
}
|
2021-02-20 13:04:38 +00:00
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
switch (key) {
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -110,6 +111,12 @@ static char *bootmenu_choice_entry(void
|
2022-07-11 10:02:49 +00:00
|
|
|
++menu->active;
|
|
|
|
/* no menu key selected, regenerate menu */
|
|
|
|
return NULL;
|
|
|
|
+ case KEY_CHOICE:
|
|
|
|
+ menu->active = choice;
|
|
|
|
+ if (!menu->last_choiced) {
|
|
|
|
+ menu->last_choiced = true;
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
case KEY_SELECT:
|
|
|
|
iter = menu->first;
|
|
|
|
for (i = 0; i < menu->active; ++i)
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -167,6 +174,9 @@ static int prepare_bootmenu_entry(struct
|
|
|
|
unsigned short int i = *index;
|
|
|
|
struct bootmenu_entry *entry = NULL;
|
|
|
|
struct bootmenu_entry *iter = *current;
|
|
|
|
+ char *choice_option;
|
|
|
|
+ char choice_char;
|
|
|
|
+ int len;
|
|
|
|
|
|
|
|
while ((option = bootmenu_getoption(i))) {
|
|
|
|
|
|
|
|
@@ -181,11 +191,24 @@ static int prepare_bootmenu_entry(struct
|
2022-07-11 10:02:49 +00:00
|
|
|
if (!entry)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
- entry->title = strndup(option, sep - option);
|
2022-08-07 10:06:56 +00:00
|
|
|
+ /* Add KEY_CHOICE support: '%d. %s\0' : len --> len + 4 */
|
|
|
|
+ len = sep - option + 4;
|
|
|
|
+ choice_option = malloc(len);
|
|
|
|
+ if (!choice_option) {
|
|
|
|
+ free(entry->title);
|
|
|
|
+ free(entry);
|
|
|
|
+ return -ENOMEM;
|
|
|
|
+ }
|
|
|
|
+ if (!get_choice_char(i, &choice_char))
|
|
|
|
+ len = snprintf(choice_option, len, "%c. %s", choice_char, option);
|
|
|
|
+ else
|
|
|
|
+ len = snprintf(choice_option, len, " %s", option);
|
|
|
|
+ entry->title = strndup(choice_option, len);
|
2022-07-11 10:02:49 +00:00
|
|
|
if (!entry->title) {
|
|
|
|
free(entry);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2022-08-07 10:06:56 +00:00
|
|
|
+ free(choice_option);
|
2022-07-11 10:02:49 +00:00
|
|
|
|
|
|
|
entry->command = strdup(sep + 1);
|
|
|
|
if (!entry->command) {
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -331,6 +354,7 @@ static struct bootmenu_data *bootmenu_cr
|
2022-07-11 10:02:49 +00:00
|
|
|
menu->delay = delay;
|
|
|
|
menu->active = 0;
|
|
|
|
menu->first = NULL;
|
|
|
|
+ menu->last_choiced = false;
|
|
|
|
|
|
|
|
default_str = env_get("bootmenu_default");
|
|
|
|
if (default_str)
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -356,9 +380,9 @@ static struct bootmenu_data *bootmenu_cr
|
2022-07-11 10:02:49 +00:00
|
|
|
|
|
|
|
/* Add Quit entry if entering U-Boot console is disabled */
|
|
|
|
if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
|
|
|
|
- entry->title = strdup("U-Boot console");
|
|
|
|
+ entry->title = strdup("0. U-Boot console");
|
|
|
|
else
|
|
|
|
- entry->title = strdup("Quit");
|
|
|
|
+ entry->title = strdup("0. Quit");
|
|
|
|
|
|
|
|
if (!entry->title) {
|
|
|
|
free(entry);
|
|
|
|
--- a/common/menu.c
|
|
|
|
+++ b/common/menu.c
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -47,6 +47,33 @@ struct menu {
|
2022-07-11 10:02:49 +00:00
|
|
|
int item_cnt;
|
|
|
|
};
|
|
|
|
|
2022-08-07 10:06:56 +00:00
|
|
|
+const char choice_chars[] = {
|
|
|
|
+ '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
|
|
|
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
|
|
|
|
+ 'u', 'v', 'w', 'x', 'y', 'z'
|
|
|
|
+};
|
|
|
|
+
|
2021-02-20 13:04:38 +00:00
|
|
|
+static int find_choice(char choice)
|
|
|
|
+{
|
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
|
|
|
|
+ if (tolower(choice) == choice_chars[i])
|
|
|
|
+ return i;
|
|
|
|
+
|
|
|
|
+ return -1;
|
|
|
|
+}
|
2022-08-07 10:06:56 +00:00
|
|
|
+
|
|
|
|
+int get_choice_char(int index, char *result)
|
|
|
|
+{
|
|
|
|
+ if (index < ARRAY_SIZE(choice_chars))
|
|
|
|
+ *result = choice_chars[index];
|
|
|
|
+ else
|
|
|
|
+ return -1;
|
|
|
|
+ return 0;
|
|
|
|
+}
|
2021-02-20 13:04:38 +00:00
|
|
|
+
|
2022-07-11 10:02:49 +00:00
|
|
|
/*
|
|
|
|
* An iterator function for menu items. callback will be called for each item
|
|
|
|
* in m, with m, a pointer to the item, and extra being passed to callback. If
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -426,7 +453,7 @@ int menu_destroy(struct menu *m)
|
2021-02-20 13:04:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
void bootmenu_autoboot_loop(struct bootmenu_data *menu,
|
|
|
|
- enum bootmenu_key *key, int *esc)
|
|
|
|
+ enum bootmenu_key *key, int *esc, int *choice)
|
2021-02-20 13:04:38 +00:00
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -456,6 +483,19 @@ void bootmenu_autoboot_loop(struct bootm
|
2021-02-20 13:04:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*key = KEY_NONE;
|
|
|
|
+ if (*esc)
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ *choice = find_choice(c);
|
|
|
|
+ if ((*choice >= 0 &&
|
|
|
|
+ *choice < menu->count - 1)) {
|
|
|
|
+ *key = KEY_CHOICE;
|
|
|
|
+ } else if (c == '0') {
|
|
|
|
+ *choice = menu->count - 1;
|
|
|
|
+ *key = KEY_CHOICE;
|
|
|
|
+ } else {
|
|
|
|
+ *key = KEY_NONE;
|
|
|
|
+ }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -475,10 +515,16 @@ void bootmenu_autoboot_loop(struct bootm
|
2021-02-20 13:04:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
void bootmenu_loop(struct bootmenu_data *menu,
|
|
|
|
- enum bootmenu_key *key, int *esc)
|
|
|
|
+ enum bootmenu_key *key, int *esc, int *choice)
|
2021-02-20 13:04:38 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
+ if (menu->last_choiced) {
|
|
|
|
+ menu->last_choiced = false;
|
|
|
|
+ *key = KEY_SELECT;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
if (*esc == 1) {
|
|
|
|
if (tstc()) {
|
|
|
|
c = getchar();
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -504,6 +550,14 @@ void bootmenu_loop(struct bootmenu_data
|
2021-02-20 13:04:38 +00:00
|
|
|
if (c == '\e') {
|
|
|
|
*esc = 1;
|
|
|
|
*key = KEY_NONE;
|
|
|
|
+ } else {
|
|
|
|
+ *choice = find_choice(c);
|
|
|
|
+ if ((*choice >= 0 && *choice < menu->count - 1)) {
|
|
|
|
+ *key = KEY_CHOICE;
|
|
|
|
+ } else if (c == '0') {
|
|
|
|
+ *choice = menu->count - 1;
|
|
|
|
+ *key = KEY_CHOICE;
|
|
|
|
+ }
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2022-07-11 10:02:49 +00:00
|
|
|
--- a/include/menu.h
|
|
|
|
+++ b/include/menu.h
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -2,10 +2,11 @@
|
|
|
|
/*
|
|
|
|
* Copyright 2010-2011 Calxeda, Inc.
|
|
|
|
*/
|
|
|
|
-
|
|
|
|
#ifndef __MENU_H__
|
|
|
|
#define __MENU_H__
|
|
|
|
|
|
|
|
+#include <linux/ctype.h>
|
|
|
|
+
|
|
|
|
struct menu;
|
|
|
|
|
|
|
|
struct menu *menu_create(char *title, int timeout, int prompt,
|
|
|
|
@@ -18,6 +19,8 @@ int menu_get_choice(struct menu *m, void
|
|
|
|
int menu_item_add(struct menu *m, char *item_key, void *item_data);
|
|
|
|
int menu_destroy(struct menu *m);
|
|
|
|
int menu_default_choice(struct menu *m, void **choice);
|
|
|
|
+/* Add KEY_CHOICE support */
|
|
|
|
+int get_choice_char(int index, char *result);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* menu_show() Show a boot menu
|
|
|
|
@@ -40,6 +43,7 @@ struct bootmenu_data {
|
2022-07-11 10:02:49 +00:00
|
|
|
int active; /* active menu entry */
|
|
|
|
int count; /* total count of menu entries */
|
|
|
|
struct bootmenu_entry *first; /* first menu entry */
|
|
|
|
+ bool last_choiced;
|
|
|
|
};
|
2021-02-20 13:04:38 +00:00
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
enum bootmenu_key {
|
2022-08-07 10:06:56 +00:00
|
|
|
@@ -48,11 +52,11 @@ enum bootmenu_key {
|
2022-07-11 10:02:49 +00:00
|
|
|
KEY_DOWN,
|
|
|
|
KEY_SELECT,
|
|
|
|
KEY_QUIT,
|
|
|
|
+ KEY_CHOICE,
|
|
|
|
};
|
2021-02-20 13:04:38 +00:00
|
|
|
|
2022-07-11 10:02:49 +00:00
|
|
|
void bootmenu_autoboot_loop(struct bootmenu_data *menu,
|
|
|
|
- enum bootmenu_key *key, int *esc);
|
|
|
|
+ enum bootmenu_key *key, int *esc, int *choice);
|
|
|
|
void bootmenu_loop(struct bootmenu_data *menu,
|
|
|
|
- enum bootmenu_key *key, int *esc);
|
2022-08-07 10:06:56 +00:00
|
|
|
-
|
2022-07-11 10:02:49 +00:00
|
|
|
+ enum bootmenu_key *key, int *esc, int *choice);
|
|
|
|
#endif /* __MENU_H__ */
|