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

📄 filesystem.c

📁 在Bf533上实现FAT32文件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
* File         : filesystem.c
* Processor    : ADSP-BF533  
* IDDE         : VisualDSP++3.5
* Description  : Define operating functions of FAT32, includes:
*                  
*            Wait_Ready();
*            Wait_ReadyBusy();
*            CheckforError();
*            IdeStandby();
*            IdeIdle();
*
*************************************************************************/
#include "fat/filesystem.h"
#include "fat/fat32.h"
#include "IDE/ide_access.h"
#include "IDE/ide_base.h"
#include "type.h"


partition_table_t *partition_table[23];
FS_t fs;

extern BYTE fnUpdateFILE(FILE_t *fp);
extern currentcluster_t fnFAT32_WriteSector(currentcluster_t,DWORD);

extern unsigned int cycles[100];

/******************************************************************
* Function     : fnDetect_Partition
* Description : get HD DPT information
* Input     : none
* Return   : none
*******************************************************************/
void fnDetect_Partition(void)
{
	BYTE partition_num=0;
	BYTE temp;
	BYTE master_table_buffer[64];
	BYTE *table_buffer;
	BYTE i;
	
	DWORD LBABEGIN;
	DWORD EXT_next_partition;
		
		// Load MBR (LBA 0) into the 512 byte buffer
	fnIDE_BufferSector(&(buffers.currentsector),0,1);	
		
		// Make Sure 0x55 and 0xAA are at end of sector
	if (fnIDE_SectorWord(SIGN_POSITION)!=SIGN_VALUE) 
	{
		printf("\r\nInvalid MBR, Please Format Drive\r\n");
		while(1);
	}
	//read main DPT (4 partitions)
	fnIDE_SectorData(PARTITION_TABLE_1,master_table_buffer,sizeof(master_table_buffer));
	//
	table_buffer=(BYTE*)malloc(sizeof(BYTE)*PARTITION_TABLE_SIZE);
	
	for (i=0;i<4;i++)
	{
		temp=master_table_buffer[i*PARTITION_TABLE_SIZE+SYSTEM_ID];
		//Processing Extended Aartition
		if((temp==EXT_TYPECODE1)||(temp==EXT_TYPECODE2))
		{
			memcpy(table_buffer,master_table_buffer+i*PARTITION_TABLE_SIZE,PARTITION_TABLE_SIZE);
			while(temp!=NO_PARTITION)
			{
				EXT_next_partition =table_buffer[OFFSET_SECTOR];
				EXT_next_partition+=((DWORD)table_buffer[OFFSET_SECTOR+1])<<8;
				EXT_next_partition+=((DWORD)table_buffer[OFFSET_SECTOR+2])<<16;
				EXT_next_partition+=((DWORD)table_buffer[OFFSET_SECTOR+3])<<24;
				fnIDE_BufferSector(&(buffers.currentsector),EXT_next_partition,1);
				
				fnIDE_SectorData(PARTITION_TABLE_1,table_buffer,sizeof(table_buffer));
				fnPartition_init(table_buffer,partition_num);
				partition_num++;
				fnIDE_SectorData(PARTITION_TABLE_2,table_buffer,sizeof(table_buffer));
				temp=table_buffer[SYSTEM_ID];
			}
			continue;
		}
		else if(temp==NO_PARTITION)
		{
			break;
		}
		
		memcpy(table_buffer,master_table_buffer+i*PARTITION_TABLE_SIZE,PARTITION_TABLE_SIZE);
		fnPartition_init(table_buffer,partition_num);
		partition_num++;
	}
	for(i=partition_num;i<23;i++)
	{
		partition_table[i]=NULL;
	}
	free(table_buffer);
	if(partition_num==0)
	{
		printf("The Disk isn't partitioned\r\n");
		return;
	}
}
/******************************************************************
* Function     : fnPartition_init
* Description : Initialize DPT, get DPT inforamtion
* Input     : 
*              BYTE *buffer-informatioin of DPT
*              BYTE num-partition number
* Return   : none
*******************************************************************/
void fnPartition_init(BYTE *buffer,BYTE num)
{
	BYTE temp;
	temp=buffer[SYSTEM_ID];
	if ((temp==FAT32_TYPECODE1)||(temp==FAT32_TYPECODE2)||(temp==FAT32_TYPECODE3))
	{
		partition_table[num]=(partition_table_t *)malloc(sizeof(partition_table_t));
		partition_table[num]->flag=1;
		partition_table[num]->fs=(FS_t *)malloc(sizeof(FS_t));
		partition_table[num]->fnFS_init = fnFAT32_init;
	}
	else if ((temp==FAT16_TYPECODE1)||(temp==FAT16_TYPECODE2)||(temp==FAT16_TYPECODE3))
	{
		/*partition_table[num]=(partition_table_t *)malloc(sizeof(partition_table_t));
		partition_table[num]->flag=1;
		partition_table[num]->fs=(FS_t *)malloc(sizeof(FS_t));
		partition_table[num]->fnFS_init = fnFAT16_init;*/
	}
	else
	{
		partition_table[num]=(partition_table_t *)malloc(sizeof(partition_table_t));
		partition_table[num]->flag=0;
		return;
	}

	partition_table[num]->offset_sector	=	buffer[OFFSET_SECTOR];
	partition_table[num]->total_sector	=	buffer[TOTAL_SECTOR];
	partition_table[num]->fnFS_init(partition_table[num]->fs,partition_table[num]->offset_sector);
}

