:: commit 2e4087416524d8f6b1bbbb2e19198edc3c474bed

mintsuki <mintsuki@protonmail.com> — 2021-02-22 05:14

parents: 60a3fab256

iso9660: Bug fix and move to stage 3

diff --git a/limine-pxe.bin b/limine-pxe.bin
index d99b96dd..9c268085 100644
Binary files a/limine-pxe.bin and b/limine-pxe.bin differ
diff --git a/limine.bin b/limine.bin
index 11a45a41..a5bd7171 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/limine.sys b/limine.sys
index bdff02c2..53cac2c0 100644
Binary files a/limine.sys and b/limine.sys differ
diff --git a/stage2/fs/file.c b/stage2/fs/file.c
index 8fbc65c4..016532cd 100644
--- a/stage2/fs/file.c
+++ b/stage2/fs/file.c
@@ -24,7 +24,7 @@ bool fs_get_guid(struct guid *guid, struct volume *part) {
 int fopen(struct file_handle *ret, struct volume *part, const char *filename) {
     ret->is_memfile = false;
 
-    if (iso9660_check_signature(part)) {
+    if (stage3_loaded && iso9660_check_signature(part)) {
         struct iso9660_file_handle *fd = ext_mem_alloc(sizeof(struct iso9660_file_handle));
 
         int r = iso9660_open(fd, part, filename);
diff --git a/stage2/fs/iso9660.c b/stage2/fs/iso9660.c
index 5326a6d0..ae5b4b0a 100644
--- a/stage2/fs/iso9660.c
+++ b/stage2/fs/iso9660.c
@@ -68,11 +68,8 @@ struct iso9660_primary_volume {
 
 
 // --- Implementation ---
-// Cached root
-static void *root = NULL;
-static uint32_t root_size = 0;
 
-static void iso9660_find_PVD(struct iso9660_volume_descriptor *desc, struct volume *vol) {
+stage3_text static void iso9660_find_PVD(struct iso9660_volume_descriptor *desc, struct volume *vol) {
     uint32_t lba = ISO9660_FIRST_VOLUME_DESCRIPTOR;
     while (true) {
         volume_read(vol, desc, lba * ISO9660_SECTOR_SIZE, ISO9660_SECTOR_SIZE);
@@ -89,16 +86,18 @@ static void iso9660_find_PVD(struct iso9660_volume_descriptor *desc, struct volu
     }
 }
 
-static void iso9660_cache_root(struct volume *vol) {
+stage3_text static void iso9660_cache_root(struct volume *vol,
+                               void **root,
+                               uint32_t *root_size) {
     struct iso9660_primary_volume pv;
     iso9660_find_PVD((struct iso9660_volume_descriptor *)&pv, vol);
 
-    root_size = pv.root.extent_size.little;
-    root = ext_mem_alloc(root_size);
-    volume_read(vol, root, pv.root.extent.little * ISO9660_SECTOR_SIZE, root_size);
+    *root_size = pv.root.extent_size.little;
+    *root = ext_mem_alloc(*root_size);
+    volume_read(vol, *root, pv.root.extent.little * ISO9660_SECTOR_SIZE, *root_size);
 }
 
-static int iso9660_strcmp(const char *a, const char *b, size_t size) {
+stage3_text static int iso9660_strcmp(const char *a, const char *b, size_t size) {
     while (size--) {
         char ca = *a++;
         char cb = *b++;
@@ -109,7 +108,7 @@ static int iso9660_strcmp(const char *a, const char *b, size_t size) {
     return 0;
 }
 
-static struct iso9660_directory_entry *iso9660_find(void *buffer, uint32_t size, const char *filename) {
+stage3_text static struct iso9660_directory_entry *iso9660_find(void *buffer, uint32_t size, const char *filename) {
     // The file can be either FILENAME or FILENAME;1
     uint32_t len = strlen(filename);
     char finalfile[len + 2];
@@ -138,7 +137,7 @@ static struct iso9660_directory_entry *iso9660_find(void *buffer, uint32_t size,
 
 
 // --- Public functions ---
-int iso9660_check_signature(struct volume *vol) {
+stage3_text int iso9660_check_signature(struct volume *vol) {
     char buf[6];
     const uint64_t signature = ISO9660_FIRST_VOLUME_DESCRIPTOR * ISO9660_SECTOR_SIZE + 1;
     volume_read(vol, buf, signature, 5);
@@ -146,20 +145,16 @@ int iso9660_check_signature(struct volume *vol) {
     return !strcmp(buf, "CD001");
 }
 
-int iso9660_open(struct iso9660_file_handle *ret, struct volume *vol, const char *path) {
-    // Is the root directory cached?
-    if (!root)
-        iso9660_cache_root(vol);
+stage3_text int iso9660_open(struct iso9660_file_handle *ret, struct volume *vol, const char *path) {
+    iso9660_cache_root(vol, &ret->context.root, &ret->context.root_size);
 
     ret->context.vol = *vol;
-    ret->context.root = root;
-    ret->context.root_size = root_size;
 
     while (*path == '/')
         ++path;
 
-    struct iso9660_directory_entry *current = root;
-    uint32_t current_size = root_size;
+    struct iso9660_directory_entry *current = ret->context.root;
+    uint32_t current_size = ret->context.root_size;
 
     uint32_t next_sector = 0;
     uint32_t next_size = 0;
@@ -191,7 +186,7 @@ int iso9660_open(struct iso9660_file_handle *ret, struct volume *vol, const char
     return 0;
 }
 
-int iso9660_read(struct iso9660_file_handle *file, void *buf, uint64_t loc, uint64_t count) {
+stage3_text int iso9660_read(struct iso9660_file_handle *file, void *buf, uint64_t loc, uint64_t count) {
     volume_read(&file->context.vol, buf, file->LBA * ISO9660_SECTOR_SIZE + loc, count);
     return 0;
 }
diff --git a/stage2/lib/blib.c b/stage2/lib/blib.c
index 878c7f81..c66d21b9 100644
--- a/stage2/lib/blib.c
+++ b/stage2/lib/blib.c
@@ -12,7 +12,7 @@ int     boot_partition = -1;
 
 bool booted_from_pxe = false;
 bool booted_from_cd = false;
-bool stage3_already_loaded = false;
+bool stage3_loaded = false;
 
 bool parse_resolution(int *width, int *height, int *bpp, const char *buf) {
     int res[3] = {0};
diff --git a/stage2/lib/blib.h b/stage2/lib/blib.h
index 70333094..b6c9f152 100644
--- a/stage2/lib/blib.h
+++ b/stage2/lib/blib.h
@@ -10,7 +10,7 @@ extern int     boot_partition;
 
 extern bool booted_from_pxe;
 extern bool booted_from_cd;
-extern bool stage3_already_loaded;
+extern bool stage3_loaded;
 
 bool parse_resolution(int *width, int *height, int *bpp, const char *buf);
 
diff --git a/stage2/lib/config.c b/stage2/lib/config.c
index 455701d4..77eefd45 100644
--- a/stage2/lib/config.c
+++ b/stage2/lib/config.c
@@ -19,7 +19,7 @@ static char *config_addr;
 extern symbol stage3_addr;
 
 int init_config_disk(struct volume *part) {
-    if (!stage3_already_loaded) {
+    if (!stage3_loaded) {
         struct file_handle stage3;
 
         if (fopen(&stage3, part, "/limine.sys")
@@ -28,6 +28,8 @@ int init_config_disk(struct volume *part) {
         }
 
         fread(&stage3, stage3_addr, 0, stage3.size);
+
+        stage3_loaded = true;
     }
 
     struct file_handle f;
diff --git a/stage2/main.c b/stage2/main.c
index 79c14b9f..5a284dff 100644
--- a/stage2/main.c
+++ b/stage2/main.c
@@ -31,7 +31,7 @@ void entry(uint8_t _boot_drive, int boot_from) {
 
     booted_from_pxe = (boot_from == BOOT_FROM_PXE);
     booted_from_cd = (boot_from == BOOT_FROM_CD);
-    stage3_already_loaded = booted_from_cd; // CD loads both stages
+    stage3_loaded = booted_from_cd; // CD loads both stages
 
     mtrr_save();
 
tab: 248 wrap: offon