Added new 'bootcd' target.
[reactos.git] / freeldr / freeldr / disk / disk.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <freeldr.h>
21 #include <disk.h>
22 #include <fs.h>
23 #include <rtl.h>
24 #include <ui.h>
25 #include <arch.h>
26 #include <debug.h>
27
28
29 /////////////////////////////////////////////////////////////////////////////////////////////
30 // FUNCTIONS
31 /////////////////////////////////////////////////////////////////////////////////////////////
32
33 VOID DiskError(PUCHAR ErrorString)
34 {
35 UCHAR ErrorCodeString[80];
36
37 sprintf(ErrorCodeString, "%s\nError Code: 0x%x", ErrorString, BiosInt13GetLastErrorCode());
38
39 DbgPrint((DPRINT_DISK, "%s\n", ErrorCodeString));
40
41 UiMessageBox(ErrorCodeString);
42 }
43
44 BOOL DiskReadLogicalSectors(U32 DriveNumber, U32 SectorNumber, U32 SectorCount, PVOID Buffer)
45 {
46 U32 PhysicalSector;
47 U32 PhysicalHead;
48 U32 PhysicalTrack;
49 GEOMETRY DriveGeometry;
50 U32 NumberOfSectorsToRead;
51
52 DbgPrint((DPRINT_DISK, "ReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer));
53
54 //
55 // Check to see if it is a fixed disk drive
56 // If so then check to see if Int13 extensions work
57 // If they do then use them, otherwise default back to BIOS calls
58 //
59 if ((DriveNumber >= 0x80) && (IsSetupLdr || BiosInt13ExtensionsSupported(DriveNumber)))
60 {
61 DbgPrint((DPRINT_DISK, "Using Int 13 Extensions for read. BiosInt13ExtensionsSupported(%d) = %s\n", DriveNumber, BiosInt13ExtensionsSupported(DriveNumber) ? "TRUE" : "FALSE"));
62
63 //
64 // LBA is easy, nothing to calculate
65 // Just do the read
66 //
67 if (!BiosInt13ReadExtended(DriveNumber, SectorNumber, SectorCount, Buffer))
68 {
69 DiskError("Disk read error.");
70 return FALSE;
71 }
72 }
73 else
74 {
75 //
76 // Get the drive geometry
77 //
78 if (!DiskGetDriveGeometry(DriveNumber, &DriveGeometry))
79 {
80 return FALSE;
81 }
82
83 while (SectorCount)
84 {
85
86 //
87 // Calculate the physical disk offsets
88 //
89 PhysicalSector = 1 + (SectorNumber % DriveGeometry.Sectors);
90 PhysicalHead = (SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads;
91 PhysicalTrack = (SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads;
92
93 //
94 // Calculate how many sectors we are supposed to read
95 //
96 if (PhysicalSector > 1)
97 {
98 if (SectorCount >= (DriveGeometry.Sectors - (PhysicalSector - 1)))
99 NumberOfSectorsToRead = (DriveGeometry.Sectors - (PhysicalSector - 1));
100 else
101 NumberOfSectorsToRead = SectorCount;
102 }
103 else
104 {
105 if (SectorCount >= DriveGeometry.Sectors)
106 NumberOfSectorsToRead = DriveGeometry.Sectors;
107 else
108 NumberOfSectorsToRead = SectorCount;
109 }
110
111 DbgPrint((DPRINT_DISK, "Calling BiosInt13Read() with PhysicalHead: %d\n", PhysicalHead));
112 DbgPrint((DPRINT_DISK, "Calling BiosInt13Read() with PhysicalTrack: %d\n", PhysicalTrack));
113 DbgPrint((DPRINT_DISK, "Calling BiosInt13Read() with PhysicalSector: %d\n", PhysicalSector));
114 DbgPrint((DPRINT_DISK, "Calling BiosInt13Read() with NumberOfSectorsToRead: %d\n", NumberOfSectorsToRead));
115
116 //
117 // Make sure the read is within the geometry boundaries
118 //
119 if ((PhysicalHead >= DriveGeometry.Heads) ||
120 (PhysicalTrack >= DriveGeometry.Cylinders) ||
121 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.Sectors + 1)) ||
122 (PhysicalSector > DriveGeometry.Sectors))
123 {
124 DiskError("Disk read exceeds drive geometry limits.");
125 return FALSE;
126 }
127
128 //
129 // Perform the read
130 //
131 if (!BiosInt13Read(DriveNumber, PhysicalHead, PhysicalTrack, PhysicalSector, NumberOfSectorsToRead, Buffer))
132 {
133 DiskError("Disk read error.");
134 return FALSE;
135 }
136
137 Buffer += (NumberOfSectorsToRead * DriveGeometry.BytesPerSector);
138 SectorCount -= NumberOfSectorsToRead;
139 SectorNumber += NumberOfSectorsToRead;
140 }
141 }
142
143 return TRUE;
144 }