📄 filesys.c
字号:
memcpy(p,DirName,strlen(DirName));
for(i=0;i<strlen(DirName);i++)
{
if((*p>=97)&&(*p<=122))
{
*p-=32;
p++;
}
}
return TRUE;
}
//////////////////////////////////////////////////////////////
BYTE OpenDir(const char *DirName)
{
U16 i,sum;
char bstop,sector,count,flag,tempname[256];
BYTE BUF[512];//,c
U32 sectorToRead;
PDIR_STRUCT pDir;
U32 dirSectorPointer,dirClusterPointer;
/////////////////////////////////////////////////////////////////
ThisFile.bOpen=0;
flag=0;
sum=0;
count=0;
bstop=0;
sector=0;
if(strlen(DirName)<9)
{
if(!GetShorDirForm(DirName,tempname))
{
printf("OpenDir错误01\n");
return FALSE;
}
tempname[11]='\0';
}
dirClusterPointer=ThisDir.StartCluster;
dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
for(;;)
{
/////////////////////////////////////////////////////////////
if(USB_Control.bIsFat32==0) //Is fat16
{
if(ThisDir.bRootDir) //Is Root directory
{
if(sector>DiskInfo.RootEntCnt>>4)
{
printf("opendir中的1错误\n");
return FALSE;
}
sectorToRead=ThisDir.StartSector+sector++;
}
else //Not Root directory
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0xfff8)
{
printf("opendir中的2错误\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
}
else //Is fat32
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0x0ffffff8)
{
printf("opendir中的3错误\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
//////////////////////////////////////////////////
if(!Read(sectorToRead,1,(BYTE *)BUF))
{
printf("opendir中的4错误%d\n",sectorToRead);
return FALSE;
}
pDir=(PDIR_STRUCT)BUF;
///////////////////////////////////////////////////
for(i=0;i<DiskInfo.BytesPerSec;pDir++,i=i+32)
{
// c=BUF[i];
if(BUF[i]==0x00)
{
// printf("opendir中的kong错误\n");
return FALSE;
}
///////////////////////////////////////////
// j=0;
if(BUF[i]==0xE5||((BUF[i+11]!=0X10)&&(strlen(DirName)<9)))
continue;
if(memcmp(pDir->DIR_Name,tempname,11)==0)
{
ThisFile.bOpen=1;
bstop=1;
break;
}
}
///////////////////////////////////////////////////////
if(bstop==1)break;
}
ThisDir.bRootDir=0;
ThisDir.DepthIndex++;
ThisDir.ParentStartCluster=ThisDir.StartCluster;
ThisDir.StartCluster=(pDir->DIR_FstClusHI<<16)|(pDir->DIR_FstClusLO);
ThisDir.Link[ThisDir.DepthIndex]=ThisDir.StartCluster;
///////////////////////////////////////////////////////
return TRUE;
}
BYTE CreateDir(const char * DirName)
{
U16 i;//j,
char bstop,sector,tempname[256];
BYTE BUF[512],flag,count;
WORD Fatbuf16[256];
DWORD Fatbuf32[128];
U32 sectorToRead,freeCluster,sector1;
DIR_STRUCT Dir;
U32 dirSectorPointer,dirClusterPointer;
U8 Cache[32768];
if(ThisDir.DepthIndex>MAXDIRDEPTH-1)
{
return FALSE;
}
for(i=0;i<DiskInfo.BytesPerSec*DiskInfo.SecPerClus;i++)
{
Cache[i]=0x00;
}
///////////////////////////////////////////////////////
freeCluster=SeekEmptyCluster();
if(freeCluster==0xffffffff)
{
printf("Createdir:错误0\n");
return FALSE;
}
Cache[0]=0x2E;
memset(Cache+1,0x20,10);
Cache[11]=0x10;
Cache[20]=((freeCluster>>16)|0x00FF);
Cache[21]=freeCluster>>24;
Cache[26]=freeCluster&0xFF;
Cache[27]=freeCluster&0xFF00;
/////////////////////////////////////////////////////
Cache[32+0]=0x2E;
Cache[32+1]=0X2E;
memset(Cache+32+2,0X20,9);
Cache[32+11]=0X10;
Cache[32+20]=(ThisDir.ParentStartCluster>>16)|0x00fFF;
Cache[32+21]=ThisDir.ParentStartCluster>>24;
Cache[32+26]=ThisDir.ParentStartCluster&0xff;
Cache[32+27]=ThisDir.ParentStartCluster&0xff00;
///////////////////////////////////////////////////////////
sector1=ClusterToSec(freeCluster);
if(!Write(sector1,1,Cache))
{
printf("Createdir:错误1,%d\n",sector1);
return FALSE;
}
//////////////////////////////////////////////////
if(USB_Control.bIsFat32==0)
{
if(!Read((freeCluster*2/DiskInfo.BytesPerSec)+DiskInfo.FatStartSector,1,(BYTE *)Fatbuf16))
{
return FALSE;
}
Fatbuf16[(freeCluster%256)]=0xffff;
if(!Write((freeCluster*2/DiskInfo.BytesPerSec)+DiskInfo.FatStartSector,1,(BYTE *)Fatbuf16))
{
return FALSE;
}
}else
{
if(!Read((freeCluster*4/DiskInfo.BytesPerSec)+DiskInfo.FatStartSector,1,(BYTE *)Fatbuf32))
{
return FALSE;
}
Fatbuf32[(freeCluster%128)]=0x0FFFFFFF;
if(!Write((freeCluster*4/DiskInfo.BytesPerSec)+DiskInfo.FatStartSector,1,(BYTE *)Fatbuf32))
{
return FALSE;
}
}
///////////////////////////////////////////////////////////////////////////////////
memset(&Dir,0,32);
//////////////////////////////////////////////////////////
sector=0;
ThisFile.bOpen=0;
count=0;
flag=0;
bstop=0;
dirClusterPointer=ThisDir.StartCluster;
dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
while(1)
{
///////////////////////////////////////////////////
if(USB_Control.bIsFat32==0)
{
if(ThisDir.bRootDir) //Is Root directory
{
if(sector>DiskInfo.RootEntCnt>>4)
{
printf("Createdir:错误2,%d\n",sectorToRead);
return FALSE;
}
sectorToRead=ThisDir.StartSector+sector++;
}
else //Not Root directory
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0xfff8)
{
// To do:
printf("Createdir:错误3\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
}
else //Is FAT32
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0x0ffffff8)
{
printf("Createdir:错误3\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
//////////////////////////////////////////////////
if(!Read(sectorToRead,1,BUF))
{
printf("Createdir:错误4,%d,\n",sectorToRead);
return FALSE;
}
///////////////////////////////////////////////////
for(i=0;i<DiskInfo.BytesPerSec;i=i+32)
{
if((flag!=0)||(BUF[i]==0x00)||(BUF[i]==0xE5))
{
if(strlen(DirName)<9)
{
if(!(GetShorDirForm(DirName,tempname)))
{
printf("CreateDir:错误7\n");
return FALSE;
}
Dir.DIR_FstClusHI=freeCluster>>16;
Dir.DIR_FstClusLO=freeCluster&0xffff;
Dir.DIR_Attr=0x10;
memcpy((char *)Dir.DIR_Name,tempname,11);
memcpy((char *)BUF+i,(char *)&Dir,32);
if(!Write(sectorToRead,1,BUF))
{
printf("Createdir:错误8,%d\n",sectorToRead);
return FALSE;
}
///////////////////////////////////////////////////////
bstop=1;
break;
}
}
}//end for
if(bstop==1)break;
}//end while
return TRUE;
}
BYTE ExitDir(void)
{
if(ThisDir.bRootDir)
return FALSE;
ThisFile.bOpen=0;
// if not root directory
if(USB_Control.bIsFat32==0) // Is fat16
{
ThisDir.DepthIndex--;
if(ThisDir.DepthIndex==0)
{
ThisDir.bRootDir=1;
ThisDir.ParentStartCluster=0;
ThisDir.StartCluster=0;
ThisDir.StartSector=DiskInfo.RootStartSector;
}
else if(ThisDir.DepthIndex==1)
{
ThisDir.bRootDir=0;
ThisDir.StartCluster=ThisDir.ParentStartCluster;
ThisDir.ParentStartCluster=0;
}
else
{
ThisDir.bRootDir=0;
ThisDir.StartCluster=ThisDir.ParentStartCluster;
ThisDir.ParentStartCluster=ThisDir.Link[ThisDir.DepthIndex-1];
}
}
else //Is fat32
{
ThisDir.DepthIndex--;
if(ThisDir.DepthIndex==0)
{
ThisDir.bRootDir=1;
ThisDir.ParentStartCluster=0;
ThisDir.StartCluster=ThisDir.Link[ThisDir.DepthIndex];
}
else
{
ThisDir.bRootDir=0;
ThisDir.StartCluster=ThisDir.ParentStartCluster;
ThisDir.ParentStartCluster=ThisDir.Link[ThisDir.DepthIndex-1];
}
}
return TRUE;
}
BYTE List(WORD *Count, BYTE *Cache)
{
U16 i,n;
U8 BUF[512],sector;
U32 sectorToRead;
U32 dirSectorPointer,dirClusterPointer;
/////////////////////////////////////////////////////////////////
sector=0;
*Count=0;
n=0;
ThisFile.bOpen=0;
dirClusterPointer=ThisDir.StartCluster;
dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
for(;;)
{
/////////////////////////////////////////////////
if(USB_Control.bIsFat32==0) //Is fat16
{
if(ThisDir.bRootDir) //Is Root directory
{
if(sector>DiskInfo.RootEntCnt>>4)
{
printf("list中的1错误\n");
return FALSE;
}
sectorToRead=ThisDir.StartSector+sector++;
}
else //Not Root directory
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0xfff8)
{
printf("list中的2错误\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
}
else //Is fat32
{
if(sector>DiskInfo.SecPerClus-1)
{
dirClusterPointer=GetNextCluster(dirClusterPointer);
if(dirClusterPointer>0x0ffffff8)
{
printf("list中的3错误\n");
return FALSE;
}
dirSectorPointer=ClusterToSec(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
//////////////////////////////////////////////////
if(!Read(sectorToRead,1,BUF ) )
{
printf("list中的4错误,%d\n",sectorToRead);
return FALSE;
}
///////////////////////////////////////////////////
for(i=0;i<DiskInfo.BytesPerSec;i=i+32)
{
////////////////////////////////////////////////
if(BUF[i]==0x00)
{
return TRUE;
}
if((BUF[i]==0xE5)||(BUF[i+11]==0x0f)||(BUF[i]==0x2E))
continue;
memcpy(Cache+n,BUF+i,32);
n=n+32;
*Count+=1;
}
}//end for
return TRUE;
}
BYTE Rootdir(void)
{
ThisFile.bOpen=0;
if(USB_Control.bIsFat32==0)
{
ThisDir.DepthIndex=0;
ThisDir.bRootDir=1;
ThisDir.ParentStartCluster=0;
ThisDir.StartCluster=0;
ThisDir.StartSector=DiskInfo.RootStartSector;
}
else
{
ThisDir.DepthIndex=0;
ThisDir.bRootDir=1;
ThisDir.ParentStartCluster=0;
ThisDir.StartCluster=DiskInfo.RootClusterNum;
}
return TRUE;
}
BYTE Getcapacity(DWORD * Total,DWORD *Free)
{
WORD Cache16[512],j,i;
DWORD Cache32[512];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -