DrvHostBase.cpp revision f3c7306224650485cfed5f020c266f860583a369
/* $Id$ */
/** @file
* DrvHostBase - Host base drive access driver.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* 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 (GPL) 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_HOST_BASE
#ifdef RT_OS_DARWIN
# include <IOKit/IOKitLib.h>
# include <DiskArbitration/DiskArbitration.h>
# include <mach/mach_error.h>
/* Nothing special requires... yeah, right. */
#elif defined(RT_OS_LINUX)
# include <errno.h>
#elif defined(RT_OS_SOLARIS)
# include <fcntl.h>
# include <errno.h>
# include <stropts.h>
# include <malloc.h>
extern "C" char *getfullblkname(char *);
#elif defined(RT_OS_WINDOWS)
# 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"
#elif defined(RT_OS_FREEBSD)
# include <errno.h>
# include <stdio.h>
#else
# error "Unsupported Platform."
#endif
#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",
/*
* Check the state.
*/
int rc;
#ifdef RT_OS_DARWIN
if ( pThis->fMediaPresent
&& pThis->ppScsiTaskDI
if ( pThis->fMediaPresent
#else
if (pThis->fMediaPresent)
#endif
{
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/*
* Issue a READ(12) request.
*/
do
{
{
SCSI_READ_12, 0,
0, 0, 0, 0, 0
};
#else
/*
* Seek and read.
*/
if (RT_SUCCESS(rc))
{
Log2(("%s-%d: drvHostBaseRead: off=%#llx cbRead=%#x\n"
"%16.*Rhxd\n",
}
else
Log(("%s-%d: drvHostBaseRead: RTFileReadAt(%RTfile, %#llx, %p, %#x) -> %Rrc ('%s')\n",
#endif
}
else
LogFlow(("%s-%d: drvHostBaseRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, 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",
Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
"%16.*Rhxd\n",
/*
* Check the state.
*/
int rc;
{
if (pThis->fMediaPresent)
{
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/** @todo write support... */
#else
/*
* Seek and write.
*/
if (RT_FAILURE(rc))
Log(("%s-%d: drvHostBaseWrite: RTFileWriteAt(%RTfile, %#llx, %p, %#x) -> %Rrc ('%s')\n",
#endif
}
else
}
else
LogFlow(("%s-%d: drvHostBaseWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIBLOCK::pfnFlush */
{
int rc;
LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
if (pThis->fMediaPresent)
{
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
rc = VINF_SUCCESS;
/** @todo scsi device buffer flush... */
#else
#endif
}
else
LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIBLOCK::pfnIsReadOnly */
{
}
/** @copydoc PDMIBLOCK::pfnGetSize */
{
if (pThis->fMediaPresent)
LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, cb));
return cb;
}
/** @copydoc PDMIBLOCK::pfnGetType */
{
LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->enmType));
}
/** @copydoc PDMIBLOCK::pfnGetUuid */
{
LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%RTuuid\n", pThis->pDrvIns->pReg->szName, 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::pfnGetPCHSGeometry */
static DECLCALLBACK(int) drvHostBaseGetPCHSGeometry(PPDMIBLOCKBIOS pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
{
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
{
}
else
}
else
LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc, pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors));
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetPCHSGeometry */
static DECLCALLBACK(int) drvHostBaseSetPCHSGeometry(PPDMIBLOCKBIOS pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
{
LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
}
else
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnGetLCHSGeometry */
static DECLCALLBACK(int) drvHostBaseGetLCHSGeometry(PPDMIBLOCKBIOS pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
{
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
{
}
else
}
else
LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc, pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors));
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetLCHSGeometry */
static DECLCALLBACK(int) drvHostBaseSetLCHSGeometry(PPDMIBLOCKBIOS pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
{
LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
}
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 (RT_SUCCESS(rc))
}
else
LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
LogFlow(("%s-%d: drvHostBaseLock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
int rc = VINF_SUCCESS;
{
if (RT_SUCCESS(rc))
}
else
LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
LogFlow(("%s-%d: drvHostBaseUnlock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
return fRc;
}
/* -=-=-=-=- IBase -=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/* -=-=-=-=- poller thread -=-=-=-=- */
#ifdef RT_OS_DARWIN
/** The runloop input source name for the disk arbitration events. */
# define MY_RUN_LOOP_MODE CFSTR("drvHostBaseDA") /** @todo r=bird: Check if this will cause trouble in the same way that the one in the USB code did. */
/**
*
* This is done by recursing down the I/O registry until we hit upon an entry
* with a BSD Name. Usually we find it two levels down. (Further down under
* the IOCDPartitionScheme, the volume (slices) BSD Name is found. We don't
* seem to have to go this far fortunately.)
*
* @return VINF_SUCCESS if found, VERR_FILE_NOT_FOUND otherwise.
* @param Entry The current I/O registry entry reference.
* @param pszName Where to store the name. 128 bytes.
* @param cRecursions Number of recursions. This is used as an precaution
* just to limit the depth and avoid blowing the stack
* should we hit a bug or something.
*/
{
int rc = VERR_FILE_NOT_FOUND;
io_iterator_t Children = 0;
if (krc == KERN_SUCCESS)
{
while ( rc == VERR_FILE_NOT_FOUND
{
CFStringRef BSDNameStrRef = (CFStringRef)IORegistryEntryCreateCFProperty(Child, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0);
if (BSDNameStrRef)
{
rc = VINF_SUCCESS;
else
AssertFailed();
}
}
}
return rc;
}
/**
* Callback notifying us that the async DADiskClaim()/DADiskUnmount call has completed.
*
* @param DiskRef The disk that was attempted claimed / unmounted.
* @param DissenterRef NULL on success, contains details on failure.
* @param pvContext Pointer to the return code variable.
*/
static void drvHostBaseDADoneCallback(DADiskRef DiskRef, DADissenterRef DissenterRef, void *pvContext)
{
if (!DissenterRef)
*prc = 0;
else
}
/**
* Obtain exclusive access to the DVD device, umount it if necessary.
*
* @return VBox status code.
* @param pThis The driver instance.
* @param DVDService The DVD service object.
*/
{
{
if (irc == kIOReturnSuccess)
{
/*
* This is a bit weird, but if we unmounted the DVD drive we also need to
* unlock it afterwards or the guest won't be able to eject it later on.
*/
{
{
SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL, 0, 0, 0, false, 0,
0,0,0,0,0,0,0,0,0,0
};
}
return VINF_SUCCESS;
}
if (irc == kIOReturnExclusiveAccess)
return VERR_SHARING_VIOLATION; /* already used exclusivly. */
if (irc != kIOReturnBusy)
return VERR_GENERAL_FAILURE; /* not mounted */
/*
* Attempt to the unmount all volumes of the device.
* It seems we can can do this all in one go without having to enumerate the
* volumes (sessions) and deal with them one by one. This is very fortuitous
* as the disk arbitration API is a bit cumbersome to deal with.
*/
if (iTry > 2)
return VERR_DRIVE_LOCKED;
char szName[128];
if (RT_SUCCESS(rc))
{
if (pThis->pDASession)
{
{
/*
* Try claim the device.
*/
int rcDA = -2;
DADiskClaim(pThis->pDADisk, kDADiskClaimOptionDefault, NULL, NULL, drvHostBaseDADoneCallback, &rcDA);
if ( rc32 == kCFRunLoopRunStopped
&& !rcDA)
{
/*
* Try unmount the device.
*/
Log(("%s-%d: calling DADiskUnmount on '%s'.\n", pDrvIns->pReg->szName, pDrvIns->iInstance, szName));
rcDA = -2;
if ( rc32 == kCFRunLoopRunStopped
&& !rcDA)
{
iTry = 99;
continue;
}
Log(("%s-%d: umount => rc32=%d & rcDA=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc32, rcDA));
/* failed - cleanup */
}
else
Log(("%s-%d: claim => rc32=%d & rcDA=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc32, rcDA));
}
else
}
else
}
RTThreadSleep(10);
}
}
#endif /* RT_OS_DARWIN */
#ifndef RT_OS_SOLARIS
/**
* Wrapper for open / RTFileOpen / IOKit.
*
* @remark The Darwin code must correspond exactly to the enumeration
*/
{
# ifdef RT_OS_DARWIN
/* Darwin is kind of special... */
/*
* Open the master port on the first invocation.
*/
/*
* Create a matching dictionary for searching for DVD services in the IOKit.
*
* [If I understand this correctly, plain CDROMs doesn't show up as
* IODVDServices. Too keep things simple, we will only support DVDs
* until somebody complains about it and we get hardware to test it on.
* (Unless I'm much mistaken, there aren't any (orignal) intel macs with
* plain cdroms.)]
*/
/*
* do the search and get a collection of keyboards.
*/
/*
* Enumerate the DVD drives (services).
* (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
*/
int rc = VERR_FILE_NOT_FOUND;
unsigned i = 0;
{
/*
* Get the properties we use to identify the DVD drive.
*
* While there is a (weird 12 byte) GUID, it isn't persistent
* across boots. So, we have to use a combination of the
* vendor name and product name properties with an optional
* sequence number for identification.
*/
if (krc == KERN_SUCCESS)
{
/* Get the Device Characteristics dictionary. */
CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
if (DevCharRef)
{
/* The vendor name. */
char szVendor[128];
if ( ValueRef
else
*pszVendor = '\0';
/* The product name. */
char szProduct[128];
char *pszProduct = &szProduct[0];
if ( ValueRef
else
*pszProduct = '\0';
/* Construct the two names and compare thwm with the one we're searching for. */
if (*pszVendor || *pszProduct)
{
if (*pszVendor && *pszProduct)
{
}
else
{
}
}
else
{
}
{
/*
* Found it! Now, get the client interface and stuff.
* Note that we could also query kIOSCSITaskDeviceUserClientTypeID here if the
* MMC client plugin is missing. For now we assume this won't be necessary.
*/
krc = IOCreatePlugInInterfaceForService(DVDService, kIOMMCDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
&ppPlugInInterface, &Score);
if (krc == KERN_SUCCESS)
{
{
if (pThis->ppScsiTaskDI)
rc = VINF_SUCCESS;
else
{
}
}
else
{
}
}
else /* Check for kIOSCSITaskDeviceUserClientTypeID? */
/* Obtain exclusive access to the device so we can send SCSI commands. */
if (RT_SUCCESS(rc))
/* Cleanup on failure. */
if (RT_FAILURE(rc))
{
if (pThis->ppScsiTaskDI)
{
}
{
}
}
break;
}
}
}
else
i++;
}
return rc;
#elif defined(RT_OS_FREEBSD)
int rc = RTFileOpen(&hFileDevice, pThis->pszDeviceOpen, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
if (RT_FAILURE(rc))
return rc;
/*
* The current device handle can't passthrough SCSI commands.
* We have to get he passthrough device path and open this.
*/
if (!rcBSD)
{
char *pszPassthroughDevice = NULL;
if (rc >= 0)
{
rc = RTFileOpen(&hPassthroughDevice, pszPassthroughDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
if (RT_SUCCESS(rc))
{
/* Get needed device parameters. */
/*
* The device path, target id and lun id. Those are
* needed for the SCSI passthrough ioctl.
*/
if (!rcBSD)
{
{
}
else
{
/* The passthrough device wasn't found. */
rc = VERR_NOT_FOUND;
}
}
else
if (RT_FAILURE(rc))
}
}
else
}
else
return rc;
#else
uint32_t fFlags = (fReadOnly ? RTFILE_O_READ : RTFILE_O_READWRITE) | RTFILE_O_OPEN | RTFILE_O_DENY_NONE;
# ifdef RT_OS_LINUX
# endif
#endif
}
#else /* RT_OS_SOLARIS */
/**
* Solaris wrapper for RTFileOpen.
*
* Solaris has to deal with two filehandles, a block and a raw one. Rather than messing
* with drvHostBaseOpen's function signature & body, having a separate one is better.
*
* @returns VBox status code.
*/
static int drvHostBaseOpen(PDRVHOSTBASE pThis, PRTFILE pFileBlockDevice, PRTFILE pFileRawDevice, bool fReadOnly)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return rc;
}
else
return rc;
}
#endif /* RT_OS_SOLARIS */
/**
* (Re)opens the device.
*
* This is used to open the device during construction, but it's also used to re-open
* the device when a media is inserted. This re-open will kill off any cached data
* that Linux for some peculiar reason thinks should survive a media change...
*
* @returns VBOX status code.
* @param pThis Instance data.
*/
{
#ifndef RT_OS_DARWIN /* Only *one* open for darwin. */
LogFlow(("%s-%d: drvHostBaseReopen: '%s'\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen));
#ifdef RT_OS_SOLARIS
{
}
{
}
#else
#endif
if (RT_FAILURE(rc))
{
if (!pThis->fReadOnlyConfig)
{
LogFlow(("%s-%d: drvHostBaseReopen: '%s' - retry readonly (%Rrc)\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen, rc));
#ifdef RT_OS_SOLARIS
#else
#endif
}
if (RT_FAILURE(rc))
{
LogFlow(("%s-%d: failed to open device '%s', rc=%Rrc\n",
return rc;
}
}
else
#ifdef RT_OS_SOLARIS
#endif
#endif /* !RT_OS_DARWIN */
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.
*/
{
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/*
* Try a READ_CAPACITY command...
*/
struct
{
} Buf = {0, 0};
{
SCSI_READ_CAPACITY, 0, 0, 0, 0, 0, 0,
0,0,0,0,0,0,0,0,0
};
if (RT_SUCCESS(rc))
{
// Buf.cbBlock = 2048;
}
return rc;
#elif defined(RT_OS_SOLARIS)
/*
* Sun docs suggests using DKIOCGGEOM instead of DKIOCGMEDIAINFO, but
* for secondary storage devices.
*/
{
return VINF_SUCCESS;
}
#elif defined(RT_OS_WINDOWS)
/* use NT api, retry a few times if the media is being verified. */
IO_STATUS_BLOCK IoStatusBlock = {0};
NTSTATUS rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(pThis->hFileDevice), &IoStatusBlock,
int cRetries = 5;
{
RTThreadSleep(10);
}
if (rcNt >= 0)
{
return VINF_SUCCESS;
}
/* convert nt status code to VBox status code. */
/** @todo Make conversion function!. */
int rc = VERR_GENERAL_FAILURE;
switch (rcNt)
{
}
return rc;
#else
#endif
}
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/**
* Execute a SCSI command.
*
* @param pThis The instance data.
* @param pbCmd Pointer to the SCSI command.
* @param cbCmd The size of the SCSI command.
* @param enmTxDir The transfer direction.
* @param pvBuf The buffer. Can be NULL if enmTxDir is PDMBLOCKTXDIR_NONE.
* @param pcbBuf Where to get the buffer size from and put the actual transfer size. Can be NULL.
* @param pbSense Where to put the sense data. Can be NULL.
* @param cbSense Size of the sense data buffer.
* @param cTimeoutMillies The timeout. 0 mean the default timeout.
*
* @returns VINF_SUCCESS on success (no sense code).
* @returns VERR_UNRESOLVED_ERROR if sense code is present.
* @returns Some other VBox status code on failures without sense code.
*
* @todo Fix VERR_UNRESOLVED_ERROR abuse.
*/
DECLCALLBACK(int) DRVHostBaseScsiCmd(PDRVHOSTBASE pThis, const uint8_t *pbCmd, size_t cbCmd, PDMBLOCKTXDIR enmTxDir,
{
/*
* Minimal input validation.
*/
Assert(enmTxDir == PDMBLOCKTXDIR_NONE || enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE || enmTxDir == PDMBLOCKTXDIR_TO_DEVICE);
if (pcbBuf)
*pcbBuf = 0;
# ifdef RT_OS_DARWIN
int rc = VERR_GENERAL_FAILURE;
if (!ppScsiTaskI)
return VERR_NO_MEMORY;
do
{
/* Setup the scsi command. */
SCSICommandDescriptorBlock cdb = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
/* Setup the buffer. */
if (enmTxDir == PDMBLOCKTXDIR_NONE)
irc = (*ppScsiTaskI)->SetScatterGatherEntries(ppScsiTaskI, NULL, 0, 0, kSCSIDataTransfer_NoDataTransfer);
else
{
}
/* Set the timeout. */
irc = (*ppScsiTaskI)->SetTimeoutDuration(ppScsiTaskI, cTimeoutMillies ? cTimeoutMillies : 30000 /*ms*/);
/* Execute the command and get the response. */
SCSI_Sense_Data SenseData = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
UInt64 cbReturned = 0;
if (pcbBuf)
if (TaskStatus == kSCSITaskStatus_GOOD)
rc = VINF_SUCCESS;
else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
&& pbSense)
{
}
/** @todo convert sense codes when caller doesn't wish to do this himself. */
/*else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
&& SenseData.ADDITIONAL_SENSE_CODE == 0x3A)
rc = VERR_MEDIA_NOT_PRESENT; */
else
{
LogRel(("DVD scsi error: cmd={%.*Rhxs} TaskStatus=%#x key=%#x ASC=%#x ASCQ=%#x (%Rrc)\n",
}
} while (0);
# elif defined(RT_OS_FREEBSD)
int rc = VINF_SUCCESS;
int rcBSD = 0;
/* The SCSI INQUIRY command can't be passed through directly. */
if (pbCmd[0] == SCSI_INQUIRY)
{
if (!rcBSD)
{
? cbBuf
: sizeof(struct scsi_inquiry_data);;
if (pcbBuf)
}
else
}
else
{
/* Copy the CDB. */
/* Set direction. */
if (enmTxDir == PDMBLOCKTXDIR_NONE)
else if (enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE)
fFlags = CAM_DIR_IN;
else
/* Send command */
if (!rcBSD)
{
{
case CAM_REQ_CMP:
rc = VINF_SUCCESS;
break;
case CAM_SEL_TIMEOUT:
break;
case CAM_CMD_TIMEOUT:
rc = VERR_TIMEOUT;
break;
default:
}
if (pcbBuf)
if (pbSense)
}
else
}
# endif
return rc;
}
#endif
/**
* Media present.
* Query the size and notify the above driver / device.
*
* @param pThis The instance data.
*/
{
/*
* Open the drive.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Determine the size.
*/
if (RT_FAILURE(rc))
{
LogFlow(("%s-%d: failed to figure media size of %s, rc=%Rrc\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;
if (pThis->pDrvMountNotify)
}
#ifdef RT_OS_WINDOWS
/**
* 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 /* RT_OS_WINDOWS */
/**
* 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 RT_OS_WINDOWS
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->pReg->szName, pThis->pDrvIns->iInstance));
return VERR_GENERAL_FAILURE;
}
LogFlow(("%s-%d: drvHostBaseMediaThread: Created hwndDeviceChange=%p\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, hwnd));
/*
* Message pump.
*/
{
if (fRet != -1)
{
}
//else: handle the error and possibly exit
}
#else /* !RT_OS_WINDOWS */
bool fFirst = true;
int cRetries = 10;
while (!pThis->fShutdownPoller)
{
/*
* Perform the polling (unless we've run out of 50ms retries).
*/
&& cRetries-- > 0)
{
if (RT_FAILURE(rc))
{
continue;
}
}
/*
* Signal EMT after the first go.
*/
if (fFirst)
{
fFirst = false;
}
/*
* Sleep.
*/
if ( RT_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
return rc;
}
cRetries = 10;
}
#endif /* !RT_OS_WINDOWS */
/* (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->pReg->szName, 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->pReg->szName, pThis->pDrvIns->iInstance));
/*
*/
if (pThis->pDrvMountNotify)
{
if (pThis->fMediaPresent)
}
return VINF_SUCCESS;
}
/** @copydoc FNPDMDRVDESTRUCT */
{
LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Terminate the thread.
*/
{
pThis->fShutdownPoller = true;
int rc;
int cTimes = 50;
do
{
#ifdef RT_OS_WINDOWS
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 or we're in passthru mode.
*/
#ifdef RT_OS_DARWIN
&& pThis->ppScsiTaskDI
#else /** @todo Check if the other guys can mix pfnDoLock with scsi passthru.
* (We're currently not unlocking the device after use. See todo in DevATA.cpp.) */
#endif
{
if (RT_SUCCESS(rc))
}
/*
* Cleanup the other resources.
*/
#ifdef RT_OS_WINDOWS
if (pThis->hwndDeviceChange)
{
PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
}
#else
{
}
#endif
#ifdef RT_OS_DARWIN
/*
* The unclaiming doesn't seem to mean much, the DVD is actually
* remounted when we release exclusive access. I'm not quite sure
* if I should put the unclaim first or not...
*
* Anyway, that it's automatically remounted very good news for us,
* because that means we don't have to mess with that ourselves. Of
* course there is the unlikely scenario that we've succeeded in claiming
* and umount the DVD but somehow failed to gain exclusive scsi access...
*/
if (pThis->ppScsiTaskDI)
{
}
{
}
{
}
if (pThis->MasterPort)
{
}
if (pThis->pDASession)
{
}
#else
{
}
#endif
#ifdef RT_OS_SOLARIS
{
}
if (pThis->pszRawDeviceOpen)
{
}
#endif
{
}
if (pThis->pszDeviceOpen)
{
}
/* Forget about the notifications. */
/* Leave the instance operational if this is just a cleanup of the state
* after an attach error happened. So don't destroy the critsect then. */
}
/**
* 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 pCfg Configuration handle.
* @param enmType Device type.
*/
{
LogFlow(("%s-%d: DRVHostBaseInitData: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Initialize most of the data members.
*/
pThis->fKeepInstance = false;
#ifdef RT_OS_DARWIN
#else
#endif
#ifdef RT_OS_SOLARIS
#endif
//pThis->cErrors = 0;
/* IBase. */
/* IBlock. */
/* IBlockBios. */
/* IMount. */
/*
*/
if (!pThis->pDrvBlockPort)
{
AssertMsgFailed(("Configuration error: No block port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Query configuration.
*/
/* Device */
if (RT_FAILURE(rc))
{
return rc;
}
/* Mountable */
if (RT_SUCCESS(rc))
else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (RT_FAILURE(rc))
{
return rc;
}
/* ReadOnly */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pThis->fReadOnlyConfig = enmType == PDMBLOCKTYPE_DVD || enmType == PDMBLOCKTYPE_CDROM ? true : false;
else if (RT_FAILURE(rc))
{
return rc;
}
/* Locked */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (RT_FAILURE(rc))
{
return rc;
}
/* BIOS visible */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pThis->fBiosVisible = true;
else if (RT_FAILURE(rc))
{
return rc;
}
/* Uuid */
char *psz;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
{
return rc;
}
}
else
{
return rc;
}
/* Define whether attach failure is an error (default) or not. */
bool fAttachFailError;
if (RT_FAILURE(rc))
fAttachFailError = true;
/* name to open & watch for */
#ifdef RT_OS_WINDOWS
{
return VERR_INVALID_PARAMETER;
}
#elif defined(RT_OS_SOLARIS)
if (!pszBlockDevName)
return VERR_NO_MEMORY;
#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.
*/
{
int src = VINF_SUCCESS;
/* log config summary */
Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%RTuuid\n",
pDrvIns->pReg->szName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
/*
* Check that there are no drivers below us.
*/
("Configuration error: Not possible to attach anything to this driver!\n"),
/*
* Register saved state.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Verify type.
*/
#ifdef RT_OS_WINDOWS
{
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 defined(RT_OS_DARWIN)
#else
#endif
if (RT_FAILURE(rc))
{
#ifndef RT_OS_DARWIN
char szPathReal[256];
if ( RTPathExists(pszDevice)
#endif
#ifdef RT_OS_SOLARIS
#endif
/*
* weird failures later when the guest issues commands. These would
* all fail because of the invalid file handle. So use the normal
* "media" - actually a complete drive in this case.
*/
switch (rc)
{
case VERR_ACCESS_DENIED:
#ifdef RT_OS_LINUX
N_("Cannot open host device '%s' for %s access. Check the permissions "
"the group settings of the current user"),
#else
N_("Cannot open host device '%s' for %s access. Check the permissions "
"of that device"),
#endif
default:
{
if (pThis->fAttachFailError)
return rc;
"DrvHost_MOUNTFAIL",
}
}
}
#ifdef RT_OS_WINDOWS
if (RT_SUCCESS(src))
#endif
/*
* Lock the drive if that's required by the configuration.
*/
{
if (RT_FAILURE(rc))
{
return rc;
}
}
#ifndef RT_OS_WINDOWS
if (RT_SUCCESS(src))
{
/*
* Create the event semaphore which the poller thread will wait on.
*/
if (RT_FAILURE(rc))
return rc;
}
#endif
/*
* Initialize the critical section used for serializing the access to the media.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_SUCCESS(src))
{
/*
* Start the thread which will poll for the media.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Wait for the thread to start up (!w32:) and do one detection loop.
*/
#ifdef RT_OS_WINDOWS
if (!pThis->hwndDeviceChange)
return VERR_GENERAL_FAILURE;
#endif
}
if (RT_FAILURE(src))
return src;
return rc;
}