- Update to r53061
[reactos.git] / drivers / filesystems / fastfat_new / fat.h
1 #pragma once
2
3 //
4 // Might be a good idea to have this as a shared
5 // header with FS Recognizer.
6 //
7 //
8 // Conversion types and macros taken from internal ntifs headers
9 //
10 typedef union _UCHAR1
11 {
12 UCHAR Uchar[1];
13 UCHAR ForceAlignment;
14 } UCHAR1, *PUCHAR1;
15
16 typedef union _UCHAR2
17 {
18 UCHAR Uchar[2];
19 USHORT ForceAlignment;
20 } UCHAR2, *PUCHAR2;
21
22 typedef union _UCHAR4
23 {
24 UCHAR Uchar[4];
25 ULONG ForceAlignment;
26 } UCHAR4, *PUCHAR4;
27
28 #define CopyUchar1(Dst,Src) { \
29 *((UCHAR1 *)(Dst)) = *((UNALIGNED UCHAR1 *)(Src)); \
30 }
31
32 #define CopyUchar2(Dst,Src) { \
33 *((UCHAR2 *)(Dst)) = *((UNALIGNED UCHAR2 *)(Src)); \
34 }
35
36 #define CopyUchar4(Dst,Src) { \
37 *((UCHAR4 *)(Dst)) = *((UNALIGNED UCHAR4 *)(Src)); \
38 }
39
40 #define FatUnpackBios(Bios,Pbios) { \
41 CopyUchar2(&(Bios)->BytesPerSector, &(Pbios)->BytesPerSector[0] ); \
42 CopyUchar1(&(Bios)->SectorsPerCluster, &(Pbios)->SectorsPerCluster[0]); \
43 CopyUchar2(&(Bios)->ReservedSectors, &(Pbios)->ReservedSectors[0] ); \
44 CopyUchar1(&(Bios)->Fats, &(Pbios)->Fats[0] ); \
45 CopyUchar2(&(Bios)->RootEntries, &(Pbios)->RootEntries[0] ); \
46 CopyUchar2(&(Bios)->Sectors, &(Pbios)->Sectors[0] ); \
47 CopyUchar1(&(Bios)->Media, &(Pbios)->Media[0] ); \
48 CopyUchar2(&(Bios)->SectorsPerFat, &(Pbios)->SectorsPerFat[0] ); \
49 CopyUchar2(&(Bios)->SectorsPerTrack, &(Pbios)->SectorsPerTrack[0] ); \
50 CopyUchar2(&(Bios)->Heads, &(Pbios)->Heads[0] ); \
51 CopyUchar4(&(Bios)->HiddenSectors, &(Pbios)->HiddenSectors[0] ); \
52 CopyUchar4(&(Bios)->LargeSectors, &(Pbios)->LargeSectors[0] ); \
53 CopyUchar4(&(Bios)->LargeSectors, &(Pbios)->LargeSectors[0] ); \
54 CopyUchar4(&(Bios)->LargeSectorsPerFat,&((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->LargeSectorsPerFat[0] ); \
55 CopyUchar2(&(Bios)->ExtendedFlags, &((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->ExtendedFlags[0] ); \
56 CopyUchar2(&(Bios)->FsVersion, &((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->FsVersion[0] ); \
57 CopyUchar4(&(Bios)->RootDirFirstCluster, \
58 &((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->RootDirFirstCluster[0] ); \
59 CopyUchar2(&(Bios)->FsInfoSector, &((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->FsInfoSector[0] ); \
60 CopyUchar2(&(Bios)->BackupBootSector, &((PPACKED_BIOS_PARAMETER_BLOCK_EX)Pbios)->BackupBootSector[0] ); \
61 }
62 //
63 // Packed versions of the BPB and Boot Sector
64 //
65 typedef struct _PACKED_BIOS_PARAMETER_BLOCK
66 {
67 UCHAR BytesPerSector[2];
68 UCHAR SectorsPerCluster[1];
69 UCHAR ReservedSectors[2];
70 UCHAR Fats[1];
71 UCHAR RootEntries[2];
72 UCHAR Sectors[2];
73 UCHAR Media[1];
74 UCHAR SectorsPerFat[2];
75 UCHAR SectorsPerTrack[2];
76 UCHAR Heads[2];
77 UCHAR HiddenSectors[4];
78 UCHAR LargeSectors[4];
79 } PACKED_BIOS_PARAMETER_BLOCK, *PPACKED_BIOS_PARAMETER_BLOCK;
80 // sizeof = 0x019
81
82 typedef struct _PACKED_BIOS_PARAMETER_BLOCK_EX
83 {
84 PACKED_BIOS_PARAMETER_BLOCK Block;
85 UCHAR LargeSectorsPerFat[4];
86 UCHAR ExtendedFlags[2];
87 UCHAR FsVersion[2];
88 UCHAR RootDirFirstCluster[4];
89 UCHAR FsInfoSector[2];
90 UCHAR BackupBootSector[2];
91 UCHAR Reserved[12];
92 } PACKED_BIOS_PARAMETER_BLOCK_EX, *PPACKED_BIOS_PARAMETER_BLOCK_EX;
93 // sizeof = 0x035 53
94
95 //
96 // Unpacked version of the BPB
97 //
98 typedef struct BIOS_PARAMETER_BLOCK
99 {
100 USHORT BytesPerSector;
101 UCHAR SectorsPerCluster;
102 USHORT ReservedSectors;
103 UCHAR Fats;
104 USHORT RootEntries;
105 USHORT Sectors;
106 UCHAR Media;
107 USHORT SectorsPerFat;
108 USHORT SectorsPerTrack;
109 USHORT Heads;
110 ULONG HiddenSectors;
111 ULONG LargeSectors;
112 ULONG LargeSectorsPerFat;
113 union
114 {
115 USHORT ExtendedFlags;
116 struct
117 {
118 ULONG ActiveFat:4;
119 ULONG Reserved0:3;
120 ULONG MirrorDisabled:1;
121 ULONG Reserved1:8;
122 };
123 };
124 USHORT FsVersion;
125 ULONG RootDirFirstCluster;
126 USHORT FsInfoSector;
127 USHORT BackupBootSector;
128 } BIOS_PARAMETER_BLOCK, *PBIOS_PARAMETER_BLOCK;
129
130 #define FatValidBytesPerSector(xBytes) \
131 (!((xBytes) & ((xBytes)-1)) && (xBytes)>=0x80 && (xBytes)<=0x1000)
132
133 #define FatValidSectorsPerCluster(xSectors) \
134 (!((xSectors) & ((xSectors)-1)) && (xSectors)>0 && (xSectors)<=0x80)
135
136 typedef struct _PACKED_BOOT_SECTOR
137 {
138 UCHAR Jump[3];
139 UCHAR Oem[8];
140 PACKED_BIOS_PARAMETER_BLOCK PackedBpb;
141 UCHAR PhysicalDriveNumber;
142 UCHAR CurrentHead;
143 UCHAR Signature;
144 UCHAR Id[4];
145 UCHAR VolumeLabel[11];
146 UCHAR SystemId[8];
147 } PACKED_BOOT_SECTOR, *PPACKED_BOOT_SECTOR;
148 // sizeof = 0x03E
149
150 typedef struct _PACKED_BOOT_SECTOR_EX
151 {
152 UCHAR Jump[3];
153 UCHAR Oem[8];
154 PACKED_BIOS_PARAMETER_BLOCK_EX PackedBpb;
155 UCHAR PhysicalDriveNumber;
156 UCHAR CurrentHead;
157 UCHAR Signature;
158 UCHAR Id[4];
159 UCHAR VolumeLabel[11];
160 UCHAR SystemId[8];
161 } PACKED_BOOT_SECTOR_EX, *PPACKED_BOOT_SECTOR_EX;
162 // sizeof = 0x060
163
164 #define FatBootSectorJumpValid(xMagic) \
165 ((xMagic)[0] == 0xe9 || (xMagic)[0] == 0xeb || (xMagic)[0] == 0x49)
166
167 typedef struct _FSINFO_SECTOR
168 {
169 ULONG SectorBeginSignature;
170 UCHAR Reserved[480];
171 ULONG FsInfoSignature;
172 ULONG FreeClusterCount;
173 ULONG NextFreeCluster;
174 UCHAR Reserved0[12];
175 ULONG SectorEndSignature;
176 } FSINFO_SECTOR, *PFSINFO_SECTOR;
177 // sizeof = 0x200
178 #define FSINFO_SECTOR_BEGIN_SIGNATURE 0x41615252
179 #define FSINFO_SECTOR_END_SIGNATURE 0xaa550000
180 #define FSINFO_SIGNATURE 0x61417272
181 //
182 // Cluster Markers:
183 //
184 #define FAT_CLUSTER_AVAILABLE 0x00000000
185 #define FAT_CLUSTER_RESERVED 0x0ffffff0
186 #define FAT_CLUSTER_BAD 0x0ffffff7
187 #define FAT_CLUSTER_LAST 0x0fffffff
188 //
189 // Directory Structure:
190 //
191 typedef struct _FAT_TIME
192 {
193 union {
194 struct {
195 USHORT DoubleSeconds : 5;
196 USHORT Minute : 6;
197 USHORT Hour : 5;
198 };
199 USHORT Value;
200 };
201 } FAT_TIME, *PFAT_TIME;
202 //
203 //
204 //
205 typedef struct _FAT_DATE {
206 union {
207 struct {
208 USHORT Day : 5;
209 USHORT Month : 4;
210 /* Relative to 1980 */
211 USHORT Year : 7;
212 };
213 USHORT Value;
214 };
215 } FAT_DATE, *PFAT_DATE;
216 //
217 //
218 //
219 typedef struct _FAT_DATETIME {
220 union {
221 struct {
222 FAT_TIME Time;
223 FAT_DATE Date;
224 };
225 ULONG Value;
226 };
227 } FAT_DATETIME, *PFAT_DATETIME;
228 //
229 //
230 //
231 typedef struct _DIR_ENTRY
232 {
233 UCHAR FileName[11];
234 UCHAR Attributes;
235 UCHAR Case;
236 UCHAR CreationTimeTenMs;
237 FAT_DATETIME CreationDateTime;
238 FAT_DATE LastAccessDate;
239 union {
240 USHORT ExtendedAttributes;
241 USHORT FirstClusterOfFileHi;
242 };
243 FAT_DATETIME LastWriteDateTime;
244 USHORT FirstCluster;
245 ULONG FileSize;
246 } DIR_ENTRY, *PDIR_ENTRY;
247 // sizeof = 0x020
248
249 typedef struct _LONG_FILE_NAME_ENTRY {
250 UCHAR SeqNum;
251 UCHAR NameA[10];
252 UCHAR Attributes;
253 UCHAR Type;
254 UCHAR Checksum;
255 USHORT NameB[6];
256 USHORT Reserved;
257 USHORT NameC[2];
258 } LONG_FILE_NAME_ENTRY, *PLONG_FILE_NAME_ENTRY;
259 // sizeof = 0x020
260
261 #define FAT_LFN_NAME_LENGTH \
262 (RTL_FIELD_SIZE(LONG_FILE_NAME_ENTRY, NameA) \
263 + RTL_FIELD_SIZE(LONG_FILE_NAME_ENTRY, NameB) \
264 + RTL_FIELD_SIZE(LONG_FILE_NAME_ENTRY, NameC))
265
266 #define FAT_FN_DIR_ENTRY_LAST 0x40
267 #define FAT_FN_MAX_DIR_ENTIES 0x14
268
269 #define FAT_BYTES_PER_DIRENT 0x20
270 #define FAT_BYTES_PER_DIRENT_LOG 0x05
271 #define FAT_DIRENT_NEVER_USED 0x00
272 #define FAT_DIRENT_REALLY_0E5 0x05
273 #define FAT_DIRENT_DIRECTORY_ALIAS 0x2e
274 #define FAT_DIRENT_DELETED 0xe5
275
276 #define FAT_CASE_LOWER_BASE 0x08
277 #define FAT_CASE_LOWER_EXT 0x10
278
279 #define FAT_DIRENT_ATTR_READ_ONLY 0x01
280 #define FAT_DIRENT_ATTR_HIDDEN 0x02
281 #define FAT_DIRENT_ATTR_SYSTEM 0x04
282 #define FAT_DIRENT_ATTR_VOLUME_ID 0x08
283 #define FAT_DIRENT_ATTR_DIRECTORY 0x10
284 #define FAT_DIRENT_ATTR_ARCHIVE 0x20
285 #define FAT_DIRENT_ATTR_DEVICE 0x40
286 #define FAT_DIRENT_ATTR_LFN (FAT_DIRENT_ATTR_READ_ONLY | \
287 FAT_DIRENT_ATTR_HIDDEN | \
288 FAT_DIRENT_ATTR_SYSTEM | \
289 FAT_DIRENT_ATTR_VOLUME_ID)