--- /dev/null
- IMAGE_NT_HEADERS pehd;
+/*
+ * Implementation of VERSION.DLL - Resource Access routines
+ *
+ * Copyright 1996,1997 Marcus Meissner
+ * Copyright 1997 David Cuthbert
+ * Copyright 1999 Ulrich Weigand
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "config.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#define NONAMELESSUNION
+#define NONAMELESSSTRUCT
+#include "windef.h"
+#include "winbase.h"
+#include "lzexpand.h"
+#include "winuser.h"
+#include "winver.h"
+#undef VS_FILE_INFO
+#define VS_FILE_INFO 16
+
+#include "wine/unicode.h"
+#include "wine/winbase16.h"
+
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(ver);
+
+
+/**********************************************************************
+ * find_entry_by_id
+ *
+ * Find an entry by id in a resource directory
+ * Copied from loader/pe_resource.c
+ */
+static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
+ WORD id, const void *root )
+{
+ const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
+ int min, max, pos;
+
+ entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
+ min = dir->NumberOfNamedEntries;
+ max = min + dir->NumberOfIdEntries - 1;
+ while (min <= max)
+ {
+ pos = (min + max) / 2;
+ if (entry[pos].u.Id == id)
+ return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s.OffsetToDirectory);
+ if (entry[pos].u.Id > id) max = pos - 1;
+ else min = pos + 1;
+ }
+ return NULL;
+}
+
+
+/**********************************************************************
+ * find_entry_default
+ *
+ * Find a default entry in a resource directory
+ * Copied from loader/pe_resource.c
+ */
+static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
+ const void *root )
+{
+ const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
+
+ entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
+ return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s.OffsetToDirectory);
+}
+
+
+/***********************************************************************
+ * read_xx_header [internal]
+ */
+static int read_xx_header( HFILE lzfd )
+{
+ IMAGE_DOS_HEADER mzh;
+ char magic[3];
+
+ LZSeek( lzfd, 0, SEEK_SET );
+ if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
+ return 0;
+ if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
+ {
+ if (!memcmp( &mzh, "\177ELF", 4 )) return 1; /* ELF */
+ if (*(UINT *)&mzh == 0xfeedface || *(UINT *)&mzh == 0xcefaedfe) return 1; /* Mach-O */
+ return 0;
+ }
+
+ LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
+ if ( 2 != LZRead( lzfd, magic, 2 ) )
+ return 0;
+
+ LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
+
+ if ( magic[0] == 'N' && magic[1] == 'E' )
+ return IMAGE_OS2_SIGNATURE;
+ if ( magic[0] == 'P' && magic[1] == 'E' )
+ return IMAGE_NT_SIGNATURE;
+
+ magic[2] = '\0';
+ WARN("Can't handle %s files.\n", magic );
+ return 0;
+}
+
+/***********************************************************************
+ * find_ne_resource [internal]
+ */
+static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
+{
+ const WORD typeid = VS_FILE_INFO | 0x8000;
+ const WORD resid = VS_VERSION_INFO | 0x8000;
+ IMAGE_OS2_HEADER nehd;
+ NE_TYPEINFO *typeInfo;
+ NE_NAMEINFO *nameInfo;
+ DWORD nehdoffset;
+ LPBYTE resTab;
+ DWORD resTabSize;
+ int count;
+
+ /* Read in NE header */
+ nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
+ if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
+
+ resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
+ if ( !resTabSize )
+ {
+ TRACE("No resources in NE dll\n" );
+ return FALSE;
+ }
+
+ /* Read in resource table */
+ resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
+ if ( !resTab ) return FALSE;
+
+ LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
+ if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
+ {
+ HeapFree( GetProcessHeap(), 0, resTab );
+ return FALSE;
+ }
+
+ /* Find resource */
+ typeInfo = (NE_TYPEINFO *)(resTab + 2);
+ while (typeInfo->type_id)
+ {
+ if (typeInfo->type_id == typeid) goto found_type;
+ typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
+ typeInfo->count * sizeof(NE_NAMEINFO));
+ }
+ TRACE("No typeid entry found\n" );
+ HeapFree( GetProcessHeap(), 0, resTab );
+ return FALSE;
+
+ found_type:
+ nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
+
+ for (count = typeInfo->count; count > 0; count--, nameInfo++)
+ if (nameInfo->id == resid) goto found_name;
+
+ TRACE("No resid entry found\n" );
+ HeapFree( GetProcessHeap(), 0, resTab );
+ return FALSE;
+
+ found_name:
+ /* Return resource data */
+ if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
+ if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
+
+ HeapFree( GetProcessHeap(), 0, resTab );
+ return TRUE;
+}
+
+/***********************************************************************
+ * find_pe_resource [internal]
+ */
+static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
+{
- int i, nSections;
++ union
++ {
++ IMAGE_NT_HEADERS32 nt32;
++ IMAGE_NT_HEADERS64 nt64;
++ } pehd;
+ DWORD pehdoffset;
+ PIMAGE_DATA_DIRECTORY resDataDir;
+ PIMAGE_SECTION_HEADER sections;
+ LPBYTE resSection;
+ DWORD resSectionSize;
+ const void *resDir;
+ const IMAGE_RESOURCE_DIRECTORY *resPtr;
+ const IMAGE_RESOURCE_DATA_ENTRY *resData;
- if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
++ int i, len, nSections;
+ BOOL ret = FALSE;
+
+ /* Read in PE header */
+ pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
- resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
++ len = LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) );
++ if (len < sizeof(pehd.nt32.FileHeader)) return 0;
++ if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
++
++ switch (pehd.nt32.OptionalHeader.Magic)
++ {
++ case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
++ resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
++ break;
++ case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
++ resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
++ break;
++ default:
++ return 0;
++ }
+
- nSections = pehd.FileHeader.NumberOfSections;
+ if ( !resDataDir->Size )
+ {
+ TRACE("No resources in PE dll\n" );
+ return FALSE;
+ }
+
+ /* Read in section table */
- LZSeek( lzfd, pehdoffset +
- sizeof(DWORD) + /* Signature */
- sizeof(IMAGE_FILE_HEADER) +
- pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
++ nSections = pehd.nt32.FileHeader.NumberOfSections;
+ sections = HeapAlloc( GetProcessHeap(), 0,
+ nSections * sizeof(IMAGE_SECTION_HEADER) );
+ if ( !sections ) return FALSE;
+
++ len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
++ LZSeek( lzfd, pehdoffset + len, SEEK_SET );
+
+ if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
+ LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
+ {
+ HeapFree( GetProcessHeap(), 0, sections );
+ return FALSE;
+ }
+
+ /* Find resource section */
+ for ( i = 0; i < nSections; i++ )
+ if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
+ && resDataDir->VirtualAddress < sections[i].VirtualAddress +
+ sections[i].SizeOfRawData )
+ break;
+
+ if ( i == nSections )
+ {
+ HeapFree( GetProcessHeap(), 0, sections );
+ TRACE("Couldn't find resource section\n" );
+ return FALSE;
+ }
+
+ /* Read in resource section */
+ resSectionSize = sections[i].SizeOfRawData;
+ resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
+ if ( !resSection )
+ {
+ HeapFree( GetProcessHeap(), 0, sections );
+ return FALSE;
+ }
+
+ LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
+ if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
+
+ /* Find resource */
+ resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
+
+ resPtr = resDir;
+ resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
+ if ( !resPtr )
+ {
+ TRACE("No typeid entry found\n" );
+ goto done;
+ }
+ resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
+ if ( !resPtr )
+ {
+ TRACE("No resid entry found\n" );
+ goto done;
+ }
+ resPtr = find_entry_default( resPtr, resDir );
+ if ( !resPtr )
+ {
+ TRACE("No default language entry found\n" );
+ goto done;
+ }
+
+ /* Find resource data section */
+ resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
+ for ( i = 0; i < nSections; i++ )
+ if ( resData->OffsetToData >= sections[i].VirtualAddress
+ && resData->OffsetToData < sections[i].VirtualAddress +
+ sections[i].SizeOfRawData )
+ break;
+
+ if ( i == nSections )
+ {
+ TRACE("Couldn't find resource data section\n" );
+ goto done;
+ }
+
+ /* Return resource data */
+ if ( resLen ) *resLen = resData->Size;
+ if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
+ + sections[i].PointerToRawData;
+ ret = TRUE;
+
+ done:
+ HeapFree( GetProcessHeap(), 0, resSection );
+ HeapFree( GetProcessHeap(), 0, sections );
+ return ret;
+}
+
+
+/***********************************************************************
+ * find_version_resource [internal]
+ */
+DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
+{
+ DWORD magic = read_xx_header( lzfd );
+
+ switch (magic)
+ {
+ case IMAGE_OS2_SIGNATURE:
+ if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
+ break;
+ case IMAGE_NT_SIGNATURE:
+ if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;
+ break;
+ }
+ return magic;
+}
--- /dev/null
- /* Check if NOVESA is present in the start options */
- if (wcsstr((PWCHAR)KeyInfo->Data, L"NOVESA"))
+/*
+ * VideoPort driver
+ *
+ * Copyright (C) 2002-2004, 2007 ReactOS Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+
+#include "videoprt.h"
+#include <wdmguid.h>
+
+/* GLOBAL VARIABLES ***********************************************************/
+
+ULONG CsrssInitialized = FALSE;
+PKPROCESS Csrss = NULL;
+ULONG VideoPortDeviceNumber = 0;
+
+/* PRIVATE FUNCTIONS **********************************************************/
+
+ULONG NTAPI
+DriverEntry(
+ IN PVOID Context1,
+ IN PVOID Context2)
+{
+ return STATUS_SUCCESS;
+}
+
+PVOID NTAPI
+IntVideoPortImageDirectoryEntryToData(
+ PVOID BaseAddress,
+ ULONG Directory)
+{
+ PIMAGE_NT_HEADERS NtHeader;
+ ULONG Va;
+
+ NtHeader = RtlImageNtHeader(BaseAddress);
+ if (NtHeader == NULL)
+ return NULL;
+
+ if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes)
+ return NULL;
+
+ Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress;
+ if (Va == 0)
+ return NULL;
+
+ return (PVOID)((ULONG_PTR)BaseAddress + Va);
+}
+
+VOID NTAPI
+IntVideoPortDeferredRoutine(
+ IN PKDPC Dpc,
+ IN PVOID DeferredContext,
+ IN PVOID SystemArgument1,
+ IN PVOID SystemArgument2)
+{
+ PVOID HwDeviceExtension =
+ &((PVIDEO_PORT_DEVICE_EXTENSION)DeferredContext)->MiniPortDeviceExtension;
+ ((PMINIPORT_DPC_ROUTINE)SystemArgument1)(HwDeviceExtension, SystemArgument2);
+}
+
+NTSTATUS
+IntCreateRegistryPath(
+ IN PCUNICODE_STRING DriverRegistryPath,
+ OUT PUNICODE_STRING DeviceRegistryPath)
+{
+ static WCHAR RegistryMachineSystem[] = L"\\REGISTRY\\MACHINE\\SYSTEM\\";
+ static WCHAR CurrentControlSet[] = L"CURRENTCONTROLSET\\";
+ static WCHAR ControlSet[] = L"CONTROLSET";
+ static WCHAR Insert1[] = L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
+ static WCHAR Insert2[] = L"\\Device0";
+ LPWSTR ProfilePath = NULL;
+ BOOLEAN Valid;
+ PWCHAR AfterControlSet;
+
+ Valid = (0 == _wcsnicmp(DriverRegistryPath->Buffer, RegistryMachineSystem,
+ wcslen(RegistryMachineSystem)));
+ {
+ AfterControlSet = DriverRegistryPath->Buffer + wcslen(RegistryMachineSystem);
+ if (0 == _wcsnicmp(AfterControlSet, CurrentControlSet, wcslen(CurrentControlSet)))
+ {
+ AfterControlSet += wcslen(CurrentControlSet);
+ }
+ else if (0 == _wcsnicmp(AfterControlSet, ControlSet, wcslen(ControlSet)))
+ {
+ AfterControlSet += wcslen(ControlSet);
+ while (L'0' <= *AfterControlSet && L'9' <= *AfterControlSet)
+ {
+ AfterControlSet++;
+ }
+ Valid = (L'\\' == *AfterControlSet);
+ AfterControlSet++;
+ }
+ else
+ {
+ Valid = FALSE;
+ }
+ }
+
+ if (Valid)
+ {
+ ProfilePath = ExAllocatePoolWithTag(PagedPool,
+ (wcslen(DriverRegistryPath->Buffer) +
+ wcslen(Insert1) + wcslen(Insert2) + 1) * sizeof(WCHAR),
+ TAG_VIDEO_PORT);
+ if (NULL != ProfilePath)
+ {
+ wcsncpy(ProfilePath, DriverRegistryPath->Buffer, AfterControlSet - DriverRegistryPath->Buffer);
+ wcscpy(ProfilePath + (AfterControlSet - DriverRegistryPath->Buffer), Insert1);
+ wcscat(ProfilePath, AfterControlSet);
+ wcscat(ProfilePath, Insert2);
+
+ Valid = NT_SUCCESS(RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE, ProfilePath));
+ }
+ else
+ {
+ Valid = FALSE;
+ }
+ }
+ else
+ {
+ WARN_(VIDEOPRT, "Unparsable registry path %wZ", DriverRegistryPath);
+ }
+
+ if (Valid)
+ {
+ RtlInitUnicodeString(DeviceRegistryPath, ProfilePath);
+ }
+ else
+ {
+ if (ProfilePath)
+ ExFreePoolWithTag(ProfilePath, TAG_VIDEO_PORT);
+
+ DeviceRegistryPath->Length =
+ DeviceRegistryPath->MaximumLength =
+ DriverRegistryPath->Length + (9 * sizeof(WCHAR));
+ DeviceRegistryPath->Length -= sizeof(WCHAR);
+ DeviceRegistryPath->Buffer = ExAllocatePoolWithTag(
+ NonPagedPool,
+ DeviceRegistryPath->MaximumLength,
+ TAG_VIDEO_PORT);
+ if (!DeviceRegistryPath->Buffer)
+ return STATUS_NO_MEMORY;
+ swprintf(DeviceRegistryPath->Buffer, L"%s\\Device0",
+ DriverRegistryPath->Buffer);
+ }
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS NTAPI
+IntVideoPortCreateAdapterDeviceObject(
+ IN PDRIVER_OBJECT DriverObject,
+ IN PVIDEO_PORT_DRIVER_EXTENSION DriverExtension,
+ IN PDEVICE_OBJECT PhysicalDeviceObject,
+ OUT PDEVICE_OBJECT *DeviceObject OPTIONAL)
+{
+ PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension;
+ ULONG DeviceNumber;
+ ULONG PciSlotNumber;
+ PCI_SLOT_NUMBER SlotNumber;
+ ULONG Size;
+ NTSTATUS Status;
+ WCHAR DeviceBuffer[20];
+ UNICODE_STRING DeviceName;
+ PDEVICE_OBJECT DeviceObject_;
+
+ if (DeviceObject == NULL)
+ DeviceObject = &DeviceObject_;
+
+ /*
+ * Find the first free device number that can be used for video device
+ * object names and symlinks.
+ */
+
+ DeviceNumber = VideoPortDeviceNumber;
+ if (DeviceNumber == 0xFFFFFFFF)
+ {
+ WARN_(VIDEOPRT, "Can't find free device number\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
+ /*
+ * Create the device object.
+ */
+
+ /* Create a unicode device name. */
+ swprintf(DeviceBuffer, L"\\Device\\Video%lu", DeviceNumber);
+ RtlInitUnicodeString(&DeviceName, DeviceBuffer);
+
+ INFO_(VIDEOPRT, "HwDeviceExtension size is: 0x%x\n",
+ DriverExtension->InitializationData.HwDeviceExtensionSize);
+
+ /* Create the device object. */
+ Status = IoCreateDevice(
+ DriverObject,
+ sizeof(VIDEO_PORT_DEVICE_EXTENSION) +
+ DriverExtension->InitializationData.HwDeviceExtensionSize,
+ &DeviceName,
+ FILE_DEVICE_VIDEO,
+ 0,
+ TRUE,
+ DeviceObject);
+
+ if (!NT_SUCCESS(Status))
+ {
+ WARN_(VIDEOPRT, "IoCreateDevice call failed with status 0x%08x\n", Status);
+ return Status;
+ }
+
+ /*
+ * Set the buffering strategy here. If you change this, remember
+ * to change VidDispatchDeviceControl too.
+ */
+
+ (*DeviceObject)->Flags |= DO_BUFFERED_IO;
+
+ /*
+ * Initialize device extension.
+ */
+
+ DeviceExtension = (PVIDEO_PORT_DEVICE_EXTENSION)((*DeviceObject)->DeviceExtension);
+ DeviceExtension->DeviceNumber = DeviceNumber;
+ DeviceExtension->DriverObject = DriverObject;
+ DeviceExtension->PhysicalDeviceObject = PhysicalDeviceObject;
+ DeviceExtension->FunctionalDeviceObject = *DeviceObject;
+ DeviceExtension->DriverExtension = DriverExtension;
+
+ /*
+ * Get the registry path associated with this driver.
+ */
+
+ Status = IntCreateRegistryPath(
+ &DriverExtension->RegistryPath,
+ &DeviceExtension->RegistryPath);
+ if (!NT_SUCCESS(Status))
+ {
+ WARN_(VIDEOPRT, "IntCreateRegistryPath() call failed with status 0x%08x\n", Status);
+ IoDeleteDevice(*DeviceObject);
+ *DeviceObject = NULL;
+ return Status;
+ }
+
+ if (PhysicalDeviceObject != NULL)
+ {
+ /* Get bus number from the upper level bus driver. */
+ Size = sizeof(ULONG);
+ Status = IoGetDeviceProperty(
+ PhysicalDeviceObject,
+ DevicePropertyBusNumber,
+ Size,
+ &DeviceExtension->SystemIoBusNumber,
+ &Size);
+ if (!NT_SUCCESS(Status))
+ {
+ WARN_(VIDEOPRT, "Couldn't get an information from bus driver. We will try to\n"
+ "use legacy detection method, but even that doesn't mean that\n"
+ "it will work.\n");
+ DeviceExtension->PhysicalDeviceObject = NULL;
+ }
+ }
+
+ DeviceExtension->AdapterInterfaceType =
+ DriverExtension->InitializationData.AdapterInterfaceType;
+
+ if (PhysicalDeviceObject != NULL)
+ {
+ /* Get bus type from the upper level bus driver. */
+ Size = sizeof(ULONG);
+ IoGetDeviceProperty(
+ PhysicalDeviceObject,
+ DevicePropertyLegacyBusType,
+ Size,
+ &DeviceExtension->AdapterInterfaceType,
+ &Size);
+
+ /* Get bus device address from the upper level bus driver. */
+ Size = sizeof(ULONG);
+ IoGetDeviceProperty(
+ PhysicalDeviceObject,
+ DevicePropertyAddress,
+ Size,
+ &PciSlotNumber,
+ &Size);
+
+ /* Convert slotnumber to PCI_SLOT_NUMBER */
+ SlotNumber.u.AsULONG = 0;
+ SlotNumber.u.bits.DeviceNumber = (PciSlotNumber >> 16) & 0xFFFF;
+ SlotNumber.u.bits.FunctionNumber = PciSlotNumber & 0xFFFF;
+ DeviceExtension->SystemIoSlotNumber = SlotNumber.u.AsULONG;
+ }
+
+ InitializeListHead(&DeviceExtension->AddressMappingListHead);
+ KeInitializeDpc(
+ &DeviceExtension->DpcObject,
+ IntVideoPortDeferredRoutine,
+ DeviceExtension);
+
+ KeInitializeMutex(&DeviceExtension->DeviceLock, 0);
+
+ /* Attach the device. */
+ if (PhysicalDeviceObject != NULL)
+ DeviceExtension->NextDeviceObject = IoAttachDeviceToDeviceStack(
+ *DeviceObject, PhysicalDeviceObject);
+
+ /* Remove the initailizing flag */
+ (*DeviceObject)->Flags &= ~DO_DEVICE_INITIALIZING;
+ return STATUS_SUCCESS;
+}
+
+
+NTSTATUS NTAPI
+IntVideoPortFindAdapter(
+ IN PDRIVER_OBJECT DriverObject,
+ IN PVIDEO_PORT_DRIVER_EXTENSION DriverExtension,
+ IN PDEVICE_OBJECT DeviceObject)
+{
+ WCHAR DeviceVideoBuffer[20];
+ PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension;
+ SIZE_T Size;
+ NTSTATUS Status;
+ VIDEO_PORT_CONFIG_INFO ConfigInfo;
+ SYSTEM_BASIC_INFORMATION SystemBasicInfo;
+ UCHAR Again = FALSE;
+ WCHAR DeviceBuffer[20];
+ UNICODE_STRING DeviceName;
+ WCHAR SymlinkBuffer[20];
+ UNICODE_STRING SymlinkName;
+ BOOL LegacyDetection = FALSE;
+ ULONG DeviceNumber, DisplayNumber;
+
+ DeviceExtension = (PVIDEO_PORT_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+ DeviceNumber = DeviceExtension->DeviceNumber;
+
+ /*
+ * Setup a ConfigInfo structure that we will pass to HwFindAdapter.
+ */
+
+ RtlZeroMemory(&ConfigInfo, sizeof(VIDEO_PORT_CONFIG_INFO));
+ ConfigInfo.Length = sizeof(VIDEO_PORT_CONFIG_INFO);
+ ConfigInfo.AdapterInterfaceType = DeviceExtension->AdapterInterfaceType;
+ if (ConfigInfo.AdapterInterfaceType == PCIBus)
+ ConfigInfo.InterruptMode = LevelSensitive;
+ else
+ ConfigInfo.InterruptMode = Latched;
+ ConfigInfo.DriverRegistryPath = DriverExtension->RegistryPath.Buffer;
+ ConfigInfo.VideoPortGetProcAddress = IntVideoPortGetProcAddress;
+ ConfigInfo.SystemIoBusNumber = DeviceExtension->SystemIoBusNumber;
+ ConfigInfo.BusInterruptLevel = DeviceExtension->InterruptLevel;
+ ConfigInfo.BusInterruptVector = DeviceExtension->InterruptVector;
+
+ Size = sizeof(SystemBasicInfo);
+ Status = ZwQuerySystemInformation(
+ SystemBasicInformation,
+ &SystemBasicInfo,
+ Size,
+ &Size);
+
+ if (NT_SUCCESS(Status))
+ {
+ ConfigInfo.SystemMemorySize =
+ SystemBasicInfo.NumberOfPhysicalPages *
+ SystemBasicInfo.PageSize;
+ }
+
+ /*
+ * Call miniport HwVidFindAdapter entry point to detect if
+ * particular device is present. There are two possible code
+ * paths. The first one is for Legacy drivers (NT4) and cases
+ * when we don't have information about what bus we're on. The
+ * second case is the standard one for Plug & Play drivers.
+ */
+ if (DeviceExtension->PhysicalDeviceObject == NULL)
+ {
+ LegacyDetection = TRUE;
+ }
+
+ if (LegacyDetection)
+ {
+ ULONG BusNumber, MaxBuses;
+
+ MaxBuses = DeviceExtension->AdapterInterfaceType == PCIBus ? 8 : 1;
+
+ for (BusNumber = 0; BusNumber < MaxBuses; BusNumber++)
+ {
+ DeviceExtension->SystemIoBusNumber =
+ ConfigInfo.SystemIoBusNumber = BusNumber;
+
+ RtlZeroMemory(&DeviceExtension->MiniPortDeviceExtension,
+ DriverExtension->InitializationData.HwDeviceExtensionSize);
+
+ /* FIXME: Need to figure out what string to pass as param 3. */
+ Status = DriverExtension->InitializationData.HwFindAdapter(
+ &DeviceExtension->MiniPortDeviceExtension,
+ DriverExtension->HwContext,
+ NULL,
+ &ConfigInfo,
+ &Again);
+
+ if (Status == ERROR_DEV_NOT_EXIST)
+ {
+ continue;
+ }
+ else if (Status == NO_ERROR)
+ {
+ break;
+ }
+ else
+ {
+ WARN_(VIDEOPRT, "HwFindAdapter call failed with error 0x%X\n", Status);
+ RtlFreeUnicodeString(&DeviceExtension->RegistryPath);
+ if (DeviceExtension->NextDeviceObject)
+ IoDetachDevice(DeviceExtension->NextDeviceObject);
+ IoDeleteDevice(DeviceObject);
+
+ return Status;
+ }
+ }
+ }
+ else
+ {
+ /* FIXME: Need to figure out what string to pass as param 3. */
+ Status = DriverExtension->InitializationData.HwFindAdapter(
+ &DeviceExtension->MiniPortDeviceExtension,
+ DriverExtension->HwContext,
+ NULL,
+ &ConfigInfo,
+ &Again);
+ }
+
+ if (Status != NO_ERROR)
+ {
+ WARN_(VIDEOPRT, "HwFindAdapter call failed with error 0x%X\n", Status);
+ RtlFreeUnicodeString(&DeviceExtension->RegistryPath);
+ if (DeviceExtension->NextDeviceObject)
+ IoDetachDevice(DeviceExtension->NextDeviceObject);
+ IoDeleteDevice(DeviceObject);
+ return Status;
+ }
+
+ /*
+ * Now we know the device is present, so let's do all additional tasks
+ * such as creating symlinks or setting up interrupts and timer.
+ */
+
+ /* Create a unicode device name. */
+ swprintf(DeviceBuffer, L"\\Device\\Video%lu", DeviceNumber);
+ RtlInitUnicodeString(&DeviceName, DeviceBuffer);
+
+ /* Create symbolic link "\??\DISPLAYx" */
+
+ /* HACK: We need this to find the first available display to
+ * use. We can't use the device number because then we could
+ * end up with \Device\Video0 being non-functional because
+ * HwFindAdapter returned an error. \Device\Video1 would be
+ * the correct primary display but it would be set to DISPLAY2
+ * so it would never be used and ROS would bugcheck on boot.
+ * By doing it this way, we ensure that DISPLAY1 is always
+ * functional. Another idea would be letting the IO manager
+ * give our video devices names then getting those names
+ * somehow and creating symbolic links to \Device\VideoX
+ * and \??\DISPLAYX once we know that HwFindAdapter has succeeded.
+ */
+ DisplayNumber = 0;
+ do
+ {
+ DisplayNumber++;
+ swprintf(SymlinkBuffer, L"\\??\\DISPLAY%lu", DisplayNumber);
+ RtlInitUnicodeString(&SymlinkName, SymlinkBuffer);
+ }
+ while (IoCreateSymbolicLink(&SymlinkName, &DeviceName) != STATUS_SUCCESS);
+
+ /* Add entry to DEVICEMAP\VIDEO key in registry. */
+ swprintf(DeviceVideoBuffer, L"\\Device\\Video%d", DisplayNumber - 1);
+ RtlWriteRegistryValue(
+ RTL_REGISTRY_DEVICEMAP,
+ L"VIDEO",
+ DeviceVideoBuffer,
+ REG_SZ,
+ DeviceExtension->RegistryPath.Buffer,
+ DeviceExtension->RegistryPath.MaximumLength);
+
+ RtlWriteRegistryValue(
+ RTL_REGISTRY_DEVICEMAP,
+ L"VIDEO",
+ L"MaxObjectNumber",
+ REG_DWORD,
+ &DeviceNumber,
+ sizeof(DeviceNumber));
+
+ /* FIXME: Allocate hardware resources for device. */
+
+ /*
+ * Allocate interrupt for device.
+ */
+
+ if (!IntVideoPortSetupInterrupt(DeviceObject, DriverExtension, &ConfigInfo))
+ {
+ RtlFreeUnicodeString(&DeviceExtension->RegistryPath);
+ if (DeviceExtension->NextDeviceObject)
+ IoDetachDevice(DeviceExtension->NextDeviceObject);
+ IoDeleteDevice(DeviceObject);
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ /*
+ * Allocate timer for device.
+ */
+
+ if (!IntVideoPortSetupTimer(DeviceObject, DriverExtension))
+ {
+ if (DeviceExtension->InterruptObject != NULL)
+ IoDisconnectInterrupt(DeviceExtension->InterruptObject);
+ if (DeviceExtension->NextDeviceObject)
+ IoDetachDevice(DeviceExtension->NextDeviceObject);
+ RtlFreeUnicodeString(&DeviceExtension->RegistryPath);
+ IoDeleteDevice(DeviceObject);
+ WARN_(VIDEOPRT, "STATUS_INSUFFICIENT_RESOURCES\n");
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ /*
+ * Query children of the device.
+ */
+ VideoPortEnumerateChildren(&DeviceExtension->MiniPortDeviceExtension, NULL);
+
+ INFO_(VIDEOPRT, "STATUS_SUCCESS\n");
+ return STATUS_SUCCESS;
+}
+
+VOID FASTCALL
+IntAttachToCSRSS(PKPROCESS *CallingProcess, PKAPC_STATE ApcState)
+{
+ *CallingProcess = (PKPROCESS)PsGetCurrentProcess();
+ if (*CallingProcess != Csrss)
+ {
+ KeStackAttachProcess(Csrss, ApcState);
+ }
+}
+
+VOID FASTCALL
+IntDetachFromCSRSS(PKPROCESS *CallingProcess, PKAPC_STATE ApcState)
+{
+ if (*CallingProcess != Csrss)
+ {
+ KeUnstackDetachProcess(ApcState);
+ }
+}
+
+/* PUBLIC FUNCTIONS ***********************************************************/
+
+/*
+ * @implemented
+ */
+
+ULONG NTAPI
+VideoPortInitialize(
+ IN PVOID Context1,
+ IN PVOID Context2,
+ IN PVIDEO_HW_INITIALIZATION_DATA HwInitializationData,
+ IN PVOID HwContext)
+{
+ PDRIVER_OBJECT DriverObject = Context1;
+ PUNICODE_STRING RegistryPath = Context2;
+ NTSTATUS Status;
+ PVIDEO_PORT_DRIVER_EXTENSION DriverExtension;
+ BOOLEAN PnpDriver = FALSE, LegacyDetection = FALSE;
+
+ TRACE_(VIDEOPRT, "VideoPortInitialize\n");
+
+ /*
+ * As a first thing do parameter checks.
+ */
+
+ if (HwInitializationData->HwInitDataSize > sizeof(VIDEO_HW_INITIALIZATION_DATA))
+ {
+ return STATUS_REVISION_MISMATCH;
+ }
+
+ if (HwInitializationData->HwFindAdapter == NULL ||
+ HwInitializationData->HwInitialize == NULL ||
+ HwInitializationData->HwStartIO == NULL)
+ {
+ return STATUS_INVALID_PARAMETER;
+ }
+
+ switch (HwInitializationData->HwInitDataSize)
+ {
+ /*
+ * NT4 drivers are special case, because we must use legacy method
+ * of detection instead of the Plug & Play one.
+ */
+
+ case SIZE_OF_NT4_VIDEO_HW_INITIALIZATION_DATA:
+ INFO_(VIDEOPRT, "We were loaded by a Windows NT miniport driver.\n");
+ break;
+
+ case SIZE_OF_W2K_VIDEO_HW_INITIALIZATION_DATA:
+ INFO_(VIDEOPRT, "We were loaded by a Windows 2000 miniport driver.\n");
+ break;
+
+ case sizeof(VIDEO_HW_INITIALIZATION_DATA):
+ INFO_(VIDEOPRT, "We were loaded by a Windows XP or later miniport driver.\n");
+ break;
+
+ default:
+ WARN_(VIDEOPRT, "Invalid HwInitializationData size.\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
+ /* Set dispatching routines */
+ DriverObject->MajorFunction[IRP_MJ_CREATE] = IntVideoPortDispatchOpen;
+ DriverObject->MajorFunction[IRP_MJ_CLOSE] = IntVideoPortDispatchClose;
+ DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
+ IntVideoPortDispatchDeviceControl;
+ DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =
+ IntVideoPortDispatchDeviceControl;
+ DriverObject->MajorFunction[IRP_MJ_WRITE] =
+ IntVideoPortDispatchWrite; // ReactOS-specific hack
+ DriverObject->DriverUnload = IntVideoPortUnload;
+
+ /* Determine type of the miniport driver */
+ if ((HwInitializationData->HwInitDataSize >=
+ FIELD_OFFSET(VIDEO_HW_INITIALIZATION_DATA, HwQueryInterface))
+ && HwInitializationData->HwSetPowerState
+ && HwInitializationData->HwGetPowerState
+ && HwInitializationData->HwGetVideoChildDescriptor)
+ {
+ INFO_(VIDEOPRT, "The miniport is a PnP miniport driver\n");
+ PnpDriver = TRUE;
+ }
+
+ /* Check if legacy detection should be applied */
+ if (!PnpDriver || HwContext)
+ {
+ INFO_(VIDEOPRT, "Legacy detection for adapter interface %d\n",
+ HwInitializationData->AdapterInterfaceType);
+
+ /* FIXME: Move the code for legacy detection
+ to another function and call it here */
+ LegacyDetection = TRUE;
+ }
+
+ /*
+ * NOTE:
+ * The driver extension can be already allocated in case that we were
+ * called by legacy driver and failed detecting device. Some miniport
+ * drivers in that case adjust parameters and call VideoPortInitialize
+ * again.
+ */
+
+ DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
+ if (DriverExtension == NULL)
+ {
+ Status = IoAllocateDriverObjectExtension(
+ DriverObject,
+ DriverObject,
+ sizeof(VIDEO_PORT_DRIVER_EXTENSION),
+ (PVOID *)&DriverExtension);
+
+ if (!NT_SUCCESS(Status))
+ {
+ return Status;
+ }
+
+ /*
+ * Save the registry path. This should be done only once even if
+ * VideoPortInitialize is called multiple times.
+ */
+
+ if (RegistryPath->Length != 0)
+ {
+ DriverExtension->RegistryPath.Length = 0;
+ DriverExtension->RegistryPath.MaximumLength =
+ RegistryPath->Length + sizeof(UNICODE_NULL);
+ DriverExtension->RegistryPath.Buffer =
+ ExAllocatePoolWithTag(
+ PagedPool,
+ DriverExtension->RegistryPath.MaximumLength,
+ 'RTSU');
+ if (DriverExtension->RegistryPath.Buffer == NULL)
+ {
+ RtlInitUnicodeString(&DriverExtension->RegistryPath, NULL);
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ RtlCopyUnicodeString(&DriverExtension->RegistryPath, RegistryPath);
+ INFO_(VIDEOPRT, "RegistryPath: %wZ\n", &DriverExtension->RegistryPath);
+ }
+ else
+ {
+ RtlInitUnicodeString(&DriverExtension->RegistryPath, NULL);
+ }
+ }
+
+ /*
+ * Copy the correct miniport initialization data to the device extension.
+ */
+
+ RtlCopyMemory(
+ &DriverExtension->InitializationData,
+ HwInitializationData,
+ HwInitializationData->HwInitDataSize);
+ if (HwInitializationData->HwInitDataSize <
+ sizeof(VIDEO_HW_INITIALIZATION_DATA))
+ {
+ RtlZeroMemory((PVOID)((ULONG_PTR)&DriverExtension->InitializationData +
+ HwInitializationData->HwInitDataSize),
+ sizeof(VIDEO_HW_INITIALIZATION_DATA) -
+ HwInitializationData->HwInitDataSize);
+ }
+ DriverExtension->HwContext = HwContext;
+
+ /*
+ * Plug & Play drivers registers the device in AddDevice routine. For
+ * legacy drivers we must do it now.
+ */
+
+ if (LegacyDetection)
+ {
+ PDEVICE_OBJECT DeviceObject;
+
+ if (HwInitializationData->HwInitDataSize != SIZE_OF_NT4_VIDEO_HW_INITIALIZATION_DATA)
+ {
+ /* power management */
+ DriverObject->MajorFunction[IRP_MJ_POWER] = IntVideoPortDispatchPower;
+ }
+ Status = IntVideoPortCreateAdapterDeviceObject(DriverObject, DriverExtension,
+ NULL, &DeviceObject);
+ INFO_(VIDEOPRT, "IntVideoPortCreateAdapterDeviceObject returned 0x%x\n", Status);
+ if (!NT_SUCCESS(Status))
+ return Status;
+ Status = IntVideoPortFindAdapter(DriverObject, DriverExtension, DeviceObject);
+ INFO_(VIDEOPRT, "IntVideoPortFindAdapter returned 0x%x\n", Status);
+ if (NT_SUCCESS(Status))
+ VideoPortDeviceNumber++;
+ return Status;
+ }
+ else
+ {
+ DriverObject->DriverExtension->AddDevice = IntVideoPortAddDevice;
+ DriverObject->MajorFunction[IRP_MJ_PNP] = IntVideoPortDispatchPnp;
+ DriverObject->MajorFunction[IRP_MJ_POWER] = IntVideoPortDispatchPower;
+ DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = IntVideoPortDispatchSystemControl;
+
+ return STATUS_SUCCESS;
+ }
+}
+
+/*
+ * @implemented
+ */
+
+VOID
+VideoPortDebugPrint(
+ IN VIDEO_DEBUG_LEVEL DebugPrintLevel,
+ IN PCHAR DebugMessage, ...)
+{
+ va_list ap;
+
+ va_start(ap, DebugMessage);
+ vDbgPrintEx(DPFLTR_IHVVIDEO_ID, DebugPrintLevel, DebugMessage, ap);
+ va_end(ap);
+}
+
+/*
+ * @unimplemented
+ */
+
+VOID NTAPI
+VideoPortLogError(
+ IN PVOID HwDeviceExtension,
+ IN PVIDEO_REQUEST_PACKET Vrp OPTIONAL,
+ IN VP_STATUS ErrorCode,
+ IN ULONG UniqueId)
+{
+ UNIMPLEMENTED;
+
+ INFO_(VIDEOPRT, "VideoPortLogError ErrorCode %d (0x%x) UniqueId %lu (0x%lx)\n",
+ ErrorCode, ErrorCode, UniqueId, UniqueId);
+ if (Vrp)
+ INFO_(VIDEOPRT, "Vrp->IoControlCode %lu (0x%lx)\n", Vrp->IoControlCode, Vrp->IoControlCode);
+}
+
+/*
+ * @implemented
+ */
+
+UCHAR NTAPI
+VideoPortGetCurrentIrql(VOID)
+{
+ return KeGetCurrentIrql();
+}
+
+typedef struct QueryRegistryCallbackContext
+{
+ PVOID HwDevic