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
22 #include "wine/port.h"
27 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winedevice
);
39 WINE_DECLARE_DEBUG_CHANNEL(relay
);
41 extern NTSTATUS CDECL
wine_ntoskrnl_main_loop( HANDLE stop_event
);
43 static WCHAR
*driver_name
;
44 static SERVICE_STATUS_HANDLE service_handle
;
45 static HKEY driver_hkey
;
46 static HANDLE stop_event
;
47 static DRIVER_OBJECT driver_obj
;
48 static DRIVER_EXTENSION driver_extension
;
50 /* find the LDR_MODULE corresponding to the driver module */
51 static LDR_MODULE
*find_ldr_module( HMODULE module
)
53 LIST_ENTRY
*entry
, *list
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
55 for (entry
= list
->Flink
; entry
!= list
; entry
= entry
->Flink
)
57 LDR_MODULE
*ldr
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
58 if (ldr
->BaseAddress
== module
) return ldr
;
59 if (ldr
->BaseAddress
> (void *)module
) break;
64 /* load the driver module file */
65 static HMODULE
load_driver_module( const WCHAR
*name
)
68 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
69 SYSTEM_BASIC_INFORMATION info
;
73 HMODULE module
= LoadLibraryW( name
);
75 if (!module
) return NULL
;
76 nt
= RtlImageNtHeader( module
);
78 if (!(delta
= (char *)module
- (char *)nt
->OptionalHeader
.ImageBase
)) return module
;
80 /* the loader does not apply relocations to non page-aligned binaries or executables,
81 * we have to do it ourselves */
83 NtQuerySystemInformation( SystemBasicInformation
, &info
, sizeof(info
), NULL
);
84 if (nt
->OptionalHeader
.SectionAlignment
< info
.PageSize
||
85 !(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
88 IMAGE_BASE_RELOCATION
*rel
, *end
;
90 if ((rel
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_BASERELOC
, &size
)))
92 WINE_TRACE( "%s: relocating from %p to %p\n",
93 wine_dbgstr_w(name
), (char *)module
- delta
, module
);
94 end
= (IMAGE_BASE_RELOCATION
*)((char *)rel
+ size
);
95 while (rel
< end
&& rel
->SizeOfBlock
)
97 void *page
= (char *)module
+ rel
->VirtualAddress
;
98 VirtualProtect( page
, info
.PageSize
, PAGE_EXECUTE_READWRITE
, &old
);
99 rel
= LdrProcessRelocationBlock( page
, (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
100 (USHORT
*)(rel
+ 1), delta
);
101 if (old
!= PAGE_EXECUTE_READWRITE
) VirtualProtect( page
, info
.PageSize
, old
, NULL
);
102 if (!rel
) goto error
;
104 /* make sure we don't try again */
105 size
= FIELD_OFFSET( IMAGE_NT_HEADERS
, OptionalHeader
) + nt
->FileHeader
.SizeOfOptionalHeader
;
106 VirtualProtect( nt
, size
, PAGE_READWRITE
, &old
);
107 nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= 0;
108 VirtualProtect( nt
, size
, old
, NULL
);
112 /* make sure imports are relocated too */
114 if ((imports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
116 for (i
= 0; imports
[i
].Name
&& imports
[i
].FirstThunk
; i
++)
118 char *name
= (char *)module
+ imports
[i
].Name
;
119 WCHAR buffer
[32], *p
= buffer
;
121 while (p
< buffer
+ 32) if (!(*p
++ = *name
++)) break;
122 if (p
<= buffer
+ 32) FreeLibrary( load_driver_module( buffer
) );
129 FreeLibrary( module
);
133 /* call the driver init entry point */
134 static NTSTATUS
init_driver( HMODULE module
, UNICODE_STRING
*keyname
)
138 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
140 if (!nt
->OptionalHeader
.AddressOfEntryPoint
) return STATUS_SUCCESS
;
142 driver_obj
.Size
= sizeof(driver_obj
);
143 driver_obj
.DriverSection
= find_ldr_module( module
);
144 driver_obj
.DriverInit
= (PDRIVER_INITIALIZE
)((char *)module
+ nt
->OptionalHeader
.AddressOfEntryPoint
);
145 driver_obj
.DriverExtension
= &driver_extension
;
147 driver_extension
.DriverObject
= &driver_obj
;
148 driver_extension
.ServiceKeyName
= *keyname
;
150 if (WINE_TRACE_ON(relay
))
151 WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
152 driver_obj
.DriverInit
, &driver_obj
, wine_dbgstr_w(keyname
->Buffer
) );
154 status
= driver_obj
.DriverInit( &driver_obj
, keyname
);
156 if (WINE_TRACE_ON(relay
))
157 WINE_DPRINTF( "%04x:Ret driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
158 driver_obj
.DriverInit
, &driver_obj
, wine_dbgstr_w(keyname
->Buffer
), status
);
160 WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name
), &driver_obj
);
161 WINE_TRACE( "- DriverInit = %p\n", driver_obj
.DriverInit
);
162 WINE_TRACE( "- DriverStartIo = %p\n", driver_obj
.DriverStartIo
);
163 WINE_TRACE( "- DriverUnload = %p\n", driver_obj
.DriverUnload
);
164 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
165 WINE_TRACE( "- MajorFunction[%d] = %p\n", i
, driver_obj
.MajorFunction
[i
] );
170 /* load the .sys module for a device driver */
171 static BOOL
load_driver(void)
173 static const WCHAR driversW
[] = {'\\','d','r','i','v','e','r','s','\\',0};
174 static const WCHAR systemrootW
[] = {'\\','S','y','s','t','e','m','R','o','o','t','\\',0};
175 static const WCHAR postfixW
[] = {'.','s','y','s',0};
176 static const WCHAR ntprefixW
[] = {'\\','?','?','\\',0};
177 static const WCHAR ImagePathW
[] = {'I','m','a','g','e','P','a','t','h',0};
178 static const WCHAR servicesW
[] = {'\\','R','e','g','i','s','t','r','y',
179 '\\','M','a','c','h','i','n','e',
180 '\\','S','y','s','t','e','m',
181 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
182 '\\','S','e','r','v','i','c','e','s','\\',0};
184 UNICODE_STRING keypath
;
186 LPWSTR path
= NULL
, str
;
189 str
= HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW
) + strlenW(driver_name
)*sizeof(WCHAR
) );
190 lstrcpyW( str
, servicesW
);
191 lstrcatW( str
, driver_name
);
193 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, str
+ 18 /* skip \registry\machine */, &driver_hkey
))
195 WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str
), GetLastError() );
196 HeapFree( GetProcessHeap(), 0, str
);
199 RtlInitUnicodeString( &keypath
, str
);
201 /* read the executable path from memory */
203 if (!RegQueryValueExW( driver_hkey
, ImagePathW
, NULL
, &type
, NULL
, &size
))
205 str
= HeapAlloc( GetProcessHeap(), 0, size
);
206 if (!RegQueryValueExW( driver_hkey
, ImagePathW
, NULL
, &type
, (LPBYTE
)str
, &size
))
208 size
= ExpandEnvironmentStringsW(str
,NULL
,0);
209 path
= HeapAlloc(GetProcessHeap(),0,size
*sizeof(WCHAR
));
210 ExpandEnvironmentStringsW(str
,path
,size
);
212 HeapFree( GetProcessHeap(), 0, str
);
213 if (!path
) return FALSE
;
215 if (!strncmpiW( path
, systemrootW
, 12 ))
217 WCHAR buffer
[MAX_PATH
];
219 GetWindowsDirectoryW(buffer
, MAX_PATH
);
221 str
= HeapAlloc(GetProcessHeap(), 0, (size
-11 + strlenW(buffer
))
223 lstrcpyW(str
, buffer
);
224 lstrcatW(str
, path
+ 11);
225 HeapFree( GetProcessHeap(), 0, path
);
228 else if (!strncmpW( path
, ntprefixW
, 4 ))
235 /* default is to use the driver name + ".sys" */
236 WCHAR buffer
[MAX_PATH
];
237 GetSystemDirectoryW(buffer
, MAX_PATH
);
238 path
= HeapAlloc(GetProcessHeap(),0,
239 (strlenW(buffer
) + strlenW(driversW
) + strlenW(driver_name
) + strlenW(postfixW
) + 1)
241 lstrcpyW(path
, buffer
);
242 lstrcatW(path
, driversW
);
243 lstrcatW(path
, driver_name
);
244 lstrcatW(path
, postfixW
);
248 WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str
) );
250 module
= load_driver_module( str
);
251 HeapFree( GetProcessHeap(), 0, path
);
252 if (!module
) return FALSE
;
254 init_driver( module
, &keypath
);
258 static DWORD WINAPI
service_handler( DWORD ctrl
, DWORD event_type
, LPVOID event_data
, LPVOID context
)
260 SERVICE_STATUS status
;
262 status
.dwServiceType
= SERVICE_WIN32
;
263 status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
264 status
.dwWin32ExitCode
= 0;
265 status
.dwServiceSpecificExitCode
= 0;
266 status
.dwCheckPoint
= 0;
267 status
.dwWaitHint
= 0;
271 case SERVICE_CONTROL_STOP
:
272 case SERVICE_CONTROL_SHUTDOWN
:
273 WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name
) );
274 status
.dwCurrentState
= SERVICE_STOP_PENDING
;
275 status
.dwControlsAccepted
= 0;
276 SetServiceStatus( service_handle
, &status
);
277 SetEvent( stop_event
);
280 WINE_FIXME( "got service ctrl %x for %s\n", ctrl
, wine_dbgstr_w(driver_name
) );
281 status
.dwCurrentState
= SERVICE_RUNNING
;
282 SetServiceStatus( service_handle
, &status
);
287 static void WINAPI
ServiceMain( DWORD argc
, LPWSTR
*argv
)
289 SERVICE_STATUS status
;
291 WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name
) );
293 stop_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
295 service_handle
= RegisterServiceCtrlHandlerExW( driver_name
, service_handler
, NULL
);
299 status
.dwServiceType
= SERVICE_WIN32
;
300 status
.dwCurrentState
= SERVICE_START_PENDING
;
301 status
.dwControlsAccepted
= 0;
302 status
.dwWin32ExitCode
= 0;
303 status
.dwServiceSpecificExitCode
= 0;
304 status
.dwCheckPoint
= 0;
305 status
.dwWaitHint
= 10000;
306 SetServiceStatus( service_handle
, &status
);
310 status
.dwCurrentState
= SERVICE_RUNNING
;
311 status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
312 SetServiceStatus( service_handle
, &status
);
314 wine_ntoskrnl_main_loop( stop_event
);
316 else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name
) );
318 status
.dwCurrentState
= SERVICE_STOPPED
;
319 status
.dwControlsAccepted
= 0;
320 SetServiceStatus( service_handle
, &status
);
321 WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name
) );
324 int wmain( int argc
, WCHAR
*argv
[] )
326 SERVICE_TABLE_ENTRYW service_table
[2];
328 if (!(driver_name
= argv
[1]))
330 WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
334 service_table
[0].lpServiceName
= argv
[1];
335 service_table
[0].lpServiceProc
= ServiceMain
;
336 service_table
[1].lpServiceName
= NULL
;
337 service_table
[1].lpServiceProc
= NULL
;
339 StartServiceCtrlDispatcherW( service_table
);