- Merge from trunk up to r45543
[reactos.git] / boot / freeldr / freeldr / arch / i386 / drivemap.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <freeldr.h>
21 #include <debug.h>
22
23 BOOLEAN DriveMapInstalled = FALSE; // Tells us if we have already installed our drive map int 13h handler code
24 ULONG OldInt13HandlerAddress = 0; // Address of BIOS int 13h handler
25 ULONG DriveMapHandlerAddress = 0; // Linear address of our drive map handler
26 ULONG DriveMapHandlerSegOff = 0; // Segment:offset style address of our drive map handler
27
28 VOID DriveMapMapDrivesInSection(PCSTR SectionName)
29 {
30 CHAR SettingName[80];
31 CHAR SettingValue[80];
32 CHAR ErrorText[260];
33 CHAR Drive1[80];
34 CHAR Drive2[80];
35 ULONG SectionId;
36 ULONG SectionItemCount;
37 ULONG Index;
38 ULONG Index2;
39 DRIVE_MAP_LIST DriveMapList;
40
41 RtlZeroMemory(&DriveMapList, sizeof(DRIVE_MAP_LIST));
42
43 if (!IniOpenSection(SectionName, &SectionId))
44 {
45 return;
46 }
47
48 // Get the number of items in this section
49 SectionItemCount = IniGetNumSectionItems(SectionId);
50
51 // Loop through each one and check if its a DriveMap= setting
52 for (Index=0; Index<SectionItemCount; Index++)
53 {
54 // Get the next setting from the .ini file section
55 if (IniReadSettingByNumber(SectionId, Index, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue)))
56 {
57 if (_stricmp(SettingName, "DriveMap") == 0)
58 {
59 // Make sure we haven't exceeded the drive map max count
60 if (DriveMapList.DriveMapCount >= 4)
61 {
62 sprintf(ErrorText, "Max DriveMap count exceeded in section [%s]:\n\n%s=%s", SectionName, SettingName, SettingValue);
63 UiMessageBox(ErrorText);
64 continue;
65 }
66
67 RtlZeroMemory(Drive1, 80);
68 RtlZeroMemory(Drive2, 80);
69
70 strcpy(Drive1, SettingValue);
71
72 // Parse the setting value and separate a string "hd0,hd1"
73 // into two strings "hd0" and "hd1"
74 for (Index2=0; Index2<strlen(Drive1); Index2++)
75 {
76 // Check if this character is the separater character (comma - ',')
77 if (Drive1[Index2] == ',')
78 {
79 Drive1[Index2] = '\0';
80 strcpy(Drive2, &Drive1[Index2+1]);
81 break;
82 }
83 }
84
85 // Make sure we got good values before we add them to the map
86 if (!DriveMapIsValidDriveString(Drive1) || !DriveMapIsValidDriveString(Drive2))
87 {
88 sprintf(ErrorText, "Error in DriveMap setting in section [%s]:\n\n%s=%s", SectionName, SettingName, SettingValue);
89 UiMessageBox(ErrorText);
90 continue;
91 }
92
93 // Add them to the map
94 DriveMapList.DriveMap[(DriveMapList.DriveMapCount * 2)] = DriveMapGetBiosDriveNumber(Drive1);
95 DriveMapList.DriveMap[(DriveMapList.DriveMapCount * 2)+1] = DriveMapGetBiosDriveNumber(Drive2);
96 DriveMapList.DriveMapCount++;
97
98 DPRINTM(DPRINT_WARNING, "Mapping BIOS drive 0x%x to drive 0x%x\n", DriveMapGetBiosDriveNumber(Drive1), DriveMapGetBiosDriveNumber(Drive2));
99 }
100 }
101 }
102
103 if (DriveMapList.DriveMapCount)
104 {
105 DPRINTM(DPRINT_WARNING, "Installing Int13 drive map for %d drives.\n", DriveMapList.DriveMapCount);
106 DriveMapInstallInt13Handler(&DriveMapList);
107 }
108 else
109 {
110 DPRINTM(DPRINT_WARNING, "Removing any previously installed Int13 drive map.\n");
111 DriveMapRemoveInt13Handler();
112 }
113 }
114
115 BOOLEAN DriveMapIsValidDriveString(PCSTR DriveString)
116 {
117 ULONG Index;
118
119 // Now verify that the user has given us appropriate strings
120 if ((strlen(DriveString) < 3) ||
121 ((DriveString[0] != 'f') && (DriveString[0] != 'F') && (DriveString[0] != 'h') && (DriveString[0] != 'H')) ||
122 ((DriveString[1] != 'd') && (DriveString[1] != 'D')))
123 {
124 return FALSE;
125 }
126
127 // Now verify that the user has given us appropriate numbers
128 // Make sure that only numeric characters were given
129 for (Index=2; Index<strlen(DriveString); Index++)
130 {
131 if (DriveString[Index] < '0' || DriveString[Index] > '9')
132 {
133 return FALSE;
134 }
135 }
136 // Now make sure that they are not outrageous values (i.e. hd90874)
137 if ((atoi(&DriveString[2]) < 0) || (atoi(&DriveString[2]) > 0xff))
138 {
139 return FALSE;
140 }
141
142 return TRUE;
143 }
144
145 ULONG DriveMapGetBiosDriveNumber(PCSTR DeviceName)
146 {
147 ULONG BiosDriveNumber = 0;
148
149 // If they passed in a number string then just
150 // convert it to decimal and return it
151 if (DeviceName[0] >= '0' && DeviceName[0] <= '9')
152 {
153 return atoi(DeviceName);
154 }
155
156 // Convert the drive number string into a number
157 // 'hd1' = 1
158 BiosDriveNumber = atoi(&DeviceName[2]);
159
160 // If it's a hard disk then set the high bit
161 if ((DeviceName[0] == 'h' || DeviceName[0] == 'H') &&
162 (DeviceName[1] == 'd' || DeviceName[1] == 'D'))
163 {
164 BiosDriveNumber |= 0x80;
165 }
166
167 return BiosDriveNumber;
168 }
169
170 VOID DriveMapInstallInt13Handler(PDRIVE_MAP_LIST DriveMap)
171 {
172 ULONG* RealModeIVT = (ULONG*)0x00000000;
173 USHORT* BiosLowMemorySize = (USHORT*)0x00000413;
174
175 if (!DriveMapInstalled)
176 {
177 // Get the old INT 13h handler address from the vector table
178 OldInt13HandlerAddress = RealModeIVT[0x13];
179
180 // Decrease the size of low memory
181 (*BiosLowMemorySize)--;
182
183 // Get linear address for drive map handler
184 DriveMapHandlerAddress = (ULONG)(*BiosLowMemorySize) << 10;
185
186 // Convert to segment:offset style address
187 DriveMapHandlerSegOff = (DriveMapHandlerAddress << 12) & 0xffff0000;
188 }
189
190 // Copy the drive map structure to the proper place
191 RtlCopyMemory(&DriveMapInt13HandlerMapList, DriveMap, sizeof(DRIVE_MAP_LIST));
192
193 // Set the address of the BIOS INT 13h handler
194 DriveMapOldInt13HandlerAddress = OldInt13HandlerAddress;
195
196 // Copy the code to our reserved area
197 RtlCopyMemory((PVOID)DriveMapHandlerAddress, &DriveMapInt13HandlerStart, ((ULONG)&DriveMapInt13HandlerEnd - (ULONG)&DriveMapInt13HandlerStart));
198
199 // Update the IVT
200 RealModeIVT[0x13] = DriveMapHandlerSegOff;
201
202 CacheInvalidateCacheData();
203 DriveMapInstalled = TRUE;
204 }
205
206 VOID DriveMapRemoveInt13Handler(VOID)
207 {
208 ULONG* RealModeIVT = (ULONG*)0x00000000;
209 USHORT* BiosLowMemorySize = (USHORT*)0x00000413;
210
211 if (DriveMapInstalled)
212 {
213 // Get the old INT 13h handler address from the vector table
214 RealModeIVT[0x13] = OldInt13HandlerAddress;
215
216 // Increase the size of low memory
217 (*BiosLowMemorySize)++;
218
219 CacheInvalidateCacheData();
220 DriveMapInstalled = FALSE;
221 }
222 }