3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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.
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.
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.
29 /////////////////////////////////////////////////////////////////////////////////////////////
31 /////////////////////////////////////////////////////////////////////////////////////////////
33 VOID
DiskError(PUCHAR ErrorString
)
35 UCHAR ErrorCodeString
[80];
37 sprintf(ErrorCodeString
, "%s\nError Code: 0x%x", ErrorString
, BiosInt13GetLastErrorCode());
39 DbgPrint((DPRINT_DISK
, "%s\n", ErrorCodeString
));
43 MessageBox(ErrorCodeString
);
47 printf("%s", ErrorCodeString
);
48 printf("\nPress any key\n");
53 BOOL
DiskReadLogicalSectors(ULONG DriveNumber
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
58 GEOMETRY DriveGeometry
;
59 ULONG NumberOfSectorsToRead
;
61 DbgPrint((DPRINT_DISK
, "ReadLogicalSector() DriveNumber: 0x%x SectorNumber: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, Buffer
));
64 // Check to see if it is a fixed disk drive
65 // If so then check to see if Int13 extensions work
66 // If they do then use them, otherwise default back to BIOS calls
68 if ((DriveNumber
>= 0x80) && (BiosInt13ExtensionsSupported(DriveNumber
)))
70 DbgPrint((DPRINT_DISK
, "Using Int 13 Extensions for read. BiosInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, BiosInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE"));
73 // LBA is easy, nothing to calculate
76 if (!BiosInt13ReadExtended(DriveNumber
, SectorNumber
, 1, Buffer
))
78 DiskError("Disk read error.");
85 // Get the drive geometry
87 if (!DiskGetDriveGeometry(DriveNumber
, &DriveGeometry
))
96 // Calculate the physical disk offsets
98 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
99 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
100 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
103 // Calculate how many sectors we are supposed to read
105 if (PhysicalSector
> 1)
107 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
108 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
110 NumberOfSectorsToRead
= SectorCount
;
114 if (SectorCount
>= DriveGeometry
.Sectors
)
115 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
117 NumberOfSectorsToRead
= SectorCount
;
120 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalHead: %d\n", PhysicalHead
));
121 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalTrack: %d\n", PhysicalTrack
));
122 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalSector: %d\n", PhysicalSector
));
123 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with NumberOfSectorsToRead: %d\n", NumberOfSectorsToRead
));
126 // Make sure the read is within the geometry boundaries
128 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
129 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
130 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
131 (PhysicalSector
> DriveGeometry
.Sectors
))
133 DiskError("Disk read exceeds drive geometry limits.");
140 if (!BiosInt13Read(DriveNumber
, PhysicalHead
, PhysicalTrack
, PhysicalSector
, NumberOfSectorsToRead
, Buffer
))
142 DiskError("Disk read error.");
146 Buffer
+= (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
);
147 SectorCount
-= NumberOfSectorsToRead
;
148 SectorNumber
+= NumberOfSectorsToRead
;