⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32.c

📁 ISP1362 Fat32 coldfire523x
💻 C
字号:
#include"Fat32.h"

extern uint32 DirStartCluster32, NowCluster32, NowSector;
extern uint8  DBUF[512];
extern SFATBPBINFO DeviceInfo;
extern SFILEINFO   ThisFile;
extern SUDEV  uDev;
extern SFLAGS bFlags;
uint8 FATBUF[512];

uint8 ChkSum (uint8 * pFcbName)
{
	uint32 loop;
	uint8 Sum = 0;		 

	for (loop = 0; loop < 11; loop++)
	{
		/* The operation is an unsigned char rotate right */
		Sum = ((Sum & 1) ? 0x80 : 0) + (Sum >> 1) + *(pFcbName + loop);
	}
	
	return Sum;
}

uint16 Hash(uint8 * string, uint16 len)
{
	uint16 keyad = 0;
	uint32 i;

	for(i = 0; i < len; i++)
	{
		keyad += (*(string + i) & 0xFF);
		keyad += (*(string + i) & 0xF0);
	}

	return keyad;
}

uint8 ScanFDT(uint8 * pName)
{
	uint32 i = 0, j;
	uint8 sector;
	
	NowCluster32 = DirStartCluster32;		
	do
	{
		NowSector = FirstSectorofCluster32(NowCluster32);
		for(sector = 0; sector < DeviceInfo.BPB_SecPerClus; sector++)
	    {   
			if(! ReadSector(DBUF, (NowSector + sector), 1))
			{
				return FALSE;				
			}

			for(i = 0; i < DeviceInfo.BPB_BytesPerSec; i = i + 32)
			{
				if(DBUF[i] == 0x00)
					return FALSE;

				j = 0;
				while(DBUF[i + j] == *(pName + j))
				{
					j = j + 1;
					if(j > 10)
					{
						return TRUE;
					}
				}
			}
		}

		NowCluster32 = GetNextClusterNum32(NowCluster32);
					
	}while(NowCluster32 <= DeviceInfo.TotCluster);
		
	if(NowCluster32 > DeviceInfo.TotCluster)
	{
		return FALSE;
	}

	return FALSE;  	
}

uint32 MakeLongEntry(uint8 * pLong, uint8 * pBuffer, uint8 sum)
{
	uint8  *pUnicode;
	uint16 pLongName[256];
	uint32 i = 0, k, sflag;
	uint32 len = 1, LastBytes, DirEntCount;

	do
	{
		len++;
		i++;
	}while(*(pBuffer + i) != 0);

	len = len * 2;
	DirEntCount = len / 26;
	LastBytes = len % 26;
	if(LastBytes)
	{								   
		DirEntCount += 1;
	}
	/* 生成UNICODE码 */
	for(i = 0; i < (len >> 1); i++)
	{
		*(pLongName + i) = WordSwap((uint16)(*(pBuffer + i))); 
	}
							 
	pUnicode = (uint8 *)pLongName;	
	for(i = 0, sflag = len - LastBytes; DirEntCount > 0; i += 32, DirEntCount--, sflag -= 26)
	{
		if(LastBytes > 0)
		{
			*(pLong + i) = (DirEntCount | LAST_LONG_ENTRY);
			*(pLong + i + 11) = 0x0F;
			*(pLong + i + 12) = 0x00;
			*(pLong + i + 13) = sum;
			*(pLong + i + 26) = 0x00;
			*(pLong + i + 27) = 0x00;
			if(LastBytes <= 10)
			{
				for(k = 0; k < LastBytes; k++)
				{
					*(pLong + i + k + 1) = *(pUnicode + sflag + k);
				}
				for(k = LastBytes; k < 10; k++)
				{
					*(pLong + i + k + 1) = 0xff;
				}
				for(k = 10; k < 22; k++)
				{
					*(pLong + i + k + 4) = 0xff;
				}
				for(k = 22; k < 26; k++)
				{
					*(pLong + i + k + 6) = 0xff;
				}
			}
			if(LastBytes > 10 && LastBytes <= 22)
			{
				for(k = 0; k < 10; k++)
				{
					*(pLong + i + k + 1) = *(pUnicode + sflag + k);
				}
				for(k = 10; k < LastBytes; k++)
				{
					*(pLong + i + k + 4) = *(pUnicode + sflag + k);
				}
				for(k = LastBytes; k < 22; k++)
				{
					*(pLong + i + k + 4) = 0xff;
				}
				for(k = 22; k < 26; k++)
				{
					*(pLong + i + k + 6) = 0xff;
				}
			}
			if(LastBytes > 22 && LastBytes <= 26)
			{
				for(k = 0; k < 10; k++)
				{
					*(pLong + i + k +1) = *(pUnicode + sflag + k);
				}
				for(k = 10; k < 22; k++)
				{
					*(pLong + i + k + 4) = *(pUnicode + sflag + k);
				}
				for(k = 22; k < LastBytes; k++)
				{
					*(pLong + i + k + 6) = *(pUnicode + sflag + k);
				}
				for(k = LastBytes; k < 26; k++)
				{
					*(pLong + i + k + 6) = 0xff;
				}
			}
			LastBytes = 0;
			
		}
		else
		{
			*(pLong + i) = DirEntCount;
			for(k = 0; k < 10; k++)
			{
				*(pLong + i + k + 1) = *(pUnicode + sflag + k);
			}
			*(pLong + i + 11) = 0x0F;
			*(pLong + i + 12) = 0x00;
			*(pLong + i + 13) = sum;
			for(k = 10; k < 22; k++)
			{
				*(pLong + i + k + 4) = *(pUnicode + sflag + k);
			}
			*(pLong + i + 26) = 0x00;
			*(pLong + i + 27) = 0x00;
			for(k = 22; k < 26; k++)
			{
				*(pLong + i + k + 6) = *(pUnicode + sflag + k);
			}
		}
	}
	
	return i;
}

