3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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.
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.
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.
28 BOOL DriveMapInstalled
= FALSE
; // Tells us if we have already installed our drive map int 13h handler code
29 ULONG OldInt13HandlerAddress
= 0; // Address of BIOS int 13h handler
30 ULONG DriveMapHandlerAddress
= 0; // Linear address of our drive map handler
31 ULONG DriveMapHandlerSegOff
= 0; // Segment:offset style address of our drive map handler
33 VOID
DriveMapMapDrivesInSection(PUCHAR SectionName
)
35 UCHAR SettingName
[80];
36 UCHAR SettingValue
[80];
41 ULONG SectionItemCount
;
44 DRIVE_MAP_LIST DriveMapList
;
46 RtlZeroMemory(&DriveMapList
, sizeof(DRIVE_MAP_LIST
));
48 if (!IniOpenSection(SectionName
, &SectionId
))
53 // Get the number of items in this section
54 SectionItemCount
= IniGetNumSectionItems(SectionId
);
56 // Loop through each one and check if its a DriveMap= setting
57 for (Index
=0; Index
<SectionItemCount
; Index
++)
59 // Get the next setting from the .ini file section
60 if (IniReadSettingByNumber(SectionId
, Index
, SettingName
, 80, SettingValue
, 80))
62 if (stricmp(SettingName
, "DriveMap") == 0)
64 // Make sure we haven't exceeded the drive map max count
65 if (DriveMapList
.DriveMapCount
>= 4)
67 sprintf(ErrorText
, "Max DriveMap count exceeded in section [%s]:\n\n%s=%s", SectionName
, SettingName
, SettingValue
);
68 UiMessageBox(ErrorText
);
72 RtlZeroMemory(Drive1
, 80);
73 RtlZeroMemory(Drive2
, 80);
75 strcpy(Drive1
, SettingValue
);
77 // Parse the setting value and separate a string "hd0,hd1"
78 // into two strings "hd0" and "hd1"
79 for (Index2
=0; Index2
<strlen(Drive1
); Index2
++)
81 // Check if this character is the separater character (comma - ',')
82 if (Drive1
[Index2
] == ',')
84 Drive1
[Index2
] = '\0';
85 strcpy(Drive2
, &Drive1
[Index2
+1]);
90 // Make sure we got good values before we add them to the map
91 if (!DriveMapIsValidDriveString(Drive1
) || !DriveMapIsValidDriveString(Drive2
))
93 sprintf(ErrorText
, "Error in DriveMap setting in section [%s]:\n\n%s=%s", SectionName
, SettingName
, SettingValue
);
94 UiMessageBox(ErrorText
);
98 // Add them to the map
99 DriveMapList
.DriveMap
[(DriveMapList
.DriveMapCount
* 2)] = DriveMapGetBiosDriveNumber(Drive1
);
100 DriveMapList
.DriveMap
[(DriveMapList
.DriveMapCount
* 2)+1] = DriveMapGetBiosDriveNumber(Drive2
);
101 DriveMapList
.DriveMapCount
++;
103 DbgPrint((DPRINT_WARNING
, "Mapping BIOS drive 0x%x to drive 0x%x\n", DriveMapGetBiosDriveNumber(Drive1
), DriveMapGetBiosDriveNumber(Drive2
)));
108 if (DriveMapList
.DriveMapCount
)
110 DbgPrint((DPRINT_WARNING
, "Installing Int13 drive map for %d drives.\n", DriveMapList
.DriveMapCount
));
111 DriveMapInstallInt13Handler(&DriveMapList
);
115 DbgPrint((DPRINT_WARNING
, "Removing any previously installed Int13 drive map.\n"));
116 DriveMapRemoveInt13Handler();
120 BOOL
DriveMapIsValidDriveString(PUCHAR DriveString
)
124 // Now verify that the user has given us appropriate strings
125 if ((strlen(DriveString
) < 3) ||
126 ((DriveString
[0] != 'f') && (DriveString
[0] != 'F') && (DriveString
[0] != 'h') && (DriveString
[0] != 'H')) ||
127 ((DriveString
[1] != 'd') && (DriveString
[1] != 'D')))
132 // Now verify that the user has given us appropriate numbers
133 // Make sure that only numeric characters were given
134 for (Index
=2; Index
<strlen(DriveString
); Index
++)
136 if (DriveString
[Index
] < '0' || DriveString
[Index
] > '9')
141 // Now make sure that they are not outrageous values (i.e. hd90874)
142 if ((atoi(&DriveString
[2]) < 0) || (atoi(&DriveString
[2]) > 0xff))
150 ULONG
DriveMapGetBiosDriveNumber(PUCHAR DeviceName
)
152 ULONG BiosDriveNumber
= 0;
154 // Convert the drive number string into a number
156 BiosDriveNumber
= atoi(&DeviceName
[2]);
158 // If it's a hard disk then set the high bit
159 if ((DeviceName
[0] == 'h' || DeviceName
[0] == 'H') &&
160 (DeviceName
[1] == 'd' || DeviceName
[1] == 'D'))
162 BiosDriveNumber
|= 0x80;
165 return BiosDriveNumber
;
168 VOID
DriveMapInstallInt13Handler(PDRIVE_MAP_LIST DriveMap
)
170 PDWORD RealModeIVT
= (PULONG
)0x00000000;
171 PWORD BiosLowMemorySize
= (PWORD
)0x00000413;
173 if (!DriveMapInstalled
)
175 // Get the old INT 13h handler address from the vector table
176 OldInt13HandlerAddress
= RealModeIVT
[0x13];
178 // Decrease the size of low memory
179 (*BiosLowMemorySize
)--;
181 // Get linear address for drive map handler
182 DriveMapHandlerAddress
= (ULONG
)(*BiosLowMemorySize
) << 10;
184 // Convert to segment:offset style address
185 DriveMapHandlerSegOff
= (DriveMapHandlerAddress
<< 12) & 0xffff0000;
188 // Copy the drive map structure to the proper place
189 RtlCopyMemory(&DriveMapInt13HandlerMapList
, DriveMap
, sizeof(DRIVE_MAP_LIST
));
191 // Set the address of the BIOS INT 13h handler
192 DriveMapOldInt13HandlerAddress
= OldInt13HandlerAddress
;
194 // Copy the code to our reserved area
195 RtlCopyMemory((PVOID
)DriveMapHandlerAddress
, &DriveMapInt13HandlerStart
, ((ULONG
)&DriveMapInt13HandlerEnd
- (ULONG
)&DriveMapInt13HandlerStart
));
198 RealModeIVT
[0x13] = DriveMapHandlerSegOff
;
200 //CacheInvalidateCacheData();
201 DriveMapInstalled
= TRUE
;
204 VOID
DriveMapRemoveInt13Handler(VOID
)
206 PDWORD RealModeIVT
= (PULONG
)0x00000000;
207 PWORD BiosLowMemorySize
= (PWORD
)0x00000413;
209 if (DriveMapInstalled
)
211 // Get the old INT 13h handler address from the vector table
212 RealModeIVT
[0x13] = OldInt13HandlerAddress
;
214 // Increase the size of low memory
215 (*BiosLowMemorySize
)++;
217 DriveMapInstalled
= FALSE
;