00001
00002
00003
00004 #include "stdafx.h"
00005 #include "IOHandlers.h"
00006
00007 #define VIRTUALKD_HOOKED_VBOX_DEVICE_NAME "pci"
00008
00009 static PCPDMDEVREG s_pOriginalDevReg = NULL;
00010
00011 static int RTCALL ConstructOverride(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
00012 {
00013 if (!s_pOriginalDevReg)
00014 return VINF_OBJECT_DESTROYED;
00015 pDevIns->pDevReg = s_pOriginalDevReg;
00016 int rc = s_pOriginalDevReg->pfnConstruct(pDevIns, iInstance, pCfgHandle);
00017 if (!RT_SUCCESS(rc))
00018 return rc;
00019 rc = PDMDevHlpIOPortRegister(pDevIns, 0x5658, 2, NULL, VirtualKDPortOutHandler, VirtualKDPortInHandler, NULL, NULL, "VirtualKD interface");
00020 if (!RT_SUCCESS(rc))
00021 return rc;
00022 return VINF_SUCCESS;
00023 }
00024
00025 struct DevRegisterCallbacksHook : public PDMDEVREGCB
00026 {
00027 PPDMDEVREGCB pOriginalCallbacks;
00028
00029 DevRegisterCallbacksHook(const PDMDEVREGCB &Callbacks) : PDMDEVREGCB(Callbacks)
00030 {
00031 pfnRegister = &RegisterOverride;
00032 pfnMMHeapAlloc = &AllocOverride;
00033 }
00034
00035 static int RTCALL RegisterOverride(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pDevReg)
00036 {
00037 if (pDevReg)
00038 {
00039 if (!strcmp(VIRTUALKD_HOOKED_VBOX_DEVICE_NAME, pDevReg->szDeviceName))
00040 {
00041 PPDMDEVREG pReg = new PDMDEVREG();
00042 *pReg = *pDevReg;
00043 s_pOriginalDevReg = pDevReg;
00044 pReg->pfnConstruct = ConstructOverride;
00045 pDevReg = pReg;
00046 }
00047 }
00048 return ((DevRegisterCallbacksHook *)pCallbacks)->pOriginalCallbacks->pfnRegister(((DevRegisterCallbacksHook *)pCallbacks)->pOriginalCallbacks, pDevReg);
00049 }
00050
00051 static void * RTCALL AllocOverride(PPDMDEVREGCB pCallbacks, size_t cb)
00052 {
00053 return ((DevRegisterCallbacksHook *)pCallbacks)->pOriginalCallbacks->pfnMMHeapAlloc(((DevRegisterCallbacksHook *)pCallbacks)->pOriginalCallbacks, cb);
00054 }
00055 };
00056
00057 extern "C"
00058 {
00059 typedef int (*PVBoxDevicesRegisterOriginal)(PPDMDEVREGCB pCallbacks, uint32_t u32Version);
00060 static int VBoxDevicesRegisterOriginal(PPDMDEVREGCB pCallbacks, uint32_t u32Version)
00061 {
00062 PVBoxDevicesRegisterOriginal p = (PVBoxDevicesRegisterOriginal)GetProcAddress(GetModuleHandle(_T("VBoxDD0.dll")), "VBoxDevicesRegister");
00063 if (!p)
00064 return VERR_UNRESOLVED_ERROR;
00065 return p(pCallbacks, u32Version);
00066 }
00067
00068 int _VBoxDevicesRegister(PPDMDEVREGCB pCallbacks, uint32_t u32Version)
00069 {
00070 if (!pCallbacks)
00071 return VBoxDevicesRegisterOriginal(pCallbacks, u32Version);
00072
00073 DevRegisterCallbacksHook Callbacks(*pCallbacks);
00074 Callbacks.pOriginalCallbacks = pCallbacks;
00075 return VBoxDevicesRegisterOriginal(&Callbacks, u32Version);
00076 }
00077 }