/******************************************************************
* Function     : fnFATMisc_If_LFN_TextOnly
* Description : Check long file name
* Input     : 
*              BYTE recordoffset-offset of long file name entry in buffers
* Return   : yes-1/no-0
*******************************************************************/
int fnFATMisc_If_LFN_TextOnly(DWORD recordoffset)
{
	if ((fnIDE_SectorByte(recordoffset+11)&0x0F)==FILE_ATTR_LFN_TEXT) 
		return 1;
	else 
		return 0;
}

/******************************************************************
* Function     : fnFATMisc_If_LFN_Invalid
* Description : Check the entry wheter ignorable, ignore the entry if the entry is null or deleted 
* Input     : 
*              BYTE recordoffset-offset of long file name entry in buffers
* Return   : yes-1/no-0
*******************************************************************/
int fnFATMisc_If_LFN_Invalid(DWORD recordoffset)
{
	if ((fnIDE_SectorByte(recordoffset)==FILE_HEADER_BLANK)||(fnIDE_SectorByte(recordoffset)==FILE_HEADER_DELETED))//||(fnIDE_SectorByte(recordoffset+11)==FILE_ATTR_VOLUME_ID)||(fnIDE_SectorByte(recordoffset+11)&FILE_ATTR_SYSHID)) 
	   	return 1;
	else 
		return 0;
}
/******************************************************************
* Function     : fnFATMisc_If_LFN_Exists
* Description : Check whether a long file name. 
* Input     : 
*              BYTE recordoffset-offset of long file name entryin buffers
* Return   : yes-1/no-0
*******************************************************************/
int fnFATMisc_If_LFN_Exists(DWORD recordoffset, BYTE LFNstrings)
{
	if ((fnIDE_SectorByte(recordoffset+11)!=FILE_ATTR_LFN_TEXT) && (fnIDE_SectorByte(recordoffset)!=FILE_HEADER_BLANK) && (fnIDE_SectorByte(recordoffset)!=FILE_HEADER_DELETED) /*&& (fnIDE_SectorByte(recordoffset+11)!=FILE_ATTR_VOLUME_ID) && (!(fnIDE_SectorByte(recordoffset+11)&FILE_ATTR_SYSHID))*/ && (LFNstrings))
		return 1;
	else 
		return 0;
}
/******************************************************************
* Function     : fnFATMisc_If_noLFN_SFN_Only
* Description : Check whether not long file name only 
* Input     : 
*              BYTE recordoffset-offset of long file name entry in buffers
* Return   : yes-1/no-0
*******************************************************************/
int fnFATMisc_If_noLFN_SFN_Only(DWORD recordoffset)
{
	if ((fnIDE_SectorByte(recordoffset+11)!=FILE_ATTR_LFN_TEXT) && (fnIDE_SectorByte(recordoffset)!=FILE_HEADER_BLANK) && (fnIDE_SectorByte(recordoffset)!=FILE_HEADER_DELETED) /*&& (fnIDE_SectorByte(recordoffset+11)!=FILE_ATTR_VOLUME_ID) && (!(fnIDE_SectorByte(recordoffset+11)&FILE_ATTR_SYSHID))*/)
		return 1;
	else 
		return 0;
}
/******************************************************************
* Function     : fnFATMisc_If_dir_entry
* Description : Check whether directory entry
* Input     : 
*              BYTE recordoffset-offset of long file name entry in buffers
* Return   : yes-1/no-0
*******************************************************************/
int fnFATMisc_If_dir_entry(DWORD recordoffset)
{
	if ((fnIDE_SectorByte(recordoffset+11))&FILE_ATTR_DIRECTORY) 
		return 1;
	else 
		return 0;
}
/******************************************************************
* Function     : fnFATMisc_cacheLFN
* Description : fetch long file name entry - ASCII charaters
* Input     : 
*              BYTE recordoffset-offset of long file name entry in buffers
*		  BYTE LFNIndex-long file name entry number
*		  BYTE *String-destinated address of store long file name
* Return   : BYTE numbers of ASCII charaters
*******************************************************************/
BYTE fnFATMisc_cacheLFN(WORD recordoffset,BYTE LFNIndex,BYTE *String)
{
	BYTE i;
	BYTE String_offset;
	//charater location in long file name entry
	BYTE char_locate[13]={1,3,5,7,9,0x0E,0x10,0x12,0x14,0x16,0x18,0x1C,0x1E};
	BYTE sub_size;    
	  
	String_offset = 0;
	for(i=1;i<LFNIndex;i++)
	{
		String_offset += 13;
	}	
	sub_size=13;	
	for(i=0;i<13;i++)
	{

              //Charaters in long file name are encoded in unicode format. 
		String[String_offset+i] = fnIDE_SectorByte(recordoffset+char_locate[i]);
		if(String[String_offset+i]==0)
		{
			sub_size=i;
			break;
		}
	}
	
	for (i=0; i<13; i++)
	 	 if (String[String_offset+i]==0xFF) 
	 	 	String[String_offset+i] = 0x0; // Replace with space
       //exclude '/0'
	return sub_size;
} 
/******************************************************************
* Function     : fnGetChksum
* Description : get check code from 11 bytes in short file name
* Input     : 
*              BYTE *shortname-address of short file name
* Return   : BYTE check code
*******************************************************************/
BYTE fnGetChksum(BYTE *shortname)
{
	BYTE i,j=0;
	BYTE chksum=0;
    	for (i=11; i>0; i--)
        	chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + shortname[j++];
        return chksum;
}

