Fixed a bug in the LBA extensions detection code.
[reactos.git] / freeldr / freeldr / fs / fat.c
index 571a9d1..c7ac7e7 100644 (file)
@@ -23,7 +23,7 @@
 #include <disk.h>
 #include <rtl.h>
 #include <ui.h>
-#include <asmcode.h>
+#include <arch.h>
 #include <mm.h>
 #include <debug.h>
 #include <cache.h>
@@ -40,12 +40,10 @@ ULONG                               RootDirSectors;                 // Number of sectors of the root directory (fat32)
 ULONG                          FatType = 0;                    // FAT12, FAT16, or FAT32
 ULONG                          FatDriveNumber = 0;
 
-
 BOOL FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector)
 {
-       ULONG   PhysicalTrack;
-       ULONG   PhysicalHead;
-       ULONG   PhysicalSector;
+
+       DbgPrint((DPRINT_FILESYSTEM, "FatOpenVolume() DriveNumber = 0x%x VolumeStartSector = %d\n", DriveNumber, VolumeStartSector));
 
        // Store the drive number
        FatDriveNumber = DriveNumber;
@@ -78,26 +76,10 @@ BOOL FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector)
 
        // Now try to read the boot sector
        // If this fails then abort
-       if (BiosInt13ExtensionsSupported(DriveNumber))
-       {
-               if (!BiosInt13ReadExtended(DriveNumber, VolumeStartSector, 1, FatVolumeBootSector))
-               {
-                       FileSystemError("Disk read error.");
-                       return FALSE;
-               }
-       }
-       else
-       {
-               // Calculate the physical disk offsets
-               PhysicalSector = 1 + (VolumeStartSector % get_sectors(DriveNumber));
-               PhysicalHead = (VolumeStartSector / get_sectors(DriveNumber)) % get_heads(DriveNumber);
-               PhysicalTrack = (VolumeStartSector / get_sectors(DriveNumber)) / get_heads(DriveNumber);
 
-               if (!BiosInt13Read(DriveNumber, PhysicalHead, PhysicalTrack, PhysicalSector, 1, FatVolumeBootSector))
-               {
-                       FileSystemError("Disk read error.");
-                       return FALSE;
-               }
+       if (!DiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, FatVolumeBootSector))
+       {
+               return FALSE;
        }
 
        // Get the FAT type
@@ -107,7 +89,7 @@ BOOL FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector)
 
        DbgPrint((DPRINT_FILESYSTEM, "Dumping boot sector:\n"));
 
-       if (FatFileSystemType == FAT32)
+       if (FatType == FAT32)
        {
                DbgPrint((DPRINT_FILESYSTEM, "sizeof(FAT32_BOOTSECTOR) = 0x%x.\n", sizeof(FAT32_BOOTSECTOR)));
 
@@ -178,14 +160,6 @@ BOOL FatOpenVolume(ULONG DriveNumber, ULONG VolumeStartSector)
                return FALSE;
        }
 
-       //
-       // Set the drive geometry
-       //
-       if (!DiskSetDriveGeometry(DriveNumber, get_cylinders(DriveNumber), get_heads(DriveNumber), get_sectors(DriveNumber), FatVolumeBootSector->BytesPerSector))
-       {
-               return FALSE;
-       }
-
        //
        // Check the FAT cluster size
        // We do not support clusters bigger than 64k
@@ -273,7 +247,12 @@ ULONG FatDetermineFatType(PFAT_BOOTSECTOR FatBootSector)
        SectorsPerFat = FatBootSector->SectorsPerFat ? FatBootSector->SectorsPerFat : Fat32BootSector->SectorsPerFatBig;
        TotalSectors = FatBootSector->TotalSectors ? FatBootSector->TotalSectors : FatBootSector->TotalSectorsBig;
        DataSectorCount = TotalSectors - (FatBootSector->ReservedSectors + (FatBootSector->NumberOfFats * SectorsPerFat) + RootDirSectors);
