void _FAT_partition_writeFSinfo(PARTITION * partition) { if(partition->filesysType != FS_FAT32) return; 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); return; } if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) || memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4)) { _FAT_mem_free(sectorBuffer); return; } u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster); u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster); // Write first sector of disc _FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer); _FAT_mem_free(sectorBuffer); }
void _FAT_partition_createFSinfo(PARTITION * partition) { if(partition->readOnly || partition->filesysType != FS_FAT32) 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]; } 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; _FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer); _FAT_mem_free(sectorBuffer); }
/* Flushes all dirty pages to disc, clearing the dirty flag. */ bool _FAT_cache_flush (CACHE* cache) { unsigned int i; for (i = 0; i < cache->numberOfPages; i++) { if (cache->cacheEntries[i].dirty) { if (!_FAT_disc_writeSectors (cache->disc, cache->cacheEntries[i].sector, cache->cacheEntries[i].count, cache->cacheEntries[i].cache)) { return false; } } cache->cacheEntries[i].dirty = false; } return true; }
CACHE_ENTRY* _FAT_cache_getPage(CACHE *cache,sec_t sector) { unsigned int i; CACHE_ENTRY* cacheEntries = cache->cacheEntries; unsigned int numberOfPages = cache->numberOfPages; unsigned int sectorsPerPage = cache->sectorsPerPage; bool foundFree = false; unsigned int oldUsed = 0; unsigned int oldAccess = UINT_MAX; for(i=0;i<numberOfPages;i++) { if(sector>=cacheEntries[i].sector && sector<(cacheEntries[i].sector + cacheEntries[i].count)) { cacheEntries[i].last_access = accessTime(); return &(cacheEntries[i]); } if(foundFree==false && (cacheEntries[i].sector==CACHE_FREE || cacheEntries[i].last_access<oldAccess)) { if(cacheEntries[i].sector==CACHE_FREE) foundFree = true; oldUsed = i; oldAccess = cacheEntries[i].last_access; } } if(foundFree==false && cacheEntries[oldUsed].dirty==true) { if(!_FAT_disc_writeSectors(cache->disc,cacheEntries[oldUsed].sector,cacheEntries[oldUsed].count,cacheEntries[oldUsed].cache)) return NULL; cacheEntries[oldUsed].dirty = false; } sector = (sector/sectorsPerPage)*sectorsPerPage; // align base sector to page size sec_t next_page = sector + sectorsPerPage; if(next_page > cache->endOfPartition) next_page = cache->endOfPartition; if(!_FAT_disc_readSectors(cache->disc,sector,next_page-sector,cacheEntries[oldUsed].cache)) return NULL; cacheEntries[oldUsed].sector = sector; cacheEntries[oldUsed].count = next_page-sector; cacheEntries[oldUsed].last_access = accessTime(); return &(cacheEntries[oldUsed]); }
static void _FAT_updateFS_INFO(PARTITION * partition, uint8_t *sectorBuffer) { 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); _FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer); }