bootboot: Fix spec compliance as noted in PR #124 comments
diff --git a/stage23/protos/bootboot/initrd.c b/stage23/protos/bootboot/initrd.c
index 39657f3e..e92f1ac7 100644
--- a/stage23/protos/bootboot/initrd.c
+++ b/stage23/protos/bootboot/initrd.c
@@ -9,26 +9,8 @@ INITRD_HANDLER(jamesm);
INITRD_HANDLER(ustar);
INITRD_HANDLER(cpio);
-#define DETECT_FAILED panic("bootboot: cannot read file `%s`: cannot detect initrd type (only ustar, cpio and jamesm is supported).", path)
-
INITRD_HANDLER(auto) {
- if (file.size < 4) {
- DETECT_FAILED;
- }
-
- if (!memcmp(file.data, "\177ELF", 4)) {
- if (strcmp("sys/core", path) == 0) {
- printv("bootboot: using ELF as initrd to open sys/core\n");
- return file;
- }
- return (struct initrd_file){0};
- }
-
- if (file.size < 5) {
- DETECT_FAILED;
- }
-
- if (file.data[4] == 0xBF) {
+ if (file.size >= 5 && file.data[4] == 0xbf) {
struct initrd_file jamesm_attempt = initrd_open_jamesm(file, path);
if (jamesm_attempt.data) {
printv("bootboot: jamesm matched when reading file `%s`\n", path);
@@ -37,7 +19,7 @@ INITRD_HANDLER(auto) {
panic("bootboot: cannot read file `%s`: no such file or directory", path);
}
- if (!memcmp("07070", file.data, 5)) {
+ if (file.size >= 5 && memcmp("07070", file.data, 5) == 0) {
struct initrd_file cpio_attempt = initrd_open_cpio(file, path);
if (cpio_attempt.data) {
printv("bootboot: cpio matched when reading file `%s`\n", path);
@@ -46,11 +28,7 @@ INITRD_HANDLER(auto) {
panic("bootboot: cannot read file `%s`: no such file or directory", path);
}
- if (file.size < 262) {
- DETECT_FAILED;
- }
-
- if (!memcmp("ustar", file.data + 257, 5)) {
+ if (file.size >= 262 && memcmp("ustar", file.data + 257, 5) == 0) {
struct initrd_file ustar_attempt = initrd_open_ustar(file, path);
if (ustar_attempt.data) {
printv("bootboot: ustar matched when reading file `%s`\n", path);
@@ -59,5 +37,17 @@ INITRD_HANDLER(auto) {
panic("bootboot: cannot read file `%s`: no such file or directory", path);
}
- DETECT_FAILED;
+ if (strcmp("sys/core", path) == 0) {
+ for (size_t i = 0; i < file.size; i += 4) {
+ if (memcmp(file.data + i, "\177ELF", 4) == 0) {
+ printv("bootboot: using ELF as initrd to open sys/core\n");
+ return (struct initrd_file){
+ .size = file.size - i,
+ .data = file.data + i
+ };
+ }
+ }
+ }
+
+ panic("bootboot: cannot read file `%s`: cannot detect initrd type (only ustar, cpio and jamesm is supported).", path);
}
