:: commit 636164a8ae75c64803362690ec82680158903de3

mintsuki <mintsuki@protonmail.com> — 2021-11-10 12:48

parents: 8b81412e14

bootboot: Further address some of the issues raise in #124

diff --git a/stage23/protos/bootboot.c b/stage23/protos/bootboot.c
index 87153337..17a206e1 100644
--- a/stage23/protos/bootboot.c
+++ b/stage23/protos/bootboot.c
@@ -103,7 +103,7 @@ void bootboot_load(char *config) {
     /// Kernel loading code ///
     uint8_t *kernel;
 
-    if (known_initrd_format(bootboot_initrd_file)) {
+    if (initrd_format(bootboot_initrd_file) != INITRD_FORMAT_UNKNOWN) {
         const char *corefile = config_get_value((char *)env, 0, "kernel");
         if (corefile == NULL) {
             corefile = "sys/core";
diff --git a/stage23/protos/bootboot/initrd.c b/stage23/protos/bootboot/initrd.c
index df547e6e..5e73d7ea 100644
--- a/stage23/protos/bootboot/initrd.c
+++ b/stage23/protos/bootboot/initrd.c
@@ -20,20 +20,20 @@ struct initrd_file bruteforce_kernel(struct initrd_file file) {
     return (struct initrd_file){0};
 }
 
-bool known_initrd_format(struct initrd_file file) {
+enum initrd_format initrd_format(struct initrd_file file) {
     if (file.size >= 5 && file.data[4] == 0xbf) {
-        return true;
+        return INITRD_FORMAT_JAMESM;
     }
 
     if (file.size >= 5 && memcmp("07070", file.data, 5) == 0) {
-        return true;
+        return INITRD_FORMAT_CPIO;
     }
 
     if (file.size >= 262 && memcmp("ustar", file.data + 257, 5) == 0) {
-        return true;
+        return INITRD_FORMAT_USTAR;
     }
 
-    return false;
+    return INITRD_FORMAT_UNKNOWN;
 }
 
 INITRD_HANDLER(jamesm);
@@ -41,17 +41,14 @@ INITRD_HANDLER(ustar);
 INITRD_HANDLER(cpio);
 
 INITRD_HANDLER(auto) {
-    if (file.size >= 5 && file.data[4] == 0xbf) {
-        return initrd_open_jamesm(file, path);
+    switch (initrd_format(file)) {
+        case INITRD_FORMAT_JAMESM:
+            return initrd_open_jamesm(file, path);
+        case INITRD_FORMAT_CPIO:
+            return initrd_open_cpio(file, path);
+        case INITRD_FORMAT_USTAR:
+            return initrd_open_ustar(file, path);
+        default:
+            return (struct initrd_file){0};
     }
-
-    if (file.size >= 5 && memcmp("07070", file.data, 5) == 0) {
-        return initrd_open_cpio(file, path);
-    }
-
-    if (file.size >= 262 && memcmp("ustar", file.data + 257, 5) == 0) {
-        return initrd_open_ustar(file, path);
-    }
-
-    return (struct initrd_file){0};
 }
diff --git a/stage23/protos/bootboot/initrd.h b/stage23/protos/bootboot/initrd.h
index 1c3d7f1d..20347e84 100644
--- a/stage23/protos/bootboot/initrd.h
+++ b/stage23/protos/bootboot/initrd.h
@@ -9,8 +9,15 @@ struct initrd_file {
     uint8_t *data;
 };
 
+enum initrd_format {
+    INITRD_FORMAT_UNKNOWN,
+    INITRD_FORMAT_JAMESM,
+    INITRD_FORMAT_CPIO,
+    INITRD_FORMAT_USTAR
+};
+
 struct initrd_file bruteforce_kernel(struct initrd_file file);
-bool known_initrd_format(struct initrd_file file);
+enum initrd_format initrd_format(struct initrd_file file);
 
 #define INITRD_HANDLER(name) struct initrd_file initrd_open_##name(struct initrd_file file, const char *path)
 
tab: 248 wrap: offon