2 * Service process to load a kernel driver
4 * Copyright 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_NO_STATUS
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winedevice
);
36 WINE_DECLARE_DEBUG_CHANNEL(relay
);
38 extern NTSTATUS
wine_ntoskrnl_main_loop( HANDLE stop_event
);
40 static WCHAR
*driver_name
;
41 static SERVICE_STATUS_HANDLE service_handle
;
42 static HKEY driver_hkey
;
43 static HANDLE stop_event
;
44 static DRIVER_OBJECT driver_obj
;
45 static DRIVER_EXTENSION driver_extension
;
47 /* find the LDR_MODULE corresponding to the driver module */
48 static LDR_MODULE
*find_ldr_module( HMODULE module
)
50 LIST_ENTRY
*entry
, *list
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
52 for (entry
= list
->Flink
; entry
!= list
; entry
= entry
->Flink
)
54 LDR_MODULE
*ldr
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
55 if (ldr
->BaseAddress
== module
) return ldr
;
56 if (ldr
->BaseAddress
> (void *)module
) break;
61 /* call the driver init entry point */
62 static NTSTATUS
init_driver( HMODULE module
, UNICODE_STRING
*keyname
)
66 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
68 if (!nt
->OptionalHeader
.AddressOfEntryPoint
) return STATUS_SUCCESS
;
70 driver_obj
.Size
= sizeof(driver_obj
);
71 driver_obj
.DriverSection
= find_ldr_module( module
);
72 driver_obj
.DriverInit
= (PDRIVER_INITIALIZE
)((char *)module
+ nt
->OptionalHeader
.AddressOfEntryPoint
);
73 driver_obj
.DriverExtension
= &driver_extension
;
75 driver_extension
.DriverObject
= &driver_obj
;
76 driver_extension
.ServiceKeyName
= *keyname
;
78 if (WINE_TRACE_ON(relay
))
79 WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
80 driver_obj
.DriverInit
, &driver_obj
, wine_dbgstr_w(keyname
->Buffer
) );
82 status
= driver_obj
.DriverInit( &driver_obj
, keyname
);
84 if (WINE_TRACE_ON(relay
))
85 WINE_DPRINTF( "%04x:Ret driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
86 driver_obj
.DriverInit
, &driver_obj
, wine_dbgstr_w(keyname
->Buffer
), status
);
88 WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name
), &driver_obj
);
89 WINE_TRACE( "- DriverInit = %p\n", driver_obj
.DriverInit
);
90 WINE_TRACE( "- DriverStartIo = %p\n", driver_obj
.DriverStartIo
);
91 WINE_TRACE( "- DriverUnload = %p\n", driver_obj
.DriverUnload
);
92 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
93 WINE_TRACE( "- MajorFunction[%d] = %p\n", i
, driver_obj
.MajorFunction
[i
] );
98 /* load the .sys module for a device driver */
99 static BOOL
load_driver(void)
101 static const WCHAR ntprefixW
[] = {'\\','?','?','\\',0};
102 static const WCHAR ImagePathW
[] = {'I','m','a','g','e','P','a','t','h',0};
103 static const WCHAR servicesW
[] = {'\\','R','e','g','i','s','t','r','y',
104 '\\','M','a','c','h','i','n','e',
105 '\\','S','y','s','t','e','m',
106 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
107 '\\','S','e','r','v','i','c','e','s','\\',0};
109 UNICODE_STRING keypath
;
111 LPWSTR path
= NULL
, str
;
114 str
= HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW
) + strlenW(driver_name
)*sizeof(WCHAR
) );
115 lstrcpyW( str
, servicesW
);
116 lstrcatW( str
, driver_name
);
118 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, str
+ 18 /* skip \registry\machine */, &driver_hkey
))
120 WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str
), GetLastError() );
121 HeapFree( GetProcessHeap(), 0, str
);
124 RtlInitUnicodeString( &keypath
, str
);
126 /* read the executable path from memory */
128 if (RegQueryValueExW( driver_hkey
, ImagePathW
, NULL
, &type
, NULL
, &size
)) return FALSE
;
130 str
= HeapAlloc( GetProcessHeap(), 0, size
);
131 if (!RegQueryValueExW( driver_hkey
, ImagePathW
, NULL
, &type
, (LPBYTE
)str
, &size
))
133 size
= ExpandEnvironmentStringsW(str
,NULL
,0);
134 path
= HeapAlloc(GetProcessHeap(),0,size
*sizeof(WCHAR
));
135 ExpandEnvironmentStringsW(str
,path
,size
);
137 HeapFree( GetProcessHeap(), 0, str
);
138 if (!path
) return FALSE
;
140 /* make sure msvcrt is loaded to resolve the ntoskrnl.exe forwards */
141 LoadLibraryA( "msvcrt.dll" );
143 /* GameGuard uses an NT-style path name */
145 if (!strncmpW( path
, ntprefixW
, 4 )) str
+= 4;
147 WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str
) );
149 module
= LoadLibraryW( str
);
150 HeapFree( GetProcessHeap(), 0, path
);
151 if (!module
) return FALSE
;
153 init_driver( module
, &keypath
);
157 static DWORD WINAPI
service_handler( DWORD ctrl
, DWORD event_type
, LPVOID event_data
, LPVOID context
)
159 SERVICE_STATUS status
;
161 status
.dwServiceType
= SERVICE_WIN32
;
162 status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
163 status
.dwWin32ExitCode
= 0;
164 status
.dwServiceSpecificExitCode
= 0;
165 status
.dwCheckPoint
= 0;
166 status
.dwWaitHint
= 0;
170 case SERVICE_CONTROL_STOP
:
171 case SERVICE_CONTROL_SHUTDOWN
:
172 WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name
) );
173 status
.dwCurrentState
= SERVICE_STOP_PENDING
;
174 status
.dwControlsAccepted
= 0;
175 SetServiceStatus( service_handle
, &status
);
176 SetEvent( stop_event
);
179 WINE_FIXME( "got service ctrl %x for %s\n", ctrl
, wine_dbgstr_w(driver_name
) );
180 status
.dwCurrentState
= SERVICE_RUNNING
;
181 SetServiceStatus( service_handle
, &status
);
186 static void WINAPI
ServiceMain( DWORD argc
, LPWSTR
*argv
)
188 SERVICE_STATUS status
;
190 WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name
) );
192 stop_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
194 service_handle
= RegisterServiceCtrlHandlerExW( driver_name
, service_handler
, NULL
);
198 status
.dwServiceType
= SERVICE_WIN32
;
199 status
.dwCurrentState
= SERVICE_START_PENDING
;
200 status
.dwControlsAccepted
= 0;
201 status
.dwWin32ExitCode
= 0;
202 status
.dwServiceSpecificExitCode
= 0;
203 status
.dwCheckPoint
= 0;
204 status
.dwWaitHint
= 10000;
205 SetServiceStatus( service_handle
, &status
);
209 status
.dwCurrentState
= SERVICE_RUNNING
;
210 status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
211 SetServiceStatus( service_handle
, &status
);
213 wine_ntoskrnl_main_loop( stop_event
);
215 else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name
) );
217 status
.dwCurrentState
= SERVICE_STOPPED
;
218 status
.dwControlsAccepted
= 0;
219 SetServiceStatus( service_handle
, &status
);
220 WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name
) );
223 int wmain( int argc
, WCHAR
*argv
[] )
225 SERVICE_TABLE_ENTRYW service_table
[2];
227 if (!(driver_name
= argv
[1]))
229 WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
233 service_table
[0].lpServiceName
= argv
[1];
234 service_table
[0].lpServiceProc
= ServiceMain
;
235 service_table
[1].lpServiceName
= NULL
;
236 service_table
[1].lpServiceProc
= NULL
;
238 StartServiceCtrlDispatcherW( service_table
);