?? file.#1
字號:
//************************************************************************
//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;i<SimpleBpb.FATSz;i++)
{
if(!Read(FatParameter.FirstFatSecNum+i,1,(BYTE *)Cache))
return FALSE;
for(j=0;j<256;j++)
{
if(Cache[j]==0)
return (i*256+j);
}
}
return 0xffff;
//************************************************************************
/* for(i=Cluster;i<SimpleBpb.BytsPerSec * SimpleBpb.FATSz / sizeof(WORD);i++)
{
OffSet=i%(SimpleBpb.BytsPerSec/2);
if(OffSet==0)
{
if(!Read((i*2/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum,1,(BYTE *)Cache))
return 0xffff;
}
if(Cache[OffSet]==0)
{
Cluster=i;
return Cluster;
}
}//*end for cycle
//************************************************************************
for(i=2;i<Cluster;i++)
{
OffSet=i%(SimpleBpb.BytsPerSec/2);
if(!Read(i*2/SimpleBpb.BytsPerSec+FatParameter.FirstFatSecNum,1,(BYTE *)Cache))
return 0xffff;
if(Cache[OffSet]==0x0)
{
Cluster=i;
return Cluster;
}
}//*end for cycl
return 0xffff;*/
}
WORD LinkClusterList(WORD Cluster)
{
static WORD VariedCluster=2;
WORD i,OffSet,FixedSec,UpdateSec,j;
BYTE p;
//////////////////////////////////////////////////////////////////////////////////
if(VariedCluster==2)
{ FatCache[0][256]=0;
// FatCache[1][256]=0;
VariedCluster=3;
}
//VariedCluster=Cluste
FixedSec=Cluster*2/SimpleBpb.BytsPerSec;
if(FatCache[0][256]!=FixedSec+FatParameter.FirstFatSecNum)
{ FatCache[0][256]=0;
// if(FatCache[1][256])
// if(!Write(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0],TRUE))
// return FALSE;
if(!Read(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0]))
return FALSE;
}
for(i=FixedSec;i<SimpleBpb.FATSz;i++)
{
if(i!=FixedSec)
{
p=1;
FatCache[0][256]=0;
if(!Read(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[1]))
return FALSE;
}
else{
p=0;
FatCache[0][256]=FixedSec+FatParameter.FirstFatSecNum;
}
for(j=0;j<256;j++)
{
if(FatCache[p][j]==0)
{// if(i==FixedSec)
// { p=0;
// FatCache[0][256]=FixedSec+FatParameter.FirstFatSecNum;
// }
//else{
// p=1;
// FatCache[0][256]=0;
// }
FatCache[p][j]=0xffff;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]=WordSwap((i*256+j));
if(i!=FixedSec)
{ if(!Write(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0],TRUE))
return FALSE;
if(!Write(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[p],TRUE))
return FALSE;
}
return (i*256+j);
}
}
}
///////////////////////////////////////////////////////////////////////////
FixedSec=Cluster*2/SimpleBpb.BytsPerSec;
if(!Read(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0]))
return FALSE;
for(i=0;i<FixedSec;i++)
{
if(!Read(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[1]))
return FALSE;
for(j=0;j<256;j++)
{
if(FatCache[1][j]==0)
{
FatCache[1][j]=0xffff;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]=WordSwap((i*256+j));
if(!Write(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0],TRUE))
return FALSE;
if(!Write(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[p],TRUE))
return FALSE;
return (i*256+j);
}
}
}
return 0Xffff;
/* FixedSec=Cluster*2/SimpleBpb.BytsPerSec;
if(!Read(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0]))
return FALSE;
for(i=FixedSec;i<SimpleBpb.FATSz;i++)
{
if(!Read(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[1]))
return FALSE;
for(j=0;j<256;j++)
{
if(FatCache[1][j]==0)
{ if(i==FixedSec)
p=0;
else p=1;
FatCache[p][j]=0xffff;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]=WordSwap((i*256+j));
if(!Write(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0],TRUE))
return FALSE;
if(!Write(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[p],TRUE))
return FALSE;
return (i*256+j);
}
}
}
for(i=0;i<FixedSec;i++)
{
if(!Read(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[1]))
return FALSE;
for(j=0;j<256;j++)
{
if(FatCache[1][j]==0)
{ if(i==FixedSec)
p=0;
else p=1;
FatCache[p][j]=0xffff;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]=WordSwap((i*256+j));
if(!Write(FixedSec+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[0],TRUE))
return FALSE;
if(!Write(i+FatParameter.FirstFatSecNum,1,(BYTE *)FatCache[p],TRUE))
return FALSE;
return (i*256+j);
}
}
}
return 0Xffff;*/
///////////////////////////////////////////////////////////////////////////////
/* FixedSec=Cluster*2/SimpleBpb.BytsPerSec+FatParameter.FirstFatSecNum;
if(!Read(FixedSec,1,(BYTE *)FatCache[0]))
return FALSE;
for(i=VariedCluster; i<SimpleBpb.BytsPerSec * SimpleBpb.FATSz / sizeof(WORD); i++)
{
UpdateSec=i*2/SimpleBpb.BytsPerSec+FatParameter.FirstFatSecNum;
OffSet=i%(SimpleBpb.BytsPerSec/2);
if(FixedSec==UpdateSec)
{
p=0;
}
else
{
if(!Read(UpdateSec,1,(BYTE *)FatCache[1]))
return FALSE;
p=1;
}
///////////////////////////////////////////////////////////////////////////
if(FatCache[p][OffSet]==0)
{
FatCache[p][OffSet]=0xffff;
VariedCluster= i;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]= WordSwap(VariedCluster);
if(!p)
{
if(!Write(FixedSec,1,(BYTE *)FatCache[p],TRUE))
return FALSE;
return VariedCluster;
}
if(!Write(FixedSec,1,(BYTE *)FatCache[0],TRUE))
return FALSE;
if(!Write(UpdateSec,1,(BYTE *)FatCache[1],TRUE))
return FALSE;
return VariedCluster;
}
}//*----------------end for cycle
//////////////////////////////////////////////////////////////////////////////////
for(i=2;i<VariedCluster;i++)
{
UpdateSec=i*2/SimpleBpb.BytsPerSec+FatParameter.FirstFatSecNum;
OffSet=i%(SimpleBpb.BytsPerSec/2);
if(FixedSec==UpdateSec)
p=0;
else
{
if(!Read(UpdateSec,1,(unsigned char *)FatCache[1]))
return FALSE;
p=1;
}
if(FatCache[p][OffSet]==0)
{
FatCache[p][OffSet]=0xffff;
VariedCluster= i;
FatCache[0][Cluster%(SimpleBpb.BytsPerSec/2)]=WordSwap(VariedCluster);
if(!p)
{
if(!Write(FixedSec,1,(unsigned char *)FatCache[p],TRUE))
return FALSE;
return VariedCluster;;
}
if(!Write(FixedSec,1,(unsigned char *)FatCache[0],TRUE))
return FALSE;
if(!Write(UpdateSec,1,(unsigned char *)FatCache[1],TRUE))
return FALSE;
return VariedCluster;;
}
}//----------------------------end for cycle
/////////////////////////////////////////////////////////////////////////////////
return 0xffff;*/
}
WORD GetListCluster(WORD Cluster)
{
WORD Fatbuf[256];
//////////////////////////////////////////////////////////////////////////////////
if(!Read(2*Cluster/SimpleBpb.BytsPerSec+FatParameter.FirstFatSecNum,1,(BYTE *)Fatbuf))
return FALSE;
return WordSwap(Fatbuf[Cluster%(SimpleBpb.BytsPerSec/2)]);
}
WORD SecToCluster(DWORD Sector)
{
if(Sector < FatParameter.FirstDataSector)
return FALSE;
else
return (WORD)((Sector - FatParameter.FirstDataSector) / SimpleBpb.SecPerClus + 2);
}
DWORD ClusterToSec(WORD Cluster)
{
if(Cluster<2)
return (FatParameter.FirstDataSector-1);
return ((Cluster-2)*SimpleBpb.SecPerClus+FatParameter.FirstDataSector);
}
DWORD SeekSector(DWORD Sector, const char dirname[11], struct _FILE *file)
{
BYTE Cache[512];
struct _DIR *dir;
UINT j, i;
WORD temp,temp1;
///////////////////////////////////////////////////////////////////////////////////
for(i=0; i<SimpleBpb.RootEntCnt * sizeof(struct _DIR) / SimpleBpb.BytsPerSec; i++)
{
if(!Read(Sector,1,(BYTE *)Cache))
return 0xffffffff;
dir = (struct _DIR *)Cache;
//********************************************************************************
for(j=0; (dir->Name[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 代表了文件中該扇區(qū)中的位置(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; i<SimpleBpb.RootEntCnt * sizeof(struct _DIR) / SimpleBpb.BytsPerSec; i++)//total root dir sectors;
{
if(!Read(DirSectorNum,1,(BYTE *)Cache))
return FALSE;
/////////////////////////////////////////////////////////////////////
for(dir = (struct _DIR *)Cache; (BYTE*)dir < Cache + SimpleBpb.BytsPerSec; dir++)
{
if(dir->Name[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完成于桃龍源
//*************************************************************************
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -