📄 fat32_base.c
字号:
#include "fat_include.h"
FAT32_t FAT32;
//-----------------------------------------------------------------------------
// FAT32_InitLBA: This function is used to find the LBA Address of the first
// volume on the disc. Also checks are performed on the signature
// and identity codes to make sure the partition is FAT32.
//-----------------------------------------------------------------------------
UI32 FAT32_FindLBABegin(void)
{
UI8 tempdata;
#ifdef DEBUG_LV0_ON
UI8 *ch;
UI16 i;
#endif
UI32 LBABEGIN;
// Load MBR (LBA 0) into the 512 byte buffer
IDE_BufferSector(0);
#ifdef DEBUG_LV0_ON
//display Sector data
ch=(UI8*)&(IDE_Internal.currentsector[0]);
DEBUG_OUT("\n\rIN <FAT32_FindLBABegin> the First Sector(LBA0) is");
for(i=0;i<HDD_SECTOR_SIZE;i=i+16)
{
DEBUG_OUT("\n\rBYTE%4x:%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x",\
i, *(ch+i+0), *(ch+i+1), *(ch+i+2), *(ch+i+3), *(ch+i+4), *(ch+i+5), *(ch+i+6), *(ch+i+7),\
*(ch+i+8), *(ch+i+9), *(ch+i+10), *(ch+i+11), *(ch+i+12), *(ch+i+13), *(ch+i+14), *(ch+i+15));
}
#endif
// Make Sure 0x55 and 0xAA are at end of sector
if (IDE_SectorWord(Signature_Position)!=Signature_Value)
{
s3c44b0x_Printf("\r\nInvalid MBR, Please Format Drive");
s3c44b0x_SysHalt();
}
// Verify Type Code as 0C or 0B for FAT32
tempdata=IDE_SectorByte(PARTITION1_TYPECODE_LOCATION);
if ((tempdata==FAT32_TYPECODE1)||(tempdata==FAT32_TYPECODE2))
s3c44b0x_Printf("\r\nValid FAT32 Partition Found");
else
{
s3c44b0x_Printf("\r\nNon FAT32 Partition Found 0x%x",tempdata);
s3c44b0x_SysHalt();
}
// Read LBA Begin for FAT32 File system is located for partition
LBABEGIN=IDE_SectorUI32(PARTITION1_LBA_BEGIN_LOCATION);
// Return the LBA address of FAT table
return LBABEGIN;
}
//-----------------------------------------------------------------------------
// LBAofCluster: This function converts a cluster number into a sector / LBA
// number.
//-----------------------------------------------------------------------------
UI32 FAT32_LBAofCluster(UI32 Cluster_Number)
{
return ((FAT32.cluster_begin_lba + ((Cluster_Number-2)*FAT32.SectorsPerCluster)));
}
//-----------------------------------------------------------------------------
// FindFATDetails: Uses FAT32_FindLBABegin to find the LBA for the volume,
// and loads into memory some specific details of the partition
// which are used in further calculations.
//-----------------------------------------------------------------------------
void FAT32_FindFAT32Details(void)
{
UI8 Number_of_FATS;
UI16 Reserved_Sectors;
UI32 Sectors_per_FAT;
UI32 LBA_BEGIN;
LBA_BEGIN = FAT32_FindLBABegin(); // Check Volume 1 and find LBA address
IDE_BufferSector(LBA_BEGIN); // Load Volume 1 table in 'clusterbuffer'
// Make sure there are 512 bytes per cluster
if (IDE_SectorWord(0x0B)!=0x200)
{
s3c44b0x_Printf("\r\nError: Bytes per cluster is not 512");
s3c44b0x_SysHalt();
}
// Load Parameters of FAT32
FAT32.SectorsPerCluster = IDE_SectorByte(0x0D);
Reserved_Sectors = IDE_SectorWord(0x0E);
Number_of_FATS = IDE_SectorByte(0x10);
Sectors_per_FAT = IDE_SectorUI32(0x24);
FAT32.RootDir_First_Cluster = IDE_SectorUI32(0x2C);
FAT32.fat_begin_lba = LBA_BEGIN + Reserved_Sectors; // First FAT LBA address
// The address of the first data cluster on this volume
FAT32.cluster_begin_lba = FAT32.fat_begin_lba + (Number_of_FATS * Sectors_per_FAT);
if (IDE_SectorWord(0x1FE)!=0xAA55) // This signature should be AA55
{
s3c44b0x_Printf("\r\nError: Incorrect Volume Signature");
s3c44b0x_SysHalt();
}
}
//-----------------------------------------------------------------------------
// FindNextCluster: Return Cluster number of next cluster in chain by reading
// FAT table and traversing it. Return 0xffffffff for end of
// chain.
//-----------------------------------------------------------------------------
UI32 FAT32_FindNextCluster(UI32 Current_Cluster)
{
UI32 FAT_sector_offset, UI32position;
UI32 nextcluster;
// Why is '..' labelled with cluster 0 when it should be 2 ??
if (Current_Cluster==0) Current_Cluster=2;
// Find which sector of FAT table to read
FAT_sector_offset = Current_Cluster / 128;
// Read FAT sector into buffer
IDE_BufferSector(FAT32.fat_begin_lba+FAT_sector_offset);
// Find 32 bit entry of current sector relating to cluster number
UI32position = (Current_Cluster - (FAT_sector_offset * 128)) * 4;
// Read Next Clusters value from Sector Buffer
nextcluster = IDE_SectorUI32(UI32position);
// Mask out MS 4 bits (its 28bit addressing)
nextcluster = nextcluster & 0x0FFFFFFF;
// If 0x0FFFFFFF then end of chain found
if (nextcluster==0x0FFFFFFF)
return (0xFFFFFFFF);
else
// Else return next cluster
return (nextcluster);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -