Leaked source code of windows server 2003
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.

669 lines
16 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.\r\n", 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. //
  98. // Initialize locals.
  99. //
  100. lError = ERROR_SUCCESS;
  101. hSettingStore = (HANDLE)INVALID_HANDLE_VALUE;
  102. //
  103. // Check global initialization.
  104. //
  105. if( (NULL == g_WorkingDirectory)
  106. || (NULL == g_SourceDirectory)
  107. || (NULL == g_MediaDirectory) )
  108. {
  109. SetupLogError("WIA Migration: MigrateSystem9x: ERROR!! Initialize failed.\r\n", LogSevError);
  110. lError = ERROR_NOT_ENOUGH_MEMORY;
  111. goto MigrateSystem9x_return;
  112. }
  113. //
  114. // Create path to the files.
  115. //
  116. wsprintfA(szFile, "%s\\%s", g_WorkingDirectory, NAME_WIN9X_SETTING_FILE_A);
  117. wsprintfA(szInfName, "%s\\%s", g_WorkingDirectory, NAME_MIGRATE_INF_A);
  118. //
  119. // Create files.
  120. //
  121. hSettingStore = CreateFileA(szFile,
  122. GENERIC_WRITE,
  123. 0,
  124. NULL,
  125. CREATE_ALWAYS,
  126. FILE_ATTRIBUTE_NORMAL,
  127. NULL);
  128. if(INVALID_HANDLE_VALUE == hSettingStore){
  129. SetupLogError("WIA Migration: MigrateSystem9x: ERROR!! Unable to create setting file.\r\n", LogSevError);
  130. lError = GetLastError();
  131. goto MigrateSystem9x_return;
  132. } // if(INVALID_HANDLE_VALUE == hSettingStore)
  133. //
  134. // Create setting file based on device registry.
  135. //
  136. lError = Mig9xGetDeviceInfo(hSettingStore);
  137. if(ERROR_SUCCESS != lError){
  138. goto MigrateSystem9x_return;
  139. } // if(ERROR_SUCCESS != lError)
  140. MigrateSystem9x_return:
  141. //
  142. // Clean up.
  143. //
  144. if(hSettingStore != INVALID_HANDLE_VALUE){
  145. CloseHandle(hSettingStore);
  146. }
  147. return lError;
  148. } // MigrateSystem9x()
  149. LONG
  150. CALLBACK
  151. Mig9xGetDeviceInfo(
  152. IN HANDLE hFile
  153. )
  154. {
  155. LONG lError;
  156. DWORD Idx;
  157. GUID Guid;
  158. HANDLE hDevInfo;
  159. SP_DEVINFO_DATA spDevInfoData;
  160. HKEY hKeyDevice;
  161. PCHAR pTempBuffer;
  162. //
  163. // Initialize locals.
  164. //
  165. lError = ERROR_SUCCESS;
  166. Guid = GUID_DEVCLASS_IMAGE;
  167. hDevInfo = (HANDLE)INVALID_HANDLE_VALUE;
  168. Idx = 0;
  169. hKeyDevice = (HKEY)INVALID_HANDLE_VALUE;
  170. pTempBuffer = NULL;
  171. //
  172. // Enumerate WIA/STI devices and spew device info.
  173. //
  174. hDevInfo = SetupDiGetClassDevs(&Guid, NULL, NULL, DIGCF_PROFILE);
  175. if(INVALID_HANDLE_VALUE == hDevInfo){
  176. SetupLogError("WIA Migration: Mig9xGetDeviceInfo: ERROR!! Unable to acquire device list.\r\n", LogSevError);
  177. lError = ERROR_NOT_ENOUGH_MEMORY;
  178. goto Mig9xGetDeviceInfo_return;
  179. } // if(INVALID_HANDLE_VALUE == hDevInfo)
  180. //
  181. // Save installed device setting.
  182. //
  183. spDevInfoData.cbSize = sizeof(spDevInfoData);
  184. for (Idx = 0; SetupDiEnumDeviceInfo (hDevInfo, Idx, &spDevInfoData); Idx++) {
  185. //
  186. // Open device registry key.
  187. //
  188. hKeyDevice = SetupDiOpenDevRegKey(hDevInfo,
  189. &spDevInfoData,
  190. DICS_FLAG_GLOBAL,
  191. 0,
  192. DIREG_DRV,
  193. KEY_READ);
  194. if (INVALID_HANDLE_VALUE != hKeyDevice) {
  195. if( (TRUE == IsSti(hKeyDevice))
  196. && (FALSE == IsKernelDriverRequired(hKeyDevice)) )
  197. {
  198. //
  199. // This is STI/WIA device with no kernel driver . Spew required info.
  200. //
  201. WriteDeviceToFile(hFile, hKeyDevice);
  202. } // if( IsSti(hKeyDevice) && !IsKernelDriverRequired(hKeyDevice))
  203. } // if (INVALID_HANDLE_VALUE != hKeyDevice)
  204. } // for (Idx = 0; SetupDiEnumDeviceInfo (hDevInfo, Idx, &spDevInfoData); Idx++)
  205. Mig9xGetDeviceInfo_return:
  206. if(NULL != pTempBuffer){
  207. FreeMem(pTempBuffer);
  208. } // if(NULL != pTempBuffer)
  209. return lError;
  210. } // Mig9xGetGlobalInfo()
  211. BOOL
  212. IsSti(
  213. HKEY hKeyDevice
  214. )
  215. {
  216. BOOL bRet;
  217. PCHAR pTempBuffer;
  218. LONG lError;
  219. //
  220. // Initialize local.
  221. //
  222. bRet = FALSE;
  223. pTempBuffer = NULL;
  224. lError = ERROR_SUCCESS;
  225. //
  226. // See if it's StillImage device.
  227. //
  228. lError = GetRegData(hKeyDevice,
  229. REGVAL_USDCLASS_A,
  230. &pTempBuffer,
  231. NULL,
  232. NULL);
  233. if( (ERROR_SUCCESS != lError)
  234. || (NULL == pTempBuffer) )
  235. {
  236. //
  237. // Unable to get "SubClass" data. This is not STI/WIA.
  238. //
  239. bRet = FALSE;
  240. goto IsSti_return;
  241. } // if( (ERROR_SUCCESS != lError) || (NULL == pTempBuffer)
  242. //
  243. // This is STI/WIA device.
  244. //
  245. bRet = TRUE;
  246. IsSti_return:
  247. //
  248. // Clean up.
  249. //
  250. if(NULL != pTempBuffer){
  251. FreeMem(pTempBuffer);
  252. } // if(NULL != pTempBuffer)
  253. return bRet;
  254. } // IsSti()
  255. BOOL
  256. IsKernelDriverRequired(
  257. HKEY hKeyDevice
  258. )
  259. {
  260. BOOL bRet;
  261. PCHAR pTempBuffer;
  262. LONG lError;
  263. //
  264. // Initialize local.
  265. //
  266. bRet = FALSE;
  267. pTempBuffer = NULL;
  268. lError = ERROR_SUCCESS;
  269. //
  270. // See if it's StillImage device.
  271. //
  272. lError = GetRegData(hKeyDevice,
  273. REGVAL_NTMPDRIVER_A,
  274. &pTempBuffer,
  275. NULL,
  276. NULL);
  277. if( (ERROR_SUCCESS != lError)
  278. || (NULL == pTempBuffer) )
  279. {
  280. //
  281. // Unable to get "NTMPDriver" data. This device doesn't require kernel mode component.
  282. //
  283. bRet = FALSE;
  284. goto IsKernelDriverRequired_return;
  285. } // if( (ERROR_SUCCESS != lError) || (NULL == pTempBuffer)
  286. //
  287. // This device requires kernel mode component.
  288. //
  289. bRet = TRUE;
  290. IsKernelDriverRequired_return:
  291. //
  292. // Clean up.
  293. //
  294. if(NULL != pTempBuffer){
  295. FreeMem(pTempBuffer);
  296. } // if(NULL != pTempBuffer)
  297. return bRet;
  298. } // IsKernelDriverRequired()
  299. LONG
  300. WriteDeviceToFile(
  301. HANDLE hFile,
  302. HKEY hKey
  303. )
  304. {
  305. LONG lError;
  306. PCHAR pFriendlyName;
  307. PCHAR pCreateFileName;
  308. PCHAR pInfPath;
  309. PCHAR pInfSection;
  310. DWORD dwType;
  311. DWORD dwSize;
  312. CHAR SpewBuffer[256];
  313. HKEY hDeviceData;
  314. //
  315. // Initialize local.
  316. //
  317. lError = ERROR_SUCCESS;
  318. pFriendlyName = NULL;
  319. pCreateFileName = NULL;
  320. pInfPath = NULL;
  321. pInfSection = NULL;
  322. dwSize = 0;
  323. hDeviceData = (HKEY)INVALID_HANDLE_VALUE;
  324. memset(SpewBuffer, 0, sizeof(SpewBuffer));
  325. //
  326. // Get FriendlyName.
  327. //
  328. dwSize = 0;
  329. lError = GetRegData(hKey, NAME_FRIENDLYNAME_A, &pFriendlyName, &dwType, &dwSize);
  330. if(ERROR_SUCCESS != lError){
  331. //
  332. // Unable to get FriendlyName.
  333. //
  334. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get FriendlyName.\r\n", LogSevError);
  335. goto WriteDeviceToFile_return;
  336. } // if(ERROR_SUCCESS != lError)
  337. if(REG_SZ != dwType){
  338. //
  339. // FriendlyName key is other than REG_SZ.
  340. //
  341. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! FriendlyName is other than REG_SZ.\r\n", LogSevError);
  342. lError = ERROR_REGISTRY_CORRUPT;
  343. goto WriteDeviceToFile_return;
  344. } // if(REG_SZ != dwType)
  345. if(dwSize > MAX_FRIENDLYNAME+1){
  346. //
  347. // Too long
  348. //
  349. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! FriendlyName is too long.\r\n", LogSevError);
  350. lError = ERROR_REGISTRY_CORRUPT;
  351. goto WriteDeviceToFile_return;
  352. }
  353. //
  354. // Get CreateFileName.
  355. //
  356. dwSize = 0;
  357. lError = GetRegData(hKey, NAME_CREATEFILENAME_A, &pCreateFileName, &dwType, &dwSize);
  358. if(ERROR_SUCCESS != lError){
  359. //
  360. // Unable to get CreateFileName.
  361. //
  362. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get CreateFileName.\r\n", LogSevError);
  363. goto WriteDeviceToFile_return;
  364. } // if(ERROR_SUCCESS != lError)
  365. if(REG_SZ != dwType){
  366. //
  367. // CreateFileName key is other than REG_SZ.
  368. //
  369. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! CreateFileName is other than REG_SZ.\r\n", LogSevError);
  370. lError = ERROR_REGISTRY_CORRUPT;
  371. goto WriteDeviceToFile_return;
  372. } // if(REG_SZ != dwType)
  373. if(dwSize > MAX_PATH+1){
  374. //
  375. // Too long
  376. //
  377. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! CreateFileName is too long.\r\n", LogSevError);
  378. lError = ERROR_REGISTRY_CORRUPT;
  379. goto WriteDeviceToFile_return;
  380. }
  381. //
  382. // Get InfPath.
  383. //
  384. dwSize = 0;
  385. lError = GetRegData(hKey, NAME_INF_PATH_A, &pInfPath, &dwType, &dwSize);
  386. if(ERROR_SUCCESS != lError){
  387. //
  388. // Unable to get InfPath.
  389. //
  390. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get InfPath.\r\n", LogSevError);
  391. goto WriteDeviceToFile_return;
  392. } // if(ERROR_SUCCESS != lError)
  393. if(REG_SZ != dwType){
  394. //
  395. // InfPath key is other than REG_SZ.
  396. //
  397. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfPath is other than REG_SZ.\r\n", LogSevError);
  398. lError = ERROR_REGISTRY_CORRUPT;
  399. goto WriteDeviceToFile_return;
  400. } // if(REG_SZ != dwType)
  401. if(dwSize > MAX_PATH+1){
  402. //
  403. // Too long
  404. //
  405. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfPath is too long.\r\n", LogSevError);
  406. lError = ERROR_REGISTRY_CORRUPT;
  407. goto WriteDeviceToFile_return;
  408. }
  409. //
  410. // Get InfSection.
  411. //
  412. dwSize = 0;
  413. lError = GetRegData(hKey, NAME_INF_SECTION_A, &pInfSection, &dwType, &dwSize);
  414. if(ERROR_SUCCESS != lError){
  415. //
  416. // Unable to get InfSection.
  417. //
  418. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! Unable to get InfSection.\r\n", LogSevError);
  419. goto WriteDeviceToFile_return;
  420. } // if(ERROR_SUCCESS != lError)
  421. if(REG_SZ != dwType){
  422. //
  423. // InfSection key is other than REG_SZ.
  424. //
  425. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfSection is other than REG_SZ.\r\n", LogSevError);
  426. lError = ERROR_REGISTRY_CORRUPT;
  427. goto WriteDeviceToFile_return;
  428. } // if(REG_SZ != dwType)
  429. if(dwSize > MAX_PATH+1){
  430. //
  431. // Too long
  432. //
  433. SetupLogError("WIA Migration: WriteDeviceToFile: ERROR!! InfSection is too long.\r\n", LogSevError);
  434. lError = ERROR_REGISTRY_CORRUPT;
  435. goto WriteDeviceToFile_return;
  436. }
  437. //
  438. // Spew device information.
  439. //
  440. WriteToFile(hFile, "\r\n");
  441. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_DEVICE_A, NAME_BEGIN_A);
  442. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_FRIENDLYNAME_A, pFriendlyName);
  443. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_CREATEFILENAME_A, pCreateFileName);
  444. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_INF_PATH_A, pInfPath);
  445. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_INF_SECTION_A, pInfSection);
  446. //
  447. // Spew DaviceData section.
  448. //
  449. lError = RegOpenKey(hKey,
  450. REGKEY_DEVICEDATA_A,
  451. &hDeviceData);
  452. if(lError != ERROR_SUCCESS){
  453. //
  454. // Unable to open DeviceData or doesn't exist.
  455. //
  456. }
  457. //
  458. // Spew DeviceData section if exists.
  459. //
  460. if(INVALID_HANDLE_VALUE != hDeviceData){
  461. lError = WriteRegistryToFile(hFile, hDeviceData, REGKEY_DEVICEDATA_A);
  462. } // if(INVALID_HANDLE_VALUE != hDeviceData)
  463. //
  464. // Indicate the end of device description.
  465. //
  466. WriteToFile(hFile, "\"%s\" = \"%s\"\r\n", NAME_DEVICE_A, NAME_END_A);
  467. WriteDeviceToFile_return:
  468. //
  469. // Clean up.
  470. //
  471. if(INVALID_HANDLE_VALUE != hDeviceData){
  472. RegCloseKey(hDeviceData);
  473. } //if(INVALID_HANDLE_VALUE != hDeviceData)
  474. if(NULL != pFriendlyName){
  475. FreeMem(pFriendlyName);
  476. }
  477. if(NULL != pCreateFileName){
  478. FreeMem(pCreateFileName);
  479. }
  480. if(NULL != pInfPath){
  481. FreeMem(pInfPath);
  482. }
  483. if(NULL != pInfSection){
  484. FreeMem(pInfSection);
  485. }
  486. return lError;
  487. } // WriteDeviceToFile()
  488. //
  489. // The following are to make sure if setup changes the header file they
  490. // first tell me (otherwise they will break build of this)
  491. //
  492. P_INITIALIZE_9X pfnInitialize9x = Initialize9x;
  493. P_MIGRATE_USER_9X pfnMigrateUser9x = MigrateUser9x;
  494. P_MIGRATE_SYSTEM_9X pfnMigrateSystem9x = MigrateSystem9x;