880b4ef182de69a5e3930b7f6a381d56258f0ff4
[reactos.git] / reactos / drivers / fs / vfat / vfat.h
1 /* $Id: vfat.h,v 1.39 2002/02/05 21:31:03 hbirr Exp $ */
2
3 #include <ddk/ntifs.h>
4
5 struct _BootSector
6 {
7 unsigned char magic0, res0, magic1;
8 unsigned char OEMName[8];
9 unsigned short BytesPerSector;
10 unsigned char SectorsPerCluster;
11 unsigned short ReservedSectors;
12 unsigned char FATCount;
13 unsigned short RootEntries, Sectors;
14 unsigned char Media;
15 unsigned short FATSectors, SectorsPerTrack, Heads;
16 unsigned long HiddenSectors, SectorsHuge;
17 unsigned char Drive, Res1, Sig;
18 unsigned long VolumeID;
19 unsigned char VolumeLabel[11], SysType[8];
20 unsigned char Res2[450];
21 } __attribute__((packed));
22
23 struct _BootSector32
24 {
25 unsigned char magic0, res0, magic1; // 0
26 unsigned char OEMName[8]; // 3
27 unsigned short BytesPerSector; // 11
28 unsigned char SectorsPerCluster; // 13
29 unsigned short ReservedSectors; // 14
30 unsigned char FATCount; // 16
31 unsigned short RootEntries, Sectors; // 17
32 unsigned char Media; // 21
33 unsigned short FATSectors, SectorsPerTrack, Heads; // 22
34 unsigned long HiddenSectors, SectorsHuge; // 28
35 unsigned long FATSectors32; // 36
36 unsigned short ExtFlag; // 40
37 unsigned short FSVersion; // 42
38 unsigned long RootCluster; // 44
39 unsigned short FSInfoSector; // 48
40 unsigned long BootBackup; // 50
41 unsigned char Res3[10]; // 54
42 unsigned char Drive; // 64
43 unsigned char Res4; // 65
44 unsigned char ExtBootSignature; // 66
45 unsigned long VolumeID; // 67
46 unsigned char VolumeLabel[11], SysType[8]; // 71
47 unsigned char Res2[418]; // 90
48 unsigned long Signature1; // 508
49 } __attribute__((packed));
50
51 struct _BootBackupSector
52 {
53 unsigned long ExtBootSignature2; // 0
54 unsigned char Res6[480]; // 4
55 unsigned long FSINFOSignature; // 484
56 unsigned long FreeCluster; // 488
57 unsigned long NextCluster; // 492
58 unsigned char Res7[12]; // 496
59 unsigned long Signatur2; // 508
60 } __attribute__((packed));
61
62 typedef struct _BootSector BootSector;
63
64 struct _FATDirEntry {
65 unsigned char Filename[8], Ext[3], Attrib, Res[2];
66 unsigned short CreationTime,CreationDate,AccessDate;
67 unsigned short FirstClusterHigh;// higher
68 unsigned short UpdateTime;//time create/update
69 unsigned short UpdateDate;//date create/update
70 unsigned short FirstCluster;
71 unsigned long FileSize;
72 } __attribute__((packed));
73
74 typedef struct _FATDirEntry FATDirEntry, FAT_DIR_ENTRY, *PFAT_DIR_ENTRY;
75
76 struct _slot
77 {
78 unsigned char id; // sequence number for slot
79 WCHAR name0_4[5]; // first 5 characters in name
80 unsigned char attr; // attribute byte
81 unsigned char reserved; // always 0
82 unsigned char alias_checksum; // checksum for 8.3 alias
83 WCHAR name5_10[6]; // 6 more characters in name
84 unsigned char start[2]; // starting cluster number
85 WCHAR name11_12[2]; // last 2 characters in name
86 } __attribute__((packed));
87
88
89 typedef struct _slot slot;
90
91 #define BLOCKSIZE 512
92
93 #define FAT16 (1)
94 #define FAT12 (2)
95 #define FAT32 (3)
96
97 typedef struct
98 {
99 ERESOURCE DirResource;
100 ERESOURCE FatResource;
101
102 KSPIN_LOCK FcbListLock;
103 LIST_ENTRY FcbListHead;
104
105 PDEVICE_OBJECT StorageDevice;
106 PFILE_OBJECT FATFileObject;
107 BootSector *Boot;
108 int rootDirectorySectors, FATStart, rootStart, dataStart;
109 int BytesPerSector;
110 int FATEntriesPerSector, FATUnit;
111 ULONG BytesPerCluster;
112 ULONG FatType;
113 ULONG LastAvailableCluster;
114 ULONG NumberOfClusters;
115 } DEVICE_EXTENSION, *PDEVICE_EXTENSION, VCB, *PVCB;
116
117 #define FCB_CACHE_INITIALIZED 0x0001
118 #define FCB_DELETE_PENDING 0x0002
119 #define FCB_IS_FAT 0x0004
120 #define FCB_IS_PAGE_FILE 0x0008
121
122 typedef struct _VFATFCB
123 {
124 REACTOS_COMMON_FCB_HEADER RFCB;
125 SECTION_OBJECT_POINTERS SectionObjectPointers;
126 FATDirEntry entry;
127 /* point on filename (250 chars max) in PathName */
128 WCHAR *ObjectName;
129 /* path+filename 260 max */
130 WCHAR PathName[MAX_PATH];
131 LONG RefCount;
132 PDEVICE_EXTENSION pDevExt;
133 LIST_ENTRY FcbListEntry;
134 struct _VFATFCB* parentFcb;
135 ULONG Flags;
136 PFILE_OBJECT FileObject;
137 ULONG dirIndex;
138 ERESOURCE PagingIoResource;
139 ERESOURCE MainResource;
140
141 /* Structure members used only for paging files. */
142 ULONG FatChainSize;
143 PULONG FatChain;
144 } VFATFCB, *PVFATFCB;
145
146 typedef struct _VFATCCB
147 {
148 VFATFCB * pFcb;
149 LIST_ENTRY NextCCB;
150 PFILE_OBJECT PtrFileObject;
151 LARGE_INTEGER CurrentByteOffset;
152 /* for DirectoryControl */
153 ULONG Entry;
154 /* for DirectoryControl */
155 PWCHAR DirectorySearchPattern;
156 ULONG LastCluster;
157 ULONG LastOffset;
158
159 } VFATCCB, *PVFATCCB;
160
161 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
162
163 #define TAG_CCB TAG('V', 'C', 'C', 'B')
164
165 #define ENTRIES_PER_SECTOR (BLOCKSIZE / sizeof(FATDirEntry))
166
167 typedef struct __DOSTIME
168 {
169 WORD Second:5;
170 WORD Minute:6;
171 WORD Hour:5;
172 } DOSTIME, *PDOSTIME;
173
174 typedef struct __DOSDATE
175 {
176 WORD Day:5;
177 WORD Month:4;
178 WORD Year:5;
179 } DOSDATE, *PDOSDATE;
180
181 #define IRPCONTEXT_CANWAIT 0x0001
182
183 typedef struct
184 {
185 PIRP Irp;
186 PDEVICE_OBJECT DeviceObject;
187 PDEVICE_EXTENSION DeviceExt;
188 ULONG Flags;
189 WORK_QUEUE_ITEM WorkQueueItem;
190 PIO_STACK_LOCATION Stack;
191 UCHAR MajorFunction;
192 UCHAR MinorFunction;
193 PFILE_OBJECT FileObject;
194 }
195 VFAT_IRP_CONTEXT, *PVFAT_IRP_CONTEXT;
196
197
198 /* functions called by i/o manager : */
199 NTSTATUS STDCALL
200 DriverEntry(PDRIVER_OBJECT _DriverObject,PUNICODE_STRING RegistryPath);
201 NTSTATUS VfatDirectoryControl (PVFAT_IRP_CONTEXT);
202 NTSTATUS VfatRead (PVFAT_IRP_CONTEXT);
203 NTSTATUS VfatWrite (PVFAT_IRP_CONTEXT);
204 NTSTATUS VfatCreate (PVFAT_IRP_CONTEXT);
205 NTSTATUS VfatClose (PVFAT_IRP_CONTEXT);
206 NTSTATUS VfatFileSystemControl (PVFAT_IRP_CONTEXT);
207 NTSTATUS VfatQueryInformation (PVFAT_IRP_CONTEXT);
208 NTSTATUS VfatSetInformation (PVFAT_IRP_CONTEXT);
209 NTSTATUS VfatCleanup (PVFAT_IRP_CONTEXT);
210 NTSTATUS STDCALL
211 VfatShutdown(PDEVICE_OBJECT DeviceObject, PIRP Irp);
212 NTSTATUS VfatQueryVolumeInformation (PVFAT_IRP_CONTEXT);
213 NTSTATUS VfatSetVolumeInformation (PVFAT_IRP_CONTEXT);
214
215
216 NTSTATUS
217 NextCluster(PDEVICE_EXTENSION DeviceExt,
218 PVFATFCB Fcb,
219 ULONG FirstCluster,
220 PULONG CurrentCluster,
221 BOOLEAN Extend);
222
223 /* internal functions in blockdev.c */
224 NTSTATUS
225 VfatReadSectors(IN PDEVICE_OBJECT pDeviceObject,
226 IN ULONG DiskSector,
227 IN ULONG SectorCount,
228 IN UCHAR* Buffer);
229
230 NTSTATUS
231 VfatWriteSectors(IN PDEVICE_OBJECT pDeviceObject,
232 IN ULONG DiskSector,
233 IN ULONG SectorCount,
234 IN UCHAR* Buffer);
235
236 /* internal functions in dir.c : */
237 BOOL FsdDosDateTimeToFileTime(WORD wDosDate,WORD wDosTime, TIME *FileTime);
238 BOOL FsdFileTimeToDosDateTime(TIME *FileTime,WORD *pwDosDate,WORD *pwDosTime);
239
240 /* internal functions in iface.c : */
241 NTSTATUS
242 FindFile(PDEVICE_EXTENSION DeviceExt, PVFATFCB Fcb,
243 PVFATFCB Parent, PWSTR FileToFind,ULONG *StartSector,ULONG *Entry);
244 NTSTATUS
245 VfatCloseFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject);
246 NTSTATUS
247 VfatOpenFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
248 PWSTR FileName);
249 NTSTATUS
250 VfatReadFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
251 PVOID Buffer, ULONG Length, ULONG ReadOffset,
252 PULONG LengthRead, ULONG NoCache);
253 NTSTATUS
254 VfatWriteFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
255 PVOID Buffer, ULONG Length, ULONG WriteOffset, BOOLEAN NoCache, BOOLEAN PageIo);
256 NTSTATUS
257 GetNextWriteCluster(PDEVICE_EXTENSION DeviceExt,
258 ULONG FirstCluster,
259 ULONG CurrentCluster,
260 PULONG NextCluster);
261 BOOLEAN
262 IsDeletedEntry(PVOID Block, ULONG Offset);
263 BOOLEAN
264 IsLastEntry(PVOID Block, ULONG Offset);
265 wchar_t*
266 vfat_wcsncpy(wchar_t * dest, const wchar_t *src,size_t wcount);
267
268 /* internal functions in dirwr.c */
269 NTSTATUS
270 addEntry(PDEVICE_EXTENSION DeviceExt,
271 PFILE_OBJECT pFileObject,ULONG RequestedOptions,UCHAR ReqAttr);
272 NTSTATUS
273 updEntry(PDEVICE_EXTENSION DeviceExt,PFILE_OBJECT pFileObject);
274 NTSTATUS
275 delEntry(PDEVICE_EXTENSION, PFILE_OBJECT);
276
277 /*
278 * String functions
279 */
280 VOID
281 vfat_initstr(wchar_t *wstr, ULONG wsize);
282 wchar_t*
283 vfat_wcsncat(wchar_t * dest, const wchar_t * src,size_t wstart, size_t wcount);
284 wchar_t*
285 vfat_wcsncpy(wchar_t * dest, const wchar_t *src,size_t wcount);
286 wchar_t*
287 vfat_movstr(wchar_t *src, ULONG dpos, ULONG spos, ULONG len);
288 BOOLEAN
289 wstrcmpi(PWSTR s1, PWSTR s2);
290 BOOLEAN
291 wstrcmpjoki(PWSTR s1, PWSTR s2);
292 PWCHAR vfatGetNextPathElement (PWCHAR pFileName);
293 void vfatWSubString (PWCHAR pTarget, const PWCHAR pSource, size_t pLength);
294 BOOL vfatIsFileNameValid (PWCHAR pFileName);
295
296 /*
297 * functions from fat.c
298 */
299 NTSTATUS
300 OffsetToCluster(PDEVICE_EXTENSION DeviceExt,
301 PVFATFCB Fcb,
302 ULONG FirstCluster,
303 ULONG FileOffset,
304 PULONG Cluster,
305 BOOLEAN Extend);
306 ULONG
307 ClusterToSector(PDEVICE_EXTENSION DeviceExt,
308 ULONG Cluster);
309 NTSTATUS
310 GetNextCluster(PDEVICE_EXTENSION DeviceExt,
311 ULONG CurrentCluster,
312 PULONG NextCluster,
313 BOOLEAN Extend);
314 NTSTATUS
315 GetNextSector(PDEVICE_EXTENSION DeviceExt,
316 ULONG CurrentSector,
317 PULONG NextSector,
318 BOOLEAN Extend);
319 NTSTATUS
320 VfatRawReadCluster(PDEVICE_EXTENSION DeviceExt,
321 ULONG FirstCluster,
322 PVOID Buffer,
323 ULONG Cluster,
324 ULONG Count);
325 NTSTATUS
326 VfatRawWriteCluster(PDEVICE_EXTENSION DeviceExt,
327 ULONG FirstCluster,
328 PVOID Buffer,
329 ULONG Cluster,
330 ULONG Count);
331 NTSTATUS
332 FAT12CountAvailableClusters(PDEVICE_EXTENSION DeviceExt,
333 PLARGE_INTEGER Clusters);
334 NTSTATUS
335 FAT16CountAvailableClusters(PDEVICE_EXTENSION DeviceExt,
336 PLARGE_INTEGER Clusters);
337 NTSTATUS
338 FAT32CountAvailableClusters(PDEVICE_EXTENSION DeviceExt,
339 PLARGE_INTEGER Clusters);
340 NTSTATUS
341 WriteCluster(PDEVICE_EXTENSION DeviceExt,
342 ULONG ClusterToWrite,
343 ULONG NewValue);
344
345 /* --------------------------------------------------------- create.c */
346
347 void vfat8Dot3ToString (PCHAR pBasename, PCHAR pExtension, PWSTR pName);
348 NTSTATUS
349 ReadVolumeLabel(PDEVICE_EXTENSION DeviceExt, PVPB Vpb);
350 NTSTATUS
351 VfatOpenFile(PDEVICE_EXTENSION DeviceExt,
352 PFILE_OBJECT FileObject,
353 PWSTR FileName);
354
355 /* ----------------------------------------------- DirEntry Functions */
356
357 ULONG vfatDirEntryGetFirstCluster (PDEVICE_EXTENSION pDeviceExt,
358 PFAT_DIR_ENTRY pDirEntry);
359 BOOL vfatIsDirEntryDeleted (FATDirEntry * pFatDirEntry);
360 BOOL vfatIsDirEntryVolume (FATDirEntry * pFatDirEntry);
361 BOOL vfatIsDirEntryEndMarker (FATDirEntry * pFatDirEntry);
362 void vfatGetDirEntryName (PFAT_DIR_ENTRY pDirEntry, PWSTR pEntryName);
363 NTSTATUS vfatGetNextDirEntry (PDEVICE_EXTENSION pDeviceExt,
364 PVFATFCB pDirectoryFCB,
365 ULONG * pDirectoryIndex,
366 PWSTR pLongFileName,
367 PFAT_DIR_ENTRY pDirEntry);
368
369 /* ----------------------------------------------------- FCB Functions */
370
371 PVFATFCB vfatNewFCB (PWCHAR pFileName);
372 void vfatDestroyFCB (PVFATFCB pFCB);
373 void vfatGrabFCB (PDEVICE_EXTENSION pVCB, PVFATFCB pFCB);
374 void vfatReleaseFCB (PDEVICE_EXTENSION pVCB, PVFATFCB pFCB);
375 void vfatAddFCBToTable (PDEVICE_EXTENSION pVCB,
376 PVFATFCB pFCB);
377 PVFATFCB vfatGrabFCBFromTable (PDEVICE_EXTENSION pDeviceExt,
378 PWSTR pFileName);
379 PVFATFCB vfatMakeRootFCB (PDEVICE_EXTENSION pVCB);
380 PVFATFCB vfatOpenRootFCB (PDEVICE_EXTENSION pVCB);
381 BOOL vfatFCBIsDirectory (PDEVICE_EXTENSION pVCB, PVFATFCB FCB);
382 NTSTATUS vfatAttachFCBToFileObject (PDEVICE_EXTENSION vcb,
383 PVFATFCB fcb,
384 PFILE_OBJECT fileObject);
385 NTSTATUS vfatDirFindFile (PDEVICE_EXTENSION pVCB,
386 PVFATFCB parentFCB,
387 PWSTR elementName,
388 PVFATFCB * fileFCB);
389 NTSTATUS vfatGetFCBForFile (PDEVICE_EXTENSION pVCB,
390 PVFATFCB *pParentFCB,
391 PVFATFCB *pFCB,
392 const PWSTR pFileName);
393 NTSTATUS vfatMakeFCBFromDirEntry(PVCB vcb,
394 PVFATFCB directoryFCB,
395 PWSTR longName,
396 PFAT_DIR_ENTRY dirEntry,
397 ULONG dirIndex,
398 PVFATFCB * fileFCB);
399
400 /* ------------------------------------------------------------- rw.c */
401
402 NTSTATUS vfatExtendSpace (PDEVICE_EXTENSION pDeviceExt, PFILE_OBJECT pFileObject, ULONG NewSize);
403
404 /* ------------------------------------------------------------- misc.c */
405 NTSTATUS VfatQueueRequest(PVFAT_IRP_CONTEXT IrpContext);
406 PVFAT_IRP_CONTEXT VfatAllocateIrpContext(PDEVICE_OBJECT DeviceObject, PIRP Irp);
407 VOID VfatFreeIrpContext(PVFAT_IRP_CONTEXT IrpContext);
408 NTSTATUS STDCALL VfatBuildRequest (PDEVICE_OBJECT DeviceObject, PIRP Irp);
409