📄 fat.c
字号:
//******************************************************
//name : fat.c
//this flie include the commands for mass storage reading
//and analyze the system structe of the mass storage
// zhdgps wikee 2003.4.10
//******************************************************
//******************************************************
//include file
//******************************************************
#include "fat.h"
#include "testuart.h"
#include "flash.h"
#include "uart.h"
unsigned char FAT_TYPE;
unsigned char databuff[SectorLength];
void Init_File_System(unsigned int *Em_FAT,unsigned char *Em_DIR)
{
unsigned int TotalCluster,BootSector,RsdSector,SectorofFatSize,i,j;
///////////////////////////////////////////////////
//得到引导扇区所在扇区号,如果介质是不带分区的,则0扇区就是BootSector了。
ReadPage(ZeroCluster,0,databuff);
if(!(databuff[0]==0xeb&&databuff[2]==0x90)){ //通过判断EB ?? 90来看是否已经是BPB了
//带分区的介质
BootSector=databuff[454]+databuff[455]*256+databuff[456]*(256*256)+databuff[457]*(256*256*256);
}
else BootSector=0;
///////////////////////////////////////////////////
////////////////////////////////////////////////
//得到保留扇区数,总扇区数,总扇区数/每簇扇区数得到簇数,是FAT类型的依据
ReadPage(ZeroCluster,BootSector,databuff);
RsdSector=databuff[14]+databuff[15]*256;
TotalCluster=(databuff[20]*256+databuff[19])/databuff[13];//FAT16的簇总数=扇区总数/每簇扇区数
SectorofFatSize=((databuff[22]+databuff[23]*256));
if(TotalCluster==0){ //FAT32的扇区总数和FAT表项长度
TotalCluster=(databuff[32]+databuff[33]*256+databuff[34]*256*256+databuff[35]*256*256*256)/databuff[13];
SectorofFatSize=(databuff[36]+databuff[37]*256+databuff[38]*256*256+databuff[39]*256*256*256);
if(SectorofFatSize>(TotalCluster*16/512)) SectorofFatSize=((databuff[22]+databuff[23]*256));
}
////////////////////////////////////////////////////////////////
// Uart_Printf("\nBootSector:%x RsdSector:%x,TotalCluster:%x SectorofFatSize:%x\n",BootSector,RsdSector,TotalCluster,SectorofFatSize);
// for(i=0;i<512;i++) {if(!(i%32)) Uart_Printf("\n");Uart_Printf("%x ",databuff[i]);}
/////////////////////////////////////////////////////////////
//开始读取第一个FAT的第一个扇区
ReadPage(ZeroCluster,BootSector+RsdSector,databuff);
//根据总的簇数判断FAT类型,并进行FAT表项转换
//decied which type of FAT this File System Belongs to
if(TotalCluster==FAT32){ //FAT32
FAT_TYPE=FAT32;
for(i=0,j=0;j<(512-4);i++,j+=4) Em_FAT[i]=(databuff[j]&0x000000ff)|((databuff[j+1]&0x000000ff)<<8)|((databuff[j+2]&0x000000ff)<<16)|((databuff[j+3]&0x000000ff)<<24);
}
else if((TotalCluster>0)&&(TotalCluster<FAT12)) {//FAT12
FAT_TYPE=FAT12;
for(i=0,j=0;i<(512-3);i+=3) { //f8 (f2 f1) ff ---> f1 f8 ff f2 TotalCluster/3
Em_FAT[j]=0;
Em_FAT[j]=databuff[i+1]<<8;//ff00
Em_FAT[j]=Em_FAT[j]|databuff[i];//fff8
Em_FAT[j]=Em_FAT[j]&0x0fff;//0ff8
Em_FAT[++j]=0;
Em_FAT[j]=databuff[i+2]<<8;//ff00
Em_FAT[j]=Em_FAT[j]|databuff[i+1];//ffff
Em_FAT[j]=Em_FAT[j]>>4;//0fff
j++;
}
}
else { //FAT16
FAT_TYPE=FAT16;
for(i=0,j=0;i<(512-2);i++,j+=2) Em_FAT[i]=(databuff[j]&0x00ff)|((databuff[j+1]&0x00ff)<<8);//如果是FAT16则直接赋值
}
//////////////////////////////////////////////////////////////
////////////////////////////////////////////////
//读根目录表的第一个扇区并进行相应的转换
ReadPage(ZeroCluster,BootSector+RsdSector+2*SectorofFatSize,databuff);
for(i=0;i<512;i++) *(Em_DIR+i)=databuff[i];//处理根目录
}
void ListName(unsigned char *Em_DIR)
{
int i,j;
j=0;
for(;Em_DIR[j*32];) {
if(Em_DIR[j*32]==0xe5) {j++;continue;}
printuf("\n");for(i=0;i<11;i++) printuf("%x",Em_DIR[j*32+i]);
j++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -