:: commit d914e6e4e40ab12a52492f87f48bfea30eb32cf2

mintsuki <mintsuki@protonmail.com> — 2024-08-02 12:39

parents: e2e6aeab44

config: Bring back support for legacy config with a warning

diff --git a/common/entry.s3.c b/common/entry.s3.c
index e6c6bde9..5706e113 100644
--- a/common/entry.s3.c
+++ b/common/entry.s3.c
@@ -101,6 +101,14 @@ could_not_match:
 
                 bool old_cif = case_insensitive_fopen;
                 case_insensitive_fopen = true;
+                if ((f = fopen(volume_index[i], "/limine.conf")) != NULL
+                 || (f = fopen(volume_index[i], "/limine/limine.conf")) != NULL
+                 || (f = fopen(volume_index[i], "/boot/limine.conf")) != NULL
+                 || (f = fopen(volume_index[i], "/boot/limine/limine.conf")) != NULL
+                 || (f = fopen(volume_index[i], "/EFI/BOOT/limine.conf")) != NULL) {
+                    goto opened;
+                }
+
                 if ((f = fopen(volume_index[i], "/limine.cfg")) == NULL
                  && (f = fopen(volume_index[i], "/limine/limine.cfg")) == NULL
                  && (f = fopen(volume_index[i], "/boot/limine.cfg")) == NULL
@@ -109,6 +117,8 @@ could_not_match:
                     case_insensitive_fopen = old_cif;
                     continue;
                 }
+
+opened:
                 case_insensitive_fopen = old_cif;
 
                 fclose(f);
diff --git a/common/lib/config.c b/common/lib/config.c
index 66d5112e..65a636c8 100644
--- a/common/lib/config.c
+++ b/common/lib/config.c
@@ -26,11 +26,21 @@ no_unwind bool bad_config = false;
 
 static char *config_addr;
 
+bool config_format_old = false;
+
 int init_config_disk(struct volume *part) {
     struct file_handle *f;
 
     bool old_cif = case_insensitive_fopen;
     case_insensitive_fopen = true;
+    if ((f = fopen(part, "/limine.conf")) != NULL
+     || (f = fopen(part, "/limine/limine.conf")) != NULL
+     || (f = fopen(part, "/boot/limine.conf")) != NULL
+     || (f = fopen(part, "/boot/limine/limine.conf")) != NULL
+     || (f = fopen(part, "/EFI/BOOT/limine.conf")) != NULL) {
+        goto opened;
+    }
+
     if ((f = fopen(part, "/limine.cfg")) == NULL
      && (f = fopen(part, "/limine/limine.cfg")) == NULL
      && (f = fopen(part, "/boot/limine.cfg")) == NULL
@@ -39,6 +49,10 @@ int init_config_disk(struct volume *part) {
         case_insensitive_fopen = old_cif;
         return -1;
     }
+
+    config_format_old = true;
+
+opened:
     case_insensitive_fopen = old_cif;
 
     size_t config_size = f->size + 2;
@@ -62,9 +76,9 @@ static int is_child(char *buf, size_t limit,
     if (strlen(buf) < current_depth + 1)
         return NOT_CHILD;
     for (size_t j = 0; j < current_depth; j++)
-        if (buf[j] != '/')
+        if (buf[j] != (config_format_old ? ':' : '/'))
             return NOT_CHILD;
-    if (buf[current_depth] == '/')
+    if (buf[current_depth] == (config_format_old ? ':' : '/'))
         return INDIRECT_CHILD;
     return DIRECT_CHILD;
 }
@@ -352,7 +366,7 @@ overflow:
     size_t s;
     char *c = config_get_entry(&s, 0);
     if (c != NULL) {
-        while (*c != '/') {
+        while (*c != (config_format_old ? ':' : '/')) {
             c--;
         }
         if (c > config_addr) {
@@ -370,7 +384,7 @@ static bool config_get_entry_name(char *ret, size_t index, size_t limit) {
     char *p = config_addr;
 
     for (size_t i = 0; i <= index; i++) {
-        while (*p != '/') {
+        while (*p != (config_format_old ? ':' : '/')) {
             if (!*p)
                 return false;
             p++;
@@ -401,7 +415,7 @@ static char *config_get_entry(size_t *size, size_t index) {
     char *p = config_addr;
 
     for (size_t i = 0; i <= index; i++) {
-        while (*p != '/') {
+        while (*p != (config_format_old ? ':' : '/')) {
             if (!*p)
                 return NULL;
             p++;
@@ -418,7 +432,7 @@ static char *config_get_entry(size_t *size, size_t index) {
     ret = p;
 
 cont:
-    while (*p != '/' && *p)
+    while (*p != (config_format_old ? ':' : '/') && *p)
         p++;
 
     if (*p && *(p - 1) != '\n') {
@@ -469,14 +483,16 @@ char *config_get_value(const char *config, size_t index, const char *key) {
     size_t key_len = strlen(key);
 
     for (size_t i = 0; config[i]; i++) {
-        if (!strncasecmp(&config[i], key, key_len) && config[i + key_len] == ':') {
+        if (!(config_format_old ? strncmp : strncasecmp)(&config[i], key, key_len) && config[i + key_len] == (config_format_old ? '=' : ':')) {
             if (i && config[i - 1] != SEPARATOR)
                 continue;
             if (index--)
                 continue;
             i += key_len + 1;
-            while (config[i] == ' ') {
-                i++;
+            if (!config_format_old) {
+                while (config[i] == ' ') {
+                    i++;
+                }
             }
             size_t value_len;
             for (value_len = 0;
diff --git a/common/lib/config.h b/common/lib/config.h
index d323aa06..54a5cfc3 100644
--- a/common/lib/config.h
+++ b/common/lib/config.h
@@ -23,6 +23,7 @@ struct conf_tuple {
     char *value2;
 };
 
+extern bool config_format_old;
 extern struct menu_entry *menu_tree;
 
 int init_config_disk(struct volume *part);
diff --git a/common/lib/uri.c b/common/lib/uri.c
index 197e985d..9ffff653 100644
--- a/common/lib/uri.c
+++ b/common/lib/uri.c
@@ -4,6 +4,7 @@
 #include <lib/misc.h>
 #include <lib/part.h>
 #include <lib/libc.h>
+#include <lib/config.h>
 #include <fs/file.h>
 #include <mm/pmm.h>
 #include <lib/print.h>
@@ -24,26 +25,26 @@ bool uri_resolve(char *uri, char **resource, char **root, char **path, char **ha
 
     // Get resource
     for (size_t i = 0; ; i++) {
-        if (strlen(uri + i) < 1)
+        if (strlen(uri + i) < (config_format_old ? 3 : 1))
             return false;
 
-        if (!memcmp(uri + i, "(", 1)) {
+        if (!memcmp(uri + i, (config_format_old ? "://" : "("), (config_format_old ? 3 : 1))) {
             *resource = uri;
             uri[i] = 0;
-            uri += i + 1;
+            uri += i + (config_format_old ? 3 : 1);
             break;
         }
     }
 
     // Get root
     for (size_t i = 0; ; i++) {
-        if (strlen(uri + i) < 3)
+        if (strlen(uri + i) < (config_format_old ? 1 : 3))
             return false;
 
-        if (!memcmp(uri + i, "):/", 3)) {
+        if (!memcmp(uri + i, (config_format_old ? "/" : "):/"), (config_format_old ? 1 : 3))) {
             *root = uri;
             uri[i] = 0;
-            uri += i + 3;
+            uri += i + (config_format_old ? 1 : 3);
             break;
         }
     }
diff --git a/common/menu.c b/common/menu.c
index 5b4bf775..f5251f3d 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -128,18 +128,18 @@ static int validate_line(const char *buffer) {
     char keybuf[64];
     size_t i;
     for (i = 0; buffer[i] && i < 64; i++) {
-        if (buffer[i] == ':') goto found_equals;
+        if (buffer[i] == (config_format_old ? '=' : ':')) goto found_equals;
         keybuf[i] = buffer[i];
     }
 fail:
     if (i < 64) keybuf[i] = 0;
-    if (keybuf[0] == '\n' || (!keybuf[0] && buffer[0] != ':')) return TOK_KEY; // blank line is valid
+    if (keybuf[0] == '\n' || (!keybuf[0] && buffer[0] != (config_format_old ? '=' : ':'))) return TOK_KEY; // blank line is valid
     invalid_syntax = true;
     return TOK_BADKEY;
 found_equals:
     if (i < 64) keybuf[i] = 0;
     for (i = 0; VALID_KEYS[i]; i++) {
-        if (!strcasecmp(keybuf, VALID_KEYS[i])) {
+        if (!(config_format_old ? strcmp : strcasecmp)(keybuf, VALID_KEYS[i])) {
             return TOK_KEY;
         }
     }
@@ -296,7 +296,7 @@ refresh:
         }
 
         // switch to token type 1 if equals sign
-        if (token_type == TOK_KEY && buffer[i] == ':') token_type = TOK_EQUALS;
+        if (token_type == TOK_KEY && buffer[i] == (config_format_old ? '=' : ':')) token_type = TOK_EQUALS;
 
         if (buffer[i] != 0 && line_offset % line_size == line_size - 1) {
             if (current_line <  window_offset + window_size
@@ -869,6 +869,18 @@ noreturn void _menu(bool first_run) {
 
     size_t tree_offset = 0;
 
+    if (config_format_old) {
+        print("The format of the config file has changed in Limine version 8.x.\n");
+        print("\n");
+        print("If the config file is named limine.cfg, Limine will still support the old\n");
+        print("configuration format for the time being, but this warning and associated 20\n");
+        print("second timeout will persist.\n");
+        print("\n");
+        print("To get rid of this warning, please update the config to the new format and\n");
+        print("rename it to limine.conf instead.\n");
+        pit_sleep_and_quit_on_keypress(20);
+    }
+
 refresh:
     if (selected_entry >= tree_offset + terms[0]->rows - 8) {
         tree_offset = selected_entry - (terms[0]->rows - 9);
diff --git a/common/protos/limine.c b/common/protos/limine.c
index 3ae1692c..dec7f0cd 100644
--- a/common/protos/limine.c
+++ b/common/protos/limine.c
@@ -1079,12 +1079,14 @@ FEAT_START
             }
             strcpy(module_path_abs_p, k_resource);
             module_path_abs_p += strlen(k_resource);
-            strcpy(module_path_abs_p, "(");
-            module_path_abs_p += 1;
+            strcpy(module_path_abs_p, (config_format_old ? "://" : "("));
+            module_path_abs_p += (config_format_old ? 3 : 1);
             strcpy(module_path_abs_p, k_root);
             module_path_abs_p += strlen(k_root);
-            strcpy(module_path_abs_p, "):");
-            module_path_abs_p += 2;
+            if (!config_format_old) {
+                strcpy(module_path_abs_p, "):");
+                module_path_abs_p += 2;
+            }
             get_absolute_path(module_path_abs_p, module_path, k_path);
 
             module_path = module_path_abs;
diff --git a/test/limine.c b/test/limine.c
index fb4facce..a0e908e8 100644
--- a/test/limine.c
+++ b/test/limine.c
@@ -69,7 +69,7 @@ struct limine_internal_module internal_module2 = {
 };
 
 struct limine_internal_module internal_module3 = {
-    .path = "./limine.cfg",
+    .path = "./limine.conf",
     .cmdline = "Third internal module"
 };
 
diff --git a/test/limine.cfg b/test/limine.conf
similarity index 100%
rename from test/limine.cfg
rename to test/limine.conf
tab: 248 wrap: offon