Dave Murphy 2011-06-28 20:35:00 +00:00
parent a7307e0c0e
commit 1bb3bab6e4
4 changed files with 105 additions and 121 deletions

View File

@ -224,7 +224,7 @@ static bool _FAT_directory_entryGetAlias (const u8* entryData, char* destName) {
caseInfo = entryData[DIR_ENTRY_caseInfo] & CASE_LOWER_BASE; caseInfo = entryData[DIR_ENTRY_caseInfo] & CASE_LOWER_BASE;
for (i = 0; (i < 8) && (entryData[DIR_ENTRY_name + i] != ' '); i++) { for (i = 0; (i < 8) && (entryData[DIR_ENTRY_name + i] != ' '); i++) {
c = entryData[DIR_ENTRY_name + i]; c = entryData[DIR_ENTRY_name + i];
destName[i] = (caseInfo ? tolower(c) : c); destName[i] = (caseInfo ? tolower((unsigned char)c) : c);
} }
// Copy the extension from the dirEntry to the string // Copy the extension from the dirEntry to the string
if (entryData[DIR_ENTRY_extension] != ' ') { if (entryData[DIR_ENTRY_extension] != ' ') {
@ -232,7 +232,7 @@ static bool _FAT_directory_entryGetAlias (const u8* entryData, char* destName) {
caseInfo = entryData[DIR_ENTRY_caseInfo] & CASE_LOWER_EXT; caseInfo = entryData[DIR_ENTRY_caseInfo] & CASE_LOWER_EXT;
for ( j = 0; (j < 3) && (entryData[DIR_ENTRY_extension + j] != ' '); j++) { for ( j = 0; (j < 3) && (entryData[DIR_ENTRY_extension + j] != ' '); j++) {
c = entryData[DIR_ENTRY_extension + j]; c = entryData[DIR_ENTRY_extension + j];
destName[i++] = (caseInfo ? tolower(c) : c); destName[i++] = (caseInfo ? tolower((unsigned char)c) : c);
} }
} }
destName[i] = '\0'; destName[i] = '\0';

View File

@ -103,20 +103,14 @@ static const char FAT_SIG[3] = {'F', 'A', 'T'};
static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'}; static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'};
static const char FS_INFO_SIG2[4] = {'r', 'r', 'A', 'a'}; static const char FS_INFO_SIG2[4] = {'r', 'r', 'A', 'a'};
sec_t FindFirstValidPartition(const DISC_INTERFACE* disc) sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuffer)
{ {
uint8_t part_table[16*4]; uint8_t part_table[16*4];
uint8_t *ptr; uint8_t *ptr;
int i; int i;
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(MAX_SECTOR_SIZE);
if(!sectorBuffer) {
return 0;
}
// Read first sector of disc // Read first sector of disc
if (!_FAT_disc_readSectors (disc, 0, 1, sectorBuffer)) { if (!_FAT_disc_readSectors (disc, 0, 1, sectorBuffer)) {
_FAT_mem_free(sectorBuffer);
return 0; return 0;
} }
@ -128,7 +122,6 @@ sec_t FindFirstValidPartition(const DISC_INTERFACE* disc)
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {
_FAT_mem_free(sectorBuffer);
return part_lba; return part_lba;
} }
@ -141,61 +134,53 @@ sec_t FindFirstValidPartition(const DISC_INTERFACE* disc)
for(n=0;n<8;n++) // max 8 logic partitions for(n=0;n<8;n++) // max 8 logic partitions
{ {
if(!_FAT_disc_readSectors (disc, part_lba+next_lba2, 1, sectorBuffer)) { if(!_FAT_disc_readSectors (disc, part_lba+next_lba2, 1, sectorBuffer)) return 0;
_FAT_mem_free(sectorBuffer);
return 0;
}
part_lba2 = part_lba + next_lba2 + u8array_to_u32(sectorBuffer, 0x1C6) ; part_lba2 = part_lba + next_lba2 + u8array_to_u32(sectorBuffer, 0x1C6) ;
next_lba2 = u8array_to_u32(sectorBuffer, 0x1D6); next_lba2 = u8array_to_u32(sectorBuffer, 0x1D6);
if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) { if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) return 0;
_FAT_mem_free(sectorBuffer);
return 0;
}
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
_FAT_mem_free(sectorBuffer); {
return part_lba2; return part_lba2;
} }
if(next_lba2==0) break; if(next_lba2==0) break;
} }
} else { } else {
if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) { if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) return 0;
_FAT_mem_free(sectorBuffer);
return 0;
}
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {
_FAT_mem_free(sectorBuffer);
return part_lba; return part_lba;
} }
} }
} }
_FAT_mem_free(sectorBuffer);
return 0; return 0;
} }
sec_t FindFirstValidPartition(const DISC_INTERFACE* disc)
{
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(MAX_SECTOR_SIZE);
if (!sectorBuffer) return 0;
sec_t ret = FindFirstValidPartition_buf(disc, sectorBuffer);
_FAT_mem_free(sectorBuffer);
return ret;
}
PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cacheSize, uint32_t sectorsPerPage, sec_t startSector) {
PARTITION* _FAT_partition_constructor_buf (const DISC_INTERFACE* disc, uint32_t cacheSize, uint32_t sectorsPerPage, sec_t startSector, uint8_t *sectorBuffer)
{
PARTITION* partition; PARTITION* partition;
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(MAX_SECTOR_SIZE);
if(!sectorBuffer) {
return NULL;
}
// Read first sector of disc // Read first sector of disc
if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) { if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) {
_FAT_mem_free(sectorBuffer);
return NULL; return NULL;
} }
// Make sure it is a valid MBR or boot sector // Make sure it is a valid MBR or boot sector
if ( (sectorBuffer[BPB_bootSig_55] != 0x55) || (sectorBuffer[BPB_bootSig_AA] != 0xAA)) { if ( (sectorBuffer[BPB_bootSig_55] != 0x55) || (sectorBuffer[BPB_bootSig_AA] != 0xAA)) {
_FAT_mem_free(sectorBuffer);
return NULL; return NULL;
} }
@ -208,23 +193,21 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
// Check for FAT32 // Check for FAT32
startSector = 0; startSector = 0;
} else { } else {
startSector = FindFirstValidPartition(disc); startSector = FindFirstValidPartition_buf(disc, sectorBuffer);
if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) { if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) {
_FAT_mem_free(sectorBuffer);
return NULL; return NULL;
} }
} }
// Now verify that this is indeed a FAT partition // Now verify that this is indeed a FAT partition
if (memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) && if (memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) &&
memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
_FAT_mem_free(sectorBuffer); {
return NULL; return NULL;
} }
partition = (PARTITION*) _FAT_mem_allocate (sizeof(PARTITION)); partition = (PARTITION*) _FAT_mem_allocate (sizeof(PARTITION));
if (partition == NULL) { if (partition == NULL) {
_FAT_mem_free(sectorBuffer);
return NULL; return NULL;
} }
@ -254,7 +237,6 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
partition->bytesPerSector = u8array_to_u16(sectorBuffer, BPB_bytesPerSector); partition->bytesPerSector = u8array_to_u16(sectorBuffer, BPB_bytesPerSector);
if(partition->bytesPerSector < MIN_SECTOR_SIZE || partition->bytesPerSector > MAX_SECTOR_SIZE) { if(partition->bytesPerSector < MIN_SECTOR_SIZE || partition->bytesPerSector > MAX_SECTOR_SIZE) {
// Unsupported sector size // Unsupported sector size
_FAT_mem_free(sectorBuffer);
_FAT_mem_free(partition); _FAT_mem_free(partition);
return NULL; return NULL;
} }
@ -314,11 +296,20 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
_FAT_partition_readFSinfo(partition); _FAT_partition_readFSinfo(partition);
_FAT_mem_free(sectorBuffer);
return partition; return partition;
} }
PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cacheSize, uint32_t sectorsPerPage, sec_t startSector)
{
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(MAX_SECTOR_SIZE);
if (!sectorBuffer) return NULL;
PARTITION *ret = _FAT_partition_constructor_buf(disc, cacheSize,
sectorsPerPage, startSector, sectorBuffer);
_FAT_mem_free(sectorBuffer);
return ret;
}
void _FAT_partition_destructor (PARTITION* partition) { void _FAT_partition_destructor (PARTITION* partition) {
FILE_STRUCT* nextFile; FILE_STRUCT* nextFile;
@ -362,10 +353,8 @@ void _FAT_partition_createFSinfo(PARTITION * partition)
if(partition->readOnly || partition->filesysType != FS_FAT32) if(partition->readOnly || partition->filesysType != FS_FAT32)
return; return;
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector);
if(!sectorBuffer) { if (!sectorBuffer) return;
return;
}
memset(sectorBuffer, 0, partition->bytesPerSector); memset(sectorBuffer, 0, partition->bytesPerSector);
int i; int i;
@ -392,10 +381,8 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
if(partition->filesysType != FS_FAT32) if(partition->filesysType != FS_FAT32)
return; return;
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector);
if(!sectorBuffer) { if (!sectorBuffer) return;
return;
}
memset(sectorBuffer, 0, partition->bytesPerSector); memset(sectorBuffer, 0, partition->bytesPerSector);
// Read first sector of disc // Read first sector of disc
if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) { if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) {
@ -405,15 +392,14 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) != 0 || if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) != 0 ||
memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4) != 0 || memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4) != 0 ||
u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster) == 0) { u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster) == 0)
{
//sector does not yet exist, create one! //sector does not yet exist, create one!
_FAT_partition_createFSinfo(partition); _FAT_partition_createFSinfo(partition);
_FAT_mem_free(sectorBuffer); } else {
return;
}
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster); partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster); partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster);
}
_FAT_mem_free(sectorBuffer); _FAT_mem_free(sectorBuffer);
} }
@ -422,10 +408,8 @@ void _FAT_partition_writeFSinfo(PARTITION * partition)
if(partition->filesysType != FS_FAT32) if(partition->filesysType != FS_FAT32)
return; return;
uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector);
if(!sectorBuffer) { if (!sectorBuffer) return;
return;
}
memset(sectorBuffer, 0, partition->bytesPerSector); memset(sectorBuffer, 0, partition->bytesPerSector);
// Read first sector of disc // Read first sector of disc
if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) { if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) {
@ -441,7 +425,7 @@ void _FAT_partition_writeFSinfo(PARTITION * partition)
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster); u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
// Read first sector of disc // Write first sector of disc
_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer); _FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
_FAT_mem_free(sectorBuffer); _FAT_mem_free(sectorBuffer);
} }