~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/winedevice/device.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Service process to load a kernel driver
  3  *
  4  * Copyright 2007 Alexandre Julliard
  5  *
  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.
 10  *
 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.
 15  *
 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
 19  */
 20 
 21 #include "config.h"
 22 #include "wine/port.h"
 23 
 24 #include <stdarg.h>
 25 
 26 #include "ntstatus.h"
 27 #define WIN32_NO_STATUS
 28 #include "windef.h"
 29 #include "winbase.h"
 30 #include "winternl.h"
 31 #include "winreg.h"
 32 #include "winnls.h"
 33 #include "winsvc.h"
 34 #include "ddk/wdm.h"
 35 #include "wine/unicode.h"
 36 #include "wine/debug.h"
 37 
 38 WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
 39 WINE_DECLARE_DEBUG_CHANNEL(relay);
 40 
 41 extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
 42 
 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;
 49 
 50 /* find the LDR_MODULE corresponding to the driver module */
 51 static LDR_MODULE *find_ldr_module( HMODULE module )
 52 {
 53     LIST_ENTRY *entry, *list = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
 54 
 55     for (entry = list->Flink; entry != list; entry = entry->Flink)
 56     {
 57         LDR_MODULE *ldr = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
 58         if (ldr->BaseAddress == module) return ldr;
 59         if (ldr->BaseAddress > (void *)module) break;
 60     }
 61     return NULL;
 62 }
 63 
 64 /* load the driver module file */
 65 static HMODULE load_driver_module( const WCHAR *name )
 66 {
 67     IMAGE_NT_HEADERS *nt;
 68     const IMAGE_IMPORT_DESCRIPTOR *imports;
 69     size_t page_size = getpagesize();
 70     int i, delta;
 71     ULONG size;
 72     HMODULE module = LoadLibraryW( name );
 73 
 74     if (!module) return NULL;
 75     nt = RtlImageNtHeader( module );
 76 
 77     if (!(delta = (char *)module - (char *)nt->OptionalHeader.ImageBase)) return module;
 78 
 79     /* the loader does not apply relocations to non page-aligned binaries or executables,
 80      * we have to do it ourselves */
 81 
 82     if (nt->OptionalHeader.SectionAlignment < page_size ||
 83         !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
 84     {
 85         DWORD old;
 86         IMAGE_BASE_RELOCATION *rel, *end;
 87 
 88         if ((rel = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size )))
 89         {
 90             WINE_TRACE( "%s: relocating from %p to %p\n",
 91                         wine_dbgstr_w(name), (char *)module - delta, module );
 92             end = (IMAGE_BASE_RELOCATION *)((char *)rel + size);
 93             while (rel < end && rel->SizeOfBlock)
 94             {
 95                 void *page = (char *)module + rel->VirtualAddress;
 96                 VirtualProtect( page, page_size, PAGE_EXECUTE_READWRITE, &old );
 97                 rel = LdrProcessRelocationBlock( page, (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
 98                                                  (USHORT *)(rel + 1), delta );
 99                 if (old != PAGE_EXECUTE_READWRITE) VirtualProtect( page, page_size, old, NULL );
100                 if (!rel) goto error;
101             }
102             /* make sure we don't try again */
103             nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = 0;
104         }
105     }
106 
107     /* make sure imports are relocated too */
108 
109     if ((imports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
110     {
111         for (i = 0; imports[i].Name && imports[i].FirstThunk; i++)
112         {
113             char *name = (char *)module + imports[i].Name;
114             WCHAR buffer[32], *p = buffer;
115 
116             while (p < buffer + 32) if (!(*p++ = *name++)) break;
117             if (p <= buffer + 32) FreeLibrary( load_driver_module( buffer ) );
118         }
119     }
120 
121     return module;
122 
123 error:
124     FreeLibrary( module );
125     return NULL;
126 }
127 
128 /* call the driver init entry point */
129 static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
130 {
131     unsigned int i;
132     NTSTATUS status;
133     const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
134 
135     if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
136 
137     driver_obj.Size            = sizeof(driver_obj);
138     driver_obj.DriverSection   = find_ldr_module( module );
139     driver_obj.DriverInit      = (PDRIVER_INITIALIZE)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
140     driver_obj.DriverExtension = &driver_extension;
141 
142     driver_extension.DriverObject   = &driver_obj;
143     driver_extension.ServiceKeyName = *keyname;
144 
145     if (WINE_TRACE_ON(relay))
146         WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
147                       driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer) );
148 
149     status = driver_obj.DriverInit( &driver_obj, keyname );
150 
151     if (WINE_TRACE_ON(relay))
152         WINE_DPRINTF( "%04x:Ret  driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
153                       driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer), status );
154 
155     WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name), &driver_obj );
156     WINE_TRACE( "- DriverInit = %p\n", driver_obj.DriverInit );
157     WINE_TRACE( "- DriverStartIo = %p\n", driver_obj.DriverStartIo );
158     WINE_TRACE( "- DriverUnload = %p\n", driver_obj.DriverUnload );
159     for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
160         WINE_TRACE( "- MajorFunction[%d] = %p\n", i, driver_obj.MajorFunction[i] );
161 
162     return status;
163 }
164 
165 /* load the .sys module for a device driver */
166 static BOOL load_driver(void)
167 {
168     static const WCHAR driversW[] = {'\\','d','r','i','v','e','r','s','\\',0};
169     static const WCHAR postfixW[] = {'.','s','y','s',0};
170     static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
171     static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
172     static const WCHAR servicesW[] = {'\\','R','e','g','i','s','t','r','y',
173                                       '\\','M','a','c','h','i','n','e',
174                                       '\\','S','y','s','t','e','m',
175                                       '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
176                                       '\\','S','e','r','v','i','c','e','s','\\',0};
177 
178     UNICODE_STRING keypath;
179     HMODULE module;
180     LPWSTR path = NULL, str;
181     DWORD type, size;
182 
183     str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
184     lstrcpyW( str, servicesW );
185     lstrcatW( str, driver_name );
186 
187     if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
188     {
189         WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
190         HeapFree( GetProcessHeap(), 0, str);
191         return FALSE;
192     }
193     RtlInitUnicodeString( &keypath, str );
194 
195     /* read the executable path from memory */
196     size = 0;
197     if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size ))
198     {
199         str = HeapAlloc( GetProcessHeap(), 0, size );
200         if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
201         {
202             size = ExpandEnvironmentStringsW(str,NULL,0);
203             path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
204             ExpandEnvironmentStringsW(str,path,size);
205         }
206         HeapFree( GetProcessHeap(), 0, str );
207         if (!path) return FALSE;
208     }
209     else
210     {
211         /* default is to use the driver name + ".sys" */
212         WCHAR buffer[MAX_PATH];
213         GetSystemDirectoryW(buffer, MAX_PATH);
214         path = HeapAlloc(GetProcessHeap(),0,
215           (strlenW(buffer) + strlenW(driversW) + strlenW(driver_name) + strlenW(postfixW) + 1)
216           *sizeof(WCHAR));
217         lstrcpyW(path, buffer);
218         lstrcatW(path, driversW);
219         lstrcatW(path, driver_name);
220         lstrcatW(path, postfixW);
221     }
222 
223     /* GameGuard uses an NT-style path name */
224     str = path;
225     if (!strncmpW( path, ntprefixW, 4 )) str += 4;
226 
227     WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
228 
229     module = load_driver_module( str );
230     HeapFree( GetProcessHeap(), 0, path );
231     if (!module) return FALSE;
232 
233     init_driver( module, &keypath );
234     return TRUE;
235 }
236 
237 static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
238 {
239     SERVICE_STATUS status;
240 
241     status.dwServiceType             = SERVICE_WIN32;
242     status.dwControlsAccepted        = SERVICE_ACCEPT_STOP;
243     status.dwWin32ExitCode           = 0;
244     status.dwServiceSpecificExitCode = 0;
245     status.dwCheckPoint              = 0;
246     status.dwWaitHint                = 0;
247 
248     switch(ctrl)
249     {
250     case SERVICE_CONTROL_STOP:
251     case SERVICE_CONTROL_SHUTDOWN:
252         WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name) );
253         status.dwCurrentState     = SERVICE_STOP_PENDING;
254         status.dwControlsAccepted = 0;
255         SetServiceStatus( service_handle, &status );
256         SetEvent( stop_event );
257         return NO_ERROR;
258     default:
259         WINE_FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
260         status.dwCurrentState = SERVICE_RUNNING;
261         SetServiceStatus( service_handle, &status );
262         return NO_ERROR;
263     }
264 }
265 
266 static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
267 {
268     SERVICE_STATUS status;
269 
270     WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
271 
272     stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
273 
274     service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
275     if (!service_handle)
276         return;
277 
278     status.dwServiceType             = SERVICE_WIN32;
279     status.dwCurrentState            = SERVICE_START_PENDING;
280     status.dwControlsAccepted        = 0;
281     status.dwWin32ExitCode           = 0;
282     status.dwServiceSpecificExitCode = 0;
283     status.dwCheckPoint              = 0;
284     status.dwWaitHint                = 10000;
285     SetServiceStatus( service_handle, &status );
286 
287     if (load_driver())
288     {
289         status.dwCurrentState     = SERVICE_RUNNING;
290         status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
291         SetServiceStatus( service_handle, &status );
292 
293         wine_ntoskrnl_main_loop( stop_event );
294     }
295     else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
296 
297     status.dwCurrentState     = SERVICE_STOPPED;
298     status.dwControlsAccepted = 0;
299     SetServiceStatus( service_handle, &status );
300     WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
301 }
302 
303 int wmain( int argc, WCHAR *argv[] )
304 {
305     SERVICE_TABLE_ENTRYW service_table[2];
306 
307     if (!(driver_name = argv[1]))
308     {
309         WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
310         return 1;
311     }
312 
313     service_table[0].lpServiceName = argv[1];
314     service_table[0].lpServiceProc = ServiceMain;
315     service_table[1].lpServiceName = NULL;
316     service_table[1].lpServiceProc = NULL;
317 
318     StartServiceCtrlDispatcherW( service_table );
319     return 0;
320 }
321 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.