3 * Copyright (C) 1998-2002 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 BYTE JumpBoot
[3]; // Jump instruction to boot code
26 UCHAR OemName
[8]; // "MSWIN4.1" for MS formatted volumes
27 WORD BytesPerSector
; // Bytes per sector
28 BYTE SectorsPerCluster
; // Number of sectors in a cluster
29 WORD ReservedSectors
; // Reserved sectors, usually 1 (the bootsector)
30 BYTE NumberOfFats
; // Number of FAT tables
31 WORD RootDirEntries
; // Number of root directory entries (fat12/16)
32 WORD TotalSectors
; // Number of total sectors on the drive, 16-bit
33 BYTE MediaDescriptor
; // Media descriptor byte
34 WORD SectorsPerFat
; // Sectors per FAT table (fat12/16)
35 WORD SectorsPerTrack
; // Number of sectors in a track
36 WORD NumberOfHeads
; // Number of heads on the disk
37 DWORD HiddenSectors
; // Hidden sectors (sectors before the partition start like the partition table)
38 DWORD TotalSectorsBig
; // This field is the new 32-bit total count of sectors on the volume
39 BYTE DriveNumber
; // Int 0x13 drive number (e.g. 0x80)
40 BYTE Reserved1
; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
41 BYTE BootSignature
; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
42 DWORD VolumeSerialNumber
; // Volume serial number
43 UCHAR VolumeLabel
[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
44 UCHAR FileSystemType
[8]; // One of the strings "FAT12 ", "FAT16 ", or "FAT "
46 BYTE BootCodeAndData
[448]; // The remainder of the boot sector
48 WORD BootSectorMagic
; // 0xAA55
50 } PACKED FAT_BOOTSECTOR
, *PFAT_BOOTSECTOR
;
52 typedef struct _FAT32_BOOTSECTOR
54 BYTE JumpBoot
[3]; // Jump instruction to boot code
55 UCHAR OemName
[8]; // "MSWIN4.1" for MS formatted volumes
56 WORD BytesPerSector
; // Bytes per sector
57 BYTE SectorsPerCluster
; // Number of sectors in a cluster
58 WORD ReservedSectors
; // Reserved sectors, usually 1 (the bootsector)
59 BYTE NumberOfFats
; // Number of FAT tables
60 WORD RootDirEntries
; // Number of root directory entries (fat12/16)
61 WORD TotalSectors
; // Number of total sectors on the drive, 16-bit
62 BYTE MediaDescriptor
; // Media descriptor byte
63 WORD SectorsPerFat
; // Sectors per FAT table (fat12/16)
64 WORD SectorsPerTrack
; // Number of sectors in a track
65 WORD NumberOfHeads
; // Number of heads on the disk
66 DWORD HiddenSectors
; // Hidden sectors (sectors before the partition start like the partition table)
67 DWORD TotalSectorsBig
; // This field is the new 32-bit total count of sectors on the volume
68 DWORD SectorsPerFatBig
; // This field is the FAT32 32-bit count of sectors occupied by ONE FAT. BPB_FATSz16 must be 0
69 WORD ExtendedFlags
; // Extended flags (fat32)
70 WORD FileSystemVersion
; // File system version (fat32)
71 DWORD RootDirStartCluster
; // Starting cluster of the root directory (fat32)
72 WORD FsInfo
; // Sector number of FSINFO structure in the reserved area of the FAT32 volume. Usually 1.
73 WORD 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 BYTE Reserved
[12]; // Reserved for future expansion
75 BYTE DriveNumber
; // Int 0x13 drive number (e.g. 0x80)
76 BYTE Reserved1
; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
77 BYTE BootSignature
; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
78 DWORD VolumeSerialNumber
; // Volume serial number
79 UCHAR VolumeLabel
[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
80 UCHAR FileSystemType
[8]; // Always set to the string "FAT32 "
82 BYTE BootCodeAndData
[420]; // The remainder of the boot sector
84 WORD BootSectorMagic
; // 0xAA55
86 } PACKED FAT32_BOOTSECTOR
, *PFAT32_BOOTSECTOR
;
89 * Structure of MSDOS directory entry
91 typedef struct //_DIRENTRY
93 UCHAR FileName
[11]; /* Filename + extension */
94 UINT8 Attr
; /* File attributes */
95 UINT8 ReservedNT
; /* Reserved for use by Windows NT */
96 UINT8 TimeInTenths
; /* Millisecond stamp at file creation */
97 UINT16 CreateTime
; /* Time file was created */
98 UINT16 CreateDate
; /* Date file was created */
99 UINT16 LastAccessDate
; /* Date file was last accessed */
100 UINT16 ClusterHigh
; /* High word of this entry's start cluster */
101 UINT16 Time
; /* Time last modified */
102 UINT16 Date
; /* Date last modified */
103 UINT16 ClusterLow
; /* First cluster number low word */
104 UINT32 Size
; /* File size */
105 } PACKED DIRENTRY
, * PDIRENTRY
;
109 UINT8 SequenceNumber
; /* Sequence number for slot */
110 WCHAR Name0_4
[5]; /* First 5 characters in name */
111 UINT8 EntryAttributes
; /* Attribute byte */
112 UINT8 Reserved
; /* Always 0 */
113 UINT8 AliasChecksum
; /* Checksum for 8.3 alias */
114 WCHAR Name5_10
[6]; /* 6 more characters in name */
115 UINT16 StartCluster
; /* Starting cluster number */
116 WCHAR Name11_12
[2]; /* Last 2 characters in name */
117 } PACKED LFN_DIRENTRY
, * PLFN_DIRENTRY
;
121 ULONG FileSize
; // File size
122 ULONG FilePointer
; // File pointer
123 PUINT32 FileFatChain
; // File fat chain array
125 } FAT_FILE_INFO
, * PFAT_FILE_INFO
;
129 BOOL
FatOpenVolume(ULONG DriveNumber
, ULONG VolumeStartSector
);
130 ULONG
FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector
);
131 PVOID
FatBufferDirectory(UINT32 DirectoryStartCluster
, PUINT32 EntryCountPointer
, BOOL RootDirectory
);
132 BOOL
FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer
, UINT32 EntryCount
, PUCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
);
133 BOOL
FatLookupFile(PUCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
);
134 ULONG
FatGetNumPathParts(PUCHAR Path
);
135 VOID
FatGetFirstNameFromPath(PUCHAR Buffer
, PUCHAR Path
);
136 void FatParseShortFileName(PUCHAR Buffer
, PDIRENTRY DirEntry
);
137 BOOL
FatGetFatEntry(UINT32 Cluster
, PUINT32 ClusterPointer
);
138 FILE* FatOpenFile(PUCHAR FileName
);
139 UINT32
FatCountClustersInChain(UINT32 StartCluster
);
140 PUINT32
FatGetClusterChainArray(UINT32 StartCluster
);
141 BOOL
FatReadCluster(ULONG ClusterNumber
, PVOID Buffer
);
142 BOOL
FatReadClusterChain(ULONG StartClusterNumber
, ULONG NumberOfClusters
, PVOID Buffer
);
143 BOOL
FatReadPartialCluster(ULONG ClusterNumber
, ULONG StartingOffset
, ULONG Length
, PVOID Buffer
);
144 BOOL
FatReadFile(FILE *FileHandle
, ULONG BytesToRead
, PULONG BytesRead
, PVOID Buffer
);
145 ULONG
FatGetFileSize(FILE *FileHandle
);
146 VOID
FatSetFilePointer(FILE *FileHandle
, ULONG NewFilePointer
);
147 ULONG
FatGetFilePointer(FILE *FileHandle
);
148 BOOL
FatReadVolumeSectors(ULONG DriveNumber
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
);
151 #define ATTR_NORMAL 0x00
152 #define ATTR_READONLY 0x01
153 #define ATTR_HIDDEN 0x02
154 #define ATTR_SYSTEM 0x04
155 #define ATTR_VOLUMENAME 0x08
156 #define ATTR_DIRECTORY 0x10
157 #define ATTR_ARCHIVE 0x20
158 #define ATTR_LONG_NAME (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUMENAME)
164 #endif // #defined __FAT_H