/******************************************************************
* Function     : fnFindFile_LongName
* Description : get long file name pointer
* Input     : 
*              DWORD sector_num-sector number that contain file entry
*              WORD  item-entry number of file entry in current sector
*              DWORD up_folder-starting sector of up directory 
*              BYTE  *String_buffer- size of file name
*              FILE_t *file-file pointer
* Return   : none
*******************************************************************/
void fnFindFile_LongName(DWORD sector_num,WORD item,DWORD up_folder,BYTE *String_buffer,WORD name_size,FILE_t *file)	
{
	WORD recordoffset=item*32;
	file->flag		|= MODE_OPEN;
	file->startcluster 	= (DWORD)fnIDE_SectorWord(recordoffset+0x1A);
	file->startcluster += ((DWORD)fnIDE_SectorWord(recordoffset+0x14))<<16;
	file->currentcluster.value =file->startcluster;
	file->currentcluster.offset = 0;

	file->curp		= 0;
	file->type		= fnIDE_SectorByte(recordoffset+0x0B);
	file->filesize		= fnIDE_SectorDWORD(recordoffset+0x1C);
	
	memcpy(file->filename,String_buffer,name_size);
	file->filename[name_size]=0;
	file->listp=sector_num*16+item;
	file->up_folder=up_folder;
}

/******************************************************************
* Function     : fnFindFile_ShortName
* Description : get short file name pointer
* Input     : 
*              DWORD sector_num-sector number that contain file entry
*              WORD  item-entry number of file entry in current sector
*              DWORD up_folder-starting sector of up directory 
*              BYTE  *String_buffer- size of file name
*              FILE_t *file-file pointer
* Return   : none
*******************************************************************/
	
void fnFindFile_ShortName(DWORD sector_num,BYTE item,DWORD up_folder,FILE_t *file)
{
	BYTE i,j=0;
	BYTE temp;
	WORD recordoffset=item*32;
	file->flag		|= MODE_OPEN;
	file->startcluster 	= (DWORD)fnIDE_SectorWord(recordoffset+0x1A);
	file->startcluster += ((DWORD)fnIDE_SectorWord(recordoffset+0x14))<<16;
	file->currentcluster.value =file->startcluster;
	file->currentcluster.offset = 0;
	
	file->curp		= 0;
	file->type		= fnIDE_SectorByte(recordoffset+0x0B);
	file->filesize		= fnIDE_SectorDWORD(recordoffset+0x1C);
	
	for(i=0;i<8;i++)
	{
		temp = fnIDE_SectorByte(recordoffset+i);
		if(temp!=0x20)
		{
			file->filename[i] = __tolower(temp);
		}
		else
		{
			break;
		}
	}
	if(fnIDE_SectorByte(recordoffset+8)!=0x20)
	{
		file->filename[i]='.';
		i++;
		for(j=0;j<3;j++)
		{
			temp = fnIDE_SectorByte(recordoffset+8+j);
			if(temp!=0x20)
			{
				file->filename[i+j] = __tolower(temp);
			}
			
			else
			{
				file->filename[i+j] = '\0';
				break;
			}
		}
	}
	else
	{
		file->filename[i]='\0';
	}
	
	file->filename[i+j] = '\0';
	file->listp=sector_num*16+item;
	file->up_folder=up_folder;
	return;
	
}
/******************************************************************
* Function     : fnDetectDirectory
* Description : get numbers of files and directores in current directory
* Input     : 
*              DWORD startcluster-starting sector of curret directory

⌨️ 快捷键说明

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