uint8 MakeShortEntry(uint8 *FileName, uint8 *ph, uint8 DirFlag, uint8 *Timer)
{
	uint8 cp[64];
	uint32 i = 0,  len = 0, BefoDotBytes = 0;
	uint16 HashAddr = 0;
	uint8 nam[8] = {' ',' ',' ',' ',' ',' ',' ',' '};/*文件名*/
	uint8 ext[3] = {' ',' ',' '};/*扩展名*/
	uint16 DirCreatTime, DirCreatDate;

	/***********uint8 *ph;***********j = 0,*******, flag = 0************************/
	do
	{
		cp[i] = *(FileName + i);
		i++;
		len++;
		if(*(FileName + i) == '.')
		{
		    BefoDotBytes = len;
		}
	}while(*(FileName + i) != '\0');
	i = 0;
	do
	{   
		if (cp[i] >= 'a' && cp[i] <= 'z' )
	 	{
		      cp[i] -=  'a' - 'A'; 
		}
		i++;
	}while(	cp[i] != 0);
	/*****************************************************/
	if(DirFlag)
	{
		for(i = 0; i < 6; i++)
		{
			nam[i] = cp[i];
		}
		nam[i++] = '~';
		nam[i++] = '1';
		for(i = 0; i < 3; i++)
		{
			ext[i] = 0x20/*cp[len + i - 3]*/;
		}
	}
	else
	{
  	if(BefoDotBytes >  8)
  	{
  	  for(i = 0; i < 6; i++)
  		{
  			nam[i] = cp[i];
  			/*if(cp[i] == '.')
  			flag = i;	
  			break;*/
  		}
  		nam[i++] = '~';
  		nam[i++] = '1';
  	
  		for(i = 0; i < 3; i++)
  		{
  			ext[i] = cp[BefoDotBytes + i + 1]/*0x20*/;
  		}
  	}
  	else
  	{
  	  for(i = 0; i < 8; i++)
  		{
  			nam[i] = cp[i];
  			if(i >= BefoDotBytes)
  			{
          		nam[i] =0x20;
  			}      
  		}
  	
  		for(i = 0; i < 3; i++)
  		{
  			ext[i] = cp[BefoDotBytes + i + 1]/*0x20*/;
  		}
  	}	
	}

	for(i = 0; i < 8; i++)
	{
		*(ph + i) = nam[i];	
	}
	for(i = 8; i < 11; i++)
	{
		*(ph + i) = ext[i - 8];
	}		
	if(DirFlag)
	{
		*(ph + 11) = ATTR_DIRECTRORY;
	}
	else
	{
		*(ph + 11) = ATTR_ARCHIVE;	
	}
	*(ph + 12)	= 0x00;

 	*(ph + 13)	= Timer[6];
	DirCreatTime = ((uint16)Timer[3] << 11) | ((uint16)Timer[4] << 5) | ((uint16)Timer[5] >> 1);
	DirCreatDate = ((uint16)(Timer[0]+20) << 9 ) | ((uint16)Timer[1] << 5) | ((uint16)Timer[2]);

	*(ph + 14)	= (DirCreatTime & 0x00ff);
	*(ph + 15)	= (DirCreatTime >>8);
	*(ph + 16)	= (DirCreatDate & 0x00ff);
	*(ph + 17)	= (DirCreatDate >>8);
	*(ph + 18)	= (DirCreatDate & 0x00ff);
	*(ph + 19)	= (DirCreatDate >>8);
	
	*(ph + 22)	= (DirCreatTime & 0x00ff);
	*(ph + 23)	= (DirCreatTime >>8);
	*(ph + 24)	= (DirCreatDate & 0x00ff);
	*(ph + 25)	= (DirCreatDate >>8);
	
	for(i=28;i<32;i++)
	{
		*(ph + i)  = 0x00;
	}
	/**********************修正~n值****************************/
		for(i = 0; i < 5; i++)
		{
			if(ScanFDT(ph))
			{
				(*(ph + 7)) += 1;	
				if(i == 4)
				{						  
					HashAddr  = Hash(&cp[2], (len - 2));
					*(ph + 2) = cp[len - 2];
					*(ph + 3) = cp[len - 1];
					*(ph + 4) = (uint8)(HashAddr & 0x00FF);					  
					*(ph + 5) = (uint8)((HashAddr & 0xFF00) >> 8);
					*(ph + 7) = '1';
				}
			}
			else
			{
				break;
			}
		}
	/*for(i = 0; i < 32; i++)
	{
		*(pshort + i) = *(ph + i);
	} */

	return TRUE;
}


