Skip to contentSkip to conversation

cmd: bootmenu: permit to select bootmenu entry with a shortcut

PATCH Superseded Christian Marangi 2 participants 3 messages May 24, 2025 – May 25, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: bootmenu: permit to select bootmenu entry with a shortcut

Permit to select a bootmenu entry with a key shortcut. This is especially useful in production or testing scenario to aitomate flashing procedure or testing procedure.

The boot entry are changed to append the shortcut key to it.

Example:
1. Run default boot command.
2. Boot system via TFTP.
3. Boot production system from NAND.
4. Boot recovery system from NAND.
5. Load production system via TFTP then write to NAND.
6. Load recovery system via TFTP then write to NAND.
7. Load BL31+U-Boot FIP via TFTP then write to NAND.
8. Load BL2 preloader via TFTP then write to NAND.
9. Reboot.
a. Reset all settings to factory defaults.
0. Exit

0 is always reserved for Exit to console.
On pressing the keyboard key 2, the bootmenu entry 2 is selected and executed.

Up to 34 key shortcut (0 excluded as reserved) are supported from 1-9 and a-z.
If a shortcut key not present in the bootmenu list is pressed, it is simply ignored and eventually the autoboot is interrupted.

Capital A-Z are converted to lower a-z and the related option is selected.

---
 cmd/bootmenu.c | 39 ++++++++++++++++++++++++++++++++++++---
 common/menu.c  | 44 ++++++++++++++++++++++++++++++++++++++++++--
 include/cli.h  |  2 ++
 include/menu.h |  3 +++
 4 files changed, 83 insertions(+), 5 deletions(-)
cmd/bootmenu.c +36 -3
index a5c979079f4..d060cda0ea6 100644
@@ -114,6 +114,12 @@ static char *bootmenu_choice_entry(void *data)
 				++menu->active;
 			/* no menu key selected, regenerate menu */
 			return NULL;
+		case BKEY_SHORTCUT:
+			/* invalid shortcut, regenerate menu */
+			if (cch->shortcut_key >= menu->count - 1)
+				return NULL;
+			menu->active = cch->shortcut_key;
+			fallthrough;
 		case BKEY_SELECT:
 			iter = menu->first;
 			for (i = 0; i < menu->active; ++i)
@@ -161,6 +167,21 @@ static void bootmenu_destroy(struct bootmenu_data *menu)
 	free(menu);
 }
 
+static char bootmenu_entry_shortcut_key(int index)
+{
+	switch (index) {
+	/* 1-9 shortcut key (0 reserved) */
+	case 0 ... 8:
+		return '1' + index;
+	/* a-z shortcut key  */
+	case 9 ... 34:
+		return 'a' + index - 9;
+	/* We support shortcut for up to 34 options (0 reserved) */
+	default:
+		return -ENOENT;
+	}
+}
+
 /**
  * prepare_bootmenu_entry() - generate the bootmenu_xx entries
  *
@@ -184,6 +205,8 @@ static int prepare_bootmenu_entry(struct bootmenu_data *menu,
 	struct bootmenu_entry *iter = *current;
 
 	while ((option = bootmenu_getoption(i))) {
+		char shortcut_key;
+		int len;
 
 		/* bootmenu_[num] format is "[title]=[commands]" */
 		sep = strchr(option, '=');
