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

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

Version: ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  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 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     const IMAGE_NT_HEADERS *nt;
 68     size_t page_size = getpagesize();
 69     int delta;
 70     HMODULE module = LoadLibraryW( name );
 71 
 72     if (!module) return NULL;
 73     nt = RtlImageNtHeader( module );
 74 
 75     if (!(delta = (char *)module - (char *)nt->OptionalHeader.ImageBase)) return module;
 76 
 77     /* the loader does not apply relocations to non page-aligned binaries or executables,
 78      * we have to do it ourselves */
 79 
 80     if (nt->OptionalHeader.SectionAlignment < page_size ||
 81         !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
 82     {
 83         ULONG size;
 84         DWORD old;
 85         IMAGE_BASE_RELOCATION *rel, *end;
 86 
 87         if ((rel = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size )))
 88         {
 89             WINE_TRACE( "%s: relocating from %p to %p\n",
 90                         wine_dbgstr_w(name), (char *)module - delta, module );
 91             end = (IMAGE_BASE_RELOCATION *)((char *)rel + size);
 92             while (rel < end && rel->SizeOfBlock)
 93             {
 94                 void *page = (char *)module + rel->VirtualAddress;
 95                 VirtualProtect( page, page_size, PAGE_EXECUTE_READWRITE, &old );
 96                 rel = LdrProcessRelocationBlock( page, (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
 97                                                  (USHORT *)(rel + 1), delta );
 98                 if (old != PAGE_EXECUTE_READWRITE) VirtualProtect( page, page_size, old, NULL );
 99                 if (!rel) goto error;
100             }
101         }
102     }
103     return module;
104 
105 error:
106     FreeLibrary( module );
107     return NULL;
108 }
109 
110 /* call the driver init entry point */
111 static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
112 {
113     unsigned int i;
114     NTSTATUS status;
115     const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
116 
117     if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
118 
119     driver_obj.Size            = sizeof(driver_obj);
120     driver_obj.DriverSection   = find_ldr_module( module );
121     driver_obj.DriverInit      = (PDRIVER_INITIALIZE)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
122     driver_obj.DriverExtension = &driver_extension;
123 
124     driver_extension.DriverObject   = &driver_obj;
125     driver_extension.ServiceKeyName = *keyname;
126 
127     if (WINE_TRACE_ON(relay))
128         WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
129                       driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer) );
130 
131     status = driver_obj.DriverInit( &driver_obj, keyname );
132 
133     if (WINE_TRACE_ON(relay))
134         WINE_DPRINTF( "%04x:Ret  driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
135                       driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer), status );
136 
137     WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name), &driver_obj );
138     WINE_TRACE( "- DriverInit = %p\n", driver_obj.DriverInit );
139     WINE_TRACE( "- DriverStartIo = %p\n", driver_obj.DriverStartIo );
140     WINE_TRACE( "- DriverUnload = %p\n", driver_obj.DriverUnload );
141     for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
142         WINE_TRACE( "- MajorFunction[%d] = %p\n", i, driver_obj.MajorFunction[i] );
143 
144     return status;
145 }
146 
147 /* load the .sys module for a device driver */
148 static BOOL load_driver(void)
149 {
150     static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
151     static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
152     static const WCHAR servicesW[] = {'\\','R','e','g','i','s','t','r','y',
153                                       '\\','M','a','c','h','i','n','e',
154                                       '\\','S','y','s','t','e','m',
155                                       '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
156                                       '\\','S','e','r','v','i','c','e','s','\\',0};
157 
158     UNICODE_STRING keypath;
159     HMODULE module;
160     LPWSTR path = NULL, str;
161     DWORD type, size;
162 
163     str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
164     lstrcpyW( str, servicesW );
165     lstrcatW( str, driver_name );
166 
167     if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
168     {
169         WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
170         HeapFree( GetProcessHeap(), 0, str);
171         return FALSE;
172     }
173     RtlInitUnicodeString( &keypath, str );
174 
175     /* read the executable path from memory */
176     size = 0;
177     if (RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size )) return FALSE;
178 
179     str = HeapAlloc( GetProcessHeap(), 0, size );
180     if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
181     {
182         size = ExpandEnvironmentStringsW(str,NULL,0);
183         path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
184         ExpandEnvironmentStringsW(str,path,size);
185     }
186     HeapFree( GetProcessHeap(), 0, str );
187     if (!path) return FALSE;
188 
189     /* make sure msvcrt is loaded to resolve the ntoskrnl.exe forwards */
190     LoadLibraryA( "msvcrt.dll" );
191 
192     /* GameGuard uses an NT-style path name */
193     str = path;
194     if (!strncmpW( path, ntprefixW, 4 )) str += 4;
195 
196     WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
197 
198     module = load_driver_module( str );
199     HeapFree( GetProcessHeap(), 0, path );
200     if (!module) return FALSE;
201 
202     init_driver( module, &keypath );
203     return TRUE;
204 }
205 
206 static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
207 {
208     SERVICE_STATUS status;
209 
210     status.dwServiceType             = SERVICE_WIN32;
211     status.dwControlsAccepted        = SERVICE_ACCEPT_STOP;
212     status.dwWin32ExitCode           = 0;
213     status.dwServiceSpecificExitCode = 0;
214     status.dwCheckPoint              = 0;
215     status.dwWaitHint                = 0;
216 
217     switch(ctrl)
218     {
219     case SERVICE_CONTROL_STOP:
220     case SERVICE_CONTROL_SHUTDOWN:
221         WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name) );
222         status.dwCurrentState     = SERVICE_STOP_PENDING;
223         status.dwControlsAccepted = 0;
224         SetServiceStatus( service_handle, &status );
225         SetEvent( stop_event );
226         return NO_ERROR;
227     default:
228         WINE_FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
229         status.dwCurrentState = SERVICE_RUNNING;
230         SetServiceStatus( service_handle, &status );
231         return NO_ERROR;
232     }
233 }
234 
235 static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
236 {
237     SERVICE_STATUS status;
238 
239     WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
240 
241     stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
242 
243     service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
244     if (!service_handle)
245         return;
246 
247     status.dwServiceType             = SERVICE_WIN32;
248     status.dwCurrentState            = SERVICE_START_PENDING;
249     status.dwControlsAccepted        = 0;
250     status.dwWin32ExitCode           = 0;
251     status.dwServiceSpecificExitCode = 0;
252     status.dwCheckPoint              = 0;
253     status.dwWaitHint                = 10000;
254     SetServiceStatus( service_handle, &status );
255 
256     if (load_driver())
257     {
258         status.dwCurrentState     = SERVICE_RUNNING;
259         status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
260         SetServiceStatus( service_handle, &status );
261 
262         wine_ntoskrnl_main_loop( stop_event );
263     }
264     else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
265 
266     status.dwCurrentState     = SERVICE_STOPPED;
267     status.dwControlsAccepted = 0;
268     SetServiceStatus( service_handle, &status );
269     WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
270 }
271 
272 int wmain( int argc, WCHAR *argv[] )
273 {
274     SERVICE_TABLE_ENTRYW service_table[2];
275 
276     if (!(driver_name = argv[1]))
277     {
278         WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
279         return 1;
280     }
281 
282     service_table[0].lpServiceName = argv[1];
283     service_table[0].lpServiceProc = ServiceMain;
284     service_table[1].lpServiceName = NULL;
285     service_table[1].lpServiceProc = NULL;
286 
287     StartServiceCtrlDispatcherW( service_table );
288     return 0;
289 }
290 

~ [ 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.