-       CountOfClusters = DataSectorCount / FatBootSector->SectorsPerCluster;
+       
+//mjl 
+       if (FatBootSector->SectorsPerCluster == 0)
+               CountOfClusters = 0;
+       else
+               CountOfClusters = DataSectorCount / FatBootSector->SectorsPerCluster;
 
        if (CountOfClusters < 4085)
        {
@@ -306,10 +285,35 @@ PVOID FatBufferDirectory(UINT32 DirectoryStartCluster, PUINT32 EntryCountPointer
        //
        if ((RootDirectory) && (FatType != FAT32))
        {
-               DirectorySize = (FatVolumeBootSector->RootDirEntries / 32) * 512;
+               DbgPrint((DPRINT_FILESYSTEM, "We are here.\n"));
+               /*DbgPrint((DPRINT_FILESYSTEM, "sizeof(FAT_BOOTSECTOR) = 0x%x.\n", sizeof(FAT_BOOTSECTOR)));
+
+               DbgPrint((DPRINT_FILESYSTEM, "JumpBoot: 0x%x 0x%x 0x%x\n", FatVolumeBootSector->JumpBoot[0], FatVolumeBootSector->JumpBoot[1], FatVolumeBootSector->JumpBoot[2]));
+               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]));*/
+               DbgPrint((DPRINT_FILESYSTEM, "BytesPerSector: %d\n", FatVolumeBootSector->BytesPerSector));
+               DbgPrint((DPRINT_FILESYSTEM, "SectorsPerCluster: %d\n", FatVolumeBootSector->SectorsPerCluster));
+               DbgPrint((DPRINT_FILESYSTEM, "ReservedSectors: %d\n", FatVolumeBootSector->ReservedSectors));
+               DbgPrint((DPRINT_FILESYSTEM, "NumberOfFats: %d\n", FatVolumeBootSector->NumberOfFats));
+               DbgPrint((DPRINT_FILESYSTEM, "RootDirEntries: %d\n", FatVolumeBootSector->RootDirEntries));
+               DbgPrint((DPRINT_FILESYSTEM, "TotalSectors: %d\n", FatVolumeBootSector->TotalSectors));
+               DbgPrint((DPRINT_FILESYSTEM, "MediaDescriptor: 0x%x\n", FatVolumeBootSector->MediaDescriptor));
+               DbgPrint((DPRINT_FILESYSTEM, "SectorsPerFat: %d\n", FatVolumeBootSector->SectorsPerFat));
+               DbgPrint((DPRINT_FILESYSTEM, "SectorsPerTrack: %d\n", FatVolumeBootSector->SectorsPerTrack));
+               DbgPrint((DPRINT_FILESYSTEM, "NumberOfHeads: %d\n", FatVolumeBootSector->NumberOfHeads));
+               DbgPrint((DPRINT_FILESYSTEM, "HiddenSectors: %d\n", FatVolumeBootSector->HiddenSectors));
+               DbgPrint((DPRINT_FILESYSTEM, "TotalSectorsBig: %d\n", FatVolumeBootSector->TotalSectorsBig));
+               DbgPrint((DPRINT_FILESYSTEM, "DriveNumber: 0x%x\n", FatVolumeBootSector->DriveNumber));
+               DbgPrint((DPRINT_FILESYSTEM, "Reserved1: 0x%x\n", FatVolumeBootSector->Reserved1));
+               DbgPrint((DPRINT_FILESYSTEM, "BootSignature: 0x%x\n", FatVolumeBootSector->BootSignature));
+               DbgPrint((DPRINT_FILESYSTEM, "VolumeSerialNumber: 0x%x\n", FatVolumeBootSector->VolumeSerialNumber));
+               /*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]));
+               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]));*/
+               DbgPrint((DPRINT_FILESYSTEM, "BootSectorMagic: 0x%x\n", FatVolumeBootSector->BootSectorMagic));
+               DirectorySize = ROUND_UP((FatVolumeBootSector->RootDirEntries * 32), FatVolumeBootSector->BytesPerSector);
        }
        else
        {
+               DbgPrint((DPRINT_FILESYSTEM, "No we are here.\n"));
                if (RootDirectory)
                {
                        DirectorySize = (FatCountClustersInChain(Fat32VolumeBootSector->RootDirStartCluster) * Fat32VolumeBootSector->SectorsPerCluster) * Fat32VolumeBootSector->BytesPerSector;
@@ -323,6 +327,7 @@ PVOID FatBufferDirectory(UINT32 DirectoryStartCluster, PUINT32 EntryCountPointer
        //
        // Attempt to allocate memory for directory buffer
        //
+       DbgPrint((DPRINT_FILESYSTEM, "Trying to allocate (DirectorySize) %d bytes.\n", DirectorySize));
        DirectoryBuffer = AllocateMemory(DirectorySize);
 
        if (DirectoryBuffer == NULL)
@@ -349,7 +354,7 @@ PVOID FatBufferDirectory(UINT32 DirectoryStartCluster, PUINT32 EntryCountPointer
                        // FAT type is not FAT32 so the root directory comes right after the fat table
                        //
                        RootDirectoryStartSector = FatVolumeBootSector->ReservedSectors + (FatVolumeBootSector->NumberOfFats * FatVolumeBootSector->SectorsPerFat);
-                       RootDirectorySectorCount = FatVolumeBootSector->RootDirEntries / 32;
+                       RootDirectorySectorCount = (DirectorySize / FatVolumeBootSector->BytesPerSector);
 
                        if (!FatReadVolumeSectors(FatDriveNumber, RootDirectoryStartSector, RootDirectorySectorCount, DirectoryBuffer))
                        {
@@ -797,20 +802,20 @@ BOOL FatGetFatEntry(UINT32 Cluster, PUINT32 ClusterPointer)
 
                if (ThisFatEntOffset == (FatVolumeBootSector->BytesPerSector - 1))
                {
-                       if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 2, (PVOID)DISKREADBUFFER))
+                       if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 2, (PVOID)FILESYSBUFFER))
                        {
                                return FALSE;
                        }
                }
                else
                {
-                       if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)DISKREADBUFFER))
+                       if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
                        {
                                return FALSE;
                        }
                }
 
