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