libfat/source/file_allocation_table.c

394 lines
12 KiB
C
Raw Normal View History

2006-07-14 04:42:37 +02:00
/*
file_allocation_table.c
Reading, writing and manipulation of the FAT structure on
a FAT partition
Copyright (c) 2006 Michael "Chishm" Chisholm
2006-07-14 04:42:37 +02:00
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "file_allocation_table.h"
#include "partition.h"
#include "mem_allocate.h"
#include <string.h>
2006-07-14 04:42:37 +02:00
/*
Gets the cluster linked from input cluster
*/
uint32_t _FAT_fat_nextCluster(PARTITION* partition, uint32_t cluster)
2006-07-14 04:42:37 +02:00
{
uint32_t nextCluster = CLUSTER_FREE;
sec_t sector;
2006-07-14 04:42:37 +02:00
int offset;
if (cluster == CLUSTER_FREE) {
return CLUSTER_FREE;
}
switch (partition->filesysType)
2006-07-14 04:42:37 +02:00
{
case FS_UNKNOWN:
return CLUSTER_ERROR;
2006-07-14 04:42:37 +02:00
break;
case FS_FAT12:
{
u32 nextCluster_h;
sector = partition->fat.fatStart + (((cluster * 3) / 2) / partition->bytesPerSector);
offset = ((cluster * 3) / 2) % partition->bytesPerSector;
2006-07-14 04:42:37 +02:00
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
offset++;
if (offset >= partition->bytesPerSector) {
2006-07-14 04:42:37 +02:00
offset = 0;
sector++;
}
nextCluster_h = 0;
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &nextCluster_h, sector, offset, sizeof(u8));
nextCluster |= (nextCluster_h << 8);
2006-07-14 04:42:37 +02:00
if (cluster & 0x01) {
nextCluster = nextCluster >> 4;
} else {
nextCluster &= 0x0FFF;
}
2006-07-14 04:42:37 +02:00
if (nextCluster >= 0x0FF7)
{
nextCluster = CLUSTER_EOF;
}
break;
}
2006-07-14 04:42:37 +02:00
case FS_FAT16:
sector = partition->fat.fatStart + ((cluster << 1) / partition->bytesPerSector);
offset = (cluster % (partition->bytesPerSector >> 1)) << 1;
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u16));
if (nextCluster >= 0xFFF7) {
2006-07-14 04:42:37 +02:00
nextCluster = CLUSTER_EOF;
}
break;
2006-07-14 04:42:37 +02:00
case FS_FAT32:
sector = partition->fat.fatStart + ((cluster << 2) / partition->bytesPerSector);
offset = (cluster % (partition->bytesPerSector >> 2)) << 2;
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u32));
if (nextCluster >= 0x0FFFFFF7) {
2006-07-14 04:42:37 +02:00
nextCluster = CLUSTER_EOF;
}
break;
2006-07-14 04:42:37 +02:00
default:
return CLUSTER_ERROR;
2006-07-14 04:42:37 +02:00
break;
}
2006-07-14 04:42:37 +02:00
return nextCluster;
}
/*
writes value into the correct offset within a partition's FAT, based
2006-07-14 04:42:37 +02:00
on the cluster number.
*/
static bool _FAT_fat_writeFatEntry (PARTITION* partition, uint32_t cluster, uint32_t value) {
sec_t sector;
2006-07-14 04:42:37 +02:00
int offset;
uint32_t oldValue;
2006-07-14 04:42:37 +02:00
if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */))
2006-07-14 04:42:37 +02:00
{
return false;
}
switch (partition->filesysType)
2006-07-14 04:42:37 +02:00
{
case FS_UNKNOWN:
return false;
break;
2006-07-14 04:42:37 +02:00
case FS_FAT12:
sector = partition->fat.fatStart + (((cluster * 3) / 2) / partition->bytesPerSector);
offset = ((cluster * 3) / 2) % partition->bytesPerSector;
2006-07-14 04:42:37 +02:00
if (cluster & 0x01) {
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &oldValue, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
value = (value << 4) | (oldValue & 0x0F);
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, value & 0xFF, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
offset++;
if (offset >= partition->bytesPerSector) {
2006-07-14 04:42:37 +02:00
offset = 0;
sector++;
}
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, (value >> 8) & 0xFF, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
} else {
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
offset++;
if (offset >= partition->bytesPerSector) {
2006-07-14 04:42:37 +02:00
offset = 0;
sector++;
}
2008-05-01 12:02:19 +02:00
_FAT_cache_readLittleEndianValue (partition->cache, &oldValue, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
value = ((value >> 8) & 0x0F) | (oldValue & 0xF0);
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u8));
2006-07-14 04:42:37 +02:00
}
break;
2006-07-14 04:42:37 +02:00
case FS_FAT16:
sector = partition->fat.fatStart + ((cluster << 1) / partition->bytesPerSector);
offset = (cluster % (partition->bytesPerSector >> 1)) << 1;
2006-07-14 04:42:37 +02:00
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u16));
2006-07-14 04:42:37 +02:00
break;
2006-07-14 04:42:37 +02:00
case FS_FAT32:
sector = partition->fat.fatStart + ((cluster << 2) / partition->bytesPerSector);
offset = (cluster % (partition->bytesPerSector >> 2)) << 2;
2008-05-01 12:02:19 +02:00
_FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u32));
2006-07-14 04:42:37 +02:00
break;
2006-07-14 04:42:37 +02:00
default:
return false;
break;
}
2006-07-14 04:42:37 +02:00
return true;
}
/*-----------------------------------------------------------------
gets the first available free cluster, sets it
to end of file, links the input cluster to it then returns the
2006-07-14 04:42:37 +02:00
cluster number
If an error occurs, return CLUSTER_ERROR
2006-07-14 04:42:37 +02:00
-----------------------------------------------------------------*/
uint32_t _FAT_fat_linkFreeCluster(PARTITION* partition, uint32_t cluster) {
uint32_t firstFree;
uint32_t curLink;
uint32_t lastCluster;
bool loopedAroundFAT = false;
2006-07-14 04:42:37 +02:00
lastCluster = partition->fat.lastCluster;
if (cluster > lastCluster) {
return CLUSTER_ERROR;
2006-07-14 04:42:37 +02:00
}
// Check if the cluster already has a link, and return it if so
curLink = _FAT_fat_nextCluster(partition, cluster);
if ((curLink >= CLUSTER_FIRST) && (curLink <= lastCluster)) {
2006-07-14 04:42:37 +02:00
return curLink; // Return the current link - don't allocate a new one
}
2006-07-14 04:42:37 +02:00
// Get a free cluster
firstFree = partition->fat.firstFree;
// Start at first valid cluster
if (firstFree < CLUSTER_FIRST) {
firstFree = CLUSTER_FIRST;
}
// Search until a free cluster is found
while (_FAT_fat_nextCluster(partition, firstFree) != CLUSTER_FREE) {
2006-07-14 04:42:37 +02:00
firstFree++;
if (firstFree > lastCluster) {
if (loopedAroundFAT) {
// If couldn't get a free cluster then return an error
partition->fat.firstFree = firstFree;
return CLUSTER_ERROR;
} else {
// Try looping back to the beginning of the FAT
// This was suggested by loopy
firstFree = CLUSTER_FIRST;
loopedAroundFAT = true;
}
}
2006-07-14 04:42:37 +02:00
}
partition->fat.firstFree = firstFree;
if(partition->fat.numberFreeCluster)
partition->fat.numberFreeCluster--;
partition->fat.numberLastAllocCluster = firstFree;
2006-07-14 04:42:37 +02:00
2010-10-12 13:36:42 +02:00
if ((cluster >= CLUSTER_FIRST) && (cluster <= lastCluster))
2006-07-14 04:42:37 +02:00
{
// Update the linked from FAT entry
_FAT_fat_writeFatEntry (partition, cluster, firstFree);
}
// Create the linked to FAT entry
_FAT_fat_writeFatEntry (partition, firstFree, CLUSTER_EOF);
return firstFree;
}
/*-----------------------------------------------------------------
gets the first available free cluster, sets it
to end of file, links the input cluster to it, clears the new
cluster to 0 valued bytes, then returns the cluster number
If an error occurs, return CLUSTER_ERROR
-----------------------------------------------------------------*/
uint32_t _FAT_fat_linkFreeClusterCleared (PARTITION* partition, uint32_t cluster) {
uint32_t newCluster;
uint32_t i;
uint8_t *emptySector;
// Link the cluster
newCluster = _FAT_fat_linkFreeCluster(partition, cluster);
if (newCluster == CLUSTER_FREE || newCluster == CLUSTER_ERROR) {
return CLUSTER_ERROR;
}
emptySector = (uint8_t*) _FAT_mem_allocate(partition->bytesPerSector);
// Clear all the sectors within the cluster
memset (emptySector, 0, partition->bytesPerSector);
for (i = 0; i < partition->sectorsPerCluster; i++) {
_FAT_cache_writeSectors (partition->cache,
_FAT_fat_clusterToSector (partition, newCluster) + i,
1, emptySector);
}
_FAT_mem_free(emptySector);
return newCluster;
}
2006-07-14 04:42:37 +02:00
/*-----------------------------------------------------------------
_FAT_fat_clearLinks
frees any cluster used by a file
-----------------------------------------------------------------*/
bool _FAT_fat_clearLinks (PARTITION* partition, uint32_t cluster) {
uint32_t nextCluster;
if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */))
2006-07-14 04:42:37 +02:00
return false;
// If this clears up more space in the FAT before the current free pointer, move it backwards
if (cluster < partition->fat.firstFree) {
partition->fat.firstFree = cluster;
}
2006-07-14 04:42:37 +02:00
while ((cluster != CLUSTER_EOF) && (cluster != CLUSTER_FREE) && (cluster != CLUSTER_ERROR)) {
2006-07-14 04:42:37 +02:00
// Store next cluster before erasing the link
nextCluster = _FAT_fat_nextCluster (partition, cluster);
// Erase the link
_FAT_fat_writeFatEntry (partition, cluster, CLUSTER_FREE);
if(partition->fat.numberFreeCluster < (partition->numberOfSectors/partition->sectorsPerCluster))
partition->fat.numberFreeCluster++;
2006-07-14 04:42:37 +02:00
// Move onto next cluster
cluster = nextCluster;
}
return true;
}
/*-----------------------------------------------------------------
_FAT_fat_trimChain
Drop all clusters past the chainLength.
If chainLength is 0, all clusters are dropped.
If chainLength is 1, the first cluster is kept and the rest are
dropped, and so on.
Return the last cluster left in the chain.
-----------------------------------------------------------------*/
uint32_t _FAT_fat_trimChain (PARTITION* partition, uint32_t startCluster, unsigned int chainLength) {
uint32_t nextCluster;
if (chainLength == 0) {
// Drop the entire chain
_FAT_fat_clearLinks (partition, startCluster);
return CLUSTER_FREE;
} else {
// Find the last cluster in the chain, and the one after it
chainLength--;
nextCluster = _FAT_fat_nextCluster (partition, startCluster);
while ((chainLength > 0) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) {
chainLength--;
startCluster = nextCluster;
nextCluster = _FAT_fat_nextCluster (partition, startCluster);
}
// Drop all clusters after the last in the chain
if (nextCluster != CLUSTER_FREE && nextCluster != CLUSTER_EOF) {
_FAT_fat_clearLinks (partition, nextCluster);
}
// Mark the last cluster in the chain as the end of the file
_FAT_fat_writeFatEntry (partition, startCluster, CLUSTER_EOF);
return startCluster;
}
}
2006-07-14 04:42:37 +02:00
/*-----------------------------------------------------------------
_FAT_fat_lastCluster
Trace the cluster links until the last one is found
-----------------------------------------------------------------*/
uint32_t _FAT_fat_lastCluster (PARTITION* partition, uint32_t cluster) {
2006-07-14 04:42:37 +02:00
while ((_FAT_fat_nextCluster(partition, cluster) != CLUSTER_FREE) && (_FAT_fat_nextCluster(partition, cluster) != CLUSTER_EOF)) {
cluster = _FAT_fat_nextCluster(partition, cluster);
}
return cluster;
}
/*-----------------------------------------------------------------
_FAT_fat_freeClusterCount
Return the number of free clusters available
-----------------------------------------------------------------*/
unsigned int _FAT_fat_freeClusterCount (PARTITION* partition) {
unsigned int count = 0;
uint32_t curCluster;
for (curCluster = CLUSTER_FIRST; curCluster <= partition->fat.lastCluster; curCluster++) {
if (_FAT_fat_nextCluster(partition, curCluster) == CLUSTER_FREE) {
count++;
}
}
return count;
}