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

📄 fat.c

📁 FAT16源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
===============================================================================
函数(CORE_INIT_1)
从root sector BPB计算FirstDataSector,FirstRootDirSecNum,etc..
入口:无
出口: 无
===============================================================================
*/
static void BPB_INIT_2(void)
{  
  CORE.RootDirSectors = (BPB.boot_entries * 32 + (BPB.bytes_per_sector - 1)) / BPB.bytes_per_sector;                             
  //The start of the data region, the first sector of cluster 2                               
  CORE.FirstDataSector = CORE.relative_sector + BPB.reserved_sector + BPB.sectors_per_FAT
                               * BPB.numbers_of_FAT + CORE.RootDirSectors;
  CORE.FirstRootDirSecNum = CORE.relative_sector + BPB.reserved_sector+ BPB.sectors_per_FAT
                               * BPB.numbers_of_FAT;
  //////////////////////////////printf("CORE.total_sector:%ld",CORE.total_sector);
  CORE.DataSec = CORE.total_sector - BPB.reserved_sector - CORE.RootDirSectors
                 - BPB.sectors_per_FAT * BPB.numbers_of_FAT;
  CORE.CountofClusters = CORE.DataSec / BPB.sector_per_cluster;
  CORE.FirstSectorofFAT1 = CORE.relative_sector + BPB.reserved_sector;
  CORE.FirstSectorofFAT2 = CORE.relative_sector + BPB.reserved_sector + BPB.sectors_per_FAT;
}
/*
===============================================================================
函数
Read Partition PBP
入口:Partition ID
出口:无
===============================================================================
*/ 
static u8 Read_partition_PBP(u8 partition_ID)
{  
  u8 *ptr1,*ptr2; 
  u16 i; 
  u8 buf[512];  
   if ((CORE.PartitionID - 'C') ==  partition_ID) 
    { 
	  //Specific BPB is already readed in the buffer
      return(SUCC);
    }  
   read_flash_sector(buf,0); //read MBR 
   if ( buf[0x1be] == 0x00 || buf[0x1be] == 0x80) // check boot indicator 00 or 0x80
    {
     BPB_INIT_1(partition_ID,buf);
     //read DBR of the specific partition into buffer 
     if ( read_flash_sector(buf,CORE.relative_sector) ) 
      if ( buf[510] == 0x55 && buf[511] == 0xaa)
       {
        ptr1 = (u8 *)(&BPB.bytes_per_sector);
        ptr2 = buf + 0xb/*BPB position offset in MBR*/;         
        for (i = 0;i < sizeof(BPB); i ++)   //copy BPB(BIOS Parameter Block) to RAM buffer
          *(ptr1 + i) = *(ptr2 + i);
        BPB_INIT_2();
        return(SUCC);
       }     
     }
     else
     {
	   CORE.relative_sector = 0; 
       CORE.total_sector =  *((u32*) (buf+32));
       CORE.system_id = buf[0x1c2]; //Partition Type 0C-FAT32,06-FAT16 ect..
       CORE.PartitionID= 'C' + partition_ID; //从C开始到Z结束 
	   if ( buf[510] == 0x55 && buf[511] == 0xaa)
       {
		////////////////////////////////printf("aa");  
        //ptr1 = (u8*)BPB_.BUFFER;
        //ptr2 = buf + 0xb/*BPB position offset in MBR*/;         
        //for (i = 0;i < sizeof(BPB); i ++)   //copy BPB(BIOS Parameter Block) to RAM buffer
		//{
		//*(ptr1 + i) = *(ptr2 + i);
		////////////////////////////////printf("%d ",*(ptr2 + i));
		//}
  //u16 bytes_per_sector;//每扇区字节数
BPB.bytes_per_sector = buf[0xb] + buf[0xc] * 256;

//  u8 sector_per_cluster; //每簇扇区数
BPB.sector_per_cluster = buf[0xd];
//////////////////////////////printf("\nsector_per_cluster=%d ",BPB.sector_per_cluster);
//  u16 reserved_sector;  //保留扇区数
BPB.reserved_sector = buf[14] + buf[15] * 256;
//////////////////////////////printf("\nreserved_sector=%d ",BPB.reserved_sector);
//  u8 numbers_of_FAT;//FAT副本数
BPB.numbers_of_FAT = buf[16];
//////////////////////////////printf("\nnumbers_of_FAT=%d ",BPB.numbers_of_FAT);
//  u16 boot_entries;//根目录项数,供FAT12/16使用
BPB.boot_entries = buf[17] + buf[18] * 256;
 //////////////////////////////printf("\nboot_entries=%d ",BPB.boot_entries);

//  u16 TotSec16; //This field is the old 16-bit total count of sectors on the volume.
BPB.TotSec16 = buf[19] + buf[20] * 256;
BPB.TotSec16 = 0xffff;
 //////////////////////////////printf("\nTotSec16=%d ",BPB.TotSec16);

//  u8 media_descriptor; //媒体描述符
BPB.media_descriptor = buf[21];
//  u16 sectors_per_FAT; //每个FAT表占用的扇区数,供FAT12/16使用
BPB.sectors_per_FAT =  buf[22] + buf[23] * 256;
//////////////////////////////printf("\nsectors_per_FAT=%ld %d %d",BPB.sectors_per_FAT,buf[22],buf[23]);
//  u16 sectors_per_track; //每道扇区数
//  u16 number_of_head; //磁头数
//  u32 BPB_HiddSec; //隐藏扇区数
//  u32 BPB_TotSec32;//总扇区数,包含FAT32总扇区数
//  u8 BS_DrvNum;
//  u8 BS_Reserved1;
//  u8 BS_BootSig;
//  u32 BS_VolID;
//  u8 BS_VolLab[11];
//  u8 BS_FilSysType[8];
CORE.system_id = 0x6; //Partition Type 0C-FAT32,06-FAT16 ect..

         
	    //////////////////////////////printf("\n%d ",BPB.bytes_per_sector);
        BPB_INIT_2();
        return(SUCC);
       } 
	 }
    
  return(FAIL);
}  

/*
===============================================================================
函数
从路径中读一个entry
入口:
出口:SUCC,FAIL
===============================================================================
*/
static u8 SplitNameFromPath(u8 *Path,u8 *Return_Entry_Name,u8 *FileExtension,u8 *RemovedCharsFromPath)
{  
  u8 i,flag,j;
  flag = FILE_NAME;
  *RemovedCharsFromPath = 0; 
  CORE.CurPathType = DirectoryPath; 
  i = 0; 
  j = 0;
  do{           
     if( ( * Path) != 0 && ( * Path ) != '\\') //Path分离得到Entry name and file extension 
       { 
        (*RemovedCharsFromPath)++; 
        if( flag == FILE_NAME)
         {            
          if(*Path == '.')
             {
              flag  = FILE_EXTENSION;
              Path ++; 
             }  
          else
            {
             Return_Entry_Name[i] =  *Path;
             i++; 
             Path++;
            }
          }
        else if( flag  == FILE_EXTENSION)
         {
          if( j >= 3 )
            return(FAIL);
          FileExtension[j] =  *Path;
          j++;
          Path++;
         } 
      }
    else
      {
       if(!( * Path))
        {
          if(CORE.FullPathType == FilePath)
		  {
		  CORE.CurPathType = FilePath;
		  //////////////////////////////printf("CORE.CurPathType = FilePath;");
		  }
		  //CORE.CurPathType = FilePath;
          FileExtension[j] = 0;
          Return_Entry_Name[i] = 0;
          return(LastSplitedNameofPath);  
        } 
       (*RemovedCharsFromPath)++; 
       FileExtension[j] = 0;
       Return_Entry_Name[i] = 0;	   
       break;
      }
 }while(1); 
 return(SUCC);
} 
/*
===============================================================================
函数
Directory Entry offset+32 
入口:buf--Current Sector Buffer
出口:SUCC,FAIL
===============================================================================
*/             
static u8 CORE_offset_add_32(u8 *buf)
{
  CORE.offset += 32;
  if (CORE.offset >= 512)
  {
	  ////////////////printf("CORE.DirectoryType = %d",CORE.DirectoryType);
  if (CORE.DirectoryType == RootDirectory)
   {
        if (CORE.SectorNum < ( CORE.RootDirSectors +  CORE.FirstRootDirSecNum))
         {
           CORE.SectorNum++;
           CORE.offset = 0; 
           read_flash_sector(buf,CORE.SectorNum);
		   if(buf[CORE.offset] == 0 && buf[CORE.offset+1] == 0)  //End of the directory
		     return(FAIL);
           return(SUCC);
         }
        else
           return(FAIL);
     }
    else  
     {
        if( (CORE.SectorNum - FirstSectorofCluster(CORE.ClusterNum) + 1) >= BPB.sector_per_cluster)
         {
           CORE.ClusterNum = Get_Next_Cluster_From_Current_Cluster(CORE.ClusterNum);
           if( CORE.ClusterNum >= 2 && CORE.ClusterNum <= 0xfff7)
            {
               CORE.SectorNum = FirstSectorofCluster(CORE.ClusterNum); 
               CORE.offset = 0;
               read_flash_sector(buf,CORE.SectorNum);
			   if(buf[CORE.offset] == 0 && buf[CORE.offset+1] == 0)  //End of the directory
                   return(FAIL);
               return(SUCC);
            }
           else
                return(FAIL);
         }
        else
         {
            CORE.SectorNum++; 
            CORE.offset = 0;
            read_flash_sector(buf,CORE.SectorNum);
			if(buf[CORE.offset] == 0 && buf[CORE.offset+1] == 0)  //End of the directory
                return(FAIL);
            return(SUCC);
         }
     }
  }
  if(buf[CORE.offset] == 0 && buf[CORE.offset+1] == 0)  //End of the directory
       return(FAIL);
  return(SUCC);
}
/*
===============================================================================
函数
从目录读一个EntryWith 8.3 Name
入口:
出口:SUCC,FAIL
===============================================================================
*/ 
static u8 GetEntryWith8_3Name(u8 *buf,u8* EntryName,u8 *Extension)
{
  u8 j;
  struct Directory_Entry_  *Directory_Entry_Local;  
  u8 *pointer;
  pointer = buf;
  Directory_Entry_Local = (struct Directory_Entry_ *) (pointer + CORE.offset);
  //////////////////////////////printf("offset Tony %d",CORE.offset);
  for(j = 0;j < 8;j++)
   {
    if(Directory_Entry_Local->filename[j] == 0x20)
      break;
    EntryName[j] = Directory_Entry_Local->filename[j];
   }   
  EntryName[j] = 0; 
  for(j = 0;j < 3;j++)
   {  
    if(Directory_Entry_Local->file_extention[j] == 0x20)
        break;
    Extension[j] = Directory_Entry_Local->file_extention[j]; 
   }
  Extension[j] = 0;

  if(CORE.FullPathType == FilePath && CORE.CurPathType == FilePath)
     CORE.FileSize = *(u32*)Directory_Entry_Local->file_length;
    Directory_Entry_Local->file_length[0] = 0;
  Directory_Entry_Local->file_length[0] = 0;
  CORE.ClusterOfDirectoryEntry = (Directory_Entry_Local->first_cluster_number_low2bytes[0]) + 
	  (Directory_Entry_Local->first_cluster_number_low2bytes[1]) * 256;
  CORE.PreEntrySectorNum = CORE.SectorNum;
  CORE.PreEntryoffset = CORE.offset;
  CORE_offset_add_32(buf);//Directory Entry offset + 32 
  return(SUCC);
}
/*
===============================================================================
函数
从目录读一个EntryWithLongFileName
入口:
出口:SUCC,FAIL
===============================================================================
*/ 
static u8 GetEntryWithLongFileName(u8 *buf,u8* longFileName,u8 *Extension)
{
 u8 j,FileNameOffset;
	 //,FileExtensionOffset;                     
 u8 flag,k,i;
 u16 len;
 struct LongNameDirectoryEntry *LongNameDirectoryEntry_Local;
 *Extension = 0;
 //FileExtensionOffset = 0; 
 FileNameOffset = 242;
 LongNameDirectoryEntry_Local = (struct LongNameDirectoryEntry *) (buf + CORE.offset);
 do{
	 //flag = FILE_NAME;
	 k = FileNameOffset;
     for(j = 1;j < 10;j+=2)
      {        
       if (LongNameDirectoryEntry_Local->dir_lname1[j] == 0)
          break;   
       longFileName[k] = LongNameDirectoryEntry_Local->dir_lname1[j];
       k ++;                 
      } 
	 longFileName[k] = 0;
       if(j >= 10)
         { 
           for(j = 0;j < 12;j += 2)
            {  
              if (LongNameDirectoryEntry_Local->dir_lname2[j] == 0)
                 break;

                 longFileName[k] = LongNameDirectoryEntry_Local->dir_lname2[j];
                 k++;           
            }
           if(j >= 12)   
                for(j = 0;j < 4;j += 2)
                 { 
                  if (LongNameDirectoryEntry_Local->dir_lname3[j] == 0)
                     break;       
                    longFileName[k] = LongNameDirectoryEntry_Local->dir_lname3[j];
                    k ++;                   
                 }
          }
	 if(k > 242)
	  longFileName[k] = 0;
	CORE.PreEntrySectorNum = CORE.SectorNum;
    CORE.PreEntryoffset = CORE.offset;
	if(CORE_offset_add_32(buf) == FAIL) //Directory Entry offset + 32 
       return(FAIL);
    FileNameOffset -= 13;
    k = FileNameOffset;
    LongNameDirectoryEntry_Local = (struct LongNameDirectoryEntry *) (buf + CORE.offset);
    if(LongNameDirectoryEntry_Local->dir_attr != ATTR_LONG_NAME)
     { 
           if ( ! (LongNameDirectoryEntry_Local->dir_attr & ATTR_VOLUME_ID)) 
           {
            //CORE.ClusterOfDirectoryEntry = *(u16*)LongNameDirectoryEntry_Local->dir_first;
			CORE.ClusterOfDirectoryEntry = LongNameDirectoryEntry_Local->dir_first[0]+
				LongNameDirectoryEntry_Local->dir_first[1] * 256;
			////////////////////////printf("CORE.ClusterOfDirectoryEntry = %d\n",CORE.ClusterOfDirectoryEntry);
            CORE.FileSize = *((u32*)LongNameDirectoryEntry_Local->dir_lname3);
			stringcpy(longFileName+FileNameOffset+13,longFileName);
			len =  LengthofString(longFileName);
			len --;
			i = 0;
			do{
				if(longFileName[len] == '.')
				{
				  longFileName[len] = 0;
                  stringcpy(longFileName + len + 1,Extension);
				  break;
				}
			   len--;
			   i++;
			   if(i >= 15 || len == 0 )
				 break;
			}while(1);
            //CORE_offset_add_32(buf);//Directory Entry offset + 32 
            break;
           }
       flag = FILE_NAME;
       //FileExtensionOffset = 0; 
       FileNameOffset = 256 - 13;
       k = FileNameOffset; 
	   do{
		  CORE.PreEntrySectorNum = CORE.SectorNum;
          CORE.PreEntryoffset = CORE.offset;
          if(CORE_offset_add_32(buf) == FAIL) //Directory Entry offset + 32 
            return(FAIL); 
          LongNameDirectoryEntry_Local = (struct LongNameDirectoryEntry *) (buf + CORE.offset);
	      if(LongNameDirectoryEntry_Local->dir_lname1[0] == 0xe5)
             continue;
	      if(LongNameDirectoryEntry_Local->dir_attr != ATTR_LONG_NAME)
		  {
	       if ( ! (LongNameDirectoryEntry_Local->dir_attr & ATTR_VOLUME_ID)) 
            return(GetEntryWith8_3Name(buf,longFileName,Extension));
		   else
			 continue;
		  }
	      else
		   break;
	   } while(1);
     } 

⌨️ 快捷键说明

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