urlmon: Don't create stgmed_obj for binding to object.
[wine] / programs / winedevice / device.c
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 <stdarg.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "winsvc.h"
31 #include "ddk/wdm.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
36 WINE_DECLARE_DEBUG_CHANNEL(relay);
37
38 extern NTSTATUS wine_ntoskrnl_main_loop( HANDLE stop_event );
39
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;
46
47 /* find the LDR_MODULE corresponding to the driver module */
48 static LDR_MODULE *find_ldr_module( HMODULE module )
49 {
50     LIST_ENTRY *entry, *list = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
51
52     for (entry = list->Flink; entry != list; entry = entry->Flink)
53     {
54         LDR_MODULE *ldr = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
55         if (ldr->BaseAddress == module) return ldr;
56         if (ldr->BaseAddress > (void *)module) break;
57     }
58     return NULL;
59 }
60
61 /* call the driver init entry point */
62 static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
63 {
64     unsigned int i;
65     NTSTATUS status;
66     const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
67
68     if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
69
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;
74
75     driver_extension.DriverObject   = &driver_obj;
76     driver_extension.ServiceKeyName = *keyname;
77
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) );
81
82     status = driver_obj.DriverInit( &driver_obj, keyname );
83
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 );
87
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] );
94
95     return status;
96 }
97
98 /* load the .sys module for a device driver */
99 static BOOL load_driver(void)
100 {
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};
108
109     UNICODE_STRING keypath;
110     HMODULE module;
111     LPWSTR path = NULL, str;
112     DWORD type, size;
113
114     str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
115     lstrcpyW( str, servicesW );
116     lstrcatW( str, driver_name );
117
118     if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
119     {
120         WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
121         HeapFree( GetProcessHeap(), 0, str);
122         return FALSE;
123     }
124     RtlInitUnicodeString( &keypath, str );
125
126     /* read the executable path from memory */
127     size = 0;
128     if (RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size )) return FALSE;
129
130     str = HeapAlloc( GetProcessHeap(), 0, size );
131     if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
132     {
133         size = ExpandEnvironmentStringsW(str,NULL,0);
134         path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
135         ExpandEnvironmentStringsW(str,path,size);
136     }
137     HeapFree( GetProcessHeap(), 0, str );
138     if (!path) return FALSE;
139
140     /* make sure msvcrt is loaded to resolve the ntoskrnl.exe forwards */
141     LoadLibraryA( "msvcrt.dll" );
142
143     /* GameGuard uses an NT-style path name */
144     str = path;
145     if (!strncmpW( path, ntprefixW, 4 )) str += 4;
146
147     WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
148
149     module = LoadLibraryW( str );
150     HeapFree( GetProcessHeap(), 0, path );
151     if (!module) return FALSE;
152
153     init_driver( module, &keypath );
154     return TRUE;
155 }
156
157 static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
158 {
159     SERVICE_STATUS status;
160
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;
167
168     switch(ctrl)
169     {
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 );
177         return NO_ERROR;
178     default:
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 );
182         return NO_ERROR;
183     }
184 }
185
186 static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
187 {
188     SERVICE_STATUS status;
189
190     WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
191
192     stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
193
194     service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
195
196     status.dwServiceType             = SERVICE_WIN32;
197     status.dwCurrentState            = SERVICE_START_PENDING;
198     status.dwControlsAccepted        = 0;
199     status.dwWin32ExitCode           = 0;
200     status.dwServiceSpecificExitCode = 0;
201     status.dwCheckPoint              = 0;
202     status.dwWaitHint                = 10000;
203     SetServiceStatus( service_handle, &status );
204
205     if (load_driver())
206     {
207         status.dwCurrentState     = SERVICE_RUNNING;
208         status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
209         SetServiceStatus( service_handle, &status );
210
211         wine_ntoskrnl_main_loop( stop_event );
212     }
213     else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
214
215     status.dwCurrentState     = SERVICE_STOPPED;
216     status.dwControlsAccepted = 0;
217     SetServiceStatus( service_handle, &status );
218     WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
219 }
220
221 int wmain( int argc, WCHAR *argv[] )
222 {
223     SERVICE_TABLE_ENTRYW service_table[2];
224
225     if (!(driver_name = argv[1]))
226     {
227         WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
228         return 1;
229     }
230
231     service_table[0].lpServiceName = argv[1];
232     service_table[0].lpServiceProc = ServiceMain;
233     service_table[1].lpServiceName = NULL;
234     service_table[1].lpServiceProc = NULL;
235
236     StartServiceCtrlDispatcherW( service_table );
237     return 0;
238 }