Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

673 lines
15 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. Win9x.c
  6. Abstract:
  7. Routines to pre-migrate Win9x to NT
  8. Author:
  9. Keisuke Tsuchida (KeisukeT) 10-Oct-2000
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #include "devguid.h"
  14. //
  15. // Globals
  16. //
  17. LPCSTR g_WorkingDirectory = NULL;
  18. LPCSTR g_SourceDirectory = NULL;
  19. LPCSTR g_MediaDirectory = NULL;
  20. //LPCSTR g_WorkingDirectory = ".";
  21. //LPCSTR g_SourceDirectory = ".";
  22. //LPCSTR g_MediaDirectory = ".";
  23. LONG
  24. CALLBACK
  25. Initialize9x(
  26. IN LPCSTR pszWorkingDir,
  27. IN LPCSTR pszSourceDir,
  28. IN LPCSTR pszMediaDir
  29. )
  30. {
  31. LONG lError;
  32. //
  33. // Initialize local.
  34. //
  35. lError = ERROR_SUCCESS;
  36. //
  37. // Save given parameters.
  38. //
  39. g_WorkingDirectory = AllocStrA(pszWorkingDir);
  40. g_SourceDirectory = AllocStrA(pszSourceDir);
  41. g_MediaDirectory = AllocStrA(pszMediaDir);
  42. if( (NULL == g_WorkingDirectory)
  43. || (NULL == g_SourceDirectory)
  44. || (NULL == g_MediaDirectory) )
  45. {
  46. SetupLogError("WIA Migration: Initialize9x: ERROR!! insufficient memory.", LogSevError);
  47. lError = ERROR_NOT_ENOUGH_MEMORY;
  48. goto Initialize9x_return;
  49. }
  50. Initialize9x_return:
  51. if(ERROR_SUCCESS != lError){
  52. //
  53. // Can't process migration. Clean up.
  54. //
  55. if(NULL != g_WorkingDirectory){
  56. FreeMem((PVOID)g_WorkingDirectory);
  57. g_WorkingDirectory = NULL;
  58. }
  59. if(NULL != g_SourceDirectory){
  60. FreeMem((PVOID)g_SourceDirectory);
  61. g_SourceDirectory = NULL;
  62. }
  63. if(NULL != g_MediaDirectory){
  64. FreeMem((PVOID)g_MediaDirectory);
  65. g_MediaDirectory = NULL;
  66. }
  67. } // if(ERROR_SUCCESS != lError)
  68. return lError;
  69. } // Initialize9x()
  70. LONG
  71. CALLBACK
  72. MigrateUser9x(
  73. IN HWND hwndParent,
  74. IN LPCSTR pszUnattendFile,
  75. IN HKEY hUserRegKey,
  76. IN LPCSTR pszUserName,
  77. LPVOID Reserved
  78. )
  79. {
  80. //
  81. // Nothing to do
  82. //
  83. return ERROR_SUCCESS;
  84. } // MigrateUser9x()
  85. LONG
  86. CALLBACK
  87. MigrateSystem9x(
  88. IN HWND hwndParent,
  89. IN LPCSTR pszUnattendFile,
  90. IN LPVOID Reserved
  91. )
  92. {
  93. LONG lError;
  94. CHAR szFile[MAX_PATH];
  95. CHAR szInfName[MAX_PATH];
  96. HANDLE hSettingStore;
  97. HANDLE hInf;
  98. //
  99. // Initialize locals.
  100. //
  101. lError = ERROR_SUCCESS;
  102. hSettingStore = (HANDLE)INVALID_HANDLE_VALUE;
  103. hInf = (HANDLE)INVALID_HANDLE_VALUE;
  104. //
  105. // Check global initialization.
  106. //
  107. if( (NULL == g_WorkingDirectory)
  108. || (NULL == g_SourceDirectory)
  109. || (NULL == g_MediaDirectory) )
  110. {
  111. SetupLogError("WIA Migration: MigrateSystem9x: ERROR!! Initialize failed.", LogSevError);
  112. lError = ERROR_NOT_ENOUGH_MEMORY;
  113. goto MigrateSystem9x_return;
  114. }
  115. //
  116. // Create path to the files.
  117. //
  118. wsprintfA(szFile, "%s\\%s", g_WorkingDirectory, NAME_WIN9X_SETTING_FILE_A);
  119. wsprintfA(szInfName, "%s\\%s", g_WorkingDirectory, NAME_MIGRATE_INF_A);
  120. //
  121. // Create files.
  122. //
  123. hSettingStore = CreateFileA(szFile,
  124. GENERIC_WRITE,
  125. 0,
  126. NULL,
  127. CREATE_ALWAYS,
  128. FILE_ATTRIBUTE_NORMAL,
  129. NULL);
  130. if(INVALID_HANDLE_VALUE == hSettingStore){
  131. SetupLogError("WIA Migration: MigrateSystem9x: ERROR!! Unable to create setting file.", LogSevError);
  132. lError = GetLastError();
  133. goto MigrateSystem9x_return;
  134. } // if(INVALID_HANDLE_VALUE == hSettingStore)
  135. //
  136. // Create setting file based on device registry.
  137. //
  138. lError = Mig9xGetDeviceInfo(hSettingStore);
  139. if(ERROR_SUCCESS != lError){
  140. goto MigrateSystem9x_return;
  141. } // if(ERROR_SUCCESS != lError)
  142. MigrateSystem9x_return:
  143. //
  144. // Clean up.
  145. //
  146. if(hSettingStore != INVALID_HANDLE_VALUE){
  147. CloseHandle(hSettingStore);
  148. }
  149. if(hInf != INVALID_HANDLE_VALUE){
  150. CloseHandle(hInf);
  151. }
  152. return lError;
  153. } // MigrateSystem9x()
  154. LONG
  155. CALLBACK
  156. Mig9xGetGlobalInfo(
  157. IN HANDLE hFile
  158. )
  159. {
  160. LONG lError;
  161. HKEY hKey;
  162. //
  163. // Initialize local.
  164. //
  165. lError = ERROR_SUCCESS;
  166. hKey = (HKEY)INVALID_HANDLE_VALUE;
  167. //
  168. // Open HKLM\SYSTEM\CurrentControlSet\Control\StillImage.
  169. //
  170. lError = RegOpenKeyA(HKEY_LOCAL_MACHINE,
  171. REGSTR_PATH_STICONTROL_A,
  172. &hKey);
  173. if(ERROR_SUCCESS != lError){
  174. SetupLogError("WIA Migration: Mig9xGetGlobalInfo: ERROR!! Unable to open Conrtol\\StilImage.", LogSevError);
  175. goto Mig9xGetGlobalInfo_return;
  176. } // if(ERROR_SUCCESS != lError)
  177. //
  178. // Spew to a file.
  179. //
  180. lError = WriteRegistryToFile(hFile, hKey, "\0");
  181. Mig9xGetGlobalInfo_return:
  182. //
  183. // Clean up.
  184. //
  185. if(INVALID_HANDLE_VALUE != hKey){
  186. RegCloseKey(hKey);
  187. } // if(INVALID_HANDLE_VALUE != hKey)
  188. return lError;
  189. } // Mig9xGetGlobalInfo()
  190. LONG
  191. CALLBACK
  192. Mig9xGetDeviceInfo(
  193. IN HANDLE hFile
  194. )
  195. {
  196. LONG lError;
  197. DWORD Idx;
  198. GUID Guid;
  199. HANDLE hDevInfo;
  200. SP_DEVINFO_DATA spDevInfoData;
  201. HKEY hKeyDevice;
  202. PCHAR pTempBuffer;
  203. //
  204. // Initialize locals.
  205. //
  206. lError = ERROR_SUCCESS;
  207. Guid = GUID_DEVCLASS_IMAGE;
  208. hDevInfo = (HANDLE)INVALID_HANDLE_VALUE;
  209. Idx = 0;
  210. hKeyDevice = (HKEY)INVALID_HANDLE_VALUE;
  211. pTempBuffer = NULL;
  212. //
  213. // Enumerate WIA/STI devices and spew device info.
  214. //
  215. hDevInfo = SetupDiGetClassDevs(&Guid, NULL, NULL, DIGCF_PROFILE);
  216. if(INVALID_HANDLE_VALUE == hDevInfo){
  217. SetupLogError("WIA Migration: Mig9xGetDeviceInfo: ERROR!! Unable to acquire device list.", LogSevError);
  218. lError = ERROR_NOT_ENOUGH_MEMORY;
  219. goto Mig9xGetDeviceInfo_return;
  220. } // if(INVALID_HANDLE_VALUE == hDevInfo)
  221. //
  222. // Save installed device setting.
  223. //
  224. spDevInfoData.cbSize = sizeof(spDevInfoData);
  225. for (Idx = 0; SetupDiEnumDeviceInfo (hDevInfo, Idx, &spDevInfoData); Idx++) {
  226. //
  227. // Open device registry key.
  228. //
  229. hKeyDevice = SetupDiOpenDevRegKey(hDevInfo,
  230. &spDevInfoData,
  231. DICS_FLAG_GLOBAL,
  232. 0,
  233. DIREG_DRV,
  234. KEY_READ);
  235. if (INVALID_HANDLE_VALUE != hKeyDevice) {
  236. if( (TRUE == IsSti(hKeyDevice))
  237. && (FALSE == IsKernelDriverRequired(hKeyDevice)) )
  238. {
  239. //
  240. // This is STI/WIA device with no kernel driver . Spew required info.
  241. //
  242. WriteDeviceToFile(hFile, hKeyDevice);
  243. } // if( IsSti(hKeyDevice) && !IsKernelDriverRequired(hKeyDevice))
  244. } // if (INVALID_HANDLE_VALUE != hKeyDevice)
  245. } // for (Idx = 0; SetupDiEnumDeviceInfo (hDevInfo, Idx, &spDevInfoData); Idx++)
  246. Mig9xGetDeviceInfo_return:
  247. if(NULL != pTempBuffer){
  248. FreeMem(pTempBuffer);
  249. } // if(NULL != pTempBuffer)
  250. return lError;
  251. } // Mig9xGetGlobalInfo()
  252. BOOL
  253. IsSti(
  254. HKEY hKeyDevice
  255. )
  256. {
  257. BOOL bRet;
  258. PCHAR pTempBuffer;
  259. LONG lError;
  260. //
  261. // Initialize local.
  262. //
  263. bRet = FALSE;
  264. pTempBuffer = NULL;
  265. lError = ERROR_SUCCESS;
  266. //
  267. // See if it's StillImage device.
  268. //
  269. lError = GetRegData(hKeyDevice,
  270. REGVAL_USDCLASS_A,
  271. &pTempBuffer,
  272. NULL,
  273. NULL);
  274. if( (ERROR_SUCCESS != lError)
  275. || (NULL == pTempBuffer) )
  276. {
  277. //
  278. // Unable to get "SubClass" data. This is not STI/WIA.
  279. //
  280. bRet = FALSE;
  281. goto IsSti_return;
  282. } // if( (ERROR_SUCCESS != lError) || (NULL == pTempBuffer)
  283. //
  284. // This is STI/WIA device.
  285. //
  286. bRet = TRUE;
  287. IsSti_return:
  288. //
  289. // Clean up.
  290. //
  291. if(NULL != pTempBuffer){
  292. FreeMem(pTempBuffer);
  293. } // if(NULL != pTempBuffer)
  294. return bRet;
  295. } // IsSti()
  296. BOOL
  297. IsKernelDriverRequired(
  298. HKEY hKeyDevice
  299. )
  300. {
  301. BOOL bRet;
  302. PCHAR pTempBuffer;
  303. LONG lError;
  304. //
  305. // Initialize local.
  306. //
  307. bRet = FALSE;
  308. pTempBuffer = NULL;
  309. lError = ERROR_SUCCESS;
  310. //
  311. // See if it's StillImage device.
  312. //
  313. lError = GetRegData(hKeyDevice,
  314. REGVAL_NTMPDRIVER_A,
  315. &pTempBuffer,
  316. NULL,
  317. NULL);
  318. if( (ERROR_SUCCESS != lError)
  319. || (NULL == pTempBuffer) )
  320. {
  321. //
  322. // Unable to get "NTMPDriver" data. This device doesn't require kernel mode component.
  323. //
  324. bRet = FALSE;
  325. goto IsKernelDriverRequired_return;
  326. } // if( (ERROR_SUCCESS != lError) || (NULL == pTempBuffer)
  327. //
  328. // This device requires kernel mode component.
  329. //
  330. bRet = TRUE;
  331. IsKernelDriverRequired_return:
  332. //
  333. // Clean up.
  334. //
  335. if(NULL != pTempBuffer){
  336. FreeMem(pTempBuffer);
  337. } // if(NULL != pTempBuffer)
  338. return bRet;
  339. } // IsKernelDriverRequired()
  340. LONG
  341. WriteDeviceToFile(
  342. HANDLE hFile,
  343. HKEY hKey
  344. )
  345. {
  346. LONG lError;
  347. PCHAR pFriendlyName;
  348. PCHAR pCreateFileName;
  349. PCHAR pInfPath;
  350. PCHAR pInfSection;
  351. DWORD dwType;
  352. CHAR SpewBuffer[256];
  353. HKEY hDeviceData;
  354. //
  355. // Initialize local.
  356. //
  357. lError = ERROR_SUCCESS;
  358. pFriendlyName = NULL;
  359. pCreateFileName = NULL;
  360. pInfPath = NULL;
  361. pInfSection = NULL;
  362. hDeviceData = (HKEY)INVALID_HANDLE_VALUE;
  363. memset(SpewBuffer, 0, sizeof(SpewBuffer));
  364. //
  365. // Get FriendlyName.
  366. //
  367. lError = GetRegData(hKey, NAME_FRIENDLYNAME_A, &pFriendlyName, &dwType, NULL);
  368. if(ERROR_SUCCESS != lError){
  369. //
  370. // Unable to get FriendlyName.
  371. //
  372. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get FriendlyName.", LogSevError);
  373. goto WriteDeviceToFile_return;
  374. } // if(ERROR_SUCCESS != lError)
  375. if(REG_SZ != dwType){
  376. //
  377. // FriendlyName key is other than REG_SZ.
  378. //
  379. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! FriendlyName is other than REG_SZ.", LogSevError);
  380. lError = ERROR_REGISTRY_CORRUPT;
  381. goto WriteDeviceToFile_return;
  382. } // if(REG_SZ != dwType)
  383. //
  384. // Get CreateFileName.
  385. //
  386. lError = GetRegData(hKey, NAME_CREATEFILENAME_A, &pCreateFileName, &dwType, NULL);
  387. if(ERROR_SUCCESS != lError){
  388. //
  389. // Unable to get CreateFileName.
  390. //
  391. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get CreateFileName.", LogSevError);
  392. goto WriteDeviceToFile_return;
  393. } // if(ERROR_SUCCESS != lError)
  394. if(REG_SZ != dwType){
  395. //
  396. // CreateFileName key is other than REG_SZ.
  397. //
  398. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! CreateFileName is other than REG_SZ.", LogSevError);
  399. lError = ERROR_REGISTRY_CORRUPT;
  400. goto WriteDeviceToFile_return;
  401. } // if(REG_SZ != dwType)
  402. //
  403. // Get InfPath.
  404. //
  405. lError = GetRegData(hKey, NAME_INF_PATH_A, &pInfPath, &dwType, NULL);
  406. if(ERROR_SUCCESS != lError){
  407. //
  408. // Unable to get InfPath.
  409. //
  410. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get InfPath.", LogSevError);
  411. goto WriteDeviceToFile_return;
  412. } // if(ERROR_SUCCESS != lError)
  413. if(REG_SZ != dwType){
  414. //
  415. // InfPath key is other than REG_SZ.
  416. //
  417. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfPath is other than REG_SZ.", LogSevError);
  418. lError = ERROR_REGISTRY_CORRUPT;
  419. goto WriteDeviceToFile_return;
  420. } // if(REG_SZ != dwType)
  421. //
  422. // Get InfSection.
  423. //
  424. lError = GetRegData(hKey, NAME_INF_SECTION_A, &pInfSection, &dwType, NULL);
  425. if(ERROR_SUCCESS != lError){
  426. //
  427. // Unable to get InfSection.
  428. //
  429. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get InfSection.", LogSevError);
  430. goto WriteDeviceToFile_return;
  431. } // if(ERROR_SUCCESS != lError)
  432. if(REG_SZ != dwType){
  433. //
  434. // InfSection key is other than REG_SZ.
  435. //
  436. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfSection is other than REG_SZ.", LogSevError);
  437. lError = ERROR_REGISTRY_CORRUPT;
  438. goto WriteDeviceToFile_return;
  439. } // if(REG_SZ != dwType)
  440. //
  441. // Spew device information.
  442. //
  443. WriteToFile(hFile, "\r\n");
  444. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_DEVICE_A, NAME_BEGIN_A);
  445. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_FRIENDLYNAME_A, pFriendlyName);
  446. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_CREATEFILENAME_A, pCreateFileName);
  447. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_INF_PATH_A, pInfPath);
  448. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_INF_SECTION_A, pInfSection);
  449. //
  450. // Spew DaviceData section.
  451. //
  452. lError = RegOpenKey(hKey,
  453. REGKEY_DEVICEDATA_A,
  454. &hDeviceData);
  455. if(lError != ERROR_SUCCESS){
  456. //
  457. // Unable to open DeviceData or doesn't exist.
  458. //
  459. }
  460. //
  461. // Spew DeviceData section if exists.
  462. //
  463. if(INVALID_HANDLE_VALUE != hDeviceData){
  464. lError = WriteRegistryToFile(hFile, hDeviceData, REGKEY_DEVICEDATA_A);
  465. } // if(INVALID_HANDLE_VALUE != hDeviceData)
  466. //
  467. // Indicate the end of device description.
  468. //
  469. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_DEVICE_A, NAME_END_A);
  470. WriteDeviceToFile_return:
  471. //
  472. // Clean up.
  473. //
  474. if(INVALID_HANDLE_VALUE != hDeviceData){
  475. RegCloseKey(hDeviceData);
  476. } //if(INVALID_HANDLE_VALUE != hDeviceData)
  477. if(NULL != pFriendlyName){
  478. FreeMem(pFriendlyName);
  479. }
  480. if(NULL != pCreateFileName){
  481. FreeMem(pCreateFileName);
  482. }
  483. if(NULL != pInfPath){
  484. FreeMem(pInfPath);
  485. }
  486. if(NULL != pInfSection){
  487. FreeMem(pInfSection);
  488. }
  489. return lError;
  490. } // WriteDeviceToFile()
  491. //
  492. // The following are to make sure if setup changes the header file they
  493. // first tell me (otherwise they will break build of this)
  494. //
  495. P_INITIALIZE_9X pfnInitialize9x = Initialize9x;
  496. P_MIGRATE_USER_9X pfnMigrateUser9x = MigrateUser9x;
  497. P_MIGRATE_SYSTEM_9X pfnMigrateSystem9x = MigrateSystem9x;