wineboot.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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.Architecture;
  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.Architecture)
  259. {
  260. case PROCESSOR_ARCHITECTURE_INTEL:
  261. case PROCESSOR_ARCHITECTURE_AMD64:
  262. features[PF_COMPARE_EXCHANGE_DOUBLE] = !!(sci.FeatureSet & CPU_FEATURE_CX8);
  263. features[PF_MMX_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_MMX);
  264. features[PF_XMMI_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSE);
  265. features[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_3DNOW);
  266. features[PF_RDTSC_INSTRUCTION_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_TSC);
  267. features[PF_PAE_ENABLED] = !!(sci.FeatureSet & CPU_FEATURE_PAE);
  268. features[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSE2);
  269. features[PF_SSE3_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSE3);
  270. features[PF_SSSE3_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSSE3);
  271. features[PF_XSAVE_ENABLED] = !!(sci.FeatureSet & CPU_FEATURE_XSAVE);
  272. features[PF_COMPARE_EXCHANGE128] = !!(sci.FeatureSet & CPU_FEATURE_CX128);
  273. features[PF_SSE_DAZ_MODE_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_DAZ);
  274. features[PF_NX_ENABLED] = !!(sci.FeatureSet & CPU_FEATURE_NX);
  275. features[PF_SECOND_LEVEL_ADDRESS_TRANSLATION] = !!(sci.FeatureSet & CPU_FEATURE_2NDLEV);
  276. features[PF_VIRT_FIRMWARE_ENABLED] = !!(sci.FeatureSet & CPU_FEATURE_VIRT);
  277. features[PF_RDWRFSGSBASE_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_RDFS);
  278. features[PF_FASTFAIL_AVAILABLE] = TRUE;
  279. features[PF_SSE4_1_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSE41);
  280. features[PF_SSE4_2_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_SSE42);
  281. features[PF_AVX_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_AVX);
  282. features[PF_AVX2_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_AVX2);
  283. break;
  284. case PROCESSOR_ARCHITECTURE_ARM:
  285. features[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_ARM_VFP_32);
  286. features[PF_ARM_NEON_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & CPU_FEATURE_ARM_NEON);
  287. features[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = (sci.Level >= 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.FeatureSet & CPU_FEATURE_ARM_V8_CRC32);
  292. features[PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE] = !!(sci.FeatureSet & 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.Architecture)
  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.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
  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.Architecture)
  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.Architecture == PROCESSOR_ARCHITECTURE_ARM ||
  606. sci.Architecture == 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.FeatureSet, 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.Architecture != PROCESSOR_ARCHITECTURE_ARM &&
  629. sci.Architecture != 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.Architecture)
  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.Architecture)
  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.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
  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.Level );
  693. set_reg_value( env_key, L"PROCESSOR_LEVEL", buffer );
  694. swprintf( buffer, ARRAY_SIZE(buffer), L"%04x", sci.Revision );
  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, BOOL wow64 )
  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 (wow64)
  1147. {
  1148. if (!GetSystemWow64DirectoryW( app, MAX_PATH )) return 0; /* not on 64-bit */
  1149. }
  1150. else GetSystemDirectoryW( app, MAX_PATH );
  1151. lstrcatW( app, L"\\rundll32.exe" );
  1152. len = lstrlenW(app) + ARRAY_SIZE(L" setupapi,InstallHinfSection DefaultInstall 128 ") + lstrlenW(inf_path);
  1153. if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
  1154. lstrcpyW( buffer, app );
  1155. lstrcatW( buffer, L" setupapi,InstallHinfSection" );
  1156. lstrcatW( buffer, wow64 ? L" Wow64Install" : L" DefaultInstall" );
  1157. lstrcatW( buffer, L" 128 " );
  1158. lstrcatW( buffer, inf_path );
  1159. if (CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
  1160. CloseHandle( pi.hThread );
  1161. else
  1162. pi.hProcess = 0;
  1163. HeapFree( GetProcessHeap(), 0, buffer );
  1164. return pi.hProcess;
  1165. }
  1166. static void install_root_pnp_devices(void)
  1167. {
  1168. static const struct
  1169. {
  1170. const char *name;
  1171. const char *hardware_id;
  1172. const char *infpath;
  1173. }
  1174. root_devices[] =
  1175. {
  1176. {"root\\wine\\winebus", "root\\winebus\0", "C:\\windows\\inf\\winebus.inf"},
  1177. {"root\\wine\\wineusb", "root\\wineusb\0", "C:\\windows\\inf\\wineusb.inf"},
  1178. };
  1179. SP_DEVINFO_DATA device = {sizeof(device)};
  1180. unsigned int i;
  1181. HDEVINFO set;
  1182. if ((set = SetupDiCreateDeviceInfoList( NULL, NULL )) == INVALID_HANDLE_VALUE)
  1183. {
  1184. WINE_ERR("Failed to create device info list, error %#x.\n", GetLastError());
  1185. return;
  1186. }
  1187. for (i = 0; i < ARRAY_SIZE(root_devices); ++i)
  1188. {
  1189. if (!SetupDiCreateDeviceInfoA( set, root_devices[i].name, &GUID_NULL, NULL, NULL, 0, &device))
  1190. {
  1191. if (GetLastError() != ERROR_DEVINST_ALREADY_EXISTS)
  1192. WINE_ERR("Failed to create device %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1193. continue;
  1194. }
  1195. if (!SetupDiSetDeviceRegistryPropertyA(set, &device, SPDRP_HARDWAREID,
  1196. (const BYTE *)root_devices[i].hardware_id, (strlen(root_devices[i].hardware_id) + 2) * sizeof(WCHAR)))
  1197. {
  1198. WINE_ERR("Failed to set hardware id for %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1199. continue;
  1200. }
  1201. if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, set, &device))
  1202. {
  1203. WINE_ERR("Failed to register device %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1204. continue;
  1205. }
  1206. if (!UpdateDriverForPlugAndPlayDevicesA(NULL, root_devices[i].hardware_id, root_devices[i].infpath, 0, NULL))
  1207. WINE_ERR("Failed to install drivers for %s, error %#x.\n", debugstr_a(root_devices[i].name), GetLastError());
  1208. }
  1209. SetupDiDestroyDeviceInfoList(set);
  1210. }
  1211. static void update_user_profile(void)
  1212. {
  1213. char token_buf[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD) * SID_MAX_SUB_AUTHORITIES];
  1214. HANDLE token;
  1215. WCHAR profile[MAX_PATH], *sid;
  1216. DWORD size;
  1217. HKEY hkey, profile_hkey;
  1218. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token))
  1219. return;
  1220. size = sizeof(token_buf);
  1221. GetTokenInformation(token, TokenUser, token_buf, size, &size);
  1222. CloseHandle(token);
  1223. ConvertSidToStringSidW(((TOKEN_USER *)token_buf)->User.Sid, &sid);
  1224. if (!RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
  1225. 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL))
  1226. {
  1227. if (!RegCreateKeyExW(hkey, sid, 0, NULL, 0,
  1228. KEY_ALL_ACCESS, NULL, &profile_hkey, NULL))
  1229. {
  1230. DWORD flags = 0;
  1231. if (SHGetSpecialFolderPathW(NULL, profile, CSIDL_PROFILE, TRUE))
  1232. set_reg_value(profile_hkey, L"ProfileImagePath", profile);
  1233. RegSetValueExW( profile_hkey, L"Flags", 0, REG_DWORD, (const BYTE *)&flags, sizeof(flags) );
  1234. RegCloseKey(profile_hkey);
  1235. }
  1236. RegCloseKey(hkey);
  1237. }
  1238. LocalFree(sid);
  1239. }
  1240. /* execute rundll32 on the wine.inf file if necessary */
  1241. static void update_wineprefix( BOOL force )
  1242. {
  1243. const WCHAR *config_dir = _wgetenv( L"WINECONFIGDIR" );
  1244. WCHAR *inf_path = get_wine_inf_path();
  1245. int fd;
  1246. struct stat st;
  1247. if (!inf_path)
  1248. {
  1249. WINE_MESSAGE( "wine: failed to update %s, wine.inf not found\n", debugstr_w( config_dir ));
  1250. return;
  1251. }
  1252. if ((fd = _wopen( inf_path, O_RDONLY )) == -1)
  1253. {
  1254. WINE_MESSAGE( "wine: failed to update %s with %s: %s\n",
  1255. debugstr_w(config_dir), debugstr_w(inf_path), strerror(errno) );
  1256. goto done;
  1257. }
  1258. fstat( fd, &st );
  1259. close( fd );
  1260. if (update_timestamp( config_dir, st.st_mtime ) || force)
  1261. {
  1262. HANDLE process;
  1263. DWORD count = 0;
  1264. if ((process = start_rundll32( inf_path, FALSE )))
  1265. {
  1266. HWND hwnd = show_wait_window();
  1267. for (;;)
  1268. {
  1269. MSG msg;
  1270. DWORD res = MsgWaitForMultipleObjects( 1, &process, FALSE, INFINITE, QS_ALLINPUT );
  1271. if (res == WAIT_OBJECT_0)
  1272. {
  1273. CloseHandle( process );
  1274. if (count++ || !(process = start_rundll32( inf_path, TRUE ))) break;
  1275. }
  1276. else while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
  1277. }
  1278. DestroyWindow( hwnd );
  1279. }
  1280. install_root_pnp_devices();
  1281. update_user_profile();
  1282. WINE_MESSAGE( "wine: configuration in %s has been updated.\n", debugstr_w(prettyprint_configdir()) );
  1283. }
  1284. done:
  1285. HeapFree( GetProcessHeap(), 0, inf_path );
  1286. }
  1287. /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
  1288. * shell links here to restart themselves after boot. */
  1289. static BOOL ProcessStartupItems(void)
  1290. {
  1291. BOOL ret = FALSE;
  1292. HRESULT hr;
  1293. IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
  1294. LPITEMIDLIST pidlStartup = NULL, pidlItem;
  1295. ULONG NumPIDLs;
  1296. IEnumIDList *iEnumList = NULL;
  1297. STRRET strret;
  1298. WCHAR wszCommand[MAX_PATH];
  1299. WINE_TRACE("Processing items in the StartUp folder.\n");
  1300. hr = SHGetDesktopFolder(&psfDesktop);
  1301. if (FAILED(hr))
  1302. {
  1303. WINE_ERR("Couldn't get desktop folder.\n");
  1304. goto done;
  1305. }
  1306. hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
  1307. if (FAILED(hr))
  1308. {
  1309. WINE_TRACE("Couldn't get StartUp folder location.\n");
  1310. goto done;
  1311. }
  1312. hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
  1313. if (FAILED(hr))
  1314. {
  1315. WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
  1316. goto done;
  1317. }
  1318. hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
  1319. if (FAILED(hr))
  1320. {
  1321. WINE_TRACE("Unable to enumerate StartUp objects.\n");
  1322. goto done;
  1323. }
  1324. while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
  1325. (NumPIDLs) == 1)
  1326. {
  1327. hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
  1328. if (FAILED(hr))
  1329. WINE_TRACE("Unable to get display name of enumeration item.\n");
  1330. else
  1331. {
  1332. hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
  1333. if (FAILED(hr))
  1334. WINE_TRACE("Unable to parse display name.\n");
  1335. else
  1336. {
  1337. HINSTANCE hinst;
  1338. hinst = ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL);
  1339. if (PtrToUlong(hinst) <= 32)
  1340. WINE_WARN("Error %p executing command %s.\n", hinst, wine_dbgstr_w(wszCommand));
  1341. }
  1342. }
  1343. ILFree(pidlItem);
  1344. }
  1345. /* Return success */
  1346. ret = TRUE;
  1347. done:
  1348. if (iEnumList) IEnumIDList_Release(iEnumList);
  1349. if (psfStartup) IShellFolder_Release(psfStartup);
  1350. if (pidlStartup) ILFree(pidlStartup);
  1351. return ret;
  1352. }
  1353. static void usage( int status )
  1354. {
  1355. WINE_MESSAGE( "Usage: wineboot [options]\n" );
  1356. WINE_MESSAGE( "Options;\n" );
  1357. WINE_MESSAGE( " -h,--help Display this help message\n" );
  1358. WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
  1359. WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
  1360. WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
  1361. WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
  1362. WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
  1363. WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
  1364. WINE_MESSAGE( " -u,--update Update the wineprefix directory\n" );
  1365. exit( status );
  1366. }
  1367. int __cdecl main( int argc, char *argv[] )
  1368. {
  1369. /* First, set the current directory to SystemRoot */
  1370. int i, j;
  1371. BOOL end_session, force, init, kill, restart, shutdown, update;
  1372. HANDLE event;
  1373. OBJECT_ATTRIBUTES attr;
  1374. UNICODE_STRING nameW;
  1375. BOOL is_wow64;
  1376. end_session = force = init = kill = restart = shutdown = update = FALSE;
  1377. GetWindowsDirectoryW( windowsdir, MAX_PATH );
  1378. if( !SetCurrentDirectoryW( windowsdir ) )
  1379. WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
  1380. if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
  1381. {
  1382. STARTUPINFOW si;
  1383. PROCESS_INFORMATION pi;
  1384. WCHAR filename[MAX_PATH];
  1385. void *redir;
  1386. DWORD exit_code;
  1387. memset( &si, 0, sizeof(si) );
  1388. si.cb = sizeof(si);
  1389. GetModuleFileNameW( 0, filename, MAX_PATH );
  1390. Wow64DisableWow64FsRedirection( &redir );
  1391. if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
  1392. {
  1393. WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
  1394. WaitForSingleObject( pi.hProcess, INFINITE );
  1395. GetExitCodeProcess( pi.hProcess, &exit_code );
  1396. ExitProcess( exit_code );
  1397. }
  1398. else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
  1399. Wow64RevertWow64FsRedirection( redir );
  1400. }
  1401. for (i = 1; i < argc; i++)
  1402. {
  1403. if (argv[i][0] != '-') continue;
  1404. if (argv[i][1] == '-')
  1405. {
  1406. if (!strcmp( argv[i], "--help" )) usage( 0 );
  1407. else if (!strcmp( argv[i], "--end-session" )) end_session = TRUE;
  1408. else if (!strcmp( argv[i], "--force" )) force = TRUE;
  1409. else if (!strcmp( argv[i], "--init" )) init = TRUE;
  1410. else if (!strcmp( argv[i], "--kill" )) kill = TRUE;
  1411. else if (!strcmp( argv[i], "--restart" )) restart = TRUE;
  1412. else if (!strcmp( argv[i], "--shutdown" )) shutdown = TRUE;
  1413. else if (!strcmp( argv[i], "--update" )) update = TRUE;
  1414. else usage( 1 );
  1415. continue;
  1416. }
  1417. for (j = 1; argv[i][j]; j++)
  1418. {
  1419. switch (argv[i][j])
  1420. {
  1421. case 'e': end_session = TRUE; break;
  1422. case 'f': force = TRUE; break;
  1423. case 'i': init = TRUE; break;
  1424. case 'k': kill = TRUE; break;
  1425. case 'r': restart = TRUE; break;
  1426. case 's': shutdown = TRUE; break;
  1427. case 'u': update = TRUE; break;
  1428. case 'h': usage(0); break;
  1429. default: usage(1); break;
  1430. }
  1431. }
  1432. }
  1433. if (end_session)
  1434. {
  1435. if (kill)
  1436. {
  1437. if (!shutdown_all_desktops( force )) return 1;
  1438. }
  1439. else if (!shutdown_close_windows( force )) return 1;
  1440. }
  1441. if (kill) kill_processes( shutdown );
  1442. if (shutdown) return 0;
  1443. /* create event to be inherited by services.exe */
  1444. InitializeObjectAttributes( &attr, &nameW, OBJ_OPENIF | OBJ_INHERIT, 0, NULL );
  1445. RtlInitUnicodeString( &nameW, L"\\KernelObjects\\__wineboot_event" );
  1446. NtCreateEvent( &event, EVENT_ALL_ACCESS, &attr, NotificationEvent, 0 );
  1447. ResetEvent( event ); /* in case this is a restart */
  1448. create_user_shared_data();
  1449. create_hardware_registry_keys();
  1450. create_dynamic_registry_keys();
  1451. create_environment_registry_keys();
  1452. create_computer_name_keys();
  1453. wininit();
  1454. pendingRename();
  1455. ProcessWindowsFileProtection();
  1456. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunServicesOnce", TRUE, FALSE );
  1457. if (init || (kill && !restart))
  1458. {
  1459. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunServices", FALSE, FALSE );
  1460. start_services_process();
  1461. }
  1462. if (init || update) update_wineprefix( update );
  1463. create_volatile_environment_registry_key();
  1464. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE );
  1465. if (!init && !restart)
  1466. {
  1467. ProcessRunKeys( HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE );
  1468. ProcessRunKeys( HKEY_CURRENT_USER, L"Run", FALSE, FALSE );
  1469. ProcessStartupItems();
  1470. }
  1471. WINE_TRACE("Operation done\n");
  1472. SetEvent( event );
  1473. return 0;
  1474. }