3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 typedef struct _FAT_BOOTSECTOR
25 UCHAR JumpBoot
[3]; // Jump instruction to boot code
26 CHAR OemName
[8]; // "MSWIN4.1" for MS formatted volumes
27 USHORT BytesPerSector
; // Bytes per sector
28 UCHAR SectorsPerCluster
; // Number of sectors in a cluster
29 USHORT ReservedSectors
; // Reserved sectors, usually 1 (the bootsector)
30 UCHAR NumberOfFats
; // Number of FAT tables
31 USHORT RootDirEntries
; // Number of root directory entries (fat12/16)
32 USHORT TotalSectors
; // Number of total sectors on the drive, 16-bit
33 UCHAR MediaDescriptor
; // Media descriptor byte
34 USHORT SectorsPerFat
; // Sectors per FAT table (fat12/16)
35 USHORT SectorsPerTrack
; // Number of sectors in a track
36 USHORT NumberOfHeads
; // Number of heads on the disk
37 ULONG HiddenSectors
; // Hidden sectors (sectors before the partition start like the partition table)
38 ULONG TotalSectorsBig
; // This field is the new 32-bit total count of sectors on the volume
39 UCHAR DriveNumber
; // Int 0x13 drive number (e.g. 0x80)
40 UCHAR Reserved1
; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
41 UCHAR BootSignature
; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
42 ULONG VolumeSerialNumber
; // Volume serial number
43 CHAR VolumeLabel
[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
44 CHAR FileSystemType
[8]; // One of the strings "FAT12 ", "FAT16 ", or "FAT "
46 UCHAR BootCodeAndData
[448]; // The remainder of the boot sector
48 USHORT BootSectorMagic
; // 0xAA55
50 } PACKED FAT_BOOTSECTOR
, *PFAT_BOOTSECTOR
;
52 typedef struct _FAT32_BOOTSECTOR
54 UCHAR JumpBoot
[3]; // Jump instruction to boot code
55 CHAR OemName
[8]; // "MSWIN4.1" for MS formatted volumes
56 USHORT BytesPerSector
; // Bytes per sector
57 UCHAR SectorsPerCluster
; // Number of sectors in a cluster
58 USHORT ReservedSectors
; // Reserved sectors, usually 1 (the bootsector)
59 UCHAR NumberOfFats
; // Number of FAT tables
60 USHORT RootDirEntries
; // Number of root directory entries (fat12/16)
61 USHORT TotalSectors
; // Number of total sectors on the drive, 16-bit
62 UCHAR MediaDescriptor
; // Media descriptor byte
63 USHORT SectorsPerFat
; // Sectors per FAT table (fat12/16)
64 USHORT SectorsPerTrack
; // Number of sectors in a track
65 USHORT NumberOfHeads
; // Number of heads on the disk
66 ULONG HiddenSectors
; // Hidden sectors (sectors before the partition start like the partition table)
67 ULONG TotalSectorsBig
; // This field is the new 32-bit total count of sectors on the volume
68 ULONG SectorsPerFatBig
; // This field is the FAT32 32-bit count of sectors occupied by ONE FAT. BPB_FATSz16 must be 0
69 USHORT ExtendedFlags
; // Extended flags (fat32)
70 USHORT FileSystemVersion
; // File system version (fat32)
71 ULONG RootDirStartCluster
; // Starting cluster of the root directory (fat32)
72 USHORT FsInfo
; // Sector number of FSINFO structure in the reserved area of the FAT32 volume. Usually 1.
73 USHORT BackupBootSector
; // If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
74 UCHAR Reserved
[12]; // Reserved for future expansion
75 UCHAR DriveNumber
; // Int 0x13 drive number (e.g. 0x80)
76 UCHAR Reserved1
; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
77 UCHAR BootSignature
; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
78 ULONG VolumeSerialNumber
; // Volume serial number
79 CHAR VolumeLabel
[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
80 CHAR FileSystemType
[8]; // Always set to the string "FAT32 "
82 UCHAR BootCodeAndData
[420]; // The remainder of the boot sector
84 USHORT BootSectorMagic
; // 0xAA55
86 } PACKED FAT32_BOOTSECTOR
, *PFAT32_BOOTSECTOR
;
88 typedef struct _FATX_BOOTSECTOR
90 CHAR FileSystemType
[4]; /* String "FATX" */
91 ULONG VolumeSerialNumber
; /* Volume serial number */
92 ULONG SectorsPerCluster
; /* Number of sectors in a cluster */
93 USHORT NumberOfFats
; /* Number of FAT tables */
94 ULONG Unknown
; /* Always 0? */
95 UCHAR Unused
[494]; /* Actually size should be 4078 (boot block is 4096 bytes) */
97 } PACKED FATX_BOOTSECTOR
, *PFATX_BOOTSECTOR
;
100 * Structure of MSDOS directory entry
102 typedef struct //_DIRENTRY
104 CHAR FileName
[11]; /* Filename + extension */
105 UCHAR Attr
; /* File attributes */
106 UCHAR ReservedNT
; /* Reserved for use by Windows NT */
107 UCHAR TimeInTenths
; /* Millisecond stamp at file creation */
108 USHORT CreateTime
; /* Time file was created */
109 USHORT CreateDate
; /* Date file was created */
110 USHORT LastAccessDate
; /* Date file was last accessed */
111 USHORT ClusterHigh
; /* High word of this entry's start cluster */
112 USHORT Time
; /* Time last modified */
113 USHORT Date
; /* Date last modified */
114 USHORT ClusterLow
; /* First cluster number low word */
115 ULONG Size
; /* File size */
116 } PACKED DIRENTRY
, * PDIRENTRY
;
120 UCHAR SequenceNumber
; /* Sequence number for slot */
121 WCHAR Name0_4
[5]; /* First 5 characters in name */
122 UCHAR EntryAttributes
; /* Attribute byte */
123 UCHAR Reserved
; /* Always 0 */
124 UCHAR AliasChecksum
; /* Checksum for 8.3 alias */
125 WCHAR Name5_10
[6]; /* 6 more characters in name */
126 USHORT StartCluster
; /* Starting cluster number */
127 WCHAR Name11_12
[2]; /* Last 2 characters in name */
128 } PACKED LFN_DIRENTRY
, * PLFN_DIRENTRY
;
132 UCHAR FileNameSize
; /* Size of filename (max 42) */
133 UCHAR Attr
; /* File attributes */
134 CHAR FileName
[42]; /* Filename in ASCII, padded with 0xff (not zero-terminated) */
135 ULONG StartCluster
; /* Starting cluster number */
136 ULONG Size
; /* File size */
137 USHORT Time
; /* Time last modified */
138 USHORT Date
; /* Date last modified */
139 USHORT CreateTime
; /* Time file was created */
140 USHORT CreateDate
; /* Date file was created */
141 USHORT LastAccessTime
; /* Time file was last accessed */
142 USHORT LastAccessDate
; /* Date file was last accessed */
143 } PACKED FATX_DIRENTRY
, * PFATX_DIRENTRY
;
147 ULONG FileSize
; /* File size */
148 ULONG FilePointer
; /* File pointer */
149 ULONG
* FileFatChain
; /* File fat chain array */
151 } FAT_FILE_INFO
, * PFAT_FILE_INFO
;
155 BOOLEAN
FatOpenVolume(ULONG DriveNumber
, ULONG VolumeStartSector
, ULONG PartitionSectorCount
);
156 ULONG
FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector
, ULONG PartitionSectorCount
);
157 PVOID
FatBufferDirectory(ULONG DirectoryStartCluster
, ULONG
* EntryCountPointer
, BOOLEAN RootDirectory
);
158 BOOLEAN
FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer
, ULONG EntryCount
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
);
159 BOOLEAN
FatLookupFile(PCSTR FileName
, PFAT_FILE_INFO FatFileInfoPointer
);
160 void FatParseShortFileName(PCHAR Buffer
, PDIRENTRY DirEntry
);
161 BOOLEAN
FatGetFatEntry(ULONG Cluster
, ULONG
* ClusterPointer
);
162 FILE* FatOpenFile(PCSTR FileName
);
163 ULONG
FatCountClustersInChain(ULONG StartCluster
);
164 ULONG
* FatGetClusterChainArray(ULONG StartCluster
);
165 BOOLEAN
FatReadCluster(ULONG ClusterNumber
, PVOID Buffer
);
166 BOOLEAN
FatReadClusterChain(ULONG StartClusterNumber
, ULONG NumberOfClusters
, PVOID Buffer
);
167 BOOLEAN
FatReadPartialCluster(ULONG ClusterNumber
, ULONG StartingOffset
, ULONG Length
, PVOID Buffer
);
168 BOOLEAN
FatReadFile(FILE *FileHandle
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
);
169 ULONG
FatGetFileSize(FILE *FileHandle
);
170 VOID
FatSetFilePointer(FILE *FileHandle
, ULONG NewFilePointer
);
171 ULONG
FatGetFilePointer(FILE *FileHandle
);
172 BOOLEAN
FatReadVolumeSectors(ULONG DriveNumber
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
);
175 #define ATTR_NORMAL 0x00
176 #define ATTR_READONLY 0x01
177 #define ATTR_HIDDEN 0x02
178 #define ATTR_SYSTEM 0x04
179 #define ATTR_VOLUMENAME 0x08
180 #define ATTR_DIRECTORY 0x10
181 #define ATTR_ARCHIVE 0x20
182 #define ATTR_LONG_NAME (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUMENAME)
190 #define ISFATX(FT) ((FT) == FATX16 || (FT) == FATX32)
192 #endif // #defined __FAT_H