📄 fat.c
字号:
#include <iom128v.h>
#include <macros.h>
#include <string.h>
#include <stdio.h>
#include "vs1001.h"
#include "generic.h"
#include "remote.h"
#include "fat.h"
#include "ata.h"
//******************************************************************
//* FAT Global Variable
//******************************************************************
struct PartRecord Part;
struct BootRecord Boot;
struct FileInfo File;
unsigned long FirstDataSector;
//*************************************
// void init_fat(void)
//*************************************
void InitFat(void)
{
ata_read_sector_byte(0, 0, 446, sizeof(Part) , (unsigned char*)&Part);
ata_read_sector_byte(0, Part.StartLBA, 0, sizeof(Boot) , (unsigned char*)&Boot);
FirstDataSector = Part.StartLBA + Boot.ResSectors + (Boot.FATs * Boot.FATSize);
}
//*************************************
// unsigned long clust2LBA(unsigned long clust)
//*************************************
unsigned long Clust2LBA(unsigned long clust)
{
return ((clust-2) * Boot.SecPerClust) + FirstDataSector;
}
//****************************************************************
// unsigned long GetNextCluster(unsigned long Clust)
//
// Description:
// Returns the FAT32 Entry for the given Cluster Number.
// The FAT32 Entry is the next cluster number of the cluster
// chain. **May return a termination
//
// if Fat32_Entry >= 0x0FFFFFF8 EOC (End of Chain)
// if Fat32_Entry == 0x0FFFFFF7 Bad Cluster
//****************************************************************
unsigned long GetNextCluster(unsigned long Clust)
{
unsigned long FatSectorNum;
unsigned long FatSectorOffset;
unsigned long Fat32_Entry;
FatSectorNum = Part.StartLBA + Boot.ResSectors + ((Clust * 4) / Boot.BytesPerSec);;
FatSectorOffset = (Clust * 4) % Boot.BytesPerSec;
ata_read_sector_byte(0, FatSectorNum, FatSectorOffset, 4 , (unsigned char*)&Fat32_Entry);
return(Fat32_Entry & 0x0FFFFFFF);
}
//*************************************
// int GetDir(unsigned long Cluster, unsigned int FileWanted, unsigned int Type)
//
//*************************************
int GetDir(unsigned long Cluster, unsigned int From, unsigned int Qte, unsigned int Display)
{
extern unsigned char SectorBuffer[512];
unsigned long LBA,i,NextCluster;
unsigned int Offset,j,k,LongFilePtr,LongFileName,Files;
unsigned char LinePtr;
union FileEntry
{
struct DirEntry Dir;
struct WinEntry Win;
};
union FileEntry Entry;
NextCluster = Cluster;
Files = 0;
LinePtr = 0;
while (1)
{
LBA = Clust2LBA(NextCluster);
for (i=0;i<Boot.SecPerClust;i++)
{
ata_read_sector_byte(0, LBA+i, 0, 512, (unsigned char*)&SectorBuffer);
for (Offset=0;Offset<Boot.BytesPerSec;Offset=Offset+32)
{
memcpy((unsigned char*)&Entry,(unsigned char*)&SectorBuffer[Offset],sizeof(Entry));
if (Entry.Dir.Attributes == ATTR_LONG_FILENAME)
{
LongFileName = TRUE;
LongFilePtr = ((Entry.Win.Cnt & 0x3f) * 13) - 13;
for (j=0;j<5;j++) File.Name[LongFilePtr++] = Entry.Win.Part1[j];
for (j=0;j<6;j++) File.Name[LongFilePtr++] = Entry.Win.Part2[j];
for (j=0;j<2;j++) File.Name[LongFilePtr++] = Entry.Win.Part3[j];
if (Entry.Win.Cnt & 0x40) File.Name[LongFilePtr++] = 0x00;
}
else
{
if ((Entry.Dir.Name[0] != SLOT_DELETED) && ((Entry.Dir.Attributes & 0x0e) == 0x00))
{
if (LongFileName == FALSE)
{
for (j=0;j<8;j++) File.Name[j] = Entry.Dir.Name[j];
File.Name[8] = 0x00;
}
File.FirstClust = Entry.Dir.FirstClustHi;
File.FirstClust = (File.FirstClust << 16) + Entry.Dir.FirstClustLo;
File.FileSize = Entry.Dir.FileSize;
File.Attr = Entry.Dir.Attributes;
if ((File.FirstClust == 0) &&
(File.Attr == ATTR_DIRECTORY)) File.FirstClust = 2; // Don't know why?
LongFileName = FALSE;
if ((File.FirstClust == 0) && (File.Attr == ATTR_NORMAL)) return Files;
if ((Files >= From) && (Files < (From + Qte)) && (Display == TRUE)) DisplayFiles(&LinePtr);
if (Files == (From + Qte - 1)) return Files;
Files++;
}
else LongFileName = FALSE;
}
}
}
NextCluster = GetNextCluster(NextCluster);
if (NextCluster > CLUST_RSRVD) break;
}
return Files;
}
//*************************************
// void DisplayFiles(unsigned int FileNum)
//
//*************************************
void DisplayFiles(unsigned char *LinePtr)
{
extern unsigned char RemoteTextLine[5][40];
int i,j;
if (*LinePtr == 0)
{
for (i=0;i<5;i++) // Clear all FileName in the commun buffer
{
for (j=0;j<40;j++) RemoteTextLine[i][j] = 0x00;
}
}
File.Name[39] = 0x00;
for (i=0;i<40;i++) RemoteTextLine[*LinePtr][i] = File.Name[i];
(*LinePtr)++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -