Merge tag 'efi-2020-07-rc3' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / cmd / bootmenu.c
index 870db7c1dea39084d38cb5824350f5bb1a3aa5ed..6af0617e653e830bd9b8fd441e29d899f8966725 100644 (file)
@@ -1,12 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
- * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
+ * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
  */
 
 #include <common.h>
 #include <command.h>
 #include <ansi.h>
+#include <env.h>
 #include <menu.h>
 #include <watchdog.h>
 #include <malloc.h>
@@ -254,6 +254,7 @@ static struct bootmenu_data *bootmenu_create(int delay)
 
        int len;
        char *sep;
+       char *default_str;
        struct bootmenu_entry *entry;
 
        menu = malloc(sizeof(struct bootmenu_data));
@@ -264,6 +265,10 @@ static struct bootmenu_data *bootmenu_create(int delay)
        menu->active = 0;
        menu->first = NULL;
 
+       default_str = env_get("bootmenu_default");
+       if (default_str)
+               menu->active = (int)simple_strtol(default_str, NULL, 10);
+
        while ((option = bootmenu_getoption(i))) {
                sep = strchr(option, '=');
                if (!sep) {
@@ -347,6 +352,12 @@ static struct bootmenu_data *bootmenu_create(int delay)
        }
 
        menu->count = i;
+
+       if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
+               printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
+               menu->active=0;
+       }
+
        return menu;
 
 cleanup:
@@ -354,6 +365,34 @@ cleanup:
        return NULL;
 }
 
+static void menu_display_statusline(struct menu *m)
+{
+       struct bootmenu_entry *entry;
+       struct bootmenu_data *menu;
+
+       if (menu_default_choice(m, (void *)&entry) < 0)
+               return;
+
+       menu = entry->menu;
+
+       printf(ANSI_CURSOR_POSITION, 1, 1);
+       puts(ANSI_CLEAR_LINE);
+       printf(ANSI_CURSOR_POSITION, 2, 1);
+       puts("  *** U-Boot Boot Menu ***");
+       puts(ANSI_CLEAR_LINE_TO_END);
+       printf(ANSI_CURSOR_POSITION, 3, 1);
+       puts(ANSI_CLEAR_LINE);
+
+       /* First 3 lines are bootmenu header + 2 empty lines between entries */
+       printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
+       puts(ANSI_CLEAR_LINE);
+       printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
+       puts("  Press UP/DOWN to move, ENTER to select");
+       puts(ANSI_CLEAR_LINE_TO_END);
+       printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
+       puts(ANSI_CLEAR_LINE);
+}
+
 static void bootmenu_show(int delay)
 {
        int init = 0;
@@ -385,8 +424,9 @@ static void bootmenu_show(int delay)
        if (!bootmenu)
                return;
 
-       menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
-                          bootmenu_choice_entry, bootmenu);
+       menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
+                          bootmenu_print_entry, bootmenu_choice_entry,
+                          bootmenu);
        if (!menu) {
                bootmenu_destroy(bootmenu);
                return;
@@ -434,35 +474,7 @@ cleanup:
 #endif
 }
 
-void menu_display_statusline(struct menu *m)
-{
-       struct bootmenu_entry *entry;
-       struct bootmenu_data *menu;
-
-       if (menu_default_choice(m, (void *)&entry) < 0)
-               return;
-
-       menu = entry->menu;
-
-       printf(ANSI_CURSOR_POSITION, 1, 1);
-       puts(ANSI_CLEAR_LINE);
-       printf(ANSI_CURSOR_POSITION, 2, 1);
-       puts("  *** U-Boot Boot Menu ***");
-       puts(ANSI_CLEAR_LINE_TO_END);
-       printf(ANSI_CURSOR_POSITION, 3, 1);
-       puts(ANSI_CLEAR_LINE);
-
-       /* First 3 lines are bootmenu header + 2 empty lines between entries */
-       printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
-       puts(ANSI_CLEAR_LINE);
-       printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
-       puts("  Press UP/DOWN to move, ENTER to select");
-       puts(ANSI_CLEAR_LINE_TO_END);
-       printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
-       puts(ANSI_CLEAR_LINE);
-}
-
-#ifdef CONFIG_MENU_SHOW
+#ifdef CONFIG_AUTOBOOT_MENU_SHOW
 int menu_show(int bootdelay)
 {
        bootmenu_show(bootdelay);