forked from TalAloni/IntegrateDrv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
353 lines (322 loc) · 12.5 KB
/
Copy pathProgram.cs
File metadata and controls
353 lines (322 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using IntegrateDrv.DeviceService;
using IntegrateDrv.Integrators;
using IntegrateDrv.PNPDriver;
using IntegrateDrv.TextModeDriver;
using IntegrateDrv.Utilities;
using IntegrateDrv.Utilities.FileSystem;
using IntegrateDrv.WindowsDirectory;
namespace IntegrateDrv
{
public static class Program
{
static void Main()
{
var args = CommandLineParser.GetCommandLineArgsIgnoreEscape();
WindowsInstallation installation;
List<TextModeDriverDirectory> textModeDriverDirectories;
List<PNPDriverDirectory> pnpDriverDirectories;
bool useLocalHardwareConfig;
string enumExportPath;
bool preconfigure;
IPAddress staticIP;
IPAddress staticSubnetMask;
IPAddress staticGateway;
bool usbBoot;
var parseSuccess = ParseCommandLineSwitches(args, out installation, out textModeDriverDirectories, out pnpDriverDirectories, out useLocalHardwareConfig, out enumExportPath, out preconfigure, out staticIP, out staticSubnetMask, out staticGateway, out usbBoot);
if (!parseSuccess)
// parser was already supposed to print an error meesage;
return;
// Make sure Windows temporary folder exist (the user may have deleted it)
if (!FileSystemUtils.IsDirectoryExist(Path.GetTempPath()))
FileSystemUtils.CreateDirectory(Path.GetTempPath());
installation.SetupRegistryHive.LoadHiveFromDirectory(installation.SetupDirectory);
TextModeDriverIntegrator.IntegrateTextModeDrivers(textModeDriverDirectories, installation);
var deviceServices = PNPDriverIntegratorUtils.IntegratePNPDrivers(pnpDriverDirectories, installation, useLocalHardwareConfig, enumExportPath, preconfigure);
var netDeviceServices = DeviceServiceUtils.FilterNetworkDeviceServices(deviceServices);
if (netDeviceServices.Count > 0)
{
if (!preconfigure && !DeviceServiceUtils.ContainsService(deviceServices, "nicbtcfg"))
{
Console.WriteLine();
Console.Write("********************************************************************************");
Console.Write("*You have supplied a device driver for a network adapter, which requires *");
Console.Write("*special registry configuration to support boot start, but you have not *");
Console.Write("*used the /preconf switch. IntegrateDrv will assume you're using NICBootConf *");
Console.Write("*to configure your network adapter during boot. (recommended) *");
Console.Write("********************************************************************************");
}
}
if (usbBoot && !DeviceServiceUtils.ContainsService(deviceServices, "wait4ufd"))
{
Console.WriteLine();
Console.Write("********************************************************************************");
Console.Write("*When booting from a USB storage device, most systems will require that you *");
Console.Write("*will use a special driver (such as Wait4UFD) that will wait for the UFD boot *");
Console.Write("*storage device to be initialized before proceeding with the boot process. *");
Console.Write("********************************************************************************");
}
if (DeviceServiceUtils.ContainsService(deviceServices, "sanbootconf"))
{
Console.WriteLine();
Console.WriteLine("sanbootconf detected, GUI boot (Windows logo) has been enabled.");
installation.TextSetupInf.EnableGUIBoot();
}
if (netDeviceServices.Count > 0)
{
var kernelAndHalIntegrator = new KernelAndHalIntegrator(installation);
kernelAndHalIntegrator.UseUniprocessorKernel();
Console.WriteLine();
Console.WriteLine("Network adapter has been added, adding TCP/IP:");
var integrator = new TCPIPIntegrator(installation, netDeviceServices);
integrator.SetTCPIPBoot();
integrator.AssignIPAddressToNetDeviceServices(staticIP, staticSubnetMask, staticGateway);
}
if (usbBoot)
{
var integrator = new USBBootIntegrator(installation);
Console.WriteLine();
Console.WriteLine("Integrating USB 2.0 Host Controller and Hub drivers.");
integrator.IntegrateUSB20HostControllerAndHubDrivers();
Console.WriteLine("Integrating USB Mass Storage Class Driver.");
integrator.IntegrateUSBStorageDriver();
}
// no need to keep migration information (current drive letter assignments)
installation.DeleteMigrationInformation();
Console.WriteLine("Committing changes.");
installation.SaveModifiedINIFiles();
installation.SaveRegistryChanges();
Console.WriteLine("Driver integration completed.");
}
/// <summary>
/// return false if args are invalid
/// </summary>
private static bool ParseCommandLineSwitches(string[] args, out WindowsInstallation installation, out List<TextModeDriverDirectory> textModeDriverDirectories, out List<PNPDriverDirectory> pnpDriverDirectories, out bool useLocalHardwareConfig, out string enumExportPath, out bool preconfigure, out IPAddress staticIP, out IPAddress staticSubnetMask, out IPAddress staticGateway, out bool usbBoot)
{
installation = null;
textModeDriverDirectories = new List<TextModeDriverDirectory>();
pnpDriverDirectories = new List<PNPDriverDirectory>();
enumExportPath = string.Empty;
useLocalHardwareConfig = false;
preconfigure = false;
staticIP = null;
staticSubnetMask = null;
staticGateway = null;
usbBoot = false;
var pnpDriverPaths = new List<string>();
var textModeDriverPaths = new List<string>();
var targetPath = "C:\\";
if (args.Length == 0)
{
ShowHelp();
return false;
}
foreach (var arg in args)
{
var switchName = arg;
var switchParameter = string.Empty;
if (arg.StartsWith("/"))
{
switchName = arg.Substring(1);
var index = switchName.IndexOfAny(new[] { ':', '=' });
if (index >= 0)
{
switchParameter = switchName.Substring(index + 1);
switchParameter = switchParameter.Trim('"');
switchName = switchName.Substring(0, index);
}
}
switch (switchName.ToLower())
{
case "driver":
{
var pnpDriverPath = switchParameter;
if (!pnpDriverPath.EndsWith("\\") && !pnpDriverPath.EndsWith(":"))
pnpDriverPath = pnpDriverPath + "\\";
pnpDriverPaths.Add(pnpDriverPath);
break;
}
case "textmodedriver":
{
var textModeDriverPath = switchParameter;
if (!textModeDriverPath.EndsWith("\\") && !textModeDriverPath.EndsWith(":"))
textModeDriverPath = textModeDriverPath + "\\";
textModeDriverPaths.Add(textModeDriverPath);
break;
}
case "target":
{
targetPath = switchParameter;
if (!targetPath.EndsWith("\\") && !targetPath.EndsWith(":"))
targetPath = targetPath + "\\";
break;
}
case "local":
{
useLocalHardwareConfig = true;
break;
}
case "enum":
{
enumExportPath = switchParameter;
break;
}
case "preconf":
{
preconfigure = true;
break;
}
case "dhcp":
{
staticIP = null;
break;
}
case "ip":
{
if (!IPAddress.TryParse(switchParameter, out staticIP))
{
ShowHelp();
return false;
}
staticIP = IPAddress.Parse(switchParameter);
break;
}
case "subnet":
{
int subnet;
if (!int.TryParse(switchParameter, out subnet) || subnet > 32 || subnet < 0)
{
ShowHelp();
return false;
}
// Calculate a subnet mask from the CIDR
var subnetMask = 0xFFFFFFFF ^ (1 << 32 - subnet) - 1;
// Then flip the bytes and make an IP address
var b1 = (subnetMask >> 0) & 0xff;
var b2 = (subnetMask >> 8) & 0xff;
var b3 = (subnetMask >> 16) & 0xff;
var b4 = (subnetMask >> 24) & 0xff;
staticSubnetMask = new IPAddress(b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0);
break;
}
case "gateway":
{
if (!IPAddress.TryParse(switchParameter, out staticGateway))
{
ShowHelp();
return false;
}
staticGateway = IPAddress.Parse(switchParameter);
break;
}
case "usbboot":
{
usbBoot = true;
break;
}
case "?":
case "help":
{
ShowHelp();
return false;
}
default:
{
Console.WriteLine("Error: Invalid command-line switch: " + switchName);
return false;
}
}
}
installation = new WindowsInstallation(targetPath);
if (!installation.IsTargetValid)
{
Console.WriteLine("Error: Could not find installation directory");
Console.WriteLine("- if you are using winnt32.exe, you should use the /makelocalsource switch.");
return false;
}
if (!installation.IsWindows2000 && !installation.IsWindowsXP && !installation.IsWindowsServer2003)
{
Console.WriteLine("Error: Unsupported operating system version");
return false;
}
if (enumExportPath == string.Empty && !useLocalHardwareConfig && preconfigure)
{
Console.WriteLine("Error: The /preconf switch can only be present with the /local or /enum switch.");
return false;
}
foreach (var textModeDriverPath in textModeDriverPaths)
{
var textModeDriverDirectory = new TextModeDriverDirectory(textModeDriverPath);
if (!textModeDriverDirectory.ContainsOEMSetupFile)
{
Console.WriteLine("Error: Could not find the OEM driver setup file (txtsetup.oem). Directory: " + textModeDriverPath);
return false;
}
textModeDriverDirectories.Add(textModeDriverDirectory);
}
foreach (var pnpDriverPath in pnpDriverPaths)
{
var driverDirectory = new PNPDriverDirectory(pnpDriverPath);
if (!driverDirectory.ContainsINFFiles)
{
Console.WriteLine("Error: Could not find any .inf file in driver directory: " + pnpDriverPath);
return false;
}
pnpDriverDirectories.Add(driverDirectory);
}
if (enumExportPath != string.Empty && !FileSystemUtils.IsFileExist(enumExportPath))
{
Console.WriteLine("Error: the file '{0}' does not exist.", enumExportPath);
return false;
}
if (usbBoot && installation.IsWindows2000 && installation.ServicePackVersion < 4)
{
Console.WriteLine("Error: the /usbboot switch is only supported with Windows 2000 SP4.");
Console.WriteLine("Note that earlier versions of Windows 2000 do not support USB 2.0.");
return false;
}
return true;
}
private static void ShowHelp()
{
Console.WriteLine("IntegrateDRV v" + Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine("Author: Tal Aloni (tal.aloni.il@gmail.com)");
Console.WriteLine("About: This software is designed to integrate mass-storage drivers into windows");
Console.WriteLine(" XP/2003 installation / temporary installation directories.");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine("------");
Console.WriteLine("- IntegrateDRV [/textmodedriver=<path>] [/target=<path>]");
Console.WriteLine("- IntegrateDRV [/driver=<path>] [/target=<path>]");
Console.WriteLine("- IntegrateDRV [/driver=<path>] [/target=<path>] [/local [/preconf]]");
Console.WriteLine("- IntegrateDRV [/driver=<path>] [/target=<path>] [/enum=<path> [/preconf]]");
Console.WriteLine("- IntegrateDRV [/usbboot] [/target=<path>]");
Console.WriteLine();
Console.WriteLine("* If [/target] is not specified, C:\\ will be used");
Console.WriteLine();
Console.WriteLine("* The /local switch will only display hardware that is present locally.");
Console.WriteLine("* The /enum switch will only display hardware that is present in a registry");
Console.WriteLine(" export of HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum.");
Console.WriteLine();
Console.WriteLine("Note about integrating PNP Drivers for network adapters:");
Console.WriteLine("--------------------------------------------------------");
Console.WriteLine("Network adapters require special registry configuration to support boot start.");
Console.WriteLine("This configuration can be set in one of 3 ways:");
Console.WriteLine("1. Integrate a driver called NICBootConf to configure the NIC during each boot.");
Console.WriteLine("2. Run this software from the target machine and use the /local and /preconf");
Console.WriteLine(" switches, IntegrateDRV will pre-configure the network adapter.");
Console.WriteLine("3. Export HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum from the target");
Console.WriteLine(" machine, and provide the path to the exported key using the /enum and");
Console.WriteLine(" /preconf switches.");
}
public static void Exit()
{
new SetupRegistryHiveFile().UnloadHive(false);
Environment.Exit(-1);
}
}
}