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

📄 file_system.c

📁 能够在单片机等小型处理器上使用的fat16文件系统
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "file_system.h"
#include "file_sect_server.h"
#include "file_Util.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include"file_formated.h"
#include<string.h>

//#define Sect_Block_Size() ((ulong)PHYSICAL_BLOCK_SIZE)

extern unsigned char VFILE[1024*1024*4];
uchar Path_Name[200];
ulong Current_Dir_Block;
static find_info findinfo; // Shared find_info for m_fopen() and m_fdelete() 
DBR_tag MBR_t;

//-----------------------------------------------------------------------------
// format_disk;
//-----------------------------------------------------------------------------
//
//
//
//
//-----------------------------------------------------------------------------
/*void m_format()
{
		//write MBR block,will be used in format functino
	read_MBR(&MBR_t,gFormat_8MB);
	MBR_t.BPB_bSecPerClus=2;
	memset(VFILE,0,1024*1024*4);
	write_MBR(&MBR_t,VFILE);

}*/
void m_format()
{
		//write MBR block,will be used in format functino
	memset(VFILE,0,1024*1024*4);
	read_MBR(&MBR_t,gFormat_8MB);
	MBR_t.BPB_bSecPerClus=1;
	MBR_t.BPB_wTotalSec=8191;
	MBR_t.BPB_wSecPerTrk=32;
	MBR_t.bDrvNum=128;
	MBR_t.BPB_wReservedSec=2;
	MBR_t.BPB_wSecPerFAT=127;
	MBR_t.BPB_dHiddSec=1;
	write_MBR(&MBR_t,VFILE);
}

//-----------------------------------------------------------------------------
// file_name_match
//-----------------------------------------------------------------------------
//
// Return Value : 1 if they match
// Parameters   : filename - pointer to file name
//     			  direntryname - pointer to enterd directory name
//
// Compares the file name and directory name
//-----------------------------------------------------------------------------




static BYTE file_name_match(char* filename,char* direntryname)
{
  BYTE i,j = filename[0];
  for(i=0;i<8;i++) {
    if(direntryname[i] == ' ' && (filename[i] == '\0' || filename[i] == '.')) 
	{
	  if(!(j == '.' && filename[i] == '.')) 
	      break;
	}
    if(tolower(direntryname[i])!=filename[i]) 
      return 0;
  }
  j = i+1;
  for(i = 8; i < 11; i++) {
    if( filename[j] == '\0' && direntryname[i] != ' ') 
      return 0;
    if( direntryname[i] == ' ' && (filename[j] == '\0' || filename[j] == '.' 
        || filename[j-1] == '\0'))
      break;
    if(tolower(direntryname[i]) != filename[j]) 
      return 0;
    j++;
  }
  return 1;
}


//-----------------------------------------------------------------------------
// write_current_dir
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function printout current directory name
//-----------------------------------------------------------------------------
/*void write_current_dir()
{
	printf("%s",Path_Name);
}

*/
//-----------------------------------------------------------------------------
// GetClusterOfParentDirectory
//-----------------------------------------------------------------------------
//
// Return Value : cluster number
// Parameters   : None
//
// Function returns cluster number which begins current directory
//-----------------------------------------------------------------------------
static ulong GetClusterOfParentDirectory()
{
	if(Current_Dir_Block == Sect_Root_Dir())  return 0;
	return (Current_Dir_Block - Sect_File_Data()) / MBR.sectors_per_cluster;
}

//-----------------------------------------------------------------------------
// Get_Cluster_From_Sector
//-----------------------------------------------------------------------------
//
// Return Value : cluster number
// Parameters   : sector - sector which belongs to returned cluster
//
// Function returns cluster number which contains sector
//-----------------------------------------------------------------------------
static ulong Get_Cluster_From_Sector(unsigned long sector)
{
	if(sector < (Sect_File_Data() + 2*MBR.sectors_per_cluster)) return 0;
	return ((sector - Sect_File_Data()) / MBR.sectors_per_cluster);
}

//-----------------------------------------------------------------------------
// Get_First_Sector
//-----------------------------------------------------------------------------
//
// Return Value : sector address
// Parameters   : cluster - cluster number
//
// Function returns first sector which belongs to the cluster
//-----------------------------------------------------------------------------
static unsigned long Get_First_Sector(ulong cluster)
{
	if(cluster >= 2) return Sect_File_Data() + cluster*MBR.sectors_per_cluster;
	else return Sect_Root_Dir();
}

//-----------------------------------------------------------------------------
// Get_First_Block_Of_Next_Cluster
//-----------------------------------------------------------------------------
//
// Return Value : first block of next cluster in chain or 0xFFFFFFFF if cluster 
//                is last in chain
//
// Parameters   : cluster - searching cluster 
//
// Function returns number of first sector in next cluster in cluster chain
//-----------------------------------------------------------------------------
static unsigned long Get_First_Block_Of_Next_Cluster(ulong cluster)
{
	 unsigned long ret = fat_chain(cluster,MBR.sectors_per_cluster);
	if(ret != 0xFFFFFFFF)
		return ret + Sect_File_Data();
	return ret;
}

