1 // SPDX-License-Identifier: GPL-2.0+
10 #include <efi_config.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 char *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 static char *bootmenu_getoption(unsigned short int n)
56 char name[MAX_ENV_SIZE];
61 sprintf(name, "bootmenu_%d", n);
65 static void bootmenu_print_entry(void *data)
67 struct bootmenu_entry *entry = data;
68 int reverse = (entry->menu->active == entry->num);
71 * Move cursor to line where the entry will be drown (entry->num)
72 * First 3 lines contain bootmenu header + 1 empty line
74 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
77 puts(ANSI_COLOR_REVERSE);
79 printf("%s", entry->title);
82 puts(ANSI_COLOR_RESET);
85 static char *bootmenu_choice_entry(void *data)
87 struct cli_ch_state s_cch, *cch = &s_cch;
88 struct bootmenu_data *menu = data;
89 struct bootmenu_entry *iter;
90 enum bootmenu_key key = BKEY_NONE;
96 if (menu->delay >= 0) {
97 /* Autoboot was not stopped */
98 key = bootmenu_autoboot_loop(menu, cch);
100 /* Some key was pressed, so autoboot was stopped */
101 key = bootmenu_loop(menu, cch);
106 if (menu->active > 0)
108 /* no menu key selected, regenerate menu */
111 if (menu->active < menu->count - 1)
113 /* no menu key selected, regenerate menu */
117 for (i = 0; i < menu->active; ++i)
121 /* Quit by choosing the last entry */
132 debug("bootmenu: this should not happen");
136 static void bootmenu_destroy(struct bootmenu_data *menu)
138 struct bootmenu_entry *iter = menu->first;
139 struct bootmenu_entry *next;
152 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
154 * This function read the "bootmenu_x" U-Boot environment variable
155 * and generate the bootmenu entries.
157 * @menu: pointer to the bootmenu structure
158 * @current: pointer to the last bootmenu entry list
159 * @index: pointer to the index of the last bootmenu entry,
160 * the number of bootmenu entry is added by this function
161 * Return: 1 on success, negative value on error
163 static int prepare_bootmenu_entry(struct bootmenu_data *menu,
164 struct bootmenu_entry **current,
165 unsigned short int *index)
169 unsigned short int i = *index;
170 struct bootmenu_entry *entry = NULL;
171 struct bootmenu_entry *iter = *current;
173 while ((option = bootmenu_getoption(i))) {
175 /* bootmenu_[num] format is "[title]=[commands]" */
176 sep = strchr(option, '=');
178 printf("Invalid bootmenu entry: %s\n", option);
182 entry = malloc(sizeof(struct bootmenu_entry));
186 entry->title = strndup(option, sep - option);
192 entry->command = strdup(sep + 1);
193 if (!entry->command) {
199 sprintf(entry->key, "%d", i);
203 entry->type = BOOTMENU_TYPE_BOOTMENU;
204 entry->bootorder = i;
215 if (i == MAX_COUNT - 1)
225 #if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
227 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
229 * This function read the "BootOrder" UEFI variable
230 * and generate the bootmenu entries in the order of "BootOrder".
232 * @menu: pointer to the bootmenu structure
233 * @current: pointer to the last bootmenu entry list
234 * @index: pointer to the index of the last bootmenu entry,
235 * the number of uefi entry is added by this function
236 * Return: 1 on success, negative value on error
238 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
239 struct bootmenu_entry **current,
240 unsigned short int *index)
245 efi_uintn_t num, size;
247 struct efi_load_option lo;
248 u16 varname[] = u"Boot####";
249 unsigned short int i = *index;
250 struct bootmenu_entry *entry = NULL;
251 struct bootmenu_entry *iter = *current;
253 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
257 num = size / sizeof(u16);
258 for (j = 0; j < num; j++) {
259 entry = malloc(sizeof(struct bootmenu_entry));
263 efi_create_indexed_name(varname, sizeof(varname),
264 "Boot", bootorder[j]);
265 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
269 ret = efi_deserialize_load_option(&lo, load_option, &size);
270 if (ret != EFI_SUCCESS) {
271 log_warning("Invalid load option for %ls\n", varname);
277 if (lo.attributes & LOAD_OPTION_ACTIVE) {
280 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
288 utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
289 entry->command = strdup("bootefi bootmgr");
290 sprintf(entry->key, "%d", i);
293 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
294 entry->bootorder = bootorder[j];
308 if (i == MAX_COUNT - 1)
320 static struct bootmenu_data *bootmenu_create(int delay)
323 unsigned short int i = 0;
324 struct bootmenu_data *menu;
325 struct bootmenu_entry *iter = NULL;
326 struct bootmenu_entry *entry;
329 menu = malloc(sizeof(struct bootmenu_data));
337 default_str = env_get("bootmenu_default");
339 menu->active = (int)simple_strtol(default_str, NULL, 10);
341 ret = prepare_bootmenu_entry(menu, &iter, &i);
345 #if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
346 if (i < MAX_COUNT - 1) {
347 efi_status_t efi_ret;
350 * UEFI specification requires booting from removal media using
351 * a architecture-specific default image name such as BOOTAA64.EFI.
353 efi_ret = efi_bootmgr_update_media_device_boot_option();
354 if (efi_ret != EFI_SUCCESS)
357 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
358 if (ret < 0 && ret != -ENOENT)
363 /* Add Exit entry at the end */
364 if (i <= MAX_COUNT - 1) {
365 entry = malloc(sizeof(struct bootmenu_entry));
369 /* Add Quit entry if exiting bootmenu is disabled */
370 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
371 entry->title = strdup("Exit");
373 entry->title = strdup("Quit");
380 entry->command = strdup("");
381 if (!entry->command) {
387 sprintf(entry->key, "%d", i);
391 entry->type = BOOTMENU_TYPE_NONE;
405 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
406 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
413 bootmenu_destroy(menu);
417 static void menu_display_statusline(struct menu *m)
419 struct bootmenu_entry *entry;
420 struct bootmenu_data *menu;
422 if (menu_default_choice(m, (void *)&entry) < 0)
427 printf(ANSI_CURSOR_POSITION, 1, 1);
428 puts(ANSI_CLEAR_LINE);
429 printf(ANSI_CURSOR_POSITION, 2, 3);
430 puts("*** U-Boot Boot Menu ***");
431 puts(ANSI_CLEAR_LINE_TO_END);
432 printf(ANSI_CURSOR_POSITION, 3, 1);
433 puts(ANSI_CLEAR_LINE);
435 /* First 3 lines are bootmenu header + 2 empty lines between entries */
436 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
437 puts(ANSI_CLEAR_LINE);
438 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
439 puts("Press UP/DOWN to move, ENTER to select, ESC to quit");
440 puts(ANSI_CLEAR_LINE_TO_END);
441 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
442 puts(ANSI_CLEAR_LINE);
445 static void handle_uefi_bootnext(void)
451 /* Initialize EFI drivers */
452 ret = efi_init_obj_list();
453 if (ret != EFI_SUCCESS) {
454 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
455 ret & ~EFI_ERROR_MASK);
460 /* If UEFI BootNext variable is set, boot the BootNext load option */
462 ret = efi_get_variable_int(u"BootNext",
463 &efi_global_variable_guid,
464 NULL, &size, &bootnext, NULL);
465 if (ret == EFI_SUCCESS)
466 /* BootNext does exist here, try to boot */
467 run_command("bootefi bootmgr", 0);
470 static enum bootmenu_ret bootmenu_show(int delay)
476 char *command = NULL;
478 struct bootmenu_entry *iter;
479 int ret = BOOTMENU_RET_SUCCESS;
480 struct bootmenu_data *bootmenu;
481 efi_status_t efi_ret = EFI_SUCCESS;
484 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
485 handle_uefi_bootnext();
487 /* If delay is 0 do not create menu, just run first entry */
489 option = bootmenu_getoption(0);
491 puts("bootmenu option 0 was not found\n");
492 return BOOTMENU_RET_FAIL;
494 sep = strchr(option, '=');
496 puts("bootmenu option 0 is invalid\n");
497 return BOOTMENU_RET_FAIL;
499 cmd_ret = run_command(sep + 1, 0);
500 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
503 bootmenu = bootmenu_create(delay);
505 return BOOTMENU_RET_FAIL;
507 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
508 bootmenu_print_entry, bootmenu_choice_entry,
511 bootmenu_destroy(bootmenu);
512 return BOOTMENU_RET_FAIL;
515 for (iter = bootmenu->first; iter; iter = iter->next) {
516 if (menu_item_add(menu, iter->key, iter) != 1)
520 /* Default menu entry is always first */
521 menu_default_set(menu, "0");
523 puts(ANSI_CURSOR_HIDE);
524 puts(ANSI_CLEAR_CONSOLE);
525 printf(ANSI_CURSOR_POSITION, 1, 1);
529 if (menu_get_choice(menu, &choice) == 1) {
531 title = strdup(iter->title);
532 command = strdup(iter->command);
534 /* last entry exits bootmenu */
535 if (iter->num == iter->menu->count - 1) {
536 ret = BOOTMENU_RET_QUIT;
544 * If the selected entry is UEFI BOOT####, set the BootNext variable.
545 * Then uefi bootmgr is invoked by the preset command in iter->command.
547 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
548 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
550 * UEFI specification requires BootNext variable needs non-volatile
551 * attribute, but this BootNext is only used inside of U-Boot and
552 * removed by efi bootmgr once BootNext is processed.
553 * So this BootNext can be volatile.
555 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
556 EFI_VARIABLE_BOOTSERVICE_ACCESS |
557 EFI_VARIABLE_RUNTIME_ACCESS,
558 sizeof(u16), &iter->bootorder, false);
559 if (efi_ret != EFI_SUCCESS)
566 bootmenu_destroy(bootmenu);
569 puts(ANSI_CURSOR_SHOW);
570 puts(ANSI_CLEAR_CONSOLE);
571 printf(ANSI_CURSOR_POSITION, 1, 1);
574 if (title && command) {
575 debug("Starting entry '%s'\n", title);
577 if (efi_ret == EFI_SUCCESS)
578 cmd_ret = run_command(command, 0);
582 #ifdef CFG_POSTBOOTMENU
583 run_command(CFG_POSTBOOTMENU, 0);
586 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
587 ret = BOOTMENU_RET_FAIL;
592 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
593 int menu_show(int bootdelay)
598 ret = bootmenu_show(bootdelay);
600 if (ret == BOOTMENU_RET_UPDATED)
603 if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) {
604 if (ret == BOOTMENU_RET_QUIT) {
605 /* default boot process */
606 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
607 run_command("bootefi bootmgr", 0);
609 run_command("run bootcmd", 0);
616 return -1; /* -1 - abort boot and run monitor code */
620 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
622 char *delay_str = NULL;
625 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
626 delay = CONFIG_BOOTDELAY;
633 delay_str = env_get("bootmenu_delay");
636 delay = (int)simple_strtol(delay_str, NULL, 10);
638 bootmenu_show(delay);
643 bootmenu, 2, 1, do_bootmenu,
644 "ANSI terminal bootmenu",
646 " - show ANSI terminal bootmenu with autoboot delay"