Re-organized things a slight bit.
[reactos.git] / freeldr / freeldr / disk / disk.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000, 2001 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 <asmcode.h>
26 #include <debug.h>
27
28
29 /////////////////////////////////////////////////////////////////////////////////////////////
30 // DATA
31 /////////////////////////////////////////////////////////////////////////////////////////////
32
33 GEOMETRY DriveGeometry;
34 ULONG VolumeHiddenSectors;
35 ULONG CurrentlyOpenDriveNumber;
36
37 /////////////////////////////////////////////////////////////////////////////////////////////
38 // FUNCTIONS
39 /////////////////////////////////////////////////////////////////////////////////////////////
40
41 VOID DiskError(PUCHAR ErrorString)
42 {
43 DbgPrint((DPRINT_DISK, "%s\n", ErrorString));
44
45 if (UserInterfaceUp)
46 {
47 MessageBox(ErrorString);
48 }
49 else
50 {
51 printf("%s", ErrorString);
52 printf("\nPress any key\n");
53 getch();
54 }
55 }
56
57 VOID DiskSetDriveGeometry(ULONG Cylinders, ULONG Heads, ULONG Sectors, ULONG BytesPerSector)
58 {
59 DriveGeometry.Cylinders = Cylinders;
60 DriveGeometry.Heads = Heads;
61 DriveGeometry.Sectors = Sectors;
62 DriveGeometry.BytesPerSector = BytesPerSector;
63
64 DbgPrint((DPRINT_DISK, "DriveGeometry.Cylinders: %d\n", DriveGeometry.Cylinders));
65 DbgPrint((DPRINT_DISK, "DriveGeometry.Heads: %d\n", DriveGeometry.Heads));
66 DbgPrint((DPRINT_DISK, "DriveGeometry.Sectors: %d\n", DriveGeometry.Sectors));
67 DbgPrint((DPRINT_DISK, "DriveGeometry.BytesPerSector: %d\n", DriveGeometry.BytesPerSector));
68 }
69
70 VOID DiskSetVolumeProperties(ULONG HiddenSectors)
71 {
72 VolumeHiddenSectors = HiddenSectors;
73 }
74
75 BOOL DiskReadMultipleLogicalSectors(ULONG SectorNumber, ULONG SectorCount, PVOID Buffer)
76 {
77 /*BOOL bRetVal;
78 int PhysicalSector;
79 int PhysicalHead;
80 int PhysicalTrack;
81 int nNum;
82
83 nSect += nHiddenSectors;
84
85 while (nNumberOfSectors)
86 {
87 PhysicalSector = 1 + (nSect % nSectorsPerTrack);
88 PhysicalHead = (nSect / nSectorsPerTrack) % nNumberOfHeads;
89 PhysicalTrack = nSect / (nSectorsPerTrack * nNumberOfHeads);
90
91 if (PhysicalSector > 1)
92 {
93 if (nNumberOfSectors >= (nSectorsPerTrack - (PhysicalSector - 1)))
94 nNum = (nSectorsPerTrack - (PhysicalSector - 1));
95 else
96 nNum = nNumberOfSectors;
97 }
98 else
99 {
100 if (nNumberOfSectors >= nSectorsPerTrack)
101 nNum = nSectorsPerTrack;
102 else
103 nNum = nNumberOfSectors;
104 }
105
106 bRetVal = biosdisk(CurrentlyOpenDriveNumber, PhysicalHead, PhysicalTrack, PhysicalSector, nNum, pBuffer);
107
108 if (!bRetVal)
109 {
110 FS_DO_ERROR("Disk Error");
111 return FALSE;
112 }
113
114 pBuffer += (nNum * 512);
115 nNumberOfSectors -= nNum;
116 nSect += nNum;
117 }*/
118
119 ULONG CurrentSector;
120 PVOID RealBuffer = Buffer;
121
122 for (CurrentSector=SectorNumber; CurrentSector<(SectorNumber + SectorCount); CurrentSector++)
123 {
124 if (!DiskReadLogicalSector(CurrentSector, RealBuffer) )
125 {
126 return FALSE;
127 }
128
129 RealBuffer += DriveGeometry.BytesPerSector;
130 }
131
132 return TRUE;
133 }
134
135 BOOL DiskReadLogicalSector(ULONG SectorNumber, PVOID Buffer)
136 {
137 ULONG PhysicalSector;
138 ULONG PhysicalHead;
139 ULONG PhysicalTrack;
140
141 DbgPrint((DPRINT_DISK, "ReadLogicalSector() SectorNumber: %d Buffer: 0x%x\n", SectorNumber, Buffer));
142
143 SectorNumber += VolumeHiddenSectors;
144 PhysicalSector = 1 + (SectorNumber % DriveGeometry.Sectors);
145 PhysicalHead = (SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads;
146 PhysicalTrack = (SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads;
147
148 //DbgPrint((DPRINT_FILESYSTEM, "Calling BiosInt13Read() with PhysicalHead: %d\n", PhysicalHead));
149 //DbgPrint((DPRINT_FILESYSTEM, "Calling BiosInt13Read() with PhysicalTrack: %d\n", PhysicalTrack));
150 //DbgPrint((DPRINT_FILESYSTEM, "Calling BiosInt13Read() with PhysicalSector: %d\n", PhysicalSector));
151 if (PhysicalHead >= DriveGeometry.Heads)
152 {
153 BugCheck((DPRINT_DISK, "PhysicalHead >= DriveGeometry.Heads\nPhysicalHead = %d\nDriveGeometry.Heads = %d\n", PhysicalHead, DriveGeometry.Heads));
154 }
155 if (PhysicalTrack >= DriveGeometry.Cylinders)
156 {
157 BugCheck((DPRINT_DISK, "PhysicalTrack >= DriveGeometry.Cylinders\nPhysicalTrack = %d\nDriveGeometry.Cylinders = %d\n", PhysicalTrack, DriveGeometry.Cylinders));
158 }
159 if (PhysicalSector > DriveGeometry.Sectors)
160 {
161 BugCheck((DPRINT_DISK, "PhysicalSector > DriveGeometry.Sectors\nPhysicalSector = %d\nDriveGeometry.Sectors = %d\n", PhysicalSector, DriveGeometry.Sectors));
162 }
163
164 //
165 // Check to see if it is a fixed disk drive
166 // If so then check to see if Int13 extensions work
167 // If they do then use them, otherwise default back to BIOS calls
168 //
169 if ((CurrentlyOpenDriveNumber >= 0x80) && (BiosInt13ExtensionsSupported(CurrentlyOpenDriveNumber)) && (SectorNumber > (DriveGeometry.Cylinders * DriveGeometry.Heads * DriveGeometry.Sectors)))
170 {
171 DbgPrint((DPRINT_DISK, "Using Int 13 Extensions for read. BiosInt13ExtensionsSupported(%d) = %s\n", CurrentlyOpenDriveNumber, BiosInt13ExtensionsSupported(CurrentlyOpenDriveNumber) ? "TRUE" : "FALSE"));
172 if ( !BiosInt13ReadExtended(CurrentlyOpenDriveNumber, SectorNumber, 1, Buffer) )
173 {
174 DiskError("Disk read error.");
175 return FALSE;
176 }
177 }
178 else
179 {
180 if ( !BiosInt13Read(CurrentlyOpenDriveNumber, PhysicalHead, PhysicalTrack, PhysicalSector, 1, Buffer) )
181 {
182 DiskError("Disk read error.");
183 return FALSE;
184 }
185 }
186
187 return TRUE;
188 }