[FREELOADER]
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / pcdisk.c
index b915141..304a867 100644 (file)
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "freeldr.h"
-#include "disk.h"
-#include "rtl.h"
-#include "arch.h"
-#include "debug.h"
-#include "portio.h"
-#include "machine.h"
-#include "machpc.h"
+#include <freeldr.h>
 
+#define NDEBUG
+#include <debug.h>
 
+#include <pshpack2.h>
 typedef struct
 {
-       U8              PacketSize;                             // 00h - Size of packet (10h or 18h)
-       U8              Reserved;                               // 01h - Reserved (0)
-       U16             LBABlockCount;                  // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
-       U16             TransferBufferOffset;   // 04h - Transfer buffer offset (seg:off)
-       U16             TransferBufferSegment;  //       Transfer buffer segment (seg:off)
-       U64             LBAStartBlock;                  // 08h - Starting absolute block number
-       U64             TransferBuffer64;               // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
+       UCHAR           PacketSize;                             // 00h - Size of packet (10h or 18h)
+       UCHAR           Reserved;                               // 01h - Reserved (0)
+       USHORT          LBABlockCount;                  // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
+       USHORT          TransferBufferOffset;   // 04h - Transfer buffer offset (seg:off)
+       USHORT          TransferBufferSegment;  //       Transfer buffer segment (seg:off)
+       ULONGLONG               LBAStartBlock;                  // 08h - Starting absolute block number
+       //ULONGLONG             TransferBuffer64;               // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
                                                                        //       used if DWORD at 04h is FFFFh:FFFFh
-} PACKED I386_DISK_ADDRESS_PACKET, *PI386_DISK_ADDRESS_PACKET;
+                                                                       //       Commented since some earlier BIOSes refuse to work with
+                                                                       //       such extended structure
+} I386_DISK_ADDRESS_PACKET, *PI386_DISK_ADDRESS_PACKET;
+#include <poppack.h>
 
 /////////////////////////////////////////////////////////////////////////////////////////////
 // FUNCTIONS
 /////////////////////////////////////////////////////////////////////////////////////////////
 
-static BOOL PcDiskResetController(U32 DriveNumber)
+static BOOLEAN PcDiskResetController(ULONG DriveNumber)
 {
        REGS    RegsIn;
        REGS    RegsOut;
 
-       DbgPrint((DPRINT_DISK, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber));
+       DPRINTM(DPRINT_DISK, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
 
        // BIOS Int 13h, function 0 - Reset disk system
        // AH = 00h
@@ -66,14 +65,14 @@ static BOOL PcDiskResetController(U32 DriveNumber)
        return INT386_SUCCESS(RegsOut);
 }
 
-static BOOL PcDiskReadLogicalSectorsLBA(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
+static BOOLEAN PcDiskReadLogicalSectorsLBA(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
 {
        REGS                                            RegsIn;
        REGS                                            RegsOut;
-       U32                                                     RetryCount;
+       ULONG                                                   RetryCount;
        PI386_DISK_ADDRESS_PACKET       Packet = (PI386_DISK_ADDRESS_PACKET)(BIOSCALLBUFFER);
 
-       DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer));
+       DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
 
        // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
        RegsIn.b.ah = 0x42;                                     // Subfunction 42h
@@ -86,10 +85,9 @@ static BOOL PcDiskReadLogicalSectorsLBA(U32 DriveNumber, U64 SectorNumber, U32 S
        Packet->PacketSize = sizeof(I386_DISK_ADDRESS_PACKET);
        Packet->Reserved = 0;
        Packet->LBABlockCount = SectorCount;
-       Packet->TransferBufferOffset = ((U32)Buffer) & 0x0F;
-       Packet->TransferBufferSegment = ((U32)Buffer) >> 4;
+       Packet->TransferBufferOffset = ((ULONG_PTR)Buffer) & 0x0F;
+       Packet->TransferBufferSegment = ((ULONG_PTR)Buffer) >> 4;
        Packet->LBAStartBlock = SectorNumber;
-       Packet->TransferBuffer64 = 0;
 
        // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
        // Return:
@@ -125,28 +123,30 @@ static BOOL PcDiskReadLogicalSectorsLBA(U32 DriveNumber, U64 SectorNumber, U32 S
        }
 
        // If we get here then the read failed
-       DiskError("Disk Read Failed", RegsOut.b.ah);
+       DiskError("Disk Read Failed in LBA mode", RegsOut.b.ah);
 
        return FALSE;
 }
 
-static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
+static BOOLEAN PcDiskReadLogicalSectorsCHS(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
 {
-       U32                     PhysicalSector;
-       U32                     PhysicalHead;
-       U32                     PhysicalTrack;
+       ULONG                   PhysicalSector;
+       ULONG                   PhysicalHead;
+       ULONG                   PhysicalTrack;
        GEOMETRY        DriveGeometry;
-       U32                     NumberOfSectorsToRead;
+       ULONG                   NumberOfSectorsToRead;
        REGS            RegsIn;
        REGS            RegsOut;
-       U32                     RetryCount;
+       ULONG                   RetryCount;
 
-       DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectorsCHS()\n"));
+       DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectorsCHS()\n");
 
        //
        // Get the drive geometry
        //
-       if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry))
+       if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry) ||
+           DriveGeometry.Sectors == 0 ||
+           DriveGeometry.Heads == 0)
        {
                return FALSE;
        }