-               fat = *((WORD *) (DISKREADBUFFER + ThisFatEntOffset));
+               fat = *((WORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset));
                if (Cluster & 0x0001) 
                        fat = fat >> 4; /* Cluster number is ODD */
                else
@@ -824,12 +829,12 @@ BOOL FatGetFatEntry(UINT32 Cluster, PUINT32 ClusterPointer)
                ThisFatSecNum = FatVolumeBootSector->ReservedSectors + (FatOffset / FatVolumeBootSector->BytesPerSector);
                ThisFatEntOffset = (FatOffset % FatVolumeBootSector->BytesPerSector);
 
-               if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)DISKREADBUFFER))
+               if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
                {
                        return FALSE;
                }
 
-               fat = *((WORD *) (DISKREADBUFFER + ThisFatEntOffset));
+               fat = *((WORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset));
 
                break;
 
@@ -840,13 +845,13 @@ BOOL FatGetFatEntry(UINT32 Cluster, PUINT32 ClusterPointer)
                ThisFatSecNum += FatVolumeBootSector->ReservedSectors + (FatOffset / FatVolumeBootSector->BytesPerSector);
                ThisFatEntOffset = (FatOffset % FatVolumeBootSector->BytesPerSector);
 
-               if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)DISKREADBUFFER))
+               if (!FatReadVolumeSectors(FatDriveNumber, ThisFatSecNum, 1, (PVOID)FILESYSBUFFER))
                {
                        return FALSE;
                }
 
                // Get the fat entry
-               fat = (*((DWORD *) (DISKREADBUFFER + ThisFatEntOffset))) & 0x0FFFFFFF;
+               fat = (*((DWORD *) ((PVOID)FILESYSBUFFER + ThisFatEntOffset))) & 0x0FFFFFFF;
 
                break;
 
@@ -994,12 +999,12 @@ BOOL FatReadCluster(ULONG ClusterNumber, PVOID Buffer)
 
        DbgPrint((DPRINT_FILESYSTEM, "FatReadCluster() ClusterNumber = %d Buffer = 0x%x ClusterStartSector = %d\n", ClusterNumber, Buffer, ClusterStartSector));
 
-       if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)DISKREADBUFFER))
+       if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
        {
                return FALSE;
        }
 
-       memcpy(Buffer, (PVOID)DISKREADBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
+       memcpy(Buffer, (PVOID)FILESYSBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
 
        return TRUE;
 }
@@ -1026,12 +1031,12 @@ BOOL FatReadClusterChain(ULONG StartClusterNumber, ULONG NumberOfClusters, PVOID
                //
                // Read cluster into memory
                //
-               if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)DISKREADBUFFER))
+               if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
                {
                        return FALSE;
                }
 
-               memcpy(Buffer, (PVOID)DISKREADBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
+               memcpy(Buffer, (PVOID)FILESYSBUFFER, FatVolumeBootSector->SectorsPerCluster * FatVolumeBootSector->BytesPerSector);
 
                //
                // Decrement count of clusters left to read
@@ -1077,12 +1082,12 @@ BOOL FatReadPartialCluster(ULONG ClusterNumber, ULONG StartingOffset, ULONG Leng
 
        ClusterStartSector = ((ClusterNumber - 2) * FatVolumeBootSector->SectorsPerCluster) + DataSectorStart;
 
-       if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)DISKREADBUFFER))
+       if (!FatReadVolumeSectors(FatDriveNumber, ClusterStartSector, FatVolumeBootSector->SectorsPerCluster, (PVOID)FILESYSBUFFER))
        {
                return FALSE;
        }
 
-       memcpy(Buffer, (PVOID)(DISKREADBUFFER + StartingOffset), Length);
+       memcpy(Buffer, ((PVOID)FILESYSBUFFER + StartingOffset), Length);
 
        return TRUE;
 }