Message ID | 20220225013257.15674-2-masahisa.kojima@linaro.org |
---|---|
State | New |
Headers | show |
Series | enable menu-driven boot device selection | expand |
On 2/25/22 02:32, Masahisa Kojima wrote: > This patch enables the menu-driven boot device selection. > User can select the Boot#### included in BootOrder variable. > > If user quits thie menu, or the selected Boot#### fails to boot, > efi bootmgr continues to boot in accordance with BootOrder variable. > > This commit also moves the user input handling from cmd/bootmenu.c > to common/menu.c to reuse it from efi boot menu. > > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org> With the series applied and CONFIG_EFI_BOOT_MENU=y I first see a line "Hit any key to stop autoboot:" with a count down and then the UEFI menu with a countdown. This duplication of countdowns does not make sense. This is the content of the new menu: *** U-Boot EFI Boot Manager *** Boot Manager Boot Manager maintenance Quit When selecting " Boot Manager maintenance" I see a second menu: *** U-Boot EFI Boot Manager *** Add Boot Option Delete Boot Option Change Boot Order Quit Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit When I select "Add Boot Option" the system (qemu_arm64_defconfig) simply hangs. Why do we need the first menu? Compare it to your laptop experience when pressing the boot menu key (often <F12> or <F8>). Can't we have a simple countdown: "Hit any key for boot menu" If a key is hit show a menu showing the different boot options and the distro-boot devices: Debian (0001) Ubuntu (0002) SATA(1) SATA(2) NVMe(1) Boot manager settings Command line Simon has proposed a patch series "Initial implementation of standard boot", https://patchwork.ozlabs.org/project/uboot/list/?series=281745&state=*. I think the proposed menu would have to iterate through all active boot methods. > --- > Changes in v2: > - move user input handling from cmd/bootmenu.c to common/menu.c > - keyboard buffer is drained before asking question in > common/menu.c::bootmenu_autoboot_loop() > - Add EFI_BOOT_MENU_DELAY Kconfig option to set delay for > autoboot and disable autoboot > - remove unnecessary "autoboot" member in struc efi_bootmgr_menu > > cmd/bootmenu.c | 145 ------------ > common/menu.c | 137 ++++++++++++ > include/menu.h | 20 ++ > lib/efi_loader/Kconfig | 20 ++ > lib/efi_loader/efi_bootmgr.c | 418 ++++++++++++++++++++++++++++++++++- > 5 files changed, 592 insertions(+), 148 deletions(-) > > diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c > index 409ef9a848..f9fdebc450 100644 > --- a/cmd/bootmenu.c > +++ b/cmd/bootmenu.c > @@ -33,21 +33,6 @@ struct bootmenu_entry { > struct bootmenu_entry *next; /* next menu entry (num+1) */ > }; > > -struct bootmenu_data { > - int delay; /* delay for autoboot */ > - int active; /* active menu entry */ > - int count; /* total count of menu entries */ > - struct bootmenu_entry *first; /* first menu entry */ > -}; > - > -enum bootmenu_key { > - KEY_NONE = 0, > - KEY_UP, > - KEY_DOWN, > - KEY_SELECT, > - KEY_QUIT, > -}; > - > static char *bootmenu_getoption(unsigned short int n) > { > char name[MAX_ENV_SIZE]; > @@ -81,136 +66,6 @@ static void bootmenu_print_entry(void *data) > puts(ANSI_COLOR_RESET); > } > > -static void bootmenu_autoboot_loop(struct bootmenu_data *menu, > - enum bootmenu_key *key, int *esc) > -{ > - int i, c; > - > - if (menu->delay > 0) { > - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > - printf(" Hit any key to stop autoboot: %2d ", menu->delay); > - } > - > - while (menu->delay > 0) { > - for (i = 0; i < 100; ++i) { > - if (!tstc()) { > - WATCHDOG_RESET(); > - mdelay(10); > - continue; > - } > - > - menu->delay = -1; > - c = getchar(); > - > - switch (c) { > - case '\e': > - *esc = 1; > - *key = KEY_NONE; > - break; > - case '\r': > - *key = KEY_SELECT; > - break; > - case 0x3: /* ^C */ > - *key = KEY_QUIT; > - break; > - default: > - *key = KEY_NONE; > - break; > - } > - > - break; > - } > - > - if (menu->delay < 0) > - break; > - > - --menu->delay; > - printf("\b\b\b%2d ", menu->delay); > - } > - > - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > - puts(ANSI_CLEAR_LINE); > - > - if (menu->delay == 0) > - *key = KEY_SELECT; > -} > - > -static void bootmenu_loop(struct bootmenu_data *menu, > - enum bootmenu_key *key, int *esc) > -{ > - int c; > - > - if (*esc == 1) { > - if (tstc()) { > - c = getchar(); > - } else { > - WATCHDOG_RESET(); > - mdelay(10); > - if (tstc()) > - c = getchar(); > - else > - c = '\e'; > - } > - } else { > - while (!tstc()) { > - WATCHDOG_RESET(); > - mdelay(10); > - } > - c = getchar(); > - } > - > - switch (*esc) { > - case 0: > - /* First char of ANSI escape sequence '\e' */ > - if (c == '\e') { > - *esc = 1; > - *key = KEY_NONE; > - } > - break; > - case 1: > - /* Second char of ANSI '[' */ > - if (c == '[') { > - *esc = 2; > - *key = KEY_NONE; > - } else { > - /* Alone ESC key was pressed */ > - *key = KEY_QUIT; > - *esc = (c == '\e') ? 1 : 0; > - } > - break; > - case 2: > - case 3: > - /* Third char of ANSI (number '1') - optional */ > - if (*esc == 2 && c == '1') { > - *esc = 3; > - *key = KEY_NONE; > - break; > - } > - > - *esc = 0; > - > - /* ANSI 'A' - key up was pressed */ > - if (c == 'A') > - *key = KEY_UP; > - /* ANSI 'B' - key down was pressed */ > - else if (c == 'B') > - *key = KEY_DOWN; > - /* other key was pressed */ > - else > - *key = KEY_NONE; > - > - break; > - } > - > - /* enter key was pressed */ > - if (c == '\r') > - *key = KEY_SELECT; > - > - /* ^C was pressed */ > - if (c == 0x3) > - *key = KEY_QUIT; > -} > - > static char *bootmenu_choice_entry(void *data) > { > struct bootmenu_data *menu = data; > diff --git a/common/menu.c b/common/menu.c > index 5fb2ffbd06..786e45f217 100644 > --- a/common/menu.c > +++ b/common/menu.c > @@ -4,11 +4,14 @@ > * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. > */ > common/menu.c is only compiled if CONFIG_MENU=y but CONFIG_EFI_BOOT_MENU does not select it. So depending on configuration this simply does not link: lib/efi_loader/efi_bootmgr.c:315: undefined reference to `menu_create' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:330: undefined reference to `menu_default_set' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:351: undefined reference to `menu_destroy' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:324: undefined reference to `menu_item_add' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:337: undefined reference to `menu_get_choice' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.o: in function `efi_bootmgr_menu_choice_entry': lib/efi_loader/efi_bootmgr.c:202: undefined reference to `bootmenu_autoboot_loop' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:205: undefined reference to `bootmenu_loop' aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.o: in function `efi_bootmgr_menu_display_statusline': lib/efi_loader/efi_bootmgr.c:168: undefined reference to `menu_default_choice > +#include <ansi.h> > #include <common.h> > #include <cli.h> > #include <malloc.h> > #include <errno.h> > +#include <linux/delay.h> > #include <linux/list.h> > +#include <watchdog.h> > > #include "menu.h" > > @@ -418,3 +421,137 @@ int menu_destroy(struct menu *m) > > return 1; > } > + > +void bootmenu_autoboot_loop(struct bootmenu_data *menu, > + enum bootmenu_key *key, int *esc) > +{ > + int i, c; > + > + if (menu->delay > 0) { > + /* flush input */ > + while (tstc()) > + getchar(); > + > + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > + printf(" Hit any key to stop autoboot: %2d ", menu->delay); > + } > + > + while (menu->delay > 0) { > + for (i = 0; i < 100; ++i) { > + if (!tstc()) { > + WATCHDOG_RESET(); > + mdelay(10); > + continue; > + } > + > + menu->delay = -1; > + c = getchar(); > + > + switch (c) { > + case '\e': > + *esc = 1; > + *key = KEY_NONE; > + break; > + case '\r': > + *key = KEY_SELECT; > + break; > + case 0x3: /* ^C */ > + *key = KEY_QUIT; > + break; > + default: > + *key = KEY_NONE; > + break; > + } > + > + break; > + } > + > + if (menu->delay < 0) > + break; > + > + --menu->delay; > + printf("\b\b\b%2d ", menu->delay); > + } > + > + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > + puts(ANSI_CLEAR_LINE); > + > + if (menu->delay == 0) > + *key = KEY_SELECT; > +} > + > +void bootmenu_loop(struct bootmenu_data *menu, > + enum bootmenu_key *key, int *esc) > +{ > + int c; > + > + if (*esc == 1) { > + if (tstc()) { > + c = getchar(); > + } else { > + WATCHDOG_RESET(); > + mdelay(10); > + if (tstc()) > + c = getchar(); > + else > + c = '\e'; > + } > + } else { > + while (!tstc()) { > + WATCHDOG_RESET(); > + mdelay(10); > + } > + c = getchar(); > + } > + > + switch (*esc) { > + case 0: > + /* First char of ANSI escape sequence '\e' */ > + if (c == '\e') { > + *esc = 1; > + *key = KEY_NONE; > + } > + break; > + case 1: > + /* Second char of ANSI '[' */ > + if (c == '[') { > + *esc = 2; > + *key = KEY_NONE; > + } else { > + /* Alone ESC key was pressed */ > + *key = KEY_QUIT; > + *esc = (c == '\e') ? 1 : 0; > + } > + break; > + case 2: > + case 3: > + /* Third char of ANSI (number '1') - optional */ > + if (*esc == 2 && c == '1') { > + *esc = 3; > + *key = KEY_NONE; > + break; > + } > + > + *esc = 0; > + > + /* ANSI 'A' - key up was pressed */ > + if (c == 'A') > + *key = KEY_UP; > + /* ANSI 'B' - key down was pressed */ > + else if (c == 'B') > + *key = KEY_DOWN; > + /* other key was pressed */ > + else > + *key = KEY_NONE; > + > + break; > + } > + > + /* enter key was pressed */ > + if (c == '\r') > + *key = KEY_SELECT; > + > + /* ^C was pressed */ > + if (c == 0x3) > + *key = KEY_QUIT; > +} > diff --git a/include/menu.h b/include/menu.h > index ad5859437e..e74616cae8 100644 > --- a/include/menu.h > +++ b/include/menu.h > @@ -35,4 +35,24 @@ int menu_default_choice(struct menu *m, void **choice); > */ > int menu_show(int bootdelay); > > +struct bootmenu_data { > + int delay; /* delay for autoboot */ > + int active; /* active menu entry */ > + int count; /* total count of menu entries */ > + struct bootmenu_entry *first; /* first menu entry */ > +}; > + > +enum bootmenu_key { > + KEY_NONE = 0, > + KEY_UP, > + KEY_DOWN, > + KEY_SELECT, > + KEY_QUIT, > +}; > + > +void bootmenu_autoboot_loop(struct bootmenu_data *menu, > + enum bootmenu_key *key, int *esc); > +void bootmenu_loop(struct bootmenu_data *menu, > + enum bootmenu_key *key, int *esc); > + > #endif /* __MENU_H__ */ > diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig > index e5e35fe51f..a3fb8b2a75 100644 > --- a/lib/efi_loader/Kconfig > +++ b/lib/efi_loader/Kconfig > @@ -39,6 +39,26 @@ config CMD_BOOTEFI_BOOTMGR > via UEFI variables Boot####, BootOrder, and BootNext. This enables the > 'bootefi bootmgr' command. > > +config EFI_BOOT_MENU > + bool "UEFI Boot Menu driven boot device selection" > + default n > + help > + Select this option if you want to enable the menu driven boot device > + selection. This menu provides the functionality to select a boot > + option to start, and allow users to edit Boot#### and BootOrder. > + If this menu is enabled, CLI can be disabled if the system boots > + via UEFI variable Boot#### and BootOrder. We end up with these symbols for menus: CONFIG_MENU CONFIG_AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENU_SHOW CONFIG_BOOTDELAY CONFIG_EFI_BOOT_MENU CONFIG_EFI_BOOT_MENU_DELAY Defining both CONFIG_AUTOBOOT_MENU_SHOW and CONFIG_EFI_BOOT_MENU does not make much sense. Same for CONFIG_BOOTDELAY and CONFIG_EFI_BOOT_MENU_DELAY. Please, unifiy the settings. Best regards Heinrich > + > +config EFI_BOOT_MENU_DELAY > + int "delay in seconds before automatically booting in UEFI Boot Menu" > + default 10 > + range -1 2147483647 > + help > + Delay before automatically booting in accordance with > + "BootOrder" variable. > + set to 0 to autoboot with no delay. > + set to -1 to disable autoboot. > + > config EFI_SETUP_EARLY > bool > > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c > index 8c04ecbdc8..bbb3fac5bd 100644 > --- a/lib/efi_loader/efi_bootmgr.c > +++ b/lib/efi_loader/efi_bootmgr.c > @@ -7,10 +7,12 @@ > > #define LOG_CATEGORY LOGC_EFI > > +#include <ansi.h> > #include <common.h> > #include <charset.h> > #include <log.h> > #include <malloc.h> > +#include <menu.h> > #include <efi_loader.h> > #include <efi_variable.h> > #include <asm/unaligned.h> > @@ -22,14 +24,400 @@ static const struct efi_runtime_services *rs; > * bootmgr implements the logic of trying to find a payload to boot > * based on the BootOrder + BootXXXX variables, and then loading it. > * > - * TODO detecting a special key held (f9?) and displaying a boot menu > - * like you would get on a PC would be clever. > - * > * TODO if we had a way to write and persist variables after the OS > * has started, we'd also want to check OsIndications to see if we > * should do normal or recovery boot. > */ > > +#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024 > + > +typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit); > + > +/** > + * struct efi_bootmgr_menu_entry - menu entry structure > + * > + * @menu_index: menu entry index > + * @title: title of entry > + * @key: unique key > + * @bootmgr_menu: pointer to the menu structure > + * @next: pointer to the next entry > + * @func: callback function to be called when this entry is selected > + * @data: data to be passed to the callback function > + */ > +struct efi_bootmgr_menu_entry { > + u32 menu_index; > + u16 *title; > + char key[6]; > + struct efi_bootmgr_menu *bootmgr_menu; > + struct efi_bootmgr_menu_entry *next; > + efi_bootmenu_entry_func func; > + void *data; > +}; > + > +/** > + * struct efi_bootmgr_menuy - bootmgr menu structure > + * > + * @delay: delay for autoboot > + * @active: active menu entry index > + * @count: total count of menu entry > + * @first: pointer to the first menu entry > + */ > +struct efi_bootmgr_menu { > + int delay; > + int active; > + int count; > + struct efi_bootmgr_menu_entry *first; > +}; > + > +struct efi_bootmgr_menu_item { > + u16 *title; > + efi_bootmenu_entry_func func; > + void *data; > +}; > + > +struct efi_bootmgr_boot_selection_data { > + u16 bootorder_index; > + void *load_option; > + int *selected; > +}; > + > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit); > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit); > + > +static struct efi_bootmgr_menu_item bootmgr_menu_items[] = { > + {u"Boot Manager", efi_bootmgr_process_boot_selection}, > + {u"Quit", NULL}, > +}; > + > +static void efi_bootmgr_menu_print_entry(void *data) > +{ > + struct efi_bootmgr_menu_entry *entry = data; > + int reverse = (entry->bootmgr_menu->active == entry->menu_index); > + > + /* TODO: support scroll or page for many entries */ > + > + /* > + * Move cursor to line where the entry will be drown (entry->count) > + * First 3 lines contain bootmgr menu header + one empty line > + * For the last "Quit" entry, add one empty line > + */ > + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) > + printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1); > + else > + printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1); > + > + puts(" "); > + > + if (reverse) > + puts(ANSI_COLOR_REVERSE); > + > + printf("%ls", entry->title); > + > + if (reverse) > + puts(ANSI_COLOR_RESET); > +} > + > +static void efi_bootmgr_menu_display_statusline(struct menu *m) > +{ > + struct efi_bootmgr_menu_entry *entry; > + struct efi_bootmgr_menu *bootmgr_menu; > + > + if (menu_default_choice(m, (void *)&entry) < 0) > + return; > + > + bootmgr_menu = entry->bootmgr_menu; > + > + printf(ANSI_CURSOR_POSITION, 1, 1); > + puts(ANSI_CLEAR_LINE); > + printf(ANSI_CURSOR_POSITION, 2, 1); > + puts(" *** U-Boot EFI Boot Manager ***"); > + puts(ANSI_CLEAR_LINE_TO_END); > + printf(ANSI_CURSOR_POSITION, 3, 1); > + puts(ANSI_CLEAR_LINE); > + > + /* First 3 lines are bootmgr_menu header + 2 empty lines between entries */ > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1); > + puts(ANSI_CLEAR_LINE); > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1); > + puts(" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"); > + puts(ANSI_CLEAR_LINE_TO_END); > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1); > + puts(ANSI_CLEAR_LINE); > +} > + > +static char *efi_bootmgr_menu_choice_entry(void *data) > +{ > + int i; > + int esc = 0; > + struct efi_bootmgr_menu_entry *iter; > + enum bootmenu_key key = KEY_NONE; > + struct efi_bootmgr_menu *bootmgr_menu = data; > + > + while (1) { > + if (bootmgr_menu->delay >= 0) { > + /* Autoboot was not stopped */ > + bootmenu_autoboot_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); > + } else { > + /* Some key was pressed, so autoboot was stopped */ > + bootmenu_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); > + } > + > + if (bootmgr_menu->delay == 0) > + key = KEY_QUIT; > + > + switch (key) { > + case KEY_UP: > + if (bootmgr_menu->active > 0) > + --bootmgr_menu->active; > + /* no menu key selected, regenerate menu */ > + return NULL; > + case KEY_DOWN: > + if (bootmgr_menu->active < bootmgr_menu->count - 1) > + ++bootmgr_menu->active; > + /* no menu key selected, regenerate menu */ > + return NULL; > + case KEY_SELECT: > + iter = bootmgr_menu->first; > + for (i = 0; i < bootmgr_menu->active; ++i) > + iter = iter->next; > + return iter->key; > + case KEY_QUIT: > + /* Quit by choosing the last entry */ > + iter = bootmgr_menu->first; > + while (iter->next) > + iter = iter->next; > + return iter->key; > + default: > + break; > + } > + } > + > + /* never happens */ > + debug("bootmgr menu: this should not happen"); > + return NULL; > +} > + > +static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu) > +{ > + struct efi_bootmgr_menu_entry *next; > + struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first; > + > + while (iter) { > + next = iter->next; > + free(iter); > + iter = next; > + } > + free(bootmgr_menu); > +} > + > +/** > + * efi_bootmgr_process_common() - main handler for uefi bootmgr menu > + * > + * Construct the structures required to show the menu, then handle > + * the user input intracting with u-boot menu functions. > + * > + * @items: pointer to the structure of each menu entry > + * @count: the number of menu entry > + * @delay: delay for autoboot/autoselect > + * Return: status code > + */ > +static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items, > + int count, int delay) > +{ > + u32 i; > + bool exit = false; > + efi_status_t ret; > + struct menu *menu; > + void *choice = NULL; > + struct efi_bootmgr_menu_entry *entry; > + struct efi_bootmgr_menu *bootmgr_menu; > + struct efi_bootmgr_menu_entry *iter = NULL; > + > + if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX) > + return EFI_OUT_OF_RESOURCES; > + > + bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu)); > + if (!bootmgr_menu) > + return EFI_OUT_OF_RESOURCES; > + > + bootmgr_menu->delay = delay; > + bootmgr_menu->active = 0; > + bootmgr_menu->first = NULL; > + > + for (i = 0; i < count; i++) { > + entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry)); > + if (!entry) { > + ret = EFI_LOAD_ERROR; > + goto out; > + } > + > + entry->menu_index = i; > + entry->title = items->title; > + snprintf(entry->key, sizeof(entry->key), "%04X", i); > + entry->bootmgr_menu = bootmgr_menu; > + entry->func = items->func; > + entry->data = items->data; > + entry->next = NULL; > + > + if (!iter) > + bootmgr_menu->first = entry; > + else > + iter->next = entry; > + > + iter = entry; > + items++; > + } > + bootmgr_menu->count = count; > + > + menu = menu_create(NULL, 0, 1, efi_bootmgr_menu_display_statusline, > + efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry, > + bootmgr_menu); > + if (!menu) { > + ret = EFI_INVALID_PARAMETER; > + goto out; > + } > + > + for (entry = bootmgr_menu->first; entry; entry = entry->next) { > + if (!menu_item_add(menu, entry->key, entry)) { > + ret = EFI_INVALID_PARAMETER; > + goto out; > + } > + } > + > + menu_default_set(menu, bootmgr_menu->first->key); > + > + while (!exit) { > + puts(ANSI_CURSOR_HIDE); > + puts(ANSI_CLEAR_CONSOLE); > + printf(ANSI_CURSOR_POSITION, 1, 1); > + > + if (menu_get_choice(menu, &choice)) { > + entry = choice; > + if (entry->func) > + ret = entry->func(entry->data, &exit); > + > + /* last entry "Quit" is selected, exit this menu */ > + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) { > + ret = EFI_ABORTED; > + break; > + } > + } > + } > + > +out: > + menu_destroy(menu); > + efi_bootmgr_menu_destroy(bootmgr_menu); > + > + puts(ANSI_CLEAR_CONSOLE); > + printf(ANSI_CURSOR_POSITION, 1, 1); > + puts(ANSI_CURSOR_SHOW); > + > + return ret; > +} > + > +static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count, > + int *selected) > +{ > + u32 i; > + efi_status_t ret; > + efi_uintn_t size; > + void *load_option; > + struct efi_load_option lo; > + u16 varname[] = u"Boot####"; > + struct efi_bootmgr_menu_item *menu_item, *iter; > + > + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); > + if (!menu_item) { > + ret = EFI_OUT_OF_RESOURCES; > + goto out; > + } > + > + iter = menu_item; > + for (i = 0; i < count; i++) { > + efi_create_indexed_name(varname, sizeof(varname), > + "Boot", bootorder[i]); > + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); > + if (!load_option) > + continue; > + > + ret = efi_deserialize_load_option(&lo, load_option, &size); > + if (ret != EFI_SUCCESS) { > + log_warning("Invalid load option for %ls\n", varname); > + free(load_option); > + continue; > + } > + > + if (lo.attributes & LOAD_OPTION_ACTIVE) { > + struct efi_bootmgr_boot_selection_data *info; > + > + info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data)); > + if (!info) { > + ret = EFI_OUT_OF_RESOURCES; > + goto out; > + } > + > + info->bootorder_index = i; > + info->load_option = load_option; > + info->selected = selected; > + iter->title = lo.label; > + iter->func = efi_bootmgr_process_boot_selected; > + iter->data = info; > + iter++; > + } > + } > + > + /* add "Quit" entry */ > + iter->title = u"Quit"; > + iter->func = NULL; > + iter->data = NULL; > + count += 1; > + > + ret = efi_bootmgr_process_common(menu_item, count, -1); > + > +out: > + iter = menu_item; > + for (i = 0; i < count - 1; i++, iter++) { > + free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option); > + free(iter->data); > + } > + > + free(menu_item); > + > + return ret; > +} > + > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit) > +{ > + struct efi_bootmgr_boot_selection_data *info = data; > + > + *exit = true; > + > + if (info) > + *info->selected = info->bootorder_index; > + > + return EFI_SUCCESS; > +} > + > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit) > +{ > + u16 *bootorder; > + efi_status_t ret; > + efi_uintn_t num, size; > + > + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > + if (!bootorder) > + return EFI_NOT_FOUND; > + > + num = size / sizeof(u16); > + ret = efi_bootmgr_show_boot_selection(bootorder, num, data); > + if (ret == EFI_SUCCESS) > + *exit = true; > + > + free(bootorder); > + > + return ret; > +} > + > /** > * try_load_entry() - try to load image for boot option > * > @@ -177,6 +565,30 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) > } > } > > + if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) { > + int selected; > + > + bootmgr_menu_items[0].data = &selected; > + ret = efi_bootmgr_process_common(bootmgr_menu_items, > + ARRAY_SIZE(bootmgr_menu_items), > + CONFIG_EFI_BOOT_MENU_DELAY); > + if (ret == EFI_SUCCESS) { > + /* bootorder may be updated in the bootmgr menu */ > + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > + if (!bootorder) { > + log_info("BootOrder not defined\n"); > + goto error; > + } > + ret = try_load_entry(bootorder[selected], handle, load_options); > + free(bootorder); > + if (ret == EFI_SUCCESS) > + return ret; > + > + log_err("Failed to start the selected entry(Boot%04X)\n", > + bootorder[selected]); > + } > + } > + > /* BootOrder */ > bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > if (!bootorder) {
Hi Heinrich, On Fri, 25 Feb 2022 at 16:26, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote: > > On 2/25/22 02:32, Masahisa Kojima wrote: > > This patch enables the menu-driven boot device selection. > > User can select the Boot#### included in BootOrder variable. > > > > If user quits thie menu, or the selected Boot#### fails to boot, > > efi bootmgr continues to boot in accordance with BootOrder variable. > > > > This commit also moves the user input handling from cmd/bootmenu.c > > to common/menu.c to reuse it from efi boot menu. > > > > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org> > > With the series applied and CONFIG_EFI_BOOT_MENU=y I first see a line > "Hit any key to stop autoboot:" with a count down and then the UEFI menu > with a countdown. This duplication of countdowns does not make sense. Thank you for your review. I agree, this duplication does not make sense. Anyway, my primary goal is to disable CLI, user can select the UEFI boot option("Boot####" variable) to boot the system, and user can edit the UEFI boot option("Boot####" and "BootOrder") through the menu. I now realize that my patch series probably do not work without CLI, and hooking the "bootefi bootmgr" command to show the new efi bootmenu is probably wrong. I will investigate further. > > This is the content of the new menu: > > *** U-Boot EFI Boot Manager *** > > Boot Manager > Boot Manager maintenance > > Quit > > When selecting " Boot Manager maintenance" I see a second menu: > > *** U-Boot EFI Boot Manager *** > > Add Boot Option > Delete Boot Option > Change Boot Order > > Quit > > Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit > > When I select "Add Boot Option" the system (qemu_arm64_defconfig) simply > hangs. Sorry, there is something wrong on my code. I will try to reproduce. > > Why do we need the first menu? Compare it to your laptop experience when > pressing the boot menu key (often <F12> or <F8>). > > Can't we have a simple countdown: Yes, I could skip the first menu. > > "Hit any key for boot menu" > > If a key is hit show a menu showing the different boot options and the > distro-boot devices: > > Debian (0001) > Ubuntu (0002) > SATA(1) > SATA(2) > NVMe(1) > Boot manager settings > Command line The current scope of this patch series are disabling CLI and booting the system with UEFI boot option. I will check what I can do for distro-boot. > > Simon has proposed a patch series "Initial implementation of standard > boot", > https://patchwork.ozlabs.org/project/uboot/list/?series=281745&state=*. > I think the proposed menu would have to iterate through all active boot > methods. > > > --- > > Changes in v2: > > - move user input handling from cmd/bootmenu.c to common/menu.c > > - keyboard buffer is drained before asking question in > > common/menu.c::bootmenu_autoboot_loop() > > - Add EFI_BOOT_MENU_DELAY Kconfig option to set delay for > > autoboot and disable autoboot > > - remove unnecessary "autoboot" member in struc efi_bootmgr_menu > > > > cmd/bootmenu.c | 145 ------------ > > common/menu.c | 137 ++++++++++++ > > include/menu.h | 20 ++ > > lib/efi_loader/Kconfig | 20 ++ > > lib/efi_loader/efi_bootmgr.c | 418 ++++++++++++++++++++++++++++++++++- > > 5 files changed, 592 insertions(+), 148 deletions(-) > > > > diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c > > index 409ef9a848..f9fdebc450 100644 > > --- a/cmd/bootmenu.c > > +++ b/cmd/bootmenu.c > > @@ -33,21 +33,6 @@ struct bootmenu_entry { > > struct bootmenu_entry *next; /* next menu entry (num+1) */ > > }; > > > > -struct bootmenu_data { > > - int delay; /* delay for autoboot */ > > - int active; /* active menu entry */ > > - int count; /* total count of menu entries */ > > - struct bootmenu_entry *first; /* first menu entry */ > > -}; > > - > > -enum bootmenu_key { > > - KEY_NONE = 0, > > - KEY_UP, > > - KEY_DOWN, > > - KEY_SELECT, > > - KEY_QUIT, > > -}; > > - > > static char *bootmenu_getoption(unsigned short int n) > > { > > char name[MAX_ENV_SIZE]; > > @@ -81,136 +66,6 @@ static void bootmenu_print_entry(void *data) > > puts(ANSI_COLOR_RESET); > > } > > > > -static void bootmenu_autoboot_loop(struct bootmenu_data *menu, > > - enum bootmenu_key *key, int *esc) > > -{ > > - int i, c; > > - > > - if (menu->delay > 0) { > > - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > > - printf(" Hit any key to stop autoboot: %2d ", menu->delay); > > - } > > - > > - while (menu->delay > 0) { > > - for (i = 0; i < 100; ++i) { > > - if (!tstc()) { > > - WATCHDOG_RESET(); > > - mdelay(10); > > - continue; > > - } > > - > > - menu->delay = -1; > > - c = getchar(); > > - > > - switch (c) { > > - case '\e': > > - *esc = 1; > > - *key = KEY_NONE; > > - break; > > - case '\r': > > - *key = KEY_SELECT; > > - break; > > - case 0x3: /* ^C */ > > - *key = KEY_QUIT; > > - break; > > - default: > > - *key = KEY_NONE; > > - break; > > - } > > - > > - break; > > - } > > - > > - if (menu->delay < 0) > > - break; > > - > > - --menu->delay; > > - printf("\b\b\b%2d ", menu->delay); > > - } > > - > > - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > > - puts(ANSI_CLEAR_LINE); > > - > > - if (menu->delay == 0) > > - *key = KEY_SELECT; > > -} > > - > > -static void bootmenu_loop(struct bootmenu_data *menu, > > - enum bootmenu_key *key, int *esc) > > -{ > > - int c; > > - > > - if (*esc == 1) { > > - if (tstc()) { > > - c = getchar(); > > - } else { > > - WATCHDOG_RESET(); > > - mdelay(10); > > - if (tstc()) > > - c = getchar(); > > - else > > - c = '\e'; > > - } > > - } else { > > - while (!tstc()) { > > - WATCHDOG_RESET(); > > - mdelay(10); > > - } > > - c = getchar(); > > - } > > - > > - switch (*esc) { > > - case 0: > > - /* First char of ANSI escape sequence '\e' */ > > - if (c == '\e') { > > - *esc = 1; > > - *key = KEY_NONE; > > - } > > - break; > > - case 1: > > - /* Second char of ANSI '[' */ > > - if (c == '[') { > > - *esc = 2; > > - *key = KEY_NONE; > > - } else { > > - /* Alone ESC key was pressed */ > > - *key = KEY_QUIT; > > - *esc = (c == '\e') ? 1 : 0; > > - } > > - break; > > - case 2: > > - case 3: > > - /* Third char of ANSI (number '1') - optional */ > > - if (*esc == 2 && c == '1') { > > - *esc = 3; > > - *key = KEY_NONE; > > - break; > > - } > > - > > - *esc = 0; > > - > > - /* ANSI 'A' - key up was pressed */ > > - if (c == 'A') > > - *key = KEY_UP; > > - /* ANSI 'B' - key down was pressed */ > > - else if (c == 'B') > > - *key = KEY_DOWN; > > - /* other key was pressed */ > > - else > > - *key = KEY_NONE; > > - > > - break; > > - } > > - > > - /* enter key was pressed */ > > - if (c == '\r') > > - *key = KEY_SELECT; > > - > > - /* ^C was pressed */ > > - if (c == 0x3) > > - *key = KEY_QUIT; > > -} > > - > > static char *bootmenu_choice_entry(void *data) > > { > > struct bootmenu_data *menu = data; > > diff --git a/common/menu.c b/common/menu.c > > index 5fb2ffbd06..786e45f217 100644 > > --- a/common/menu.c > > +++ b/common/menu.c > > @@ -4,11 +4,14 @@ > > * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. > > */ > > > > common/menu.c is only compiled if CONFIG_MENU=y but CONFIG_EFI_BOOT_MENU > does not select it. So depending on configuration this simply does not link: I will fix this. > > lib/efi_loader/efi_bootmgr.c:315: undefined reference to `menu_create' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:330: undefined > reference to `menu_default_set' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:351: undefined > reference to `menu_destroy' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:324: undefined > reference to `menu_item_add' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:337: undefined > reference to `menu_get_choice' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.o: in function > `efi_bootmgr_menu_choice_entry': > lib/efi_loader/efi_bootmgr.c:202: undefined reference to > `bootmenu_autoboot_loop' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.c:205: undefined > reference to `bootmenu_loop' > aarch64-linux-gnu-ld.bfd: lib/efi_loader/efi_bootmgr.o: in function > `efi_bootmgr_menu_display_statusline': > lib/efi_loader/efi_bootmgr.c:168: undefined reference to > `menu_default_choice > > > +#include <ansi.h> > > #include <common.h> > > #include <cli.h> > > #include <malloc.h> > > #include <errno.h> > > +#include <linux/delay.h> > > #include <linux/list.h> > > +#include <watchdog.h> > > > > #include "menu.h" > > > > @@ -418,3 +421,137 @@ int menu_destroy(struct menu *m) > > > > return 1; > > } > > + > > +void bootmenu_autoboot_loop(struct bootmenu_data *menu, > > + enum bootmenu_key *key, int *esc) > > +{ > > + int i, c; > > + > > + if (menu->delay > 0) { > > + /* flush input */ > > + while (tstc()) > > + getchar(); > > + > > + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > > + printf(" Hit any key to stop autoboot: %2d ", menu->delay); > > + } > > + > > + while (menu->delay > 0) { > > + for (i = 0; i < 100; ++i) { > > + if (!tstc()) { > > + WATCHDOG_RESET(); > > + mdelay(10); > > + continue; > > + } > > + > > + menu->delay = -1; > > + c = getchar(); > > + > > + switch (c) { > > + case '\e': > > + *esc = 1; > > + *key = KEY_NONE; > > + break; > > + case '\r': > > + *key = KEY_SELECT; > > + break; > > + case 0x3: /* ^C */ > > + *key = KEY_QUIT; > > + break; > > + default: > > + *key = KEY_NONE; > > + break; > > + } > > + > > + break; > > + } > > + > > + if (menu->delay < 0) > > + break; > > + > > + --menu->delay; > > + printf("\b\b\b%2d ", menu->delay); > > + } > > + > > + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); > > + puts(ANSI_CLEAR_LINE); > > + > > + if (menu->delay == 0) > > + *key = KEY_SELECT; > > +} > > + > > +void bootmenu_loop(struct bootmenu_data *menu, > > + enum bootmenu_key *key, int *esc) > > +{ > > + int c; > > + > > + if (*esc == 1) { > > + if (tstc()) { > > + c = getchar(); > > + } else { > > + WATCHDOG_RESET(); > > + mdelay(10); > > + if (tstc()) > > + c = getchar(); > > + else > > + c = '\e'; > > + } > > + } else { > > + while (!tstc()) { > > + WATCHDOG_RESET(); > > + mdelay(10); > > + } > > + c = getchar(); > > + } > > + > > + switch (*esc) { > > + case 0: > > + /* First char of ANSI escape sequence '\e' */ > > + if (c == '\e') { > > + *esc = 1; > > + *key = KEY_NONE; > > + } > > + break; > > + case 1: > > + /* Second char of ANSI '[' */ > > + if (c == '[') { > > + *esc = 2; > > + *key = KEY_NONE; > > + } else { > > + /* Alone ESC key was pressed */ > > + *key = KEY_QUIT; > > + *esc = (c == '\e') ? 1 : 0; > > + } > > + break; > > + case 2: > > + case 3: > > + /* Third char of ANSI (number '1') - optional */ > > + if (*esc == 2 && c == '1') { > > + *esc = 3; > > + *key = KEY_NONE; > > + break; > > + } > > + > > + *esc = 0; > > + > > + /* ANSI 'A' - key up was pressed */ > > + if (c == 'A') > > + *key = KEY_UP; > > + /* ANSI 'B' - key down was pressed */ > > + else if (c == 'B') > > + *key = KEY_DOWN; > > + /* other key was pressed */ > > + else > > + *key = KEY_NONE; > > + > > + break; > > + } > > + > > + /* enter key was pressed */ > > + if (c == '\r') > > + *key = KEY_SELECT; > > + > > + /* ^C was pressed */ > > + if (c == 0x3) > > + *key = KEY_QUIT; > > +} > > diff --git a/include/menu.h b/include/menu.h > > index ad5859437e..e74616cae8 100644 > > --- a/include/menu.h > > +++ b/include/menu.h > > @@ -35,4 +35,24 @@ int menu_default_choice(struct menu *m, void **choice); > > */ > > int menu_show(int bootdelay); > > > > +struct bootmenu_data { > > + int delay; /* delay for autoboot */ > > + int active; /* active menu entry */ > > + int count; /* total count of menu entries */ > > + struct bootmenu_entry *first; /* first menu entry */ > > +}; > > + > > +enum bootmenu_key { > > + KEY_NONE = 0, > > + KEY_UP, > > + KEY_DOWN, > > + KEY_SELECT, > > + KEY_QUIT, > > +}; > > + > > +void bootmenu_autoboot_loop(struct bootmenu_data *menu, > > + enum bootmenu_key *key, int *esc); > > +void bootmenu_loop(struct bootmenu_data *menu, > > + enum bootmenu_key *key, int *esc); > > + > > #endif /* __MENU_H__ */ > > diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig > > index e5e35fe51f..a3fb8b2a75 100644 > > --- a/lib/efi_loader/Kconfig > > +++ b/lib/efi_loader/Kconfig > > @@ -39,6 +39,26 @@ config CMD_BOOTEFI_BOOTMGR > > via UEFI variables Boot####, BootOrder, and BootNext. This enables the > > 'bootefi bootmgr' command. > > > > +config EFI_BOOT_MENU > > + bool "UEFI Boot Menu driven boot device selection" > > + default n > > + help > > + Select this option if you want to enable the menu driven boot device > > + selection. This menu provides the functionality to select a boot > > + option to start, and allow users to edit Boot#### and BootOrder. > > + If this menu is enabled, CLI can be disabled if the system boots > > + via UEFI variable Boot#### and BootOrder. > > We end up with these symbols for menus: > > CONFIG_MENU > CONFIG_AUTOBOOT_MENUKEY > CONFIG_AUTOBOOT_MENU_SHOW > CONFIG_BOOTDELAY > CONFIG_EFI_BOOT_MENU > CONFIG_EFI_BOOT_MENU_DELAY > > Defining both CONFIG_AUTOBOOT_MENU_SHOW and CONFIG_EFI_BOOT_MENU does > not make much sense. Same for CONFIG_BOOTDELAY and > CONFIG_EFI_BOOT_MENU_DELAY. Please, unifiy the settings. I will consider how I can unify these options. Thanks, Masahisa Kojima > > Best regards > > Heinrich > > > + > > +config EFI_BOOT_MENU_DELAY > > + int "delay in seconds before automatically booting in UEFI Boot Menu" > > + default 10 > > + range -1 2147483647 > > + help > > + Delay before automatically booting in accordance with > > + "BootOrder" variable. > > + set to 0 to autoboot with no delay. > > + set to -1 to disable autoboot. > > + > > config EFI_SETUP_EARLY > > bool > > > > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c > > index 8c04ecbdc8..bbb3fac5bd 100644 > > --- a/lib/efi_loader/efi_bootmgr.c > > +++ b/lib/efi_loader/efi_bootmgr.c > > @@ -7,10 +7,12 @@ > > > > #define LOG_CATEGORY LOGC_EFI > > > > +#include <ansi.h> > > #include <common.h> > > #include <charset.h> > > #include <log.h> > > #include <malloc.h> > > +#include <menu.h> > > #include <efi_loader.h> > > #include <efi_variable.h> > > #include <asm/unaligned.h> > > @@ -22,14 +24,400 @@ static const struct efi_runtime_services *rs; > > * bootmgr implements the logic of trying to find a payload to boot > > * based on the BootOrder + BootXXXX variables, and then loading it. > > * > > - * TODO detecting a special key held (f9?) and displaying a boot menu > > - * like you would get on a PC would be clever. > > - * > > * TODO if we had a way to write and persist variables after the OS > > * has started, we'd also want to check OsIndications to see if we > > * should do normal or recovery boot. > > */ > > > > +#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024 > > + > > +typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit); > > + > > +/** > > + * struct efi_bootmgr_menu_entry - menu entry structure > > + * > > + * @menu_index: menu entry index > > + * @title: title of entry > > + * @key: unique key > > + * @bootmgr_menu: pointer to the menu structure > > + * @next: pointer to the next entry > > + * @func: callback function to be called when this entry is selected > > + * @data: data to be passed to the callback function > > + */ > > +struct efi_bootmgr_menu_entry { > > + u32 menu_index; > > + u16 *title; > > + char key[6]; > > + struct efi_bootmgr_menu *bootmgr_menu; > > + struct efi_bootmgr_menu_entry *next; > > + efi_bootmenu_entry_func func; > > + void *data; > > +}; > > + > > +/** > > + * struct efi_bootmgr_menuy - bootmgr menu structure > > + * > > + * @delay: delay for autoboot > > + * @active: active menu entry index > > + * @count: total count of menu entry > > + * @first: pointer to the first menu entry > > + */ > > +struct efi_bootmgr_menu { > > + int delay; > > + int active; > > + int count; > > + struct efi_bootmgr_menu_entry *first; > > +}; > > + > > +struct efi_bootmgr_menu_item { > > + u16 *title; > > + efi_bootmenu_entry_func func; > > + void *data; > > +}; > > + > > +struct efi_bootmgr_boot_selection_data { > > + u16 bootorder_index; > > + void *load_option; > > + int *selected; > > +}; > > + > > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit); > > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit); > > + > > +static struct efi_bootmgr_menu_item bootmgr_menu_items[] = { > > + {u"Boot Manager", efi_bootmgr_process_boot_selection}, > > + {u"Quit", NULL}, > > +}; > > + > > +static void efi_bootmgr_menu_print_entry(void *data) > > +{ > > + struct efi_bootmgr_menu_entry *entry = data; > > + int reverse = (entry->bootmgr_menu->active == entry->menu_index); > > + > > + /* TODO: support scroll or page for many entries */ > > + > > + /* > > + * Move cursor to line where the entry will be drown (entry->count) > > + * First 3 lines contain bootmgr menu header + one empty line > > + * For the last "Quit" entry, add one empty line > > + */ > > + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) > > + printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1); > > + else > > + printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1); > > + > > + puts(" "); > > + > > + if (reverse) > > + puts(ANSI_COLOR_REVERSE); > > + > > + printf("%ls", entry->title); > > + > > + if (reverse) > > + puts(ANSI_COLOR_RESET); > > +} > > + > > +static void efi_bootmgr_menu_display_statusline(struct menu *m) > > +{ > > + struct efi_bootmgr_menu_entry *entry; > > + struct efi_bootmgr_menu *bootmgr_menu; > > + > > + if (menu_default_choice(m, (void *)&entry) < 0) > > + return; > > + > > + bootmgr_menu = entry->bootmgr_menu; > > + > > + printf(ANSI_CURSOR_POSITION, 1, 1); > > + puts(ANSI_CLEAR_LINE); > > + printf(ANSI_CURSOR_POSITION, 2, 1); > > + puts(" *** U-Boot EFI Boot Manager ***"); > > + puts(ANSI_CLEAR_LINE_TO_END); > > + printf(ANSI_CURSOR_POSITION, 3, 1); > > + puts(ANSI_CLEAR_LINE); > > + > > + /* First 3 lines are bootmgr_menu header + 2 empty lines between entries */ > > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1); > > + puts(ANSI_CLEAR_LINE); > > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1); > > + puts(" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"); > > + puts(ANSI_CLEAR_LINE_TO_END); > > + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1); > > + puts(ANSI_CLEAR_LINE); > > +} > > + > > +static char *efi_bootmgr_menu_choice_entry(void *data) > > +{ > > + int i; > > + int esc = 0; > > + struct efi_bootmgr_menu_entry *iter; > > + enum bootmenu_key key = KEY_NONE; > > + struct efi_bootmgr_menu *bootmgr_menu = data; > > + > > + while (1) { > > + if (bootmgr_menu->delay >= 0) { > > + /* Autoboot was not stopped */ > > + bootmenu_autoboot_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); > > + } else { > > + /* Some key was pressed, so autoboot was stopped */ > > + bootmenu_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); > > + } > > + > > + if (bootmgr_menu->delay == 0) > > + key = KEY_QUIT; > > + > > + switch (key) { > > + case KEY_UP: > > + if (bootmgr_menu->active > 0) > > + --bootmgr_menu->active; > > + /* no menu key selected, regenerate menu */ > > + return NULL; > > + case KEY_DOWN: > > + if (bootmgr_menu->active < bootmgr_menu->count - 1) > > + ++bootmgr_menu->active; > > + /* no menu key selected, regenerate menu */ > > + return NULL; > > + case KEY_SELECT: > > + iter = bootmgr_menu->first; > > + for (i = 0; i < bootmgr_menu->active; ++i) > > + iter = iter->next; > > + return iter->key; > > + case KEY_QUIT: > > + /* Quit by choosing the last entry */ > > + iter = bootmgr_menu->first; > > + while (iter->next) > > + iter = iter->next; > > + return iter->key; > > + default: > > + break; > > + } > > + } > > + > > + /* never happens */ > > + debug("bootmgr menu: this should not happen"); > > + return NULL; > > +} > > + > > +static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu) > > +{ > > + struct efi_bootmgr_menu_entry *next; > > + struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first; > > + > > + while (iter) { > > + next = iter->next; > > + free(iter); > > + iter = next; > > + } > > + free(bootmgr_menu); > > +} > > + > > +/** > > + * efi_bootmgr_process_common() - main handler for uefi bootmgr menu > > + * > > + * Construct the structures required to show the menu, then handle > > + * the user input intracting with u-boot menu functions. > > + * > > + * @items: pointer to the structure of each menu entry > > + * @count: the number of menu entry > > + * @delay: delay for autoboot/autoselect > > + * Return: status code > > + */ > > +static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items, > > + int count, int delay) > > +{ > > + u32 i; > > + bool exit = false; > > + efi_status_t ret; > > + struct menu *menu; > > + void *choice = NULL; > > + struct efi_bootmgr_menu_entry *entry; > > + struct efi_bootmgr_menu *bootmgr_menu; > > + struct efi_bootmgr_menu_entry *iter = NULL; > > + > > + if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX) > > + return EFI_OUT_OF_RESOURCES; > > + > > + bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu)); > > + if (!bootmgr_menu) > > + return EFI_OUT_OF_RESOURCES; > > + > > + bootmgr_menu->delay = delay; > > + bootmgr_menu->active = 0; > > + bootmgr_menu->first = NULL; > > + > > + for (i = 0; i < count; i++) { > > + entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry)); > > + if (!entry) { > > + ret = EFI_LOAD_ERROR; > > + goto out; > > + } > > + > > + entry->menu_index = i; > > + entry->title = items->title; > > + snprintf(entry->key, sizeof(entry->key), "%04X", i); > > + entry->bootmgr_menu = bootmgr_menu; > > + entry->func = items->func; > > + entry->data = items->data; > > + entry->next = NULL; > > + > > + if (!iter) > > + bootmgr_menu->first = entry; > > + else > > + iter->next = entry; > > + > > + iter = entry; > > + items++; > > + } > > + bootmgr_menu->count = count; > > + > > + menu = menu_create(NULL, 0, 1, efi_bootmgr_menu_display_statusline, > > + efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry, > > + bootmgr_menu); > > + if (!menu) { > > + ret = EFI_INVALID_PARAMETER; > > + goto out; > > + } > > + > > + for (entry = bootmgr_menu->first; entry; entry = entry->next) { > > + if (!menu_item_add(menu, entry->key, entry)) { > > + ret = EFI_INVALID_PARAMETER; > > + goto out; > > + } > > + } > > + > > + menu_default_set(menu, bootmgr_menu->first->key); > > + > > + while (!exit) { > > + puts(ANSI_CURSOR_HIDE); > > + puts(ANSI_CLEAR_CONSOLE); > > + printf(ANSI_CURSOR_POSITION, 1, 1); > > + > > + if (menu_get_choice(menu, &choice)) { > > + entry = choice; > > + if (entry->func) > > + ret = entry->func(entry->data, &exit); > > + > > + /* last entry "Quit" is selected, exit this menu */ > > + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) { > > + ret = EFI_ABORTED; > > + break; > > + } > > + } > > + } > > + > > +out: > > + menu_destroy(menu); > > + efi_bootmgr_menu_destroy(bootmgr_menu); > > + > > + puts(ANSI_CLEAR_CONSOLE); > > + printf(ANSI_CURSOR_POSITION, 1, 1); > > + puts(ANSI_CURSOR_SHOW); > > + > > + return ret; > > +} > > + > > +static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count, > > + int *selected) > > +{ > > + u32 i; > > + efi_status_t ret; > > + efi_uintn_t size; > > + void *load_option; > > + struct efi_load_option lo; > > + u16 varname[] = u"Boot####"; > > + struct efi_bootmgr_menu_item *menu_item, *iter; > > + > > + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); > > + if (!menu_item) { > > + ret = EFI_OUT_OF_RESOURCES; > > + goto out; > > + } > > + > > + iter = menu_item; > > + for (i = 0; i < count; i++) { > > + efi_create_indexed_name(varname, sizeof(varname), > > + "Boot", bootorder[i]); > > + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); > > + if (!load_option) > > + continue; > > + > > + ret = efi_deserialize_load_option(&lo, load_option, &size); > > + if (ret != EFI_SUCCESS) { > > + log_warning("Invalid load option for %ls\n", varname); > > + free(load_option); > > + continue; > > + } > > + > > + if (lo.attributes & LOAD_OPTION_ACTIVE) { > > + struct efi_bootmgr_boot_selection_data *info; > > + > > + info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data)); > > + if (!info) { > > + ret = EFI_OUT_OF_RESOURCES; > > + goto out; > > + } > > + > > + info->bootorder_index = i; > > + info->load_option = load_option; > > + info->selected = selected; > > + iter->title = lo.label; > > + iter->func = efi_bootmgr_process_boot_selected; > > + iter->data = info; > > + iter++; > > + } > > + } > > + > > + /* add "Quit" entry */ > > + iter->title = u"Quit"; > > + iter->func = NULL; > > + iter->data = NULL; > > + count += 1; > > + > > + ret = efi_bootmgr_process_common(menu_item, count, -1); > > + > > +out: > > + iter = menu_item; > > + for (i = 0; i < count - 1; i++, iter++) { > > + free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option); > > + free(iter->data); > > + } > > + > > + free(menu_item); > > + > > + return ret; > > +} > > + > > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit) > > +{ > > + struct efi_bootmgr_boot_selection_data *info = data; > > + > > + *exit = true; > > + > > + if (info) > > + *info->selected = info->bootorder_index; > > + > > + return EFI_SUCCESS; > > +} > > + > > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit) > > +{ > > + u16 *bootorder; > > + efi_status_t ret; > > + efi_uintn_t num, size; > > + > > + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > > + if (!bootorder) > > + return EFI_NOT_FOUND; > > + > > + num = size / sizeof(u16); > > + ret = efi_bootmgr_show_boot_selection(bootorder, num, data); > > + if (ret == EFI_SUCCESS) > > + *exit = true; > > + > > + free(bootorder); > > + > > + return ret; > > +} > > + > > /** > > * try_load_entry() - try to load image for boot option > > * > > @@ -177,6 +565,30 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) > > } > > } > > > > + if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) { > > + int selected; > > + > > + bootmgr_menu_items[0].data = &selected; > > + ret = efi_bootmgr_process_common(bootmgr_menu_items, > > + ARRAY_SIZE(bootmgr_menu_items), > > + CONFIG_EFI_BOOT_MENU_DELAY); > > + if (ret == EFI_SUCCESS) { > > + /* bootorder may be updated in the bootmgr menu */ > > + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > > + if (!bootorder) { > > + log_info("BootOrder not defined\n"); > > + goto error; > > + } > > + ret = try_load_entry(bootorder[selected], handle, load_options); > > + free(bootorder); > > + if (ret == EFI_SUCCESS) > > + return ret; > > + > > + log_err("Failed to start the selected entry(Boot%04X)\n", > > + bootorder[selected]); > > + } > > + } > > + > > /* BootOrder */ > > bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); > > if (!bootorder) { >
diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index 409ef9a848..f9fdebc450 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -33,21 +33,6 @@ struct bootmenu_entry { struct bootmenu_entry *next; /* next menu entry (num+1) */ }; -struct bootmenu_data { - int delay; /* delay for autoboot */ - int active; /* active menu entry */ - int count; /* total count of menu entries */ - struct bootmenu_entry *first; /* first menu entry */ -}; - -enum bootmenu_key { - KEY_NONE = 0, - KEY_UP, - KEY_DOWN, - KEY_SELECT, - KEY_QUIT, -}; - static char *bootmenu_getoption(unsigned short int n) { char name[MAX_ENV_SIZE]; @@ -81,136 +66,6 @@ static void bootmenu_print_entry(void *data) puts(ANSI_COLOR_RESET); } -static void bootmenu_autoboot_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) -{ - int i, c; - - if (menu->delay > 0) { - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); - printf(" Hit any key to stop autoboot: %2d ", menu->delay); - } - - while (menu->delay > 0) { - for (i = 0; i < 100; ++i) { - if (!tstc()) { - WATCHDOG_RESET(); - mdelay(10); - continue; - } - - menu->delay = -1; - c = getchar(); - - switch (c) { - case '\e': - *esc = 1; - *key = KEY_NONE; - break; - case '\r': - *key = KEY_SELECT; - break; - case 0x3: /* ^C */ - *key = KEY_QUIT; - break; - default: - *key = KEY_NONE; - break; - } - - break; - } - - if (menu->delay < 0) - break; - - --menu->delay; - printf("\b\b\b%2d ", menu->delay); - } - - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); - puts(ANSI_CLEAR_LINE); - - if (menu->delay == 0) - *key = KEY_SELECT; -} - -static void bootmenu_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) -{ - int c; - - if (*esc == 1) { - if (tstc()) { - c = getchar(); - } else { - WATCHDOG_RESET(); - mdelay(10); - if (tstc()) - c = getchar(); - else - c = '\e'; - } - } else { - while (!tstc()) { - WATCHDOG_RESET(); - mdelay(10); - } - c = getchar(); - } - - switch (*esc) { - case 0: - /* First char of ANSI escape sequence '\e' */ - if (c == '\e') { - *esc = 1; - *key = KEY_NONE; - } - break; - case 1: - /* Second char of ANSI '[' */ - if (c == '[') { - *esc = 2; - *key = KEY_NONE; - } else { - /* Alone ESC key was pressed */ - *key = KEY_QUIT; - *esc = (c == '\e') ? 1 : 0; - } - break; - case 2: - case 3: - /* Third char of ANSI (number '1') - optional */ - if (*esc == 2 && c == '1') { - *esc = 3; - *key = KEY_NONE; - break; - } - - *esc = 0; - - /* ANSI 'A' - key up was pressed */ - if (c == 'A') - *key = KEY_UP; - /* ANSI 'B' - key down was pressed */ - else if (c == 'B') - *key = KEY_DOWN; - /* other key was pressed */ - else - *key = KEY_NONE; - - break; - } - - /* enter key was pressed */ - if (c == '\r') - *key = KEY_SELECT; - - /* ^C was pressed */ - if (c == 0x3) - *key = KEY_QUIT; -} - static char *bootmenu_choice_entry(void *data) { struct bootmenu_data *menu = data; diff --git a/common/menu.c b/common/menu.c index 5fb2ffbd06..786e45f217 100644 --- a/common/menu.c +++ b/common/menu.c @@ -4,11 +4,14 @@ * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. */ +#include <ansi.h> #include <common.h> #include <cli.h> #include <malloc.h> #include <errno.h> +#include <linux/delay.h> #include <linux/list.h> +#include <watchdog.h> #include "menu.h" @@ -418,3 +421,137 @@ int menu_destroy(struct menu *m) return 1; } + +void bootmenu_autoboot_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc) +{ + int i, c; + + if (menu->delay > 0) { + /* flush input */ + while (tstc()) + getchar(); + + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); + printf(" Hit any key to stop autoboot: %2d ", menu->delay); + } + + while (menu->delay > 0) { + for (i = 0; i < 100; ++i) { + if (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + continue; + } + + menu->delay = -1; + c = getchar(); + + switch (c) { + case '\e': + *esc = 1; + *key = KEY_NONE; + break; + case '\r': + *key = KEY_SELECT; + break; + case 0x3: /* ^C */ + *key = KEY_QUIT; + break; + default: + *key = KEY_NONE; + break; + } + + break; + } + + if (menu->delay < 0) + break; + + --menu->delay; + printf("\b\b\b%2d ", menu->delay); + } + + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); + puts(ANSI_CLEAR_LINE); + + if (menu->delay == 0) + *key = KEY_SELECT; +} + +void bootmenu_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc) +{ + int c; + + if (*esc == 1) { + if (tstc()) { + c = getchar(); + } else { + WATCHDOG_RESET(); + mdelay(10); + if (tstc()) + c = getchar(); + else + c = '\e'; + } + } else { + while (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + } + c = getchar(); + } + + switch (*esc) { + case 0: + /* First char of ANSI escape sequence '\e' */ + if (c == '\e') { + *esc = 1; + *key = KEY_NONE; + } + break; + case 1: + /* Second char of ANSI '[' */ + if (c == '[') { + *esc = 2; + *key = KEY_NONE; + } else { + /* Alone ESC key was pressed */ + *key = KEY_QUIT; + *esc = (c == '\e') ? 1 : 0; + } + break; + case 2: + case 3: + /* Third char of ANSI (number '1') - optional */ + if (*esc == 2 && c == '1') { + *esc = 3; + *key = KEY_NONE; + break; + } + + *esc = 0; + + /* ANSI 'A' - key up was pressed */ + if (c == 'A') + *key = KEY_UP; + /* ANSI 'B' - key down was pressed */ + else if (c == 'B') + *key = KEY_DOWN; + /* other key was pressed */ + else + *key = KEY_NONE; + + break; + } + + /* enter key was pressed */ + if (c == '\r') + *key = KEY_SELECT; + + /* ^C was pressed */ + if (c == 0x3) + *key = KEY_QUIT; +} diff --git a/include/menu.h b/include/menu.h index ad5859437e..e74616cae8 100644 --- a/include/menu.h +++ b/include/menu.h @@ -35,4 +35,24 @@ int menu_default_choice(struct menu *m, void **choice); */ int menu_show(int bootdelay); +struct bootmenu_data { + int delay; /* delay for autoboot */ + int active; /* active menu entry */ + int count; /* total count of menu entries */ + struct bootmenu_entry *first; /* first menu entry */ +}; + +enum bootmenu_key { + KEY_NONE = 0, + KEY_UP, + KEY_DOWN, + KEY_SELECT, + KEY_QUIT, +}; + +void bootmenu_autoboot_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc); +void bootmenu_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc); + #endif /* __MENU_H__ */ diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig index e5e35fe51f..a3fb8b2a75 100644 --- a/lib/efi_loader/Kconfig +++ b/lib/efi_loader/Kconfig @@ -39,6 +39,26 @@ config CMD_BOOTEFI_BOOTMGR via UEFI variables Boot####, BootOrder, and BootNext. This enables the 'bootefi bootmgr' command. +config EFI_BOOT_MENU + bool "UEFI Boot Menu driven boot device selection" + default n + help + Select this option if you want to enable the menu driven boot device + selection. This menu provides the functionality to select a boot + option to start, and allow users to edit Boot#### and BootOrder. + If this menu is enabled, CLI can be disabled if the system boots + via UEFI variable Boot#### and BootOrder. + +config EFI_BOOT_MENU_DELAY + int "delay in seconds before automatically booting in UEFI Boot Menu" + default 10 + range -1 2147483647 + help + Delay before automatically booting in accordance with + "BootOrder" variable. + set to 0 to autoboot with no delay. + set to -1 to disable autoboot. + config EFI_SETUP_EARLY bool diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index 8c04ecbdc8..bbb3fac5bd 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -7,10 +7,12 @@ #define LOG_CATEGORY LOGC_EFI +#include <ansi.h> #include <common.h> #include <charset.h> #include <log.h> #include <malloc.h> +#include <menu.h> #include <efi_loader.h> #include <efi_variable.h> #include <asm/unaligned.h> @@ -22,14 +24,400 @@ static const struct efi_runtime_services *rs; * bootmgr implements the logic of trying to find a payload to boot * based on the BootOrder + BootXXXX variables, and then loading it. * - * TODO detecting a special key held (f9?) and displaying a boot menu - * like you would get on a PC would be clever. - * * TODO if we had a way to write and persist variables after the OS * has started, we'd also want to check OsIndications to see if we * should do normal or recovery boot. */ +#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024 + +typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit); + +/** + * struct efi_bootmgr_menu_entry - menu entry structure + * + * @menu_index: menu entry index + * @title: title of entry + * @key: unique key + * @bootmgr_menu: pointer to the menu structure + * @next: pointer to the next entry + * @func: callback function to be called when this entry is selected + * @data: data to be passed to the callback function + */ +struct efi_bootmgr_menu_entry { + u32 menu_index; + u16 *title; + char key[6]; + struct efi_bootmgr_menu *bootmgr_menu; + struct efi_bootmgr_menu_entry *next; + efi_bootmenu_entry_func func; + void *data; +}; + +/** + * struct efi_bootmgr_menuy - bootmgr menu structure + * + * @delay: delay for autoboot + * @active: active menu entry index + * @count: total count of menu entry + * @first: pointer to the first menu entry + */ +struct efi_bootmgr_menu { + int delay; + int active; + int count; + struct efi_bootmgr_menu_entry *first; +}; + +struct efi_bootmgr_menu_item { + u16 *title; + efi_bootmenu_entry_func func; + void *data; +}; + +struct efi_bootmgr_boot_selection_data { + u16 bootorder_index; + void *load_option; + int *selected; +}; + +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit); +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit); + +static struct efi_bootmgr_menu_item bootmgr_menu_items[] = { + {u"Boot Manager", efi_bootmgr_process_boot_selection}, + {u"Quit", NULL}, +}; + +static void efi_bootmgr_menu_print_entry(void *data) +{ + struct efi_bootmgr_menu_entry *entry = data; + int reverse = (entry->bootmgr_menu->active == entry->menu_index); + + /* TODO: support scroll or page for many entries */ + + /* + * Move cursor to line where the entry will be drown (entry->count) + * First 3 lines contain bootmgr menu header + one empty line + * For the last "Quit" entry, add one empty line + */ + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) + printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1); + else + printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1); + + puts(" "); + + if (reverse) + puts(ANSI_COLOR_REVERSE); + + printf("%ls", entry->title); + + if (reverse) + puts(ANSI_COLOR_RESET); +} + +static void efi_bootmgr_menu_display_statusline(struct menu *m) +{ + struct efi_bootmgr_menu_entry *entry; + struct efi_bootmgr_menu *bootmgr_menu; + + if (menu_default_choice(m, (void *)&entry) < 0) + return; + + bootmgr_menu = entry->bootmgr_menu; + + printf(ANSI_CURSOR_POSITION, 1, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 2, 1); + puts(" *** U-Boot EFI Boot Manager ***"); + puts(ANSI_CLEAR_LINE_TO_END); + printf(ANSI_CURSOR_POSITION, 3, 1); + puts(ANSI_CLEAR_LINE); + + /* First 3 lines are bootmgr_menu header + 2 empty lines between entries */ + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1); + puts(" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"); + puts(ANSI_CLEAR_LINE_TO_END); + printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1); + puts(ANSI_CLEAR_LINE); +} + +static char *efi_bootmgr_menu_choice_entry(void *data) +{ + int i; + int esc = 0; + struct efi_bootmgr_menu_entry *iter; + enum bootmenu_key key = KEY_NONE; + struct efi_bootmgr_menu *bootmgr_menu = data; + + while (1) { + if (bootmgr_menu->delay >= 0) { + /* Autoboot was not stopped */ + bootmenu_autoboot_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); + } else { + /* Some key was pressed, so autoboot was stopped */ + bootmenu_loop((struct bootmenu_data *)bootmgr_menu, &key, &esc); + } + + if (bootmgr_menu->delay == 0) + key = KEY_QUIT; + + switch (key) { + case KEY_UP: + if (bootmgr_menu->active > 0) + --bootmgr_menu->active; + /* no menu key selected, regenerate menu */ + return NULL; + case KEY_DOWN: + if (bootmgr_menu->active < bootmgr_menu->count - 1) + ++bootmgr_menu->active; + /* no menu key selected, regenerate menu */ + return NULL; + case KEY_SELECT: + iter = bootmgr_menu->first; + for (i = 0; i < bootmgr_menu->active; ++i) + iter = iter->next; + return iter->key; + case KEY_QUIT: + /* Quit by choosing the last entry */ + iter = bootmgr_menu->first; + while (iter->next) + iter = iter->next; + return iter->key; + default: + break; + } + } + + /* never happens */ + debug("bootmgr menu: this should not happen"); + return NULL; +} + +static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu) +{ + struct efi_bootmgr_menu_entry *next; + struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first; + + while (iter) { + next = iter->next; + free(iter); + iter = next; + } + free(bootmgr_menu); +} + +/** + * efi_bootmgr_process_common() - main handler for uefi bootmgr menu + * + * Construct the structures required to show the menu, then handle + * the user input intracting with u-boot menu functions. + * + * @items: pointer to the structure of each menu entry + * @count: the number of menu entry + * @delay: delay for autoboot/autoselect + * Return: status code + */ +static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items, + int count, int delay) +{ + u32 i; + bool exit = false; + efi_status_t ret; + struct menu *menu; + void *choice = NULL; + struct efi_bootmgr_menu_entry *entry; + struct efi_bootmgr_menu *bootmgr_menu; + struct efi_bootmgr_menu_entry *iter = NULL; + + if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX) + return EFI_OUT_OF_RESOURCES; + + bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu)); + if (!bootmgr_menu) + return EFI_OUT_OF_RESOURCES; + + bootmgr_menu->delay = delay; + bootmgr_menu->active = 0; + bootmgr_menu->first = NULL; + + for (i = 0; i < count; i++) { + entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry)); + if (!entry) { + ret = EFI_LOAD_ERROR; + goto out; + } + + entry->menu_index = i; + entry->title = items->title; + snprintf(entry->key, sizeof(entry->key), "%04X", i); + entry->bootmgr_menu = bootmgr_menu; + entry->func = items->func; + entry->data = items->data; + entry->next = NULL; + + if (!iter) + bootmgr_menu->first = entry; + else + iter->next = entry; + + iter = entry; + items++; + } + bootmgr_menu->count = count; + + menu = menu_create(NULL, 0, 1, efi_bootmgr_menu_display_statusline, + efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry, + bootmgr_menu); + if (!menu) { + ret = EFI_INVALID_PARAMETER; + goto out; + } + + for (entry = bootmgr_menu->first; entry; entry = entry->next) { + if (!menu_item_add(menu, entry->key, entry)) { + ret = EFI_INVALID_PARAMETER; + goto out; + } + } + + menu_default_set(menu, bootmgr_menu->first->key); + + while (!exit) { + puts(ANSI_CURSOR_HIDE); + puts(ANSI_CLEAR_CONSOLE); + printf(ANSI_CURSOR_POSITION, 1, 1); + + if (menu_get_choice(menu, &choice)) { + entry = choice; + if (entry->func) + ret = entry->func(entry->data, &exit); + + /* last entry "Quit" is selected, exit this menu */ + if (entry->menu_index == (entry->bootmgr_menu->count - 1)) { + ret = EFI_ABORTED; + break; + } + } + } + +out: + menu_destroy(menu); + efi_bootmgr_menu_destroy(bootmgr_menu); + + puts(ANSI_CLEAR_CONSOLE); + printf(ANSI_CURSOR_POSITION, 1, 1); + puts(ANSI_CURSOR_SHOW); + + return ret; +} + +static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count, + int *selected) +{ + u32 i; + efi_status_t ret; + efi_uintn_t size; + void *load_option; + struct efi_load_option lo; + u16 varname[] = u"Boot####"; + struct efi_bootmgr_menu_item *menu_item, *iter; + + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); + if (!menu_item) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + iter = menu_item; + for (i = 0; i < count; i++) { + efi_create_indexed_name(varname, sizeof(varname), + "Boot", bootorder[i]); + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); + if (!load_option) + continue; + + ret = efi_deserialize_load_option(&lo, load_option, &size); + if (ret != EFI_SUCCESS) { + log_warning("Invalid load option for %ls\n", varname); + free(load_option); + continue; + } + + if (lo.attributes & LOAD_OPTION_ACTIVE) { + struct efi_bootmgr_boot_selection_data *info; + + info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data)); + if (!info) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + info->bootorder_index = i; + info->load_option = load_option; + info->selected = selected; + iter->title = lo.label; + iter->func = efi_bootmgr_process_boot_selected; + iter->data = info; + iter++; + } + } + + /* add "Quit" entry */ + iter->title = u"Quit"; + iter->func = NULL; + iter->data = NULL; + count += 1; + + ret = efi_bootmgr_process_common(menu_item, count, -1); + +out: + iter = menu_item; + for (i = 0; i < count - 1; i++, iter++) { + free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option); + free(iter->data); + } + + free(menu_item); + + return ret; +} + +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit) +{ + struct efi_bootmgr_boot_selection_data *info = data; + + *exit = true; + + if (info) + *info->selected = info->bootorder_index; + + return EFI_SUCCESS; +} + +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit) +{ + u16 *bootorder; + efi_status_t ret; + efi_uintn_t num, size; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) + return EFI_NOT_FOUND; + + num = size / sizeof(u16); + ret = efi_bootmgr_show_boot_selection(bootorder, num, data); + if (ret == EFI_SUCCESS) + *exit = true; + + free(bootorder); + + return ret; +} + /** * try_load_entry() - try to load image for boot option * @@ -177,6 +565,30 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) } } + if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) { + int selected; + + bootmgr_menu_items[0].data = &selected; + ret = efi_bootmgr_process_common(bootmgr_menu_items, + ARRAY_SIZE(bootmgr_menu_items), + CONFIG_EFI_BOOT_MENU_DELAY); + if (ret == EFI_SUCCESS) { + /* bootorder may be updated in the bootmgr menu */ + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) { + log_info("BootOrder not defined\n"); + goto error; + } + ret = try_load_entry(bootorder[selected], handle, load_options); + free(bootorder); + if (ret == EFI_SUCCESS) + return ret; + + log_err("Failed to start the selected entry(Boot%04X)\n", + bootorder[selected]); + } + } + /* BootOrder */ bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); if (!bootorder) {
This patch enables the menu-driven boot device selection. User can select the Boot#### included in BootOrder variable. If user quits thie menu, or the selected Boot#### fails to boot, efi bootmgr continues to boot in accordance with BootOrder variable. This commit also moves the user input handling from cmd/bootmenu.c to common/menu.c to reuse it from efi boot menu. Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org> --- Changes in v2: - move user input handling from cmd/bootmenu.c to common/menu.c - keyboard buffer is drained before asking question in common/menu.c::bootmenu_autoboot_loop() - Add EFI_BOOT_MENU_DELAY Kconfig option to set delay for autoboot and disable autoboot - remove unnecessary "autoboot" member in struc efi_bootmgr_menu cmd/bootmenu.c | 145 ------------ common/menu.c | 137 ++++++++++++ include/menu.h | 20 ++ lib/efi_loader/Kconfig | 20 ++ lib/efi_loader/efi_bootmgr.c | 418 ++++++++++++++++++++++++++++++++++- 5 files changed, 592 insertions(+), 148 deletions(-)