2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: Virtual Device Drivers (VDD) Support
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9 /* INCLUDES *******************************************************************/
17 #include "registers.h"
21 typedef VOID (WINAPI
*VDD_PROC
)(VOID
);
23 typedef struct _VDD_MODULE
26 VDD_PROC DispatchRoutine
;
27 } VDD_MODULE
, *PVDD_MODULE
;
29 /* PRIVATE VARIABLES **********************************************************/
31 // TODO: Maybe use a linked list.
32 // But the number of elements must be <= MAXUSHORT (MAXWORD)
33 #define MAX_VDD_MODULES 0xFF + 1
34 VDD_MODULE VDDList
[MAX_VDD_MODULES
] = {{NULL
}};
36 // Valid handles of VDD DLLs start at 1 and finish at MAX_VDD_MODULES
37 #define ENTRY_TO_HANDLE(Entry) ((Entry) + 1)
38 #define HANDLE_TO_ENTRY(Handle) ((Handle) - 1)
39 #define IS_VALID_HANDLE(Handle) ((Handle) > 0 && (Handle) <= MAX_VDD_MODULES)
41 /* PRIVATE FUNCTIONS **********************************************************/
43 USHORT
GetNextFreeVDDEntry(VOID
)
45 USHORT Entry
= MAX_VDD_MODULES
;
46 for (Entry
= 0; Entry
< sizeof(VDDList
)/sizeof(VDDList
[0]); ++Entry
)
48 if (VDDList
[Entry
].hDll
== NULL
) break;
53 VOID WINAPI
ThirdPartyVDDBop(LPWORD Stack
)
55 /* Get the Function Number and skip it */
56 BYTE FuncNum
= *(PBYTE
)SEG_OFF_TO_PTR(getCS(), getIP());
67 LPCSTR DllName
= NULL
,
68 InitRoutineName
= NULL
,
69 DispatchRoutineName
= NULL
;
71 VDD_PROC InitRoutine
= NULL
,
72 DispatchRoutine
= NULL
;
74 DPRINT("RegisterModule() called\n");
76 /* Clear the Carry Flag (no error happened so far) */
79 /* Retrieve the next free entry in the table (used later on) */
80 Entry
= GetNextFreeVDDEntry();
81 if (Entry
>= MAX_VDD_MODULES
)
83 DPRINT1("Failed to create a new VDD module entry\n");
89 /* Retrieve the VDD name in DS:SI */
90 DllName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getSI());
92 /* Retrieve the initialization routine API name in ES:DI (optional --> ES=DI=0) */
93 if (TO_LINEAR(getES(), getDI()) != 0)
94 InitRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getES(), getDI());
96 /* Retrieve the dispatch routine API name in DS:BX */
97 DispatchRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getBX());
99 DPRINT1("DllName = '%s' - InitRoutineName = '%s' - DispatchRoutineName = '%s'\n",
100 (DllName
? DllName
: "n/a"),
101 (InitRoutineName
? InitRoutineName
: "n/a"),
102 (DispatchRoutineName
? DispatchRoutineName
: "n/a"));
104 /* Load the VDD DLL */
105 hDll
= LoadLibraryA(DllName
);
108 DWORD LastError
= GetLastError();
111 if (LastError
== ERROR_NOT_ENOUGH_MEMORY
)
113 DPRINT1("Not enough memory to load DLL '%s'\n", DllName
);
119 DPRINT1("Failed to load DLL '%s'; last error = %d\n", DllName
, LastError
);
125 /* Load the initialization routine if needed */
128 InitRoutine
= (VDD_PROC
)GetProcAddress(hDll
, InitRoutineName
);
129 if (InitRoutine
== NULL
)
131 DPRINT1("Failed to load the initialization routine '%s'\n", InitRoutineName
);
138 /* Load the dispatch routine */
139 DispatchRoutine
= (VDD_PROC
)GetProcAddress(hDll
, DispatchRoutineName
);
140 if (DispatchRoutine
== NULL
)
142 DPRINT1("Failed to load the dispatch routine '%s'\n", DispatchRoutineName
);
148 /* If we arrived there, that means everything is OK */
150 /* Register the VDD DLL */
151 VDDList
[Entry
].hDll
= hDll
;
152 VDDList
[Entry
].DispatchRoutine
= DispatchRoutine
;
154 /* Call the initialization routine if needed */
155 if (InitRoutine
) InitRoutine();
157 /* We succeeded. RetVal will contain a valid VDD DLL handle */
159 RetVal
= ENTRY_TO_HANDLE(Entry
); // Convert the entry to a valid handle
164 /* Unload the VDD DLL */
165 if (hDll
) FreeLibrary(hDll
);
167 /* Set the Carry Flag to indicate that an error happened */
172 // /* Clear the Carry Flag (success) */
179 /* UnRegisterModule */
182 WORD Handle
= getAX();
183 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
185 DPRINT("UnRegisterModule() called\n");
188 if (!IS_VALID_HANDLE(Handle
) || VDDList
[Entry
].hDll
== NULL
)
190 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
196 /* Unregister the VDD DLL */
197 FreeLibrary(VDDList
[Entry
].hDll
);
198 VDDList
[Entry
].hDll
= NULL
;
199 VDDList
[Entry
].DispatchRoutine
= NULL
;
206 WORD Handle
= getAX();
207 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
209 DPRINT("DispatchCall() called\n");
212 if (!IS_VALID_HANDLE(Handle
) ||
213 VDDList
[Entry
].hDll
== NULL
||
214 VDDList
[Entry
].DispatchRoutine
== NULL
)
216 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
222 /* Call the dispatch routine */
223 VDDList
[Entry
].DispatchRoutine();
229 DPRINT1("Unknown 3rd-party VDD BOP Function: 0x%02X\n", FuncNum
);
236 BOOL
LoadInstallableVDD(VOID
)
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."
248 LPCWSTR VDDKeyName
= L
"SYSTEM\\CurrentControlSet\\Control\\VirtualDeviceDrivers";
249 LPWSTR VDDValueName
= L
"VDD";
250 LPWSTR VDDList
= NULL
;
254 /* Try to open the VDD registry key */
255 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
260 if (Error
== ERROR_FILE_NOT_FOUND
)
262 /* If the key just doesn't exist, don't do anything else */
265 else if (Error
!= ERROR_SUCCESS
)
267 /* The key exists but there was an access error: display an error and quit */
268 DisplayMessage(ERROR_REGVDD
);
273 * Retrieve the size of the VDD registry value
274 * and check that it's of REG_MULTI_SZ type.
276 Error
= RegQueryValueExW(hVDDKey
,
282 if (Error
== ERROR_FILE_NOT_FOUND
)
284 /* If the value just doesn't exist, don't do anything else */
288 else if (Error
!= ERROR_SUCCESS
|| Type
!= REG_MULTI_SZ
)
291 * The value exists but there was an access error or
292 * is of the wrong type: display an error and quit.
294 DisplayMessage(ERROR_REGVDD
);
299 /* Allocate the buffer */
300 BufSize
= (BufSize
< 2*sizeof(WCHAR
) ? 2*sizeof(WCHAR
) : BufSize
);
301 VDDList
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, BufSize
);
304 DisplayMessage(ERROR_MEMORYVDD
);
309 /* Retrieve the list of VDDs to load */
310 if (RegQueryValueExW(hVDDKey
,
315 &BufSize
) != ERROR_SUCCESS
)
317 DisplayMessage(ERROR_REGVDD
);
323 VDDValueName
= VDDList
;
326 DPRINT1("Loading VDD '%S'...", VDDList
);
327 hVDD
= LoadLibraryW(VDDList
);
330 DbgPrint("Failed\n");
331 DisplayMessage(ERROR_LOADVDD
);
335 DbgPrint("Succeeded\n");
337 /* Go to next string */
338 VDDList
+= wcslen(VDDList
) + 1;
340 VDDList
= VDDValueName
;
343 if (VDDList
) HeapFree(GetProcessHeap(), 0, VDDList
);
344 RegCloseKey(hVDDKey
);
348 /* PUBLIC FUNCTIONS ***********************************************************/
350 VOID
VDDSupInitialize(VOID
)
352 /* Register the 3rd-party VDD BOP Handler */
353 RegisterBop(BOP_3RDPARTY
, ThirdPartyVDDBop
);
355 /* Load the installable VDDs from the registry */
356 LoadInstallableVDD();