📄 libfat.h
字号:
//This is "libfat.h" - Driver's constants, structures and prototypes #ifndef __BITS_LIBFAT_H#define __BITS_LIBFAT_H#ifndef __INSIDE_LIBFAT_H#error "Never use <bits/libfat.h> directly; include <libfat.h> instead."#endif#include <endian.h>#if __BYTE_ORDER == __BIG_ENDIAN#include <byteswap.h>#define EFW(X) bswap_16(X)#define EFD(X) bswap_32(X)#else#define EFW(X) (X)#define EFD(X) (X)#endif#include <config.h>#include <glib.h>/* Maximum number of file opened */#define MAX_OPENED_FILES 4096 /* largest sector size */#define MAX_SECTORSIZE 8192 /* FAT directory entry size in bytes*/#define FAT_DIRSIZE 32 /* largest cluster size (sure?) */#define MAX_CLUSTERSIZE 8192 /* largest MSDOS path length */#define MAX_PATHLENGTH 128 /* largest directory (in sectors) */#define MAX_DIRSIZE 64 /* size of free cluster array in Volume_t */#ifndef FCLUS_BUFSZ#define FCLUS_BUFSZ 8192 #endif/* Maximum number of bytes per cluster: 32k, according to MS for max compatibility */#define MAX_BYTES_PER_CLUSTER (32*1024) #ifdef LIBFAT_USE_MUTEX#include <pthread.h>#define fat_lock(V) pthread_mutex_lock(&(V->fat_mutex))#define fat_unlock(V) pthread_mutex_unlock(&(V->fat_mutex))#else#define fat_lock(V) #define fat_unlock(V)#endif#ifndef ZERO_BFSZ#define ZERO_BFSZ 8192#endif/* FAT Types */typedef enum { FAT12, FAT16, FAT32 } FatType_t;/* EOC (End Of Clusterchain) check macros. *//* These expressions are true (nonzero) if the value of a FAT entry is *//* an EOC for the FAT type. An EOC indicates the last cluster of a file. */#define FAT12_ISEOC(EntryValue) ((EntryValue) >= 0x0FF8)#define FAT16_ISEOC(EntryValue) ((EntryValue) >= 0xFFF8)#define FAT32_ISEOC(EntryValue) (((EntryValue) & 0x0FFFFFFF) >= 0x0FFFFFF8) //??????#define FAT12_ISFREE(EntryValue) (((EntryValue) & 0x0FFF) == 0x0)#define FAT16_ISFREE(EntryValue) (((EntryValue) & 0xFFFF) == 0x0)#define FAT32_ISFREE(EntryValue) (((EntryValue) & 0x0FFFFFFF) == 0x00000000)#define FAT12_EOC_VALUE 0x0FFF#define FAT16_EOC_VALUE 0xFFFF#define FAT32_EOC_VALUE 0x0FFFFFF8 //it was 0x0FFFFFFF but linux driver set it to F8/* Bad cluster marks. *//* Set a FAT entry to the FATxx_BAD value to mark the cluster as bad. *//* *//* The FAT file system specification says that to avoid confusion, no *//* FAT32 volume should ever be configured such that 0x0FFFFFF7 is an *//* allocatable cluster number. In fact an entry that would point to the *//* cluster 0x0FFFFFF7 would be recognised as Bad instead. Since values *//* greater or equal than 0x0FFFFFF8 are interpreted as EOC, I think we *//* can assume that the max cluster for a FAT32 volume is 0x0FFFFFF6. *//* That problem doesn't exist on FAT12 and FAT16 volumes, in fact: *//* 0x0FF7 = 4087 is greater than 4086 (max cluster for a FAT12 volume) *//* 0xFFF7 = 65527 is greater than 65526 (max cluster for a FAT16 volume) */#define FAT12_BAD_VALUE 0x0FF7#define FAT16_BAD_VALUE 0xFFF7#define FAT32_BAD_VALUE 0x0FFFFFF7#define FAT12_ISBAD(EntryValue) (EntryValue == 0x0FF7)#define FAT16_ISBAD(EntryValue) (EntryValue == 0xFFF7)#define FAT32_ISBAD(EntryValue) (EntryValue == 0x0FFFFFF7)#define FAT12_LEGALCLUS(EntryValue) (!( (FAT12_ISEOC(EntryValue)) || (FAT12_ISFREE(EntryValue)) || (FAT12_ISBAD(EntryValue))))#define FAT16_LEGALCLUS(EntryValue) (!( (FAT16_ISEOC(EntryValue)) || (FAT16_ISFREE(EntryValue)) || (FAT16_ISBAD(EntryValue))))#define FAT32_LEGALCLUS(EntryValue) (!( (FAT32_ISEOC(EntryValue)) || (FAT32_ISFREE(EntryValue)) || (FAT32_ISBAD(EntryValue)))) /* FAT Date Encoding *//* * hi byte | low byte * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0| * | | | | | | | | | | | | | | | | | * \ 7 bits /\4 bits/\ 5 bits / * year +80 month day*/#define FILE_YEAR(dir) (( ((BYTE *) &((dir)->DIR_WrtDate))[1] >> 1) + 1980)#define FILE_MONTH(dir) ((((((BYTE *) &((dir)->DIR_WrtDate))[1]&0x1) << 3) + (((BYTE *) &((dir)->DIR_WrtDate))[0] >> 5)))#define FILE_DAY(dir) (((BYTE *) &((dir)->DIR_WrtDate))[0] & 0x1f) /* FAT Time Encoding *//* * hi byte | low byte * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0| * | | | | | | | | | | | | | | | | | * \ 5 bits /\ 6 bits /\ 5 bits / * hour minutes sec*2*/#define FILE_HOUR(dir) (((BYTE *) &((dir)->DIR_WrtTime))[1] >> 3)#define FILE_MINUTE(dir) ((((((BYTE *) &((dir)->DIR_WrtTime))[1]&0x7) << 3) + (((BYTE *) &((dir)->DIR_WrtTime))[0] >> 5)))#define FILE_SEC(dir) ((((BYTE *) &((dir)->DIR_WrtTime))[0] & 0x1f) * 2)/* FAT32 Boot Sector and BIOS Parameter Block *//* This structure is used in all driver functions, even if the volume *//* is FAT12 or FAT16. In fact some FAT32 fields are checked anyway *//* like BPB_FATSz32). So we load the boot sector from the disk, we *//* detect the FAT type and if it's a FAT32, we fill the following *//* struct as is, while if the volume is FAT12/FAT16 we copy in the *//* right position the appropriate (common) fields. */typedef struct{ BYTE BS_jmpBoot[3]; /* 0 Jump to boot code. BS_jmpBoot */ BYTE BS_OEMName[8]; /* 3 OEM name & version. BS_OEMName */ WORD BPB_BytsPerSec; /* 11 Bytes per sector hopefully 512. BPB_BytsPerSec */ BYTE BPB_SecPerClus; /* 13 Cluster size in sectors. BPB_SecPerClus */ WORD BPB_ResvdSecCnt; /* 14 Number of reserved (boot) sectors. BPB_RsvdSecCount */ BYTE BPB_NumFATs; /* 16 Number of FAT tables hopefully 2. BPB_NumFATs */ WORD BPB_RootEntCnt; /* 17 Number of directory slots. BPB_RootEntCnt */ WORD BPB_TotSec16; /* 19 Total sectors on disk. BPB_TotSec16 */ BYTE BPB_Media; /* 21 Media descriptor=first byte of FAT. BPB_Media */ /* 0xF8 is the standard value for fixed media */ WORD BPB_FATSz16; /* 22 Sectors in FAT. BPB_FATSz16 */ WORD BPB_SecPerTrk; /* 24 Sectors/track. BPB_SecPerTrk */ WORD BPB_NumHeads; /* 26 Heads. BPB_NumHeads */ DWORD BPB_HiddSec; /* 28 number of hidden sectors. BPB_HiddSec */ DWORD BPB_TotSec32; /* 32 big total sectors. BPB_TotSec32 */ /* Here start the FAT32 specific fields (offset 36) */ DWORD BPB_FATSz32; /* 36 32bit count of sectors occupied by each FAT. BPB_FATSz16 must be 0 */ WORD BPB_ExtFlags; /* 40 extension flags. Usually 0. * Bits 0-3: Zero-based number of active Fat. Valid if mirroring is disabled * Bits 4-6: Reserved * Bit 7: 0 = FAT mirrored into all FATs at runtime. 1 = only 1 FAT active * Bits 8-15: Reserved */ WORD BPB_FSVer; /* 42 Version number of the FAT32 volume. For future extension. Must be 0:0 */ DWORD BPB_RootClus; /* 44 start cluster of root dir. Usually 2 */ WORD BPB_FSInfo; /* 48 changeable global info */ WORD BPB_BkBootSec; /* 50 back up boot sector. Recomended 6 */ BYTE BPB_Reserved[12]; /* 52 reserved for future expansion */ /* The following fields are present also in a FAT12/FAT16 BPB, (labelblk_t) */ /* but at offset 36. In a FAT32 BPB they are at offset 64 instead. */ BYTE BS_DrvNum; /* 64 physical drive ? */ BYTE BS_Reserved1; /* 65 reserved */ BYTE BS_BootSig; /* 66 dos > 4.0 diskette. signature. */ DWORD BS_VolID; /* 67 serial number */ BYTE BS_VolLab[11]; /* 71 disk label */ BYTE BS_FilSysType[8]; /* 82 FAT type */}__attribute__ ((packed)) Bpb_t;/* FAT12 and FAT16 Structure starting at Offset 36 of sector 0 */typedef struct { BYTE BS_DrvNum; /* 36 physical drive ? */ BYTE BS_Reserved1; /* 37 reserved */ BYTE BS_BootSig; /* 38 dos > 4.0 diskette. signature. */ DWORD BS_VolID; /* 39 serial number */ BYTE BS_VolLab[11]; /* 43 disk label */ BYTE BS_FilSysType[8]; /* 54 FAT type */} __attribute__ ((packed)) labelblk_t;/* FAT32 FSInfo Sector structure */typedef struct{ DWORD FSI_LeadSig; /* FSI_LeadSig. 0x41615252 */ BYTE FSI_Reserved1[480]; /* FSI_Reserved1 (size 480 bytes should be 0) */ DWORD FSI_StrucSig; /* FSI_StructSig. 0x61417272 */ DWORD FSI_Free_Count; /* FSI_Free_Count. */ DWORD FSI_Nxt_Free; /* FSI_Nxt_Free. Just an hint. 0xFFFFFFFF = no hint available */ BYTE FSI_Reserved2[12]; /* FSI_Reserved2 */ DWORD FSI_TrailSig; /* FSI_TrailSig. 0xAA550000 */}__attribute__ ((packed)) FSInfo_t;/* FAT 32-byte Directory Entry structure */typedef struct{ BYTE DIR_Name[11]; /* 0 file name (8+3) */ BYTE DIR_Attr; /* 11 attribute byte */ BYTE DIR_NTRes; /* 12 case of short filename */ /* MS says "reserved for NT */ BYTE DIR_CrtTimeTenth; /* 13 creation time, milliseconds (?) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -