?? fat32_access.c
字號:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FAT32 File IO Library
// V2.0
// Rob Riglar
// Copyright 2003 - 2007
//
// Email: rob@robriglar.com
//
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include "FAT32_Definitions.h"
#include "FAT32_Base.h"
#include "FAT32_Table.h"
#include "FAT32_Access.h"
#include "FAT32_Write.h"
#include "FAT32_Filestring.h"
#include "FAT32_Misc.h"
//-----------------------------------------------------------------------------
// FAT32_InitFAT: Load FAT32 Parameters
//-----------------------------------------------------------------------------
BOOL FAT32_InitFAT(void)
{
FATFS_Internal.SectorCurrentlyLoaded = 0xFFFFFFFF;
FATFS_Internal.NextFreeCluster = 0xFFFFFFFF;
return FAT32_Init();
}
//-----------------------------------------------------------------------------
// FAT32_SectorReader: From the provided startcluster and sector offset
// Returns True if success, returns False if not (including if read out of range)
//-----------------------------------------------------------------------------
BOOL FAT32_SectorReader(UINT32 Startcluster, UINT32 offset)
{
UINT32 SectortoRead = 0;
UINT32 ClustertoRead = 0;
UINT32 ClusterChain = 0;
UINT32 i;
UINT32 lba;
// Set start of cluster chain to initial value
ClusterChain = Startcluster;
// Find parameters
ClustertoRead = offset / FAT32.SectorsPerCluster;
SectortoRead = offset - (ClustertoRead*FAT32.SectorsPerCluster);
// Follow chain to find cluster to read
for (i=0; i<ClustertoRead; i++)
ClusterChain = FAT32_FindNextCluster(ClusterChain);
// If end of cluster chain then return false
if (ClusterChain==0xFFFFFFFF)
return FALSE;
// Calculate sector address
lba = FAT32_LBAofCluster(ClusterChain)+SectortoRead;
// Else read sector if not already loaded
if (lba!=FATFS_Internal.SectorCurrentlyLoaded)
{
FATFS_Internal.SectorCurrentlyLoaded = lba;
return FAT_ReadSector(FATFS_Internal.SectorCurrentlyLoaded, FATFS_Internal.currentsector);
}
else
return TRUE;
}
//-----------------------------------------------------------------------------
// FAT32_SectorWriter: Write to the provided startcluster and sector offset
// Returns True if success, returns False if not
//-----------------------------------------------------------------------------
#ifdef INCLUDE_WRITE_SUPPORT
BOOL FAT32_SectorWriter(UINT32 Startcluster, UINT32 offset)
{
UINT32 SectortoWrite = 0;
UINT32 ClustertoWrite = 0;
UINT32 ClusterChain = 0;
UINT32 LastClusterChain = 0xFFFFFFFF;
UINT32 i;
// Set start of cluster chain to initial value
ClusterChain = Startcluster;
// Find parameters
ClustertoWrite = offset / FAT32.SectorsPerCluster;
SectortoWrite = offset - (ClustertoWrite*FAT32.SectorsPerCluster);
// Follow chain to find cluster to read
for (i=0; i<ClustertoWrite; i++)
{
// Find next link in the chain
LastClusterChain = ClusterChain;
ClusterChain = FAT32_FindNextCluster(ClusterChain);
// Dont keep following a dead end
if (ClusterChain==0xFFFFFFFF)
break;
}
// If end of cluster chain
if (ClusterChain==0xFFFFFFFF)
{
// Add another cluster to the last good cluster chain
if (!FAT32_AddFreeSpaceToChain(&LastClusterChain))
return FALSE;
ClusterChain = LastClusterChain;
}
// Calculate write address
FATFS_Internal.SectorCurrentlyLoaded = FAT32_LBAofCluster(ClusterChain)+SectortoWrite;
// Write to disk
return FAT_WriteSector(FATFS_Internal.SectorCurrentlyLoaded, FATFS_Internal.currentsector);
}
#endif
//-----------------------------------------------------------------------------
// FAT32_ShowFATDetails: Show the details about the filesystem
//-----------------------------------------------------------------------------
void FAT32_ShowFATDetails(void)
{
printf("\r\nCurrent Disc FAT details\r\n------------------------\r\nRoot Dir First Cluster = ");
printf("0x%x",FAT32.RootDir_First_Cluster);
printf("\r\nFAT Begin LBA = ");
printf("0x%x",FAT32.fat_begin_lba);
printf("\r\nCluster Begin LBA = ");
printf("0x%x",FAT32.cluster_begin_lba);
printf("\r\nSectors Per Cluster = ");
printf("%d",FAT32.SectorsPerCluster);
printf("\r\n\r\nFormula for conversion from Cluster num to LBA is;");
printf("\r\nLBA = (cluster_begin_lba + ((Cluster_Number-2)*SectorsPerCluster)))\r\n");
}
//-----------------------------------------------------------------------------
// FAT32_GetRootCluster: Get the root dir cluster
//-----------------------------------------------------------------------------
UINT32 FAT32_GetRootCluster()
{
return FAT32.RootDir_First_Cluster;
}
//-------------------------------------------------------------
// FAT32_GetFileEntry: Find the file entry for a filename
//-------------------------------------------------------------
UINT32 FAT32_GetFileEntry(UINT32 Cluster, char *nametofind, FAT32_ShortEntry *sfEntry)
{
BYTE item=0;
UINT16 recordoffset = 0;
BYTE i=0;
int x=0;
char LongFilename[MAX_LONG_FILENAME];
char ShortFilename[13];
FAT32_ShortEntry *directoryEntry;
FATMisc_ClearLFN(TRUE);
// Main cluster following loop
while (TRUE)
{
// Read sector
if (FAT32_SectorReader(Cluster, x++)) // If sector read was successfull
{
// Analyse Sector
for (item=0; item<=15;item++)
{
// Create the multiplier for sector access
recordoffset = (32*item);
// Overlay directory entry over buffer
directoryEntry = (FAT32_ShortEntry*)(FATFS_Internal.currentsector+recordoffset);
// Long File Name Text Found
if (FATMisc_If_LFN_TextOnly(directoryEntry) )
FATMisc_CacheLFN(FATFS_Internal.currentsector+recordoffset);
// If Invalid record found delete any long file name information collated
else if (FATMisc_If_LFN_Invalid(directoryEntry) )
FATMisc_ClearLFN(FALSE);
// Normal SFN Entry and Long text exists
else if (FATMisc_If_LFN_Exists(directoryEntry) )
{
FATMisc_GetLFNCache((BYTE*)LongFilename);
// Compare names to see if they match
if (FileString_Compare(LongFilename, nametofind))
{
memcpy(sfEntry,directoryEntry,sizeof(FAT32_ShortEntry));
return TRUE;
}
FATMisc_ClearLFN(FALSE);
}
// Normal Entry, only 8.3 Text
else if (FATMisc_If_noLFN_SFN_Only(directoryEntry) )
{
memset(ShortFilename, 0, sizeof(ShortFilename));
// Copy name to string
for (i=0; i<8; i++)
ShortFilename[i] = directoryEntry->Name[i];
// If not . or .. entry
if (ShortFilename[0]!='.')
ShortFilename[8] = '.';
else
ShortFilename[8] = ' ';
// Extension
for (i=8; i<11; i++)
ShortFilename[i+1] = directoryEntry->Name[i];
// Compare names to see if they match
if (FileString_Compare(ShortFilename, nametofind))
{
memcpy(sfEntry,directoryEntry,sizeof(FAT32_ShortEntry));
return TRUE;
}
FATMisc_ClearLFN(FALSE);
}
} // End of if
}
else
break;
} // End of while loop
return FALSE;
}
//-------------------------------------------------------------
// FAT32_SFNexists: Check if a short filename exists.
// NOTE: shortname is XXXXXXXXYYY not XXXXXXXX.YYY
//-------------------------------------------------------------
#ifdef INCLUDE_WRITE_SUPPORT
BOOL FAT32_SFNexists(UINT32 Cluster, char *shortname)
{
BYTE item=0;
UINT16 recordoffset = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -