PAGED NTSTATUS SetRegDWORDValue( _In_ PMS_FILTER pFilter, _In_ PCWSTR ValueName, _In_ ULONG ValueData ) { NTSTATUS status; UNICODE_STRING UValueName; PAGED_CODE(); RtlInitUnicodeString(&UValueName, ValueName); status = ZwSetValueKey( pFilter->InterfaceRegKey, &UValueName, 0, REG_DWORD, (PVOID)&ValueData, sizeof(ValueData)); return status; }
void RegSetValueKey(LPWSTR KeyName, LPWSTR ValueName, DWORD DataType, PVOID DataBuffer, DWORD DataLength) { OBJECT_ATTRIBUTES objectAttributes; UNICODE_STRING usKeyName,usValueName; NTSTATUS ntStatus; HANDLE hRegister; ULONG Type; RtlInitUnicodeString(&usKeyName, KeyName); RtlInitUnicodeString(&usValueName, ValueName); InitializeObjectAttributes(&objectAttributes, &usKeyName, OBJ_CASE_INSENSITIVE,//对大小写敏感 NULL, NULL ); ntStatus = ZwOpenKey(&hRegister, KEY_ALL_ACCESS, &objectAttributes); if (NT_SUCCESS(ntStatus)) { ntStatus = ZwSetValueKey(hRegister, &usValueName, 0, DataType, DataBuffer, DataLength); ZwFlushKey(hRegister); ZwClose(hRegister); DbgPrint("ZwSetValueKey success!\n"); } else { DbgPrint("ZwSetValueKey failed!\n"); } }
NTSTATUS MrSetRegValue(PWCHAR n, PVOID d, ULONG l) { OBJECT_ATTRIBUTES oa = {0}; HANDLE hk = NULL; NTSTATUS status; UNICODE_STRING name; InitializeObjectAttributes( &oa, &g_core.mc_reg, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL); status = ZwOpenKey(&hk, KEY_ALL_ACCESS, &oa); if (!NT_SUCCESS(status)) goto errorout; RtlInitUnicodeString(&name, n); status = ZwSetValueKey(hk, &name, 0, REG_MULTI_SZ, d, l); errorout: if (hk != 0) ZwClose(hk); return status; }
VOID HalpLogErrorInfo( IN PCWSTR RegistryKey, IN PCWSTR ValueName, IN ULONG Type, IN PVOID Data, IN ULONG Size ) /*++ Routine Description: This function ? Arguments: None. Return Value: None. --*/ { UNICODE_STRING unicodeString; OBJECT_ATTRIBUTES objectAttributes; HANDLE hMFunc; NTSTATUS status; RtlInitUnicodeString(&unicodeString, RegistryKey); InitializeObjectAttributes ( &objectAttributes, &unicodeString, OBJ_CASE_INSENSITIVE, NULL, NULL); status = ZwOpenKey (&hMFunc, KEY_WRITE, &objectAttributes); if (!NT_SUCCESS(status)) { return ; } RtlInitUnicodeString(&unicodeString, ValueName); ZwSetValueKey(hMFunc, &unicodeString, 0, Type, Data, Size); ZwClose(hMFunc); }
NTSTATUS FilterWriteSetting(_In_ PMS_FILTER pFilter, uint16_t aKey, int aIndex, const uint8_t *aValue, uint16_t aValueLength) { HANDLE regKey = NULL; OBJECT_ATTRIBUTES attributes; DECLARE_UNICODE_STRING_SIZE(Name, 20); // Convert 'aKey' to a string RtlIntegerToUnicodeString((ULONG)aKey, 16, &Name); InitializeObjectAttributes( &attributes, &Name, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, pFilter->otSettingsRegKey, NULL); // Create/Open the registry key NTSTATUS status = ZwCreateKey( ®Key, KEY_ALL_ACCESS, &attributes, 0, NULL, REG_OPTION_NON_VOLATILE, NULL); NT_ASSERT(NT_SUCCESS(status)); if (!NT_SUCCESS(status)) { LogError(DRIVER_DEFAULT, "ZwCreateKey for %S key failed, %!STATUS!", Name.Buffer, status); goto error; } // Convert 'aIndex' to a string RtlIntegerToUnicodeString((ULONG)aIndex, 16, &Name); // Write the data to the registry status = ZwSetValueKey( regKey, &Name, 0, REG_BINARY, (PVOID)aValue, aValueLength); if (!NT_SUCCESS(status)) { LogError(DRIVER_DEFAULT, "ZwSetValueKey for %S value failed, %!STATUS!", Name.Buffer, status); goto error; } error: if (regKey) ZwClose(regKey); return status; }
VOID FxPkgPnp::WriteStateToRegistry( __in HANDLE RegKey, __in PUNICODE_STRING ValueName, __in ULONG Value ) { ZwSetValueKey(RegKey, ValueName, 0, REG_DWORD, &Value, sizeof(Value)); }
NTSTATUS vboxWddmRegSetValueDword(IN HANDLE hKey, IN PWCHAR pName, OUT DWORD val) { UNICODE_STRING RtlStr; RtlInitUnicodeString(&RtlStr, pName); return ZwSetValueKey(hKey, &RtlStr, NULL, /* IN ULONG TitleIndex OPTIONAL, reserved */ REG_DWORD, &val, sizeof(val)); }
VBOXDRVTOOL_DECL(NTSTATUS) VBoxDrvToolRegSetValueDword(IN HANDLE hKey, IN PWCHAR pName, OUT ULONG val) { UNICODE_STRING RtlStr; RtlInitUnicodeString(&RtlStr, pName); return ZwSetValueKey(hKey, &RtlStr, NULL, /* IN ULONG TitleIndex OPTIONAL, reserved */ REG_DWORD, &val, sizeof(val)); }
NTSTATUS NTAPI ExpSetCurrentUserUILanguage(IN PWSTR MuiName, IN LANGID LanguageId) { OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"Control Panel\\Desktop"); UNICODE_STRING ValueName; WCHAR ValueBuffer[8]; ULONG ValueLength; HANDLE UserHandle; HANDLE KeyHandle; NTSTATUS Status; PAGED_CODE(); /* Setup the key name */ RtlInitUnicodeString(&ValueName, MuiName); /* Open the use key */ Status = RtlOpenCurrentUser(KEY_WRITE, &UserHandle); if (!NT_SUCCESS(Status)) return Status; /* Initialize the attributes */ InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, UserHandle, NULL); /* Open the key */ Status = ZwOpenKey(&KeyHandle, KEY_SET_VALUE, &ObjectAttributes); if (NT_SUCCESS(Status)) { /* Setup the value name */ ValueLength = swprintf(ValueBuffer, L"%04lX", (ULONG)LanguageId); /* Set the length for the call and set the value */ ValueLength = (ValueLength + 1) * sizeof(WCHAR); Status = ZwSetValueKey(KeyHandle, &ValueName, 0, REG_SZ, ValueBuffer, ValueLength); /* Close the handle for this key */ ZwClose(KeyHandle); } /* Close the user key and return status */ ZwClose(UserHandle); return Status; }
static NTSTATUS IopUpdateControlKeyWithResources(IN PDEVICE_NODE DeviceNode) { UNICODE_STRING EnumRoot = RTL_CONSTANT_STRING(ENUM_ROOT); UNICODE_STRING Control = RTL_CONSTANT_STRING(L"Control"); UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"AllocConfig"); HANDLE EnumKey, InstanceKey, ControlKey; NTSTATUS Status; OBJECT_ATTRIBUTES ObjectAttributes; /* Open the Enum key */ Status = IopOpenRegistryKeyEx(&EnumKey, NULL, &EnumRoot, KEY_ENUMERATE_SUB_KEYS); if (!NT_SUCCESS(Status)) return Status; /* Open the instance key (eg. Root\PNP0A03) */ Status = IopOpenRegistryKeyEx(&InstanceKey, EnumKey, &DeviceNode->InstancePath, KEY_ENUMERATE_SUB_KEYS); ZwClose(EnumKey); if (!NT_SUCCESS(Status)) return Status; /* Create/Open the Control key */ InitializeObjectAttributes(&ObjectAttributes, &Control, OBJ_CASE_INSENSITIVE, InstanceKey, NULL); Status = ZwCreateKey(&ControlKey, KEY_SET_VALUE, &ObjectAttributes, 0, NULL, REG_OPTION_VOLATILE, NULL); ZwClose(InstanceKey); if (!NT_SUCCESS(Status)) return Status; /* Write the resource list */ Status = ZwSetValueKey(ControlKey, &ValueName, 0, REG_RESOURCE_LIST, DeviceNode->ResourceList, PnpDetermineResourceListSize(DeviceNode->ResourceList)); ZwClose(ControlKey); if (!NT_SUCCESS(Status)) return Status; return STATUS_SUCCESS; }
NTSTATUS RegistryUpdateDwordValue( IN HANDLE Key, IN PCHAR Name, IN ULONG Value ) { ANSI_STRING Ansi; UNICODE_STRING Unicode; PKEY_VALUE_PARTIAL_INFORMATION Partial; NTSTATUS status; RtlInitAnsiString(&Ansi, Name); status = RtlAnsiStringToUnicodeString(&Unicode, &Ansi, TRUE); if (!NT_SUCCESS(status)) goto fail1; Partial = __RegistryAllocate(FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data) + sizeof (ULONG)); status = STATUS_NO_MEMORY; if (Partial == NULL) goto fail2; Partial->TitleIndex = 0; Partial->Type = REG_DWORD; Partial->DataLength = sizeof (ULONG); *(PULONG)Partial->Data = Value; status = ZwSetValueKey(Key, &Unicode, Partial->TitleIndex, Partial->Type, Partial->Data, Partial->DataLength); if (!NT_SUCCESS(status)) goto fail3; __RegistryFree(Partial); RtlFreeUnicodeString(&Unicode); return STATUS_SUCCESS; fail3: __RegistryFree(Partial); fail2: RtlFreeUnicodeString(&Unicode); fail1: return status; }
NTSTATUS PptRegSetDeviceParameterDword( IN PDEVICE_OBJECT Pdo, IN __nullterminated PWSTR ParameterName, IN PULONG ParameterValue ) /*++ Routine Description: Create/set a devnode registry parameter of type dword Arguments: Pdo - ParPort PDO ParameterName - parameter name ParameterValue - parameter value Return Value: Status - status from attempt --*/ { NTSTATUS status; HANDLE hKey; UNICODE_STRING valueName; // PPDO_EXTENSION pdx = Pdo->DeviceExtension; PAGED_CODE(); status = IoOpenDeviceRegistryKey(Pdo, PLUGPLAY_REGKEY_DEVICE, KEY_WRITE, &hKey); if( !NT_SUCCESS( status ) ) { DbgPrint("PptRegSetDeviceParameterDword - openKey FAILED w/status=%x",status); return status; } RtlInitUnicodeString( &valueName, ParameterName ); status = ZwSetValueKey( hKey, &valueName, 0, REG_DWORD, ParameterValue, sizeof(ULONG) ); if( !NT_SUCCESS( status ) ) { DbgPrint("PptRegSetDeviceParameterDword - setValue FAILED w/status=%x",status); } ZwClose(hKey); return status; }
NTSTATUS PPJoy_DeleteMappingFromReg (UCHAR JoyType, HANDLE RegKey) { NTSTATUS ntStatus; UNICODE_STRING ValueName; WCHAR ValueNameBuffer[128]; PAGED_CODE(); swprintf (ValueNameBuffer,L"MapV1_%02X",JoyType); RtlInitUnicodeString(&ValueName,ValueNameBuffer); ntStatus= ZwSetValueKey (RegKey,&ValueName,0,REG_BINARY,ValueNameBuffer,0); return ntStatus; }
/* Function: setRegistryValue Parameters: io: reference to <IO>-Controller */ void setRegistryValue(IO &io,WCHAR *keyName,WCHAR *valueName,WCHAR *value) { Indenter i(io); UNICODE_STRING KeyName, ValueName; HANDLE SoftwareKeyHandle; ULONG Status; OBJECT_ATTRIBUTES ObjectAttributes; ULONG Disposition; //io.print("Schreibe Registry-Key "); //DbgBreakPoint(); // // Open the Software key // NT::RtlInitUnicodeString(&KeyName,keyName); InitializeObjectAttributes( &ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = ZwCreateKey( &SoftwareKeyHandle, KEY_ALL_ACCESS, &ObjectAttributes, 0, NULL, REG_OPTION_NON_VOLATILE, &Disposition); CHECK_STATUS(Status,Öffnen des Schlüssels) NT::RtlInitUnicodeString(&ValueName,valueName); Status = ZwSetValueKey( SoftwareKeyHandle, &ValueName, 0, REG_SZ, value, (wcslen( value )+1) * sizeof(WCHAR)); CHECK_STATUSA(Status,Setzen des Schlüssels); Status = ZwClose(SoftwareKeyHandle); CHECK_STATUS(Status,Schließen des Schlüssels); }
/* * @implemented */ NTSTATUS NTAPI IoSetSystemPartition(IN PUNICODE_STRING VolumeNameString) { NTSTATUS Status; HANDLE RootHandle, KeyHandle; UNICODE_STRING HKLMSystem, KeyString; WCHAR Buffer[sizeof(L"SystemPartition") / sizeof(WCHAR)]; RtlInitUnicodeString(&HKLMSystem, L"\\REGISTRY\\MACHINE\\SYSTEM"); /* Open registry to save data (HKLM\SYSTEM) */ Status = IopOpenRegistryKeyEx(&RootHandle, 0, &HKLMSystem, KEY_ALL_ACCESS); if (!NT_SUCCESS(Status)) { return Status; } /* Create or open Setup subkey */ KeyString.Buffer = Buffer; KeyString.Length = sizeof(L"Setup") - sizeof(UNICODE_NULL); KeyString.MaximumLength = sizeof(L"Setup"); RtlCopyMemory(Buffer, L"Setup", sizeof(L"Setup")); Status = IopCreateRegistryKeyEx(&KeyHandle, RootHandle, &KeyString, KEY_ALL_ACCESS, REG_OPTION_NON_VOLATILE, NULL); ZwClose(RootHandle); if (!NT_SUCCESS(Status)) { return Status; } /* Store caller value */ KeyString.Length = sizeof(L"SystemPartition") - sizeof(UNICODE_NULL); KeyString.MaximumLength = sizeof(L"SystemPartition"); RtlCopyMemory(Buffer, L"SystemPartition", sizeof(L"SystemPartition")); Status = ZwSetValueKey(KeyHandle, &KeyString, 0, REG_SZ, VolumeNameString->Buffer, VolumeNameString->Length + sizeof(UNICODE_NULL)); ZwClose(KeyHandle); return Status; }
VOID NbfWriteSingleParameter( IN HANDLE ParametersHandle, IN PWCHAR ValueName, IN ULONG ValueData ) /*++ Routine Description: This routine is called by NBF to write a single parameter from the registry. Arguments: ParametersHandle - A pointer to the open registry. ValueName - The name of the value to store. ValueData - The data to store at the value. Return Value: None. --*/ { UNICODE_STRING ValueKeyName; NTSTATUS Status; ULONG TmpValueData = ValueData; RtlInitUnicodeString (&ValueKeyName, ValueName); Status = ZwSetValueKey( ParametersHandle, &ValueKeyName, 0, REG_DWORD, (PVOID)&TmpValueData, sizeof(ULONG)); if (!NT_SUCCESS(Status)) { NbfPrint1("NBF: Could not write dword key: %lx\n", Status); } } /* NbfWriteSingleParameter */
NTSTATUS SoundSaveDeviceName( IN PWSTR RegistryPathName, IN PLOCAL_DEVICE_INFO pLDI ) { UNICODE_STRING DeviceName; NTSTATUS Status; HANDLE hKey; ULONG DeviceType; Status = SoundCreateDeviceName( L"", pLDI->DeviceInit->PrototypeName + wcslen(L"\\Device\\"), pLDI->DeviceNumber, &DeviceName); if (!NT_SUCCESS(Status)) { return Status; } Status = SoundOpenDevicesKey(RegistryPathName, &hKey); if (!NT_SUCCESS(Status)) { ExFreePool(DeviceName.Buffer); return Status; } /* ** The device's type is stored in a value whose name is the (unique) ** device name (minus \Device\). */ DeviceType = (ULONG)pLDI->DeviceType; Status = ZwSetValueKey(hKey, &DeviceName, 0, REG_DWORD, (PVOID)&DeviceType, sizeof(DeviceType)); ExFreePool(DeviceName.Buffer); ZwClose(hKey); return Status; }
INIT_FUNCTION NTSTATUS NTAPI HalpMarkChipsetDecode(BOOLEAN OverrideEnable) { NTSTATUS Status; UNICODE_STRING KeyString; ULONG Data = OverrideEnable; HANDLE KeyHandle, Handle; /* Open CCS key */ RtlInitUnicodeString(&KeyString, L"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET"); Status = HalpOpenRegistryKey(&Handle, 0, &KeyString, KEY_ALL_ACCESS, FALSE); if (NT_SUCCESS(Status)) { /* Open PNP Bios key */ RtlInitUnicodeString(&KeyString, L"Control\\Biosinfo\\PNPBios"); Status = HalpOpenRegistryKey(&KeyHandle, Handle, &KeyString, KEY_ALL_ACCESS, TRUE); /* Close root key */ ZwClose(Handle); /* Check if PNP BIOS key exists */ if (NT_SUCCESS(Status)) { /* Set the override value */ RtlInitUnicodeString(&KeyString, L"FullDecodeChipsetOverride"); Status = ZwSetValueKey(KeyHandle, &KeyString, 0, REG_DWORD, &Data, sizeof(Data)); /* Close subkey */ ZwClose(KeyHandle); } } /* Return status */ return Status; }
NTSTATUS NTAPI RtlApplyRXact( PRXACT_CONTEXT Context) { UNICODE_STRING ValueName; NTSTATUS Status; /* Temporarily safe the current transaction in the 'Log' key value */ RtlInitUnicodeString(&ValueName, L"Log"); Status = ZwSetValueKey(Context->KeyHandle, &ValueName, 0, REG_BINARY, Context->Data, Context->Data->CurrentSize); if (!NT_SUCCESS(Status)) { return Status; } /* Flush the key */ Status = NtFlushKey(Context->KeyHandle); if (!NT_SUCCESS(Status)) { NtDeleteValueKey(Context->KeyHandle, &ValueName); return Status; } /* Now commit the transaction */ Status = RXactpCommit(Context); if (!NT_SUCCESS(Status)) { NtDeleteValueKey(Context->KeyHandle, &ValueName); return Status; } /* Delete the 'Log' key value */ Status = NtDeleteValueKey(Context->KeyHandle, &ValueName); ASSERT(NT_SUCCESS(Status)); /* Reset the transaction */ Status = RtlAbortRXact(Context); ASSERT(NT_SUCCESS(Status)); return STATUS_SUCCESS; }
NTSTATUS PPJoy_WriteMappingToReg (PJOYSTICK_MAP Mapping, UCHAR JoyType, HANDLE RegKey) { NTSTATUS ntStatus; UNICODE_STRING ValueName; WCHAR ValueNameBuffer[128]; ULONG MapSize; PAGED_CODE(); swprintf (ValueNameBuffer,L"MapV1_%02X",JoyType); RtlInitUnicodeString(&ValueName,ValueNameBuffer); MapSize= PPJOY_ComputeMappingSize(Mapping); ntStatus= ZwSetValueKey (RegKey,&ValueName,0,REG_BINARY,Mapping,MapSize); return ntStatus; }
NTSTATUS SampRegSetValue(HANDLE KeyHandle, LPCWSTR ValueName, ULONG Type, LPVOID Data, ULONG DataLength) { UNICODE_STRING Name; RtlInitUnicodeString(&Name, ValueName); return ZwSetValueKey(KeyHandle, &Name, 0, Type, Data, DataLength); }
NTSTATUS PPJoy_WriteTimingToReg (PJOYTIMING Timing, UCHAR JoyType, HANDLE RegKey) { NTSTATUS ntStatus; UNICODE_STRING ValueName; WCHAR ValueNameBuffer[128]; ULONG TimingSize; PAGED_CODE(); swprintf (ValueNameBuffer,L"TimingV1_%02X",JoyType); RtlInitUnicodeString(&ValueName,ValueNameBuffer); TimingSize= PPJOY_ComputeTimingSize(JoyType); if (!TimingSize) return STATUS_SUCCESS; ntStatus= ZwSetValueKey (RegKey,&ValueName,0,REG_BINARY,Timing,TimingSize); return ntStatus; }
static NTSTATUS ExpSaveUuidSequence(PULONG Sequence) { OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING Name; HANDLE KeyHandle; NTSTATUS Status; RtlInitUnicodeString(&Name, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc"); InitializeObjectAttributes(&ObjectAttributes, &Name, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = ZwOpenKey(&KeyHandle, KEY_SET_VALUE, &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT("ZwOpenKey() failed (Status %lx)\n", Status); return Status; } RtlInitUnicodeString(&Name, L"UuidSequenceNumber"); Status = ZwSetValueKey(KeyHandle, &Name, 0, REG_DWORD, Sequence, sizeof(ULONG)); ZwClose(KeyHandle); if (!NT_SUCCESS(Status)) { DPRINT("ZwSetValueKey() failed (Status %lx)\n", Status); } return Status; }
NTSTATUS DrWriteKeyValue( HANDLE RegKey, PWCHAR KeyName, ULONG KeyType, PVOID Buffer, ULONG BufferLength ) { NTSTATUS status; UNICODE_STRING valueName; RtlInitUnicodeString(&valueName, KeyName); status = ZwSetValueKey( RegKey, &valueName, 0, KeyType, Buffer, BufferLength ); return status; }
static VOID ProcessorSetFriendlyName( PDEVICE_OBJECT DeviceObject) { KEY_VALUE_PARTIAL_INFORMATION *Buffer = NULL; OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING HardwareKeyName, ValueName, EnumKeyName; HANDLE KeyHandle = NULL; ULONG DataLength = 0; ULONG BufferLength = 0; NTSTATUS Status; PWSTR KeyNameBuffer = NULL; PWSTR DeviceId = NULL; PWSTR InstanceId = NULL; PWSTR pszPrefix = L"\\Registry\\Machine\\System\\CurrentcontrolSet\\Enum"; RtlInitUnicodeString(&HardwareKeyName, L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); InitializeObjectAttributes(&ObjectAttributes, &HardwareKeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL); Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT1("ZwOpenKey() failed (Status 0x%08lx)\n", Status); return; } RtlInitUnicodeString(&ValueName, L"ProcessorNameString"); Status = ZwQueryValueKey(KeyHandle, &ValueName, KeyValuePartialInformation, NULL, 0, &DataLength); if (Status != STATUS_BUFFER_OVERFLOW && Status != STATUS_BUFFER_TOO_SMALL && Status != STATUS_SUCCESS) { DPRINT1("ZwQueryValueKey() failed (Status 0x%08lx)\n", Status); goto done; } Buffer = ExAllocatePool(PagedPool, DataLength + sizeof(KEY_VALUE_PARTIAL_INFORMATION)); if (Buffer == NULL) { DPRINT1("ExAllocatePool() failed\n"); goto done; } Status = ZwQueryValueKey(KeyHandle, &ValueName, KeyValuePartialInformation, Buffer, DataLength + sizeof(KEY_VALUE_PARTIAL_INFORMATION), &DataLength); if (!NT_SUCCESS(Status)) { DPRINT1("ZwQueryValueKey() failed (Status 0x%08lx)\n", Status); goto done; } DPRINT("ProcessorNameString: %S\n", (PWSTR)&Buffer->Data[0]); ZwClose(KeyHandle); KeyHandle = NULL; Status = GetDeviceId(DeviceObject, BusQueryDeviceID, &DeviceId); if (!NT_SUCCESS(Status)) { DPRINT1("GetDeviceId() failed (Status 0x%08lx)\n", Status); goto done; } DPRINT("DeviceId: %S\n", DeviceId); Status = GetDeviceId(DeviceObject, BusQueryInstanceID, &InstanceId); if (!NT_SUCCESS(Status)) { DPRINT1("GetDeviceId() failed (Status 0x%08lx)\n", Status); goto done; } DPRINT("InstanceId: %S\n", InstanceId); BufferLength = wcslen(pszPrefix) + 1 + wcslen(DeviceId) + 1 + wcslen(InstanceId) + 1; KeyNameBuffer = ExAllocatePool(PagedPool, BufferLength * sizeof(WCHAR)); if (KeyNameBuffer == NULL) { DPRINT1("ExAllocatePool() failed\n"); goto done; } swprintf(KeyNameBuffer, L"%s\\%s\\%s", pszPrefix, DeviceId, InstanceId); RtlInitUnicodeString(&EnumKeyName, KeyNameBuffer); InitializeObjectAttributes(&ObjectAttributes, &EnumKeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL); Status = ZwOpenKey(&KeyHandle, KEY_WRITE, &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT1("ZwOpenKey() failed (Status 0x%08lx)\n", Status); goto done; } RtlInitUnicodeString(&ValueName, L"FriendlyName"); Status = ZwSetValueKey(KeyHandle, &ValueName, 0, REG_SZ, (PVOID)&Buffer->Data[0], Buffer->DataLength); if (!NT_SUCCESS(Status)) { DPRINT1("ZwSetValueKey() failed (Status 0x%08lx)\n", Status); goto done; } done: if (KeyHandle != NULL) ZwClose(KeyHandle); if (KeyNameBuffer != NULL) ExFreePool(KeyNameBuffer); if (InstanceId != NULL) ExFreePool(InstanceId); if (DeviceId != NULL) ExFreePool(DeviceId); if (Buffer != NULL) ExFreePool(Buffer); }
VOID HalpGetPciBridgeNeeds ( IN ULONG HwType, IN PUCHAR MaxPciBus, IN PCONFIGBRIDGE Current ) { ACCESS_MASK DesiredAccess; UNICODE_STRING unicodeString; PUCHAR buffer; HANDLE handle; OBJECT_ATTRIBUTES objectAttributes; PCM_FULL_RESOURCE_DESCRIPTOR Descriptor; PCONFIGURATION_COMPONENT Component; CONFIGBRIDGE CB; ULONG mnum, d, f; NTSTATUS status; buffer = ExAllocatePool (PagedPool, 1024); // init CB.PciData = (PPCI_COMMON_CONFIG) CB.Buffer; CB.SlotNumber.u.bits.Reserved = 0; Current->IO = Current->Memory = Current->PFMemory = 0; // // Assign this bridge an ID, and turn on configuration space // Current->PciData->u.type1.PrimaryBus = (UCHAR) Current->BusNo; Current->PciData->u.type1.SecondaryBus = (UCHAR) *MaxPciBus; Current->PciData->u.type1.SubordinateBus = (UCHAR) 0xFF; Current->PciData->u.type1.SecondaryStatus = 0xffff; Current->PciData->Status = 0xffff; Current->PciData->Command = 0; Current->PciData->u.type1.BridgeControl = PCI_ASSERT_BRIDGE_RESET; HalpWritePCIConfig ( Current->BusHandler, Current->SlotNumber, Current->PciData, 0, PCI_COMMON_HDR_LENGTH ); KeStallExecutionProcessor (100); Current->PciData->u.type1.BridgeControl = 0; HalpWritePCIConfig ( Current->BusHandler, Current->SlotNumber, Current->PciData, 0, PCI_COMMON_HDR_LENGTH ); KeStallExecutionProcessor (100); // // Allocate new handler for bus // CB.BusHandler = HalpAllocateAndInitPciBusHandler (HwType, *MaxPciBus, FALSE); CB.BusData = (PPCIPBUSDATA) CB.BusHandler->BusData; CB.BusNo = *MaxPciBus; *MaxPciBus += 1; // // Add another PCI bus in the registry // mnum = 0; for (; ;) { // // Find next available MultiFunctionAdapter key // DesiredAccess = KEY_READ | KEY_WRITE; swprintf ((PWCHAR) buffer, L"%s\\%d", rgzMultiFunctionAdapter, mnum); RtlInitUnicodeString (&unicodeString, (PWCHAR) buffer); InitializeObjectAttributes( &objectAttributes, &unicodeString, OBJ_CASE_INSENSITIVE, NULL, (PSECURITY_DESCRIPTOR) NULL ); status = ZwOpenKey( &handle, DesiredAccess, &objectAttributes); if (!NT_SUCCESS(status)) { break; } // already exists, next ZwClose (handle); mnum += 1; } ZwCreateKey (&handle, DesiredAccess, &objectAttributes, 0, NULL, REG_OPTION_VOLATILE, &d ); // // Add needed registry values for this MultifucntionAdapter entry // RtlInitUnicodeString (&unicodeString, rgzIdentifier); ZwSetValueKey (handle, &unicodeString, 0L, REG_SZ, L"PCI", sizeof (L"PCI") ); RtlInitUnicodeString (&unicodeString, rgzConfigurationData); Descriptor = (PCM_FULL_RESOURCE_DESCRIPTOR) buffer; Descriptor->InterfaceType = PCIBus; Descriptor->BusNumber = CB.BusNo; Descriptor->PartialResourceList.Version = 0; Descriptor->PartialResourceList.Revision = 0; Descriptor->PartialResourceList.Count = 0; ZwSetValueKey (handle, &unicodeString, 0L, REG_FULL_RESOURCE_DESCRIPTOR, Descriptor, sizeof (*Descriptor) ); RtlInitUnicodeString (&unicodeString, L"Component Information"); Component = (PCONFIGURATION_COMPONENT) buffer; RtlZeroMemory (Component, sizeof (*Component)); Component->AffinityMask = 0xffffffff; ZwSetValueKey (handle, &unicodeString, 0L, REG_BINARY, Component, FIELD_OFFSET (CONFIGURATION_COMPONENT, ConfigurationDataLength) ); ZwClose (handle); // // Since the firmware didn't configure this bridge we'll assume that // the PCI interrupts are bridged. // for (d = 0; d < PCI_MAX_DEVICES; d++) { CB.SlotNumber.u.bits.DeviceNumber = d; for (f = 0; f < PCI_MAX_FUNCTION; f++) { CB.SlotNumber.u.bits.FunctionNumber = f; HalpReadPCIConfig ( Current->BusHandler, CB.SlotNumber, CB.PciData, 0, PCI_COMMON_HDR_LENGTH ); if (CB.PciData->VendorID == PCI_INVALID_VENDORID) { break; } if (CB.PciData->u.type0.InterruptPin && (PCI_CONFIG_TYPE (CB.PciData) == PCI_DEVICE_TYPE || PCI_CONFIG_TYPE (CB.PciData) == PCI_BRIDGE_TYPE)) { // // Get IRQ value from table since firmware // did not initialize the bridge. // Current->PciData->u.type1.InterruptPin = CB.PciData->u.type0.InterruptPin + (UCHAR)d % 4; Current->PciData->u.type1.InterruptLine = HalpPCIPinToLineTable[(CB.BusNo*8) + CB.SlotNumber.u.bits.DeviceNumber]; } } } // // Look at each device on the bus and determine it's resource needs // for (d = 0; d < PCI_MAX_DEVICES; d++) { CB.SlotNumber.u.bits.DeviceNumber = d; for (f = 0; f < PCI_MAX_FUNCTION; f++) { CB.SlotNumber.u.bits.FunctionNumber = f; HalpReadPCIConfig ( CB.BusHandler, CB.SlotNumber, CB.PciData, 0, PCI_COMMON_HDR_LENGTH ); if (CB.PciData->VendorID == PCI_INVALID_VENDORID) { break; } if (IsPciBridge (CB.PciData)) { // oh look - another bridge ... HalpGetPciBridgeNeeds (HwType, MaxPciBus, &CB); continue; } if (PCI_CONFIG_TYPE (CB.PciData) != PCI_DEVICE_TYPE) { continue; } // found a device - figure out the resources it needs } } // // Found all sub-buses set SubordinateBus accordingly // Current->PciData->u.type1.SubordinateBus = (UCHAR) *MaxPciBus - 1; HalpWritePCIConfig ( Current->BusHandler, Current->SlotNumber, Current->PciData, 0, PCI_COMMON_HDR_LENGTH ); // // Set the bridges IO, Memory, and Prefetch Memory windows // // For now just pick some numbers & set everyone the same // IO 0x6000 - 0xFFFF // MEM 0x40000000 - 0x4FFFFFFF // PFMEM 0x50000000 - 0x5FFFFFFF Current->PciData->u.type1.IOBase = 0xD000 >> 12 << 4; Current->PciData->u.type1.IOLimit = 0xffff >> 12 << 4; Current->PciData->u.type1.MemoryBase = 0x0 >> 20 << 4; Current->PciData->u.type1.MemoryLimit = 0x0 >> 20 << 4; Current->PciData->u.type1.PrefetchBase = 0 >> 20 << 4; Current->PciData->u.type1.PrefetchLimit = 0 >> 20 << 4; Current->PciData->u.type1.PrefetchBaseUpper32 = 0; Current->PciData->u.type1.PrefetchLimitUpper32 = 0; Current->PciData->u.type1.IOBaseUpper16 = 0; Current->PciData->u.type1.IOLimitUpper16 = 0; Current->PciData->u.type1.BridgeControl = PCI_ENABLE_BRIDGE_ISA; HalpWritePCIConfig ( Current->BusHandler, Current->SlotNumber, Current->PciData, 0, PCI_COMMON_HDR_LENGTH ); HalpReadPCIConfig ( Current->BusHandler, Current->SlotNumber, Current->PciData, 0, PCI_COMMON_HDR_LENGTH ); // enable memory & io decodes Current->PciData->Command = PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE | PCI_ENABLE_BUS_MASTER; HalpWritePCIConfig ( Current->BusHandler, Current->SlotNumber, &Current->PciData->Command, FIELD_OFFSET (PCI_COMMON_CONFIG, Command), sizeof (Current->PciData->Command) ); ExFreePool (buffer); }
VOID HalpSetPciBridgedVgaCronk ( IN ULONG BusNumber, IN ULONG BaseAddress, IN ULONG LimitAddress ) /*++ Routine Description: . The 'vga compatible addresses' bit is set in the bridge control regiter. This causes the bridge to pass any I/O address in the range of: 10bit decode 3b0-3bb & 3c0-3df, as TEN bit addresses. As far as I can tell this "feature" is an attempt to solve some problem which the folks solving it did not fully understand, so instead of doing it right we have this fine mess. The solution is to take the least of all evils which is to remove any I/O port ranges which are getting remapped from any IoAssignResource request. (ie, IoAssignResources will never contimplate giving any I/O port out in the suspected ranges). note: memory allocation error here is fatal so don't bother with the return codes. Arguments: Base - Base of IO address range in question Limit - Limit of IO address range in question --*/ { UNICODE_STRING unicodeString; OBJECT_ATTRIBUTES objectAttributes; HANDLE handle; ULONG Length; PCM_RESOURCE_LIST ResourceList; PCM_PARTIAL_RESOURCE_DESCRIPTOR Descriptor; ULONG AddressMSBs; WCHAR ValueName[80]; NTSTATUS status; // // Open reserved resource settings // RtlInitUnicodeString (&unicodeString, rgzReservedResources); InitializeObjectAttributes( &objectAttributes, &unicodeString, OBJ_CASE_INSENSITIVE, NULL, (PSECURITY_DESCRIPTOR) NULL ); status = ZwOpenKey( &handle, KEY_READ|KEY_WRITE, &objectAttributes); if (!NT_SUCCESS(status)) { return; } // // Build resource list of reseved ranges // Length = ((LimitAddress - BaseAddress) / 1024 + 2) * 2 * sizeof (CM_PARTIAL_RESOURCE_DESCRIPTOR) + sizeof (CM_RESOURCE_LIST); ResourceList = (PCM_RESOURCE_LIST) ExAllocatePool (PagedPool, Length); memset (ResourceList, 0, Length); ResourceList->Count = 1; ResourceList->List[0].InterfaceType = PCIBus; ResourceList->List[0].BusNumber = BusNumber; Descriptor = ResourceList->List[0].PartialResourceList.PartialDescriptors; while (BaseAddress < LimitAddress) { AddressMSBs = BaseAddress & ~0x3ff; // get upper 10bits of addr // // Add xx3b0 through xx3bb // Descriptor->Type = CmResourceTypePort; Descriptor->ShareDisposition = CmResourceShareDeviceExclusive; Descriptor->Flags = CM_RESOURCE_PORT_IO; Descriptor->u.Port.Start.QuadPart = AddressMSBs | 0x3b0; Descriptor->u.Port.Length = 0xb; Descriptor += 1; ResourceList->List[0].PartialResourceList.Count += 1; // // Add xx3c0 through xx3df // Descriptor->Type = CmResourceTypePort; Descriptor->ShareDisposition = CmResourceShareDeviceExclusive; Descriptor->Flags = CM_RESOURCE_PORT_IO; Descriptor->u.Port.Start.QuadPart = AddressMSBs | 0x3c0; Descriptor->u.Port.Length = 0x1f; Descriptor += 1; ResourceList->List[0].PartialResourceList.Count += 1; // // Next range // BaseAddress += 1024; } // // Add the reserved ranges to avoid during IoAssignResource // swprintf (ValueName, L"HAL_PCI_%d", BusNumber); RtlInitUnicodeString (&unicodeString, ValueName); ZwSetValueKey (handle, &unicodeString, 0L, REG_RESOURCE_LIST, ResourceList, (ULONG) Descriptor - (ULONG) ResourceList ); ExFreePool (ResourceList); ZwClose (handle); }
NTSTATUS PiAddBuiltInBusToEnumRoot ( IN PUNICODE_STRING PlugPlayIdString, IN ULONG BusNumber, IN INTERFACE_TYPE InterfaceType, IN BUS_DATA_TYPE BusDataType, OUT PUNICODE_STRING DeviceInstancePath ) /*++ Routine Description: This routine registers the specified Plug and Play device ID representing a bus controlled by a HAL-provided bus extender. A volatile key under HKLM\System\Enum\Root is created with this device ID name. (The key is made volatile because there is no state information that needs to be stored across boots, and this keeps us from having to go clean up on next boot.) The device instance subkey is created as a 4-digit, base-10 number representing the specified bus number. The full device instance path (relative to HKLM\System\Enum) is returned. It is the caller's responsibility to free the (PagedPool) memory allocated for the unicode string buffer returned in DeviceInstancePath. The PnP device registry resource must have been acquired for exclusive (write) access before calling this routine. Arguments: PlugPlayIdString - (made-up) Plug and Play ID for this built-in bus device. BusNumber - Supplies the ordinal of this bus instance. InterfaceType - Supplies the interface type of this bus. BusDataType - Supplies the configuration space for this bus. DeviceInstancePath - Receives the resulting path (relative to HKLM\System\Enum) where this device instance was registered. Return Value: NT status indicating whether the routine was successful. --*/ { NTSTATUS Status; HANDLE EnumHandle, EnumRootHandle, DeviceHandle, DevInstHandle; UNICODE_STRING UnicodeString; ULONG StringLength, CurStringLocation, TmpDwordValue; UNICODE_STRING InstanceKeyName, ValueName; // // Create the full device instance path (relative to HKLM\System\Enum to be returned // in the DeviceInstancePath parameter. // StringLength = sizeof(REGSTR_KEY_ROOTENUM) // includes terminating NULL + PlugPlayIdString->Length + 12; // 4-digit instance name & 2 backslashes DeviceInstancePath->Buffer = (PWCHAR)ExAllocatePool(PagedPool, StringLength); if(!DeviceInstancePath->Buffer) { return STATUS_INSUFFICIENT_RESOURCES; } DeviceInstancePath->Length = (DeviceInstancePath->MaximumLength = (USHORT)StringLength) - sizeof(UNICODE_NULL); RtlMoveMemory(DeviceInstancePath->Buffer, REGSTR_KEY_ROOTENUM, (CurStringLocation = sizeof(REGSTR_KEY_ROOTENUM) - sizeof(WCHAR)) ); CurStringLocation = CB_TO_CWC(CurStringLocation); DeviceInstancePath->Buffer[CurStringLocation++] = OBJ_NAME_PATH_SEPARATOR; RtlMoveMemory(&(DeviceInstancePath->Buffer[CurStringLocation]), PlugPlayIdString->Buffer, PlugPlayIdString->Length ); CurStringLocation += CB_TO_CWC(PlugPlayIdString->Length); DeviceInstancePath->Buffer[CurStringLocation++] = OBJ_NAME_PATH_SEPARATOR; // // Add the device instance name to the path, and while we're at it, initialize // a unicode string with this name as well. // PiUlongToInstanceKeyUnicodeString(&InstanceKeyName, &(DeviceInstancePath->Buffer[CurStringLocation]), StringLength - CWC_TO_CB(CurStringLocation), BusNumber ); // // Next, open HKLM\System\Enum key, then Root sukey, creating these as persistent // keys if they don't already exist. // Status = IopOpenRegistryKeyPersist(&EnumHandle, NULL, &CmRegistryMachineSystemCurrentControlSetEnumName, KEY_ALL_ACCESS, TRUE, NULL ); if(!NT_SUCCESS(Status)) { #if DBG DbgPrint("PiAddBuiltInBusToEnumRoot: Couldn't open/create HKLM\\System\\Enum (%x).\n", Status ); #endif goto PrepareForReturn; } PiWstrToUnicodeString(&UnicodeString, REGSTR_KEY_ROOTENUM); Status = IopOpenRegistryKeyPersist(&EnumRootHandle, EnumHandle, &UnicodeString, KEY_ALL_ACCESS, TRUE, NULL ); NtClose(EnumHandle); if(!NT_SUCCESS(Status)) { #if DBG DbgPrint("PiAddBuiltInBusToEnumRoot: Couldn't open/create HKLM\\System\\Enum\\Root (%x).\n", Status ); #endif goto PrepareForReturn; } // // Now create a volatile key under HKLM\System\Enum\Root for this bus device. // Status = IopOpenRegistryKey(&DeviceHandle, EnumRootHandle, PlugPlayIdString, KEY_ALL_ACCESS, TRUE ); NtClose(EnumRootHandle); if(!NT_SUCCESS(Status)) { #if DBG DbgPrint("PiAddBuiltInBusToEnumRoot: Couldn't create bus device key\n"); DbgPrint(" %wZ. (Status %x).\n", PlugPlayIdString, Status ); #endif goto PrepareForReturn; } // // Fill in default value entries under the device key (ignore status returned from // ZwSetValueKey). // #if 0 // NewDevice = REG_DWORD : 0 // PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_NEWDEVICE); TmpDwordValue = 0; ZwSetValueKey(DeviceHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); #endif // // Create a device instance key under this device key. // Status = IopOpenRegistryKey(&DevInstHandle, DeviceHandle, &InstanceKeyName, KEY_ALL_ACCESS, TRUE ); NtClose(DeviceHandle); if(!NT_SUCCESS(Status)) { #if DBG DbgPrint("PiAddBuiltInBusToEnumRoot: Couldn't create bus device key\n"); DbgPrint(" %wZ. (Status %x).\n", PlugPlayIdString, Status ); #endif goto PrepareForReturn; } // // Fill in default value entries under the device instance key (ignore // status returned from ZwSetValueKey). // // NewInstance = REG_DWORD : 0 // FountAtEnum = REG_DWORD : 1 // InterfaceType = REG_DWORD : <InterfaceType> // BusDataType = REG_DWORD : <BusDataType> // SystemBusNumber = REG_DWORD : <BusNumber> // Class = REG_SZ : "System" // #if 0 PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_NEWINSTANCE); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); #endif TmpDwordValue = 1; PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_FOUNDATENUM); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); TmpDwordValue = (ULONG)InterfaceType; PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_INTERFACETYPE); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); TmpDwordValue = (ULONG)BusDataType; PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_BUSDATATYPE); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); TmpDwordValue = BusNumber; PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_SYSTEMBUSNUMBER); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_DWORD, &TmpDwordValue, sizeof(TmpDwordValue) ); PiWstrToUnicodeString(&ValueName, REGSTR_VALUE_CLASS); ZwSetValueKey(DevInstHandle, &ValueName, TITLE_INDEX_VALUE, REG_SZ, REGSTR_KEY_SYSTEM, sizeof(REGSTR_KEY_SYSTEM) ); NtClose(DevInstHandle); PrepareForReturn: if(!NT_SUCCESS(Status)) { ExFreePool(DeviceInstancePath->Buffer); } return Status; }
NTSTATUS NTAPI RtlInitializeRXact( HANDLE RootDirectory, BOOLEAN Commit, PRXACT_CONTEXT *OutContext) { NTSTATUS Status, TmpStatus; PRXACT_CONTEXT Context; PKEY_VALUE_FULL_INFORMATION KeyValueInformation; KEY_VALUE_BASIC_INFORMATION KeyValueBasicInfo; UNICODE_STRING ValueName; UNICODE_STRING KeyName; OBJECT_ATTRIBUTES ObjectAttributes; RXACT_INFO TransactionInfo; ULONG Disposition; ULONG ValueType; ULONG ValueDataLength; ULONG Length; HANDLE KeyHandle; /* Open or create the 'RXACT' key in the root directory */ RtlInitUnicodeString(&KeyName, L"RXACT"); InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, RootDirectory, NULL); Status = ZwCreateKey(&KeyHandle, KEY_READ | KEY_WRITE | DELETE, &ObjectAttributes, 0, NULL, 0, &Disposition); if (!NT_SUCCESS(Status)) { return Status; } /* Allocate a new context */ Context = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(*Context)); *OutContext = Context; if (Context == NULL) { TmpStatus = ZwDeleteKey(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); return STATUS_NO_MEMORY; } /* Initialize the context */ RXactInitializeContext(Context, RootDirectory, KeyHandle); /* Check if we created a new key */ if (Disposition == REG_CREATED_NEW_KEY) { /* The key is new, set the default value */ TransactionInfo.Revision = 1; RtlInitUnicodeString(&ValueName, NULL); Status = ZwSetValueKey(KeyHandle, &ValueName, 0, REG_NONE, &TransactionInfo, sizeof(TransactionInfo)); if (!NT_SUCCESS(Status)) { TmpStatus = ZwDeleteKey(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); RtlFreeHeap(RtlGetProcessHeap(), 0, *OutContext); return Status; } return STATUS_RXACT_STATE_CREATED; } else { /* The key exited, get the default key value */ ValueDataLength = sizeof(TransactionInfo); Status = RtlpNtQueryValueKey(KeyHandle, &ValueType, &TransactionInfo, &ValueDataLength, 0); if (!NT_SUCCESS(Status)) { TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); RtlFreeHeap(RtlGetProcessHeap(), 0, Context); return Status; } /* Check if the value date is valid */ if ((ValueDataLength != sizeof(TransactionInfo)) || (TransactionInfo.Revision != 1)) { TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); RtlFreeHeap(RtlGetProcessHeap(), 0, Context); return STATUS_UNKNOWN_REVISION; } /* Query the 'Log' key value */ RtlInitUnicodeString(&ValueName, L"Log"); Status = ZwQueryValueKey(KeyHandle, &ValueName, KeyValueBasicInformation, &KeyValueBasicInfo, sizeof(KeyValueBasicInfo), &Length); if (!NT_SUCCESS(Status)) { /* There is no 'Log', so we are done */ return STATUS_SUCCESS; } /* Check if the caller asked to commit the current state */ if (!Commit) { /* We have a log, that must be committed first! */ return STATUS_RXACT_COMMIT_NECESSARY; } /* Query the size of the 'Log' key value */ Status = ZwQueryValueKey(KeyHandle, &ValueName, KeyValueFullInformation, NULL, 0, &Length); if (Status != STATUS_BUFFER_TOO_SMALL) { return Status; } /* Allocate a buffer for the key value information */ KeyValueInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length); if (KeyValueInformation == NULL) { return STATUS_NO_MEMORY; } /* Query the 'Log' key value */ Status = ZwQueryValueKey(KeyHandle, &ValueName, KeyValueFullInformation, KeyValueInformation, Length, &Length); if (!NT_SUCCESS(Status)) { RtlFreeHeap(RtlGetProcessHeap(), 0, KeyValueInformation); RtlFreeHeap(RtlGetProcessHeap(), 0, Context); return Status; } /* Set the Data pointer to the key value data */ Context->Data = (PRXACT_DATA)((PUCHAR)KeyValueInformation + KeyValueInformation->DataOffset); /* This is an old log, don't use handles when committing! */ Context->CanUseHandles = FALSE; /* Commit the data */ Status = RXactpCommit(Context); if (!NT_SUCCESS(Status)) { RtlFreeHeap(RtlGetProcessHeap(), 0, KeyValueInformation); RtlFreeHeap(RtlGetProcessHeap(), 0, Context); return Status; } /* Delete the old key */ Status = NtDeleteValueKey(KeyHandle, &ValueName); ASSERT(NT_SUCCESS(Status)); /* Set the data member to the allocated buffer, so it will get freed */ Context->Data = (PRXACT_DATA)KeyValueInformation; /* Abort the old transaction */ Status = RtlAbortRXact(Context); ASSERT(NT_SUCCESS(Status)); return Status; } }
static NTSTATUS NTAPI RXactpCommit( PRXACT_CONTEXT Context) { PRXACT_DATA Data; PRXACT_ACTION Action; NTSTATUS Status, TmpStatus; HANDLE KeyHandle; ULONG i; Data = Context->Data; /* The first action record starts after the data header */ Action = (PRXACT_ACTION)(Data + 1); /* Loop all recorded actions */ for (i = 0; i < Data->ActionCount; i++) { /* Translate relative offsets to actual pointers */ Action->KeyName.Buffer = (PWSTR)((PUCHAR)Data + (ULONG_PTR)Action->KeyName.Buffer); Action->ValueName.Buffer = (PWSTR)((PUCHAR)Data + (ULONG_PTR)Action->ValueName.Buffer); Action->ValueData = (PUCHAR)Data + (ULONG_PTR)Action->ValueData; /* Check what kind of action this is */ if (Action->Type == RXactDeleteKey) { /* This is a delete action. Check if we can use a handle */ if ((Action->KeyHandle != INVALID_HANDLE_VALUE) && Context->CanUseHandles) { /* Delete the key by the given handle */ Status = ZwDeleteKey(Action->KeyHandle); if (!NT_SUCCESS(Status)) { return Status; } } else { /* We cannot use a handle, open the key first by it's name */ Status = RXactpOpenTargetKey(Context->RootDirectory, RXactDeleteKey, &Action->KeyName, &KeyHandle); if (NT_SUCCESS(Status)) { Status = ZwDeleteKey(KeyHandle); TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); if (!NT_SUCCESS(Status)) { return Status; } } else { /* Failed to open the key, it's ok, if it was not found */ if (Status != STATUS_OBJECT_NAME_NOT_FOUND) return Status; } } } else if (Action->Type == RXactSetValueKey) { /* This is a set action. Check if we can use a handle */ if ((Action->KeyHandle != INVALID_HANDLE_VALUE) && Context->CanUseHandles) { /* Set the key value using the given key handle */ Status = ZwSetValueKey(Action->KeyHandle, &Action->ValueName, 0, Action->ValueType, Action->ValueData, Action->ValueDataSize); if (!NT_SUCCESS(Status)) { return Status; } } else { /* We cannot use a handle, open the key first by it's name */ Status = RXactpOpenTargetKey(Context->RootDirectory, RXactSetValueKey, &Action->KeyName, &KeyHandle); if (!NT_SUCCESS(Status)) { return Status; } /* Set the key value */ Status = ZwSetValueKey(KeyHandle, &Action->ValueName, 0, Action->ValueType, Action->ValueData, Action->ValueDataSize); TmpStatus = NtClose(KeyHandle); ASSERT(NT_SUCCESS(TmpStatus)); if (!NT_SUCCESS(Status)) { return Status; } } } else { ASSERT(FALSE); return STATUS_INVALID_PARAMETER; } /* Go to the next action record */ Action = (PRXACT_ACTION)((PUCHAR)Action + Action->Size); } return STATUS_SUCCESS; }