@@ -213,8 +213,8 @@ static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 S
                RegsIn.b.cl = (PhysicalSector + ((PhysicalTrack & 0x300) >> 2));
                RegsIn.b.dh = PhysicalHead;
                RegsIn.b.dl = DriveNumber;
-               RegsIn.w.es = ((U32)Buffer) >> 4;
-               RegsIn.w.bx = ((U32)Buffer) & 0x0F;
+               RegsIn.w.es = ((ULONG_PTR)Buffer) >> 4;
+               RegsIn.w.bx = ((ULONG_PTR)Buffer) & 0x0F;
 
                //
                // Perform the read
@@ -246,7 +246,7 @@ static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 S
                // If we retried 3 times then fail
                if (RetryCount >= 3)
                {
-                       DiskError("Disk Read Failed", RegsOut.b.ah);
+                       DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut.b.ah);
                        return FALSE;
                }
 
@@ -256,7 +256,7 @@ static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 S
                // of checking the sector read count we will rely solely
                // on the carry flag being set on error
 
-               Buffer += (NumberOfSectorsToRead * DriveGeometry.BytesPerSector);
+               Buffer = (PVOID)((ULONG_PTR)Buffer + (NumberOfSectorsToRead * DriveGeometry.BytesPerSector));
                SectorCount -= NumberOfSectorsToRead;
                SectorNumber += NumberOfSectorsToRead;
        }
@@ -264,108 +264,19 @@ static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 S
        return TRUE;
 }
 
