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

📄 filesystem.c

📁 在Bf533上实现FAT32文件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
* Return   :DWORD amount of files and directories
*******************************************************************/	
DWORD fnDetectDirectory(DWORD startcluster)
{
	BYTE item;
	WORD recordoffset;
	DWORD x=0;
	BYTE stringnum=0;
	DWORD filenum=0;
	currentcluster_t current_cluster;
	
	current_cluster.value=startcluster;
	current_cluster.offset=0;
	
	while (current_cluster.value!= 0xFFFFFFFF) // Do while not end of cluster chain
	{
		// Read Sector
		current_cluster = fnFAT32_SectorReader(current_cluster, x++);

		if (current_cluster.value!=0xFFFFFFFF)
		{
			for (item=0; item<=15;item++)
			{
		 		recordoffset = (32*item);
 		 		if ( fnFATMisc_If_LFN_Exists(recordoffset,stringnum) )
		 		{
 	   	      			filenum++; //File / Dir Count
 	   	      		}
 	   	      		else if ( fnFATMisc_If_noLFN_SFN_Only(recordoffset) )
		 		{
			  		filenum++; //File / Dir Count
			  	}
			}// end of for
		}
	}
	return filenum;
} 

/******************************************************************
* Function     : fnListDirectory
* Description : open a directory, get all file pointers of all files in the directory and store them in FILELIST_t.
* Input     : 
*              DWORD startcluster-starting sector of curret directory
* Return   :FILELIST_t file list pointer
*******************************************************************/
FILELIST_t* fnListDirectory(DWORD startcluster)
{
	BYTE i,item,index;
	WORD recordoffset;
	BYTE LFNIndex=0;
	DWORD x=0;
	currentcluster_t current_cluster;
	
	BYTE stringnum=0,next_num=0;
	BYTE chksum;
	DWORD filenumber=0;
	BYTE String_buffer[20*13];
	BYTE short_name[11];
	WORD name_size=0;
	
	current_cluster.value=startcluster;
	current_cluster.offset=0;
	
	FILELIST_t *filelist=NULL;
	
	
       //get numbers of files in current directory
	filenumber = fnDetectDirectory(startcluster);
	
	
	filelist=(FILELIST_t *)malloc(sizeof(FILELIST_t));
	filelist->list=(FILE_t **)malloc(sizeof(FILE_t *)*(filenumber));
	filelist->num=filenumber;
	
	if(filenumber==0)
	{
		
		return NULL;
	}
	
	filenumber=0;
	
	  
	while (current_cluster.value!= 0xFFFFFFFF) // Do while not end of cluster chain
	{
		// Read Sector
		current_cluster = fnFAT32_SectorReader(current_cluster, x++);

		if (current_cluster.value!=0xFFFFFFFF)
		{
			LFNIndex=0;
			for (item=0; item<=15;item++)
			{
		 		recordoffset = (32*item);
				//check entry type
 				if ( fnFATMisc_If_LFN_Invalid(recordoffset) ) 	
 				{
                                   //invalid entry
		 			stringnum = 0;
		 			name_size = 0;
		 			next_num =0 ;
		 			continue;
		 		} 
				// Long File Name Text Found
				else if ( fnFATMisc_If_LFN_TextOnly(recordoffset) )
				{
					// if file name entry
					if(stringnum==0)
					{	
                                     //check last entry by checking bit-7, first byte in file name entry
						if((fnIDE_SectorByte(recordoffset)&0x40)!=0)
						{
							LFNIndex  =fnIDE_SectorByte(recordoffset)&0x1F;
							stringnum = LFNIndex;
							next_num  = stringnum;
							chksum	  = fnIDE_SectorByte(recordoffset+0x0D);
						}
						else
						{
                                                 // do nothing if bit-7 is not '1'.
							continue;
						}
					}
					else
					{      
					       // check file name serial number and check code, correct-get file name charaters, error-reset with 0
						if(((fnIDE_SectorByte(recordoffset)&0x1F)==next_num)&&(fnIDE_SectorByte(recordoffset+0x0D)==chksum))
						{
							LFNIndex =(fnIDE_SectorByte(recordoffset)&0x1F);
						}
						else
						{
							stringnum = 0;
		 					name_size = 0;
		 					next_num =0 ;
		 					continue;
		 				}
		 			}
					name_size += fnFATMisc_cacheLFN(recordoffset,LFNIndex,String_buffer);
					next_num--;
				} 
		 		// Normal Entry and Long text exists 
		 		else if ( fnFATMisc_If_LFN_Exists(recordoffset,stringnum) )
		 		{
		 			filelist->list[filenumber]=(FILE_t*)malloc(sizeof(FILE_t));
		 			fnIDE_SectorData(recordoffset,short_name,11);
					// check file name serial number and check code, correct-get long file name charaters, error-get short file name
		 			if((next_num!=0)||(chksum!=fnGetChksum(short_name)))
		 				fnFindFile_ShortName(x-1,item,startcluster,filelist->list[filenumber]);
		 			else
		 				fnFindFile_LongName(x-1,item,startcluster,String_buffer,name_size,filelist->list[filenumber]);
		 			filenumber++;
		 			stringnum = 0;
		 			name_size = 0;
		 			next_num =0 ;		
 	   	      					  
		 		}
		 
		 		// Normal Entry, only 8.3 Text		 
		 		else if ( fnFATMisc_If_noLFN_SFN_Only(recordoffset) )
		 		{
		 			filelist->list[filenumber]=(FILE_t*)malloc(sizeof(FILE_t));
		 			fnFindFile_ShortName(x-1,item,startcluster,filelist->list[filenumber]);
       		  			filenumber++;
		 			stringnum = 0;
		 			name_size = 0;
		 			next_num =0 ; 
		 		}
			}// end of for
		}
	}
	return filelist;
	
}

