2007-05-17 16:51:44 +02:00
|
|
|
/*
|
|
|
|
* Service process to load a kernel driver
|
|
|
|
*
|
|
|
|
* Copyright 2007 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "winreg.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
#include "winsvc.h"
|
|
|
|
#include "ddk/wdm.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(relay);
|
|
|
|
|
2008-12-16 15:36:59 +01:00
|
|
|
extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
static WCHAR *driver_name;
|
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
|
|
|
static HANDLE stop_event;
|
|
|
|
static DRIVER_OBJECT driver_obj;
|
|
|
|
static DRIVER_EXTENSION driver_extension;
|
|
|
|
|
|
|
|
/* find the LDR_MODULE corresponding to the driver module */
|
|
|
|
static LDR_MODULE *find_ldr_module( HMODULE module )
|
|
|
|
{
|
2016-02-26 06:11:26 +01:00
|
|
|
LDR_MODULE *ldr;
|
|
|
|
ULONG_PTR magic;
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-02-26 06:11:26 +01:00
|
|
|
LdrLockLoaderLock( 0, NULL, &magic );
|
|
|
|
if (LdrFindEntryForAddress( module, &ldr ))
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2016-02-26 06:11:26 +01:00
|
|
|
WARN( "module not found for %p\n", module );
|
|
|
|
ldr = NULL;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
2016-02-26 06:11:26 +01:00
|
|
|
LdrUnlockLoaderLock( 0, magic );
|
|
|
|
|
|
|
|
return ldr;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
/* load the driver module file */
|
|
|
|
static HMODULE load_driver_module( const WCHAR *name )
|
|
|
|
{
|
2008-12-22 15:51:25 +01:00
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
const IMAGE_IMPORT_DESCRIPTOR *imports;
|
2013-01-08 22:01:48 +01:00
|
|
|
SYSTEM_BASIC_INFORMATION info;
|
2010-06-23 13:51:46 +02:00
|
|
|
int i;
|
|
|
|
INT_PTR delta;
|
2008-12-22 15:51:25 +01:00
|
|
|
ULONG size;
|
2008-04-02 20:40:27 +02:00
|
|
|
HMODULE module = LoadLibraryW( name );
|
|
|
|
|
|
|
|
if (!module) return NULL;
|
|
|
|
nt = RtlImageNtHeader( module );
|
|
|
|
|
|
|
|
if (!(delta = (char *)module - (char *)nt->OptionalHeader.ImageBase)) return module;
|
|
|
|
|
|
|
|
/* the loader does not apply relocations to non page-aligned binaries or executables,
|
|
|
|
* we have to do it ourselves */
|
|
|
|
|
2013-01-08 22:01:48 +01:00
|
|
|
NtQuerySystemInformation( SystemBasicInformation, &info, sizeof(info), NULL );
|
|
|
|
if (nt->OptionalHeader.SectionAlignment < info.PageSize ||
|
2008-04-02 20:40:27 +02:00
|
|
|
!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
|
|
|
|
{
|
|
|
|
DWORD old;
|
|
|
|
IMAGE_BASE_RELOCATION *rel, *end;
|
|
|
|
|
|
|
|
if ((rel = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size )))
|
|
|
|
{
|
|
|
|
WINE_TRACE( "%s: relocating from %p to %p\n",
|
|
|
|
wine_dbgstr_w(name), (char *)module - delta, module );
|
|
|
|
end = (IMAGE_BASE_RELOCATION *)((char *)rel + size);
|
|
|
|
while (rel < end && rel->SizeOfBlock)
|
|
|
|
{
|
|
|
|
void *page = (char *)module + rel->VirtualAddress;
|
2013-01-08 22:01:48 +01:00
|
|
|
VirtualProtect( page, info.PageSize, PAGE_EXECUTE_READWRITE, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
rel = LdrProcessRelocationBlock( page, (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
|
|
|
|
(USHORT *)(rel + 1), delta );
|
2015-05-02 18:43:34 +02:00
|
|
|
if (old != PAGE_EXECUTE_READWRITE) VirtualProtect( page, info.PageSize, old, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
if (!rel) goto error;
|
|
|
|
}
|
2008-12-22 15:51:25 +01:00
|
|
|
/* make sure we don't try again */
|
2010-06-15 14:29:25 +02:00
|
|
|
size = FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader ) + nt->FileHeader.SizeOfOptionalHeader;
|
|
|
|
VirtualProtect( nt, size, PAGE_READWRITE, &old );
|
2008-12-22 15:51:25 +01:00
|
|
|
nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = 0;
|
2015-05-02 18:43:34 +02:00
|
|
|
VirtualProtect( nt, size, old, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-22 15:51:25 +01:00
|
|
|
|
|
|
|
/* make sure imports are relocated too */
|
|
|
|
|
|
|
|
if ((imports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
|
|
|
|
{
|
|
|
|
for (i = 0; imports[i].Name && imports[i].FirstThunk; i++)
|
|
|
|
{
|
|
|
|
char *name = (char *)module + imports[i].Name;
|
|
|
|
WCHAR buffer[32], *p = buffer;
|
|
|
|
|
|
|
|
while (p < buffer + 32) if (!(*p++ = *name++)) break;
|
|
|
|
if (p <= buffer + 32) FreeLibrary( load_driver_module( buffer ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
return module;
|
|
|
|
|
|
|
|
error:
|
|
|
|
FreeLibrary( module );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
/* call the driver init entry point */
|
|
|
|
static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
NTSTATUS status;
|
|
|
|
const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
|
|
|
|
|
|
|
|
if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
driver_obj.Size = sizeof(driver_obj);
|
|
|
|
driver_obj.DriverSection = find_ldr_module( module );
|
|
|
|
driver_obj.DriverInit = (PDRIVER_INITIALIZE)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
|
|
|
|
driver_obj.DriverExtension = &driver_extension;
|
|
|
|
|
|
|
|
driver_extension.DriverObject = &driver_obj;
|
|
|
|
driver_extension.ServiceKeyName = *keyname;
|
|
|
|
|
|
|
|
if (WINE_TRACE_ON(relay))
|
|
|
|
WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
|
|
|
|
driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer) );
|
|
|
|
|
|
|
|
status = driver_obj.DriverInit( &driver_obj, keyname );
|
|
|
|
|
|
|
|
if (WINE_TRACE_ON(relay))
|
|
|
|
WINE_DPRINTF( "%04x:Ret driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
|
|
|
|
driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer), status );
|
|
|
|
|
|
|
|
WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name), &driver_obj );
|
|
|
|
WINE_TRACE( "- DriverInit = %p\n", driver_obj.DriverInit );
|
|
|
|
WINE_TRACE( "- DriverStartIo = %p\n", driver_obj.DriverStartIo );
|
|
|
|
WINE_TRACE( "- DriverUnload = %p\n", driver_obj.DriverUnload );
|
|
|
|
for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
|
|
|
|
WINE_TRACE( "- MajorFunction[%d] = %p\n", i, driver_obj.MajorFunction[i] );
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:31:10 +01:00
|
|
|
/* call the driver unload function */
|
|
|
|
static void unload_driver( HMODULE module, DRIVER_OBJECT *driver_obj )
|
|
|
|
{
|
|
|
|
if (driver_obj->DriverUnload)
|
|
|
|
{
|
|
|
|
if (WINE_TRACE_ON(relay))
|
|
|
|
WINE_DPRINTF( "%04x:Call driver unload %p (obj=%p)\n", GetCurrentThreadId(),
|
|
|
|
driver_obj->DriverUnload, driver_obj );
|
|
|
|
|
|
|
|
driver_obj->DriverUnload( driver_obj );
|
|
|
|
|
|
|
|
if (WINE_TRACE_ON(relay))
|
|
|
|
WINE_DPRINTF( "%04x:Ret driver unload %p (obj=%p)\n", GetCurrentThreadId(),
|
|
|
|
driver_obj->DriverUnload, driver_obj );
|
|
|
|
}
|
|
|
|
FreeLibrary( module );
|
|
|
|
}
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
/* load the .sys module for a device driver */
|
2015-01-26 04:31:10 +01:00
|
|
|
static HMODULE load_driver(void)
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2008-09-17 00:33:26 +02:00
|
|
|
static const WCHAR driversW[] = {'\\','d','r','i','v','e','r','s','\\',0};
|
2011-11-23 13:12:31 +01:00
|
|
|
static const WCHAR systemrootW[] = {'\\','S','y','s','t','e','m','R','o','o','t','\\',0};
|
2008-09-17 00:33:26 +02:00
|
|
|
static const WCHAR postfixW[] = {'.','s','y','s',0};
|
2007-05-17 16:51:44 +02:00
|
|
|
static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
|
|
|
|
static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
|
|
|
|
static const WCHAR servicesW[] = {'\\','R','e','g','i','s','t','r','y',
|
|
|
|
'\\','M','a','c','h','i','n','e',
|
|
|
|
'\\','S','y','s','t','e','m',
|
|
|
|
'\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
|
|
|
|
'\\','S','e','r','v','i','c','e','s','\\',0};
|
|
|
|
|
|
|
|
UNICODE_STRING keypath;
|
2016-02-26 06:11:36 +01:00
|
|
|
HKEY driver_hkey;
|
2007-05-17 16:51:44 +02:00
|
|
|
HMODULE module;
|
|
|
|
LPWSTR path = NULL, str;
|
|
|
|
DWORD type, size;
|
|
|
|
|
|
|
|
str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
|
|
|
|
lstrcpyW( str, servicesW );
|
|
|
|
lstrcatW( str, driver_name );
|
|
|
|
|
|
|
|
if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
|
|
|
|
{
|
|
|
|
WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
|
2007-12-14 14:57:17 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, str);
|
2016-02-26 06:11:36 +01:00
|
|
|
return NULL;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
RtlInitUnicodeString( &keypath, str );
|
|
|
|
|
|
|
|
/* read the executable path from memory */
|
|
|
|
size = 0;
|
2008-09-17 00:33:26 +02:00
|
|
|
if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size ))
|
|
|
|
{
|
|
|
|
str = HeapAlloc( GetProcessHeap(), 0, size );
|
|
|
|
if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
|
|
|
|
{
|
|
|
|
size = ExpandEnvironmentStringsW(str,NULL,0);
|
|
|
|
path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
|
|
|
|
ExpandEnvironmentStringsW(str,path,size);
|
|
|
|
}
|
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
2016-02-26 06:11:36 +01:00
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
RtlFreeUnicodeString( &keypath );
|
|
|
|
RegCloseKey( driver_hkey );
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-23 13:12:31 +01:00
|
|
|
|
|
|
|
if (!strncmpiW( path, systemrootW, 12 ))
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
|
|
|
|
GetWindowsDirectoryW(buffer, MAX_PATH);
|
|
|
|
|
|
|
|
str = HeapAlloc(GetProcessHeap(), 0, (size -11 + strlenW(buffer))
|
|
|
|
* sizeof(WCHAR));
|
|
|
|
lstrcpyW(str, buffer);
|
|
|
|
lstrcatW(str, path + 11);
|
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
|
|
|
path = str;
|
|
|
|
}
|
|
|
|
else if (!strncmpW( path, ntprefixW, 4 ))
|
|
|
|
str = path + 4;
|
|
|
|
else
|
|
|
|
str = path;
|
2008-09-17 00:33:26 +02:00
|
|
|
}
|
|
|
|
else
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2008-09-17 00:33:26 +02:00
|
|
|
/* default is to use the driver name + ".sys" */
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetSystemDirectoryW(buffer, MAX_PATH);
|
|
|
|
path = HeapAlloc(GetProcessHeap(),0,
|
|
|
|
(strlenW(buffer) + strlenW(driversW) + strlenW(driver_name) + strlenW(postfixW) + 1)
|
|
|
|
*sizeof(WCHAR));
|
|
|
|
lstrcpyW(path, buffer);
|
|
|
|
lstrcatW(path, driversW);
|
|
|
|
lstrcatW(path, driver_name);
|
|
|
|
lstrcatW(path, postfixW);
|
2011-11-23 13:12:31 +01:00
|
|
|
str = path;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
2016-02-26 06:11:36 +01:00
|
|
|
RegCloseKey( driver_hkey );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
module = load_driver_module( str );
|
2007-05-17 16:51:44 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
2016-02-26 06:11:36 +01:00
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
RtlFreeUnicodeString( &keypath );
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
init_driver( module, &keypath );
|
2015-01-26 04:31:10 +01:00
|
|
|
return module;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
|
|
|
|
switch(ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name) );
|
|
|
|
status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
SetEvent( stop_event );
|
|
|
|
return NO_ERROR;
|
|
|
|
default:
|
|
|
|
WINE_FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
2015-01-26 04:31:10 +01:00
|
|
|
HMODULE driver_module;
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
|
|
|
|
|
|
|
|
stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
|
|
|
|
|
|
|
|
service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
|
2008-03-28 18:52:24 +01:00
|
|
|
if (!service_handle)
|
|
|
|
return;
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 10000;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
|
2015-01-26 04:31:10 +01:00
|
|
|
driver_module = load_driver();
|
|
|
|
if (driver_module)
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
2007-06-07 11:20:24 +02:00
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
2007-05-17 16:51:44 +02:00
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
|
|
|
|
wine_ntoskrnl_main_loop( stop_event );
|
2015-01-26 04:31:10 +01:00
|
|
|
unload_driver( driver_module, &driver_obj );
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
|
|
|
|
|
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain( int argc, WCHAR *argv[] )
|
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRYW service_table[2];
|
|
|
|
|
|
|
|
if (!(driver_name = argv[1]))
|
|
|
|
{
|
|
|
|
WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
service_table[0].lpServiceName = argv[1];
|
|
|
|
service_table[0].lpServiceProc = ServiceMain;
|
|
|
|
service_table[1].lpServiceName = NULL;
|
|
|
|
service_table[1].lpServiceProc = NULL;
|
|
|
|
|
|
|
|
StartServiceCtrlDispatcherW( service_table );
|
|
|
|
return 0;
|
|
|
|
}
|