1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
11 #include <efi_config.h>
12 #include <efi_variable.h>
18 #include <linux/delay.h>
19 #include <linux/string.h>
21 /* maximum bootmenu entries */
24 /* maximal size of bootmenu env
25 * 9 = strlen("bootmenu_")
26 * 2 = strlen(MAX_COUNT)
29 #define MAX_ENV_SIZE (9 + 2 + 1)
32 BOOTMENU_RET_SUCCESS = 0,
39 BOOTMENU_TYPE_NONE = 0,
40 BOOTMENU_TYPE_BOOTMENU,
41 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
44 struct bootmenu_entry {
45 unsigned short int num; /* unique number 0 .. MAX_COUNT */
46 char key[3]; /* key identifier of number */
47 char *title; /* title of entry */
48 char *command; /* hush command of entry */
49 enum boot_type type; /* boot type of entry */
50 u16 bootorder; /* order for each boot type */
51 struct bootmenu_data *menu; /* this bootmenu */
52 struct bootmenu_entry *next; /* next menu entry (num+1) */
55 static char *bootmenu_getoption(unsigned short int n)
57 char name[MAX_ENV_SIZE];
62 sprintf(name, "bootmenu_%d", n);
66 static void bootmenu_print_entry(void *data)
68 struct bootmenu_entry *entry = data;
69 int reverse = (entry->menu->active == entry->num);
72 * Move cursor to line where the entry will be drown (entry->num)
73 * First 3 lines contain bootmenu header + 1 empty line
75 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
78 puts(ANSI_COLOR_REVERSE);
80 printf("%s", entry->title);
83 puts(ANSI_COLOR_RESET);
86 static char *bootmenu_choice_entry(void *data)
88 struct cli_ch_state s_cch, *cch = &s_cch;
89 struct bootmenu_data *menu = data;
90 struct bootmenu_entry *iter;
91 enum bootmenu_key key = BKEY_NONE;
97 if (menu->delay >= 0) {
98 /* Autoboot was not stopped */
99 key = bootmenu_autoboot_loop(menu, cch);
101 /* Some key was pressed, so autoboot was stopped */
102 key = bootmenu_loop(menu, cch);
107 if (menu->active > 0)
109 /* no menu key selected, regenerate menu */
112 if (menu->active < menu->count - 1)
114 /* no menu key selected, regenerate menu */
118 for (i = 0; i < menu->active; ++i)
122 /* Quit by choosing the last entry - U-Boot console */
133 debug("bootmenu: this should not happen");
137 static void bootmenu_destroy(struct bootmenu_data *menu)
139 struct bootmenu_entry *iter = menu->first;
140 struct bootmenu_entry *next;
153 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
155 * This function read the "bootmenu_x" U-Boot environment variable
156 * and generate the bootmenu entries.
158 * @menu: pointer to the bootmenu structure
159 * @current: pointer to the last bootmenu entry list
160 * @index: pointer to the index of the last bootmenu entry,
161 * the number of bootmenu entry is added by this function
162 * Return: 1 on success, negative value on error
164 static int prepare_bootmenu_entry(struct bootmenu_data *menu,
165 struct bootmenu_entry **current,
166 unsigned short int *index)
170 unsigned short int i = *index;
171 struct bootmenu_entry *entry = NULL;
172 struct bootmenu_entry *iter = *current;
174 while ((option = bootmenu_getoption(i))) {
176 /* bootmenu_[num] format is "[title]=[commands]" */
177 sep = strchr(option, '=');
179 printf("Invalid bootmenu entry: %s\n", option);
183 entry = malloc(sizeof(struct bootmenu_entry));
187 entry->title = strndup(option, sep - option);
193 entry->command = strdup(sep + 1);
194 if (!entry->command) {
200 sprintf(entry->key, "%d", i);
204 entry->type = BOOTMENU_TYPE_BOOTMENU;
205 entry->bootorder = i;
216 if (i == MAX_COUNT - 1)
226 #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR)) && (CONFIG_IS_ENABLED(CMD_EFICONFIG))
228 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
230 * This function read the "BootOrder" UEFI variable
231 * and generate the bootmenu entries in the order of "BootOrder".
233 * @menu: pointer to the bootmenu structure
234 * @current: pointer to the last bootmenu entry list
235 * @index: pointer to the index of the last bootmenu entry,
236 * the number of uefi entry is added by this function
237 * Return: 1 on success, negative value on error
239 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
240 struct bootmenu_entry **current,
241 unsigned short int *index)
246 efi_uintn_t num, size;
248 struct efi_load_option lo;
249 u16 varname[] = u"Boot####";
250 unsigned short int i = *index;
251 struct bootmenu_entry *entry = NULL;
252 struct bootmenu_entry *iter = *current;
254 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
258 num = size / sizeof(u16);
259 for (j = 0; j < num; j++) {
260 entry = malloc(sizeof(struct bootmenu_entry));
264 efi_create_indexed_name(varname, sizeof(varname),
265 "Boot", bootorder[j]);
266 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
270 ret = efi_deserialize_load_option(&lo, load_option, &size);
271 if (ret != EFI_SUCCESS) {
272 log_warning("Invalid load option for %ls\n", varname);
278 if (lo.attributes & LOAD_OPTION_ACTIVE) {
281 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
289 utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
290 entry->command = strdup("bootefi bootmgr");
291 sprintf(entry->key, "%d", i);
294 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
295 entry->bootorder = bootorder[j];
309 if (i == MAX_COUNT - 1)
321 static struct bootmenu_data *bootmenu_create(int delay)
324 unsigned short int i = 0;
325 struct bootmenu_data *menu;
326 struct bootmenu_entry *iter = NULL;
327 struct bootmenu_entry *entry;
330 menu = malloc(sizeof(struct bootmenu_data));
338 default_str = env_get("bootmenu_default");
340 menu->active = (int)simple_strtol(default_str, NULL, 10);
342 ret = prepare_bootmenu_entry(menu, &iter, &i);
346 #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR)) && (CONFIG_IS_ENABLED(CMD_EFICONFIG))
347 if (i < MAX_COUNT - 1) {
348 efi_status_t efi_ret;
351 * UEFI specification requires booting from removal media using
352 * a architecture-specific default image name such as BOOTAA64.EFI.
354 efi_ret = eficonfig_generate_media_device_boot_option();
355 if (efi_ret != EFI_SUCCESS && efi_ret != EFI_NOT_FOUND)
358 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
359 if (ret < 0 && ret != -ENOENT)
364 /* Add U-Boot console entry at the end */
365 if (i <= MAX_COUNT - 1) {
366 entry = malloc(sizeof(struct bootmenu_entry));
370 /* Add Quit entry if entering U-Boot console is disabled */
371 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
372 entry->title = strdup("U-Boot console");
374 entry->title = strdup("Quit");
381 entry->command = strdup("");
382 if (!entry->command) {
388 sprintf(entry->key, "%d", i);
392 entry->type = BOOTMENU_TYPE_NONE;
406 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
407 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
414 bootmenu_destroy(menu);
418 static void menu_display_statusline(struct menu *m)
420 struct bootmenu_entry *entry;
421 struct bootmenu_data *menu;
423 if (menu_default_choice(m, (void *)&entry) < 0)
428 printf(ANSI_CURSOR_POSITION, 1, 1);
429 puts(ANSI_CLEAR_LINE);
430 printf(ANSI_CURSOR_POSITION, 2, 3);
431 puts("*** U-Boot Boot Menu ***");
432 puts(ANSI_CLEAR_LINE_TO_END);
433 printf(ANSI_CURSOR_POSITION, 3, 1);
434 puts(ANSI_CLEAR_LINE);
436 /* First 3 lines are bootmenu header + 2 empty lines between entries */
437 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
438 puts(ANSI_CLEAR_LINE);
439 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
440 puts("Press UP/DOWN to move, ENTER to select, ESC to quit");
441 puts(ANSI_CLEAR_LINE_TO_END);
442 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
443 puts(ANSI_CLEAR_LINE);
446 static void handle_uefi_bootnext(void)
452 /* Initialize EFI drivers */
453 ret = efi_init_obj_list();
454 if (ret != EFI_SUCCESS) {
455 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
456 ret & ~EFI_ERROR_MASK);
461 /* If UEFI BootNext variable is set, boot the BootNext load option */
463 ret = efi_get_variable_int(u"BootNext",
464 &efi_global_variable_guid,
465 NULL, &size, &bootnext, NULL);
466 if (ret == EFI_SUCCESS)
467 /* BootNext does exist here, try to boot */
468 run_command("bootefi bootmgr", 0);
471 static enum bootmenu_ret bootmenu_show(int delay)
477 char *command = NULL;
479 struct bootmenu_entry *iter;
480 int ret = BOOTMENU_RET_SUCCESS;
481 struct bootmenu_data *bootmenu;
482 efi_status_t efi_ret = EFI_SUCCESS;
485 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
486 handle_uefi_bootnext();
488 /* If delay is 0 do not create menu, just run first entry */
490 option = bootmenu_getoption(0);
492 puts("bootmenu option 0 was not found\n");
493 return BOOTMENU_RET_FAIL;
495 sep = strchr(option, '=');
497 puts("bootmenu option 0 is invalid\n");
498 return BOOTMENU_RET_FAIL;
500 cmd_ret = run_command(sep + 1, 0);
501 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
504 bootmenu = bootmenu_create(delay);
506 return BOOTMENU_RET_FAIL;
508 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
509 bootmenu_print_entry, bootmenu_choice_entry,
512 bootmenu_destroy(bootmenu);
513 return BOOTMENU_RET_FAIL;
516 for (iter = bootmenu->first; iter; iter = iter->next) {
517 if (menu_item_add(menu, iter->key, iter) != 1)
521 /* Default menu entry is always first */
522 menu_default_set(menu, "0");
524 puts(ANSI_CURSOR_HIDE);
525 puts(ANSI_CLEAR_CONSOLE);
526 printf(ANSI_CURSOR_POSITION, 1, 1);
530 if (menu_get_choice(menu, &choice) == 1) {
532 title = strdup(iter->title);
533 command = strdup(iter->command);
535 /* last entry is U-Boot console or Quit */
536 if (iter->num == iter->menu->count - 1) {
537 ret = BOOTMENU_RET_QUIT;
545 * If the selected entry is UEFI BOOT####, set the BootNext variable.
546 * Then uefi bootmgr is invoked by the preset command in iter->command.
548 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
549 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
551 * UEFI specification requires BootNext variable needs non-volatile
552 * attribute, but this BootNext is only used inside of U-Boot and
553 * removed by efi bootmgr once BootNext is processed.
554 * So this BootNext can be volatile.
556 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
557 EFI_VARIABLE_BOOTSERVICE_ACCESS |
558 EFI_VARIABLE_RUNTIME_ACCESS,
559 sizeof(u16), &iter->bootorder, false);
560 if (efi_ret != EFI_SUCCESS)
567 bootmenu_destroy(bootmenu);
570 puts(ANSI_CURSOR_SHOW);
571 puts(ANSI_CLEAR_CONSOLE);
572 printf(ANSI_CURSOR_POSITION, 1, 1);
575 if (title && command) {
576 debug("Starting entry '%s'\n", title);
578 if (efi_ret == EFI_SUCCESS)
579 cmd_ret = run_command(command, 0);
583 #ifdef CFG_POSTBOOTMENU
584 run_command(CFG_POSTBOOTMENU, 0);
587 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
588 ret = BOOTMENU_RET_FAIL;
593 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
594 int menu_show(int bootdelay)
599 ret = bootmenu_show(bootdelay);
601 if (ret == BOOTMENU_RET_UPDATED)
604 if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) {
605 if (ret == BOOTMENU_RET_QUIT) {
606 /* default boot process */
607 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
608 run_command("bootefi bootmgr", 0);
610 run_command("run bootcmd", 0);
617 return -1; /* -1 - abort boot and run monitor code */
621 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
623 char *delay_str = NULL;
626 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
627 delay = CONFIG_BOOTDELAY;
634 delay_str = env_get("bootmenu_delay");
637 delay = (int)simple_strtol(delay_str, NULL, 10);
639 bootmenu_show(delay);
644 bootmenu, 2, 1, do_bootmenu,
645 "ANSI terminal bootmenu",
647 " - show ANSI terminal bootmenu with autoboot delay"