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

📄 fat16.h

📁 我自己实现的fat16文件系统驱动
💻 H
字号:
/*fat16.h	define some structure of fat16*/
#ifndef __FAT16_H
#define __FAT16_H

#ifndef BYTE
typedef unsigned char BYTE;
#endif

#ifndef WORD
typedef unsigned short WORD;
#endif

#ifndef DWORD
typedef unsigned long DWORD;
#endif

struct _BPB
{
	BYTE NumFATs;	//The count of FAT data structures on the volume.

	BYTE SecPerClus;	//Number of sectors per allocation unit.

	WORD BytsPerSec;	//Count of bytes per sector.

	WORD RsvdSecCnt;	//Number of reserved sectors in the Reserved region of the volume starting at the first sector of the volume.

	WORD RootEntCnt;	//this field contains the count of 32-byte directory entries in the root directory.

	DWORD TotSec;	//This field is the total count of sectors on the volume.

	DWORD FATSz;	//This field is the FAT12/FAT16 16-bit count of sectors occupied by ONE FAT.
};

#define ERCHAR	0xe5

struct _DIR
{
	BYTE Name[11];
	BYTE Attr;
	BYTE NTRes;
	BYTE CrtTimeTenth;
	WORD CrtTime;
	WORD CrtDate;
	WORD LstAccDate;
	WORD FstClusHI;
	WORD WrtTime;
	WORD WrtDate;
	WORD FstClusLO;
	DWORD FileSize;
};

#define ATTR_READ_ONLY	0x01
#define ATTR_HIDDEN		0x02
#define ATTR_SYSTEM		0x04
#define ATTR_VOLUME_ID	0x08
#define ATTR_DIRECTORY	0x10
#define ATTR_ARCHIVE	0x20

//The sector number of the first sector of that cluster.
//FirstSectorofCluster = ((N – 2) * BPB_SecPerClus) + FirstDataSector;
#define FirstSectorofCluster(N)	(((N – 2) * Bpb.SecPerClus) + FirstDataSector)

int fat_init();
void fat_deinit();

//
//FAT16 Apis
//

struct _stat
{
	BYTE Attr;
	BYTE CrtTimeTenth;
	WORD CrtTime;
	WORD CrtDate;
	WORD LstAccDate;
	WORD WrtTime;
	WORD WrtDate;
	DWORD FileSize;
};

struct _file
{
	int valid; // 1 valid, 0 free.

	DWORD DirSectorNum;
	int DirIndex;

	DWORD StartSectorNum;
	DWORD CurrentSectorNum;
	DWORD SectorOffset;

	struct _DIR dir;

	unsigned long offset;
};

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

struct FatDate
{
	WORD Day : 5;
	WORD Month : 4;
	WORD Year : 7;
};

struct FatTime
{
	WORD Second_2s : 5;
	WORD Minutes : 6;
	WORD Hours : 5;
};

struct FatDateTime
{
	union
	{
		struct FatDate fatdate;
		WORD Date;
	}Date;

	union
	{
		struct FatTime fattime;
		WORD Time;
	}Time;

	BYTE TimeTenth;
};

struct FatGet
{
	DWORD DirSectorNum;
	int DirIndex;

	int IsRootDir;

	char filename[13];
};

extern struct _file handles[16];
extern struct FatGet fat_get;

int fat_mkdir( const char *dirname );
int fat_rmdir( const char *dirname );
int fat_getfirst(const char *path, char* filename);
int fat_getnext(char* filename);

int fat_close(int handle);
int fat_creat(const char* filename, BYTE attribute);
long fat_lseek(int handle, long offset, int origin);
int fat_open(const char* filename);
unsigned int fat_read(int handle, void* buffer, unsigned int bytes);
unsigned int fat_write(int handle, const char* buffer, unsigned int bytes);

int fat_remove( const char *filename);
int fat_get_stat( const char *filename, struct __stat* stat);
int fat_set_stat( const char *filename, struct __stat* stat);
int fat_rename( const char *oldname, const char *newname );

#endif //__FAT16_H

⌨️ 快捷键说明

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