//-----------------------------------------------------------------------------
// Get_Next_Cluster
//-----------------------------------------------------------------------------
//
// Return Value : cluster number
// Parameters   : next cluster in chain
//
// Function returns number of next cluster in chain
//-----------------------------------------------------------------------------
static ulong Get_Next_Cluster(ulong cluster)
{
	uint*  fat_table=(uint *)Scratch;

    Sect_Read(Sect_Fat1() + cluster/(Sect_Block_Size()/2));

    return ntohs(fat_table[cluster%(Sect_Block_Size()/2)]);
}

//-----------------------------------------------------------------------------
// Get_File_Name
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : file_name - name of file [out] (must be allocated outside
//                            this function)
//				      direntry_name - name used in direntry [in]
//
// Function gets file name from direntry
//-----------------------------------------------------------------------------
static void Get_File_Name(char* direntry_name,char* file_name)
{
	ulong i,j = 0,k = 0;
	for(i=0;i<11;i++)
	{
		if(direntry_name[i] == ' ')
		{
			j = 1;
			continue;
		}
		if(j)
		{
			file_name[k++] = '.';
			j = 0;
		}
		file_name[k++] = tolower(direntry_name[i]);
	}
	file_name[k] = 0;
}

//-----------------------------------------------------------------------------
// Get_First_Block_Directory_Cluster
//-----------------------------------------------------------------------------
//
// Return Value : first sector of cluster which belongs to current directory 
//                fat chain
// Parameters   : sector - sector to check				
//
// Function returns first sector of cluster which contains sector if this
// cluster belongs to FAT chain of current directory
//-----------------------------------------------------------------------------
static unsigned long Get_First_Block_Directory_Cluster(unsigned long sector)
{
	 ulong cluster = Get_Cluster_From_Sector(sector);
	 ulong next_dir_cluster = GetClusterOfParentDirectory();
	while(next_dir_cluster != cluster)
	{
		next_dir_cluster = Get_Next_Cluster(next_dir_cluster);
		if(next_dir_cluster >= 0xfff8) return next_dir_cluster;
	}
	return Get_First_Sector(cluster);
}


//-----------------------------------------------------------------------------
// Clear_Cluster
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : cluster - cluster number				
//
// Function is used to clear all sectors of cluster
//-----------------------------------------------------------------------------
static void Clear_Cluster(ulong cluster)
{
	 unsigned long sector = Get_First_Sector(cluster);
	 ulong i;
	memset(Scratch,0,512);
	for(i=0;i<MBR.sectors_per_cluster;i++)
	{
		Sect_Write(sector+i);
	}

}

//-----------------------------------------------------------------------------
// FillDirEntry
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : direntry - dir entry
//				  dir_name - file/directory name							
//
// Function fills dir entry with file name
//-----------------------------------------------------------------------------
static void FillDirEntry(dir_entry* direntry,char* dir_name)
{
   BYTE i;
  for( i = 0; i < 10; i++) 
    direntry->sfn.reserved[i] = 0;
  
  direntry->sfn.time = findinfo.direntry->sfn.date = 0;
  direntry->sfn.filesize = 0;

  // Fill in the filename
  for( i = 0; i < 11; i++ ) 
    direntry->sfn.name[i] = ' ';
 
  for( i = 0; i < 11; i++ ) {
    if(!dir_name[i])
	     break;
    direntry->sfn.name[i] = toupper(dir_name[i]);
  }
}

//-----------------------------------------------------------------------------
// FileSys_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None							
//
// Function initializes some data used to navigate over directories
//-----------------------------------------------------------------------------
void FileSys_Init()
{
	Current_Dir_Block = Sect_Root_Dir();
	strcpy(Path_Name,"\\");
}


//-----------------------------------------------------------------------------
// m_fcreate
//-----------------------------------------------------------------------------
//
// Return Value : If ok returns TRUE
// Parameters   : find_info - pointer to info about file
//				  filename  - pointer to file name
//
// This function creates file
//-----------------------------------------------------------------------------
unsigned short LSB_to_MSB(unsigned short in)
{
	unsigned short out;
	out=in;
	out=out<<8;
	out+=(in&0xff00)>>8;
	return out;
}

static BYTE m_fcreate(find_info* findinfo,char* filename)
{
   BYTE i,j;
   unsigned char finded;
   unsigned short start_cluster;
   unsigned short m,n;
   unsigned short direntry_num;
   unsigned short *sect_start;
    dir_entry * pre_dir_entry;
	unsigned char Scratch_temp[512];
//update dir 10-24
  // Find the first empty directory entry
   direntry_num=findfirst(findinfo,1);
  if(!direntry_num) return 0;

  // Fill in the direntry

⌨️ 快捷键说明

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