Fixed bug in LBA code.
[reactos.git] / freeldr / freeldr / fs / fat.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <freeldr.h>
21 #include <fs.h>
22 #include "fat.h"
23 #include <disk.h>
24 #include <rtl.h>
25 #include <ui.h>
26 #include <arch.h>
27 #include <mm.h>
28 #include <debug.h>
29 #include <cache.h>
30
31
32 PFAT_BOOTSECTOR FatVolumeBootSector = NULL;
33 PFAT32_BOOTSECTOR Fat32VolumeBootSector = NULL;
34
35 ULONG RootDirSectorStart; // Starting sector of the root directory (fat12/16)
36 ULONG DataSectorStart; // Starting sector of the data area
37 ULONG SectorsPerFat; // Sectors per FAT table
38 ULONG RootDirSectors; // Number of sectors of the root directory (fat32)
39
40 ULONG FatType = 0; // FAT12, FAT16, or FAT32
41 ULONG FatDriveNumber = 0;
42
43 BOOL FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector)
44 {
45
46 DbgPrint((DPRINT_FILESYSTEM, "FatOpenVolume() DriveNumber = 0x%x VolumeStartSector = %d\n", DriveNumber, VolumeStartSector));
47
48 // Store the drive number
49 FatDriveNumber = DriveNumber;
50
51 //
52 // Free any memory previously allocated
53 //
54 if (FatVolumeBootSector != NULL)
55 {
56 FreeMemory(FatVolumeBootSector);
57
58 FatVolumeBootSector = NULL;
59 Fat32VolumeBootSector = NULL;
60 }
61
62 //
63 // Now allocate the memory to hold the boot sector
64 //
65 FatVolumeBootSector = (PFAT_BOOTSECTOR) AllocateMemory(512);
66 Fat32VolumeBootSector = (PFAT32_BOOTSECTOR) FatVolumeBootSector;
67
68 //
69 // Make sure we got the memory
70 //
71 if (FatVolumeBootSector == NULL)
72 {
73 FileSystemError("Out of memory.");
74 return FALSE;
75 }
76
77 // Now try to read the boot sector
78 // If this fails then abort
79 if (!DiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, FatVolumeBootSector))
80 {
81 return FALSE;
82 }
83
84 // Get the FAT type
85 FatType = FatDetermineFatType(FatVolumeBootSector);
86
87 #ifdef DEBUG
88
89 DbgPrint((DPRINT_FILESYSTEM, "Dumping boot sector:\n"));
90
91 if (FatType == FAT32)
92 {
93 DbgPrint((DPRINT_FILESYSTEM, "sizeof(FAT32_BOOTSECTOR) = 0x%x.\n", sizeof(FAT32_BOOTSECTOR)));
94
95 DbgPrint((DPRINT_FILESYSTEM, "JumpBoot: 0x%x 0x%x 0x%x\n", Fat32VolumeBootSector->JumpBoot[0], Fat32VolumeBootSector->JumpBoot[1], Fat32VolumeBootSector->JumpBoot[2]));
96 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]));
97 DbgPrint((DPRINT_FILESYSTEM, "BytesPerSector: %d\n", Fat32VolumeBootSector->BytesPerSector));
98 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerCluster: %d\n", Fat32VolumeBootSector->SectorsPerCluster));
99 DbgPrint((DPRINT_FILESYSTEM, "ReservedSectors: %d\n", Fat32VolumeBootSector->ReservedSectors));
100 DbgPrint((DPRINT_FILESYSTEM, "NumberOfFats: %d\n", Fat32VolumeBootSector->NumberOfFats));
101 DbgPrint((DPRINT_FILESYSTEM, "RootDirEntries: %d\n", Fat32VolumeBootSector->RootDirEntries));
102 DbgPrint((DPRINT_FILESYSTEM, "TotalSectors: %d\n", Fat32VolumeBootSector->TotalSectors));
103 DbgPrint((DPRINT_FILESYSTEM, "MediaDescriptor: 0x%x\n", Fat32VolumeBootSector->MediaDescriptor));
104 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerFat: %d\n", Fat32VolumeBootSector->SectorsPerFat));
105 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerTrack: %d\n", Fat32VolumeBootSector->SectorsPerTrack));
106 DbgPrint((DPRINT_FILESYSTEM, "NumberOfHeads: %d\n", Fat32VolumeBootSector->NumberOfHeads));
107 DbgPrint((DPRINT_FILESYSTEM, "HiddenSectors: %d\n", Fat32VolumeBootSector->HiddenSectors));
108 DbgPrint((DPRINT_FILESYSTEM, "TotalSectorsBig: %d\n", Fat32VolumeBootSector->TotalSectorsBig));
109 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerFatBig: %d\n", Fat32VolumeBootSector->SectorsPerFatBig));
110 DbgPrint((DPRINT_FILESYSTEM, "ExtendedFlags: 0x%x\n", Fat32VolumeBootSector->ExtendedFlags));
111 DbgPrint((DPRINT_FILESYSTEM, "FileSystemVersion: 0x%x\n", Fat32VolumeBootSector->FileSystemVersion));
112 DbgPrint((DPRINT_FILESYSTEM, "RootDirStartCluster: %d\n", Fat32VolumeBootSector->RootDirStartCluster));
113 DbgPrint((DPRINT_FILESYSTEM, "FsInfo: %d\n", Fat32VolumeBootSector->FsInfo));
114 DbgPrint((DPRINT_FILESYSTEM, "BackupBootSector: %d\n", Fat32VolumeBootSector->BackupBootSector));
115 DbgPrint((DPRINT_FILESYSTEM, "Reserved: 0x%x\n", Fat32VolumeBootSector->Reserved));
116 DbgPrint((DPRINT_FILESYSTEM, "DriveNumber: 0x%x\n", Fat32VolumeBootSector->DriveNumber));
117 DbgPrint((DPRINT_FILESYSTEM, "Reserved1: 0x%x\n", Fat32VolumeBootSector->Reserved1));
118 DbgPrint((DPRINT_FILESYSTEM, "BootSignature: 0x%x\n", Fat32VolumeBootSector->BootSignature));
119 DbgPrint((DPRINT_FILESYSTEM, "VolumeSerialNumber: 0x%x\n", Fat32VolumeBootSector->VolumeSerialNumber));
120 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]));
121 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]));
122 DbgPrint((DPRINT_FILESYSTEM, "BootSectorMagic: 0x%x\n", Fat32VolumeBootSector->BootSectorMagic));
123 }
124 else
125 {
126 DbgPrint((DPRINT_FILESYSTEM, "sizeof(FAT_BOOTSECTOR) = 0x%x.\n", sizeof(FAT_BOOTSECTOR)));
127
128 DbgPrint((DPRINT_FILESYSTEM, "JumpBoot: 0x%x 0x%x 0x%x\n", FatVolumeBootSector->JumpBoot[0], FatVolumeBootSector->JumpBoot[1], FatVolumeBootSector->JumpBoot[2]));
129 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]));
130 DbgPrint((DPRINT_FILESYSTEM, "BytesPerSector: %d\n", FatVolumeBootSector->BytesPerSector));
131 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerCluster: %d\n", FatVolumeBootSector->SectorsPerCluster));
132 DbgPrint((DPRINT_FILESYSTEM, "ReservedSectors: %d\n", FatVolumeBootSector->ReservedSectors));
133 DbgPrint((DPRINT_FILESYSTEM, "NumberOfFats: %d\n", FatVolumeBootSector->NumberOfFats));
134 DbgPrint((DPRINT_FILESYSTEM, "RootDirEntries: %d\n", FatVolumeBootSector->RootDirEntries));
135 DbgPrint((DPRINT_FILESYSTEM, "TotalSectors: %d\n", FatVolumeBootSector->TotalSectors));
136 DbgPrint((DPRINT_FILESYSTEM, "MediaDescriptor: 0x%x\n", FatVolumeBootSector->MediaDescriptor));
137 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerFat: %d\n", FatVolumeBootSector->SectorsPerFat));
138 DbgPrint((DPRINT_FILESYSTEM, "SectorsPerTrack: %d\n", FatVolumeBootSector->SectorsPerTrack));
139 DbgPrint((DPRINT_FILESYSTEM, "NumberOfHeads: %d\n", FatVolumeBootSector->NumberOfHeads));
140 DbgPrint((DPRINT_FILESYSTEM, "HiddenSectors: %d\n", FatVolumeBootSector->HiddenSectors));
141 DbgPrint((DPRINT_FILESYSTEM, "TotalSectorsBig: %d\n", FatVolumeBootSector->TotalSectorsBig));
142 DbgPrint((DPRINT_FILESYSTEM, "DriveNumber: 0x%x\n", FatVolumeBootSector->DriveNumber));
143 DbgPrint((DPRINT_FILESYSTEM, "Reserved1: 0x%x\n", FatVolumeBootSector->Reserved1));
144 DbgPrint((DPRINT_FILESYSTEM, "BootSignature: 0x%x\n", FatVolumeBootSector->BootSignature));
145 DbgPrint((DPRINT_FILESYSTEM, "VolumeSerialNumber: 0x%x\n", FatVolumeBootSector->VolumeSerialNumber));
146 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]));
147 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]));
148 DbgPrint((DPRINT_FILESYSTEM, "BootSectorMagic: 0x%x\n", FatVolumeBootSector->BootSectorMagic));
149 }
150
151 #endif // defined DEBUG
152
153 //
154 // Check the boot sector magic
155 //
156 if (FatVolumeBootSector->BootSectorMagic != 0xaa55)
157 {
158 FileSystemError("Invalid boot sector magic (0xaa55)");
159 return FALSE;
160 }
161
162 //
163 // Check the FAT cluster size
164 // We do not support clusters bigger than 64k
165 //
166 if ((FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector) > (64 * 1024))
167 {
168 FileSystemError("This file system has cluster sizes bigger than 64k.\nFreeLoader does not support this.");
169 return FALSE;
170 }
171
172 //
173 // Clear our variables
174 //
175 RootDirSectorStart = 0;
176 DataSectorStart = 0;
177 SectorsPerFat = 0;
178 RootDirSectors = 0;
179
180 //
181 // Get the sectors per FAT,
182 // root directory starting sector,
183 // and data sector start
184 //
185 if (FatType != FAT32)
186 {
187 SectorsPerFat = FatVolumeBootSector->SectorsPerFat;
188
189 RootDirSectorStart = (FatVolumeBootSector->NumberOfFats * SectorsPerFat) + FatVolumeBootSector->ReservedSectors;
190 RootDirSectors = ((FatVolumeBootSector->RootDirEntries * 32) + (FatVolumeBootSector->BytesPerSector - 1)) / FatVolumeBootSector->BytesPerSector;
191
192 DataSectorStart = FatVolumeBootSector->ReservedSectors + (FatVolumeBootSector->NumberOfFats * FatVolumeBootSector->SectorsPerFat) + RootDirSectors;
193 }
194 else
195 {
196 SectorsPerFat = Fat32VolumeBootSector->SectorsPerFatBig;
197
198 DataSectorStart = FatVolumeBootSector->ReservedSectors + (FatVolumeBootSector->NumberOfFats * Fat32VolumeBootSector->SectorsPerFatBig) + RootDirSectors;
199
200
201 //
202 // Check version
203 // we only work with version 0
204 //
205 if (Fat32VolumeBootSector->FileSystemVersion != 0)
206 {
207 FileSystemError("FreeLoader is too old to work with this FAT32 filesystem.\nPlease update FreeLoader.");
208 return FALSE;
209 }
210 }
211
212 //
213 // Initialize the disk cache for this drive
214 //
215 if (!CacheInitializeDrive(DriveNumber))
216 {
217 return FALSE;
218 }
219
220 //
221 // Force the FAT sectors into the cache
222 // as long as it is FAT12 or FAT16. FAT32 can
223 // have a multi-megabyte FAT so we don't want that.
224 //
225 if (FatType != FAT32)
226 {
227 if (!CacheForceDiskSectorsIntoCache(DriveNumber, FatVolumeBootSector->HiddenSectors + FatVolumeBootSector->ReservedSectors, FatVolumeBootSector->SectorsPerFat))
228 {
229 return FALSE;
230 }
231 }
232
233 return TRUE;
234 }
235
236 ULONG FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector)
237 {
238 ULONG RootDirSectors;
239 ULONG DataSectorCount;
240 ULONG SectorsPerFat;
241 ULONG TotalSectors;
242 ULONG CountOfClusters;
243 PFAT32_BOOTSECTOR Fat32BootSector = (PFAT32_BOOTSECTOR)FatBootSector;
244
245 RootDirSectors = ((FatBootSector->RootDirEntries * 32) + (FatBootSector->BytesPerSector - 1)) / FatBootSector->BytesPerSector;
246 SectorsPerFat = FatBootSector->SectorsPerFat ? FatBootSector->SectorsPerFat : Fat32BootSector->SectorsPerFatBig;
247 TotalSectors = FatBootSector->TotalSectors ? FatBootSector->TotalSectors : FatBootSector->TotalSectorsBig;
248 DataSectorCount = TotalSectors - (FatBootSector->ReservedSectors + (FatBootSector->NumberOfFats * SectorsPerFat) + RootDirSectors);
249
250 //mjl
251 if (FatBootSector->SectorsPerCluster == 0)
252 CountOfClusters = 0;
253 else
254 CountOfClusters = DataSectorCount / FatBootSector->SectorsPerCluster;
255
256 if (CountOfClusters < 4085)
257 {
258 /* Volume is FAT12 */
259 return FAT12;
260 }
261 else if (CountOfClusters < 65525)
262 {
263 /* Volume is FAT16 */
264 return FAT16;
265 }
266 else
267 {
268 /* Volume is FAT32 */
269 return FAT32;
270 }
271 }
272
273 PVOID FatBufferDirectory(UINT32 DirectoryStartCluster, PUINT32 EntryCountPointer, BOOL RootDirectory)
274 {
275 UINT32 RootDirectoryStartSector;
276 UINT32 RootDirectorySectorCount;
277 PVOID DirectoryBuffer;
278 UINT32 DirectorySize;
279
280 DbgPrint((DPRINT_FILESYSTEM, "FatBufferDirectory() DirectoryStartCluster = %d RootDirectory = %s\n", DirectoryStartCluster, (RootDirectory ? "TRUE" : "FALSE")));
281
282 //
283 // Calculate the size of the directory
284 //
285 if ((RootDirectory) && (FatType != FAT32))
286 {
287 DirectorySize = ROUND_UP((FatVolumeBootSector->RootDirEntries * 32), FatVolumeBootSector->BytesPerSector);
288 }
289 else
290 {
291 if (RootDirectory)
292 {
293 DirectorySize = (FatCountClustersInChain(Fat32VolumeBootSector->RootDirStartCluster) * Fat32VolumeBootSector->SectorsPerCluster) * Fat32VolumeBootSector->BytesPerSector;
294 }
295 else
296 {
297 DirectorySize = (FatCountClustersInChain(DirectoryStartCluster) * FatVolumeBootSector->SectorsPerCluster) * FatVolumeBootSector->BytesPerSector;
298 }
299 }
300
301 //
302 // Attempt to allocate memory for directory buffer
303 //
304 DbgPrint((DPRINT_FILESYSTEM, "Trying to allocate (DirectorySize) %d bytes.\n", DirectorySize));
305 DirectoryBuffer = AllocateMemory(DirectorySize);
306
307 if (DirectoryBuffer == NULL)
308 {
309 return NULL;
310 }
311
312 //
313 // Now read directory contents into DirectoryBuffer
314 //
315 if (RootDirectory)
316 {
317 if (FatType == FAT32)
318 {
319 if (!FatReadClusterChain(Fat32VolumeBootSector->RootDirStartCluster, 0xFFFFFFFF, DirectoryBuffer))
320 {
321 FreeMemory(DirectoryBuffer);
322 return NULL;
323 }
324 }
325 else
326 {
327 //
328 // FAT type is not FAT32 so the root directory comes right after the fat table
329 //
330 RootDirectoryStartSector = FatVolumeBootSector->ReservedSectors + (FatVolumeBootSector->NumberOfFats * FatVolumeBootSector->SectorsPerFat);
331 RootDirectorySectorCount = (DirectorySize / FatVolumeBootSector->BytesPerSector);
332
333 if (!FatReadVolumeSectors(FatDriveNumber, RootDirectoryStartSector, RootDirectorySectorCount, DirectoryBuffer))
334 {
335 FreeMemory(DirectoryBuffer);
336 return NULL;
337 }
338 }
339 }
340 else
341 {
342 if (!FatReadClusterChain(DirectoryStartCluster, 0xFFFFFFFF, DirectoryBuffer))
343 {
344 FreeMemory(DirectoryBuffer);
345 return NULL;
346 }
347 }
348
349 *EntryCountPointer = (DirectorySize / 32);
350
351 return DirectoryBuffer;
352 }
353
354 BOOL FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer, UINT32 EntryCount, PUCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer)
355 {
356 ULONG CurrentEntry;
357 PDIRENTRY DirEntry;
358 PLFN_DIRENTRY LfnDirEntry;
359 UCHAR LfnNameBuffer[261];
360 UCHAR ShortNameBuffer[13];
361 UINT32 StartCluster;
362
363 DbgPrint((DPRINT_FILESYSTEM, "FatSearchDirectoryBufferForFile() DirectoryBuffer = 0x%x EntryCount = %d FileName = %s\n", DirectoryBuffer, EntryCount, FileName));
364
365 memset(ShortNameBuffer, 0, 13 * sizeof(UCHAR));
366 memset(LfnNameBuffer, 0, 261 * sizeof(UCHAR));
367
368 for (CurrentEntry=0; CurrentEntry<EntryCount; CurrentEntry++)
369 {
370 DirEntry = (PDIRENTRY)(DirectoryBuffer + (CurrentEntry * 32) );
371 LfnDirEntry = (PLFN_DIRENTRY)DirEntry;
372
373 //
374 // Check if this is the last file in the directory
375 // If DirEntry[0] == 0x00 then that means all the
376 // entries after this one are unused. If this is the
377 // last entry then we didn't find the file in this directory.
378 //
379 if (DirEntry->FileName[0] == 0x00)
380 {
381 return FALSE;
382 }
383
384 //
385 // Check if this is a deleted entry or not
386 //
387 if (DirEntry->FileName[0] == 0xE5)
388 {
389 memset(ShortNameBuffer, 0, 13 * sizeof(UCHAR));
390 memset(LfnNameBuffer, 0, 261 * sizeof(UCHAR));
391 continue;
392 }
393
394 //
395 // Check if this is a LFN entry
396 // If so it needs special handling
397 //
398 if (DirEntry->Attr == ATTR_LONG_NAME)
399 {
400 //
401 // Check to see if this is a deleted LFN entry, if so continue
402 //
403 if (LfnDirEntry->SequenceNumber & 0x80)
404 {
405 continue;
406 }
407
408 //
409 // Mask off high two bits of sequence number
410 // and make the sequence number zero-based
411 //
412 LfnDirEntry->SequenceNumber &= 0x3F;
413 LfnDirEntry->SequenceNumber--;
414
415 //
416 // Get all 13 LFN entry characters
417 //
418 if (LfnDirEntry->Name0_4[0] != 0xFFFF)
419 {
420 LfnNameBuffer[0 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name0_4[0];
421 }
422 if (LfnDirEntry->Name0_4[1] != 0xFFFF)
423 {
424 LfnNameBuffer[1 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name0_4[1];
425 }
426 if (LfnDirEntry->Name0_4[2] != 0xFFFF)
427 {
428 LfnNameBuffer[2 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name0_4[2];
429 }
430 if (LfnDirEntry->Name0_4[3] != 0xFFFF)
431 {
432 LfnNameBuffer[3 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name0_4[3];
433 }
434 if (LfnDirEntry->Name0_4[4] != 0xFFFF)
435 {
436 LfnNameBuffer[4 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name0_4[4];
437 }
438 if (LfnDirEntry->Name5_10[0] != 0xFFFF)
439 {
440 LfnNameBuffer[5 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[0];
441 }
442 if (LfnDirEntry->Name5_10[1] != 0xFFFF)
443 {
444 LfnNameBuffer[6 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[1];
445 }
446 if (LfnDirEntry->Name5_10[2] != 0xFFFF)
447 {
448 LfnNameBuffer[7 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[2];
449 }
450 if (LfnDirEntry->Name5_10[3] != 0xFFFF)
451 {
452 LfnNameBuffer[8 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[3];
453 }
454 if (LfnDirEntry->Name5_10[4] != 0xFFFF)
455 {
456 LfnNameBuffer[9 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[4];
457 }
458 if (LfnDirEntry->Name5_10[5] != 0xFFFF)
459 {
460 LfnNameBuffer[10 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name5_10[5];
461 }
462 if (LfnDirEntry->Name11_12[0] != 0xFFFF)
463 {
464 LfnNameBuffer[11 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name11_12[0];
465 }
466 if (LfnDirEntry->Name11_12[1] != 0xFFFF)
467 {
468 LfnNameBuffer[12 + (LfnDirEntry->SequenceNumber * 13)] = (UCHAR)LfnDirEntry->Name11_12[1];
469 }
470
471 continue;
472 }
473
474 //
475 // Check for the volume label attribute
476 // and skip over this entry if found
477 //
478 if (DirEntry->Attr & ATTR_VOLUMENAME)
479 {
480 memset(ShortNameBuffer, 0, 13 * sizeof(UCHAR));
481 memset(LfnNameBuffer, 0, 261 * sizeof(UCHAR));
482 continue;
483 }
484
485 //
486 // If we get here then we've found a short file name
487 // entry and LfnNameBuffer contains the long file
488 // name or zeroes. All we have to do now is see if the
489 // file name matches either the short or long file name
490 // and fill in the FAT_FILE_INFO structure if it does
491 // or zero our buffers and continue looking.
492 //
493
494 //
495 // Get short file name
496 //
497 FatParseShortFileName(ShortNameBuffer, DirEntry);
498
499 DbgPrint((DPRINT_FILESYSTEM, "Entry: %d LFN = %s\n", CurrentEntry, LfnNameBuffer));
500 DbgPrint((DPRINT_FILESYSTEM, "Entry: %d DOS name = %s\n", CurrentEntry, ShortNameBuffer));
501
502 //
503 // See if the file name matches either the short or long name
504 //
505 if (((strlen(FileName) == strlen(LfnNameBuffer)) && (stricmp(FileName, LfnNameBuffer) == 0)) ||
506 ((strlen(FileName) == strlen(ShortNameBuffer)) && (stricmp(FileName, ShortNameBuffer) == 0)))
507 {
508 //
509 // We found the entry, now fill in the FAT_FILE_INFO struct
510 //
511 FatFileInfoPointer->FileSize = DirEntry->Size;
512 FatFileInfoPointer->FilePointer = 0;
513
514 DbgPrint((DPRINT_FILESYSTEM, "MSDOS Directory Entry:\n"));
515 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]));
516 DbgPrint((DPRINT_FILESYSTEM, "Attr = 0x%x\n", DirEntry->Attr));
517 DbgPrint((DPRINT_FILESYSTEM, "ReservedNT = 0x%x\n", DirEntry->ReservedNT));
518 DbgPrint((DPRINT_FILESYSTEM, "TimeInTenths = %d\n", DirEntry->TimeInTenths));
519 DbgPrint((DPRINT_FILESYSTEM, "CreateTime = %d\n", DirEntry->CreateTime));
520 DbgPrint((DPRINT_FILESYSTEM, "CreateDate = %d\n", DirEntry->CreateDate));
521 DbgPrint((DPRINT_FILESYSTEM, "LastAccessDate = %d\n", DirEntry->LastAccessDate));
522 DbgPrint((DPRINT_FILESYSTEM, "ClusterHigh = 0x%x\n", DirEntry->ClusterHigh));
523 DbgPrint((DPRINT_FILESYSTEM, "Time = %d\n", DirEntry->Time));
524 DbgPrint((DPRINT_FILESYSTEM, "Date = %d\n", DirEntry->Date));
525 DbgPrint((DPRINT_FILESYSTEM, "ClusterLow = 0x%x\n", DirEntry->ClusterLow));
526 DbgPrint((DPRINT_FILESYSTEM, "Size = %d\n", DirEntry->Size));
527
528 //
529 // Get the cluster chain
530 //
531 StartCluster = ((UINT32)DirEntry->ClusterHigh << 16) + DirEntry->ClusterLow;
532 DbgPrint((DPRINT_FILESYSTEM, "StartCluster = 0x%x\n", StartCluster));
533 FatFileInfoPointer->FileFatChain = FatGetClusterChainArray(StartCluster);
534
535 //
536 // See if memory allocation failed
537 //
538 if (FatFileInfoPointer->FileFatChain == NULL)
539 {
540 return FALSE;
541 }
542
543 return TRUE;
544 }
545
546 //
547 // Nope, no match - zero buffers and continue looking
548 //
549 memset(ShortNameBuffer, 0, 13 * sizeof(UCHAR));
550 memset(LfnNameBuffer, 0, 261 * sizeof(UCHAR));
551 continue;
552 }
553
554 return FALSE;
555 }
556
557 /*
558 * FatLookupFile()
559 * This function searches the file system for the
560 * specified filename and fills in a FAT_STRUCT structure
561 * with info describing the file, etc. returns true
562 * if the file exists or false otherwise
563 */
564 BOOL FatLookupFile(PUCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer)
565 {
566 int i;
567 ULONG NumberOfPathParts;
568 UCHAR PathPart[261];
569 PVOID DirectoryBuffer;
570 UINT32 DirectoryStartCluster = 0;
571 ULONG DirectoryEntryCount;
572 FAT_FILE_INFO FatFileInfo;
573
574 DbgPrint((DPRINT_FILESYSTEM, "FatLookupFile() FileName = %s\n", FileName));
575
576 memset(FatFileInfoPointer, 0, sizeof(FAT_FILE_INFO));
577
578 //
579 // Check and see if the first character is '\' and remove it if so
580 //
581 while (*FileName == '\\')
582 {
583 FileName++;
584 }
585
586 //
587 // Figure out how many sub-directories we are nested in
588 //
589 NumberOfPathParts = FatGetNumPathParts(FileName);
590
591 //
592 // Loop once for each part
593 //
594 for (i=0; i<NumberOfPathParts; i++)
595 {
596 //
597 // Get first path part
598 //
599 FatGetFirstNameFromPath(PathPart, FileName);
600
601 //
602 // Advance to the next part of the path
603 //
604 for (; (*FileName != '\\') && (*FileName != '\0'); FileName++)
605 {
606 }
607 FileName++;
608
609 //
610 // Buffer the directory contents
611 //
612 DirectoryBuffer = FatBufferDirectory(DirectoryStartCluster, &DirectoryEntryCount, (i == 0) );
613 if (DirectoryBuffer == NULL)
614 {
615 return FALSE;
616 }
617
618 //
619 // Search for file name in directory
620 //
621 if (!FatSearchDirectoryBufferForFile(DirectoryBuffer, DirectoryEntryCount, PathPart, &FatFileInfo))
622 {
623 FreeMemory(DirectoryBuffer);
624 return FALSE;
625 }
626
627 FreeMemory(DirectoryBuffer);
628
629 //
630 // If we have another sub-directory to go then
631 // grab the start cluster and free the fat chain array
632 //
633 if ((i+1) < NumberOfPathParts)
634 {
635 DirectoryStartCluster = FatFileInfo.FileFatChain[0];
636 FreeMemory(FatFileInfo.FileFatChain);
637 }
638 }
639
640 memcpy(FatFileInfoPointer, &FatFileInfo, sizeof(FAT_FILE_INFO));
641
642 return TRUE;
643 }
644
645 /*
646 * FatGetNumPathParts()
647 * This function parses a path in the form of dir1\dir2\file1.ext
648 * and returns the number of parts it has (i.e. 3 - dir1,dir2,file1.ext)
649 */
650 ULONG FatGetNumPathParts(PUCHAR Path)
651 {
652 ULONG i;
653 ULONG num;
654
655 for (i=0,num=0; i<(int)strlen(Path); i++)
656 {
657 if (Path[i] == '\\')
658 {
659 num++;
660 }
661 }
662 num++;
663
664 DbgPrint((DPRINT_FILESYSTEM, "FatGetNumPathParts() Path = %s NumPathParts = %d\n", Path, num));
665
666 return num;
667 }
668
669 /*
670 * FatGetFirstNameFromPath()
671 * This function parses a path in the form of dir1\dir2\file1.ext
672 * and puts the first name of the path (e.g. "dir1") in buffer
673 * compatible with the MSDOS directory structure
674 */
675 VOID FatGetFirstNameFromPath(PUCHAR Buffer, PUCHAR Path)
676 {
677 ULONG i;
678
679 // Copy all the characters up to the end of the
680 // string or until we hit a '\' character
681 // and put them in Buffer
682 for (i=0; i<(int)strlen(Path); i++)
683 {
684 if (Path[i] == '\\')
685 {
686 break;
687 }
688 else
689 {
690 Buffer[i] = Path[i];
691 }
692 }
693
694 Buffer[i] = 0;
695
696 DbgPrint((DPRINT_FILESYSTEM, "FatGetFirstNameFromPath() Path = %s FirstName = %s\n", Path, Buffer));
697 }
698
699 /*
700 * FatParseFileName()
701 * This function parses a directory entry name which
702 * is in the form of "FILE EXT" and puts it in Buffer
703 * in the form of "file.ext"
704 */
705 void FatParseShortFileName(PUCHAR Buffer, PDIRENTRY DirEntry)
706 {
707 ULONG Idx;
708
709 Idx = 0;
710
711 //
712 // Fixup first character
713 //
714 if (DirEntry->FileName[0] == 0x05)
715 {
716 DirEntry->FileName[0] = 0xE5;
717 }
718
719 //
720 // Get the file name
721 //
722 while (Idx < 8)
723 {
724 if (DirEntry->FileName[Idx] == ' ')
725 {
726 break;
727 }
728
729 Buffer[Idx] = DirEntry->FileName[Idx];
730 Idx++;
731 }
732
733 //
734 // Get extension
735 //
736 if ((DirEntry->FileName[8] != ' '))
737 {
738 Buffer[Idx++] = '.';
739 Buffer[Idx++] = (DirEntry->FileName[8] == ' ') ? '\0' : DirEntry->FileName[8];
740 Buffer[Idx++] = (DirEntry->FileName[9] == ' ') ? '\0' : DirEntry->FileName[9];
741 Buffer[Idx++] = (DirEntry->FileName[10] == ' ') ? '\0' : DirEntry->FileName[10];
742 }
743
744 //
745 // Null-Terminate string
746 //
747 Buffer[Idx + 4] = '\0';
748
749 DbgPrint((DPRINT_FILESYSTEM, "FatParseShortFileName() ShortName = %s\n", Buffer));
750 }
751
752 /*
753 * FatGetFatEntry()
754 * returns the Fat entry for a given cluster number
755 */
756 BOOL FatGetFatEntry(UINT32 Cluster, PUINT32 ClusterPointer)
757 {
758 DWORD fat = 0;
759 int FatOffset;
760 int ThisFatSecNum;
761 int ThisFatEntOffset;
762
763 DbgPrint((DPRINT_FILESYSTEM, "FatGetFatEntry() Retrieving FAT entry for cluster %d.\n", Cluster));
764
765 switch(FatType)
766 {
767 case FAT12:
768
769 FatOffset = Cluster + (Cluster / 2);
770 ThisFatSecNum = FatVolumeBootSector->ReservedSectors + (FatOffset / FatVolumeBootSector->BytesPerSector);
771 ThisFatEntOffset = (FatOffset % FatVolumeBootSector->BytesPerSector);
772
773 DbgPrint((DPRINT_FILESYSTEM, "FatOffset: %d\n", FatOffset));
774 DbgPrint((DPRINT_FILESYSTEM, "ThisFatSecNum: %d\n", ThisFatSecNum));
775 DbgPrint((DPRINT_FILESYSTEM, "ThisFatEntOffset: %d\n", ThisFatEntOffset));
776
777 if (ThisFatEntOffset == (FatVolumeBootSector->BytesPerSector - 1))
778 {
779 if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 2, (PVOID)FILESYSBUFFER))
780 {
781 return FALSE;
782 }
783 }
784 else
785 {
786 if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
787 {
788 return FALSE;
789 }
790 }
791
792 fat = *((WORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset));
793 if (Cluster & 0x0001)
794 fat = fat >> 4; /* Cluster number is ODD */
795 else
796 fat = fat & 0x0FFF; /* Cluster number is EVEN */
797
798 break;
799
800 case FAT16:
801
802 FatOffset = (Cluster * 2);
803 ThisFatSecNum = FatVolumeBootSector->ReservedSectors + (FatOffset / FatVolumeBootSector->BytesPerSector);
804 ThisFatEntOffset = (FatOffset % FatVolumeBootSector->BytesPerSector);
805
806 if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
807 {
808 return FALSE;
809 }
810
811 fat = *((WORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset));
812
813 break;
814
815 case FAT32:
816
817 FatOffset = (Cluster * 4);
818 ThisFatSecNum = (Fat32VolumeBootSector->ExtendedFlags & 0x80) ? ((Fat32VolumeBootSector->ExtendedFlags & 0x0f) * Fat32VolumeBootSector->SectorsPerFatBig) : 0; // Get the active fat sector offset
819 ThisFatSecNum += FatVolumeBootSector->ReservedSectors + (FatOffset / FatVolumeBootSector->BytesPerSector);
820 ThisFatEntOffset = (FatOffset % FatVolumeBootSector->BytesPerSector);
821
822 if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
823 {
824 return FALSE;
825 }
826
827 // Get the fat entry
828 fat = (*((DWORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset))) & 0x0FFFFFFF;
829
830 break;
831
832 }
833
834 DbgPrint((DPRINT_FILESYSTEM, "FAT entry is 0x%x.\n", fat));
835
836 *ClusterPointer = fat;
837
838 return TRUE;
839 }
840
841 /*
842 * FatOpenFile()
843 * Tries to open the file 'name' and returns true or false
844 * for success and failure respectively
845 */
846 FILE* FatOpenFile(PUCHAR FileName)
847 {
848 FAT_FILE_INFO TempFatFileInfo;
849 PFAT_FILE_INFO FileHandle;
850
851 DbgPrint((DPRINT_FILESYSTEM, "FatOpenFile() FileName = %s\n", FileName));
852
853 if (!FatLookupFile(FileName, &TempFatFileInfo))
854 {
855 return NULL;
856 }
857
858 FileHandle = AllocateMemory(sizeof(FAT_FILE_INFO));
859
860 if (FileHandle == NULL)
861 {
862 return NULL;
863 }
864
865 memcpy(FileHandle, &TempFatFileInfo, sizeof(FAT_FILE_INFO));
866
867 return (FILE*)FileHandle;
868 }
869
870 UINT32 FatCountClustersInChain(UINT32 StartCluster)
871 {
872 UINT32 ClusterCount = 0;
873
874 DbgPrint((DPRINT_FILESYSTEM, "FatCountClustersInChain() StartCluster = %d\n", StartCluster));
875
876 while (1)
877 {
878 //
879 // If end of chain then break out of our cluster counting loop
880 //
881 if (((FatType == FAT12) && (StartCluster >= 0xff8)) ||
882 ((FatType == FAT16) && (StartCluster >= 0xfff8)) ||
883 ((FatType == FAT32) && (StartCluster >= 0x0ffffff8)))
884 {
885 break;
886 }
887
888 //
889 // Increment count
890 //
891 ClusterCount++;
892
893 //
894 // Get next cluster
895 //
896 if (!FatGetFatEntry(StartCluster, &StartCluster))
897 {
898 return 0;
899 }
900 }
901
902 DbgPrint((DPRINT_FILESYSTEM, "FatCountClustersInChain() ClusterCount = %d\n", ClusterCount));
903
904 return ClusterCount;
905 }
906
907 PUINT32 FatGetClusterChainArray(UINT32 StartCluster)
908 {
909 UINT32 ClusterCount;
910 ULONG ArraySize;
911 PUINT32 ArrayPointer;
912 ULONG Idx;
913
914 DbgPrint((DPRINT_FILESYSTEM, "FatGetClusterChainArray() StartCluster = %d\n", StartCluster));
915
916 ClusterCount = FatCountClustersInChain(StartCluster) + 1; // Lets get the 0x0ffffff8 on the end of the array
917 ArraySize = ClusterCount * sizeof(UINT32);
918
919 //
920 // Allocate array memory
921 //
922 ArrayPointer = AllocateMemory(ArraySize);
923
924 if (ArrayPointer == NULL)
925 {
926 return NULL;
927 }
928
929 //
930 // Loop through and set array values
931 //
932 for (Idx=0; Idx<ClusterCount; Idx++)
933 {
934 //
935 // Set current cluster
936 //
937 ArrayPointer[Idx] = StartCluster;
938
939 //
940 // Don't try to get next cluster for last cluster
941 //
942 if (((FatType == FAT12) && (StartCluster >= 0xff8)) ||
943 ((FatType == FAT16) && (StartCluster >= 0xfff8)) ||
944 ((FatType == FAT32) && (StartCluster >= 0x0ffffff8)))
945 {
946 Idx++;
947 break;
948 }
949
950 //
951 // Get next cluster
952 //
953 if (!FatGetFatEntry(StartCluster, &StartCluster))
954 {
955 FreeMemory(ArrayPointer);
956 return NULL;
957 }
958 }
959
960 return ArrayPointer;
961 }
962
963 /*
964 * FatReadCluster()
965 * Reads the specified cluster into memory
966 * and returns the number of bytes read
967 */
968 BOOL FatReadCluster(ULONG ClusterNumber, PVOID Buffer)
969 {
970 ULONG ClusterStartSector;
971
972 ClusterStartSector = ((ClusterNumber - 2) * FatVolumeBootSector->SectorsPerCluster) + DataSectorStart;
973
974 DbgPrint((DPRINT_FILESYSTEM, "FatReadCluster() ClusterNumber = %d Buffer = 0x%x ClusterStartSector = %d\n", ClusterNumber, Buffer, ClusterStartSector));
975
976 if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
977 {
978 return FALSE;
979 }
980
981 memcpy(Buffer, (PVOID)FILESYSBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
982
983 return TRUE;
984 }
985
986 /*
987 * FatReadClusterChain()
988 * Reads the specified clusters into memory
989 */
990 BOOL FatReadClusterChain(ULONG StartClusterNumber, ULONG NumberOfClusters, PVOID Buffer)
991 {
992 ULONG ClusterStartSector;
993
994 DbgPrint((DPRINT_FILESYSTEM, "FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber, NumberOfClusters, Buffer));
995
996 while (NumberOfClusters > 0)
997 {
998
999 DbgPrint((DPRINT_FILESYSTEM, "FatReadClusterChain() StartClusterNumber = %d NumberOfClusters = %d Buffer = 0x%x\n", StartClusterNumber, NumberOfClusters, Buffer));
1000 //
1001 // Calculate starting sector for cluster
1002 //
1003 ClusterStartSector = ((StartClusterNumber - 2) * FatVolumeBootSector->SectorsPerCluster) + DataSectorStart;
1004
1005 //
1006 // Read cluster into memory
1007 //
1008 if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
1009 {
1010 return FALSE;
1011 }
1012
1013 memcpy(Buffer, (PVOID)FILESYSBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
1014
1015 //
1016 // Decrement count of clusters left to read
1017 //
1018 NumberOfClusters--;
1019
1020 //
1021 // Increment buffer address by cluster size
1022 //
1023 Buffer += (FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
1024
1025 //
1026 // Get next cluster
1027 //
1028 if (!FatGetFatEntry(StartClusterNumber, &StartClusterNumber))
1029 {
1030 return FALSE;
1031 }
1032
1033 //
1034 // If end of chain then break out of our cluster reading loop
1035 //
1036 if (((FatType == FAT12) && (StartClusterNumber >= 0xff8)) ||
1037 ((FatType == FAT16) && (StartClusterNumber >= 0xfff8)) ||
1038 ((FatType == FAT32) && (StartClusterNumber >= 0x0ffffff8)))
1039 {
1040 break;
1041 }
1042 }
1043
1044 return TRUE;
1045 }
1046
1047 /*
1048 * FatReadPartialCluster()
1049 * Reads part of a cluster into memory
1050 */
1051 BOOL FatReadPartialCluster(ULONG ClusterNumber, ULONG StartingOffset, ULONG Length, PVOID Buffer)
1052 {
1053 ULONG ClusterStartSector;
1054
1055 DbgPrint((DPRINT_FILESYSTEM, "FatReadPartialCluster() ClusterNumber = %d StartingOffset = %d Length = %d Buffer = 0x%x\n", ClusterNumber, StartingOffset, Length, Buffer));
1056
1057 ClusterStartSector = ((ClusterNumber - 2) * FatVolumeBootSector->SectorsPerCluster) + DataSectorStart;
1058
1059 if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
1060 {
1061 return FALSE;
1062 }
1063
1064 memcpy(Buffer, ((PVOID)FILESYSBUFFER + StartingOffset), Length);
1065
1066 return TRUE;
1067 }
1068
1069 /*
1070 * FatReadFile()
1071 * Reads BytesToRead from open file and
1072 * returns the number of bytes read in BytesRead
1073 */
1074 BOOL FatReadFile(FILE *FileHandle, ULONG BytesToRead, PULONG BytesRead, PVOID Buffer)
1075 {
1076 PFAT_FILE_INFO FatFileInfo = (PFAT_FILE_INFO)FileHandle;
1077 UINT32 ClusterNumber;
1078 UINT32 OffsetInCluster;
1079 UINT32 LengthInCluster;
1080 UINT32 NumberOfClusters;
1081 UINT32 BytesPerCluster;
1082
1083 DbgPrint((DPRINT_FILESYSTEM, "FatReadFile() BytesToRead = %d Buffer = 0x%x\n", BytesToRead, Buffer));
1084
1085 if (BytesRead != NULL)
1086 {
1087 *BytesRead = 0;
1088 }
1089
1090 //
1091 // If they are trying to read past the
1092 // end of the file then return success
1093 // with BytesRead == 0
1094 //
1095 if (FatFileInfo->FilePointer >= FatFileInfo->FileSize)
1096 {
1097 return TRUE;
1098 }
1099
1100 //
1101 // If they are trying to read more than there is to read
1102 // then adjust the amount to read
1103 //
1104 if ((FatFileInfo->FilePointer + BytesToRead) > FatFileInfo->FileSize)
1105 {
1106 BytesToRead = (FatFileInfo->FileSize - FatFileInfo->FilePointer);
1107 }
1108
1109 //
1110 // Ok, now we have to perform at most 3 calculations
1111 // I'll draw you a picture (using nifty ASCII art):
1112 //
1113 // CurrentFilePointer -+
1114 // |
1115 // +----------------+
1116 // |
1117 // +-----------+-----------+-----------+-----------+
1118 // | Cluster 1 | Cluster 2 | Cluster 3 | Cluster 4 |
1119 // +-----------+-----------+-----------+-----------+
1120 // | |
1121 // +---------------+--------------------+
1122 // |
1123 // BytesToRead -------+
1124 //
1125 // 1 - The first calculation (and read) will align
1126 // the file pointer with the next cluster.
1127 // boundary (if we are supposed to read that much)
1128 // 2 - The next calculation (and read) will read
1129 // in all the full clusters that the requested
1130 // amount of data would cover (in this case
1131 // clusters 2 & 3).
1132 // 3 - The last calculation (and read) would read
1133 // in the remainder of the data requested out of
1134 // the last cluster.
1135 //
1136
1137 BytesPerCluster = (FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
1138
1139 //
1140 // Only do the first read if we
1141 // aren't aligned on a cluster boundary
1142 //
1143 if (FatFileInfo->FilePointer % BytesPerCluster)
1144 {
1145 //
1146 // Do the math for our first read
1147 //
1148 ClusterNumber = (FatFileInfo->FilePointer / BytesPerCluster);
1149 ClusterNumber = FatFileInfo->FileFatChain[ClusterNumber];
1150 OffsetInCluster = (FatFileInfo->FilePointer % BytesPerCluster);
1151 LengthInCluster = (BytesToRead > (BytesPerCluster - OffsetInCluster)) ? (BytesPerCluster - OffsetInCluster) : BytesToRead;
1152
1153 //
1154 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1155 //
1156 if (!FatReadPartialCluster(ClusterNumber, OffsetInCluster, LengthInCluster, Buffer))
1157 {
1158 return FALSE;
1159 }
1160 if (BytesRead != NULL)
1161 {
1162 *BytesRead += LengthInCluster;
1163 }
1164 BytesToRead -= LengthInCluster;
1165 FatFileInfo->FilePointer += LengthInCluster;
1166 Buffer += LengthInCluster;
1167 }
1168
1169 //
1170 // Do the math for our second read (if any data left)
1171 //
1172 if (BytesToRead > 0)
1173 {
1174 //
1175 // Determine how many full clusters we need to read
1176 //
1177 NumberOfClusters = (BytesToRead / BytesPerCluster);
1178
1179 if (NumberOfClusters > 0)
1180 {
1181 ClusterNumber = (FatFileInfo->FilePointer / BytesPerCluster);
1182 ClusterNumber = FatFileInfo->FileFatChain[ClusterNumber];
1183
1184 //
1185 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1186 //
1187 if (!FatReadClusterChain(ClusterNumber, NumberOfClusters, Buffer))
1188 {
1189 return FALSE;
1190 }
1191 if (BytesRead != NULL)
1192 {
1193 *BytesRead += (NumberOfClusters * BytesPerCluster);
1194 }
1195 BytesToRead -= (NumberOfClusters * BytesPerCluster);
1196 FatFileInfo->FilePointer += (NumberOfClusters * BytesPerCluster);
1197 Buffer += (NumberOfClusters * BytesPerCluster);
1198 }
1199 }
1200
1201 //
1202 // Do the math for our third read (if any data left)
1203 //
1204 if (BytesToRead > 0)
1205 {
1206 ClusterNumber = (FatFileInfo->FilePointer / BytesPerCluster);
1207 ClusterNumber = FatFileInfo->FileFatChain[ClusterNumber];
1208
1209 //
1210 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
1211 //
1212 if (!FatReadPartialCluster(ClusterNumber, 0, BytesToRead, Buffer))
1213 {
1214 return FALSE;
1215 }
1216 if (BytesRead != NULL)
1217 {
1218 *BytesRead += BytesToRead;
1219 }
1220 BytesToRead -= BytesToRead;
1221 FatFileInfo->FilePointer += BytesToRead;
1222 Buffer += BytesToRead;
1223 }
1224
1225 return TRUE;
1226 }
1227
1228 ULONG FatGetFileSize(FILE *FileHandle)
1229 {
1230 PFAT_FILE_INFO FatFileHandle = (PFAT_FILE_INFO)FileHandle;
1231
1232 DbgPrint((DPRINT_FILESYSTEM, "FatGetFileSize() FileSize = %d\n", FatFileHandle->FileSize));
1233
1234 return FatFileHandle->FileSize;
1235 }
1236
1237 VOID FatSetFilePointer(FILE *FileHandle, ULONG NewFilePointer)
1238 {
1239 PFAT_FILE_INFO FatFileHandle = (PFAT_FILE_INFO)FileHandle;
1240
1241 DbgPrint((DPRINT_FILESYSTEM, "FatSetFilePointer() NewFilePointer = %d\n", NewFilePointer));
1242
1243 FatFileHandle->FilePointer = NewFilePointer;
1244 }
1245
1246 ULONG FatGetFilePointer(FILE *FileHandle)
1247 {
1248 PFAT_FILE_INFO FatFileHandle = (PFAT_FILE_INFO)FileHandle;
1249
1250 DbgPrint((DPRINT_FILESYSTEM, "FatGetFilePointer() FilePointer = %d\n", FatFileHandle->FilePointer));
1251
1252 return FatFileHandle->FilePointer;
1253 }
1254
1255 BOOL FatReadVolumeSectors(ULONG DriveNumber, ULONG SectorNumber, ULONG SectorCount, PVOID Buffer)
1256 {
1257 //return DiskReadMultipleLogicalSectors(DriveNumber, SectorNumber + FatVolumeBootSector->HiddenSectors, SectorCount, Buffer);
1258 return CacheReadDiskSectors(DriveNumber, SectorNumber + FatVolumeBootSector->HiddenSectors, SectorCount, Buffer);
1259 }