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