Author: hpoussin
Date: Sun Sep 6 23:33:27 2009
New Revision: 43008
URL:
http://svn.reactos.org/svn/reactos?rev=43008&view=rev
Log:
Revert r43007 to try to please buildbot
Modified:
trunk/reactos/boot/freeldr/freeldr/fs/fat.c
trunk/reactos/boot/freeldr/freeldr/include/fs/fat.h
Modified: trunk/reactos/boot/freeldr/freeldr/fs/fat.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/boot/freeldr/freeldr/fs/fa…
==============================================================================
--- trunk/reactos/boot/freeldr/freeldr/fs/fat.c [iso-8859-1] (original)
+++ trunk/reactos/boot/freeldr/freeldr/fs/fat.c [iso-8859-1] Sun Sep 6 23:33:27 2009
@@ -23,34 +23,33 @@
#include <debug.h>
ULONG FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector, ULONG PartitionSectorCount);
-PVOID FatBufferDirectory(PFAT_VOLUME_INFO Volume, ULONG DirectoryStartCluster, ULONG*
EntryCountPointer, BOOLEAN RootDirectory);
-BOOLEAN FatSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume, PVOID DirectoryBuffer,
ULONG EntryCount, PCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer);
-LONG FatLookupFile(PFAT_VOLUME_INFO Volume, PCSTR FileName, ULONG DeviceId,
PFAT_FILE_INFO FatFileInfoPointer);
+PVOID FatBufferDirectory(ULONG DirectoryStartCluster, ULONG* EntryCountPointer, BOOLEAN
RootDirectory);
+BOOLEAN FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer, ULONG EntryCount, PCHAR
FileName, PFAT_FILE_INFO FatFileInfoPointer);
+LONG FatLookupFile(PCSTR FileName, ULONG DeviceId, PFAT_FILE_INFO FatFileInfoPointer);
void FatParseShortFileName(PCHAR Buffer, PDIRENTRY DirEntry);
-BOOLEAN FatGetFatEntry(PFAT_VOLUME_INFO Volume, ULONG Cluster, ULONG* ClusterPointer);
-ULONG FatCountClustersInChain(PFAT_VOLUME_INFO Volume, ULONG StartCluster);
-ULONG* FatGetClusterChainArray(PFAT_VOLUME_INFO Volume, ULONG StartCluster);
-BOOLEAN FatReadClusterChain(PFAT_VOLUME_INFO Volume, ULONG StartClusterNumber, ULONG
NumberOfClusters, PVOID Buffer);
-BOOLEAN FatReadPartialCluster(PFAT_VOLUME_INFO Volume, ULONG ClusterNumber, ULONG
StartingOffset, ULONG Length, PVOID Buffer);
-BOOLEAN FatReadVolumeSectors(PFAT_VOLUME_INFO Volume, ULONG SectorNumber, ULONG
SectorCount, PVOID Buffer);
-
-typedef struct _FAT_VOLUME_INFO
-{
- ULONG BytesPerSector; /* Number of bytes per sector */
- ULONG SectorsPerCluster; /* Number of sectors per cluster */
- ULONG FatSectorStart; /* Starting sector of 1st FAT table */
- ULONG ActiveFatSectorStart; /* Starting sector of active FAT table */
- ULONG NumberOfFats; /* Number of FAT tables */
- ULONG SectorsPerFat; /* Sectors per FAT table */
- ULONG RootDirSectorStart; /* Starting sector of the root directory (non-fat32) */
- ULONG RootDirSectors; /* Number of sectors of the root directory (non-fat32) */
- ULONG RootDirStartCluster; /* Starting cluster number of the root directory (fat32 only)
*/
- ULONG DataSectorStart; /* Starting sector of the data area */
- ULONG FatType; /* FAT12, FAT16, FAT32, FATX16 or FATX32 */
- ULONG DeviceId;
-} FAT_VOLUME_INFO;
-
-static PFAT_VOLUME_INFO FatVolume = NULL;
+BOOLEAN FatGetFatEntry(ULONG Cluster, ULONG* ClusterPointer);
+ULONG FatCountClustersInChain(ULONG StartCluster);
+ULONG* FatGetClusterChainArray(ULONG StartCluster);
+BOOLEAN FatReadCluster(ULONG ClusterNumber, PVOID Buffer);
+BOOLEAN FatReadClusterChain(ULONG StartClusterNumber, ULONG NumberOfClusters, PVOID
Buffer);
+BOOLEAN FatReadPartialCluster(ULONG ClusterNumber, ULONG StartingOffset, ULONG Length,
PVOID Buffer);
+BOOLEAN FatReadFile(PFAT_FILE_INFO FatFileInfo, ULONG BytesToRead, ULONG* BytesRead,
PVOID Buffer);
+BOOLEAN FatReadVolumeSectors(ULONG DriveNumber, ULONG SectorNumber, ULONG SectorCount,
PVOID Buffer);
+
+ULONG BytesPerSector; /* Number of bytes per sector */
+ULONG SectorsPerCluster; /* Number of sectors per cluster */
+ULONG FatVolumeStartSector; /* Absolute starting sector of the partition */
+ULONG FatSectorStart; /* Starting sector of 1st FAT table */
+ULONG ActiveFatSectorStart; /* Starting sector of active FAT table */
+ULONG NumberOfFats; /* Number of FAT tables */
+ULONG SectorsPerFat; /* Sectors per FAT table */
+ULONG RootDirSectorStart; /* Starting sector of the root directory (non-fat32) */
+ULONG RootDirSectors; /* Number of sectors of the root directory (non-fat32) */
+ULONG RootDirStartCluster; /* Starting cluster number of the root directory (fat32
only) */
+ULONG DataSectorStart; /* Starting sector of the data area */
+
+ULONG FatType = 0; /* FAT12, FAT16, FAT32, FATX16 or FATX32 */
+ULONG FatDriveNumber = 0;
VOID FatSwapFatBootSector(PFAT_BOOTSECTOR Obj)
{
@@ -130,7 +129,7 @@
SW(Obj, LastAccessDate);
}
-BOOLEAN FatOpenVolume(PFAT_VOLUME_INFO Volume, PFAT_BOOTSECTOR BootSector, ULONGLONG
PartitionSectorCount)
+BOOLEAN FatOpenVolume(UCHAR DriveNumber, ULONGLONG VolumeStartSector, ULONGLONG
PartitionSectorCount)
{
char ErrMsg[80];
ULONG FatSize;
@@ -138,21 +137,42 @@
PFAT32_BOOTSECTOR Fat32VolumeBootSector;
PFATX_BOOTSECTOR FatXVolumeBootSector;
- DPRINTM(DPRINT_FILESYSTEM, "FatOpenVolume() DeviceId = %d\n",
Volume->DeviceId);
+ DPRINTM(DPRINT_FILESYSTEM, "FatOpenVolume() DriveNumber = 0x%x VolumeStartSector =
%d\n", DriveNumber, VolumeStartSector);
+
+ // Store the drive number
+ FatDriveNumber = DriveNumber;
//
// Allocate the memory to hold the boot sector
//
- FatVolumeBootSector = (PFAT_BOOTSECTOR)BootSector;
- Fat32VolumeBootSector = (PFAT32_BOOTSECTOR)BootSector;
- FatXVolumeBootSector = (PFATX_BOOTSECTOR)BootSector;
+ FatVolumeBootSector = (PFAT_BOOTSECTOR) MmHeapAlloc(512);
+ Fat32VolumeBootSector = (PFAT32_BOOTSECTOR) FatVolumeBootSector;
+ FatXVolumeBootSector = (PFATX_BOOTSECTOR) FatVolumeBootSector;
+
+ //
+ // Make sure we got the memory
+ //
+ if (FatVolumeBootSector == NULL)
+ {
+ FileSystemError("Out of memory.");
+ return FALSE;
+ }
+
+ // Now try to read the boot sector
+ // If this fails then abort
+ if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1,
(PVOID)DISKREADBUFFER))
+ {
+ MmHeapFree(FatVolumeBootSector);
+ return FALSE;
+ }
+ RtlCopyMemory(FatVolumeBootSector, (PVOID)DISKREADBUFFER, 512);
// Get the FAT type
- Volume->FatType = FatDetermineFatType(FatVolumeBootSector, PartitionSectorCount);
+ FatType = FatDetermineFatType(FatVolumeBootSector, PartitionSectorCount);
// Dump boot sector (and swap it for big endian systems)
DPRINTM(DPRINT_FILESYSTEM, "Dumping boot sector:\n");
- if (ISFATX(Volume->FatType))
+ if (ISFATX(FatType))
{
FatSwapFatXBootSector(FatXVolumeBootSector);
DPRINTM(DPRINT_FILESYSTEM, "sizeof(FATX_BOOTSECTOR) = 0x%x.\n",
sizeof(FATX_BOOTSECTOR));
@@ -163,10 +183,10 @@
DPRINTM(DPRINT_FILESYSTEM, "NumberOfFats: %d\n",
FatXVolumeBootSector->NumberOfFats);
DPRINTM(DPRINT_FILESYSTEM, "Unknown: 0x%x\n",
FatXVolumeBootSector->Unknown);
- DPRINTM(DPRINT_FILESYSTEM, "FatType %s\n", Volume->FatType == FATX16 ?
"FATX16" : "FATX32");
-
- }
- else if (Volume->FatType == FAT32)
+ DPRINTM(DPRINT_FILESYSTEM, "FatType %s\n", FatType == FATX16 ?
"FATX16" : "FATX32");
+
+ }
+ else if (FatType == FAT32)
{
FatSwapFat32BootSector(Fat32VolumeBootSector);
DPRINTM(DPRINT_FILESYSTEM, "sizeof(FAT32_BOOTSECTOR) = 0x%x.\n",
sizeof(FAT32_BOOTSECTOR));
@@ -229,13 +249,19 @@
}
//
+ // Set the correct partition offset
+ //
+ FatVolumeStartSector = VolumeStartSector;
+
+ //
// Check the boot sector magic
//
- if (! ISFATX(Volume->FatType) && FatVolumeBootSector->BootSectorMagic !=
0xaa55)
- {
- sprintf(ErrMsg, "Invalid boot sector magic (expected 0xaa55 found 0x%x)",
- FatVolumeBootSector->BootSectorMagic);
+ if (! ISFATX(FatType) && FatVolumeBootSector->BootSectorMagic != 0xaa55)
+ {
+ sprintf(ErrMsg, "Invalid boot sector magic on drive 0x%x (expected 0xaa55 found
0x%x)",
+ DriveNumber, FatVolumeBootSector->BootSectorMagic);
FileSystemError(ErrMsg);
+ MmHeapFree(FatVolumeBootSector);
return FALSE;
}
@@ -243,60 +269,72 @@
// Check the FAT cluster size
// We do not support clusters bigger than 64k
//
- if ((ISFATX(Volume->FatType) && 64 * 1024 <
FatXVolumeBootSector->SectorsPerCluster * 512) ||
- (! ISFATX(Volume->FatType) && 64 * 1024 <
FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector))
+ if ((ISFATX(FatType) && 64 * 1024 <
FatXVolumeBootSector->SectorsPerCluster * 512) ||
+ (! ISFATX(FatType) && 64 * 1024 <
FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector))
{
FileSystemError("This file system has cluster sizes bigger than 64k.\nFreeLoader
does not support this.");
+ MmHeapFree(FatVolumeBootSector);
return FALSE;
}
+
+ //
+ // Clear our variables
+ //
+ FatSectorStart = 0;
+ ActiveFatSectorStart = 0;
+ NumberOfFats = 0;
+ RootDirSectorStart = 0;
+ DataSectorStart = 0;
+ SectorsPerFat = 0;
+ RootDirSectors = 0;
//
// Get the sectors per FAT,
// root directory starting sector,
// and data sector start
//
- if (ISFATX(Volume->FatType))
- {
- Volume->BytesPerSector = 512;
- Volume->SectorsPerCluster = SWAPD(FatXVolumeBootSector->SectorsPerCluster);
- Volume->FatSectorStart = (4096 / Volume->BytesPerSector);
- Volume->ActiveFatSectorStart = Volume->FatSectorStart;
- Volume->NumberOfFats = 1;
- FatSize = PartitionSectorCount / Volume->SectorsPerCluster *
- (Volume->FatType == FATX16 ? 2 : 4);
- Volume->SectorsPerFat = (((FatSize + 4095) / 4096) * 4096) /
Volume->BytesPerSector;
-
- Volume->RootDirSectorStart = Volume->FatSectorStart + Volume->NumberOfFats *
Volume->SectorsPerFat;
- Volume->RootDirSectors = FatXVolumeBootSector->SectorsPerCluster;
-
- Volume->DataSectorStart = Volume->RootDirSectorStart +
Volume->RootDirSectors;
- }
- else if (Volume->FatType != FAT32)
- {
- Volume->BytesPerSector = FatVolumeBootSector->BytesPerSector;
- Volume->SectorsPerCluster = FatVolumeBootSector->SectorsPerCluster;
- Volume->FatSectorStart = FatVolumeBootSector->ReservedSectors;
- Volume->ActiveFatSectorStart = Volume->FatSectorStart;
- Volume->NumberOfFats = FatVolumeBootSector->NumberOfFats;
- Volume->SectorsPerFat = FatVolumeBootSector->SectorsPerFat;
-
- Volume->RootDirSectorStart = Volume->FatSectorStart + Volume->NumberOfFats *
Volume->SectorsPerFat;
- Volume->RootDirSectors = ((FatVolumeBootSector->RootDirEntries * 32) +
(Volume->BytesPerSector - 1)) / Volume->BytesPerSector;
-
- Volume->DataSectorStart = Volume->RootDirSectorStart +
Volume->RootDirSectors;
+ if (ISFATX(FatType))
+ {
+ BytesPerSector = 512;
+ SectorsPerCluster = SWAPD(FatXVolumeBootSector->SectorsPerCluster);
+ FatSectorStart = (4096 / BytesPerSector);
+ ActiveFatSectorStart = FatSectorStart;
+ NumberOfFats = 1;
+ FatSize = PartitionSectorCount / SectorsPerCluster *
+ (FATX16 == FatType ? 2 : 4);
+ SectorsPerFat = (((FatSize + 4095) / 4096) * 4096) / BytesPerSector;
+
+ RootDirSectorStart = FatSectorStart + NumberOfFats * SectorsPerFat;
+ RootDirSectors = FatXVolumeBootSector->SectorsPerCluster;
+
+ DataSectorStart = RootDirSectorStart + RootDirSectors;
+ }
+ else if (FatType != FAT32)
+ {
+ BytesPerSector = FatVolumeBootSector->BytesPerSector;
+ SectorsPerCluster = FatVolumeBootSector->SectorsPerCluster;
+ FatSectorStart = FatVolumeBootSector->ReservedSectors;
+ ActiveFatSectorStart = FatSectorStart;
+ NumberOfFats = FatVolumeBootSector->NumberOfFats;
+ SectorsPerFat = FatVolumeBootSector->SectorsPerFat;
+
+ RootDirSectorStart = FatSectorStart + NumberOfFats * SectorsPerFat;
+ RootDirSectors = ((FatVolumeBootSector->RootDirEntries * 32) + (BytesPerSector - 1))
/ BytesPerSector;
+
+ DataSectorStart = RootDirSectorStart + RootDirSectors;
}
else
{
- Volume->BytesPerSector = Fat32VolumeBootSector->BytesPerSector;
- Volume->SectorsPerCluster = Fat32VolumeBootSector->SectorsPerCluster;
- Volume->FatSectorStart = Fat32VolumeBootSector->ReservedSectors;
- Volume->ActiveFatSectorStart = Volume->FatSectorStart +
- ((Fat32VolumeBootSector->ExtendedFlags & 0x80) ?
((Fat32VolumeBootSector->ExtendedFlags & 0x0f) *
Fat32VolumeBootSector->SectorsPerFatBig) : 0);
- Volume->NumberOfFats = Fat32VolumeBootSector->NumberOfFats;
- Volume->SectorsPerFat = Fat32VolumeBootSector->SectorsPerFatBig;
-
- Volume->RootDirStartCluster = Fat32VolumeBootSector->RootDirStartCluster;
- Volume->DataSectorStart = Volume->FatSectorStart + Volume->NumberOfFats *
Volume->SectorsPerFat;
+ BytesPerSector = Fat32VolumeBootSector->BytesPerSector;
+ SectorsPerCluster = Fat32VolumeBootSector->SectorsPerCluster;
+ FatSectorStart = Fat32VolumeBootSector->ReservedSectors;
+ ActiveFatSectorStart = FatSectorStart +
+ ((Fat32VolumeBootSector->ExtendedFlags & 0x80) ?
((Fat32VolumeBootSector->ExtendedFlags & 0x0f) *
Fat32VolumeBootSector->SectorsPerFatBig) : 0);
+ NumberOfFats = Fat32VolumeBootSector->NumberOfFats;
+ SectorsPerFat = Fat32VolumeBootSector->SectorsPerFatBig;
+
+ RootDirStartCluster = Fat32VolumeBootSector->RootDirStartCluster;
+ DataSectorStart = FatSectorStart + NumberOfFats * SectorsPerFat;
//
// Check version
@@ -307,6 +345,20 @@
FileSystemError("FreeLoader is too old to work with this FAT32
filesystem.\nPlease update FreeLoader.");
return FALSE;
}
+ }
+ MmHeapFree(FatVolumeBootSector);
+
+ {
+ GEOMETRY DriveGeometry;
+ ULONG BlockSize;
+
+ // Initialize drive by getting its geometry
+ if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry))
+ {
+ return FALSE;
+ }
+
+ BlockSize = MachDiskGetCacheableBlockCount(DriveNumber);
}
return TRUE;
@@ -367,7 +419,7 @@
}
}
-PVOID FatBufferDirectory(PFAT_VOLUME_INFO Volume, ULONG DirectoryStartCluster, ULONG
*DirectorySize, BOOLEAN RootDirectory)
+PVOID FatBufferDirectory(ULONG DirectoryStartCluster, ULONG *DirectorySize, BOOLEAN
RootDirectory)
{
PVOID DirectoryBuffer;
@@ -377,9 +429,9 @@
* For FAT32, the root directory is nothing special. We can treat it the same
* as a subdirectory.
*/
- if (RootDirectory && Volume->FatType == FAT32)
- {
- DirectoryStartCluster = Volume->RootDirStartCluster;
+ if (RootDirectory && FAT32 == FatType)
+ {
+ DirectoryStartCluster = RootDirStartCluster;
RootDirectory = FALSE;
}
@@ -388,11 +440,11 @@
//
if (RootDirectory)
{
- *DirectorySize = Volume->RootDirSectors * Volume->BytesPerSector;
+ *DirectorySize = RootDirSectors * BytesPerSector;
}
else
{
- *DirectorySize = FatCountClustersInChain(Volume, DirectoryStartCluster) *
Volume->SectorsPerCluster * Volume->BytesPerSector;
+ *DirectorySize = FatCountClustersInChain(DirectoryStartCluster) * SectorsPerCluster *
BytesPerSector;
}
//
@@ -411,7 +463,7 @@
//
if (RootDirectory)
{
- if (!FatReadVolumeSectors(Volume, Volume->RootDirSectorStart,
Volume->RootDirSectors, DirectoryBuffer))
+ if (!FatReadVolumeSectors(FatDriveNumber, RootDirSectorStart, RootDirSectors,
DirectoryBuffer))
{
MmHeapFree(DirectoryBuffer);
return NULL;
@@ -419,7 +471,7 @@
}
else
{
- if (!FatReadClusterChain(Volume, DirectoryStartCluster, 0xFFFFFFFF, DirectoryBuffer))
+ if (!FatReadClusterChain(DirectoryStartCluster, 0xFFFFFFFF, DirectoryBuffer))
{
MmHeapFree(DirectoryBuffer);
return NULL;
@@ -429,7 +481,7 @@
return DirectoryBuffer;
}
-BOOLEAN FatSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume, PVOID DirectoryBuffer,
ULONG DirectorySize, PCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer)
+BOOLEAN FatSearchDirectoryBufferForFile(PVOID DirectoryBuffer, ULONG DirectorySize, PCHAR
FileName, PFAT_FILE_INFO FatFileInfoPointer)
{
ULONG EntryCount;
ULONG CurrentEntry;
@@ -620,7 +672,7 @@
//
StartCluster = ((ULONG)DirEntry->ClusterHigh << 16) +
DirEntry->ClusterLow;
DPRINTM(DPRINT_FILESYSTEM, "StartCluster = 0x%x\n", StartCluster);
- FatFileInfoPointer->FileFatChain = FatGetClusterChainArray(Volume, StartCluster);
+ FatFileInfoPointer->FileFatChain = FatGetClusterChainArray(StartCluster);
//
// See if memory allocation failed
@@ -644,7 +696,7 @@
return FALSE;
}
-static BOOLEAN FatXSearchDirectoryBufferForFile(PFAT_VOLUME_INFO Volume, PVOID
DirectoryBuffer, ULONG DirectorySize, PCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer)
+static BOOLEAN FatXSearchDirectoryBufferForFile(PVOID DirectoryBuffer, ULONG
DirectorySize, PCHAR FileName, PFAT_FILE_INFO FatFileInfoPointer)
{
ULONG EntryCount;
ULONG CurrentEntry;
@@ -694,7 +746,7 @@
/*
* Get the cluster chain
*/
- FatFileInfoPointer->FileFatChain = FatGetClusterChainArray(Volume,
DirEntry->StartCluster);
+ FatFileInfoPointer->FileFatChain =
FatGetClusterChainArray(DirEntry->StartCluster);
/*
* See if memory allocation failed
@@ -717,7 +769,7 @@
* specified filename and fills in an FAT_FILE_INFO structure
* with info describing the file, etc. returns ARC error code
*/
-LONG FatLookupFile(PFAT_VOLUME_INFO Volume, PCSTR FileName, ULONG DeviceId,
PFAT_FILE_INFO FatFileInfoPointer)
+LONG FatLookupFile(PCSTR FileName, ULONG DeviceId, PFAT_FILE_INFO FatFileInfoPointer)
{
UINT32 i;
ULONG NumberOfPathParts;
@@ -757,7 +809,7 @@
//
// Buffer the directory contents
//
- DirectoryBuffer = FatBufferDirectory(Volume, DirectoryStartCluster, &DirectorySize,
(i == 0) );
+ DirectoryBuffer = FatBufferDirectory(DirectoryStartCluster, &DirectorySize, (i ==
0) );
if (DirectoryBuffer == NULL)
{
return ENOMEM;
@@ -766,9 +818,9 @@
//
// Search for file name in directory
//
- if (ISFATX(Volume->FatType))
- {
- if (!FatXSearchDirectoryBufferForFile(Volume, DirectoryBuffer, DirectorySize,
PathPart, &FatFileInfo))
+ if (ISFATX(FatType))
+ {
+ if (!FatXSearchDirectoryBufferForFile(DirectoryBuffer, DirectorySize, PathPart,
&FatFileInfo))
{
MmHeapFree(DirectoryBuffer);
return ENOENT;
@@ -776,7 +828,7 @@
}
else
{
- if (!FatSearchDirectoryBufferForFile(Volume, DirectoryBuffer, DirectorySize, PathPart,
&FatFileInfo))
+ if (!FatSearchDirectoryBufferForFile(DirectoryBuffer, DirectorySize, PathPart,
&FatFileInfo))
{
MmHeapFree(DirectoryBuffer);
return ENOENT;
@@ -854,7 +906,7 @@
* FatGetFatEntry()
* returns the Fat entry for a given cluster number
*/
-BOOLEAN FatGetFatEntry(PFAT_VOLUME_INFO Volume, ULONG Cluster, ULONG* ClusterPointer)
+BOOLEAN FatGetFatEntry(ULONG Cluster, ULONG* ClusterPointer)
{
ULONG fat = 0;
UINT32 FatOffset;
@@ -863,28 +915,28 @@
DPRINTM(DPRINT_FILESYSTEM, "FatGetFatEntry() Retrieving FAT entry for cluster
%d.\n", Cluster);
- switch(Volume->FatType)
+ switch(FatType)
{
case FAT12:
FatOffset = Cluster + (Cluster / 2);
- ThisFatSecNum = Volume->ActiveFatSectorStart + (FatOffset /
Volume->BytesPerSector);
- ThisFatEntOffset = (FatOffset % Volume->BytesPerSector);
+ ThisFatSecNum = ActiveFatSectorStart + (FatOffset / BytesPerSector);
+ ThisFatEntOffset = (FatOffset % BytesPerSector);
DPRINTM(DPRINT_FILESYSTEM, "FatOffset: %d\n", FatOffset);
DPRINTM(DPRINT_FILESYSTEM, "ThisFatSecNum: %d\n", ThisFatSecNum);
DPRINTM(DPRINT_FILESYSTEM, "ThisFatEntOffset: %d\n", ThisFatEntOffset);
- if (ThisFatEntOffset == (Volume->BytesPerSector - 1))
- {
- if (!FatReadVolumeSectors(Volume, ThisFatSecNum, 2, (PVOID)FILESYSBUFFER))
+ if (ThisFatEntOffset == (BytesPerSector - 1))
+ {
+ if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 2, (PVOID)FILESYSBUFFER))
{
return FALSE;
}
}
else
{
- if (!FatReadVolumeSectors(Volume, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
+ if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
{
return FALSE;
}
@@ -903,10 +955,10 @@
case FATX16:
FatOffset = (Cluster * 2);
- ThisFatSecNum = Volume->ActiveFatSectorStart + (FatOffset /
Volume->BytesPerSector);
- ThisFatEntOffset = (FatOffset % Volume->BytesPerSector);
-
- if (!FatReadVolumeSectors(Volume, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
+ ThisFatSecNum = ActiveFatSectorStart + (FatOffset / BytesPerSector);
+ ThisFatEntOffset = (FatOffset % BytesPerSector);
+
+ if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
{
return FALSE;
}
@@ -920,10 +972,10 @@
case FATX32:
FatOffset = (Cluster * 4);
- ThisFatSecNum = Volume->ActiveFatSectorStart + (FatOffset /
Volume->BytesPerSector);
- ThisFatEntOffset = (FatOffset % Volume->BytesPerSector);
-
- if (!FatReadVolumeSectors(Volume, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
+ ThisFatSecNum = ActiveFatSectorStart + (FatOffset / BytesPerSector);
+ ThisFatEntOffset = (FatOffset % BytesPerSector);
+
+ if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
{
return FALSE;
}
@@ -934,10 +986,6 @@
break;
- default:
- DPRINTM(DPRINT_FILESYSTEM, "Unknown FAT type %d\n", Volume->FatType);
- return FALSE;
-
}
DPRINTM(DPRINT_FILESYSTEM, "FAT entry is 0x%x.\n", fat);
@@ -947,7 +995,7 @@
return TRUE;
}
-ULONG FatCountClustersInChain(PFAT_VOLUME_INFO Volume, ULONG StartCluster)
+ULONG FatCountClustersInChain(ULONG StartCluster)
{
ULONG ClusterCount = 0;
@@ -958,9 +1006,9 @@
//
// If end of chain then break out of our cluster counting loop
//
- if (((Volume->FatType == FAT12) && (StartCluster >= 0xff8)) ||
- ((Volume->FatType == FAT16 || Volume->FatType == FATX16) &&
(StartCluster >= 0xfff8)) ||
- ((Volume->FatType == FAT32 || Volume->FatType == FATX32) &&
(StartCluster >= 0x0ffffff8)))
+ if (((FatType == FAT12) && (StartCluster >= 0xff8)) ||
+ ((FatType == FAT16 || FatType == FATX16) && (StartCluster >= 0xfff8)) ||
+ ((FatType == FAT32 || FatType == FATX32) && (StartCluster >= 0x0ffffff8)))
{
break;
}
@@ -973,7 +1021,7 @@
//
// Get next cluster
//
- if (!FatGetFatEntry(Volume, StartCluster, &StartCluster))
+ if (!FatGetFatEntry(StartCluster, &StartCluster))
{
return 0;
}
@@ -984,7 +1032,7 @@
return ClusterCount;
}
-ULONG* FatGetClusterChainArray(PFAT_VOLUME_INFO Volume, ULONG StartCluster)
+ULONG* FatGetClusterChainArray(ULONG StartCluster)
{
ULONG ClusterCount;
ULONG ArraySize;
@@ -993,7 +1041,7 @@
DPRINTM(DPRINT_FILESYSTEM, "FatGetClusterChainArray() StartCluster = %d\n",
StartCluster);
- ClusterCount = FatCountClustersInChain(Volume, StartCluster) + 1; // Lets get the
0x0ffffff8 on the end of the array
+ ClusterCount = FatCountClustersInChain(StartCluster) + 1; // Lets get the 0x0ffffff8 on
the end of the array
ArraySize = ClusterCount * sizeof(ULONG);
//
@@ -1019,9 +1067,9 @@
//
// Don't try to get next cluster for last cluster
//
- if (((Volume->FatType == FAT12) && (StartCluster >= 0xff8)) ||
- ((Volume->FatType == FAT16 || Volume->FatType == FATX16) &&
(StartCluster >= 0xfff8)) ||
- ((Volume->FatType == FAT32 || Volume->FatType == FATX32) &&
(StartCluster >= 0x0ffffff8)))
+ if (((FatType == FAT12) && (StartCluster >= 0xff8)) ||
+ ((FatType == FAT16 || FatType == FATX16) && (StartCluster >= 0xfff8)) ||
+ ((FatType == FAT32 || FatType == FATX32) && (StartCluster >= 0x0ffffff8)))
{
Idx++;
break;
@@ -1030,7 +1078,7 @@
//
// Get next cluster
//
- if (!FatGetFatEntry(Volume, StartCluster, &StartCluster))
+ if (!FatGetFatEntry(StartCluster, &StartCluster))
{
MmHeapFree(ArrayPointer);
return NULL;
@@ -1038,13 +1086,35 @@
}
return ArrayPointer;
+}
+
+/*
+ * FatReadCluster()
+ * Reads the specified cluster into memory
+ */
+BOOLEAN FatReadCluster(ULONG ClusterNumber, PVOID Buffer)
+{
+ ULONG ClusterStartSector;
+
+ ClusterStartSector = ((ClusterNumber - 2) * SectorsPerCluster) + DataSectorStart;
+
+ DPRINTM(DPRINT_FILESYSTEM, "FatReadCluster() ClusterNumber = %d Buffer = 0x%x
ClusterStartSector = %d\n", ClusterNumber, Buffer, ClusterStartSector);
+
+ if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, SectorsPerCluster,
(PVOID)FILESYSBUFFER))
+ {
+ return FALSE;
+ }
+
+ memcpy(Buffer, (PVOID)FILESYSBUFFER, SectorsPerCluster * BytesPerSector);
+
+ return TRUE;
}
/*
* FatReadClusterChain()
* Reads the specified clusters into memory
*/
-BOOLEAN FatReadClusterChain(PFAT_VOLUME_INFO Volume, ULONG StartClusterNumber, ULONG
NumberOfClusters, PVOID Buffer)
+BOOLEAN FatReadClusterChain(ULONG StartClusterNumber, ULONG NumberOfClusters, PVOID
Buffer)
{
ULONG ClusterStartSector;
@@ -1057,17 +1127,17 @@
//
// Calculate starting sector for cluster
//
- ClusterStartSector = ((StartClusterNumber - 2) * Volume->SectorsPerCluster) +
Volume->DataSectorStart;
+ ClusterStartSector = ((StartClusterNumber - 2) * SectorsPerCluster) + DataSectorStart;
//
// Read cluster into memory
//
- if (!FatReadVolumeSectors(Volume, ClusterStartSector, Volume->SectorsPerCluster,
(PVOID)FILESYSBUFFER))
+ if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, SectorsPerCluster,
(PVOID)FILESYSBUFFER))
{
return FALSE;
}
- memcpy(Buffer, (PVOID)FILESYSBUFFER, Volume->SectorsPerCluster *
Volume->BytesPerSector);
+ memcpy(Buffer, (PVOID)FILESYSBUFFER, SectorsPerCluster * BytesPerSector);
//
// Decrement count of clusters left to read
@@ -1077,12 +1147,12 @@
//
// Increment buffer address by cluster size
//
- Buffer = (PVOID)((ULONG_PTR)Buffer + (Volume->SectorsPerCluster *
Volume->BytesPerSector));
+ Buffer = (PVOID)((ULONG_PTR)Buffer + (SectorsPerCluster * BytesPerSector));
//
// Get next cluster
//
- if (!FatGetFatEntry(Volume, StartClusterNumber, &StartClusterNumber))
+ if (!FatGetFatEntry(StartClusterNumber, &StartClusterNumber))
{
return FALSE;
}
@@ -1090,9 +1160,9 @@
//
// If end of chain then break out of our cluster reading loop
//
- if (((Volume->FatType == FAT12) && (StartClusterNumber >= 0xff8)) ||
- ((Volume->FatType == FAT16 || Volume->FatType == FATX16) &&
(StartClusterNumber >= 0xfff8)) ||
- ((Volume->FatType == FAT32 || Volume->FatType == FATX32) &&
(StartClusterNumber >= 0x0ffffff8)))
+ if (((FatType == FAT12) && (StartClusterNumber >= 0xff8)) ||
+ ((FatType == FAT16 || FatType == FATX16) && (StartClusterNumber >= 0xfff8))
||
+ ((FatType == FAT32 || FatType == FATX32) && (StartClusterNumber >=
0x0ffffff8)))
{
break;
}
@@ -1105,15 +1175,15 @@
* FatReadPartialCluster()
* Reads part of a cluster into memory
*/
-BOOLEAN FatReadPartialCluster(PFAT_VOLUME_INFO Volume, ULONG ClusterNumber, ULONG
StartingOffset, ULONG Length, PVOID Buffer)
+BOOLEAN FatReadPartialCluster(ULONG ClusterNumber, ULONG StartingOffset, ULONG Length,
PVOID Buffer)
{
ULONG ClusterStartSector;
DPRINTM(DPRINT_FILESYSTEM, "FatReadPartialCluster() ClusterNumber = %d
StartingOffset = %d Length = %d Buffer = 0x%x\n", ClusterNumber, StartingOffset,
Length, Buffer);
- ClusterStartSector = ((ClusterNumber - 2) * Volume->SectorsPerCluster) +
Volume->DataSectorStart;
-
- if (!FatReadVolumeSectors(Volume, ClusterStartSector, Volume->SectorsPerCluster,
(PVOID)FILESYSBUFFER))
+ ClusterStartSector = ((ClusterNumber - 2) * SectorsPerCluster) + DataSectorStart;
+
+ if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, SectorsPerCluster,
(PVOID)FILESYSBUFFER))
{
return FALSE;
}
@@ -1130,7 +1200,6 @@
*/
BOOLEAN FatReadFile(PFAT_FILE_INFO FatFileInfo, ULONG BytesToRead, ULONG* BytesRead,
PVOID Buffer)
{
- PFAT_VOLUME_INFO Volume = FatFileInfo->Volume;
ULONG ClusterNumber;
ULONG OffsetInCluster;
ULONG LengthInCluster;
@@ -1191,7 +1260,7 @@
// the last cluster.
//
- BytesPerCluster = Volume->SectorsPerCluster * Volume->BytesPerSector;
+ BytesPerCluster = SectorsPerCluster * BytesPerSector;
//
// Only do the first read if we
@@ -1210,7 +1279,7 @@
//
// Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
//
- if (!FatReadPartialCluster(Volume, ClusterNumber, OffsetInCluster, LengthInCluster,
Buffer))
+ if (!FatReadPartialCluster(ClusterNumber, OffsetInCluster, LengthInCluster, Buffer))
{
return FALSE;
}
@@ -1241,7 +1310,7 @@
//
// Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
//
- if (!FatReadClusterChain(Volume, ClusterNumber, NumberOfClusters, Buffer))
+ if (!FatReadClusterChain(ClusterNumber, NumberOfClusters, Buffer))
{
return FALSE;
}
@@ -1266,7 +1335,7 @@
//
// Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
//
- if (!FatReadPartialCluster(Volume, ClusterNumber, 0, BytesToRead, Buffer))
+ if (!FatReadPartialCluster(ClusterNumber, 0, BytesToRead, Buffer))
{
return FALSE;
}
@@ -1282,35 +1351,16 @@
return TRUE;
}
-BOOLEAN FatReadVolumeSectors(PFAT_VOLUME_INFO Volume, ULONG SectorNumber, ULONG
SectorCount, PVOID Buffer)
-{
- LARGE_INTEGER Position;
- ULONG Count;
- LONG ret;
-
- DPRINTM(DPRINT_FILESYSTEM, "FatReadVolumeSectors(): SectorNumber %d, SectorCount
%d, Buffer %p\n",
- SectorNumber, SectorCount, Buffer);
-
- //
- // Seek to right position
- //
- Position.QuadPart = SectorNumber * 512;
- ret = ArcSeek(Volume->DeviceId, &Position, SeekAbsolute);
- if (ret != ESUCCESS)
- {
- DPRINTM(DPRINT_FILESYSTEM, "FatReadVolumeSectors() Failed to seek\n");
+BOOLEAN FatReadVolumeSectors(ULONG DriveNumber, ULONG SectorNumber, ULONG SectorCount,
PVOID Buffer)
+{
+ // Now try to read in the block
+ if (!MachDiskReadLogicalSectors(DriveNumber, SectorNumber + FatVolumeStartSector,
SectorCount, (PVOID)DISKREADBUFFER))
+ {
return FALSE;
}
- //
- // Read data
- //
- ret = ArcRead(Volume->DeviceId, Buffer, SectorCount * 512, &Count);
- if (ret != ESUCCESS || Count != SectorCount * 512)
- {
- DPRINTM(DPRINT_FILESYSTEM, "FatReadVolumeSectors() Failed to read\n");
- return FALSE;
- }
+ // Copy data to the caller
+ RtlCopyMemory(Buffer, (PVOID)DISKREADBUFFER, SectorCount * BytesPerSector);
// Return success
return TRUE;
@@ -1356,7 +1406,7 @@
DPRINTM(DPRINT_FILESYSTEM, "FatOpen() FileName = %s\n", Path);
RtlZeroMemory(&TempFileInfo, sizeof(TempFileInfo));
- ret = FatLookupFile(FatVolume, Path, DeviceId, &TempFileInfo);
+ ret = FatLookupFile(Path, DeviceId, &TempFileInfo);
if (ret != ESUCCESS)
return ENOENT;
@@ -1365,7 +1415,6 @@
return ENOMEM;
RtlCopyMemory(FileHandle, &TempFileInfo, sizeof(FAT_FILE_INFO));
- FileHandle->Volume = FatVolume;
FsSetDeviceSpecific(*FileId, FileHandle);
return ESUCCESS;
@@ -1418,24 +1467,13 @@
const DEVVTBL* FatMount(ULONG DeviceId)
{
- PFAT_VOLUME_INFO Volume;
UCHAR Buffer[512];
PFAT_BOOTSECTOR BootSector = (PFAT_BOOTSECTOR)Buffer;
PFAT32_BOOTSECTOR BootSector32 = (PFAT32_BOOTSECTOR)Buffer;
PFATX_BOOTSECTOR BootSectorX = (PFATX_BOOTSECTOR)Buffer;
- FILEINFORMATION FileInformation;
LARGE_INTEGER Position;
ULONG Count;
- ULARGE_INTEGER SectorCount;
LONG ret;
-
- //
- // Allocate data for volume information
- //
- Volume = MmHeapAlloc(sizeof(FAT_VOLUME_INFO));
- if (!Volume)
- return NULL;
- RtlZeroMemory(Volume, sizeof(FAT_VOLUME_INFO));
//
// Read the BootSector
@@ -1444,68 +1482,31 @@
Position.LowPart = 0;
ret = ArcSeek(DeviceId, &Position, SeekAbsolute);
if (ret != ESUCCESS)
- {
- MmHeapFree(Volume);
return NULL;
- }
ret = ArcRead(DeviceId, Buffer, sizeof(Buffer), &Count);
if (ret != ESUCCESS || Count != sizeof(Buffer))
- {
- MmHeapFree(Volume);
return NULL;
- }
-
- //
- // Check if BootSector is valid. If no, return early
- //
- if (!RtlEqualMemory(BootSector->FileSystemType, "FAT12 ", 8) &&
- !RtlEqualMemory(BootSector->FileSystemType, "FAT16 ", 8) &&
- !RtlEqualMemory(BootSector32->FileSystemType, "FAT32 ", 8)
&&
- !RtlEqualMemory(BootSectorX->FileSystemType, "FATX", 4))
- {
- MmHeapFree(Volume);
+
+ //
+ // Check if BootSector is valid. If yes, return FAT function table
+ //
+ if (RtlEqualMemory(BootSector->FileSystemType, "FAT12 ", 8) ||
+ RtlEqualMemory(BootSector->FileSystemType, "FAT16 ", 8) ||
+ RtlEqualMemory(BootSector32->FileSystemType, "FAT32 ", 8) ||
+ RtlEqualMemory(BootSectorX->FileSystemType, "FATX", 4))
+ {
+ //
+ // Compatibility hack as long as FS is not using underlying device DeviceId
+ //
+ ULONG DriveNumber;
+ ULONGLONG StartSector;
+ ULONGLONG SectorCount;
+ int Type;
+ if (!DiskGetBootVolume(&DriveNumber, &StartSector, &SectorCount,
&Type))
+ return NULL;
+ FatOpenVolume(DriveNumber, StartSector, SectorCount);
+ return &FatFuncTable;
+ }
+ else
return NULL;
- }
-
- //
- // Determine sector count
- //
- ret = ArcGetFileInformation(DeviceId, &FileInformation);
- if (ret != ESUCCESS)
- {
- MmHeapFree(Volume);
- return NULL;
- }
- SectorCount.HighPart = FileInformation.EndingAddress.HighPart;
- SectorCount.LowPart = FileInformation.EndingAddress.LowPart;
- SectorCount.QuadPart /= SECTOR_SIZE;
-
- //
- // Keep device id
- //
- Volume->DeviceId = DeviceId;
-
- //
- // Really open the volume
- //
- if (!FatOpenVolume(Volume, BootSector, SectorCount.QuadPart))
- {
- MmHeapFree(Volume);
- return NULL;
- }
-
- //
- // HACK: for now, only support one FAT volume
- //
- if (FatVolume)
- {
- MmHeapFree(Volume);
- return NULL;
- }
- FatVolume = Volume;
-
- //
- // Return success
- //
- return &FatFuncTable;
-}
+}
Modified: trunk/reactos/boot/freeldr/freeldr/include/fs/fat.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/boot/freeldr/freeldr/inclu…
==============================================================================
--- trunk/reactos/boot/freeldr/freeldr/include/fs/fat.h [iso-8859-1] (original)
+++ trunk/reactos/boot/freeldr/freeldr/include/fs/fat.h [iso-8859-1] Sun Sep 6 23:33:27
2009
@@ -144,15 +144,12 @@
} FATX_DIRENTRY, * PFATX_DIRENTRY;
#include <poppack.h>
-typedef struct _FAT_VOLUME_INFO *PFAT_VOLUME_INFO;
-
typedef struct
{
ULONG FileSize; /* File size */
ULONG FilePointer; /* File pointer */
ULONG* FileFatChain; /* File fat chain array */
ULONG DriveNumber;
- PFAT_VOLUME_INFO Volume;
} FAT_FILE_INFO, * PFAT_FILE_INFO;
#define ATTR_NORMAL 0x00