:: commit d96bdf775be967aa9cac3e398b5697ed37a4b1d2

mintsuki <mintsuki@protonmail.com> — 2021-03-07 02:03

parents: 5d3f8b4eb0

fat32: Fix base calculation for cluster sizes of more than 1 sector

diff --git a/stage23/fs/fat32.s2.c b/stage23/fs/fat32.s2.c
index d17c8bf4..e27f5ee8 100644
--- a/stage23/fs/fat32.s2.c
+++ b/stage23/fs/fat32.s2.c
@@ -94,13 +94,9 @@ static int fat32_init_context(struct fat32_context* context, struct volume *part
 }
 
 static int fat32_read_cluster_from_map(struct fat32_context* context, uint32_t cluster, uint32_t* out) {
-    const uint32_t sector = cluster / (FAT32_SECTOR_SIZE / 4);
-    const uint32_t offset = cluster % (FAT32_SECTOR_SIZE / 4);
+    volume_read(context->part, out, context->fat_start_lba * FAT32_SECTOR_SIZE + cluster * sizeof(uint32_t), sizeof(uint32_t));
 
-    uint32_t clusters[FAT32_SECTOR_SIZE / sizeof(uint32_t)];
-    volume_read(context->part, &clusters[0], (context->fat_start_lba + sector) * FAT32_SECTOR_SIZE, sizeof(clusters));
-
-    *out = clusters[offset] & 0x0FFFFFFF;
+    *out &= 0x0fffffff;
     return 0;
 }
 
@@ -138,7 +134,7 @@ static bool read_cluster_chain(struct fat32_context *context,
         if (chunk > block_size - offset)
             chunk = block_size - offset;
 
-        uint64_t base = (context->data_start_lba + (cluster_chain[block] - 2)) * block_size;
+        uint64_t base = (context->data_start_lba + (cluster_chain[block] - 2) * context->sectors_per_cluster) * FAT32_SECTOR_SIZE;
         volume_read(context->part, buf + progress, base + offset, chunk);
 
         progress += chunk;
tab: 248 wrap: offon