-static BOOL PcDiskInt13ExtensionsSupported(U32 DriveNumber)
+BOOLEAN PcDiskReadLogicalSectors(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
 {
-       static U32      LastDriveNumber = 0xffffffff;
-       static BOOL     LastSupported;
-       REGS    RegsIn;
-       REGS    RegsOut;
-
-       DbgPrint((DPRINT_DISK, "PcDiskInt13ExtensionsSupported()\n"));
-
-       if (DriveNumber == LastDriveNumber)
-       {
-               DbgPrint((DPRINT_DISK, "Using cached value %s for drive 0x%x\n", LastSupported ? "TRUE" : "FALSE", DriveNumber));
-               return LastSupported;
-       }
-
-       LastDriveNumber = DriveNumber;
-
-       // IBM/MS INT 13 Extensions - INSTALLATION CHECK
-       // AH = 41h
-       // BX = 55AAh
-       // DL = drive (80h-FFh)
-       // Return:
-       // CF set on error (extensions not supported)
-       // AH = 01h (invalid function)
-       // CF clear if successful
-       // BX = AA55h if installed
-       // AH = major version of extensions
-       // 01h = 1.x
-       // 20h = 2.0 / EDD-1.0
-       // 21h = 2.1 / EDD-1.1
-       // 30h = EDD-3.0
-       // AL = internal use
-       // CX = API subset support bitmap
-       // DH = extension version (v2.0+ ??? -- not present in 1.x)
-       //
-       // Bitfields for IBM/MS INT 13 Extensions API support bitmap
-       // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
-       // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
-       // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
-       //        extended drive parameter table is valid
-       // Bits 3-15 reserved
-       RegsIn.b.ah = 0x41;
-       RegsIn.w.bx = 0x55AA;
-       RegsIn.b.dl = DriveNumber;
-
-       // Reset the disk controller
-       Int386(0x13, &RegsIn, &RegsOut);
-
-       if (!INT386_SUCCESS(RegsOut))
-       {
-               // CF set on error (extensions not supported)
-               LastSupported = FALSE;
-               return FALSE;
-       }
 
-       if (RegsOut.w.bx != 0xAA55)
-       {
-               // BX = AA55h if installed
-               LastSupported = FALSE;
-               return FALSE;
-       }
-
-       // Note:
-       // The original check is too strict because some BIOSes report that
-       // extended disk access functions are not suported when booting
-       // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
-#if 0
-       if (!(RegsOut.w.cx & 0x0001))
-       {
-               // CX = API subset support bitmap
-               // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
-               LastSupported = FALSE;
-               return FALSE;
-       }
-#endif
-
-       // Use this relaxed check instead (most BIOSes seem to use 0x9f as CD-Rom)
-       if (RegsOut.w.cx == 0x0000 && DriveNumber != 0x9f)
-       {
-               // CX = API subset support bitmap
-               printf("Suspicious API subset support bitmap 0x%x on device 0x%x\n", RegsOut.w.cx, DriveNumber);
-               LastSupported = FALSE;
-               return LastSupported;
-       }
-
-       LastSupported = TRUE;
-       return TRUE;
-}
-
-BOOL PcDiskReadLogicalSectors(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
-{
-
-       DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer));
+       DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
 
        //
        // Check to see if it is a fixed disk drive
        // If so then check to see if Int13 extensions work
        // If they do then use them, otherwise default back to BIOS calls
        //
-       if ((DriveNumber >= 0x80) && PcDiskInt13ExtensionsSupported(DriveNumber))
+       if ((DriveNumber >= 0x80) && DiskInt13ExtensionsSupported(DriveNumber))
        {
-               DbgPrint((DPRINT_DISK, "Using Int 13 Extensions for read. PcDiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, PcDiskInt13ExtensionsSupported(DriveNumber) ? "TRUE" : "FALSE"));
+               DPRINTM(DPRINT_DISK, "Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, DiskInt13ExtensionsSupported(DriveNumber) ? "TRUE" : "FALSE");
 
                //
                // LBA is easy, nothing to calculate
@@ -382,21 +293,14 @@ BOOL PcDiskReadLogicalSectors(U32 DriveNumber, U64 SectorNumber, U32 SectorCount
        return TRUE;
 }
 
-BOOL
-PcDiskGetPartitionEntry(U32 DriveNumber, U32 PartitionNumber, PPARTITION_TABLE_ENTRY PartitionTableEntry)
-{
-  /* Just use the standard routine */
-  return DiskGetPartitionEntry(DriveNumber, PartitionNumber, PartitionTableEntry);
-}
-
-BOOL
-PcDiskGetDriveGeometry(U32 DriveNumber, PGEOMETRY Geometry)
+BOOLEAN
+PcDiskGetDriveGeometry(ULONG DriveNumber, PGEOMETRY Geometry)
 {
   REGS RegsIn;
   REGS RegsOut;
-  U32 Cylinders;
+  ULONG Cylinders;
 
-  DbgPrint((DPRINT_DISK, "DiskGetDriveGeometry()\n"));
+  DPRINTM(DPRINT_DISK, "DiskGetDriveGeometry()\n");
 
   /* BIOS Int 13h, function 08h - Get drive parameters
    * AH = 08h
@@ -440,8 +344,8 @@ PcDiskGetDriveGeometry(U32 DriveNumber, PGEOMETRY Geometry)
   return TRUE;
 }
 
-U32
-PcDiskGetCacheableBlockCount(U32 DriveNumber)
+ULONG
+PcDiskGetCacheableBlockCount(ULONG DriveNumber)
 {
   GEOMETRY     Geometry;