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.

451 lines
14 KiB

  1. /*++
  2. Copyright (c) 1994 - 1995 Microsoft Corporation
  3. Module Name:
  4. upgrade2.c
  5. Abstract:
  6. The routines in this file are Registry Mungers for the Upgrade case of
  7. NT 3.1 to NT 3.5.
  8. In NT 3.1 we didn't have registry entries Version-0 Version-1 etc. nor directories.
  9. Also Setup copies the drivers to both \0 and \1 directories
  10. so we now have to generate Version-0 and Version-1 structures in the
  11. registry and validate that the drivers in \1 really are version 1
  12. and those in \0 are Version-0.
  13. These routines only operate on the Environment that matches the CPU
  14. that we are running on. This is because Setup doesn't upgrade drivers
  15. for other platforms during upgrade.
  16. Code for "Upgrading" the other CPU environments is found in upgrade.c
  17. Author:
  18. Krishna Ganugapati (KrishnaG) 21-Apr-1994
  19. Revision History:
  20. --*/
  21. #include <precomp.h>
  22. #define DAYTONA_VERSION 1
  23. #define PRODUCT1_VERSION 0
  24. DWORD
  25. GetDriverMajorVersion(
  26. LPWSTR pFileName
  27. );
  28. DWORD
  29. UpgradeValidateDriverFilesinVersionDirectory(
  30. LPWSTR pEnvironmentDirectory,
  31. DWORD dwVersion,
  32. LPWSTR pDriverFile,
  33. LPWSTR pConfigFile,
  34. LPWSTR pDataFile
  35. );
  36. BOOL
  37. UpgradeWriteDriverIni(
  38. PINIDRIVER pIniDriver,
  39. DWORD dwVersion,
  40. LPWSTR pszEnvironmentName,
  41. PINISPOOLER pIniSpooler
  42. );
  43. extern WCHAR *szSpoolDirectory;
  44. extern WCHAR *szDirectory;
  45. extern DWORD dwUpgradeFlag;
  46. VOID
  47. UpgradeOurDriver(
  48. HKEY hEnvironmentsRootKey,
  49. LPWSTR pszEnvironmentName,
  50. PINISPOOLER pIniSpooler
  51. )
  52. /*++
  53. Routine Description:
  54. This function upgrades drivers for our spooler's environment. It expects driver
  55. files to have been migrated by setup to the 1 and to the 0 directories. We
  56. will scan the registry, check if there are any driver entries around and
  57. migrate each entry.
  58. Arguments:
  59. hEnvironmentsRootKey this is a handle to Print\Environments opened with
  60. KEY_ALL_ACCESS
  61. pszEnvironmentName this is the string describing our environment
  62. Return Value:
  63. VOID
  64. --*/
  65. {
  66. HKEY hEnvironmentKey;
  67. HKEY hDriversKey;
  68. WCHAR VersionName[MAX_PATH];
  69. DWORD cbBuffer;
  70. DWORD cVersion;
  71. PINIDRIVER pIniDriver;
  72. WCHAR szEnvironmentScratchDirectory[MAX_PATH];
  73. DWORD Type;
  74. DWORD Level = 2;
  75. DWORD dwVersion = 0;
  76. if ( RegOpenKeyEx( hEnvironmentsRootKey, pszEnvironmentName, 0,
  77. KEY_ALL_ACCESS, &hEnvironmentKey) != ERROR_SUCCESS) {
  78. DBGMSG(DBG_WARNING, ("UpgradeOurDrivers Could not open %ws key\n", pszEnvironmentName ));
  79. RegCloseKey(hEnvironmentsRootKey);
  80. return;
  81. }
  82. cbBuffer = sizeof( szEnvironmentScratchDirectory );
  83. if ( RegQueryValueEx( hEnvironmentKey, L"Directory",
  84. NULL, NULL, (LPBYTE)szEnvironmentScratchDirectory,
  85. &cbBuffer) != ERROR_SUCCESS) {
  86. DBGMSG(DBG_TRACE, ("RegQueryValueEx -- Error %d\n", GetLastError()));
  87. }
  88. if ( RegOpenKeyEx( hEnvironmentKey, szDriversKey, 0,
  89. KEY_ALL_ACCESS, &hDriversKey) != ERROR_SUCCESS) {
  90. DBGMSG(DBG_TRACE, ("Could not open %ws key\n", szDriversKey));
  91. RegCloseKey( hEnvironmentKey );
  92. RegCloseKey( hEnvironmentsRootKey );
  93. return;
  94. }
  95. cVersion = 0;
  96. memset( VersionName, 0, sizeof(WCHAR)*MAX_PATH );
  97. cbBuffer = COUNTOF( VersionName );
  98. while ( RegEnumKeyEx( hDriversKey, cVersion, VersionName,
  99. &cbBuffer, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
  100. DBGMSG(DBG_TRACE, ("Name of the sub-key is %ws\n", VersionName));
  101. if ( !_wcsnicmp( VersionName, L"Version-", 8 )) {
  102. cVersion++;
  103. memset( VersionName, 0, sizeof(WCHAR)*MAX_PATH );
  104. cbBuffer = COUNTOF( VersionName );
  105. continue;
  106. }
  107. DBGMSG(DBG_TRACE,("Older Driver Version Found", VersionName));
  108. if ( !(pIniDriver = GetDriver( hDriversKey, VersionName, pIniSpooler ))) {
  109. RegDeleteKey( hDriversKey, VersionName );
  110. cVersion = 0;
  111. memset(VersionName, 0, sizeof(WCHAR)*MAX_PATH);
  112. cbBuffer = COUNTOF( VersionName );
  113. continue;
  114. }
  115. //
  116. // Validate that the file is in the 1 directory and in the 0
  117. // directory.
  118. //
  119. if (UpgradeValidateDriverFilesinVersionDirectory(
  120. szEnvironmentScratchDirectory,
  121. (DWORD)DAYTONA_VERSION,
  122. pIniDriver->pDriverFile,
  123. pIniDriver->pConfigFile,
  124. pIniDriver->pDataFile) == DAYTONA_VERSION){
  125. UpgradeWriteDriverIni(pIniDriver, DAYTONA_VERSION, pszEnvironmentName, pIniSpooler);
  126. }
  127. if (UpgradeValidateDriverFilesinVersionDirectory(
  128. szEnvironmentScratchDirectory,
  129. (DWORD)PRODUCT1_VERSION,
  130. pIniDriver->pDriverFile,
  131. pIniDriver->pConfigFile,
  132. pIniDriver->pDataFile) == PRODUCT1_VERSION){
  133. UpgradeWriteDriverIni(pIniDriver, PRODUCT1_VERSION, pszEnvironmentName, pIniSpooler);
  134. }
  135. RegDeleteKey( hDriversKey, VersionName );
  136. cVersion = 0;
  137. memset( VersionName, 0, sizeof(WCHAR)*MAX_PATH );
  138. cbBuffer = COUNTOF( VersionName );
  139. FreeSplStr( pIniDriver->pName );
  140. FreeSplStr( pIniDriver->pDriverFile );
  141. FreeSplStr( pIniDriver->pConfigFile );
  142. FreeSplStr( pIniDriver->pDataFile );
  143. FreeSplMem( pIniDriver );
  144. }
  145. RegCloseKey( hDriversKey );
  146. RegCloseKey( hEnvironmentKey );
  147. }
  148. VOID
  149. Upgrade31DriversRegistryForThisEnvironment(
  150. LPWSTR szEnvironment,
  151. PINISPOOLER pIniSpooler
  152. )
  153. {
  154. HKEY hEnvironmentsRootKey;
  155. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryEnvironments, 0,
  156. KEY_ALL_ACCESS, &hEnvironmentsRootKey) != ERROR_SUCCESS) {
  157. DBGMSG(DBG_TRACE, ("Could not open %ws key\n", pIniSpooler->pszRegistryEnvironments));
  158. return;
  159. }
  160. UpgradeOurDriver( hEnvironmentsRootKey, szEnvironment, pIniSpooler );
  161. RegCloseKey( hEnvironmentsRootKey );
  162. }
  163. DWORD
  164. UpgradeGetEnvironmentDriverDirectory(
  165. LPWSTR pDir,
  166. LPWSTR pEnvironmentDirectory
  167. )
  168. {
  169. DWORD i=0;
  170. LPWSTR psz;
  171. WCHAR Buffer[MAX_PATH];
  172. GetSystemDirectory(Buffer, sizeof(Buffer));
  173. wcscat(Buffer, szSpoolDirectory);
  174. psz = Buffer;
  175. while (pDir[i++] = *psz++)
  176. ;
  177. pDir[i-1] = L'\\';
  178. psz = szDriverDir;
  179. while (pDir[i++] = *psz++)
  180. ;
  181. pDir[i-1] = L'\\';
  182. psz = pEnvironmentDirectory;
  183. while (pDir[i++]=*psz++)
  184. ;
  185. return i-1;
  186. }
  187. DWORD
  188. UpgradeValidateDriverFilesinVersionDirectory(
  189. LPWSTR pEnvironmentDirectory,
  190. DWORD dwVersion,
  191. LPWSTR pDriverFile,
  192. LPWSTR pConfigFile,
  193. LPWSTR pDataFile
  194. )
  195. {
  196. WCHAR szDriverBuffer[MAX_PATH];
  197. WCHAR szTempBuffer[MAX_PATH];
  198. WCHAR pDir[MAX_PATH];
  199. DBGMSG(DBG_TRACE, ("UpgradeValidateDriverFilesinVersionDirectory\n"));
  200. DBGMSG(DBG_TRACE, ("\tpEnvironmentDirectory: %ws\n", pEnvironmentDirectory));
  201. DBGMSG(DBG_TRACE, ("\tdwVersion: %d\n", dwVersion));
  202. DBGMSG(DBG_TRACE, ("\tpDriverFile: %ws\n", pDriverFile));
  203. DBGMSG(DBG_TRACE, ("\tpConfigFile: %ws\n", pConfigFile));
  204. DBGMSG(DBG_TRACE, ("\tpDataFile: %ws\n", pDataFile));
  205. if (!pDriverFile || !pConfigFile || !pDataFile) {
  206. return((DWORD)-1);
  207. }
  208. UpgradeGetEnvironmentDriverDirectory(pDir,pEnvironmentDirectory);
  209. wsprintf(szDriverBuffer, L"%ws\\%d\\%ws",pDir, dwVersion, pDriverFile);
  210. DBGMSG( DBG_TRACE,("Driver File is %ws\n", szDriverBuffer));
  211. if ( !FileExists( szDriverBuffer )) {
  212. return((DWORD)-1);
  213. }
  214. wsprintf(szTempBuffer, L"%ws\\%d\\%ws", pDir, dwVersion, pConfigFile);
  215. DBGMSG(DBG_TRACE,("Configuration File is %ws\n", szTempBuffer));
  216. if ( !FileExists( szTempBuffer )) {
  217. return((DWORD)-1);
  218. }
  219. wsprintf(szTempBuffer, L"%ws\\%d\\%ws", pDir, dwVersion, pDataFile);
  220. DBGMSG(DBG_TRACE,("Data File is %ws\n", szTempBuffer));
  221. if ( !FileExists( szTempBuffer )) {
  222. return((DWORD)-1);
  223. }
  224. //
  225. // Fixed with right version
  226. //
  227. return (GetDriverMajorVersion(szDriverBuffer));
  228. }
  229. BOOL
  230. UpgradeWriteDriverIni(
  231. PINIDRIVER pIniDriver,
  232. DWORD dwVersion,
  233. LPWSTR pszEnvironmentName,
  234. PINISPOOLER pIniSpooler
  235. )
  236. {
  237. HKEY hEnvironmentsRootKey, hEnvironmentKey, hDriversKey, hDriverKey;
  238. HKEY hVersionKey;
  239. WCHAR szVersionName[MAX_PATH];
  240. DWORD dwMinorVersion = 0;
  241. WCHAR szVersionDirectory[MAX_PATH];
  242. memset(szVersionDirectory, 0, sizeof(WCHAR)*MAX_PATH);
  243. wsprintf(szVersionDirectory,L"%d", dwVersion);
  244. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryEnvironments, 0,
  245. NULL, 0, KEY_WRITE, NULL, &hEnvironmentsRootKey, NULL)
  246. == ERROR_SUCCESS) {
  247. DBGMSG(DBG_TRACE,("Created key %ws\n", pIniSpooler->pszRegistryEnvironments));
  248. if (RegCreateKeyEx(hEnvironmentsRootKey, pszEnvironmentName, 0,
  249. NULL, 0, KEY_WRITE, NULL, &hEnvironmentKey, NULL)
  250. == ERROR_SUCCESS) {
  251. DBGMSG(DBG_TRACE, ("Created key %ws\n", pszEnvironmentName));
  252. if (RegCreateKeyEx(hEnvironmentKey, szDriversKey, 0,
  253. NULL, 0, KEY_WRITE, NULL, &hDriversKey, NULL)
  254. == ERROR_SUCCESS) {
  255. DBGMSG(DBG_TRACE, ("Created key %ws\n", szDriversKey));
  256. wsprintf(szVersionName,L"Version-%d",dwVersion);
  257. DBGMSG(DBG_TRACE, ("Trying to create version key %ws\n", szVersionName));
  258. if (RegCreateKeyEx(hDriversKey, szVersionName, 0, NULL,
  259. 0, KEY_WRITE, NULL, &hVersionKey, NULL)
  260. == ERROR_SUCCESS) {
  261. DBGMSG(DBG_TRACE, ("Created key %ws\n", szVersionName));
  262. RegSetValueEx(hVersionKey, szDirectory, 0, REG_SZ,
  263. (LPBYTE)szVersionDirectory,
  264. wcslen(szVersionDirectory)*sizeof(WCHAR) +
  265. sizeof(WCHAR));
  266. RegSetValueEx(hVersionKey, szMajorVersion, 0, REG_DWORD,
  267. (LPBYTE)&dwVersion,
  268. sizeof(DWORD));
  269. RegSetValueEx(hVersionKey, szMinorVersion, 0, REG_DWORD,
  270. (LPBYTE)&dwMinorVersion,
  271. sizeof(DWORD));
  272. if (RegCreateKeyEx(hVersionKey, pIniDriver->pName, 0, NULL,
  273. 0, KEY_WRITE, NULL, &hDriverKey, NULL)
  274. == ERROR_SUCCESS) {
  275. DBGMSG(DBG_TRACE,("Created key %ws\n", pIniDriver->pName));
  276. RegSetValueEx(hDriverKey, szConfigurationKey, 0, REG_SZ,
  277. (LPBYTE)pIniDriver->pConfigFile,
  278. wcslen(pIniDriver->pConfigFile)*sizeof(WCHAR) +
  279. sizeof(WCHAR));
  280. RegSetValueEx(hDriverKey, szDataFileKey, 0, REG_SZ,
  281. (LPBYTE)pIniDriver->pDataFile,
  282. wcslen(pIniDriver->pDataFile)*sizeof(WCHAR) +
  283. sizeof(WCHAR));
  284. RegSetValueEx(hDriverKey, szDriverFile, 0, REG_SZ,
  285. (LPBYTE)pIniDriver->pDriverFile,
  286. wcslen(pIniDriver->pDriverFile)*sizeof(WCHAR) +
  287. sizeof(WCHAR));
  288. RegSetValueEx(hDriverKey, szDriverVersion, 0, REG_DWORD,
  289. (LPBYTE)&dwVersion,
  290. sizeof(pIniDriver->cVersion));
  291. RegCloseKey(hDriverKey);
  292. }
  293. RegCloseKey(hVersionKey);
  294. }
  295. RegCloseKey(hDriversKey);
  296. }
  297. RegCloseKey(hEnvironmentKey);
  298. }
  299. RegCloseKey(hEnvironmentsRootKey);
  300. }
  301. return TRUE;
  302. }
  303. VOID
  304. QueryUpgradeFlag(
  305. PINISPOOLER pIniSpooler
  306. )
  307. /*++
  308. Description: the query update flag is set up by TedM. We will read this flag
  309. if the flag has been set, we will set a boolean variable saying that we're in
  310. the upgrade mode. All upgrade activities will be carried out based on this flag.
  311. For subsequents startups of the spooler, this flag will be unvailable so we
  312. won't run the spooler in upgrade mode.
  313. --*/
  314. {
  315. DWORD dwRet;
  316. DWORD cbData;
  317. DWORD dwType = 0;
  318. HKEY hKey;
  319. dwUpgradeFlag = 0;
  320. dwRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryRoot, 0, KEY_ALL_ACCESS, &hKey);
  321. if (dwRet != ERROR_SUCCESS) {
  322. DBGMSG(DBG_TRACE, ("The Spooler Upgrade flag is %d\n", dwUpgradeFlag));
  323. return;
  324. }
  325. cbData = sizeof(DWORD);
  326. dwRet = RegQueryValueEx(hKey, L"Upgrade", NULL, &dwType, (LPBYTE)&dwUpgradeFlag, &cbData);
  327. if (dwRet != ERROR_SUCCESS) {
  328. dwUpgradeFlag = 0;
  329. }
  330. dwRet = RegDeleteValue(hKey, L"Upgrade");
  331. if (dwRet != ERROR_SUCCESS) {
  332. DBGMSG(DBG_TRACE, ("QueryUpgradeFlag: failed to delete the Upgrade Value\n"));
  333. }
  334. RegCloseKey(hKey);
  335. DBGMSG(DBG_TRACE, ("The Spooler Upgrade flag is %d\n", dwUpgradeFlag));
  336. return;
  337. }