Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5079 lines
139 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. cmsysini.c
  5. Abstract:
  6. This module contains init support for the configuration manager,
  7. particularly the registry.
  8. Author:
  9. Bryan M. Willman (bryanwi) 26-Aug-1991
  10. Revision History:
  11. Elliot Shmukler (t-ellios) 24-Aug-1998
  12. Added CmpSaveBootControlSet & CmpDeleteCloneTree in order to
  13. perform some of the LKG work that has been moved into the kernel.
  14. Modified system initialization to permit operation and LKG control
  15. set saves without a CurrentControlSet clone.
  16. --*/
  17. #include "cmp.h"
  18. #include "arc.h"
  19. #pragma hdrstop
  20. #include "arccodes.h"
  21. typedef struct _VERSION_DATA_KEY
  22. {
  23. PWCHAR InitialKeyPath;
  24. PWCHAR AdditionalKeyPath;
  25. } VERSION_DATA_KEY, *PVERSION_DATA_KEY;
  26. VERSION_DATA_KEY VersionDataKeys[] =
  27. {
  28. { L"\\REGISTRY\\MACHINE\\SOFTWARE\\Microsoft", NULL },
  29. #if defined(_WIN64)
  30. { L"\\REGISTRY\\MACHINE\\SOFTWARE\\Wow6432Node", L"Microsoft" },
  31. #endif
  32. { NULL, NULL }
  33. } ;
  34. //
  35. // paths
  36. //
  37. #define INIT_REGISTRY_MASTERPATH L"\\REGISTRY\\"
  38. extern PKPROCESS CmpSystemProcess;
  39. extern ERESOURCE CmpRegistryLock;
  40. extern ERESOURCE CmpKcbLock;
  41. extern FAST_MUTEX CmpPostLock;
  42. extern FAST_MUTEX CmpWriteLock;
  43. extern BOOLEAN CmFirstTime;
  44. extern BOOLEAN HvShutdownComplete;
  45. //
  46. // List of MACHINE hives to load.
  47. //
  48. extern HIVE_LIST_ENTRY CmpMachineHiveList[];
  49. extern UCHAR SystemHiveFullPathBuffer[];
  50. extern UNICODE_STRING SystemHiveFullPathName;
  51. #define SYSTEM_PATH L"\\registry\\machine\\system"
  52. //
  53. // special keys for backwards compatibility with 1.0
  54. //
  55. #define HKEY_PERFORMANCE_TEXT (( HANDLE ) (ULONG_PTR)((LONG)0x80000050) )
  56. #define HKEY_PERFORMANCE_NLSTEXT (( HANDLE ) (ULONG_PTR)((LONG)0x80000060) )
  57. extern UNICODE_STRING CmpSystemFileName;
  58. extern UNICODE_STRING CmSymbolicLinkValueName;
  59. extern UNICODE_STRING CmpLoadOptions; // sys options from FW or boot.ini
  60. extern PWCHAR CmpProcessorControl;
  61. extern PWCHAR CmpControlSessionManager;
  62. //
  63. //
  64. // Object type definition support.
  65. //
  66. // Key objects (CmpKeyObjectType) represent open instances of keys in the
  67. // registry. They do not have object names, rather, their names are
  68. // defined by the registry backing store.
  69. //
  70. //
  71. // Master Hive
  72. //
  73. // The KEY_NODEs for \REGISTRY, \REGISTRY\MACHINE, and \REGISTRY\USER
  74. // are stored in a small memory only hive called the Master Hive.
  75. // All other hives have link nodes in this hive which point to them.
  76. //
  77. extern PCMHIVE CmpMasterHive;
  78. extern BOOLEAN CmpNoMasterCreates; // Init False, Set TRUE after we're done to
  79. // prevent random creates in the
  80. // master hive, which is not backed
  81. // by a file.
  82. extern LIST_ENTRY CmpHiveListHead; // List of CMHIVEs
  83. //
  84. // Addresses of object type descriptors:
  85. //
  86. extern POBJECT_TYPE CmpKeyObjectType;
  87. //
  88. // Define attributes that Key objects are not allowed to have.
  89. //
  90. #define CMP_KEY_INVALID_ATTRIBUTES (OBJ_EXCLUSIVE |\
  91. OBJ_PERMANENT)
  92. //
  93. // Global control values
  94. //
  95. //
  96. // Write-Control:
  97. // CmpNoWrite is initially true. When set this way write and flush
  98. // do nothing, simply returning success. When cleared to FALSE, I/O
  99. // is enabled. This change is made after the I/O system is started
  100. // AND autocheck (chkdsk) has done its thing.
  101. //
  102. extern BOOLEAN CmpNoWrite;
  103. //
  104. // Buffer used for quick-stash transfers in CmSetValueKey
  105. //
  106. extern PUCHAR CmpStashBuffer;
  107. extern ULONG CmpStashBufferSize;
  108. //
  109. // set to true if disk full when trying to save the changes made between system hive loading and registry initalization
  110. //
  111. extern BOOLEAN CmpCannotWriteConfiguration;
  112. //
  113. // Global "constants"
  114. //
  115. extern const UNICODE_STRING nullclass;
  116. extern BOOLEAN CmpTrackHiveClose;
  117. //
  118. // Private prototypes
  119. //
  120. VOID
  121. CmpCreatePredefined(
  122. IN HANDLE Root,
  123. IN PWSTR KeyName,
  124. IN HANDLE PredefinedHandle
  125. );
  126. VOID
  127. CmpCreatePerfKeys(
  128. VOID
  129. );
  130. BOOLEAN
  131. CmpLinkKeyToHive(
  132. PWSTR KeyPath,
  133. PWSTR HivePath
  134. );
  135. NTSTATUS
  136. CmpCreateControlSet(
  137. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  138. );
  139. NTSTATUS
  140. CmpCloneControlSet(
  141. VOID
  142. );
  143. NTSTATUS
  144. CmpCreateObjectTypes(
  145. VOID
  146. );
  147. BOOLEAN
  148. CmpCreateRegistryRoot(
  149. VOID
  150. );
  151. BOOLEAN
  152. CmpCreateRootNode(
  153. IN PHHIVE Hive,
  154. IN PWSTR Name,
  155. OUT PHCELL_INDEX RootCellIndex
  156. );
  157. VOID
  158. CmpFreeDriverList(
  159. IN PHHIVE Hive,
  160. IN PLIST_ENTRY DriverList
  161. );
  162. VOID
  163. CmpInitializeHiveList(
  164. VOID
  165. );
  166. BOOLEAN
  167. CmpInitializeSystemHive(
  168. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  169. );
  170. NTSTATUS
  171. CmpInterlockedFunction (
  172. PWCHAR RegistryValueKey,
  173. VOID (*InterlockedFunction)(VOID)
  174. );
  175. VOID
  176. CmpConfigureProcessors (
  177. VOID
  178. );
  179. #if i386
  180. VOID
  181. KeOptimizeProcessorControlState (
  182. VOID
  183. );
  184. #endif
  185. NTSTATUS
  186. CmpAddDockingInfo (
  187. IN HANDLE Key,
  188. IN PROFILE_PARAMETER_BLOCK * ProfileBlock
  189. );
  190. NTSTATUS
  191. CmpAddAliasEntry (
  192. IN HANDLE IDConfigDB,
  193. IN PROFILE_PARAMETER_BLOCK * ProfileBlock,
  194. IN ULONG ProfileNumber
  195. );
  196. NTSTATUS CmpDeleteCloneTree(VOID);
  197. VOID
  198. CmpDiskFullWarning(
  199. VOID
  200. );
  201. VOID
  202. CmpLoadHiveThread(
  203. IN PVOID StartContext
  204. );
  205. NTSTATUS
  206. CmpSetupPrivateWrite(
  207. PCMHIVE CmHive
  208. );
  209. NTSTATUS
  210. CmpSetSystemValues(
  211. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  212. );
  213. NTSTATUS
  214. CmpSetNetworkValue(
  215. IN PNETWORK_LOADER_BLOCK NetworkLoaderBlock
  216. );
  217. VOID
  218. CmpInitCallback(VOID);
  219. VOID
  220. CmpMarkCurrentValueDirty(
  221. IN PHHIVE SystemHive,
  222. IN HCELL_INDEX RootCell
  223. );
  224. #ifdef ALLOC_PRAGMA
  225. NTSTATUS
  226. CmpHwprofileDefaultSelect (
  227. IN PCM_HARDWARE_PROFILE_LIST ProfileList,
  228. OUT PULONG ProfileIndexToUse,
  229. IN PVOID Context
  230. );
  231. #pragma alloc_text(INIT,CmInitSystem1)
  232. #pragma alloc_text(INIT,CmIsLastKnownGoodBoot)
  233. #pragma alloc_text(INIT,CmpHwprofileDefaultSelect)
  234. #pragma alloc_text(INIT,CmpCreateControlSet)
  235. #pragma alloc_text(INIT,CmpCloneControlSet)
  236. #pragma alloc_text(INIT,CmpCreateObjectTypes)
  237. #pragma alloc_text(INIT,CmpCreateRegistryRoot)
  238. #pragma alloc_text(INIT,CmpCreateRootNode)
  239. #pragma alloc_text(INIT,CmpInitializeSystemHive)
  240. #pragma alloc_text(INIT,CmGetSystemDriverList)
  241. #pragma alloc_text(INIT,CmpFreeDriverList)
  242. #pragma alloc_text(INIT,CmpSetSystemValues)
  243. #pragma alloc_text(INIT,CmpSetNetworkValue)
  244. #pragma alloc_text(PAGE,CmpInitializeHiveList)
  245. #pragma alloc_text(PAGE,CmpLinkHiveToMaster)
  246. #pragma alloc_text(PAGE,CmpSetVersionData)
  247. #pragma alloc_text(PAGE,CmBootLastKnownGood)
  248. #pragma alloc_text(PAGE,CmpSaveBootControlSet)
  249. #pragma alloc_text(PAGE,CmpInitHiveFromFile)
  250. #pragma alloc_text(PAGE,CmpLinkKeyToHive)
  251. #pragma alloc_text(PAGE,CmpCreatePredefined)
  252. #pragma alloc_text(PAGE,CmpCreatePerfKeys)
  253. #pragma alloc_text(PAGE,CmpInterlockedFunction)
  254. #pragma alloc_text(PAGE,CmpConfigureProcessors)
  255. #pragma alloc_text(INIT,CmpAddDockingInfo)
  256. #pragma alloc_text(INIT,CmpAddAliasEntry)
  257. #pragma alloc_text(PAGE,CmpDeleteCloneTree)
  258. #pragma alloc_text(PAGE,CmpSetupPrivateWrite)
  259. #pragma alloc_text(PAGE,CmpLoadHiveThread)
  260. #pragma alloc_text(PAGE,CmpMarkCurrentValueDirty)
  261. #endif
  262. BOOLEAN
  263. CmInitSystem1(
  264. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  265. )
  266. /*++
  267. Routine Description:
  268. This function is called as part of phase1 init, after the object
  269. manager has been inited, but before IoInit. It's purpose is to
  270. set up basic registry object operations, and transform data
  271. captured during boot into registry format (whether it was read
  272. from the SYSTEM hive file by the osloader or computed by recognizers.)
  273. After this call, Nt*Key calls work, but only part of the name
  274. space is available and any changes written must be held in
  275. memory.
  276. CmpMachineHiveList entries marked CM_PHASE_1 are available
  277. after return from this call, but writes must be held in memory.
  278. This function will:
  279. 1. Create the regisrty worker/lazy-write thread
  280. 2. Create the registry key object type
  281. 4. Create the master hive
  282. 5. Create the \REGISTRY node
  283. 6. Create a KEY object that refers to \REGISTRY
  284. 7. Create \REGISTRY\MACHINE node
  285. 8. Create the SYSTEM hive, fill in with data from loader
  286. 9. Create the HARDWARE hive, fill in with data from loader
  287. 10. Create:
  288. \REGISTRY\MACHINE\SYSTEM
  289. \REGISTRY\MACHINE\HARDWARE
  290. Both of which will be link nodes in the master hive.
  291. NOTE: We do NOT free allocated pool in failure case. This is because
  292. our caller is going to bugcheck anyway, and having the memory
  293. object to look at is useful.
  294. Arguments:
  295. LoaderBlock - supplies the LoaderBlock passed in from the OSLoader.
  296. By looking through the memory descriptor list we can find the
  297. SYSTEM hive which the OSLoader has placed in memory for us.
  298. Return Value:
  299. TRUE if all operations were successful, false if any failed.
  300. Bugchecks when something went wrong (CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,.....)
  301. --*/
  302. {
  303. HANDLE key1;
  304. OBJECT_ATTRIBUTES ObjectAttributes;
  305. NTSTATUS status;
  306. PSECURITY_DESCRIPTOR SecurityDescriptor;
  307. PCMHIVE HardwareHive;
  308. PCMHIVE CloneHive;
  309. //
  310. // Set the mini NT flag if we are booting into Mini NT
  311. // environment
  312. //
  313. if (InitIsWinPEMode) {
  314. CmpMiniNTBoot = InitIsWinPEMode;
  315. //
  316. // On Remote boot client share the system hives
  317. //
  318. // NOTE : We can't assume exclusive access to WinPE
  319. // remote boot clients. We don't flush anything to
  320. // system hives in WinPE. All the system hives are
  321. // loaded in memory in scratch mode
  322. //
  323. CmpShareSystemHives = TRUE;
  324. }
  325. PAGED_CODE();
  326. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmInitSystem1\n"));
  327. //
  328. // Initialize Names of all registry paths.
  329. // This simply initializes unicode strings so we don't have to bother
  330. // with it later. This can not fail.
  331. //
  332. CmpInitializeRegistryNames();
  333. //
  334. // Compute registry global quota
  335. //
  336. CmpComputeGlobalQuotaAllowed();
  337. //
  338. // Initialize the hive list head
  339. //
  340. InitializeListHead(&CmpHiveListHead);
  341. ExInitializeFastMutex(&CmpHiveListHeadLock);
  342. //
  343. // Initialize the global registry resource
  344. //
  345. ExInitializeResourceLite(&CmpRegistryLock);
  346. //
  347. // Initialize the KCB tree mutex
  348. //
  349. ExInitializeResourceLite(&CmpKcbLock);
  350. //
  351. // Initialize the PostList mutex
  352. //
  353. ExInitializeFastMutex(&CmpPostLock);
  354. //
  355. // Initialize the Stash Buffer mutex
  356. //
  357. ExInitializeFastMutex(&CmpStashBufferLock);
  358. //
  359. // Initialize the Write mutex
  360. //
  361. ExInitializeFastMutex(&CmpWriteLock);
  362. //
  363. // Initialize the cache
  364. //
  365. CmpInitializeCache ();
  366. //
  367. // Initialize private allocator
  368. //
  369. CmpInitCmPrivateAlloc();
  370. //
  371. // Initialize callback module
  372. //
  373. CmpInitCallback();
  374. //
  375. // Save the current process to allow us to attach to it later.
  376. //
  377. CmpSystemProcess = &PsGetCurrentProcess()->Pcb;
  378. CmpLockRegistryExclusive();
  379. //
  380. // Create the Key object type.
  381. //
  382. status = CmpCreateObjectTypes();
  383. if (!NT_SUCCESS(status) ) {
  384. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmInitSystem1: CmpCreateObjectTypes failed\n"));
  385. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,1,status,0); // could not registrate with object manager
  386. return FALSE;
  387. }
  388. //
  389. // Create the master hive and initialize it.
  390. //
  391. status = CmpInitializeHive(&CmpMasterHive,
  392. HINIT_CREATE,
  393. HIVE_VOLATILE,
  394. HFILE_TYPE_PRIMARY, // i.e. no logging, no alterate
  395. NULL,
  396. NULL,
  397. NULL,
  398. NULL,
  399. NULL,
  400. 0);
  401. if (!NT_SUCCESS(status)) {
  402. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmInitSystem1: CmpInitializeHive(master) failed\n"));
  403. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,2,status,0); // could not initialize master hive
  404. return (FALSE);
  405. }
  406. //
  407. // try to allocate a stash buffer. if we can't get 1 page this
  408. // early on, we're in deep trouble, so punt.
  409. //
  410. CmpStashBuffer = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE,CM_STASHBUFFER_TAG);
  411. if (CmpStashBuffer == NULL) {
  412. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,3,0,0); // odds against this are huge
  413. return FALSE;
  414. }
  415. CmpStashBufferSize = PAGE_SIZE;
  416. //
  417. // Create the \REGISTRY node
  418. //
  419. if (!CmpCreateRegistryRoot()) {
  420. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmInitSystem1: CmpCreateRegistryRoot failed\n"));
  421. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,4,0,0); // could not create root of the registry
  422. return FALSE;
  423. }
  424. //
  425. // --- 6. Create \REGISTRY\MACHINE and \REGISTRY\USER nodes ---
  426. //
  427. //
  428. // Get default security descriptor for the nodes we will create.
  429. //
  430. SecurityDescriptor = CmpHiveRootSecurityDescriptor();
  431. InitializeObjectAttributes(
  432. &ObjectAttributes,
  433. &CmRegistryMachineName,
  434. OBJ_CASE_INSENSITIVE,
  435. (HANDLE)NULL,
  436. SecurityDescriptor
  437. );
  438. if (!NT_SUCCESS(status = NtCreateKey(
  439. &key1,
  440. KEY_READ | KEY_WRITE,
  441. &ObjectAttributes,
  442. 0,
  443. (PUNICODE_STRING)&nullclass,
  444. 0,
  445. NULL
  446. )))
  447. {
  448. ExFreePool(SecurityDescriptor);
  449. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmInitSystem1: NtCreateKey(MACHINE) failed\n"));
  450. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,5,status,0); // could not create HKLM
  451. return FALSE;
  452. }
  453. NtClose(key1);
  454. InitializeObjectAttributes(
  455. &ObjectAttributes,
  456. &CmRegistryUserName,
  457. OBJ_CASE_INSENSITIVE,
  458. (HANDLE)NULL,
  459. SecurityDescriptor
  460. );
  461. if (!NT_SUCCESS(status = NtCreateKey(
  462. &key1,
  463. KEY_READ | KEY_WRITE,
  464. &ObjectAttributes,
  465. 0,
  466. (PUNICODE_STRING)&nullclass,
  467. 0,
  468. NULL
  469. )))
  470. {
  471. ExFreePool(SecurityDescriptor);
  472. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmInitSystem1: NtCreateKey(USER) failed\n"));
  473. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,6,status,0); // could not create HKUSER
  474. return FALSE;
  475. }
  476. NtClose(key1);
  477. //
  478. // --- 7. Create the SYSTEM hive, fill in with data from loader ---
  479. //
  480. if (!CmpInitializeSystemHive(LoaderBlock)) {
  481. ExFreePool(SecurityDescriptor);
  482. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitSystem1: "));
  483. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"Hive allocation failure for SYSTEM\n"));
  484. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,7,0,0); // could not create SystemHive
  485. return(FALSE);
  486. }
  487. //
  488. // Create the symbolic link \Registry\Machine\System\CurrentControlSet
  489. //
  490. status = CmpCreateControlSet(LoaderBlock);
  491. if (!NT_SUCCESS(status)) {
  492. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,8,status,0); // could not create CurrentControlSet
  493. return(FALSE);
  494. }
  495. //
  496. // Handle the copying of the CurrentControlSet to a Clone volatile
  497. // hive (but only if we really want to have a clone)
  498. //
  499. #if CLONE_CONTROL_SET
  500. //
  501. // Create the Clone temporary hive, link it into the master hive,
  502. // and make a symbolic link to it.
  503. //
  504. status = CmpInitializeHive(&CloneHive,
  505. HINIT_CREATE,
  506. HIVE_VOLATILE,
  507. HFILE_TYPE_PRIMARY,
  508. NULL,
  509. NULL,
  510. NULL,
  511. NULL,
  512. NULL,
  513. 0);
  514. if (!NT_SUCCESS(status)) {
  515. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitSystem1: "));
  516. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"Could not initialize CLONE hive\n"));
  517. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,9,status,0); // could not initialize clone hive
  518. return(FALSE);
  519. }
  520. status = CmpLinkHiveToMaster(
  521. &CmRegistrySystemCloneName,
  522. NULL,
  523. CloneHive,
  524. TRUE,
  525. SecurityDescriptor
  526. );
  527. if ( status != STATUS_SUCCESS)
  528. {
  529. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmInitSystem1: CmpLinkHiveToMaster(Clone) failed\n"));
  530. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,10,status,0); // could not link clone hive to master hive
  531. return FALSE;
  532. }
  533. CmpAddToHiveFileList(CloneHive);
  534. CmpMachineHiveList[CLONE_HIVE_INDEX].CmHive = CloneHive;
  535. CmpLinkKeyToHive(
  536. L"\\Registry\\Machine\\System\\Clone",
  537. L"\\Registry\\Machine\\CLONE\\CLONE"
  538. );
  539. //
  540. // Clone the current control set for the service controller
  541. //
  542. status = CmpCloneControlSet();
  543. //
  544. // If this didn't work, it's bad, but not bad enough to fail the boot
  545. //
  546. ASSERT(NT_SUCCESS(status));
  547. #endif
  548. //
  549. // --- 8. Create the HARDWARE hive, fill in with data from loader ---
  550. //
  551. status = CmpInitializeHive(&HardwareHive,
  552. HINIT_CREATE,
  553. HIVE_VOLATILE,
  554. HFILE_TYPE_PRIMARY, // i.e. no log, no alternate
  555. NULL,
  556. NULL,
  557. NULL,
  558. NULL,
  559. NULL,
  560. 0);
  561. if (!NT_SUCCESS(status)) {
  562. ExFreePool(SecurityDescriptor);
  563. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitSystem1: "));
  564. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"Could not initialize HARDWARE hive\n"));
  565. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,11,status,0); // could not initialize hardware hive
  566. return(FALSE);
  567. }
  568. //
  569. // Allocate the root node
  570. //
  571. status = CmpLinkHiveToMaster(
  572. &CmRegistryMachineHardwareName,
  573. NULL,
  574. HardwareHive,
  575. TRUE,
  576. SecurityDescriptor
  577. );
  578. if ( status != STATUS_SUCCESS )
  579. {
  580. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmInitSystem1: CmpLinkHiveToMaster(Hardware) failed\n"));
  581. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,12,status,0); // could not link hardware hive to master hive
  582. return FALSE;
  583. }
  584. CmpAddToHiveFileList(HardwareHive);
  585. ExFreePool(SecurityDescriptor);
  586. CmpMachineHiveList[0].CmHive = HardwareHive;
  587. //
  588. // put loader configuration tree data to our hardware registry.
  589. //
  590. status = CmpInitializeHardwareConfiguration(LoaderBlock);
  591. if (!NT_SUCCESS(status)) {
  592. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,13,status,0); // could not initialize hardware configuration
  593. return(FALSE);
  594. }
  595. CmpNoMasterCreates = TRUE;
  596. CmpUnlockRegistry();
  597. //
  598. // put machine dependant configuration data to our hardware registry.
  599. //
  600. status = CmpInitializeMachineDependentConfiguration(LoaderBlock);
  601. if (!NT_SUCCESS(status)) {
  602. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,14,status,0); // could not open CurrentControlSet\\Control
  603. #if defined(_CM_LDR_)
  604. return(FALSE);
  605. #endif
  606. }
  607. //
  608. // Write system start options to registry
  609. //
  610. status = CmpSetSystemValues(LoaderBlock);
  611. if (!NT_SUCCESS(status)) {
  612. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,15,status,0);
  613. #if defined(_CM_LDR_)
  614. return(FALSE);
  615. #endif
  616. }
  617. ExFreePool(CmpLoadOptions.Buffer);
  618. #if !defined(_IA64_)
  619. // chuckl 16-Apr-2002
  620. // Turned this off temporarily for IA64. It's only needed for remote boot,
  621. // which we're not doing on IA64 at the moment. And there seems to be a
  622. // problem with getting the IA64 update.exe to put a new ia64ldr.efi in
  623. // place. We need the new ia64ldr.efi in order for the NetworkLoaderBlock
  624. // field to valid -- it should be NULL for non-remote boot, but with the
  625. // old loader, we get an uninitialized value. So without the new loader,
  626. // we bugcheck in CmpSetNetworkValue(). Therefore, this code is turned
  627. // off for IA64 until both of the following are true: 1) we need to remote
  628. // boot IA64; and 2) update.exe knows how to put a new ia64ldr.efi in
  629. // place.
  630. //
  631. // Write Network LoaderBlock values to registry
  632. //
  633. if ( (LoaderBlock->Extension->Size >=
  634. RTL_SIZEOF_THROUGH_FIELD(LOADER_PARAMETER_EXTENSION, NetworkLoaderBlock)) &&
  635. (LoaderBlock->Extension->NetworkLoaderBlock != NULL) ) {
  636. status = CmpSetNetworkValue(LoaderBlock->Extension->NetworkLoaderBlock);
  637. if (!NT_SUCCESS(status)) {
  638. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM1,16,status,0);
  639. #if defined(_CM_LDR_)
  640. return(FALSE);
  641. #endif
  642. }
  643. }
  644. #endif
  645. return TRUE;
  646. }
  647. //
  648. // All paralel threads will get this shared, and CmpInitializeHiveList will wait for it exclusive
  649. //
  650. KEVENT CmpLoadWorkerEvent;
  651. ULONG CmpLoadWorkerIncrement = 0;
  652. KEVENT CmpLoadWorkerDebugEvent;
  653. VOID
  654. CmpInitializeHiveList(
  655. VOID
  656. )
  657. /*++
  658. Routine Description:
  659. This function is called to map hive files to hives. It both
  660. maps existing hives to files, and creates new hives from files.
  661. It operates on files in "\SYSTEMROOT\CONFIG".
  662. NOTE: MUST run in the context of the process that the CmpWorker
  663. thread runs in. Caller is expected to arrange this.
  664. NOTE: Will bugcheck on failure.
  665. Arguments:
  666. Return Value:
  667. NONE.
  668. --*/
  669. {
  670. #define MAX_NAME 128
  671. HANDLE Thread;
  672. NTSTATUS Status;
  673. UCHAR FileBuffer[MAX_NAME];
  674. UCHAR RegBuffer[MAX_NAME];
  675. UNICODE_STRING TempName;
  676. UNICODE_STRING FileName;
  677. UNICODE_STRING RegName;
  678. USHORT FileStart;
  679. USHORT RegStart;
  680. ULONG i;
  681. PSECURITY_DESCRIPTOR SecurityDescriptor;
  682. #ifdef CM_PERF_ISSUES
  683. LARGE_INTEGER StartSystemTime;
  684. LARGE_INTEGER EndSystemTime;
  685. LARGE_INTEGER deltaTime;
  686. #endif //CM_PERF_ISSUES
  687. PAGED_CODE();
  688. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmpInitializeHiveList\n"));
  689. #ifdef CM_PERF_ISSUES
  690. KeQuerySystemTime(&StartSystemTime);
  691. #endif //CM_PERF_ISSUES
  692. CmpNoWrite = FALSE;
  693. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  694. FileName.MaximumLength = MAX_NAME;
  695. FileName.Length = 0;
  696. FileName.Buffer = (PWSTR)&(FileBuffer[0]);
  697. RegName.MaximumLength = MAX_NAME;
  698. RegName.Length = 0;
  699. RegName.Buffer = (PWSTR)&(RegBuffer[0]);
  700. RtlInitUnicodeString(
  701. &TempName,
  702. INIT_SYSTEMROOT_HIVEPATH
  703. );
  704. RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
  705. FileStart = FileName.Length;
  706. RtlInitUnicodeString(
  707. &TempName,
  708. INIT_REGISTRY_MASTERPATH
  709. );
  710. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  711. RegStart = RegName.Length;
  712. //
  713. // Initialize the syncronization event
  714. //
  715. KeInitializeEvent (&CmpLoadWorkerEvent, SynchronizationEvent, FALSE);
  716. KeInitializeEvent (&CmpLoadWorkerDebugEvent, SynchronizationEvent, FALSE);
  717. CmpSpecialBootCondition = TRUE;
  718. SecurityDescriptor = CmpHiveRootSecurityDescriptor();
  719. if (CmpShareSystemHives) {
  720. for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++) {
  721. if (CmpMachineHiveList[i].Name) {
  722. CmpMachineHiveList[i].Flags |= HIVE_VOLATILE;
  723. }
  724. }
  725. }
  726. for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++) {
  727. ASSERT( CmpMachineHiveList[i].Name != NULL );
  728. //
  729. // just spawn the Threads to load the hives in paralel
  730. //
  731. Status = PsCreateSystemThread(
  732. &Thread,
  733. THREAD_ALL_ACCESS,
  734. NULL,
  735. 0,
  736. NULL,
  737. CmpLoadHiveThread,
  738. (PVOID)(ULONG_PTR)(ULONG)i
  739. );
  740. if (NT_SUCCESS(Status)) {
  741. ZwClose(Thread);
  742. } else {
  743. //
  744. // cannot spawn thread; Fatal error
  745. //
  746. CM_BUGCHECK(BAD_SYSTEM_CONFIG_INFO,BAD_HIVE_LIST,3,i,Status);
  747. }
  748. }
  749. ASSERT( CmpMachineHiveList[i].Name == NULL );
  750. KeWaitForSingleObject( &CmpLoadWorkerEvent,
  751. Executive,
  752. KernelMode,
  753. FALSE,
  754. NULL );
  755. CmpSpecialBootCondition = FALSE;
  756. ASSERT( CmpLoadWorkerIncrement == CM_NUMBER_OF_MACHINE_HIVES );
  757. //
  758. // Now add all hives to the hivelist
  759. //
  760. for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++) {
  761. ASSERT( CmpMachineHiveList[i].ThreadFinished == TRUE );
  762. ASSERT( CmpMachineHiveList[i].ThreadStarted == TRUE );
  763. if (CmpMachineHiveList[i].CmHive == NULL) {
  764. ASSERT( CmpMachineHiveList[i].CmHive2 != NULL );
  765. //
  766. // Compute the name of the file, and the name to link to in
  767. // the registry.
  768. //
  769. // REGISTRY
  770. RegName.Length = RegStart;
  771. RtlInitUnicodeString(
  772. &TempName,
  773. CmpMachineHiveList[i].BaseName
  774. );
  775. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  776. // REGISTRY\MACHINE or REGISTRY\USER
  777. if (RegName.Buffer[ (RegName.Length / sizeof( WCHAR )) - 1 ] == '\\') {
  778. RtlInitUnicodeString(
  779. &TempName,
  780. CmpMachineHiveList[i].Name
  781. );
  782. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  783. }
  784. // REGISTRY\[MACHINE|USER]\HIVE
  785. // <sysroot>\config
  786. //
  787. // Link hive into master hive
  788. //
  789. Status = CmpLinkHiveToMaster(
  790. &RegName,
  791. NULL,
  792. CmpMachineHiveList[i].CmHive2,
  793. CmpMachineHiveList[i].Allocate,
  794. SecurityDescriptor
  795. );
  796. if ( Status != STATUS_SUCCESS)
  797. {
  798. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitializeHiveList: "));
  799. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpLinkHiveToMaster failed\n"));
  800. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"\ti=%d s='%ws'\n", i, CmpMachineHiveList[i]));
  801. CM_BUGCHECK(CONFIG_LIST_FAILED,BAD_CORE_HIVE,Status,i,&RegName);
  802. }
  803. if( CmpMachineHiveList[i].Allocate == TRUE ) {
  804. HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
  805. }
  806. } else {
  807. //
  808. // do nothing here as all of it has been done in separate thread.
  809. //
  810. }
  811. if( CmpMachineHiveList[i].CmHive2 != NULL ) {
  812. CmpAddToHiveFileList(CmpMachineHiveList[i].CmHive2);
  813. }
  814. } // for
  815. ExFreePool(SecurityDescriptor);
  816. //
  817. // Create symbolic link from SECURITY hive into SAM hive.
  818. //
  819. CmpLinkKeyToHive(
  820. L"\\Registry\\Machine\\Security\\SAM",
  821. L"\\Registry\\Machine\\SAM\\SAM"
  822. );
  823. //
  824. // Create symbolic link from S-1-5-18 to .Default
  825. //
  826. CmpNoMasterCreates = FALSE;
  827. CmpLinkKeyToHive(
  828. L"\\Registry\\User\\S-1-5-18",
  829. L"\\Registry\\User\\.Default"
  830. );
  831. CmpNoMasterCreates = TRUE;
  832. //
  833. // Create predefined handles.
  834. //
  835. CmpCreatePerfKeys();
  836. //
  837. // from now on we will attempt to self heal hives
  838. // we set this to true here for an eye towards longhorn where this is more useful
  839. //
  840. CmpSelfHeal = TRUE;
  841. #ifdef CM_PERF_ISSUES
  842. KeQuerySystemTime(&EndSystemTime);
  843. deltaTime.QuadPart = EndSystemTime.QuadPart - StartSystemTime.QuadPart;
  844. DbgPrint("\nCmpInitializeHiveList took %lu.%lu ms\n",(ULONG)(deltaTime.LowPart/10000),(ULONG)(deltaTime.LowPart%10000));
  845. if( deltaTime.HighPart != 0 ) {
  846. DbgPrint("deltaTime.HighPart = %lu\n",(ULONG)deltaTime.HighPart);
  847. }
  848. #endif //CM_PERF_ISSUES
  849. return;
  850. }
  851. NTSTATUS
  852. CmpCreateObjectTypes(
  853. VOID
  854. )
  855. /*++
  856. Routine Description:
  857. Create the Key object type
  858. Arguments:
  859. NONE.
  860. Return Value:
  861. Status of the ObCreateType call
  862. --*/
  863. {
  864. NTSTATUS Status;
  865. OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
  866. UNICODE_STRING TypeName;
  867. //
  868. // Structure that describes the mapping of generic access rights to object
  869. // specific access rights for registry key objects.
  870. //
  871. GENERIC_MAPPING CmpKeyMapping = {
  872. KEY_READ,
  873. KEY_WRITE,
  874. KEY_EXECUTE,
  875. KEY_ALL_ACCESS
  876. };
  877. PAGED_CODE();
  878. //
  879. // --- Create the registry key object type ---
  880. //
  881. //
  882. // Initialize string descriptor.
  883. //
  884. RtlInitUnicodeString(&TypeName, L"Key");
  885. //
  886. // Create key object type descriptor.
  887. //
  888. RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
  889. ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
  890. ObjectTypeInitializer.InvalidAttributes = CMP_KEY_INVALID_ATTRIBUTES;
  891. ObjectTypeInitializer.GenericMapping = CmpKeyMapping;
  892. ObjectTypeInitializer.ValidAccessMask = KEY_ALL_ACCESS;
  893. ObjectTypeInitializer.DefaultPagedPoolCharge = sizeof(CM_KEY_BODY);
  894. ObjectTypeInitializer.SecurityRequired = TRUE;
  895. ObjectTypeInitializer.PoolType = PagedPool;
  896. ObjectTypeInitializer.MaintainHandleCount = FALSE;
  897. ObjectTypeInitializer.UseDefaultObject = TRUE;
  898. ObjectTypeInitializer.DumpProcedure = NULL;
  899. ObjectTypeInitializer.OpenProcedure = NULL;
  900. ObjectTypeInitializer.CloseProcedure = CmpCloseKeyObject;
  901. ObjectTypeInitializer.DeleteProcedure = CmpDeleteKeyObject;
  902. ObjectTypeInitializer.ParseProcedure = CmpParseKey;
  903. ObjectTypeInitializer.SecurityProcedure = CmpSecurityMethod;
  904. ObjectTypeInitializer.QueryNameProcedure = CmpQueryKeyName;
  905. Status = ObCreateObjectType(
  906. &TypeName,
  907. &ObjectTypeInitializer,
  908. (PSECURITY_DESCRIPTOR)NULL,
  909. &CmpKeyObjectType
  910. );
  911. if (!NT_SUCCESS(Status)) {
  912. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpCreateObjectTypes: "));
  913. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"ObCreateObjectType(Key) failed %08lx\n", Status));
  914. }
  915. return Status;
  916. }
  917. BOOLEAN
  918. CmpCreateRegistryRoot(
  919. VOID
  920. )
  921. /*++
  922. Routine Description:
  923. Manually create \REGISTRY in the master hive, create a key
  924. object to refer to it, and insert the key object into
  925. the root (\) of the object space.
  926. Arguments:
  927. None
  928. Return Value:
  929. TRUE == success, FALSE == failure
  930. --*/
  931. {
  932. NTSTATUS Status;
  933. UNICODE_STRING NullString = { 0, 0, NULL };
  934. PVOID ObjectPointer;
  935. PCM_KEY_BODY Object;
  936. OBJECT_ATTRIBUTES ObjectAttributes;
  937. PCM_KEY_CONTROL_BLOCK kcb;
  938. HCELL_INDEX RootCellIndex;
  939. PSECURITY_DESCRIPTOR SecurityDescriptor;
  940. PCM_KEY_NODE TempNode;
  941. PAGED_CODE();
  942. //
  943. // --- Create hive entry for \REGISTRY ---
  944. //
  945. if (!CmpCreateRootNode(
  946. &(CmpMasterHive->Hive), L"REGISTRY", &RootCellIndex))
  947. {
  948. return FALSE;
  949. }
  950. //
  951. // --- Create a KEY object that refers to \REGISTRY ---
  952. //
  953. //
  954. // Create the object manager object
  955. //
  956. //
  957. // WARNING: \\REGISTRY is not in pool, so if anybody ever tries to
  958. // free it, we are in deep trouble. On the other hand,
  959. // this implies somebody has removed \\REGISTRY from the
  960. // root, so we're in trouble anyway.
  961. //
  962. SecurityDescriptor = CmpHiveRootSecurityDescriptor();
  963. InitializeObjectAttributes(
  964. &ObjectAttributes,
  965. &CmRegistryRootName,
  966. OBJ_CASE_INSENSITIVE,
  967. (HANDLE)NULL,
  968. SecurityDescriptor
  969. );
  970. Status = ObCreateObject(
  971. KernelMode,
  972. CmpKeyObjectType,
  973. &ObjectAttributes,
  974. UserMode,
  975. NULL, // Parse context
  976. sizeof(CM_KEY_BODY),
  977. 0,
  978. 0,
  979. (PVOID *)&Object
  980. );
  981. ExFreePool(SecurityDescriptor);
  982. if (!NT_SUCCESS(Status)) {
  983. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpCreateRegistryRoot: "));
  984. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"ObCreateObject(\\REGISTRY) failed %08lx\n", Status));
  985. return FALSE;
  986. }
  987. ASSERT( (&CmpMasterHive->Hive)->ReleaseCellRoutine == NULL );
  988. TempNode = (PCM_KEY_NODE)HvGetCell(&CmpMasterHive->Hive,RootCellIndex);
  989. if( TempNode == NULL ) {
  990. //
  991. // we couldn't map the bin containing this cell
  992. //
  993. return FALSE;
  994. }
  995. //
  996. // Create the key control block
  997. //
  998. kcb = CmpCreateKeyControlBlock(
  999. &(CmpMasterHive->Hive),
  1000. RootCellIndex,
  1001. TempNode,
  1002. NULL,
  1003. FALSE,
  1004. &CmRegistryRootName
  1005. );
  1006. if (kcb==NULL) {
  1007. return(FALSE);
  1008. }
  1009. //
  1010. // Initialize the type specific body
  1011. //
  1012. Object->Type = KEY_BODY_TYPE;
  1013. Object->KeyControlBlock = kcb;
  1014. Object->NotifyBlock = NULL;
  1015. Object->Process = PsGetCurrentProcess();
  1016. ENLIST_KEYBODY_IN_KEYBODY_LIST(Object);
  1017. //
  1018. // Put the object in the root directory
  1019. //
  1020. Status = ObInsertObject(
  1021. Object,
  1022. NULL,
  1023. (ACCESS_MASK)0,
  1024. 0,
  1025. NULL,
  1026. &CmpRegistryRootHandle
  1027. );
  1028. if (!NT_SUCCESS(Status)) {
  1029. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpCreateRegistryRoot: "));
  1030. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"ObInsertObject(\\REGISTRY) failed %08lx\n", Status));
  1031. return FALSE;
  1032. }
  1033. //
  1034. // We cannot make the root permanent because registry objects in
  1035. // general are not allowed to be. (They're stable via virtue of being
  1036. // stored in the registry, not the object manager.) But we never
  1037. // ever want the root to go away. So reference it.
  1038. //
  1039. if (! NT_SUCCESS(Status = ObReferenceObjectByHandle(
  1040. CmpRegistryRootHandle,
  1041. KEY_READ,
  1042. NULL,
  1043. KernelMode,
  1044. &ObjectPointer,
  1045. NULL
  1046. )))
  1047. {
  1048. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpCreateRegistryRoot: "));
  1049. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"ObReferenceObjectByHandle failed %08lx\n", Status));
  1050. return FALSE;
  1051. }
  1052. return TRUE;
  1053. }
  1054. BOOLEAN
  1055. CmpCreateRootNode(
  1056. IN PHHIVE Hive,
  1057. IN PWSTR Name,
  1058. OUT PHCELL_INDEX RootCellIndex
  1059. )
  1060. /*++
  1061. Routine Description:
  1062. Manually create the root node of a hive.
  1063. Arguments:
  1064. Hive - pointer to a Hive (Hv level) control structure
  1065. Name - pointer to a unicode name string
  1066. RootCellIndex - supplies pointer to a variable to recieve
  1067. the cell index of the created node.
  1068. Return Value:
  1069. TRUE == success, FALSE == failure
  1070. --*/
  1071. {
  1072. UNICODE_STRING temp;
  1073. PCELL_DATA CellData;
  1074. CM_KEY_REFERENCE Key;
  1075. LARGE_INTEGER systemtime;
  1076. PAGED_CODE();
  1077. //
  1078. // Allocate the node.
  1079. //
  1080. RtlInitUnicodeString(&temp, Name);
  1081. *RootCellIndex = HvAllocateCell(
  1082. Hive,
  1083. CmpHKeyNodeSize(Hive, &temp),
  1084. Stable,
  1085. HCELL_NIL
  1086. );
  1087. if (*RootCellIndex == HCELL_NIL) {
  1088. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpCreateRootNode: HvAllocateCell failed\n"));
  1089. return FALSE;
  1090. }
  1091. Hive->BaseBlock->RootCell = *RootCellIndex;
  1092. CellData = HvGetCell(Hive, *RootCellIndex);
  1093. if( CellData == NULL ) {
  1094. //
  1095. // we couldn't map the bin containing this cell
  1096. //
  1097. return FALSE;
  1098. }
  1099. //
  1100. // Initialize the node
  1101. //
  1102. CellData->u.KeyNode.Signature = CM_KEY_NODE_SIGNATURE;
  1103. CellData->u.KeyNode.Flags = KEY_HIVE_ENTRY | KEY_NO_DELETE;
  1104. KeQuerySystemTime(&systemtime);
  1105. CellData->u.KeyNode.LastWriteTime = systemtime;
  1106. // CellData->u.KeyNode.TitleIndex = 0;
  1107. CellData->u.KeyNode.Parent = HCELL_NIL;
  1108. CellData->u.KeyNode.SubKeyCounts[Stable] = 0;
  1109. CellData->u.KeyNode.SubKeyCounts[Volatile] = 0;
  1110. CellData->u.KeyNode.SubKeyLists[Stable] = HCELL_NIL;
  1111. CellData->u.KeyNode.SubKeyLists[Volatile] = HCELL_NIL;
  1112. CellData->u.KeyNode.ValueList.Count = 0;
  1113. CellData->u.KeyNode.ValueList.List = HCELL_NIL;
  1114. CellData->u.KeyNode.Security = HCELL_NIL;
  1115. CellData->u.KeyNode.Class = HCELL_NIL;
  1116. CellData->u.KeyNode.ClassLength = 0;
  1117. CellData->u.KeyNode.MaxValueDataLen = 0;
  1118. CellData->u.KeyNode.MaxNameLen = 0;
  1119. CellData->u.KeyNode.MaxValueNameLen = 0;
  1120. CellData->u.KeyNode.MaxClassLen = 0;
  1121. CellData->u.KeyNode.NameLength = CmpCopyName(Hive,
  1122. CellData->u.KeyNode.Name,
  1123. &temp);
  1124. if (CellData->u.KeyNode.NameLength < temp.Length) {
  1125. CellData->u.KeyNode.Flags |= KEY_COMP_NAME;
  1126. }
  1127. Key.KeyHive = Hive;
  1128. Key.KeyCell = *RootCellIndex;
  1129. HvReleaseCell(Hive, *RootCellIndex);
  1130. return TRUE;
  1131. }
  1132. NTSTATUS
  1133. CmpLinkHiveToMaster(
  1134. PUNICODE_STRING LinkName,
  1135. HANDLE RootDirectory,
  1136. PCMHIVE CmHive,
  1137. BOOLEAN Allocate,
  1138. PSECURITY_DESCRIPTOR SecurityDescriptor
  1139. )
  1140. /*++
  1141. Routine Description:
  1142. The existing, "free floating" hive CmHive describes is linked into
  1143. the name space at the node named by LinkName. The node will be created.
  1144. The hive is assumed to already have an appropriate root node.
  1145. Arguments:
  1146. LinkName - supplies a pointer to a unicode string which describes where
  1147. in the registry name space the hive is to be linked.
  1148. All components but the last must exist. The last must not.
  1149. RootDirectory - Supplies the handle the LinkName is relative to.
  1150. CmHive - pointer to a CMHIVE structure describing the hive to link in.
  1151. Allocate - TRUE indicates that the root cell is to be created
  1152. FALSE indicates the root cell already exists.
  1153. SecurityDescriptor - supplies a pointer to the security descriptor to
  1154. be placed on the hive root.
  1155. Return Value:
  1156. TRUE == success, FALSE == failure
  1157. --*/
  1158. {
  1159. OBJECT_ATTRIBUTES ObjectAttributes;
  1160. HANDLE KeyHandle;
  1161. CM_PARSE_CONTEXT ParseContext;
  1162. NTSTATUS Status;
  1163. PCM_KEY_BODY KeyBody;
  1164. PAGED_CODE();
  1165. //
  1166. // Fill in special ParseContext to indicate that we are creating
  1167. // a link node and opening or creating a root node.
  1168. //
  1169. ParseContext.TitleIndex = 0;
  1170. ParseContext.Class.Length = 0;
  1171. ParseContext.Class.MaximumLength = 0;
  1172. ParseContext.Class.Buffer = NULL;
  1173. ParseContext.CreateOptions = 0;
  1174. ParseContext.CreateLink = TRUE;
  1175. ParseContext.ChildHive.KeyHive = &CmHive->Hive;
  1176. if (Allocate) {
  1177. //
  1178. // Creating a new root node
  1179. //
  1180. ParseContext.ChildHive.KeyCell = HCELL_NIL;
  1181. } else {
  1182. //
  1183. // Opening an existing root node
  1184. //
  1185. ParseContext.ChildHive.KeyCell = CmHive->Hive.BaseBlock->RootCell;
  1186. }
  1187. //
  1188. // Create a path to the hive
  1189. //
  1190. InitializeObjectAttributes(
  1191. &ObjectAttributes,
  1192. LinkName,
  1193. OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  1194. (HANDLE)RootDirectory,
  1195. SecurityDescriptor
  1196. );
  1197. Status = ObOpenObjectByName( &ObjectAttributes,
  1198. CmpKeyObjectType,
  1199. KernelMode,
  1200. NULL,
  1201. KEY_READ | KEY_WRITE,
  1202. (PVOID)&ParseContext,
  1203. &KeyHandle );
  1204. if (!NT_SUCCESS(Status)) {
  1205. #ifdef CM_CHECK_FOR_ORPHANED_KCBS
  1206. DbgPrint("CmpLinkHiveToMaster: ObOpenObjectByName for CmHive = %p , LinkName = %.*S failed with status %lx\n",CmHive,LinkName->Length/2,LinkName->Buffer,Status);
  1207. #endif //CM_CHECK_FOR_ORPHANED_KCBS
  1208. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpLinkHiveToMaster: "));
  1209. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"ObOpenObjectByName() failed %08lx\n", Status));
  1210. //CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"\tLinkName='%ws'\n", LinkName->Buffer));
  1211. return Status;
  1212. }
  1213. //
  1214. // Report the notification event
  1215. //
  1216. Status = ObReferenceObjectByHandle(KeyHandle,
  1217. 0,
  1218. CmpKeyObjectType,
  1219. KernelMode,
  1220. (PVOID *)&KeyBody,
  1221. NULL);
  1222. ASSERT(NT_SUCCESS(Status));
  1223. if (NT_SUCCESS(Status)) {
  1224. CmpReportNotify(KeyBody->KeyControlBlock,
  1225. KeyBody->KeyControlBlock->KeyHive,
  1226. KeyBody->KeyControlBlock->KeyCell,
  1227. REG_NOTIFY_CHANGE_NAME);
  1228. ObDereferenceObject((PVOID)KeyBody);
  1229. }
  1230. ZwClose(KeyHandle);
  1231. return STATUS_SUCCESS;
  1232. }
  1233. VOID
  1234. CmpSetVersionData(
  1235. VOID
  1236. )
  1237. /*++
  1238. Routine Description:
  1239. Create \REGISTRY\MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion:
  1240. CurrentVersion = VER_PRODUCTVERSION_STR // From ntverp.h
  1241. CurrentBuildNumber = VER_PRODUCTBUILD // From ntverp.h
  1242. CurrentType = "[Multiprocessor|Uniprocessor] // From NT_UP
  1243. [Retail|Free|Checked]" // From DBG, DEVL
  1244. SystemRoot = "[c:\nt]"
  1245. BuildLab = BUILD_MACHINE_TAG // From ntos\inti.c from makefile.def
  1246. NOTE: It is not worth bugchecking over this, so if it doesn't
  1247. work, just fail.
  1248. Arguments:
  1249. Return Value:
  1250. --*/
  1251. {
  1252. ANSI_STRING AnsiString;
  1253. UNICODE_STRING NameString;
  1254. UNICODE_STRING ValueString;
  1255. HANDLE key1, key2;
  1256. UCHAR WorkString[128];
  1257. WCHAR ValueBuffer[128];
  1258. OBJECT_ATTRIBUTES ObjectAttributes;
  1259. NTSTATUS status;
  1260. PUCHAR proctype;
  1261. PUCHAR buildtype;
  1262. PVERSION_DATA_KEY VersionDataKey;
  1263. PSECURITY_DESCRIPTOR SecurityDescriptor;
  1264. PAGED_CODE();
  1265. //
  1266. // Get default security descriptor for the nodes we will create.
  1267. //
  1268. SecurityDescriptor = CmpHiveRootSecurityDescriptor();
  1269. for (VersionDataKey = VersionDataKeys; VersionDataKey->InitialKeyPath != NULL ; VersionDataKey++) {
  1270. //
  1271. // Create the key
  1272. //
  1273. RtlInitUnicodeString(
  1274. &NameString,
  1275. VersionDataKey->InitialKeyPath
  1276. );
  1277. InitializeObjectAttributes(
  1278. &ObjectAttributes,
  1279. &NameString,
  1280. OBJ_CASE_INSENSITIVE,
  1281. (HANDLE)NULL,
  1282. SecurityDescriptor
  1283. );
  1284. status = NtCreateKey(
  1285. &key1,
  1286. KEY_CREATE_SUB_KEY,
  1287. &ObjectAttributes,
  1288. 0,
  1289. (PUNICODE_STRING)&nullclass,
  1290. 0,
  1291. NULL
  1292. );
  1293. if (!NT_SUCCESS(status)) {
  1294. #if DBG
  1295. #ifndef _CM_LDR_
  1296. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_WARNING_LEVEL,"CMINIT: CreateKey of %wZ failed - Status == %lx\n",
  1297. &NameString, status);
  1298. #endif //_CM_LDR_
  1299. #endif
  1300. ExFreePool(SecurityDescriptor);
  1301. return;
  1302. }
  1303. #if defined(_WIN64)
  1304. if (VersionDataKey->AdditionalKeyPath != NULL) {
  1305. RtlInitUnicodeString(
  1306. &NameString,
  1307. VersionDataKey->AdditionalKeyPath
  1308. );
  1309. InitializeObjectAttributes(
  1310. &ObjectAttributes,
  1311. &NameString,
  1312. OBJ_CASE_INSENSITIVE,
  1313. key1,
  1314. SecurityDescriptor
  1315. );
  1316. status = NtCreateKey(
  1317. &key2,
  1318. KEY_SET_VALUE,
  1319. &ObjectAttributes,
  1320. 0,
  1321. (PUNICODE_STRING)&nullclass,
  1322. 0,
  1323. NULL
  1324. );
  1325. NtClose(key1);
  1326. key1 = key2;
  1327. }
  1328. #endif
  1329. RtlInitUnicodeString(
  1330. &NameString,
  1331. L"Windows NT"
  1332. );
  1333. InitializeObjectAttributes(
  1334. &ObjectAttributes,
  1335. &NameString,
  1336. OBJ_CASE_INSENSITIVE,
  1337. key1,
  1338. SecurityDescriptor
  1339. );
  1340. status = NtCreateKey(
  1341. &key2,
  1342. KEY_SET_VALUE,
  1343. &ObjectAttributes,
  1344. 0,
  1345. (PUNICODE_STRING)&nullclass,
  1346. 0,
  1347. NULL
  1348. );
  1349. NtClose(key1);
  1350. RtlInitUnicodeString(
  1351. &NameString,
  1352. L"CurrentVersion"
  1353. );
  1354. InitializeObjectAttributes(
  1355. &ObjectAttributes,
  1356. &NameString,
  1357. OBJ_CASE_INSENSITIVE,
  1358. key2,
  1359. SecurityDescriptor
  1360. );
  1361. status = NtCreateKey(
  1362. &key1,
  1363. KEY_SET_VALUE,
  1364. &ObjectAttributes,
  1365. 0,
  1366. (PUNICODE_STRING)&nullclass,
  1367. 0,
  1368. NULL
  1369. );
  1370. NtClose(key2);
  1371. if (!NT_SUCCESS(status)) {
  1372. #if DBG
  1373. #ifndef _CM_LDR_
  1374. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_WARNING_LEVEL,"CMINIT: CreateKey of %wZ failed - Status == %lx\n",
  1375. &NameString, status);
  1376. #endif //_CM_LDR_
  1377. #endif
  1378. ExFreePool(SecurityDescriptor);
  1379. return;
  1380. }
  1381. //
  1382. // Set the value entries for the key
  1383. //
  1384. RtlInitUnicodeString(
  1385. &NameString,
  1386. L"CurrentVersion"
  1387. );
  1388. status = NtSetValueKey(
  1389. key1,
  1390. &NameString,
  1391. 0, // TitleIndex
  1392. REG_SZ,
  1393. CmVersionString.Buffer,
  1394. CmVersionString.Length + sizeof( UNICODE_NULL )
  1395. );
  1396. #if DBG
  1397. if (!NT_SUCCESS(status)) {
  1398. #ifndef _CM_LDR_
  1399. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: SetValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1400. #endif //_CM_LDR_
  1401. }
  1402. #endif
  1403. RtlInitUnicodeString(
  1404. &NameString,
  1405. L"CurrentBuildNumber"
  1406. );
  1407. sprintf(
  1408. WorkString,
  1409. "%u",
  1410. NtBuildNumber & 0xFFFF
  1411. );
  1412. RtlInitAnsiString( &AnsiString, WorkString );
  1413. ValueString.Buffer = ValueBuffer;
  1414. ValueString.Length = 0;
  1415. ValueString.MaximumLength = sizeof( ValueBuffer );
  1416. RtlAnsiStringToUnicodeString( &ValueString, &AnsiString, FALSE );
  1417. status = NtSetValueKey(
  1418. key1,
  1419. &NameString,
  1420. 0, // TitleIndex
  1421. REG_SZ,
  1422. ValueString.Buffer,
  1423. ValueString.Length + sizeof( UNICODE_NULL )
  1424. );
  1425. #if DBG
  1426. if (!NT_SUCCESS(status)) {
  1427. #ifndef _CM_LDR_
  1428. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: SetValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1429. #endif //_CM_LDR_
  1430. }
  1431. #endif
  1432. RtlInitUnicodeString(
  1433. &NameString,
  1434. L"BuildLab"
  1435. );
  1436. RtlInitAnsiString( &AnsiString, NtBuildLab );
  1437. ValueString.Buffer = ValueBuffer;
  1438. ValueString.Length = 0;
  1439. ValueString.MaximumLength = sizeof( ValueBuffer );
  1440. status = RtlAnsiStringToUnicodeString( &ValueString, &AnsiString, FALSE );
  1441. if (NT_SUCCESS(status)) {
  1442. status = NtSetValueKey(
  1443. key1,
  1444. &NameString,
  1445. 0,
  1446. REG_SZ,
  1447. ValueString.Buffer,
  1448. ValueString.Length + sizeof( UNICODE_NULL )
  1449. );
  1450. #if DBG
  1451. if (!NT_SUCCESS(status)) {
  1452. DbgPrint("CMINIT: SetValueKey of %wZ failed - Status == %lx\n",
  1453. &NameString, status);
  1454. }
  1455. } else {
  1456. DbgPrint("CMINIT: RtlAnsiStringToUnicodeString of %wZ failed - Status == %lx\n",
  1457. &NameString, status);
  1458. #endif
  1459. }
  1460. RtlInitUnicodeString(
  1461. &NameString,
  1462. L"CurrentType"
  1463. );
  1464. #if defined(NT_UP)
  1465. proctype = "Uniprocessor";
  1466. #else
  1467. proctype = "Multiprocessor";
  1468. #endif
  1469. #if DBG
  1470. buildtype = "Checked";
  1471. #else
  1472. #if DEVL
  1473. buildtype = "Free";
  1474. #else
  1475. buildtype = "Retail";
  1476. #endif
  1477. #endif
  1478. sprintf(
  1479. WorkString,
  1480. "%s %s",
  1481. proctype,
  1482. buildtype
  1483. );
  1484. RtlInitAnsiString( &AnsiString, WorkString );
  1485. ValueString.Buffer = ValueBuffer;
  1486. ValueString.Length = 0;
  1487. ValueString.MaximumLength = sizeof( ValueBuffer );
  1488. RtlAnsiStringToUnicodeString( &ValueString, &AnsiString, FALSE );
  1489. status = NtSetValueKey(
  1490. key1,
  1491. &NameString,
  1492. 0, // TitleIndex
  1493. REG_SZ,
  1494. ValueString.Buffer,
  1495. ValueString.Length + sizeof( UNICODE_NULL )
  1496. );
  1497. RtlInitUnicodeString(
  1498. &NameString,
  1499. L"CSDVersion"
  1500. );
  1501. if (CmCSDVersionString.Length != 0) {
  1502. status = NtSetValueKey(
  1503. key1,
  1504. &NameString,
  1505. 0, // TitleIndex
  1506. REG_SZ,
  1507. CmCSDVersionString.Buffer,
  1508. CmCSDVersionString.Length + sizeof( UNICODE_NULL )
  1509. );
  1510. #if DBG
  1511. if (!NT_SUCCESS(status)) {
  1512. #ifndef _CM_LDR_
  1513. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: SetValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1514. #endif //_CM_LDR_
  1515. }
  1516. #endif
  1517. (RtlFreeStringRoutine)( CmCSDVersionString.Buffer );
  1518. RtlInitUnicodeString( &CmCSDVersionString, NULL );
  1519. } else {
  1520. status = NtDeleteValueKey(
  1521. key1,
  1522. &NameString
  1523. );
  1524. #if DBG
  1525. if (!NT_SUCCESS(status) && status != STATUS_OBJECT_NAME_NOT_FOUND) {
  1526. #ifndef _CM_LDR_
  1527. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: DeleteValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1528. #endif //_CM_LDR_
  1529. }
  1530. #endif
  1531. }
  1532. //
  1533. // High-order 16-bits of CSDVersion contain RC number or build number. If non-zero
  1534. // display it after the Service Pack number.
  1535. //
  1536. RtlInitUnicodeString(
  1537. &NameString,
  1538. L"CSDBuildNumber"
  1539. );
  1540. if (CmNtCSDVersion & 0xFFFF0000) {
  1541. ULONG Value = (CmNtCSDVersion & 0xFFFF0000) >> 16;
  1542. sprintf(
  1543. WorkString,
  1544. "%u",
  1545. Value
  1546. );
  1547. RtlInitAnsiString( &AnsiString, WorkString );
  1548. ValueString.Buffer = ValueBuffer;
  1549. ValueString.Length = 0;
  1550. ValueString.MaximumLength = sizeof( ValueBuffer );
  1551. RtlAnsiStringToUnicodeString( &ValueString, &AnsiString, FALSE );
  1552. status = NtSetValueKey(
  1553. key1,
  1554. &NameString,
  1555. 0,
  1556. REG_SZ,
  1557. ValueString.Buffer,
  1558. ValueString.Length + sizeof( UNICODE_NULL )
  1559. );
  1560. #if DBG
  1561. if (!NT_SUCCESS(status)) {
  1562. #ifndef _CM_LDR_
  1563. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: SetValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1564. #endif //_CM_LDR_
  1565. }
  1566. #endif
  1567. } else {
  1568. status = NtDeleteValueKey(
  1569. key1,
  1570. &NameString
  1571. );
  1572. #if DBG
  1573. if (!NT_SUCCESS(status) && status != STATUS_OBJECT_NAME_NOT_FOUND) {
  1574. #ifndef _CM_LDR_
  1575. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: DeleteValueKey of %wZ failed - Status == %lx\n",&NameString, status);
  1576. #endif //_CM_LDR_
  1577. }
  1578. #endif
  1579. }
  1580. RtlInitUnicodeString(&NameString,
  1581. L"SystemRoot");
  1582. status = NtSetValueKey(key1,
  1583. &NameString,
  1584. 0,
  1585. REG_SZ,
  1586. NtSystemRoot.Buffer,
  1587. NtSystemRoot.Length + sizeof(UNICODE_NULL));
  1588. #if DBG
  1589. if (!NT_SUCCESS(status)) {
  1590. #ifndef _CM_LDR_
  1591. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CMINIT: SetValueKey of %wZ failed - Status == %lx\n",&NameString,status);
  1592. #endif //_CM_LDR_
  1593. }
  1594. #endif
  1595. NtClose(key1);
  1596. }
  1597. (RtlFreeStringRoutine)( CmVersionString.Buffer );
  1598. RtlInitUnicodeString( &CmVersionString, NULL );
  1599. ExFreePool(SecurityDescriptor);
  1600. //
  1601. // Set each processor to it's optimal configuration.
  1602. //
  1603. // Note: this call is performed interlocked such that the user
  1604. // can disable this automatic configuration update.
  1605. //
  1606. CmpInterlockedFunction(CmpProcessorControl, CmpConfigureProcessors);
  1607. return;
  1608. }
  1609. NTSTATUS
  1610. CmpInterlockedFunction (
  1611. PWCHAR RegistryValueKey,
  1612. VOID (*InterlockedFunction)(VOID)
  1613. )
  1614. /*++
  1615. Routine Description:
  1616. This routine guards calling the InterlockedFunction in the
  1617. passed RegistryValueKey.
  1618. The RegistryValueKey will record the status of the first
  1619. call to the InterlockedFunction. If the system crashes
  1620. durning this call then ValueKey will be left in a state
  1621. where the InterlockedFunction will not be called on subsequent
  1622. attempts.
  1623. Arguments:
  1624. RegistryValueKey - ValueKey name for Control\Session Manager
  1625. InterlockedFunction - Function to call
  1626. Return Value:
  1627. STATUS_SUCCESS - The interlocked function was successfully called
  1628. --*/
  1629. {
  1630. OBJECT_ATTRIBUTES objectAttributes;
  1631. HANDLE hControl, hSession;
  1632. UNICODE_STRING Name;
  1633. UCHAR Buffer [sizeof(KEY_VALUE_PARTIAL_INFORMATION)+sizeof(ULONG)];
  1634. ULONG length, Value;
  1635. NTSTATUS status;
  1636. PAGED_CODE();
  1637. //
  1638. // Open CurrentControlSet
  1639. //
  1640. InitializeObjectAttributes (
  1641. &objectAttributes,
  1642. &CmRegistryMachineSystemCurrentControlSet,
  1643. OBJ_CASE_INSENSITIVE,
  1644. NULL,
  1645. NULL
  1646. );
  1647. status = NtOpenKey (&hControl, KEY_READ | KEY_WRITE, &objectAttributes);
  1648. if (!NT_SUCCESS(status)) {
  1649. return status;
  1650. }
  1651. //
  1652. // Open Control\Session Manager
  1653. //
  1654. RtlInitUnicodeString (&Name, CmpControlSessionManager);
  1655. InitializeObjectAttributes (
  1656. &objectAttributes,
  1657. &Name,
  1658. OBJ_CASE_INSENSITIVE,
  1659. hControl,
  1660. NULL
  1661. );
  1662. status = NtOpenKey (&hSession, KEY_READ | KEY_WRITE, &objectAttributes );
  1663. NtClose (hControl);
  1664. if (!NT_SUCCESS(status)) {
  1665. return status;
  1666. }
  1667. //
  1668. // Read ValueKey to interlock operation with
  1669. //
  1670. RtlInitUnicodeString (&Name, RegistryValueKey);
  1671. status = NtQueryValueKey (hSession,
  1672. &Name,
  1673. KeyValuePartialInformation,
  1674. Buffer,
  1675. sizeof (Buffer),
  1676. &length );
  1677. Value = 0;
  1678. if (NT_SUCCESS(status)) {
  1679. Value = ((PKEY_VALUE_PARTIAL_INFORMATION)Buffer)->Data[0];
  1680. }
  1681. //
  1682. // Value 0 - Before InterlockedFunction
  1683. // 1 - In the middle of InterlockedFunction
  1684. // 2 - After InterlockedFunction
  1685. //
  1686. // If the value is a 0, then we haven't tried calling this
  1687. // interlocked function, set the value to a 1 and try it.
  1688. //
  1689. // If the value is a 1, then we crased durning an execution
  1690. // of the interlocked function last time, don't try it again.
  1691. //
  1692. // If the value is a 2, then we called the interlocked function
  1693. // before and it worked. Call it again this time.
  1694. //
  1695. if (Value != 1) {
  1696. if (Value != 2) {
  1697. //
  1698. // This interlocked function is not known to work. Write
  1699. // a 1 to this value so we can detect if we crash durning
  1700. // this call.
  1701. //
  1702. Value = 1;
  1703. NtSetValueKey (hSession, &Name, 0L, REG_DWORD, &Value, sizeof (Value));
  1704. NtFlushKey (hSession); // wait until it's on the disk
  1705. }
  1706. InterlockedFunction();
  1707. if (Value != 2) {
  1708. //
  1709. // The worker function didn't crash - update the value for
  1710. // this interlocked function to 2.
  1711. //
  1712. Value = 2;
  1713. NtSetValueKey (hSession, &Name, 0L, REG_DWORD, &Value, sizeof (Value));
  1714. }
  1715. } else {
  1716. status = STATUS_UNSUCCESSFUL;
  1717. }
  1718. NtClose (hSession);
  1719. return status;
  1720. }
  1721. VOID
  1722. CmpConfigureProcessors (
  1723. VOID
  1724. )
  1725. /*++
  1726. Routine Description:
  1727. Set each processor to it's optimal settings for NT.
  1728. --*/
  1729. {
  1730. ULONG i;
  1731. PAGED_CODE();
  1732. //
  1733. // Set each processor into its best NT configuration
  1734. //
  1735. for (i=0; i < (ULONG)KeNumberProcessors; i++) {
  1736. KeSetSystemAffinityThread(AFFINITY_MASK(i));
  1737. #if i386
  1738. // for now x86 only
  1739. KeOptimizeProcessorControlState ();
  1740. #endif
  1741. }
  1742. //
  1743. // Restore threads affinity
  1744. //
  1745. KeRevertToUserAffinityThread();
  1746. }
  1747. BOOLEAN
  1748. CmpInitializeSystemHive(
  1749. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  1750. )
  1751. /*++
  1752. Routine Description:
  1753. Initializes the SYSTEM hive based on the raw hive image passed in
  1754. from the OS Loader.
  1755. Arguments:
  1756. LoaderBlock - Supplies a pointer to the Loader Block passed in by
  1757. the OS Loader.
  1758. Return Value:
  1759. TRUE - it worked
  1760. FALSE - it failed
  1761. --*/
  1762. {
  1763. PCMHIVE SystemHive;
  1764. PVOID HiveImageBase;
  1765. BOOLEAN Allocate=FALSE;
  1766. PSECURITY_DESCRIPTOR SecurityDescriptor;
  1767. NTSTATUS Status;
  1768. STRING TempString;
  1769. PAGED_CODE();
  1770. //
  1771. // capture tail of boot.ini line (load options, portable)
  1772. //
  1773. RtlInitAnsiString(
  1774. &TempString,
  1775. LoaderBlock->LoadOptions
  1776. );
  1777. CmpLoadOptions.Length = 0;
  1778. CmpLoadOptions.MaximumLength = (TempString.Length+1)*sizeof(WCHAR);
  1779. CmpLoadOptions.Buffer = ExAllocatePool(
  1780. PagedPool, (TempString.Length+1)*sizeof(WCHAR));
  1781. if (CmpLoadOptions.Buffer == NULL) {
  1782. CM_BUGCHECK(BAD_SYSTEM_CONFIG_INFO,BAD_SYSTEM_HIVE,1,LoaderBlock,0);
  1783. }
  1784. RtlAnsiStringToUnicodeString(
  1785. &CmpLoadOptions,
  1786. &TempString,
  1787. FALSE
  1788. );
  1789. CmpLoadOptions.Buffer[TempString.Length] = UNICODE_NULL;
  1790. CmpLoadOptions.Length += sizeof(WCHAR);
  1791. //
  1792. // move the loaded registry into the real registry
  1793. //
  1794. HiveImageBase = LoaderBlock->RegistryBase;
  1795. //
  1796. // We need to initialize the system hive as NO_LAZY_FLUSH
  1797. // - this is just temporary, untill we get a chance to open the primary
  1798. // file for the hive. Failure to do so, will result in loss of data on the
  1799. // LazyFlush worker (see CmpFileWrite, the
  1800. // if (FileHandle == NULL) {
  1801. // return TRUE;
  1802. // }
  1803. // test. This might be a problem in 5.0 too, if system crashes between the
  1804. // LazyFlush reported the hive as saved and the moment we actually open the
  1805. // file and save it again
  1806. //
  1807. if (HiveImageBase == NULL) {
  1808. //
  1809. // No memory descriptor for the hive, so we must recreate it.
  1810. //
  1811. Status = CmpInitializeHive(&SystemHive,
  1812. HINIT_CREATE,
  1813. HIVE_NOLAZYFLUSH,
  1814. HFILE_TYPE_LOG,
  1815. NULL,
  1816. NULL,
  1817. NULL,
  1818. NULL,
  1819. &CmpSystemFileName,
  1820. 0);
  1821. if (!NT_SUCCESS(Status)) {
  1822. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitializeSystemHive: "));
  1823. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"Couldn't initialize newly allocated SYSTEM hive\n"));
  1824. return(FALSE);
  1825. }
  1826. Allocate = TRUE;
  1827. } else {
  1828. //
  1829. // There is a memory image for the hive, copy it and make it active
  1830. //
  1831. Status = CmpInitializeHive(&SystemHive,
  1832. HINIT_MEMORY,
  1833. HIVE_NOLAZYFLUSH,
  1834. HFILE_TYPE_LOG,
  1835. HiveImageBase,
  1836. NULL,
  1837. NULL,
  1838. NULL,
  1839. &CmpSystemFileName,
  1840. CM_CHECK_REGISTRY_SYSTEM_CLEAN);
  1841. if (!NT_SUCCESS(Status)) {
  1842. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitializeSystemHive: "));
  1843. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"Couldn't initialize OS Loader-loaded SYSTEM hive\n"));
  1844. CM_BUGCHECK(BAD_SYSTEM_CONFIG_INFO,BAD_SYSTEM_HIVE,2,SystemHive,Status);
  1845. }
  1846. Allocate = FALSE;
  1847. //
  1848. // Mark the system hive as volatile, while in MiniNT boot
  1849. // case
  1850. //
  1851. if (CmpShareSystemHives) {
  1852. SystemHive->Hive.HiveFlags = HIVE_VOLATILE;
  1853. }
  1854. }
  1855. CmpBootType = SystemHive->Hive.BaseBlock->BootType;
  1856. //
  1857. // Create the link node
  1858. //
  1859. SecurityDescriptor = CmpHiveRootSecurityDescriptor();
  1860. Status = CmpLinkHiveToMaster(&CmRegistryMachineSystemName,
  1861. NULL,
  1862. SystemHive,
  1863. Allocate,
  1864. SecurityDescriptor);
  1865. ExFreePool(SecurityDescriptor);
  1866. if (!NT_SUCCESS(Status)) {
  1867. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmInitSystem1: CmpLinkHiveToMaster(Hardware) failed\n"));
  1868. return(FALSE);
  1869. }
  1870. CmpMachineHiveList[SYSTEM_HIVE_INDEX].CmHive = SystemHive;
  1871. return(TRUE);
  1872. }
  1873. PHANDLE
  1874. CmGetSystemDriverList(
  1875. VOID
  1876. )
  1877. /*++
  1878. Routine Description:
  1879. Traverses the current SERVICES subtree and creates the list of drivers
  1880. to be loaded during Phase 1 initialization.
  1881. Arguments:
  1882. None
  1883. Return Value:
  1884. A pointer to an array of handles, each of which refers to a key in
  1885. the \Services section of the control set. The caller will traverse
  1886. this array and load and initialize the drivers described by the keys.
  1887. The last key will be NULL. The array is allocated in Pool and should
  1888. be freed by the caller.
  1889. --*/
  1890. {
  1891. OBJECT_ATTRIBUTES ObjectAttributes;
  1892. HANDLE SystemHandle;
  1893. UNICODE_STRING Name;
  1894. NTSTATUS Status;
  1895. PCM_KEY_BODY KeyBody;
  1896. LIST_ENTRY DriverList;
  1897. PHHIVE Hive;
  1898. HCELL_INDEX RootCell;
  1899. HCELL_INDEX ControlCell;
  1900. ULONG DriverCount;
  1901. PLIST_ENTRY Current;
  1902. PHANDLE Handle;
  1903. PBOOT_DRIVER_LIST_ENTRY DriverEntry;
  1904. BOOLEAN Success;
  1905. BOOLEAN AutoSelect;
  1906. PAGED_CODE();
  1907. InitializeListHead(&DriverList);
  1908. RtlInitUnicodeString(&Name,
  1909. L"\\Registry\\Machine\\System");
  1910. InitializeObjectAttributes(&ObjectAttributes,
  1911. &Name,
  1912. OBJ_CASE_INSENSITIVE,
  1913. (HANDLE)NULL,
  1914. NULL);
  1915. Status = NtOpenKey(&SystemHandle,
  1916. KEY_READ,
  1917. &ObjectAttributes);
  1918. if (!NT_SUCCESS(Status)) {
  1919. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't open registry key %wZ\n",&Name));
  1920. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: status %08lx\n", Status));
  1921. return(NULL);
  1922. }
  1923. Status = ObReferenceObjectByHandle( SystemHandle,
  1924. KEY_QUERY_VALUE,
  1925. CmpKeyObjectType,
  1926. KernelMode,
  1927. (PVOID *)(&KeyBody),
  1928. NULL );
  1929. if (!NT_SUCCESS(Status)) {
  1930. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't dereference System handle\n"));
  1931. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: status %08lx\n", Status));
  1932. NtClose(SystemHandle);
  1933. return(NULL);
  1934. }
  1935. CmpLockRegistryExclusive();
  1936. Hive = KeyBody->KeyControlBlock->KeyHive;
  1937. RootCell = KeyBody->KeyControlBlock->KeyCell;
  1938. //
  1939. // Now we have found out the PHHIVE and HCELL_INDEX of the root of the
  1940. // SYSTEM hive, we can use all the same code that the OS Loader does.
  1941. //
  1942. RtlInitUnicodeString(&Name, L"Current");
  1943. ControlCell = CmpFindControlSet(Hive,
  1944. RootCell,
  1945. &Name,
  1946. &AutoSelect);
  1947. if (ControlCell == HCELL_NIL) {
  1948. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't find control set\n"));
  1949. CmpUnlockRegistry();
  1950. ObDereferenceObject((PVOID)KeyBody);
  1951. NtClose(SystemHandle);
  1952. return(NULL);
  1953. }
  1954. Success = CmpFindDrivers(Hive,
  1955. ControlCell,
  1956. SystemLoad,
  1957. NULL,
  1958. &DriverList);
  1959. if (!Success) {
  1960. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't find any valid drivers\n"));
  1961. CmpFreeDriverList(Hive, &DriverList);
  1962. CmpUnlockRegistry();
  1963. ObDereferenceObject((PVOID)KeyBody);
  1964. NtClose(SystemHandle);
  1965. return(NULL);
  1966. }
  1967. if (!CmpSortDriverList(Hive,
  1968. ControlCell,
  1969. &DriverList)) {
  1970. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't sort driver list\n"));
  1971. CmpFreeDriverList(Hive, &DriverList);
  1972. CmpUnlockRegistry();
  1973. ObDereferenceObject((PVOID)KeyBody);
  1974. NtClose(SystemHandle);
  1975. return(NULL);
  1976. }
  1977. if (!CmpResolveDriverDependencies(&DriverList)) {
  1978. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't resolve driver dependencies\n"));
  1979. CmpFreeDriverList(Hive, &DriverList);
  1980. CmpUnlockRegistry();
  1981. ObDereferenceObject((PVOID)KeyBody);
  1982. NtClose(SystemHandle);
  1983. return(NULL);
  1984. }
  1985. CmpUnlockRegistry();
  1986. ObDereferenceObject((PVOID)KeyBody);
  1987. NtClose(SystemHandle);
  1988. //
  1989. // We now have a fully sorted and ordered list of drivers to be loaded
  1990. // by IoInit.
  1991. //
  1992. //
  1993. // Count the nodes in the list.
  1994. //
  1995. Current = DriverList.Flink;
  1996. DriverCount = 0;
  1997. while (Current != &DriverList) {
  1998. ++DriverCount;
  1999. Current = Current->Flink;
  2000. }
  2001. Handle = (PHANDLE)ExAllocatePool(NonPagedPool,
  2002. (DriverCount+1) * sizeof(HANDLE));
  2003. if (Handle == NULL) {
  2004. CM_BUGCHECK(CONFIG_INITIALIZATION_FAILED,INIT_SYSTEM_DRIVER_LIST,1,0,0); // odds against this are huge
  2005. }
  2006. //
  2007. // Walk the list, opening each registry key and adding it to the
  2008. // table of handles.
  2009. //
  2010. Current = DriverList.Flink;
  2011. DriverCount = 0;
  2012. while (Current != &DriverList) {
  2013. DriverEntry = CONTAINING_RECORD(Current,
  2014. BOOT_DRIVER_LIST_ENTRY,
  2015. Link);
  2016. InitializeObjectAttributes(&ObjectAttributes,
  2017. &DriverEntry->RegistryPath,
  2018. OBJ_CASE_INSENSITIVE,
  2019. (HANDLE)NULL,
  2020. NULL);
  2021. Status = NtOpenKey(Handle+DriverCount,
  2022. KEY_READ | KEY_WRITE,
  2023. &ObjectAttributes);
  2024. if (!NT_SUCCESS(Status)) {
  2025. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmGetSystemDriverList couldn't open driver "));
  2026. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"key %wZ\n", &DriverEntry->RegistryPath));
  2027. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," status %08lx\n",Status));
  2028. } else {
  2029. ++DriverCount;
  2030. }
  2031. Current = Current->Flink;
  2032. }
  2033. Handle[DriverCount] = NULL;
  2034. CmpFreeDriverList(Hive, &DriverList);
  2035. return(Handle);
  2036. }
  2037. VOID
  2038. CmpFreeDriverList(
  2039. IN PHHIVE Hive,
  2040. IN PLIST_ENTRY DriverList
  2041. )
  2042. /*++
  2043. Routine Description:
  2044. Walks down the driver list, freeing each node in it.
  2045. Note that this calls the hive's free routine pointer to free the memory.
  2046. Arguments:
  2047. Hive - Supplies a pointer to the hive control structure.
  2048. DriverList - Supplies a pointer to the head of the Driver List. Note
  2049. that the head of the list is not actually freed, only all the
  2050. entries in the list.
  2051. Return Value:
  2052. None.
  2053. --*/
  2054. {
  2055. PLIST_ENTRY Next;
  2056. PLIST_ENTRY Current;
  2057. PBOOT_DRIVER_NODE DriverNode;
  2058. PAGED_CODE();
  2059. Current = DriverList->Flink;
  2060. while (Current != DriverList) {
  2061. Next = Current->Flink;
  2062. DriverNode = (PBOOT_DRIVER_NODE)Current;
  2063. if( DriverNode->Name.Buffer != NULL ){
  2064. (Hive->Free)(DriverNode->Name.Buffer,DriverNode->Name.Length);
  2065. }
  2066. if( DriverNode->ListEntry.RegistryPath.Buffer != NULL ){
  2067. (Hive->Free)(DriverNode->ListEntry.RegistryPath.Buffer,DriverNode->ListEntry.RegistryPath.MaximumLength);
  2068. }
  2069. if( DriverNode->ListEntry.FilePath.Buffer != NULL ){
  2070. (Hive->Free)(DriverNode->ListEntry.FilePath.Buffer,DriverNode->ListEntry.FilePath.MaximumLength);
  2071. }
  2072. (Hive->Free)((PVOID)Current, sizeof(BOOT_DRIVER_NODE));
  2073. Current = Next;
  2074. }
  2075. }
  2076. NTSTATUS
  2077. CmpInitHiveFromFile(
  2078. IN PUNICODE_STRING FileName,
  2079. IN ULONG HiveFlags,
  2080. OUT PCMHIVE *CmHive,
  2081. IN OUT PBOOLEAN Allocate,
  2082. IN OUT PBOOLEAN RegistryLocked,
  2083. IN ULONG CheckFlags
  2084. )
  2085. /*++
  2086. Routine Description:
  2087. This routine opens a file and log, allocates a CMHIVE, and initializes
  2088. it.
  2089. Arguments:
  2090. FileName - Supplies name of file to be loaded.
  2091. HiveFlags - Supplies hive flags to be passed to CmpInitializeHive
  2092. CmHive - Returns pointer to initialized hive (if successful)
  2093. Allocate - IN: if TRUE ok to allocate, if FALSE hive must exist
  2094. (bug .log may get created)
  2095. OUT: TRUE if actually created hive, FALSE if existed before
  2096. Return Value:
  2097. NTSTATUS
  2098. --*/
  2099. {
  2100. PCMHIVE NewHive;
  2101. ULONG Disposition;
  2102. ULONG SecondaryDisposition;
  2103. HANDLE PrimaryHandle;
  2104. HANDLE LogHandle;
  2105. NTSTATUS Status;
  2106. ULONG FileType;
  2107. ULONG Operation;
  2108. PULONG_PTR HiveSection;
  2109. PVOID HiveData = NULL;
  2110. BOOLEAN Success;
  2111. PEPROCESS Process;
  2112. LARGE_INTEGER liOffset;
  2113. BOOLEAN NoBuffering = FALSE;
  2114. BOOLEAN LockedHeldOnCall;
  2115. PAGED_CODE();
  2116. #ifndef CM_ENABLE_MAPPED_VIEWS
  2117. NoBuffering = TRUE;
  2118. #endif //CM_ENABLE_MAPPED_VIEWS
  2119. RetryNoBuffering:
  2120. *CmHive = NULL;
  2121. LockedHeldOnCall = *RegistryLocked;
  2122. Status = CmpOpenHiveFiles(FileName,
  2123. L".LOG",
  2124. &PrimaryHandle,
  2125. &LogHandle,
  2126. &Disposition,
  2127. &SecondaryDisposition,
  2128. *Allocate,
  2129. FALSE,
  2130. NoBuffering,
  2131. NULL);
  2132. if (!NT_SUCCESS(Status)) {
  2133. return(Status);
  2134. }
  2135. if (LogHandle == NULL) {
  2136. FileType = HFILE_TYPE_PRIMARY;
  2137. } else {
  2138. FileType = HFILE_TYPE_LOG;
  2139. }
  2140. if (Disposition == FILE_CREATED) {
  2141. Operation = HINIT_CREATE;
  2142. *Allocate = TRUE;
  2143. } else {
  2144. if( NoBuffering == TRUE ) {
  2145. Operation = HINIT_FILE;
  2146. } else {
  2147. Operation = HINIT_MAPFILE;
  2148. }
  2149. *Allocate = FALSE;
  2150. }
  2151. if (CmpShareSystemHives) {
  2152. FileType = HFILE_TYPE_PRIMARY;
  2153. if (LogHandle) {
  2154. ZwClose(LogHandle);
  2155. LogHandle = NULL;
  2156. }
  2157. }
  2158. if( !(*RegistryLocked) ) {
  2159. //
  2160. // Registry should be locked exclusive
  2161. // if not, lock it now and signal this to the caller
  2162. //
  2163. CmpLockRegistryExclusive();
  2164. *RegistryLocked = TRUE;
  2165. }
  2166. if( HvShutdownComplete == TRUE ) {
  2167. ZwClose(PrimaryHandle);
  2168. if (LogHandle != NULL) {
  2169. ZwClose(LogHandle);
  2170. }
  2171. return STATUS_TOO_LATE;
  2172. }
  2173. Status = CmpInitializeHive(&NewHive,
  2174. Operation,
  2175. HiveFlags,
  2176. FileType,
  2177. HiveData,
  2178. PrimaryHandle,
  2179. LogHandle,
  2180. NULL,
  2181. FileName,
  2182. CheckFlags
  2183. );
  2184. if (!NT_SUCCESS(Status)) {
  2185. CmpTrackHiveClose = TRUE;
  2186. ZwClose(PrimaryHandle);
  2187. CmpTrackHiveClose = FALSE;
  2188. if (LogHandle != NULL) {
  2189. ZwClose(LogHandle);
  2190. }
  2191. if( Status == STATUS_RETRY ) {
  2192. if( NoBuffering == FALSE ) {
  2193. NoBuffering = TRUE;
  2194. if( !LockedHeldOnCall ) {
  2195. *RegistryLocked = FALSE;
  2196. CmpUnlockRegistry();
  2197. }
  2198. goto RetryNoBuffering;
  2199. }
  2200. }
  2201. return(Status);
  2202. } else {
  2203. *CmHive = NewHive;
  2204. //
  2205. // mark handles as protected. If other kernel component tries to close them ==> bugcheck.
  2206. //
  2207. CmpSetHandleProtection(PrimaryHandle,TRUE);
  2208. if (LogHandle != NULL) {
  2209. CmpSetHandleProtection(LogHandle,TRUE);
  2210. }
  2211. //
  2212. // Capture the file name; in case we need it later for double load check
  2213. //
  2214. (*CmHive)->FileUserName.Buffer = ExAllocatePoolWithTag(PagedPool,
  2215. FileName->Length,
  2216. CM_NAME_TAG | PROTECTED_POOL);
  2217. if ((*CmHive)->FileUserName.Buffer) {
  2218. RtlCopyMemory((*CmHive)->FileUserName.Buffer,
  2219. FileName->Buffer,
  2220. FileName->Length);
  2221. (*CmHive)->FileUserName.Length = FileName->Length;
  2222. (*CmHive)->FileUserName.MaximumLength = FileName->Length;
  2223. }
  2224. if(((PHHIVE)(*CmHive))->BaseBlock->BootType & HBOOT_SELFHEAL) {
  2225. //
  2226. // Warn the user;
  2227. //
  2228. CmpRaiseSelfHealWarning(&((*CmHive)->FileUserName));
  2229. }
  2230. return(STATUS_SUCCESS);
  2231. }
  2232. }
  2233. NTSTATUS
  2234. CmpAddDockingInfo (
  2235. IN HANDLE Key,
  2236. IN PROFILE_PARAMETER_BLOCK * ProfileBlock
  2237. )
  2238. /*++
  2239. Routine Description:
  2240. Write DockID SerialNumber DockState and Capabilities intot the given
  2241. registry key.
  2242. --*/
  2243. {
  2244. NTSTATUS status = STATUS_SUCCESS;
  2245. UNICODE_STRING name;
  2246. ULONG value;
  2247. PAGED_CODE ();
  2248. value = ProfileBlock->DockingState;
  2249. RtlInitUnicodeString (&name, CM_HARDWARE_PROFILE_STR_DOCKING_STATE);
  2250. status = NtSetValueKey (Key,
  2251. &name,
  2252. 0,
  2253. REG_DWORD,
  2254. &value,
  2255. sizeof (value));
  2256. if (!NT_SUCCESS (status)) {
  2257. return status;
  2258. }
  2259. value = ProfileBlock->Capabilities;
  2260. RtlInitUnicodeString (&name, CM_HARDWARE_PROFILE_STR_CAPABILITIES);
  2261. status = NtSetValueKey (Key,
  2262. &name,
  2263. 0,
  2264. REG_DWORD,
  2265. &value,
  2266. sizeof (value));
  2267. if (!NT_SUCCESS (status)) {
  2268. return status;
  2269. }
  2270. value = ProfileBlock->DockID;
  2271. RtlInitUnicodeString (&name, CM_HARDWARE_PROFILE_STR_DOCKID);
  2272. status = NtSetValueKey (Key,
  2273. &name,
  2274. 0,
  2275. REG_DWORD,
  2276. &value,
  2277. sizeof (value));
  2278. if (!NT_SUCCESS (status)) {
  2279. return status;
  2280. }
  2281. value = ProfileBlock->SerialNumber;
  2282. RtlInitUnicodeString (&name, CM_HARDWARE_PROFILE_STR_SERIAL_NUMBER);
  2283. status = NtSetValueKey (Key,
  2284. &name,
  2285. 0,
  2286. REG_DWORD,
  2287. &value,
  2288. sizeof (value));
  2289. if (!NT_SUCCESS (status)) {
  2290. return status;
  2291. }
  2292. return status;
  2293. }
  2294. NTSTATUS
  2295. CmpAddAliasEntry (
  2296. IN HANDLE IDConfigDB,
  2297. IN PROFILE_PARAMETER_BLOCK * ProfileBlock,
  2298. IN ULONG ProfileNumber
  2299. )
  2300. /*++
  2301. Routine Description:
  2302. Create an alias entry in the IDConfigDB database for the given
  2303. hardware profile.
  2304. Create the "Alias" key if it does not exist.
  2305. Parameters:
  2306. IDConfigDB - Pointer to "..\CurrentControlSet\Control\IDConfigDB"
  2307. ProfileBlock - Description of the current Docking information
  2308. ProfileNumber -
  2309. --*/
  2310. {
  2311. OBJECT_ATTRIBUTES attributes;
  2312. NTSTATUS status = STATUS_SUCCESS;
  2313. CHAR asciiBuffer [128];
  2314. WCHAR unicodeBuffer [128];
  2315. ANSI_STRING ansiString;
  2316. UNICODE_STRING name;
  2317. HANDLE aliasKey = NULL;
  2318. HANDLE aliasEntry = NULL;
  2319. ULONG value;
  2320. ULONG disposition;
  2321. ULONG aliasNumber = 0;
  2322. PAGED_CODE ();
  2323. //
  2324. // Find the Alias Key or Create it if it does not already exist.
  2325. //
  2326. RtlInitUnicodeString (&name,CM_HARDWARE_PROFILE_STR_ALIAS);
  2327. InitializeObjectAttributes (&attributes,
  2328. &name,
  2329. OBJ_CASE_INSENSITIVE,
  2330. IDConfigDB,
  2331. NULL);
  2332. status = NtOpenKey (&aliasKey,
  2333. KEY_READ | KEY_WRITE,
  2334. &attributes);
  2335. if (STATUS_OBJECT_NAME_NOT_FOUND == status) {
  2336. status = NtCreateKey (&aliasKey,
  2337. KEY_READ | KEY_WRITE,
  2338. &attributes,
  2339. 0, // no title
  2340. NULL, // no class
  2341. 0, // no options
  2342. &disposition);
  2343. }
  2344. if (!NT_SUCCESS (status)) {
  2345. aliasKey = NULL;
  2346. goto Exit;
  2347. }
  2348. //
  2349. // Create an entry key
  2350. //
  2351. while (aliasNumber < 200) {
  2352. aliasNumber++;
  2353. sprintf(asciiBuffer, "%04d", aliasNumber);
  2354. RtlInitAnsiString(&ansiString, asciiBuffer);
  2355. name.MaximumLength = sizeof(unicodeBuffer);
  2356. name.Buffer = unicodeBuffer;
  2357. status = RtlAnsiStringToUnicodeString(&name,
  2358. &ansiString,
  2359. FALSE);
  2360. ASSERT (STATUS_SUCCESS == status);
  2361. InitializeObjectAttributes(&attributes,
  2362. &name,
  2363. OBJ_CASE_INSENSITIVE,
  2364. aliasKey,
  2365. NULL);
  2366. status = NtOpenKey (&aliasEntry,
  2367. KEY_READ | KEY_WRITE,
  2368. &attributes);
  2369. if (NT_SUCCESS (status)) {
  2370. NtClose (aliasEntry);
  2371. } else if (STATUS_OBJECT_NAME_NOT_FOUND == status) {
  2372. status = STATUS_SUCCESS;
  2373. break;
  2374. } else {
  2375. break;
  2376. }
  2377. }
  2378. if (!NT_SUCCESS (status)) {
  2379. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: cmpCreateAliasEntry error finding new set %08lx\n",status));
  2380. aliasEntry = 0;
  2381. goto Exit;
  2382. }
  2383. status = NtCreateKey (&aliasEntry,
  2384. KEY_READ | KEY_WRITE,
  2385. &attributes,
  2386. 0,
  2387. NULL,
  2388. 0,
  2389. &disposition);
  2390. if (!NT_SUCCESS (status)) {
  2391. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: cmpCreateAliasEntry error creating new set %08lx\n",status));
  2392. aliasEntry = 0;
  2393. goto Exit;
  2394. }
  2395. //
  2396. // Write the standard goo
  2397. //
  2398. CmpAddDockingInfo (aliasEntry, ProfileBlock);
  2399. //
  2400. // Write the Profile Number
  2401. //
  2402. value = ProfileNumber;
  2403. RtlInitUnicodeString (&name, CM_HARDWARE_PROFILE_STR_PROFILE_NUMBER);
  2404. status = NtSetValueKey (aliasEntry,
  2405. &name,
  2406. 0,
  2407. REG_DWORD,
  2408. &value,
  2409. sizeof (value));
  2410. Exit:
  2411. if (aliasKey) {
  2412. NtClose (aliasKey);
  2413. }
  2414. if (aliasEntry) {
  2415. NtClose (aliasEntry);
  2416. }
  2417. return status;
  2418. }
  2419. NTSTATUS
  2420. CmpHwprofileDefaultSelect (
  2421. IN PCM_HARDWARE_PROFILE_LIST ProfileList,
  2422. OUT PULONG ProfileIndexToUse,
  2423. IN PVOID Context
  2424. )
  2425. {
  2426. UNREFERENCED_PARAMETER (Context);
  2427. * ProfileIndexToUse = 0;
  2428. return STATUS_SUCCESS;
  2429. }
  2430. NTSTATUS
  2431. CmpCreateControlSet(
  2432. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  2433. )
  2434. /*++
  2435. Routine Description:
  2436. This routine sets up the symbolic links from
  2437. \Registry\Machine\System\CurrentControlSet to
  2438. \Registry\Machine\System\ControlSetNNN
  2439. \Registry\Machine\System\CurrentControlSet\Hardware Profiles\Current to
  2440. \Registry\Machine\System\ControlSetNNN\Hardware Profiles\NNNN
  2441. based on the value of \Registry\Machine\System\Select:Current. and
  2442. \Registry\Machine\System\ControlSetNNN\Control\IDConfigDB:CurrentConfig
  2443. Arguments:
  2444. None
  2445. Return Value:
  2446. status
  2447. --*/
  2448. {
  2449. UNICODE_STRING IDConfigDBName;
  2450. UNICODE_STRING SelectName;
  2451. UNICODE_STRING CurrentName;
  2452. OBJECT_ATTRIBUTES Attributes;
  2453. HANDLE SelectHandle;
  2454. HANDLE CurrentHandle;
  2455. HANDLE IDConfigDB = NULL;
  2456. HANDLE CurrentProfile = NULL;
  2457. HANDLE ParentOfProfile = NULL;
  2458. CHAR AsciiBuffer[128];
  2459. WCHAR UnicodeBuffer[128];
  2460. UCHAR ValueBuffer[128];
  2461. ULONG ControlSet;
  2462. ULONG HWProfile;
  2463. PKEY_VALUE_FULL_INFORMATION Value;
  2464. ANSI_STRING AnsiString;
  2465. NTSTATUS Status;
  2466. ULONG ResultLength;
  2467. ULONG Disposition;
  2468. BOOLEAN signalAcpiEvent = FALSE;
  2469. PAGED_CODE();
  2470. RtlInitUnicodeString(&SelectName, L"\\Registry\\Machine\\System\\Select");
  2471. InitializeObjectAttributes(&Attributes,
  2472. &SelectName,
  2473. OBJ_CASE_INSENSITIVE,
  2474. NULL,
  2475. NULL);
  2476. Status = NtOpenKey(&SelectHandle,
  2477. KEY_READ,
  2478. &Attributes);
  2479. if (!NT_SUCCESS(Status)) {
  2480. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: Couldn't open Select node %08lx\n",Status));
  2481. return(Status);
  2482. }
  2483. RtlInitUnicodeString(&CurrentName, L"Current");
  2484. Status = NtQueryValueKey(SelectHandle,
  2485. &CurrentName,
  2486. KeyValueFullInformation,
  2487. ValueBuffer,
  2488. sizeof(ValueBuffer),
  2489. &ResultLength);
  2490. NtClose(SelectHandle);
  2491. if (!NT_SUCCESS(Status)) {
  2492. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: Couldn't query Select value %08lx\n",Status));
  2493. return(Status);
  2494. }
  2495. Value = (PKEY_VALUE_FULL_INFORMATION)ValueBuffer;
  2496. ControlSet = *(PULONG)((PUCHAR)Value + Value->DataOffset);
  2497. RtlInitUnicodeString(&CurrentName, L"\\Registry\\Machine\\System\\CurrentControlSet");
  2498. InitializeObjectAttributes(&Attributes,
  2499. &CurrentName,
  2500. OBJ_CASE_INSENSITIVE,
  2501. NULL,
  2502. NULL);
  2503. Status = NtCreateKey(&CurrentHandle,
  2504. KEY_CREATE_LINK,
  2505. &Attributes,
  2506. 0,
  2507. NULL,
  2508. REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
  2509. &Disposition);
  2510. if (!NT_SUCCESS(Status)) {
  2511. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: couldn't create CurrentControlSet %08lx\n",Status));
  2512. return(Status);
  2513. }
  2514. //
  2515. // Check to make sure that the key was created, not just opened. Since
  2516. // this key is always created volatile, it should never be present in
  2517. // the hive when we boot.
  2518. //
  2519. ASSERT(Disposition == REG_CREATED_NEW_KEY);
  2520. //
  2521. // Create symbolic link for current hardware profile.
  2522. //
  2523. sprintf(AsciiBuffer, "\\Registry\\Machine\\System\\ControlSet%03d", ControlSet);
  2524. RtlInitAnsiString(&AnsiString, AsciiBuffer);
  2525. CurrentName.MaximumLength = sizeof(UnicodeBuffer);
  2526. CurrentName.Buffer = UnicodeBuffer;
  2527. Status = RtlAnsiStringToUnicodeString(&CurrentName,
  2528. &AnsiString,
  2529. FALSE);
  2530. Status = NtSetValueKey(CurrentHandle,
  2531. &CmSymbolicLinkValueName,
  2532. 0,
  2533. REG_LINK,
  2534. CurrentName.Buffer,
  2535. CurrentName.Length);
  2536. if (!NT_SUCCESS(Status)) {
  2537. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: couldn't create symbolic link "));
  2538. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"to %wZ\n",&CurrentName));
  2539. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," Status=%08lx\n",Status));
  2540. NtClose(CurrentHandle);
  2541. return(Status);
  2542. }
  2543. //
  2544. // Determine the Current Hardware Profile Number
  2545. //
  2546. RtlInitUnicodeString(&IDConfigDBName, L"Control\\IDConfigDB");
  2547. InitializeObjectAttributes(&Attributes,
  2548. &IDConfigDBName,
  2549. OBJ_CASE_INSENSITIVE,
  2550. CurrentHandle,
  2551. NULL);
  2552. Status = NtOpenKey(&IDConfigDB,
  2553. KEY_READ,
  2554. &Attributes);
  2555. NtClose(CurrentHandle);
  2556. if (!NT_SUCCESS(Status)) {
  2557. IDConfigDB = 0;
  2558. goto Cleanup;
  2559. }
  2560. RtlInitUnicodeString(&CurrentName, L"CurrentConfig");
  2561. Status = NtQueryValueKey(IDConfigDB,
  2562. &CurrentName,
  2563. KeyValueFullInformation,
  2564. ValueBuffer,
  2565. sizeof(ValueBuffer),
  2566. &ResultLength);
  2567. if (!NT_SUCCESS(Status) ||
  2568. (((PKEY_VALUE_FULL_INFORMATION)ValueBuffer)->Type != REG_DWORD)) {
  2569. goto Cleanup;
  2570. }
  2571. Value = (PKEY_VALUE_FULL_INFORMATION)ValueBuffer;
  2572. HWProfile = *(PULONG)((PUCHAR)Value + Value->DataOffset);
  2573. //
  2574. // We know now the config set that the user selected.
  2575. // namely: HWProfile.
  2576. //
  2577. RtlInitUnicodeString(
  2578. &CurrentName,
  2579. L"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles");
  2580. InitializeObjectAttributes(&Attributes,
  2581. &CurrentName,
  2582. OBJ_CASE_INSENSITIVE,
  2583. NULL,
  2584. NULL);
  2585. Status = NtOpenKey(&ParentOfProfile,
  2586. KEY_READ,
  2587. &Attributes);
  2588. if (!NT_SUCCESS (Status)) {
  2589. ParentOfProfile = 0;
  2590. goto Cleanup;
  2591. }
  2592. sprintf(AsciiBuffer, "%04d",HWProfile);
  2593. RtlInitAnsiString(&AnsiString, AsciiBuffer);
  2594. CurrentName.MaximumLength = sizeof(UnicodeBuffer);
  2595. CurrentName.Buffer = UnicodeBuffer;
  2596. Status = RtlAnsiStringToUnicodeString(&CurrentName,
  2597. &AnsiString,
  2598. FALSE);
  2599. ASSERT (STATUS_SUCCESS == Status);
  2600. InitializeObjectAttributes(&Attributes,
  2601. &CurrentName,
  2602. OBJ_CASE_INSENSITIVE,
  2603. ParentOfProfile,
  2604. NULL);
  2605. Status = NtOpenKey (&CurrentProfile,
  2606. KEY_READ | KEY_WRITE,
  2607. &Attributes);
  2608. if (!NT_SUCCESS (Status)) {
  2609. CurrentProfile = 0;
  2610. goto Cleanup;
  2611. }
  2612. //
  2613. // We need to determine if Value was selected by exact match
  2614. // (TRUE_MATCH) or because the profile selected was aliasable.
  2615. //
  2616. // If aliasable we need to manufacture another alias entry in the
  2617. // alias table.
  2618. //
  2619. // If the profile information is there and not failed then we should
  2620. // mark the Docking state information:
  2621. // (DockID, SerialNumber, DockState, and Capabilities)
  2622. //
  2623. if (NULL != LoaderBlock->Extension) {
  2624. PLOADER_PARAMETER_EXTENSION extension;
  2625. extension = LoaderBlock->Extension;
  2626. switch (extension->Profile.Status) {
  2627. case HW_PROFILE_STATUS_PRISTINE_MATCH:
  2628. //
  2629. // If the selected profile is pristine then we need to clone.
  2630. //
  2631. Status = CmpCloneHwProfile (IDConfigDB,
  2632. ParentOfProfile,
  2633. CurrentProfile,
  2634. HWProfile,
  2635. extension->Profile.DockingState,
  2636. &CurrentProfile,
  2637. &HWProfile);
  2638. if (!NT_SUCCESS (Status)) {
  2639. CurrentProfile = 0;
  2640. goto Cleanup;
  2641. }
  2642. RtlInitUnicodeString(&CurrentName, L"CurrentConfig");
  2643. Status = NtSetValueKey (IDConfigDB,
  2644. &CurrentName,
  2645. 0,
  2646. REG_DWORD,
  2647. &HWProfile,
  2648. sizeof (HWProfile));
  2649. if (!NT_SUCCESS (Status)) {
  2650. goto Cleanup;
  2651. }
  2652. //
  2653. // Fall through
  2654. //
  2655. case HW_PROFILE_STATUS_ALIAS_MATCH:
  2656. //
  2657. // Create the alias entry for this profile.
  2658. //
  2659. Status = CmpAddAliasEntry (IDConfigDB,
  2660. &extension->Profile,
  2661. HWProfile);
  2662. //
  2663. // Fall through
  2664. //
  2665. case HW_PROFILE_STATUS_TRUE_MATCH:
  2666. //
  2667. // Write DockID, SerialNumber, DockState, and Caps into the current
  2668. // Hardware profile.
  2669. //
  2670. RtlInitUnicodeString (&CurrentName,
  2671. CM_HARDWARE_PROFILE_STR_CURRENT_DOCK_INFO);
  2672. InitializeObjectAttributes (&Attributes,
  2673. &CurrentName,
  2674. OBJ_CASE_INSENSITIVE,
  2675. IDConfigDB,
  2676. NULL);
  2677. Status = NtCreateKey (&CurrentHandle,
  2678. KEY_READ | KEY_WRITE,
  2679. &Attributes,
  2680. 0,
  2681. NULL,
  2682. REG_OPTION_VOLATILE,
  2683. &Disposition);
  2684. ASSERT (STATUS_SUCCESS == Status);
  2685. Status = CmpAddDockingInfo (CurrentHandle, &extension->Profile);
  2686. NtClose(CurrentHandle);
  2687. if (HW_PROFILE_DOCKSTATE_UNDOCKED == extension->Profile.DockingState) {
  2688. signalAcpiEvent = TRUE;
  2689. }
  2690. break;
  2691. case HW_PROFILE_STATUS_SUCCESS:
  2692. case HW_PROFILE_STATUS_FAILURE:
  2693. break;
  2694. default:
  2695. ASSERTMSG ("Invalid Profile status state", FALSE);
  2696. }
  2697. }
  2698. //
  2699. // Create the symbolic link.
  2700. //
  2701. RtlInitUnicodeString(&CurrentName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current");
  2702. InitializeObjectAttributes(&Attributes,
  2703. &CurrentName,
  2704. OBJ_CASE_INSENSITIVE,
  2705. NULL,
  2706. NULL);
  2707. Status = NtCreateKey(&CurrentHandle,
  2708. KEY_CREATE_LINK,
  2709. &Attributes,
  2710. 0,
  2711. NULL,
  2712. REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
  2713. &Disposition);
  2714. if (!NT_SUCCESS(Status)) {
  2715. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: couldn't create Hardware Profile\\Current %08lx\n",Status));
  2716. } else {
  2717. ASSERT(Disposition == REG_CREATED_NEW_KEY);
  2718. sprintf(AsciiBuffer, "\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\%04d",HWProfile);
  2719. RtlInitAnsiString(&AnsiString, AsciiBuffer);
  2720. CurrentName.MaximumLength = sizeof(UnicodeBuffer);
  2721. CurrentName.Buffer = UnicodeBuffer;
  2722. Status = RtlAnsiStringToUnicodeString(&CurrentName,
  2723. &AnsiString,
  2724. FALSE);
  2725. ASSERT (STATUS_SUCCESS == Status);
  2726. Status = NtSetValueKey(CurrentHandle,
  2727. &CmSymbolicLinkValueName,
  2728. 0,
  2729. REG_LINK,
  2730. CurrentName.Buffer,
  2731. CurrentName.Length);
  2732. NtClose(CurrentHandle);
  2733. if (!NT_SUCCESS(Status)) {
  2734. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCreateControlSet: couldn't create symbolic link "));
  2735. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"to %wZ\n",&CurrentName));
  2736. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," Status=%08lx\n",Status));
  2737. }
  2738. }
  2739. if (signalAcpiEvent) {
  2740. //
  2741. // We are booting in the undocked state.
  2742. // This is interesting because our buddies in PnP cannot tell
  2743. // us when we are booting without a dock. They can only tell
  2744. // us when they see a hot undock.
  2745. //
  2746. // Therefore in the interest of matching a boot undocked with
  2747. // a hot undock, we need to simulate an acpi undock event.
  2748. //
  2749. PROFILE_ACPI_DOCKING_STATE newDockState;
  2750. HANDLE profile;
  2751. BOOLEAN changed;
  2752. newDockState.DockingState = HW_PROFILE_DOCKSTATE_UNDOCKED;
  2753. newDockState.SerialLength = 2;
  2754. newDockState.SerialNumber[0] = L'\0';
  2755. Status = CmSetAcpiHwProfile (&newDockState,
  2756. CmpHwprofileDefaultSelect,
  2757. NULL,
  2758. &profile,
  2759. &changed);
  2760. ASSERT (NT_SUCCESS (Status));
  2761. NtClose (profile);
  2762. }
  2763. Cleanup:
  2764. if (IDConfigDB) {
  2765. NtClose (IDConfigDB);
  2766. }
  2767. if (CurrentProfile) {
  2768. NtClose (CurrentProfile);
  2769. }
  2770. if (ParentOfProfile) {
  2771. NtClose (ParentOfProfile);
  2772. }
  2773. return(STATUS_SUCCESS);
  2774. }
  2775. NTSTATUS
  2776. CmpCloneControlSet(
  2777. VOID
  2778. )
  2779. /*++
  2780. Routine Description:
  2781. First, create a new hive, \registry\machine\clone, which will be
  2782. HIVE_VOLATILE.
  2783. Second, link \Registry\Machine\System\Clone to it.
  2784. Third, tree copy \Registry\Machine\System\CurrentControlSet into
  2785. \Registry\Machine\System\Clone (and thus into the clone hive.)
  2786. When the service controller is done with the clone hive, it can
  2787. simply NtUnloadKey it to free its storage.
  2788. Arguments:
  2789. None. \Registry\Machine\System\CurrentControlSet must already exist.
  2790. Return Value:
  2791. NTSTATUS
  2792. --*/
  2793. {
  2794. UNICODE_STRING Current;
  2795. UNICODE_STRING Clone;
  2796. HANDLE CurrentHandle;
  2797. HANDLE CloneHandle;
  2798. OBJECT_ATTRIBUTES Attributes;
  2799. NTSTATUS Status;
  2800. PCM_KEY_BODY CurrentKey;
  2801. PCM_KEY_BODY CloneKey;
  2802. ULONG Disposition;
  2803. PSECURITY_DESCRIPTOR Security;
  2804. ULONG SecurityLength;
  2805. PAGED_CODE();
  2806. RtlInitUnicodeString(&Current,
  2807. L"\\Registry\\Machine\\System\\CurrentControlSet");
  2808. RtlInitUnicodeString(&Clone,
  2809. L"\\Registry\\Machine\\System\\Clone");
  2810. InitializeObjectAttributes(&Attributes,
  2811. &Current,
  2812. OBJ_CASE_INSENSITIVE,
  2813. NULL,
  2814. NULL);
  2815. Status = NtOpenKey(&CurrentHandle,
  2816. KEY_READ,
  2817. &Attributes);
  2818. if (!NT_SUCCESS(Status)) {
  2819. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet couldn't open CurrentControlSet %08lx\n",Status));
  2820. return(Status);
  2821. }
  2822. //
  2823. // Get the security descriptor from the key so we can create the clone
  2824. // tree with the correct ACL.
  2825. //
  2826. Status = NtQuerySecurityObject(CurrentHandle,
  2827. DACL_SECURITY_INFORMATION,
  2828. NULL,
  2829. 0,
  2830. &SecurityLength);
  2831. if (Status==STATUS_BUFFER_TOO_SMALL) {
  2832. Security=ExAllocatePool(PagedPool,SecurityLength);
  2833. if (Security!=NULL) {
  2834. Status = NtQuerySecurityObject(CurrentHandle,
  2835. DACL_SECURITY_INFORMATION,
  2836. Security,
  2837. SecurityLength,
  2838. &SecurityLength);
  2839. if (!NT_SUCCESS(Status)) {
  2840. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet - NtQuerySecurityObject failed %08lx\n",Status));
  2841. ExFreePool(Security);
  2842. Security=NULL;
  2843. }
  2844. }
  2845. } else {
  2846. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet - NtQuerySecurityObject returned %08lx\n",Status));
  2847. Security=NULL;
  2848. }
  2849. InitializeObjectAttributes(&Attributes,
  2850. &Clone,
  2851. OBJ_CASE_INSENSITIVE,
  2852. NULL,
  2853. Security);
  2854. Status = NtCreateKey(&CloneHandle,
  2855. KEY_READ | KEY_WRITE,
  2856. &Attributes,
  2857. 0,
  2858. NULL,
  2859. REG_OPTION_VOLATILE,
  2860. &Disposition);
  2861. if (Security!=NULL) {
  2862. ExFreePool(Security);
  2863. }
  2864. if (!NT_SUCCESS(Status)) {
  2865. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet couldn't create Clone %08lx\n",Status));
  2866. NtClose(CurrentHandle);
  2867. return(Status);
  2868. }
  2869. //
  2870. // Check to make sure the key was created. If it already exists,
  2871. // something is wrong.
  2872. //
  2873. if (Disposition != REG_CREATED_NEW_KEY) {
  2874. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet: Clone tree already exists!\n"));
  2875. //
  2876. // WARNNOTE:
  2877. // If somebody somehow managed to create a key in our way,
  2878. // they'll thwart last known good. Tough luck.
  2879. // Claim it worked and go on.
  2880. //
  2881. Status = STATUS_SUCCESS;
  2882. goto Exit;
  2883. }
  2884. Status = ObReferenceObjectByHandle(CurrentHandle,
  2885. KEY_READ,
  2886. CmpKeyObjectType,
  2887. KernelMode,
  2888. (PVOID *)(&CurrentKey),
  2889. NULL);
  2890. if (!NT_SUCCESS(Status)) {
  2891. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet: couldn't reference CurrentHandle %08lx\n",Status));
  2892. goto Exit;
  2893. }
  2894. Status = ObReferenceObjectByHandle(CloneHandle,
  2895. KEY_WRITE,
  2896. CmpKeyObjectType,
  2897. KernelMode,
  2898. (PVOID *)(&CloneKey),
  2899. NULL);
  2900. if (!NT_SUCCESS(Status)) {
  2901. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet: couldn't reference CurrentHandle %08lx\n",Status));
  2902. ObDereferenceObject((PVOID)CurrentKey);
  2903. goto Exit;
  2904. }
  2905. CmpLockRegistryExclusive();
  2906. if (CmpCopyTree(CurrentKey->KeyControlBlock->KeyHive,
  2907. CurrentKey->KeyControlBlock->KeyCell,
  2908. CloneKey->KeyControlBlock->KeyHive,
  2909. CloneKey->KeyControlBlock->KeyCell)) {
  2910. //
  2911. // Set the max subkey name property for the new target key.
  2912. //
  2913. CmpRebuildKcbCache(CloneKey->KeyControlBlock);
  2914. Status = STATUS_SUCCESS;
  2915. } else {
  2916. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpCloneControlSet: tree copy failed.\n"));
  2917. Status = STATUS_REGISTRY_CORRUPT;
  2918. }
  2919. CmpUnlockRegistry();
  2920. ObDereferenceObject((PVOID)CurrentKey);
  2921. ObDereferenceObject((PVOID)CloneKey);
  2922. Exit:
  2923. NtClose(CurrentHandle);
  2924. NtClose(CloneHandle);
  2925. return(Status);
  2926. }
  2927. NTSTATUS
  2928. CmpSaveBootControlSet(USHORT ControlSetNum)
  2929. /*++
  2930. Routine Description:
  2931. This routine is responsible for saving the control set
  2932. used to accomplish the latest boot into a different control
  2933. set (presumably so that the different control set may be
  2934. marked as the LKG control set).
  2935. This routine is called from NtInitializeRegistry when
  2936. a boot is accepted via that routine.
  2937. Arguments:
  2938. ControlSetNum - The number of the control set that will
  2939. be used to save the boot control set.
  2940. Return Value:
  2941. NTSTATUS result code from call, among the following:
  2942. STATUS_SUCCESS - everything worked perfectly
  2943. STATUS_REGISTRY_CORRUPT - could not save the boot control set,
  2944. it is likely that the copy or sync
  2945. operation used for this save failed
  2946. and some part of the boot control
  2947. set was not saved.
  2948. --*/
  2949. {
  2950. UNICODE_STRING SavedBoot, Boot;
  2951. HANDLE BootHandle, SavedBootHandle;
  2952. OBJECT_ATTRIBUTES Attributes;
  2953. NTSTATUS Status;
  2954. PCM_KEY_BODY BootKey, SavedBootKey;
  2955. ULONG Disposition;
  2956. PSECURITY_DESCRIPTOR Security;
  2957. ULONG SecurityLength;
  2958. BOOLEAN CopyRet;
  2959. WCHAR Buffer[128];
  2960. //
  2961. // Figure out where the boot control set is
  2962. //
  2963. #if CLONE_CONTROL_SET
  2964. //
  2965. // If we have cloned the control set, then use the clone
  2966. // since it is guaranteed to have an untouched copy of the
  2967. // boot control set
  2968. //
  2969. RtlInitUnicodeString(&Boot,
  2970. L"\\Registry\\Machine\\System\\Clone");
  2971. InitializeObjectAttributes(&Attributes,
  2972. &Boot,
  2973. OBJ_CASE_INSENSITIVE,
  2974. NULL,
  2975. NULL);
  2976. #else
  2977. //
  2978. // If we are not using the clone, then just use the
  2979. // current control set.
  2980. //
  2981. InitializeObjectAttributes(&Attributes,
  2982. &CmRegistryMachineSystemCurrentControlSet,
  2983. OBJ_CASE_INSENSITIVE,
  2984. NULL,
  2985. NULL);
  2986. #endif
  2987. //
  2988. // Open the boot control set
  2989. //
  2990. Status = NtOpenKey(&BootHandle,
  2991. KEY_READ,
  2992. &Attributes);
  2993. if (!NT_SUCCESS(Status)) return(Status);
  2994. //
  2995. // We may be saving the boot control set into a brand new
  2996. // tree that we will create. If this is true, then we will
  2997. // need to create the root node of this tree below
  2998. // and give it the right security descriptor. So, we fish
  2999. // the security descriptor out of the root node of the
  3000. // boot control set tree.
  3001. //
  3002. Status = NtQuerySecurityObject(BootHandle,
  3003. DACL_SECURITY_INFORMATION,
  3004. NULL,
  3005. 0,
  3006. &SecurityLength);
  3007. if (Status==STATUS_BUFFER_TOO_SMALL) {
  3008. Security=ExAllocatePool(PagedPool,SecurityLength);
  3009. if (Security!=NULL) {
  3010. Status = NtQuerySecurityObject(BootHandle,
  3011. DACL_SECURITY_INFORMATION,
  3012. Security,
  3013. SecurityLength,
  3014. &SecurityLength);
  3015. if (!NT_SUCCESS(Status)) {
  3016. ExFreePool(Security);
  3017. Security=NULL;
  3018. }
  3019. }
  3020. } else {
  3021. Security=NULL;
  3022. }
  3023. //
  3024. // Now, create the path of the control set we will be saving to
  3025. //
  3026. swprintf(Buffer, L"\\Registry\\Machine\\System\\ControlSet%03d", ControlSetNum);
  3027. RtlInitUnicodeString(&SavedBoot,
  3028. Buffer);
  3029. //
  3030. // Open/Create the control set to which we are saving
  3031. //
  3032. InitializeObjectAttributes(&Attributes,
  3033. &SavedBoot,
  3034. OBJ_CASE_INSENSITIVE,
  3035. NULL,
  3036. Security);
  3037. Status = NtCreateKey(&SavedBootHandle,
  3038. KEY_READ | KEY_WRITE,
  3039. &Attributes,
  3040. 0,
  3041. NULL,
  3042. REG_OPTION_NON_VOLATILE,
  3043. &Disposition);
  3044. if (Security) ExFreePool(Security);
  3045. if (!NT_SUCCESS(Status)) {
  3046. NtClose(BootHandle);
  3047. return(Status);
  3048. }
  3049. //
  3050. // Get the key objects for out two controls
  3051. //
  3052. Status = ObReferenceObjectByHandle(BootHandle,
  3053. KEY_READ,
  3054. CmpKeyObjectType,
  3055. KernelMode,
  3056. (PVOID *)(&BootKey),
  3057. NULL);
  3058. if (!NT_SUCCESS(Status)) goto Exit;
  3059. Status = ObReferenceObjectByHandle(SavedBootHandle,
  3060. KEY_WRITE,
  3061. CmpKeyObjectType,
  3062. KernelMode,
  3063. (PVOID *)(&SavedBootKey),
  3064. NULL);
  3065. if (!NT_SUCCESS(Status)) {
  3066. ObDereferenceObject((PVOID)BootKey);
  3067. goto Exit;
  3068. }
  3069. //
  3070. // Lock the registry and do the actual saving
  3071. //
  3072. CmpLockRegistryExclusive();
  3073. if (Disposition == REG_CREATED_NEW_KEY) {
  3074. //
  3075. // If we are saving to a control set that we have just
  3076. // created, it is most efficient to just copy
  3077. // the boot control set tree into the new control set.
  3078. //
  3079. //
  3080. // N.B. We copy the volatile keys only if we are using
  3081. // a clone and thus our boot control set tree is
  3082. // composed only of volatile keys.
  3083. //
  3084. CopyRet = CmpCopyTreeEx(BootKey->KeyControlBlock->KeyHive,
  3085. BootKey->KeyControlBlock->KeyCell,
  3086. SavedBootKey->KeyControlBlock->KeyHive,
  3087. SavedBootKey->KeyControlBlock->KeyCell,
  3088. CLONE_CONTROL_SET);
  3089. //
  3090. // Set the max subkey name property for the new target key.
  3091. //
  3092. CmpRebuildKcbCache(SavedBootKey->KeyControlBlock);
  3093. } else {
  3094. //
  3095. // If we are saving to a control set that already exists
  3096. // then its likely that this control set is nearly identical
  3097. // to the boot control set (control sets don't change much
  3098. // between boots).
  3099. //
  3100. // Furthermore, the control set we are saving to must be old
  3101. // and hence has not been modified at all since it ceased
  3102. // being a current control set.
  3103. //
  3104. // Thus, it is most efficient for us to simply synchronize
  3105. // the target control set with the boot control set.
  3106. //
  3107. //
  3108. // N.B. We sync the volatile keys only if we are using
  3109. // a clone for the same reasons as stated above.
  3110. //
  3111. CopyRet = CmpSyncTrees(BootKey->KeyControlBlock->KeyHive,
  3112. BootKey->KeyControlBlock->KeyCell,
  3113. SavedBootKey->KeyControlBlock->KeyHive,
  3114. SavedBootKey->KeyControlBlock->KeyCell,
  3115. CLONE_CONTROL_SET);
  3116. CmpRebuildKcbCache(SavedBootKey->KeyControlBlock);
  3117. }
  3118. //
  3119. // Check if the Copy/Sync succeeded and adjust our return code
  3120. // accordingly.
  3121. //
  3122. if (CopyRet) {
  3123. Status = STATUS_SUCCESS;
  3124. } else {
  3125. Status = STATUS_REGISTRY_CORRUPT;
  3126. }
  3127. //
  3128. // All done. Clean up.
  3129. //
  3130. CmpUnlockRegistry();
  3131. ObDereferenceObject((PVOID)BootKey);
  3132. ObDereferenceObject((PVOID)SavedBootKey);
  3133. Exit:
  3134. NtClose(BootHandle);
  3135. NtClose(SavedBootHandle);
  3136. #if CLONE_CONTROL_SET
  3137. //
  3138. // If we have been using a clone, then the clone is no longer
  3139. // needed since we have saved its contents into a non-volatile
  3140. // control set. Thus, we can just erase it.
  3141. //
  3142. if(NT_SUCCESS(Status))
  3143. {
  3144. CmpDeleteCloneTree();
  3145. }
  3146. #endif
  3147. return(Status);
  3148. }
  3149. NTSTATUS
  3150. CmpDeleteCloneTree()
  3151. /*++
  3152. Routine Description:
  3153. Deletes the cloned CurrentControlSet by unloading the CLONE hive.
  3154. Arguments:
  3155. NONE.
  3156. Return Value:
  3157. NTSTATUS return from NtUnloadKey.
  3158. --*/
  3159. {
  3160. OBJECT_ATTRIBUTES Obja;
  3161. InitializeObjectAttributes(
  3162. &Obja,
  3163. &CmRegistrySystemCloneName,
  3164. OBJ_CASE_INSENSITIVE,
  3165. (HANDLE)NULL,
  3166. NULL);
  3167. return NtUnloadKey(&Obja);
  3168. }
  3169. VOID
  3170. CmBootLastKnownGood(
  3171. ULONG ErrorLevel
  3172. )
  3173. /*++
  3174. Routine Description:
  3175. This function is called to indicate a failure during the boot process.
  3176. The actual result is based on the value of ErrorLevel:
  3177. IGNORE - Will return, boot should proceed
  3178. NORMAL - Will return, boot should proceed
  3179. SEVERE - If not booting LastKnownGood, will switch to LastKnownGood
  3180. and reboot the system.
  3181. If already booting LastKnownGood, will return. Boot should
  3182. proceed.
  3183. CRITICAL - If not booting LastKnownGood, will switch to LastKnownGood
  3184. and reboot the system.
  3185. If already booting LastKnownGood, will bugcheck.
  3186. Arguments:
  3187. ErrorLevel - Supplies the severity level of the failure
  3188. Return Value:
  3189. None. If it returns, boot should proceed. May cause the system to
  3190. reboot.
  3191. --*/
  3192. {
  3193. ARC_STATUS Status;
  3194. PAGED_CODE();
  3195. if (CmFirstTime != TRUE) {
  3196. //
  3197. // NtInitializeRegistry has been called, so handling
  3198. // driver errors is not a task for ScReg.
  3199. // Treat all errors as Normal
  3200. //
  3201. return;
  3202. }
  3203. switch (ErrorLevel) {
  3204. case NormalError:
  3205. case IgnoreError:
  3206. break;
  3207. case SevereError:
  3208. if (CmIsLastKnownGoodBoot()) {
  3209. break;
  3210. } else {
  3211. Status = HalSetEnvironmentVariable("LastKnownGood", "TRUE");
  3212. if (Status == ESUCCESS) {
  3213. HalReturnToFirmware(HalRebootRoutine);
  3214. }
  3215. }
  3216. break;
  3217. case CriticalError:
  3218. if (CmIsLastKnownGoodBoot()) {
  3219. CM_BUGCHECK( CRITICAL_SERVICE_FAILED, BAD_LAST_KNOWN_GOOD, 1, 0, 0 );
  3220. } else {
  3221. Status = HalSetEnvironmentVariable("LastKnownGood", "TRUE");
  3222. if (Status == ESUCCESS) {
  3223. HalReturnToFirmware(HalRebootRoutine);
  3224. } else {
  3225. CM_BUGCHECK( SET_ENV_VAR_FAILED, BAD_LAST_KNOWN_GOOD, 2, 0, 0 );
  3226. }
  3227. }
  3228. break;
  3229. }
  3230. return;
  3231. }
  3232. BOOLEAN
  3233. CmIsLastKnownGoodBoot(
  3234. VOID
  3235. )
  3236. /*++
  3237. Routine Description:
  3238. Determines whether the current system boot is a LastKnownGood boot or
  3239. not. It does this by comparing the following two values:
  3240. \registry\machine\system\select:Current
  3241. \registry\machine\system\select:LastKnownGood
  3242. If both of these values refer to the same control set, and this control
  3243. set is different from:
  3244. \registry\machine\system\select:Default
  3245. we are booting LastKnownGood.
  3246. Arguments:
  3247. None.
  3248. Return Value:
  3249. TRUE - Booting LastKnownGood
  3250. FALSE - Not booting LastKnownGood
  3251. --*/
  3252. {
  3253. NTSTATUS Status;
  3254. ULONG Default;
  3255. ULONG Current;
  3256. ULONG LKG;
  3257. RTL_QUERY_REGISTRY_TABLE QueryTable[] = {
  3258. {NULL, RTL_QUERY_REGISTRY_DIRECT,
  3259. L"Current", &Current,
  3260. REG_DWORD, (PVOID)&Current, 0 },
  3261. {NULL, RTL_QUERY_REGISTRY_DIRECT,
  3262. L"LastKnownGood", &LKG,
  3263. REG_DWORD, (PVOID)&LKG, 0 },
  3264. {NULL, RTL_QUERY_REGISTRY_DIRECT,
  3265. L"Default", &Default,
  3266. REG_DWORD, (PVOID)&Default, 0 },
  3267. {NULL, 0,
  3268. NULL, NULL,
  3269. REG_NONE, NULL, 0 }
  3270. };
  3271. PAGED_CODE();
  3272. Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
  3273. L"\\Registry\\Machine\\System\\Select",
  3274. QueryTable,
  3275. NULL,
  3276. NULL);
  3277. //
  3278. // If this failed, something is severely wrong.
  3279. //
  3280. ASSERT(NT_SUCCESS(Status));
  3281. if (!NT_SUCCESS(Status)) {
  3282. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmIsLastKnownGoodBoot: RtlQueryRegistryValues "));
  3283. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"failed, Status %08lx\n", Status));
  3284. return(FALSE);
  3285. }
  3286. if ((LKG == Current) && (Current != Default)){
  3287. return(TRUE);
  3288. } else {
  3289. return(FALSE);
  3290. }
  3291. }
  3292. BOOLEAN
  3293. CmpLinkKeyToHive(
  3294. PWSTR KeyPath,
  3295. PWSTR HivePath
  3296. )
  3297. /*++
  3298. Routine Description:
  3299. Creates a symbolic link at KeyPath that points to HivePath.
  3300. Arguments:
  3301. KeyPath - pointer to unicode string with name of key
  3302. (e.g. L"\\Registry\\Machine\\Security\\SAM")
  3303. HivePath - pointer to unicode string with name of hive root
  3304. (e.g. L"\\Registry\\Machine\\SAM\\SAM")
  3305. Return Value:
  3306. TRUE if links were successfully created, FALSE otherwise
  3307. --*/
  3308. {
  3309. UNICODE_STRING KeyName;
  3310. UNICODE_STRING LinkName;
  3311. OBJECT_ATTRIBUTES Attributes;
  3312. HANDLE LinkHandle;
  3313. ULONG Disposition;
  3314. NTSTATUS Status;
  3315. PAGED_CODE();
  3316. //
  3317. // Create link for CLONE hive
  3318. //
  3319. RtlInitUnicodeString(&KeyName, KeyPath);
  3320. InitializeObjectAttributes(&Attributes,
  3321. &KeyName,
  3322. OBJ_CASE_INSENSITIVE,
  3323. NULL,
  3324. NULL);
  3325. Status = NtCreateKey(&LinkHandle,
  3326. KEY_CREATE_LINK,
  3327. &Attributes,
  3328. 0,
  3329. NULL,
  3330. REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
  3331. &Disposition);
  3332. if (!NT_SUCCESS(Status)) {
  3333. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpLinkKeyToHive: couldn't create %S\n", &KeyName));
  3334. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," Status = %08lx\n",Status));
  3335. return(FALSE);
  3336. }
  3337. //
  3338. // Check to make sure that the key was created, not just opened. Since
  3339. // this key is always created volatile, it should never be present in
  3340. // the hive when we boot.
  3341. //
  3342. if (Disposition != REG_CREATED_NEW_KEY) {
  3343. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpLinkKeyToHive: %S already exists!\n", &KeyName));
  3344. NtClose(LinkHandle);
  3345. return(FALSE);
  3346. }
  3347. RtlInitUnicodeString(&LinkName, HivePath);
  3348. Status = NtSetValueKey(LinkHandle,
  3349. &CmSymbolicLinkValueName,
  3350. 0,
  3351. REG_LINK,
  3352. LinkName.Buffer,
  3353. LinkName.Length);
  3354. NtClose(LinkHandle);
  3355. if (!NT_SUCCESS(Status)) {
  3356. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CM: CmpLinkKeyToHive: couldn't create symbolic link for %S\n", HivePath));
  3357. return(FALSE);
  3358. }
  3359. return(TRUE);
  3360. }
  3361. VOID
  3362. CmpCreatePerfKeys(
  3363. VOID
  3364. )
  3365. /*++
  3366. Routine Description:
  3367. Creates predefined keys for the performance text to support old apps on 1.0a
  3368. Arguments:
  3369. None.
  3370. Return Value:
  3371. None.
  3372. --*/
  3373. {
  3374. HANDLE Perflib;
  3375. NTSTATUS Status;
  3376. WCHAR LanguageId[4];
  3377. OBJECT_ATTRIBUTES Attributes;
  3378. UNICODE_STRING String;
  3379. USHORT Language;
  3380. LONG i;
  3381. WCHAR c;
  3382. extern PWCHAR CmpRegistryPerflibString;
  3383. RtlInitUnicodeString(&String, CmpRegistryPerflibString);
  3384. InitializeObjectAttributes(&Attributes,
  3385. &String,
  3386. OBJ_CASE_INSENSITIVE,
  3387. NULL,
  3388. NULL);
  3389. Status = NtOpenKey(&Perflib,
  3390. KEY_WRITE,
  3391. &Attributes);
  3392. if (!NT_SUCCESS(Status)) {
  3393. return;
  3394. }
  3395. //
  3396. // Always create the predefined keys for the english language
  3397. //
  3398. CmpCreatePredefined(Perflib,
  3399. L"009",
  3400. HKEY_PERFORMANCE_TEXT);
  3401. //
  3402. // If the default language is not english, create a predefined key for
  3403. // that, too.
  3404. //
  3405. if (PsDefaultSystemLocaleId != 0x00000409) {
  3406. Language = LANGIDFROMLCID(PsDefaultSystemLocaleId) & 0xff;
  3407. LanguageId[3] = L'\0';
  3408. for (i=2;i>=0;i--) {
  3409. c = Language % 16;
  3410. if (c>9) {
  3411. LanguageId[i]= c+L'A'-10;
  3412. } else {
  3413. LanguageId[i]= c+L'0';
  3414. }
  3415. Language = Language >> 4;
  3416. }
  3417. CmpCreatePredefined(Perflib,
  3418. LanguageId,
  3419. HKEY_PERFORMANCE_NLSTEXT);
  3420. }
  3421. }
  3422. VOID
  3423. CmpCreatePredefined(
  3424. IN HANDLE Root,
  3425. IN PWSTR KeyName,
  3426. IN HANDLE PredefinedHandle
  3427. )
  3428. /*++
  3429. Routine Description:
  3430. Creates a special key that will always return the given predefined handle
  3431. instead of a real handle.
  3432. Arguments:
  3433. Root - supplies the handle the keyname is relative to
  3434. KeyName - supplies the name of the key.
  3435. PredefinedHandle - supplies the predefined handle to be returned when this
  3436. key is opened.
  3437. Return Value:
  3438. None.
  3439. --*/
  3440. {
  3441. OBJECT_ATTRIBUTES ObjectAttributes;
  3442. CM_PARSE_CONTEXT ParseContext;
  3443. NTSTATUS Status;
  3444. UNICODE_STRING Name;
  3445. HANDLE Handle;
  3446. ParseContext.Class.Length = 0;
  3447. ParseContext.Class.Buffer = NULL;
  3448. ParseContext.TitleIndex = 0;
  3449. ParseContext.CreateOptions = REG_OPTION_VOLATILE | REG_OPTION_PREDEF_HANDLE;
  3450. ParseContext.Disposition = 0;
  3451. ParseContext.CreateLink = FALSE;
  3452. ParseContext.PredefinedHandle = PredefinedHandle;
  3453. RtlInitUnicodeString(&Name, KeyName);
  3454. InitializeObjectAttributes(&ObjectAttributes,
  3455. &Name,
  3456. OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  3457. Root,
  3458. NULL);
  3459. Status = ObOpenObjectByName(&ObjectAttributes,
  3460. CmpKeyObjectType,
  3461. KernelMode,
  3462. NULL,
  3463. KEY_READ,
  3464. (PVOID)&ParseContext,
  3465. &Handle);
  3466. ASSERT(CmpMiniNTBoot || NT_SUCCESS(Status));
  3467. if (NT_SUCCESS(Status))
  3468. ZwClose(Handle);
  3469. }
  3470. BOOLEAN CmpSystemHiveConversionFailed = FALSE;
  3471. NTSTATUS
  3472. CmpSetupPrivateWrite(
  3473. PCMHIVE CmHive
  3474. )
  3475. /*++
  3476. Routine Description:
  3477. Converts the primary file to private write stream
  3478. Arguments:
  3479. CmHive - hive to convert, tipically SYSTEM
  3480. Return Value:
  3481. NONE; bugchecks if something wrong
  3482. --*/
  3483. {
  3484. ULONG FileOffset;
  3485. ULONG Data;
  3486. NTSTATUS Status;
  3487. PAGED_CODE()
  3488. //
  3489. // We need to issue a read from the file, to trigger the cache initialization
  3490. //
  3491. FileOffset = 0;
  3492. if ( ! (((PHHIVE)CmHive)->FileRead)(
  3493. (PHHIVE)CmHive,
  3494. HFILE_TYPE_PRIMARY,
  3495. &FileOffset,
  3496. (PVOID)&Data,
  3497. sizeof(ULONG)
  3498. )
  3499. )
  3500. {
  3501. return STATUS_REGISTRY_IO_FAILED;
  3502. }
  3503. //
  3504. // Aquire the file object for the primary; This should be called AFTER the
  3505. // cache has been initialized.
  3506. //
  3507. Status = CmpAquireFileObjectForFile(CmHive,CmHive->FileHandles[HFILE_TYPE_PRIMARY],&(CmHive->FileObject));
  3508. if( !NT_SUCCESS(Status) ) {
  3509. return Status;
  3510. }
  3511. //
  3512. // set the getCell and releaseCell routines to the right one(s)
  3513. //
  3514. CmHive->Hive.GetCellRoutine = HvpGetCellMapped;
  3515. CmHive->Hive.ReleaseCellRoutine = HvpReleaseCellMapped;
  3516. return STATUS_SUCCESS;
  3517. }
  3518. //
  3519. // This thread is used to load the machine hives in paralel
  3520. //
  3521. extern ULONG CmpCheckHiveIndex;
  3522. VOID
  3523. CmpLoadHiveThread(
  3524. IN PVOID StartContext
  3525. )
  3526. /*++
  3527. Routine Description:
  3528. Loads the hive at index StartContext in CmpMachineHiveList
  3529. Warning. We need to protect when enlisting the hives in CmpHiveListHead !!!
  3530. Arguments:
  3531. Return Value:
  3532. --*/
  3533. {
  3534. UCHAR FileBuffer[MAX_NAME];
  3535. UCHAR RegBuffer[MAX_NAME];
  3536. UNICODE_STRING TempName;
  3537. UNICODE_STRING FileName;
  3538. UNICODE_STRING RegName;
  3539. USHORT FileStart;
  3540. USHORT RegStart;
  3541. ULONG i;
  3542. PCMHIVE CmHive;
  3543. HANDLE PrimaryHandle;
  3544. HANDLE LogHandle;
  3545. ULONG PrimaryDisposition;
  3546. ULONG SecondaryDisposition;
  3547. ULONG Length;
  3548. NTSTATUS Status;
  3549. BOOLEAN RegistryLocked = TRUE;
  3550. PVOID ErrorParameters;
  3551. ULONG ErrorResponse;
  3552. ULONG ClusterSize;
  3553. ULONG LocalWorkerIncrement;
  3554. PAGED_CODE();
  3555. i = (ULONG)(ULONG_PTR)StartContext;
  3556. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmpLoadHiveThread %i ... starting\n",i));
  3557. ASSERT( CmpMachineHiveList[i].Name != NULL );
  3558. if( i == CmpCheckHiveIndex ) {
  3559. //
  3560. // we want to hold this thread until all the others finish, so we have a chance to debug it.
  3561. // last one that finishes will wake us
  3562. //
  3563. KeWaitForSingleObject( &CmpLoadWorkerDebugEvent,
  3564. Executive,
  3565. KernelMode,
  3566. FALSE,
  3567. NULL );
  3568. ASSERT( CmpLoadWorkerIncrement == (CM_NUMBER_OF_MACHINE_HIVES - 1) );
  3569. DbgBreakPoint();
  3570. }
  3571. //
  3572. // signal that we have started
  3573. //
  3574. CmpMachineHiveList[i].ThreadStarted = TRUE;
  3575. FileName.MaximumLength = MAX_NAME;
  3576. FileName.Length = 0;
  3577. FileName.Buffer = (PWSTR)&(FileBuffer[0]);
  3578. RegName.MaximumLength = MAX_NAME;
  3579. RegName.Length = 0;
  3580. RegName.Buffer = (PWSTR)&(RegBuffer[0]);
  3581. RtlInitUnicodeString(
  3582. &TempName,
  3583. INIT_SYSTEMROOT_HIVEPATH
  3584. );
  3585. RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
  3586. FileStart = FileName.Length;
  3587. RtlInitUnicodeString(
  3588. &TempName,
  3589. INIT_REGISTRY_MASTERPATH
  3590. );
  3591. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  3592. RegStart = RegName.Length;
  3593. //
  3594. // Compute the name of the file, and the name to link to in
  3595. // the registry.
  3596. //
  3597. // REGISTRY
  3598. RegName.Length = RegStart;
  3599. RtlInitUnicodeString(
  3600. &TempName,
  3601. CmpMachineHiveList[i].BaseName
  3602. );
  3603. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  3604. // REGISTRY\MACHINE or REGISTRY\USER
  3605. if (RegName.Buffer[ (RegName.Length / sizeof( WCHAR )) - 1 ] == '\\') {
  3606. RtlInitUnicodeString(
  3607. &TempName,
  3608. CmpMachineHiveList[i].Name
  3609. );
  3610. RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
  3611. }
  3612. // REGISTRY\[MACHINE|USER]\HIVE
  3613. // <sysroot>\config
  3614. RtlInitUnicodeString(
  3615. &TempName,
  3616. CmpMachineHiveList[i].Name
  3617. );
  3618. FileName.Length = FileStart;
  3619. RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
  3620. // <sysroot>\config\hive
  3621. if (CmpMachineHiveList[i].CmHive == NULL) {
  3622. //
  3623. // Hive has not been inited in any way.
  3624. //
  3625. CmpMachineHiveList[i].Allocate = TRUE;
  3626. Status = CmpInitHiveFromFile(&FileName,
  3627. CmpMachineHiveList[i].Flags,
  3628. &CmHive,
  3629. &(CmpMachineHiveList[i].Allocate),
  3630. &RegistryLocked,
  3631. CM_CHECK_REGISTRY_CHECK_CLEAN
  3632. );
  3633. if ( (!NT_SUCCESS(Status)) ||
  3634. (!CmpShareSystemHives && (CmHive->FileHandles[HFILE_TYPE_LOG] == NULL)) )
  3635. {
  3636. ErrorParameters = &FileName;
  3637. ExRaiseHardError(
  3638. STATUS_CANNOT_LOAD_REGISTRY_FILE,
  3639. 1,
  3640. 1,
  3641. (PULONG_PTR)&ErrorParameters,
  3642. OptionOk,
  3643. &ErrorResponse
  3644. );
  3645. }
  3646. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmpInitializeHiveList:\n"));
  3647. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"\tCmHive for '%ws' @", CmpMachineHiveList[i]));
  3648. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"%08lx", CmHive));
  3649. CmpMachineHiveList[i].CmHive2 = CmHive;
  3650. /*
  3651. //
  3652. // Dragos: This cannot be done here; we need to do it one step at the time back in CmpInitializeHiveList
  3653. //
  3654. //
  3655. // Link hive into master hive
  3656. //
  3657. Status = CmpLinkHiveToMaster(
  3658. &RegName,
  3659. NULL,
  3660. CmHive,
  3661. Allocate,
  3662. SecurityDescriptor
  3663. );
  3664. if ( Status != STATUS_SUCCESS)
  3665. {
  3666. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpInitializeHiveList: "));
  3667. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmpLinkHiveToMaster failed\n"));
  3668. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"\ti=%d s='%ws'\n", i, CmpMachineHiveList[i]));
  3669. CM_BUGCHECK(CONFIG_LIST_FAILED,BAD_CORE_HIVE,Status,i,&RegName);
  3670. }
  3671. CmpAddToHiveFileList(CmHive);
  3672. if (Allocate) {
  3673. //
  3674. // I suspect this is the problem.
  3675. //HvSyncHive((PHHIVE)CmHive);
  3676. //
  3677. }
  3678. */
  3679. } else {
  3680. CmHive = CmpMachineHiveList[i].CmHive;
  3681. if (!(CmHive->Hive.HiveFlags & HIVE_VOLATILE)) {
  3682. //
  3683. // CmHive already exists. It is not an entirely volatile
  3684. // hive (we do nothing for those.)
  3685. //
  3686. // First, open the files (Primary and Alternate) that
  3687. // back the hive. Stuff their handles into the CmHive
  3688. // object. Force the size of the files to match the
  3689. // in memory images. Call HvSyncHive to write changes
  3690. // out to disk.
  3691. //
  3692. BOOLEAN NoBufering = FALSE; // first try to open it cached;
  3693. retryNoBufering:
  3694. Status = CmpOpenHiveFiles(&FileName,
  3695. L".LOG",
  3696. &PrimaryHandle,
  3697. &LogHandle,
  3698. &PrimaryDisposition,
  3699. &SecondaryDisposition,
  3700. TRUE,
  3701. TRUE,
  3702. NoBufering,
  3703. &ClusterSize);
  3704. if ( ( ! NT_SUCCESS(Status)) ||
  3705. (LogHandle == NULL) )
  3706. {
  3707. fatal:
  3708. ErrorParameters = &FileName;
  3709. ExRaiseHardError(
  3710. STATUS_CANNOT_LOAD_REGISTRY_FILE,
  3711. 1,
  3712. 1,
  3713. (PULONG_PTR)&ErrorParameters,
  3714. OptionOk,
  3715. &ErrorResponse
  3716. );
  3717. //
  3718. // WARNNOTE
  3719. // We've just told the user that something essential,
  3720. // like the SYSTEM hive, is hosed. Don't try to run,
  3721. // we just risk destroying user data. Punt.
  3722. //
  3723. CM_BUGCHECK(BAD_SYSTEM_CONFIG_INFO,BAD_HIVE_LIST,0,i,Status);
  3724. }
  3725. CmHive->FileHandles[HFILE_TYPE_LOG] = LogHandle;
  3726. CmHive->FileHandles[HFILE_TYPE_PRIMARY] = PrimaryHandle;
  3727. if( NoBufering == FALSE ) {
  3728. //
  3729. // intitialize cache and mark the stream as PRIVATE_WRITE;
  3730. // next flush will do the actual conversion
  3731. //
  3732. Status = CmpSetupPrivateWrite(CmHive);
  3733. }
  3734. if( !NT_SUCCESS(Status) ) {
  3735. if( (NoBufering == TRUE) || (Status != STATUS_RETRY) ) {
  3736. //
  3737. // we have tried both ways and it didn't work; bad luck
  3738. //
  3739. goto fatal;
  3740. }
  3741. #ifndef _CM_LDR_
  3742. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"Failed to convert SYSTEM hive to mapped (0x%lx) ... loading it in paged pool\n",Status);
  3743. #endif //_CM_LDR_
  3744. //
  3745. // close handle and make another attempt to open them without buffering
  3746. //
  3747. CmpTrackHiveClose = TRUE;
  3748. ZwClose(PrimaryHandle);
  3749. CmpTrackHiveClose = FALSE;
  3750. ZwClose(LogHandle);
  3751. NoBufering = TRUE;
  3752. goto retryNoBufering;
  3753. }
  3754. //
  3755. // now that we successfully opened the hive files, clear off the lazy flush flag
  3756. //
  3757. ASSERT( CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH );
  3758. CmHive->Hive.HiveFlags &= (~HIVE_NOLAZYFLUSH);
  3759. Length = CmHive->Hive.Storage[Stable].Length + HBLOCK_SIZE;
  3760. //
  3761. // When an in-memory hive is opened with no backing
  3762. // file, ClusterSize is assumed to be 1. When the file
  3763. // is opened later (for the SYSTEM hive) we need
  3764. // to update this field in the hive if we are
  3765. // booting from media where the cluster size > 1
  3766. //
  3767. if (CmHive->Hive.Cluster != ClusterSize) {
  3768. //
  3769. // The cluster size is different than previous assumed.
  3770. // Since a cluster in the dirty vector must be either
  3771. // completely dirty or completely clean, go through the
  3772. // dirty vector and mark all clusters that contain a dirty
  3773. // logical sector as completely dirty.
  3774. //
  3775. PRTL_BITMAP BitMap;
  3776. ULONG Index;
  3777. BitMap = &(CmHive->Hive.DirtyVector);
  3778. for (Index = 0;
  3779. Index < CmHive->Hive.DirtyVector.SizeOfBitMap;
  3780. Index += ClusterSize)
  3781. {
  3782. if (!RtlAreBitsClear (BitMap, Index, ClusterSize)) {
  3783. RtlSetBits (BitMap, Index, ClusterSize);
  3784. }
  3785. }
  3786. //
  3787. // Update DirtyCount and Cluster
  3788. //
  3789. CmHive->Hive.DirtyCount = RtlNumberOfSetBits(&CmHive->Hive.DirtyVector);
  3790. CmHive->Hive.Cluster = ClusterSize;
  3791. }
  3792. if (!CmpFileSetSize(
  3793. (PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length,Length)
  3794. )
  3795. {
  3796. //
  3797. // WARNNOTE
  3798. // Data written into the system hive since boot
  3799. // cannot be written out, punt.
  3800. //
  3801. CmpCannotWriteConfiguration = TRUE;
  3802. }
  3803. ASSERT(FIELD_OFFSET(CMHIVE, Hive) == 0);
  3804. if( CmHive->Hive.BaseBlock->BootRecover != 0 ) {
  3805. //
  3806. // boot loader recovered the hive; we need to flush it all to the disk
  3807. // mark everything dirty; the next flush will do take care of the rest
  3808. //
  3809. PRTL_BITMAP BitMap;
  3810. BitMap = &(CmHive->Hive.DirtyVector);
  3811. RtlSetAllBits(BitMap);
  3812. CmHive->Hive.DirtyCount = BitMap->SizeOfBitMap;
  3813. //
  3814. // we only need to flush the hive when the loader has recovered it
  3815. //
  3816. HvSyncHive((PHHIVE)CmHive);
  3817. }
  3818. CmpMachineHiveList[i].CmHive2 = CmHive;
  3819. ASSERT( CmpMachineHiveList[i].CmHive == CmpMachineHiveList[i].CmHive2 );
  3820. /*
  3821. Cannot do that here as it requires the registry lock
  3822. CmpAddToHiveFileList(CmpMachineHiveList[i].CmHive);
  3823. */
  3824. if( CmpCannotWriteConfiguration ) {
  3825. //
  3826. // The system disk is full; Give user a chance to log-on and make room
  3827. //
  3828. CmpDiskFullWarning();
  3829. }
  3830. //
  3831. // copy the full file name for the conversion worker thread
  3832. //
  3833. SystemHiveFullPathName.MaximumLength = MAX_NAME;
  3834. SystemHiveFullPathName.Length = 0;
  3835. SystemHiveFullPathName.Buffer = (PWSTR)&(SystemHiveFullPathBuffer[0]);
  3836. RtlAppendStringToString((PSTRING)&SystemHiveFullPathName, (PSTRING)&FileName);
  3837. } else if (CmpMiniNTBoot) {
  3838. //
  3839. // copy the full file name for the conversion worker thread
  3840. //
  3841. SystemHiveFullPathName.MaximumLength = MAX_NAME;
  3842. SystemHiveFullPathName.Length = 0;
  3843. SystemHiveFullPathName.Buffer = (PWSTR)&(SystemHiveFullPathBuffer[0]);
  3844. RtlAppendStringToString((PSTRING)&SystemHiveFullPathName, (PSTRING)&FileName);
  3845. }
  3846. if(i == SYSTEM_HIVE_INDEX) {
  3847. //
  3848. // marks the System\Select!Current value dirty so we preserve what was set by the loader.
  3849. //
  3850. CmpMarkCurrentValueDirty((PHHIVE)CmHive,CmHive->Hive.BaseBlock->RootCell);
  3851. }
  3852. }
  3853. CmpMachineHiveList[i].ThreadFinished = TRUE;
  3854. LocalWorkerIncrement = InterlockedIncrement (&CmpLoadWorkerIncrement);
  3855. if ( LocalWorkerIncrement == CM_NUMBER_OF_MACHINE_HIVES ) {
  3856. //
  3857. // this was the last thread (the lazyest); signal the main thread
  3858. //
  3859. KeSetEvent (&CmpLoadWorkerEvent, 0, FALSE);
  3860. }
  3861. if ( (LocalWorkerIncrement == (CM_NUMBER_OF_MACHINE_HIVES -1)) && // there is one more thread
  3862. (CmpCheckHiveIndex < CM_NUMBER_OF_MACHINE_HIVES ) // which is waiting to be debugged
  3863. ) {
  3864. //
  3865. // wake up the thread to be debugged
  3866. //
  3867. KeSetEvent (&CmpLoadWorkerDebugEvent, 0, FALSE);
  3868. }
  3869. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_INIT,"CmpLoadHiveThread %i ... terminating\n",i));
  3870. PsTerminateSystemThread(Status);
  3871. }
  3872. NTSTATUS
  3873. CmpSetNetworkValue(
  3874. IN PNETWORK_LOADER_BLOCK NetworkLoaderBlock
  3875. )
  3876. /*++
  3877. Routine Description:
  3878. This function will save the information in the Network Loader
  3879. Block to the registry.
  3880. Arguments:
  3881. NetworkLoaderBlock - Supplies a pointer to the network loader block
  3882. that was created by the OS Loader.
  3883. Return Value:
  3884. NTSTATUS code.
  3885. --*/
  3886. {
  3887. NTSTATUS status;
  3888. OBJECT_ATTRIBUTES objectAttributes;
  3889. UNICODE_STRING string;
  3890. HANDLE handle;
  3891. ULONG disposition;
  3892. ASSERT( NetworkLoaderBlock != NULL );
  3893. ASSERT( NetworkLoaderBlock->DHCPServerACKLength > 0 );
  3894. RtlInitUnicodeString( &string, L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control\\PXE" );
  3895. InitializeObjectAttributes(
  3896. &objectAttributes,
  3897. &string,
  3898. OBJ_CASE_INSENSITIVE,
  3899. NULL,
  3900. NULL
  3901. );
  3902. status = NtCreateKey(&handle,
  3903. KEY_ALL_ACCESS,
  3904. &objectAttributes,
  3905. 0,
  3906. (PUNICODE_STRING)NULL,
  3907. 0,
  3908. &disposition
  3909. );
  3910. if ( !NT_SUCCESS(status) ) {
  3911. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL, "CmpSetNetworkValue: Unable to open PXE key: %x\n", status ));
  3912. goto Error;
  3913. }
  3914. RtlInitUnicodeString( &string, L"DHCPServerACK" );
  3915. status = NtSetValueKey(handle,
  3916. &string,
  3917. 0,
  3918. REG_BINARY,
  3919. NetworkLoaderBlock->DHCPServerACK,
  3920. NetworkLoaderBlock->DHCPServerACKLength
  3921. );
  3922. if ( !NT_SUCCESS(status) ) {
  3923. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL, "CmpSetNetworkValue: Unable to set DHCPServerACK key: %x\n", status ));
  3924. goto Error;
  3925. }
  3926. RtlInitUnicodeString( &string, L"BootServerReply" );
  3927. status = NtSetValueKey(handle,
  3928. &string,
  3929. 0,
  3930. REG_BINARY,
  3931. NetworkLoaderBlock->BootServerReplyPacket,
  3932. NetworkLoaderBlock->BootServerReplyPacketLength
  3933. );
  3934. if ( !NT_SUCCESS(status) ) {
  3935. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL, "CmpSetNetworkValue: Unable to set BootServerReplyPacket key: %x\n", status ));
  3936. goto Error;
  3937. }
  3938. status = STATUS_SUCCESS;
  3939. Cleanup:
  3940. NtClose( handle );
  3941. return status;
  3942. Error:
  3943. goto Cleanup;
  3944. }
  3945. NTSTATUS
  3946. CmpSetSystemValues(
  3947. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  3948. )
  3949. /*++
  3950. Routine Description:
  3951. This function will save the system start information to
  3952. the registry.
  3953. Arguments:
  3954. LoaderBlock - Supplies a pointer to the loader block.
  3955. Return Value:
  3956. NTSTATUS code.
  3957. --*/
  3958. {
  3959. NTSTATUS status;
  3960. OBJECT_ATTRIBUTES objectAttributes;
  3961. UNICODE_STRING string;
  3962. UNICODE_STRING value;
  3963. HANDLE handle;
  3964. ASSERT( LoaderBlock != NULL );
  3965. value.Buffer = NULL;
  3966. //
  3967. // Open the control key
  3968. //
  3969. RtlInitUnicodeString( &string, L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control" );
  3970. InitializeObjectAttributes(
  3971. &objectAttributes,
  3972. &string,
  3973. OBJ_CASE_INSENSITIVE,
  3974. NULL,
  3975. NULL
  3976. );
  3977. status = NtOpenKey(
  3978. &handle,
  3979. KEY_ALL_ACCESS,
  3980. &objectAttributes
  3981. );
  3982. if ( !NT_SUCCESS(status) ) {
  3983. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpSetSystemValues: Unable to Open Control Key: %x\n", status ));
  3984. goto Error;
  3985. }
  3986. //
  3987. // Set the System start options key
  3988. //
  3989. RtlInitUnicodeString( &string, L"SystemStartOptions" );
  3990. status = NtSetValueKey (
  3991. handle,
  3992. &string,
  3993. 0,
  3994. REG_SZ,
  3995. CmpLoadOptions.Buffer,
  3996. CmpLoadOptions.Length
  3997. );
  3998. if ( !NT_SUCCESS(status) ) {
  3999. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpSetSystemValue: Unable to set SystemStartOptions key: %x\n", status ));
  4000. goto Error;
  4001. }
  4002. //
  4003. // Set the System Boot Device
  4004. //
  4005. RtlInitUnicodeString( &string, L"SystemBootDevice" );
  4006. RtlCreateUnicodeStringFromAsciiz( &value, LoaderBlock->ArcBootDeviceName );
  4007. status = NtSetValueKey(handle,
  4008. &string,
  4009. 0,
  4010. REG_SZ,
  4011. value.Buffer,
  4012. value.Length + sizeof(WCHAR)
  4013. );
  4014. if ( !NT_SUCCESS(status) ) {
  4015. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpSetSystemValue: Unable to set SystemBootDevice key: %x\n", status ));
  4016. goto Error;
  4017. }
  4018. status = STATUS_SUCCESS;
  4019. Cleanup:
  4020. if ( value.Buffer ) {
  4021. RtlFreeUnicodeString(&value);
  4022. }
  4023. NtClose( handle );
  4024. return status;
  4025. Error:
  4026. goto Cleanup;
  4027. }
  4028. VOID
  4029. CmpMarkCurrentValueDirty(
  4030. IN PHHIVE SystemHive,
  4031. IN HCELL_INDEX RootCell
  4032. )
  4033. {
  4034. PCM_KEY_NODE Node;
  4035. HCELL_INDEX Select;
  4036. UNICODE_STRING Name;
  4037. HCELL_INDEX ValueCell;
  4038. PAGED_CODE();
  4039. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  4040. //
  4041. // Find \SYSTEM\SELECT node.
  4042. //
  4043. Node = (PCM_KEY_NODE)HvGetCell(SystemHive,RootCell);
  4044. if( Node == NULL ) {
  4045. //
  4046. // we couldn't map a view for the bin containing this cell
  4047. //
  4048. return;
  4049. }
  4050. HvReleaseCell(SystemHive,RootCell);
  4051. RtlInitUnicodeString(&Name, L"select");
  4052. Select = CmpFindSubKeyByName(SystemHive,
  4053. Node,
  4054. &Name);
  4055. if (Select == HCELL_NIL) {
  4056. return;
  4057. }
  4058. Node = (PCM_KEY_NODE)HvGetCell(SystemHive,Select);
  4059. if( Node == NULL ) {
  4060. //
  4061. // we couldn't map a view for the bin containing this cell
  4062. //
  4063. return;
  4064. }
  4065. HvReleaseCell(SystemHive,Select);
  4066. RtlInitUnicodeString(&Name, L"Current");
  4067. ValueCell = CmpFindValueByName(SystemHive,
  4068. Node,
  4069. &Name);
  4070. if (ValueCell != HCELL_NIL) {
  4071. HvMarkCellDirty(SystemHive, ValueCell);
  4072. }
  4073. }