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.
25 ULONG BytesPerSector
; /* Number of bytes per sector */
26 ULONG SectorsPerCluster
; /* Number of sectors per cluster */
27 ULONG FatVolumeStartSector
; /* Absolute starting sector of the partition */
28 ULONG FatSectorStart
; /* Starting sector of 1st FAT table */
29 ULONG ActiveFatSectorStart
; /* Starting sector of active FAT table */
30 ULONG NumberOfFats
; /* Number of FAT tables */
31 ULONG SectorsPerFat
; /* Sectors per FAT table */
32 ULONG RootDirSectorStart
; /* Starting sector of the root directory (non-fat32) */
33 ULONG RootDirSectors
; /* Number of sectors of the root directory (non-fat32) */
34 ULONG RootDirStartCluster
; /* Starting cluster number of the root directory (fat32 only) */
35 ULONG DataSectorStart
; /* Starting sector of the data area */
37 ULONG FatType
= 0; /* FAT12, FAT16, FAT32, FATX16 or FATX32 */
38 ULONG FatDriveNumber
= 0;
40 BOOL
FatOpenVolume(ULONG DriveNumber
, ULONG VolumeStartSector
, ULONG PartitionSectorCount
)
44 PFAT_BOOTSECTOR FatVolumeBootSector
;
45 PFAT32_BOOTSECTOR Fat32VolumeBootSector
;
46 PFATX_BOOTSECTOR FatXVolumeBootSector
;
48 DbgPrint((DPRINT_FILESYSTEM
, "FatOpenVolume() DriveNumber = 0x%x VolumeStartSector = %d\n", DriveNumber
, VolumeStartSector
));
50 // Store the drive number
51 FatDriveNumber
= DriveNumber
;
54 // Allocate the memory to hold the boot sector
56 FatVolumeBootSector
= (PFAT_BOOTSECTOR
) MmAllocateMemory(512);
57 Fat32VolumeBootSector
= (PFAT32_BOOTSECTOR
) FatVolumeBootSector
;
58 FatXVolumeBootSector
= (PFATX_BOOTSECTOR
) FatVolumeBootSector
;
61 // Make sure we got the memory
63 if (FatVolumeBootSector
== NULL
)
65 FileSystemError("Out of memory.");
69 // Now try to read the boot sector
70 // If this fails then abort
71 if (!MachDiskReadLogicalSectors(DriveNumber
, VolumeStartSector
, 1, (PVOID
)DISKREADBUFFER
))
73 MmFreeMemory(FatVolumeBootSector
);
76 RtlCopyMemory(FatVolumeBootSector
, (PVOID
)DISKREADBUFFER
, 512);
79 FatType
= FatDetermineFatType(FatVolumeBootSector
, PartitionSectorCount
);
83 DbgPrint((DPRINT_FILESYSTEM
, "Dumping boot sector:\n"));
87 DbgPrint((DPRINT_FILESYSTEM
, "sizeof(FATX_BOOTSECTOR) = 0x%x.\n", sizeof(FATX_BOOTSECTOR
)));
89 DbgPrint((DPRINT_FILESYSTEM
, "FileSystemType: %c%c%c%c.\n", FatXVolumeBootSector
->FileSystemType
[0], FatXVolumeBootSector
->FileSystemType
[1], FatXVolumeBootSector
->FileSystemType
[2], FatXVolumeBootSector
->FileSystemType
[3]));
90 DbgPrint((DPRINT_FILESYSTEM
, "VolumeSerialNumber: 0x%x\n", FatXVolumeBootSector
->VolumeSerialNumber
));
91 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerCluster: %d\n", FatXVolumeBootSector
->SectorsPerCluster
));
92 DbgPrint((DPRINT_FILESYSTEM
, "NumberOfFats: %d\n", FatXVolumeBootSector
->NumberOfFats
));
93 DbgPrint((DPRINT_FILESYSTEM
, "Unknown: 0x%x\n", FatXVolumeBootSector
->Unknown
));
95 DbgPrint((DPRINT_FILESYSTEM
, "FatType %s\n", FatType
== FATX16
? "FATX16" : "FATX32"));
98 else if (FatType
== FAT32
)
100 DbgPrint((DPRINT_FILESYSTEM
, "sizeof(FAT32_BOOTSECTOR) = 0x%x.\n", sizeof(FAT32_BOOTSECTOR
)));
102 DbgPrint((DPRINT_FILESYSTEM
, "JumpBoot: 0x%x 0x%x 0x%x\n", Fat32VolumeBootSector
->JumpBoot
[0], Fat32VolumeBootSector
->JumpBoot
[1], Fat32VolumeBootSector
->JumpBoot
[2]));
103 DbgPrint((DPRINT_FILESYSTEM
, "OemName: %c%c%c%c%c%c%c%c\n", Fat32VolumeBootSector
->OemName
[0], Fat32VolumeBootSector
->OemName
[1], Fat32VolumeBootSector
->OemName
[2], Fat32VolumeBootSector
->OemName
[3], Fat32VolumeBootSector
->OemName
[4], Fat32VolumeBootSector
->OemName
[5], Fat32VolumeBootSector
->OemName
[6], Fat32VolumeBootSector
->OemName
[7]));
104 DbgPrint((DPRINT_FILESYSTEM
, "BytesPerSector: %d\n", Fat32VolumeBootSector
->BytesPerSector
));
105 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerCluster: %d\n", Fat32VolumeBootSector
->SectorsPerCluster
));
106 DbgPrint((DPRINT_FILESYSTEM
, "ReservedSectors: %d\n", Fat32VolumeBootSector
->ReservedSectors
));
107 DbgPrint((DPRINT_FILESYSTEM
, "NumberOfFats: %d\n", Fat32VolumeBootSector
->NumberOfFats
));
108 DbgPrint((DPRINT_FILESYSTEM
, "RootDirEntries: %d\n", Fat32VolumeBootSector
->RootDirEntries
));
109 DbgPrint((DPRINT_FILESYSTEM
, "TotalSectors: %d\n", Fat32VolumeBootSector
->TotalSectors
));
110 DbgPrint((DPRINT_FILESYSTEM
, "MediaDescriptor: 0x%x\n", Fat32VolumeBootSector
->MediaDescriptor
));
111 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerFat: %d\n", Fat32VolumeBootSector
->SectorsPerFat
));
112 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerTrack: %d\n", Fat32VolumeBootSector
->SectorsPerTrack
));
113 DbgPrint((DPRINT_FILESYSTEM
, "NumberOfHeads: %d\n", Fat32VolumeBootSector
->NumberOfHeads
));
114 DbgPrint((DPRINT_FILESYSTEM
, "HiddenSectors: %d\n", Fat32VolumeBootSector
->HiddenSectors
));
115 DbgPrint((DPRINT_FILESYSTEM
, "TotalSectorsBig: %d\n", Fat32VolumeBootSector
->TotalSectorsBig
));
116 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerFatBig: %d\n", Fat32VolumeBootSector
->SectorsPerFatBig
));
117 DbgPrint((DPRINT_FILESYSTEM
, "ExtendedFlags: 0x%x\n", Fat32VolumeBootSector
->ExtendedFlags
));
118 DbgPrint((DPRINT_FILESYSTEM
, "FileSystemVersion: 0x%x\n", Fat32VolumeBootSector
->FileSystemVersion
));
119 DbgPrint((DPRINT_FILESYSTEM
, "RootDirStartCluster: %d\n", Fat32VolumeBootSector
->RootDirStartCluster
));
120 DbgPrint((DPRINT_FILESYSTEM
, "FsInfo: %d\n", Fat32VolumeBootSector
->FsInfo
));
121 DbgPrint((DPRINT_FILESYSTEM
, "BackupBootSector: %d\n", Fat32VolumeBootSector
->BackupBootSector
));
122 DbgPrint((DPRINT_FILESYSTEM
, "Reserved: 0x%x\n", Fat32VolumeBootSector
->Reserved
));
123 DbgPrint((DPRINT_FILESYSTEM
, "DriveNumber: 0x%x\n", Fat32VolumeBootSector
->DriveNumber
));
124 DbgPrint((DPRINT_FILESYSTEM
, "Reserved1: 0x%x\n", Fat32VolumeBootSector
->Reserved1
));
125 DbgPrint((DPRINT_FILESYSTEM
, "BootSignature: 0x%x\n", Fat32VolumeBootSector
->BootSignature
));
126 DbgPrint((DPRINT_FILESYSTEM
, "VolumeSerialNumber: 0x%x\n", Fat32VolumeBootSector
->VolumeSerialNumber
));
127 DbgPrint((DPRINT_FILESYSTEM
, "VolumeLabel: %c%c%c%c%c%c%c%c%c%c%c\n", Fat32VolumeBootSector
->VolumeLabel
[0], Fat32VolumeBootSector
->VolumeLabel
[1], Fat32VolumeBootSector
->VolumeLabel
[2], Fat32VolumeBootSector
->VolumeLabel
[3], Fat32VolumeBootSector
->VolumeLabel
[4], Fat32VolumeBootSector
->VolumeLabel
[5], Fat32VolumeBootSector
->VolumeLabel
[6], Fat32VolumeBootSector
->VolumeLabel
[7], Fat32VolumeBootSector
->VolumeLabel
[8], Fat32VolumeBootSector
->VolumeLabel
[9], Fat32VolumeBootSector
->VolumeLabel
[10]));
128 DbgPrint((DPRINT_FILESYSTEM
, "FileSystemType: %c%c%c%c%c%c%c%c\n", Fat32VolumeBootSector
->FileSystemType
[0], Fat32VolumeBootSector
->FileSystemType
[1], Fat32VolumeBootSector
->FileSystemType
[2], Fat32VolumeBootSector
->FileSystemType
[3], Fat32VolumeBootSector
->FileSystemType
[4], Fat32VolumeBootSector
->FileSystemType
[5], Fat32VolumeBootSector
->FileSystemType
[6], Fat32VolumeBootSector
->FileSystemType
[7]));
129 DbgPrint((DPRINT_FILESYSTEM
, "BootSectorMagic: 0x%x\n", Fat32VolumeBootSector
->BootSectorMagic
));
133 DbgPrint((DPRINT_FILESYSTEM
, "sizeof(FAT_BOOTSECTOR) = 0x%x.\n", sizeof(FAT_BOOTSECTOR
)));
135 DbgPrint((DPRINT_FILESYSTEM
, "JumpBoot: 0x%x 0x%x 0x%x\n", FatVolumeBootSector
->JumpBoot
[0], FatVolumeBootSector
->JumpBoot
[1], FatVolumeBootSector
->JumpBoot
[2]));
136 DbgPrint((DPRINT_FILESYSTEM
, "OemName: %c%c%c%c%c%c%c%c\n", FatVolumeBootSector
->OemName
[0], FatVolumeBootSector
->OemName
[1], FatVolumeBootSector
->OemName
[2], FatVolumeBootSector
->OemName
[3], FatVolumeBootSector
->OemName
[4], FatVolumeBootSector
->OemName
[5], FatVolumeBootSector
->OemName
[6], FatVolumeBootSector
->OemName
[7]));
137 DbgPrint((DPRINT_FILESYSTEM
, "BytesPerSector: %d\n", FatVolumeBootSector
->BytesPerSector
));
138 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerCluster: %d\n", FatVolumeBootSector
->SectorsPerCluster
));
139 DbgPrint((DPRINT_FILESYSTEM
, "ReservedSectors: %d\n", FatVolumeBootSector
->ReservedSectors
));
140 DbgPrint((DPRINT_FILESYSTEM
, "NumberOfFats: %d\n", FatVolumeBootSector
->NumberOfFats
));
141 DbgPrint((DPRINT_FILESYSTEM
, "RootDirEntries: %d\n", FatVolumeBootSector
->RootDirEntries
));
142 DbgPrint((DPRINT_FILESYSTEM
, "TotalSectors: %d\n", FatVolumeBootSector
->TotalSectors
));
143 DbgPrint((DPRINT_FILESYSTEM
, "MediaDescriptor: 0x%x\n", FatVolumeBootSector
->MediaDescriptor
));
144 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerFat: %d\n", FatVolumeBootSector
->SectorsPerFat
));
145 DbgPrint((DPRINT_FILESYSTEM
, "SectorsPerTrack: %d\n", FatVolumeBootSector
->SectorsPerTrack
));
146 DbgPrint((DPRINT_FILESYSTEM
, "NumberOfHeads: %d\n", FatVolumeBootSector
->NumberOfHeads
));
147 DbgPrint((DPRINT_FILESYSTEM
, "HiddenSectors: %d\n", FatVolumeBootSector
->HiddenSectors
));
148 DbgPrint((DPRINT_FILESYSTEM
, "TotalSectorsBig: %d\n", FatVolumeBootSector
->TotalSectorsBig
));
149 DbgPrint((DPRINT_FILESYSTEM
, "DriveNumber: 0x%x\n", FatVolumeBootSector
->DriveNumber
));
150 DbgPrint((DPRINT_FILESYSTEM
, "Reserved1: 0x%x\n", FatVolumeBootSector
->Reserved1
));
151 DbgPrint((DPRINT_FILESYSTEM
, "BootSignature: 0x%x\n", FatVolumeBootSector
->BootSignature
));
152 DbgPrint((DPRINT_FILESYSTEM
, "VolumeSerialNumber: 0x%x\n", FatVolumeBootSector
->VolumeSerialNumber
));
153 DbgPrint((DPRINT_FILESYSTEM
, "VolumeLabel: %c%c%c%c%c%c%c%c%c%c%c\n", FatVolumeBootSector
->VolumeLabel
[0], FatVolumeBootSector
->VolumeLabel
[1], FatVolumeBootSector
->VolumeLabel
[2], FatVolumeBootSector
->VolumeLabel
[3], FatVolumeBootSector
->VolumeLabel
[4], FatVolumeBootSector
->VolumeLabel
[5], FatVolumeBootSector
->VolumeLabel
[6], FatVolumeBootSector
->VolumeLabel
[7], FatVolumeBootSector
->VolumeLabel
[8], FatVolumeBootSector
->VolumeLabel
[9], FatVolumeBootSector
->VolumeLabel
[10]));
154 DbgPrint((DPRINT_FILESYSTEM
, "FileSystemType: %c%c%c%c%c%c%c%c\n", FatVolumeBootSector
->FileSystemType
[0], FatVolumeBootSector
->FileSystemType
[1], FatVolumeBootSector
->FileSystemType
[2], FatVolumeBootSector
->FileSystemType
[3], FatVolumeBootSector
->FileSystemType
[4], FatVolumeBootSector
->FileSystemType
[5], FatVolumeBootSector
->FileSystemType
[6], FatVolumeBootSector
->FileSystemType
[7]));
155 DbgPrint((DPRINT_FILESYSTEM
, "BootSectorMagic: 0x%x\n", FatVolumeBootSector
->BootSectorMagic
));
158 #endif // defined DEBUG
161 // Set the correct partition offset
163 FatVolumeStartSector
= VolumeStartSector
;
166 // Check the boot sector magic
168 if (! ISFATX(FatType
) && FatVolumeBootSector
->BootSectorMagic
!= 0xaa55)
170 sprintf(ErrMsg
, "Invalid boot sector magic on drive 0x%x (expected 0xaa55 found 0x%x)",
171 DriveNumber
, FatVolumeBootSector
->BootSectorMagic
);
172 FileSystemError(ErrMsg
);
173 MmFreeMemory(FatVolumeBootSector
);
178 // Check the FAT cluster size
179 // We do not support clusters bigger than 64k
181 if ((ISFATX(FatType
) && 64 * 1024 < FatXVolumeBootSector
->SectorsPerCluster
* 512) ||
182 (! ISFATX(FatType
) && 64 * 1024 < FatVolumeBootSector
->SectorsPerCluster
* FatVolumeBootSector
->BytesPerSector
))
184 FileSystemError("This file system has cluster sizes bigger than 64k.\nFreeLoader does not support this.");
185 MmFreeMemory(FatVolumeBootSector
);
190 // Clear our variables
193 ActiveFatSectorStart
= 0;
195 RootDirSectorStart
= 0;
201 // Get the sectors per FAT,
202 // root directory starting sector,
203 // and data sector start
207 BytesPerSector
= 512;
208 SectorsPerCluster
= FatXVolumeBootSector
->SectorsPerCluster
;
209 FatSectorStart
= (4096 / BytesPerSector
);
210 ActiveFatSectorStart
= FatSectorStart
;
212 FatSize
= PartitionSectorCount
/ SectorsPerCluster
*
213 (FATX16
== FatType
? 2 : 4);
214 SectorsPerFat
= (((FatSize
+ 4095) / 4096) * 4096) / BytesPerSector
;
216 RootDirSectorStart
= FatSectorStart
+ NumberOfFats
* SectorsPerFat
;
217 RootDirSectors
= FatXVolumeBootSector
->SectorsPerCluster
;
219 DataSectorStart
= RootDirSectorStart
+ RootDirSectors
;
221 else if (FatType
!= FAT32
)
223 BytesPerSector
= FatVolumeBootSector
->BytesPerSector
;
224 SectorsPerCluster
= FatVolumeBootSector
->SectorsPerCluster
;
225 FatSectorStart
= FatVolumeBootSector
->ReservedSectors
;
226 ActiveFatSectorStart
= FatSectorStart
;
227 NumberOfFats
= FatVolumeBootSector
->NumberOfFats
;
228 SectorsPerFat
= FatVolumeBootSector
->SectorsPerFat
;
230 RootDirSectorStart
= FatSectorStart
+ NumberOfFats
* SectorsPerFat
;
231 RootDirSectors
= ((FatVolumeBootSector
->RootDirEntries
* 32) + (BytesPerSector
- 1)) / BytesPerSector
;
233 DataSectorStart
= RootDirSectorStart
+ RootDirSectors
;
237 BytesPerSector
= Fat32VolumeBootSector
->BytesPerSector
;
238 SectorsPerCluster
= Fat32VolumeBootSector
->SectorsPerCluster
;
239 FatSectorStart
= Fat32VolumeBootSector
->ReservedSectors
;
240 ActiveFatSectorStart
= FatSectorStart
+
241 ((Fat32VolumeBootSector
->ExtendedFlags
& 0x80) ? ((Fat32VolumeBootSector
->ExtendedFlags
& 0x0f) * Fat32VolumeBootSector
->SectorsPerFatBig
) : 0);
242 NumberOfFats
= Fat32VolumeBootSector
->NumberOfFats
;
243 SectorsPerFat
= Fat32VolumeBootSector
->SectorsPerFatBig
;
245 RootDirStartCluster
= Fat32VolumeBootSector
->RootDirStartCluster
;
246 DataSectorStart
= FatSectorStart
+ NumberOfFats
* SectorsPerFat
;
250 // we only work with version 0
252 if (Fat32VolumeBootSector
->FileSystemVersion
!= 0)
254 FileSystemError("FreeLoader is too old to work with this FAT32 filesystem.\nPlease update FreeLoader.");
258 MmFreeMemory(FatVolumeBootSector
);
261 // Initialize the disk cache for this drive
263 if (!CacheInitializeDrive(DriveNumber
))
269 // Force the FAT sectors into the cache
270 // as long as it is FAT12 or FAT16. FAT32 can
271 // have a multi-megabyte FAT so we don't want that.
273 if (FatType
!= FAT32
&& FatType
!= FATX32
)
275 if (!CacheForceDiskSectorsIntoCache(DriveNumber
, ActiveFatSectorStart
, SectorsPerFat
))
284 ULONG
FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector
, ULONG PartitionSectorCount
)
286 ULONG RootDirSectors
;
287 ULONG DataSectorCount
;
290 ULONG CountOfClusters
;
291 PFAT32_BOOTSECTOR Fat32BootSector
= (PFAT32_BOOTSECTOR
)FatBootSector
;
292 PFATX_BOOTSECTOR FatXBootSector
= (PFATX_BOOTSECTOR
)FatBootSector
;
294 if (0 == strncmp(FatXBootSector
->FileSystemType
, "FATX", 4))
296 CountOfClusters
= PartitionSectorCount
/ FatXBootSector
->SectorsPerCluster
;
297 if (CountOfClusters
< 65525)
299 /* Volume is FATX16 */
304 /* Volume is FAT32 */
310 RootDirSectors
= ((FatBootSector
->RootDirEntries
* 32) + (FatBootSector
->BytesPerSector
- 1)) / FatBootSector
->BytesPerSector
;
311 SectorsPerFat
= FatBootSector
->SectorsPerFat
? FatBootSector
->SectorsPerFat
: Fat32BootSector
->SectorsPerFatBig
;
312 TotalSectors
= FatBootSector
->TotalSectors
? FatBootSector
->TotalSectors
: FatBootSector
->TotalSectorsBig
;
313 DataSectorCount
= TotalSectors
- (FatBootSector
->ReservedSectors
+ (FatBootSector
->NumberOfFats
* SectorsPerFat
) + RootDirSectors
);
316 if (FatBootSector
->SectorsPerCluster
== 0)
319 CountOfClusters
= DataSectorCount
/ FatBootSector
->SectorsPerCluster
;
321 if (CountOfClusters
< 4085)
323 /* Volume is FAT12 */
326 else if (CountOfClusters
< 65525)
328 /* Volume is FAT16 */
333 /* Volume is FAT32 */
339 PVOID
FatBufferDirectory(ULONG DirectoryStartCluster
, ULONG
*DirectorySize
, BOOL RootDirectory
)
341 PVOID DirectoryBuffer
;
343 DbgPrint((DPRINT_FILESYSTEM
, "FatBufferDirectory() DirectoryStartCluster = %d RootDirectory = %s\n", DirectoryStartCluster
, (RootDirectory
? "TRUE" : "FALSE")));
346 * For FAT32, the root directory is nothing special. We can treat it the same
349 if (RootDirectory
&& FAT32
== FatType
)
351 DirectoryStartCluster
= RootDirStartCluster
;
352 RootDirectory
= FALSE
;
356 // Calculate the size of the directory
360 *DirectorySize
= RootDirSectors
* BytesPerSector
;
364 *DirectorySize
= FatCountClustersInChain(DirectoryStartCluster
) * SectorsPerCluster
* BytesPerSector
;
368 // Attempt to allocate memory for directory buffer
370 DbgPrint((DPRINT_FILESYSTEM
, "Trying to allocate (DirectorySize) %d bytes.\n", *DirectorySize
));
371 DirectoryBuffer
= MmAllocateMemory(*DirectorySize
);
373 if (DirectoryBuffer
== NULL
)
379 // Now read directory contents into DirectoryBuffer
383 if (!FatReadVolumeSectors(FatDriveNumber
, RootDirSectorStart
, RootDirSectors
, DirectoryBuffer
))
385 MmFreeMemory(DirectoryBuffer
);
391 if (!FatReadClusterChain(DirectoryStartCluster
, 0xFFFFFFFF, DirectoryBuffer
))
393 MmFreeMemory(DirectoryBuffer
);
398 return DirectoryBuffer
;
401 BOOL
FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer
, ULONG DirectorySize
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
)
406 PLFN_DIRENTRY LfnDirEntry
;
407 CHAR LfnNameBuffer
[265];
408 CHAR ShortNameBuffer
[20];
411 EntryCount
= DirectorySize
/ sizeof(DIRENTRY
);
413 DbgPrint((DPRINT_FILESYSTEM
, "FatSearchDirectoryBufferForFile() DirectoryBuffer = 0x%x EntryCount = %d FileName = %s\n", DirectoryBuffer
, EntryCount
, FileName
));
415 memset(ShortNameBuffer
, 0, 13 * sizeof(CHAR
));
416 memset(LfnNameBuffer
, 0, 261 * sizeof(CHAR
));
418 DirEntry
= (PDIRENTRY
) DirectoryBuffer
;
419 for (CurrentEntry
=0; CurrentEntry
<EntryCount
; CurrentEntry
++, DirEntry
++)
421 LfnDirEntry
= (PLFN_DIRENTRY
)DirEntry
;
423 //DbgPrint((DPRINT_FILESYSTEM, "Dumping directory entry %d:\n", CurrentEntry));
424 //DbgDumpBuffer(DPRINT_FILESYSTEM, DirEntry, sizeof(DIRENTRY));
427 // Check if this is the last file in the directory
428 // If DirEntry[0] == 0x00 then that means all the
429 // entries after this one are unused. If this is the
430 // last entry then we didn't find the file in this directory.
432 if (DirEntry
->FileName
[0] == '\0')
438 // Check if this is a deleted entry or not
440 if (DirEntry
->FileName
[0] == '\xE5')
442 memset(ShortNameBuffer
, 0, 13 * sizeof(CHAR
));
443 memset(LfnNameBuffer
, 0, 261 * sizeof(CHAR
));
448 // Check if this is a LFN entry
449 // If so it needs special handling
451 if (DirEntry
->Attr
== ATTR_LONG_NAME
)
454 // Check to see if this is a deleted LFN entry, if so continue
456 if (LfnDirEntry
->SequenceNumber
& 0x80)
462 // Mask off high two bits of sequence number
463 // and make the sequence number zero-based
465 LfnDirEntry
->SequenceNumber
&= 0x3F;
466 LfnDirEntry
->SequenceNumber
--;
469 // Get all 13 LFN entry characters
471 if (LfnDirEntry
->Name0_4
[0] != 0xFFFF)
473 LfnNameBuffer
[0 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[0];
475 if (LfnDirEntry
->Name0_4
[1] != 0xFFFF)
477 LfnNameBuffer
[1 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[1];
479 if (LfnDirEntry
->Name0_4
[2] != 0xFFFF)
481 LfnNameBuffer
[2 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[2];
483 if (LfnDirEntry
->Name0_4
[3] != 0xFFFF)
485 LfnNameBuffer
[3 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[3];
487 if (LfnDirEntry
->Name0_4
[4] != 0xFFFF)
489 LfnNameBuffer
[4 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[4];
491 if (LfnDirEntry
->Name5_10
[0] != 0xFFFF)
493 LfnNameBuffer
[5 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[0];
495 if (LfnDirEntry
->Name5_10
[1] != 0xFFFF)
497 LfnNameBuffer
[6 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[1];
499 if (LfnDirEntry
->Name5_10
[2] != 0xFFFF)
501 LfnNameBuffer
[7 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[2];
503 if (LfnDirEntry
->Name5_10
[3] != 0xFFFF)
505 LfnNameBuffer
[8 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[3];
507 if (LfnDirEntry
->Name5_10
[4] != 0xFFFF)
509 LfnNameBuffer
[9 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[4];
511 if (LfnDirEntry
->Name5_10
[5] != 0xFFFF)
513 LfnNameBuffer
[10 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[5];
515 if (LfnDirEntry
->Name11_12
[0] != 0xFFFF)
517 LfnNameBuffer
[11 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name11_12
[0];
519 if (LfnDirEntry
->Name11_12
[1] != 0xFFFF)
521 LfnNameBuffer
[12 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name11_12
[1];
524 //DbgPrint((DPRINT_FILESYSTEM, "Dumping long name buffer:\n"));
525 //DbgDumpBuffer(DPRINT_FILESYSTEM, LfnNameBuffer, 260);
531 // Check for the volume label attribute
532 // and skip over this entry if found
534 if (DirEntry
->Attr
& ATTR_VOLUMENAME
)
536 memset(ShortNameBuffer
, 0, 13 * sizeof(UCHAR
));
537 memset(LfnNameBuffer
, 0, 261 * sizeof(UCHAR
));
542 // If we get here then we've found a short file name
543 // entry and LfnNameBuffer contains the long file
544 // name or zeroes. All we have to do now is see if the
545 // file name matches either the short or long file name
546 // and fill in the FAT_FILE_INFO structure if it does
547 // or zero our buffers and continue looking.
551 // Get short file name
553 FatParseShortFileName(ShortNameBuffer
, DirEntry
);
555 DbgPrint((DPRINT_FILESYSTEM
, "Entry: %d LFN = %s\n", CurrentEntry
, LfnNameBuffer
));
556 DbgPrint((DPRINT_FILESYSTEM
, "Entry: %d DOS name = %s\n", CurrentEntry
, ShortNameBuffer
));
559 // See if the file name matches either the short or long name
561 if (((strlen(FileName
) == strlen(LfnNameBuffer
)) && (_stricmp(FileName
, LfnNameBuffer
) == 0)) ||
562 ((strlen(FileName
) == strlen(ShortNameBuffer
)) && (_stricmp(FileName
, ShortNameBuffer
) == 0))) {
564 // We found the entry, now fill in the FAT_FILE_INFO struct
566 FatFileInfoPointer
->FileSize
= DirEntry
->Size
;
567 FatFileInfoPointer
->FilePointer
= 0;
569 DbgPrint((DPRINT_FILESYSTEM
, "MSDOS Directory Entry:\n"));
570 DbgPrint((DPRINT_FILESYSTEM
, "FileName[11] = %c%c%c%c%c%c%c%c%c%c%c\n", DirEntry
->FileName
[0], DirEntry
->FileName
[1], DirEntry
->FileName
[2], DirEntry
->FileName
[3], DirEntry
->FileName
[4], DirEntry
->FileName
[5], DirEntry
->FileName
[6], DirEntry
->FileName
[7], DirEntry
->FileName
[8], DirEntry
->FileName
[9], DirEntry
->FileName
[10]));
571 DbgPrint((DPRINT_FILESYSTEM
, "Attr = 0x%x\n", DirEntry
->Attr
));
572 DbgPrint((DPRINT_FILESYSTEM
, "ReservedNT = 0x%x\n", DirEntry
->ReservedNT
));
573 DbgPrint((DPRINT_FILESYSTEM
, "TimeInTenths = %d\n", DirEntry
->TimeInTenths
));
574 DbgPrint((DPRINT_FILESYSTEM
, "CreateTime = %d\n", DirEntry
->CreateTime
));
575 DbgPrint((DPRINT_FILESYSTEM
, "CreateDate = %d\n", DirEntry
->CreateDate
));
576 DbgPrint((DPRINT_FILESYSTEM
, "LastAccessDate = %d\n", DirEntry
->LastAccessDate
));
577 DbgPrint((DPRINT_FILESYSTEM
, "ClusterHigh = 0x%x\n", DirEntry
->ClusterHigh
));
578 DbgPrint((DPRINT_FILESYSTEM
, "Time = %d\n", DirEntry
->Time
));
579 DbgPrint((DPRINT_FILESYSTEM
, "Date = %d\n", DirEntry
->Date
));
580 DbgPrint((DPRINT_FILESYSTEM
, "ClusterLow = 0x%x\n", DirEntry
->ClusterLow
));
581 DbgPrint((DPRINT_FILESYSTEM
, "Size = %d\n", DirEntry
->Size
));
584 // Get the cluster chain
586 StartCluster
= ((ULONG
)DirEntry
->ClusterHigh
<< 16) + DirEntry
->ClusterLow
;
587 DbgPrint((DPRINT_FILESYSTEM
, "StartCluster = 0x%x\n", StartCluster
));
588 FatFileInfoPointer
->FileFatChain
= FatGetClusterChainArray(StartCluster
);
591 // See if memory allocation failed
593 if (FatFileInfoPointer
->FileFatChain
== NULL
)
602 // Nope, no match - zero buffers and continue looking
604 memset(ShortNameBuffer
, 0, 13 * sizeof(UCHAR
));
605 memset(LfnNameBuffer
, 0, 261 * sizeof(UCHAR
));
612 static BOOL
FatXSearchDirectoryBufferForFile(PVOID DirectoryBuffer
, ULONG DirectorySize
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
)
616 PFATX_DIRENTRY DirEntry
;
619 EntryCount
= DirectorySize
/ sizeof(FATX_DIRENTRY
);
621 DbgPrint((DPRINT_FILESYSTEM
, "FatXSearchDirectoryBufferForFile() DirectoryBuffer = 0x%x EntryCount = %d FileName = %s\n", DirectoryBuffer
, EntryCount
, FileName
));
623 FileNameLen
= strlen(FileName
);
624 DirEntry
= (PFATX_DIRENTRY
) DirectoryBuffer
;
625 for (CurrentEntry
= 0; CurrentEntry
< EntryCount
; CurrentEntry
++, DirEntry
++)
627 if (0xff == DirEntry
->FileNameSize
)
631 if (0xe5 == DirEntry
->FileNameSize
)
635 if (FileNameLen
== DirEntry
->FileNameSize
&&
636 0 == _strnicmp(FileName
, DirEntry
->FileName
, FileNameLen
))
639 * We found the entry, now fill in the FAT_FILE_INFO struct
641 FatFileInfoPointer
->FileSize
= DirEntry
->Size
;
642 FatFileInfoPointer
->FilePointer
= 0;
644 DbgPrint((DPRINT_FILESYSTEM
, "FATX Directory Entry:\n"));
645 DbgPrint((DPRINT_FILESYSTEM
, "FileNameSize = %d\n", DirEntry
->FileNameSize
));
646 DbgPrint((DPRINT_FILESYSTEM
, "Attr = 0x%x\n", DirEntry
->Attr
));
647 DbgPrint((DPRINT_FILESYSTEM
, "StartCluster = 0x%x\n", DirEntry
->StartCluster
));
648 DbgPrint((DPRINT_FILESYSTEM
, "Size = %d\n", DirEntry
->Size
));
649 DbgPrint((DPRINT_FILESYSTEM
, "Time = %d\n", DirEntry
->Time
));
650 DbgPrint((DPRINT_FILESYSTEM
, "Date = %d\n", DirEntry
->Date
));
651 DbgPrint((DPRINT_FILESYSTEM
, "CreateTime = %d\n", DirEntry
->CreateTime
));
652 DbgPrint((DPRINT_FILESYSTEM
, "CreateDate = %d\n", DirEntry
->CreateDate
));
653 DbgPrint((DPRINT_FILESYSTEM
, "LastAccessTime = %d\n", DirEntry
->LastAccessTime
));
654 DbgPrint((DPRINT_FILESYSTEM
, "LastAccessDate = %d\n", DirEntry
->LastAccessDate
));
657 * Get the cluster chain
659 FatFileInfoPointer
->FileFatChain
= FatGetClusterChainArray(DirEntry
->StartCluster
);
662 * See if memory allocation failed
664 if (NULL
== FatFileInfoPointer
->FileFatChain
)
678 * This function searches the file system for the
679 * specified filename and fills in a FAT_FILE_INFO structure
680 * with info describing the file, etc. returns true
681 * if the file exists or false otherwise
683 BOOL
FatLookupFile(PCSTR FileName
, PFAT_FILE_INFO FatFileInfoPointer
)
686 ULONG NumberOfPathParts
;
688 PVOID DirectoryBuffer
;
689 ULONG DirectoryStartCluster
= 0;
691 FAT_FILE_INFO FatFileInfo
;
693 DbgPrint((DPRINT_FILESYSTEM
, "FatLookupFile() FileName = %s\n", FileName
));
695 memset(FatFileInfoPointer
, 0, sizeof(FAT_FILE_INFO
));
698 // Figure out how many sub-directories we are nested in
700 NumberOfPathParts
= FsGetNumPathParts(FileName
);
703 // Loop once for each part
705 for (i
=0; i
<NumberOfPathParts
; i
++)
708 // Get first path part
710 FsGetFirstNameFromPath(PathPart
, FileName
);
713 // Advance to the next part of the path
715 for (; (*FileName
!= '\\') && (*FileName
!= '/') && (*FileName
!= '\0'); FileName
++)
721 // Buffer the directory contents
723 DirectoryBuffer
= FatBufferDirectory(DirectoryStartCluster
, &DirectorySize
, (i
== 0) );
724 if (DirectoryBuffer
== NULL
)
730 // Search for file name in directory
734 if (!FatXSearchDirectoryBufferForFile(DirectoryBuffer
, DirectorySize
, PathPart
, &FatFileInfo
))
736 MmFreeMemory(DirectoryBuffer
);
742 if (!FatSearchDirectoryBufferForFile(DirectoryBuffer
, DirectorySize
, PathPart
, &FatFileInfo
))
744 MmFreeMemory(DirectoryBuffer
);
749 MmFreeMemory(DirectoryBuffer
);
752 // If we have another sub-directory to go then
753 // grab the start cluster and free the fat chain array
755 if ((i
+1) < NumberOfPathParts
)
757 DirectoryStartCluster
= FatFileInfo
.FileFatChain
[0];
758 MmFreeMemory(FatFileInfo
.FileFatChain
);
762 memcpy(FatFileInfoPointer
, &FatFileInfo
, sizeof(FAT_FILE_INFO
));
769 * This function parses a directory entry name which
770 * is in the form of "FILE EXT" and puts it in Buffer
771 * in the form of "file.ext"
773 void FatParseShortFileName(PCHAR Buffer
, PDIRENTRY DirEntry
)
778 RtlZeroMemory(Buffer
, 13);
781 // Fixup first character
783 if (DirEntry
->FileName
[0] == 0x05)
785 DirEntry
->FileName
[0] = 0xE5;
793 if (DirEntry
->FileName
[Idx
] == ' ')
798 Buffer
[Idx
] = DirEntry
->FileName
[Idx
];
805 if ((DirEntry
->FileName
[8] != ' '))
808 Buffer
[Idx
++] = (DirEntry
->FileName
[8] == ' ') ? '\0' : DirEntry
->FileName
[8];
809 Buffer
[Idx
++] = (DirEntry
->FileName
[9] == ' ') ? '\0' : DirEntry
->FileName
[9];
810 Buffer
[Idx
++] = (DirEntry
->FileName
[10] == ' ') ? '\0' : DirEntry
->FileName
[10];
813 DbgPrint((DPRINT_FILESYSTEM
, "FatParseShortFileName() ShortName = %s\n", Buffer
));
818 * returns the Fat entry for a given cluster number
820 BOOL
FatGetFatEntry(ULONG Cluster
, ULONG
* ClusterPointer
)
825 UINT ThisFatEntOffset
;
827 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFatEntry() Retrieving FAT entry for cluster %d.\n", Cluster
));
833 FatOffset
= Cluster
+ (Cluster
/ 2);
834 ThisFatSecNum
= ActiveFatSectorStart
+ (FatOffset
/ BytesPerSector
);
835 ThisFatEntOffset
= (FatOffset
% BytesPerSector
);
837 DbgPrint((DPRINT_FILESYSTEM
, "FatOffset: %d\n", FatOffset
));
838 DbgPrint((DPRINT_FILESYSTEM
, "ThisFatSecNum: %d\n", ThisFatSecNum
));
839 DbgPrint((DPRINT_FILESYSTEM
, "ThisFatEntOffset: %d\n", ThisFatEntOffset
));
841 if (ThisFatEntOffset
== (BytesPerSector
- 1))
843 if (!FatReadVolumeSectors(FatDriveNumber
, ThisFatSecNum
, 2, (PVOID
)FILESYSBUFFER
))
850 if (!FatReadVolumeSectors(FatDriveNumber
, ThisFatSecNum
, 1, (PVOID
)FILESYSBUFFER
))
856 fat
= *((USHORT
*) ((ULONG_PTR
)FILESYSBUFFER
+ ThisFatEntOffset
));
857 if (Cluster
& 0x0001)
858 fat
= fat
>> 4; /* Cluster number is ODD */
860 fat
= fat
& 0x0FFF; /* Cluster number is EVEN */
867 FatOffset
= (Cluster
* 2);
868 ThisFatSecNum
= ActiveFatSectorStart
+ (FatOffset
/ BytesPerSector
);
869 ThisFatEntOffset
= (FatOffset
% BytesPerSector
);
871 if (!FatReadVolumeSectors(FatDriveNumber
, ThisFatSecNum
, 1, (PVOID
)FILESYSBUFFER
))
876 fat
= *((USHORT
*) ((ULONG_PTR
)FILESYSBUFFER
+ ThisFatEntOffset
));
883 FatOffset
= (Cluster
* 4);
884 ThisFatSecNum
= ActiveFatSectorStart
+ (FatOffset
/ BytesPerSector
);
885 ThisFatEntOffset
= (FatOffset
% BytesPerSector
);
887 if (!FatReadVolumeSectors(FatDriveNumber
, ThisFatSecNum
, 1, (PVOID
)FILESYSBUFFER
))
893 fat
= (*((ULONG
*) ((ULONG_PTR
)FILESYSBUFFER
+ ThisFatEntOffset
))) & 0x0FFFFFFF;
899 DbgPrint((DPRINT_FILESYSTEM
, "FAT entry is 0x%x.\n", fat
));
901 *ClusterPointer
= fat
;
908 * Tries to open the file 'name' and returns true or false
909 * for success and failure respectively
911 FILE* FatOpenFile(PCSTR FileName
)
913 FAT_FILE_INFO TempFatFileInfo
;
914 PFAT_FILE_INFO FileHandle
;
916 DbgPrint((DPRINT_FILESYSTEM
, "FatOpenFile() FileName = %s\n", FileName
));
918 if (!FatLookupFile(FileName
, &TempFatFileInfo
))
923 FileHandle
= MmAllocateMemory(sizeof(FAT_FILE_INFO
));
925 if (FileHandle
== NULL
)
930 memcpy(FileHandle
, &TempFatFileInfo
, sizeof(FAT_FILE_INFO
));
932 return (FILE*)FileHandle
;
935 ULONG
FatCountClustersInChain(ULONG StartCluster
)
937 ULONG ClusterCount
= 0;
939 DbgPrint((DPRINT_FILESYSTEM
, "FatCountClustersInChain() StartCluster = %d\n", StartCluster
));
944 // If end of chain then break out of our cluster counting loop
946 if (((FatType
== FAT12
) && (StartCluster
>= 0xff8)) ||
947 ((FatType
== FAT16
|| FatType
== FATX16
) && (StartCluster
>= 0xfff8)) ||
948 ((FatType
== FAT32
|| FatType
== FATX32
) && (StartCluster
>= 0x0ffffff8)))
961 if (!FatGetFatEntry(StartCluster
, &StartCluster
))
967 DbgPrint((DPRINT_FILESYSTEM
, "FatCountClustersInChain() ClusterCount = %d\n", ClusterCount
));
972 ULONG
* FatGetClusterChainArray(ULONG StartCluster
)
979 DbgPrint((DPRINT_FILESYSTEM
, "FatGetClusterChainArray() StartCluster = %d\n", StartCluster
));
981 ClusterCount
= FatCountClustersInChain(StartCluster
) + 1; // Lets get the 0x0ffffff8 on the end of the array
982 ArraySize
= ClusterCount
* sizeof(ULONG
);
985 // Allocate array memory
987 ArrayPointer
= MmAllocateMemory(ArraySize
);
989 if (ArrayPointer
== NULL
)
995 // Loop through and set array values
997 for (Idx
=0; Idx
<ClusterCount
; Idx
++)
1000 // Set current cluster
1002 ArrayPointer
[Idx
] = StartCluster
;
1005 // Don't try to get next cluster for last cluster
1007 if (((FatType
== FAT12
) && (StartCluster
>= 0xff8)) ||
1008 ((FatType
== FAT16
|| FatType
== FATX16
) && (StartCluster
>= 0xfff8)) ||
1009 ((FatType
== FAT32
|| FatType
== FATX32
) && (StartCluster
>= 0x0ffffff8)))
1018 if (!FatGetFatEntry(StartCluster
, &StartCluster
))
1020 MmFreeMemory(ArrayPointer
);
1025 return ArrayPointer
;
1030 * Reads the specified cluster into memory
1032 BOOL
FatReadCluster(ULONG ClusterNumber
, PVOID Buffer
)
1034 ULONG ClusterStartSector
;
1036 ClusterStartSector
= ((ClusterNumber
- 2) * SectorsPerCluster
) + DataSectorStart
;
1038 DbgPrint((DPRINT_FILESYSTEM
, "FatReadCluster() ClusterNumber = %d Buffer = 0x%x ClusterStartSector = %d\n", ClusterNumber
, Buffer
, ClusterStartSector
));
1040 if (!FatReadVolumeSectors(FatDriveNumber
, ClusterStartSector
, SectorsPerCluster
, (PVOID
)FILESYSBUFFER
))
1045 memcpy(Buffer
, (PVOID
)FILESYSBUFFER
, SectorsPerCluster
* BytesPerSector
);
1051 * FatReadClusterChain()
1052 * Reads the specified clusters into memory
1054 BOOL
FatReadClusterChain(ULONG StartClusterNumber
, ULONG NumberOfClusters
, PVOID Buffer
)
1056 ULONG ClusterStartSector
;
1058 DbgPrint((DPRINT_FILESYSTEM
, "FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber
, NumberOfClusters
, Buffer
));
1060 while (NumberOfClusters
> 0)
1063 DbgPrint((DPRINT_FILESYSTEM
, "FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber
, NumberOfClusters
, Buffer
));
1065 // Calculate starting sector for cluster
1067 ClusterStartSector
= ((StartClusterNumber
- 2) * SectorsPerCluster
) + DataSectorStart
;
1070 // Read cluster into memory
1072 if (!FatReadVolumeSectors(FatDriveNumber
, ClusterStartSector
, SectorsPerCluster
, (PVOID
)FILESYSBUFFER
))
1077 memcpy(Buffer
, (PVOID
)FILESYSBUFFER
, SectorsPerCluster
* BytesPerSector
);
1080 // Decrement count of clusters left to read
1085 // Increment buffer address by cluster size
1087 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (SectorsPerCluster
* BytesPerSector
));
1092 if (!FatGetFatEntry(StartClusterNumber
, &StartClusterNumber
))
1098 // If end of chain then break out of our cluster reading loop
1100 if (((FatType
== FAT12
) && (StartClusterNumber
>= 0xff8)) ||
1101 ((FatType
== FAT16
|| FatType
== FATX16
) && (StartClusterNumber
>= 0xfff8)) ||
1102 ((FatType
== FAT32
|| FatType
== FATX32
) && (StartClusterNumber
>= 0x0ffffff8)))
1112 * FatReadPartialCluster()
1113 * Reads part of a cluster into memory
1115 BOOL
FatReadPartialCluster(ULONG ClusterNumber
, ULONG StartingOffset
, ULONG Length
, PVOID Buffer
)
1117 ULONG ClusterStartSector
;
1119 DbgPrint((DPRINT_FILESYSTEM
, "FatReadPartialCluster() ClusterNumber = %d StartingOffset = %d Length = %d Buffer = 0x%x\n", ClusterNumber
, StartingOffset
, Length
, Buffer
));
1121 ClusterStartSector
= ((ClusterNumber
- 2) * SectorsPerCluster
) + DataSectorStart
;
1123 if (!FatReadVolumeSectors(FatDriveNumber
, ClusterStartSector
, SectorsPerCluster
, (PVOID
)FILESYSBUFFER
))
1128 memcpy(Buffer
, (PVOID
)((ULONG_PTR
)FILESYSBUFFER
+ StartingOffset
), Length
);
1135 * Reads BytesToRead from open file and
1136 * returns the number of bytes read in BytesRead
1138 BOOL
FatReadFile(FILE *FileHandle
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
)
1140 PFAT_FILE_INFO FatFileInfo
= (PFAT_FILE_INFO
)FileHandle
;
1141 ULONG ClusterNumber
;
1142 ULONG OffsetInCluster
;
1143 ULONG LengthInCluster
;
1144 ULONG NumberOfClusters
;
1145 ULONG BytesPerCluster
;
1147 DbgPrint((DPRINT_FILESYSTEM
, "FatReadFile() BytesToRead = %d Buffer = 0x%x\n", BytesToRead
, Buffer
));
1149 if (BytesRead
!= NULL
)
1155 // If they are trying to read past the
1156 // end of the file then return success
1157 // with BytesRead == 0
1159 if (FatFileInfo
->FilePointer
>= FatFileInfo
->FileSize
)
1165 // If they are trying to read more than there is to read
1166 // then adjust the amount to read
1168 if ((FatFileInfo
->FilePointer
+ BytesToRead
) > FatFileInfo
->FileSize
)
1170 BytesToRead
= (FatFileInfo
->FileSize
- FatFileInfo
->FilePointer
);
1174 // Ok, now we have to perform at most 3 calculations
1175 // I'll draw you a picture (using nifty ASCII art):
1177 // CurrentFilePointer -+
1179 // +----------------+
1181 // +-----------+-----------+-----------+-----------+
1182 // | Cluster 1 | Cluster 2 | Cluster 3 | Cluster 4 |
1183 // +-----------+-----------+-----------+-----------+
1185 // +---------------+--------------------+
1187 // BytesToRead -------+
1189 // 1 - The first calculation (and read) will align
1190 // the file pointer with the next cluster.
1191 // boundary (if we are supposed to read that much)
1192 // 2 - The next calculation (and read) will read
1193 // in all the full clusters that the requested
1194 // amount of data would cover (in this case
1196 // 3 - The last calculation (and read) would read
1197 // in the remainder of the data requested out of
1198 // the last cluster.
1201 BytesPerCluster
= SectorsPerCluster
* BytesPerSector
;
1204 // Only do the first read if we
1205 // aren't aligned on a cluster boundary
1207 if (FatFileInfo
->FilePointer
% BytesPerCluster
)
1210 // Do the math for our first read
1212 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1213 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1214 OffsetInCluster
= (FatFileInfo
->FilePointer
% BytesPerCluster
);
1215 LengthInCluster
= (BytesToRead
> (BytesPerCluster
- OffsetInCluster
)) ? (BytesPerCluster
- OffsetInCluster
) : BytesToRead
;
1218 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1220 if (!FatReadPartialCluster(ClusterNumber
, OffsetInCluster
, LengthInCluster
, Buffer
))
1224 if (BytesRead
!= NULL
)
1226 *BytesRead
+= LengthInCluster
;
1228 BytesToRead
-= LengthInCluster
;
1229 FatFileInfo
->FilePointer
+= LengthInCluster
;
1230 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ LengthInCluster
);
1234 // Do the math for our second read (if any data left)
1236 if (BytesToRead
> 0)
1239 // Determine how many full clusters we need to read
1241 NumberOfClusters
= (BytesToRead
/ BytesPerCluster
);
1243 if (NumberOfClusters
> 0)
1245 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1246 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1249 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1251 if (!FatReadClusterChain(ClusterNumber
, NumberOfClusters
, Buffer
))
1255 if (BytesRead
!= NULL
)
1257 *BytesRead
+= (NumberOfClusters
* BytesPerCluster
);
1259 BytesToRead
-= (NumberOfClusters
* BytesPerCluster
);
1260 FatFileInfo
->FilePointer
+= (NumberOfClusters
* BytesPerCluster
);
1261 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfClusters
* BytesPerCluster
));
1266 // Do the math for our third read (if any data left)
1268 if (BytesToRead
> 0)
1270 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1271 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1274 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1276 if (!FatReadPartialCluster(ClusterNumber
, 0, BytesToRead
, Buffer
))
1280 if (BytesRead
!= NULL
)
1282 *BytesRead
+= BytesToRead
;
1284 FatFileInfo
->FilePointer
+= BytesToRead
;
1285 BytesToRead
-= BytesToRead
;
1286 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ BytesToRead
);
1292 ULONG
FatGetFileSize(FILE *FileHandle
)
1294 PFAT_FILE_INFO FatFileHandle
= (PFAT_FILE_INFO
)FileHandle
;
1296 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFileSize() FileSize = %d\n", FatFileHandle
->FileSize
));
1298 return FatFileHandle
->FileSize
;
1301 VOID
FatSetFilePointer(FILE *FileHandle
, ULONG NewFilePointer
)
1303 PFAT_FILE_INFO FatFileHandle
= (PFAT_FILE_INFO
)FileHandle
;
1305 DbgPrint((DPRINT_FILESYSTEM
, "FatSetFilePointer() NewFilePointer = %d\n", NewFilePointer
));
1307 FatFileHandle
->FilePointer
= NewFilePointer
;
1310 ULONG
FatGetFilePointer(FILE *FileHandle
)
1312 PFAT_FILE_INFO FatFileHandle
= (PFAT_FILE_INFO
)FileHandle
;
1314 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFilePointer() FilePointer = %d\n", FatFileHandle
->FilePointer
));
1316 return FatFileHandle
->FilePointer
;
1319 BOOL
FatReadVolumeSectors(ULONG DriveNumber
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
1321 return CacheReadDiskSectors(DriveNumber
, SectorNumber
+ FatVolumeStartSector
, SectorCount
, Buffer
);