uint8 ShortEntSearchAddr(uint8 * pLongName, uint8 * pShortName, uint8 DirFlag)
{
	uint8 cp[256];
	uint32 i = 0, j = 0;
	
	do
	{
		cp[i] = *(pLongName + i);
		i++;
		j++;	
	}while(*(pLongName + i) != '\0');
	
	i = 0;
	do
	{   
		if (cp[i] >= 'a' && cp[i] <= 'z' )
	 	{
		      cp[i] -=  'a' - 'A'; 
		}
		i++;
	}while(	cp[i] != 0);

	if(DirFlag)
	{
		for(i = 0; i < 6; i++)
		{
			pShortName[i] = cp[i];
		}
		pShortName[i++] = '~';
		pShortName[i++] = '1';
		for(i = 0; i < 3; i++)
		{
			pShortName[i + 8] = 0x20/*cp[j + i - 3]*/;
		}
	}
	else
	{
		for(i = 0; i < 6; i++)
		{
			pShortName[i] = cp[i];
		}
		pShortName[i++] = '~';
		pShortName[i++] = '1';
	
		for(i = 0; i < 3; i++)
		{
			pShortName[i + 8] = 0x20/*cp[j + i - 3]*/;
		}
	}

	return TRUE;
}

uint32 FirstSectorofCluster32(uint32 clusterNum)
{
	uint32 temp;
	temp = clusterNum - 2;
	temp = temp * DeviceInfo.BPB_SecPerClus;
	temp = temp + DeviceInfo.FirstDataSector;

	return temp;
}

uint32 ThisFatSecNum32(uint32 clusterNum)
{
   uint32 temp;
   temp = clusterNum * 4;
   temp = temp / DeviceInfo.BPB_BytesPerSec;
   temp = temp + DeviceInfo.FatStartSector;

   return temp;
}
uint32 ThisFatEntOffset32(uint32 clusterNum)
{
	uint32 temp1,temp2;
	temp1 = 4 * clusterNum;
	temp2 = temp1 / DeviceInfo.BPB_BytesPerSec;
	temp1 = temp1 - temp2 * DeviceInfo.BPB_BytesPerSec;
	
	return temp1;
}

uint32 GetNextClusterNum32(uint32 clusterNum)
{
	uint32 FatSecNum,FatEntOffset;
	/*uint8 FATBUF[512];*/
	
	FatSecNum = ThisFatSecNum32(clusterNum);
	FatEntOffset = ThisFatEntOffset32(clusterNum);
	if(ThisFile.FatSectorPointer != FatSecNum)
	{			
		if(! ReadSector(FATBUF, FatSecNum, 1))
			return 0xFFFFFFFF;
		ThisFile.FatSectorPointer = FatSecNum;
	}
	
	/*clusterNum = LSwapuint32(DBUF[FatEntOffset], DBUF[FatEntOffset+1], DBUF[FatEntOffset+2], DBUF[FatEntOffset+3]);*/
	clusterNum = LSwapuint32(FATBUF[FatEntOffset], FATBUF[FatEntOffset+1], FATBUF[FatEntOffset+2], FATBUF[FatEntOffset+3]);

	return clusterNum;
}