/******************************************************************
* Function     : fnFindFile
* Description : search a file or directory.
* Input     : 
*              DWORD startcluster-starting sector of curret directory
*              BYTE* filename-target file name
*              BYTE mode -file type: file or directory
* Return   :FILE_t - file pointer if found, or none
*******************************************************************/
FILE_t *fnFindFile(DWORD startcluster,BYTE *filename,BYTE mode)
{
	BYTE i,item,index;
	WORD recordoffset;
	BYTE LFNIndex=0;
	DWORD x=0;
	currentcluster_t current_cluster;
	
	BYTE stringnum=0,next_num=0;
	BYTE chksum;
	DWORD filenumber=0;
	BYTE String_buffer[20*13];
	BYTE short_name[11];
	WORD name_size=0;
	FILE_t *fp;
	
	
	current_cluster.value = startcluster;
	current_cluster.offset = 0;
	
	fp = (FILE_t*)malloc(sizeof(FILE_t));
	fp->flag=0;  
	//
	while (current_cluster.value!= 0xFFFFFFFF) // Do while not end of cluster chain
	{
		// Read Sector
		current_cluster = fnFAT32_SectorReader(current_cluster, x++);

		if (current_cluster.value!=0xFFFFFFFF)
		{
			LFNIndex=0;
			for (item=0; item<=15;item++)
			{
		 		recordoffset = (32*item);
 					//check entry type
 				if ( fnFATMisc_If_LFN_Invalid(recordoffset) ) 	
 				{
                               // check file name, reset with 0 if invalid
		 			stringnum = 0;
		 			name_size = 0;
		 			next_num =0 ;
		 			continue;
		 		} 
				// Long File Name Text Found
				else if ( fnFATMisc_If_LFN_TextOnly(recordoffset) )
				{
					//if long file name entry
					if(stringnum==0)
					{
						 //check last entry by checking bit-7, first byte in file name entry
						if((fnIDE_SectorByte(recordoffset)&0x40)!=0)
						{
							LFNIndex  =fnIDE_SectorByte(recordoffset)&0x1F;
							stringnum = LFNIndex;
							next_num  = stringnum;
							chksum	  = fnIDE_SectorByte(recordoffset+0x0D);
						}
						else
						{
							continue;
						}
					}
					else
					{
						if(((fnIDE_SectorByte(recordoffset)&0x1F)==next_num)&&(fnIDE_SectorByte(recordoffset+0x0D)==chksum))
						{
							LFNIndex =(fnIDE_SectorByte(recordoffset)&0x1F);
						}
						else
						{
							stringnum = 0;
		 					name_size = 0;
		 					next_num =0 ;
		 					continue;
		 				}
		 			}
					name_size += fnFATMisc_cacheLFN(recordoffset,LFNIndex,String_buffer);
					next_num--;
				} 
		 		// Normal Entry and Long text exists 
		 		else if ( fnFATMisc_If_LFN_Exists(recordoffset,stringnum) )
		 		{
		 			//check search mode and entry type matched or not.
		 			if(((fnIDE_SectorByte(recordoffset+0x0B)&FILE_ATTR_DIRECTORY)==0&&mode==FIND_FILE_MODE)||
		 			   ((fnIDE_SectorByte(recordoffset+0x0B)&FILE_ATTR_DIRECTORY)!=0&&mode==FIND_DIR_MODE)||
		 			   (mode==FIND_ALL_MODE))
		 			{
		 				fnIDE_SectorData(recordoffset,short_name,11);
		 				if((next_num!=0)||(chksum!=fnGetChksum(short_name)))
		 					fnFindFile_ShortName(x-1,item,startcluster,fp);
		 				else
		 					fnFindFile_LongName(x-1,item,startcluster,String_buffer,name_size,fp);
		 				
		 				filenumber++;
			 			stringnum = 0;
		 				name_size = 0;
		 				next_num =0 ;
		 				i=0;
			 			while(1)
			 			{
			 				if(__tolower(filename[i])!=__tolower(fp->filename[i]))
		 					{
		 						break;
		 					}
		 					if(filename[i]=='\0')
		 					{
		 						return fp;
		 					}
			 				i++;
			 			}
		 			}
		 	   	      					  
		 		}
		 
		 		// Normal Entry, only 8.3 Text		 
		 		else if ( fnFATMisc_If_noLFN_SFN_Only(recordoffset) )
		 		{
		 			if(((fnIDE_SectorByte(recordoffset+0x0B)&FILE_ATTR_DIRECTORY)==0&&mode==FIND_FILE_MODE)||
		 			   ((fnIDE_SectorByte(recordoffset+0x0B)&FILE_ATTR_DIRECTORY)!=0&&mode==FIND_DIR_MODE)||
		 			   (mode==FIND_ALL_MODE))
		 			{
		 				fnFindFile_ShortName(x-1,item,startcluster,fp);
       		  				filenumber++;
		 				i=0;
		 				while(1)
		 				{
		 					if(__tolower(filename[i])!=__tolower(fp->filename[i]))
		 					{
		 						break;
		 					}
			 				if(filename[i]=='\0')
			 				{
			 					return fp;
		 					}
		 					i++;
		 				}
		 			
			 			stringnum = 0;
			 			name_size = 0;
			 			next_num =0 ; 
			 		}
			 		
		 			
		 		}
			}// end of for
		}
	}
	free(fp);
	return fp;
} 

/******************************************************************
* Function     : fnReadFile
* Description : read a file into data buffer
* Input     : 
*              BYTE *buffer-data buffer addess
*              BYTE size-size of data entry
*              DWORD count -number of data entry
*              FILE_t *fp-file pointer
* Return   :DWORD count of reading data
*******************************************************************/
DWORD fnReadFile(BYTE *buffer, BYTE size, DWORD count, FILE_t *fp)
{

	
	DWORD read_num;
	DWORD  offset,sp1,sp2,length;
	DWORD bytepercluster = current_fs->byte_per_cluster;
	DWORD bytepersector = current_fs->bpb.byte_per_sector;
	BYTE  *rbuffer;
	DWORD remain;
	
	rbuffer=buffers.currentsector.u8data;
	//which sector *fp locates	
	offset=fp->curp/bytepersector;

	//sp1 is starting address of target sector
	sp1=fp->curp-offset*bytepersector;
	
	read_num=size*count;
	//counting  reading data, cut data if overflow
	if((read_num+fp->curp)>fp->filesize)
	{
		read_num=fp->filesize-fp->curp;
	}
	//length: numbers of Byte will be read next operation
	//if length more than one sector, cut length or do next reading operation

⌨️ 快捷键说明

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