:: commit 3ee1ee880bfa0057b4ef735fb83de98a3285fbbf

mintsuki <mintsuki@protonmail.com> — 2020-12-03 11:38

parents: fe022c49f5

menu: Fix bug where variable may be used uninitialised, make DEFAULT_ENTRY 1-based, add 'no' setting for TIMEOUT

diff --git a/CONFIG.md b/CONFIG.md
index 0f31d191..dfbb34a5 100644
--- a/CONFIG.md
+++ b/CONFIG.md
@@ -36,8 +36,8 @@ Some *local assignments* are shared between entries using any *protocol*, while
 Some keys take *URIs* as values; these are described in the next section.
 
 *Globally assignable* keys are:
-* `TIMEOUT` - Specifies the timeout in seconds before the first *entry* is automatically booted.
-* `DEFAULT_ENTRY` - 0-based entry index of the entry which will be automatically selected at startup. If unspecified, it is `0`.
+* `TIMEOUT` - Specifies the timeout in seconds before the first *entry* is automatically booted. If set to `no`, disable automatic boot. If set to `0`, boots default entry instantly (see `DEFAULT_ENTRY` key).
+* `DEFAULT_ENTRY` - 1-based entry index of the entry which will be automatically selected at startup. If unspecified, it is `1`.
 * `GRAPHICS` - If set to `yes`, do use graphical VESA framebuffer for the boot menu, else use text mode.
 * `MENU_RESOLUTION` - Specify screen resolution to be used by the Limine menu in the form `<width>x<height>`. This will *only* affect the menu, not any booted OS. If not specified, Limine will pick a resolution automatically. If the resolution is not available, Limine will pick another one automatically. Ignored if `GRAPHICS` is not `yes`.
 * `THEME_COLOURS` - Specifies the colour palette used by the terminal (AARRGGBB). It is a `;` separated array of 8 colours: black, red, green, brown, blue, magenta, cyan, and gray, respectively. Ignored if `GRAPHICS` is not `yes`.
diff --git a/limine-pxe.bin b/limine-pxe.bin
index d7f913e8..cb1762bf 100644
Binary files a/limine-pxe.bin and b/limine-pxe.bin differ
diff --git a/limine.bin b/limine.bin
index a1f48842..49bf68f3 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2.map b/stage2.map
index c72e4551..e51c1e78 100644
Binary files a/stage2.map and b/stage2.map differ
diff --git a/stage2/menu.c b/stage2/menu.c
index 2e945c4a..f511e53a 100644
--- a/stage2/menu.c
+++ b/stage2/menu.c
@@ -194,8 +194,8 @@ refresh:
 }
 
 static int print_tree(int level, int base_index, int selected_entry,
-                         struct menu_entry *current_entry,
-                         struct menu_entry **selected_menu_entry) {
+                      struct menu_entry *current_entry,
+                      struct menu_entry **selected_menu_entry) {
     int max_entries = 0;
     for (;;) {
         if (current_entry == NULL)
@@ -239,23 +239,40 @@ static int print_tree(int level, int base_index, int selected_entry,
 }
 
 char *menu(char **cmdline) {
+    if (menu_tree == NULL)
+        panic("Config contains no valid entries.");
+
     bool skip_timeout = false;
-    struct menu_entry *selected_menu_entry;
+    struct menu_entry *selected_menu_entry = NULL;
 
     int selected_entry = 0;
     char *default_entry = config_get_value(NULL, 0, "DEFAULT_ENTRY");
     if (default_entry != NULL) {
         selected_entry = strtoui(default_entry, NULL, 10);
+        if (selected_entry)
+            selected_entry--;
     }
 
     int timeout = 5;
     char *timeout_config = config_get_value(NULL, 0, "TIMEOUT");
     if (timeout_config != NULL) {
-        timeout = strtoui(timeout_config, NULL, 10);
+        if (!strcmp(timeout_config, "no"))
+            skip_timeout = true;
+        else
+            timeout = strtoui(timeout_config, NULL, 10);
     }
 
-    if (!timeout)
-        goto autoboot;
+    if (!timeout) {
+        // Use print tree to load up selected_menu_entry and determine if the
+        // default entry is valid.
+        print_tree(0, 0, selected_entry, menu_tree, &selected_menu_entry);
+        if (selected_menu_entry == NULL || selected_menu_entry->sub != NULL) {
+            print("Default entry is not valid or directory, booting to menu.\n");
+            skip_timeout = true;
+        } else {
+            goto autoboot;
+        }
+    }
 
     // If there is GRAPHICS config key and the value is "yes", enable graphics
     char *graphics = config_get_value(NULL, 0, "GRAPHICS");
@@ -321,9 +338,6 @@ char *menu(char **cmdline) {
 
     disable_cursor();
 
-    if (menu_tree == NULL)
-        panic("Config contains no entries.");
-
     term_double_buffer(true);
 
 refresh:
diff --git a/test/limine.cfg b/test/limine.cfg
index d1bf4aab..6581d109 100644
--- a/test/limine.cfg
+++ b/test/limine.cfg
@@ -1,4 +1,4 @@
-DEFAULT_ENTRY=1
+DEFAULT_ENTRY=2
 TIMEOUT=3
 GRAPHICS=yes
 MENU_RESOLUTION=1024x768
tab: 248 wrap: offon