wineboot.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /*
  2. * Copyright (C) 2002 Andreas Mohr
  3. * Copyright (C) 2002 Shachar Shemesh
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. /* Wine "bootup" handler application
  20. *
  21. * This app handles the various "hooks" windows allows for applications to perform
  22. * as part of the bootstrap process. These are roughly divided into three types.
  23. * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
  24. * Also, 119941 has some info on grpconv.exe
  25. * The operations performed are (by order of execution):
  26. *
  27. * Preboot (prior to fully loading the Windows kernel):
  28. * - wininit.exe (rename operations left in wininit.ini - Win 9x only)
  29. * - PendingRenameOperations (rename operations left in the registry - Win NT+ only)
  30. *
  31. * Startup (before the user logs in)
  32. * - Services (NT)
  33. * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
  34. * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
  35. *
  36. * After log in
  37. * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
  38. * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
  39. * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
  40. * - Startup folders (all, ?asynch?)
  41. * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
  42. *
  43. * Somewhere in there is processing the RunOnceEx entries (also no imp)
  44. *
  45. * Bugs:
  46. * - If a pending rename registry does not start with \??\ the entry is
  47. * processed anyways. I'm not sure that is the Windows behaviour.
  48. * - Need to check what is the windows behaviour when trying to delete files
  49. * and directories that are read-only
  50. * - In the pending rename registry processing - there are no traces of the files
  51. * processed (requires translations from Unicode to Ansi).
  52. */
  53. #define COBJMACROS
  54. #include <errno.h>
  55. #include <fcntl.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <intrin.h>
  59. #include <sys/stat.h>
  60. #include <unistd.h>
  61. #include <ntstatus.h>
  62. #define WIN32_NO_STATUS
  63. #include <windows.h>
  64. #include <ws2tcpip.h>
  65. #include <winternl.h>
  66. #include <ddk/wdm.h>
  67. #include <sddl.h>
  68. #include <wine/svcctl.h>
  69. #include <wine/asm.h>
  70. #include <wine/debug.h>
  71. #include <shlobj.h>
  72. #include <shobjidl.h>
  73. #include <shlwapi.h>
  74. #include <shellapi.h>
  75. #include <setupapi.h>
  76. #include <newdev.h>
  77. #include "resource.h"
  78. WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
  79. extern BOOL shutdown_close_windows( BOOL force );
  80. extern BOOL shutdown_all_desktops( BOOL force );
  81. extern void kill_processes( BOOL kill_desktop );
  82. static WCHAR windowsdir[MAX_PATH];
  83. static const BOOL is_64bit = sizeof(void *) > sizeof(int);
  84. /* retrieve the path to the wine.inf file */
  85. static WCHAR *get_wine_inf_path(void)
  86. {
  87. WCHAR *dir, *name = NULL;
  88. if ((dir = _wgetenv( L"WINEBUILDDIR" )))
  89. {
  90. if (!(name = HeapAlloc( GetProcessHeap(), 0,
  91. sizeof(L"\\loader\\wine.inf") + lstrlenW(dir) * sizeof(WCHAR) )))
  92. return NULL;
  93. lstrcpyW( name, dir );
  94. lstrcatW( name, L"\\loader" );
  95. }
  96. else if ((dir = _wgetenv( L"WINEDATADIR" )))
  97. {
  98. if (!(name = HeapAlloc( GetProcessHeap(), 0, sizeof(L"\\wine.inf") + lstrlenW(dir) * sizeof(WCHAR) )))
  99. return NULL;
  100. lstrcpyW( name, dir );
  101. }
  102. else return NULL;
  103. lstrcatW( name, L"\\wine.inf" );
  104. name[1] = '\\'; /* change \??\ to \\?\ */
  105. return name;
  106. }
  107. /* update the timestamp if different from the reference time */
  108. static BOOL update_timestamp( const WCHAR *config_dir, unsigned long timestamp )
  109. {
  110. BOOL ret = FALSE;
  111. int fd, count;
  112. char buffer[100];
  113. WCHAR *file = HeapAlloc( GetProcessHeap(), 0, lstrlenW(config_dir) * sizeof(WCHAR) + sizeof(L"\\.update-timestamp") );
  114. if (!file) return FALSE;
  115. lstrcpyW( file, config_dir );
  116. lstrcatW( file, L"\\.update-timestamp" );
  117. if ((fd = _wopen( file, O_RDWR )) != -1)
  118. {
  119. if ((count = read( fd, buffer, sizeof(buffer) - 1 )) >= 0)
  120. {
  121. buffer[count] = 0;
  122. if (!strncmp( buffer, "disable", sizeof("disable")-1 )) goto done;
  123. if (timestamp == strtoul( buffer, NULL, 10 )) goto done;
  124. }
  125. lseek( fd, 0, SEEK_SET );
  126. chsize( fd, 0 );
  127. }
  128. else
  129. {
  130. if (errno != ENOENT) goto done;
  131. if ((fd = _wopen( file, O_WRONLY | O_CREAT | O_TRUNC, 0666 )) == -1) goto done;
  132. }
  133. count = sprintf( buffer, "%lu\n", timestamp );
  134. if (write( fd, buffer, count ) != count)
  135. {
  136. WINE_WARN( "failed to update timestamp in %s\n", debugstr_w(file) );
  137. chsize( fd, 0 );
  138. }
  139. else ret = TRUE;
  140. done:
  141. if (fd != -1) close( fd );
  142. HeapFree( GetProcessHeap(), 0, file );
  143. return ret;
  144. }
  145. /* print the config directory in a more Unix-ish way */
  146. static const WCHAR *prettyprint_configdir(void)
  147. {
  148. static WCHAR buffer[MAX_PATH];
  149. WCHAR *p, *path = _wgetenv( L"WINECONFIGDIR" );
  150. lstrcpynW( buffer, path, ARRAY_SIZE(buffer) );
  151. if (lstrlenW( path ) >= ARRAY_SIZE(buffer) )
  152. lstrcpyW( buffer + ARRAY_SIZE(buffer) - 4, L"..." );
  153. if (!wcsncmp( buffer, L"\\??\\unix\\", 9 ))
  154. {
  155. for (p = buffer + 9; *p; p++) if (*p == '\\') *p = '/';
  156. return buffer + 9;
  157. }
  158. else if (!wcsncmp( buffer, L"\\??\\Z:\\", 7 ))
  159. {
  160. for (p = buffer + 6; *p; p++) if (*p == '\\') *p = '/';
  161. return buffer + 6;
  162. }
  163. else return buffer + 4;
  164. }
  165. /* wrapper for RegSetValueExW */
  166. static DWORD set_reg_value( HKEY hkey, const WCHAR *name, const WCHAR *value )
  167. {
  168. return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, (lstrlenW(value) + 1) * sizeof(WCHAR) );
  169. }
  170. static DWORD set_reg_value_dword( HKEY hkey, const WCHAR *name, DWORD value )
  171. {
  172. return RegSetValueExW( hkey, name, 0, REG_DWORD, (const BYTE *)&value, sizeof(value) );
  173. }
  174. #if defined(__i386__) || defined(__x86_64__)
  175. static void initialize_xstate_features(struct _KUSER_SHARED_DATA *data)
  176. {
  177. XSTATE_CONFIGURATION *xstate = &data->XState;
  178. unsigned int i;
  179. int regs[4];
  180. if (!data->ProcessorFeatures[PF_AVX_INSTRUCTIONS_AVAILABLE])
  181. return;
  182. __cpuidex(regs, 0, 0);
  183. TRACE("Max cpuid level %#x.\n", regs[0]);
  184. if (regs[0] < 0xd)
  185. return;
  186. __cpuidex(regs, 1, 0);
  187. TRACE("CPU features %#x, %#x, %#x, %#x.\n", regs[0], regs[1], regs[2], regs[3]);
  188. if (!(regs[2] & (0x1 << 27))) /* xsave OS enabled */
  189. return;
  190. __cpuidex(regs, 0xd, 0);
  191. TRACE("XSAVE details %#x, %#x, %#x, %#x.\n", regs[0], regs[1], regs[2], regs[3]);
  192. if (!(regs[0] & XSTATE_AVX))
  193. return;
  194. xstate->EnabledFeatures = (1 << XSTATE_LEGACY_FLOATING_POINT) | (1 << XSTATE_LEGACY_SSE) | (1 << XSTATE_AVX);
  195. xstate->EnabledVolatileFeatures = xstate->EnabledFeatures;
  196. xstate->Size = sizeof(XSAVE_FORMAT) + sizeof(XSTATE);
  197. xstate->AllFeatureSize = regs[1];
  198. xstate->AllFeatures[0] = offsetof(XSAVE_FORMAT, XmmRegisters);
  199. xstate->AllFeatures[1] = sizeof(M128A) * 16;
  200. xstate->AllFeatures[2] = sizeof(YMMCONTEXT);
  201. for (i = 0; i < 3; ++i)
  202. xstate->Features[i].Size = xstate->AllFeatures[i];
  203. xstate->Features[1].Offset = xstate->Features[0].Size;
  204. xstate->Features[2].Offset = sizeof(XSAVE_FORMAT) + offsetof(XSTATE, YmmContext);
  205. __cpuidex(regs, 0xd, 1);
  206. xstate->OptimizedSave = regs[0] & 1;
  207. xstate->CompactionEnabled = !!(regs[0] & 2);
  208. __cpuidex(regs, 0xd, 2);
  209. TRACE("XSAVE feature 2 %#x, %#x, %#x, %#x.\n", regs[0], regs[1], regs[2], regs[3]);
  210. }
  211. #else
  212. static void initialize_xstate_features(struct _KUSER_SHARED_DATA *data)
  213. {
  214. }
  215. #endif
  216. static void create_user_shared_data(void)
  217. {
  218. struct _KUSER_SHARED_DATA *data;
  219. RTL_OSVERSIONINFOEXW version;
  220. SYSTEM_CPU_INFORMATION sci;
  221. SYSTEM_BASIC_INFORMATION sbi;
  222. BOOLEAN *features;
  223. OBJECT_ATTRIBUTES attr = {sizeof(attr)};
  224. UNICODE_STRING name;
  225. NTSTATUS status;
  226. HANDLE handle;
  227. RtlInitUnicodeString( &name, L"\\KernelObjects\\__wine_user_shared_data" );
  228. InitializeObjectAttributes( &attr, &name, OBJ_OPENIF, NULL, NULL );
  229. if ((status = NtOpenSection( &handle, SECTION_ALL_ACCESS, &attr )))
  230. {
  231. ERR( "cannot open __wine_user_shared_data: %x\n", status );
  232. return;
  233. }
  234. data = MapViewOfFile( handle, FILE_MAP_WRITE, 0, 0, sizeof(*data) );
  235. CloseHandle( handle );
  236. if (!data)
  237. {
  238. ERR( "cannot map __wine_user_shared_data\n" );
  239. return;
  240. }
  241. version.dwOSVersionInfoSize = sizeof(version);
  242. RtlGetVersion( &version );
  243. NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), NULL );
  244. NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
  245. data->TickCountMultiplier = 1 << 24;
  246. data->LargePageMinimum = 2 * 1024 * 1024;
  247. data->NtBuildNumber = version.dwBuildNumber;
  248. data->NtProductType = version.wProductType;
  249. data->ProductTypeIsValid = TRUE;
  250. data->NativeProcessorArchitecture = sci.ProcessorArchitecture;
  251. data->NtMajorVersion = version.dwMajorVersion;
  252. data->NtMinorVersion = version.dwMinorVersion;
  253. data->SuiteMask = version.wSuiteMask;
  254. data->NumberOfPhysicalPages = sbi.MmNumberOfPhysicalPages;
  255. data->NXSupportPolicy = NX_SUPPORT_POLICY_OPTIN;
  256. wcscpy( data->NtSystemRoot, L"C:\\windows" );
  257. features = data->ProcessorFeatures;
  258. switch (sci.ProcessorArchitecture)
  259. {
  260. case PROCESSOR_ARCHITECTURE_INTEL:
  261. case PROCESSOR_ARCHITECTURE_AMD64:
  262. features[PF_COMPARE_EXCHANGE_DOUBLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_CX8);
  263. features[PF_MMX_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_MMX);
  264. features[PF_XMMI_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSE);
  265. features[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_3DNOW);
  266. features[PF_RDTSC_INSTRUCTION_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_TSC);
  267. features[PF_PAE_ENABLED] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_PAE);
  268. features[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSE2);
  269. features[PF_SSE3_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSE3);
  270. features[PF_SSSE3_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSSE3);
  271. features[PF_XSAVE_ENABLED] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_XSAVE);
  272. features[PF_COMPARE_EXCHANGE128] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_CX128);
  273. features[PF_SSE_DAZ_MODE_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_DAZ);
  274. features[PF_NX_ENABLED] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_NX);
  275. features[PF_SECOND_LEVEL_ADDRESS_TRANSLATION] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_2NDLEV);
  276. features[PF_VIRT_FIRMWARE_ENABLED] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_VIRT);
  277. features[PF_RDWRFSGSBASE_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_RDFS);
  278. features[PF_FASTFAIL_AVAILABLE] = TRUE;
  279. features[PF_SSE4_1_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSE41);
  280. features[PF_SSE4_2_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_SSE42);
  281. features[PF_AVX_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_AVX);
  282. features[PF_AVX2_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_AVX2);
  283. break;
  284. case PROCESSOR_ARCHITECTURE_ARM:
  285. features[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_ARM_VFP_32);
  286. features[PF_ARM_NEON_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_ARM_NEON);
  287. features[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = (sci.ProcessorLevel >= 8);
  288. break;
  289. case PROCESSOR_ARCHITECTURE_ARM64:
  290. features[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = TRUE;
  291. features[PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_ARM_V8_CRC32);
  292. features[PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE] = !!(sci.ProcessorFeatureBits & CPU_FEATURE_ARM_V8_CRYPTO);
  293. break;
  294. }
  295. data->ActiveProcessorCount = NtCurrentTeb()->Peb->NumberOfProcessors;
  296. data->ActiveGroupCount = 1;
  297. initialize_xstate_features( data );
  298. UnmapViewOfFile( data );
  299. }
  300. #if defined(__i386__) || defined(__x86_64__)
  301. static void regs_to_str( int *regs, unsigned int len, WCHAR *buffer )
  302. {
  303. unsigned int i;
  304. unsigned char *p = (unsigned char *)regs;
  305. for (i = 0; i < len; i++) { buffer[i] = *p++; }
  306. buffer[i] = 0;
  307. }
  308. static unsigned int get_model( unsigned int reg0, unsigned int *stepping, unsigned int *family )
  309. {
  310. unsigned int model, family_id = (reg0 & (0x0f << 8)) >> 8;
  311. model = (reg0 & (0x0f << 4)) >> 4;
  312. if (family_id == 6 || family_id == 15) model |= (reg0 & (0x0f << 16)) >> 12;
  313. *family = family_id;
  314. if (family_id == 15) *family += (reg0 & (0xff << 20)) >> 20;
  315. *stepping = reg0 & 0x0f;
  316. return model;
  317. }
  318. static void get_identifier( WCHAR *buf, size_t size, const WCHAR *arch )
  319. {
  320. unsigned int family, model, stepping;
  321. int regs[4] = {0, 0, 0, 0};
  322. __cpuid( regs, 1 );
  323. model = get_model( regs[0], &stepping, &family );
  324. swprintf( buf, size, L"%s Family %u Model %u Stepping %u", arch, family, model, stepping );
  325. }
  326. static void get_vendorid( WCHAR *buf )
  327. {
  328. int tmp, regs[4] = {0, 0, 0, 0};
  329. __cpuid( regs, 0 );
  330. tmp = regs[2]; /* swap edx and ecx */
  331. regs[2] = regs[3];
  332. regs[3] = tmp;
  333. regs_to_str( regs + 1, 12, buf );
  334. }
  335. static void get_namestring( WCHAR *buf )
  336. {
  337. int regs[4] = {0, 0, 0, 0};
  338. int i;
  339. __cpuid( regs, 0x80000000 );
  340. if (regs[0] >= 0x80000004)
  341. {
  342. __cpuid( regs, 0x80000002 );
  343. regs_to_str( regs, 16, buf );
  344. __cpuid( regs, 0x80000003 );
  345. regs_to_str( regs, 16, buf + 16 );
  346. __cpuid( regs, 0x80000004 );
  347. regs_to_str( regs, 16, buf + 32 );
  348. }
  349. for (i = lstrlenW(buf) - 1; i >= 0 && buf[i] == ' '; i--) buf[i] = 0;
  350. }
  351. #else /* __i386__ || __x86_64__ */
  352. static void get_identifier( WCHAR *buf, size_t size, const WCHAR *arch ) { }
  353. static void get_vendorid( WCHAR *buf ) { }
  354. static void get_namestring( WCHAR *buf ) { }
  355. #endif /* __i386__ || __x86_64__ */
  356. #include "pshpack1.h"
  357. struct smbios_prologue
  358. {
  359. BYTE calling_method;
  360. BYTE major_version;
  361. BYTE minor_version;
  362. BYTE revision;
  363. DWORD length;
  364. };
  365. enum smbios_type
  366. {
  367. SMBIOS_TYPE_BIOS,
  368. SMBIOS_TYPE_SYSTEM,
  369. SMBIOS_TYPE_BASEBOARD,
  370. };
  371. struct smbios_header
  372. {
  373. BYTE type;
  374. BYTE length;
  375. WORD handle;
  376. };
  377. struct smbios_baseboard
  378. {
  379. struct smbios_header hdr;
  380. BYTE vendor;
  381. BYTE product;
  382. BYTE version;
  383. BYTE serial;
  384. };
  385. struct smbios_bios
  386. {
  387. struct smbios_header hdr;
  388. BYTE vendor;
  389. BYTE version;
  390. WORD start;
  391. BYTE date;
  392. BYTE size;
  393. UINT64 characteristics;
  394. BYTE characteristics_ext[2];
  395. BYTE system_bios_major_release;
  396. BYTE system_bios_minor_release;
  397. BYTE ec_firmware_major_release;
  398. BYTE ec_firmware_minor_release;
  399. };
  400. struct smbios_system
  401. {
  402. struct smbios_header hdr;
  403. BYTE vendor;
  404. BYTE product;
  405. BYTE version;
  406. BYTE serial;
  407. BYTE uuid[16];
  408. BYTE wake_up_type;
  409. BYTE sku;
  410. BYTE family;
  411. };
  412. #include "poppack.h"
  413. #define RSMB (('R' << 24) | ('S' << 16) | ('M' << 8) | 'B')
  414. static const struct smbios_header *find_smbios_entry( enum smbios_type type, const char *buf, UINT len )
  415. {
  416. const char *ptr, *start;
  417. const struct smbios_prologue *prologue;
  418. const struct smbios_header *hdr;
  419. if (len < sizeof(struct smbios_prologue)) return NULL;
  420. prologue = (const struct smbios_prologue *)buf;
  421. if (prologue->length > len - sizeof(*prologue) || prologue->length < sizeof(*hdr)) return NULL;
  422. start = (const char *)(prologue + 1);
  423. hdr = (const struct smbios_header *)start;
  424. for (;;)
  425. {
  426. if ((const char *)hdr - start >= prologue->length - sizeof(*hdr)) return NULL;
  427. if (!hdr->length)
  428. {
  429. WARN( "invalid entry\n" );
  430. return NULL;
  431. }
  432. if (hdr->type == type)
  433. {
  434. if ((const char *)hdr - start + hdr->length > prologue->length) return NULL;
  435. break;
  436. }
  437. else /* skip other entries and their strings */
  438. {
  439. for (ptr = (const char *)hdr + hdr->length; ptr - buf < len && *ptr; ptr++)
  440. {
  441. for (; ptr - buf < len; ptr++) if (!*ptr) break;
  442. }
  443. if (ptr == (const char *)hdr + hdr->length) ptr++;
  444. hdr = (const struct smbios_header *)(ptr + 1);
  445. }
  446. }
  447. return hdr;
  448. }
  449. static inline WCHAR *heap_strdupAW( const char *src )
  450. {
  451. int len;
  452. WCHAR *dst;
  453. if (!src) return NULL;
  454. len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
  455. if ((dst = HeapAlloc( GetProcessHeap(), 0, len * sizeof(*dst) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
  456. return dst;
  457. }
  458. static WCHAR *get_smbios_string( BYTE id, const char *buf, UINT offset, UINT buflen )
  459. {
  460. const char *ptr = buf + offset;
  461. UINT i = 0;
  462. if (!id || offset >= buflen) return NULL;
  463. for (ptr = buf + offset; ptr - buf < buflen && *ptr; ptr++)
  464. {
  465. if (++i == id) return heap_strdupAW( ptr );
  466. for (; ptr - buf < buflen; ptr++) if (!*ptr) break;
  467. }
  468. return NULL;
  469. }
  470. static void set_value_from_smbios_string( HKEY key, const WCHAR *value, BYTE id, const char *buf, UINT offset, UINT buflen )
  471. {
  472. WCHAR *str;
  473. str = get_smbios_string( id, buf, offset, buflen );
  474. set_reg_value( key, value, str ? str : L"" );
  475. HeapFree( GetProcessHeap(), 0, str );
  476. }
  477. static void create_bios_baseboard_values( HKEY bios_key, const char *buf, UINT len )
  478. {
  479. const struct smbios_header *hdr;
  480. const struct smbios_baseboard *baseboard;
  481. UINT offset;
  482. if (!(hdr = find_smbios_entry( SMBIOS_TYPE_BASEBOARD, buf, len ))) return;
  483. baseboard = (const struct smbios_baseboard *)hdr;
  484. offset = (const char *)baseboard - buf + baseboard->hdr.length;
  485. set_value_from_smbios_string( bios_key, L"BaseBoardManufacturer", baseboard->vendor, buf, offset, len );
  486. set_value_from_smbios_string( bios_key, L"BaseBoardProduct", baseboard->product, buf, offset, len );
  487. set_value_from_smbios_string( bios_key, L"BaseBoardVersion", baseboard->version, buf, offset, len );
  488. }
  489. static void create_bios_bios_values( HKEY bios_key, const char *buf, UINT len )
  490. {
  491. const struct smbios_header *hdr;
  492. const struct smbios_bios *bios;
  493. UINT offset;
  494. if (!(hdr = find_smbios_entry( SMBIOS_TYPE_BIOS, buf, len ))) return;
  495. bios = (const struct smbios_bios *)hdr;
  496. offset = (const char *)bios - buf + bios->hdr.length;
  497. set_value_from_smbios_string( bios_key, L"BIOSVendor", bios->vendor, buf, offset, len );
  498. set_value_from_smbios_string( bios_key, L"BIOSVersion", bios->version, buf, offset, len );
  499. set_value_from_smbios_string( bios_key, L"BIOSReleaseDate", bios->date, buf, offset, len );
  500. if (bios->hdr.length >= 0x18)
  501. {
  502. set_reg_value_dword( bios_key, L"BiosMajorRelease", bios->system_bios_major_release );
  503. set_reg_value_dword( bios_key, L"BiosMinorRelease", bios->system_bios_minor_release );
  504. set_reg_value_dword( bios_key, L"ECFirmwareMajorVersion", bios->ec_firmware_major_release );
  505. set_reg_value_dword( bios_key, L"ECFirmwareMinorVersion", bios->ec_firmware_minor_release );
  506. }
  507. else
  508. {
  509. set_reg_value_dword( bios_key, L"BiosMajorRelease", 0xFF );
  510. set_reg_value_dword( bios_key, L"BiosMinorRelease", 0xFF );
  511. set_reg_value_dword( bios_key, L"ECFirmwareMajorVersion", 0xFF );
  512. set_reg_value_dword( bios_key, L"ECFirmwareMinorVersion", 0xFF );
  513. }
  514. }
  515. static void create_bios_system_values( HKEY bios_key, const char *buf, UINT len )
  516. {
  517. const struct smbios_header *hdr;
  518. const struct smbios_system *system;
  519. UINT offset;
  520. if (!(hdr = find_smbios_entry( SMBIOS_TYPE_SYSTEM, buf, len ))) return;
  521. system = (const struct smbios_system *)hdr;
  522. offset = (const char *)system - buf + system->hdr.length;
  523. set_value_from_smbios_string( bios_key, L"SystemManufacturer", system->vendor, buf, offset, len );
  524. set_value_from_smbios_string( bios_key, L"SystemProductName", system->product, buf, offset, len );
  525. set_value_from_smbios_string( bios_key, L"SystemVersion", system->version, buf, offset, len );
  526. if (system->hdr.length >= 0x1B)
  527. {
  528. set_value_from_smbios_string( bios_key, L"SystemSKU", system->sku, buf, offset, len );
  529. set_value_from_smbios_string( bios_key, L"SystemFamily", system->family, buf, offset, len );
  530. }
  531. else
  532. {
  533. set_value_from_smbios_string( bios_key, L"SystemSKU", 0, buf, offset, len );
  534. set_value_from_smbios_string( bios_key, L"SystemFamily", 0, buf, offset, len );
  535. }
  536. }
  537. static void create_bios_key( HKEY system_key )
  538. {
  539. HKEY bios_key;
  540. UINT len;
  541. char *buf;
  542. if (RegCreateKeyExW( system_key, L"BIOS", 0, NULL, REG_OPTION_VOLATILE,
  543. KEY_ALL_ACCESS, NULL, &bios_key, NULL ))
  544. return;
  545. len = GetSystemFirmwareTable( RSMB, 0, NULL, 0 );
  546. if (!(buf = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
  547. len = GetSystemFirmwareTable( RSMB, 0, buf, len );
  548. create_bios_baseboard_values( bios_key, buf, len );
  549. create_bios_bios_values( bios_key, buf, len );
  550. create_bios_system_values( bios_key, buf, len );
  551. done:
  552. HeapFree( GetProcessHeap(), 0, buf );
  553. RegCloseKey( bios_key );
  554. }
  555. /* create the volatile hardware registry keys */
  556. static void create_hardware_registry_keys(void)
  557. {
  558. unsigned int i;
  559. HKEY hkey, system_key, cpu_key, fpu_key;
  560. SYSTEM_CPU_INFORMATION sci;
  561. PROCESSOR_POWER_INFORMATION* power_info;
  562. ULONG sizeof_power_info = sizeof(PROCESSOR_POWER_INFORMATION) * NtCurrentTeb()->Peb->NumberOfProcessors;
  563. WCHAR id[60], namestr[49], vendorid[13];
  564. get_namestring( namestr );
  565. get_vendorid( vendorid );
  566. NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
  567. power_info = HeapAlloc( GetProcessHeap(), 0, sizeof_power_info );
  568. if (power_info == NULL)
  569. return;
  570. if (NtPowerInformation( ProcessorInformation, NULL, 0, power_info, sizeof_power_info ))
  571. memset( power_info, 0, sizeof_power_info );
  572. switch (sci.ProcessorArchitecture)
  573. {
  574. case PROCESSOR_ARCHITECTURE_ARM:
  575. case PROCESSOR_ARCHITECTURE_ARM64:
  576. swprintf( id, ARRAY_SIZE(id), L"ARM Family %u Model %u Revision %u",
  577. sci.ProcessorLevel, HIBYTE(sci.ProcessorRevision), LOBYTE(sci.ProcessorRevision) );
  578. break;
  579. case PROCESSOR_ARCHITECTURE_AMD64:
  580. get_identifier( id, ARRAY_SIZE(id), !wcscmp(vendorid, L"AuthenticAMD") ? L"AMD64" : L"Intel64" );
  581. break;
  582. case PROCESSOR_ARCHITECTURE_INTEL:
  583. default:
  584. get_identifier( id, ARRAY_SIZE(id), L"x86" );
  585. break;
  586. }
  587. if (RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"Hardware\\Description\\System", 0, NULL,
  588. REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &system_key, NULL ))
  589. {
  590. HeapFree( GetProcessHeap(), 0, power_info );
  591. return;
  592. }
  593. switch (sci.ProcessorArchitecture)
  594. {
  595. case PROCESSOR_ARCHITECTURE_ARM:
  596. case PROCESSOR_ARCHITECTURE_ARM64:
  597. set_reg_value( system_key, L"Identifier", L"ARM processor family" );
  598. break;
  599. case PROCESSOR_ARCHITECTURE_INTEL:
  600. case PROCESSOR_ARCHITECTURE_AMD64:
  601. default:
  602. set_reg_value( system_key, L"Identifier", L"AT compatible" );
  603. break;
  604. }
  605. if (sci.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM ||
  606. sci.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64 ||
  607. RegCreateKeyExW( system_key, L"FloatingPointProcessor", 0, NULL, REG_OPTION_VOLATILE,
  608. KEY_ALL_ACCESS, NULL, &fpu_key, NULL ))
  609. fpu_key = 0;
  610. if (RegCreateKeyExW( system_key, L"CentralProcessor", 0, NULL, REG_OPTION_VOLATILE,
  611. KEY_ALL_ACCESS, NULL, &cpu_key, NULL ))
  612. cpu_key = 0;
  613. for (i = 0; i < NtCurrentTeb()->Peb->NumberOfProcessors; i++)
  614. {
  615. WCHAR numW[10];
  616. swprintf( numW, ARRAY_SIZE(numW), L"%u", i );
  617. if (!RegCreateKeyExW( cpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
  618. KEY_ALL_ACCESS, NULL, &hkey, NULL ))
  619. {
  620. RegSetValueExW( hkey, L"FeatureSet", 0, REG_DWORD, (BYTE *)&sci.ProcessorFeatureBits, sizeof(DWORD) );
  621. set_reg_value( hkey, L"Identifier", id );
  622. /* TODO: report ARM properly */
  623. set_reg_value( hkey, L"ProcessorNameString", namestr );
  624. set_reg_value( hkey, L"VendorIdentifier", vendorid );
  625. RegSetValueExW( hkey, L"~MHz", 0, REG_DWORD, (BYTE *)&power_info[i].MaxMhz, sizeof(DWORD) );
  626. RegCloseKey( hkey );
  627. }
  628. if (sci.ProcessorArchitecture != PROCESSOR_ARCHITECTURE_ARM &&
  629. sci.ProcessorArchitecture != PROCESSOR_ARCHITECTURE_ARM64 &&
  630. !RegCreateKeyExW( fpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
  631. KEY_ALL_ACCESS, NULL, &hkey, NULL ))
  632. {
  633. set_reg_value( hkey, L"Identifier", id );
  634. RegCloseKey( hkey );
  635. }
  636. }
  637. create_bios_key( system_key );
  638. RegCloseKey( fpu_key );
  639. RegCloseKey( cpu_key );
  640. RegCloseKey( system_key );
  641. HeapFree( GetProcessHeap(), 0, power_info );
  642. }
  643. /* create the DynData registry keys */
  644. static void create_dynamic_registry_keys(void)
  645. {
  646. HKEY key;
  647. if (!RegCreateKeyExW( HKEY_DYN_DATA, L"PerfStats\\StatData", 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
  648. RegCloseKey( key );
  649. if (!RegCreateKeyExW( HKEY_DYN_DATA, L"Config Manager\\Enum", 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
  650. RegCloseKey( key );
  651. }
  652. /* create the platform-specific environment registry keys */
  653. static void create_environment_registry_keys( void )
  654. {
  655. HKEY env_key;
  656. SYSTEM_CPU_INFORMATION sci;
  657. WCHAR buffer[60], vendorid[13];
  658. const WCHAR *arch, *parch;
  659. if (RegCreateKeyW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Session Manager\\Environment", &env_key )) return;
  660. get_vendorid( vendorid );
  661. NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
  662. swprintf( buffer, ARRAY_SIZE(buffer), L"%u", NtCurrentTeb()->Peb->NumberOfProcessors );
  663. set_reg_value( env_key, L"NUMBER_OF_PROCESSORS", buffer );
  664. switch (sci.ProcessorArchitecture)
  665. {
  666. case PROCESSOR_ARCHITECTURE_AMD64:
  667. arch = L"AMD64";
  668. parch = !wcscmp(vendorid, L"AuthenticAMD") ? L"AMD64" : L"Intel64";
  669. break;
  670. case PROCESSOR_ARCHITECTURE_INTEL:
  671. default:
  672. arch = parch = L"x86";
  673. break;
  674. }
  675. set_reg_value( env_key, L"PROCESSOR_ARCHITECTURE", arch );
  676. switch (sci.ProcessorArchitecture)
  677. {
  678. case PROCESSOR_ARCHITECTURE_ARM:
  679. case PROCESSOR_ARCHITECTURE_ARM64:
  680. swprintf( buffer, ARRAY_SIZE(buffer), L"ARM Family %u Model %u Revision %u",
  681. sci.ProcessorLevel, HIBYTE(sci.ProcessorRevision), LOBYTE(sci.ProcessorRevision) );
  682. break;
  683. case PROCESSOR_ARCHITECTURE_AMD64:
  684. case PROCESSOR_ARCHITECTURE_INTEL:
  685. default:
  686. get_identifier( buffer, ARRAY_SIZE(buffer), parch );
  687. lstrcatW( buffer, L", " );
  688. lstrcatW( buffer, vendorid );
  689. break;
  690. }
  691. set_reg_value( env_key, L"PROCESSOR_IDENTIFIER", buffer );
  692. swprintf( buffer, ARRAY_SIZE(buffer), L"%u", sci.ProcessorLevel );
  693. set_reg_value( env_key, L"PROCESSOR_LEVEL", buffer );
  694. swprintf( buffer, ARRAY_SIZE(buffer), L"%04x", sci.ProcessorRevision );
  695. set_reg_value( env_key, L"PROCESSOR_REVISION", buffer );
  696. RegCloseKey( env_key );
  697. }
  698. /* create the ComputerName registry keys */
  699. static void create_computer_name_keys(void)
  700. {
  701. struct addrinfo hints = {0}, *res;
  702. char *dot, buffer[256], *name = buffer;
  703. HKEY key, subkey;
  704. if (gethostname( buffer, sizeof(buffer) )) return;
  705. hints.ai_flags = AI_CANONNAME;
  706. if (!getaddrinfo( buffer, NULL, &hints, &res )) name = res->ai_canonname;
  707. dot = strchr( name, '.' );
  708. if (dot) *dot++ = 0;
  709. else dot = name + strlen(name);
  710. SetComputerNameExA( ComputerNamePhysicalDnsDomain, dot );
  711. SetComputerNameExA( ComputerNamePhysicalDnsHostname, name );
  712. if (name != buffer) freeaddrinfo( res );
  713. if (RegOpenKeyW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName", &key ))
  714. return;
  715. if (!RegOpenKeyW( key, L"ComputerName", &subkey ))
  716. {
  717. DWORD type, size = sizeof(buffer);
  718. if (RegQueryValueExW( subkey, L"ComputerName", NULL, &type, (BYTE *)buffer, &size )) size = 0;
  719. RegCloseKey( subkey );
  720. if (size && !RegCreateKeyExW( key, L"ActiveComputerName", 0, NULL, REG_OPTION_VOLATILE,
  721. KEY_ALL_ACCESS, NULL, &subkey, NULL ))
  722. {
  723. RegSetValueExW( subkey, L"ComputerName", 0, type, (const BYTE *)buffer, size );
  724. RegCloseKey( subkey );
  725. }
  726. }
  727. RegCloseKey( key );
  728. }
  729. static void create_volatile_environment_registry_key(void)
  730. {
  731. WCHAR path[MAX_PATH];
  732. WCHAR computername[MAX_COMPUTERNAME_LENGTH + 1 + 2];
  733. DWORD size;
  734. HKEY hkey;
  735. HRESULT hr;
  736. if (RegCreateKeyExW( HKEY_CURRENT_USER, L"Volatile Environment", 0, NULL, REG_OPTION_VOLATILE,
  737. KEY_ALL_ACCESS, NULL, &hkey, NULL ))
  738. return;
  739. hr = SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
  740. if (SUCCEEDED(hr)) set_reg_value( hkey, L"APPDATA", path );
  741. set_reg_value( hkey, L"CLIENTNAME", L"Console" );
  742. /* Write the profile path's drive letter and directory components into
  743. * HOMEDRIVE and HOMEPATH respectively. */
  744. hr = SHGetFolderPathW( NULL, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
  745. if (SUCCEEDED(hr))
  746. {
  747. set_reg_value( hkey, L"USERPROFILE", path );
  748. set_reg_value( hkey, L"HOMEPATH", path + 2 );
  749. path[2] = '\0';
  750. set_reg_value( hkey, L"HOMEDRIVE", path );
  751. }
  752. size = ARRAY_SIZE(path);
  753. if (GetUserNameW( path, &size )) set_reg_value( hkey, L"USERNAME", path );
  754. set_reg_value( hkey, L"HOMESHARE", L"" );
  755. hr = SHGetFolderPathW( NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
  756. if (SUCCEEDED(hr))
  757. set_reg_value( hkey, L"LOCALAPPDATA", path );
  758. size = ARRAY_SIZE(computername) - 2;
  759. if (GetComputerNameW(&computername[2], &size))
  760. {
  761. set_reg_value( hkey, L"USERDOMAIN", &computername[2] );
  762. computername[0] = computername[1] = '\\';
  763. set_reg_value( hkey, L"LOGONSERVER", computername );
  764. }
  765. set_reg_value( hkey, L"SESSIONNAME", L"Console" );
  766. RegCloseKey( hkey );
  767. }
  768. /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
  769. * Returns FALSE if there was an error, or otherwise if all is ok.
  770. */
  771. static BOOL wininit(void)
  772. {
  773. WCHAR initial_buffer[1024];
  774. WCHAR *str, *buffer = initial_buffer;
  775. DWORD size = ARRAY_SIZE(initial_buffer);
  776. DWORD res;
  777. for (;;)
  778. {
  779. if (!(res = GetPrivateProfileSectionW( L"rename", buffer, size, L"wininit.ini" ))) return TRUE;
  780. if (res < size - 2) break;
  781. if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
  782. size *= 2;
  783. if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
  784. }
  785. for (str = buffer; *str; str += lstrlenW(str) + 1)
  786. {
  787. WCHAR *value;
  788. if (*str == ';') continue; /* comment */
  789. if (!(value = wcschr( str, '=' ))) continue;
  790. /* split the line into key and value */
  791. *value++ = 0;
  792. if (!lstrcmpiW( L"NUL", str ))
  793. {
  794. WINE_TRACE("Deleting file %s\n", wine_dbgstr_w(value) );
  795. if( !DeleteFileW( value ) )
  796. WINE_WARN("Error deleting file %s\n", wine_dbgstr_w(value) );
  797. }
  798. else
  799. {
  800. WINE_TRACE("Renaming file %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
  801. if( !MoveFileExW(value, str, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) )
  802. WINE_WARN("Error renaming %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
  803. }
  804. str = value;
  805. }
  806. if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
  807. if( !MoveFileExW( L"wininit.ini", L"wininit.bak", MOVEFILE_REPLACE_EXISTING) )
  808. {
  809. WINE_ERR("Couldn't rename wininit.ini, error %d\n", GetLastError() );
  810. return FALSE;
  811. }
  812. return TRUE;
  813. }
  814. static void pendingRename(void)
  815. {
  816. WCHAR *buffer=NULL;
  817. const WCHAR *src=NULL, *dst=NULL;
  818. DWORD dataLength=0;
  819. HKEY hSession;
  820. if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Session Manager",
  821. 0, KEY_ALL_ACCESS, &hSession ))
  822. return;
  823. if (RegQueryValueExW( hSession, L"PendingFileRenameOperations", NULL, NULL, NULL, &dataLength ))
  824. goto end;
  825. if (!(buffer = HeapAlloc( GetProcessHeap(), 0, dataLength ))) goto end;
  826. if (RegQueryValueExW( hSession, L"PendingFileRenameOperations", NULL, NULL,
  827. (LPBYTE)buffer, &dataLength ))
  828. goto end;
  829. /* Make sure that the data is long enough and ends with two NULLs. This
  830. * simplifies the code later on.
  831. */
  832. if( dataLength<2*sizeof(buffer[0]) ||
  833. buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
  834. buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
  835. goto end;
  836. for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
  837. src=dst+lstrlenW(dst)+1 )
  838. {
  839. DWORD dwFlags=0;
  840. dst=src+lstrlenW(src)+1;
  841. /* We need to skip the \??\ header */
  842. if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
  843. src+=4;
  844. if( dst[0]=='!' )
  845. {
  846. dwFlags|=MOVEFILE_REPLACE_EXISTING;
  847. dst++;
  848. }
  849. if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
  850. dst+=4;
  851. if( *dst!='\0' )
  852. {
  853. /* Rename the file */
  854. MoveFileExW( src, dst, dwFlags );
  855. } else
  856. {
  857. /* Delete the file or directory */
  858. if (!RemoveDirectoryW( src ) && GetLastError() == ERROR_DIRECTORY) DeleteFileW( src );
  859. }
  860. }
  861. RegDeleteValueW(hSession, L"PendingFileRenameOperations");
  862. end:
  863. HeapFree(GetProcessHeap(), 0, buffer);
  864. RegCloseKey( hSession );
  865. }
  866. #define INVALID_RUNCMD_RETURN -1
  867. /*
  868. * This function runs the specified command in the specified dir.
  869. * [in,out] cmdline - the command line to run. The function may change the passed buffer.
  870. * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
  871. * [in] wait - whether to wait for the run program to finish before returning.
  872. * [in] minimized - Whether to ask the program to run minimized.
  873. *
  874. * Returns:
  875. * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
  876. * If wait is FALSE - returns 0 if successful.
  877. * If wait is TRUE - returns the program's return value.
  878. */
  879. static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
  880. {
  881. STARTUPINFOW si;
  882. PROCESS_INFORMATION info;
  883. DWORD exit_code=0;
  884. memset(&si, 0, sizeof(si));
  885. si.cb=sizeof(si);
  886. if( minimized )
  887. {
  888. si.dwFlags=STARTF_USESHOWWINDOW;
  889. si.wShowWindow=SW_MINIMIZE;
  890. }
  891. memset(&info, 0, sizeof(info));
  892. if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
  893. {
  894. WINE_WARN("Failed to run command %s (%d)\n", wine_dbgstr_w(cmdline), GetLastError() );
  895. return INVALID_RUNCMD_RETURN;
  896. }
  897. WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
  898. wine_dbgstr_w(cmdline), info.hProcess );
  899. if(wait)
  900. { /* wait for the process to exit */
  901. WaitForSingleObject(info.hProcess, INFINITE);
  902. GetExitCodeProcess(info.hProcess, &exit_code);
  903. }
  904. CloseHandle( info.hThread );
  905. CloseHandle( info.hProcess );
  906. return exit_code;
  907. }
  908. static void process_run_key( HKEY key, const WCHAR *keyname, BOOL delete, BOOL synchronous )
  909. {
  910. HKEY runkey;
  911. LONG res;
  912. DWORD disp, i, max_cmdline = 0, max_value = 0;
  913. WCHAR *cmdline = NULL, *value = NULL;
  914. if (RegCreateKeyExW( key, keyname, 0, NULL, 0, delete ? KEY_ALL_ACCESS : KEY_READ, NULL, &runkey, &disp ))
  915. return;
  916. if (disp == REG_CREATED_NEW_KEY)
  917. goto end;
  918. if (RegQueryInfoKeyW( runkey, NULL, NULL, NULL, NULL, NULL, NULL, &i, &max_value, &max_cmdline, NULL, NULL ))
  919. goto end;
  920. if (!i)
  921. {
  922. WINE_TRACE( "No commands to execute.\n" );
  923. goto end;
  924. }
  925. if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, max_cmdline )))
  926. {
  927. WINE_ERR( "Couldn't allocate memory for the commands to be executed.\n" );
  928. goto end;
  929. }
  930. if (!(value = HeapAlloc( GetProcessHeap(), 0, ++max_value * sizeof(*value) )))
  931. {
  932. WINE_ERR( "Couldn't allocate memory for the value names.\n" );
  933. goto end;
  934. }
  935. while (i)
  936. {
  937. DWORD len = max_value, len_data = max_cmdline, type;
  938. if ((res = RegEnumValueW( runkey, --i, value, &len, 0, &type, (BYTE *)cmdline, &len_data )))
  939. {
  940. WINE_ERR( "Couldn't read value %u (%d).\n", i, res );
  941. continue;
  942. }
  943. if (delete && (res = RegDeleteValueW( runkey, value )))
  944. {
  945. WINE_ERR( "Couldn't delete value %u (%d). Running command anyways.\n", i, res );
  946. }
  947. if (type != REG_SZ)
  948. {
  949. WINE_ERR( "Incorrect type of value %u (%u).\n", i, type );
  950. continue;
  951. }
  952. if (runCmd( cmdline, NULL, synchronous, FALSE ) == INVALID_RUNCMD_RETURN)
  953. {
  954. WINE_ERR( "Error running cmd %s (%u).\n", wine_dbgstr_w(cmdline), GetLastError() );
  955. }
  956. WINE_TRACE( "Done processing cmd %u.\n", i );
  957. }
  958. end:
  959. HeapFree( GetProcessHeap(), 0, value );
  960. HeapFree( GetProcessHeap(), 0, cmdline );
  961. RegCloseKey( runkey );
  962. WINE_TRACE( "Done.\n" );
  963. }
  964. /*
  965. * Process a "Run" type registry key.
  966. * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
  967. * opened.
  968. * szKeyName is the key holding the actual entries.
  969. * bDelete tells whether we should delete each value right before executing it.
  970. * bSynchronous tells whether we should wait for the prog to complete before
  971. * going on to the next prog.
  972. */
  973. static void ProcessRunKeys( HKEY root, const WCHAR *keyname, BOOL delete, BOOL synchronous )
  974. {
  975. HKEY key;
  976. if (root == HKEY_LOCAL_MACHINE)
  977. {
  978. WINE_TRACE( "Processing %s entries under HKLM.\n", wine_dbgstr_w(keyname) );
  979. if (!RegCreateKeyExW( root, L"Software\\Microsoft\\Windows\\CurrentVersion",
  980. 0, NULL, 0, KEY_READ, NULL, &key, NULL ))
  981. {
  982. process_run_key( key, keyname, delete, synchronous );
  983. RegCloseKey( key );
  984. }
  985. if (is_64bit && !RegCreateKeyExW( root, L"Software\\Microsoft\\Windows\\CurrentVersion",
  986. 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &key, NULL ))
  987. {
  988. process_run_key( key, keyname, delete, synchronous );
  989. RegCloseKey( key );
  990. }
  991. }
  992. else
  993. {
  994. WINE_TRACE( "Processing %s entries under HKCU.\n", wine_dbgstr_w(keyname) );
  995. if (!RegCreateKeyExW( root, L"Software\\Microsoft\\Windows\\CurrentVersion",
  996. 0, NULL, 0, KEY_READ, NULL, &key, NULL ))
  997. {
  998. process_run_key( key, keyname, delete, synchronous );
  999. RegCloseKey( key );
  1000. }
  1001. }
  1002. }
  1003. /*
  1004. * WFP is Windows File Protection, in NT5 and Windows 2000 it maintains a cache
  1005. * of known good dlls and scans through and replaces corrupted DLLs with these
  1006. * known good versions. The only programs that should install into this dll
  1007. * cache are Windows Updates and IE (which is treated like a Windows Update)
  1008. *
  1009. * Implementing this allows installing ie in win2k mode to actually install the
  1010. * system dlls that we expect and need
  1011. */
  1012. static int ProcessWindowsFileProtection(void)
  1013. {
  1014. WIN32_FIND_DATAW finddata;
  1015. HANDLE find_handle;
  1016. BOOL find_rc;
  1017. DWORD rc;
  1018. HKEY hkey;
  1019. LPWSTR dllcache = NULL;
  1020. if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", &hkey ))
  1021. {
  1022. DWORD sz = 0;
  1023. if (!RegQueryValueExW( hkey, L"SFCDllCacheDir", 0, NULL, NULL, &sz))
  1024. {
  1025. sz += sizeof(WCHAR);
  1026. dllcache = HeapAlloc(GetProcessHeap(),0,sz + sizeof(L"\\*"));
  1027. RegQueryValueExW( hkey, L"SFCDllCacheDir", 0, NULL, (LPBYTE)dllcache, &sz);
  1028. lstrcatW( dllcache, L"\\*" );
  1029. }
  1030. }
  1031. RegCloseKey(hkey);
  1032. if (!dllcache)
  1033. {
  1034. DWORD sz = GetSystemDirectoryW( NULL, 0 );
  1035. dllcache = HeapAlloc( GetProcessHeap(), 0, sz * sizeof(WCHAR) + sizeof(L"\\dllcache\\*"));
  1036. GetSystemDirectoryW( dllcache, sz );
  1037. lstrcatW( dllcache, L"\\dllcache\\*" );
  1038. }
  1039. find_handle = FindFirstFileW(dllcache,&finddata);
  1040. dllcache[ lstrlenW(dllcache) - 2] = 0; /* strip off wildcard */
  1041. find_rc = find_handle != INVALID_HANDLE_VALUE;
  1042. while (find_rc)
  1043. {
  1044. WCHAR targetpath[MAX_PATH];
  1045. WCHAR currentpath[MAX_PATH];
  1046. UINT sz;
  1047. UINT sz2;
  1048. WCHAR tempfile[MAX_PATH];
  1049. if (wcscmp(finddata.cFileName,L".") == 0 || wcscmp(finddata.cFileName,L"..") == 0)
  1050. {
  1051. find_rc = FindNextFileW(find_handle,&finddata);
  1052. continue;
  1053. }
  1054. sz = MAX_PATH;
  1055. sz2 = MAX_PATH;
  1056. VerFindFileW(VFFF_ISSHAREDFILE, finddata.cFileName, windowsdir,
  1057. windowsdir, currentpath, &sz, targetpath, &sz2);
  1058. sz = MAX_PATH;
  1059. rc = VerInstallFileW(0, finddata.cFileName, finddata.cFileName,
  1060. dllcache, targetpath, currentpath, tempfile, &sz);
  1061. if (rc != ERROR_SUCCESS)
  1062. {
  1063. WINE_WARN("WFP: %s error 0x%x\n",wine_dbgstr_w(finddata.cFileName),rc);
  1064. DeleteFileW(tempfile);
  1065. }
  1066. /* now delete the source file so that we don't try to install it over and over again */
  1067. lstrcpynW( targetpath, dllcache, MAX_PATH - 1 );
  1068. sz = lstrlenW( targetpath );
  1069. targetpath[sz++] = '\\';
  1070. lstrcpynW( targetpath + sz, finddata.cFileName, MAX_PATH - sz );
  1071. if (!DeleteFileW( targetpath ))
  1072. WINE_WARN( "failed to delete %s: error %u\n", wine_dbgstr_w(targetpath), GetLastError() );
  1073. find_rc = FindNextFileW(find_handle,&finddata);
  1074. }
  1075. FindClose(find_handle);
  1076. HeapFree(GetProcessHeap(),0,dllcache);
  1077. return 1;
  1078. }
  1079. static BOOL start_services_process(void)
  1080. {
  1081. static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
  1082. PROCESS_INFORMATION pi;
  1083. STARTUPINFOW si = { sizeof(si) };
  1084. HANDLE wait_handles[2];
  1085. if (!CreateProcessW(L"C:\\windows\\system32\\services.exe", NULL,
  1086. NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
  1087. {
  1088. WINE_ERR("Couldn't start services.exe: error %u\n", GetLastError());
  1089. return FALSE;
  1090. }
  1091. CloseHandle(pi.hThread);
  1092. wait_handles[0] = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
  1093. wait_handles[1] = pi.hProcess;
  1094. /* wait for the event to become available or the process to exit */
  1095. if ((WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE)) == WAIT_OBJECT_0 + 1)
  1096. {
  1097. DWORD exit_code;
  1098. GetExitCodeProcess(pi.hProcess, &exit_code);
  1099. WINE_ERR("Unexpected termination of services.exe - exit code %d\n", exit_code);
  1100. CloseHandle(pi.hProcess);
  1101. CloseHandle(wait_handles[0]);
  1102. return FALSE;
  1103. }
  1104. CloseHandle(pi.hProcess);
  1105. CloseHandle(wait_handles[0]);
  1106. return TRUE;
  1107. }
  1108. static INT_PTR CALLBACK wait_dlgproc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
  1109. {
  1110. switch (msg)
  1111. {
  1112. case WM_INITDIALOG:
  1113. {
  1114. DWORD len;
  1115. WCHAR *buffer, text[1024];
  1116. const WCHAR *name = (WCHAR *)lp;
  1117. HICON icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 48, 48, LR_SHARED );
  1118. SendDlgItemMessageW( hwnd, IDC_WAITICON, STM_SETICON, (WPARAM)icon, 0 );
  1119. SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_GETTEXT, 1024, (LPARAM)text );
  1120. len = lstrlenW(text) + lstrlenW(name) + 1;
  1121. buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
  1122. swprintf( buffer, len, text, name );
  1123. SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_SETTEXT, 0, (LPARAM)buffer );
  1124. HeapFree( GetProcessHeap(), 0, buffer );
  1125. }
  1126. break;
  1127. }
  1128. return 0;
  1129. }
  1130. static HWND show_wait_window(void)
  1131. {
  1132. HWND hwnd = CreateDialogParamW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_WAITDLG), 0,
  1133. wait_dlgproc, (LPARAM)prettyprint_configdir() );
  1134. ShowWindow( hwnd, SW_SHOWNORMAL );
  1135. return hwnd;
  1136. }
  1137. static HANDLE start_rundll32( const WCHAR *inf_path, const WCHAR *install, WORD machine )
  1138. {
  1139. WCHAR app[MAX_PATH + ARRAY_SIZE(L"\\rundll32.exe" )];
  1140. STARTUPINFOW si;
  1141. PROCESS_INFORMATION pi;
  1142. WCHAR *buffer;
  1143. DWORD len;
  1144. memset( &si, 0, sizeof(si) );
  1145. si.cb = sizeof(si);
  1146. if (!GetSystemWow64Directory2W( app, MAX_PATH, machine )) return 0;
  1147. lstrcatW( app, L"\\rundll32.exe" );
  1148. TRACE( "machine %x starting %s\n", machine, debugstr_w(app) );
  1149. len = lstrlenW(app) + ARRAY_SIZE(L" setupapi,InstallHinfSection DefaultInstall 128 ") + lstrlenW(inf_path);
  1150. if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
  1151. swprintf( buffer, len, L"%s setupapi,InstallHinfSection %s 128 %s", app, install, inf_path );
  1152. if (CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
  1153. CloseHandle( pi.hThread );
  1154. else
  1155. pi.hProcess = 0;
  1156. HeapFree( GetProcessHeap(), 0, buffer );
  1157. return pi.hProcess;
  1158. }
  1159. static void install_root_pnp_devices(void)
  1160. {
  1161. static const struct
  1162. {
  1163. const char *name;
  1164. const char *hardware_id;
  1165. const char *infpath;
  1166. }
  1167. root_devices[] =
  1168. {
  1169. {"root\\wine\\winebus", "root\\winebus\0", "C:\\windows\\inf\\winebus.inf"},
  1170. {"root\\wine\\wineusb", "root\\wineusb\0", "C:\\windows\\inf\\wineusb.inf"},
  1171. };
  1172. SP_DEVINFO_DATA device = {sizeof(device)};
  1173. unsigned int i;
  1174. HDEVINFO set;
  1175. if ((set = SetupDiCreateDeviceInfoList( NULL, NULL )) == INVALID_HANDLE_VALUE)
  1176. {
  1177. WINE_ERR("Failed to create device info list, error %#x.\n", GetLastError());
  1178. return;
  1179. }
  1180. for (i = 0; i < ARRAY_SIZE(root_devices); ++i)
  1181. {
  1182. if (!SetupDiCreateDeviceInfoA( set, root_devices[i].name, &GUID_NULL, NULL, NULL, 0, &device))
  1183. {
  1184. if (GetLastError() != ERROR_DEVINST_ALREADY_EXISTS)
  1185. WINE_ERR("Failed to create device %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1186. continue;
  1187. }
  1188. if (!SetupDiSetDeviceRegistryPropertyA(set, &device, SPDRP_HARDWAREID,
  1189. (const BYTE *)root_devices[i].hardware_id, (strlen(root_devices[i].hardware_id) + 2) * sizeof(WCHAR)))
  1190. {
  1191. WINE_ERR("Failed to set hardware id for %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1192. continue;
  1193. }
  1194. if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, set, &device))
  1195. {
  1196. WINE_ERR("Failed to register device %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1197. continue;
  1198. }
  1199. if (!UpdateDriverForPlugAndPlayDevicesA(NULL, root_devices[i].hardware_id, root_devices[i].infpath, 0, NULL))
  1200. WINE_ERR("Failed to install drivers for %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1201. }
  1202. SetupDiDestroyDeviceInfoList(set);
  1203. }
  1204. static void update_user_profile(void)
  1205. {
  1206. char token_buf[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD) * SID_MAX_SUB_AUTHORITIES];
  1207. HANDLE token;
  1208. WCHAR profile[MAX_PATH], *sid;
  1209. DWORD size;
  1210. HKEY hkey, profile_hkey;
  1211. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token))
  1212. return;
  1213. size = sizeof(token_buf);
  1214. GetTokenInformation(token, TokenUser, token_buf, size, &size);
  1215. CloseHandle(token);
  1216. ConvertSidToStringSidW(((TOKEN_USER *)token_buf)->User.Sid, &sid);
  1217. if (!RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
  1218. 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL))
  1219. {
  1220. if (!RegCreateKeyExW(hkey, sid, 0, NULL, 0,
  1221. KEY_ALL_ACCESS, NULL, &profile_hkey, NULL))
  1222. {
  1223. DWORD flags = 0;
  1224. if (SHGetSpecialFolderPathW(NULL, profile, CSIDL_PROFILE, TRUE))
  1225. set_reg_value(profile_hkey, L"ProfileImagePath", profile);
  1226. RegSetValueExW( profile_hkey, L"Flags", 0, REG_DWORD, (const BYTE *)&flags, sizeof(flags) );
  1227. RegCloseKey(profile_hkey);
  1228. }
  1229. RegCloseKey(hkey);
  1230. }
  1231. LocalFree(sid);
  1232. }
  1233. /* execute rundll32 on the wine.inf file if necessary */
  1234. static void update_wineprefix( BOOL force )
  1235. {
  1236. const WCHAR *config_dir = _wgetenv( L"WINECONFIGDIR" );
  1237. WCHAR *inf_path = get_wine_inf_path();
  1238. int fd;
  1239. struct stat st;
  1240. if (!inf_path)
  1241. {
  1242. WINE_MESSAGE( "wine: failed to update %s, wine.inf not found\n", debugstr_w( config_dir ));
  1243. return;
  1244. }
  1245. if ((fd = _wopen( inf_path, O_RDONLY )) == -1)
  1246. {
  1247. WINE_MESSAGE( "wine: failed to update %s with %s: %s\n",
  1248. debugstr_w(config_dir), debugstr_w(inf_path), strerror(errno) );
  1249. goto done;
  1250. }
  1251. fstat( fd, &st );
  1252. close( fd );
  1253. if (update_timestamp( config_dir, st.st_mtime ) || force)
  1254. {
  1255. ULONG machines[8];
  1256. HANDLE process = 0;
  1257. DWORD count = 0;
  1258. if (NtQuerySystemInformationEx( SystemSupportedProcessorArchitectures, &process, sizeof(process),
  1259. machines, sizeof(machines), NULL )) machines[0] = 0;
  1260. if ((process = start_rundll32( inf_path, L"PreInstall", IMAGE_FILE_MACHINE_TARGET_HOST )))
  1261. {
  1262. HWND hwnd = show_wait_window();
  1263. for (;;)
  1264. {
  1265. MSG msg;
  1266. DWORD res = MsgWaitForMultipleObjects( 1, &process, FALSE, INFINITE, QS_ALLINPUT );
  1267. if (res == WAIT_OBJECT_0)
  1268. {
  1269. CloseHandle( process );
  1270. if (!machines[count]) break;
  1271. if (HIWORD(machines[count]) & 4 /* native machine */)
  1272. process = start_rundll32( inf_path, L"DefaultInstall", IMAGE_FILE_MACHINE_TARGET_HOST );
  1273. else
  1274. process = start_rundll32( inf_path, L"Wow64Install", LOWORD(machines[count]) );
  1275. count++;
  1276. if (!process) break;
  1277. }
  1278. else while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
  1279. }
  1280. DestroyWindow( hwnd );
  1281. }
  1282. install_root_pnp_devices();
  1283. update_user_profile();
  1284. WINE_MESSAGE( "wine: configuration in %s has been updated.\n", debugstr_w(prettyprint_configdir()) );
  1285. }
  1286. done:
  1287. HeapFree( GetProcessHeap(), 0, inf_path );
  1288. }
  1289. /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
  1290. * shell links here to restart themselves after boot. */
  1291. static BOOL ProcessStartupItems(void)
  1292. {
  1293. BOOL ret = FALSE;
  1294. HRESULT hr;
  1295. IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
  1296. LPITEMIDLIST pidlStartup = NULL, pidlItem;
  1297. ULONG NumPIDLs;
  1298. IEnumIDList *iEnumList = NULL;
  1299. STRRET strret;
  1300. WCHAR wszCommand[MAX_PATH];
  1301. WINE_TRACE("Processing items in the StartUp folder.\n");
  1302. hr = SHGetDesktopFolder(&psfDesktop);
  1303. if (FAILED(hr))
  1304. {
  1305. WINE_ERR("Couldn't get desktop folder.\n");
  1306. goto done;
  1307. }
  1308. hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
  1309. if (FAILED(hr))
  1310. {
  1311. WINE_TRACE("Couldn't get StartUp folder location.\n");
  1312. goto done;
  1313. }
  1314. hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
  1315. if (FAILED(hr))
  1316. {
  1317. WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
  1318. goto done;
  1319. }
  1320. hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
  1321. if (FAILED(hr))
  1322. {
  1323. WINE_TRACE("Unable to enumerate StartUp objects.\n");
  1324. goto done;
  1325. }
  1326. while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
  1327. (NumPIDLs) == 1)
  1328. {
  1329. hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
  1330. if (FAILED(hr))
  1331. WINE_TRACE("Unable to get display name of enumeration item.\n");
  1332. else
  1333. {
  1334. hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
  1335. if (FAILED(hr))
  1336. WINE_TRACE("Unable to parse display name.\n");
  1337. else
  1338. {
  1339. HINSTANCE hinst;
  1340. hinst = ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL);
  1341. if (PtrToUlong(hinst) <= 32)
  1342. WINE_WARN("Error %p executing command %s.\n", hinst, wine_dbgstr_w(wszCommand));
  1343. }
  1344. }
  1345. ILFree(pidlItem);
  1346. }
  1347. /* Return success */
  1348. ret = TRUE;
  1349. done:
  1350. if (iEnumList) IEnumIDList_Release(iEnumList);
  1351. if (psfStartup) IShellFolder_Release(psfStartup);
  1352. if (pidlStartup) ILFree(pidlStartup);
  1353. return ret;
  1354. }
  1355. static void usage( int status )
  1356. {
  1357. WINE_MESSAGE( "Usage: wineboot [options]\n" );
  1358. WINE_MESSAGE( "Options;\n" );
  1359. WINE_MESSAGE( " -h,--help Display this help message\n" );
  1360. WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
  1361. WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
  1362. WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
  1363. WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
  1364. WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
  1365. WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
  1366. WINE_MESSAGE( " -u,--update Update the wineprefix directory\n" );
  1367. exit( status );
  1368. }
  1369. int __cdecl main( int argc, char *argv[] )
  1370. {
  1371. /* First, set the current directory to SystemRoot */
  1372. int i, j;
  1373. BOOL end_session, force, init, kill, restart, shutdown, update;
  1374. HANDLE event;
  1375. OBJECT_ATTRIBUTES attr;
  1376. UNICODE_STRING nameW;
  1377. BOOL is_wow64;
  1378. end_session = force = init = kill = restart = shutdown = update = FALSE;
  1379. GetWindowsDirectoryW( windowsdir, MAX_PATH );
  1380. if( !SetCurrentDirectoryW( windowsdir ) )
  1381. WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
  1382. if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
  1383. {
  1384. STARTUPINFOW si;
  1385. PROCESS_INFORMATION pi;
  1386. WCHAR filename[MAX_PATH];
  1387. void *redir;
  1388. DWORD exit_code;
  1389. memset( &si, 0, sizeof(si) );
  1390. si.cb = sizeof(si);
  1391. GetSystemDirectoryW( filename, MAX_PATH );
  1392. wcscat( filename, L"\\wineboot.exe" );
  1393. Wow64DisableWow64FsRedirection( &redir );
  1394. if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
  1395. {
  1396. WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
  1397. WaitForSingleObject( pi.hProcess, INFINITE );
  1398. GetExitCodeProcess( pi.hProcess, &exit_code );
  1399. ExitProcess( exit_code );
  1400. }
  1401. else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
  1402. Wow64RevertWow64FsRedirection( redir );
  1403. }
  1404. for (i = 1; i < argc; i++)
  1405. {
  1406. if (argv[i][0] != '-') continue;
  1407. if (argv[i][1] == '-')
  1408. {
  1409. if (!strcmp( argv[i], "--help" )) usage( 0 );
  1410. else if (!strcmp( argv[i], "--end-session" )) end_session = TRUE;
  1411. else if (!strcmp( argv[i], "--force" )) force = TRUE;
  1412. else if (!strcmp( argv[i], "--init" )) init = TRUE;
  1413. else if (!strcmp( argv[i], "--kill" )) kill = TRUE;
  1414. else if (!strcmp( argv[i], "--restart" )) restart = TRUE;
  1415. else if (!strcmp( argv[i], "--shutdown" )) shutdown = TRUE;
  1416. else if (!strcmp( argv[i], "--update" )) update = TRUE;
  1417. else usage( 1 );
  1418. continue;
  1419. }
  1420. for (j = 1; argv[i][j]; j++)
  1421. {
  1422. switch (argv[i][j])
  1423. {
  1424. case 'e': end_session = TRUE; break;
  1425. case 'f': force = TRUE; break;
  1426. case 'i': init = TRUE; break;
  1427. case 'k': kill = TRUE; break;
  1428. case 'r': restart = TRUE; break;
  1429. case 's': shutdown = TRUE; break;
  1430. case 'u': update = TRUE; break;
  1431. case 'h': usage(0); break;
  1432. default: usage(1); break;
  1433. }
  1434. }
  1435. }
  1436. if (end_session)
  1437. {
  1438. if (kill)
  1439. {
  1440. if (!shutdown_all_desktops( force )) return 1;
  1441. }
  1442. else if (!shutdown_close_windows( force )) return 1;
  1443. }
  1444. if (kill) kill_processes( shutdown );
  1445. if (shutdown) return 0;
  1446. /* create event to be inherited by services.exe */
  1447. InitializeObjectAttributes( &attr, &nameW, OBJ_OPENIF | OBJ_INHERIT, 0, NULL );
  1448. RtlInitUnicodeString( &nameW, L"\\KernelObjects\\__wineboot_event" );
  1449. NtCreateEvent( &event, EVENT_ALL_ACCESS, &attr, NotificationEvent, 0 );
  1450. ResetEvent( event ); /* in case this is a restart */
  1451. create_user_shared_data();
  1452. create_hardware_registry_keys();
  1453. create_dynamic_registry_keys();
  1454. create_environment_registry_keys();
  1455. create_computer_name_keys();
  1456. wininit();
  1457. pendingRename();
  1458. ProcessWindowsFileProtection();
  1459. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunServicesOnce", TRUE, FALSE );
  1460. if (init || (kill && !restart))
  1461. {
  1462. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunServices", FALSE, FALSE );
  1463. start_services_process();
  1464. }
  1465. if (init || update) update_wineprefix( update );
  1466. create_volatile_environment_registry_key();
  1467. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE );
  1468. if (!init && !restart)
  1469. {
  1470. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE );
  1471. ProcessRunKeys( HKEY_CURRENT_USER, L"Run", FALSE, FALSE );
  1472. ProcessStartupItems();
  1473. }
  1474. WINE_TRACE("Operation done\n");
  1475. SetEvent( event );
  1476. return 0;
  1477. }