3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 * Copyright (C) 2009 Hervé Poussineau
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 DBG_DEFAULT_CHANNEL(FILESYSTEM
);
28 ULONG
FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector
, ULONGLONG PartitionSectorCount
);
29 PVOID
FatBufferDirectory(PFAT_VOLUME_INFO Volume
, ULONG DirectoryStartCluster
, ULONG
* EntryCountPointer
, BOOLEAN RootDirectory
);
30 BOOLEAN
FatSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume
, PVOID DirectoryBuffer
, ULONG EntryCount
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
);
31 LONG
FatLookupFile(PFAT_VOLUME_INFO Volume
, PCSTR FileName
, ULONG DeviceId
, PFAT_FILE_INFO FatFileInfoPointer
);
32 void FatParseShortFileName(PCHAR Buffer
, PDIRENTRY DirEntry
);
33 BOOLEAN
FatGetFatEntry(PFAT_VOLUME_INFO Volume
, ULONG Cluster
, ULONG
* ClusterPointer
);
34 ULONG
FatCountClustersInChain(PFAT_VOLUME_INFO Volume
, ULONG StartCluster
);
35 ULONG
* FatGetClusterChainArray(PFAT_VOLUME_INFO Volume
, ULONG StartCluster
);
36 BOOLEAN
FatReadClusterChain(PFAT_VOLUME_INFO Volume
, ULONG StartClusterNumber
, ULONG NumberOfClusters
, PVOID Buffer
);
37 BOOLEAN
FatReadPartialCluster(PFAT_VOLUME_INFO Volume
, ULONG ClusterNumber
, ULONG StartingOffset
, ULONG Length
, PVOID Buffer
);
38 BOOLEAN
FatReadVolumeSectors(PFAT_VOLUME_INFO Volume
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
);
40 typedef struct _FAT_VOLUME_INFO
42 ULONG BytesPerSector
; /* Number of bytes per sector */
43 ULONG SectorsPerCluster
; /* Number of sectors per cluster */
44 ULONG FatSectorStart
; /* Starting sector of 1st FAT table */
45 ULONG ActiveFatSectorStart
; /* Starting sector of active FAT table */
46 ULONG NumberOfFats
; /* Number of FAT tables */
47 ULONG SectorsPerFat
; /* Sectors per FAT table */
48 ULONG RootDirSectorStart
; /* Starting sector of the root directory (non-fat32) */
49 ULONG RootDirSectors
; /* Number of sectors of the root directory (non-fat32) */
50 ULONG RootDirStartCluster
; /* Starting cluster number of the root directory (fat32 only) */
51 ULONG DataSectorStart
; /* Starting sector of the data area */
52 ULONG FatType
; /* FAT12, FAT16, FAT32, FATX16 or FATX32 */
56 PFAT_VOLUME_INFO FatVolumes
[MAX_FDS
];
58 VOID
FatSwapFatBootSector(PFAT_BOOTSECTOR Obj
)
60 SW(Obj
, BytesPerSector
);
61 SW(Obj
, ReservedSectors
);
62 SW(Obj
, RootDirEntries
);
63 SW(Obj
, TotalSectors
);
64 SW(Obj
, SectorsPerFat
);
65 SW(Obj
, SectorsPerTrack
);
66 SW(Obj
, NumberOfHeads
);
67 SD(Obj
, HiddenSectors
);
68 SD(Obj
, TotalSectorsBig
);
69 SD(Obj
, VolumeSerialNumber
);
70 SW(Obj
, BootSectorMagic
);
73 VOID
FatSwapFat32BootSector(PFAT32_BOOTSECTOR Obj
)
75 SW(Obj
, BytesPerSector
);
76 SW(Obj
, ReservedSectors
);
77 SW(Obj
, RootDirEntries
);
78 SW(Obj
, TotalSectors
);
79 SW(Obj
, SectorsPerFat
);
80 SW(Obj
, NumberOfHeads
);
81 SD(Obj
, HiddenSectors
);
82 SD(Obj
, TotalSectorsBig
);
83 SD(Obj
, SectorsPerFatBig
);
84 SW(Obj
, ExtendedFlags
);
85 SW(Obj
, FileSystemVersion
);
86 SD(Obj
, RootDirStartCluster
);
88 SW(Obj
, BackupBootSector
);
89 SD(Obj
, VolumeSerialNumber
);
90 SW(Obj
, BootSectorMagic
);
93 VOID
FatSwapFatXBootSector(PFATX_BOOTSECTOR Obj
)
95 SD(Obj
, VolumeSerialNumber
);
96 SD(Obj
, SectorsPerCluster
);
97 SW(Obj
, NumberOfFats
);
100 VOID
FatSwapDirEntry(PDIRENTRY Obj
)
104 SW(Obj
, LastAccessDate
);
105 SW(Obj
, ClusterHigh
);
112 VOID
FatSwapLFNDirEntry(PLFN_DIRENTRY Obj
)
115 SW(Obj
, StartCluster
);
116 for(i
= 0; i
< 5; i
++)
117 Obj
->Name0_4
[i
] = SWAPW(Obj
->Name0_4
[i
]);
118 for(i
= 0; i
< 6; i
++)
119 Obj
->Name5_10
[i
] = SWAPW(Obj
->Name5_10
[i
]);
120 for(i
= 0; i
< 2; i
++)
121 Obj
->Name11_12
[i
] = SWAPW(Obj
->Name11_12
[i
]);
124 VOID
FatSwapFatXDirEntry(PFATX_DIRENTRY Obj
)
126 SD(Obj
, StartCluster
);
132 SW(Obj
, LastAccessTime
);
133 SW(Obj
, LastAccessDate
);
136 BOOLEAN
FatOpenVolume(PFAT_VOLUME_INFO Volume
, PFAT_BOOTSECTOR BootSector
, ULONGLONG PartitionSectorCount
)
140 PFAT_BOOTSECTOR FatVolumeBootSector
;
141 PFAT32_BOOTSECTOR Fat32VolumeBootSector
;
142 PFATX_BOOTSECTOR FatXVolumeBootSector
;
144 TRACE("FatOpenVolume() DeviceId = %d\n", Volume
->DeviceId
);
147 // Allocate the memory to hold the boot sector
149 FatVolumeBootSector
= (PFAT_BOOTSECTOR
)BootSector
;
150 Fat32VolumeBootSector
= (PFAT32_BOOTSECTOR
)BootSector
;
151 FatXVolumeBootSector
= (PFATX_BOOTSECTOR
)BootSector
;
154 Volume
->FatType
= FatDetermineFatType(FatVolumeBootSector
, PartitionSectorCount
);
156 // Dump boot sector (and swap it for big endian systems)
157 TRACE("Dumping boot sector:\n");
158 if (ISFATX(Volume
->FatType
))
160 FatSwapFatXBootSector(FatXVolumeBootSector
);
161 TRACE("sizeof(FATX_BOOTSECTOR) = 0x%x.\n", sizeof(FATX_BOOTSECTOR
));
163 TRACE("FileSystemType: %c%c%c%c.\n", FatXVolumeBootSector
->FileSystemType
[0], FatXVolumeBootSector
->FileSystemType
[1], FatXVolumeBootSector
->FileSystemType
[2], FatXVolumeBootSector
->FileSystemType
[3]);
164 TRACE("VolumeSerialNumber: 0x%x\n", FatXVolumeBootSector
->VolumeSerialNumber
);
165 TRACE("SectorsPerCluster: %d\n", FatXVolumeBootSector
->SectorsPerCluster
);
166 TRACE("NumberOfFats: %d\n", FatXVolumeBootSector
->NumberOfFats
);
167 TRACE("Unknown: 0x%x\n", FatXVolumeBootSector
->Unknown
);
169 TRACE("FatType %s\n", Volume
->FatType
== FATX16
? "FATX16" : "FATX32");
172 else if (Volume
->FatType
== FAT32
)
174 FatSwapFat32BootSector(Fat32VolumeBootSector
);
175 TRACE("sizeof(FAT32_BOOTSECTOR) = 0x%x.\n", sizeof(FAT32_BOOTSECTOR
));
177 TRACE("JumpBoot: 0x%x 0x%x 0x%x\n", Fat32VolumeBootSector
->JumpBoot
[0], Fat32VolumeBootSector
->JumpBoot
[1], Fat32VolumeBootSector
->JumpBoot
[2]);
178 TRACE("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]);
179 TRACE("BytesPerSector: %d\n", Fat32VolumeBootSector
->BytesPerSector
);
180 TRACE("SectorsPerCluster: %d\n", Fat32VolumeBootSector
->SectorsPerCluster
);
181 TRACE("ReservedSectors: %d\n", Fat32VolumeBootSector
->ReservedSectors
);
182 TRACE("NumberOfFats: %d\n", Fat32VolumeBootSector
->NumberOfFats
);
183 TRACE("RootDirEntries: %d\n", Fat32VolumeBootSector
->RootDirEntries
);
184 TRACE("TotalSectors: %d\n", Fat32VolumeBootSector
->TotalSectors
);
185 TRACE("MediaDescriptor: 0x%x\n", Fat32VolumeBootSector
->MediaDescriptor
);
186 TRACE("SectorsPerFat: %d\n", Fat32VolumeBootSector
->SectorsPerFat
);
187 TRACE("SectorsPerTrack: %d\n", Fat32VolumeBootSector
->SectorsPerTrack
);
188 TRACE("NumberOfHeads: %d\n", Fat32VolumeBootSector
->NumberOfHeads
);
189 TRACE("HiddenSectors: %d\n", Fat32VolumeBootSector
->HiddenSectors
);
190 TRACE("TotalSectorsBig: %d\n", Fat32VolumeBootSector
->TotalSectorsBig
);
191 TRACE("SectorsPerFatBig: %d\n", Fat32VolumeBootSector
->SectorsPerFatBig
);
192 TRACE("ExtendedFlags: 0x%x\n", Fat32VolumeBootSector
->ExtendedFlags
);
193 TRACE("FileSystemVersion: 0x%x\n", Fat32VolumeBootSector
->FileSystemVersion
);
194 TRACE("RootDirStartCluster: %d\n", Fat32VolumeBootSector
->RootDirStartCluster
);
195 TRACE("FsInfo: %d\n", Fat32VolumeBootSector
->FsInfo
);
196 TRACE("BackupBootSector: %d\n", Fat32VolumeBootSector
->BackupBootSector
);
197 TRACE("Reserved: 0x%x\n", Fat32VolumeBootSector
->Reserved
);
198 TRACE("DriveNumber: 0x%x\n", Fat32VolumeBootSector
->DriveNumber
);
199 TRACE("Reserved1: 0x%x\n", Fat32VolumeBootSector
->Reserved1
);
200 TRACE("BootSignature: 0x%x\n", Fat32VolumeBootSector
->BootSignature
);
201 TRACE("VolumeSerialNumber: 0x%x\n", Fat32VolumeBootSector
->VolumeSerialNumber
);
202 TRACE("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]);
203 TRACE("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]);
204 TRACE("BootSectorMagic: 0x%x\n", Fat32VolumeBootSector
->BootSectorMagic
);
208 FatSwapFatBootSector(FatVolumeBootSector
);
209 TRACE("sizeof(FAT_BOOTSECTOR) = 0x%x.\n", sizeof(FAT_BOOTSECTOR
));
211 TRACE("JumpBoot: 0x%x 0x%x 0x%x\n", FatVolumeBootSector
->JumpBoot
[0], FatVolumeBootSector
->JumpBoot
[1], FatVolumeBootSector
->JumpBoot
[2]);
212 TRACE("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]);
213 TRACE("BytesPerSector: %d\n", FatVolumeBootSector
->BytesPerSector
);
214 TRACE("SectorsPerCluster: %d\n", FatVolumeBootSector
->SectorsPerCluster
);
215 TRACE("ReservedSectors: %d\n", FatVolumeBootSector
->ReservedSectors
);
216 TRACE("NumberOfFats: %d\n", FatVolumeBootSector
->NumberOfFats
);
217 TRACE("RootDirEntries: %d\n", FatVolumeBootSector
->RootDirEntries
);
218 TRACE("TotalSectors: %d\n", FatVolumeBootSector
->TotalSectors
);
219 TRACE("MediaDescriptor: 0x%x\n", FatVolumeBootSector
->MediaDescriptor
);
220 TRACE("SectorsPerFat: %d\n", FatVolumeBootSector
->SectorsPerFat
);
221 TRACE("SectorsPerTrack: %d\n", FatVolumeBootSector
->SectorsPerTrack
);
222 TRACE("NumberOfHeads: %d\n", FatVolumeBootSector
->NumberOfHeads
);
223 TRACE("HiddenSectors: %d\n", FatVolumeBootSector
->HiddenSectors
);
224 TRACE("TotalSectorsBig: %d\n", FatVolumeBootSector
->TotalSectorsBig
);
225 TRACE("DriveNumber: 0x%x\n", FatVolumeBootSector
->DriveNumber
);
226 TRACE("Reserved1: 0x%x\n", FatVolumeBootSector
->Reserved1
);
227 TRACE("BootSignature: 0x%x\n", FatVolumeBootSector
->BootSignature
);
228 TRACE("VolumeSerialNumber: 0x%x\n", FatVolumeBootSector
->VolumeSerialNumber
);
229 TRACE("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]);
230 TRACE("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]);
231 TRACE("BootSectorMagic: 0x%x\n", FatVolumeBootSector
->BootSectorMagic
);
235 // Check the boot sector magic
237 if (! ISFATX(Volume
->FatType
) && FatVolumeBootSector
->BootSectorMagic
!= 0xaa55)
239 sprintf(ErrMsg
, "Invalid boot sector magic (expected 0xaa55 found 0x%x)",
240 FatVolumeBootSector
->BootSectorMagic
);
241 FileSystemError(ErrMsg
);
246 // Check the FAT cluster size
247 // We do not support clusters bigger than 64k
249 if ((ISFATX(Volume
->FatType
) && 64 * 1024 < FatXVolumeBootSector
->SectorsPerCluster
* 512) ||
250 (! ISFATX(Volume
->FatType
) && 64 * 1024 < FatVolumeBootSector
->SectorsPerCluster
* FatVolumeBootSector
->BytesPerSector
))
252 FileSystemError("This file system has cluster sizes bigger than 64k.\nFreeLoader does not support this.");
257 // Get the sectors per FAT,
258 // root directory starting sector,
259 // and data sector start
261 if (ISFATX(Volume
->FatType
))
263 Volume
->BytesPerSector
= 512;
264 Volume
->SectorsPerCluster
= SWAPD(FatXVolumeBootSector
->SectorsPerCluster
);
265 Volume
->FatSectorStart
= (4096 / Volume
->BytesPerSector
);
266 Volume
->ActiveFatSectorStart
= Volume
->FatSectorStart
;
267 Volume
->NumberOfFats
= 1;
268 FatSize
= (ULONG
)(PartitionSectorCount
/ Volume
->SectorsPerCluster
*
269 (Volume
->FatType
== FATX16
? 2 : 4));
270 Volume
->SectorsPerFat
= (((FatSize
+ 4095) / 4096) * 4096) / Volume
->BytesPerSector
;
272 Volume
->RootDirSectorStart
= Volume
->FatSectorStart
+ Volume
->NumberOfFats
* Volume
->SectorsPerFat
;
273 Volume
->RootDirSectors
= FatXVolumeBootSector
->SectorsPerCluster
;
275 Volume
->DataSectorStart
= Volume
->RootDirSectorStart
+ Volume
->RootDirSectors
;
277 else if (Volume
->FatType
!= FAT32
)
279 Volume
->BytesPerSector
= FatVolumeBootSector
->BytesPerSector
;
280 Volume
->SectorsPerCluster
= FatVolumeBootSector
->SectorsPerCluster
;
281 Volume
->FatSectorStart
= FatVolumeBootSector
->ReservedSectors
;
282 Volume
->ActiveFatSectorStart
= Volume
->FatSectorStart
;
283 Volume
->NumberOfFats
= FatVolumeBootSector
->NumberOfFats
;
284 Volume
->SectorsPerFat
= FatVolumeBootSector
->SectorsPerFat
;
286 Volume
->RootDirSectorStart
= Volume
->FatSectorStart
+ Volume
->NumberOfFats
* Volume
->SectorsPerFat
;
287 Volume
->RootDirSectors
= ((FatVolumeBootSector
->RootDirEntries
* 32) + (Volume
->BytesPerSector
- 1)) / Volume
->BytesPerSector
;
289 Volume
->DataSectorStart
= Volume
->RootDirSectorStart
+ Volume
->RootDirSectors
;
293 Volume
->BytesPerSector
= Fat32VolumeBootSector
->BytesPerSector
;
294 Volume
->SectorsPerCluster
= Fat32VolumeBootSector
->SectorsPerCluster
;
295 Volume
->FatSectorStart
= Fat32VolumeBootSector
->ReservedSectors
;
296 Volume
->ActiveFatSectorStart
= Volume
->FatSectorStart
+
297 ((Fat32VolumeBootSector
->ExtendedFlags
& 0x80) ? ((Fat32VolumeBootSector
->ExtendedFlags
& 0x0f) * Fat32VolumeBootSector
->SectorsPerFatBig
) : 0);
298 Volume
->NumberOfFats
= Fat32VolumeBootSector
->NumberOfFats
;
299 Volume
->SectorsPerFat
= Fat32VolumeBootSector
->SectorsPerFatBig
;
301 Volume
->RootDirStartCluster
= Fat32VolumeBootSector
->RootDirStartCluster
;
302 Volume
->DataSectorStart
= Volume
->FatSectorStart
+ Volume
->NumberOfFats
* Volume
->SectorsPerFat
;
306 // we only work with version 0
308 if (Fat32VolumeBootSector
->FileSystemVersion
!= 0)
310 FileSystemError("FreeLoader is too old to work with this FAT32 filesystem.\nPlease update FreeLoader.");
318 ULONG
FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector
, ULONGLONG PartitionSectorCount
)
320 ULONG RootDirSectors
;
321 ULONG DataSectorCount
;
324 ULONG CountOfClusters
;
325 PFAT32_BOOTSECTOR Fat32BootSector
= (PFAT32_BOOTSECTOR
)FatBootSector
;
326 PFATX_BOOTSECTOR FatXBootSector
= (PFATX_BOOTSECTOR
)FatBootSector
;
328 if (0 == strncmp(FatXBootSector
->FileSystemType
, "FATX", 4))
330 CountOfClusters
= (ULONG
)(PartitionSectorCount
/ FatXBootSector
->SectorsPerCluster
);
331 if (CountOfClusters
< 65525)
333 /* Volume is FATX16 */
338 /* Volume is FAT32 */
344 RootDirSectors
= ((SWAPW(FatBootSector
->RootDirEntries
) * 32) + (SWAPW(FatBootSector
->BytesPerSector
) - 1)) / SWAPW(FatBootSector
->BytesPerSector
);
345 SectorsPerFat
= SWAPW(FatBootSector
->SectorsPerFat
) ? SWAPW(FatBootSector
->SectorsPerFat
) : SWAPD(Fat32BootSector
->SectorsPerFatBig
);
346 TotalSectors
= SWAPW(FatBootSector
->TotalSectors
) ? SWAPW(FatBootSector
->TotalSectors
) : SWAPD(FatBootSector
->TotalSectorsBig
);
347 DataSectorCount
= TotalSectors
- (SWAPW(FatBootSector
->ReservedSectors
) + (FatBootSector
->NumberOfFats
* SectorsPerFat
) + RootDirSectors
);
350 if (FatBootSector
->SectorsPerCluster
== 0)
353 CountOfClusters
= DataSectorCount
/ FatBootSector
->SectorsPerCluster
;
355 if (CountOfClusters
< 4085)
357 /* Volume is FAT12 */
360 else if (CountOfClusters
< 65525)
362 /* Volume is FAT16 */
367 /* Volume is FAT32 */
373 typedef struct _DIRECTORY_BUFFER
377 ULONG DirectoryStartCluster
;
380 } DIRECTORY_BUFFER
, *PDIRECTORY_BUFFER
;
382 LIST_ENTRY DirectoryBufferListHead
= {&DirectoryBufferListHead
, &DirectoryBufferListHead
};
384 PVOID
FatBufferDirectory(PFAT_VOLUME_INFO Volume
, ULONG DirectoryStartCluster
, ULONG
*DirectorySize
, BOOLEAN RootDirectory
)
386 PDIRECTORY_BUFFER DirectoryBuffer
;
389 TRACE("FatBufferDirectory() DirectoryStartCluster = %d RootDirectory = %s\n", DirectoryStartCluster
, (RootDirectory
? "TRUE" : "FALSE"));
392 * For FAT32, the root directory is nothing special. We can treat it the same
395 if (RootDirectory
&& Volume
->FatType
== FAT32
)
397 DirectoryStartCluster
= Volume
->RootDirStartCluster
;
398 RootDirectory
= FALSE
;
401 /* Search the list for a match */
402 for (Entry
= DirectoryBufferListHead
.Flink
;
403 Entry
!= &DirectoryBufferListHead
;
404 Entry
= Entry
->Flink
)
406 DirectoryBuffer
= CONTAINING_RECORD(Entry
, DIRECTORY_BUFFER
, Link
);
408 /* Check if it matches */
409 if ((DirectoryBuffer
->Volume
== Volume
) &&
410 (DirectoryBuffer
->DirectoryStartCluster
== DirectoryStartCluster
))
412 TRACE("Found cached buffer\n");
413 *DirectorySize
= DirectoryBuffer
->DirectorySize
;
414 return DirectoryBuffer
->Data
;
419 // Calculate the size of the directory
423 *DirectorySize
= Volume
->RootDirSectors
* Volume
->BytesPerSector
;
427 *DirectorySize
= FatCountClustersInChain(Volume
, DirectoryStartCluster
) * Volume
->SectorsPerCluster
* Volume
->BytesPerSector
;
431 // Attempt to allocate memory for directory buffer
433 TRACE("Trying to allocate (DirectorySize) %d bytes.\n", *DirectorySize
);
434 DirectoryBuffer
= MmHeapAlloc(*DirectorySize
+ sizeof(DIRECTORY_BUFFER
));
436 if (DirectoryBuffer
== NULL
)
442 // Now read directory contents into DirectoryBuffer
446 if (!FatReadVolumeSectors(Volume
, Volume
->RootDirSectorStart
, Volume
->RootDirSectors
, DirectoryBuffer
->Data
))
448 MmHeapFree(DirectoryBuffer
);
454 if (!FatReadClusterChain(Volume
, DirectoryStartCluster
, 0xFFFFFFFF, DirectoryBuffer
->Data
))
456 MmHeapFree(DirectoryBuffer
);
461 /* Enqueue it in the list */
462 DirectoryBuffer
->Volume
= Volume
;
463 DirectoryBuffer
->DirectoryStartCluster
= DirectoryStartCluster
;
464 DirectoryBuffer
->DirectorySize
= *DirectorySize
;
465 InsertTailList(&DirectoryBufferListHead
, &DirectoryBuffer
->Link
);
467 return DirectoryBuffer
->Data
;
470 BOOLEAN
FatSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume
, PVOID DirectoryBuffer
, ULONG DirectorySize
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
)
474 CHAR LfnNameBuffer
[265];
475 CHAR ShortNameBuffer
[20];
477 DIRENTRY OurDirEntry
;
478 LFN_DIRENTRY OurLfnDirEntry
;
479 PDIRENTRY DirEntry
= &OurDirEntry
;
480 PLFN_DIRENTRY LfnDirEntry
= &OurLfnDirEntry
;
482 EntryCount
= DirectorySize
/ sizeof(DIRENTRY
);
484 TRACE("FatSearchDirectoryBufferForFile() DirectoryBuffer = 0x%x EntryCount = %d FileName = %s\n", DirectoryBuffer
, EntryCount
, FileName
);
486 memset(ShortNameBuffer
, 0, 13 * sizeof(CHAR
));
487 memset(LfnNameBuffer
, 0, 261 * sizeof(CHAR
));
489 for (CurrentEntry
=0; CurrentEntry
<EntryCount
; CurrentEntry
++, DirectoryBuffer
= ((PDIRENTRY
)DirectoryBuffer
)+1)
491 OurLfnDirEntry
= *((PLFN_DIRENTRY
) DirectoryBuffer
);
492 FatSwapLFNDirEntry(LfnDirEntry
);
493 OurDirEntry
= *((PDIRENTRY
) DirectoryBuffer
);
494 FatSwapDirEntry(DirEntry
);
496 //TRACE("Dumping directory entry %d:\n", CurrentEntry);
497 //DbgDumpBuffer(DPRINT_FILESYSTEM, DirEntry, sizeof(DIRENTRY));
500 // Check if this is the last file in the directory
501 // If DirEntry[0] == 0x00 then that means all the
502 // entries after this one are unused. If this is the
503 // last entry then we didn't find the file in this directory.
505 if (DirEntry
->FileName
[0] == '\0')
511 // Check if this is a deleted entry or not
513 if (DirEntry
->FileName
[0] == '\xE5')
515 memset(ShortNameBuffer
, 0, 13 * sizeof(CHAR
));
516 memset(LfnNameBuffer
, 0, 261 * sizeof(CHAR
));
521 // Check if this is a LFN entry
522 // If so it needs special handling
524 if (DirEntry
->Attr
== ATTR_LONG_NAME
)
527 // Check to see if this is a deleted LFN entry, if so continue
529 if (LfnDirEntry
->SequenceNumber
& 0x80)
535 // Mask off high two bits of sequence number
536 // and make the sequence number zero-based
538 LfnDirEntry
->SequenceNumber
&= 0x3F;
539 LfnDirEntry
->SequenceNumber
--;
542 // Get all 13 LFN entry characters
544 if (LfnDirEntry
->Name0_4
[0] != 0xFFFF)
546 LfnNameBuffer
[0 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[0];
548 if (LfnDirEntry
->Name0_4
[1] != 0xFFFF)
550 LfnNameBuffer
[1 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[1];
552 if (LfnDirEntry
->Name0_4
[2] != 0xFFFF)
554 LfnNameBuffer
[2 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[2];
556 if (LfnDirEntry
->Name0_4
[3] != 0xFFFF)
558 LfnNameBuffer
[3 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[3];
560 if (LfnDirEntry
->Name0_4
[4] != 0xFFFF)
562 LfnNameBuffer
[4 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name0_4
[4];
564 if (LfnDirEntry
->Name5_10
[0] != 0xFFFF)
566 LfnNameBuffer
[5 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[0];
568 if (LfnDirEntry
->Name5_10
[1] != 0xFFFF)
570 LfnNameBuffer
[6 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[1];
572 if (LfnDirEntry
->Name5_10
[2] != 0xFFFF)
574 LfnNameBuffer
[7 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[2];
576 if (LfnDirEntry
->Name5_10
[3] != 0xFFFF)
578 LfnNameBuffer
[8 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[3];
580 if (LfnDirEntry
->Name5_10
[4] != 0xFFFF)
582 LfnNameBuffer
[9 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[4];
584 if (LfnDirEntry
->Name5_10
[5] != 0xFFFF)
586 LfnNameBuffer
[10 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name5_10
[5];
588 if (LfnDirEntry
->Name11_12
[0] != 0xFFFF)
590 LfnNameBuffer
[11 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name11_12
[0];
592 if (LfnDirEntry
->Name11_12
[1] != 0xFFFF)
594 LfnNameBuffer
[12 + (LfnDirEntry
->SequenceNumber
* 13)] = (UCHAR
)LfnDirEntry
->Name11_12
[1];
597 //TRACE("Dumping long name buffer:\n");
598 //DbgDumpBuffer(DPRINT_FILESYSTEM, LfnNameBuffer, 260);
604 // Check for the volume label attribute
605 // and skip over this entry if found
607 if (DirEntry
->Attr
& ATTR_VOLUMENAME
)
609 memset(ShortNameBuffer
, 0, 13 * sizeof(UCHAR
));
610 memset(LfnNameBuffer
, 0, 261 * sizeof(UCHAR
));
615 // If we get here then we've found a short file name
616 // entry and LfnNameBuffer contains the long file
617 // name or zeroes. All we have to do now is see if the
618 // file name matches either the short or long file name
619 // and fill in the FAT_FILE_INFO structure if it does
620 // or zero our buffers and continue looking.
624 // Get short file name
626 FatParseShortFileName(ShortNameBuffer
, DirEntry
);
628 //TRACE("Entry: %d LFN = %s\n", CurrentEntry, LfnNameBuffer);
629 //TRACE("Entry: %d DOS name = %s\n", CurrentEntry, ShortNameBuffer);
632 // See if the file name matches either the short or long name
634 if (((strlen(FileName
) == strlen(LfnNameBuffer
)) && (_stricmp(FileName
, LfnNameBuffer
) == 0)) ||
635 ((strlen(FileName
) == strlen(ShortNameBuffer
)) && (_stricmp(FileName
, ShortNameBuffer
) == 0))) {
637 // We found the entry, now fill in the FAT_FILE_INFO struct
639 FatFileInfoPointer
->Attributes
= DirEntry
->Attr
;
640 FatFileInfoPointer
->FileSize
= DirEntry
->Size
;
641 FatFileInfoPointer
->FilePointer
= 0;
643 TRACE("MSDOS Directory Entry:\n");
644 TRACE("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]);
645 TRACE("Attr = 0x%x\n", DirEntry
->Attr
);
646 TRACE("ReservedNT = 0x%x\n", DirEntry
->ReservedNT
);
647 TRACE("TimeInTenths = %d\n", DirEntry
->TimeInTenths
);
648 TRACE("CreateTime = %d\n", DirEntry
->CreateTime
);
649 TRACE("CreateDate = %d\n", DirEntry
->CreateDate
);
650 TRACE("LastAccessDate = %d\n", DirEntry
->LastAccessDate
);
651 TRACE("ClusterHigh = 0x%x\n", DirEntry
->ClusterHigh
);
652 TRACE("Time = %d\n", DirEntry
->Time
);
653 TRACE("Date = %d\n", DirEntry
->Date
);
654 TRACE("ClusterLow = 0x%x\n", DirEntry
->ClusterLow
);
655 TRACE("Size = %d\n", DirEntry
->Size
);
658 // Get the cluster chain
660 StartCluster
= ((ULONG
)DirEntry
->ClusterHigh
<< 16) + DirEntry
->ClusterLow
;
661 TRACE("StartCluster = 0x%x\n", StartCluster
);
662 FatFileInfoPointer
->FileFatChain
= FatGetClusterChainArray(Volume
, StartCluster
);
665 // See if memory allocation failed
667 if (FatFileInfoPointer
->FileFatChain
== NULL
)
676 // Nope, no match - zero buffers and continue looking
678 memset(ShortNameBuffer
, 0, 13 * sizeof(UCHAR
));
679 memset(LfnNameBuffer
, 0, 261 * sizeof(UCHAR
));
686 static BOOLEAN
FatXSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume
, PVOID DirectoryBuffer
, ULONG DirectorySize
, PCHAR FileName
, PFAT_FILE_INFO FatFileInfoPointer
)
691 FATX_DIRENTRY OurDirEntry
;
692 PFATX_DIRENTRY DirEntry
= &OurDirEntry
;
694 EntryCount
= DirectorySize
/ sizeof(FATX_DIRENTRY
);
696 TRACE("FatXSearchDirectoryBufferForFile() DirectoryBuffer = 0x%x EntryCount = %d FileName = %s\n", DirectoryBuffer
, EntryCount
, FileName
);
698 FileNameLen
= strlen(FileName
);
700 for (CurrentEntry
= 0; CurrentEntry
< EntryCount
; CurrentEntry
++, DirectoryBuffer
= ((PFATX_DIRENTRY
)DirectoryBuffer
)+1)
702 OurDirEntry
= *(PFATX_DIRENTRY
) DirectoryBuffer
;
703 FatSwapFatXDirEntry(&OurDirEntry
);
704 if (0xff == DirEntry
->FileNameSize
)
708 if (0xe5 == DirEntry
->FileNameSize
)
712 if (FileNameLen
== DirEntry
->FileNameSize
&&
713 0 == _strnicmp(FileName
, DirEntry
->FileName
, FileNameLen
))
716 * We found the entry, now fill in the FAT_FILE_INFO struct
718 FatFileInfoPointer
->FileSize
= DirEntry
->Size
;
719 FatFileInfoPointer
->FilePointer
= 0;
721 TRACE("FATX Directory Entry:\n");
722 TRACE("FileNameSize = %d\n", DirEntry
->FileNameSize
);
723 TRACE("Attr = 0x%x\n", DirEntry
->Attr
);
724 TRACE("StartCluster = 0x%x\n", DirEntry
->StartCluster
);
725 TRACE("Size = %d\n", DirEntry
->Size
);
726 TRACE("Time = %d\n", DirEntry
->Time
);
727 TRACE("Date = %d\n", DirEntry
->Date
);
728 TRACE("CreateTime = %d\n", DirEntry
->CreateTime
);
729 TRACE("CreateDate = %d\n", DirEntry
->CreateDate
);
730 TRACE("LastAccessTime = %d\n", DirEntry
->LastAccessTime
);
731 TRACE("LastAccessDate = %d\n", DirEntry
->LastAccessDate
);
734 * Get the cluster chain
736 FatFileInfoPointer
->FileFatChain
= FatGetClusterChainArray(Volume
, DirEntry
->StartCluster
);
739 * See if memory allocation failed
741 if (NULL
== FatFileInfoPointer
->FileFatChain
)
755 * This function searches the file system for the
756 * specified filename and fills in an FAT_FILE_INFO structure
757 * with info describing the file, etc. returns ARC error code
759 LONG
FatLookupFile(PFAT_VOLUME_INFO Volume
, PCSTR FileName
, ULONG DeviceId
, PFAT_FILE_INFO FatFileInfoPointer
)
762 ULONG NumberOfPathParts
;
764 PVOID DirectoryBuffer
;
765 ULONG DirectoryStartCluster
= 0;
767 FAT_FILE_INFO FatFileInfo
;
769 TRACE("FatLookupFile() FileName = %s\n", FileName
);
771 memset(FatFileInfoPointer
, 0, sizeof(FAT_FILE_INFO
));
774 // Figure out how many sub-directories we are nested in
776 NumberOfPathParts
= FsGetNumPathParts(FileName
);
779 // Loop once for each part
781 for (i
=0; i
<NumberOfPathParts
; i
++)
784 // Get first path part
786 FsGetFirstNameFromPath(PathPart
, FileName
);
789 // Advance to the next part of the path
791 for (; (*FileName
!= '\\') && (*FileName
!= '/') && (*FileName
!= '\0'); FileName
++)
797 // Buffer the directory contents
799 DirectoryBuffer
= FatBufferDirectory(Volume
, DirectoryStartCluster
, &DirectorySize
, (i
== 0) );
800 if (DirectoryBuffer
== NULL
)
806 // Search for file name in directory
808 if (ISFATX(Volume
->FatType
))
810 if (!FatXSearchDirectoryBufferForFile(Volume
, DirectoryBuffer
, DirectorySize
, PathPart
, &FatFileInfo
))
817 if (!FatSearchDirectoryBufferForFile(Volume
, DirectoryBuffer
, DirectorySize
, PathPart
, &FatFileInfo
))
824 // If we have another sub-directory to go then
825 // grab the start cluster and free the fat chain array
827 if ((i
+1) < NumberOfPathParts
)
830 // Check if current entry is a directory
832 if (!(FatFileInfo
.Attributes
& ATTR_DIRECTORY
))
834 MmHeapFree(FatFileInfo
.FileFatChain
);
837 DirectoryStartCluster
= FatFileInfo
.FileFatChain
[0];
838 MmHeapFree(FatFileInfo
.FileFatChain
);
839 FatFileInfo
.FileFatChain
= NULL
;
843 memcpy(FatFileInfoPointer
, &FatFileInfo
, sizeof(FAT_FILE_INFO
));
850 * This function parses a directory entry name which
851 * is in the form of "FILE EXT" and puts it in Buffer
852 * in the form of "file.ext"
854 void FatParseShortFileName(PCHAR Buffer
, PDIRENTRY DirEntry
)
859 RtlZeroMemory(Buffer
, 13);
862 // Fixup first character
864 if (DirEntry
->FileName
[0] == 0x05)
866 DirEntry
->FileName
[0] = 0xE5;
874 if (DirEntry
->FileName
[Idx
] == ' ')
879 Buffer
[Idx
] = DirEntry
->FileName
[Idx
];
886 if ((DirEntry
->FileName
[8] != ' '))
889 Buffer
[Idx
++] = (DirEntry
->FileName
[8] == ' ') ? '\0' : DirEntry
->FileName
[8];
890 Buffer
[Idx
++] = (DirEntry
->FileName
[9] == ' ') ? '\0' : DirEntry
->FileName
[9];
891 Buffer
[Idx
++] = (DirEntry
->FileName
[10] == ' ') ? '\0' : DirEntry
->FileName
[10];
894 //TRACE("FatParseShortFileName() ShortName = %s\n", Buffer);
899 * returns the Fat entry for a given cluster number
901 BOOLEAN
FatGetFatEntry(PFAT_VOLUME_INFO Volume
, ULONG Cluster
, ULONG
* ClusterPointer
)
905 UINT32 ThisFatSecNum
;
906 UINT32 ThisFatEntOffset
;
909 BOOLEAN status
= TRUE
;;
911 //TRACE("FatGetFatEntry() Retrieving FAT entry for cluster %d.\n", Cluster);
913 // We need a buffer for 2 secors
914 ReadBuffer
= HeapAllocate(FrLdrTempHeap
, 2 * Volume
->BytesPerSector
, 'xTAF');
920 switch(Volume
->FatType
)
924 FatOffset
= Cluster
+ (Cluster
/ 2);
925 ThisFatSecNum
= Volume
->ActiveFatSectorStart
+ (FatOffset
/ Volume
->BytesPerSector
);
926 ThisFatEntOffset
= (FatOffset
% Volume
->BytesPerSector
);
928 TRACE("FatOffset: %d\n", FatOffset
);
929 TRACE("ThisFatSecNum: %d\n", ThisFatSecNum
);
930 TRACE("ThisFatEntOffset: %d\n", ThisFatEntOffset
);
933 if (ThisFatEntOffset
== (Volume
->BytesPerSector
- 1))
942 if (!FatReadVolumeSectors(Volume
, ThisFatSecNum
, SectorCount
, ReadBuffer
))
948 fat
= *((USHORT
*) (ReadBuffer
+ ThisFatEntOffset
));
950 if (Cluster
& 0x0001)
951 fat
= fat
>> 4; /* Cluster number is ODD */
953 fat
= fat
& 0x0FFF; /* Cluster number is EVEN */
960 FatOffset
= (Cluster
* 2);
961 ThisFatSecNum
= Volume
->ActiveFatSectorStart
+ (FatOffset
/ Volume
->BytesPerSector
);
962 ThisFatEntOffset
= (FatOffset
% Volume
->BytesPerSector
);
964 if (!FatReadVolumeSectors(Volume
, ThisFatSecNum
, 1, ReadBuffer
))
970 fat
= *((USHORT
*) (ReadBuffer
+ ThisFatEntOffset
));
978 FatOffset
= (Cluster
* 4);
979 ThisFatSecNum
= Volume
->ActiveFatSectorStart
+ (FatOffset
/ Volume
->BytesPerSector
);
980 ThisFatEntOffset
= (FatOffset
% Volume
->BytesPerSector
);
982 if (!FatReadVolumeSectors(Volume
, ThisFatSecNum
, 1, ReadBuffer
))
988 fat
= (*((ULONG
*) (ReadBuffer
+ ThisFatEntOffset
))) & 0x0FFFFFFF;
994 ERR("Unknown FAT type %d\n", Volume
->FatType
);
999 //TRACE("FAT entry is 0x%x.\n", fat);
1001 HeapFree(FrLdrTempHeap
, ReadBuffer
, 'xTAF');
1003 *ClusterPointer
= fat
;
1008 ULONG
FatCountClustersInChain(PFAT_VOLUME_INFO Volume
, ULONG StartCluster
)
1010 ULONG ClusterCount
= 0;
1012 TRACE("FatCountClustersInChain() StartCluster = %d\n", StartCluster
);
1017 // If end of chain then break out of our cluster counting loop
1019 if (((Volume
->FatType
== FAT12
) && (StartCluster
>= 0xff8)) ||
1020 ((Volume
->FatType
== FAT16
|| Volume
->FatType
== FATX16
) && (StartCluster
>= 0xfff8)) ||
1021 ((Volume
->FatType
== FAT32
|| Volume
->FatType
== FATX32
) && (StartCluster
>= 0x0ffffff8)))
1034 if (!FatGetFatEntry(Volume
, StartCluster
, &StartCluster
))
1040 TRACE("FatCountClustersInChain() ClusterCount = %d\n", ClusterCount
);
1042 return ClusterCount
;
1045 ULONG
* FatGetClusterChainArray(PFAT_VOLUME_INFO Volume
, ULONG StartCluster
)
1049 ULONG
* ArrayPointer
;
1052 TRACE("FatGetClusterChainArray() StartCluster = %d\n", StartCluster
);
1054 ClusterCount
= FatCountClustersInChain(Volume
, StartCluster
) + 1; // Lets get the 0x0ffffff8 on the end of the array
1055 ArraySize
= ClusterCount
* sizeof(ULONG
);
1058 // Allocate array memory
1060 ArrayPointer
= MmHeapAlloc(ArraySize
);
1062 if (ArrayPointer
== NULL
)
1068 // Loop through and set array values
1070 for (Idx
=0; Idx
<ClusterCount
; Idx
++)
1073 // Set current cluster
1075 ArrayPointer
[Idx
] = StartCluster
;
1078 // Don't try to get next cluster for last cluster
1080 if (((Volume
->FatType
== FAT12
) && (StartCluster
>= 0xff8)) ||
1081 ((Volume
->FatType
== FAT16
|| Volume
->FatType
== FATX16
) && (StartCluster
>= 0xfff8)) ||
1082 ((Volume
->FatType
== FAT32
|| Volume
->FatType
== FATX32
) && (StartCluster
>= 0x0ffffff8)))
1091 if (!FatGetFatEntry(Volume
, StartCluster
, &StartCluster
))
1093 MmHeapFree(ArrayPointer
);
1098 return ArrayPointer
;
1102 * FatReadClusterChain()
1103 * Reads the specified clusters into memory
1105 BOOLEAN
FatReadClusterChain(PFAT_VOLUME_INFO Volume
, ULONG StartClusterNumber
, ULONG NumberOfClusters
, PVOID Buffer
)
1107 ULONG ClusterStartSector
;
1109 TRACE("FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber
, NumberOfClusters
, Buffer
);
1111 while (NumberOfClusters
> 0)
1114 //TRACE("FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber, NumberOfClusters, Buffer);
1116 // Calculate starting sector for cluster
1118 ClusterStartSector
= ((StartClusterNumber
- 2) * Volume
->SectorsPerCluster
) + Volume
->DataSectorStart
;
1121 // Read cluster into memory
1123 if (!FatReadVolumeSectors(Volume
, ClusterStartSector
, Volume
->SectorsPerCluster
, Buffer
))
1129 // Decrement count of clusters left to read
1134 // Increment buffer address by cluster size
1136 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (Volume
->SectorsPerCluster
* Volume
->BytesPerSector
));
1141 if (!FatGetFatEntry(Volume
, StartClusterNumber
, &StartClusterNumber
))
1147 // If end of chain then break out of our cluster reading loop
1149 if (((Volume
->FatType
== FAT12
) && (StartClusterNumber
>= 0xff8)) ||
1150 ((Volume
->FatType
== FAT16
|| Volume
->FatType
== FATX16
) && (StartClusterNumber
>= 0xfff8)) ||
1151 ((Volume
->FatType
== FAT32
|| Volume
->FatType
== FATX32
) && (StartClusterNumber
>= 0x0ffffff8)))
1161 * FatReadPartialCluster()
1162 * Reads part of a cluster into memory
1164 BOOLEAN
FatReadPartialCluster(PFAT_VOLUME_INFO Volume
, ULONG ClusterNumber
, ULONG StartingOffset
, ULONG Length
, PVOID Buffer
)
1166 ULONG ClusterStartSector
;
1167 ULONG SectorOffset
, ReadSize
, SectorCount
;
1169 BOOLEAN status
= FALSE
;
1171 //TRACE("FatReadPartialCluster() ClusterNumber = %d StartingOffset = %d Length = %d Buffer = 0x%x\n", ClusterNumber, StartingOffset, Length, Buffer);
1173 ClusterStartSector
= ((ClusterNumber
- 2) * Volume
->SectorsPerCluster
) + Volume
->DataSectorStart
;
1175 // This is the offset of the data in sectors
1176 SectorOffset
= (StartingOffset
/ Volume
->BytesPerSector
);
1177 StartingOffset
%= Volume
->BytesPerSector
;
1179 // Calculate how many sectors we need to read
1180 SectorCount
= (StartingOffset
+ Length
+ Volume
->BytesPerSector
- 1) / Volume
->BytesPerSector
;
1182 // Calculate rounded up read size
1183 ReadSize
= SectorCount
* Volume
->BytesPerSector
;
1185 ReadBuffer
= HeapAllocate(FrLdrTempHeap
, ReadSize
, 'xTAF');
1191 if (FatReadVolumeSectors(Volume
, ClusterStartSector
+ SectorOffset
, SectorCount
, ReadBuffer
))
1193 memcpy(Buffer
, ReadBuffer
+ StartingOffset
, Length
);
1197 HeapFree(FrLdrTempHeap
, ReadBuffer
, 'xTAF');
1204 * Reads BytesToRead from open file and
1205 * returns the number of bytes read in BytesRead
1207 BOOLEAN
FatReadFile(PFAT_FILE_INFO FatFileInfo
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
)
1209 PFAT_VOLUME_INFO Volume
= FatFileInfo
->Volume
;
1210 ULONG ClusterNumber
;
1211 ULONG OffsetInCluster
;
1212 ULONG LengthInCluster
;
1213 ULONG NumberOfClusters
;
1214 ULONG BytesPerCluster
;
1216 TRACE("FatReadFile() BytesToRead = %d Buffer = 0x%x\n", BytesToRead
, Buffer
);
1218 if (BytesRead
!= NULL
)
1224 // If they are trying to read past the
1225 // end of the file then return success
1226 // with BytesRead == 0
1228 if (FatFileInfo
->FilePointer
>= FatFileInfo
->FileSize
)
1234 // If they are trying to read more than there is to read
1235 // then adjust the amount to read
1237 if ((FatFileInfo
->FilePointer
+ BytesToRead
) > FatFileInfo
->FileSize
)
1239 BytesToRead
= (FatFileInfo
->FileSize
- FatFileInfo
->FilePointer
);
1243 // Ok, now we have to perform at most 3 calculations
1244 // I'll draw you a picture (using nifty ASCII art):
1246 // CurrentFilePointer -+
1248 // +----------------+
1250 // +-----------+-----------+-----------+-----------+
1251 // | Cluster 1 | Cluster 2 | Cluster 3 | Cluster 4 |
1252 // +-----------+-----------+-----------+-----------+
1254 // +---------------+--------------------+
1256 // BytesToRead -------+
1258 // 1 - The first calculation (and read) will align
1259 // the file pointer with the next cluster.
1260 // boundary (if we are supposed to read that much)
1261 // 2 - The next calculation (and read) will read
1262 // in all the full clusters that the requested
1263 // amount of data would cover (in this case
1265 // 3 - The last calculation (and read) would read
1266 // in the remainder of the data requested out of
1267 // the last cluster.
1270 BytesPerCluster
= Volume
->SectorsPerCluster
* Volume
->BytesPerSector
;
1273 // Only do the first read if we
1274 // aren't aligned on a cluster boundary
1276 if (FatFileInfo
->FilePointer
% BytesPerCluster
)
1279 // Do the math for our first read
1281 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1282 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1283 OffsetInCluster
= (FatFileInfo
->FilePointer
% BytesPerCluster
);
1284 LengthInCluster
= (BytesToRead
> (BytesPerCluster
- OffsetInCluster
)) ? (BytesPerCluster
- OffsetInCluster
) : BytesToRead
;
1287 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1289 if (!FatReadPartialCluster(Volume
, ClusterNumber
, OffsetInCluster
, LengthInCluster
, Buffer
))
1293 if (BytesRead
!= NULL
)
1295 *BytesRead
+= LengthInCluster
;
1297 BytesToRead
-= LengthInCluster
;
1298 FatFileInfo
->FilePointer
+= LengthInCluster
;
1299 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ LengthInCluster
);
1303 // Do the math for our second read (if any data left)
1305 if (BytesToRead
> 0)
1308 // Determine how many full clusters we need to read
1310 NumberOfClusters
= (BytesToRead
/ BytesPerCluster
);
1312 if (NumberOfClusters
> 0)
1314 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1315 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1318 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1320 if (!FatReadClusterChain(Volume
, ClusterNumber
, NumberOfClusters
, Buffer
))
1324 if (BytesRead
!= NULL
)
1326 *BytesRead
+= (NumberOfClusters
* BytesPerCluster
);
1328 BytesToRead
-= (NumberOfClusters
* BytesPerCluster
);
1329 FatFileInfo
->FilePointer
+= (NumberOfClusters
* BytesPerCluster
);
1330 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfClusters
* BytesPerCluster
));
1335 // Do the math for our third read (if any data left)
1337 if (BytesToRead
> 0)
1339 ClusterNumber
= (FatFileInfo
->FilePointer
/ BytesPerCluster
);
1340 ClusterNumber
= FatFileInfo
->FileFatChain
[ClusterNumber
];
1343 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1345 if (!FatReadPartialCluster(Volume
, ClusterNumber
, 0, BytesToRead
, Buffer
))
1349 if (BytesRead
!= NULL
)
1351 *BytesRead
+= BytesToRead
;
1353 FatFileInfo
->FilePointer
+= BytesToRead
;
1354 BytesToRead
-= BytesToRead
;
1355 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ BytesToRead
);
1361 BOOLEAN
FatReadVolumeSectors(PFAT_VOLUME_INFO Volume
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
1363 LARGE_INTEGER Position
;
1367 //TRACE("FatReadVolumeSectors(): SectorNumber %d, SectorCount %d, Buffer %p\n",
1368 // SectorNumber, SectorCount, Buffer);
1371 // Seek to right position
1373 Position
.QuadPart
= (ULONGLONG
)SectorNumber
* 512;
1374 ret
= ArcSeek(Volume
->DeviceId
, &Position
, SeekAbsolute
);
1375 if (ret
!= ESUCCESS
)
1377 TRACE("FatReadVolumeSectors() Failed to seek\n");
1384 ret
= ArcRead(Volume
->DeviceId
, Buffer
, SectorCount
* 512, &Count
);
1385 if (ret
!= ESUCCESS
|| Count
!= SectorCount
* 512)
1387 TRACE("FatReadVolumeSectors() Failed to read\n");
1395 LONG
FatClose(ULONG FileId
)
1397 PFAT_FILE_INFO FileHandle
= FsGetDeviceSpecific(FileId
);
1399 if (FileHandle
->FileFatChain
) MmHeapFree(FileHandle
->FileFatChain
);
1400 MmHeapFree(FileHandle
);
1405 LONG
FatGetFileInformation(ULONG FileId
, FILEINFORMATION
* Information
)
1407 PFAT_FILE_INFO FileHandle
= FsGetDeviceSpecific(FileId
);
1409 RtlZeroMemory(Information
, sizeof(FILEINFORMATION
));
1410 Information
->EndingAddress
.LowPart
= FileHandle
->FileSize
;
1411 Information
->CurrentAddress
.LowPart
= FileHandle
->FilePointer
;
1413 TRACE("FatGetFileInformation() FileSize = %d\n",
1414 Information
->EndingAddress
.LowPart
);
1415 TRACE("FatGetFileInformation() FilePointer = %d\n",
1416 Information
->CurrentAddress
.LowPart
);
1421 LONG
FatOpen(CHAR
* Path
, OPENMODE OpenMode
, ULONG
* FileId
)
1423 PFAT_VOLUME_INFO FatVolume
;
1424 FAT_FILE_INFO TempFileInfo
;
1425 PFAT_FILE_INFO FileHandle
;
1427 BOOLEAN IsDirectory
;
1430 if (OpenMode
!= OpenReadOnly
&& OpenMode
!= OpenDirectory
)
1433 DeviceId
= FsGetDeviceId(*FileId
);
1434 FatVolume
= FatVolumes
[DeviceId
];
1436 TRACE("FatOpen() FileName = %s\n", Path
);
1438 RtlZeroMemory(&TempFileInfo
, sizeof(TempFileInfo
));
1439 ret
= FatLookupFile(FatVolume
, Path
, DeviceId
, &TempFileInfo
);
1440 if (ret
!= ESUCCESS
)
1444 // Check if caller opened what he expected (dir vs file)
1446 IsDirectory
= (TempFileInfo
.Attributes
& ATTR_DIRECTORY
) != 0;
1447 if (IsDirectory
&& OpenMode
!= OpenDirectory
)
1449 else if (!IsDirectory
&& OpenMode
!= OpenReadOnly
)
1452 FileHandle
= MmHeapAlloc(sizeof(FAT_FILE_INFO
));
1456 RtlCopyMemory(FileHandle
, &TempFileInfo
, sizeof(FAT_FILE_INFO
));
1457 FileHandle
->Volume
= FatVolume
;
1459 FsSetDeviceSpecific(*FileId
, FileHandle
);
1463 LONG
FatRead(ULONG FileId
, VOID
* Buffer
, ULONG N
, ULONG
* Count
)
1465 PFAT_FILE_INFO FileHandle
= FsGetDeviceSpecific(FileId
);
1469 // Call old read method
1471 ret
= FatReadFile(FileHandle
, N
, Count
, Buffer
);
1474 // Check for success
1482 LONG
FatSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
1484 PFAT_FILE_INFO FileHandle
= FsGetDeviceSpecific(FileId
);
1486 TRACE("FatSeek() NewFilePointer = %lu\n", Position
->LowPart
);
1488 if (SeekMode
!= SeekAbsolute
)
1490 if (Position
->HighPart
!= 0)
1492 if (Position
->LowPart
>= FileHandle
->FileSize
)
1495 FileHandle
->FilePointer
= Position
->LowPart
;
1499 const DEVVTBL FatFuncTable
=
1502 FatGetFileInformation
,
1509 const DEVVTBL
* FatMount(ULONG DeviceId
)
1511 PFAT_VOLUME_INFO Volume
;
1513 PFAT_BOOTSECTOR BootSector
= (PFAT_BOOTSECTOR
)Buffer
;
1514 PFAT32_BOOTSECTOR BootSector32
= (PFAT32_BOOTSECTOR
)Buffer
;
1515 PFATX_BOOTSECTOR BootSectorX
= (PFATX_BOOTSECTOR
)Buffer
;
1516 FILEINFORMATION FileInformation
;
1517 LARGE_INTEGER Position
;
1519 ULARGE_INTEGER SectorCount
;
1523 // Allocate data for volume information
1525 Volume
= MmHeapAlloc(sizeof(FAT_VOLUME_INFO
));
1528 RtlZeroMemory(Volume
, sizeof(FAT_VOLUME_INFO
));
1531 // Read the BootSector
1533 Position
.HighPart
= 0;
1534 Position
.LowPart
= 0;
1535 ret
= ArcSeek(DeviceId
, &Position
, SeekAbsolute
);
1536 if (ret
!= ESUCCESS
)
1541 ret
= ArcRead(DeviceId
, Buffer
, sizeof(Buffer
), &Count
);
1542 if (ret
!= ESUCCESS
|| Count
!= sizeof(Buffer
))
1549 // Check if BootSector is valid. If no, return early
1551 if (!RtlEqualMemory(BootSector
->FileSystemType
, "FAT12 ", 8) &&
1552 !RtlEqualMemory(BootSector
->FileSystemType
, "FAT16 ", 8) &&
1553 !RtlEqualMemory(BootSector32
->FileSystemType
, "FAT32 ", 8) &&
1554 !RtlEqualMemory(BootSectorX
->FileSystemType
, "FATX", 4))
1561 // Determine sector count
1563 ret
= ArcGetFileInformation(DeviceId
, &FileInformation
);
1564 if (ret
!= ESUCCESS
)
1569 SectorCount
.HighPart
= FileInformation
.EndingAddress
.HighPart
;
1570 SectorCount
.LowPart
= FileInformation
.EndingAddress
.LowPart
;
1571 SectorCount
.QuadPart
/= SECTOR_SIZE
;
1576 Volume
->DeviceId
= DeviceId
;
1579 // Really open the volume
1581 if (!FatOpenVolume(Volume
, BootSector
, SectorCount
.QuadPart
))
1588 // Remember FAT volume information
1590 FatVolumes
[DeviceId
] = Volume
;
1595 return &FatFuncTable
;