1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
10 #include <efi_loader.h>
11 #include <efi_variable.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
20 /* maximum bootmenu entries */
23 /* maximal size of bootmenu env
24 * 9 = strlen("bootmenu_")
25 * 2 = strlen(MAX_COUNT)
28 #define MAX_ENV_SIZE (9 + 2 + 1)
31 BOOTMENU_RET_SUCCESS = 0,
38 BOOTMENU_TYPE_NONE = 0,
39 BOOTMENU_TYPE_BOOTMENU,
40 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
43 struct bootmenu_entry {
44 unsigned short int num; /* unique number 0 .. MAX_COUNT */
45 char key[3]; /* key identifier of number */
46 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 bootmenu_data *menu = data;
88 struct bootmenu_entry *iter;
89 enum bootmenu_key key = KEY_NONE;
94 if (menu->delay >= 0) {
95 /* Autoboot was not stopped */
96 bootmenu_autoboot_loop(menu, &key, &esc);
98 /* Some key was pressed, so autoboot was stopped */
99 bootmenu_loop(menu, &key, &esc);
104 if (menu->active > 0)
106 /* no menu key selected, regenerate menu */
109 if (menu->active < menu->count - 1)
111 /* no menu key selected, regenerate menu */
115 for (i = 0; i < menu->active; ++i)
119 /* Quit by choosing the last entry - U-Boot console */
130 debug("bootmenu: this should not happen");
134 static void bootmenu_destroy(struct bootmenu_data *menu)
136 struct bootmenu_entry *iter = menu->first;
137 struct bootmenu_entry *next;
150 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
152 * This function read the "bootmenu_x" U-Boot environment variable
153 * and generate the bootmenu entries.
155 * @menu: pointer to the bootmenu structure
156 * @current: pointer to the last bootmenu entry list
157 * @index: pointer to the index of the last bootmenu entry,
158 * the number of bootmenu entry is added by this function
159 * Return: 1 on success, negative value on error
161 static int prepare_bootmenu_entry(struct bootmenu_data *menu,
162 struct bootmenu_entry **current,
163 unsigned short int *index)
167 unsigned short int i = *index;
168 struct bootmenu_entry *entry = NULL;
169 struct bootmenu_entry *iter = *current;
171 while ((option = bootmenu_getoption(i))) {
173 /* bootmenu_[num] format is "[title]=[commands]" */
174 sep = strchr(option, '=');
176 printf("Invalid bootmenu entry: %s\n", option);
180 entry = malloc(sizeof(struct bootmenu_entry));
184 entry->title = strndup(option, sep - option);
190 entry->command = strdup(sep + 1);
191 if (!entry->command) {
197 sprintf(entry->key, "%d", i);
201 entry->type = BOOTMENU_TYPE_BOOTMENU;
202 entry->bootorder = i;
213 if (i == MAX_COUNT - 1)
223 #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR))
225 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
227 * This function read the "BootOrder" UEFI variable
228 * and generate the bootmenu entries in the order of "BootOrder".
230 * @menu: pointer to the bootmenu structure
231 * @current: pointer to the last bootmenu entry list
232 * @index: pointer to the index of the last bootmenu entry,
233 * the number of uefi entry is added by this function
234 * Return: 1 on success, negative value on error
236 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
237 struct bootmenu_entry **current,
238 unsigned short int *index)
243 efi_uintn_t num, size;
245 struct efi_load_option lo;
246 u16 varname[] = u"Boot####";
247 unsigned short int i = *index;
248 struct bootmenu_entry *entry = NULL;
249 struct bootmenu_entry *iter = *current;
251 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
255 num = size / sizeof(u16);
256 for (j = 0; j < num; j++) {
257 entry = malloc(sizeof(struct bootmenu_entry));
261 efi_create_indexed_name(varname, sizeof(varname),
262 "Boot", bootorder[j]);
263 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
267 ret = efi_deserialize_load_option(&lo, load_option, &size);
268 if (ret != EFI_SUCCESS) {
269 log_warning("Invalid load option for %ls\n", varname);
275 if (lo.attributes & LOAD_OPTION_ACTIVE) {
278 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
286 utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
287 entry->command = strdup("bootefi bootmgr");
288 sprintf(entry->key, "%d", i);
291 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
292 entry->bootorder = bootorder[j];
306 if (i == MAX_COUNT - 1)
318 static struct bootmenu_data *bootmenu_create(int delay)
321 unsigned short int i = 0;
322 struct bootmenu_data *menu;
323 struct bootmenu_entry *iter = NULL;
324 struct bootmenu_entry *entry;
327 menu = malloc(sizeof(struct bootmenu_data));
335 default_str = env_get("bootmenu_default");
337 menu->active = (int)simple_strtol(default_str, NULL, 10);
339 ret = prepare_bootmenu_entry(menu, &iter, &i);
343 #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR))
344 if (i < MAX_COUNT - 1) {
345 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
346 if (ret < 0 && ret != -ENOENT)
351 /* Add U-Boot console entry at the end */
352 if (i <= MAX_COUNT - 1) {
353 entry = malloc(sizeof(struct bootmenu_entry));
357 /* Add Quit entry if entering U-Boot console is disabled */
358 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
359 entry->title = strdup("U-Boot console");
361 entry->title = strdup("Quit");
368 entry->command = strdup("");
369 if (!entry->command) {
375 sprintf(entry->key, "%d", i);
379 entry->type = BOOTMENU_TYPE_NONE;
393 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
394 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
401 bootmenu_destroy(menu);
405 static void menu_display_statusline(struct menu *m)
407 struct bootmenu_entry *entry;
408 struct bootmenu_data *menu;
410 if (menu_default_choice(m, (void *)&entry) < 0)
415 printf(ANSI_CURSOR_POSITION, 1, 1);
416 puts(ANSI_CLEAR_LINE);
417 printf(ANSI_CURSOR_POSITION, 2, 3);
418 puts("*** U-Boot Boot Menu ***");
419 puts(ANSI_CLEAR_LINE_TO_END);
420 printf(ANSI_CURSOR_POSITION, 3, 1);
421 puts(ANSI_CLEAR_LINE);
423 /* First 3 lines are bootmenu header + 2 empty lines between entries */
424 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
425 puts(ANSI_CLEAR_LINE);
426 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
427 puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
428 puts(ANSI_CLEAR_LINE_TO_END);
429 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
430 puts(ANSI_CLEAR_LINE);
433 static void handle_uefi_bootnext(void)
439 /* Initialize EFI drivers */
440 ret = efi_init_obj_list();
441 if (ret != EFI_SUCCESS) {
442 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
443 ret & ~EFI_ERROR_MASK);
448 /* If UEFI BootNext variable is set, boot the BootNext load option */
450 ret = efi_get_variable_int(u"BootNext",
451 &efi_global_variable_guid,
452 NULL, &size, &bootnext, NULL);
453 if (ret == EFI_SUCCESS)
454 /* BootNext does exist here, try to boot */
455 run_command("bootefi bootmgr", 0);
458 static enum bootmenu_ret bootmenu_show(int delay)
464 char *command = NULL;
466 struct bootmenu_entry *iter;
467 int ret = BOOTMENU_RET_SUCCESS;
468 struct bootmenu_data *bootmenu;
469 efi_status_t efi_ret = EFI_SUCCESS;
472 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
473 handle_uefi_bootnext();
475 /* If delay is 0 do not create menu, just run first entry */
477 option = bootmenu_getoption(0);
479 puts("bootmenu option 0 was not found\n");
480 return BOOTMENU_RET_FAIL;
482 sep = strchr(option, '=');
484 puts("bootmenu option 0 is invalid\n");
485 return BOOTMENU_RET_FAIL;
487 cmd_ret = run_command(sep + 1, 0);
488 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
491 bootmenu = bootmenu_create(delay);
493 return BOOTMENU_RET_FAIL;
495 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
496 bootmenu_print_entry, bootmenu_choice_entry,
499 bootmenu_destroy(bootmenu);
500 return BOOTMENU_RET_FAIL;
503 for (iter = bootmenu->first; iter; iter = iter->next) {
504 if (menu_item_add(menu, iter->key, iter) != 1)
508 /* Default menu entry is always first */
509 menu_default_set(menu, "0");
511 puts(ANSI_CURSOR_HIDE);
512 puts(ANSI_CLEAR_CONSOLE);
513 printf(ANSI_CURSOR_POSITION, 1, 1);
517 if (menu_get_choice(menu, &choice) == 1) {
519 title = strdup(iter->title);
520 command = strdup(iter->command);
522 /* last entry is U-Boot console or Quit */
523 if (iter->num == iter->menu->count - 1) {
524 ret = BOOTMENU_RET_QUIT;
532 * If the selected entry is UEFI BOOT####, set the BootNext variable.
533 * Then uefi bootmgr is invoked by the preset command in iter->command.
535 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
536 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
538 * UEFI specification requires BootNext variable needs non-volatile
539 * attribute, but this BootNext is only used inside of U-Boot and
540 * removed by efi bootmgr once BootNext is processed.
541 * So this BootNext can be volatile.
543 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
544 EFI_VARIABLE_BOOTSERVICE_ACCESS |
545 EFI_VARIABLE_RUNTIME_ACCESS,
546 sizeof(u16), &iter->bootorder, false);
547 if (efi_ret != EFI_SUCCESS)
554 bootmenu_destroy(bootmenu);
557 puts(ANSI_CURSOR_SHOW);
558 puts(ANSI_CLEAR_CONSOLE);
559 printf(ANSI_CURSOR_POSITION, 1, 1);
562 if (title && command) {
563 debug("Starting entry '%s'\n", title);
565 if (efi_ret == EFI_SUCCESS)
566 cmd_ret = run_command(command, 0);
570 #ifdef CONFIG_POSTBOOTMENU
571 run_command(CONFIG_POSTBOOTMENU, 0);
574 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
575 ret = BOOTMENU_RET_FAIL;
580 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
581 int menu_show(int bootdelay)
586 ret = bootmenu_show(bootdelay);
588 if (ret == BOOTMENU_RET_UPDATED)
591 if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) {
592 if (ret == BOOTMENU_RET_QUIT) {
593 /* default boot process */
594 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
595 run_command("bootefi bootmgr", 0);
597 run_command("run bootcmd", 0);
604 return -1; /* -1 - abort boot and run monitor code */
608 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
610 char *delay_str = NULL;
613 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
614 delay = CONFIG_BOOTDELAY;
621 delay_str = env_get("bootmenu_delay");
624 delay = (int)simple_strtol(delay_str, NULL, 10);
626 bootmenu_show(delay);
631 bootmenu, 2, 1, do_bootmenu,
632 "ANSI terminal bootmenu",
634 " - show ANSI terminal bootmenu with autoboot delay"