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.

5551 lines
172 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. dynupdt.c
  5. Abstract:
  6. The Dynamic Update feature of WINNT32.
  7. Author:
  8. Ovidiu Temereanca (ovidiut) 02-Jul-2000
  9. Revision History:
  10. <alias> <date> <comment>
  11. --*/
  12. #include "precomp.h"
  13. //
  14. // BUGBUG - comment functions
  15. //
  16. #define GUIDRVS_FIELD_CABNAME 1
  17. #define GUIDRVS_FIELD_INFNAME 2
  18. #define GUIDRVS_FIELD_DRIVERVER 3
  19. #define GUIDRVS_FIELD_HARDWAREID 4
  20. #define MAX_UPGCHK_ELAPSED_SECONDS (30 * 60)
  21. PDYNUPDT_STATUS g_DynUpdtStatus;
  22. static WORD g_MapProductTypeToSuite[] = {
  23. 0, // pro
  24. VER_SUITE_SMALLBUSINESS, // srv
  25. VER_SUITE_ENTERPRISE, // ads
  26. VER_SUITE_DATACENTER, // dtc
  27. VER_SUITE_PERSONAL, // per
  28. VER_SUITE_BLADE, // bla
  29. };
  30. static BYTE g_MapProductTypeToPT[] = {
  31. VER_NT_WORKSTATION,
  32. VER_NT_SERVER,
  33. VER_NT_SERVER,
  34. VER_NT_SERVER,
  35. VER_NT_WORKSTATION,
  36. VER_NT_SERVER,
  37. };
  38. typedef
  39. BOOL
  40. (*PCOMPLOADFN) (
  41. IN PCTSTR LibraryPath
  42. );
  43. BOOL
  44. DynUpdtDebugLog(
  45. IN Winnt32DebugLevel Level,
  46. IN LPCTSTR Text,
  47. IN UINT MessageId,
  48. ...
  49. )
  50. {
  51. va_list arglist;
  52. BOOL b;
  53. TCHAR bigBuffer[1024];
  54. PCTSTR prefix;
  55. DWORD rc = GetLastError ();
  56. //
  57. // this param is never used momentarily
  58. //
  59. MYASSERT (Text);
  60. if (!Text) {
  61. return FALSE;
  62. }
  63. MYASSERT (!MessageId);
  64. if (Level <= Winnt32LogError) {
  65. prefix = TEXT("DUError: ");
  66. } else if (Level == Winnt32LogWarning) {
  67. prefix = TEXT("DUWarning: ");
  68. } else {
  69. prefix = TEXT("DUInfo: ");
  70. }
  71. _sntprintf (bigBuffer, sizeof (bigBuffer) / sizeof (TCHAR), TEXT("%s%s"), prefix, Text);
  72. va_start(arglist,MessageId);
  73. b = DebugLog2 (Level, bigBuffer, MessageId, arglist);
  74. va_end(arglist);
  75. SetLastError (rc);
  76. return b;
  77. }
  78. BOOL
  79. pDoesFileExist (
  80. IN PCTSTR FilePath
  81. )
  82. {
  83. WIN32_FIND_DATA fd;
  84. return FileExists (FilePath, &fd) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  85. }
  86. BOOL
  87. pDoesDirectoryExist (
  88. IN PCTSTR DirPath
  89. )
  90. {
  91. WIN32_FIND_DATA fd;
  92. return FileExists (DirPath, &fd) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  93. }
  94. BOOL
  95. pNonemptyFilePresent (
  96. IN PCTSTR FilePath
  97. )
  98. {
  99. WIN32_FIND_DATA fd;
  100. return FileExists (FilePath, &fd) &&
  101. fd.nFileSizeLow > 0 &&
  102. !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  103. }
  104. BOOL
  105. pGetTargetInfo (
  106. OUT POSVERSIONINFOEX TargetVersion, OPTIONAL
  107. OUT PTSTR TargetPlatform, OPTIONAL
  108. IN DWORD TargetPlatformChars, OPTIONAL
  109. OUT PLCID LocaleID OPTIONAL
  110. )
  111. {
  112. TCHAR buffer[256];
  113. UINT productType;
  114. //
  115. // get some data from the main inf
  116. //
  117. if (!FullInfName[0]) {
  118. if (!FindPathToWinnt32File (InfName, FullInfName, MAX_PATH)) {
  119. DynUpdtDebugLog (
  120. Winnt32LogError,
  121. TEXT("pGetTargetInfo: FindPathToWinnt32File failed"),
  122. 0
  123. );
  124. return FALSE;
  125. }
  126. }
  127. if (TargetVersion) {
  128. if (!GetPrivateProfileString (
  129. TEXT("Miscellaneous"),
  130. TEXT("ProductType"),
  131. TEXT(""),
  132. buffer,
  133. 256,
  134. FullInfName
  135. )) {
  136. DynUpdtDebugLog (
  137. Winnt32LogError,
  138. TEXT("%1 key in [%2] section is missing from %3; aborting operation"),
  139. 0,
  140. TEXT("ProductType"),
  141. TEXT("Miscellaneous"),
  142. FullInfName
  143. );
  144. return FALSE;
  145. }
  146. if (buffer[0] < TEXT('0') || buffer[0] > TEXT('5') || buffer[1]) {
  147. DynUpdtDebugLog (
  148. Winnt32LogError,
  149. TEXT("Invalid %1 value (%2) in %3"),
  150. 0,
  151. TEXT("ProductType"),
  152. buffer,
  153. FullInfName
  154. );
  155. return FALSE;
  156. }
  157. productType = buffer[0] - TEXT('0');
  158. if (!GetPrivateProfileString (
  159. TEXT("Miscellaneous"),
  160. TEXT("ServicePack"),
  161. TEXT(""),
  162. buffer,
  163. 256,
  164. FullInfName
  165. )) {
  166. DynUpdtDebugLog (
  167. Winnt32LogError,
  168. TEXT("%1 key in [%2] section is missing from %3; aborting operation"),
  169. 0,
  170. TEXT("ServicePack"),
  171. TEXT("Miscellaneous"),
  172. FullInfName
  173. );
  174. return FALSE;
  175. }
  176. if (_stscanf (
  177. buffer,
  178. TEXT("%hu.%hu"),
  179. &TargetVersion->wServicePackMajor,
  180. &TargetVersion->wServicePackMinor
  181. ) != 2) {
  182. DynUpdtDebugLog (
  183. Winnt32LogError,
  184. TEXT("Invalid %1 value (%2) in %3"),
  185. 0,
  186. TEXT("ServicePack"),
  187. buffer,
  188. FullInfName
  189. );
  190. return FALSE;
  191. }
  192. TargetVersion->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  193. TargetVersion->dwMajorVersion = VER_PRODUCTMAJORVERSION;
  194. TargetVersion->dwMinorVersion = VER_PRODUCTMINORVERSION;
  195. TargetVersion->dwBuildNumber = VER_PRODUCTBUILD;
  196. TargetVersion->dwPlatformId = VER_PLATFORM_WIN32_NT;
  197. TargetVersion->wSuiteMask = g_MapProductTypeToSuite[productType];
  198. TargetVersion->wProductType = g_MapProductTypeToPT[productType];
  199. }
  200. if (TargetPlatform) {
  201. if (!GetPrivateProfileString (
  202. TEXT("Miscellaneous"),
  203. TEXT("DestinationPlatform"),
  204. TEXT(""),
  205. TargetPlatform,
  206. TargetPlatformChars,
  207. FullInfName
  208. )) {
  209. DynUpdtDebugLog (
  210. Winnt32LogError,
  211. TEXT("%1 key in [%2] section is missing from %3; aborting operation"),
  212. 0,
  213. TEXT("DestinationPlatform"),
  214. TEXT("Miscellaneous"),
  215. FullInfName
  216. );
  217. return FALSE;
  218. }
  219. }
  220. if (LocaleID) {
  221. MYASSERT (SourceNativeLangID);
  222. *LocaleID = SourceNativeLangID;
  223. }
  224. return TRUE;
  225. }
  226. BOOL
  227. pInitializeSupport (
  228. IN PCTSTR ComponentName,
  229. IN PCOMPLOADFN LoadFn,
  230. IN BOOL UseRegistryReplacement
  231. )
  232. {
  233. TCHAR pathSupportLib[MAX_PATH];
  234. if (UseRegistryReplacement) {
  235. HKEY key;
  236. DWORD rc;
  237. BOOL b = FALSE;
  238. rc = RegOpenKey (
  239. HKEY_LOCAL_MACHINE,
  240. TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Setup\\Winnt32\\5.1"),
  241. &key
  242. );
  243. if (rc == ERROR_SUCCESS) {
  244. DWORD size = 0;
  245. rc = RegQueryValueEx (key, ComponentName, NULL, NULL, NULL, &size);
  246. if (rc == ERROR_SUCCESS && size > 0) {
  247. PTSTR buf = MALLOC (size);
  248. if (buf) {
  249. rc = RegQueryValueEx (key, ComponentName, NULL, NULL, (LPBYTE)buf, &size);
  250. if (rc == ERROR_SUCCESS && (*LoadFn) (buf)) {
  251. DynUpdtDebugLog (
  252. Winnt32LogInformation,
  253. TEXT("pInitializeSupport: using registry specified replacement file <%1>"),
  254. 0,
  255. buf
  256. );
  257. b = TRUE;
  258. }
  259. FREE (buf);
  260. }
  261. }
  262. RegCloseKey (key);
  263. }
  264. if (b) {
  265. return TRUE;
  266. }
  267. }
  268. if (FindPathToWinnt32File (ComponentName, pathSupportLib, MAX_PATH)) {
  269. if ((*LoadFn) (pathSupportLib)) {
  270. return TRUE;
  271. }
  272. }
  273. DynUpdtDebugLog (
  274. Winnt32LogError,
  275. TEXT("pInitializeSupport: %1 could not be loaded or is corrupt"),
  276. 0,
  277. ComponentName
  278. );
  279. return FALSE;
  280. }
  281. BOOL
  282. pLoadHwdbLib (
  283. IN PCTSTR LibraryPath
  284. )
  285. {
  286. DWORD rc;
  287. //
  288. // Use WinVerifyTrust first?
  289. //
  290. g_DynUpdtStatus->HwdbLib = LoadLibraryEx (LibraryPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  291. if (!g_DynUpdtStatus->HwdbLib) {
  292. return FALSE;
  293. }
  294. g_DynUpdtStatus->HwdbInitialize = (PHWDBINITIALIZE) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBINITIALIZE);
  295. g_DynUpdtStatus->HwdbTerminate = (PHWDBTERMINATE) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBTERMINATE);
  296. g_DynUpdtStatus->HwdbOpen = (PHWDBOPEN) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBOPEN);
  297. g_DynUpdtStatus->HwdbClose = (PHWDBCLOSE) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBCLOSE);
  298. g_DynUpdtStatus->HwdbAppendInfs = (PHWDBAPPENDINFS) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBAPPENDINFS);
  299. g_DynUpdtStatus->HwdbFlush = (PHWDBFLUSH) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBFLUSH);
  300. g_DynUpdtStatus->HwdbHasDriver = (PHWDBHASDRIVER) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBHASDRIVER);
  301. g_DynUpdtStatus->HwdbHasAnyDriver = (PHWDBHASANYDRIVER) GetProcAddress (g_DynUpdtStatus->HwdbLib, S_HWDBAPI_HWDBHASANYDRIVER);
  302. if (!g_DynUpdtStatus->HwdbInitialize ||
  303. !g_DynUpdtStatus->HwdbTerminate ||
  304. !g_DynUpdtStatus->HwdbOpen ||
  305. !g_DynUpdtStatus->HwdbClose ||
  306. !g_DynUpdtStatus->HwdbAppendInfs ||
  307. !g_DynUpdtStatus->HwdbFlush ||
  308. !g_DynUpdtStatus->HwdbHasDriver ||
  309. !g_DynUpdtStatus->HwdbHasAnyDriver
  310. ) {
  311. g_DynUpdtStatus->HwdbInitialize = NULL;
  312. g_DynUpdtStatus->HwdbTerminate = NULL;
  313. g_DynUpdtStatus->HwdbOpen = NULL;
  314. g_DynUpdtStatus->HwdbClose = NULL;
  315. g_DynUpdtStatus->HwdbAppendInfs = NULL;
  316. g_DynUpdtStatus->HwdbFlush = NULL;
  317. g_DynUpdtStatus->HwdbHasDriver = NULL;
  318. g_DynUpdtStatus->HwdbHasAnyDriver = NULL;
  319. rc = GetLastError ();
  320. FreeLibrary (g_DynUpdtStatus->HwdbLib);
  321. g_DynUpdtStatus->HwdbLib = NULL;
  322. SetLastError (rc);
  323. return FALSE;
  324. }
  325. return TRUE;
  326. }
  327. BOOL
  328. pLoadDuLib (
  329. IN PCTSTR LibraryPath
  330. )
  331. {
  332. DWORD rc;
  333. //
  334. // BUGBUG - Use WinVerifyTrust first?
  335. //
  336. g_DynUpdtStatus->DuLib = LoadLibraryEx (LibraryPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  337. if (!g_DynUpdtStatus->DuLib) {
  338. return FALSE;
  339. }
  340. g_DynUpdtStatus->DuIsSupported = (PDUISSUPPORTED) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_ISSUPPORTED);
  341. g_DynUpdtStatus->DuInitialize = (PDUINITIALIZE) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_INITIALIZE);
  342. g_DynUpdtStatus->DuDoDetection = (PDUDODETECTION) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_DODETECTION);
  343. g_DynUpdtStatus->DuQueryUnsupDrvs = (PDUQUERYUNSUPDRVS) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_QUERYUNSUPDRVS);
  344. g_DynUpdtStatus->DuBeginDownload = (PDUBEGINDOWNLOAD) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_BEGINDOWNLOAD);
  345. g_DynUpdtStatus->DuAbortDownload = (PDUABORTDOWNLOAD) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_ABORTDOWNLOAD);
  346. g_DynUpdtStatus->DuUninitialize = (PDUUNINITIALIZE) GetProcAddress (g_DynUpdtStatus->DuLib, API_DU_UNINITIALIZE);
  347. if (!g_DynUpdtStatus->DuIsSupported ||
  348. !g_DynUpdtStatus->DuInitialize ||
  349. !g_DynUpdtStatus->DuDoDetection ||
  350. !g_DynUpdtStatus->DuQueryUnsupDrvs ||
  351. !g_DynUpdtStatus->DuBeginDownload ||
  352. !g_DynUpdtStatus->DuAbortDownload ||
  353. !g_DynUpdtStatus->DuUninitialize
  354. ) {
  355. DynUpdtDebugLog (
  356. Winnt32LogError,
  357. TEXT("pLoadDuLib: %1 is missing one or more required entry points"),
  358. 0,
  359. LibraryPath
  360. );
  361. g_DynUpdtStatus->DuIsSupported = NULL;
  362. g_DynUpdtStatus->DuInitialize = NULL;
  363. g_DynUpdtStatus->DuDoDetection = NULL;
  364. g_DynUpdtStatus->DuQueryUnsupDrvs = NULL;
  365. g_DynUpdtStatus->DuBeginDownload = NULL;
  366. g_DynUpdtStatus->DuAbortDownload = NULL;
  367. g_DynUpdtStatus->DuUninitialize = NULL;
  368. rc = GetLastError ();
  369. FreeLibrary (g_DynUpdtStatus->DuLib);
  370. g_DynUpdtStatus->DuLib = NULL;
  371. SetLastError (rc);
  372. return FALSE;
  373. }
  374. return TRUE;
  375. }
  376. #ifndef UNICODE
  377. BOOL
  378. pLoadWin9xDuSupport (
  379. VOID
  380. )
  381. {
  382. if (!UpgradeSupport.DllModuleHandle) {
  383. return FALSE;
  384. }
  385. g_DynUpdtStatus->Win9xGetIncompDrvs = (PWIN9XGETINCOMPDRVS)
  386. GetProcAddress (UpgradeSupport.DllModuleHandle, "Win9xGetIncompDrvs");
  387. g_DynUpdtStatus->Win9xReleaseIncompDrvs = (PWIN9XRELEASEINCOMPDRVS)
  388. GetProcAddress (UpgradeSupport.DllModuleHandle, "Win9xReleaseIncompDrvs");
  389. if (!g_DynUpdtStatus->Win9xGetIncompDrvs) {
  390. DynUpdtDebugLog (
  391. Winnt32LogError,
  392. TEXT("Winnt32DuIsSupported: %1 is missing in the upgrade support module"),
  393. 0,
  394. "Win9xGetIncompDrvs"
  395. );
  396. return FALSE;
  397. }
  398. return TRUE;
  399. }
  400. #endif
  401. BOOL
  402. pInitSupportLibs (
  403. VOID
  404. )
  405. {
  406. return (Winnt32Restarted () || pInitializeSupport (S_DUCTRL_DLL, pLoadDuLib, TRUE)) &&
  407. pInitializeSupport (S_HWDB_DLL, pLoadHwdbLib, FALSE) &&
  408. g_DynUpdtStatus->HwdbInitialize (g_DynUpdtStatus->TempDir);
  409. }
  410. BOOL
  411. pInitNtPnpDb (
  412. IN BOOL AllowRebuild
  413. )
  414. {
  415. TCHAR hwdbPath[MAX_PATH];
  416. BOOL b = TRUE;
  417. if (!FindPathToWinnt32File (S_HWCOMP_DAT, hwdbPath, MAX_PATH)) {
  418. DynUpdtDebugLog (Winnt32LogError, TEXT("pInitNtPnpDb: %1 not found"), 0, S_HWCOMP_DAT);
  419. b = FALSE;
  420. }
  421. MYASSERT (g_DynUpdtStatus->HwdbInitialize);
  422. if (b && !g_DynUpdtStatus->HwdbInitialize (g_DynUpdtStatus->TempDir)) {
  423. DynUpdtDebugLog (Winnt32LogError, TEXT("pInitNtPnpDb: HwdbInitialize(%1) FAILED"), 0, g_DynUpdtStatus->TempDir);
  424. b = FALSE;
  425. }
  426. MYASSERT (g_DynUpdtStatus->HwdbOpen);
  427. if (b) {
  428. g_DynUpdtStatus->HwdbDatabase = g_DynUpdtStatus->HwdbOpen (hwdbPath);
  429. if (!g_DynUpdtStatus->HwdbDatabase) {
  430. DynUpdtDebugLog (Winnt32LogError, TEXT("pInitNtPnpDb: HwdbOpen(%1) FAILED"), 0, hwdbPath);
  431. b = FALSE;
  432. }
  433. }
  434. if (!b && AllowRebuild) {
  435. //
  436. // just rebuild the database in memory
  437. //
  438. g_DynUpdtStatus->HwdbDatabase = g_DynUpdtStatus->HwdbOpen (NULL);
  439. if (g_DynUpdtStatus->HwdbDatabase) {
  440. b = g_DynUpdtStatus->HwdbAppendInfs (g_DynUpdtStatus->HwdbDatabase, NativeSourcePaths[0], NULL, NULL);
  441. if (b) {
  442. DynUpdtDebugLog (Winnt32LogWarning, TEXT("pInitNtPnpDb: PnP database was successfully rebuilt"), 0);
  443. //
  444. // also try to save the database
  445. //
  446. if (g_DynUpdtStatus->HwdbFlush) {
  447. BuildPath (hwdbPath, NativeSourcePaths[0], S_HWCOMP_DAT);
  448. g_DynUpdtStatus->HwdbFlush (g_DynUpdtStatus->HwdbDatabase, hwdbPath);
  449. }
  450. }
  451. }
  452. }
  453. return b;
  454. }
  455. BOOL
  456. IsNetConnectivityAvailable (
  457. VOID
  458. )
  459. {
  460. #ifdef UNICODE
  461. return TRUE;
  462. #else
  463. BOOL (*pfnWin9xAnyNetDevicePresent) (VOID);
  464. if (UpgradeSupport.DllModuleHandle) {
  465. (FARPROC)pfnWin9xAnyNetDevicePresent = GetProcAddress (UpgradeSupport.DllModuleHandle, "Win9xAnyNetDevicePresent");
  466. if (pfnWin9xAnyNetDevicePresent) {
  467. return pfnWin9xAnyNetDevicePresent();
  468. }
  469. }
  470. return TRUE;
  471. #endif
  472. }
  473. BOOL
  474. DynamicUpdateIsSupported (
  475. IN HWND ParentWnd
  476. )
  477. {
  478. DWORD rc;
  479. if (g_DynUpdtStatus->Disabled) {
  480. return FALSE;
  481. }
  482. //
  483. // disable this for DTC
  484. //
  485. if (ProductFlavor == DATACENTER_PRODUCTTYPE) {
  486. return FALSE;
  487. }
  488. if (AnyBlockingCompatibilityItems ()) {
  489. //
  490. // no point in supporting DU; setup will stop anyway
  491. //
  492. return FALSE;
  493. }
  494. CleanUpOldLocalSources (ParentWnd);
  495. if (!InspectFilesystems (ParentWnd)) {
  496. DynUpdtDebugLog (Winnt32LogWarning, TEXT("InspectFilesystems blocks DU"), 0);
  497. return FALSE;
  498. }
  499. if (!EnoughMemory (ParentWnd, TRUE)) {
  500. return FALSE;
  501. }
  502. if (!FindLocalSourceAndCheckSpace (ParentWnd, TRUE, DYN_DISKSPACE_PADDING)) {
  503. DynUpdtDebugLog (Winnt32LogWarning, TEXT("Not enough disk space blocks DU"), 0);
  504. return FALSE;
  505. }
  506. if (!g_DynUpdtStatus->SupportQueried) {
  507. g_DynUpdtStatus->SupportQueried = TRUE;
  508. if (g_DynUpdtStatus->DynamicUpdatesSource[0]) {
  509. g_DynUpdtStatus->SupportPresent = TRUE;
  510. } else {
  511. g_DynUpdtStatus->SupportPresent =
  512. Winnt32DuIsSupported () &&
  513. IsNetConnectivityAvailable ();
  514. if (!g_DynUpdtStatus->SupportPresent) {
  515. rc = GetLastError ();
  516. DynamicUpdateUninitialize ();
  517. SetLastError (rc);
  518. }
  519. }
  520. }
  521. return g_DynUpdtStatus->SupportPresent;
  522. }
  523. BOOL
  524. DynamicUpdateInitDownload (
  525. IN HWND hNotifyWnd
  526. )
  527. {
  528. DYNUPDT_INIT dynUpdtInit;
  529. MYASSERT (!g_DynUpdtStatus->Disabled);
  530. MYASSERT (!Winnt32Restarted ());
  531. MyDelnode (g_DynUpdtStatus->WorkingDir);
  532. BuildPath (g_DynUpdtStatus->TempDir, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_TEMP);
  533. if (CreateMultiLevelDirectory (g_DynUpdtStatus->TempDir) != ERROR_SUCCESS) {
  534. DynUpdtDebugLog (
  535. Winnt32LogError,
  536. TEXT("DynamicUpdateInitDownload: CreateMultiLevelDirectory failed"),
  537. 0
  538. );
  539. return FALSE;
  540. }
  541. if (!pGetTargetInfo (
  542. &g_DynUpdtStatus->TargetOsVersion,
  543. NULL,
  544. 0,
  545. &g_DynUpdtStatus->TargetLCID
  546. )) {
  547. return FALSE;
  548. }
  549. dynUpdtInit.TargetOsVersion = &g_DynUpdtStatus->TargetOsVersion;
  550. dynUpdtInit.TargetPlatform = g_DynUpdtStatus->TargetPlatform;
  551. dynUpdtInit.TargetLCID = g_DynUpdtStatus->TargetLCID;
  552. dynUpdtInit.Upgrade = Upgrade;
  553. dynUpdtInit.SourceDirs = info.SourceDirectories;
  554. dynUpdtInit.SourceDirsCount = SourceCount;
  555. dynUpdtInit.Unattend = UnattendSwitchSpecified;
  556. dynUpdtInit.AnswerFile = UnattendedScriptFile;
  557. dynUpdtInit.ProgressWindow = hNotifyWnd;
  558. dynUpdtInit.DownloadRoot = g_DynUpdtStatus->WorkingDir;
  559. dynUpdtInit.TempDir = g_DynUpdtStatus->TempDir;
  560. return Winnt32DuInitialize (&dynUpdtInit);
  561. }
  562. BOOL
  563. DynamicUpdateStart (
  564. OUT PDWORD TotalEstimatedTime,
  565. OUT PDWORD TotalEstimatedSize
  566. )
  567. {
  568. if (g_DynUpdtStatus->Disabled) {
  569. return FALSE;
  570. }
  571. return Winnt32DuInitiateGetUpdates (TotalEstimatedTime, TotalEstimatedSize);
  572. }
  573. VOID
  574. DynamicUpdateCancel (
  575. VOID
  576. )
  577. {
  578. if (g_DynUpdtStatus->Disabled) {
  579. return;
  580. }
  581. Winnt32DuCancel ();
  582. }
  583. BOOL
  584. DynamicUpdateProcessFiles (
  585. OUT PBOOL StopSetup
  586. )
  587. {
  588. if (g_DynUpdtStatus->Disabled) {
  589. return TRUE;
  590. }
  591. return Winnt32DuProcessFiles (StopSetup);
  592. }
  593. BOOL
  594. DynamicUpdateWriteParams (
  595. IN PCTSTR ParamsFile
  596. )
  597. {
  598. return Winnt32DuWriteParams (ParamsFile);
  599. }
  600. VOID
  601. DynamicUpdateUninitialize (
  602. VOID
  603. )
  604. {
  605. if (g_DynUpdtStatus->Disabled) {
  606. return;
  607. }
  608. Winnt32DuUninitialize ();
  609. }
  610. BOOL
  611. DynamicUpdatePrepareRestart (
  612. VOID
  613. )
  614. {
  615. PCTSTR prevCmdLine;
  616. DWORD size;
  617. #ifdef _X86_
  618. TCHAR reportNum[16];
  619. #endif
  620. if (g_DynUpdtStatus->Disabled) {
  621. return FALSE;
  622. }
  623. #define S_ARG_RESTART TEXT("Restart")
  624. if (!UnattendedOperation) {
  625. //
  626. // build the restart answer file
  627. //
  628. BuildPath (g_DynUpdtStatus->RestartAnswerFile, g_DynUpdtStatus->WorkingDir, S_RESTART_TXT);
  629. #ifdef _X86_
  630. wsprintf (reportNum, TEXT("%u"), g_UpgradeReportMode);
  631. #endif
  632. //
  633. // write data to the restart answer file
  634. //
  635. if (!WritePrivateProfileString (
  636. WINNT_UNATTENDED,
  637. ISNT() ? WINNT_D_NTUPGRADE : WINNT_D_WIN95UPGRADE,
  638. Upgrade ? WINNT_A_YES : WINNT_A_NO,
  639. g_DynUpdtStatus->RestartAnswerFile
  640. ) ||
  641. ProductId[0] && !WritePrivateProfileString (
  642. WINNT_USERDATA,
  643. WINNT_US_PRODUCTKEY,
  644. ProductId,
  645. g_DynUpdtStatus->RestartAnswerFile
  646. ) ||
  647. !WritePrivateProfileString (
  648. WINNT_UNATTENDED,
  649. WINNT_U_DYNAMICUPDATESHARE,
  650. g_DynUpdtStatus->DynamicUpdatesSource,
  651. g_DynUpdtStatus->RestartAnswerFile
  652. ) ||
  653. #ifdef _X86_
  654. !WritePrivateProfileString (
  655. WINNT_UNATTENDED,
  656. WINNT_D_REPORTMODE,
  657. reportNum,
  658. g_DynUpdtStatus->RestartAnswerFile
  659. ) ||
  660. #endif
  661. (ForceNTFSConversion &&
  662. !WritePrivateProfileString (
  663. WINNT_UNATTENDED,
  664. TEXT("ForceNTFSConversion"),
  665. WINNT_A_YES,
  666. g_DynUpdtStatus->RestartAnswerFile
  667. )) ||
  668. !SaveAdvancedOptions (
  669. g_DynUpdtStatus->RestartAnswerFile
  670. ) ||
  671. !SaveLanguageOptions (
  672. g_DynUpdtStatus->RestartAnswerFile
  673. ) ||
  674. !SaveAccessibilityOptions (
  675. g_DynUpdtStatus->RestartAnswerFile
  676. )
  677. ) {
  678. return FALSE;
  679. }
  680. }
  681. prevCmdLine = GetCommandLine ();
  682. size = (lstrlen (prevCmdLine) + 1 + 1) * sizeof (TCHAR) + sizeof (S_ARG_RESTART) +
  683. (UnattendedOperation ? 0 : (1 + lstrlen (g_DynUpdtStatus->RestartAnswerFile)) * sizeof (TCHAR));
  684. g_DynUpdtStatus->RestartCmdLine = HeapAlloc (GetProcessHeap (), 0, size);
  685. if (!g_DynUpdtStatus->RestartCmdLine) {
  686. return FALSE;
  687. }
  688. wsprintf (
  689. g_DynUpdtStatus->RestartCmdLine,
  690. UnattendedOperation ? TEXT("%s /%s") : TEXT("%s /%s:%s"),
  691. prevCmdLine,
  692. S_ARG_RESTART,
  693. g_DynUpdtStatus->RestartAnswerFile
  694. );
  695. return TRUE;
  696. }
  697. BOOL
  698. pComputeChecksum (
  699. IN PCTSTR FileName,
  700. OUT PDWORD Chksum
  701. )
  702. {
  703. DWORD chksum, size, dwords, bytes;
  704. HANDLE hFile, hMap;
  705. PVOID viewBase;
  706. PDWORD base, limit;
  707. PBYTE base2;
  708. DWORD rc;
  709. rc = MapFileForRead (FileName, &size, &hFile, &hMap, &viewBase);
  710. if (rc != ERROR_SUCCESS) {
  711. SetLastError (rc);
  712. return FALSE;
  713. }
  714. dwords = size / sizeof (DWORD);
  715. base = (PDWORD)viewBase;
  716. limit = base + dwords;
  717. chksum = 0;
  718. while (base < limit) {
  719. chksum += *base;
  720. base++;
  721. }
  722. bytes = size % sizeof (DWORD);
  723. base2 = (PBYTE)base;
  724. while (bytes) {
  725. chksum += *base2;
  726. base2++;
  727. bytes--;
  728. }
  729. UnmapFile (hMap, viewBase);
  730. CloseHandle (hFile);
  731. *Chksum = chksum;
  732. return TRUE;
  733. }
  734. BOOL
  735. pGetFiletimeStamps (
  736. IN PCTSTR FileName,
  737. OUT PFILETIME CreationTime,
  738. OUT PFILETIME LastWriteTime
  739. )
  740. {
  741. WIN32_FIND_DATA fd;
  742. HANDLE h;
  743. h = FindFirstFile (FileName, &fd);
  744. if (h == INVALID_HANDLE_VALUE) {
  745. return FALSE;
  746. }
  747. FindClose (h);
  748. *CreationTime = fd.ftCreationTime;
  749. *LastWriteTime = fd.ftLastWriteTime;
  750. return TRUE;
  751. }
  752. BOOL
  753. pSaveLastDownloadInfo (
  754. VOID
  755. )
  756. {
  757. SYSTEMTIME currentTime;
  758. DWORD chksum;
  759. FILETIME ftCreationTime;
  760. FILETIME ftLastWriteTime;
  761. ULONGLONG data[2];
  762. DWORD rc;
  763. HKEY key;
  764. TCHAR keyName[MAX_PATH];
  765. TCHAR filePath[MAX_PATH];
  766. PTSTR p;
  767. //
  768. // we always want to get to the CD dosnet.inf (in the same directory as winnt32.exe)
  769. //
  770. if (!GetModuleFileName (NULL, filePath, MAX_PATH)) {
  771. return FALSE;
  772. }
  773. p = _tcsrchr (filePath, TEXT('\\'));
  774. if (!p) {
  775. return FALSE;
  776. }
  777. lstrcpy (p + 1, InfName);
  778. GetCurrentWinnt32RegKey (keyName, MAX_PATH);
  779. ConcatenatePaths (keyName, WINNT_U_DYNAMICUPDATESHARE, MAX_PATH);
  780. rc = RegCreateKey (HKEY_LOCAL_MACHINE, keyName, &key);
  781. if (rc == ERROR_SUCCESS) {
  782. GetSystemTime (&currentTime);
  783. rc = RegSetValueEx (
  784. key,
  785. TEXT("LastDownloadTime"),
  786. 0,
  787. REG_BINARY,
  788. (CONST BYTE *) (&currentTime),
  789. sizeof (currentTime)
  790. );
  791. if (rc == ERROR_SUCCESS) {
  792. rc = RegSetValueEx (
  793. key,
  794. TEXT(""),
  795. 0,
  796. REG_SZ,
  797. (CONST BYTE *) g_DynUpdtStatus->DynamicUpdatesSource,
  798. (lstrlen (g_DynUpdtStatus->DynamicUpdatesSource) + 1) * sizeof (TCHAR)
  799. );
  800. }
  801. if (rc == ERROR_SUCCESS) {
  802. if (pComputeChecksum (filePath, &chksum)) {
  803. rc = RegSetValueEx (
  804. key,
  805. TEXT("Checksum"),
  806. 0,
  807. REG_DWORD,
  808. (CONST BYTE *) (&chksum),
  809. sizeof (chksum)
  810. );
  811. }
  812. }
  813. if (rc == ERROR_SUCCESS) {
  814. if (pGetFiletimeStamps (filePath, &ftCreationTime, &ftLastWriteTime)) {
  815. data[0] = ((ULONGLONG)ftCreationTime.dwHighDateTime << 32) | (ULONGLONG)ftCreationTime.dwLowDateTime;
  816. data[1] = ((ULONGLONG)ftLastWriteTime.dwHighDateTime << 32 ) | (ULONGLONG)ftLastWriteTime.dwLowDateTime;
  817. rc = RegSetValueEx (
  818. key,
  819. TEXT("TimeStamp"),
  820. 0,
  821. REG_BINARY,
  822. (CONST BYTE *)data,
  823. sizeof (data)
  824. );
  825. }
  826. }
  827. RegCloseKey (key);
  828. }
  829. if (rc != ERROR_SUCCESS) {
  830. SetLastError (rc);
  831. }
  832. return rc == ERROR_SUCCESS;
  833. }
  834. BOOL
  835. pGetRecentDUShare (
  836. IN DWORD MaxElapsedSeconds
  837. )
  838. {
  839. SYSTEMTIME lastDownload, currentTime;
  840. ULONGLONG lastDownloadIn100Ns, currentTimeIn100Ns;
  841. ULONGLONG difference;
  842. DWORD rc, size, type;
  843. HKEY key = NULL;
  844. BOOL b = FALSE;
  845. PTSTR duShare = NULL;
  846. TCHAR keyName[MAX_PATH];
  847. FILETIME ftCreationTime;
  848. FILETIME ftLastWriteTime;
  849. ULONGLONG data[2], storedData[2];
  850. DWORD chksum, storedChksum;
  851. TCHAR filePath[MAX_PATH];
  852. PTSTR p;
  853. if (!GetModuleFileName (NULL, filePath, MAX_PATH)) {
  854. return FALSE;
  855. }
  856. p = _tcsrchr (filePath, TEXT('\\'));
  857. if (!p) {
  858. return FALSE;
  859. }
  860. lstrcpy (p + 1, InfName);
  861. GetCurrentWinnt32RegKey (keyName, MAX_PATH);
  862. ConcatenatePaths (keyName, WINNT_U_DYNAMICUPDATESHARE, MAX_PATH);
  863. rc = RegOpenKeyEx (
  864. HKEY_LOCAL_MACHINE,
  865. keyName,
  866. 0,
  867. KEY_READ,
  868. &key
  869. );
  870. if (rc == ERROR_SUCCESS) {
  871. size = sizeof (lastDownload);
  872. rc = RegQueryValueEx (
  873. key,
  874. TEXT("LastDownloadTime"),
  875. NULL,
  876. &type,
  877. (PBYTE) (&lastDownload),
  878. &size
  879. );
  880. }
  881. if (rc == ERROR_SUCCESS && type == REG_BINARY && size == sizeof (lastDownload)) {
  882. //
  883. // Compare current time to report time
  884. //
  885. GetSystemTime (&currentTime);
  886. lastDownloadIn100Ns = SystemTimeToFileTime64 (&lastDownload);
  887. currentTimeIn100Ns = SystemTimeToFileTime64 (&currentTime);
  888. if (currentTimeIn100Ns > lastDownloadIn100Ns) {
  889. //
  890. // Compute difference in seconds
  891. //
  892. difference = currentTimeIn100Ns - lastDownloadIn100Ns;
  893. difference /= (10 * 1000 * 1000);
  894. if (difference < MaxElapsedSeconds) {
  895. b = TRUE;
  896. }
  897. }
  898. }
  899. if (b) {
  900. b = FALSE;
  901. rc = RegQueryValueEx (
  902. key,
  903. TEXT(""),
  904. NULL,
  905. &type,
  906. NULL,
  907. &size
  908. );
  909. if (rc == ERROR_SUCCESS && type == REG_SZ && size > 0 && size <= sizeof (g_DynUpdtStatus->DynamicUpdatesSource)) {
  910. duShare = MALLOC (size);
  911. if (duShare) {
  912. rc = RegQueryValueEx (
  913. key,
  914. TEXT(""),
  915. NULL,
  916. NULL,
  917. (LPBYTE)duShare,
  918. &size
  919. );
  920. if (rc == ERROR_SUCCESS && pDoesDirectoryExist (duShare)) {
  921. b = TRUE;
  922. } else {
  923. FREE (duShare);
  924. duShare = NULL;
  925. }
  926. }
  927. }
  928. }
  929. if (b) {
  930. b = FALSE;
  931. if (pGetFiletimeStamps (filePath, &ftCreationTime, &ftLastWriteTime)) {
  932. rc = RegQueryValueEx (
  933. key,
  934. TEXT("TimeStamp"),
  935. 0,
  936. &type,
  937. (LPBYTE)storedData,
  938. &size
  939. );
  940. if (rc == ERROR_SUCCESS && type == REG_BINARY) {
  941. data[0] = ((ULONGLONG)ftCreationTime.dwHighDateTime << 32) | (ULONGLONG)ftCreationTime.dwLowDateTime;
  942. data[1] = ((ULONGLONG)ftLastWriteTime.dwHighDateTime << 32 ) | (ULONGLONG)ftLastWriteTime.dwLowDateTime;
  943. if (data[0] == storedData[0] && data[1] == storedData[1]) {
  944. b = TRUE;
  945. }
  946. }
  947. }
  948. }
  949. if (b) {
  950. b = FALSE;
  951. if (pComputeChecksum (filePath, &chksum)) {
  952. rc = RegQueryValueEx (
  953. key,
  954. TEXT("Checksum"),
  955. NULL,
  956. &type,
  957. (LPBYTE)&storedChksum,
  958. &size
  959. );
  960. if (rc == ERROR_SUCCESS && type == REG_DWORD && storedChksum == chksum) {
  961. b = TRUE;
  962. }
  963. }
  964. }
  965. if (!b && duShare) {
  966. FREE (duShare);
  967. duShare = NULL;
  968. }
  969. if (duShare) {
  970. MYASSERT (b);
  971. MYASSERT (!g_DynUpdtStatus->DynamicUpdatesSource[0]);
  972. lstrcpy (g_DynUpdtStatus->DynamicUpdatesSource, duShare);
  973. RemoveTrailingWack (g_DynUpdtStatus->DynamicUpdatesSource);
  974. g_DynUpdtStatus->UserSpecifiedUpdates = TRUE;
  975. }
  976. if (key) {
  977. RegCloseKey (key);
  978. }
  979. return b;
  980. }
  981. BOOL
  982. DynamicUpdateInitialize (
  983. VOID
  984. )
  985. {
  986. if (g_DynUpdtStatus->Disabled) {
  987. return TRUE;
  988. }
  989. if (!MyGetWindowsDirectory (g_DynUpdtStatus->WorkingDir, MAX_PATH)) {
  990. return FALSE;
  991. }
  992. ConcatenatePaths (g_DynUpdtStatus->WorkingDir, S_DOWNLOAD_ROOT, MAX_PATH);
  993. BuildPath (g_DynUpdtStatus->TempDir, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_TEMP);
  994. if (!CheckUpgradeOnly && !g_DynUpdtStatus->UserSpecifiedUpdates) {
  995. if (pGetRecentDUShare (MAX_UPGCHK_ELAPSED_SECONDS)) {
  996. g_DynUpdtStatus->PreserveWorkingDir = TRUE;
  997. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Using recent share %1"), 0, g_DynUpdtStatus->DynamicUpdatesSource);
  998. }
  999. }
  1000. if (!g_DynUpdtStatus->PreserveWorkingDir && !Winnt32Restarted ()) {
  1001. MyDelnode (g_DynUpdtStatus->WorkingDir);
  1002. }
  1003. if (g_DynUpdtStatus->UserSpecifiedUpdates) {
  1004. BuildPath (g_DynUpdtStatus->DriversSource, g_DynUpdtStatus->DynamicUpdatesSource, S_SUBDIRNAME_DRIVERS);
  1005. } else {
  1006. BuildPath (g_DynUpdtStatus->DriversSource, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_DRIVERS);
  1007. }
  1008. BuildPath (g_DynUpdtStatus->SelectedDrivers, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_DRIVERS);
  1009. if (Winnt32Restarted ()) {
  1010. BuildPath (g_DynUpdtStatus->Winnt32Path, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_WINNT32);
  1011. } else {
  1012. BuildPath (
  1013. g_DynUpdtStatus->Winnt32Path,
  1014. g_DynUpdtStatus->UserSpecifiedUpdates ?
  1015. g_DynUpdtStatus->DynamicUpdatesSource :
  1016. g_DynUpdtStatus->WorkingDir,
  1017. S_SUBDIRNAME_WINNT32
  1018. );
  1019. }
  1020. if (!pGetTargetInfo (
  1021. NULL,
  1022. g_DynUpdtStatus->TargetPlatform,
  1023. sizeof (g_DynUpdtStatus->TargetPlatform) / sizeof (TCHAR),
  1024. NULL
  1025. )) {
  1026. return FALSE;
  1027. }
  1028. return TRUE;
  1029. }
  1030. PTSTR
  1031. GetFileExtension (
  1032. IN PCTSTR FileSpec
  1033. )
  1034. {
  1035. PTSTR p;
  1036. p = _tcsrchr (FileSpec, TEXT('.'));
  1037. if (p && _tcschr (p, TEXT('\\'))) {
  1038. p = NULL;
  1039. }
  1040. return p;
  1041. }
  1042. VOID
  1043. BuildSifName (
  1044. IN PCTSTR CabName,
  1045. OUT PTSTR SifName
  1046. )
  1047. {
  1048. PTSTR p;
  1049. lstrcpy (SifName, CabName);
  1050. p = GetFileExtension (SifName);
  1051. if (!p) {
  1052. p = _tcschr (SifName, 0);
  1053. }
  1054. lstrcpy (p, TEXT(".sif"));
  1055. }
  1056. BOOL
  1057. WINAPI
  1058. Winnt32QueryCallback (
  1059. IN DWORD SetupQueryId,
  1060. IN PVOID InData,
  1061. IN DWORD InDataSize,
  1062. IN OUT PVOID OutData, OPTIONAL
  1063. IN OUT PDWORD OutDataSize
  1064. )
  1065. {
  1066. BOOL b = FALSE;
  1067. BOOL bException = FALSE;
  1068. switch (SetupQueryId) {
  1069. case SETUPQUERYID_PNPID:
  1070. {
  1071. PPNPID_INFO p;
  1072. PTSTR listPnpIds = NULL;
  1073. if (!OutData ||
  1074. !OutDataSize ||
  1075. *OutDataSize < sizeof (PNPID_INFO)
  1076. ) {
  1077. SetLastError (ERROR_INVALID_PARAMETER);
  1078. break;
  1079. }
  1080. if (!g_DynUpdtStatus->HwdbHasAnyDriver) {
  1081. SetLastError (ERROR_INVALID_FUNCTION);
  1082. break;
  1083. }
  1084. __try {
  1085. p = (PPNPID_INFO)OutData;
  1086. if (g_DynUpdtStatus->HwdbDatabase) {
  1087. #ifdef UNICODE
  1088. listPnpIds = MultiSzAnsiToUnicode ((PCSTR)InData);
  1089. #else
  1090. listPnpIds = (PSTR)InData;
  1091. #endif
  1092. p->Handled = (*g_DynUpdtStatus->HwdbHasAnyDriver) (
  1093. g_DynUpdtStatus->HwdbDatabase,
  1094. listPnpIds,
  1095. &p->Unsupported
  1096. );
  1097. } else {
  1098. //
  1099. // disable all driver downloads by doing this
  1100. //
  1101. p->Handled = TRUE;
  1102. }
  1103. }
  1104. __except (EXCEPTION_EXECUTE_HANDLER) {
  1105. SetLastError (GetExceptionCode());
  1106. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32QueryCallback: HwdbHasAnyDriver threw an exception"), 0);
  1107. bException = TRUE;
  1108. p->Handled = TRUE;
  1109. }
  1110. if (bException) {
  1111. __try {
  1112. //
  1113. // bad string passed back, or some internal error
  1114. // try to print the string
  1115. //
  1116. if (listPnpIds) {
  1117. PTSTR multisz = CreatePrintableString (listPnpIds);
  1118. DynUpdtDebugLog (Winnt32LogError, TEXT(" - The string was %1"), 0, multisz);
  1119. FREE (multisz);
  1120. }
  1121. }
  1122. __except (EXCEPTION_EXECUTE_HANDLER) {
  1123. DynUpdtDebugLog (Winnt32LogError, TEXT(" - Bad string"), 0);
  1124. }
  1125. }
  1126. #ifdef UNICODE
  1127. if (listPnpIds) {
  1128. FREE (listPnpIds);
  1129. }
  1130. #endif
  1131. b = TRUE;
  1132. }
  1133. break;
  1134. case SETUPQUERYID_DOWNLOADDRIVER:
  1135. {
  1136. if (!OutData ||
  1137. !OutDataSize ||
  1138. *OutDataSize < sizeof (BOOL)
  1139. ) {
  1140. SetLastError (ERROR_INVALID_PARAMETER);
  1141. break;
  1142. }
  1143. b = TRUE;
  1144. }
  1145. break;
  1146. }
  1147. return b;
  1148. }
  1149. BOOL
  1150. WINAPI
  1151. Winnt32DuIsSupported (
  1152. VOID
  1153. )
  1154. {
  1155. BOOL b;
  1156. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuIsSupported"), 0);
  1157. #ifndef UNICODE
  1158. if (!pLoadWin9xDuSupport ()) {
  1159. DynUpdtDebugLog (
  1160. Winnt32LogWarning,
  1161. TEXT("Winnt32DuIsSupported: %1 support module not loaded; no drivers will be downloaded"),
  1162. 0,
  1163. "w95upg.dll"
  1164. );
  1165. }
  1166. #endif
  1167. TRY {
  1168. b = pInitSupportLibs () &&
  1169. (Winnt32Restarted () || g_DynUpdtStatus->DuIsSupported ());
  1170. }
  1171. EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  1172. SetLastError (_exception_code ());
  1173. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuIsSupported: an exception occured"), 0);
  1174. }
  1175. END_EXCEPT
  1176. #ifndef UNICODE
  1177. if (!b) {
  1178. g_DynUpdtStatus->Win9xGetIncompDrvs = NULL;
  1179. g_DynUpdtStatus->Win9xReleaseIncompDrvs = NULL;
  1180. }
  1181. #endif
  1182. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuIsSupported (%1!d!)"), 0, b);
  1183. return b;
  1184. }
  1185. BOOL
  1186. WINAPI
  1187. Winnt32DuInitialize (
  1188. IN PDYNUPDT_INIT InitData
  1189. )
  1190. {
  1191. DWORD rc;
  1192. BOOL b = FALSE;
  1193. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuInitialize"), 0);
  1194. __try {
  1195. MYASSERT (InitData);
  1196. MYASSERT (InitData->TempDir);
  1197. MYASSERT (g_DynUpdtStatus->DuInitialize);
  1198. MYASSERT (!Winnt32Restarted ());
  1199. if (CreateMultiLevelDirectory (InitData->TempDir) != ERROR_SUCCESS) {
  1200. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuInitialize: CreateDirectory(%1) FAILED"), 0, InitData->TempDir);
  1201. __leave;
  1202. }
  1203. //
  1204. // initialize the Whistler PNP database
  1205. //
  1206. if (!g_DynUpdtStatus->HwdbDatabase) {
  1207. //
  1208. // ignore db load error
  1209. //
  1210. pInitNtPnpDb (FALSE);
  1211. }
  1212. TRY {
  1213. //
  1214. // if a download source already exists, no need to initialize the control
  1215. // since no download will be really necessary
  1216. //
  1217. MYASSERT (!g_DynUpdtStatus->DynamicUpdatesSource[0]);
  1218. g_DynUpdtStatus->Connection = (*g_DynUpdtStatus->DuInitialize) (
  1219. InitData->DownloadRoot,
  1220. InitData->TempDir,
  1221. InitData->TargetOsVersion,
  1222. InitData->TargetPlatform,
  1223. InitData->TargetLCID,
  1224. InitData->Unattend,
  1225. InitData->Upgrade,
  1226. Winnt32QueryCallback
  1227. );
  1228. if (g_DynUpdtStatus->Connection == INVALID_HANDLE_VALUE) {
  1229. DynUpdtDebugLog (
  1230. Winnt32LogError,
  1231. TEXT("DuInitialize FAILED"),
  1232. 0
  1233. );
  1234. __leave;
  1235. }
  1236. g_DynUpdtStatus->ProgressWindow = InitData->ProgressWindow;
  1237. b = TRUE;
  1238. }
  1239. EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  1240. SetLastError (_exception_code ());
  1241. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuInitialize: an exception occured"), 0);
  1242. }
  1243. END_EXCEPT
  1244. }
  1245. __finally {
  1246. if (!b) {
  1247. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuInitialize FAILED"), 0);
  1248. }
  1249. }
  1250. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuInitialize (%1!d!)"), 0, b);
  1251. return b;
  1252. }
  1253. BOOL
  1254. WINAPI
  1255. Winnt32DuInitiateGetUpdates (
  1256. OUT PDWORD TotalEstimatedTime,
  1257. OUT PDWORD TotalEstimatedSize
  1258. )
  1259. {
  1260. BOOL b = FALSE;
  1261. #ifndef UNICODE
  1262. PSTR* incompWin9xDrivers;
  1263. PSTRINGLIST listEntry;
  1264. PCSTR* q;
  1265. #endif
  1266. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuInitiateGetUpdates"), 0);
  1267. if (g_DynUpdtStatus->Connection != INVALID_HANDLE_VALUE &&
  1268. g_DynUpdtStatus->DuDoDetection &&
  1269. g_DynUpdtStatus->DuBeginDownload
  1270. ) {
  1271. TRY {
  1272. #ifdef UNICODE
  1273. b = (*g_DynUpdtStatus->DuDoDetection) (g_DynUpdtStatus->Connection, TotalEstimatedTime, TotalEstimatedSize);
  1274. if (!b) {
  1275. DynUpdtDebugLog (
  1276. DynUpdtLogLevel,
  1277. TEXT("DuDoDetection returned FALSE; no files will be downloaded"),
  1278. 0
  1279. );
  1280. }
  1281. #else
  1282. b = TRUE;
  1283. g_DynUpdtStatus->IncompatibleDriversCount = 0;
  1284. if (g_DynUpdtStatus->Win9xGetIncompDrvs) {
  1285. //
  1286. // let the upgrade module do detection on Win95
  1287. //
  1288. b = (*g_DynUpdtStatus->Win9xGetIncompDrvs) (&incompWin9xDrivers);
  1289. if (b) {
  1290. b = (*g_DynUpdtStatus->DuQueryUnsupDrvs) (
  1291. g_DynUpdtStatus->Connection,
  1292. incompWin9xDrivers,
  1293. TotalEstimatedTime,
  1294. TotalEstimatedSize
  1295. );
  1296. if (incompWin9xDrivers) {
  1297. for (q = incompWin9xDrivers; *q; q++) {
  1298. g_DynUpdtStatus->IncompatibleDriversCount++;
  1299. }
  1300. }
  1301. if (g_DynUpdtStatus->Win9xReleaseIncompDrvs) {
  1302. (*g_DynUpdtStatus->Win9xReleaseIncompDrvs) (incompWin9xDrivers);
  1303. }
  1304. if (!b) {
  1305. DynUpdtDebugLog (
  1306. DynUpdtLogLevel,
  1307. TEXT("DuQueryUnsupportedDrivers returned FALSE; no files will be downloaded"),
  1308. 0
  1309. );
  1310. }
  1311. } else {
  1312. DynUpdtDebugLog (
  1313. DynUpdtLogLevel,
  1314. TEXT("Win9xGetIncompDrvs returned FALSE; no files will be downloaded"),
  1315. 0
  1316. );
  1317. }
  1318. }
  1319. #endif
  1320. if (b) {
  1321. b = (*g_DynUpdtStatus->DuBeginDownload) (g_DynUpdtStatus->Connection, g_DynUpdtStatus->ProgressWindow);
  1322. if (!b) {
  1323. DynUpdtDebugLog (
  1324. DynUpdtLogLevel,
  1325. TEXT("DuBeginDownload returned FALSE; no files will be downloaded"),
  1326. 0
  1327. );
  1328. }
  1329. }
  1330. }
  1331. EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  1332. SetLastError (_exception_code ());
  1333. DynUpdtDebugLog (
  1334. Winnt32LogError,
  1335. TEXT("Winnt32DuInitiateGetUpdates: an exception occured; no files will be downloaded"),
  1336. 0
  1337. );
  1338. }
  1339. END_EXCEPT
  1340. } else {
  1341. SetLastError (ERROR_INVALID_FUNCTION);
  1342. }
  1343. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuInitiateGetUpdates (%1!d!)"), 0, b);
  1344. return b;
  1345. }
  1346. VOID
  1347. WINAPI
  1348. Winnt32DuCancel (
  1349. VOID
  1350. )
  1351. {
  1352. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuCancel"), 0);
  1353. TRY {
  1354. if (g_DynUpdtStatus->Connection != INVALID_HANDLE_VALUE &&
  1355. g_DynUpdtStatus->DuAbortDownload
  1356. ) {
  1357. (*g_DynUpdtStatus->DuAbortDownload) (g_DynUpdtStatus->Connection);
  1358. }
  1359. //
  1360. // BUGBUG - is this definitive?
  1361. //
  1362. }
  1363. EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  1364. SetLastError (_exception_code ());
  1365. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuCancel: an exception occured"), 0);
  1366. }
  1367. END_EXCEPT
  1368. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuCancel"), 0);
  1369. }
  1370. BOOL
  1371. WINAPI
  1372. Winnt32DuProcessFiles (
  1373. OUT PBOOL StopSetup
  1374. )
  1375. {
  1376. BOOL b;
  1377. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuProcessFiles"), 0);
  1378. b = ProcessDownloadedFiles (StopSetup);
  1379. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuProcessFiles (%1!d!)"), 0, b);
  1380. return b;
  1381. }
  1382. BOOL
  1383. WINAPI
  1384. Winnt32DuWriteParams (
  1385. IN PCTSTR ParamsFile
  1386. )
  1387. {
  1388. PSDLIST p;
  1389. PSTRINGLIST q;
  1390. DWORD len1, len2;
  1391. PTSTR pathList1 = NULL, pathList2 = NULL;
  1392. PTSTR append1, append2;
  1393. BOOL b = TRUE;
  1394. if (!DynamicUpdateSuccessful ()) {
  1395. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Winnt32DuWriteParams: disabled because DU did not succeed"), 0);
  1396. return TRUE;
  1397. }
  1398. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuWriteParams"), 0);
  1399. //
  1400. // store paths to all downloaded drivers in a key in the answer file,
  1401. // so later on Textmode Setup (or GUI setup) will append this list to the OemPnPDriversPath
  1402. //
  1403. if (g_DynUpdtStatus->NewDriversList) {
  1404. len1 = len2 = 0;
  1405. for (p = g_DynUpdtStatus->NewDriversList; p; p = p->Next) {
  1406. if (p->Data) {
  1407. len1 += lstrlen (p->String) + 1;
  1408. } else {
  1409. len2 += lstrlen (p->String) + 1;
  1410. }
  1411. }
  1412. if (len1) {
  1413. pathList1 = (PTSTR) MALLOC (len1 * sizeof (TCHAR));
  1414. if (!pathList1) {
  1415. b = FALSE;
  1416. goto exit;
  1417. }
  1418. *pathList1 = 0;
  1419. append1 = pathList1;
  1420. }
  1421. if (len2) {
  1422. pathList2 = (PTSTR) MALLOC (len2 * sizeof (TCHAR));
  1423. if (!pathList2) {
  1424. FREE (pathList1);
  1425. b = FALSE;
  1426. goto exit;
  1427. }
  1428. *pathList2 = 0;
  1429. append2 = pathList2;
  1430. }
  1431. for (p = g_DynUpdtStatus->NewDriversList; p; p = p->Next) {
  1432. if (p->Data) {
  1433. if (append1 != pathList1) {
  1434. *append1++ = TEXT(',');
  1435. }
  1436. lstrcpy (append1, p->String);
  1437. append1 = _tcschr (append1, 0);
  1438. } else {
  1439. if (append2 != pathList2) {
  1440. *append2++ = TEXT(',');
  1441. }
  1442. lstrcpy (append2, p->String);
  1443. append2 = _tcschr (append2, 0);
  1444. }
  1445. }
  1446. if (len1) {
  1447. if (!WritePrivateProfileString (
  1448. WINNT_SETUPPARAMS,
  1449. WINNT_SP_DYNUPDTADDITIONALGUIDRIVERS,
  1450. pathList1,
  1451. ParamsFile
  1452. )) {
  1453. b = FALSE;
  1454. }
  1455. }
  1456. if (b && len2) {
  1457. if (!WritePrivateProfileString (
  1458. WINNT_SETUPPARAMS,
  1459. WINNT_SP_DYNUPDTADDITIONALPOSTGUIDRIVERS,
  1460. pathList2,
  1461. ParamsFile
  1462. )) {
  1463. b = FALSE;
  1464. }
  1465. }
  1466. if (pathList1) {
  1467. FREE (pathList1);
  1468. }
  1469. if (pathList2) {
  1470. FREE (pathList2);
  1471. }
  1472. if (b && g_DynUpdtStatus->GuidrvsInfSource[0]) {
  1473. if (!WritePrivateProfileString (
  1474. WINNT_SETUPPARAMS,
  1475. WINNT_SP_DYNUPDTDRIVERINFOFILE,
  1476. g_DynUpdtStatus->GuidrvsInfSource,
  1477. ParamsFile
  1478. )) {
  1479. b = FALSE;
  1480. }
  1481. }
  1482. }
  1483. //
  1484. // store paths to all downloaded BOOT drivers in a key in the answer file,
  1485. // so later on Textmode Setup will append this to the boot drivers list
  1486. //
  1487. if (b && g_DynUpdtStatus->BootDriverPathList) {
  1488. len1 = 0;
  1489. for (q = g_DynUpdtStatus->BootDriverPathList; q; q = q->Next) {
  1490. len1 += lstrlen (q->String) + 1;
  1491. }
  1492. pathList1 = (PTSTR) MALLOC (len1 * sizeof (TCHAR));
  1493. if (!pathList1) {
  1494. b = FALSE;
  1495. goto exit;
  1496. }
  1497. *pathList1 = 0;
  1498. append1 = pathList1;
  1499. for (q = g_DynUpdtStatus->BootDriverPathList; q; q = q->Next) {
  1500. if (append1 != pathList1) {
  1501. *append1++ = TEXT(',');
  1502. }
  1503. lstrcpy (append1, q->String);
  1504. append1 = _tcschr (append1, 0);
  1505. }
  1506. if (!WritePrivateProfileString (
  1507. WINNT_SETUPPARAMS,
  1508. WINNT_SP_DYNUPDTBOOTDRIVERPRESENT,
  1509. WINNT_A_YES,
  1510. ParamsFile
  1511. ) ||
  1512. !WritePrivateProfileString (
  1513. WINNT_SETUPPARAMS,
  1514. WINNT_SP_DYNUPDTBOOTDRIVERROOT,
  1515. S_SUBDIRNAME_DRIVERS,
  1516. ParamsFile
  1517. ) ||
  1518. !WritePrivateProfileString (
  1519. WINNT_SETUPPARAMS,
  1520. WINNT_SP_DYNUPDTBOOTDRIVERS,
  1521. pathList1,
  1522. ParamsFile
  1523. )) {
  1524. b = FALSE;
  1525. }
  1526. FREE (pathList1);
  1527. }
  1528. if (b && g_DynUpdtStatus->UpdatesCabTarget[0]) {
  1529. TCHAR buffer[4*MAX_PATH];
  1530. wsprintf (
  1531. buffer,
  1532. TEXT("\"%s\""),
  1533. g_DynUpdtStatus->UpdatesCabTarget
  1534. );
  1535. b = WritePrivateProfileString (
  1536. WINNT_SETUPPARAMS,
  1537. WINNT_SP_UPDATEDSOURCES,
  1538. buffer,
  1539. ParamsFile
  1540. );
  1541. }
  1542. //
  1543. // new assemblies to be installed during GUI setup
  1544. //
  1545. if (b && g_DynUpdtStatus->DuasmsTarget[0]) {
  1546. b = WritePrivateProfileString (
  1547. WINNT_SETUPPARAMS,
  1548. WINNT_SP_UPDATEDDUASMS,
  1549. g_DynUpdtStatus->DuasmsTarget,
  1550. ParamsFile
  1551. );
  1552. }
  1553. #ifdef _X86_
  1554. //
  1555. // last but not least, replace the Win9xupg NT side migration dll (w95upgnt.dll)
  1556. // if a new one is available
  1557. //
  1558. if (b && Upgrade && !ISNT() && g_DynUpdtStatus->Winnt32Path[0]) {
  1559. TCHAR source[MAX_PATH];
  1560. TCHAR target[MAX_PATH];
  1561. BuildPath (target, g_DynUpdtStatus->Winnt32Path, TEXT("w95upgnt.dll"));
  1562. if (pDoesFileExist (target)) {
  1563. //
  1564. // check file versions first
  1565. //
  1566. BuildPath (source, NativeSourcePaths[0], TEXT("w95upgnt.dll"));
  1567. if (!IsFileVersionLesser (target, source)) {
  1568. if (_tcsnicmp (
  1569. g_DynUpdtStatus->Winnt32Path,
  1570. g_DynUpdtStatus->WorkingDir,
  1571. lstrlen (g_DynUpdtStatus->WorkingDir)
  1572. )) {
  1573. //
  1574. // copy the file in a local directory first
  1575. //
  1576. BuildPath (source, g_DynUpdtStatus->WorkingDir, TEXT("w95upgnt.dll"));
  1577. if (CopyFile (target, source, FALSE)) {
  1578. lstrcpy (target, source);
  1579. } else {
  1580. //
  1581. // failed to copy the NT-side upgrade module!
  1582. // fail the upgrade
  1583. //
  1584. DynUpdtDebugLog (
  1585. Winnt32LogSevereError,
  1586. TEXT("Failed to copy replacement %1 to %2; upgrade aborted"),
  1587. 0,
  1588. target,
  1589. source
  1590. );
  1591. b = FALSE;
  1592. }
  1593. }
  1594. if (b) {
  1595. b = WritePrivateProfileString (
  1596. WINNT_WIN95UPG_95_DIR,
  1597. WINNT_WIN95UPG_NTKEY,
  1598. target,
  1599. ParamsFile
  1600. );
  1601. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Will use replacement %1 on the NT side of migration"), 0, target);
  1602. }
  1603. }
  1604. }
  1605. }
  1606. #endif
  1607. if (b) {
  1608. if (g_DynUpdtStatus->WorkingDir[0]) {
  1609. b = WritePrivateProfileString (
  1610. WINNT_SETUPPARAMS,
  1611. WINNT_SP_DYNUPDTWORKINGDIR,
  1612. g_DynUpdtStatus->WorkingDir,
  1613. ParamsFile
  1614. );
  1615. }
  1616. }
  1617. if (b) {
  1618. //
  1619. // flush it to disk
  1620. //
  1621. WritePrivateProfileString (NULL, NULL, NULL, ParamsFile);
  1622. }
  1623. exit:
  1624. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuWriteParams (%1!d!)"), 0, b);
  1625. return b;
  1626. }
  1627. VOID
  1628. WINAPI
  1629. Winnt32DuUninitialize (
  1630. VOID
  1631. )
  1632. {
  1633. DWORD i;
  1634. TCHAR pathPss[MAX_PATH];
  1635. PTSTR p;
  1636. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Enter Winnt32DuUninitialize"), 0);
  1637. TRY {
  1638. if (g_DynUpdtStatus->Connection != INVALID_HANDLE_VALUE) {
  1639. if (g_DynUpdtStatus->DuUninitialize) {
  1640. (*g_DynUpdtStatus->DuUninitialize) (g_DynUpdtStatus->Connection);
  1641. }
  1642. g_DynUpdtStatus->Connection = INVALID_HANDLE_VALUE;
  1643. }
  1644. g_DynUpdtStatus->DuIsSupported = NULL;
  1645. g_DynUpdtStatus->ProgressWindow = NULL;
  1646. g_DynUpdtStatus->DuInitialize = NULL;
  1647. g_DynUpdtStatus->DuDoDetection = NULL;
  1648. g_DynUpdtStatus->DuQueryUnsupDrvs = NULL;
  1649. g_DynUpdtStatus->DuBeginDownload = NULL;
  1650. g_DynUpdtStatus->DuAbortDownload = NULL;
  1651. g_DynUpdtStatus->DuUninitialize = NULL;
  1652. if (g_DynUpdtStatus->DuLib) {
  1653. FreeLibrary (g_DynUpdtStatus->DuLib);
  1654. g_DynUpdtStatus->DuLib = NULL;
  1655. }
  1656. if (g_DynUpdtStatus->HwdbDatabase) {
  1657. g_DynUpdtStatus->HwdbClose (g_DynUpdtStatus->HwdbDatabase);
  1658. g_DynUpdtStatus->HwdbDatabase = NULL;
  1659. }
  1660. if (g_DynUpdtStatus->HwdbTerminate) {
  1661. (*g_DynUpdtStatus->HwdbTerminate) ();
  1662. }
  1663. g_DynUpdtStatus->HwdbInitialize = NULL;
  1664. g_DynUpdtStatus->HwdbTerminate = NULL;
  1665. g_DynUpdtStatus->HwdbOpen = NULL;
  1666. g_DynUpdtStatus->HwdbClose = NULL;
  1667. g_DynUpdtStatus->HwdbAppendInfs = NULL;
  1668. g_DynUpdtStatus->HwdbFlush = NULL;
  1669. g_DynUpdtStatus->HwdbHasDriver = NULL;
  1670. g_DynUpdtStatus->HwdbHasAnyDriver = NULL;
  1671. if (g_DynUpdtStatus->HwdbLib) {
  1672. FreeLibrary (g_DynUpdtStatus->HwdbLib);
  1673. g_DynUpdtStatus->HwdbLib = NULL;
  1674. }
  1675. #ifndef UNICODE
  1676. g_DynUpdtStatus->Win9xGetIncompDrvs = NULL;
  1677. g_DynUpdtStatus->Win9xReleaseIncompDrvs = NULL;
  1678. #endif
  1679. if (g_DynUpdtStatus->WorkingDir[0]) {
  1680. p = _tcsrchr (g_DynUpdtStatus->WorkingDir, TEXT('\\'));
  1681. if (!p) {
  1682. p = g_DynUpdtStatus->WorkingDir;
  1683. }
  1684. lstrcpyn (pathPss, g_DynUpdtStatus->WorkingDir, (INT)(p - g_DynUpdtStatus->WorkingDir + 2));
  1685. lstrcat (pathPss, TEXT("setup.pss"));
  1686. CreateMultiLevelDirectory (pathPss);
  1687. lstrcat (pathPss, p);
  1688. MyDelnode (pathPss);
  1689. }
  1690. if (!DynamicUpdateSuccessful ()) {
  1691. if (g_DynUpdtStatus->WorkingDir[0]) {
  1692. //
  1693. // rename this directory to make sure no module uses any DU files
  1694. //
  1695. MYASSERT (pathPss[0]);
  1696. if (!MoveFile (g_DynUpdtStatus->WorkingDir, pathPss)) {
  1697. DynUpdtDebugLog (
  1698. Winnt32LogError,
  1699. TEXT("Winnt32DuUninitialize: MoveFile %1 -> %2 failed"),
  1700. 0,
  1701. g_DynUpdtStatus->WorkingDir,
  1702. pathPss
  1703. );
  1704. MyDelnode (g_DynUpdtStatus->WorkingDir);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  1710. SetLastError (_exception_code ());
  1711. DynUpdtDebugLog (Winnt32LogError, TEXT("Winnt32DuUninitialize: an exception occured"), 0);
  1712. }
  1713. END_EXCEPT
  1714. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Leave Winnt32DuUninitialize"), 0);
  1715. }
  1716. BOOL
  1717. pAddMissingPrinterDrivers (
  1718. IN OUT PSTRINGLIST* List
  1719. )
  1720. {
  1721. DWORD nBytesNeeded = 0;
  1722. DWORD nDriverRetrieved = 0;
  1723. DWORD rc;
  1724. PDRIVER_INFO_6 buffer = NULL;
  1725. DWORD index;
  1726. PCTSTR printerPnpId;
  1727. PSTRINGLIST p;
  1728. BOOL unsupported;
  1729. BOOL b = FALSE;
  1730. if (!EnumPrinterDrivers (
  1731. NULL,
  1732. NULL,
  1733. 6,
  1734. NULL,
  1735. 0,
  1736. &nBytesNeeded,
  1737. &nDriverRetrieved
  1738. )) {
  1739. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  1740. goto exit;
  1741. }
  1742. }
  1743. if (nBytesNeeded) {
  1744. buffer = (PDRIVER_INFO_6) MALLOC (nBytesNeeded);
  1745. if (!buffer) {
  1746. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  1747. goto exit;
  1748. }
  1749. //
  1750. // get printer driver information
  1751. //
  1752. if (!EnumPrinterDrivers (
  1753. NULL,
  1754. NULL,
  1755. 6,
  1756. (LPBYTE)buffer,
  1757. nBytesNeeded,
  1758. &nBytesNeeded,
  1759. &nDriverRetrieved
  1760. )) {
  1761. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  1762. goto exit;
  1763. }
  1764. }
  1765. for (index = 0; index < nDriverRetrieved; index++) {
  1766. printerPnpId = buffer[index].pszHardwareID;
  1767. if (!printerPnpId) {
  1768. continue;
  1769. }
  1770. if (g_DynUpdtStatus->HwdbHasDriver (
  1771. g_DynUpdtStatus->HwdbDatabase,
  1772. printerPnpId,
  1773. &unsupported
  1774. )) {
  1775. continue;
  1776. }
  1777. //
  1778. // not an in-box driver
  1779. //
  1780. p = (PSTRINGLIST) MALLOC (sizeof (STRINGLIST));
  1781. if (!p) {
  1782. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  1783. goto exit;
  1784. }
  1785. p->String = MALLOC ((lstrlen (printerPnpId) + 2) * sizeof (TCHAR));
  1786. if (!p->String) {
  1787. FREE (p);
  1788. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  1789. goto exit;
  1790. }
  1791. wsprintf (p->String, TEXT("%s%c"), printerPnpId, TEXT('\0'));
  1792. p->Next = NULL;
  1793. if (!InsertList ((PGENERIC_LIST*)List, (PGENERIC_LIST)p)) {
  1794. DeleteStringCell (p);
  1795. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  1796. goto exit;
  1797. }
  1798. }
  1799. }
  1800. b = TRUE;
  1801. exit:
  1802. rc = GetLastError();
  1803. if (buffer) {
  1804. FREE (buffer);
  1805. }
  1806. SetLastError(rc);
  1807. return b;
  1808. }
  1809. HDEVINFO
  1810. (WINAPI* SetupapiDiGetClassDevs) (
  1811. IN CONST GUID *ClassGuid, OPTIONAL
  1812. IN PCWSTR Enumerator, OPTIONAL
  1813. IN HWND hwndParent, OPTIONAL
  1814. IN DWORD Flags
  1815. );
  1816. BOOL
  1817. (WINAPI* SetupapiDiGetDeviceRegistryProperty) (
  1818. IN HDEVINFO DeviceInfoSet,
  1819. IN PSP_DEVINFO_DATA DeviceInfoData,
  1820. IN DWORD Property,
  1821. OUT PDWORD PropertyRegDataType, OPTIONAL
  1822. OUT PBYTE PropertyBuffer,
  1823. IN DWORD PropertyBufferSize,
  1824. OUT PDWORD RequiredSize OPTIONAL
  1825. );
  1826. BOOL
  1827. (WINAPI* SetupapiDiEnumDeviceInfo) (
  1828. IN HDEVINFO DeviceInfoSet,
  1829. IN DWORD MemberIndex,
  1830. OUT PSP_DEVINFO_DATA DeviceInfoData
  1831. );
  1832. BOOL
  1833. (WINAPI* SetupapiDiDestroyDeviceInfoList) (
  1834. IN HDEVINFO DeviceInfoSet
  1835. );
  1836. #ifdef UNICODE
  1837. PSTRINGLIST
  1838. BuildMissingPnpIdList (
  1839. VOID
  1840. )
  1841. {
  1842. HDEVINFO hDeviceInfoSet;
  1843. INT nIndex = 0;
  1844. SP_DEVINFO_DATA DeviceInfoData;
  1845. PTSTR buffer = NULL;
  1846. ULONG uHwidSize, uCompatidSize;
  1847. DWORD rc;
  1848. BOOL unsupported;
  1849. PSTRINGLIST p;
  1850. PSTRINGLIST list = NULL;
  1851. HMODULE hSetupapi;
  1852. BOOL b;
  1853. BOOL bException;
  1854. if (OsVersion.dwMajorVersion <= 4) {
  1855. return list;
  1856. }
  1857. hSetupapi = LoadLibrary (TEXT("setupapi.dll"));
  1858. if (!hSetupapi) {
  1859. return list;
  1860. }
  1861. //
  1862. // get the entry points
  1863. //
  1864. (FARPROC)SetupapiDiEnumDeviceInfo = GetProcAddress (hSetupapi, "SetupDiEnumDeviceInfo");
  1865. (FARPROC)SetupapiDiDestroyDeviceInfoList = GetProcAddress (hSetupapi, "SetupDiDestroyDeviceInfoList");
  1866. (FARPROC)SetupapiDiGetClassDevs = GetProcAddress (hSetupapi, "SetupDiGetClassDevsW");
  1867. (FARPROC)SetupapiDiGetDeviceRegistryProperty = GetProcAddress (hSetupapi, "SetupDiGetDeviceRegistryPropertyW");
  1868. if (!SetupapiDiEnumDeviceInfo ||
  1869. !SetupapiDiDestroyDeviceInfoList ||
  1870. !SetupapiDiGetClassDevs ||
  1871. !SetupapiDiGetDeviceRegistryProperty
  1872. ) {
  1873. FreeLibrary (hSetupapi);
  1874. return list;
  1875. }
  1876. hDeviceInfoSet = SetupapiDiGetClassDevs (NULL, NULL, NULL, DIGCF_ALLCLASSES);
  1877. if (hDeviceInfoSet == INVALID_HANDLE_VALUE) {
  1878. return list;
  1879. }
  1880. DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA);
  1881. while (SetupapiDiEnumDeviceInfo (hDeviceInfoSet, nIndex++, &DeviceInfoData)) {
  1882. uHwidSize = uCompatidSize = 0;
  1883. if (!SetupapiDiGetDeviceRegistryProperty (
  1884. hDeviceInfoSet,
  1885. &DeviceInfoData,
  1886. SPDRP_HARDWAREID,
  1887. NULL,
  1888. NULL,
  1889. 0,
  1890. &uHwidSize
  1891. )) {
  1892. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER &&
  1893. GetLastError() != ERROR_INVALID_DATA
  1894. ) {
  1895. goto exit;
  1896. }
  1897. }
  1898. if (!SetupapiDiGetDeviceRegistryProperty (
  1899. hDeviceInfoSet,
  1900. &DeviceInfoData,
  1901. SPDRP_COMPATIBLEIDS,
  1902. NULL,
  1903. NULL,
  1904. 0,
  1905. &uCompatidSize
  1906. )) {
  1907. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER &&
  1908. GetLastError() != ERROR_INVALID_DATA
  1909. ) {
  1910. goto exit;
  1911. }
  1912. }
  1913. //
  1914. // allocate memory for the multi-sz buffer
  1915. //
  1916. if (!uHwidSize && !uCompatidSize) {
  1917. continue;
  1918. }
  1919. buffer = (PTSTR) MALLOC ((uHwidSize + uCompatidSize) * sizeof (TCHAR));
  1920. if (!buffer) {
  1921. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  1922. goto exit;
  1923. }
  1924. //
  1925. // get the hardware id and compatible id
  1926. //
  1927. if (uHwidSize) {
  1928. if (!SetupapiDiGetDeviceRegistryProperty(
  1929. hDeviceInfoSet,
  1930. &DeviceInfoData,
  1931. SPDRP_HARDWAREID,
  1932. NULL,
  1933. (PBYTE)buffer,
  1934. uHwidSize,
  1935. NULL
  1936. )) {
  1937. goto exit;
  1938. }
  1939. }
  1940. if (uCompatidSize) {
  1941. if (!SetupapiDiGetDeviceRegistryProperty(
  1942. hDeviceInfoSet,
  1943. &DeviceInfoData,
  1944. SPDRP_COMPATIBLEIDS,
  1945. NULL,
  1946. (PBYTE)&buffer[uHwidSize / sizeof (TCHAR) - 1],
  1947. uCompatidSize,
  1948. NULL
  1949. )) {
  1950. goto exit;
  1951. }
  1952. }
  1953. //
  1954. // check if there is an inbox driver for this device
  1955. //
  1956. bException = FALSE;
  1957. __try {
  1958. b = g_DynUpdtStatus->HwdbHasAnyDriver (
  1959. g_DynUpdtStatus->HwdbDatabase,
  1960. buffer,
  1961. &unsupported
  1962. );
  1963. }
  1964. __except (EXCEPTION_EXECUTE_HANDLER) {
  1965. SetLastError (GetExceptionCode());
  1966. DynUpdtDebugLog (Winnt32LogError, TEXT("BuildMissingPnpIdList: HwdbHasAnyDriver threw an exception"), 0);
  1967. bException = TRUE;
  1968. b = TRUE;
  1969. }
  1970. if (bException) {
  1971. __try {
  1972. //
  1973. // bad string passed back, or some internal error
  1974. // try to print the string
  1975. //
  1976. PTSTR multisz = CreatePrintableString (buffer);
  1977. DynUpdtDebugLog (Winnt32LogError, TEXT(" - The string was %1"), 0, multisz);
  1978. FREE (multisz);
  1979. }
  1980. __except (EXCEPTION_EXECUTE_HANDLER) {
  1981. DynUpdtDebugLog (Winnt32LogError, TEXT(" - Bad string"), 0);
  1982. }
  1983. }
  1984. if (b) {
  1985. FREE (buffer);
  1986. buffer = NULL;
  1987. continue;
  1988. }
  1989. //
  1990. // no inbox driver - add it to the list
  1991. //
  1992. p = (PSTRINGLIST) MALLOC (sizeof (STRINGLIST));
  1993. if (!p) {
  1994. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  1995. goto exit;
  1996. }
  1997. p->String = buffer;
  1998. p->Next = NULL;
  1999. buffer = NULL;
  2000. if (!InsertList ((PGENERIC_LIST*)&list, (PGENERIC_LIST)p)) {
  2001. DeleteStringCell (p);
  2002. DeleteStringList (list);
  2003. list = NULL;
  2004. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  2005. goto exit;
  2006. }
  2007. }
  2008. if (GetLastError() == ERROR_NO_MORE_ITEMS) {
  2009. SetLastError (ERROR_SUCCESS);
  2010. }
  2011. exit:
  2012. rc = GetLastError();
  2013. SetupapiDiDestroyDeviceInfoList(hDeviceInfoSet);
  2014. FreeLibrary (hSetupapi);
  2015. if (buffer) {
  2016. FREE (buffer);
  2017. }
  2018. if (rc == ERROR_SUCCESS) {
  2019. //
  2020. // get printer drivers
  2021. //
  2022. if (ISNT()) {
  2023. if (!pAddMissingPrinterDrivers (&list)) {
  2024. rc = GetLastError();
  2025. DeleteStringList (list);
  2026. list = NULL;
  2027. }
  2028. }
  2029. }
  2030. SetLastError(rc);
  2031. return list;
  2032. }
  2033. #endif
  2034. BOOL
  2035. pHwdbHasAnyMissingDrivers (
  2036. IN HANDLE Hwdb,
  2037. IN PSTRINGLIST MissingPnpIds
  2038. )
  2039. {
  2040. PSTRINGLIST p;
  2041. BOOL unsupported;
  2042. BOOL b = FALSE;
  2043. for (p = MissingPnpIds; p; p = p->Next) {
  2044. BOOL bException = FALSE;
  2045. __try {
  2046. if (g_DynUpdtStatus->HwdbHasAnyDriver (Hwdb, p->String, &unsupported)) {
  2047. b = TRUE;
  2048. break;
  2049. }
  2050. }
  2051. __except (EXCEPTION_EXECUTE_HANDLER) {
  2052. SetLastError (GetExceptionCode());
  2053. DynUpdtDebugLog (
  2054. Winnt32LogError,
  2055. TEXT("pHwdbHasAnyMissingDrivers: HwdbHasAnyDriver threw an exception"),
  2056. 0
  2057. );
  2058. bException = TRUE;
  2059. }
  2060. if (bException) {
  2061. __try {
  2062. //
  2063. // bad string passed back, or some internal error
  2064. // try to print the string
  2065. //
  2066. PTSTR multisz = CreatePrintableString (p->String);
  2067. DynUpdtDebugLog (Winnt32LogError, TEXT(" - The string was %1"), 0, multisz);
  2068. FREE (multisz);
  2069. }
  2070. __except (EXCEPTION_EXECUTE_HANDLER) {
  2071. DynUpdtDebugLog (Winnt32LogError, TEXT(" - Bad string"), 0);
  2072. }
  2073. }
  2074. }
  2075. return b;
  2076. }
  2077. typedef struct {
  2078. PCTSTR BaseDir;
  2079. PCTSTR Filename;
  2080. } CONTEXT_EXTRACTFILEINDIR, *PCONTEXT_EXTRACTFILEINDIR;
  2081. UINT
  2082. pExtractFileInDir (
  2083. IN PVOID Context,
  2084. IN UINT Code,
  2085. IN UINT_PTR Param1,
  2086. IN UINT_PTR Param2
  2087. )
  2088. {
  2089. if (g_DynUpdtStatus->Cancelled) {
  2090. return ERROR_CANCELLED;
  2091. }
  2092. switch (Code) {
  2093. case SPFILENOTIFY_FILEINCABINET:
  2094. {
  2095. PFILE_IN_CABINET_INFO FileInCabInfo = (PFILE_IN_CABINET_INFO)Param1;
  2096. PCONTEXT_EXTRACTFILEINDIR ctx = (PCONTEXT_EXTRACTFILEINDIR)Context;
  2097. PTSTR p;
  2098. if (lstrcmpi (FileInCabInfo->NameInCabinet, ctx->Filename)) {
  2099. return FILEOP_SKIP;
  2100. }
  2101. BuildPath (FileInCabInfo->FullTargetName, ctx->BaseDir, FileInCabInfo->NameInCabinet);
  2102. if (_tcschr (FileInCabInfo->NameInCabinet, TEXT('\\'))) {
  2103. //
  2104. // target file is in a subdir; first create it
  2105. //
  2106. p = _tcsrchr (FileInCabInfo->FullTargetName, TEXT('\\'));
  2107. if (p) {
  2108. *p = 0;
  2109. }
  2110. if (CreateMultiLevelDirectory (FileInCabInfo->FullTargetName) != ERROR_SUCCESS) {
  2111. return FILEOP_ABORT;
  2112. }
  2113. if (p) {
  2114. *p = TEXT('\\');
  2115. }
  2116. }
  2117. return FILEOP_DOIT;
  2118. }
  2119. case SPFILENOTIFY_NEEDNEWCABINET:
  2120. {
  2121. PCABINET_INFO CabInfo = (PCABINET_INFO)Param1;
  2122. DynUpdtDebugLog (
  2123. Winnt32LogError,
  2124. TEXT("pExtractFileInDir: NeedNewCabinet %1\\%2 on %3 (SetId=%4!u!;CabinetNumber=%5!u!)"),
  2125. 0,
  2126. CabInfo->CabinetPath,
  2127. CabInfo->CabinetFile,
  2128. CabInfo->DiskName,
  2129. CabInfo->SetId,
  2130. CabInfo->CabinetNumber
  2131. );
  2132. return ERROR_FILE_NOT_FOUND;
  2133. }
  2134. }
  2135. return NO_ERROR;
  2136. }
  2137. UINT
  2138. pExpandCabInDir (
  2139. IN PVOID Context,
  2140. IN UINT Code,
  2141. IN UINT_PTR Param1,
  2142. IN UINT_PTR Param2
  2143. )
  2144. {
  2145. if (g_DynUpdtStatus->Cancelled) {
  2146. return ERROR_CANCELLED;
  2147. }
  2148. switch (Code) {
  2149. case SPFILENOTIFY_FILEINCABINET:
  2150. {
  2151. PFILE_IN_CABINET_INFO FileInCabInfo = (PFILE_IN_CABINET_INFO)Param1;
  2152. PTSTR p;
  2153. BuildPath (FileInCabInfo->FullTargetName, (PCTSTR)Context, FileInCabInfo->NameInCabinet);
  2154. if (_tcschr (FileInCabInfo->NameInCabinet, TEXT('\\'))) {
  2155. //
  2156. // target file is in a subdir; first create it
  2157. //
  2158. p = _tcsrchr (FileInCabInfo->FullTargetName, TEXT('\\'));
  2159. if (p) {
  2160. *p = 0;
  2161. }
  2162. if (CreateMultiLevelDirectory (FileInCabInfo->FullTargetName) != ERROR_SUCCESS) {
  2163. return FILEOP_ABORT;
  2164. }
  2165. if (p) {
  2166. *p = TEXT('\\');
  2167. }
  2168. }
  2169. return FILEOP_DOIT;
  2170. }
  2171. case SPFILENOTIFY_NEEDNEWCABINET:
  2172. {
  2173. PCABINET_INFO CabInfo = (PCABINET_INFO)Param1;
  2174. DynUpdtDebugLog (
  2175. Winnt32LogError,
  2176. TEXT("pExpandCabInDir: NeedNewCabinet %1\\%2 on %3 (SetId=%4!u!;CabinetNumber=%5!u!)"),
  2177. 0,
  2178. CabInfo->CabinetPath,
  2179. CabInfo->CabinetFile,
  2180. CabInfo->DiskName,
  2181. CabInfo->SetId,
  2182. CabInfo->CabinetNumber
  2183. );
  2184. return ERROR_FILE_NOT_FOUND;
  2185. }
  2186. }
  2187. return NO_ERROR;
  2188. }
  2189. BOOL
  2190. pGetAutoSubdirName (
  2191. IN PCTSTR FilePath,
  2192. OUT PTSTR DirName
  2193. )
  2194. {
  2195. PTSTR p, q;
  2196. lstrcpy (DirName, FilePath);
  2197. p = _tcsrchr (DirName, TEXT('.'));
  2198. q = _tcsrchr (DirName, TEXT('\\'));
  2199. if (!p || (q && p < q)) {
  2200. return FALSE;
  2201. }
  2202. *p = 0;
  2203. return TRUE;
  2204. }
  2205. BOOL
  2206. pAddLibrariesForCompToCopyQueue (
  2207. IN PCTSTR ModuleName,
  2208. IN PCTSTR Subdir,
  2209. IN PCTSTR BaseDir,
  2210. IN HSPFILEQ SetupQueue
  2211. )
  2212. {
  2213. static struct {
  2214. PCTSTR SubDir;
  2215. PCTSTR LibsMultiSz;
  2216. } g_SubdirReqLibs [] = {
  2217. TEXT("win9xupg"), TEXT("setupapi.dll\0cfgmgr32.dll\0msvcrt.dll\0cabinet.dll\0imagehlp.dll\0"),
  2218. TEXT("winntupg"), TEXT("setupapi.dll\0cfgmgr32.dll\0msvcrt.dll\0"),
  2219. };
  2220. INT i;
  2221. TCHAR dst[MAX_PATH];
  2222. TCHAR src[MAX_PATH];
  2223. TCHAR sourceFile[MAX_PATH];
  2224. PTSTR p, q;
  2225. PCTSTR fileName;
  2226. for (i = 0; i < SIZEOFARRAY (g_SubdirReqLibs); i++) {
  2227. if (Subdir == g_SubdirReqLibs[i].SubDir ||
  2228. Subdir && !lstrcmpi (Subdir, g_SubdirReqLibs[i].SubDir)
  2229. ) {
  2230. break;
  2231. }
  2232. }
  2233. if (i >= SIZEOFARRAY (g_SubdirReqLibs)) {
  2234. return TRUE;
  2235. }
  2236. //
  2237. // prepare src and dest path
  2238. //
  2239. if (!GetModuleFileName (NULL, src, MAX_PATH)) {
  2240. return FALSE;
  2241. }
  2242. p = _tcsrchr (src, TEXT('\\'));
  2243. if (!p) {
  2244. return FALSE;
  2245. }
  2246. *p = 0;
  2247. q = dst + wsprintf (dst, TEXT("%s"), BaseDir);
  2248. if (Subdir) {
  2249. p = p + wsprintf (p, TEXT("\\%s"), Subdir);
  2250. q = q + wsprintf (q, TEXT("\\%s"), Subdir);
  2251. }
  2252. //
  2253. // copy each source file
  2254. //
  2255. for (fileName = g_SubdirReqLibs[i].LibsMultiSz;
  2256. *fileName;
  2257. fileName = _tcschr (fileName, 0) + 1) {
  2258. wsprintf (q, TEXT("\\%s"), fileName);
  2259. //
  2260. // check if file already exists at dest
  2261. //
  2262. if (!pDoesFileExist (dst)) {
  2263. //
  2264. // check if the source file actually exists
  2265. //
  2266. BuildPath (sourceFile, src, fileName);
  2267. if (!pDoesFileExist (sourceFile)) {
  2268. DynUpdtDebugLog (Winnt32LogError, TEXT("Source file %1 not found"), 0, sourceFile);
  2269. return FALSE;
  2270. }
  2271. //
  2272. // prepare source path and copy file
  2273. //
  2274. *q = 0;
  2275. if (!SetupapiQueueCopy (
  2276. SetupQueue,
  2277. src,
  2278. NULL,
  2279. fileName,
  2280. NULL,
  2281. NULL,
  2282. dst,
  2283. NULL,
  2284. SP_COPY_SOURCEPATH_ABSOLUTE
  2285. )) {
  2286. return FALSE;
  2287. }
  2288. }
  2289. }
  2290. return TRUE;
  2291. }
  2292. BOOL
  2293. pIsBootDriver (
  2294. IN PCTSTR DriverFilesDir
  2295. )
  2296. {
  2297. FILEPATTERN_ENUM e;
  2298. BOOL b = FALSE;
  2299. if (EnumFirstFilePattern (&e, DriverFilesDir, TEXT("txtsetup.oem"))) {
  2300. b = !(e.FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  2301. AbortEnumFilePattern (&e);
  2302. }
  2303. return b;
  2304. }
  2305. BOOL
  2306. pIsExcluded (
  2307. IN HINF GuiDrvsInf,
  2308. IN PCTSTR PnPId,
  2309. IN PCTSTR ActualInfFilename,
  2310. IN PCTSTR SourceDir,
  2311. IN PCTSTR FullInfPath
  2312. )
  2313. {
  2314. TCHAR buffer[MAX_PATH];
  2315. PTSTR packageName;
  2316. INFCONTEXT ic;
  2317. TCHAR field[MAX_PATH];
  2318. TCHAR driverVer[MAX_PATH];
  2319. if (!((GuiDrvsInf != INVALID_HANDLE_VALUE && PnPId && ActualInfFilename && SourceDir && FullInfPath))) {
  2320. MYASSERT (FALSE);
  2321. return FALSE;
  2322. }
  2323. lstrcpy (buffer, SourceDir);
  2324. packageName = _tcsrchr (buffer, TEXT('\\'));
  2325. if (!packageName) {
  2326. return FALSE;
  2327. }
  2328. packageName++;
  2329. if (SetupapiFindFirstLine (GuiDrvsInf, TEXT("ExcludedDrivers"), NULL, &ic)) {
  2330. do {
  2331. if (!SetupapiGetStringField (&ic, GUIDRVS_FIELD_CABNAME, field, MAX_PATH, NULL)) {
  2332. continue;
  2333. }
  2334. if (lstrcmpi (field, packageName)) {
  2335. continue;
  2336. }
  2337. if (!SetupapiGetStringField (&ic, GUIDRVS_FIELD_INFNAME, field, MAX_PATH, NULL)) {
  2338. return TRUE;
  2339. }
  2340. if (lstrcmpi (field, ActualInfFilename)) {
  2341. continue;
  2342. }
  2343. if (SetupapiGetStringField (&ic, GUIDRVS_FIELD_DRIVERVER, field, MAX_PATH, NULL)) {
  2344. if (field[0] != TEXT('*')) {
  2345. //
  2346. // read the DriverVer value out of this INF
  2347. //
  2348. GetPrivateProfileString (
  2349. TEXT("Version"),
  2350. TEXT("DriverVer"),
  2351. TEXT(""),
  2352. driverVer,
  2353. MAX_PATH,
  2354. FullInfPath
  2355. );
  2356. if (lstrcmpi (field, driverVer)) {
  2357. continue;
  2358. }
  2359. }
  2360. }
  2361. if (SetupapiGetStringField (&ic, GUIDRVS_FIELD_HARDWAREID, field, MAX_PATH, NULL) &&
  2362. lstrcmpi (field, PnPId)
  2363. ) {
  2364. continue;
  2365. }
  2366. return TRUE;
  2367. } while (SetupapiFindNextLine (&ic, &ic));
  2368. }
  2369. return FALSE;
  2370. }
  2371. BOOL
  2372. Winnt32HwdbAppendInfsCallback (
  2373. IN PVOID Context,
  2374. IN PCTSTR PnpId,
  2375. IN PCTSTR ActualInfFilename,
  2376. IN PCTSTR SourceDir,
  2377. IN PCTSTR FullInfPath
  2378. )
  2379. {
  2380. HINF hGuiDrvs = (HINF)Context;
  2381. MYASSERT (hGuiDrvs != INVALID_HANDLE_VALUE);
  2382. return !pIsExcluded (hGuiDrvs, PnpId, ActualInfFilename, SourceDir, FullInfPath);
  2383. }
  2384. BOOL
  2385. pBuildHwcompDat (
  2386. IN PCTSTR DriverDir,
  2387. IN HINF GuidrvsInf, OPTIONAL
  2388. IN BOOL AlwaysRebuild,
  2389. IN BOOL AllowUI
  2390. )
  2391. {
  2392. HANDLE hDB;
  2393. TCHAR datFile[MAX_PATH];
  2394. BOOL b = TRUE;
  2395. BuildPath (datFile, DriverDir, S_HWCOMP_DAT);
  2396. if (AlwaysRebuild) {
  2397. SetFileAttributes (datFile, FILE_ATTRIBUTE_NORMAL);
  2398. DeleteFile (datFile);
  2399. }
  2400. if (pDoesFileExist (datFile)) {
  2401. return TRUE;
  2402. }
  2403. hDB = g_DynUpdtStatus->HwdbOpen (NULL);
  2404. if (!hDB) {
  2405. return FALSE;
  2406. }
  2407. if (g_DynUpdtStatus->HwdbAppendInfs (
  2408. hDB,
  2409. DriverDir,
  2410. GuidrvsInf != INVALID_HANDLE_VALUE ? Winnt32HwdbAppendInfsCallback : NULL,
  2411. (PVOID)GuidrvsInf
  2412. )) {
  2413. if (g_DynUpdtStatus->HwdbFlush (hDB, datFile)) {
  2414. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Successfully built precompiled hardware database %1"), 0, datFile);
  2415. } else {
  2416. if (AllowUI) {
  2417. MessageBoxFromMessage (
  2418. g_DynUpdtStatus->ProgressWindow,
  2419. MSG_ERROR_WRITING_FILE,
  2420. FALSE,
  2421. AppTitleStringId,
  2422. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  2423. GetLastError (),
  2424. datFile
  2425. );
  2426. }
  2427. b = FALSE;
  2428. }
  2429. } else {
  2430. if (AllowUI) {
  2431. MessageBoxFromMessage (
  2432. g_DynUpdtStatus->ProgressWindow,
  2433. MSG_ERROR_PROCESSING_DRIVER,
  2434. FALSE,
  2435. AppTitleStringId,
  2436. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  2437. DriverDir
  2438. );
  2439. }
  2440. b = FALSE;
  2441. }
  2442. g_DynUpdtStatus->HwdbClose (hDB);
  2443. return b;
  2444. }
  2445. UINT
  2446. pWriteAnsiFilelistToFile (
  2447. IN PVOID Context,
  2448. IN UINT Code,
  2449. IN UINT_PTR Param1,
  2450. IN UINT_PTR Param2
  2451. )
  2452. {
  2453. if (g_DynUpdtStatus->Cancelled) {
  2454. return ERROR_CANCELLED;
  2455. }
  2456. switch (Code) {
  2457. case SPFILENOTIFY_FILEINCABINET:
  2458. {
  2459. PFILE_IN_CABINET_INFO FileInCabInfo = (PFILE_IN_CABINET_INFO)Param1;
  2460. CHAR ansi[MAX_PATH];
  2461. DWORD size;
  2462. DWORD bytes;
  2463. PCTSTR p;
  2464. MYASSERT (!_tcschr (FileInCabInfo->NameInCabinet, TEXT('\\')));
  2465. #ifdef UNICODE
  2466. size = wsprintfA (ansi, "%ls\r\n", FileInCabInfo->NameInCabinet);
  2467. #else
  2468. size = wsprintfA (ansi, "%s\r\n", FileInCabInfo->NameInCabinet);
  2469. #endif
  2470. if (!WriteFile ((HANDLE)Context, ansi, size, &bytes, NULL) || bytes != size) {
  2471. return FILEOP_ABORT;
  2472. }
  2473. return FILEOP_SKIP;
  2474. }
  2475. }
  2476. return NO_ERROR;
  2477. }
  2478. BOOL
  2479. CreateFileListSif (
  2480. IN PCTSTR SifPath,
  2481. IN PCTSTR SectionName,
  2482. IN PCTSTR CabinetToScan
  2483. )
  2484. {
  2485. HANDLE sif;
  2486. CHAR ansi[MAX_PATH];
  2487. DWORD size;
  2488. DWORD bytes;
  2489. BOOL b = TRUE;
  2490. sif = CreateFile (
  2491. SifPath,
  2492. GENERIC_WRITE,
  2493. FILE_SHARE_READ,
  2494. NULL,
  2495. CREATE_ALWAYS,
  2496. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  2497. NULL
  2498. );
  2499. if (sif == INVALID_HANDLE_VALUE) {
  2500. return FALSE;
  2501. }
  2502. #ifdef UNICODE
  2503. size = wsprintfA (ansi, "[%ls]\r\n", SectionName);
  2504. #else
  2505. size = wsprintfA (ansi, "[%s]\r\n", SectionName);
  2506. #endif
  2507. if (!WriteFile (sif, ansi, size, &bytes, NULL) || bytes != size) {
  2508. b = FALSE;
  2509. goto exit;
  2510. }
  2511. b = SetupapiCabinetRoutine (CabinetToScan, 0, pWriteAnsiFilelistToFile, (PVOID)sif);
  2512. exit:
  2513. CloseHandle (sif);
  2514. if (!b) {
  2515. DeleteFile (SifPath);
  2516. }
  2517. return b;
  2518. }
  2519. BOOL
  2520. pIsExecutableModule (
  2521. IN PCTSTR ModulePath
  2522. )
  2523. {
  2524. PCTSTR p;
  2525. p = GetFileExtension (ModulePath);
  2526. return p && !lstrcmpi (p, TEXT(".dll"));
  2527. }
  2528. UINT
  2529. pCopyFilesCallback (
  2530. IN PVOID Context, //context used by the callback routine
  2531. IN UINT Notification, //notification sent to callback routine
  2532. IN UINT_PTR Param1, //additional notification information
  2533. IN UINT_PTR Param2 //additional notification information
  2534. )
  2535. {
  2536. switch (Notification) {
  2537. case SPFILENOTIFY_COPYERROR:
  2538. return FILEOP_ABORT;
  2539. case SPFILENOTIFY_STARTCOPY:
  2540. //
  2541. // try to avoid unnecessary setupapi warnings that the files are not signed
  2542. // or even blocked because of this; try to copy the file ourselves first
  2543. //
  2544. {
  2545. PFILEPATHS paths = (PFILEPATHS)Param1;
  2546. if (CopyFile (paths->Source, paths->Target, FALSE)) {
  2547. return FILEOP_SKIP;
  2548. }
  2549. }
  2550. break;
  2551. case SPFILENOTIFY_STARTQUEUE:
  2552. case SPFILENOTIFY_STARTSUBQUEUE:
  2553. case SPFILENOTIFY_ENDSUBQUEUE:
  2554. case SPFILENOTIFY_ENDQUEUE:
  2555. return !g_DynUpdtStatus->Cancelled;
  2556. }
  2557. return SetupapiDefaultQueueCallback (Context, Notification, Param1, Param2);
  2558. }
  2559. BOOL
  2560. pProcessWinnt32Files (
  2561. IN PCTSTR Winnt32Cab,
  2562. IN BOOL ClientInstall,
  2563. OUT PBOOL StopSetup
  2564. )
  2565. {
  2566. FILEPATTERNREC_ENUM e;
  2567. TCHAR winnt32WorkingDir[MAX_PATH];
  2568. TCHAR dst[MAX_PATH];
  2569. PTSTR p, subdir;
  2570. HSPFILEQ hq;
  2571. BOOL bLoaded;
  2572. BOOL bRestartRequired;
  2573. BOOL bReloadMainInf = FALSE;
  2574. TCHAR buffer[MAX_PATH];
  2575. TCHAR origSubPath[MAX_PATH];
  2576. TCHAR origSubPathCompressed[MAX_PATH];
  2577. TCHAR origFileName[MAX_PATH];
  2578. TCHAR origFilePath[MAX_PATH];
  2579. TCHAR destFilePath[MAX_PATH];
  2580. TCHAR version[100];
  2581. DWORD i;
  2582. DWORD attr;
  2583. BOOL b = TRUE;
  2584. *StopSetup = FALSE;
  2585. if (!pNonemptyFilePresent (Winnt32Cab)) {
  2586. if (!ClientInstall) {
  2587. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Package %1 is not present"), 0, Winnt32Cab);
  2588. }
  2589. return TRUE;
  2590. } else {
  2591. //
  2592. // don't process the cabinet in client installation mode; just warn about that
  2593. //
  2594. if (ClientInstall) {
  2595. //
  2596. // user specified updates location, but they didn't run winnt32 /prepare now or before
  2597. //
  2598. MessageBoxFromMessage (
  2599. g_DynUpdtStatus->ProgressWindow,
  2600. MSG_MUST_PREPARE_SHARE,
  2601. FALSE,
  2602. AppTitleStringId,
  2603. MB_OK | MB_ICONSTOP | MB_TASKMODAL,
  2604. g_DynUpdtStatus->DynamicUpdatesSource
  2605. );
  2606. *StopSetup = TRUE;
  2607. return FALSE;
  2608. }
  2609. }
  2610. DynUpdtDebugLog (
  2611. DynUpdtLogLevel,
  2612. TEXT("Analyzing package %1..."),
  2613. 0,
  2614. Winnt32Cab
  2615. );
  2616. //
  2617. // expand it in the corresponding subdir
  2618. //
  2619. BuildPath (
  2620. winnt32WorkingDir,
  2621. g_DynUpdtStatus->PrepareWinnt32 ?
  2622. g_DynUpdtStatus->DynamicUpdatesSource :
  2623. g_DynUpdtStatus->WorkingDir,
  2624. S_SUBDIRNAME_WINNT32
  2625. );
  2626. //
  2627. // expand CAB in this dir
  2628. //
  2629. if (CreateMultiLevelDirectory (winnt32WorkingDir) != ERROR_SUCCESS) {
  2630. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, winnt32WorkingDir);
  2631. return FALSE;
  2632. }
  2633. if (!(*SetupapiCabinetRoutine) (Winnt32Cab, 0, pExpandCabInDir, (PVOID)winnt32WorkingDir)) {
  2634. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, Winnt32Cab);
  2635. return FALSE;
  2636. }
  2637. //
  2638. // ISSUE: the patching support is currently not available for platforms other than x86
  2639. //
  2640. #ifdef _X86_
  2641. //
  2642. // now let's look for any patches
  2643. //
  2644. if (EnumFirstFilePatternRecursive (&e, winnt32WorkingDir, S_PATCH_FILE_EXT, 0)) {
  2645. do {
  2646. BOOL bDeleteTempFile = FALSE;
  2647. if (g_DynUpdtStatus->Cancelled) {
  2648. SetLastError (ERROR_CANCELLED);
  2649. b = FALSE;
  2650. break;
  2651. }
  2652. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  2653. continue;
  2654. }
  2655. DynUpdtDebugLog (
  2656. DynUpdtLogLevel,
  2657. TEXT("pProcessWinnt32Files: found patch %1"),
  2658. 0,
  2659. e.FullPath
  2660. );
  2661. //
  2662. // get the original file from the sources location
  2663. // the filename is obtained cutting the ._p1 extension
  2664. //
  2665. lstrcpy (origFileName, e.FileName);
  2666. p = GetFileExtension (origFileName);
  2667. if (!p) {
  2668. MYASSERT (FALSE);
  2669. continue;
  2670. }
  2671. *p = 0;
  2672. lstrcpy (origSubPath, e.SubPath);
  2673. p = GetFileExtension (origSubPath);
  2674. if (!p) {
  2675. MYASSERT (FALSE);
  2676. continue;
  2677. }
  2678. *p = 0;
  2679. if (!GetModuleFileName (NULL, origFilePath, MAX_PATH) ||
  2680. !(p = _tcsrchr (origFilePath, TEXT('\\')))) {
  2681. b = FALSE;
  2682. break;
  2683. }
  2684. *p = 0;
  2685. ConcatenatePaths (origFilePath, origSubPath, MAX_PATH);
  2686. //
  2687. // now check if this file (in it's compressed form or not) actually exists
  2688. //
  2689. if (!pDoesFileExist (origFilePath)) {
  2690. //
  2691. // try the compressed form
  2692. //
  2693. p = _tcschr (origFilePath, 0);
  2694. MYASSERT (p);
  2695. if (!p) {
  2696. continue;
  2697. }
  2698. p = _tcsdec (origFilePath, p);
  2699. MYASSERT (p);
  2700. if (!p) {
  2701. continue;
  2702. }
  2703. *p = TEXT('_');
  2704. if (!pDoesFileExist (origFilePath)) {
  2705. //
  2706. // the file might exist on the original installation share (like w95upgnt.dll etc)
  2707. // generate compressed form of the file
  2708. //
  2709. lstrcpy (origSubPathCompressed, origSubPath);
  2710. p = _tcschr (origSubPathCompressed, 0);
  2711. MYASSERT (p);
  2712. if (!p) {
  2713. continue;
  2714. }
  2715. p = _tcsdec (origSubPathCompressed, p);
  2716. MYASSERT (p);
  2717. if (!p) {
  2718. continue;
  2719. }
  2720. *p = TEXT('_');
  2721. //
  2722. // now search for it
  2723. //
  2724. b = FALSE;
  2725. for (i = 0; i < SourceCount; i++) {
  2726. lstrcpyn (origFilePath, NativeSourcePaths[i], SIZEOFARRAY(origFilePath));
  2727. ConcatenatePaths (origFilePath, origSubPathCompressed, SIZEOFARRAY(origFilePath));
  2728. attr = GetFileAttributes (origFilePath);
  2729. if (attr != (DWORD)-1 && !(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  2730. b = TRUE;
  2731. break;
  2732. }
  2733. }
  2734. if (!b) {
  2735. DynUpdtDebugLog (
  2736. Winnt32LogError,
  2737. TEXT("pProcessWinnt32Files: Unable to find original file %1 to apply the patch"),
  2738. 0,
  2739. origSubPath
  2740. );
  2741. break;
  2742. }
  2743. }
  2744. //
  2745. // expand the file to the temp dir
  2746. //
  2747. BuildPath (buffer, g_DynUpdtStatus->TempDir, origSubPath);
  2748. p = _tcsrchr (buffer, TEXT('\\'));
  2749. MYASSERT (p);
  2750. if (!p) {
  2751. continue;
  2752. }
  2753. *p = 0;
  2754. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  2755. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessWinnt32Files: Unable to create dir %1"), 0, buffer);
  2756. b = FALSE;
  2757. break;
  2758. }
  2759. if (!(*SetupapiCabinetRoutine) (origFilePath, 0, pExpandCabInDir, buffer)) {
  2760. DynUpdtDebugLog (
  2761. Winnt32LogError,
  2762. TEXT("pProcessWinnt32Files: Unable to expand original file %1 to dir %2"),
  2763. 0,
  2764. origFilePath,
  2765. buffer
  2766. );
  2767. b = FALSE;
  2768. break;
  2769. }
  2770. *p = TEXT('\\');
  2771. lstrcpy (origFilePath, buffer);
  2772. bDeleteTempFile = TRUE;
  2773. }
  2774. BuildPath (destFilePath, winnt32WorkingDir, TEXT("$$temp$$.~~~"));
  2775. //
  2776. // now really apply the patch
  2777. //
  2778. if (!ApplyPatchToFile (e.FullPath, origFilePath, destFilePath, 0)) {
  2779. DynUpdtDebugLog (
  2780. Winnt32LogError,
  2781. TEXT("pProcessWinnt32Files: ApplyPatchToFile failed to apply patch %1 to file %2"),
  2782. 0,
  2783. e.FullPath,
  2784. origFilePath
  2785. );
  2786. b = FALSE;
  2787. break;
  2788. }
  2789. //
  2790. // success! now move the file to the real destination
  2791. //
  2792. BuildPath (buffer, winnt32WorkingDir, origSubPath);
  2793. p = _tcsrchr (buffer, TEXT('\\'));
  2794. MYASSERT (p);
  2795. if (!p) {
  2796. continue;
  2797. }
  2798. *p = 0;
  2799. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  2800. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessWinnt32Files: Unable to create dir %1"), 0, buffer);
  2801. b = FALSE;
  2802. break;
  2803. }
  2804. *p = TEXT('\\');
  2805. SetFileAttributes (buffer, FILE_ATTRIBUTE_NORMAL);
  2806. DeleteFile (buffer);
  2807. if (!MoveFile (destFilePath, buffer)) {
  2808. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessWinnt32Files: Unable to move file %1 to final dest %2"), 0, destFilePath, buffer);
  2809. b = FALSE;
  2810. break;
  2811. }
  2812. if (!GetFileVersion (buffer, version)) {
  2813. lstrcpy (version, TEXT("<unknown>"));
  2814. }
  2815. DynUpdtDebugLog (
  2816. DynUpdtLogLevel,
  2817. TEXT("pProcessWinnt32Files: successfully applied patch %1 to file %2; the new file %3 has version %4"),
  2818. 0,
  2819. e.FullPath,
  2820. origFilePath,
  2821. buffer,
  2822. version
  2823. );
  2824. //
  2825. // now remove the patch file
  2826. //
  2827. SetFileAttributes (e.FullPath, FILE_ATTRIBUTE_NORMAL);
  2828. DeleteFile (e.FullPath);
  2829. if (bDeleteTempFile) {
  2830. SetFileAttributes (origFilePath, FILE_ATTRIBUTE_NORMAL);
  2831. DeleteFile (origFilePath);
  2832. }
  2833. } while (EnumNextFilePatternRecursive (&e));
  2834. AbortEnumFilePatternRecursive (&e);
  2835. }
  2836. #endif
  2837. if (!b) {
  2838. goto exit;
  2839. }
  2840. //
  2841. // process new Winnt32 components
  2842. //
  2843. hq = SetupapiOpenFileQueue ();
  2844. if (hq == INVALID_HANDLE_VALUE) {
  2845. return FALSE;
  2846. }
  2847. if (EnumFirstFilePatternRecursive (&e, winnt32WorkingDir, TEXT("*"), 0)) {
  2848. do {
  2849. if (g_DynUpdtStatus->Cancelled) {
  2850. SetLastError (ERROR_CANCELLED);
  2851. b = FALSE;
  2852. break;
  2853. }
  2854. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  2855. continue;
  2856. }
  2857. if (!GetModuleFileName (NULL, dst, MAX_PATH) ||
  2858. !(p = _tcsrchr (dst, TEXT('\\')))) {
  2859. b = FALSE;
  2860. break;
  2861. }
  2862. *p = 0;
  2863. ConcatenatePaths (dst, e.SubPath, MAX_PATH);
  2864. //
  2865. // check file versions first
  2866. //
  2867. if (IsFileVersionLesser (e.FullPath, dst)) {
  2868. continue;
  2869. }
  2870. //
  2871. // if there's a file named winnt32.rst, force restart
  2872. //
  2873. if (!lstrcmpi (e.FileName, S_RESTART_FILENAME)) {
  2874. if (!g_DynUpdtStatus->PrepareWinnt32) {
  2875. g_DynUpdtStatus->RestartWinnt32 = TRUE;
  2876. DynUpdtDebugLog (
  2877. DynUpdtLogLevel,
  2878. TEXT("File %1 present; winnt32 will restart"),
  2879. 0,
  2880. e.FileName
  2881. );
  2882. }
  2883. } else {
  2884. //
  2885. // check if dosnet.inf is present; if it is, reset the global variables
  2886. //
  2887. if (!lstrcmpi (e.FileName, InfName) && FullInfName[0]) {
  2888. FullInfName[0] = 0;
  2889. bReloadMainInf = TRUE;
  2890. }
  2891. bLoaded = FALSE;
  2892. bRestartRequired = Upgrade || !_tcschr (e.SubPath, TEXT('\\'));
  2893. if (GetModuleHandle (e.FileName) != NULL) {
  2894. bLoaded = TRUE;
  2895. if (!g_DynUpdtStatus->PrepareWinnt32) {
  2896. //
  2897. // do NOT restart if it's NOT an upgrade and this is one of the upgrade modules
  2898. //
  2899. if (bRestartRequired) {
  2900. //
  2901. // need to restart winnt32 so the newly registered component can be used instead
  2902. //
  2903. g_DynUpdtStatus->RestartWinnt32 = TRUE;
  2904. DynUpdtDebugLog (
  2905. DynUpdtLogLevel,
  2906. TEXT("A newer version is available for %1; winnt32 will restart"),
  2907. 0,
  2908. e.SubPath
  2909. );
  2910. }
  2911. }
  2912. }
  2913. if ((bLoaded || pIsExecutableModule (e.FullPath)) &&
  2914. (bRestartRequired || g_DynUpdtStatus->PrepareWinnt32)
  2915. ) {
  2916. //
  2917. // make all required libraries available for this module
  2918. //
  2919. p = _tcsrchr (e.SubPath, TEXT('\\'));
  2920. if (p) {
  2921. *p = 0;
  2922. subdir = e.SubPath;
  2923. } else {
  2924. subdir = NULL;
  2925. }
  2926. if (!pAddLibrariesForCompToCopyQueue (e.FileName, subdir, winnt32WorkingDir, hq)) {
  2927. b = FALSE;
  2928. break;
  2929. }
  2930. if (p) {
  2931. *p = TEXT('\\');
  2932. }
  2933. }
  2934. }
  2935. } while (EnumNextFilePatternRecursive (&e));
  2936. AbortEnumFilePatternRecursive (&e);
  2937. if (b) {
  2938. PVOID ctx;
  2939. ctx = SetupapiInitDefaultQueueCallback (NULL);
  2940. b = SetupapiCommitFileQueue (NULL, hq, pCopyFilesCallback, ctx);
  2941. SetupapiTermDefaultQueueCallback (ctx);
  2942. if (!b) {
  2943. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessWinnt32Files: SetupapiCommitFileQueue failed"), 0);
  2944. }
  2945. }
  2946. }
  2947. SetupapiCloseFileQueue (hq);
  2948. if (b) {
  2949. SetFileAttributes (Winnt32Cab, FILE_ATTRIBUTE_NORMAL);
  2950. if (!DeleteFile (Winnt32Cab)) {
  2951. DynUpdtDebugLog (Winnt32LogSevereError, TEXT("pProcessWinnt32Files: unable to delete file %1"), 0, Winnt32Cab);
  2952. b = FALSE;
  2953. }
  2954. }
  2955. if (b && bReloadMainInf) {
  2956. b = FindPathToWinnt32File (InfName, FullInfName, MAX_PATH);
  2957. MYASSERT (b);
  2958. if (MainInf) {
  2959. UnloadInfFile (MainInf);
  2960. MainInf = NULL;
  2961. b = LoadInfFile (FullInfName, TRUE, &MainInf) == NO_ERROR;
  2962. }
  2963. }
  2964. exit:
  2965. return b;
  2966. }
  2967. BOOL
  2968. pProcessUpdates (
  2969. IN PCTSTR UpdatesCab,
  2970. IN BOOL ClientInstall,
  2971. OUT PBOOL StopSetup
  2972. )
  2973. {
  2974. TCHAR updatesSourceDir[MAX_PATH];
  2975. TCHAR buffer[MAX_PATH];
  2976. FILEPATTERNREC_ENUM e;
  2977. TCHAR origSubPath[MAX_PATH];
  2978. TCHAR origFileName[MAX_PATH];
  2979. TCHAR origFilePath[MAX_PATH];
  2980. TCHAR destFilePath[MAX_PATH];
  2981. TCHAR version[100];
  2982. PTSTR p;
  2983. TCHAR updatesCabPath[MAX_PATH];
  2984. HANDLE hCabContext;
  2985. BOOL result;
  2986. BOOL bPatchApplied = FALSE;
  2987. HANDLE hCab;
  2988. PSTRINGLIST listUpdatesFiles = NULL;
  2989. PCTSTR cabPath;
  2990. BOOL bCatalogFileFound;
  2991. HANDLE hDiamond = NULL;
  2992. BOOL b = TRUE;
  2993. *StopSetup = FALSE;
  2994. if (!pNonemptyFilePresent (UpdatesCab)) {
  2995. if (!ClientInstall) {
  2996. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Package %1 is not present"), 0, UpdatesCab);
  2997. }
  2998. return TRUE;
  2999. }
  3000. DynUpdtDebugLog (
  3001. DynUpdtLogLevel,
  3002. TEXT("Analyzing package %1..."),
  3003. 0,
  3004. UpdatesCab
  3005. );
  3006. if (ClientInstall) {
  3007. BOOL bMustPrepare = FALSE;
  3008. BuildSifName (UpdatesCab, destFilePath);
  3009. if (!pDoesFileExist (destFilePath)) {
  3010. bMustPrepare = TRUE;
  3011. } else {
  3012. BuildPath (updatesSourceDir, g_DynUpdtStatus->DynamicUpdatesSource, S_SUBDIRNAME_UPDATES);
  3013. if (!DoesDirectoryExist (updatesSourceDir)) {
  3014. bMustPrepare = TRUE;
  3015. }
  3016. }
  3017. if (bMustPrepare) {
  3018. //
  3019. // user specified updates location, but they didn't run winnt32 /prepare now or before
  3020. //
  3021. MessageBoxFromMessage (
  3022. g_DynUpdtStatus->ProgressWindow,
  3023. MSG_MUST_PREPARE_SHARE,
  3024. FALSE,
  3025. AppTitleStringId,
  3026. MB_OK | MB_ICONSTOP | MB_TASKMODAL,
  3027. g_DynUpdtStatus->DynamicUpdatesSource
  3028. );
  3029. *StopSetup = TRUE;
  3030. return FALSE;
  3031. }
  3032. } else {
  3033. if (g_DynUpdtStatus->PrepareWinnt32) {
  3034. BuildPath (updatesSourceDir, g_DynUpdtStatus->DynamicUpdatesSource, S_SUBDIRNAME_UPDATES);
  3035. } else {
  3036. BuildPath (updatesSourceDir, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_UPDATES);
  3037. }
  3038. //
  3039. // expand CAB in this dir
  3040. // make sure dir is initially empty
  3041. //
  3042. MyDelnode (updatesSourceDir);
  3043. if (CreateMultiLevelDirectory (updatesSourceDir) != ERROR_SUCCESS) {
  3044. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, updatesSourceDir);
  3045. return FALSE;
  3046. }
  3047. if (!(*SetupapiCabinetRoutine) (UpdatesCab, 0, pExpandCabInDir, (PVOID)updatesSourceDir)) {
  3048. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, UpdatesCab);
  3049. return FALSE;
  3050. }
  3051. hDiamond = DiamondInitialize (g_DynUpdtStatus->TempDir);
  3052. if (!hDiamond) {
  3053. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to initialize compression/decompression engine"), 0);
  3054. return FALSE;
  3055. }
  3056. //
  3057. // ISSUE: the patching support is currently not available for platforms other than x86
  3058. //
  3059. #ifdef _X86_
  3060. //
  3061. // now let's look for any patches
  3062. //
  3063. if (EnumFirstFilePatternRecursive (&e, updatesSourceDir, S_PATCH_FILE_EXT, 0)) {
  3064. //
  3065. // load drvindex.inf in advance
  3066. //
  3067. TCHAR driverInfName[MAX_PATH];
  3068. PVOID driverInfHandle;
  3069. TCHAR driverCabName[MAX_PATH];
  3070. TCHAR driverCabPath[MAX_PATH];
  3071. if (!FindPathToInstallationFile (DRVINDEX_INF, driverInfName, MAX_PATH)) {
  3072. DynUpdtDebugLog (
  3073. Winnt32LogError,
  3074. TEXT("pProcessUpdates: Unable to find %1"),
  3075. 0,
  3076. DRVINDEX_INF
  3077. );
  3078. AbortEnumFilePatternRecursive (&e);
  3079. b = FALSE;
  3080. goto exit;
  3081. }
  3082. if (LoadInfFile (driverInfName, FALSE, &driverInfHandle) != NO_ERROR) {
  3083. DynUpdtDebugLog (
  3084. Winnt32LogError,
  3085. TEXT("pProcessUpdates: Unable to load %1"),
  3086. 0,
  3087. driverInfName
  3088. );
  3089. AbortEnumFilePatternRecursive (&e);
  3090. b = FALSE;
  3091. goto exit;
  3092. }
  3093. do {
  3094. BOOL bDeleteTempFile = FALSE;
  3095. if (g_DynUpdtStatus->Cancelled) {
  3096. SetLastError (ERROR_CANCELLED);
  3097. b = FALSE;
  3098. break;
  3099. }
  3100. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  3101. continue;
  3102. }
  3103. DynUpdtDebugLog (
  3104. DynUpdtLogLevel,
  3105. TEXT("pProcessUpdates: found patch %1"),
  3106. 0,
  3107. e.FullPath
  3108. );
  3109. //
  3110. // get the original file from the sources location
  3111. // the filename is obtained cutting the ._p1 extension
  3112. //
  3113. lstrcpy (origFileName, e.FileName);
  3114. p = GetFileExtension (origFileName);
  3115. if (!p) {
  3116. MYASSERT (FALSE);
  3117. continue;
  3118. }
  3119. *p = 0;
  3120. lstrcpy (origSubPath, e.SubPath);
  3121. p = GetFileExtension (origSubPath);
  3122. if (!p) {
  3123. MYASSERT (FALSE);
  3124. continue;
  3125. }
  3126. *p = 0;
  3127. BuildPath (origFilePath, SourcePaths[0], origSubPath);
  3128. //
  3129. // now check if this file (in it's compressed form or not) actually exists
  3130. // note that the file may exist in driver.cab
  3131. //
  3132. if (InDriverCacheInf (driverInfHandle, origFileName, driverCabName, MAX_PATH)) {
  3133. CONTEXT_EXTRACTFILEINDIR ctx;
  3134. //
  3135. // extract the file to the temp dir
  3136. //
  3137. if (!driverCabName[0]) {
  3138. DynUpdtDebugLog (
  3139. Winnt32LogError,
  3140. TEXT("pProcessUpdates: cab name not found for %1 in %2"),
  3141. 0,
  3142. origFileName,
  3143. driverInfName
  3144. );
  3145. b = FALSE;
  3146. break;
  3147. }
  3148. BuildPath (buffer, g_DynUpdtStatus->TempDir, origSubPath);
  3149. p = _tcsrchr (buffer, TEXT('\\'));
  3150. MYASSERT (p);
  3151. if (!p) {
  3152. continue;
  3153. }
  3154. *p = 0;
  3155. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  3156. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to create dir %1"), 0, buffer);
  3157. b = FALSE;
  3158. break;
  3159. }
  3160. ctx.BaseDir = buffer;
  3161. ctx.Filename = origFileName;
  3162. if (!FindPathToInstallationFile (driverCabName, driverCabPath, MAX_PATH)) {
  3163. DynUpdtDebugLog (
  3164. Winnt32LogError,
  3165. TEXT("pProcessUpdates: Unable to find cabinet %1"),
  3166. 0,
  3167. driverCabName
  3168. );
  3169. b = FALSE;
  3170. break;
  3171. }
  3172. if (!(*SetupapiCabinetRoutine) (driverCabPath, 0, pExtractFileInDir, &ctx)) {
  3173. DynUpdtDebugLog (
  3174. Winnt32LogError,
  3175. TEXT("pProcessUpdates: Unable to extract file %1 from %2 to %3"),
  3176. 0,
  3177. origFileName,
  3178. driverCabName,
  3179. buffer
  3180. );
  3181. b = FALSE;
  3182. break;
  3183. }
  3184. *p = TEXT('\\');
  3185. lstrcpy (origFilePath, buffer);
  3186. bDeleteTempFile = TRUE;
  3187. } else {
  3188. if (!pDoesFileExist (origFilePath)) {
  3189. //
  3190. // try the compressed form
  3191. //
  3192. p = _tcschr (origFilePath, 0);
  3193. MYASSERT (p);
  3194. if (!p) {
  3195. continue;
  3196. }
  3197. p = _tcsdec (origFilePath, p);
  3198. MYASSERT (p);
  3199. if (!p) {
  3200. continue;
  3201. }
  3202. *p = TEXT('_');
  3203. if (!pDoesFileExist (origFilePath)) {
  3204. DynUpdtDebugLog (
  3205. Winnt32LogError,
  3206. TEXT("pProcessUpdates: Unable to find original file %1 to apply the patch"),
  3207. 0,
  3208. origSubPath
  3209. );
  3210. b = FALSE;
  3211. break;
  3212. }
  3213. //
  3214. // expand the file to the temp dir
  3215. //
  3216. BuildPath (buffer, g_DynUpdtStatus->TempDir, origSubPath);
  3217. p = _tcsrchr (buffer, TEXT('\\'));
  3218. MYASSERT (p);
  3219. if (!p) {
  3220. continue;
  3221. }
  3222. *p = 0;
  3223. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  3224. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to create dir %1"), 0, buffer);
  3225. b = FALSE;
  3226. break;
  3227. }
  3228. if (!(*SetupapiCabinetRoutine) (origFilePath, 0, pExpandCabInDir, buffer)) {
  3229. DynUpdtDebugLog (
  3230. Winnt32LogError,
  3231. TEXT("pProcessUpdates: Unable to expand original file %1 to dir %2"),
  3232. 0,
  3233. origFilePath,
  3234. buffer
  3235. );
  3236. b = FALSE;
  3237. break;
  3238. }
  3239. *p = TEXT('\\');
  3240. lstrcpy (origFilePath, buffer);
  3241. bDeleteTempFile = TRUE;
  3242. }
  3243. }
  3244. BuildPath (destFilePath, updatesSourceDir, TEXT("$$temp$$.~~~"));
  3245. //
  3246. // now really apply the patch
  3247. //
  3248. if (!ApplyPatchToFile (e.FullPath, origFilePath, destFilePath, 0)) {
  3249. DynUpdtDebugLog (
  3250. Winnt32LogError,
  3251. TEXT("pProcessUpdates: ApplyPatchToFile failed to apply patch %1 to file %2"),
  3252. 0,
  3253. e.FullPath,
  3254. origFilePath
  3255. );
  3256. b = FALSE;
  3257. break;
  3258. }
  3259. //
  3260. // success! now move the file to the real destination
  3261. //
  3262. BuildPath (buffer, updatesSourceDir, origSubPath);
  3263. p = _tcsrchr (buffer, TEXT('\\'));
  3264. MYASSERT (p);
  3265. if (!p) {
  3266. continue;
  3267. }
  3268. *p = 0;
  3269. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  3270. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to create dir %1"), 0, buffer);
  3271. b = FALSE;
  3272. break;
  3273. }
  3274. *p = TEXT('\\');
  3275. SetFileAttributes (buffer, FILE_ATTRIBUTE_NORMAL);
  3276. DeleteFile (buffer);
  3277. if (!MoveFile (destFilePath, buffer)) {
  3278. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to move file %1 to final dest %2"), 0, destFilePath, buffer);
  3279. b = FALSE;
  3280. break;
  3281. }
  3282. if (!GetFileVersion (buffer, version)) {
  3283. lstrcpy (version, TEXT("<unknown>"));
  3284. }
  3285. DynUpdtDebugLog (
  3286. DynUpdtLogLevel,
  3287. TEXT("pProcessUpdates: successfully applied patch %1 to file %2; the new file %3 has version %4"),
  3288. 0,
  3289. e.FullPath,
  3290. origFilePath,
  3291. buffer,
  3292. version
  3293. );
  3294. //
  3295. // now remove the patch file
  3296. //
  3297. SetFileAttributes (e.FullPath, FILE_ATTRIBUTE_NORMAL);
  3298. DeleteFile (e.FullPath);
  3299. bPatchApplied = TRUE;
  3300. if (bDeleteTempFile) {
  3301. SetFileAttributes (origFilePath, FILE_ATTRIBUTE_NORMAL);
  3302. DeleteFile (origFilePath);
  3303. }
  3304. } while (EnumNextFilePatternRecursive (&e));
  3305. AbortEnumFilePatternRecursive (&e);
  3306. UnloadInfFile (driverInfHandle);
  3307. if (!b) {
  3308. goto exit;
  3309. }
  3310. }
  3311. #endif
  3312. //
  3313. // build a new updates.cab that will contain the patched versions of files
  3314. // and no relative paths
  3315. //
  3316. BuildPath (updatesCabPath, g_DynUpdtStatus->TempDir, S_CABNAME_UPDATES);
  3317. SetFileAttributes (updatesCabPath, FILE_ATTRIBUTE_NORMAL);
  3318. DeleteFile (updatesCabPath);
  3319. hCabContext = DiamondStartNewCabinet (updatesCabPath);
  3320. if (!hCabContext) {
  3321. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: DiamondStartNewCabinet failed"), 0);
  3322. b = FALSE;
  3323. goto exit;
  3324. }
  3325. bCatalogFileFound = FALSE;
  3326. if (EnumFirstFilePatternRecursive (&e, updatesSourceDir, TEXT("*"), 0)) {
  3327. do {
  3328. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  3329. continue;
  3330. }
  3331. hCab = hCabContext;
  3332. cabPath = updatesCabPath;
  3333. //
  3334. // search for a previous file with the same name
  3335. //
  3336. if (FindStringCell (listUpdatesFiles, e.FileName, FALSE)) {
  3337. DynUpdtDebugLog (
  3338. Winnt32LogError,
  3339. TEXT("pProcessUpdates: found duplicate filename %1; aborting operation"),
  3340. 0,
  3341. updatesCabPath
  3342. );
  3343. b = FALSE;
  3344. break;
  3345. }
  3346. if (!InsertList (
  3347. (PGENERIC_LIST*)&listUpdatesFiles,
  3348. (PGENERIC_LIST)CreateStringCell (e.FileName))
  3349. ) {
  3350. b = FALSE;
  3351. break;
  3352. }
  3353. b = DiamondAddFileToCabinet (hCab, e.FullPath, e.FileName);
  3354. if (!b) {
  3355. DynUpdtDebugLog (
  3356. Winnt32LogError,
  3357. TEXT("pProcessUpdates: DiamondAddFileToCabinet(%1,%2) failed"),
  3358. 0,
  3359. e.FullPath,
  3360. cabPath
  3361. );
  3362. break;
  3363. }
  3364. DynUpdtDebugLog (
  3365. DynUpdtLogLevel,
  3366. TEXT(" ... successfully added file %1 to %2"),
  3367. 0,
  3368. e.FullPath,
  3369. cabPath
  3370. );
  3371. p = GetFileExtension (e.FileName);
  3372. if (p && !lstrcmpi (p, TEXT(".cat"))) {
  3373. bCatalogFileFound = TRUE;
  3374. }
  3375. } while (EnumNextFilePatternRecursive (&e));
  3376. AbortEnumFilePatternRecursive (&e);
  3377. if (!b) {
  3378. goto exit;
  3379. }
  3380. }
  3381. result = DiamondTerminateCabinet (hCabContext);
  3382. if (!b) {
  3383. DiamondTerminateCabinet (hCabContext);
  3384. goto exit;
  3385. }
  3386. if (!result) {
  3387. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: DiamondTerminateCabinet(%1) failed"), 0, updatesCabPath);
  3388. b = FALSE;
  3389. goto exit;
  3390. }
  3391. DynUpdtDebugLog (DynUpdtLogLevel, TEXT(" ... done"), 0);
  3392. if (!bCatalogFileFound) {
  3393. DynUpdtDebugLog (Winnt32LogWarning, TEXT("pProcessUpdates: no catalog found in package %1"), 0, UpdatesCab);
  3394. }
  3395. BuildPath (
  3396. buffer,
  3397. g_DynUpdtStatus->PrepareWinnt32 ? g_DynUpdtStatus->DynamicUpdatesSource : g_DynUpdtStatus->WorkingDir,
  3398. S_CABNAME_UPDATES
  3399. );
  3400. if (!SetFileAttributes (buffer, FILE_ATTRIBUTE_NORMAL) ||
  3401. !DeleteFile (buffer)) {
  3402. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to remove file %1 in order to replace it"), 0, buffer);
  3403. b = FALSE;
  3404. goto exit;
  3405. }
  3406. SetFileAttributes (buffer, FILE_ATTRIBUTE_NORMAL);
  3407. DeleteFile (buffer);
  3408. if (!MoveFile (updatesCabPath, buffer)) {
  3409. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpdates: Unable to move file %1 to %2"), 0, updatesCabPath, buffer);
  3410. b = FALSE;
  3411. goto exit;
  3412. }
  3413. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("pProcessUpdates: moved file %1 to %2"), 0, updatesCabPath, buffer);
  3414. lstrcpy (updatesCabPath, buffer);
  3415. BuildSifName (updatesCabPath, destFilePath);
  3416. if (!CreateFileListSif (destFilePath, S_SECTIONNAME_UPDATES, updatesCabPath)) {
  3417. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to build file %1"), 0, destFilePath);
  3418. b = FALSE;
  3419. goto exit;
  3420. }
  3421. DynUpdtDebugLog (
  3422. DynUpdtLogLevel,
  3423. TEXT("pProcessUpdates: created %1 containing the list of files in %2"),
  3424. 0,
  3425. destFilePath,
  3426. updatesCabPath
  3427. );
  3428. }
  3429. if (!g_DynUpdtStatus->PrepareWinnt32) {
  3430. //
  3431. // build the default path to updates.cab used by the rest of setup
  3432. //
  3433. MYASSERT (IsArc() ? LocalSourceWithPlatform[0] : LocalBootDirectory[0]);
  3434. BuildPath (
  3435. g_DynUpdtStatus->UpdatesCabTarget,
  3436. IsArc() ? LocalSourceWithPlatform : LocalBootDirectory,
  3437. S_CABNAME_UPDATES
  3438. );
  3439. //
  3440. // remember current location of updates.cab
  3441. //
  3442. lstrcpy (g_DynUpdtStatus->UpdatesCabSource, UpdatesCab);
  3443. //
  3444. // the location of updated files for replacement
  3445. //
  3446. lstrcpy (g_DynUpdtStatus->UpdatesPath, updatesSourceDir);
  3447. //
  3448. // also check for the presence of a file that will cause winnt32 to build the ~LS directory
  3449. //
  3450. BuildPath (destFilePath, updatesSourceDir, S_MAKE_LS_FILENAME);
  3451. if (pDoesFileExist (destFilePath)) {
  3452. MakeLocalSource = TRUE;
  3453. }
  3454. }
  3455. exit:
  3456. if (hDiamond) {
  3457. DiamondTerminate (hDiamond);
  3458. }
  3459. if (listUpdatesFiles) {
  3460. DeleteStringList (listUpdatesFiles);
  3461. }
  3462. if (!b && UpgradeAdvisorMode) {
  3463. //
  3464. // in UpgradeAdvisor mode we expect failures
  3465. //
  3466. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Unable to process %1 in UpgradeAdvisor mode; ignoring error"), 0, UpdatesCab);
  3467. g_DynUpdtStatus->ForceRemoveWorkingDir = TRUE;
  3468. b = TRUE;
  3469. }
  3470. return b;
  3471. }
  3472. BOOL
  3473. pProcessDuasms (
  3474. IN PCTSTR DuasmsCab,
  3475. IN BOOL ClientInstall
  3476. )
  3477. {
  3478. FILEPATTERN_ENUM e;
  3479. TCHAR duasmsLocalDir[MAX_PATH];
  3480. TCHAR dirName[MAX_PATH];
  3481. DWORD rc;
  3482. HKEY key;
  3483. PCTSTR strDuasmsRegKey;
  3484. BOOL duasms = FALSE;
  3485. BOOL b = TRUE;
  3486. if (!pNonemptyFilePresent (DuasmsCab)) {
  3487. if (!ClientInstall) {
  3488. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Package %1 is not present"), 0, DuasmsCab);
  3489. }
  3490. return TRUE;
  3491. }
  3492. if (g_DynUpdtStatus->PrepareWinnt32) {
  3493. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("pProcessDuasms: Skipping it due to /%1 switch"), 0, WINNT_U_DYNAMICUPDATESPREPARE);
  3494. return TRUE;
  3495. }
  3496. DynUpdtDebugLog (
  3497. DynUpdtLogLevel,
  3498. TEXT("Analyzing package %1..."),
  3499. 0,
  3500. DuasmsCab
  3501. );
  3502. BuildPath (duasmsLocalDir, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_DUASMS);
  3503. //
  3504. // expand CAB in this dir
  3505. //
  3506. MyDelnode (duasmsLocalDir);
  3507. if (CreateMultiLevelDirectory (duasmsLocalDir) != ERROR_SUCCESS) {
  3508. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, duasmsLocalDir);
  3509. return FALSE;
  3510. }
  3511. if (!(*SetupapiCabinetRoutine) (DuasmsCab, 0, pExpandCabInDir, (PVOID)duasmsLocalDir)) {
  3512. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, DuasmsCab);
  3513. return FALSE;
  3514. }
  3515. MYASSERT (IsArc() ? LocalSourceWithPlatform[0] : LocalBootDirectory[0]);
  3516. BuildPath (
  3517. g_DynUpdtStatus->DuasmsTarget,
  3518. IsArc() ? LocalSourceWithPlatform : LocalBootDirectory,
  3519. S_SUBDIRNAME_DUASMS
  3520. );
  3521. //
  3522. // remember current location of duasms folder
  3523. //
  3524. lstrcpy (g_DynUpdtStatus->DuasmsSource, duasmsLocalDir);
  3525. return TRUE;
  3526. }
  3527. BOOL
  3528. pFindPackage (
  3529. IN HINF InfHandle,
  3530. IN PCTSTR Section,
  3531. IN PCTSTR CabName,
  3532. OUT PBOOL Partial
  3533. )
  3534. {
  3535. INFCONTEXT ic;
  3536. TCHAR value[MAX_PATH];
  3537. if (SetupapiFindFirstLine (InfHandle, Section, NULL, &ic)) {
  3538. do {
  3539. if (SetupapiGetStringField (&ic, GUIDRVS_FIELD_CABNAME, value, MAX_PATH, NULL) &&
  3540. !lstrcmpi (value, CabName)
  3541. ) {
  3542. if (Partial) {
  3543. *Partial = SetupapiGetStringField (&ic, GUIDRVS_FIELD_INFNAME, value, MAX_PATH, NULL);
  3544. }
  3545. return TRUE;
  3546. }
  3547. } while (SetupapiFindNextLine (&ic, &ic));
  3548. }
  3549. return FALSE;
  3550. }
  3551. VOID
  3552. pSanitizeDriverCabName (
  3553. IN PTSTR CabName
  3554. )
  3555. {
  3556. #define CRC_SUFFIX_LENGTH 40
  3557. PTSTR p, q;
  3558. DWORD len;
  3559. //
  3560. // cut an extension like _B842485F4D3B024E675653929B247BE9C685BBD7 from the cab name
  3561. //
  3562. p = GetFileExtension (CabName);
  3563. if (p) {
  3564. MYASSERT (*p == TEXT('.'));
  3565. *p = 0;
  3566. q = _tcsrchr (CabName, TEXT('_'));
  3567. if (q) {
  3568. q++;
  3569. len = lstrlen (q);
  3570. if (len == CRC_SUFFIX_LENGTH) {
  3571. PTSTR s = q;
  3572. TCHAR ch;
  3573. while (ch = (TCHAR)_totlower (*s++)) {
  3574. if (!((ch >= TEXT('0') && ch <= TEXT('9')) ||
  3575. (ch >= TEXT('a') && ch <= TEXT('f')))
  3576. ) {
  3577. break;
  3578. }
  3579. }
  3580. if (!ch) {
  3581. //
  3582. // we found what we expect
  3583. //
  3584. *(q - 1) = TEXT('.');
  3585. lstrcpy (q, p + 1);
  3586. p = NULL;
  3587. }
  3588. }
  3589. }
  3590. if (p) {
  3591. *p = TEXT('.');
  3592. }
  3593. }
  3594. }
  3595. BOOL
  3596. pIsDriverExcluded (
  3597. IN HINF InfHandle,
  3598. IN PCTSTR CabName
  3599. )
  3600. {
  3601. BOOL bPartial;
  3602. if (!pFindPackage (InfHandle, S_SECTION_EXCLUDED_DRVS, CabName, &bPartial)) {
  3603. return FALSE;
  3604. }
  3605. return !bPartial;
  3606. }
  3607. BOOL
  3608. pIsPrivateCabinet (
  3609. IN PCTSTR Filename
  3610. )
  3611. {
  3612. static PCTSTR privateCabNames[] = {
  3613. S_CABNAME_IDENT,
  3614. S_CABNAME_WSDUENG,
  3615. S_CABNAME_UPDATES,
  3616. S_CABNAME_UPGINFS,
  3617. S_CABNAME_WINNT32,
  3618. S_CABNAME_MIGDLLS,
  3619. S_CABNAME_DUASMS,
  3620. };
  3621. INT i;
  3622. for (i = 0; i < sizeof (privateCabNames) / sizeof (privateCabNames[0]); i++) {
  3623. if (!lstrcmpi (Filename, privateCabNames[i])) {
  3624. return TRUE;
  3625. }
  3626. }
  3627. return FALSE;
  3628. }
  3629. BOOL
  3630. pIsPrivateSubdir (
  3631. IN PCTSTR Subdir
  3632. )
  3633. {
  3634. static PCTSTR privateSubDirNames[] = {
  3635. S_SUBDIRNAME_TEMP,
  3636. S_SUBDIRNAME_DRIVERS,
  3637. S_SUBDIRNAME_WINNT32,
  3638. S_SUBDIRNAME_UPDATES,
  3639. S_SUBDIRNAME_UPGINFS,
  3640. S_SUBDIRNAME_MIGDLLS,
  3641. S_SUBDIRNAME_DUASMS,
  3642. };
  3643. INT i;
  3644. for (i = 0; i < sizeof (privateSubDirNames) / sizeof (privateSubDirNames[0]); i++) {
  3645. if (!lstrcmpi (Subdir, privateSubDirNames[i])) {
  3646. return TRUE;
  3647. }
  3648. }
  3649. return FALSE;
  3650. }
  3651. BOOL
  3652. pFindValueInSectionAtFieldIndex (
  3653. IN HINF InfHandle,
  3654. IN PCTSTR Section,
  3655. IN DWORD FieldIndex,
  3656. IN PCTSTR FieldValue
  3657. )
  3658. {
  3659. INFCONTEXT ic;
  3660. TCHAR value[MAX_PATH];
  3661. if (SetupapiFindFirstLine (InfHandle, Section, NULL, &ic)) {
  3662. do {
  3663. if (SetupapiGetStringField (&ic, FieldIndex, value, MAX_PATH, NULL)) {
  3664. if (lstrcmpi (FieldValue, value) == 0) {
  3665. return TRUE;
  3666. }
  3667. }
  3668. } while (SetupapiFindNextLine (&ic, &ic));
  3669. }
  3670. return FALSE;
  3671. }
  3672. BOOL
  3673. pProcessNewdrvs (
  3674. IN PCTSTR NewdrvDir,
  3675. IN BOOL ClientInstall
  3676. )
  3677. /*++
  3678. All CABs in this dir except pIsPrivateCabinet() files are considered
  3679. as containing new drivers. Each cab will be expanded in its own subdir (derived from cab filename)
  3680. --*/
  3681. {
  3682. FILEPATTERN_ENUM e;
  3683. FILEPATTERNREC_ENUM er;
  3684. TCHAR dirName[MAX_PATH];
  3685. TCHAR datFile[MAX_PATH];
  3686. TCHAR relocDriverPath[MAX_PATH];
  3687. PTSTR p;
  3688. PSDLIST entry;
  3689. HANDLE hDB;
  3690. BOOL bCreateHwdb;
  3691. BOOL bDriverNeeded;
  3692. HINF infHandle;
  3693. enum {
  3694. CT_UNKNOWN,
  3695. CT_GUI_APPROVED,
  3696. CT_GUI_NOT_APPROVED
  3697. } eContentType;
  3698. BOOL bDriverIsGuiApproved;
  3699. PSTRINGLIST missingPnpIds = NULL;
  3700. PSTRINGLIST listEntry;
  3701. BOOL bEntryFound;
  3702. INFCONTEXT ic;
  3703. TCHAR value[MAX_PATH];
  3704. TCHAR sanitizedName[MAX_PATH];
  3705. BOOL b = TRUE;
  3706. __try {
  3707. //
  3708. // first open guidrvs.inf
  3709. //
  3710. BuildPath (datFile, g_DynUpdtStatus->DynamicUpdatesSource, S_GUI_DRIVERS_INF);
  3711. infHandle = SetupapiOpenInfFile (datFile, NULL, INF_STYLE_WIN4, NULL);
  3712. if (infHandle != INVALID_HANDLE_VALUE) {
  3713. //
  3714. // copy this file together with the drivers packages (if any)
  3715. //
  3716. lstrcpy (g_DynUpdtStatus->GuidrvsInfSource, datFile);
  3717. } else {
  3718. DynUpdtDebugLog (
  3719. Winnt32LogWarning,
  3720. TEXT("Could not open INF file %1 (rc=%2!u!)"),
  3721. 0,
  3722. datFile,
  3723. GetLastError ()
  3724. );
  3725. }
  3726. //
  3727. // look for CAB files and expand each one in its own subdir
  3728. //
  3729. if (!ClientInstall) {
  3730. if (EnumFirstFilePatternRecursive (&er, NewdrvDir, TEXT("*.cab"), ECF_ENUM_SUBDIRS)) {
  3731. do {
  3732. if (g_DynUpdtStatus->Cancelled) {
  3733. SetLastError (ERROR_CANCELLED);
  3734. b = FALSE;
  3735. break;
  3736. }
  3737. if (er.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  3738. if (pIsPrivateSubdir (er.SubPath)) {
  3739. er.ControlFlags |= ECF_ABORT_ENUM_DIR;
  3740. }
  3741. continue;
  3742. }
  3743. if (!er.FindData->nFileSizeLow) {
  3744. DynUpdtDebugLog (Winnt32LogWarning, TEXT("File %1 has size 0 and will be ignored"), 0, er.FullPath);
  3745. continue;
  3746. }
  3747. lstrcpy (sanitizedName, er.FileName);
  3748. pSanitizeDriverCabName (sanitizedName);
  3749. if (pIsPrivateCabinet (sanitizedName)) {
  3750. continue;
  3751. }
  3752. BuildPath (dirName, g_DynUpdtStatus->DriversSource, sanitizedName);
  3753. p = GetFileExtension (dirName);
  3754. if (!p) {
  3755. MYASSERT (FALSE);
  3756. continue;
  3757. }
  3758. *p = 0;
  3759. //
  3760. // is this an excluded driver?
  3761. //
  3762. lstrcpy (datFile, sanitizedName);
  3763. p = GetFileExtension (datFile);
  3764. if (!p) {
  3765. MYASSERT (FALSE);
  3766. continue;
  3767. }
  3768. *p = 0;
  3769. if (pIsDriverExcluded (infHandle, datFile)) {
  3770. DynUpdtDebugLog (
  3771. Winnt32LogWarning,
  3772. TEXT("Driver %1 is excluded from processing via %2"),
  3773. 0,
  3774. sanitizedName,
  3775. g_DynUpdtStatus->GuidrvsInfSource
  3776. );
  3777. if (DoesDirectoryExist (dirName)) {
  3778. //
  3779. // make sure there's no hwcomp.dat in this folder
  3780. //
  3781. BuildPath (datFile, dirName, S_HWCOMP_DAT);
  3782. if (pDoesFileExist (datFile)) {
  3783. SetFileAttributes (datFile, FILE_ATTRIBUTE_NORMAL);
  3784. DeleteFile (datFile);
  3785. }
  3786. }
  3787. continue;
  3788. }
  3789. DynUpdtDebugLog (
  3790. DynUpdtLogLevel,
  3791. TEXT("Analyzing driver package %1..."),
  3792. 0,
  3793. er.FullPath
  3794. );
  3795. if (DoesDirectoryExist (dirName)) {
  3796. DynUpdtDebugLog (
  3797. Winnt32LogWarning,
  3798. TEXT("Recreating existing driver %1"),
  3799. 0,
  3800. dirName
  3801. );
  3802. MyDelnode (dirName);
  3803. }
  3804. if (CreateMultiLevelDirectory (dirName) != ERROR_SUCCESS) {
  3805. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, dirName);
  3806. continue;
  3807. }
  3808. //
  3809. // expand CAB in this dir
  3810. //
  3811. if (!(*SetupapiCabinetRoutine) (er.FullPath, 0, pExpandCabInDir, dirName)) {
  3812. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, er.FullPath);
  3813. if (GetLastError () == ERROR_DISK_FULL) {
  3814. DynUpdtDebugLog (Winnt32LogSevereError, TEXT("Disk is full; aborting operation"), 0);
  3815. b = FALSE;
  3816. break;
  3817. }
  3818. continue;
  3819. }
  3820. if (g_DynUpdtStatus->PrepareWinnt32) {
  3821. //
  3822. // just rebuild the hardware database
  3823. //
  3824. if (!pBuildHwcompDat (dirName, infHandle, TRUE, TRUE)) {
  3825. DynUpdtDebugLog (
  3826. Winnt32LogError,
  3827. TEXT("Unable to build %1 (pBuildHwcompDat failed)"),
  3828. 0,
  3829. dirName
  3830. );
  3831. continue;
  3832. }
  3833. }
  3834. } while (EnumNextFilePatternRecursive (&er));
  3835. AbortEnumFilePatternRecursive (&er);
  3836. }
  3837. }
  3838. if (!b) {
  3839. __leave;
  3840. }
  3841. if (!g_DynUpdtStatus->PrepareWinnt32 &&
  3842. (!ISNT() || OsVersion.dwMajorVersion > 4)
  3843. ) {
  3844. //
  3845. // look for driver dirs and analyze them
  3846. //
  3847. if (infHandle != INVALID_HANDLE_VALUE) {
  3848. //
  3849. // read the value of "DriversAreGuiApproved" key
  3850. // 1. if set to "Yes" that means all drivers listed in the [Drivers] section are approved
  3851. // for installation in GUI setup; any other driver is not approved for installation in GUI setup
  3852. // 2. if set to "No" all drivers listed are NOT good for installation in GUI setup; their install
  3853. // will be deferred post setup; any driver not listed in that section is good for GUI
  3854. // 3. if not present or set to any other value, it is ignored and the section [Drivers] is ignored;
  3855. // all drivers will be installed post GUI setup
  3856. //
  3857. eContentType = CT_UNKNOWN;
  3858. if (SetupapiFindFirstLine (infHandle, S_SECTION_VERSION, S_DRIVER_TYPE_KEY, &ic) &&
  3859. SetupapiGetStringField (&ic, 1, value, MAX_PATH, NULL)
  3860. ) {
  3861. if (!lstrcmpi (value, WINNT_A_YES)) {
  3862. eContentType = CT_GUI_APPROVED;
  3863. } else if (!lstrcmpi (value, WINNT_A_NO)) {
  3864. eContentType = CT_GUI_NOT_APPROVED;
  3865. }
  3866. }
  3867. if (eContentType != CT_UNKNOWN) {
  3868. DynUpdtDebugLog (
  3869. Winnt32LogInformation,
  3870. TEXT("Entries in section [%1] of %2 will be treated as drivers to %3 be installed during GUI setup"),
  3871. 0,
  3872. S_SECTION_DRIVERS,
  3873. g_DynUpdtStatus->GuidrvsInfSource,
  3874. eContentType == CT_GUI_APPROVED ? TEXT("") : TEXT("NOT")
  3875. );
  3876. } else {
  3877. DynUpdtDebugLog (
  3878. Winnt32LogWarning,
  3879. TEXT("Key %1 %5 in file %2 section [%3];")
  3880. TEXT(" entries in section [%4] will be ignored and all drivers will be installed post setup"),
  3881. 0,
  3882. S_DRIVER_TYPE_KEY,
  3883. g_DynUpdtStatus->GuidrvsInfSource,
  3884. S_SECTION_VERSION,
  3885. S_SECTION_DRIVERS,
  3886. value ? TEXT("has an invalid value") : TEXT("is not present")
  3887. );
  3888. }
  3889. }
  3890. if (EnumFirstFilePattern (&e, g_DynUpdtStatus->DriversSource, TEXT("*"))) {
  3891. //
  3892. // initialize the Whistler PNP database
  3893. //
  3894. if (!g_DynUpdtStatus->HwdbDatabase) {
  3895. //
  3896. // ignore db load error
  3897. //
  3898. pInitNtPnpDb (TRUE);
  3899. }
  3900. do {
  3901. if (g_DynUpdtStatus->Cancelled) {
  3902. SetLastError (ERROR_CANCELLED);
  3903. b = FALSE;
  3904. break;
  3905. }
  3906. if (!(e.FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  3907. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("File %1 is NOT a directory and will be ignored"), 0, e.FullPath);
  3908. continue;
  3909. }
  3910. //
  3911. // is this a needed driver?
  3912. //
  3913. bDriverNeeded = TRUE;
  3914. if (g_DynUpdtStatus->UserSpecifiedUpdates) {
  3915. //
  3916. // first build the list of missing drivers
  3917. //
  3918. if (!missingPnpIds) {
  3919. #ifdef UNICODE
  3920. missingPnpIds = BuildMissingPnpIdList ();
  3921. if (!missingPnpIds) {
  3922. DynUpdtDebugLog (
  3923. Winnt32LogInformation,
  3924. TEXT("No PNP device drivers are needed"),
  3925. 0
  3926. );
  3927. }
  3928. #else
  3929. //
  3930. // let the upgrade module do driver detection on Win9x
  3931. //
  3932. if (pLoadWin9xDuSupport ()) {
  3933. PSTR* incompWin9xDrivers;
  3934. PCSTR* q;
  3935. if (g_DynUpdtStatus->Win9xGetIncompDrvs (&incompWin9xDrivers)) {
  3936. //
  3937. // convert the array returned by this function to a list style
  3938. //
  3939. g_DynUpdtStatus->IncompatibleDriversCount = 0;
  3940. if (incompWin9xDrivers) {
  3941. for (q = incompWin9xDrivers; *q; q++) {
  3942. listEntry = (PSTRINGLIST) MALLOC (sizeof (STRINGLIST));
  3943. if (listEntry) {
  3944. listEntry->String = DupMultiSz (*q);
  3945. if (!listEntry->String) {
  3946. break;
  3947. }
  3948. listEntry->Next = NULL;
  3949. if (!InsertList ((PGENERIC_LIST*)&missingPnpIds, (PGENERIC_LIST)listEntry)) {
  3950. DeleteStringCell (listEntry);
  3951. break;
  3952. }
  3953. g_DynUpdtStatus->IncompatibleDriversCount++;
  3954. }
  3955. }
  3956. }
  3957. if (g_DynUpdtStatus->Win9xReleaseIncompDrvs) {
  3958. g_DynUpdtStatus->Win9xReleaseIncompDrvs (incompWin9xDrivers);
  3959. }
  3960. } else {
  3961. DynUpdtDebugLog (
  3962. DynUpdtLogLevel,
  3963. TEXT("Win9xGetIncompDrvs returned FALSE; no drivers will be analyzed"),
  3964. 0
  3965. );
  3966. }
  3967. }
  3968. #endif
  3969. if (!missingPnpIds) {
  3970. break;
  3971. }
  3972. }
  3973. bCreateHwdb = FALSE;
  3974. //
  3975. // use the existing hardware database
  3976. //
  3977. BuildPath (datFile, e.FullPath, S_HWCOMP_DAT);
  3978. if (!pDoesFileExist (datFile)) {
  3979. bCreateHwdb = TRUE;
  3980. }
  3981. hDB = g_DynUpdtStatus->HwdbOpen (bCreateHwdb ? NULL : datFile);
  3982. if (!hDB) {
  3983. if (bCreateHwdb) {
  3984. b = FALSE;
  3985. break;
  3986. }
  3987. DynUpdtDebugLog (
  3988. Winnt32LogError,
  3989. TEXT("Hardware database %1 is corrupt; contact your system administrator"),
  3990. 0,
  3991. datFile
  3992. );
  3993. continue;
  3994. }
  3995. if (bCreateHwdb) {
  3996. if (!g_DynUpdtStatus->HwdbAppendInfs (
  3997. hDB,
  3998. e.FullPath,
  3999. infHandle != INVALID_HANDLE_VALUE ? Winnt32HwdbAppendInfsCallback : NULL,
  4000. (PVOID)infHandle
  4001. )) {
  4002. DynUpdtDebugLog (
  4003. Winnt32LogError,
  4004. TEXT("Unable to build %1; contact your system administrator"),
  4005. 0,
  4006. datFile
  4007. );
  4008. g_DynUpdtStatus->HwdbClose (hDB);
  4009. continue;
  4010. }
  4011. //
  4012. // rebuild the default HW precompiled database
  4013. //
  4014. BuildPath (datFile, e.FullPath, S_HWCOMP_DAT);
  4015. SetFileAttributes (datFile, FILE_ATTRIBUTE_NORMAL);
  4016. DeleteFile (datFile);
  4017. if (!g_DynUpdtStatus->HwdbFlush (hDB, datFile)) {
  4018. DynUpdtDebugLog (
  4019. Winnt32LogError,
  4020. TEXT("Unable to build %1; contact your system administrator"),
  4021. 0,
  4022. datFile
  4023. );
  4024. g_DynUpdtStatus->HwdbClose (hDB);
  4025. continue;
  4026. }
  4027. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Successfully built precompiled hardware database %1"), 0, datFile);
  4028. }
  4029. //
  4030. // check if this particular driver is among the ones that are needed
  4031. //
  4032. if (!pHwdbHasAnyMissingDrivers (hDB, missingPnpIds)) {
  4033. //
  4034. // this driver is not needed
  4035. //
  4036. bDriverNeeded = FALSE;
  4037. }
  4038. g_DynUpdtStatus->HwdbClose (hDB);
  4039. }
  4040. if (!bDriverNeeded) {
  4041. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("No needed drivers found in package %1"), 0, e.FullPath);
  4042. continue;
  4043. }
  4044. //
  4045. // is this a boot driver or a regular one?
  4046. //
  4047. if (pIsBootDriver (e.FullPath)) {
  4048. //
  4049. // add this driver to the list of boot drivers
  4050. //
  4051. if (!InsertList (
  4052. (PGENERIC_LIST*)&g_DynUpdtStatus->BootDriverPathList,
  4053. (PGENERIC_LIST)CreateStringCell (e.FileName))
  4054. ) {
  4055. b = FALSE;
  4056. break;
  4057. }
  4058. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Added driver %1 to the list of BOOT drivers"), 0, e.FullPath);
  4059. }
  4060. //
  4061. // all needed drivers will be copied under LocalBootDir to be protected
  4062. // from being deleted if user decides to remove the current OS partition
  4063. //
  4064. BuildPath (relocDriverPath, IsArc() ? LocalSourceWithPlatform : LocalBootDirectory, S_SUBDIRNAME_DRIVERS);
  4065. ConcatenatePaths (relocDriverPath, e.FileName, MAX_PATH);
  4066. //
  4067. // is this a GUI driver or not?
  4068. //
  4069. if (eContentType == CT_UNKNOWN) {
  4070. bDriverIsGuiApproved = FALSE;
  4071. } else {
  4072. if (pFindValueInSectionAtFieldIndex (infHandle, S_SECTION_EXCLUDED_DRVS, GUIDRVS_FIELD_CABNAME, e.FileName)) {
  4073. //
  4074. // we don't support "partially excluded" packages for the device install
  4075. // phase of GUI setup
  4076. //
  4077. DynUpdtDebugLog (
  4078. DynUpdtLogLevel,
  4079. TEXT("Driver %1 is partially excluded; it will be installed at the end of GUI setup"),
  4080. 0,
  4081. e.FullPath
  4082. );
  4083. bDriverIsGuiApproved = FALSE;
  4084. } else {
  4085. BOOL bPartial;
  4086. bEntryFound = pFindPackage (infHandle, S_SECTION_DRIVERS, e.FileName, &bPartial);
  4087. bDriverIsGuiApproved = eContentType == CT_GUI_APPROVED && bEntryFound && !bPartial ||
  4088. eContentType == CT_GUI_NOT_APPROVED && !bEntryFound;
  4089. }
  4090. }
  4091. //
  4092. // always make sure there's a precompiled database hwcomp.dat
  4093. // to be used at the time setup will install these additional drivers
  4094. //
  4095. if (!pBuildHwcompDat (e.FullPath, infHandle, FALSE, FALSE)) {
  4096. DynUpdtDebugLog (
  4097. Winnt32LogError,
  4098. TEXT("Unable to build %1 (pBuildHwcompDat failed)"),
  4099. 0,
  4100. datFile
  4101. );
  4102. continue;
  4103. }
  4104. entry = MALLOC (sizeof (SDLIST));
  4105. if (!entry) {
  4106. b = FALSE;
  4107. break;
  4108. }
  4109. entry->String = DupString (relocDriverPath);
  4110. if (!entry->String) {
  4111. FREE (entry);
  4112. b = FALSE;
  4113. break;
  4114. }
  4115. entry->Data = (DWORD_PTR)bDriverIsGuiApproved;
  4116. entry->Next = NULL;
  4117. if (!InsertList (
  4118. (PGENERIC_LIST*)&g_DynUpdtStatus->NewDriversList,
  4119. (PGENERIC_LIST)entry
  4120. )) {
  4121. FREE (entry);
  4122. b = FALSE;
  4123. break;
  4124. }
  4125. DynUpdtDebugLog (
  4126. DynUpdtLogLevel,
  4127. bDriverIsGuiApproved ?
  4128. TEXT("Driver %1 is approved for installation during GUI setup") :
  4129. TEXT("Driver %1 is NOT approved for installation during GUI setup; installation will be deferred post-setup"),
  4130. 0,
  4131. e.FullPath
  4132. );
  4133. //
  4134. // copy locally this driver package (if from a share)
  4135. //
  4136. BuildPath (relocDriverPath, g_DynUpdtStatus->SelectedDrivers, e.FileName);
  4137. if (lstrcmpi (e.FullPath, relocDriverPath)) {
  4138. if (!CopyTree (e.FullPath, relocDriverPath)) {
  4139. DynUpdtDebugLog (
  4140. Winnt32LogError,
  4141. TEXT("Unable to copy driver %1 to %2"),
  4142. 0,
  4143. e.FullPath,
  4144. relocDriverPath
  4145. );
  4146. b = FALSE;
  4147. break;
  4148. }
  4149. DynUpdtDebugLog (
  4150. Winnt32LogInformation,
  4151. TEXT("Driver %1 successfully copied to %2"),
  4152. 0,
  4153. e.FullPath,
  4154. relocDriverPath
  4155. );
  4156. }
  4157. } while (EnumNextFilePattern (&e));
  4158. AbortEnumFilePattern (&e);
  4159. } else {
  4160. DynUpdtDebugLog (
  4161. DynUpdtLogLevel,
  4162. TEXT("No drivers found in %1"),
  4163. 0,
  4164. g_DynUpdtStatus->DriversSource
  4165. );
  4166. }
  4167. if (!b) {
  4168. __leave;
  4169. }
  4170. //
  4171. // copy guidrvs.inf if present and any driver package that will be migrated over
  4172. //
  4173. if (g_DynUpdtStatus->GuidrvsInfSource[0] && g_DynUpdtStatus->NewDriversList) {
  4174. BuildPath (datFile, g_DynUpdtStatus->SelectedDrivers, S_GUI_DRIVERS_INF);
  4175. if (lstrcmpi (g_DynUpdtStatus->GuidrvsInfSource, datFile)) {
  4176. if (!CopyFile (g_DynUpdtStatus->GuidrvsInfSource, datFile, FALSE)) {
  4177. DynUpdtDebugLog (
  4178. Winnt32LogError,
  4179. TEXT("Failed to copy %1 to %2"),
  4180. 0,
  4181. g_DynUpdtStatus->GuidrvsInfSource,
  4182. datFile
  4183. );
  4184. b = FALSE;
  4185. }
  4186. }
  4187. if (b) {
  4188. //
  4189. // update the location of guidrvs.inf after file copy will have been done
  4190. //
  4191. BuildPath (
  4192. g_DynUpdtStatus->GuidrvsInfSource,
  4193. IsArc() ? LocalSourceWithPlatform : LocalBootDirectory,
  4194. S_SUBDIRNAME_DRIVERS
  4195. );
  4196. ConcatenatePaths (g_DynUpdtStatus->GuidrvsInfSource, S_GUI_DRIVERS_INF, MAX_PATH);
  4197. }
  4198. }
  4199. }
  4200. }
  4201. __finally {
  4202. if (missingPnpIds) {
  4203. DeleteStringList (missingPnpIds);
  4204. }
  4205. if (infHandle != INVALID_HANDLE_VALUE) {
  4206. SetupapiCloseInfFile (infHandle);
  4207. }
  4208. }
  4209. return b;
  4210. }
  4211. BOOL
  4212. pProcessUpginfs (
  4213. IN PCTSTR UpginfsCab,
  4214. IN BOOL ClientInstall
  4215. )
  4216. {
  4217. FILEPATTERNREC_ENUM e;
  4218. TCHAR upginfsSourceDir[MAX_PATH];
  4219. TCHAR upginfsDir[MAX_PATH];
  4220. TCHAR upginfsFile[MAX_PATH];
  4221. TCHAR origSubPath[MAX_PATH];
  4222. TCHAR origFileName[MAX_PATH];
  4223. TCHAR origFilePath[MAX_PATH];
  4224. TCHAR destFilePath[MAX_PATH];
  4225. TCHAR buffer[MAX_PATH];
  4226. PTSTR p;
  4227. BOOL b = TRUE;
  4228. BuildPath (upginfsSourceDir, g_DynUpdtStatus->DynamicUpdatesSource, S_SUBDIRNAME_UPGINFS);
  4229. if (ClientInstall) {
  4230. if (!DoesDirectoryExist (upginfsSourceDir)) {
  4231. return TRUE;
  4232. }
  4233. } else {
  4234. //
  4235. // expand it in the corresponding subdir
  4236. //
  4237. if (!pNonemptyFilePresent (UpginfsCab)) {
  4238. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("Package %1 is not present"), 0, UpginfsCab);
  4239. return TRUE;
  4240. }
  4241. DynUpdtDebugLog (
  4242. DynUpdtLogLevel,
  4243. TEXT("Analyzing package %1..."),
  4244. 0,
  4245. UpginfsCab
  4246. );
  4247. //
  4248. // expand CAB in this dir
  4249. // make sure dir is initially empty
  4250. //
  4251. MyDelnode (upginfsSourceDir);
  4252. if (CreateMultiLevelDirectory (upginfsSourceDir) != ERROR_SUCCESS) {
  4253. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, upginfsSourceDir);
  4254. return FALSE;
  4255. }
  4256. if (!(*SetupapiCabinetRoutine) (UpginfsCab, 0, pExpandCabInDir, (PVOID)upginfsSourceDir)) {
  4257. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, UpginfsCab);
  4258. return FALSE;
  4259. }
  4260. //
  4261. // ISSUE: the patching support is currently not available for platforms other than x86
  4262. //
  4263. #ifdef _X86_
  4264. //
  4265. // now let's look for any patches
  4266. //
  4267. if (EnumFirstFilePatternRecursive (&e, upginfsSourceDir, S_PATCH_FILE_EXT, 0)) {
  4268. do {
  4269. BOOL bDeleteTempFile = FALSE;
  4270. if (g_DynUpdtStatus->Cancelled) {
  4271. SetLastError (ERROR_CANCELLED);
  4272. b = FALSE;
  4273. break;
  4274. }
  4275. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  4276. continue;
  4277. }
  4278. DynUpdtDebugLog (
  4279. DynUpdtLogLevel,
  4280. TEXT("pProcessUpginfs: found patch %1"),
  4281. 0,
  4282. e.FullPath
  4283. );
  4284. //
  4285. // get the original file from the sources location
  4286. // the filename is obtained cutting the ._p1 extension
  4287. //
  4288. lstrcpy (origFileName, e.FileName);
  4289. p = GetFileExtension (origFileName);
  4290. if (!p) {
  4291. MYASSERT (FALSE);
  4292. continue;
  4293. }
  4294. *p = 0;
  4295. lstrcpy (origSubPath, e.SubPath);
  4296. p = GetFileExtension (origSubPath);
  4297. if (!p) {
  4298. MYASSERT (FALSE);
  4299. continue;
  4300. }
  4301. *p = 0;
  4302. BuildPath (origFilePath, NativeSourcePaths[0], origSubPath);
  4303. //
  4304. // now check if this file (in it's compressed form or not) actually exists
  4305. //
  4306. if (!pDoesFileExist (origFilePath)) {
  4307. //
  4308. // try the compressed form
  4309. //
  4310. p = _tcschr (origFilePath, 0);
  4311. MYASSERT (p);
  4312. if (!p) {
  4313. continue;
  4314. }
  4315. p = _tcsdec (origFilePath, p);
  4316. MYASSERT (p);
  4317. if (!p) {
  4318. continue;
  4319. }
  4320. *p = TEXT('_');
  4321. if (!pDoesFileExist (origFilePath)) {
  4322. DynUpdtDebugLog (
  4323. Winnt32LogError,
  4324. TEXT("pProcessUpginfs: Unable to find original file %1 to apply the patch"),
  4325. 0,
  4326. origSubPath
  4327. );
  4328. b = FALSE;
  4329. break;
  4330. }
  4331. //
  4332. // expand the file to the temp dir
  4333. //
  4334. BuildPath (buffer, g_DynUpdtStatus->TempDir, origSubPath);
  4335. p = _tcsrchr (buffer, TEXT('\\'));
  4336. MYASSERT (p);
  4337. if (!p) {
  4338. continue;
  4339. }
  4340. *p = 0;
  4341. if (CreateMultiLevelDirectory (buffer) != ERROR_SUCCESS) {
  4342. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: Unable to create dir %1"), 0, buffer);
  4343. b = FALSE;
  4344. break;
  4345. }
  4346. if (!(*SetupapiCabinetRoutine) (origFilePath, 0, pExpandCabInDir, buffer)) {
  4347. DynUpdtDebugLog (
  4348. Winnt32LogError,
  4349. TEXT("pProcessUpginfs: Unable to expand original file %1 to dir %2"),
  4350. 0,
  4351. origFilePath,
  4352. buffer
  4353. );
  4354. b = FALSE;
  4355. break;
  4356. }
  4357. *p = TEXT('\\');
  4358. lstrcpy (origFilePath, buffer);
  4359. bDeleteTempFile = TRUE;
  4360. }
  4361. BuildPath (destFilePath, upginfsSourceDir, TEXT("$$temp$$.~~~"));
  4362. //
  4363. // now really apply the patch
  4364. //
  4365. if (!ApplyPatchToFile (e.FullPath, origFilePath, destFilePath, 0)) {
  4366. DynUpdtDebugLog (
  4367. Winnt32LogError,
  4368. TEXT("pProcessUpginfs: ApplyPatchToFile failed to apply patch %1 to file %2"),
  4369. 0,
  4370. e.FullPath,
  4371. origFilePath
  4372. );
  4373. b = FALSE;
  4374. break;
  4375. }
  4376. //
  4377. // success! now move the file to the real destination
  4378. //
  4379. BuildPath (buffer, upginfsSourceDir, origFileName);
  4380. if (pDoesFileExist (buffer)) {
  4381. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: duplicate file found %1"), 0, origFileName);
  4382. b = FALSE;
  4383. break;
  4384. }
  4385. //
  4386. // all patches MUST be .rep files; change extension from .inf to .rep
  4387. //
  4388. p = GetFileExtension (buffer);
  4389. if (!p || lstrcmpi (p, TEXT(".inf"))) {
  4390. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: Unexpected file extension in %1"), 0, buffer);
  4391. b = FALSE;
  4392. break;
  4393. }
  4394. lstrcpy (p, TEXT(".rep"));
  4395. SetFileAttributes (buffer, FILE_ATTRIBUTE_NORMAL);
  4396. DeleteFile (buffer);
  4397. if (!MoveFile (destFilePath, buffer)) {
  4398. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: Unable to move file %1 to final dest %2"), 0, destFilePath, buffer);
  4399. b = FALSE;
  4400. break;
  4401. }
  4402. DynUpdtDebugLog (
  4403. DynUpdtLogLevel,
  4404. TEXT("pProcessUpginfs: successfully applied patch %1 to file %2; the new file was renamed %3"),
  4405. 0,
  4406. e.FullPath,
  4407. origFilePath,
  4408. buffer
  4409. );
  4410. //
  4411. // now remove the patch file
  4412. //
  4413. SetFileAttributes (e.FullPath, FILE_ATTRIBUTE_NORMAL);
  4414. DeleteFile (e.FullPath);
  4415. if (bDeleteTempFile) {
  4416. SetFileAttributes (origFilePath, FILE_ATTRIBUTE_NORMAL);
  4417. DeleteFile (origFilePath);
  4418. }
  4419. } while (EnumNextFilePatternRecursive (&e));
  4420. AbortEnumFilePatternRecursive (&e);
  4421. if (!b) {
  4422. goto exit;
  4423. }
  4424. }
  4425. SetFileAttributes (UpginfsCab, FILE_ATTRIBUTE_NORMAL);
  4426. DeleteFile (UpginfsCab);
  4427. #endif
  4428. }
  4429. if (!b) {
  4430. goto exit;
  4431. }
  4432. if (!g_DynUpdtStatus->PrepareWinnt32) {
  4433. //
  4434. // only do file installation on Win9x platforms
  4435. //
  4436. OSVERSIONINFO vi;
  4437. vi.dwOSVersionInfoSize = sizeof (vi);
  4438. GetVersionEx (&vi);
  4439. if (vi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
  4440. DynUpdtDebugLog (
  4441. DynUpdtLogLevel,
  4442. TEXT("Package %1 ignored on NT platforms"),
  4443. 0,
  4444. UpginfsCab
  4445. );
  4446. return TRUE;
  4447. }
  4448. //
  4449. // prepare the target directory (%windir%\upginfs)
  4450. //
  4451. if (!MyGetWindowsDirectory (upginfsDir, MAX_PATH)) {
  4452. return FALSE;
  4453. }
  4454. ConcatenatePaths (upginfsDir, S_SUBDIRNAME_UPGINFS, MAX_PATH);
  4455. if (!CreateDir (upginfsDir)) {
  4456. return FALSE;
  4457. }
  4458. //
  4459. // copy relevant files to %windir%\Upginfs
  4460. //
  4461. if (EnumFirstFilePatternRecursive (&e, upginfsSourceDir, TEXT("*.add"), 0)) {
  4462. do {
  4463. if (g_DynUpdtStatus->Cancelled) {
  4464. SetLastError (ERROR_CANCELLED);
  4465. b = FALSE;
  4466. break;
  4467. }
  4468. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  4469. continue;
  4470. }
  4471. if (!e.FindData->nFileSizeLow) {
  4472. DynUpdtDebugLog (Winnt32LogWarning, TEXT("File %1 has size 0 and will be ignored"), 0, e.FullPath);
  4473. continue;
  4474. }
  4475. BuildPath (upginfsFile, upginfsDir, e.FileName);
  4476. SetFileAttributes (upginfsFile, FILE_ATTRIBUTE_NORMAL);
  4477. if (!CopyFile (e.FullPath, upginfsFile, FALSE)) {
  4478. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: Error copying %1 to %2"), 0, e.FullPath, upginfsFile);
  4479. b = FALSE;
  4480. break;
  4481. }
  4482. //
  4483. // let w95upg.dll know about the new files
  4484. //
  4485. UpginfsUpdated = TRUE;
  4486. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("pProcessUpginfs: INF %1 successfully copied to %2"), 0, e.FullPath, upginfsFile);
  4487. } while (EnumNextFilePatternRecursive (&e));
  4488. AbortEnumFilePatternRecursive (&e);
  4489. }
  4490. if (b) {
  4491. if (EnumFirstFilePatternRecursive (&e, upginfsSourceDir, TEXT("*.rep"), 0)) {
  4492. do {
  4493. if (g_DynUpdtStatus->Cancelled) {
  4494. SetLastError (ERROR_CANCELLED);
  4495. b = FALSE;
  4496. break;
  4497. }
  4498. if (e.FindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  4499. continue;
  4500. }
  4501. if (!e.FindData->nFileSizeLow) {
  4502. DynUpdtDebugLog (Winnt32LogWarning, TEXT("File %1 has size 0 and will be ignored"), 0, e.FullPath);
  4503. continue;
  4504. }
  4505. BuildPath (upginfsFile, upginfsDir, e.FileName);
  4506. SetFileAttributes (upginfsFile, FILE_ATTRIBUTE_NORMAL);
  4507. if (!CopyFile (e.FullPath, upginfsFile, FALSE)) {
  4508. DynUpdtDebugLog (Winnt32LogError, TEXT("pProcessUpginfs: Error copying %1 to %2"), 0, e.FullPath, upginfsFile);
  4509. b = FALSE;
  4510. break;
  4511. }
  4512. //
  4513. // let w95upg.dll know about the new files
  4514. //
  4515. UpginfsUpdated = TRUE;
  4516. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("pProcessUpginfs: INF %1 successfully copied to %2"), 0, e.FullPath, upginfsFile);
  4517. } while (EnumNextFilePatternRecursive (&e));
  4518. AbortEnumFilePatternRecursive (&e);
  4519. }
  4520. }
  4521. }
  4522. exit:
  4523. return b;
  4524. }
  4525. #ifdef _X86_
  4526. BOOL
  4527. pProcessMigdlls (
  4528. IN PCTSTR MigdllsCab,
  4529. IN BOOL ClientInstall
  4530. )
  4531. {
  4532. FILEPATTERN_ENUM e;
  4533. TCHAR migdllsLocalDir[MAX_PATH];
  4534. TCHAR dirName[MAX_PATH];
  4535. DWORD rc;
  4536. HKEY key;
  4537. PCTSTR strMigdllsRegKey;
  4538. BOOL migdlls = FALSE;
  4539. BOOL b = TRUE;
  4540. if (!pNonemptyFilePresent (MigdllsCab)) {
  4541. return TRUE;
  4542. }
  4543. if (g_DynUpdtStatus->PrepareWinnt32) {
  4544. DynUpdtDebugLog (DynUpdtLogLevel, TEXT("pProcessMigdlls: Skipping it due to /%1 switch"), 0, WINNT_U_DYNAMICUPDATESPREPARE);
  4545. return TRUE;
  4546. }
  4547. DynUpdtDebugLog (
  4548. DynUpdtLogLevel,
  4549. TEXT("Analyzing package %1..."),
  4550. 0,
  4551. MigdllsCab
  4552. );
  4553. BuildPath (migdllsLocalDir, g_DynUpdtStatus->WorkingDir, S_SUBDIRNAME_MIGDLLS);
  4554. //
  4555. // expand CAB in this dir
  4556. //
  4557. MyDelnode (migdllsLocalDir);
  4558. if (CreateMultiLevelDirectory (migdllsLocalDir) != ERROR_SUCCESS) {
  4559. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1"), 0, migdllsLocalDir);
  4560. return FALSE;
  4561. }
  4562. if (!(*SetupapiCabinetRoutine) (MigdllsCab, 0, pExpandCabInDir, (PVOID)migdllsLocalDir)) {
  4563. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1"), 0, MigdllsCab);
  4564. return FALSE;
  4565. }
  4566. //
  4567. // look for CAB files and expand each one in its own subdir
  4568. //
  4569. if (EnumFirstFilePattern (&e, migdllsLocalDir, TEXT("*.cab"))) {
  4570. do {
  4571. if (g_DynUpdtStatus->Cancelled) {
  4572. SetLastError (ERROR_CANCELLED);
  4573. b = FALSE;
  4574. break;
  4575. }
  4576. if (e.FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  4577. continue;
  4578. }
  4579. if (!e.FindData.nFileSizeLow) {
  4580. DynUpdtDebugLog (Winnt32LogWarning, TEXT("File %1 has size 0 and will be ignored"), 0, e.FullPath);
  4581. continue;
  4582. }
  4583. pGetAutoSubdirName (e.FullPath, dirName);
  4584. if (CreateMultiLevelDirectory (dirName) != ERROR_SUCCESS) {
  4585. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to create dir %1; skipping it"), 0, dirName);
  4586. continue;
  4587. }
  4588. //
  4589. // expand CAB in this dir
  4590. //
  4591. if (!(*SetupapiCabinetRoutine) (e.FullPath, 0, pExpandCabInDir, (PVOID)dirName)) {
  4592. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to expand cabinet %1; skipping it"), 0, e.FullPath);
  4593. continue;
  4594. }
  4595. migdlls = TRUE;
  4596. } while (EnumNextFilePattern (&e));
  4597. }
  4598. if (b && migdlls) {
  4599. //
  4600. // register them
  4601. //
  4602. strMigdllsRegKey = ISNT () ? S_REGKEY_MIGRATION_DLLS_WINNT : S_REGKEY_MIGRATION_DLLS_WIN9X;
  4603. rc = RegCreateKey (HKEY_LOCAL_MACHINE, strMigdllsRegKey, &key);
  4604. if (rc == ERROR_SUCCESS) {
  4605. rc = RegSetValueEx (key, S_REGVALUE_DYNUPDT, 0, REG_SZ, (CONST BYTE*)migdllsLocalDir, (lstrlen (migdllsLocalDir) + 1) * sizeof (TCHAR));
  4606. }
  4607. if (rc != ERROR_SUCCESS) {
  4608. DynUpdtDebugLog (Winnt32LogError, TEXT("Unable to register downloaded migdlls (rc=%1!u!)"), 0, rc);
  4609. b = FALSE;
  4610. }
  4611. }
  4612. return b;
  4613. }
  4614. #endif
  4615. BOOL
  4616. ProcessDownloadedFiles (
  4617. OUT PBOOL StopSetup
  4618. )
  4619. {
  4620. TCHAR cabName[MAX_PATH];
  4621. BOOL bClientInstall = FALSE;
  4622. if (g_DynUpdtStatus->UserSpecifiedUpdates && !g_DynUpdtStatus->PrepareWinnt32) {
  4623. bClientInstall = TRUE;
  4624. }
  4625. DynUpdtDebugLog (
  4626. DynUpdtLogLevel,
  4627. TEXT("Source=%1"),
  4628. 0,
  4629. g_DynUpdtStatus->UserSpecifiedUpdates ? g_DynUpdtStatus->DynamicUpdatesSource : TEXT("Windows Update")
  4630. );
  4631. if (!g_DynUpdtStatus->HwdbInitialize) {
  4632. if (CreateMultiLevelDirectory (g_DynUpdtStatus->TempDir) != ERROR_SUCCESS ||
  4633. !pInitializeSupport (S_HWDB_DLL, pLoadHwdbLib, FALSE) ||
  4634. !g_DynUpdtStatus->HwdbInitialize (g_DynUpdtStatus->TempDir)
  4635. ) {
  4636. return FALSE;
  4637. }
  4638. }
  4639. if (!Winnt32Restarted ()) {
  4640. BuildPath (cabName, g_DynUpdtStatus->DynamicUpdatesSource, S_CABNAME_WINNT32);
  4641. if (!pProcessWinnt32Files (cabName, bClientInstall, StopSetup)) {
  4642. return FALSE;
  4643. }
  4644. if (g_DynUpdtStatus->RestartWinnt32) {
  4645. MYASSERT (!g_DynUpdtStatus->PrepareWinnt32);
  4646. return TRUE;
  4647. }
  4648. }
  4649. BuildPath (cabName, g_DynUpdtStatus->DynamicUpdatesSource, S_CABNAME_UPDATES);
  4650. if (!pProcessUpdates (cabName, bClientInstall, StopSetup)) {
  4651. if (g_DynUpdtStatus->PrepareWinnt32) {
  4652. MessageBoxFromMessage (
  4653. g_DynUpdtStatus->ProgressWindow,
  4654. MSG_ERROR_PROCESSING_UPDATES,
  4655. FALSE,
  4656. AppTitleStringId,
  4657. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  4658. GetLastError (),
  4659. cabName
  4660. );
  4661. }
  4662. return FALSE;
  4663. }
  4664. //
  4665. // process New Assemblies on WU
  4666. //
  4667. BuildPath (cabName, g_DynUpdtStatus->DynamicUpdatesSource, S_CABNAME_DUASMS);
  4668. if (!pProcessDuasms (cabName, bClientInstall)) {
  4669. //
  4670. // don't fail DU if we didn't install them
  4671. //
  4672. }
  4673. BuildPath (cabName, g_DynUpdtStatus->DynamicUpdatesSource, S_CABNAME_UPGINFS);
  4674. if (!pProcessUpginfs (cabName, bClientInstall)) {
  4675. return FALSE;
  4676. }
  4677. #ifdef _X86_
  4678. BuildPath (cabName, g_DynUpdtStatus->DynamicUpdatesSource, S_CABNAME_MIGDLLS);
  4679. if (!pProcessMigdlls (cabName, bClientInstall)) {
  4680. return FALSE;
  4681. }
  4682. #endif
  4683. if (!pProcessNewdrvs (g_DynUpdtStatus->DynamicUpdatesSource, bClientInstall)) {
  4684. return FALSE;
  4685. }
  4686. return TRUE;
  4687. }
  4688. BOOL
  4689. Winnt32Restarted (
  4690. VOID
  4691. )
  4692. {
  4693. return g_DynUpdtStatus->Winnt32Restarted;
  4694. }
  4695. BOOL
  4696. Winnt32RestartedWithAF (
  4697. VOID
  4698. )
  4699. {
  4700. return g_DynUpdtStatus->RestartAnswerFile[0];
  4701. }
  4702. VOID
  4703. pLogWininetError (
  4704. IN DWORD Error
  4705. )
  4706. {
  4707. HMODULE hWinInet = LoadLibrary (TEXT("wininet.dll"));
  4708. if (hWinInet) {
  4709. HLOCAL msg = NULL;
  4710. FormatMessage (
  4711. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  4712. hWinInet,
  4713. Error,
  4714. 0,
  4715. (LPTSTR)&msg,
  4716. 0,
  4717. NULL
  4718. );
  4719. if (msg) {
  4720. DynUpdtDebugLog (Winnt32LogError, TEXT("Failure with wininet error code %1!u!: \"%2\""), 0, Error, msg);
  4721. LocalFree (msg);
  4722. }
  4723. FreeLibrary (hWinInet);
  4724. }
  4725. }
  4726. VOID
  4727. pLogStandardError (
  4728. IN DWORD Error
  4729. )
  4730. {
  4731. HLOCAL msg = NULL;
  4732. FormatMessage (
  4733. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  4734. NULL,
  4735. Error,
  4736. 0,
  4737. (LPTSTR)&msg,
  4738. 0,
  4739. NULL
  4740. );
  4741. if (msg) {
  4742. DynUpdtDebugLog (Winnt32LogError, TEXT("Failure with standard error code %1!u!:\r\n%2"), 0, Error, msg);
  4743. LocalFree (msg);
  4744. }
  4745. }
  4746. VOID
  4747. pUpdateDUStatus (
  4748. IN DWORD Error
  4749. )
  4750. {
  4751. MYASSERT (Error != ERROR_SUCCESS);
  4752. if (Error == ERROR_SUCCESS) {
  4753. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4754. return;
  4755. }
  4756. switch (Error) {
  4757. case ERROR_CONNECTION_UNAVAIL:
  4758. //
  4759. // ask for manual connection
  4760. //
  4761. MYASSERT (g_DynUpdtStatus->DUStatus == DUS_PREPARING);
  4762. g_DynUpdtStatus->DUStatus = DUS_PREPARING_CONNECTIONUNAVAILABLE;
  4763. break;
  4764. case ERROR_INTERNET_INVALID_URL:
  4765. case ERROR_INTERNET_NAME_NOT_RESOLVED:
  4766. //
  4767. // site not available; ask user if they want to retry
  4768. //
  4769. MYASSERT (g_DynUpdtStatus->DUStatus == DUS_PREPARING);
  4770. g_DynUpdtStatus->DUStatus = DUS_PREPARING_INVALIDURL;
  4771. break;
  4772. case ERROR_INVALID_PARAMETER:
  4773. case ERROR_NOT_ENOUGH_MEMORY:
  4774. case ERROR_OLD_WIN_VERSION:
  4775. case ERROR_OUTOFMEMORY:
  4776. case ERROR_NO_MORE_ITEMS:
  4777. case ERROR_FILE_NOT_FOUND:
  4778. case ERROR_INVALID_DATA:
  4779. case ERROR_UNSUPPORTED_TYPE:
  4780. case ERROR_INVALID_HANDLE:
  4781. pLogStandardError (Error);
  4782. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4783. break;
  4784. case DU_ERROR_MISSING_DLL:
  4785. case DU_NOT_INITIALIZED:
  4786. DynUpdtDebugLog (Winnt32LogError, TEXT("Failure with custom error code %1!u!"), 0, Error);
  4787. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4788. break;
  4789. case ERROR_INTERNET_NO_CONTEXT:
  4790. pLogWininetError (Error);
  4791. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4792. break;
  4793. default:
  4794. if (Error > INTERNET_ERROR_BASE) {
  4795. pLogWininetError (Error);
  4796. } else {
  4797. pLogStandardError (Error);
  4798. }
  4799. }
  4800. }
  4801. DWORD
  4802. WINAPI
  4803. DoDynamicUpdate (
  4804. LPVOID Parameter
  4805. )
  4806. {
  4807. #define MIN_INTERVAL_BETWEEN_TASKS 3000
  4808. HWND hUIWindow = (HWND)Parameter;
  4809. DWORD rc = ERROR_SUCCESS;
  4810. LONG ticks;
  4811. LONG sleep;
  4812. DWORD estTime, estSize;
  4813. TCHAR drive[4];
  4814. DWORD sectorsPerCluster;
  4815. DWORD bytesPerSector;
  4816. ULARGE_INTEGER freeClusters = {0, 0};
  4817. ULARGE_INTEGER totalClusters = {0, 0};
  4818. DWORD clusterSize;
  4819. ULONGLONG availableBytes;
  4820. HANDLE hEvent;
  4821. BOOL bStopSetup;
  4822. BOOL bContinue = TRUE;
  4823. hEvent = OpenEvent (EVENT_ALL_ACCESS, FALSE, S_DU_SYNC_EVENT_NAME);
  4824. if (!hEvent) {
  4825. DynUpdtDebugLog (
  4826. Winnt32LogError,
  4827. TEXT("OpenEvent(%1) failed"),
  4828. 0,
  4829. S_DU_SYNC_EVENT_NAME
  4830. );
  4831. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4832. goto exit;
  4833. }
  4834. while (bContinue) {
  4835. if (g_DynUpdtStatus->Cancelled) {
  4836. g_DynUpdtStatus->DUStatus = DUS_CANCELLED;
  4837. rc = ERROR_CANCELLED;
  4838. DynamicUpdateUninitialize ();
  4839. break;
  4840. }
  4841. switch (g_DynUpdtStatus->DUStatus) {
  4842. case DUS_INITIAL:
  4843. if (Winnt32Restarted () || g_DynUpdtStatus->UserSpecifiedUpdates) {
  4844. g_DynUpdtStatus->DUStatus = DUS_PROCESSING;
  4845. break;
  4846. }
  4847. g_DynUpdtStatus->DUStatus = DUS_PREPARING;
  4848. SendMessage (hUIWindow, WMX_SETUPUPDATE_PREPARING, 0, 0);
  4849. break;
  4850. case DUS_PREPARING:
  4851. ticks = GetTickCount ();
  4852. if (!DynamicUpdateInitDownload (hUIWindow)) {
  4853. DynUpdtDebugLog (
  4854. Winnt32LogError,
  4855. TEXT("DynamicUpdateInitDownload failed"),
  4856. 0
  4857. );
  4858. pUpdateDUStatus (GetLastError ());
  4859. if (g_DynUpdtStatus->DUStatus != DUS_SKIP &&
  4860. g_DynUpdtStatus->DUStatus != DUS_ERROR) {
  4861. //
  4862. // the UI thread will decide what the next state will be
  4863. // based on user's selection
  4864. //
  4865. PostMessage (hUIWindow, WMX_SETUPUPDATE_INIT_RETRY, 0, 0);
  4866. rc = WaitForSingleObject (hEvent, INFINITE);
  4867. if (rc != WAIT_OBJECT_0) {
  4868. DynUpdtDebugLog (
  4869. Winnt32LogError,
  4870. TEXT("WaitForSingleObject failed (%1!u!)"),
  4871. 0,
  4872. rc
  4873. );
  4874. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4875. break;
  4876. }
  4877. }
  4878. break;
  4879. }
  4880. sleep = ticks + MIN_INTERVAL_BETWEEN_TASKS - (LONG)GetTickCount ();
  4881. if (sleep > 0 && sleep <= MIN_INTERVAL_BETWEEN_TASKS) {
  4882. Sleep (sleep);
  4883. }
  4884. g_DynUpdtStatus->DUStatus = DUS_DOWNLOADING;
  4885. break;
  4886. case DUS_DOWNLOADING:
  4887. ticks = GetTickCount ();
  4888. estSize = estTime = 0;
  4889. if (!DynamicUpdateStart (&estTime, &estSize)) {
  4890. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4891. break;
  4892. }
  4893. //
  4894. // check if there is enough disk space available for this operation
  4895. //
  4896. lstrcpyn (drive, g_DynUpdtStatus->WorkingDir, 3);
  4897. if (GetDiskFreeSpaceNew (
  4898. drive,
  4899. &sectorsPerCluster,
  4900. &bytesPerSector,
  4901. &freeClusters,
  4902. &totalClusters
  4903. )) {
  4904. clusterSize = bytesPerSector * sectorsPerCluster;
  4905. availableBytes = (ULONGLONG)clusterSize * freeClusters.QuadPart;
  4906. //
  4907. // assume the average-worst case where each file occupies 1/2 cluster
  4908. // then the space required is the double of estimated space
  4909. //
  4910. if (availableBytes < (ULONGLONG)estSize * 2) {
  4911. DynUpdtDebugLog (
  4912. Winnt32LogError,
  4913. TEXT("DoDynamicUpdate: not enough free space on drive %1 to perform download (available=%2!u! MB, needed=%3!u! MB)"),
  4914. 0,
  4915. drive,
  4916. (DWORD)(availableBytes >> 20),
  4917. (DWORD)(estSize >> 20)
  4918. );
  4919. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4920. DynamicUpdateCancel ();
  4921. //
  4922. // wait for the UI thread to signal the event, no more than about a minute
  4923. //
  4924. rc = WaitForSingleObject (hEvent, 66000);
  4925. if (rc == WAIT_TIMEOUT) {
  4926. //
  4927. // why?
  4928. //
  4929. MYASSERT (FALSE);
  4930. } else if (rc != WAIT_OBJECT_0) {
  4931. DynUpdtDebugLog (
  4932. Winnt32LogError,
  4933. TEXT("WaitForSingleObject failed (%1!u!)"),
  4934. 0,
  4935. rc
  4936. );
  4937. }
  4938. break;
  4939. }
  4940. }
  4941. SendMessage (hUIWindow, WMX_SETUPUPDATE_DOWNLOADING, estTime, estSize);
  4942. rc = WaitForSingleObject (hEvent, INFINITE);
  4943. if (rc != WAIT_OBJECT_0) {
  4944. DynUpdtDebugLog (
  4945. Winnt32LogError,
  4946. TEXT("WaitForSingleObject failed (%1!u!)"),
  4947. 0,
  4948. rc
  4949. );
  4950. g_DynUpdtStatus->DUStatus = DUS_ERROR;
  4951. break;
  4952. }
  4953. sleep = ticks + MIN_INTERVAL_BETWEEN_TASKS - (LONG)GetTickCount ();
  4954. if (sleep > 0 && sleep <= MIN_INTERVAL_BETWEEN_TASKS) {
  4955. Sleep (sleep);
  4956. }
  4957. //
  4958. // the UI thread has already set the next state,
  4959. // based on the result of download
  4960. //
  4961. break;
  4962. case DUS_PROCESSING:
  4963. ticks = GetTickCount ();
  4964. SendMessage (hUIWindow, WMX_SETUPUPDATE_PROCESSING, 0, 0);
  4965. if (!g_DynUpdtStatus->UserSpecifiedUpdates) {
  4966. lstrcpy (g_DynUpdtStatus->DynamicUpdatesSource, g_DynUpdtStatus->WorkingDir);
  4967. }
  4968. bStopSetup = FALSE;
  4969. if (!DynamicUpdateProcessFiles (&bStopSetup)) {
  4970. g_DynUpdtStatus->DUStatus = bStopSetup ? DUS_FATALERROR : DUS_ERROR;
  4971. break;
  4972. }
  4973. sleep = ticks + MIN_INTERVAL_BETWEEN_TASKS - (LONG)GetTickCount ();
  4974. if (sleep > 0 && sleep <= MIN_INTERVAL_BETWEEN_TASKS) {
  4975. Sleep (sleep);
  4976. }
  4977. g_DynUpdtStatus->DUStatus = DUS_SUCCESSFUL;
  4978. break;
  4979. case DUS_SUCCESSFUL:
  4980. if (CheckUpgradeOnly && !g_DynUpdtStatus->RestartWinnt32 && !g_DynUpdtStatus->UserSpecifiedUpdates) {
  4981. if (pSaveLastDownloadInfo ()) {
  4982. g_DynUpdtStatus->PreserveWorkingDir = TRUE;
  4983. }
  4984. }
  4985. //
  4986. // fall through
  4987. //
  4988. case DUS_ERROR:
  4989. case DUS_FATALERROR:
  4990. case DUS_SKIP:
  4991. //
  4992. // always make sure to uninitialize DU
  4993. // if the user had a modem connection active, this should close
  4994. // the connection
  4995. // DynamicUpdateUninitialize () will not reset any DU data
  4996. // in case the processing was successful
  4997. //
  4998. DynamicUpdateUninitialize ();
  4999. bContinue = FALSE;
  5000. break;
  5001. default:
  5002. MYASSERT (FALSE);
  5003. }
  5004. }
  5005. CloseHandle (hEvent);
  5006. exit:
  5007. //
  5008. // always notify the UI thread before exiting
  5009. //
  5010. PostMessage (hUIWindow, WMX_SETUPUPDATE_THREAD_DONE, 0, 0);
  5011. return rc;
  5012. }