www.pudn.com > UDISK.rar > FILE.C
//************************************************************************
//author:dragon
//web:8dragon.com
//2004.2.5完成于桃龙源
//*************************************************************************
#include "AT89X52.h"
#include "host_811.h"
#include "file.h"
#include "math.h"
#include "string.h"
#include "ufi.h"
struct _BPB SimpleBpb;
struct _FILE FileControl;
FAT_PARAMETER FatParameter;
WORD FatCache[2][256];
BYTE InitFsys()
{
BYTE buf[512];
UINT m_Offset;
DWORD DataSectors,TotalClusters;
///////////////////////////////////////////////////////////////////////
DelayMs(100);
if(Read(0,1,(BYTE *)buf)==FALSE)
return FALSE;
DelayMs(100);
if(buf[0x00]==0xEB ||buf[0x02]==0x90)
m_Offset=0;
else
m_Offset=ConvTwoBytes((buf+0x1c6));
if(Read(0+m_Offset,1,(BYTE *)buf)==FALSE)
return FALSE;
SimpleBpb.BytsPerSec =ConvTwoBytes((&buf[11]));
SimpleBpb.SecPerClus = buf[13];
SimpleBpb.RsvdSecCnt =ConvTwoBytes((&buf[14]));
SimpleBpb.NumFATs = buf[16];
SimpleBpb.RootEntCnt = ConvTwoBytes((&buf[17]));
if((SimpleBpb.TotSec = ConvTwoBytes((&buf[19])))==0)
SimpleBpb.TotSec = ConvFourBytes((&buf[32]));
if((SimpleBpb.FATSz = ConvTwoBytes((&buf[22])))==0)
SimpleBpb.FATSz =ConvFourBytes((&buf[36]));
//------------------------------------------------------------------------
//Compute the corresponding data for Fat information;
//------------------------------------------------------------------------
FatParameter.RootDirSectors= ((SimpleBpb.RootEntCnt * 32) + (SimpleBpb.BytsPerSec - 1)) / SimpleBpb.BytsPerSec;
DataSectors = SimpleBpb.TotSec - (SimpleBpb.RsvdSecCnt + (SimpleBpb.NumFATs * SimpleBpb.FATSz) + FatParameter.RootDirSectors);
TotalClusters = DataSectors / SimpleBpb.SecPerClus;
//判断文件格式是否为fat16;
if(TotalClusters < 4085 || TotalClusters >= 65525)
return FALSE;
FatParameter.FirstFatSecNum=SimpleBpb.RsvdSecCnt+m_Offset;
FatParameter.FirstRootDirSecNum = SimpleBpb.RsvdSecCnt + (SimpleBpb.NumFATs * SimpleBpb.FATSz)+m_Offset;
FatParameter.FirstDataSector = SimpleBpb.RsvdSecCnt + (SimpleBpb.NumFATs * SimpleBpb.FATSz) + FatParameter.RootDirSectors+m_Offset;
return TRUE;
}
WORD SeekEmptyCluster()
{
// static WORD Cluster=2;//加快寻找进度
WORD i,Cache[256],OffSet,j;
// WORD i,Cache[256],OffSet;
for(i=0;iName[0] != '\0') && (j< SimpleBpb.BytsPerSec / sizeof(struct _DIR)); j++)
{
if(memcmp((const char*)dir->Name, dirname, 11)== 0)
{
if(file != NULL)
{
memset(file, 0, sizeof(struct _FILE));
file->DirSectorNum = Sector;
file->DirIndex = j; //j 代表了文件中该扇区中的位置(0-15);
memcpy(&file->dir, dir, sizeof(struct _DIR));
}
temp=WordSwap(dir->FstClusLO);
temp1=ClusterToSec(temp);
return temp1;
}
dir++;
}//end for cycle
Sector++;
}//end for cycle
return 0xffffffff;
}
DWORD LocateFile(const char *filename, struct _FILE *file)
{
DWORD Sector = FatParameter.FirstRootDirSecNum;
char NewFile[12];
char *p=NewFile;
///////////////////////////////////////////////////////////////////////////////
if(!CheckFileName(filename,p))
return FALSE;
Sector = SeekSector(Sector, p, file);
if(*(p+11)==NULL)
return Sector;
return 0Xffffffff;
}
BYTE LocateDir(struct _DIR* new_dir, struct _FILE * fp)
{
BYTE Cache[512];
struct _DIR *dir;
DWORD i;
DWORD DirSectorNum = FatParameter.FirstRootDirSecNum;
////////////////////////////////////////////////////////////////////////////////
for(i=0; iName[0] == '\0'||dir->Name[0] == 0xE5)
{
memcpy(dir, new_dir, sizeof(struct _DIR));
if(!Write(DirSectorNum,1,Cache,TRUE))
if(!Write(DirSectorNum,1,Cache,TRUE))
return FALSE;
if(fp)
{
fp->DirSectorNum = DirSectorNum;
fp->DirIndex = ((BYTE*)dir - Cache)/sizeof(struct _DIR);
memcpy(&fp->dir, new_dir, sizeof(struct _DIR));
}
return TRUE;
}
}//--------------------------end for cycle
DirSectorNum++;
}//-------------------------------end for cycle
return FALSE;
}
BYTE CheckFileName(const char * oldfilename, char newfilename[11])
{
char* p=newfilename;
char path[12];
char* ppath = path;
int dir_len_count; //count dir len.
int i;
////////////////////////////////////////////////////////////////////////////////////
if(oldfilename==NULL || strlen(oldfilename) >=12)
return FALSE;
dir_len_count=0;
strcpy(path, oldfilename);
//变小写字母为大写
for(i=0;(*ppath!=NULL&&i<12);i++)
{
if( *ppath > 96 && *ppath < 123)
*ppath -= 32;
ppath++;
}
memset(p, 0, 12);
ppath = path;
for(;;)
{
switch(*ppath)
{
case 0:
{
if(dir_len_count>=11)
return TRUE;
else
{
dir_len_count ++;
*p = 0x20;
p++;
}
continue;
}
////////////////////////////////////////////////////////////////////////////////
case '.':
{
if(dir_len_count > 8 || dir_len_count ==0)
{
return FALSE;
}
if(ppath[1] == '.')
{
return FALSE;
}
for(i=0; i<(8 - dir_len_count); i++)
{
*p = 0x20;
p++;
}
dir_len_count =8;
ppath++;
continue;
break;
}
/////////////////////////////////////////////////////////////////////////////////
case 0x22:
case 0x2A:
case 0x2B:
case 0x2C:
case 0x2F:
case 0x3A:
case 0x3B:
case 0x3C:
case 0x3D:
case 0x3E:
case 0x3F:
case 0x5B:
case 0x5D:
case 0x7C:
return FALSE;
/////////////////////////////////////////////////////////////////////////////////
default:
{
if(*ppath < 0x20)
return FALSE;
break;
}
////////////////////////////////////////////////////////////////////////////////
}
*p = *ppath;
dir_len_count ++;
if(dir_len_count >= 11)
return TRUE;
p++;
ppath++;
}//*end for cycle
return FALSE;
}
//************************************************************************
//author:dragon
//web:8dragon.com
//2004.2.5完成于桃龙源
//*************************************************************************