2013-12-17 02:19:52 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: GPL - See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Virtual DOS Machine
|
|
|
|
* FILE: vddsup.c
|
2013-12-17 23:13:11 +00:00
|
|
|
* PURPOSE: Virtual Device Drivers (VDD) Support
|
2013-12-17 02:19:52 +00:00
|
|
|
* PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
|
|
|
|
#include "emulator.h"
|
|
|
|
#include "vddsup.h"
|
|
|
|
|
|
|
|
#include "bop.h"
|
|
|
|
#include "registers.h"
|
|
|
|
|
2013-12-17 22:10:58 +00:00
|
|
|
typedef VOID (WINAPI *VDD_PROC)(VOID);
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
typedef struct _VDD_MODULE
|
|
|
|
{
|
2013-12-17 22:10:58 +00:00
|
|
|
HMODULE hDll;
|
|
|
|
VDD_PROC DispatchRoutine;
|
2013-12-17 02:19:52 +00:00
|
|
|
} VDD_MODULE, *PVDD_MODULE;
|
|
|
|
|
|
|
|
/* BOP Identifiers */
|
|
|
|
#define BOP_3RDPARTY 0x58 // 3rd-party VDD BOP
|
|
|
|
|
|
|
|
/* PRIVATE VARIABLES **********************************************************/
|
|
|
|
|
2013-12-17 22:34:08 +00:00
|
|
|
// TODO: Maybe use a linked list.
|
|
|
|
// But the number of elements must be <= MAXUSHORT
|
2013-12-17 22:10:58 +00:00
|
|
|
#define MAX_VDD_MODULES 0xFF + 1
|
2013-12-17 22:34:08 +00:00
|
|
|
VDD_MODULE VDDList[MAX_VDD_MODULES] = {{NULL}};
|
|
|
|
|
|
|
|
// Valid handles of VDD DLLs start at 1 and finish at MAX_VDD_MODULES
|
|
|
|
#define ENTRY_TO_HANDLE(Entry) ((Entry) + 1)
|
|
|
|
#define HANDLE_TO_ENTRY(Handle) ((Handle) - 1)
|
|
|
|
#define IS_VALID_HANDLE(Handle) ((Handle) > 0 && (Handle) <= MAX_VDD_MODULES)
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
/* PRIVATE FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
USHORT GetNextFreeVDDEntry(VOID)
|
|
|
|
{
|
|
|
|
USHORT Entry = MAX_VDD_MODULES;
|
|
|
|
for (Entry = 0; Entry < sizeof(VDDList)/sizeof(VDDList[0]); ++Entry)
|
|
|
|
{
|
|
|
|
if (VDDList[Entry].hDll == NULL) break;
|
|
|
|
}
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID WINAPI ThirdPartyVDDBop(LPWORD Stack)
|
|
|
|
{
|
|
|
|
/* Get the Function Number and skip it */
|
|
|
|
BYTE FuncNum = *(PBYTE)SEG_OFF_TO_PTR(getCS(), getIP());
|
|
|
|
setIP(getIP() + 1);
|
|
|
|
|
|
|
|
switch (FuncNum)
|
|
|
|
{
|
|
|
|
/* RegisterModule */
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
BOOL Success = TRUE;
|
2013-12-17 22:10:58 +00:00
|
|
|
WORD RetVal = 0;
|
|
|
|
WORD Entry = 0;
|
2013-12-17 02:19:52 +00:00
|
|
|
LPCSTR DllName = NULL,
|
2013-12-17 22:10:58 +00:00
|
|
|
InitRoutineName = NULL,
|
2013-12-17 02:19:52 +00:00
|
|
|
DispatchRoutineName = NULL;
|
|
|
|
HMODULE hDll = NULL;
|
2013-12-17 22:10:58 +00:00
|
|
|
VDD_PROC InitRoutine = NULL,
|
|
|
|
DispatchRoutine = NULL;
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
DPRINT1("RegisterModule() called\n");
|
|
|
|
|
|
|
|
/* Clear the Carry Flag (no error happened so far) */
|
|
|
|
setCF(0);
|
|
|
|
|
|
|
|
/* Retrieve the VDD name in DS:SI */
|
2013-12-17 22:34:08 +00:00
|
|
|
DllName = (LPCSTR)SEG_OFF_TO_PTR(getDS(), getSI());
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
/* Retrieve the initialization routine API name in ES:DI (optional --> ES=DI=0) */
|
|
|
|
if (getES() != 0 || getDI() != 0)
|
2013-12-17 22:34:08 +00:00
|
|
|
InitRoutineName = (LPCSTR)SEG_OFF_TO_PTR(getES(), getDI());
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
/* Retrieve the dispatch routine API name in DS:BX */
|
2013-12-17 22:34:08 +00:00
|
|
|
DispatchRoutineName = (LPCSTR)SEG_OFF_TO_PTR(getDS(), getBX());
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
DPRINT1("DllName = '%s' - InitRoutineName = '%s' - DispatchRoutineName = '%s'\n",
|
|
|
|
(DllName ? DllName : "n/a"),
|
|
|
|
(InitRoutineName ? InitRoutineName : "n/a"),
|
|
|
|
(DispatchRoutineName ? DispatchRoutineName : "n/a"));
|
|
|
|
|
|
|
|
/* Load the VDD DLL */
|
|
|
|
hDll = LoadLibraryA(DllName);
|
|
|
|
if (hDll == NULL)
|
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
Success = FALSE;
|
|
|
|
|
|
|
|
if (LastError == ERROR_NOT_ENOUGH_MEMORY)
|
|
|
|
{
|
|
|
|
DPRINT1("Not enough memory to load DLL '%s'\n", DllName);
|
|
|
|
RetVal = 4;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to load DLL '%s'; last error = %d\n", DllName, LastError);
|
|
|
|
RetVal = 1;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the initialization routine if needed */
|
|
|
|
if (InitRoutineName)
|
|
|
|
{
|
2013-12-17 22:10:58 +00:00
|
|
|
InitRoutine = (VDD_PROC)GetProcAddress(hDll, InitRoutineName);
|
2013-12-17 02:19:52 +00:00
|
|
|
if (InitRoutine == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to load the initialization routine '%s'\n", InitRoutineName);
|
|
|
|
Success = FALSE;
|
|
|
|
RetVal = 3;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the dispatch routine */
|
2013-12-17 22:10:58 +00:00
|
|
|
DispatchRoutine = (VDD_PROC)GetProcAddress(hDll, DispatchRoutineName);
|
2013-12-17 02:19:52 +00:00
|
|
|
if (DispatchRoutine == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to load the dispatch routine '%s'\n", DispatchRoutineName);
|
|
|
|
Success = FALSE;
|
|
|
|
RetVal = 2;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we arrived there, that means everything is OK */
|
|
|
|
|
|
|
|
/* Register the VDD DLL */
|
|
|
|
Entry = GetNextFreeVDDEntry();
|
|
|
|
if (Entry == MAX_VDD_MODULES)
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to create a new VDD module entry\n");
|
|
|
|
Success = FALSE;
|
|
|
|
RetVal = 4;
|
|
|
|
goto Quit;
|
|
|
|
}
|
|
|
|
VDDList[Entry].hDll = hDll;
|
|
|
|
VDDList[Entry].DispatchRoutine = DispatchRoutine;
|
|
|
|
|
|
|
|
/* Call the initialization routine if needed */
|
|
|
|
if (InitRoutine) InitRoutine();
|
|
|
|
|
2013-12-17 22:34:08 +00:00
|
|
|
/* We succeeded. RetVal will contain a valid VDD DLL handle */
|
2013-12-17 02:19:52 +00:00
|
|
|
Success = TRUE;
|
2013-12-17 22:34:08 +00:00
|
|
|
RetVal = ENTRY_TO_HANDLE(Entry); // Convert the entry to a valid handle
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
Quit:
|
|
|
|
if (!Success)
|
|
|
|
{
|
|
|
|
/* Unload the VDD DLL */
|
|
|
|
if (hDll) FreeLibrary(hDll);
|
|
|
|
|
|
|
|
/* Set the Carry Flag to indicate that an error happened */
|
|
|
|
setCF(1);
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// /* Clear the Carry Flag (success) */
|
|
|
|
// setCF(0);
|
|
|
|
// }
|
|
|
|
setAX(RetVal);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UnRegisterModule */
|
|
|
|
case 1:
|
|
|
|
{
|
2013-12-17 22:34:08 +00:00
|
|
|
WORD Handle = getAX();
|
|
|
|
WORD Entry = HANDLE_TO_ENTRY(Handle); // Convert the handle to a valid entry
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
DPRINT1("UnRegisterModule() called\n");
|
|
|
|
|
|
|
|
/* Sanity checks */
|
2013-12-17 22:34:08 +00:00
|
|
|
if (!IS_VALID_HANDLE(Handle) || VDDList[Entry].hDll == NULL)
|
2013-12-17 02:19:52 +00:00
|
|
|
{
|
|
|
|
DPRINT1("Invalid VDD DLL Handle: %d\n", Entry);
|
|
|
|
/* Stop the VDM */
|
|
|
|
VdmRunning = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unregister the VDD DLL */
|
|
|
|
FreeLibrary(VDDList[Entry].hDll);
|
|
|
|
VDDList[Entry].hDll = NULL;
|
|
|
|
VDDList[Entry].DispatchRoutine = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DispatchCall */
|
|
|
|
case 2:
|
|
|
|
{
|
2013-12-17 22:34:08 +00:00
|
|
|
WORD Handle = getAX();
|
|
|
|
WORD Entry = HANDLE_TO_ENTRY(Handle); // Convert the handle to a valid entry
|
2013-12-17 02:19:52 +00:00
|
|
|
|
|
|
|
DPRINT1("DispatchCall() called\n");
|
|
|
|
|
|
|
|
/* Sanity checks */
|
2013-12-17 22:34:08 +00:00
|
|
|
if (!IS_VALID_HANDLE(Handle) ||
|
2013-12-17 02:19:52 +00:00
|
|
|
VDDList[Entry].hDll == NULL ||
|
|
|
|
VDDList[Entry].DispatchRoutine == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Invalid VDD DLL Handle: %d\n", Entry);
|
|
|
|
/* Stop the VDM */
|
|
|
|
VdmRunning = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the dispatch routine */
|
|
|
|
VDDList[Entry].DispatchRoutine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
DPRINT1("Unknown 3rd-party VDD BOP Function: 0x%02X\n", FuncNum);
|
|
|
|
setCF(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
VOID VDDSupInitialize(VOID)
|
|
|
|
{
|
|
|
|
/* Register the 3rd-party VDD BOP Handler */
|
|
|
|
RegisterBop(BOP_3RDPARTY, ThirdPartyVDDBop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|