winex11: Implement large data transfers.
[wine/multimedia.git] / programs / winedevice / device.c
blob7afe97edf65c04b32403057044845c3d76223567
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
39 WINE_DECLARE_DEBUG_CHANNEL(relay);
41 extern NTSTATUS 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;
61 return NULL;
64 /* load the driver module file */
65 static HMODULE load_driver_module( const WCHAR *name )
67 const IMAGE_NT_HEADERS *nt;
68 size_t page_size = getpagesize();
69 int delta;
70 HMODULE module = LoadLibraryW( name );
72 if (!module) return NULL;
73 nt = RtlImageNtHeader( module );
75 if (!(delta = (char *)module - (char *)nt->OptionalHeader.ImageBase)) return module;
77 /* the loader does not apply relocations to non page-aligned binaries or executables,
78 * we have to do it ourselves */
80 if (nt->OptionalHeader.SectionAlignment < page_size ||
81 !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
83 ULONG size;
84 DWORD old;
85 IMAGE_BASE_RELOCATION *rel, *end;
87 if ((rel = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size )))
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)
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;
103 return module;
105 error:
106 FreeLibrary( module );
107 return NULL;
110 /* call the driver init entry point */
111 static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
113 unsigned int i;
114 NTSTATUS status;
115 const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
117 if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
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;
124 driver_extension.DriverObject = &driver_obj;
125 driver_extension.ServiceKeyName = *keyname;
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) );
131 status = driver_obj.DriverInit( &driver_obj, keyname );
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 );
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] );
144 return status;
147 /* load the .sys module for a device driver */
148 static BOOL load_driver(void)
150 static const WCHAR driversW[] = {'\\','d','r','i','v','e','r','s','\\',0};
151 static const WCHAR postfixW[] = {'.','s','y','s',0};
152 static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
153 static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
154 static const WCHAR servicesW[] = {'\\','R','e','g','i','s','t','r','y',
155 '\\','M','a','c','h','i','n','e',
156 '\\','S','y','s','t','e','m',
157 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
158 '\\','S','e','r','v','i','c','e','s','\\',0};
160 UNICODE_STRING keypath;
161 HMODULE module;
162 LPWSTR path = NULL, str;
163 DWORD type, size;
165 str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
166 lstrcpyW( str, servicesW );
167 lstrcatW( str, driver_name );
169 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
171 WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
172 HeapFree( GetProcessHeap(), 0, str);
173 return FALSE;
175 RtlInitUnicodeString( &keypath, str );
177 /* read the executable path from memory */
178 size = 0;
179 if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size ))
181 str = HeapAlloc( GetProcessHeap(), 0, size );
182 if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
184 size = ExpandEnvironmentStringsW(str,NULL,0);
185 path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
186 ExpandEnvironmentStringsW(str,path,size);
188 HeapFree( GetProcessHeap(), 0, str );
189 if (!path) return FALSE;
191 else
193 /* default is to use the driver name + ".sys" */
194 WCHAR buffer[MAX_PATH];
195 GetSystemDirectoryW(buffer, MAX_PATH);
196 path = HeapAlloc(GetProcessHeap(),0,
197 (strlenW(buffer) + strlenW(driversW) + strlenW(driver_name) + strlenW(postfixW) + 1)
198 *sizeof(WCHAR));
199 lstrcpyW(path, buffer);
200 lstrcatW(path, driversW);
201 lstrcatW(path, driver_name);
202 lstrcatW(path, postfixW);
205 /* GameGuard uses an NT-style path name */
206 str = path;
207 if (!strncmpW( path, ntprefixW, 4 )) str += 4;
209 WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
211 module = load_driver_module( str );
212 HeapFree( GetProcessHeap(), 0, path );
213 if (!module) return FALSE;
215 init_driver( module, &keypath );
216 return TRUE;
219 static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
221 SERVICE_STATUS status;
223 status.dwServiceType = SERVICE_WIN32;
224 status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
225 status.dwWin32ExitCode = 0;
226 status.dwServiceSpecificExitCode = 0;
227 status.dwCheckPoint = 0;
228 status.dwWaitHint = 0;
230 switch(ctrl)
232 case SERVICE_CONTROL_STOP:
233 case SERVICE_CONTROL_SHUTDOWN:
234 WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name) );
235 status.dwCurrentState = SERVICE_STOP_PENDING;
236 status.dwControlsAccepted = 0;
237 SetServiceStatus( service_handle, &status );
238 SetEvent( stop_event );
239 return NO_ERROR;
240 default:
241 WINE_FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
242 status.dwCurrentState = SERVICE_RUNNING;
243 SetServiceStatus( service_handle, &status );
244 return NO_ERROR;
248 static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
250 SERVICE_STATUS status;
252 WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
254 stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
256 service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
257 if (!service_handle)
258 return;
260 status.dwServiceType = SERVICE_WIN32;
261 status.dwCurrentState = SERVICE_START_PENDING;
262 status.dwControlsAccepted = 0;
263 status.dwWin32ExitCode = 0;
264 status.dwServiceSpecificExitCode = 0;
265 status.dwCheckPoint = 0;
266 status.dwWaitHint = 10000;
267 SetServiceStatus( service_handle, &status );
269 if (load_driver())
271 status.dwCurrentState = SERVICE_RUNNING;
272 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
273 SetServiceStatus( service_handle, &status );
275 wine_ntoskrnl_main_loop( stop_event );
277 else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
279 status.dwCurrentState = SERVICE_STOPPED;
280 status.dwControlsAccepted = 0;
281 SetServiceStatus( service_handle, &status );
282 WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
285 int wmain( int argc, WCHAR *argv[] )
287 SERVICE_TABLE_ENTRYW service_table[2];
289 if (!(driver_name = argv[1]))
291 WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
292 return 1;
295 service_table[0].lpServiceName = argv[1];
296 service_table[0].lpServiceProc = ServiceMain;
297 service_table[1].lpServiceName = NULL;
298 service_table[1].lpServiceProc = NULL;
300 StartServiceCtrlDispatcherW( service_table );
301 return 0;