DrvHostBase.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/** @file
*
* VBox storage devices:
* Host base drive access driver
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_HOST_BASE
#ifdef __LINUX__
# include <errno.h>
# define WIN32_NO_STATUS
# include <Windows.h>
# include <dbt.h>
# include <ntstatus.h>
/* from ntdef.h */
/* from ntddk.h */
typedef struct _IO_STATUS_BLOCK {
union {
};
/* from ntinternals.com */
typedef enum _FS_INFORMATION_CLASS {
typedef struct _FILE_FS_SIZE_INFORMATION {
extern "C"
#else /* !__WIN__ nor __LINUX__ nor __L4ENV__ */
# error "Unsupported Platform."
#endif /* !__WIN__ nor __LINUX__ nor __L4ENV__ */
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include "DrvHostBase.h"
/* -=-=-=-=- IBlock -=-=-=-=- */
/** @copydoc PDMIBLOCK::pfnRead */
static DECLCALLBACK(int) drvHostBaseRead(PPDMIBLOCK pInterface, uint64_t off, void *pvBuf, size_t cbRead)
{
LogFlow(("%s-%d: drvHostBaseRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n",
pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, pvBuf, cbRead, pThis->pszDevice));
/*
* Check the state.
*/
int rc;
if (pThis->fMediaPresent)
{
/*
* Seek and read.
*/
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc))
{
Log2(("%s-%d: drvHostBaseRead: off=%#llx cbRead=%#x\n"
"%16.*Vhxd\n",
}
else
Log(("%s-%d: drvHostBaseRead: RTFileRead(%d, %p, %#x) -> %Vrc (off=%#llx '%s')\n",
}
else
Log(("%s-%d: drvHostBaseRead: RTFileSeek(%d,%#llx,) -> %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName,
}
else
LogFlow(("%s-%d: drvHostBaseRead: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIBLOCK::pfnWrite */
static DECLCALLBACK(int) drvHostBaseWrite(PPDMIBLOCK pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
{
LogFlow(("%s-%d: drvHostBaseWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n",
pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, pvBuf, cbWrite, pThis->pszDevice));
Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
"%16.*Vhxd\n",
/*
* Check the state.
*/
int rc;
{
if (pThis->fMediaPresent)
{
/*
* Seek and write.
*/
if (VBOX_SUCCESS(rc))
{
if (VBOX_FAILURE(rc))
Log(("%s-%d: drvHostBaseWrite: RTFileWrite(%d, %p, %#x) -> %Vrc (off=%#llx '%s')\n",
}
else
Log(("%s-%d: drvHostBaseWrite: RTFileSeek(%d,%#llx,) -> %Vrc\n",
}
else
}
else
LogFlow(("%s-%d: drvHostBaseWrite: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIBLOCK::pfnFlush */
{
int rc;
LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
if (pThis->fMediaPresent)
{
}
else
LogFlow(("%s-%d: drvHostBaseFlush: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIBLOCK::pfnIsReadOnly */
{
}
/** @copydoc PDMIBLOCK::pfnGetSize */
{
if (pThis->fMediaPresent)
LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, cb));
return cb;
}
/** @copydoc PDMIBLOCK::pfnGetType */
{
LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->enmType));
}
/** @copydoc PDMIBLOCK::pfnGetUuid */
{
LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%Vuuid\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pUuid));
return VINF_SUCCESS;
}
/* -=-=-=-=- IBlockBios -=-=-=-=- */
/** Makes a PDRVHOSTBASE out of a PPDMIBLOCKBIOS. */
#define PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface) ( (PDRVHOSTBASE((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTBASE, IBlockBios))) )
/** @copydoc PDMIBLOCKBIOS::pfnGetGeometry */
static DECLCALLBACK(int) drvHostBaseGetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t *pcCylinders, uint32_t *pcHeads, uint32_t *pcSectors)
{
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
if ( pThis->cCylinders > 0
{
}
else
}
else
LogFlow(("%s-%d: drvHostBaseGetGeometry: returns %Vrc CHS={%d,%d,%d}\n",
pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc, *pcCylinders, *pcHeads, *pcSectors));
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetGeometry */
static DECLCALLBACK(int) drvHostBaseSetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors)
{
LogFlow(("%s-%d: drvHostBaseSetGeometry: cCylinders=%d cHeads=%d cSectors=%d\n",
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
}
else
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnGetTranslation */
static DECLCALLBACK(int) drvHostBaseGetTranslation(PPDMIBLOCKBIOS pInterface, PPDMBIOSTRANSLATION penmTranslation)
{
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
if (pThis->fTranslationSet)
else
}
else
LogFlow(("%s-%d: drvHostBaseGetTranslation: returns %Vrc *penmTranslation=%d\n",
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetTranslation */
static DECLCALLBACK(int) drvHostBaseSetTranslation(PPDMIBLOCKBIOS pInterface, PDMBIOSTRANSLATION enmTranslation)
{
LogFlow(("%s-%d: drvHostBaseSetTranslation: enmTranslation=%d\n",
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
pThis->fTranslationSet = true;
}
else
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnIsVisible */
{
return pThis->fBiosVisible;
}
/** @copydoc PDMIBLOCKBIOS::pfnGetType */
{
}
/* -=-=-=-=- IMount -=-=-=-=- */
/** @copydoc PDMIMOUNT::pfnMount */
static DECLCALLBACK(int) drvHostBaseMount(PPDMIMOUNT pInterface, const char *pszFilename, const char *pszCoreDriver)
{
/* We're not mountable. */
AssertMsgFailed(("drvHostBaseMount: This shouldn't be called!\n"));
return VERR_PDM_MEDIA_MOUNTED;
}
/** @copydoc PDMIMOUNT::pfnUnmount */
{
LogFlow(("drvHostBaseUnmount: returns VERR_NOT_SUPPORTED\n"));
return VERR_NOT_SUPPORTED;
}
/** @copydoc PDMIMOUNT::pfnIsMounted */
{
return fRc;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
int rc = VINF_SUCCESS;
{
if (VBOX_SUCCESS(rc))
}
else
LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
LogFlow(("%s-%d: drvHostBaseLock: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
int rc = VINF_SUCCESS;
{
if (VBOX_SUCCESS(rc))
}
else
LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
LogFlow(("%s-%d: drvHostBaseUnlock: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
return fRc;
}
/* -=-=-=-=- IBase -=-=-=-=- */
/** @copydoc PDMIBASE::pfnQueryInterface. */
static DECLCALLBACK(void *) drvHostBaseQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_BLOCK:
case PDMINTERFACE_BLOCK_BIOS:
case PDMINTERFACE_MOUNT:
default:
return NULL;
}
}
/* -=-=-=-=- poller thread -=-=-=-=- */
/**
* Wrapper for open / RTFileOpen.
*/
{
#ifdef __LINUX__
int FileDevice = open(pThis->pszDeviceOpen, (pThis->fReadOnlyConfig ? O_RDONLY : O_RDWR) | O_NONBLOCK);
if (FileDevice < 0)
return RTErrConvertFromErrno(errno);
return VINF_SUCCESS;
#else
#endif
}
/**
* (Re)opens the device.
*
* @returns VBOX status code.
* @param pThis Instance data.
*/
{
LogFlow(("%s-%d: drvHostBaseReopen: '%s'\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen));
/*
* Reopen the device to kill any cached data which for some peculiar reason stays on some OSes (linux)...
*/
if (VBOX_FAILURE(rc))
{
if (!pThis->fReadOnlyConfig)
{
LogFlow(("%s-%d: drvHostBaseReopen: '%s' - retry readonly (%Vrc)\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen, rc));
}
if (VBOX_FAILURE(rc))
{
LogFlow(("%s-%d: failed to open device '%s', rc=%Vrc\n",
return rc;
}
}
else
return VINF_SUCCESS;
}
/**
* Queries the media size.
*
* @returns VBox status code.
* @param pThis Pointer to the instance data.
* @param pcb Where to store the media size in bytes.
*/
{
#ifdef __WIN__
/* use NT api, retry a few times if the media is being verified. */
IO_STATUS_BLOCK IoStatusBlock = {0};
int cRetries = 5;
{
RTThreadSleep(10);
}
if (rcNt >= 0)
{
return VINF_SUCCESS;
}
/* convert nt status code to VBox status code. */
/** @todo Make convertion function!. */
int rc = VERR_GENERAL_FAILURE;
switch (rcNt)
{
}
return rc;
#else
#endif
}
/**
* Media present.
* Query the size and notify the above driver / device.
*
* @param pThis The instance data.
*/
{
/*
* Open the drive.
*/
if (VBOX_FAILURE(rc))
return rc;
/*
* Determin the size.
*/
if (VBOX_FAILURE(rc))
{
LogFlow(("%s-%d: failed to figure media size of %s, rc=%Vrc\n",
return rc;
}
/*
* Update the data and inform the unit.
*/
pThis->fMediaPresent = true;
if (pThis->pDrvMountNotify)
LogFlow(("%s-%d: drvHostBaseMediaPresent: cbSize=%lld (%#llx)\n",
return VINF_SUCCESS;
}
/**
* Media no longer present.
* @param pThis The instance data.
*/
{
pThis->fMediaPresent = false;
pThis->fTranslationSet = false;
if (pThis->pDrvMountNotify)
}
#ifdef __WIN__
/**
* Window procedure for the invisible window used to catch the WM_DEVICECHANGE broadcasts.
*/
{
if (uMsg == WM_DESTROY)
{
if (pThis)
PostQuitMessage(0);
}
if (uMsg != WM_DEVICECHANGE)
return 0;
switch (wParam)
{
case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE:
// Check whether a CD or DVD was inserted into or removed from a drive.
{
{
if (wParam == DBT_DEVICEARRIVAL)
{
int cRetries = 10;
{
RTThreadSleep(50);
}
}
else
}
}
break;
}
return TRUE;
}
#endif /* __WIN__ */
/**
* This thread will periodically poll the device for media presence.
*
* @returns Ignored.
* @param ThreadSelf Handle of this thread. Ignored.
* @param pvUser Pointer to the driver instance structure.
*/
{
LogFlow(("%s-%d: drvHostBaseMediaThread: ThreadSelf=%p pvUser=%p\n",
#ifdef __WIN__
static WNDCLASS s_classDeviceChange = {0};
static ATOM s_hAtomDeviceChange = 0;
/*
* Register custom window class.
*/
if (s_hAtomDeviceChange == 0)
{
}
/*
* Create Window w/ the pThis as user data.
*/
HWND hwnd = CreateWindow((LPCTSTR)s_hAtomDeviceChange, "", WS_POPUP, 0, 0, 0, 0, 0, 0, s_classDeviceChange.hInstance, 0);
/*
* Signal the waiting EMT thread that everything went fine.
*/
if (!hwnd)
{
LogFlow(("%s-%d: drvHostBaseMediaThread: returns VERR_GENERAL_FAILURE\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
return VERR_GENERAL_FAILURE;
}
LogFlow(("%s-%d: drvHostBaseMediaThread: Created hwndDeviceChange=%p\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, hwnd));
/*
* Message pump.
*/
{
if (fRet != -1)
{
}
//else: handle the error and possibly exit
}
#else /* !__WIN__ */
bool fFirst = true;
int cRetries = 10;
while (!pThis->fShutdownPoller)
{
/*
* Perform the polling (unless we've run out of 50ms retries).
*/
&& cRetries-- > 0)
{
if (VBOX_FAILURE(rc))
{
continue;
}
}
/*
* Signal EMT after the first go.
*/
if (fFirst)
{
fFirst = false;
}
/*
* Sleep.
*/
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
return rc;
}
cRetries = 10;
}
#endif /* !__WIN__ */
/* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
LogFlow(("%s-%d: drvHostBaseMediaThread: returns VINF_SUCCESS\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
return VINF_SUCCESS;
}
/* -=-=-=-=- driver interface -=-=-=-=- */
/**
* Done state load operation.
*
* @returns VBox load code.
* @param pDrvIns Driver instance of the driver which registered the data unit.
* @param pSSM SSM operation handle.
*/
{
LogFlow(("%s-%d: drvHostBaseMediaThread:\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
/*
*/
if (pThis->pDrvMountNotify)
{
if (pThis->fMediaPresent)
}
return VINF_SUCCESS;
}
/** @copydoc FNPDMDRVDESTRUCT */
{
LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Terminate the thread.
*/
{
pThis->fShutdownPoller = true;
int rc;
int cTimes = 50;
do
{
#ifdef __WIN__
if (pThis->hwndDeviceChange)
PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
#else
#endif
if (!rc)
}
/*
* Unlock the drive if we've locked it.
*/
{
if (VBOX_SUCCESS(rc))
}
/*
* Cleanup the other resources.
*/
#ifdef __WIN__
if (pThis->hwndDeviceChange)
{
PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
}
#else
{
}
#endif
{
}
{
}
if (pThis->pszDeviceOpen)
{
}
}
/**
* Initializes the instance data (init part 1).
*
* The driver which derives from this base driver will override function pointers after
* calling this method, and complete the construction by calling DRVHostBaseInitFinish().
*
* On failure call DRVHostBaseDestruct().
*
* @returns VBox status code.
* @param pDrvIns Driver instance.
* @param pCfgHandle Configuration handle.
* @param enmType Device type.
*/
{
LogFlow(("%s-%d: DRVHostBaseInitData: iInstance=%d\n", pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Initialize most of the data members.
*/
/* IBase. */
/* IBlock. */
/* IBlockBios. */
/* IMount. */
/*
*/
pThis->pDrvBlockPort = (PPDMIBLOCKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_BLOCK_PORT);
if (!pThis->pDrvBlockPort)
{
AssertMsgFailed(("Configuration error: No block port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
pThis->pDrvMountNotify = (PPDMIMOUNTNOTIFY)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUNT_NOTIFY);
/*
* Query configuration.
*/
/* Device */
if (VBOX_FAILURE(rc))
{
return rc;
}
/* Mountable */
if (VBOX_SUCCESS(rc))
else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* ReadOnly */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pThis->fReadOnlyConfig = enmType == PDMBLOCKTYPE_DVD || enmType == PDMBLOCKTYPE_CDROM ? true : false;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Locked */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* BIOS visible */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pThis->fBiosVisible = true;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Uuid */
char *psz;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_SUCCESS(rc))
{
if (VBOX_FAILURE(rc))
{
return rc;
}
}
else
{
return rc;
}
/* name to open & watch for */
#ifdef __WIN__
{
return VERR_INVALID_PARAMETER;
}
#else
#endif
if (!pThis->pszDeviceOpen)
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
/**
* Do the 2nd part of the init after the derived driver has overridden the defaults.
*
* On failure call DRVHostBaseDestruct().
*
* @returns VBox status code.
* @param pThis Pointer to the instance data.
*/
{
/* log config summary */
Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%Vuuid\n",
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
/*
* Check that there are no drivers below us.
*/
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
return VERR_PDM_DRVINS_NO_ATTACH;
}
/*
* Register saved state.
*/
rc = pDrvIns->pDrvHlp->pfnSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, 1, 0,
if (VBOX_FAILURE(rc))
return rc;
/*
* Verify type.
*/
#ifdef __WIN__
{
case PDMBLOCKTYPE_FLOPPY_360:
case PDMBLOCKTYPE_FLOPPY_720:
case PDMBLOCKTYPE_FLOPPY_1_20:
case PDMBLOCKTYPE_FLOPPY_1_44:
case PDMBLOCKTYPE_FLOPPY_2_88:
if (uDriveType != DRIVE_REMOVABLE)
{
AssertMsgFailed(("Configuration error: '%s' is not a floppy (type=%d)\n",
return VERR_INVALID_PARAMETER;
}
break;
case PDMBLOCKTYPE_CDROM:
case PDMBLOCKTYPE_DVD:
if (uDriveType != DRIVE_CDROM)
{
AssertMsgFailed(("Configuration error: '%s' is not a cdrom (type=%d)\n",
return VERR_INVALID_PARAMETER;
}
break;
case PDMBLOCKTYPE_HARD_DISK:
default:
return VERR_INVALID_PARAMETER;
}
#endif
/*
* Open the device.
*/
if (VBOX_FAILURE(rc))
{
return rc;
}
#ifdef __WIN__
#endif
/*
* Lock the drive if that's required by the configuration.
*/
{
if (VBOX_FAILURE(rc))
{
return rc;
}
}
#ifndef __WIN__
/*
* Create the event semaphore which the poller thread will wait on.
*/
if (VBOX_FAILURE(rc))
return rc;
#endif
/*
* Initialize the critical section used for serializing the access to the media.
*/
if (VBOX_FAILURE(rc))
return rc;
/*
* Start the thread which will poll for the media.
*/
if (VBOX_FAILURE(rc))
{
return rc;
}
/*
* Wait for the thread to start up (!w32:) and do one detection loop.
*/
#ifdef __WIN__
if (!pThis->hwndDeviceChange)
return VERR_GENERAL_FAILURE;
#endif
return rc;
}