From 817e654e745ebf34399db7264c0816c72192b1bf Mon Sep 17 00:00:00 2001 From: Yann MAGNIN Date: Fri, 11 Apr 2025 12:46:45 +0200 Subject: [PATCH] fygue: fix FAT12 cluster handling + fix file seek() handling --- src/fs/fygue/fat/cluster.c | 22 ++++++++++------------ src/fs/fygue/fat/fat_file_read.c | 7 +++++++ src/fs/fygue/fat/fat_file_seek.c | 16 ++++++++-------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/fs/fygue/fat/cluster.c b/src/fs/fygue/fat/cluster.c index 71f11a0..409177b 100644 --- a/src/fs/fygue/fat/cluster.c +++ b/src/fs/fygue/fat/cluster.c @@ -34,7 +34,6 @@ static int _fygue_fat16_cluster_get_next( } /* _fygue_fat12_cluster_get_next(): FAT12 cluster handling */ -// TODO: assume fixed sector size to 512 // TODO: better calculus static int _fygue_fat12_cluster_get_next( struct fygue_fat *fat, @@ -51,39 +50,38 @@ static int _fygue_fat12_cluster_get_next( fat_index0 = *cluster + (*cluster / 2); fat_index1 = fat_index0 + 1; - fat_sector0 = fat->FAT0SectorID + (fat_index0 / fat->SectorSize); - fat_sector1 = fat->FAT0SectorID + (fat_index1 / fat->SectorSize); + fat_sector0 = fat->FAT0SectorID + (fat_index0 / 512); + fat_sector1 = fat->FAT0SectorID + (fat_index1 / 512); if (fat_sector0 == fat_sector1) { if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0) return -1; fat_data1 = fat_data0; - fat_offset0 = fat_index0 - (fat_sector0 * fat->SectorSize); + fat_offset0 = fat_index0 - ((fat_index0 / 512) * 512); fat_offset1 = fat_offset0 + 1; } else { if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0) return -1; if (fygue_fat_sector_get_addr(fat, &fat_data1, fat_sector1) != 0) return -1; - fat_offset0 = fat_index0 - (fat_sector0 * fat->SectorSize); - fat_offset1 = fat_index1 - (fat_sector1 * fat->SectorSize); + fat_offset0 = fat_index0 - ((fat_index0 / 512) * 512); + fat_offset1 = fat_index1 - ((fat_index1 / 512) * 512); } if (*cluster & 1) { *cluster = ( - ((((uint8_t*)fat_data0)[fat_offset0] & 0xf0) >> 4) | - ((((uint8_t*)fat_data1)[fat_offset1] & 0xff) << 4) + (((((uint8_t*)fat_data0)[fat_offset0] & 0xf0) >> 4) << 0) | + (((((uint8_t*)fat_data1)[fat_offset1] & 0xff) >> 0) << 4) ); } else { *cluster = ( - ((((uint8_t*)fat_data0)[fat_offset0] & 0xff) >> 0) | - ((((uint8_t*)fat_data1)[fat_offset1] & 0xf0) << 8) + (((((uint8_t*)fat_data0)[fat_offset0] & 0xff) >> 0) << 0) | + (((((uint8_t*)fat_data1)[fat_offset1] & 0x0f) >> 0) << 8) ); } - if (*cluster == 0x0fff) *cluster = 0xffff; - return -1; + return 0; } //--- diff --git a/src/fs/fygue/fat/fat_file_read.c b/src/fs/fygue/fat/fat_file_read.c index 034714f..07639b9 100644 --- a/src/fs/fygue/fat/fat_file_read.c +++ b/src/fs/fygue/fat/fat_file_read.c @@ -2,6 +2,10 @@ #include #include "fat.h" +uintptr_t debug_read_addr = 0; +size_t debug_read_size = 0; +int debug_read_off = 0; + /* fygue_fat_file_read(): read primitive */ int fygue_fat_file_read( struct fygue_fat *fat, @@ -30,6 +34,9 @@ int fygue_fat_file_read( chunk_data_available -= chunk_data_offset; if (read + chunk_data_available > size) { + debug_read_addr = chunk_data_addr; + debug_read_size = size - read; + debug_read_off = chunk_data_offset; chunk_data_offset += size - read; memcpy(buffer, (void*)chunk_data_addr, size - read); read += size - read; diff --git a/src/fs/fygue/fat/fat_file_seek.c b/src/fs/fygue/fat/fat_file_seek.c index c9b3c37..b8da951 100644 --- a/src/fs/fygue/fat/fat_file_seek.c +++ b/src/fs/fygue/fat/fat_file_seek.c @@ -7,20 +7,20 @@ int fygue_fat_file_seek( struct fygue_fat_file *file, off_t offset ) { - off_t offset_test; + off_t offset_current; - if (fat == NULL || file == NULL) { - errno = EIO; + if (fat == NULL || file == NULL) return -1; - } - offset_test = 0; + offset_current = 0; file->chunk_rd_index = 0; file->chunk_rd_offset = 0; for (int i = 0; i < file->chunk_count; i++) { - offset_test += file->chunk[i].size; - if (offset_test > offset) { - file->chunk_rd_offset = offset - offset_test; + offset_current += file->chunk[i].size; + if (offset_current > offset) { + file->chunk_rd_offset = ( + file->chunk[i].size - (offset_current - offset) + ); file->cursor = offset; return 0; }