VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvHostBase.cpp@ 1965

Last change on this file since 1965 was 1965, checked in by vboxsync, 18 years ago

HostDVD support for Darwin.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.3 KB
Line 
1/** @file
2 *
3 * VBox storage devices:
4 * Host base drive access driver
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#define LOG_GROUP LOG_GROUP_DRV_HOST_BASE
28#ifdef __DARWIN__
29# include <mach/mach.h>
30# include <Carbon/Carbon.h>
31# include <IOKit/IOKitLib.h>
32# include <IOKit/storage/IOStorageDeviceCharacteristics.h>
33# include <IOKit/scsi-commands/SCSITaskLib.h>
34# include <IOKit/scsi-commands/SCSICommandOperationCodes.h>
35# include <mach/mach_error.h>
36# include <VBox/scsi.h>
37
38#elif defined(__L4ENV__)
39 /* Nothing special requires... yeah, right. */
40
41#elif defined(__LINUX__)
42# include <sys/ioctl.h>
43# include <sys/fcntl.h>
44# include <errno.h>
45
46#elif defined(__WIN__)
47# define WIN32_NO_STATUS
48# include <Windows.h>
49# include <dbt.h>
50# undef WIN32_NO_STATUS
51# include <ntstatus.h>
52
53/* from ntdef.h */
54typedef LONG NTSTATUS;
55
56/* from ntddk.h */
57typedef struct _IO_STATUS_BLOCK {
58 union {
59 NTSTATUS Status;
60 PVOID Pointer;
61 };
62 ULONG_PTR Information;
63} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
64
65
66/* from ntinternals.com */
67typedef enum _FS_INFORMATION_CLASS {
68 FileFsVolumeInformation=1,
69 FileFsLabelInformation,
70 FileFsSizeInformation,
71 FileFsDeviceInformation,
72 FileFsAttributeInformation,
73 FileFsControlInformation,
74 FileFsFullSizeInformation,
75 FileFsObjectIdInformation,
76 FileFsMaximumInformation
77} FS_INFORMATION_CLASS, *PFS_INFORMATION_CLASS;
78
79typedef struct _FILE_FS_SIZE_INFORMATION {
80 LARGE_INTEGER TotalAllocationUnits;
81 LARGE_INTEGER AvailableAllocationUnits;
82 ULONG SectorsPerAllocationUnit;
83 ULONG BytesPerSector;
84} FILE_FS_SIZE_INFORMATION, *PFILE_FS_SIZE_INFORMATION;
85
86extern "C"
87NTSTATUS __stdcall NtQueryVolumeInformationFile(
88 /*IN*/ HANDLE FileHandle,
89 /*OUT*/ PIO_STATUS_BLOCK IoStatusBlock,
90 /*OUT*/ PVOID FileSystemInformation,
91 /*IN*/ ULONG Length,
92 /*IN*/ FS_INFORMATION_CLASS FileSystemInformationClass );
93
94#else
95# error "Unsupported Platform."
96#endif
97
98#include <VBox/pdm.h>
99#include <VBox/cfgm.h>
100#include <VBox/mm.h>
101#include <VBox/err.h>
102
103#include <VBox/log.h>
104#include <iprt/assert.h>
105#include <iprt/file.h>
106#include <iprt/path.h>
107#include <iprt/string.h>
108#include <iprt/thread.h>
109#include <iprt/semaphore.h>
110#include <iprt/uuid.h>
111#include <iprt/asm.h>
112#include <iprt/critsect.h>
113#include <iprt/ctype.h>
114
115#include "DrvHostBase.h"
116
117
118
119
120/* -=-=-=-=- IBlock -=-=-=-=- */
121
122/** @copydoc PDMIBLOCK::pfnRead */
123static DECLCALLBACK(int) drvHostBaseRead(PPDMIBLOCK pInterface, uint64_t off, void *pvBuf, size_t cbRead)
124{
125 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
126 LogFlow(("%s-%d: drvHostBaseRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n",
127 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, pvBuf, cbRead, pThis->pszDevice));
128 RTCritSectEnter(&pThis->CritSect);
129
130 /*
131 * Check the state.
132 */
133 int rc;
134#ifdef __DARWIN__
135 if ( pThis->fMediaPresent
136 && pThis->ppScsiTaskDI
137 && pThis->cbBlock)
138#else
139 if (pThis->fMediaPresent)
140#endif
141 {
142#ifdef __DARWIN__
143 /*
144 * Issue a READ(12) request.
145 */
146 const uint32_t LBA = off / pThis->cbBlock;
147 AssertReturn(!(off % pThis->cbBlock), VERR_INVALID_PARAMETER);
148 const uint32_t cBlocks = cbRead / pThis->cbBlock;
149 AssertReturn(!(cbRead % pThis->cbBlock), VERR_INVALID_PARAMETER);
150 uint8_t abCmd[16] =
151 {
152 SCSI_READ_12, 0,
153 RT_BYTE4(LBA), RT_BYTE3(LBA), RT_BYTE2(LBA), RT_BYTE1(LBA),
154 RT_BYTE4(cBlocks), RT_BYTE3(cBlocks), RT_BYTE2(cBlocks), RT_BYTE1(cBlocks),
155 0, 0, 0, 0, 0
156 };
157 rc = DRVHostBaseScsiCmd(pThis, abCmd, 12, PDMBLOCKTXDIR_FROM_DEVICE, pvBuf, &cbRead, NULL, 0, 0);
158
159#else
160 /*
161 * Seek and read.
162 */
163 rc = RTFileSeek(pThis->FileDevice, off, RTFILE_SEEK_BEGIN, NULL);
164 if (VBOX_SUCCESS(rc))
165 {
166 rc = RTFileRead(pThis->FileDevice, pvBuf, cbRead, NULL);
167 if (VBOX_SUCCESS(rc))
168 {
169 Log2(("%s-%d: drvHostBaseRead: off=%#llx cbRead=%#x\n"
170 "%16.*Vhxd\n",
171 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
172 }
173 else
174 Log(("%s-%d: drvHostBaseRead: RTFileRead(%d, %p, %#x) -> %Vrc (off=%#llx '%s')\n",
175 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->FileDevice,
176 pvBuf, cbRead, rc, off, pThis->pszDevice));
177 }
178 else
179 Log(("%s-%d: drvHostBaseRead: RTFileSeek(%d,%#llx,) -> %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName,
180 pThis->pDrvIns->iInstance, pThis->FileDevice, off, rc));
181#endif
182 }
183 else
184 rc = VERR_MEDIA_NOT_PRESENT;
185
186 RTCritSectLeave(&pThis->CritSect);
187 LogFlow(("%s-%d: drvHostBaseRead: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
188 return rc;
189}
190
191
192/** @copydoc PDMIBLOCK::pfnWrite */
193static DECLCALLBACK(int) drvHostBaseWrite(PPDMIBLOCK pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
194{
195 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
196 LogFlow(("%s-%d: drvHostBaseWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n",
197 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, pvBuf, cbWrite, pThis->pszDevice));
198 Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
199 "%16.*Vhxd\n",
200 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
201 RTCritSectEnter(&pThis->CritSect);
202
203 /*
204 * Check the state.
205 */
206 int rc;
207 if (!pThis->fReadOnly)
208 {
209 if (pThis->fMediaPresent)
210 {
211#ifdef __DARWIN__
212 /** @todo write support... */
213 rc = VERR_WRITE_PROTECT;
214
215#else
216 /*
217 * Seek and write.
218 */
219 rc = RTFileSeek(pThis->FileDevice, off, RTFILE_SEEK_BEGIN, NULL);
220 if (VBOX_SUCCESS(rc))
221 {
222 rc = RTFileWrite(pThis->FileDevice, pvBuf, cbWrite, NULL);
223 if (VBOX_FAILURE(rc))
224 Log(("%s-%d: drvHostBaseWrite: RTFileWrite(%d, %p, %#x) -> %Vrc (off=%#llx '%s')\n",
225 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->FileDevice,
226 pvBuf, cbWrite, rc, off, pThis->pszDevice));
227 }
228 else
229 Log(("%s-%d: drvHostBaseWrite: RTFileSeek(%d,%#llx,) -> %Vrc\n",
230 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->FileDevice, off, rc));
231#endif
232 }
233 else
234 rc = VERR_MEDIA_NOT_PRESENT;
235 }
236 else
237 rc = VERR_WRITE_PROTECT;
238
239 RTCritSectLeave(&pThis->CritSect);
240 LogFlow(("%s-%d: drvHostBaseWrite: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
241 return rc;
242}
243
244
245/** @copydoc PDMIBLOCK::pfnFlush */
246static DECLCALLBACK(int) drvHostBaseFlush(PPDMIBLOCK pInterface)
247{
248 int rc;
249 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
250 LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
251 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDevice));
252 RTCritSectEnter(&pThis->CritSect);
253
254 if (pThis->fMediaPresent)
255 {
256#ifdef __DARWIN__
257 rc = VINF_SUCCESS;
258 /** @todo scsi device buffer flush... */
259#else
260 rc = RTFileFlush(pThis->FileDevice);
261#endif
262 }
263 else
264 rc = VERR_MEDIA_NOT_PRESENT;
265
266 RTCritSectLeave(&pThis->CritSect);
267 LogFlow(("%s-%d: drvHostBaseFlush: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
268 return rc;
269}
270
271
272/** @copydoc PDMIBLOCK::pfnIsReadOnly */
273static DECLCALLBACK(bool) drvHostBaseIsReadOnly(PPDMIBLOCK pInterface)
274{
275 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
276 return pThis->fReadOnly;
277}
278
279
280/** @copydoc PDMIBLOCK::pfnGetSize */
281static DECLCALLBACK(uint64_t) drvHostBaseGetSize(PPDMIBLOCK pInterface)
282{
283 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
284 RTCritSectEnter(&pThis->CritSect);
285
286 uint64_t cb = 0;
287 if (pThis->fMediaPresent)
288 cb = pThis->cbSize;
289
290 RTCritSectLeave(&pThis->CritSect);
291 LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, cb));
292 return cb;
293}
294
295
296/** @copydoc PDMIBLOCK::pfnGetType */
297static DECLCALLBACK(PDMBLOCKTYPE) drvHostBaseGetType(PPDMIBLOCK pInterface)
298{
299 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
300 LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->enmType));
301 return pThis->enmType;
302}
303
304
305/** @copydoc PDMIBLOCK::pfnGetUuid */
306static DECLCALLBACK(int) drvHostBaseGetUuid(PPDMIBLOCK pInterface, PRTUUID pUuid)
307{
308 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
309
310 *pUuid = pThis->Uuid;
311
312 LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%Vuuid\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pUuid));
313 return VINF_SUCCESS;
314}
315
316
317/* -=-=-=-=- IBlockBios -=-=-=-=- */
318
319/** Makes a PDRVHOSTBASE out of a PPDMIBLOCKBIOS. */
320#define PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface) ( (PDRVHOSTBASE((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTBASE, IBlockBios))) )
321
322
323/** @copydoc PDMIBLOCKBIOS::pfnGetGeometry */
324static DECLCALLBACK(int) drvHostBaseGetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t *pcCylinders, uint32_t *pcHeads, uint32_t *pcSectors)
325{
326 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
327 RTCritSectEnter(&pThis->CritSect);
328
329 int rc = VINF_SUCCESS;
330 if (pThis->fMediaPresent)
331 {
332 if ( pThis->cCylinders > 0
333 && pThis->cHeads > 0
334 && pThis->cSectors > 0)
335 {
336 *pcCylinders = pThis->cCylinders;
337 *pcHeads = pThis->cHeads;
338 *pcSectors = pThis->cSectors;
339 }
340 else
341 rc = VERR_PDM_GEOMETRY_NOT_SET;
342 }
343 else
344 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
345
346 RTCritSectLeave(&pThis->CritSect);
347 LogFlow(("%s-%d: drvHostBaseGetGeometry: returns %Vrc CHS={%d,%d,%d}\n",
348 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc, *pcCylinders, *pcHeads, *pcSectors));
349 return rc;
350}
351
352
353/** @copydoc PDMIBLOCKBIOS::pfnSetGeometry */
354static DECLCALLBACK(int) drvHostBaseSetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors)
355{
356 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
357 LogFlow(("%s-%d: drvHostBaseSetGeometry: cCylinders=%d cHeads=%d cSectors=%d\n",
358 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, cCylinders, cHeads, cSectors));
359 RTCritSectEnter(&pThis->CritSect);
360
361 int rc = VINF_SUCCESS;
362 if (pThis->fMediaPresent)
363 {
364 pThis->cCylinders = cCylinders;
365 pThis->cHeads = cHeads;
366 pThis->cSectors = cSectors;
367 }
368 else
369 {
370 AssertMsgFailed(("Invalid state! Not mounted!\n"));
371 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
372 }
373
374 RTCritSectLeave(&pThis->CritSect);
375 return rc;
376}
377
378
379/** @copydoc PDMIBLOCKBIOS::pfnGetTranslation */
380static DECLCALLBACK(int) drvHostBaseGetTranslation(PPDMIBLOCKBIOS pInterface, PPDMBIOSTRANSLATION penmTranslation)
381{
382 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
383 RTCritSectEnter(&pThis->CritSect);
384
385 int rc = VINF_SUCCESS;
386 if (pThis->fMediaPresent)
387 {
388 if (pThis->fTranslationSet)
389 *penmTranslation = pThis->enmTranslation;
390 else
391 rc = VERR_PDM_TRANSLATION_NOT_SET;
392 }
393 else
394 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
395
396 RTCritSectLeave(&pThis->CritSect);
397 LogFlow(("%s-%d: drvHostBaseGetTranslation: returns %Vrc *penmTranslation=%d\n",
398 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc, *penmTranslation));
399 return rc;
400}
401
402
403/** @copydoc PDMIBLOCKBIOS::pfnSetTranslation */
404static DECLCALLBACK(int) drvHostBaseSetTranslation(PPDMIBLOCKBIOS pInterface, PDMBIOSTRANSLATION enmTranslation)
405{
406 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
407 LogFlow(("%s-%d: drvHostBaseSetTranslation: enmTranslation=%d\n",
408 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, enmTranslation));
409 RTCritSectEnter(&pThis->CritSect);
410
411 int rc = VINF_SUCCESS;
412 if (pThis->fMediaPresent)
413 {
414 pThis->fTranslationSet = true;
415 pThis->enmTranslation = enmTranslation;
416 }
417 else
418 {
419 AssertMsgFailed(("Invalid state! Not mounted!\n"));
420 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
421 }
422
423 RTCritSectLeave(&pThis->CritSect);
424 return rc;
425}
426
427
428/** @copydoc PDMIBLOCKBIOS::pfnIsVisible */
429static DECLCALLBACK(bool) drvHostBaseIsVisible(PPDMIBLOCKBIOS pInterface)
430{
431 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
432 return pThis->fBiosVisible;
433}
434
435
436/** @copydoc PDMIBLOCKBIOS::pfnGetType */
437static DECLCALLBACK(PDMBLOCKTYPE) drvHostBaseBiosGetType(PPDMIBLOCKBIOS pInterface)
438{
439 PDRVHOSTBASE pThis = PDMIBLOCKBIOS_2_DRVHOSTBASE(pInterface);
440 return pThis->enmType;
441}
442
443
444
445/* -=-=-=-=- IMount -=-=-=-=- */
446
447/** @copydoc PDMIMOUNT::pfnMount */
448static DECLCALLBACK(int) drvHostBaseMount(PPDMIMOUNT pInterface, const char *pszFilename, const char *pszCoreDriver)
449{
450 /* We're not mountable. */
451 AssertMsgFailed(("drvHostBaseMount: This shouldn't be called!\n"));
452 return VERR_PDM_MEDIA_MOUNTED;
453}
454
455
456/** @copydoc PDMIMOUNT::pfnUnmount */
457static DECLCALLBACK(int) drvHostBaseUnmount(PPDMIMOUNT pInterface)
458{
459 LogFlow(("drvHostBaseUnmount: returns VERR_NOT_SUPPORTED\n"));
460 return VERR_NOT_SUPPORTED;
461}
462
463
464/** @copydoc PDMIMOUNT::pfnIsMounted */
465static DECLCALLBACK(bool) drvHostBaseIsMounted(PPDMIMOUNT pInterface)
466{
467 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
468 RTCritSectEnter(&pThis->CritSect);
469
470 bool fRc = pThis->fMediaPresent;
471
472 RTCritSectLeave(&pThis->CritSect);
473 return fRc;
474}
475
476
477/** @copydoc PDMIMOUNT::pfnIsLocked */
478static DECLCALLBACK(int) drvHostBaseLock(PPDMIMOUNT pInterface)
479{
480 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
481 RTCritSectEnter(&pThis->CritSect);
482
483 int rc = VINF_SUCCESS;
484 if (!pThis->fLocked)
485 {
486 if (pThis->pfnDoLock)
487 rc = pThis->pfnDoLock(pThis, true);
488 if (VBOX_SUCCESS(rc))
489 pThis->fLocked = true;
490 }
491 else
492 LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
493
494 RTCritSectLeave(&pThis->CritSect);
495 LogFlow(("%s-%d: drvHostBaseLock: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
496 return rc;
497}
498
499
500/** @copydoc PDMIMOUNT::pfnIsLocked */
501static DECLCALLBACK(int) drvHostBaseUnlock(PPDMIMOUNT pInterface)
502{
503 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
504 RTCritSectEnter(&pThis->CritSect);
505
506 int rc = VINF_SUCCESS;
507 if (pThis->fLocked)
508 {
509 if (pThis->pfnDoLock)
510 rc = pThis->pfnDoLock(pThis, false);
511 if (VBOX_SUCCESS(rc))
512 pThis->fLocked = false;
513 }
514 else
515 LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
516
517 RTCritSectLeave(&pThis->CritSect);
518 LogFlow(("%s-%d: drvHostBaseUnlock: returns %Vrc\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, rc));
519 return rc;
520}
521
522
523/** @copydoc PDMIMOUNT::pfnIsLocked */
524static DECLCALLBACK(bool) drvHostBaseIsLocked(PPDMIMOUNT pInterface)
525{
526 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
527 RTCritSectEnter(&pThis->CritSect);
528
529 bool fRc = pThis->fLocked;
530
531 RTCritSectLeave(&pThis->CritSect);
532 return fRc;
533}
534
535
536/* -=-=-=-=- IBase -=-=-=-=- */
537
538/** @copydoc PDMIBASE::pfnQueryInterface. */
539static DECLCALLBACK(void *) drvHostBaseQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
540{
541 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
542 PDRVHOSTBASE pThis = PDMINS2DATA(pDrvIns, PDRVHOSTBASE);
543 switch (enmInterface)
544 {
545 case PDMINTERFACE_BASE:
546 return &pDrvIns->IBase;
547 case PDMINTERFACE_BLOCK:
548 return &pThis->IBlock;
549 case PDMINTERFACE_BLOCK_BIOS:
550 return pThis->fBiosVisible ? &pThis->IBlockBios : NULL;
551 case PDMINTERFACE_MOUNT:
552 return &pThis->IMount;
553 default:
554 return NULL;
555 }
556}
557
558
559/* -=-=-=-=- poller thread -=-=-=-=- */
560
561/**
562 * Wrapper for open / RTFileOpen / IOKit.
563 *
564 * @remark The Darwin code must correspond exactly to the enumeration
565 * done in Main/darwin/iokit.c.
566 */
567static int drvHostBaseOpen(PDRVHOSTBASE pThis, PRTFILE pFileDevice, bool fReadOnly)
568{
569#ifdef __DARWIN__
570 /* Darwin is kind of special... */
571 Assert(!pFileDevice); NOREF(pFileDevice);
572 Assert(!pThis->cbBlock);
573 Assert(!pThis->MasterPort);
574 Assert(!pThis->ppMMCDI);
575 Assert(!pThis->ppScsiTaskDI);
576
577 /*
578 * Open the master port on the first invocation.
579 */
580 kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &pThis->MasterPort);
581 AssertReturn(krc == KERN_SUCCESS, VERR_GENERAL_FAILURE);
582
583 /*
584 * Create a matching dictionary for searching for DVD services in the IOKit.
585 *
586 * [If I understand this correctly, plain CDROMs doesn't show up as
587 * IODVDServices. Too keep things simple, we will only support DVDs
588 * until somebody complains about it and we get hardware to test it on.
589 * (Unless I'm much mistaken, there aren't any (orignal) intel macs with
590 * plain cdroms.)]
591 */
592 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IODVDServices");
593 AssertReturn(RefMatchingDict, NULL);
594
595 /*
596 * do the search and get a collection of keyboards.
597 */
598 io_iterator_t DVDServices = NULL;
599 IOReturn irc = IOServiceGetMatchingServices(pThis->MasterPort, RefMatchingDict, &DVDServices);
600 AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%d\n", irc), NULL);
601 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
602
603 /*
604 * Enumerate the DVD drives (services).
605 * (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
606 */
607 int rc = VERR_FILE_NOT_FOUND;
608 unsigned i = 0;
609 io_object_t DVDService;
610 while ((DVDService = IOIteratorNext(DVDServices)) != 0)
611 {
612 /*
613 * Get the properties we use to identify the DVD drive.
614 *
615 * While there is a (weird 12 byte) GUID, it isn't persistent
616 * accross boots. So, we have to use a combination of the
617 * vendor name and product name properties with an optional
618 * sequence number for identification.
619 */
620 CFMutableDictionaryRef PropsRef = 0;
621 kern_return_t krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
622 if (krc == KERN_SUCCESS)
623 {
624 /* Get the Device Characteristics dictionary. */
625 CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
626 if (DevCharRef)
627 {
628 /* The vendor name. */
629 char szVendor[128];
630 char *pszVendor = &szVendor[0];
631 CFTypeRef ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyVendorNameKey));
632 if ( ValueRef
633 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
634 && CFStringGetCString((CFStringRef)ValueRef, szVendor, sizeof(szVendor), kCFStringEncodingUTF8))
635 pszVendor = RTStrStrip(szVendor);
636 else
637 *pszVendor = '\0';
638
639 /* The product name. */
640 char szProduct[128];
641 char *pszProduct = &szProduct[0];
642 ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyProductNameKey));
643 if ( ValueRef
644 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
645 && CFStringGetCString((CFStringRef)ValueRef, szProduct, sizeof(szProduct), kCFStringEncodingUTF8))
646 pszProduct = RTStrStrip(szProduct);
647 else
648 *pszProduct = '\0';
649
650 /* Construct the two names and compare thwm with the one we're searching for. */
651 char szName1[256 + 32];
652 char szName2[256 + 32];
653 if (*pszVendor || *pszProduct)
654 {
655 if (*pszVendor && *pszProduct)
656 {
657 RTStrPrintf(szName1, sizeof(szName1), "%s %s", pszVendor, pszProduct);
658 RTStrPrintf(szName2, sizeof(szName2), "%s %s (#%u)", pszVendor, pszProduct, i);
659 }
660 else
661 {
662 strcpy(szName1, *pszVendor ? pszVendor : pszProduct);
663 RTStrPrintf(szName2, sizeof(szName2), "%s %s (#%u)", *pszVendor ? pszVendor : pszProduct, i);
664 }
665 }
666 else
667 {
668 RTStrPrintf(szName1, sizeof(szName1), "(#%u)", i);
669 strcpy(szName2, szName1);
670 }
671
672 if ( !strcmp(szName1, pThis->pszDeviceOpen)
673 || !strcmp(szName2, pThis->pszDeviceOpen))
674 {
675 /*
676 * Found it! Now, get the client interface and stuff.
677 * Note that we could also query kIOSCSITaskDeviceUserClientTypeID here if the
678 * MMC client plugin is missing. For now we assume this won't be necessary.
679 */
680 SInt32 Score = 0;
681 IOCFPlugInInterface **ppPlugInInterface = NULL;
682 krc = IOCreatePlugInInterfaceForService(DVDService, kIOMMCDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
683 &ppPlugInInterface, &Score);
684 if (krc == KERN_SUCCESS)
685 {
686 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
687 CFUUIDGetUUIDBytes(kIOMMCDeviceInterfaceID),
688 (LPVOID *)&pThis->ppMMCDI);
689 (*ppPlugInInterface)->Release(ppPlugInInterface);
690 ppPlugInInterface = NULL;
691 if (hrc == S_OK)
692 {
693 pThis->ppScsiTaskDI = (*pThis->ppMMCDI)->GetSCSITaskDeviceInterface(pThis->ppMMCDI);
694 if (pThis->ppScsiTaskDI)
695 rc = VINF_SUCCESS;
696 else
697 {
698 LogRel(("GetSCSITaskDeviceInterface failed on '%s'\n", pThis->pszDeviceOpen));
699 rc = VERR_NOT_SUPPORTED;
700 (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
701 }
702 }
703 else
704 {
705 rc = VERR_GENERAL_FAILURE;//RTErrConvertFromDarwinCOM(krc);
706 pThis->ppMMCDI = NULL;
707 }
708 }
709 else /* Check for kIOSCSITaskDeviceUserClientTypeID? */
710 rc = VERR_GENERAL_FAILURE;//RTErrConvertFromDarwinKern(krc);
711
712 /*
713 * Obtain exclusive access to the device
714 * (to prevent the host and/or user from interfering).
715 */
716 if (VBOX_SUCCESS(rc))
717 {
718 irc = (*pThis->ppScsiTaskDI)->ObtainExclusiveAccess(pThis->ppScsiTaskDI);
719 if (irc == kIOReturnSuccess)
720 rc = VINF_SUCCESS;
721 else if (irc == kIOReturnBusy)
722 rc = VERR_DRIVE_LOCKED; /* mounted. */
723 else if (irc == kIOReturnExclusiveAccess)
724 rc = VERR_SHARING_VIOLATION; /* already used exclusivly. */
725 else
726 rc = VERR_GENERAL_FAILURE;
727 }
728
729 /* Cleanup on failure. */
730 if (VBOX_FAILURE(rc))
731 {
732 if (pThis->ppScsiTaskDI)
733 {
734 (*pThis->ppScsiTaskDI)->Release(pThis->ppScsiTaskDI);
735 pThis->ppScsiTaskDI = NULL;
736 }
737 if (pThis->ppMMCDI)
738 {
739 (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
740 pThis->ppMMCDI = NULL;
741 }
742 }
743
744 IOObjectRelease(DVDService);
745 break;
746 }
747 }
748 CFRelease(PropsRef);
749 }
750 else
751 AssertMsgFailed(("krc=%#x\n", krc));
752
753 IOObjectRelease(DVDService);
754 i++;
755 }
756
757 IOObjectRelease(DVDServices);
758 return rc;
759
760#elif defined(__LINUX__)
761 /** @todo we've got RTFILE_O_NON_BLOCK now. Change the code to use RTFileOpen. */
762 int FileDevice = open(pThis->pszDeviceOpen, (pThis->fReadOnlyConfig ? O_RDONLY : O_RDWR) | O_NONBLOCK);
763 if (FileDevice < 0)
764 return RTErrConvertFromErrno(errno);
765 *pFileDevice = FileDevice;
766 return VINF_SUCCESS;
767
768#else
769 return RTFileOpen(pFileDevice, pThis->pszDeviceOpen,
770 (fReadOnly ? RTFILE_O_READ : RTFILE_O_READWRITE) | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
771#endif
772}
773
774
775/**
776 * (Re)opens the device.
777 *
778 * This is used to open the device during construction, but it's also used to re-open
779 * the device when a media is inserted. This re-open will kill off any cached data
780 * that Linux for some peculiar reason thinks should survive a media change...
781 *
782 * @returns VBOX status code.
783 * @param pThis Instance data.
784 */
785static int drvHostBaseReopen(PDRVHOSTBASE pThis)
786{
787#ifndef __DARWIN__ /* Only *one* open for darwin. */
788 LogFlow(("%s-%d: drvHostBaseReopen: '%s'\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen));
789
790 RTFILE FileDevice;
791 int rc = drvHostBaseOpen(pThis, &FileDevice, pThis->fReadOnlyConfig);
792 if (VBOX_FAILURE(rc))
793 {
794 if (!pThis->fReadOnlyConfig)
795 {
796 LogFlow(("%s-%d: drvHostBaseReopen: '%s' - retry readonly (%Vrc)\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen, rc));
797 rc = drvHostBaseOpen(pThis, &FileDevice, false);
798 }
799 if (VBOX_FAILURE(rc))
800 {
801 LogFlow(("%s-%d: failed to open device '%s', rc=%Vrc\n",
802 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
803 return rc;
804 }
805 pThis->fReadOnly = true;
806 }
807 else
808 pThis->fReadOnly = pThis->fReadOnlyConfig;
809
810 if (pThis->FileDevice != NIL_RTFILE)
811 RTFileClose(pThis->FileDevice);
812 pThis->FileDevice = FileDevice;
813#endif /* !__DARWIN__ */
814 return VINF_SUCCESS;
815}
816
817
818/**
819 * Queries the media size.
820 *
821 * @returns VBox status code.
822 * @param pThis Pointer to the instance data.
823 * @param pcb Where to store the media size in bytes.
824 */
825static int drvHostBaseGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
826{
827#ifdef __DARWIN__
828 /*
829 * Try a READ_CAPACITY command...
830 */
831 struct
832 {
833 uint32_t cBlocks;
834 uint32_t cbBlock;
835 } Buf = {0, 0};
836 size_t cbBuf = sizeof(Buf);
837 uint8_t abCmd[16] =
838 {
839 SCSI_READ_CAPACITY, 0, 0, 0, 0, 0, 0,
840 0,0,0,0,0,0,0,0,0
841 };
842 int rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_FROM_DEVICE, &Buf, &cbBuf, NULL, 0, 0);
843 if (VBOX_SUCCESS(rc))
844 {
845 Assert(cbBuf == sizeof(Buf));
846 Buf.cBlocks = RT_BE2H_U32(Buf.cBlocks);
847 Buf.cbBlock = RT_BE2H_U32(Buf.cbBlock);
848 //if (Buf.cbBlock > 2048) /* everyone else is doing this... check if it needed/right.*/
849 // Buf.cbBlock = 2048;
850 pThis->cbBlock = Buf.cbBlock;
851
852 *pcb = (uint64_t)Buf.cBlocks * Buf.cbBlock;
853 }
854 return rc;
855
856#elif defined(__WIN__)
857 /* use NT api, retry a few times if the media is being verified. */
858 IO_STATUS_BLOCK IoStatusBlock = {0};
859 FILE_FS_SIZE_INFORMATION FsSize= {0};
860 NTSTATUS rcNt = NtQueryVolumeInformationFile((HANDLE)pThis->FileDevice, &IoStatusBlock,
861 &FsSize, sizeof(FsSize), FileFsSizeInformation);
862 int cRetries = 5;
863 while (rcNt == STATUS_VERIFY_REQUIRED && cRetries-- > 0)
864 {
865 RTThreadSleep(10);
866 rcNt = NtQueryVolumeInformationFile((HANDLE)pThis->FileDevice, &IoStatusBlock,
867 &FsSize, sizeof(FsSize), FileFsSizeInformation);
868 }
869 if (rcNt >= 0)
870 {
871 *pcb = FsSize.TotalAllocationUnits.QuadPart * FsSize.BytesPerSector;
872 return VINF_SUCCESS;
873 }
874
875 /* convert nt status code to VBox status code. */
876 /** @todo Make convertion function!. */
877 int rc = VERR_GENERAL_FAILURE;
878 switch (rcNt)
879 {
880 case STATUS_NO_MEDIA_IN_DEVICE: rc = VERR_MEDIA_NOT_PRESENT; break;
881 case STATUS_VERIFY_REQUIRED: rc = VERR_TRY_AGAIN; break;
882 }
883 LogFlow(("drvHostBaseGetMediaSize: NtQueryVolumeInformationFile -> %#lx\n", rcNt, rc));
884 return rc;
885#else
886 return RTFileSeek(pThis->FileDevice, 0, RTFILE_SEEK_END, pcb);
887#endif
888}
889
890
891#ifdef __DARWIN__
892/**
893 * Execute a SCSI command.
894 *
895 * @param pThis The instance data.
896 * @param pbCmd Pointer to the SCSI command.
897 * @param cbCmd The size of the SCSI command.
898 * @param enmTxDir The transfer direction.
899 * @param pvBuf The buffer. Can be NULL if enmTxDir is PDMBLOCKTXDIR_NONE.
900 * @param pcbBuf Where to get the buffer size from and put the actual transfer size. Can be NULL.
901 * @param pbSense Where to put the sense data. Can be NULL.
902 * @param cbSense Size of the sense data buffer.
903 * @param cTimeoutMillies The timeout. 0 mean the default timeout.
904 *
905 * @returns VINF_SUCCESS on success (no sense code).
906 * @returns VERR_UNRESOLVED_ERROR if sense code is present.
907 * @returns Some other VBox status code on failures without sense code.
908 *
909 * @todo Fix VERR_UNRESOLVED_ERROR abuse.
910 */
911DECLCALLBACK(int) DRVHostBaseScsiCmd(PDRVHOSTBASE pThis, const uint8_t *pbCmd, size_t cbCmd, PDMBLOCKTXDIR enmTxDir,
912 void *pvBuf, size_t *pcbBuf, uint8_t *pbSense, size_t cbSense, uint32_t cTimeoutMillies)
913{
914 /*
915 * Minimal input validation.
916 */
917 Assert(enmTxDir == PDMBLOCKTXDIR_NONE || enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE || enmTxDir == PDMBLOCKTXDIR_TO_DEVICE);
918 Assert(!pvBuf || (pcbBuf && *pcbBuf));
919 Assert(pvBuf || enmTxDir == PDMBLOCKTXDIR_NONE);
920 Assert(pbSense || !cbSense);
921 AssertPtr(pbCmd);
922 Assert(cbCmd <= 16 && cbCmd >= 1);
923 const size_t cbBuf = pcbBuf ? *pcbBuf : 0;
924 if (pcbBuf)
925 *pcbBuf = 0;
926
927# ifdef __DARWIN__
928 Assert(pThis->ppScsiTaskDI);
929
930 int rc = VERR_GENERAL_FAILURE;
931 SCSITaskInterface **ppScsiTaskI = (*pThis->ppScsiTaskDI)->CreateSCSITask(pThis->ppScsiTaskDI);
932 if (!ppScsiTaskI)
933 return VERR_NO_MEMORY;
934 do
935 {
936 /* Setup the scsi command. */
937 SCSICommandDescriptorBlock cdb = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
938 memcpy(&cdb[0], pbCmd, cbCmd);
939 IOReturn irc = (*ppScsiTaskI)->SetCommandDescriptorBlock(ppScsiTaskI, cdb, cbCmd);
940 AssertBreak(irc == kIOReturnSuccess,);
941
942 /* Setup the buffer. */
943 if (enmTxDir == PDMBLOCKTXDIR_NONE)
944 irc = (*ppScsiTaskI)->SetScatterGatherEntries(ppScsiTaskI, NULL, 0, 0, kSCSIDataTransfer_NoDataTransfer);
945 else
946 {
947 IOVirtualRange Range = { (IOVirtualAddress)pvBuf, cbBuf };
948 irc = (*ppScsiTaskI)->SetScatterGatherEntries(ppScsiTaskI, &Range, 1, cbBuf,
949 enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE
950 ? kSCSIDataTransfer_FromTargetToInitiator
951 : kSCSIDataTransfer_FromInitiatorToTarget);
952 }
953 AssertBreak(irc == kIOReturnSuccess,);
954
955 /* Set the timeout. */
956 irc = (*ppScsiTaskI)->SetTimeoutDuration(ppScsiTaskI, cTimeoutMillies ? cTimeoutMillies : 30000 /*ms*/);
957 AssertBreak(irc == kIOReturnSuccess,);
958
959 /* Execute the command and get the response. */
960 SCSI_Sense_Data SenseData = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
961 SCSIServiceResponse ServiceResponse = kSCSIServiceResponse_Request_In_Process;
962 SCSITaskStatus TaskStatus = kSCSITaskStatus_GOOD;
963 UInt64 cbReturned = 0;
964 irc = (*ppScsiTaskI)->ExecuteTaskSync(ppScsiTaskI, &SenseData, &TaskStatus, &cbReturned);
965 AssertBreak(irc == kIOReturnSuccess,);
966 if (pcbBuf)
967 *pcbBuf = cbReturned;
968
969 irc = (*ppScsiTaskI)->GetSCSIServiceResponse(ppScsiTaskI, &ServiceResponse);
970 AssertBreak(irc == kIOReturnSuccess,);
971 AssertBreak(ServiceResponse == kSCSIServiceResponse_TASK_COMPLETE,);
972
973 if (TaskStatus == kSCSITaskStatus_GOOD)
974 rc = VINF_SUCCESS;
975 else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
976 && pbSense)
977 {
978 memset(pbSense, 0, cbSense); /* lazy */
979 memcpy(pbSense, &SenseData, RT_MIN(sizeof(SenseData), cbSense));
980 rc = VERR_UNRESOLVED_ERROR;
981 }
982 /** @todo convert sense codes when caller doesn't wish to do this himself. */
983 /*else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
984 && SenseData.ADDITIONAL_SENSE_CODE == 0x3A)
985 rc = VERR_MEDIA_NOT_PRESENT; */
986 else
987 {
988 rc = enmTxDir == PDMBLOCKTXDIR_NONE
989 ? VERR_DEV_IO_ERROR
990 : enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE
991 ? VERR_READ_ERROR
992 : VERR_WRITE_ERROR;
993 if (pThis->cLogRelErrors++ < 10)
994 LogRel(("DVD scsi error: cmd={%.*Rhxs} TaskStatus=%#x key=%#x ASC=%#x ASCQ=%#x (%Vrc)\n",
995 cbCmd, pbCmd, TaskStatus, SenseData.SENSE_KEY, SenseData.ADDITIONAL_SENSE_CODE,
996 SenseData.ADDITIONAL_SENSE_CODE_QUALIFIER, rc));
997 }
998 } while (0);
999
1000 (*ppScsiTaskI)->Release(ppScsiTaskI);
1001
1002# endif
1003
1004 return rc;
1005}
1006#endif
1007
1008
1009/**
1010 * Media present.
1011 * Query the size and notify the above driver / device.
1012 *
1013 * @param pThis The instance data.
1014 */
1015int DRVHostBaseMediaPresent(PDRVHOSTBASE pThis)
1016{
1017 /*
1018 * Open the drive.
1019 */
1020 int rc = drvHostBaseReopen(pThis);
1021 if (VBOX_FAILURE(rc))
1022 return rc;
1023
1024 /*
1025 * Determin the size.
1026 */
1027 uint64_t cb;
1028 rc = pThis->pfnGetMediaSize(pThis, &cb);
1029 if (VBOX_FAILURE(rc))
1030 {
1031 LogFlow(("%s-%d: failed to figure media size of %s, rc=%Vrc\n",
1032 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
1033 return rc;
1034 }
1035
1036 /*
1037 * Update the data and inform the unit.
1038 */
1039 pThis->cbSize = cb;
1040 pThis->fMediaPresent = true;
1041 if (pThis->pDrvMountNotify)
1042 pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
1043 LogFlow(("%s-%d: drvHostBaseMediaPresent: cbSize=%lld (%#llx)\n",
1044 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, pThis->cbSize, pThis->cbSize));
1045 return VINF_SUCCESS;
1046}
1047
1048
1049/**
1050 * Media no longer present.
1051 * @param pThis The instance data.
1052 */
1053void DRVHostBaseMediaNotPresent(PDRVHOSTBASE pThis)
1054{
1055 pThis->fMediaPresent = false;
1056 pThis->fLocked = false;
1057 pThis->fTranslationSet = false;
1058 pThis->cSectors = 0;
1059 if (pThis->pDrvMountNotify)
1060 pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
1061}
1062
1063
1064#ifdef __WIN__
1065
1066/**
1067 * Window procedure for the invisible window used to catch the WM_DEVICECHANGE broadcasts.
1068 */
1069static LRESULT CALLBACK DeviceChangeWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1070{
1071 Log2(("DeviceChangeWindowProc: hwnd=%08x uMsg=%08x\n", hwnd, uMsg));
1072 if (uMsg == WM_DESTROY)
1073 {
1074 PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLong(hwnd, GWLP_USERDATA);
1075 if (pThis)
1076 ASMAtomicXchgSize(&pThis->hwndDeviceChange, NULL);
1077 PostQuitMessage(0);
1078 }
1079
1080 if (uMsg != WM_DEVICECHANGE)
1081 return DefWindowProc(hwnd, uMsg, wParam, lParam);
1082
1083 PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
1084 PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
1085 Assert(pThis);
1086 if (pThis == NULL)
1087 return 0;
1088
1089 switch (wParam)
1090 {
1091 case DBT_DEVICEARRIVAL:
1092 case DBT_DEVICEREMOVECOMPLETE:
1093 // Check whether a CD or DVD was inserted into or removed from a drive.
1094 if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
1095 {
1096 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
1097 if ( (lpdbv->dbcv_flags & DBTF_MEDIA)
1098 && (pThis->fUnitMask & lpdbv->dbcv_unitmask))
1099 {
1100 RTCritSectEnter(&pThis->CritSect);
1101 if (wParam == DBT_DEVICEARRIVAL)
1102 {
1103 int cRetries = 10;
1104 int rc = DRVHostBaseMediaPresent(pThis);
1105 while (VBOX_FAILURE(rc) && cRetries-- > 0)
1106 {
1107 RTThreadSleep(50);
1108 rc = DRVHostBaseMediaPresent(pThis);
1109 }
1110 }
1111 else
1112 DRVHostBaseMediaNotPresent(pThis);
1113 RTCritSectLeave(&pThis->CritSect);
1114 }
1115 }
1116 break;
1117 }
1118 return TRUE;
1119}
1120
1121#endif /* __WIN__ */
1122
1123
1124/**
1125 * This thread will periodically poll the device for media presence.
1126 *
1127 * @returns Ignored.
1128 * @param ThreadSelf Handle of this thread. Ignored.
1129 * @param pvUser Pointer to the driver instance structure.
1130 */
1131static DECLCALLBACK(int) drvHostBaseMediaThread(RTTHREAD ThreadSelf, void *pvUser)
1132{
1133 PDRVHOSTBASE pThis = (PDRVHOSTBASE)pvUser;
1134 LogFlow(("%s-%d: drvHostBaseMediaThread: ThreadSelf=%p pvUser=%p\n",
1135 pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, ThreadSelf, pvUser));
1136#ifdef __WIN__
1137 static WNDCLASS s_classDeviceChange = {0};
1138 static ATOM s_hAtomDeviceChange = 0;
1139
1140 /*
1141 * Register custom window class.
1142 */
1143 if (s_hAtomDeviceChange == 0)
1144 {
1145 memset(&s_classDeviceChange, 0, sizeof(s_classDeviceChange));
1146 s_classDeviceChange.lpfnWndProc = DeviceChangeWindowProc;
1147 s_classDeviceChange.lpszClassName = "VBOX_DeviceChangeClass";
1148 s_classDeviceChange.hInstance = GetModuleHandle("VBOXDD.DLL");
1149 Assert(s_classDeviceChange.hInstance);
1150 s_hAtomDeviceChange = RegisterClassA(&s_classDeviceChange);
1151 Assert(s_hAtomDeviceChange);
1152 }
1153
1154 /*
1155 * Create Window w/ the pThis as user data.
1156 */
1157 HWND hwnd = CreateWindow((LPCTSTR)s_hAtomDeviceChange, "", WS_POPUP, 0, 0, 0, 0, 0, 0, s_classDeviceChange.hInstance, 0);
1158 AssertMsg(hwnd, ("CreateWindow failed with %d\n", GetLastError()));
1159 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
1160
1161 /*
1162 * Signal the waiting EMT thread that everything went fine.
1163 */
1164 ASMAtomicXchgSize(&pThis->hwndDeviceChange, hwnd);
1165 RTThreadUserSignal(ThreadSelf);
1166 if (!hwnd)
1167 {
1168 LogFlow(("%s-%d: drvHostBaseMediaThread: returns VERR_GENERAL_FAILURE\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
1169 return VERR_GENERAL_FAILURE;
1170 }
1171 LogFlow(("%s-%d: drvHostBaseMediaThread: Created hwndDeviceChange=%p\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance, hwnd));
1172
1173 /*
1174 * Message pump.
1175 */
1176 MSG Msg;
1177 BOOL fRet;
1178 while ((fRet = GetMessage(&Msg, NULL, 0, 0)) != FALSE)
1179 {
1180 if (fRet != -1)
1181 {
1182 TranslateMessage(&Msg);
1183 DispatchMessage(&Msg);
1184 }
1185 //else: handle the error and possibly exit
1186 }
1187 Assert(!pThis->hwndDeviceChange);
1188
1189#else /* !__WIN__ */
1190 bool fFirst = true;
1191 int cRetries = 10;
1192 while (!pThis->fShutdownPoller)
1193 {
1194 /*
1195 * Perform the polling (unless we've run out of 50ms retries).
1196 */
1197 if ( pThis->pfnPoll
1198 && cRetries-- > 0)
1199 {
1200
1201 int rc = pThis->pfnPoll(pThis);
1202 if (VBOX_FAILURE(rc))
1203 {
1204 RTSemEventWait(pThis->EventPoller, 50);
1205 continue;
1206 }
1207 }
1208
1209 /*
1210 * Signal EMT after the first go.
1211 */
1212 if (fFirst)
1213 {
1214 RTThreadUserSignal(ThreadSelf);
1215 fFirst = false;
1216 }
1217
1218 /*
1219 * Sleep.
1220 */
1221 int rc = RTSemEventWait(pThis->EventPoller, pThis->cMilliesPoller);
1222 if ( VBOX_FAILURE(rc)
1223 && rc != VERR_TIMEOUT)
1224 {
1225 AssertMsgFailed(("rc=%Vrc\n", rc));
1226 pThis->ThreadPoller = NIL_RTTHREAD;
1227 LogFlow(("drvHostBaseMediaThread: returns %Vrc\n", rc));
1228 return rc;
1229 }
1230 cRetries = 10;
1231 }
1232
1233#endif /* !__WIN__ */
1234
1235 /* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
1236 LogFlow(("%s-%d: drvHostBaseMediaThread: returns VINF_SUCCESS\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
1237 return VINF_SUCCESS;
1238}
1239
1240/* -=-=-=-=- driver interface -=-=-=-=- */
1241
1242
1243/**
1244 * Done state load operation.
1245 *
1246 * @returns VBox load code.
1247 * @param pDrvIns Driver instance of the driver which registered the data unit.
1248 * @param pSSM SSM operation handle.
1249 */
1250static DECLCALLBACK(int) drvHostBaseLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
1251{
1252 PDRVHOSTBASE pThis = PDMINS2DATA(pDrvIns, PDRVHOSTBASE);
1253 LogFlow(("%s-%d: drvHostBaseMediaThread:\n", pThis->pDrvIns->pDrvReg->szDriverName, pThis->pDrvIns->iInstance));
1254 RTCritSectEnter(&pThis->CritSect);
1255
1256 /*
1257 * Tell the device/driver above us that the media status is uncertain.
1258 */
1259 if (pThis->pDrvMountNotify)
1260 {
1261 pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
1262 if (pThis->fMediaPresent)
1263 pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
1264 }
1265
1266 RTCritSectLeave(&pThis->CritSect);
1267 return VINF_SUCCESS;
1268}
1269
1270
1271/** @copydoc FNPDMDRVDESTRUCT */
1272DECLCALLBACK(void) DRVHostBaseDestruct(PPDMDRVINS pDrvIns)
1273{
1274 PDRVHOSTBASE pThis = PDMINS2DATA(pDrvIns, PDRVHOSTBASE);
1275 LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pDrvIns->iInstance));
1276
1277 /*
1278 * Terminate the thread.
1279 */
1280 if (pThis->ThreadPoller != NIL_RTTHREAD)
1281 {
1282 pThis->fShutdownPoller = true;
1283 int rc;
1284 int cTimes = 50;
1285 do
1286 {
1287#ifdef __WIN__
1288 if (pThis->hwndDeviceChange)
1289 PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
1290#else
1291 RTSemEventSignal(pThis->EventPoller);
1292#endif
1293 rc = RTThreadWait(pThis->ThreadPoller, 100, NULL);
1294 } while (cTimes-- > 0 && rc == VERR_TIMEOUT);
1295
1296 if (!rc)
1297 pThis->ThreadPoller = NIL_RTTHREAD;
1298 }
1299
1300 /*
1301 * Unlock the drive if we've locked it.
1302 */
1303 if ( pThis->fLocked
1304#ifdef __DARWIN__
1305 && pThis->ppScsiTaskDI
1306#else
1307 && pThis->FileDevice != NIL_RTFILE
1308#endif
1309 && pThis->pfnDoLock)
1310 {
1311 int rc = pThis->pfnDoLock(pThis, false);
1312 if (VBOX_SUCCESS(rc))
1313 pThis->fLocked = false;
1314 }
1315
1316 /*
1317 * Cleanup the other resources.
1318 */
1319#ifdef __WIN__
1320 if (pThis->hwndDeviceChange)
1321 {
1322 if (SetWindowLongPtr(pThis->hwndDeviceChange, GWLP_USERDATA, 0) == (LONG_PTR)pThis)
1323 PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
1324 pThis->hwndDeviceChange = NULL;
1325 }
1326#else
1327 if (pThis->EventPoller != NULL)
1328 {
1329 RTSemEventDestroy(pThis->EventPoller);
1330 pThis->EventPoller = NULL;
1331 }
1332#endif
1333
1334#ifdef __DARWIN__
1335 if (pThis->ppScsiTaskDI)
1336 {
1337 (*pThis->ppScsiTaskDI)->ReleaseExclusiveAccess(pThis->ppScsiTaskDI);
1338 (*pThis->ppScsiTaskDI)->Release(pThis->ppScsiTaskDI);
1339 pThis->ppScsiTaskDI = NULL;
1340 }
1341 if (pThis->ppMMCDI)
1342 {
1343 (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
1344 pThis->ppMMCDI = NULL;
1345 }
1346 if (pThis->MasterPort)
1347 {
1348 mach_port_deallocate(mach_task_self(), pThis->MasterPort);
1349 pThis->MasterPort = NULL;
1350 }
1351#else
1352 if (pThis->FileDevice != NIL_RTFILE)
1353 {
1354 int rc = RTFileClose(pThis->FileDevice);
1355 AssertRC(rc);
1356 pThis->FileDevice = NIL_RTFILE;
1357 }
1358#endif
1359
1360 if (pThis->pszDevice)
1361 {
1362 MMR3HeapFree(pThis->pszDevice);
1363 pThis->pszDevice = NULL;
1364 }
1365
1366 if (pThis->pszDeviceOpen)
1367 {
1368 RTStrFree(pThis->pszDeviceOpen);
1369 pThis->pszDeviceOpen = NULL;
1370 }
1371
1372 if (RTCritSectIsInitialized(&pThis->CritSect))
1373 RTCritSectDelete(&pThis->CritSect);
1374}
1375
1376
1377/**
1378 * Initializes the instance data (init part 1).
1379 *
1380 * The driver which derives from this base driver will override function pointers after
1381 * calling this method, and complete the construction by calling DRVHostBaseInitFinish().
1382 *
1383 * On failure call DRVHostBaseDestruct().
1384 *
1385 * @returns VBox status code.
1386 * @param pDrvIns Driver instance.
1387 * @param pCfgHandle Configuration handle.
1388 * @param enmType Device type.
1389 */
1390int DRVHostBaseInitData(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, PDMBLOCKTYPE enmType)
1391{
1392 PDRVHOSTBASE pThis = PDMINS2DATA(pDrvIns, PDRVHOSTBASE);
1393 LogFlow(("%s-%d: DRVHostBaseInitData: iInstance=%d\n", pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pDrvIns->iInstance));
1394
1395 /*
1396 * Initialize most of the data members.
1397 */
1398 pThis->pDrvIns = pDrvIns;
1399 pThis->ThreadPoller = NIL_RTTHREAD;
1400#ifdef __DARWIN__
1401 pThis->MasterPort = NULL;
1402 pThis->ppMMCDI = NULL;
1403 pThis->ppScsiTaskDI = NULL;
1404 pThis->cbBlock = 0;
1405#else
1406 pThis->FileDevice = NIL_RTFILE;
1407#endif
1408 pThis->enmType = enmType;
1409 //pThis->cErrors = 0;
1410
1411 pThis->pfnGetMediaSize = drvHostBaseGetMediaSize;
1412
1413 /* IBase. */
1414 pDrvIns->IBase.pfnQueryInterface = drvHostBaseQueryInterface;
1415
1416 /* IBlock. */
1417 pThis->IBlock.pfnRead = drvHostBaseRead;
1418 pThis->IBlock.pfnWrite = drvHostBaseWrite;
1419 pThis->IBlock.pfnFlush = drvHostBaseFlush;
1420 pThis->IBlock.pfnIsReadOnly = drvHostBaseIsReadOnly;
1421 pThis->IBlock.pfnGetSize = drvHostBaseGetSize;
1422 pThis->IBlock.pfnGetType = drvHostBaseGetType;
1423 pThis->IBlock.pfnGetUuid = drvHostBaseGetUuid;
1424
1425 /* IBlockBios. */
1426 pThis->IBlockBios.pfnGetGeometry = drvHostBaseGetGeometry;
1427 pThis->IBlockBios.pfnSetGeometry = drvHostBaseSetGeometry;
1428 pThis->IBlockBios.pfnGetTranslation = drvHostBaseGetTranslation;
1429 pThis->IBlockBios.pfnSetTranslation = drvHostBaseSetTranslation;
1430 pThis->IBlockBios.pfnIsVisible = drvHostBaseIsVisible;
1431 pThis->IBlockBios.pfnGetType = drvHostBaseBiosGetType;
1432
1433 /* IMount. */
1434 pThis->IMount.pfnMount = drvHostBaseMount;
1435 pThis->IMount.pfnUnmount = drvHostBaseUnmount;
1436 pThis->IMount.pfnIsMounted = drvHostBaseIsMounted;
1437 pThis->IMount.pfnLock = drvHostBaseLock;
1438 pThis->IMount.pfnUnlock = drvHostBaseUnlock;
1439 pThis->IMount.pfnIsLocked = drvHostBaseIsLocked;
1440
1441 /*
1442 * Get the IBlockPort & IMountNotify interfaces of the above driver/device.
1443 */
1444 pThis->pDrvBlockPort = (PPDMIBLOCKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_BLOCK_PORT);
1445 if (!pThis->pDrvBlockPort)
1446 {
1447 AssertMsgFailed(("Configuration error: No block port interface above!\n"));
1448 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1449 }
1450 pThis->pDrvMountNotify = (PPDMIMOUNTNOTIFY)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUNT_NOTIFY);
1451
1452 /*
1453 * Query configuration.
1454 */
1455 /* Device */
1456 int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Path", &pThis->pszDevice);
1457 if (VBOX_FAILURE(rc))
1458 {
1459 AssertMsgFailed(("Configuration error: query for \"Path\" string returned %Vra.\n", rc));
1460 return rc;
1461 }
1462
1463 /* Mountable */
1464 uint32_t u32;
1465 rc = CFGMR3QueryU32(pCfgHandle, "Interval", &u32);
1466 if (VBOX_SUCCESS(rc))
1467 pThis->cMilliesPoller = u32;
1468 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1469 pThis->cMilliesPoller = 1000;
1470 else if (VBOX_FAILURE(rc))
1471 {
1472 AssertMsgFailed(("Configuration error: Query \"Mountable\" resulted in %Vrc.\n", rc));
1473 return rc;
1474 }
1475
1476 /* ReadOnly */
1477 rc = CFGMR3QueryBool(pCfgHandle, "ReadOnly", &pThis->fReadOnlyConfig);
1478 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1479 pThis->fReadOnlyConfig = enmType == PDMBLOCKTYPE_DVD || enmType == PDMBLOCKTYPE_CDROM ? true : false;
1480 else if (VBOX_FAILURE(rc))
1481 {
1482 AssertMsgFailed(("Configuration error: Query \"ReadOnly\" resulted in %Vrc.\n", rc));
1483 return rc;
1484 }
1485
1486 /* Locked */
1487 rc = CFGMR3QueryBool(pCfgHandle, "Locked", &pThis->fLocked);
1488 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1489 pThis->fLocked = false;
1490 else if (VBOX_FAILURE(rc))
1491 {
1492 AssertMsgFailed(("Configuration error: Query \"Locked\" resulted in %Vrc.\n", rc));
1493 return rc;
1494 }
1495
1496 /* BIOS visible */
1497 rc = CFGMR3QueryBool(pCfgHandle, "BIOSVisible", &pThis->fBiosVisible);
1498 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1499 pThis->fBiosVisible = true;
1500 else if (VBOX_FAILURE(rc))
1501 {
1502 AssertMsgFailed(("Configuration error: Query \"BIOSVisible\" resulted in %Vrc.\n", rc));
1503 return rc;
1504 }
1505
1506 /* Uuid */
1507 char *psz;
1508 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Uuid", &psz);
1509 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1510 RTUuidClear(&pThis->Uuid);
1511 else if (VBOX_SUCCESS(rc))
1512 {
1513 rc = RTUuidFromStr(&pThis->Uuid, psz);
1514 if (VBOX_FAILURE(rc))
1515 {
1516 AssertMsgFailed(("Configuration error: Uuid from string failed on \"%s\", rc=%Vrc.\n", psz, rc));
1517 MMR3HeapFree(psz);
1518 return rc;
1519 }
1520 MMR3HeapFree(psz);
1521 }
1522 else
1523 {
1524 AssertMsgFailed(("Configuration error: Failed to obtain the uuid, rc=%Vrc.\n", rc));
1525 return rc;
1526 }
1527
1528 /* name to open & watch for */
1529#ifdef __WIN__
1530 int iBit = toupper(pThis->pszDevice[0]) - 'A';
1531 if ( iBit > 'Z' - 'A'
1532 || pThis->pszDevice[1] != ':'
1533 || pThis->pszDevice[2])
1534 {
1535 AssertMsgFailed(("Configuration error: Invalid drive specification: '%s'\n", pThis->pszDevice));
1536 return VERR_INVALID_PARAMETER;
1537 }
1538 pThis->fUnitMask = 1 << iBit;
1539 RTStrAPrintf(&pThis->pszDeviceOpen, "\\\\.\\%s", pThis->pszDevice);
1540#else
1541 pThis->pszDeviceOpen = RTStrDup(pThis->pszDevice);
1542#endif
1543 if (!pThis->pszDeviceOpen)
1544 return VERR_NO_MEMORY;
1545
1546 return VINF_SUCCESS;
1547}
1548
1549
1550/**
1551 * Do the 2nd part of the init after the derived driver has overridden the defaults.
1552 *
1553 * On failure call DRVHostBaseDestruct().
1554 *
1555 * @returns VBox status code.
1556 * @param pThis Pointer to the instance data.
1557 */
1558int DRVHostBaseInitFinish(PDRVHOSTBASE pThis)
1559{
1560 PPDMDRVINS pDrvIns = pThis->pDrvIns;
1561
1562 /* log config summary */
1563 Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%Vuuid\n",
1564 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
1565 pThis->fReadOnlyConfig, pThis->fLocked, pThis->fBiosVisible, &pThis->Uuid));
1566
1567 /*
1568 * Check that there are no drivers below us.
1569 */
1570 PPDMIBASE pBase;
1571 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBase);
1572 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
1573 {
1574 AssertMsgFailed(("Configuration error: No attached driver, please! (rc=%Vrc)\n", rc));
1575 return VERR_PDM_DRVINS_NO_ATTACH;
1576 }
1577
1578 /*
1579 * Register saved state.
1580 */
1581 rc = pDrvIns->pDrvHlp->pfnSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, 1, 0,
1582 NULL, NULL, NULL,
1583 NULL, NULL, drvHostBaseLoadDone);
1584 if (VBOX_FAILURE(rc))
1585 return rc;
1586
1587 /*
1588 * Verify type.
1589 */
1590#ifdef __WIN__
1591 UINT uDriveType = GetDriveType(pThis->pszDevice);
1592 switch (pThis->enmType)
1593 {
1594 case PDMBLOCKTYPE_FLOPPY_360:
1595 case PDMBLOCKTYPE_FLOPPY_720:
1596 case PDMBLOCKTYPE_FLOPPY_1_20:
1597 case PDMBLOCKTYPE_FLOPPY_1_44:
1598 case PDMBLOCKTYPE_FLOPPY_2_88:
1599 if (uDriveType != DRIVE_REMOVABLE)
1600 {
1601 AssertMsgFailed(("Configuration error: '%s' is not a floppy (type=%d)\n",
1602 pThis->pszDevice, uDriveType));
1603 return VERR_INVALID_PARAMETER;
1604 }
1605 break;
1606 case PDMBLOCKTYPE_CDROM:
1607 case PDMBLOCKTYPE_DVD:
1608 if (uDriveType != DRIVE_CDROM)
1609 {
1610 AssertMsgFailed(("Configuration error: '%s' is not a cdrom (type=%d)\n",
1611 pThis->pszDevice, uDriveType));
1612 return VERR_INVALID_PARAMETER;
1613 }
1614 break;
1615 case PDMBLOCKTYPE_HARD_DISK:
1616 default:
1617 AssertMsgFailed(("enmType=%d\n", pThis->enmType));
1618 return VERR_INVALID_PARAMETER;
1619 }
1620#endif
1621
1622 /*
1623 * Open the device.
1624 */
1625#ifdef __DARWIN__
1626 rc = drvHostBaseOpen(pThis, NULL, pThis->fReadOnlyConfig);
1627#else
1628 rc = drvHostBaseReopen(pThis);
1629#endif
1630 if (VBOX_FAILURE(rc))
1631 {
1632 char *pszDevice = pThis->pszDevice;
1633#ifndef __DARWIN__
1634 char szPathReal[256];
1635 if (RT_SUCCESS(RTPathReal(pszDevice, szPathReal, sizeof(szPathReal))))
1636 pszDevice = szPathReal;
1637 pThis->FileDevice = NIL_RTFILE;
1638#endif
1639 AssertMsgFailed(("Could not open host device %s, rc=%Vrc\n", pszDevice, rc));
1640 switch (rc)
1641 {
1642 case VERR_ACCESS_DENIED:
1643 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1644#ifdef __LINUX__
1645 N_("Cannot open host device '%s' for %s access. Check the permissions "
1646 "of that device ('/bin/ls -l %s'): Most probably you need to be member "
1647 "of the device group. Make sure that you logout/login after changing "
1648 "the group settings of the current user"),
1649#else
1650 N_("Cannot open host device '%s' for %s access. Check the permissions "
1651 "of that device"),
1652#endif
1653 pszDevice, pThis->fReadOnlyConfig ? "readonly" : "read/write",
1654 pszDevice);
1655 default:
1656 return rc;
1657 }
1658 }
1659#ifdef __WIN__
1660 DRVHostBaseMediaPresent(pThis);
1661#endif
1662
1663 /*
1664 * Lock the drive if that's required by the configuration.
1665 */
1666 if (pThis->fLocked)
1667 {
1668 if (pThis->pfnDoLock)
1669 rc = pThis->pfnDoLock(pThis, true);
1670 if (VBOX_FAILURE(rc))
1671 {
1672 AssertMsgFailed(("Failed to lock the dvd drive. rc=%Vrc\n", rc));
1673 return rc;
1674 }
1675 }
1676
1677#ifndef __WIN__
1678 /*
1679 * Create the event semaphore which the poller thread will wait on.
1680 */
1681 rc = RTSemEventCreate(&pThis->EventPoller);
1682 if (VBOX_FAILURE(rc))
1683 return rc;
1684#endif
1685
1686 /*
1687 * Initialize the critical section used for serializing the access to the media.
1688 */
1689 rc = RTCritSectInit(&pThis->CritSect);
1690 if (VBOX_FAILURE(rc))
1691 return rc;
1692
1693 /*
1694 * Start the thread which will poll for the media.
1695 */
1696 rc = RTThreadCreate(&pThis->ThreadPoller, drvHostBaseMediaThread, pThis, 0,
1697 RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "DVDMEDIA");
1698 if (VBOX_FAILURE(rc))
1699 {
1700 AssertMsgFailed(("Failed to create poller thread. rc=%Vrc\n", rc));
1701 return rc;
1702 }
1703
1704 /*
1705 * Wait for the thread to start up (!w32:) and do one detection loop.
1706 */
1707 rc = RTThreadUserWait(pThis->ThreadPoller, 10000);
1708 AssertRC(rc);
1709#ifdef __WIN__
1710 if (!pThis->hwndDeviceChange)
1711 return VERR_GENERAL_FAILURE;
1712#endif
1713
1714 return rc;
1715}
1716
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette