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

📄 fat32_base.c

📁 一个FAT32文件系统的源代码,可用于嵌入式系统,并能够直接在linux下直接验证.
💻 C
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//					    FAT32 File IO Library Linux Test Version
//									  V1.0L
// 	  								Rob Riglar
//								Copyright 2003,2004 
//
//   					  Email: admin@robs-projects.com
//
//			   Compiled and tested on Redhat 'Shrike' with GNU gcc
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "FAT32_Base.h"
//-----------------------------------------------------------------------------
// LBAofCluster: This function converts a cluster number into a sector / LBA 
//				 number.
//-----------------------------------------------------------------------------
UI32 FAT32_LBAofCluster(UI32 Cluster_Number)
{
 return ((FAT32.cluster_begin_lba + ((Cluster_Number-2)*FAT32.SectorsPerCluster)));
}

//-----------------------------------------------------------------------------
// FindFATDetails: Uses FAT32_FindLBABegin to find the LBA for the volume, 
//				   and loads into memory some specific details of the partition
//				   which are used in further calculations.
//-----------------------------------------------------------------------------
void FAT32_FindFAT32Details(void)
{
	 byte Number_of_FATS;
	 UI32 Sectors_per_FAT;
	 word Reserved_Sectors;
	 UI32 LBA_BEGIN;
	 
	 LBA_BEGIN = 0;	// Starting point is the partition rather than MBR
					//	So no LBA Begin offset needed

	 // Load FAT32 Volume table into 'clusterbuffer'
	 IDE_BufferSector(LBA_BEGIN);	
	 
	 // Make sure there are 512 bytes per cluster
	 if (IDE_SectorWord(0x0B)!=0x200) 
	 {
	  	 printf("\r\nError: Bytes per cluster is not 512");
		 while(1);
	 }

	 // Load Parameters of FAT32	 
 	 FAT32.SectorsPerCluster = IDE_SectorByte(0x0D);
	 Reserved_Sectors = IDE_SectorWord(0x0E);
	 Number_of_FATS = IDE_SectorByte(0x10);
	 Sectors_per_FAT = IDE_SectorUI32(0x24);
	 FAT32.RootDir_First_Cluster = IDE_SectorUI32(0x2C);
	 
	 FAT32.fat_begin_lba = LBA_BEGIN + Reserved_Sectors;	// First FAT LBA address
	 
	 // The address of the first data cluster on this volume
	 FAT32.cluster_begin_lba = FAT32.fat_begin_lba + (Number_of_FATS * Sectors_per_FAT);

	 if (IDE_SectorWord(0x1FE)!=0xAA55) // This signature should be AA55
	 {
	  	 printf("\r\nError: Incorrect Volume Signature");
		 while(1);
	 } 
}

//-----------------------------------------------------------------------------
// FindNextCluster: Return Cluster number of next cluster in chain by reading
//					FAT table and traversing it. Return 0xffffffff for end of 
//					chain.
//-----------------------------------------------------------------------------
UI32 FAT32_FindNextCluster(UI32 Current_Cluster)
{
	UI32 FAT_sector_offset, UI32position;
	UI32 nextcluster;
	
	// Why is '..' labelled with cluster 0 when it should be 2 ??
	if (Current_Cluster==0) Current_Cluster=2;
	
	// Find which sector of FAT table to read
	FAT_sector_offset = Current_Cluster / 128;
	 
	// Read FAT sector into buffer
	IDE_BufferSector(FAT32.fat_begin_lba+FAT_sector_offset);
	 
	// Find 32 bit entry of current sector relating to cluster number 
	UI32position = (Current_Cluster - (FAT_sector_offset * 128)) * 4; 

	// Read Next Clusters value from Sector Buffer
	nextcluster = IDE_SectorUI32(UI32position);	 

	// Mask out MS 4 bits (its 28bit addressing)
	nextcluster = nextcluster & 0x0FFFFFFF;		
				 
	// If 0x0FFFFFFF then end of chain found
	if (nextcluster==0x0FFFFFFF) 
	   return (0xFFFFFFFF); 
	else 
		 // Else return next cluster
		 return (nextcluster);						 
} 

⌨️ 快捷键说明

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