1 // SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
11 #include <efi_variable.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
20 /* maximum bootmenu entries */
23 /* maximal size of bootmenu env
24 * 9 = strlen("bootmenu_")
25 * 2 = strlen(MAX_COUNT)
28 #define MAX_ENV_SIZE (9 + 2 + 1)
31 BOOTMENU_RET_SUCCESS = 0,
38 BOOTMENU_TYPE_NONE = 0,
39 BOOTMENU_TYPE_BOOTMENU,
40 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
43 struct bootmenu_entry {
44 unsigned short int num; /* unique number 0 .. MAX_COUNT */
45 char key[3]; /* key identifier of number */
46 u16 *title; /* title of entry */
47 char *command; /* hush command of entry */
48 enum boot_type type; /* boot type of entry */
49 u16 bootorder; /* order for each boot type */
50 struct bootmenu_data *menu; /* this bootmenu */
51 struct bootmenu_entry *next; /* next menu entry (num+1) */
54 struct bootmenu_data {
55 int delay; /* delay for autoboot */
56 int active; /* active menu entry */
57 int count; /* total count of menu entries */
58 struct bootmenu_entry *first; /* first menu entry */
69 static char *bootmenu_getoption(unsigned short int n)
71 char name[MAX_ENV_SIZE];
76 sprintf(name, "bootmenu_%d", n);
80 static void bootmenu_print_entry(void *data)
82 struct bootmenu_entry *entry = data;
83 int reverse = (entry->menu->active == entry->num);
86 * Move cursor to line where the entry will be drown (entry->num)
87 * First 3 lines contain bootmenu header + 1 empty line
89 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
92 puts(ANSI_COLOR_REVERSE);
94 printf("%ls", entry->title);
97 puts(ANSI_COLOR_RESET);
100 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
101 enum bootmenu_key *key, int *esc)
105 while (menu->delay > 0) {
106 printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
107 printf("Hit any key to stop autoboot: %d ", menu->delay);
108 for (i = 0; i < 100; ++i) {
143 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
144 puts(ANSI_CLEAR_LINE);
146 if (menu->delay == 0)
150 static void bootmenu_loop(struct bootmenu_data *menu,
151 enum bootmenu_key *key, int *esc)
176 /* First char of ANSI escape sequence '\e' */
183 /* Second char of ANSI '[' */
188 /* Alone ESC key was pressed */
190 *esc = (c == '\e') ? 1 : 0;
195 /* Third char of ANSI (number '1') - optional */
196 if (*esc == 2 && c == '1') {
204 /* ANSI 'A' - key up was pressed */
207 /* ANSI 'B' - key down was pressed */
210 /* other key was pressed */
217 /* enter key was pressed */
226 static char *bootmenu_choice_entry(void *data)
228 struct bootmenu_data *menu = data;
229 struct bootmenu_entry *iter;
230 enum bootmenu_key key = KEY_NONE;
235 if (menu->delay >= 0) {
236 /* Autoboot was not stopped */
237 bootmenu_autoboot_loop(menu, &key, &esc);
239 /* Some key was pressed, so autoboot was stopped */
240 bootmenu_loop(menu, &key, &esc);
245 if (menu->active > 0)
247 /* no menu key selected, regenerate menu */
250 if (menu->active < menu->count - 1)
252 /* no menu key selected, regenerate menu */
256 for (i = 0; i < menu->active; ++i)
260 /* Quit by choosing the last entry - U-Boot console */
271 debug("bootmenu: this should not happen");
275 static void bootmenu_destroy(struct bootmenu_data *menu)
277 struct bootmenu_entry *iter = menu->first;
278 struct bootmenu_entry *next;
291 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
293 * This function read the "bootmenu_x" U-Boot environment variable
294 * and generate the bootmenu entries.
296 * @menu: pointer to the bootmenu structure
297 * @current: pointer to the last bootmenu entry list
298 * @index: pointer to the index of the last bootmenu entry,
299 * the number of bootmenu entry is added by this function
300 * Return: 1 on success, negative value on error
302 static int prepare_bootmenu_entry(struct bootmenu_data *menu,
303 struct bootmenu_entry **current,
304 unsigned short int *index)
309 unsigned short int i = *index;
310 struct bootmenu_entry *entry = NULL;
311 struct bootmenu_entry *iter = *current;
313 while ((option = bootmenu_getoption(i))) {
316 sep = strchr(option, '=');
318 printf("Invalid bootmenu entry: %s\n", option);
322 entry = malloc(sizeof(struct bootmenu_entry));
327 buf = calloc(1, (len + 1) * sizeof(u16));
333 utf8_utf16_strncpy(&buf, option, len);
335 len = strlen(sep + 1);
336 entry->command = malloc(len + 1);
337 if (!entry->command) {
342 memcpy(entry->command, sep + 1, len);
343 entry->command[len] = 0;
345 sprintf(entry->key, "%d", i);
349 entry->type = BOOTMENU_TYPE_BOOTMENU;
350 entry->bootorder = i;
361 if (i == MAX_COUNT - 1)
372 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
374 * This function read the "BootOrder" UEFI variable
375 * and generate the bootmenu entries in the order of "BootOrder".
377 * @menu: pointer to the bootmenu structure
378 * @current: pointer to the last bootmenu entry list
379 * @index: pointer to the index of the last bootmenu entry,
380 * the number of uefi entry is added by this function
381 * Return: 1 on success, negative value on error
383 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
384 struct bootmenu_entry **current,
385 unsigned short int *index)
390 efi_uintn_t num, size;
392 struct efi_load_option lo;
393 u16 varname[] = u"Boot####";
394 unsigned short int i = *index;
395 struct bootmenu_entry *entry = NULL;
396 struct bootmenu_entry *iter = *current;
398 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
402 num = size / sizeof(u16);
403 for (j = 0; j < num; j++) {
404 entry = malloc(sizeof(struct bootmenu_entry));
408 efi_create_indexed_name(varname, sizeof(varname),
409 "Boot", bootorder[j]);
410 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
414 ret = efi_deserialize_load_option(&lo, load_option, &size);
415 if (ret != EFI_SUCCESS) {
416 log_warning("Invalid load option for %ls\n", varname);
422 if (lo.attributes & LOAD_OPTION_ACTIVE) {
423 entry->title = u16_strdup(lo.label);
430 entry->command = strdup("bootefi bootmgr");
431 sprintf(entry->key, "%d", i);
434 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
435 entry->bootorder = bootorder[j];
449 if (i == MAX_COUNT - 1)
460 static struct bootmenu_data *bootmenu_create(int delay)
463 unsigned short int i = 0;
464 struct bootmenu_data *menu;
465 struct bootmenu_entry *iter = NULL;
466 struct bootmenu_entry *entry;
469 menu = malloc(sizeof(struct bootmenu_data));
477 default_str = env_get("bootmenu_default");
479 menu->active = (int)simple_strtol(default_str, NULL, 10);
481 ret = prepare_bootmenu_entry(menu, &iter, &i);
485 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
486 if (i < MAX_COUNT - 1) {
487 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
488 if (ret < 0 && ret != -ENOENT)
493 /* Add U-Boot console entry at the end */
494 if (i <= MAX_COUNT - 1) {
495 entry = malloc(sizeof(struct bootmenu_entry));
499 /* Add Quit entry if entering U-Boot console is disabled */
500 if (IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE))
501 entry->title = u16_strdup(u"U-Boot console");
503 entry->title = u16_strdup(u"Quit");
510 entry->command = strdup("");
511 if (!entry->command) {
517 sprintf(entry->key, "%d", i);
521 entry->type = BOOTMENU_TYPE_NONE;
535 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
536 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
543 bootmenu_destroy(menu);
547 static void menu_display_statusline(struct menu *m)
549 struct bootmenu_entry *entry;
550 struct bootmenu_data *menu;
552 if (menu_default_choice(m, (void *)&entry) < 0)
557 printf(ANSI_CURSOR_POSITION, 1, 1);
558 puts(ANSI_CLEAR_LINE);
559 printf(ANSI_CURSOR_POSITION, 2, 3);
560 puts("*** U-Boot Boot Menu ***");
561 puts(ANSI_CLEAR_LINE_TO_END);
562 printf(ANSI_CURSOR_POSITION, 3, 1);
563 puts(ANSI_CLEAR_LINE);
565 /* First 3 lines are bootmenu header + 2 empty lines between entries */
566 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
567 puts(ANSI_CLEAR_LINE);
568 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
569 puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
570 puts(ANSI_CLEAR_LINE_TO_END);
571 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
572 puts(ANSI_CLEAR_LINE);
575 static void handle_uefi_bootnext(void)
581 /* Initialize EFI drivers */
582 ret = efi_init_obj_list();
583 if (ret != EFI_SUCCESS) {
584 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
585 ret & ~EFI_ERROR_MASK);
590 /* If UEFI BootNext variable is set, boot the BootNext load option */
592 ret = efi_get_variable_int(u"BootNext",
593 &efi_global_variable_guid,
594 NULL, &size, &bootnext, NULL);
595 if (ret == EFI_SUCCESS)
596 /* BootNext does exist here, try to boot */
597 run_command("bootefi bootmgr", 0);
600 static enum bootmenu_ret bootmenu_show(int delay)
606 char *command = NULL;
608 struct bootmenu_entry *iter;
609 int ret = BOOTMENU_RET_SUCCESS;
610 struct bootmenu_data *bootmenu;
611 efi_status_t efi_ret = EFI_SUCCESS;
614 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
615 handle_uefi_bootnext();
617 /* If delay is 0 do not create menu, just run first entry */
619 option = bootmenu_getoption(0);
621 puts("bootmenu option 0 was not found\n");
622 return BOOTMENU_RET_FAIL;
624 sep = strchr(option, '=');
626 puts("bootmenu option 0 is invalid\n");
627 return BOOTMENU_RET_FAIL;
629 cmd_ret = run_command(sep + 1, 0);
630 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
633 bootmenu = bootmenu_create(delay);
635 return BOOTMENU_RET_FAIL;
637 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
638 bootmenu_print_entry, bootmenu_choice_entry,
641 bootmenu_destroy(bootmenu);
642 return BOOTMENU_RET_FAIL;
645 for (iter = bootmenu->first; iter; iter = iter->next) {
646 if (menu_item_add(menu, iter->key, iter) != 1)
650 /* Default menu entry is always first */
651 menu_default_set(menu, "0");
653 puts(ANSI_CURSOR_HIDE);
654 puts(ANSI_CLEAR_CONSOLE);
655 printf(ANSI_CURSOR_POSITION, 1, 1);
659 if (menu_get_choice(menu, &choice) == 1) {
661 title = u16_strdup(iter->title);
662 command = strdup(iter->command);
664 /* last entry is U-Boot console or Quit */
665 if (iter->num == iter->menu->count - 1) {
666 ret = BOOTMENU_RET_QUIT;
674 * If the selected entry is UEFI BOOT####, set the BootNext variable.
675 * Then uefi bootmgr is invoked by the preset command in iter->command.
677 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
678 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
680 * UEFI specification requires BootNext variable needs non-volatile
681 * attribute, but this BootNext is only used inside of U-Boot and
682 * removed by efi bootmgr once BootNext is processed.
683 * So this BootNext can be volatile.
685 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
686 EFI_VARIABLE_BOOTSERVICE_ACCESS |
687 EFI_VARIABLE_RUNTIME_ACCESS,
688 sizeof(u16), &iter->bootorder, false);
689 if (efi_ret != EFI_SUCCESS)
696 bootmenu_destroy(bootmenu);
699 puts(ANSI_CURSOR_SHOW);
700 puts(ANSI_CLEAR_CONSOLE);
701 printf(ANSI_CURSOR_POSITION, 1, 1);
704 if (title && command) {
705 debug("Starting entry '%ls'\n", title);
707 if (efi_ret == EFI_SUCCESS)
708 cmd_ret = run_command(command, 0);
712 #ifdef CONFIG_POSTBOOTMENU
713 run_command(CONFIG_POSTBOOTMENU, 0);
716 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
717 ret = BOOTMENU_RET_FAIL;
722 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
723 int menu_show(int bootdelay)
728 ret = bootmenu_show(bootdelay);
730 if (ret == BOOTMENU_RET_UPDATED)
733 if (!IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE)) {
734 if (ret == BOOTMENU_RET_QUIT) {
735 /* default boot process */
736 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
737 run_command("bootefi bootmgr", 0);
739 run_command("run bootcmd", 0);
746 return -1; /* -1 - abort boot and run monitor code */
750 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
752 char *delay_str = NULL;
755 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
756 delay = CONFIG_BOOTDELAY;
763 delay_str = env_get("bootmenu_delay");
766 delay = (int)simple_strtol(delay_str, NULL, 10);
768 bootmenu_show(delay);
773 bootmenu, 2, 1, do_bootmenu,
774 "ANSI terminal bootmenu",
776 " - show ANSI terminal bootmenu with autoboot delay"