fygue: fix FAT12 cluster handling + fix file seek() handling

This commit is contained in:
Yann MAGNIN 2025-04-11 12:46:45 +02:00
parent ec6f7cedc5
commit 817e654e74
No known key found for this signature in database
GPG key ID: D82629D933EADC59
3 changed files with 25 additions and 20 deletions

View file

@ -34,7 +34,6 @@ static int _fygue_fat16_cluster_get_next(
} }
/* _fygue_fat12_cluster_get_next(): FAT12 cluster handling */ /* _fygue_fat12_cluster_get_next(): FAT12 cluster handling */
// TODO: assume fixed sector size to 512
// TODO: better calculus // TODO: better calculus
static int _fygue_fat12_cluster_get_next( static int _fygue_fat12_cluster_get_next(
struct fygue_fat *fat, struct fygue_fat *fat,
@ -51,39 +50,38 @@ static int _fygue_fat12_cluster_get_next(
fat_index0 = *cluster + (*cluster / 2); fat_index0 = *cluster + (*cluster / 2);
fat_index1 = fat_index0 + 1; fat_index1 = fat_index0 + 1;
fat_sector0 = fat->FAT0SectorID + (fat_index0 / fat->SectorSize); fat_sector0 = fat->FAT0SectorID + (fat_index0 / 512);
fat_sector1 = fat->FAT0SectorID + (fat_index1 / fat->SectorSize); fat_sector1 = fat->FAT0SectorID + (fat_index1 / 512);
if (fat_sector0 == fat_sector1) { if (fat_sector0 == fat_sector1) {
if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0) if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0)
return -1; return -1;
fat_data1 = fat_data0; 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; fat_offset1 = fat_offset0 + 1;
} else { } else {
if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0) if (fygue_fat_sector_get_addr(fat, &fat_data0, fat_sector0) != 0)
return -1; return -1;
if (fygue_fat_sector_get_addr(fat, &fat_data1, fat_sector1) != 0) if (fygue_fat_sector_get_addr(fat, &fat_data1, fat_sector1) != 0)
return -1; return -1;
fat_offset0 = fat_index0 - (fat_sector0 * fat->SectorSize); fat_offset0 = fat_index0 - ((fat_index0 / 512) * 512);
fat_offset1 = fat_index1 - (fat_sector1 * fat->SectorSize); fat_offset1 = fat_index1 - ((fat_index1 / 512) * 512);
} }
if (*cluster & 1) { if (*cluster & 1) {
*cluster = ( *cluster = (
((((uint8_t*)fat_data0)[fat_offset0] & 0xf0) >> 4) | (((((uint8_t*)fat_data0)[fat_offset0] & 0xf0) >> 4) << 0) |
((((uint8_t*)fat_data1)[fat_offset1] & 0xff) << 4) (((((uint8_t*)fat_data1)[fat_offset1] & 0xff) >> 0) << 4)
); );
} else { } else {
*cluster = ( *cluster = (
((((uint8_t*)fat_data0)[fat_offset0] & 0xff) >> 0) | (((((uint8_t*)fat_data0)[fat_offset0] & 0xff) >> 0) << 0) |
((((uint8_t*)fat_data1)[fat_offset1] & 0xf0) << 8) (((((uint8_t*)fat_data1)[fat_offset1] & 0x0f) >> 0) << 8)
); );
} }
if (*cluster == 0x0fff) if (*cluster == 0x0fff)
*cluster = 0xffff; *cluster = 0xffff;
return -1; return 0;
} }
//--- //---

View file

@ -2,6 +2,10 @@
#include <string.h> #include <string.h>
#include "fat.h" #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 */ /* fygue_fat_file_read(): read primitive */
int fygue_fat_file_read( int fygue_fat_file_read(
struct fygue_fat *fat, struct fygue_fat *fat,
@ -30,6 +34,9 @@ int fygue_fat_file_read(
chunk_data_available -= chunk_data_offset; chunk_data_available -= chunk_data_offset;
if (read + chunk_data_available > size) 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; chunk_data_offset += size - read;
memcpy(buffer, (void*)chunk_data_addr, size - read); memcpy(buffer, (void*)chunk_data_addr, size - read);
read += size - read; read += size - read;

View file

@ -7,20 +7,20 @@ int fygue_fat_file_seek(
struct fygue_fat_file *file, struct fygue_fat_file *file,
off_t offset off_t offset
) { ) {
off_t offset_test; off_t offset_current;
if (fat == NULL || file == NULL) { if (fat == NULL || file == NULL)
errno = EIO;
return -1; return -1;
} offset_current = 0;
offset_test = 0;
file->chunk_rd_index = 0; file->chunk_rd_index = 0;
file->chunk_rd_offset = 0; file->chunk_rd_offset = 0;
for (int i = 0; i < file->chunk_count; i++) for (int i = 0; i < file->chunk_count; i++)
{ {
offset_test += file->chunk[i].size; offset_current += file->chunk[i].size;
if (offset_test > offset) { if (offset_current > offset) {
file->chunk_rd_offset = offset - offset_test; file->chunk_rd_offset = (
file->chunk[i].size - (offset_current - offset)
);
file->cursor = offset; file->cursor = offset;
return 0; return 0;
} }