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

📄 file_sect_server.c

📁 能够在单片机等小型处理器上使用的fat16文件系统
💻 C
字号:
#include"file_sect_server.h"
#include<stdlib.h>

// Size of an entry in the root directory
#define DIRENTRY_SIZE 0x20

// Buffer for read/write transfers:
unsigned char VFILE[1024*1024*4];
char Scratch[PHYSICAL_BLOCK_SIZE];
// File space
//char VFILE[1024*1024*4];
//extern char *VFILE;
//extern char VFILEend[];

// Permanent copy of important fields from bootrecord:
 bootrecord_small MBR;

//bootrecord
 DBR_tag bootrecord;

//----------------------------------------------------------------------------
// Sect_Read
//----------------------------------------------------------------------------
//
// Reads one sector into Scratch buffer
//
// Parameters   : sector - sector's number
// Return Value : error number
//----------------------------------------------------------------------------

unsigned int Sect_Read(unsigned long sector) 
{
	int i,start;
	start=sector*PHYSICAL_BLOCK_SIZE;
	/*for(i=0;i<PHYSICAL_BLOCK_SIZE;i++)
	{
		Scratch[i]=VFILE[start+i];
	}*/
	memcpy(Scratch,(char *)(VFILE+start),PHYSICAL_BLOCK_SIZE);
	return 1;
}
unsigned int direct_read(unsigned int address,unsigned char * buffer,unsigned int size)
{
	memcpy(buffer,(unsigned char *)(VFILE+address),size);
	return 0;
}
unsigned int direct_write(unsigned int address,unsigned char * buffer,unsigned int size)
{
	memcpy((unsigned char *)(VFILE+address),buffer,size);
	return 0;
}
unsigned char * get_vfile_add()
{
	return (unsigned char *)(VFILE);
}
//----------------------------------------------------------------------------
// Sect_Write
//----------------------------------------------------------------------------
//
// It writes one sector from Scratch buffer
//
// Parameters   : sector - sector's number
// Return Value :
//----------------------------------------------------------------------------

void Sect_Write(unsigned long sector) 
{
	int i,start;
	start=sector*PHYSICAL_BLOCK_SIZE;
	/*for(i=0;i<PHYSICAL_BLOCK_SIZE;i++)
	{
		VFILE[start+i]=Scratch[i];
	}*/
	memcpy((char *)(VFILE+start),Scratch,PHYSICAL_BLOCK_SIZE);
	return 1;
}

//----------------------------------------------------------------------------
// Sect_Validate
//----------------------------------------------------------------------------
//
// Function checks the validate of bootrecord.
//
// Parameters   :
// Return Value :
//----------------------------------------------------------------------------

void Sect_Validate(void)
{
	unsigned  fat_sec = 0;
	read_MBR(&bootrecord,Scratch);

	MBR.valid=0;
	MBR.hidden_sectors = 0;

	// Make a permanent copy of the important fields of the bootrecord:
	MBR.fat_copies = bootrecord.BPB_bNumFATs;//fat_copies;
	MBR.root_directory_entries = bootrecord.BPB_wRootEntry;//root_directory_entries;
	MBR.number_of_sectors = bootrecord.BPB_wTotalSec;//number_of_sectors;
	MBR.sectors_per_fat = bootrecord.BPB_wSecPerFAT;//sectors_per_fat;
	MBR.total_sectors = bootrecord.BPB_dBigTotalSec;//total_sectors;
	MBR.reserved_sectors = bootrecord.BPB_wReservedSec;//reserved_sectors;
	MBR.sectors_per_cluster = bootrecord.BPB_bSecPerClus;//sectors_per_cluster;
	MBR.valid=1;

}

//----------------------------------------------------------------------------
// Sect_Init
//----------------------------------------------------------------------------
//
// Function initializes memory  reads sector 0 and 
// checks the validate of bootrecord.
//
// Parameters   :
// Return Value :
//----------------------------------------------------------------------------

void Sect_Init(void)
{
	Sect_Read(0);	
	Sect_Validate();
}

//----------------------------------------------------------------------------
// Sect_Root_Dir
//----------------------------------------------------------------------------
//
// Returns number of sector for root directory
//
// Parameters   :
// Return Value : number of sectors
//----------------------------------------------------------------------------

unsigned Sect_Root_Dir(void) 
{
  return MBR.hidden_sectors + 
  MBR.reserved_sectors + /* Boot record followed by FATs */ 
  MBR.fat_copies*MBR.sectors_per_fat;
}

//----------------------------------------------------------------------------
// Sect_Root_Dir_Last
//----------------------------------------------------------------------------
//
// Returns number of last sector for root directory
//
// Parameters   :
// Return Value : number of last sector
//----------------------------------------------------------------------------

unsigned Sect_Root_Dir_Last(void)
{
  return Sect_Root_Dir() - 1
      + (MBR.root_directory_entries*DIRENTRY_SIZE)/PHYSICAL_BLOCK_SIZE;
}

//----------------------------------------------------------------------------
// Sect_File_Data
//----------------------------------------------------------------------------
//
// Returns first sector of file data
//
// Parameters   :
// Return Value : number of sector
//----------------------------------------------------------------------------

unsigned Sect_File_Data(void)
{
  return Sect_Root_Dir_Last() + 1 - (MBR.sectors_per_cluster*2); // First file data block is called "number 2".
}

//----------------------------------------------------------------------------
// Sect_Fat1
//----------------------------------------------------------------------------
//
// Returns first sector of 1-st FAT
//
// Parameters   :
// Return Value : number of sector
//
// NOTE: Changed this function in a #define, to safe code memory
//----------------------------------------------------------------------------

unsigned Sect_Fat1(void)
{
  return MBR.hidden_sectors + MBR.reserved_sectors;
}

//----------------------------------------------------------------------------
// Sect_Fat2
//----------------------------------------------------------------------------
//
// Returns first sector of 2-st FAT
//
// Parameters   :
// Return Value : number of sector
//
// NOTE: Changed this function in a #define, to safe code memory
//----------------------------------------------------------------------------

unsigned Sect_Fat2(void)
{
  return MBR.hidden_sectors + MBR.reserved_sectors+MBR.sectors_per_fat;
}

//----------------------------------------------------------------------------
// Sect_Write_Multi_Fat
//----------------------------------------------------------------------------
//
// Automatically handle multiple FAT copies
//
// Parameters   : sector - sector's number
// Return Value :
//----------------------------------------------------------------------------

void Sect_Write_Multi_Fat(unsigned long sector)
{
  if(sector<Sect_Fat1() || sector>=Sect_Root_Dir()) {
		// This is a 'normal' block, not in the FAT:
    Sect_Write(sector);
  } else {
		// Writing to one of the FAT's will automagically write the same block to the 
		// other FAT copies.
    while(sector>=Sect_Fat2()) // Decrement 'sector' to refer to 1st FAT copy.
		  sector-=MBR.sectors_per_fat;
    while(sector<Sect_Root_Dir()){ // Write same data to each FAT copy.
      Sect_Write(sector);
      sector+=MBR.sectors_per_fat;
    }
  }
}


//----------------------------------------------------------------------------
// Sect_Sectors
//----------------------------------------------------------------------------
//
// Returns number of sectors.
//
// Parameters   :
// Return Value :
//----------------------------------------------------------------------------

unsigned long Sect_Sectors(void)
{
  return MBR.number_of_sectors;
}

unsigned int  Sect_Cluster_Size()
{
	return MBR.sectors_per_cluster*512;
}

⌨️ 快捷键说明

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