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