3 * Copyright (C) 1999, 2000, 2001 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 DbgPrint((DPRINT_DISK
, "%s\n", ErrorString
));
39 MessageBox(ErrorString
);
43 printf("%s", ErrorString
);
44 printf("\nPress any key\n");
49 BOOL
DiskReadMultipleLogicalSectors(ULONG DriveNumber
, ULONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
54 GEOMETRY DriveGeometry
;
55 ULONG NumberOfSectorsToRead
;
57 DbgPrint((DPRINT_DISK
, "ReadLogicalSector() DriveNumber: 0x%x SectorNumber: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, Buffer
));
60 // Check to see if it is a fixed disk drive
61 // If so then check to see if Int13 extensions work
62 // If they do then use them, otherwise default back to BIOS calls
64 if ((DriveNumber
>= 0x80) && (BiosInt13ExtensionsSupported(DriveNumber
)))
66 DbgPrint((DPRINT_DISK
, "Using Int 13 Extensions for read. BiosInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, BiosInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE"));
69 // LBA is easy, nothing to calculate
72 if (!BiosInt13ReadExtended(DriveNumber
, SectorNumber
, 1, Buffer
))
74 DiskError("Disk read error.");
81 // Get the drive geometry
83 if (!DiskGetDriveGeometry(DriveNumber
, &DriveGeometry
))
92 // Calculate the physical disk offsets
94 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
95 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
96 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
99 // Calculate how many sectors we are supposed to read
101 if (PhysicalSector
> 1)
103 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
104 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
106 NumberOfSectorsToRead
= SectorCount
;
110 if (SectorCount
>= DriveGeometry
.Sectors
)
111 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
113 NumberOfSectorsToRead
= SectorCount
;
116 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalHead: %d\n", PhysicalHead
));
117 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalTrack: %d\n", PhysicalTrack
));
118 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalSector: %d\n", PhysicalSector
));
119 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with NumberOfSectorsToRead: %d\n", NumberOfSectorsToRead
));
122 // Make sure the read is within the geometry boundaries
124 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
125 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
126 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
127 (PhysicalSector
> DriveGeometry
.Sectors
))
129 DiskError("Disk read exceeds drive geometry limits.");
136 if (!BiosInt13Read(DriveNumber
, PhysicalHead
, PhysicalTrack
, PhysicalSector
, NumberOfSectorsToRead
, Buffer
))
138 DiskError("Disk read error.");
142 Buffer
+= (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
);
143 SectorCount
-= NumberOfSectorsToRead
;
144 SectorNumber
+= NumberOfSectorsToRead
;
151 BOOL
DiskReadLogicalSector(ULONG DriveNumber
, ULONG SectorNumber
, PVOID Buffer
)
153 ULONG PhysicalSector
;
156 GEOMETRY DriveGeometry
;
158 DbgPrint((DPRINT_DISK
, "ReadLogicalSector() DriveNumber: 0x%x SectorNumber: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, Buffer
));
161 // Check to see if it is a fixed disk drive
162 // If so then check to see if Int13 extensions work
163 // If they do then use them, otherwise default back to BIOS calls
165 if ((DriveNumber
>= 0x80) && (BiosInt13ExtensionsSupported(DriveNumber
)))
167 DbgPrint((DPRINT_DISK
, "Using Int 13 Extensions for read. BiosInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, BiosInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE"));
170 // LBA is easy, nothing to calculate
173 if (!BiosInt13ReadExtended(DriveNumber
, SectorNumber
, 1, Buffer
))
175 DiskError("Disk read error.");
182 // Get the drive geometry
184 if (!DiskGetDriveGeometry(DriveNumber
, &DriveGeometry
))
190 // Calculate the physical disk offsets
192 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
193 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
194 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
196 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalHead: %d\n", PhysicalHead
));
197 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalTrack: %d\n", PhysicalTrack
));
198 DbgPrint((DPRINT_DISK
, "Calling BiosInt13Read() with PhysicalSector: %d\n", PhysicalSector
));
201 // Make sure the read is within the geometry boundaries
203 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
204 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
205 (PhysicalSector
> DriveGeometry
.Sectors
))
207 DiskError("Disk read exceeds drive geometry limits.");
214 if (!BiosInt13Read(DriveNumber
, PhysicalHead
, PhysicalTrack
, PhysicalSector
, 1, Buffer
))
216 DiskError("Disk read error.");