From 1bb3bab6e4aa9872a62ff8ec58ff749dfe86090b Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 28 Jun 2011 20:35:00 +0000 Subject: [PATCH] cleanup patch - http://sourceforge.net/tracker/?func=detail&aid=3331605&group_id=114505&atid=668553 --- source/directory.c | 4 +- source/fatdir.c | 18 +-- source/file_allocation_table.c | 8 +- source/partition.c | 196 +++++++++++++++------------------ 4 files changed, 105 insertions(+), 121 deletions(-) diff --git a/source/directory.c b/source/directory.c index 4ec6fce..c09debe 100644 --- a/source/directory.c +++ b/source/directory.c @@ -224,7 +224,7 @@ static bool _FAT_directory_entryGetAlias (const u8* entryData, char* destName) { caseInfo = entryData[DIR_ENTRY_caseInfo] & CASE_LOWER_BASE; for (i = 0; (i < 8) && (entryData[DIR_ENTRY_name + i] != ' '); 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 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; for ( j = 0; (j < 3) && (entryData[DIR_ENTRY_extension + j] != ' '); j++) { c = entryData[DIR_ENTRY_extension + j]; - destName[i++] = (caseInfo ? tolower(c) : c); + destName[i++] = (caseInfo ? tolower((unsigned char)c) : c); } } destName[i] = '\0'; diff --git a/source/fatdir.c b/source/fatdir.c index 52a6fb5..fe0e781 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -465,16 +465,16 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf) _FAT_lock(&partition->lock); - if(memcmp(&buf->f_flag, "SCAN", 4) == 0) - { - //Special command was given to sync the numberFreeCluster - _FAT_partition_createFSinfo(partition); - } + if(memcmp(&buf->f_flag, "SCAN", 4) == 0) + { + //Special command was given to sync the numberFreeCluster + _FAT_partition_createFSinfo(partition); + } - if(partition->filesysType == FS_FAT32) - freeClusterCount = partition->fat.numberFreeCluster; - else - freeClusterCount = _FAT_fat_freeClusterCount (partition); + if(partition->filesysType == FS_FAT32) + freeClusterCount = partition->fat.numberFreeCluster; + else + freeClusterCount = _FAT_fat_freeClusterCount (partition); // FAT clusters = POSIX blocks buf->f_bsize = partition->bytesPerCluster; // File system block size. diff --git a/source/file_allocation_table.c b/source/file_allocation_table.c index 9a3cc88..72f8aa7 100644 --- a/source/file_allocation_table.c +++ b/source/file_allocation_table.c @@ -247,7 +247,7 @@ uint32_t _FAT_fat_linkFreeCluster(PARTITION* partition, uint32_t cluster) { } partition->fat.firstFree = firstFree; if(partition->fat.numberFreeCluster) - partition->fat.numberFreeCluster--; + partition->fat.numberFreeCluster--; partition->fat.numberLastAllocCluster = firstFree; if ((cluster >= CLUSTER_FIRST) && (cluster <= lastCluster)) @@ -279,7 +279,7 @@ uint32_t _FAT_fat_linkFreeClusterCleared (PARTITION* partition, uint32_t cluster return CLUSTER_ERROR; } - emptySector = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); + emptySector = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); // Clear all the sectors within the cluster memset (emptySector, 0, partition->bytesPerSector); @@ -317,8 +317,8 @@ bool _FAT_fat_clearLinks (PARTITION* partition, uint32_t cluster) { // Erase the link _FAT_fat_writeFatEntry (partition, cluster, CLUSTER_FREE); - if(partition->fat.numberFreeCluster < (partition->numberOfSectors/partition->sectorsPerCluster)) - partition->fat.numberFreeCluster++; + if(partition->fat.numberFreeCluster < (partition->numberOfSectors/partition->sectorsPerCluster)) + partition->fat.numberFreeCluster++; // Move onto next cluster cluster = nextCluster; } diff --git a/source/partition.c b/source/partition.c index 0f5d12d..9d22041 100644 --- a/source/partition.c +++ b/source/partition.c @@ -91,10 +91,10 @@ enum BPB { // File system information block offsets enum FSIB { - FSIB_SIG1 = 0x00, - FSIB_SIG2 = 0x1e4, - FSIB_numberOfFreeCluster = 0x1e8, - FSIB_numberLastAllocCluster = 0x1ec, + FSIB_SIG1 = 0x00, + FSIB_SIG2 = 0x1e4, + FSIB_numberOfFreeCluster = 0x1e8, + FSIB_numberLastAllocCluster = 0x1ec, FSIB_bootSig_55 = 0x1FE, FSIB_bootSig_AA = 0x1FF }; @@ -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_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 *ptr; int i; - uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(MAX_SECTOR_SIZE); - if(!sectorBuffer) { - return 0; - } - // Read first sector of disc if (!_FAT_disc_readSectors (disc, 0, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); return 0; } @@ -128,7 +122,6 @@ sec_t FindFirstValidPartition(const DISC_INTERFACE* disc) if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { - _FAT_mem_free(sectorBuffer); return part_lba; } @@ -141,61 +134,53 @@ sec_t FindFirstValidPartition(const DISC_INTERFACE* disc) for(n=0;n<8;n++) // max 8 logic partitions { - if(!_FAT_disc_readSectors (disc, part_lba+next_lba2, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); - return 0; - } + if(!_FAT_disc_readSectors (disc, part_lba+next_lba2, 1, sectorBuffer)) return 0; part_lba2 = part_lba + next_lba2 + u8array_to_u32(sectorBuffer, 0x1C6) ; next_lba2 = u8array_to_u32(sectorBuffer, 0x1D6); - if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); - return 0; - } + if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) return 0; if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || - !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { - _FAT_mem_free(sectorBuffer); + !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) + { return part_lba2; } if(next_lba2==0) break; } } else { - if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); - return 0; - } + if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) return 0; if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) || !memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { - _FAT_mem_free(sectorBuffer); return part_lba; } } } - _FAT_mem_free(sectorBuffer); 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; - uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(MAX_SECTOR_SIZE); - if(!sectorBuffer) { - return NULL; - } - // Read first sector of disc if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); return NULL; } // Make sure it is a valid MBR or boot sector if ( (sectorBuffer[BPB_bootSig_55] != 0x55) || (sectorBuffer[BPB_bootSig_AA] != 0xAA)) { - _FAT_mem_free(sectorBuffer); return NULL; } @@ -208,23 +193,21 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach // Check for FAT32 startSector = 0; } else { - startSector = FindFirstValidPartition(disc); + startSector = FindFirstValidPartition_buf(disc, sectorBuffer); if (!_FAT_disc_readSectors (disc, startSector, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); return NULL; } } // Now verify that this is indeed a FAT partition if (memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) && - memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) { - _FAT_mem_free(sectorBuffer); + memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) + { return NULL; } partition = (PARTITION*) _FAT_mem_allocate (sizeof(PARTITION)); if (partition == NULL) { - _FAT_mem_free(sectorBuffer); return NULL; } @@ -253,11 +236,10 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach partition->bytesPerSector = u8array_to_u16(sectorBuffer, BPB_bytesPerSector); if(partition->bytesPerSector < MIN_SECTOR_SIZE || partition->bytesPerSector > MAX_SECTOR_SIZE) { - // Unsupported sector size - _FAT_mem_free(sectorBuffer); - _FAT_mem_free(partition); - return NULL; - } + // Unsupported sector size + _FAT_mem_free(partition); + return NULL; + } partition->sectorsPerCluster = sectorBuffer[BPB_sectorsPerCluster]; partition->bytesPerCluster = partition->bytesPerSector * partition->sectorsPerCluster; @@ -269,14 +251,14 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach partition->totalSize = ((uint64_t)partition->numberOfSectors - (partition->dataStart - startSector)) * (uint64_t)partition->bytesPerSector; - //FS info sector - partition->fsInfoSector = startSector + (u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) ? u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) : 1); + //FS info sector + partition->fsInfoSector = startSector + (u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) ? u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) : 1); // Store info about FAT uint32_t clusterCount = (partition->numberOfSectors - (uint32_t)(partition->dataStart - startSector)) / partition->sectorsPerCluster; partition->fat.lastCluster = clusterCount + CLUSTER_FIRST - 1; partition->fat.firstFree = CLUSTER_FIRST; - partition->fat.numberFreeCluster = 0; + partition->fat.numberFreeCluster = 0; partition->fat.numberLastAllocCluster = 0; if (clusterCount < CLUSTERS_PER_FAT12) { @@ -314,11 +296,20 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach _FAT_partition_readFSinfo(partition); - _FAT_mem_free(sectorBuffer); - 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) { FILE_STRUCT* nextFile; @@ -331,8 +322,8 @@ void _FAT_partition_destructor (PARTITION* partition) { nextFile = nextFile->nextOpenFile; } - // Write out the fs info sector - _FAT_partition_writeFSinfo(partition); + // Write out the fs info sector + _FAT_partition_writeFSinfo(partition); // Free memory used by the cache, writing it to disc at the same time _FAT_cache_destructor (partition->cache); @@ -359,89 +350,82 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) { void _FAT_partition_createFSinfo(PARTITION * partition) { - if(partition->readOnly || partition->filesysType != FS_FAT32) - return; + if(partition->readOnly || partition->filesysType != FS_FAT32) + return; - uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); - if(!sectorBuffer) { - return; + uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector); + if (!sectorBuffer) return; + memset(sectorBuffer, 0, partition->bytesPerSector); + + int i; + for(i = 0; i < 4; ++i) + { + sectorBuffer[FSIB_SIG1+i] = FS_INFO_SIG1[i]; + sectorBuffer[FSIB_SIG2+i] = FS_INFO_SIG2[i]; } - memset(sectorBuffer, 0, partition->bytesPerSector); - int i; - for(i = 0; i < 4; ++i) - { - sectorBuffer[FSIB_SIG1+i] = FS_INFO_SIG1[i]; - sectorBuffer[FSIB_SIG2+i] = FS_INFO_SIG2[i]; - } + partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition); + u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); + u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster); - partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition); - u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); - u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster); + sectorBuffer[FSIB_bootSig_55] = 0x55; + sectorBuffer[FSIB_bootSig_AA] = 0xAA; - sectorBuffer[FSIB_bootSig_55] = 0x55; - sectorBuffer[FSIB_bootSig_AA] = 0xAA; + _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); } void _FAT_partition_readFSinfo(PARTITION * partition) { - if(partition->filesysType != FS_FAT32) - return; + if(partition->filesysType != FS_FAT32) + return; - uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); - if(!sectorBuffer) { - return; - } - memset(sectorBuffer, 0, partition->bytesPerSector); + uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector); + if (!sectorBuffer) return; + memset(sectorBuffer, 0, partition->bytesPerSector); // Read first sector of disc if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); + _FAT_mem_free(sectorBuffer); return; } - if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) != 0 || - memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4) != 0 || - u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster) == 0) { - //sector does not yet exist, create one! - _FAT_partition_createFSinfo(partition); - _FAT_mem_free(sectorBuffer); - return; - } - - partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster); - partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster); - _FAT_mem_free(sectorBuffer); + if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) != 0 || + memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4) != 0 || + u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster) == 0) + { + //sector does not yet exist, create one! + _FAT_partition_createFSinfo(partition); + } else { + partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster); + partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster); + } + _FAT_mem_free(sectorBuffer); } void _FAT_partition_writeFSinfo(PARTITION * partition) { - if(partition->filesysType != FS_FAT32) - return; + if(partition->filesysType != FS_FAT32) + return; - uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector); - if(!sectorBuffer) { - return; - } - memset(sectorBuffer, 0, partition->bytesPerSector); + uint8_t *sectorBuffer = (uint8_t*) _FAT_mem_align(partition->bytesPerSector); + if (!sectorBuffer) return; + memset(sectorBuffer, 0, partition->bytesPerSector); // Read first sector of disc if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) { - _FAT_mem_free(sectorBuffer); + _FAT_mem_free(sectorBuffer); return; } if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) || memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4)) { - _FAT_mem_free(sectorBuffer); - return; + _FAT_mem_free(sectorBuffer); + return; } - u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); - u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster); + u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); + 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_mem_free(sectorBuffer); }