@@ -196,12 +219,22 @@ static int prepare_bootmenu_entry(struct bootmenu_data *menu,
 		if (!entry)
 			return -ENOMEM;
 
-		entry->title = strndup(option, sep - option);
+		/* Add shotcut key option: %c. %s\0 */
+		len = sep - option + 4;
+
+		entry->title = malloc(len);
 		if (!entry->title) {
 			free(entry);
 			return -ENOMEM;
 		}
 
+		shortcut_key = bootmenu_entry_shortcut_key(i);
+		/* Use emtpy space if entry doesn't support shortcut key */
+		snprintf(entry->title, len, "%c%c %s",
+			 shortcut_key > 0 ? shortcut_key : ' ',
+			 shortcut_key > 0 ? '.' : ' ',
+			 option);
+
 		entry->command = strdup(sep + 1);
 		if (!entry->command) {
 			free(entry->title);
@@ -388,9 +421,9 @@ static struct bootmenu_data *bootmenu_create(int uefi, int delay)
 
 		/* Add Quit entry if exiting bootmenu is disabled */
 		if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
-			entry->title = strdup("Exit");
+			entry->title = strdup("0. Exit");
 		else
-			entry->title = strdup("Quit");
+			entry->title = strdup("0. Quit");
 
 		if (!entry->title) {
 			free(entry);
common/menu.c +42 -2

89 lines not on this page · raw message

include/cli.h +2 -0
index e183d561369..453e88fa96d 100644
@@ -17,12 +17,14 @@
  * @esc_save: Escape characters collected so far
  * @emit_upto: Next index to emit from esc_save
  * @emitting: true if emitting from esc_save
+ * @shortcut_key: Selected shortcut option index
  */
 struct cli_ch_state {
 	int esc_len;
 	char esc_save[8];
 	int emit_upto;
 	bool emitting;
+	int shortcut_key;
 };
 
 /**
include/menu.h +3 -0
index 6cede89b950..54ff3b2e17a 100644
@@ -54,6 +54,9 @@ enum bootmenu_key {
 	BKEY_QUIT,
 	BKEY_SAVE,
 
+	/* shortcut key to select menu option directly */
+	BKEY_SHORTCUT,
+
 	/* 'extra' keys, which are used by menus but not cedit */
 	BKEY_PLUS,
 	BKEY_MINUS,
Signature · 1 line
2.48.1
#1 lore raw

Christian Marangi <[email protected]> [2025-05-25 00:12:52]:

Hi,

thanks a lot for your upstreaming efforts!

Quoted 2 lines

Permit to select a bootmenu entry with a key shortcut. This is especially useful in production or testing scenario to aitomate flashing

aitomate -> automate

Quoted 1 line

0 is always reserved for Exit to console.

BTW this is the only shortcut key which currently doesn't work :-) See below.

cmd/bootmenu.c static char *bootmenu_choice_entry(void *data) · line 119

Quoted 3 lines
cmd/bootmenu.c +3 -0
+		case BKEY_SHORTCUT:
+			/* invalid shortcut, regenerate menu */
+			if (cch->shortcut_key >= menu->count - 1)

IMO it should be:

			if (cch->shortcut_key >= menu->count)

Considering menu with 4 items and user presses '0':

* bootmenu_conv_shortcut_key() returns 3 (menu->count - 1), so shortcut_key = 3

* With the original condition
if (cch->shortcut_key >= menu->count - 1)
if (3 >= 3) → true, so it returns NULL (bug)

* With the fixed condition
if (cch->shortcut_key >= menu->count):
if (3 >= 4) → false, so it correctly proceeds

cmd/bootmenu.c static char *bootmenu_choice_entry(void *data) · line 122

Quoted 3 lines
cmd/bootmenu.c +3 -0
+				return NULL;
+			menu->active = cch->shortcut_key;
+			fallthrough;

Cheers,

Petr

#2 lore raw
Quoted 6 lines · Petr Štetiar

Christian Marangi <[email protected]> [2025-05-25 00:12:52]:

Hi,

thanks a lot for your upstreaming efforts!

Quoted 9 lines
Quoted 2 lines

Permit to select a bootmenu entry with a key shortcut. This is especially useful in production or testing scenario to aitomate flashing

aitomate -> automate

Quoted 1 line

0 is always reserved for Exit to console.

BTW this is the only shortcut key which currently doesn't work :-) See below.

Eh last time bugfix and I forgot to test the 0 case... feel stupid ahhaha

Quoted 12 lines

cmd/bootmenu.c static char *bootmenu_choice_entry(void *data) · line 119

Quoted 3 lines
cmd/bootmenu.c +3 -0
+		case BKEY_SHORTCUT:
+			/* invalid shortcut, regenerate menu */
+			if (cch->shortcut_key >= menu->count - 1)

IMO it should be:

			if (cch->shortcut_key >= menu->count)

Considering menu with 4 items and user presses '0':

* bootmenu_conv_shortcut_key() returns 3 (menu->count - 1), so shortcut_key = 3

Quoted 8 lines

* With the original condition
if (cch->shortcut_key >= menu->count - 1)
if (3 >= 3) → true, so it returns NULL (bug)

* With the fixed condition
if (cch->shortcut_key >= menu->count):
if (3 >= 4) → false, so it correctly proceeds

This is also problematic since pressing 'b' will act as exit. I have a simple fix for this, sending v2.

Quoted 7 lines

cmd/bootmenu.c static char *bootmenu_choice_entry(void *data) · line 122

Quoted 3 lines
cmd/bootmenu.c +3 -0
+				return NULL;
+			menu->active = cch->shortcut_key;
+			fallthrough;

Cheers,

Petr

Signature · 1 line
	Ansuel