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 u16 *title; /* title of entry */
47 char *command; /* hush command of entry */
48 enum boot_type type; /* boot type of entry */
49 u16 bootorder; /* order for each boot type */
50 struct bootmenu_data *menu; /* this bootmenu */
51 struct bootmenu_entry *next; /* next menu entry (num+1) */
54 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("%ls", 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)
168 unsigned short int i = *index;
169 struct bootmenu_entry *entry = NULL;
170 struct bootmenu_entry *iter = *current;
172 while ((option = bootmenu_getoption(i))) {
175 sep = strchr(option, '=');
177 printf("Invalid bootmenu entry: %s\n", option);
181 entry = malloc(sizeof(struct bootmenu_entry));
186 buf = calloc(1, (len + 1) * sizeof(u16));
192 utf8_utf16_strncpy(&buf, option, len);
194 len = strlen(sep + 1);
195 entry->command = malloc(len + 1);
196 if (!entry->command) {
201 memcpy(entry->command, sep + 1, len);
202 entry->command[len] = 0;
204 sprintf(entry->key, "%d", i);
208 entry->type = BOOTMENU_TYPE_BOOTMENU;
209 entry->bootorder = i;
220 if (i == MAX_COUNT - 1)
231 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
233 * This function read the "BootOrder" UEFI variable
234 * and generate the bootmenu entries in the order of "BootOrder".
236 * @menu: pointer to the bootmenu structure
237 * @current: pointer to the last bootmenu entry list
238 * @index: pointer to the index of the last bootmenu entry,
239 * the number of uefi entry is added by this function
240 * Return: 1 on success, negative value on error
242 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
243 struct bootmenu_entry **current,
244 unsigned short int *index)
249 efi_uintn_t num, size;
251 struct efi_load_option lo;
252 u16 varname[] = u"Boot####";
253 unsigned short int i = *index;
254 struct bootmenu_entry *entry = NULL;
255 struct bootmenu_entry *iter = *current;
257 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
261 num = size / sizeof(u16);
262 for (j = 0; j < num; j++) {
263 entry = malloc(sizeof(struct bootmenu_entry));
267 efi_create_indexed_name(varname, sizeof(varname),
268 "Boot", bootorder[j]);
269 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
273 ret = efi_deserialize_load_option(&lo, load_option, &size);
274 if (ret != EFI_SUCCESS) {
275 log_warning("Invalid load option for %ls\n", varname);
281 if (lo.attributes & LOAD_OPTION_ACTIVE) {
282 entry->title = u16_strdup(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)
319 static struct bootmenu_data *bootmenu_create(int delay)
322 unsigned short int i = 0;
323 struct bootmenu_data *menu;
324 struct bootmenu_entry *iter = NULL;
325 struct bootmenu_entry *entry;
328 menu = malloc(sizeof(struct bootmenu_data));
336 default_str = env_get("bootmenu_default");
338 menu->active = (int)simple_strtol(default_str, NULL, 10);
340 ret = prepare_bootmenu_entry(menu, &iter, &i);
344 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
345 if (i < MAX_COUNT - 1) {
346 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
347 if (ret < 0 && ret != -ENOENT)
352 /* Add U-Boot console entry at the end */
353 if (i <= MAX_COUNT - 1) {
354 entry = malloc(sizeof(struct bootmenu_entry));
358 /* Add Quit entry if entering U-Boot console is disabled */
359 if (IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE))
360 entry->title = u16_strdup(u"U-Boot console");
362 entry->title = u16_strdup(u"Quit");
369 entry->command = strdup("");
370 if (!entry->command) {
376 sprintf(entry->key, "%d", i);
380 entry->type = BOOTMENU_TYPE_NONE;
394 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
395 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
402 bootmenu_destroy(menu);
406 static void menu_display_statusline(struct menu *m)
408 struct bootmenu_entry *entry;
409 struct bootmenu_data *menu;
411 if (menu_default_choice(m, (void *)&entry) < 0)
416 printf(ANSI_CURSOR_POSITION, 1, 1);
417 puts(ANSI_CLEAR_LINE);
418 printf(ANSI_CURSOR_POSITION, 2, 3);
419 puts("*** U-Boot Boot Menu ***");
420 puts(ANSI_CLEAR_LINE_TO_END);
421 printf(ANSI_CURSOR_POSITION, 3, 1);
422 puts(ANSI_CLEAR_LINE);
424 /* First 3 lines are bootmenu header + 2 empty lines between entries */
425 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
426 puts(ANSI_CLEAR_LINE);
427 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
428 puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
429 puts(ANSI_CLEAR_LINE_TO_END);
430 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
431 puts(ANSI_CLEAR_LINE);
434 static void handle_uefi_bootnext(void)
440 /* Initialize EFI drivers */
441 ret = efi_init_obj_list();
442 if (ret != EFI_SUCCESS) {
443 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
444 ret & ~EFI_ERROR_MASK);
449 /* If UEFI BootNext variable is set, boot the BootNext load option */
451 ret = efi_get_variable_int(u"BootNext",
452 &efi_global_variable_guid,
453 NULL, &size, &bootnext, NULL);
454 if (ret == EFI_SUCCESS)
455 /* BootNext does exist here, try to boot */
456 run_command("bootefi bootmgr", 0);
459 static enum bootmenu_ret bootmenu_show(int delay)
465 char *command = NULL;
467 struct bootmenu_entry *iter;
468 int ret = BOOTMENU_RET_SUCCESS;
469 struct bootmenu_data *bootmenu;
470 efi_status_t efi_ret = EFI_SUCCESS;
473 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
474 handle_uefi_bootnext();
476 /* If delay is 0 do not create menu, just run first entry */
478 option = bootmenu_getoption(0);
480 puts("bootmenu option 0 was not found\n");
481 return BOOTMENU_RET_FAIL;
483 sep = strchr(option, '=');
485 puts("bootmenu option 0 is invalid\n");
486 return BOOTMENU_RET_FAIL;
488 cmd_ret = run_command(sep + 1, 0);
489 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
492 bootmenu = bootmenu_create(delay);
494 return BOOTMENU_RET_FAIL;
496 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
497 bootmenu_print_entry, bootmenu_choice_entry,
500 bootmenu_destroy(bootmenu);
501 return BOOTMENU_RET_FAIL;
504 for (iter = bootmenu->first; iter; iter = iter->next) {
505 if (menu_item_add(menu, iter->key, iter) != 1)
509 /* Default menu entry is always first */
510 menu_default_set(menu, "0");
512 puts(ANSI_CURSOR_HIDE);
513 puts(ANSI_CLEAR_CONSOLE);
514 printf(ANSI_CURSOR_POSITION, 1, 1);
518 if (menu_get_choice(menu, &choice) == 1) {
520 title = u16_strdup(iter->title);
521 command = strdup(iter->command);
523 /* last entry is U-Boot console or Quit */
524 if (iter->num == iter->menu->count - 1) {
525 ret = BOOTMENU_RET_QUIT;
533 * If the selected entry is UEFI BOOT####, set the BootNext variable.
534 * Then uefi bootmgr is invoked by the preset command in iter->command.
536 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
537 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
539 * UEFI specification requires BootNext variable needs non-volatile
540 * attribute, but this BootNext is only used inside of U-Boot and
541 * removed by efi bootmgr once BootNext is processed.
542 * So this BootNext can be volatile.
544 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
545 EFI_VARIABLE_BOOTSERVICE_ACCESS |
546 EFI_VARIABLE_RUNTIME_ACCESS,
547 sizeof(u16), &iter->bootorder, false);
548 if (efi_ret != EFI_SUCCESS)
555 bootmenu_destroy(bootmenu);
558 puts(ANSI_CURSOR_SHOW);
559 puts(ANSI_CLEAR_CONSOLE);
560 printf(ANSI_CURSOR_POSITION, 1, 1);
563 if (title && command) {
564 debug("Starting entry '%ls'\n", title);
566 if (efi_ret == EFI_SUCCESS)
567 cmd_ret = run_command(command, 0);
571 #ifdef CONFIG_POSTBOOTMENU
572 run_command(CONFIG_POSTBOOTMENU, 0);
575 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
576 ret = BOOTMENU_RET_FAIL;
581 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
582 int menu_show(int bootdelay)
587 ret = bootmenu_show(bootdelay);
589 if (ret == BOOTMENU_RET_UPDATED)
592 if (!IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE)) {
593 if (ret == BOOTMENU_RET_QUIT) {
594 /* default boot process */
595 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
596 run_command("bootefi bootmgr", 0);
598 run_command("run bootcmd", 0);
605 return -1; /* -1 - abort boot and run monitor code */
609 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
611 char *delay_str = NULL;
614 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
615 delay = CONFIG_BOOTDELAY;
622 delay_str = env_get("bootmenu_delay");
625 delay = (int)simple_strtol(delay_str, NULL, 10);
627 bootmenu_show(delay);
632 bootmenu, 2, 1, do_bootmenu,
633 "ANSI terminal bootmenu",
635 " - show ANSI terminal bootmenu with autoboot delay"