VirtualBox

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

Last change on this file since 64369 was 64320, checked in by vboxsync, 8 years ago

Devices/Storage/DrvHost*: Start working on implementing PDMIMEDIAEX and SCSI command passthrough (wip)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.9 KB
Line 
1/* $Id: DrvHostBase.cpp 64320 2016-10-19 14:55:59Z vboxsync $ */
2/** @file
3 * DrvHostBase - Host base drive access driver.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_HOST_BASE
23
24#include <VBox/vmm/pdmdrv.h>
25#include <VBox/vmm/pdmstorageifs.h>
26#include <iprt/assert.h>
27#include <iprt/file.h>
28#include <iprt/path.h>
29#include <iprt/string.h>
30#include <iprt/thread.h>
31#include <iprt/semaphore.h>
32#include <iprt/uuid.h>
33#include <iprt/asm.h>
34#include <iprt/critsect.h>
35#include <iprt/ctype.h>
36#include <iprt/mem.h>
37
38#include "DrvHostBase.h"
39
40
41
42
43/* -=-=-=-=- IBlock -=-=-=-=- */
44
45/** @interface_method_impl{PDMIMEDIA,pfnRead} */
46static DECLCALLBACK(int) drvHostBaseRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
47{
48 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
49 LogFlow(("%s-%d: drvHostBaseRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n",
50 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbRead, pThis->pszDevice));
51 RTCritSectEnter(&pThis->CritSect);
52
53 /*
54 * Check the state.
55 */
56 int rc;
57 if (pThis->fMediaPresent)
58 {
59 /*
60 * Seek and read.
61 */
62 rc = drvHostBaseReadOs(pThis, off, pvBuf, cbRead);
63 if (RT_SUCCESS(rc))
64 {
65 Log2(("%s-%d: drvHostBaseReadOs: off=%#llx cbRead=%#x\n"
66 "%16.*Rhxd\n",
67 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
68 }
69 else
70 Log(("%s-%d: drvHostBaseRead: drvHostBaseReadOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
71 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
72 off, pvBuf, cbRead, rc, pThis->pszDevice));
73 }
74 else
75 rc = VERR_MEDIA_NOT_PRESENT;
76
77 RTCritSectLeave(&pThis->CritSect);
78 LogFlow(("%s-%d: drvHostBaseRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
79 return rc;
80}
81
82
83/** @interface_method_impl{PDMIMEDIA,pfnWrite} */
84static DECLCALLBACK(int) drvHostBaseWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
85{
86 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
87 LogFlow(("%s-%d: drvHostBaseWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n",
88 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbWrite, pThis->pszDevice));
89 Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
90 "%16.*Rhxd\n",
91 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
92 RTCritSectEnter(&pThis->CritSect);
93
94 /*
95 * Check the state.
96 */
97 int rc;
98 if (!pThis->fReadOnly)
99 {
100 if (pThis->fMediaPresent)
101 {
102 /*
103 * Seek and write.
104 */
105 rc = drvHostBaseWriteOs(pThis, off, pvBuf, cbWrite);
106 if (RT_FAILURE(rc))
107 Log(("%s-%d: drvHostBaseWrite: drvHostBaseWriteOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
108 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
109 off, pvBuf, cbWrite, rc, pThis->pszDevice));
110 }
111 else
112 rc = VERR_MEDIA_NOT_PRESENT;
113 }
114 else
115 rc = VERR_WRITE_PROTECT;
116
117 RTCritSectLeave(&pThis->CritSect);
118 LogFlow(("%s-%d: drvHostBaseWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
119 return rc;
120}
121
122
123/** @interface_method_impl{PDMIMEDIA,pfnFlush} */
124static DECLCALLBACK(int) drvHostBaseFlush(PPDMIMEDIA pInterface)
125{
126 int rc;
127 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
128 LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
129 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice));
130 RTCritSectEnter(&pThis->CritSect);
131
132 if (pThis->fMediaPresent)
133 rc = drvHostBaseFlushOs(pThis);
134 else
135 rc = VERR_MEDIA_NOT_PRESENT;
136
137 RTCritSectLeave(&pThis->CritSect);
138 LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
139 return rc;
140}
141
142
143/** @interface_method_impl{PDMIMEDIA,pfnIsReadOnly} */
144static DECLCALLBACK(bool) drvHostBaseIsReadOnly(PPDMIMEDIA pInterface)
145{
146 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
147 return pThis->fReadOnly;
148}
149
150
151/** @interface_method_impl{PDMIMEDIA,pfnIsNonRotational} */
152static DECLCALLBACK(bool) drvHostBaseIsNonRotational(PPDMIMEDIA pInterface)
153{
154 RT_NOREF1(pInterface);
155 return false;
156}
157
158
159/** @interface_method_impl{PDMIMEDIA,pfnGetSize} */
160static DECLCALLBACK(uint64_t) drvHostBaseGetSize(PPDMIMEDIA pInterface)
161{
162 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
163 RTCritSectEnter(&pThis->CritSect);
164
165 uint64_t cb = 0;
166 if (pThis->fMediaPresent)
167 cb = pThis->cbSize;
168
169 RTCritSectLeave(&pThis->CritSect);
170 LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, cb));
171 return cb;
172}
173
174
175/** @interface_method_impl{PDMIMEDIA,pfnGetType} */
176static DECLCALLBACK(PDMMEDIATYPE) drvHostBaseGetType(PPDMIMEDIA pInterface)
177{
178 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
179 LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->enmType));
180 return pThis->enmType;
181}
182
183
184/** @interface_method_impl{PDMIMEDIA,pfnGetUuid} */
185static DECLCALLBACK(int) drvHostBaseGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
186{
187 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
188
189 *pUuid = pThis->Uuid;
190
191 LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%RTuuid\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pUuid));
192 return VINF_SUCCESS;
193}
194
195
196/** @interface_method_impl{PDMIMEDIA,pfnBiosGetPCHSGeometry} */
197static DECLCALLBACK(int) drvHostBaseGetPCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
198{
199 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
200 RTCritSectEnter(&pThis->CritSect);
201
202 int rc = VINF_SUCCESS;
203 if (pThis->fMediaPresent)
204 {
205 if ( pThis->PCHSGeometry.cCylinders > 0
206 && pThis->PCHSGeometry.cHeads > 0
207 && pThis->PCHSGeometry.cSectors > 0)
208 {
209 *pPCHSGeometry = pThis->PCHSGeometry;
210 }
211 else
212 rc = VERR_PDM_GEOMETRY_NOT_SET;
213 }
214 else
215 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
216
217 RTCritSectLeave(&pThis->CritSect);
218 LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
219 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc,
220 pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors));
221 return rc;
222}
223
224
225/** @interface_method_impl{PDMIMEDIA,pfnBiosSetPCHSGeometry} */
226static DECLCALLBACK(int) drvHostBaseSetPCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
227{
228 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
229 LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
230 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__,
231 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
232 RTCritSectEnter(&pThis->CritSect);
233
234 int rc = VINF_SUCCESS;
235 if (pThis->fMediaPresent)
236 {
237 pThis->PCHSGeometry = *pPCHSGeometry;
238 }
239 else
240 {
241 AssertMsgFailed(("Invalid state! Not mounted!\n"));
242 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
243 }
244
245 RTCritSectLeave(&pThis->CritSect);
246 return rc;
247}
248
249
250/** @interface_method_impl{PDMIMEDIA,pfnBiosGetLCHSGeometry} */
251static DECLCALLBACK(int) drvHostBaseGetLCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
252{
253 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
254 RTCritSectEnter(&pThis->CritSect);
255
256 int rc = VINF_SUCCESS;
257 if (pThis->fMediaPresent)
258 {
259 if ( pThis->LCHSGeometry.cCylinders > 0
260 && pThis->LCHSGeometry.cHeads > 0
261 && pThis->LCHSGeometry.cSectors > 0)
262 {
263 *pLCHSGeometry = pThis->LCHSGeometry;
264 }
265 else
266 rc = VERR_PDM_GEOMETRY_NOT_SET;
267 }
268 else
269 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
270
271 RTCritSectLeave(&pThis->CritSect);
272 LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
273 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc,
274 pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors));
275 return rc;
276}
277
278
279/** @interface_method_impl{PDMIMEDIA,pfnBiosSetLCHSGeometry} */
280static DECLCALLBACK(int) drvHostBaseSetLCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
281{
282 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
283 LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
284 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__,
285 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
286 RTCritSectEnter(&pThis->CritSect);
287
288 int rc = VINF_SUCCESS;
289 if (pThis->fMediaPresent)
290 {
291 pThis->LCHSGeometry = *pLCHSGeometry;
292 }
293 else
294 {
295 AssertMsgFailed(("Invalid state! Not mounted!\n"));
296 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
297 }
298
299 RTCritSectLeave(&pThis->CritSect);
300 return rc;
301}
302
303
304/** @interface_method_impl{PDMIMEDIA,pfnBiosIsVisible} */
305static DECLCALLBACK(bool) drvHostBaseIsVisible(PPDMIMEDIA pInterface)
306{
307 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
308 return pThis->fBiosVisible;
309}
310
311
312
313/* -=-=-=-=- IMediaEx -=-=-=-=- */
314
315DECLHIDDEN(int) drvHostBaseBufferRetain(PDRVHOSTBASE pThis, PDRVHOSTBASEREQ pReq, size_t cbBuf, bool fWrite, void **ppvBuf)
316{
317 int rc = VINF_SUCCESS;
318
319 if (pThis->cbBuf < cbBuf)
320 {
321 RTMemFree(pThis->pvBuf);
322 pThis->cbBuf = 0;
323 pThis->pvBuf = RTMemAlloc(cbBuf);
324 if (pThis->pvBuf)
325 pThis->cbBuf = cbBuf;
326 else
327 rc = VERR_NO_MEMORY;
328 }
329
330 if (RT_SUCCESS(rc) && fWrite)
331 {
332 RTSGSEG Seg;
333 RTSGBUF SgBuf;
334
335 Seg.pvSeg = pThis->pvBuf;
336 Seg.cbSeg = cbBuf;
337 RTSgBufInit(&SgBuf, &Seg, 1);
338 rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, (PDMMEDIAEXIOREQ)pReq,
339 &pReq->abAlloc[0], 0, &SgBuf, cbBuf);
340 }
341
342 if (RT_SUCCESS(rc))
343 *ppvBuf = pThis->pvBuf;
344
345 return rc;
346}
347
348DECLHIDDEN(int) drvHostBaseBufferRelease(PDRVHOSTBASE pThis, PDRVHOSTBASEREQ pReq, size_t cbBuf, bool fWrite, void *pvBuf)
349{
350 int rc = VINF_SUCCESS;
351
352 if (!fWrite)
353 {
354 RTSGSEG Seg;
355 RTSGBUF SgBuf;
356
357 Seg.pvSeg = pvBuf;
358 Seg.cbSeg = cbBuf;
359 RTSgBufInit(&SgBuf, &Seg, 1);
360 rc = pThis->pDrvMediaExPort->pfnIoReqCopyFromBuf(pThis->pDrvMediaExPort, (PDMMEDIAEXIOREQ)pReq,
361 &pReq->abAlloc[0], 0, &SgBuf, cbBuf);
362 }
363
364 return rc;
365}
366
367/** @interface_method_impl{PDMIMEDIAEX,pfnQueryFeatures} */
368static DECLCALLBACK(int) drvHostBaseQueryFeatures(PPDMIMEDIAEX pInterface, uint32_t *pfFeatures)
369{
370 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
371
372 *pfFeatures = pThis->IMediaEx.pfnIoReqSendScsiCmd ? PDMIMEDIAEX_FEATURE_F_RAWSCSICMD : 0;
373 return VINF_SUCCESS;
374}
375
376/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet} */
377static DECLCALLBACK(int) drvHostBaseIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
378{
379 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
380
381 pThis->cbIoReqAlloc = RT_OFFSETOF(DRVHOSTBASEREQ, abAlloc[cbIoReqAlloc]);
382 return VINF_SUCCESS;
383}
384
385/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc} */
386static DECLCALLBACK(int) drvHostBaseIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
387 PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
388{
389 RT_NOREF2(uIoReqId, fFlags);
390
391 int rc = VINF_SUCCESS;
392 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
393 PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)RTMemAllocZ(pThis->cbIoReqAlloc);
394 if (RT_LIKELY(pReq))
395 {
396 *phIoReq = (PDMMEDIAEXIOREQ)pReq;
397 *ppvIoReqAlloc = &pReq->abAlloc[0];
398 }
399 else
400 rc = VERR_NO_MEMORY;
401
402 return rc;
403}
404
405/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree} */
406static DECLCALLBACK(int) drvHostBaseIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
407{
408 RT_NOREF1(pInterface);
409 PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
410
411 RTMemFree(pReq);
412 return VINF_SUCCESS;
413}
414
415/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryResidual} */
416static DECLCALLBACK(int) drvHostBaseIoReqQueryResidual(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbResidual)
417{
418 RT_NOREF2(pInterface, hIoReq);
419
420 *pcbResidual = 0; /** @todo: Implement. */
421 return VINF_SUCCESS;
422}
423
424/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancelAll} */
425static DECLCALLBACK(int) drvHostBaseIoReqCancelAll(PPDMIMEDIAEX pInterface)
426{
427 RT_NOREF1(pInterface);
428 return VINF_SUCCESS;
429}
430
431/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel} */
432static DECLCALLBACK(int) drvHostBaseIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
433{
434 RT_NOREF2(pInterface, uIoReqId);
435 return VERR_PDM_MEDIAEX_IOREQID_NOT_FOUND;
436}
437
438/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead} */
439static DECLCALLBACK(int) drvHostBaseIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
440{
441 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
442 PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
443 LogFlow(("%s-%d: drvHostBaseIoReqRead: off=%#llx cbRead=%#x (%s)\n",
444 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, pThis->pszDevice));
445 RTCritSectEnter(&pThis->CritSect);
446
447 /*
448 * Check the state.
449 */
450 int rc;
451 if (pThis->fMediaPresent)
452 {
453 void *pvBuf;
454 rc = drvHostBaseBufferRetain(pThis, pReq, cbRead, false, &pvBuf);
455 if (RT_SUCCESS(rc))
456 {
457 /*
458 * Seek and read.
459 */
460 rc = drvHostBaseReadOs(pThis, off, pvBuf, cbRead);
461 if (RT_SUCCESS(rc))
462 {
463 Log2(("%s-%d: drvHostBaseReadOs: off=%#llx cbRead=%#x\n"
464 "%16.*Rhxd\n",
465 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
466 }
467 else
468 Log(("%s-%d: drvHostBaseIoReqRead: drvHostBaseReadOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
469 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
470 off, pvBuf, cbRead, rc, pThis->pszDevice));
471
472 rc = drvHostBaseBufferRelease(pThis, pReq, cbRead, false, pvBuf);
473 }
474 else
475 Log(("%s-%d: drvHostBaseIoReqRead: drvHostBaseBufferRetain(%#llx, %p, %#x) -> %Rrc ('%s')\n",
476 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
477 off, pvBuf, cbRead, rc, pThis->pszDevice));
478 }
479 else
480 rc = VERR_MEDIA_NOT_PRESENT;
481
482 RTCritSectLeave(&pThis->CritSect);
483 LogFlow(("%s-%d: drvHostBaseIoReqRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
484 return rc;
485}
486
487/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite} */
488static DECLCALLBACK(int) drvHostBaseIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
489{
490 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
491 PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
492 LogFlow(("%s-%d: drvHostBaseIoReqWrite: off=%#llx cbWrite=%#x (%s)\n",
493 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, pThis->pszDevice));
494 RTCritSectEnter(&pThis->CritSect);
495
496 /*
497 * Check the state.
498 */
499 int rc;
500 if (!pThis->fReadOnly)
501 {
502 if (pThis->fMediaPresent)
503 {
504 void *pvBuf;
505 rc = drvHostBaseBufferRetain(pThis, pReq, cbWrite, true, &pvBuf);
506 if (RT_SUCCESS(rc))
507 {
508 Log2(("%s-%d: drvHostBaseIoReqWrite: off=%#llx cbWrite=%#x\n"
509 "%16.*Rhxd\n",
510 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
511 /*
512 * Seek and write.
513 */
514 rc = drvHostBaseWriteOs(pThis, off, pvBuf, cbWrite);
515 if (RT_FAILURE(rc))
516 Log(("%s-%d: drvHostBaseIoReqWrite: drvHostBaseWriteOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
517 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
518 off, pvBuf, cbWrite, rc, pThis->pszDevice));
519
520 rc = drvHostBaseBufferRelease(pThis, pReq, cbWrite, true, pvBuf);
521 }
522 }
523 else
524 rc = VERR_MEDIA_NOT_PRESENT;
525 }
526 else
527 rc = VERR_WRITE_PROTECT;
528
529 RTCritSectLeave(&pThis->CritSect);
530 LogFlow(("%s-%d: drvHostBaseIoReqWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
531 return rc;
532}
533
534/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush} */
535static DECLCALLBACK(int) drvHostBaseIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
536{
537 RT_NOREF1(hIoReq);
538
539 int rc;
540 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
541 LogFlow(("%s-%d: drvHostBaseIoReqFlush: (%s)\n",
542 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice));
543 RTCritSectEnter(&pThis->CritSect);
544
545 if (pThis->fMediaPresent)
546 rc = drvHostBaseFlushOs(pThis);
547 else
548 rc = VERR_MEDIA_NOT_PRESENT;
549
550 RTCritSectLeave(&pThis->CritSect);
551 LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
552 return rc;
553}
554
555/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard} */
556static DECLCALLBACK(int) drvHostBaseIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
557{
558 RT_NOREF3(pInterface, hIoReq, cRangesMax);
559 return VERR_NOT_SUPPORTED;
560}
561
562/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount} */
563static DECLCALLBACK(uint32_t) drvHostBaseIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
564{
565 RT_NOREF1(pInterface);
566 return 0;
567}
568
569/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount} */
570static DECLCALLBACK(uint32_t) drvHostBaseIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
571{
572 RT_NOREF1(pInterface);
573 return 0;
574}
575
576/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart} */
577static DECLCALLBACK(int) drvHostBaseIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc)
578{
579 RT_NOREF3(pInterface, phIoReq, ppvIoReqAlloc);
580 return VERR_NOT_IMPLEMENTED;
581}
582
583/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext} */
584static DECLCALLBACK(int) drvHostBaseIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
585 PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
586{
587 RT_NOREF4(pInterface, hIoReq, phIoReqNext, ppvIoReqAllocNext);
588 return VERR_NOT_IMPLEMENTED;
589}
590
591/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave} */
592static DECLCALLBACK(int) drvHostBaseIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
593{
594 RT_NOREF3(pInterface, pSSM, hIoReq);
595 return VERR_NOT_IMPLEMENTED;
596}
597
598/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad} */
599static DECLCALLBACK(int) drvHostBaseIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
600{
601 RT_NOREF3(pInterface, pSSM, hIoReq);
602 return VERR_NOT_IMPLEMENTED;
603}
604
605
606
607/* -=-=-=-=- IMount -=-=-=-=- */
608
609/** @interface_method_impl{PDMIMOUNT,pfnUnmount} */
610static DECLCALLBACK(int) drvHostBaseUnmount(PPDMIMOUNT pInterface, bool fForce, bool fEject)
611{
612 RT_NOREF(fEject);
613 /* While we're not mountable (see drvHostBaseMount), we're unmountable. */
614 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
615 RTCritSectEnter(&pThis->CritSect);
616
617 /*
618 * Validate state.
619 */
620 int rc = VINF_SUCCESS;
621 if (!pThis->fLocked || fForce)
622 {
623 /* Unlock drive if necessary. */
624 if (pThis->fLocked)
625 {
626 if (pThis->pfnDoLock)
627 rc = pThis->pfnDoLock(pThis, false);
628 if (RT_SUCCESS(rc))
629 pThis->fLocked = false;
630 }
631
632 if (fEject)
633 {
634 /*
635 * Eject the disc.
636 */
637 rc = drvHostBaseEjectOs(pThis);
638 }
639
640 /*
641 * Media is no longer present.
642 */
643 DRVHostBaseMediaNotPresent(pThis);
644 }
645 else
646 {
647 Log(("drvHostBaseUnmount: Locked\n"));
648 rc = VERR_PDM_MEDIA_LOCKED;
649 }
650
651 RTCritSectLeave(&pThis->CritSect);
652 LogFlow(("drvHostBaseUnmount: returns %Rrc\n", rc));
653 return rc;
654}
655
656
657/** @interface_method_impl{PDMIMOUNT,pfnIsMounted} */
658static DECLCALLBACK(bool) drvHostBaseIsMounted(PPDMIMOUNT pInterface)
659{
660 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
661 RTCritSectEnter(&pThis->CritSect);
662
663 bool fRc = pThis->fMediaPresent;
664
665 RTCritSectLeave(&pThis->CritSect);
666 return fRc;
667}
668
669
670/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
671static DECLCALLBACK(int) drvHostBaseLock(PPDMIMOUNT pInterface)
672{
673 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
674 RTCritSectEnter(&pThis->CritSect);
675
676 int rc = VINF_SUCCESS;
677 if (!pThis->fLocked)
678 {
679 if (pThis->pfnDoLock)
680 rc = pThis->pfnDoLock(pThis, true);
681 if (RT_SUCCESS(rc))
682 pThis->fLocked = true;
683 }
684 else
685 LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
686
687 RTCritSectLeave(&pThis->CritSect);
688 LogFlow(("%s-%d: drvHostBaseLock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
689 return rc;
690}
691
692
693/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
694static DECLCALLBACK(int) drvHostBaseUnlock(PPDMIMOUNT pInterface)
695{
696 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
697 RTCritSectEnter(&pThis->CritSect);
698
699 int rc = VINF_SUCCESS;
700 if (pThis->fLocked)
701 {
702 if (pThis->pfnDoLock)
703 rc = pThis->pfnDoLock(pThis, false);
704 if (RT_SUCCESS(rc))
705 pThis->fLocked = false;
706 }
707 else
708 LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
709
710 RTCritSectLeave(&pThis->CritSect);
711 LogFlow(("%s-%d: drvHostBaseUnlock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
712 return rc;
713}
714
715
716/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
717static DECLCALLBACK(bool) drvHostBaseIsLocked(PPDMIMOUNT pInterface)
718{
719 PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
720 RTCritSectEnter(&pThis->CritSect);
721
722 bool fRc = pThis->fLocked;
723
724 RTCritSectLeave(&pThis->CritSect);
725 return fRc;
726}
727
728
729/* -=-=-=-=- IBase -=-=-=-=- */
730
731/**
732 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
733 */
734static DECLCALLBACK(void *) drvHostBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
735{
736 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
737 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
738
739 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
740 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
741 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUNT, &pThis->IMount);
742 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, pThis->pDrvMediaExPort ? &pThis->IMediaEx : NULL);
743 return NULL;
744}
745
746
747/* -=-=-=-=- poller thread -=-=-=-=- */
748
749
750/**
751 * Media present.
752 * Query the size and notify the above driver / device.
753 *
754 * @param pThis The instance data.
755 */
756DECLHIDDEN(int) DRVHostBaseMediaPresent(PDRVHOSTBASE pThis)
757{
758 /*
759 * Open the drive.
760 */
761 int rc = drvHostBaseMediaRefreshOs(pThis);
762 if (RT_FAILURE(rc))
763 return rc;
764
765 /*
766 * Determine the size.
767 */
768 uint64_t cb;
769 rc = drvHostBaseGetMediaSizeOs(pThis, &cb);
770 if (RT_FAILURE(rc))
771 {
772 LogFlow(("%s-%d: failed to figure media size of %s, rc=%Rrc\n",
773 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
774 return rc;
775 }
776
777 /*
778 * Update the data and inform the unit.
779 */
780 pThis->cbSize = cb;
781 pThis->fMediaPresent = true;
782 if (pThis->pDrvMountNotify)
783 pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
784 LogFlow(("%s-%d: drvHostBaseMediaPresent: cbSize=%lld (%#llx)\n",
785 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->cbSize, pThis->cbSize));
786 return VINF_SUCCESS;
787}
788
789
790/**
791 * Media no longer present.
792 * @param pThis The instance data.
793 */
794DECLHIDDEN(void) DRVHostBaseMediaNotPresent(PDRVHOSTBASE pThis)
795{
796 pThis->fMediaPresent = false;
797 pThis->fLocked = false;
798 pThis->PCHSGeometry.cCylinders = 0;
799 pThis->PCHSGeometry.cHeads = 0;
800 pThis->PCHSGeometry.cSectors = 0;
801 pThis->LCHSGeometry.cCylinders = 0;
802 pThis->LCHSGeometry.cHeads = 0;
803 pThis->LCHSGeometry.cSectors = 0;
804 if (pThis->pDrvMountNotify)
805 pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
806}
807
808
809static int drvHostBaseMediaPoll(PDRVHOSTBASE pThis)
810{
811 /*
812 * Poll for media change.
813 */
814 bool fMediaPresent = false;
815 bool fMediaChanged = false;
816 drvHostBaseQueryMediaStatusOs(pThis, &fMediaChanged, &fMediaPresent);
817
818 RTCritSectEnter(&pThis->CritSect);
819
820 int rc = VINF_SUCCESS;
821 if (pThis->fMediaPresent != fMediaPresent)
822 {
823 LogFlow(("drvHostDvdPoll: %d -> %d\n", pThis->fMediaPresent, fMediaPresent));
824 pThis->fMediaPresent = false;
825 if (fMediaPresent)
826 rc = DRVHostBaseMediaPresent(pThis);
827 else
828 DRVHostBaseMediaNotPresent(pThis);
829 }
830 else if (fMediaPresent)
831 {
832 /*
833 * Poll for media change.
834 */
835 if (fMediaChanged)
836 {
837 LogFlow(("drvHostDVDMediaThread: Media changed!\n"));
838 DRVHostBaseMediaNotPresent(pThis);
839 rc = DRVHostBaseMediaPresent(pThis);
840 }
841 }
842
843 RTCritSectLeave(&pThis->CritSect);
844 return rc;
845}
846
847
848/**
849 * This thread will periodically poll the device for media presence.
850 *
851 * @returns Ignored.
852 * @param ThreadSelf Handle of this thread. Ignored.
853 * @param pvUser Pointer to the driver instance structure.
854 */
855static DECLCALLBACK(int) drvHostBaseMediaThread(RTTHREAD ThreadSelf, void *pvUser)
856{
857 PDRVHOSTBASE pThis = (PDRVHOSTBASE)pvUser;
858 LogFlow(("%s-%d: drvHostBaseMediaThread: ThreadSelf=%p pvUser=%p\n",
859 pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, ThreadSelf, pvUser));
860 bool fFirst = true;
861 int cRetries = 10;
862 while (!pThis->fShutdownPoller)
863 {
864 /*
865 * Perform the polling (unless we've run out of 50ms retries).
866 */
867 if (cRetries-- > 0)
868 {
869
870 int rc = drvHostBaseMediaPoll(pThis);
871 if (RT_FAILURE(rc))
872 {
873 RTSemEventWait(pThis->EventPoller, 50);
874 continue;
875 }
876 }
877
878 /*
879 * Signal EMT after the first go.
880 */
881 if (fFirst)
882 {
883 RTThreadUserSignal(ThreadSelf);
884 fFirst = false;
885 }
886
887 /*
888 * Sleep.
889 */
890 int rc = RTSemEventWait(pThis->EventPoller, pThis->cMilliesPoller);
891 if ( RT_FAILURE(rc)
892 && rc != VERR_TIMEOUT)
893 {
894 AssertMsgFailed(("rc=%Rrc\n", rc));
895 pThis->ThreadPoller = NIL_RTTHREAD;
896 LogFlow(("drvHostBaseMediaThread: returns %Rrc\n", rc));
897 return rc;
898 }
899 cRetries = 10;
900 }
901
902 /* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
903 LogFlow(("%s-%d: drvHostBaseMediaThread: returns VINF_SUCCESS\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
904 return VINF_SUCCESS;
905}
906
907/* -=-=-=-=- driver interface -=-=-=-=- */
908
909
910/**
911 * Done state load operation.
912 *
913 * @returns VBox load code.
914 * @param pDrvIns Driver instance of the driver which registered the data unit.
915 * @param pSSM SSM operation handle.
916 */
917static DECLCALLBACK(int) drvHostBaseLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
918{
919 RT_NOREF(pSSM);
920 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
921 LogFlow(("%s-%d: drvHostBaseMediaThread:\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
922 RTCritSectEnter(&pThis->CritSect);
923
924 /*
925 * Tell the device/driver above us that the media status is uncertain.
926 */
927 if (pThis->pDrvMountNotify)
928 {
929 pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
930 if (pThis->fMediaPresent)
931 pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
932 }
933
934 RTCritSectLeave(&pThis->CritSect);
935 return VINF_SUCCESS;
936}
937
938
939/** @copydoc FNPDMDRVDESTRUCT */
940DECLCALLBACK(void) DRVHostBaseDestruct(PPDMDRVINS pDrvIns)
941{
942 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
943 LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
944
945 /*
946 * Terminate the thread.
947 */
948 if (pThis->ThreadPoller != NIL_RTTHREAD)
949 {
950 pThis->fShutdownPoller = true;
951 int rc;
952 int cTimes = 50;
953 do
954 {
955 RTSemEventSignal(pThis->EventPoller);
956 rc = RTThreadWait(pThis->ThreadPoller, 100, NULL);
957 } while (cTimes-- > 0 && rc == VERR_TIMEOUT);
958
959 if (!rc)
960 pThis->ThreadPoller = NIL_RTTHREAD;
961 }
962
963 /*
964 * Cleanup the other resources.
965 */
966 drvHostBaseDestructOs(pThis);
967
968 if (pThis->EventPoller != NULL)
969 {
970 RTSemEventDestroy(pThis->EventPoller);
971 pThis->EventPoller = NULL;
972 }
973
974 if (pThis->pszDevice)
975 {
976 MMR3HeapFree(pThis->pszDevice);
977 pThis->pszDevice = NULL;
978 }
979
980 if (pThis->pszDeviceOpen)
981 {
982 RTStrFree(pThis->pszDeviceOpen);
983 pThis->pszDeviceOpen = NULL;
984 }
985
986 if (pThis->pvBuf)
987 {
988 RTMemFree(pThis->pvBuf);
989 pThis->pvBuf = NULL;
990 pThis->cbBuf = 0;
991 }
992
993 /* Forget about the notifications. */
994 pThis->pDrvMountNotify = NULL;
995
996 /* Leave the instance operational if this is just a cleanup of the state
997 * after an attach error happened. So don't destroy the critsect then. */
998 if (!pThis->fKeepInstance && RTCritSectIsInitialized(&pThis->CritSect))
999 RTCritSectDelete(&pThis->CritSect);
1000 LogFlow(("%s-%d: drvHostBaseDestruct completed\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
1001}
1002
1003
1004/**
1005 * Initializes the instance data .
1006 *
1007 * On failure call DRVHostBaseDestruct().
1008 *
1009 * @returns VBox status code.
1010 * @param pDrvIns Driver instance.
1011 * @param pszCfgValid Pointer to a string of valid CFGM options.
1012 * @param pCfg Configuration handle.
1013 * @param enmType Device type.
1014 */
1015DECLHIDDEN(int) DRVHostBaseInit(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, const char *pszCfgValid, PDMMEDIATYPE enmType)
1016{
1017 int src = VINF_SUCCESS;
1018 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
1019 LogFlow(("%s-%d: DRVHostBaseInit: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
1020
1021 /*
1022 * Initialize most of the data members.
1023 */
1024 pThis->pDrvIns = pDrvIns;
1025 pThis->fKeepInstance = false;
1026 pThis->ThreadPoller = NIL_RTTHREAD;
1027 pThis->enmType = enmType;
1028 pThis->fAttachFailError = true; /* It's an error until we've read the config. */
1029
1030 /* IBase. */
1031 pDrvIns->IBase.pfnQueryInterface = drvHostBaseQueryInterface;
1032
1033 /* IMedia. */
1034 pThis->IMedia.pfnRead = drvHostBaseRead;
1035 pThis->IMedia.pfnWrite = drvHostBaseWrite;
1036 pThis->IMedia.pfnFlush = drvHostBaseFlush;
1037 pThis->IMedia.pfnIsReadOnly = drvHostBaseIsReadOnly;
1038 pThis->IMedia.pfnIsNonRotational = drvHostBaseIsNonRotational;
1039 pThis->IMedia.pfnGetSize = drvHostBaseGetSize;
1040 pThis->IMedia.pfnGetType = drvHostBaseGetType;
1041 pThis->IMedia.pfnGetUuid = drvHostBaseGetUuid;
1042 pThis->IMedia.pfnBiosGetPCHSGeometry = drvHostBaseGetPCHSGeometry;
1043 pThis->IMedia.pfnBiosSetPCHSGeometry = drvHostBaseSetPCHSGeometry;
1044 pThis->IMedia.pfnBiosGetLCHSGeometry = drvHostBaseGetLCHSGeometry;
1045 pThis->IMedia.pfnBiosSetLCHSGeometry = drvHostBaseSetLCHSGeometry;
1046 pThis->IMedia.pfnBiosIsVisible = drvHostBaseIsVisible;
1047
1048 /* IMediaEx */
1049 pThis->IMediaEx.pfnQueryFeatures = drvHostBaseQueryFeatures;
1050 pThis->IMediaEx.pfnIoReqAllocSizeSet = drvHostBaseIoReqAllocSizeSet;
1051 pThis->IMediaEx.pfnIoReqAlloc = drvHostBaseIoReqAlloc;
1052 pThis->IMediaEx.pfnIoReqFree = drvHostBaseIoReqFree;
1053 pThis->IMediaEx.pfnIoReqQueryResidual = drvHostBaseIoReqQueryResidual;
1054 pThis->IMediaEx.pfnIoReqCancelAll = drvHostBaseIoReqCancelAll;
1055 pThis->IMediaEx.pfnIoReqCancel = drvHostBaseIoReqCancel;
1056 pThis->IMediaEx.pfnIoReqRead = drvHostBaseIoReqRead;
1057 pThis->IMediaEx.pfnIoReqWrite = drvHostBaseIoReqWrite;
1058 pThis->IMediaEx.pfnIoReqFlush = drvHostBaseIoReqFlush;
1059 pThis->IMediaEx.pfnIoReqDiscard = drvHostBaseIoReqDiscard;
1060 pThis->IMediaEx.pfnIoReqGetActiveCount = drvHostBaseIoReqGetActiveCount;
1061 pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvHostBaseIoReqGetSuspendedCount;
1062 pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvHostBaseIoReqQuerySuspendedStart;
1063 pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvHostBaseIoReqQuerySuspendedNext;
1064 pThis->IMediaEx.pfnIoReqSuspendedSave = drvHostBaseIoReqSuspendedSave;
1065 pThis->IMediaEx.pfnIoReqSuspendedLoad = drvHostBaseIoReqSuspendedLoad;
1066
1067 /* IMount. */
1068 pThis->IMount.pfnUnmount = drvHostBaseUnmount;
1069 pThis->IMount.pfnIsMounted = drvHostBaseIsMounted;
1070 pThis->IMount.pfnLock = drvHostBaseLock;
1071 pThis->IMount.pfnUnlock = drvHostBaseUnlock;
1072 pThis->IMount.pfnIsLocked = drvHostBaseIsLocked;
1073
1074 drvHostBaseInitOs(pThis);
1075
1076 if (!CFGMR3AreValuesValid(pCfg, pszCfgValid))
1077 {
1078 pThis->fAttachFailError = true;
1079 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1080 }
1081
1082 /*
1083 * Get the IMediaPort & IMountNotify interfaces of the above driver/device.
1084 */
1085 pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1086 if (!pThis->pDrvMediaPort)
1087 {
1088 AssertMsgFailed(("Configuration error: No media port interface above!\n"));
1089 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1090 }
1091 pThis->pDrvMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
1092 pThis->pDrvMountNotify = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMOUNTNOTIFY);
1093
1094 /*
1095 * Query configuration.
1096 */
1097 /* Device */
1098 int rc = CFGMR3QueryStringAlloc(pCfg, "Path", &pThis->pszDevice);
1099 if (RT_FAILURE(rc))
1100 {
1101 AssertMsgFailed(("Configuration error: query for \"Path\" string returned %Rra.\n", rc));
1102 return rc;
1103 }
1104
1105 /* Mountable */
1106 uint32_t u32;
1107 rc = CFGMR3QueryU32Def(pCfg, "Interval", &u32, 1000);
1108 if (RT_SUCCESS(rc))
1109 pThis->cMilliesPoller = u32;
1110 else
1111 {
1112 AssertMsgFailed(("Configuration error: Query \"Mountable\" resulted in %Rrc.\n", rc));
1113 return rc;
1114 }
1115
1116 /* ReadOnly - passthrough mode requires read/write access in any case. */
1117 if ( (pThis->enmType == PDMMEDIATYPE_CDROM || pThis->enmType == PDMMEDIATYPE_DVD)
1118 && pThis->IMedia.pfnSendCmd)
1119 pThis->fReadOnlyConfig = false;
1120 else
1121 {
1122 rc = CFGMR3QueryBoolDef(pCfg, "ReadOnly", &pThis->fReadOnlyConfig,
1123 enmType == PDMMEDIATYPE_DVD || enmType == PDMMEDIATYPE_CDROM
1124 ? true
1125 : false);
1126 if (RT_FAILURE(rc))
1127 {
1128 AssertMsgFailed(("Configuration error: Query \"ReadOnly\" resulted in %Rrc.\n", rc));
1129 return rc;
1130 }
1131 }
1132
1133 /* Locked */
1134 rc = CFGMR3QueryBoolDef(pCfg, "Locked", &pThis->fLocked, false);
1135 if (RT_FAILURE(rc))
1136 {
1137 AssertMsgFailed(("Configuration error: Query \"Locked\" resulted in %Rrc.\n", rc));
1138 return rc;
1139 }
1140
1141 /* BIOS visible */
1142 rc = CFGMR3QueryBoolDef(pCfg, "BIOSVisible", &pThis->fBiosVisible, true);
1143 if (RT_FAILURE(rc))
1144 {
1145 AssertMsgFailed(("Configuration error: Query \"BIOSVisible\" resulted in %Rrc.\n", rc));
1146 return rc;
1147 }
1148
1149 /* Uuid */
1150 char *psz;
1151 rc = CFGMR3QueryStringAlloc(pCfg, "Uuid", &psz);
1152 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1153 RTUuidClear(&pThis->Uuid);
1154 else if (RT_SUCCESS(rc))
1155 {
1156 rc = RTUuidFromStr(&pThis->Uuid, psz);
1157 if (RT_FAILURE(rc))
1158 {
1159 AssertMsgFailed(("Configuration error: Uuid from string failed on \"%s\", rc=%Rrc.\n", psz, rc));
1160 MMR3HeapFree(psz);
1161 return rc;
1162 }
1163 MMR3HeapFree(psz);
1164 }
1165 else
1166 {
1167 AssertMsgFailed(("Configuration error: Failed to obtain the uuid, rc=%Rrc.\n", rc));
1168 return rc;
1169 }
1170
1171 /* Define whether attach failure is an error (default) or not. */
1172 bool fAttachFailError = true;
1173 rc = CFGMR3QueryBoolDef(pCfg, "AttachFailError", &fAttachFailError, true);
1174 pThis->fAttachFailError = fAttachFailError;
1175
1176 /* log config summary */
1177 Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%RTuuid\n",
1178 pDrvIns->pReg->szName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
1179 pThis->fReadOnlyConfig, pThis->fLocked, pThis->fBiosVisible, &pThis->Uuid));
1180
1181 /*
1182 * Check that there are no drivers below us.
1183 */
1184 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1185 ("Configuration error: Not possible to attach anything to this driver!\n"),
1186 VERR_PDM_DRVINS_NO_ATTACH);
1187
1188 /*
1189 * Register saved state.
1190 */
1191 rc = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvHostBaseLoadDone);
1192 if (RT_FAILURE(rc))
1193 return rc;
1194
1195 /*
1196 * Initialize the critical section used for serializing the access to the media.
1197 */
1198 rc = RTCritSectInit(&pThis->CritSect);
1199 if (RT_FAILURE(rc))
1200 return rc;
1201
1202 /*
1203 * Open the device.
1204 */
1205 rc = drvHostBaseOpenOs(pThis, pThis->fReadOnlyConfig);
1206 if (RT_FAILURE(rc))
1207 {
1208 char *pszDevice = pThis->pszDevice;
1209#ifndef RT_OS_DARWIN
1210 char szPathReal[256];
1211 if ( RTPathExists(pszDevice)
1212 && RT_SUCCESS(RTPathReal(pszDevice, szPathReal, sizeof(szPathReal))))
1213 pszDevice = szPathReal;
1214#endif
1215
1216 /*
1217 * Disable CD/DVD passthrough in case it was enabled. Would cause
1218 * weird failures later when the guest issues commands. These would
1219 * all fail because of the invalid file handle. So use the normal
1220 * virtual CD/DVD code, which deals more gracefully with unavailable
1221 * "media" - actually a complete drive in this case.
1222 */
1223 pThis->IMedia.pfnSendCmd = NULL;
1224 AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pszDevice, rc));
1225 switch (rc)
1226 {
1227 case VERR_ACCESS_DENIED:
1228 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1229#ifdef RT_OS_LINUX
1230 N_("Cannot open host device '%s' for %s access. Check the permissions "
1231 "of that device ('/bin/ls -l %s'): Most probably you need to be member "
1232 "of the device group. Make sure that you logout/login after changing "
1233 "the group settings of the current user"),
1234#else
1235 N_("Cannot open host device '%s' for %s access. Check the permissions "
1236 "of that device"),
1237#endif
1238 pszDevice, pThis->fReadOnlyConfig ? "readonly" : "read/write",
1239 pszDevice);
1240 default:
1241 {
1242 if (pThis->fAttachFailError)
1243 return rc;
1244 int erc = PDMDrvHlpVMSetRuntimeError(pDrvIns, 0 /*fFlags*/,
1245 "DrvHost_MOUNTFAIL",
1246 N_("Cannot attach to host device '%s'"), pszDevice);
1247 AssertRC(erc);
1248 src = rc;
1249 }
1250 }
1251 }
1252
1253 /*
1254 * Lock the drive if that's required by the configuration.
1255 */
1256 if (pThis->fLocked)
1257 {
1258 if (pThis->pfnDoLock)
1259 rc = pThis->pfnDoLock(pThis, true);
1260 if (RT_FAILURE(rc))
1261 {
1262 AssertMsgFailed(("Failed to lock the dvd drive. rc=%Rrc\n", rc));
1263 return rc;
1264 }
1265 }
1266
1267 if (RT_SUCCESS(src) && drvHostBaseIsMediaPollingRequiredOs(pThis))
1268 {
1269 /*
1270 * Create the event semaphore which the poller thread will wait on.
1271 */
1272 rc = RTSemEventCreate(&pThis->EventPoller);
1273 if (RT_FAILURE(rc))
1274 return rc;
1275
1276 /*
1277 * Start the thread which will poll for the media.
1278 */
1279 rc = RTThreadCreate(&pThis->ThreadPoller, drvHostBaseMediaThread, pThis, 0,
1280 RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "DVDMEDIA");
1281 if (RT_FAILURE(rc))
1282 {
1283 AssertMsgFailed(("Failed to create poller thread. rc=%Rrc\n", rc));
1284 return rc;
1285 }
1286
1287 /*
1288 * Wait for the thread to start up (!w32:) and do one detection loop.
1289 */
1290 rc = RTThreadUserWait(pThis->ThreadPoller, 10000);
1291 AssertRC(rc);
1292 }
1293
1294 if (RT_FAILURE(rc))
1295 {
1296 if (!pThis->fAttachFailError)
1297 {
1298 /* Suppressing the attach failure error must not affect the normal
1299 * DRVHostBaseDestruct, so reset this flag below before leaving. */
1300 pThis->fKeepInstance = true;
1301 rc = VINF_SUCCESS;
1302 }
1303 DRVHostBaseDestruct(pDrvIns);
1304 pThis->fKeepInstance = false;
1305 }
1306
1307 if (RT_FAILURE(src))
1308 return src;
1309
1310 return rc;
1311}
1312
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