uint8 GoToPointer32(uint32 pointer)
{
  uint32 clusterSize;

  clusterSize = DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
  ThisFile.ClusterPointer = ThisFile.StartCluster;
  while(pointer>=clusterSize) //add pzx 081223
  {
    pointer -= clusterSize;	
    ThisFile.ClusterPointer = GetNextClusterNum32(ThisFile.ClusterPointer);
    if(ThisFile.ClusterPointer == 0xffffffff)
    {
      return FALSE;
    }
  }
  ThisFile.SectorofCluster = pointer / DeviceInfo.BPB_BytesPerSec % DeviceInfo.BPB_SecPerClus;
  ThisFile.SectorPointer = FirstSectorofCluster32(ThisFile.ClusterPointer) + ThisFile.SectorofCluster;
  ThisFile.OffsetofSector = pointer - ThisFile.SectorofCluster * DeviceInfo.BPB_BytesPerSec;
//  ThisFile.FatSectorPointer = 0;  //delete 081223 原因:查找偏移数据不正确
  return TRUE;	
}

uint8 DeleteClusterLink32(uint32 clusterNum)
{
	uint32 FatSecNum,FatEntOffset;
	uint32 i;
	while((clusterNum>1)&&(clusterNum<DeviceInfo.TotCluster))
	{
		FatSecNum=ThisFatSecNum32(clusterNum);
		FatEntOffset=ThisFatEntOffset32(clusterNum);
		if(ReadSector(DBUF, FatSecNum, 1))
			clusterNum=LSwapuint32(DBUF[FatEntOffset],DBUF[FatEntOffset+1],DBUF[FatEntOffset+2],DBUF[FatEntOffset+3]);
		else
			return FALSE;
		DBUF[FatEntOffset]=0x00;DBUF[FatEntOffset+1]=0x00;DBUF[FatEntOffset+2]=0x00;DBUF[FatEntOffset+3]=0x00;			
		for(i=0;i<DeviceInfo.BPB_NumFATs;i++)
		{
			if(!WriteSector(DBUF, FatSecNum + (i * DeviceInfo.BPB_FATSz32), 1))
				return FALSE;
		}	
	}
	return TRUE;
}

uint32 GetFreeClusterNum32(void)
{
	uint32 clusterNum,i;
	uint32 sectorNum;
	uint32 j;

	clusterNum = 0;
	sectorNum = DeviceInfo.FatStartSector;

	while(sectorNum < (DeviceInfo.BPB_FATSz32 + DeviceInfo.FatStartSector))
	{		
		if(! ReadSector(DBUF, sectorNum, 1))
			return FALSE;

		for(i = 0; i < DeviceInfo.BPB_BytesPerSec; i = i + 4)
		{									
			if((DBUF[i] == 0) && (DBUF[i + 1] == 0) && (DBUF[i + 2] == 0) && (DBUF[i + 3] == 0))
			{	
		  		DBUF[i] = 0xff; DBUF[i + 1] = 0xff; DBUF[i + 2] = 0xff; DBUF[i + 3] = 0xff;
				for(j = 0; j < DeviceInfo.BPB_NumFATs; j++)
				{
					WriteSector(DBUF, (sectorNum + (j * DeviceInfo.BPB_FATSz32)), 1);
				}
		  		return	clusterNum; 
			}
			clusterNum++;
		}					
		sectorNum = ((4 * clusterNum ) / DeviceInfo.BPB_BytesPerSec) + DeviceInfo.FatStartSector;	
	}	
	return 0x0;
}

uint32 CreateClusterLink32(uint32 currentCluster)
{
	uint32 newCluster;
	uint32 FatSecNum, FatEntOffset;
	uint8 i;

	newCluster = GetFreeClusterNum32();
		
	FatSecNum = ThisFatSecNum32(currentCluster);
	FatEntOffset = ThisFatEntOffset32(currentCluster);

	if(ReadSector(DBUF, FatSecNum, 1))
	{
		DBUF[FatEntOffset]   = newCluster;
		DBUF[FatEntOffset+1] = newCluster >> 8;
		DBUF[FatEntOffset+2] = newCluster >> 16;
		DBUF[FatEntOffset+3] = newCluster >> 24;

		for(i=0; i < DeviceInfo.BPB_NumFATs; i++)
		{
			WriteSector(DBUF, (FatSecNum + i * DeviceInfo.BPB_FATSz32), 1);
		}	
	}
	
	return newCluster;
}

uint8 ClrACluster(uint32 ClusterNum)
{
	        uint32 i = 0, sector = 0;
	        
	        /*************************把该簇擦掉***********************/
			NowSector = FirstSectorofCluster32(ClusterNum);
			for(i; i < DeviceInfo.BPB_BytesPerSec; i++)
			{
				DBUF[i]=0x00;
			}
			for(sector; sector < DeviceInfo.BPB_SecPerClus; sector++)
			{
				if(! WriteSector(DBUF, NowSector + sector, 1))
		  	 		return FALSE;
			}

            return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -