[NTVDM]
[reactos.git] / reactos / subsystems / mvdm / ntvdm / vddsup.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: vddsup.c
5 * PURPOSE: Virtual Device Drivers (VDD) Support
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "ntvdm.h"
14 #include "emulator.h"
15 #include "vddsup.h"
16
17 #include "cpu/bop.h"
18 #include <isvbop.h>
19
20 typedef VOID (WINAPI *VDD_PROC)(VOID);
21
22 typedef struct _VDD_MODULE
23 {
24 HMODULE hDll;
25 VDD_PROC DispatchRoutine;
26 } VDD_MODULE, *PVDD_MODULE;
27
28 /* PRIVATE VARIABLES **********************************************************/
29
30 // TODO: Maybe use a linked list.
31 // But the number of elements must be <= MAXUSHORT (MAXWORD)
32 #define MAX_VDD_MODULES 0xFF + 1
33 static VDD_MODULE VDDList[MAX_VDD_MODULES] = {{NULL}};
34
35 // Valid handles of VDD DLLs start at 1 and finish at MAX_VDD_MODULES
36 #define ENTRY_TO_HANDLE(Entry) ((Entry) + 1)
37 #define HANDLE_TO_ENTRY(Handle) ((Handle) - 1)
38 #define IS_VALID_HANDLE(Handle) ((Handle) > 0 && (Handle) <= MAX_VDD_MODULES)
39
40 /* PRIVATE FUNCTIONS **********************************************************/
41
42 static USHORT GetNextFreeVDDEntry(VOID)
43 {
44 USHORT Entry = MAX_VDD_MODULES;
45 for (Entry = 0; Entry < ARRAYSIZE(VDDList); ++Entry)
46 {
47 if (VDDList[Entry].hDll == NULL) break;
48 }
49 return Entry;
50 }
51
52 static VOID WINAPI ThirdPartyVDDBop(LPWORD Stack)
53 {
54 /* Get the Function Number and skip it */
55 BYTE FuncNum = *(PBYTE)SEG_OFF_TO_PTR(getCS(), getIP());
56 setIP(getIP() + 1);
57
58 switch (FuncNum)
59 {
60 /* RegisterModule */
61 case 0:
62 {
63 BOOL Success = TRUE;
64 WORD RetVal = 0;
65 WORD Entry = 0;
66 LPCSTR DllName = NULL,
67 InitRoutineName = NULL,
68 DispatchRoutineName = NULL;
69 HMODULE hDll = NULL;
70 VDD_PROC InitRoutine = NULL,
71 DispatchRoutine = NULL;
72
73 DPRINT("RegisterModule() called\n");
74
75 /* Clear the Carry Flag (no error happened so far) */
76 setCF(0);
77
78 /* Retrieve the next free entry in the table (used later on) */
79 Entry = GetNextFreeVDDEntry();
80 if (Entry >= MAX_VDD_MODULES)
81 {
82 DPRINT1("Failed to create a new VDD module entry\n");
83 Success = FALSE;
84 RetVal = 4;
85 goto Quit;
86 }
87
88 /* Retrieve the VDD name in DS:SI */
89 DllName = (LPCSTR)SEG_OFF_TO_PTR(getDS(), getSI());
90
91 /* Retrieve the initialization routine API name in ES:DI (optional --> ES=DI=0) */
92 if (TO_LINEAR(getES(), getDI()) != 0)
93 InitRoutineName = (LPCSTR)SEG_OFF_TO_PTR(getES(), getDI());
94
95 /* Retrieve the dispatch routine API name in DS:BX */
96 DispatchRoutineName = (LPCSTR)SEG_OFF_TO_PTR(getDS(), getBX());
97
98 DPRINT1("DllName = '%s' - InitRoutineName = '%s' - DispatchRoutineName = '%s'\n",
99 (DllName ? DllName : "n/a"),
100 (InitRoutineName ? InitRoutineName : "n/a"),
101 (DispatchRoutineName ? DispatchRoutineName : "n/a"));
102
103 /* Load the VDD DLL */
104 hDll = LoadLibraryA(DllName);
105 if (hDll == NULL)
106 {
107 DWORD LastError = GetLastError();
108 Success = FALSE;
109
110 if (LastError == ERROR_NOT_ENOUGH_MEMORY)
111 {
112 DPRINT1("Not enough memory to load DLL '%s'\n", DllName);
113 RetVal = 4;
114 goto Quit;
115 }
116 else
117 {
118 DPRINT1("Failed to load DLL '%s'; last error = %d\n", DllName, LastError);
119 RetVal = 1;
120 goto Quit;
121 }
122 }
123
124 /* Load the initialization routine if needed */
125 if (InitRoutineName)
126 {
127 InitRoutine = (VDD_PROC)GetProcAddress(hDll, InitRoutineName);
128 if (InitRoutine == NULL)
129 {
130 DPRINT1("Failed to load the initialization routine '%s'\n", InitRoutineName);
131 Success = FALSE;
132 RetVal = 3;
133 goto Quit;
134 }
135 }
136
137 /* Load the dispatch routine */
138 DispatchRoutine = (VDD_PROC)GetProcAddress(hDll, DispatchRoutineName);
139 if (DispatchRoutine == NULL)
140 {
141 DPRINT1("Failed to load the dispatch routine '%s'\n", DispatchRoutineName);
142 Success = FALSE;
143 RetVal = 2;
144 goto Quit;
145 }
146
147 /* If we arrived there, that means everything is OK */
148
149 /* Register the VDD DLL */
150 VDDList[Entry].hDll = hDll;
151 VDDList[Entry].DispatchRoutine = DispatchRoutine;
152
153 /* Call the initialization routine if needed */
154 if (InitRoutine) InitRoutine();
155
156 /* We succeeded. RetVal will contain a valid VDD DLL handle */
157 Success = TRUE;
158 RetVal = ENTRY_TO_HANDLE(Entry); // Convert the entry to a valid handle
159
160 Quit:
161 if (!Success)
162 {
163 /* Unload the VDD DLL */
164 if (hDll) FreeLibrary(hDll);
165
166 /* Set the Carry Flag to indicate that an error happened */
167 setCF(1);
168 }
169 // else
170 // {
171 // /* Clear the Carry Flag (success) */
172 // setCF(0);
173 // }
174 setAX(RetVal);
175 break;
176 }
177
178 /* UnRegisterModule */
179 case 1:
180 {
181 WORD Handle = getAX();
182 WORD Entry = HANDLE_TO_ENTRY(Handle); // Convert the handle to a valid entry
183
184 DPRINT("UnRegisterModule() called\n");
185
186 /* Sanity checks */
187 if (!IS_VALID_HANDLE(Handle) || VDDList[Entry].hDll == NULL)
188 {
189 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry);
190 /* Stop the VDM */
191 EmulatorTerminate();
192 return;
193 }
194
195 /* Unregister the VDD DLL */
196 FreeLibrary(VDDList[Entry].hDll);
197 VDDList[Entry].hDll = NULL;
198 VDDList[Entry].DispatchRoutine = NULL;
199 break;
200 }
201
202 /* DispatchCall */
203 case 2:
204 {
205 WORD Handle = getAX();
206 WORD Entry = HANDLE_TO_ENTRY(Handle); // Convert the handle to a valid entry
207
208 DPRINT("DispatchCall() called\n");
209
210 /* Sanity checks */
211 if (!IS_VALID_HANDLE(Handle) ||
212 VDDList[Entry].hDll == NULL ||
213 VDDList[Entry].DispatchRoutine == NULL)
214 {
215 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry);
216 /* Stop the VDM */
217 EmulatorTerminate();
218 return;
219 }
220
221 /* Call the dispatch routine */
222 VDDList[Entry].DispatchRoutine();
223 break;
224 }
225
226 default:
227 {
228 DPRINT1("Unknown 3rd-party VDD BOP Function: 0x%02X\n", FuncNum);
229 setCF(1);
230 break;
231 }
232 }
233 }
234
235 static BOOL LoadInstallableVDD(VOID)
236 {
237 // FIXME: These strings should be localized.
238 #define ERROR_MEMORYVDD L"Insufficient memory to load installable Virtual Device Drivers."
239 #define ERROR_REGVDD L"Virtual Device Driver format in the registry is invalid."
240 #define ERROR_LOADVDD L"An installable Virtual Device Driver failed Dll initialization."
241
242 BOOL Success = TRUE;
243 LONG Error = 0;
244 DWORD Type = 0;
245 DWORD BufSize = 0;
246
247 HKEY hVDDKey;
248 LPCWSTR VDDKeyName = L"SYSTEM\\CurrentControlSet\\Control\\VirtualDeviceDrivers";
249 LPWSTR VDDValueName = L"VDD";
250 LPWSTR VDDList = NULL;
251
252 HANDLE hVDD;
253
254 /* Try to open the VDD registry key */
255 Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
256 VDDKeyName,
257 0,
258 KEY_QUERY_VALUE,
259 &hVDDKey);
260 if (Error == ERROR_FILE_NOT_FOUND)
261 {
262 /* If the key just doesn't exist, don't do anything else */
263 return TRUE;
264 }
265 else if (Error != ERROR_SUCCESS)
266 {
267 /* The key exists but there was an access error: display an error and quit */
268 DisplayMessage(ERROR_REGVDD);
269 return FALSE;
270 }
271
272 /*
273 * Retrieve the size of the VDD registry value
274 * and check that it's of REG_MULTI_SZ type.
275 */
276 Error = RegQueryValueExW(hVDDKey,
277 VDDValueName,
278 NULL,
279 &Type,
280 NULL,
281 &BufSize);
282 if (Error == ERROR_FILE_NOT_FOUND)
283 {
284 /* If the value just doesn't exist, don't do anything else */
285 Success = TRUE;
286 goto Quit;
287 }
288 else if (Error != ERROR_SUCCESS || Type != REG_MULTI_SZ)
289 {
290 /*
291 * The value exists but there was an access error or
292 * is of the wrong type: display an error and quit.
293 */
294 DisplayMessage(ERROR_REGVDD);
295 Success = FALSE;
296 goto Quit;
297 }
298
299 /* Allocate the buffer */
300 BufSize = (BufSize < 2*sizeof(WCHAR) ? 2*sizeof(WCHAR) : BufSize);
301 VDDList = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, BufSize);
302 if (VDDList == NULL)
303 {
304 DisplayMessage(ERROR_MEMORYVDD);
305 Success = FALSE;
306 goto Quit;
307 }
308
309 /* Retrieve the list of VDDs to load */
310 if (RegQueryValueExW(hVDDKey,
311 VDDValueName,
312 NULL,
313 NULL,
314 (LPBYTE)VDDList,
315 &BufSize) != ERROR_SUCCESS)
316 {
317 DisplayMessage(ERROR_REGVDD);
318 Success = FALSE;
319 goto Quit;
320 }
321
322 /* Load the VDDs */
323 VDDValueName = VDDList;
324 while (*VDDList)
325 {
326 DPRINT1("Loading VDD '%S'...", VDDList);
327 hVDD = LoadLibraryW(VDDList);
328 if (hVDD == NULL)
329 {
330 DbgPrint("Failed\n");
331 DisplayMessage(ERROR_LOADVDD);
332 }
333 else
334 {
335 DbgPrint("Succeeded\n");
336 }
337 /* Go to next string */
338 VDDList += wcslen(VDDList) + 1;
339 }
340 VDDList = VDDValueName;
341
342 Quit:
343 if (VDDList) RtlFreeHeap(RtlGetProcessHeap(), 0, VDDList);
344 RegCloseKey(hVDDKey);
345 return Success;
346 }
347
348 /* PUBLIC FUNCTIONS ***********************************************************/
349
350 VOID VDDSupInitialize(VOID)
351 {
352 /* Register the 3rd-party VDD BOP Handler */
353 RegisterBop(BOP_3RDPARTY, ThirdPartyVDDBop);
354
355 /* Load the installable VDDs from the registry */
356 LoadInstallableVDD();
357 }
358
359 /* EOF */