1 | /* $Id: DrvHostBase.cpp 63482 2016-08-15 14:24:48Z 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 | #ifdef RT_OS_DARWIN
|
---|
24 | # include <mach/mach.h>
|
---|
25 | # include <Carbon/Carbon.h>
|
---|
26 | # include <IOKit/IOKitLib.h>
|
---|
27 | # include <IOKit/storage/IOStorageDeviceCharacteristics.h>
|
---|
28 | # include <IOKit/scsi/SCSITaskLib.h>
|
---|
29 | # include <IOKit/scsi/SCSICommandOperationCodes.h>
|
---|
30 | # include <IOKit/IOBSD.h>
|
---|
31 | # include <DiskArbitration/DiskArbitration.h>
|
---|
32 | # include <mach/mach_error.h>
|
---|
33 | # include <VBox/scsi.h>
|
---|
34 |
|
---|
35 | #elif defined(RT_OS_L4)
|
---|
36 | /* Nothing special requires... yeah, right. */
|
---|
37 |
|
---|
38 | #elif defined(RT_OS_LINUX)
|
---|
39 | # include <sys/ioctl.h>
|
---|
40 | # include <sys/fcntl.h>
|
---|
41 | # include <errno.h>
|
---|
42 |
|
---|
43 | #elif defined(RT_OS_SOLARIS)
|
---|
44 | # include <fcntl.h>
|
---|
45 | # include <errno.h>
|
---|
46 | # include <stropts.h>
|
---|
47 | # include <malloc.h>
|
---|
48 | # include <sys/dkio.h>
|
---|
49 | extern "C" char *getfullblkname(char *);
|
---|
50 |
|
---|
51 | #elif defined(RT_OS_WINDOWS)
|
---|
52 | # define WIN32_NO_STATUS
|
---|
53 | # include <iprt/win/windows.h>
|
---|
54 | # include <dbt.h>
|
---|
55 | # undef WIN32_NO_STATUS
|
---|
56 | # include <ntstatus.h>
|
---|
57 |
|
---|
58 | /* from ntdef.h */
|
---|
59 | typedef LONG NTSTATUS;
|
---|
60 |
|
---|
61 | /* from ntddk.h */
|
---|
62 | typedef struct _IO_STATUS_BLOCK {
|
---|
63 | union {
|
---|
64 | NTSTATUS Status;
|
---|
65 | PVOID Pointer;
|
---|
66 | };
|
---|
67 | ULONG_PTR Information;
|
---|
68 | } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
|
---|
69 |
|
---|
70 |
|
---|
71 | /* from ntinternals.com */
|
---|
72 | typedef enum _FS_INFORMATION_CLASS {
|
---|
73 | FileFsVolumeInformation=1,
|
---|
74 | FileFsLabelInformation,
|
---|
75 | FileFsSizeInformation,
|
---|
76 | FileFsDeviceInformation,
|
---|
77 | FileFsAttributeInformation,
|
---|
78 | FileFsControlInformation,
|
---|
79 | FileFsFullSizeInformation,
|
---|
80 | FileFsObjectIdInformation,
|
---|
81 | FileFsMaximumInformation
|
---|
82 | } FS_INFORMATION_CLASS, *PFS_INFORMATION_CLASS;
|
---|
83 |
|
---|
84 | typedef struct _FILE_FS_SIZE_INFORMATION {
|
---|
85 | LARGE_INTEGER TotalAllocationUnits;
|
---|
86 | LARGE_INTEGER AvailableAllocationUnits;
|
---|
87 | ULONG SectorsPerAllocationUnit;
|
---|
88 | ULONG BytesPerSector;
|
---|
89 | } FILE_FS_SIZE_INFORMATION, *PFILE_FS_SIZE_INFORMATION;
|
---|
90 |
|
---|
91 | extern "C"
|
---|
92 | NTSTATUS __stdcall NtQueryVolumeInformationFile(
|
---|
93 | /*IN*/ HANDLE FileHandle,
|
---|
94 | /*OUT*/ PIO_STATUS_BLOCK IoStatusBlock,
|
---|
95 | /*OUT*/ PVOID FileSystemInformation,
|
---|
96 | /*IN*/ ULONG Length,
|
---|
97 | /*IN*/ FS_INFORMATION_CLASS FileSystemInformationClass );
|
---|
98 |
|
---|
99 | #elif defined(RT_OS_FREEBSD)
|
---|
100 | # include <sys/cdefs.h>
|
---|
101 | # include <sys/param.h>
|
---|
102 | # include <errno.h>
|
---|
103 | # include <stdio.h>
|
---|
104 | # include <cam/cam.h>
|
---|
105 | # include <cam/cam_ccb.h>
|
---|
106 | # include <cam/scsi/scsi_message.h>
|
---|
107 | # include <cam/scsi/scsi_pass.h>
|
---|
108 | # include <VBox/scsi.h>
|
---|
109 | # include <iprt/log.h>
|
---|
110 | #else
|
---|
111 | # error "Unsupported Platform."
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | #include <VBox/vmm/pdmdrv.h>
|
---|
115 | #include <VBox/vmm/pdmstorageifs.h>
|
---|
116 | #include <iprt/assert.h>
|
---|
117 | #include <iprt/file.h>
|
---|
118 | #include <iprt/path.h>
|
---|
119 | #include <iprt/string.h>
|
---|
120 | #include <iprt/thread.h>
|
---|
121 | #include <iprt/semaphore.h>
|
---|
122 | #include <iprt/uuid.h>
|
---|
123 | #include <iprt/asm.h>
|
---|
124 | #include <iprt/critsect.h>
|
---|
125 | #include <iprt/ctype.h>
|
---|
126 | #include <iprt/mem.h>
|
---|
127 |
|
---|
128 | #include "DrvHostBase.h"
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | /* -=-=-=-=- IBlock -=-=-=-=- */
|
---|
134 |
|
---|
135 | /** @interface_method_impl{PDMIBLOCK,pfnRead} */
|
---|
136 | static DECLCALLBACK(int) drvHostBaseRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
137 | {
|
---|
138 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
139 | LogFlow(("%s-%d: drvHostBaseRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n",
|
---|
140 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbRead, pThis->pszDevice));
|
---|
141 | RTCritSectEnter(&pThis->CritSect);
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Check the state.
|
---|
145 | */
|
---|
146 | int rc;
|
---|
147 | #ifdef RT_OS_DARWIN
|
---|
148 | if ( pThis->fMediaPresent
|
---|
149 | && pThis->ppScsiTaskDI
|
---|
150 | && pThis->cbBlock)
|
---|
151 | #elif defined(RT_OS_FREEBSD)
|
---|
152 | if ( pThis->fMediaPresent
|
---|
153 | && pThis->cbBlock)
|
---|
154 | #else
|
---|
155 | if (pThis->fMediaPresent)
|
---|
156 | #endif
|
---|
157 | {
|
---|
158 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
159 | /*
|
---|
160 | * Issue a READ(12) request.
|
---|
161 | */
|
---|
162 | do
|
---|
163 | {
|
---|
164 | const uint32_t LBA = off / pThis->cbBlock;
|
---|
165 | AssertReturn(!(off % pThis->cbBlock), VERR_INVALID_PARAMETER);
|
---|
166 | uint32_t cbRead32 = cbRead > SCSI_MAX_BUFFER_SIZE
|
---|
167 | ? SCSI_MAX_BUFFER_SIZE
|
---|
168 | : (uint32_t)cbRead;
|
---|
169 | const uint32_t cBlocks = cbRead32 / pThis->cbBlock;
|
---|
170 | AssertReturn(!(cbRead % pThis->cbBlock), VERR_INVALID_PARAMETER);
|
---|
171 | uint8_t abCmd[16] =
|
---|
172 | {
|
---|
173 | SCSI_READ_12, 0,
|
---|
174 | RT_BYTE4(LBA), RT_BYTE3(LBA), RT_BYTE2(LBA), RT_BYTE1(LBA),
|
---|
175 | RT_BYTE4(cBlocks), RT_BYTE3(cBlocks), RT_BYTE2(cBlocks), RT_BYTE1(cBlocks),
|
---|
176 | 0, 0, 0, 0, 0
|
---|
177 | };
|
---|
178 | rc = DRVHostBaseScsiCmd(pThis, abCmd, 12, PDMMEDIATXDIR_FROM_DEVICE, pvBuf, &cbRead32, NULL, 0, 0);
|
---|
179 |
|
---|
180 | off += cbRead32;
|
---|
181 | cbRead -= cbRead32;
|
---|
182 | pvBuf = (uint8_t *)pvBuf + cbRead32;
|
---|
183 | } while ((cbRead > 0) && RT_SUCCESS(rc));
|
---|
184 |
|
---|
185 | #else
|
---|
186 | /*
|
---|
187 | * Seek and read.
|
---|
188 | */
|
---|
189 | rc = RTFileReadAt(pThis->hFileDevice, off, pvBuf, cbRead, NULL);
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | Log2(("%s-%d: drvHostBaseRead: off=%#llx cbRead=%#x\n"
|
---|
193 | "%16.*Rhxd\n",
|
---|
194 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
|
---|
195 | }
|
---|
196 | else
|
---|
197 | Log(("%s-%d: drvHostBaseRead: RTFileReadAt(%RTfile, %#llx, %p, %#x) -> %Rrc ('%s')\n",
|
---|
198 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->hFileDevice,
|
---|
199 | off, pvBuf, cbRead, rc, pThis->pszDevice));
|
---|
200 | #endif
|
---|
201 | }
|
---|
202 | else
|
---|
203 | rc = VERR_MEDIA_NOT_PRESENT;
|
---|
204 |
|
---|
205 | RTCritSectLeave(&pThis->CritSect);
|
---|
206 | LogFlow(("%s-%d: drvHostBaseRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | /** @interface_method_impl{PDMIBLOCK,pfnWrite} */
|
---|
212 | static DECLCALLBACK(int) drvHostBaseWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
213 | {
|
---|
214 | RT_NOREF(off, pvBuf, cbWrite);
|
---|
215 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
216 | LogFlow(("%s-%d: drvHostBaseWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n",
|
---|
217 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbWrite, pThis->pszDevice));
|
---|
218 | Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
|
---|
219 | "%16.*Rhxd\n",
|
---|
220 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
|
---|
221 | RTCritSectEnter(&pThis->CritSect);
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Check the state.
|
---|
225 | */
|
---|
226 | int rc;
|
---|
227 | if (!pThis->fReadOnly)
|
---|
228 | {
|
---|
229 | if (pThis->fMediaPresent)
|
---|
230 | {
|
---|
231 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
232 | /** @todo write support... */
|
---|
233 | rc = VERR_WRITE_PROTECT;
|
---|
234 |
|
---|
235 | #else
|
---|
236 | /*
|
---|
237 | * Seek and write.
|
---|
238 | */
|
---|
239 | rc = RTFileWriteAt(pThis->hFileDevice, off, pvBuf, cbWrite, NULL);
|
---|
240 | if (RT_FAILURE(rc))
|
---|
241 | Log(("%s-%d: drvHostBaseWrite: RTFileWriteAt(%RTfile, %#llx, %p, %#x) -> %Rrc ('%s')\n",
|
---|
242 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->hFileDevice,
|
---|
243 | off, pvBuf, cbWrite, rc, pThis->pszDevice));
|
---|
244 | #endif
|
---|
245 | }
|
---|
246 | else
|
---|
247 | rc = VERR_MEDIA_NOT_PRESENT;
|
---|
248 | }
|
---|
249 | else
|
---|
250 | rc = VERR_WRITE_PROTECT;
|
---|
251 |
|
---|
252 | RTCritSectLeave(&pThis->CritSect);
|
---|
253 | LogFlow(("%s-%d: drvHostBaseWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /** @interface_method_impl{PDMIBLOCK,pfnFlush} */
|
---|
259 | static DECLCALLBACK(int) drvHostBaseFlush(PPDMIMEDIA pInterface)
|
---|
260 | {
|
---|
261 | int rc;
|
---|
262 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
263 | LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
|
---|
264 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice));
|
---|
265 | RTCritSectEnter(&pThis->CritSect);
|
---|
266 |
|
---|
267 | if (pThis->fMediaPresent)
|
---|
268 | {
|
---|
269 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
270 | rc = VINF_SUCCESS;
|
---|
271 | /** @todo scsi device buffer flush... */
|
---|
272 | #else
|
---|
273 | rc = RTFileFlush(pThis->hFileDevice);
|
---|
274 | #endif
|
---|
275 | }
|
---|
276 | else
|
---|
277 | rc = VERR_MEDIA_NOT_PRESENT;
|
---|
278 |
|
---|
279 | RTCritSectLeave(&pThis->CritSect);
|
---|
280 | LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
|
---|
281 | return rc;
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | /** @interface_method_impl{PDMIBLOCK,pfnIsReadOnly} */
|
---|
286 | static DECLCALLBACK(bool) drvHostBaseIsReadOnly(PPDMIMEDIA pInterface)
|
---|
287 | {
|
---|
288 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
289 | return pThis->fReadOnly;
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | /** @interface_method_impl{PDMIBLOCK,pfnGetSize} */
|
---|
294 | static DECLCALLBACK(uint64_t) drvHostBaseGetSize(PPDMIMEDIA pInterface)
|
---|
295 | {
|
---|
296 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
297 | RTCritSectEnter(&pThis->CritSect);
|
---|
298 |
|
---|
299 | uint64_t cb = 0;
|
---|
300 | if (pThis->fMediaPresent)
|
---|
301 | cb = pThis->cbSize;
|
---|
302 |
|
---|
303 | RTCritSectLeave(&pThis->CritSect);
|
---|
304 | LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, cb));
|
---|
305 | return cb;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /** @interface_method_impl{PDMIBLOCK,pfnGetType} */
|
---|
310 | static DECLCALLBACK(PDMMEDIATYPE) drvHostBaseGetType(PPDMIMEDIA pInterface)
|
---|
311 | {
|
---|
312 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
313 | LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->enmType));
|
---|
314 | return pThis->enmType;
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | /** @interface_method_impl{PDMIBLOCK,pfnGetUuid} */
|
---|
319 | static DECLCALLBACK(int) drvHostBaseGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
|
---|
320 | {
|
---|
321 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
322 |
|
---|
323 | *pUuid = pThis->Uuid;
|
---|
324 |
|
---|
325 | LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%RTuuid\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pUuid));
|
---|
326 | return VINF_SUCCESS;
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | /** @interface_method_impl{PDMIBLOCK,pfnIoBufAlloc} */
|
---|
331 | static DECLCALLBACK(int) drvHostBaseIoBufAlloc(PPDMIMEDIA pInterface, size_t cb, void **ppvNew)
|
---|
332 | {
|
---|
333 | RT_NOREF(pInterface);
|
---|
334 | LogFlowFunc(("\n"));
|
---|
335 |
|
---|
336 | int rc;
|
---|
337 | void *pvNew = RTMemAlloc(cb);
|
---|
338 | if (RT_LIKELY(pvNew))
|
---|
339 | {
|
---|
340 | *ppvNew = pvNew;
|
---|
341 | rc = VINF_SUCCESS;
|
---|
342 | }
|
---|
343 | else
|
---|
344 | rc = VERR_NO_MEMORY;
|
---|
345 |
|
---|
346 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** @interface_method_impl{PDMIBLOCK,pfnIoBufFree} */
|
---|
351 | static DECLCALLBACK(int) drvHostBaseIoBufFree(PPDMIMEDIA pInterface, void *pv, size_t cb)
|
---|
352 | {
|
---|
353 | RT_NOREF(pInterface, cb);
|
---|
354 | LogFlowFunc(("\n"));
|
---|
355 |
|
---|
356 | RTMemFree(pv);
|
---|
357 |
|
---|
358 | LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
|
---|
359 | return VINF_SUCCESS;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | /** @interface_method_impl{PDMIBLOCKBIOS,pfnBiosGetPCHSGeometry} */
|
---|
364 | static DECLCALLBACK(int) drvHostBaseGetPCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
365 | {
|
---|
366 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
367 | RTCritSectEnter(&pThis->CritSect);
|
---|
368 |
|
---|
369 | int rc = VINF_SUCCESS;
|
---|
370 | if (pThis->fMediaPresent)
|
---|
371 | {
|
---|
372 | if ( pThis->PCHSGeometry.cCylinders > 0
|
---|
373 | && pThis->PCHSGeometry.cHeads > 0
|
---|
374 | && pThis->PCHSGeometry.cSectors > 0)
|
---|
375 | {
|
---|
376 | *pPCHSGeometry = pThis->PCHSGeometry;
|
---|
377 | }
|
---|
378 | else
|
---|
379 | rc = VERR_PDM_GEOMETRY_NOT_SET;
|
---|
380 | }
|
---|
381 | else
|
---|
382 | rc = VERR_PDM_MEDIA_NOT_MOUNTED;
|
---|
383 |
|
---|
384 | RTCritSectLeave(&pThis->CritSect);
|
---|
385 | LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
|
---|
386 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc, pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors));
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /** @interface_method_impl{PDMIBLOCKBIOS,pfnBiosSetPCHSGeometry} */
|
---|
392 | static DECLCALLBACK(int) drvHostBaseSetPCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
393 | {
|
---|
394 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
395 | LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
|
---|
396 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
397 | RTCritSectEnter(&pThis->CritSect);
|
---|
398 |
|
---|
399 | int rc = VINF_SUCCESS;
|
---|
400 | if (pThis->fMediaPresent)
|
---|
401 | {
|
---|
402 | pThis->PCHSGeometry = *pPCHSGeometry;
|
---|
403 | }
|
---|
404 | else
|
---|
405 | {
|
---|
406 | AssertMsgFailed(("Invalid state! Not mounted!\n"));
|
---|
407 | rc = VERR_PDM_MEDIA_NOT_MOUNTED;
|
---|
408 | }
|
---|
409 |
|
---|
410 | RTCritSectLeave(&pThis->CritSect);
|
---|
411 | return rc;
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 | /** @interface_method_impl{PDMIBLOCKBIOS,pfnGetLCHSGeometry} */
|
---|
416 | static DECLCALLBACK(int) drvHostBaseGetLCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
417 | {
|
---|
418 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
419 | RTCritSectEnter(&pThis->CritSect);
|
---|
420 |
|
---|
421 | int rc = VINF_SUCCESS;
|
---|
422 | if (pThis->fMediaPresent)
|
---|
423 | {
|
---|
424 | if ( pThis->LCHSGeometry.cCylinders > 0
|
---|
425 | && pThis->LCHSGeometry.cHeads > 0
|
---|
426 | && pThis->LCHSGeometry.cSectors > 0)
|
---|
427 | {
|
---|
428 | *pLCHSGeometry = pThis->LCHSGeometry;
|
---|
429 | }
|
---|
430 | else
|
---|
431 | rc = VERR_PDM_GEOMETRY_NOT_SET;
|
---|
432 | }
|
---|
433 | else
|
---|
434 | rc = VERR_PDM_MEDIA_NOT_MOUNTED;
|
---|
435 |
|
---|
436 | RTCritSectLeave(&pThis->CritSect);
|
---|
437 | LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
|
---|
438 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc, pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors));
|
---|
439 | return rc;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | /** @interface_method_impl{PDMIBLOCKBIOS,pfnSetLCHSGeometry} */
|
---|
444 | static DECLCALLBACK(int) drvHostBaseSetLCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
445 | {
|
---|
446 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
447 | LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
|
---|
448 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
449 | RTCritSectEnter(&pThis->CritSect);
|
---|
450 |
|
---|
451 | int rc = VINF_SUCCESS;
|
---|
452 | if (pThis->fMediaPresent)
|
---|
453 | {
|
---|
454 | pThis->LCHSGeometry = *pLCHSGeometry;
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | AssertMsgFailed(("Invalid state! Not mounted!\n"));
|
---|
459 | rc = VERR_PDM_MEDIA_NOT_MOUNTED;
|
---|
460 | }
|
---|
461 |
|
---|
462 | RTCritSectLeave(&pThis->CritSect);
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /** @interface_method_impl{PDMIBLOCKBIOS,pfnIsVisible} */
|
---|
468 | static DECLCALLBACK(bool) drvHostBaseIsVisible(PPDMIMEDIA pInterface)
|
---|
469 | {
|
---|
470 | PDRVHOSTBASE pThis = PDMIMEDIA_2_DRVHOSTBASE(pInterface);
|
---|
471 | return pThis->fBiosVisible;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 |
|
---|
476 | /* -=-=-=-=- IMount -=-=-=-=- */
|
---|
477 |
|
---|
478 | /** @interface_method_impl{PDMIMOUNT,pfnUnmount} */
|
---|
479 | static DECLCALLBACK(int) drvHostBaseUnmount(PPDMIMOUNT pInterface, bool fForce, bool fEject)
|
---|
480 | {
|
---|
481 | RT_NOREF(fEject);
|
---|
482 | /* While we're not mountable (see drvHostBaseMount), we're unmountable. */
|
---|
483 | PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
|
---|
484 | RTCritSectEnter(&pThis->CritSect);
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * Validate state.
|
---|
488 | */
|
---|
489 | int rc = VINF_SUCCESS;
|
---|
490 | if (!pThis->fLocked || fForce)
|
---|
491 | {
|
---|
492 | /* Unlock drive if necessary. */
|
---|
493 | if (pThis->fLocked)
|
---|
494 | {
|
---|
495 | if (pThis->pfnDoLock)
|
---|
496 | rc = pThis->pfnDoLock(pThis, false);
|
---|
497 | if (RT_SUCCESS(rc))
|
---|
498 | pThis->fLocked = false;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /*
|
---|
502 | * Media is no longer present.
|
---|
503 | */
|
---|
504 | DRVHostBaseMediaNotPresent(pThis);
|
---|
505 | }
|
---|
506 | else
|
---|
507 | {
|
---|
508 | Log(("drvHostiBaseUnmount: Locked\n"));
|
---|
509 | rc = VERR_PDM_MEDIA_LOCKED;
|
---|
510 | }
|
---|
511 |
|
---|
512 | RTCritSectLeave(&pThis->CritSect);
|
---|
513 | LogFlow(("drvHostBaseUnmount: returns %Rrc\n", rc));
|
---|
514 | return rc;
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /** @interface_method_impl{PDMIMOUNT,pfnIsMounted} */
|
---|
519 | static DECLCALLBACK(bool) drvHostBaseIsMounted(PPDMIMOUNT pInterface)
|
---|
520 | {
|
---|
521 | PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
|
---|
522 | RTCritSectEnter(&pThis->CritSect);
|
---|
523 |
|
---|
524 | bool fRc = pThis->fMediaPresent;
|
---|
525 |
|
---|
526 | RTCritSectLeave(&pThis->CritSect);
|
---|
527 | return fRc;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | /** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
|
---|
532 | static DECLCALLBACK(int) drvHostBaseLock(PPDMIMOUNT pInterface)
|
---|
533 | {
|
---|
534 | PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
|
---|
535 | RTCritSectEnter(&pThis->CritSect);
|
---|
536 |
|
---|
537 | int rc = VINF_SUCCESS;
|
---|
538 | if (!pThis->fLocked)
|
---|
539 | {
|
---|
540 | if (pThis->pfnDoLock)
|
---|
541 | rc = pThis->pfnDoLock(pThis, true);
|
---|
542 | if (RT_SUCCESS(rc))
|
---|
543 | pThis->fLocked = true;
|
---|
544 | }
|
---|
545 | else
|
---|
546 | LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
547 |
|
---|
548 | RTCritSectLeave(&pThis->CritSect);
|
---|
549 | LogFlow(("%s-%d: drvHostBaseLock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
|
---|
550 | return rc;
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
|
---|
555 | static DECLCALLBACK(int) drvHostBaseUnlock(PPDMIMOUNT pInterface)
|
---|
556 | {
|
---|
557 | PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
|
---|
558 | RTCritSectEnter(&pThis->CritSect);
|
---|
559 |
|
---|
560 | int rc = VINF_SUCCESS;
|
---|
561 | if (pThis->fLocked)
|
---|
562 | {
|
---|
563 | if (pThis->pfnDoLock)
|
---|
564 | rc = pThis->pfnDoLock(pThis, false);
|
---|
565 | if (RT_SUCCESS(rc))
|
---|
566 | pThis->fLocked = false;
|
---|
567 | }
|
---|
568 | else
|
---|
569 | LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
570 |
|
---|
571 | RTCritSectLeave(&pThis->CritSect);
|
---|
572 | LogFlow(("%s-%d: drvHostBaseUnlock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | /** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
|
---|
578 | static DECLCALLBACK(bool) drvHostBaseIsLocked(PPDMIMOUNT pInterface)
|
---|
579 | {
|
---|
580 | PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
|
---|
581 | RTCritSectEnter(&pThis->CritSect);
|
---|
582 |
|
---|
583 | bool fRc = pThis->fLocked;
|
---|
584 |
|
---|
585 | RTCritSectLeave(&pThis->CritSect);
|
---|
586 | return fRc;
|
---|
587 | }
|
---|
588 |
|
---|
589 |
|
---|
590 | /* -=-=-=-=- IBase -=-=-=-=- */
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
594 | */
|
---|
595 | static DECLCALLBACK(void *) drvHostBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
596 | {
|
---|
597 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
598 | PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
|
---|
599 |
|
---|
600 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
601 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
|
---|
602 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUNT, &pThis->IMount);
|
---|
603 | return NULL;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /* -=-=-=-=- poller thread -=-=-=-=- */
|
---|
608 |
|
---|
609 | #ifdef RT_OS_DARWIN
|
---|
610 | /** The runloop input source name for the disk arbitration events. */
|
---|
611 | # define MY_RUN_LOOP_MODE CFSTR("drvHostBaseDA") /** @todo r=bird: Check if this will cause trouble in the same way that the one in the USB code did. */
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Gets the BSD Name (/dev/disc[0-9]+) for the service.
|
---|
615 | *
|
---|
616 | * This is done by recursing down the I/O registry until we hit upon an entry
|
---|
617 | * with a BSD Name. Usually we find it two levels down. (Further down under
|
---|
618 | * the IOCDPartitionScheme, the volume (slices) BSD Name is found. We don't
|
---|
619 | * seem to have to go this far fortunately.)
|
---|
620 | *
|
---|
621 | * @return VINF_SUCCESS if found, VERR_FILE_NOT_FOUND otherwise.
|
---|
622 | * @param Entry The current I/O registry entry reference.
|
---|
623 | * @param pszName Where to store the name. 128 bytes.
|
---|
624 | * @param cRecursions Number of recursions. This is used as an precaution
|
---|
625 | * just to limit the depth and avoid blowing the stack
|
---|
626 | * should we hit a bug or something.
|
---|
627 | */
|
---|
628 | static int drvHostBaseGetBSDName(io_registry_entry_t Entry, char *pszName, unsigned cRecursions)
|
---|
629 | {
|
---|
630 | int rc = VERR_FILE_NOT_FOUND;
|
---|
631 | io_iterator_t Children = 0;
|
---|
632 | kern_return_t krc = IORegistryEntryGetChildIterator(Entry, kIOServicePlane, &Children);
|
---|
633 | if (krc == KERN_SUCCESS)
|
---|
634 | {
|
---|
635 | io_object_t Child;
|
---|
636 | while ( rc == VERR_FILE_NOT_FOUND
|
---|
637 | && (Child = IOIteratorNext(Children)) != 0)
|
---|
638 | {
|
---|
639 | CFStringRef BSDNameStrRef = (CFStringRef)IORegistryEntryCreateCFProperty(Child, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0);
|
---|
640 | if (BSDNameStrRef)
|
---|
641 | {
|
---|
642 | if (CFStringGetCString(BSDNameStrRef, pszName, 128, kCFStringEncodingUTF8))
|
---|
643 | rc = VINF_SUCCESS;
|
---|
644 | else
|
---|
645 | AssertFailed();
|
---|
646 | CFRelease(BSDNameStrRef);
|
---|
647 | }
|
---|
648 | if (rc == VERR_FILE_NOT_FOUND && cRecursions < 10)
|
---|
649 | rc = drvHostBaseGetBSDName(Child, pszName, cRecursions + 1);
|
---|
650 | IOObjectRelease(Child);
|
---|
651 | }
|
---|
652 | IOObjectRelease(Children);
|
---|
653 | }
|
---|
654 | return rc;
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Callback notifying us that the async DADiskClaim()/DADiskUnmount call has completed.
|
---|
660 | *
|
---|
661 | * @param DiskRef The disk that was attempted claimed / unmounted.
|
---|
662 | * @param DissenterRef NULL on success, contains details on failure.
|
---|
663 | * @param pvContext Pointer to the return code variable.
|
---|
664 | */
|
---|
665 | static void drvHostBaseDADoneCallback(DADiskRef DiskRef, DADissenterRef DissenterRef, void *pvContext)
|
---|
666 | {
|
---|
667 | RT_NOREF(DiskRef);
|
---|
668 | int *prc = (int *)pvContext;
|
---|
669 | if (!DissenterRef)
|
---|
670 | *prc = 0;
|
---|
671 | else
|
---|
672 | *prc = DADissenterGetStatus(DissenterRef) ? DADissenterGetStatus(DissenterRef) : -1;
|
---|
673 | CFRunLoopStop(CFRunLoopGetCurrent());
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | /**
|
---|
678 | * Obtain exclusive access to the DVD device, umount it if necessary.
|
---|
679 | *
|
---|
680 | * @return VBox status code.
|
---|
681 | * @param pThis The driver instance.
|
---|
682 | * @param DVDService The DVD service object.
|
---|
683 | */
|
---|
684 | static int drvHostBaseObtainExclusiveAccess(PDRVHOSTBASE pThis, io_object_t DVDService)
|
---|
685 | {
|
---|
686 | PPDMDRVINS pDrvIns = pThis->pDrvIns; NOREF(pDrvIns);
|
---|
687 |
|
---|
688 | for (unsigned iTry = 0;; iTry++)
|
---|
689 | {
|
---|
690 | IOReturn irc = (*pThis->ppScsiTaskDI)->ObtainExclusiveAccess(pThis->ppScsiTaskDI);
|
---|
691 | if (irc == kIOReturnSuccess)
|
---|
692 | {
|
---|
693 | /*
|
---|
694 | * This is a bit weird, but if we unmounted the DVD drive we also need to
|
---|
695 | * unlock it afterwards or the guest won't be able to eject it later on.
|
---|
696 | */
|
---|
697 | if (pThis->pDADisk)
|
---|
698 | {
|
---|
699 | uint8_t abCmd[16] =
|
---|
700 | {
|
---|
701 | SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL, 0, 0, 0, false, 0,
|
---|
702 | 0,0,0,0,0,0,0,0,0,0
|
---|
703 | };
|
---|
704 | DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMMEDIATXDIR_NONE, NULL, NULL, NULL, 0, 0);
|
---|
705 | }
|
---|
706 | return VINF_SUCCESS;
|
---|
707 | }
|
---|
708 | if (irc == kIOReturnExclusiveAccess)
|
---|
709 | return VERR_SHARING_VIOLATION; /* already used exclusivly. */
|
---|
710 | if (irc != kIOReturnBusy)
|
---|
711 | return VERR_GENERAL_FAILURE; /* not mounted */
|
---|
712 |
|
---|
713 | /*
|
---|
714 | * Attempt to the unmount all volumes of the device.
|
---|
715 | * It seems we can can do this all in one go without having to enumerate the
|
---|
716 | * volumes (sessions) and deal with them one by one. This is very fortuitous
|
---|
717 | * as the disk arbitration API is a bit cumbersome to deal with.
|
---|
718 | */
|
---|
719 | if (iTry > 2)
|
---|
720 | return VERR_DRIVE_LOCKED;
|
---|
721 | char szName[128];
|
---|
722 | int rc = drvHostBaseGetBSDName(DVDService, &szName[0], 0);
|
---|
723 | if (RT_SUCCESS(rc))
|
---|
724 | {
|
---|
725 | pThis->pDASession = DASessionCreate(kCFAllocatorDefault);
|
---|
726 | if (pThis->pDASession)
|
---|
727 | {
|
---|
728 | DASessionScheduleWithRunLoop(pThis->pDASession, CFRunLoopGetCurrent(), MY_RUN_LOOP_MODE);
|
---|
729 | pThis->pDADisk = DADiskCreateFromBSDName(kCFAllocatorDefault, pThis->pDASession, szName);
|
---|
730 | if (pThis->pDADisk)
|
---|
731 | {
|
---|
732 | /*
|
---|
733 | * Try claim the device.
|
---|
734 | */
|
---|
735 | Log(("%s-%d: calling DADiskClaim on '%s'.\n", pDrvIns->pReg->szName, pDrvIns->iInstance, szName));
|
---|
736 | int rcDA = -2;
|
---|
737 | DADiskClaim(pThis->pDADisk, kDADiskClaimOptionDefault, NULL, NULL, drvHostBaseDADoneCallback, &rcDA);
|
---|
738 | SInt32 rc32 = CFRunLoopRunInMode(MY_RUN_LOOP_MODE, 120.0, FALSE);
|
---|
739 | AssertMsg(rc32 == kCFRunLoopRunStopped, ("rc32=%RI32 (%RX32)\n", rc32, rc32));
|
---|
740 | if ( rc32 == kCFRunLoopRunStopped
|
---|
741 | && !rcDA)
|
---|
742 | {
|
---|
743 | /*
|
---|
744 | * Try unmount the device.
|
---|
745 | */
|
---|
746 | Log(("%s-%d: calling DADiskUnmount on '%s'.\n", pDrvIns->pReg->szName, pDrvIns->iInstance, szName));
|
---|
747 | rcDA = -2;
|
---|
748 | DADiskUnmount(pThis->pDADisk, kDADiskUnmountOptionWhole, drvHostBaseDADoneCallback, &rcDA);
|
---|
749 | rc32 = CFRunLoopRunInMode(MY_RUN_LOOP_MODE, 120.0, FALSE);
|
---|
750 | AssertMsg(rc32 == kCFRunLoopRunStopped, ("rc32=%RI32 (%RX32)\n", rc32, rc32));
|
---|
751 | if ( rc32 == kCFRunLoopRunStopped
|
---|
752 | && !rcDA)
|
---|
753 | {
|
---|
754 | iTry = 99;
|
---|
755 | DASessionUnscheduleFromRunLoop(pThis->pDASession, CFRunLoopGetCurrent(), MY_RUN_LOOP_MODE);
|
---|
756 | Log(("%s-%d: unmount succeed - retrying.\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
757 | continue;
|
---|
758 | }
|
---|
759 | Log(("%s-%d: umount => rc32=%d & rcDA=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc32, rcDA));
|
---|
760 |
|
---|
761 | /* failed - cleanup */
|
---|
762 | DADiskUnclaim(pThis->pDADisk);
|
---|
763 | }
|
---|
764 | else
|
---|
765 | Log(("%s-%d: claim => rc32=%d & rcDA=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc32, rcDA));
|
---|
766 |
|
---|
767 | CFRelease(pThis->pDADisk);
|
---|
768 | pThis->pDADisk = NULL;
|
---|
769 | }
|
---|
770 | else
|
---|
771 | Log(("%s-%d: failed to open disk '%s'!\n", pDrvIns->pReg->szName, pDrvIns->iInstance, szName));
|
---|
772 |
|
---|
773 | DASessionUnscheduleFromRunLoop(pThis->pDASession, CFRunLoopGetCurrent(), MY_RUN_LOOP_MODE);
|
---|
774 | CFRelease(pThis->pDASession);
|
---|
775 | pThis->pDASession = NULL;
|
---|
776 | }
|
---|
777 | else
|
---|
778 | Log(("%s-%d: failed to create DA session!\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
779 | }
|
---|
780 | RTThreadSleep(10);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | #endif /* RT_OS_DARWIN */
|
---|
784 |
|
---|
785 |
|
---|
786 | #ifndef RT_OS_SOLARIS
|
---|
787 | /**
|
---|
788 | * Wrapper for open / RTFileOpen / IOKit.
|
---|
789 | *
|
---|
790 | * @remark The Darwin code must correspond exactly to the enumeration
|
---|
791 | * done in Main/darwin/iokit.c.
|
---|
792 | */
|
---|
793 | static int drvHostBaseOpen(PDRVHOSTBASE pThis, PRTFILE pFileDevice, bool fReadOnly)
|
---|
794 | {
|
---|
795 | # ifdef RT_OS_DARWIN
|
---|
796 | RT_NOREF(fReadOnly);
|
---|
797 |
|
---|
798 | /* Darwin is kind of special... */
|
---|
799 | Assert(!pFileDevice); NOREF(pFileDevice);
|
---|
800 | Assert(!pThis->cbBlock);
|
---|
801 | Assert(pThis->MasterPort == IO_OBJECT_NULL);
|
---|
802 | Assert(!pThis->ppMMCDI);
|
---|
803 | Assert(!pThis->ppScsiTaskDI);
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Open the master port on the first invocation.
|
---|
807 | */
|
---|
808 | kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &pThis->MasterPort);
|
---|
809 | AssertReturn(krc == KERN_SUCCESS, VERR_GENERAL_FAILURE);
|
---|
810 |
|
---|
811 | /*
|
---|
812 | * Create a matching dictionary for searching for CD, DVD and BlueRay services in the IOKit.
|
---|
813 | *
|
---|
814 | * The idea is to find all the devices which are of class IOCDBlockStorageDevice.
|
---|
815 | * CD devices are represented by IOCDBlockStorageDevice class itself, while DVD and BlueRay ones
|
---|
816 | * have it as a parent class.
|
---|
817 | */
|
---|
818 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IOCDBlockStorageDevice");
|
---|
819 | AssertReturn(RefMatchingDict, VERR_NOT_FOUND);
|
---|
820 |
|
---|
821 | /*
|
---|
822 | * do the search and get a collection of keyboards.
|
---|
823 | */
|
---|
824 | io_iterator_t DVDServices = IO_OBJECT_NULL;
|
---|
825 | IOReturn irc = IOServiceGetMatchingServices(pThis->MasterPort, RefMatchingDict, &DVDServices);
|
---|
826 | AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%d\n", irc), VERR_NOT_FOUND);
|
---|
827 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
828 |
|
---|
829 | /*
|
---|
830 | * Enumerate the matching drives (services).
|
---|
831 | * (This enumeration must be identical to the one performed in Main/src-server/darwin/iokit.cpp.)
|
---|
832 | */
|
---|
833 | int rc = VERR_FILE_NOT_FOUND;
|
---|
834 | unsigned i = 0;
|
---|
835 | io_object_t DVDService;
|
---|
836 | while ((DVDService = IOIteratorNext(DVDServices)) != 0)
|
---|
837 | {
|
---|
838 | /*
|
---|
839 | * Get the properties we use to identify the DVD drive.
|
---|
840 | *
|
---|
841 | * While there is a (weird 12 byte) GUID, it isn't persistent
|
---|
842 | * across boots. So, we have to use a combination of the
|
---|
843 | * vendor name and product name properties with an optional
|
---|
844 | * sequence number for identification.
|
---|
845 | */
|
---|
846 | CFMutableDictionaryRef PropsRef = 0;
|
---|
847 | krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
848 | if (krc == KERN_SUCCESS)
|
---|
849 | {
|
---|
850 | /* Get the Device Characteristics dictionary. */
|
---|
851 | CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
|
---|
852 | if (DevCharRef)
|
---|
853 | {
|
---|
854 | /* The vendor name. */
|
---|
855 | char szVendor[128];
|
---|
856 | char *pszVendor = &szVendor[0];
|
---|
857 | CFTypeRef ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyVendorNameKey));
|
---|
858 | if ( ValueRef
|
---|
859 | && CFGetTypeID(ValueRef) == CFStringGetTypeID()
|
---|
860 | && CFStringGetCString((CFStringRef)ValueRef, szVendor, sizeof(szVendor), kCFStringEncodingUTF8))
|
---|
861 | pszVendor = RTStrStrip(szVendor);
|
---|
862 | else
|
---|
863 | *pszVendor = '\0';
|
---|
864 |
|
---|
865 | /* The product name. */
|
---|
866 | char szProduct[128];
|
---|
867 | char *pszProduct = &szProduct[0];
|
---|
868 | ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyProductNameKey));
|
---|
869 | if ( ValueRef
|
---|
870 | && CFGetTypeID(ValueRef) == CFStringGetTypeID()
|
---|
871 | && CFStringGetCString((CFStringRef)ValueRef, szProduct, sizeof(szProduct), kCFStringEncodingUTF8))
|
---|
872 | pszProduct = RTStrStrip(szProduct);
|
---|
873 | else
|
---|
874 | *pszProduct = '\0';
|
---|
875 |
|
---|
876 | /* Construct the two names and compare thwm with the one we're searching for. */
|
---|
877 | char szName1[256 + 32];
|
---|
878 | char szName2[256 + 32];
|
---|
879 | if (*pszVendor || *pszProduct)
|
---|
880 | {
|
---|
881 | if (*pszVendor && *pszProduct)
|
---|
882 | {
|
---|
883 | RTStrPrintf(szName1, sizeof(szName1), "%s %s", pszVendor, pszProduct);
|
---|
884 | RTStrPrintf(szName2, sizeof(szName2), "%s %s (#%u)", pszVendor, pszProduct, i);
|
---|
885 | }
|
---|
886 | else
|
---|
887 | {
|
---|
888 | strcpy(szName1, *pszVendor ? pszVendor : pszProduct);
|
---|
889 | RTStrPrintf(szName2, sizeof(szName2), "%s (#%u)", *pszVendor ? pszVendor : pszProduct, i);
|
---|
890 | }
|
---|
891 | }
|
---|
892 | else
|
---|
893 | {
|
---|
894 | RTStrPrintf(szName1, sizeof(szName1), "(#%u)", i);
|
---|
895 | strcpy(szName2, szName1);
|
---|
896 | }
|
---|
897 |
|
---|
898 | if ( !strcmp(szName1, pThis->pszDeviceOpen)
|
---|
899 | || !strcmp(szName2, pThis->pszDeviceOpen))
|
---|
900 | {
|
---|
901 | /*
|
---|
902 | * Found it! Now, get the client interface and stuff.
|
---|
903 | * Note that we could also query kIOSCSITaskDeviceUserClientTypeID here if the
|
---|
904 | * MMC client plugin is missing. For now we assume this won't be necessary.
|
---|
905 | */
|
---|
906 | SInt32 Score = 0;
|
---|
907 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
908 | krc = IOCreatePlugInInterfaceForService(DVDService, kIOMMCDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
|
---|
909 | &ppPlugInInterface, &Score);
|
---|
910 | if (krc == KERN_SUCCESS)
|
---|
911 | {
|
---|
912 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
913 | CFUUIDGetUUIDBytes(kIOMMCDeviceInterfaceID),
|
---|
914 | (LPVOID *)&pThis->ppMMCDI);
|
---|
915 | (*ppPlugInInterface)->Release(ppPlugInInterface);
|
---|
916 | ppPlugInInterface = NULL;
|
---|
917 | if (hrc == S_OK)
|
---|
918 | {
|
---|
919 | pThis->ppScsiTaskDI = (*pThis->ppMMCDI)->GetSCSITaskDeviceInterface(pThis->ppMMCDI);
|
---|
920 | if (pThis->ppScsiTaskDI)
|
---|
921 | rc = VINF_SUCCESS;
|
---|
922 | else
|
---|
923 | {
|
---|
924 | LogRel(("GetSCSITaskDeviceInterface failed on '%s'\n", pThis->pszDeviceOpen));
|
---|
925 | rc = VERR_NOT_SUPPORTED;
|
---|
926 | (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
|
---|
927 | }
|
---|
928 | }
|
---|
929 | else
|
---|
930 | {
|
---|
931 | rc = VERR_GENERAL_FAILURE;//RTErrConvertFromDarwinCOM(krc);
|
---|
932 | pThis->ppMMCDI = NULL;
|
---|
933 | }
|
---|
934 | }
|
---|
935 | else /* Check for kIOSCSITaskDeviceUserClientTypeID? */
|
---|
936 | rc = VERR_GENERAL_FAILURE;//RTErrConvertFromDarwinKern(krc);
|
---|
937 |
|
---|
938 | /* Obtain exclusive access to the device so we can send SCSI commands. */
|
---|
939 | if (RT_SUCCESS(rc))
|
---|
940 | rc = drvHostBaseObtainExclusiveAccess(pThis, DVDService);
|
---|
941 |
|
---|
942 | /* Cleanup on failure. */
|
---|
943 | if (RT_FAILURE(rc))
|
---|
944 | {
|
---|
945 | if (pThis->ppScsiTaskDI)
|
---|
946 | {
|
---|
947 | (*pThis->ppScsiTaskDI)->Release(pThis->ppScsiTaskDI);
|
---|
948 | pThis->ppScsiTaskDI = NULL;
|
---|
949 | }
|
---|
950 | if (pThis->ppMMCDI)
|
---|
951 | {
|
---|
952 | (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
|
---|
953 | pThis->ppMMCDI = NULL;
|
---|
954 | }
|
---|
955 | }
|
---|
956 |
|
---|
957 | IOObjectRelease(DVDService);
|
---|
958 | break;
|
---|
959 | }
|
---|
960 | }
|
---|
961 | CFRelease(PropsRef);
|
---|
962 | }
|
---|
963 | else
|
---|
964 | AssertMsgFailed(("krc=%#x\n", krc));
|
---|
965 |
|
---|
966 | IOObjectRelease(DVDService);
|
---|
967 | i++;
|
---|
968 | }
|
---|
969 |
|
---|
970 | IOObjectRelease(DVDServices);
|
---|
971 | return rc;
|
---|
972 |
|
---|
973 | #elif defined(RT_OS_FREEBSD)
|
---|
974 | RTFILE hFileDevice;
|
---|
975 | int rc = RTFileOpen(&hFileDevice, pThis->pszDeviceOpen, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
976 | if (RT_FAILURE(rc))
|
---|
977 | return rc;
|
---|
978 |
|
---|
979 | /*
|
---|
980 | * The current device handle can't passthrough SCSI commands.
|
---|
981 | * We have to get he passthrough device path and open this.
|
---|
982 | */
|
---|
983 | union ccb DeviceCCB;
|
---|
984 | memset(&DeviceCCB, 0, sizeof(DeviceCCB));
|
---|
985 |
|
---|
986 | DeviceCCB.ccb_h.func_code = XPT_GDEVLIST;
|
---|
987 | int rcBSD = ioctl(RTFileToNative(hFileDevice), CAMGETPASSTHRU, &DeviceCCB);
|
---|
988 | if (!rcBSD)
|
---|
989 | {
|
---|
990 | char *pszPassthroughDevice = NULL;
|
---|
991 | rc = RTStrAPrintf(&pszPassthroughDevice, "/dev/%s%u",
|
---|
992 | DeviceCCB.cgdl.periph_name, DeviceCCB.cgdl.unit_number);
|
---|
993 | if (rc >= 0)
|
---|
994 | {
|
---|
995 | RTFILE hPassthroughDevice;
|
---|
996 | rc = RTFileOpen(&hPassthroughDevice, pszPassthroughDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
997 | RTStrFree(pszPassthroughDevice);
|
---|
998 | if (RT_SUCCESS(rc))
|
---|
999 | {
|
---|
1000 | /* Get needed device parameters. */
|
---|
1001 |
|
---|
1002 | /*
|
---|
1003 | * The device path, target id and lun id. Those are
|
---|
1004 | * needed for the SCSI passthrough ioctl.
|
---|
1005 | */
|
---|
1006 | memset(&DeviceCCB, 0, sizeof(DeviceCCB));
|
---|
1007 | DeviceCCB.ccb_h.func_code = XPT_GDEVLIST;
|
---|
1008 |
|
---|
1009 | rcBSD = ioctl(RTFileToNative(hPassthroughDevice), CAMGETPASSTHRU, &DeviceCCB);
|
---|
1010 | if (!rcBSD)
|
---|
1011 | {
|
---|
1012 | if (DeviceCCB.cgdl.status != CAM_GDEVLIST_ERROR)
|
---|
1013 | {
|
---|
1014 | pThis->ScsiBus = DeviceCCB.ccb_h.path_id;
|
---|
1015 | pThis->ScsiTargetID = DeviceCCB.ccb_h.target_id;
|
---|
1016 | pThis->ScsiLunID = DeviceCCB.ccb_h.target_lun;
|
---|
1017 | *pFileDevice = hPassthroughDevice;
|
---|
1018 | }
|
---|
1019 | else
|
---|
1020 | {
|
---|
1021 | /* The passthrough device wasn't found. */
|
---|
1022 | rc = VERR_NOT_FOUND;
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | rc = RTErrConvertFromErrno(errno);
|
---|
1027 |
|
---|
1028 | if (RT_FAILURE(rc))
|
---|
1029 | RTFileClose(hPassthroughDevice);
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 | else
|
---|
1033 | rc = VERR_NO_STR_MEMORY;
|
---|
1034 | }
|
---|
1035 | else
|
---|
1036 | rc = RTErrConvertFromErrno(errno);
|
---|
1037 |
|
---|
1038 | RTFileClose(hFileDevice);
|
---|
1039 | return rc;
|
---|
1040 |
|
---|
1041 | #else
|
---|
1042 | uint32_t fFlags = (fReadOnly ? RTFILE_O_READ : RTFILE_O_READWRITE) | RTFILE_O_OPEN | RTFILE_O_DENY_NONE;
|
---|
1043 | # ifdef RT_OS_LINUX
|
---|
1044 | fFlags |= RTFILE_O_NON_BLOCK;
|
---|
1045 | # endif
|
---|
1046 | return RTFileOpen(pFileDevice, pThis->pszDeviceOpen, fFlags);
|
---|
1047 | #endif
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | #else /* RT_OS_SOLARIS */
|
---|
1051 |
|
---|
1052 | /**
|
---|
1053 | * Solaris wrapper for RTFileOpen.
|
---|
1054 | *
|
---|
1055 | * Solaris has to deal with two filehandles, a block and a raw one. Rather than messing
|
---|
1056 | * with drvHostBaseOpen's function signature & body, having a separate one is better.
|
---|
1057 | *
|
---|
1058 | * @returns VBox status code.
|
---|
1059 | */
|
---|
1060 | static int drvHostBaseOpen(PDRVHOSTBASE pThis, PRTFILE pFileBlockDevice, PRTFILE pFileRawDevice, bool fReadOnly)
|
---|
1061 | {
|
---|
1062 | unsigned fFlags = (fReadOnly ? RTFILE_O_READ : RTFILE_O_READWRITE)
|
---|
1063 | | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK;
|
---|
1064 | int rc = RTFileOpen(pFileBlockDevice, pThis->pszDeviceOpen, fFlags);
|
---|
1065 | if (RT_SUCCESS(rc))
|
---|
1066 | {
|
---|
1067 | rc = RTFileOpen(pFileRawDevice, pThis->pszRawDeviceOpen, fFlags);
|
---|
1068 | if (RT_SUCCESS(rc))
|
---|
1069 | return rc;
|
---|
1070 |
|
---|
1071 | LogRel(("DVD: failed to open device %s rc=%Rrc\n", pThis->pszRawDeviceOpen, rc));
|
---|
1072 | RTFileClose(*pFileBlockDevice);
|
---|
1073 | }
|
---|
1074 | else
|
---|
1075 | LogRel(("DVD: failed to open device %s rc=%Rrc\n", pThis->pszDeviceOpen, rc));
|
---|
1076 | return rc;
|
---|
1077 | }
|
---|
1078 | #endif /* RT_OS_SOLARIS */
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | /**
|
---|
1082 | * (Re)opens the device.
|
---|
1083 | *
|
---|
1084 | * This is used to open the device during construction, but it's also used to re-open
|
---|
1085 | * the device when a media is inserted. This re-open will kill off any cached data
|
---|
1086 | * that Linux for some peculiar reason thinks should survive a media change...
|
---|
1087 | *
|
---|
1088 | * @returns VBOX status code.
|
---|
1089 | * @param pThis Instance data.
|
---|
1090 | */
|
---|
1091 | static int drvHostBaseReopen(PDRVHOSTBASE pThis)
|
---|
1092 | {
|
---|
1093 | #ifndef RT_OS_DARWIN /* Only *one* open for darwin. */
|
---|
1094 | LogFlow(("%s-%d: drvHostBaseReopen: '%s'\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen));
|
---|
1095 |
|
---|
1096 | RTFILE hFileDevice;
|
---|
1097 | #ifdef RT_OS_SOLARIS
|
---|
1098 | if (pThis->hFileRawDevice != NIL_RTFILE)
|
---|
1099 | {
|
---|
1100 | RTFileClose(pThis->hFileRawDevice);
|
---|
1101 | pThis->hFileRawDevice = NIL_RTFILE;
|
---|
1102 | }
|
---|
1103 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
1104 | {
|
---|
1105 | RTFileClose(pThis->hFileDevice);
|
---|
1106 | pThis->hFileDevice = NIL_RTFILE;
|
---|
1107 | }
|
---|
1108 | RTFILE hFileRawDevice;
|
---|
1109 | int rc = drvHostBaseOpen(pThis, &hFileDevice, &hFileRawDevice, pThis->fReadOnlyConfig);
|
---|
1110 | #else
|
---|
1111 | int rc = drvHostBaseOpen(pThis, &hFileDevice, pThis->fReadOnlyConfig);
|
---|
1112 | #endif
|
---|
1113 | if (RT_FAILURE(rc))
|
---|
1114 | {
|
---|
1115 | if (!pThis->fReadOnlyConfig)
|
---|
1116 | {
|
---|
1117 | LogFlow(("%s-%d: drvHostBaseReopen: '%s' - retry readonly (%Rrc)\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDeviceOpen, rc));
|
---|
1118 | #ifdef RT_OS_SOLARIS
|
---|
1119 | rc = drvHostBaseOpen(pThis, &hFileDevice, &hFileRawDevice, false);
|
---|
1120 | #else
|
---|
1121 | rc = drvHostBaseOpen(pThis, &hFileDevice, false);
|
---|
1122 | #endif
|
---|
1123 | }
|
---|
1124 | if (RT_FAILURE(rc))
|
---|
1125 | {
|
---|
1126 | LogFlow(("%s-%d: failed to open device '%s', rc=%Rrc\n",
|
---|
1127 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
|
---|
1128 | return rc;
|
---|
1129 | }
|
---|
1130 | pThis->fReadOnly = true;
|
---|
1131 | }
|
---|
1132 | else
|
---|
1133 | pThis->fReadOnly = pThis->fReadOnlyConfig;
|
---|
1134 |
|
---|
1135 | #ifdef RT_OS_SOLARIS
|
---|
1136 | if (pThis->hFileRawDevice != NIL_RTFILE)
|
---|
1137 | RTFileClose(pThis->hFileRawDevice);
|
---|
1138 | pThis->hFileRawDevice = hFileRawDevice;
|
---|
1139 | #endif
|
---|
1140 |
|
---|
1141 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
1142 | RTFileClose(pThis->hFileDevice);
|
---|
1143 | pThis->hFileDevice = hFileDevice;
|
---|
1144 | #else /* RT_OS_DARWIN */
|
---|
1145 | RT_NOREF(pThis);
|
---|
1146 | #endif /* RT_OS_DARWIN */
|
---|
1147 | return VINF_SUCCESS;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 |
|
---|
1151 | /**
|
---|
1152 | * Queries the media size.
|
---|
1153 | *
|
---|
1154 | * @returns VBox status code.
|
---|
1155 | * @param pThis Pointer to the instance data.
|
---|
1156 | * @param pcb Where to store the media size in bytes.
|
---|
1157 | */
|
---|
1158 | static DECLCALLBACK(int) drvHostBaseGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
|
---|
1159 | {
|
---|
1160 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
1161 | /*
|
---|
1162 | * Try a READ_CAPACITY command...
|
---|
1163 | */
|
---|
1164 | struct
|
---|
1165 | {
|
---|
1166 | uint32_t cBlocks;
|
---|
1167 | uint32_t cbBlock;
|
---|
1168 | } Buf = {0, 0};
|
---|
1169 | uint32_t cbBuf = sizeof(Buf);
|
---|
1170 | uint8_t abCmd[16] =
|
---|
1171 | {
|
---|
1172 | SCSI_READ_CAPACITY, 0, 0, 0, 0, 0, 0,
|
---|
1173 | 0,0,0,0,0,0,0,0,0
|
---|
1174 | };
|
---|
1175 | int rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMMEDIATXDIR_FROM_DEVICE, &Buf, &cbBuf, NULL, 0, 0);
|
---|
1176 | if (RT_SUCCESS(rc))
|
---|
1177 | {
|
---|
1178 | Assert(cbBuf == sizeof(Buf));
|
---|
1179 | Buf.cBlocks = RT_BE2H_U32(Buf.cBlocks);
|
---|
1180 | Buf.cbBlock = RT_BE2H_U32(Buf.cbBlock);
|
---|
1181 | //if (Buf.cbBlock > 2048) /* everyone else is doing this... check if it needed/right.*/
|
---|
1182 | // Buf.cbBlock = 2048;
|
---|
1183 | pThis->cbBlock = Buf.cbBlock;
|
---|
1184 |
|
---|
1185 | *pcb = (uint64_t)Buf.cBlocks * Buf.cbBlock;
|
---|
1186 | }
|
---|
1187 | return rc;
|
---|
1188 |
|
---|
1189 | #elif defined(RT_OS_SOLARIS)
|
---|
1190 | /*
|
---|
1191 | * Sun docs suggests using DKIOCGGEOM instead of DKIOCGMEDIAINFO, but
|
---|
1192 | * Sun themselves use DKIOCGMEDIAINFO for DVDs/CDs, and use DKIOCGGEOM
|
---|
1193 | * for secondary storage devices.
|
---|
1194 | */
|
---|
1195 | struct dk_minfo MediaInfo;
|
---|
1196 | if (ioctl(RTFileToNative(pThis->hFileRawDevice), DKIOCGMEDIAINFO, &MediaInfo) == 0)
|
---|
1197 | {
|
---|
1198 | *pcb = MediaInfo.dki_capacity * (uint64_t)MediaInfo.dki_lbsize;
|
---|
1199 | return VINF_SUCCESS;
|
---|
1200 | }
|
---|
1201 | return RTFileSeek(pThis->hFileDevice, 0, RTFILE_SEEK_END, pcb);
|
---|
1202 |
|
---|
1203 | #elif defined(RT_OS_WINDOWS)
|
---|
1204 | /* use NT api, retry a few times if the media is being verified. */
|
---|
1205 | IO_STATUS_BLOCK IoStatusBlock = {0};
|
---|
1206 | FILE_FS_SIZE_INFORMATION FsSize= {0};
|
---|
1207 | NTSTATUS rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(pThis->hFileDevice), &IoStatusBlock,
|
---|
1208 | &FsSize, sizeof(FsSize), FileFsSizeInformation);
|
---|
1209 | int cRetries = 5;
|
---|
1210 | while (rcNt == STATUS_VERIFY_REQUIRED && cRetries-- > 0)
|
---|
1211 | {
|
---|
1212 | RTThreadSleep(10);
|
---|
1213 | rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(pThis->hFileDevice), &IoStatusBlock,
|
---|
1214 | &FsSize, sizeof(FsSize), FileFsSizeInformation);
|
---|
1215 | }
|
---|
1216 | if (rcNt >= 0)
|
---|
1217 | {
|
---|
1218 | *pcb = FsSize.TotalAllocationUnits.QuadPart * FsSize.BytesPerSector;
|
---|
1219 | return VINF_SUCCESS;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /* convert nt status code to VBox status code. */
|
---|
1223 | /** @todo Make conversion function!. */
|
---|
1224 | int rc = VERR_GENERAL_FAILURE;
|
---|
1225 | switch (rcNt)
|
---|
1226 | {
|
---|
1227 | case STATUS_NO_MEDIA_IN_DEVICE: rc = VERR_MEDIA_NOT_PRESENT; break;
|
---|
1228 | case STATUS_VERIFY_REQUIRED: rc = VERR_TRY_AGAIN; break;
|
---|
1229 | }
|
---|
1230 | LogFlow(("drvHostBaseGetMediaSize: NtQueryVolumeInformationFile -> %#lx\n", rcNt, rc));
|
---|
1231 | return rc;
|
---|
1232 | #else
|
---|
1233 | return RTFileSeek(pThis->hFileDevice, 0, RTFILE_SEEK_END, pcb);
|
---|
1234 | #endif
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 |
|
---|
1238 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
1239 | /**
|
---|
1240 | * Execute a SCSI command.
|
---|
1241 | *
|
---|
1242 | * @param pThis The instance data.
|
---|
1243 | * @param pbCmd Pointer to the SCSI command.
|
---|
1244 | * @param cbCmd The size of the SCSI command.
|
---|
1245 | * @param enmTxDir The transfer direction.
|
---|
1246 | * @param pvBuf The buffer. Can be NULL if enmTxDir is PDMMEDIATXDIR_NONE.
|
---|
1247 | * @param pcbBuf Where to get the buffer size from and put the actual transfer size. Can be NULL.
|
---|
1248 | * @param pbSense Where to put the sense data. Can be NULL.
|
---|
1249 | * @param cbSense Size of the sense data buffer.
|
---|
1250 | * @param cTimeoutMillies The timeout. 0 mean the default timeout.
|
---|
1251 | *
|
---|
1252 | * @returns VINF_SUCCESS on success (no sense code).
|
---|
1253 | * @returns VERR_UNRESOLVED_ERROR if sense code is present.
|
---|
1254 | * @returns Some other VBox status code on failures without sense code.
|
---|
1255 | *
|
---|
1256 | * @todo Fix VERR_UNRESOLVED_ERROR abuse.
|
---|
1257 | */
|
---|
1258 | DECLCALLBACK(int) DRVHostBaseScsiCmd(PDRVHOSTBASE pThis, const uint8_t *pbCmd, size_t cbCmd, PDMMEDIATXDIR enmTxDir,
|
---|
1259 | void *pvBuf, uint32_t *pcbBuf, uint8_t *pbSense, size_t cbSense, uint32_t cTimeoutMillies)
|
---|
1260 | {
|
---|
1261 | /*
|
---|
1262 | * Minimal input validation.
|
---|
1263 | */
|
---|
1264 | Assert(enmTxDir == PDMMEDIATXDIR_NONE || enmTxDir == PDMMEDIATXDIR_FROM_DEVICE || enmTxDir == PDMMEDIATXDIR_TO_DEVICE);
|
---|
1265 | Assert(!pvBuf || pcbBuf);
|
---|
1266 | Assert(pvBuf || enmTxDir == PDMMEDIATXDIR_NONE);
|
---|
1267 | Assert(pbSense || !cbSense);
|
---|
1268 | AssertPtr(pbCmd);
|
---|
1269 | Assert(cbCmd <= 16 && cbCmd >= 1);
|
---|
1270 | const uint32_t cbBuf = pcbBuf ? *pcbBuf : 0;
|
---|
1271 | if (pcbBuf)
|
---|
1272 | *pcbBuf = 0;
|
---|
1273 |
|
---|
1274 | # ifdef RT_OS_DARWIN
|
---|
1275 | Assert(pThis->ppScsiTaskDI);
|
---|
1276 |
|
---|
1277 | int rc = VERR_GENERAL_FAILURE;
|
---|
1278 | SCSITaskInterface **ppScsiTaskI = (*pThis->ppScsiTaskDI)->CreateSCSITask(pThis->ppScsiTaskDI);
|
---|
1279 | if (!ppScsiTaskI)
|
---|
1280 | return VERR_NO_MEMORY;
|
---|
1281 | do
|
---|
1282 | {
|
---|
1283 | /* Setup the scsi command. */
|
---|
1284 | SCSICommandDescriptorBlock cdb = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
|
---|
1285 | memcpy(&cdb[0], pbCmd, cbCmd);
|
---|
1286 | IOReturn irc = (*ppScsiTaskI)->SetCommandDescriptorBlock(ppScsiTaskI, cdb, cbCmd);
|
---|
1287 | AssertBreak(irc == kIOReturnSuccess);
|
---|
1288 |
|
---|
1289 | /* Setup the buffer. */
|
---|
1290 | if (enmTxDir == PDMMEDIATXDIR_NONE)
|
---|
1291 | irc = (*ppScsiTaskI)->SetScatterGatherEntries(ppScsiTaskI, NULL, 0, 0, kSCSIDataTransfer_NoDataTransfer);
|
---|
1292 | else
|
---|
1293 | {
|
---|
1294 | IOVirtualRange Range = { (IOVirtualAddress)pvBuf, cbBuf };
|
---|
1295 | irc = (*ppScsiTaskI)->SetScatterGatherEntries(ppScsiTaskI, &Range, 1, cbBuf,
|
---|
1296 | enmTxDir == PDMMEDIATXDIR_FROM_DEVICE
|
---|
1297 | ? kSCSIDataTransfer_FromTargetToInitiator
|
---|
1298 | : kSCSIDataTransfer_FromInitiatorToTarget);
|
---|
1299 | }
|
---|
1300 | AssertBreak(irc == kIOReturnSuccess);
|
---|
1301 |
|
---|
1302 | /* Set the timeout. */
|
---|
1303 | irc = (*ppScsiTaskI)->SetTimeoutDuration(ppScsiTaskI, cTimeoutMillies ? cTimeoutMillies : 30000 /*ms*/);
|
---|
1304 | AssertBreak(irc == kIOReturnSuccess);
|
---|
1305 |
|
---|
1306 | /* Execute the command and get the response. */
|
---|
1307 | SCSI_Sense_Data SenseData = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
|
---|
1308 | SCSIServiceResponse ServiceResponse = kSCSIServiceResponse_Request_In_Process;
|
---|
1309 | SCSITaskStatus TaskStatus = kSCSITaskStatus_GOOD;
|
---|
1310 | UInt64 cbReturned = 0;
|
---|
1311 | irc = (*ppScsiTaskI)->ExecuteTaskSync(ppScsiTaskI, &SenseData, &TaskStatus, &cbReturned);
|
---|
1312 | AssertBreak(irc == kIOReturnSuccess);
|
---|
1313 | if (pcbBuf)
|
---|
1314 | *pcbBuf = (int32_t)cbReturned;
|
---|
1315 |
|
---|
1316 | irc = (*ppScsiTaskI)->GetSCSIServiceResponse(ppScsiTaskI, &ServiceResponse);
|
---|
1317 | AssertBreak(irc == kIOReturnSuccess);
|
---|
1318 | AssertBreak(ServiceResponse == kSCSIServiceResponse_TASK_COMPLETE);
|
---|
1319 |
|
---|
1320 | if (TaskStatus == kSCSITaskStatus_GOOD)
|
---|
1321 | rc = VINF_SUCCESS;
|
---|
1322 | else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
|
---|
1323 | && pbSense)
|
---|
1324 | {
|
---|
1325 | memset(pbSense, 0, cbSense); /* lazy */
|
---|
1326 | memcpy(pbSense, &SenseData, RT_MIN(sizeof(SenseData), cbSense));
|
---|
1327 | rc = VERR_UNRESOLVED_ERROR;
|
---|
1328 | }
|
---|
1329 | /** @todo convert sense codes when caller doesn't wish to do this himself. */
|
---|
1330 | /*else if ( TaskStatus == kSCSITaskStatus_CHECK_CONDITION
|
---|
1331 | && SenseData.ADDITIONAL_SENSE_CODE == 0x3A)
|
---|
1332 | rc = VERR_MEDIA_NOT_PRESENT; */
|
---|
1333 | else
|
---|
1334 | {
|
---|
1335 | rc = enmTxDir == PDMMEDIATXDIR_NONE
|
---|
1336 | ? VERR_DEV_IO_ERROR
|
---|
1337 | : enmTxDir == PDMMEDIATXDIR_FROM_DEVICE
|
---|
1338 | ? VERR_READ_ERROR
|
---|
1339 | : VERR_WRITE_ERROR;
|
---|
1340 | if (pThis->cLogRelErrors++ < 10)
|
---|
1341 | LogRel(("DVD scsi error: cmd={%.*Rhxs} TaskStatus=%#x key=%#x ASC=%#x ASCQ=%#x (%Rrc)\n",
|
---|
1342 | cbCmd, pbCmd, TaskStatus, SenseData.SENSE_KEY, SenseData.ADDITIONAL_SENSE_CODE,
|
---|
1343 | SenseData.ADDITIONAL_SENSE_CODE_QUALIFIER, rc));
|
---|
1344 | }
|
---|
1345 | } while (0);
|
---|
1346 |
|
---|
1347 | (*ppScsiTaskI)->Release(ppScsiTaskI);
|
---|
1348 |
|
---|
1349 | # elif defined(RT_OS_FREEBSD)
|
---|
1350 | int rc = VINF_SUCCESS;
|
---|
1351 | int rcBSD = 0;
|
---|
1352 | union ccb DeviceCCB;
|
---|
1353 | union ccb *pDeviceCCB = &DeviceCCB;
|
---|
1354 | u_int32_t fFlags;
|
---|
1355 |
|
---|
1356 | memset(pDeviceCCB, 0, sizeof(DeviceCCB));
|
---|
1357 | pDeviceCCB->ccb_h.path_id = pThis->ScsiBus;
|
---|
1358 | pDeviceCCB->ccb_h.target_id = pThis->ScsiTargetID;
|
---|
1359 | pDeviceCCB->ccb_h.target_lun = pThis->ScsiLunID;
|
---|
1360 |
|
---|
1361 | /* The SCSI INQUIRY command can't be passed through directly. */
|
---|
1362 | if (pbCmd[0] == SCSI_INQUIRY)
|
---|
1363 | {
|
---|
1364 | pDeviceCCB->ccb_h.func_code = XPT_GDEV_TYPE;
|
---|
1365 |
|
---|
1366 | rcBSD = ioctl(RTFileToNative(pThis->hFileDevice), CAMIOCOMMAND, pDeviceCCB);
|
---|
1367 | if (!rcBSD)
|
---|
1368 | {
|
---|
1369 | uint32_t cbCopy = cbBuf < sizeof(struct scsi_inquiry_data)
|
---|
1370 | ? cbBuf
|
---|
1371 | : sizeof(struct scsi_inquiry_data);;
|
---|
1372 | memcpy(pvBuf, &pDeviceCCB->cgd.inq_data, cbCopy);
|
---|
1373 | memset(pbSense, 0, cbSense);
|
---|
1374 |
|
---|
1375 | if (pcbBuf)
|
---|
1376 | *pcbBuf = cbCopy;
|
---|
1377 | }
|
---|
1378 | else
|
---|
1379 | rc = RTErrConvertFromErrno(errno);
|
---|
1380 | }
|
---|
1381 | else
|
---|
1382 | {
|
---|
1383 | /* Copy the CDB. */
|
---|
1384 | memcpy(&pDeviceCCB->csio.cdb_io.cdb_bytes, pbCmd, cbCmd);
|
---|
1385 |
|
---|
1386 | /* Set direction. */
|
---|
1387 | if (enmTxDir == PDMMEDIATXDIR_NONE)
|
---|
1388 | fFlags = CAM_DIR_NONE;
|
---|
1389 | else if (enmTxDir == PDMMEDIATXDIR_FROM_DEVICE)
|
---|
1390 | fFlags = CAM_DIR_IN;
|
---|
1391 | else
|
---|
1392 | fFlags = CAM_DIR_OUT;
|
---|
1393 |
|
---|
1394 | fFlags |= CAM_DEV_QFRZDIS;
|
---|
1395 |
|
---|
1396 | cam_fill_csio(&pDeviceCCB->csio, 1, NULL, fFlags, MSG_SIMPLE_Q_TAG,
|
---|
1397 | (u_int8_t *)pvBuf, cbBuf, cbSense, cbCmd,
|
---|
1398 | cTimeoutMillies ? cTimeoutMillies : 30000/* timeout */);
|
---|
1399 |
|
---|
1400 | /* Send command */
|
---|
1401 | rcBSD = ioctl(RTFileToNative(pThis->hFileDevice), CAMIOCOMMAND, pDeviceCCB);
|
---|
1402 | if (!rcBSD)
|
---|
1403 | {
|
---|
1404 | switch (pDeviceCCB->ccb_h.status & CAM_STATUS_MASK)
|
---|
1405 | {
|
---|
1406 | case CAM_REQ_CMP:
|
---|
1407 | rc = VINF_SUCCESS;
|
---|
1408 | break;
|
---|
1409 | case CAM_SEL_TIMEOUT:
|
---|
1410 | rc = VERR_DEV_IO_ERROR;
|
---|
1411 | break;
|
---|
1412 | case CAM_CMD_TIMEOUT:
|
---|
1413 | rc = VERR_TIMEOUT;
|
---|
1414 | break;
|
---|
1415 | default:
|
---|
1416 | rc = VERR_DEV_IO_ERROR;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | if (pcbBuf)
|
---|
1420 | *pcbBuf = cbBuf - pDeviceCCB->csio.resid;
|
---|
1421 |
|
---|
1422 | if (pbSense)
|
---|
1423 | memcpy(pbSense, &pDeviceCCB->csio.sense_data,
|
---|
1424 | cbSense - pDeviceCCB->csio.sense_resid);
|
---|
1425 | }
|
---|
1426 | else
|
---|
1427 | rc = RTErrConvertFromErrno(errno);
|
---|
1428 | }
|
---|
1429 | # endif
|
---|
1430 |
|
---|
1431 | return rc;
|
---|
1432 | }
|
---|
1433 | #endif
|
---|
1434 |
|
---|
1435 |
|
---|
1436 | /**
|
---|
1437 | * Media present.
|
---|
1438 | * Query the size and notify the above driver / device.
|
---|
1439 | *
|
---|
1440 | * @param pThis The instance data.
|
---|
1441 | */
|
---|
1442 | int DRVHostBaseMediaPresent(PDRVHOSTBASE pThis)
|
---|
1443 | {
|
---|
1444 | /*
|
---|
1445 | * Open the drive.
|
---|
1446 | */
|
---|
1447 | int rc = drvHostBaseReopen(pThis);
|
---|
1448 | if (RT_FAILURE(rc))
|
---|
1449 | return rc;
|
---|
1450 |
|
---|
1451 | /*
|
---|
1452 | * Determine the size.
|
---|
1453 | */
|
---|
1454 | uint64_t cb;
|
---|
1455 | rc = pThis->pfnGetMediaSize(pThis, &cb);
|
---|
1456 | if (RT_FAILURE(rc))
|
---|
1457 | {
|
---|
1458 | LogFlow(("%s-%d: failed to figure media size of %s, rc=%Rrc\n",
|
---|
1459 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
|
---|
1460 | return rc;
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /*
|
---|
1464 | * Update the data and inform the unit.
|
---|
1465 | */
|
---|
1466 | pThis->cbSize = cb;
|
---|
1467 | pThis->fMediaPresent = true;
|
---|
1468 | if (pThis->pDrvMountNotify)
|
---|
1469 | pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
|
---|
1470 | LogFlow(("%s-%d: drvHostBaseMediaPresent: cbSize=%lld (%#llx)\n",
|
---|
1471 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->cbSize, pThis->cbSize));
|
---|
1472 | return VINF_SUCCESS;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 |
|
---|
1476 | /**
|
---|
1477 | * Media no longer present.
|
---|
1478 | * @param pThis The instance data.
|
---|
1479 | */
|
---|
1480 | void DRVHostBaseMediaNotPresent(PDRVHOSTBASE pThis)
|
---|
1481 | {
|
---|
1482 | pThis->fMediaPresent = false;
|
---|
1483 | pThis->fLocked = false;
|
---|
1484 | pThis->PCHSGeometry.cCylinders = 0;
|
---|
1485 | pThis->PCHSGeometry.cHeads = 0;
|
---|
1486 | pThis->PCHSGeometry.cSectors = 0;
|
---|
1487 | pThis->LCHSGeometry.cCylinders = 0;
|
---|
1488 | pThis->LCHSGeometry.cHeads = 0;
|
---|
1489 | pThis->LCHSGeometry.cSectors = 0;
|
---|
1490 | if (pThis->pDrvMountNotify)
|
---|
1491 | pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 |
|
---|
1495 | #ifdef RT_OS_WINDOWS
|
---|
1496 |
|
---|
1497 | /**
|
---|
1498 | * Window procedure for the invisible window used to catch the WM_DEVICECHANGE broadcasts.
|
---|
1499 | */
|
---|
1500 | static LRESULT CALLBACK DeviceChangeWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
1501 | {
|
---|
1502 | Log2(("DeviceChangeWindowProc: hwnd=%08x uMsg=%08x\n", hwnd, uMsg));
|
---|
1503 | if (uMsg == WM_DESTROY)
|
---|
1504 | {
|
---|
1505 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
---|
1506 | if (pThis)
|
---|
1507 | ASMAtomicXchgSize(&pThis->hwndDeviceChange, NULL);
|
---|
1508 | PostQuitMessage(0);
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | if (uMsg != WM_DEVICECHANGE)
|
---|
1512 | return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
---|
1513 |
|
---|
1514 | PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
|
---|
1515 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
---|
1516 | Assert(pThis);
|
---|
1517 | if (pThis == NULL)
|
---|
1518 | return 0;
|
---|
1519 |
|
---|
1520 | switch (wParam)
|
---|
1521 | {
|
---|
1522 | case DBT_DEVICEARRIVAL:
|
---|
1523 | case DBT_DEVICEREMOVECOMPLETE:
|
---|
1524 | // Check whether a CD or DVD was inserted into or removed from a drive.
|
---|
1525 | if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
|
---|
1526 | {
|
---|
1527 | PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
---|
1528 | if ( (lpdbv->dbcv_flags & DBTF_MEDIA)
|
---|
1529 | && (pThis->fUnitMask & lpdbv->dbcv_unitmask))
|
---|
1530 | {
|
---|
1531 | RTCritSectEnter(&pThis->CritSect);
|
---|
1532 | if (wParam == DBT_DEVICEARRIVAL)
|
---|
1533 | {
|
---|
1534 | int cRetries = 10;
|
---|
1535 | int rc = DRVHostBaseMediaPresent(pThis);
|
---|
1536 | while (RT_FAILURE(rc) && cRetries-- > 0)
|
---|
1537 | {
|
---|
1538 | RTThreadSleep(50);
|
---|
1539 | rc = DRVHostBaseMediaPresent(pThis);
|
---|
1540 | }
|
---|
1541 | }
|
---|
1542 | else
|
---|
1543 | DRVHostBaseMediaNotPresent(pThis);
|
---|
1544 | RTCritSectLeave(&pThis->CritSect);
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 | break;
|
---|
1548 | }
|
---|
1549 | return TRUE;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | #endif /* RT_OS_WINDOWS */
|
---|
1553 |
|
---|
1554 |
|
---|
1555 | /**
|
---|
1556 | * This thread will periodically poll the device for media presence.
|
---|
1557 | *
|
---|
1558 | * @returns Ignored.
|
---|
1559 | * @param ThreadSelf Handle of this thread. Ignored.
|
---|
1560 | * @param pvUser Pointer to the driver instance structure.
|
---|
1561 | */
|
---|
1562 | static DECLCALLBACK(int) drvHostBaseMediaThread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
1563 | {
|
---|
1564 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)pvUser;
|
---|
1565 | LogFlow(("%s-%d: drvHostBaseMediaThread: ThreadSelf=%p pvUser=%p\n",
|
---|
1566 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, ThreadSelf, pvUser));
|
---|
1567 | #ifdef RT_OS_WINDOWS
|
---|
1568 | static WNDCLASS s_classDeviceChange = {0};
|
---|
1569 | static ATOM s_hAtomDeviceChange = 0;
|
---|
1570 |
|
---|
1571 | /*
|
---|
1572 | * Register custom window class.
|
---|
1573 | */
|
---|
1574 | if (s_hAtomDeviceChange == 0)
|
---|
1575 | {
|
---|
1576 | memset(&s_classDeviceChange, 0, sizeof(s_classDeviceChange));
|
---|
1577 | s_classDeviceChange.lpfnWndProc = DeviceChangeWindowProc;
|
---|
1578 | s_classDeviceChange.lpszClassName = "VBOX_DeviceChangeClass";
|
---|
1579 | s_classDeviceChange.hInstance = GetModuleHandle("VBoxDD.dll");
|
---|
1580 | Assert(s_classDeviceChange.hInstance);
|
---|
1581 | s_hAtomDeviceChange = RegisterClassA(&s_classDeviceChange);
|
---|
1582 | Assert(s_hAtomDeviceChange);
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | /*
|
---|
1586 | * Create Window w/ the pThis as user data.
|
---|
1587 | */
|
---|
1588 | HWND hwnd = CreateWindow((LPCTSTR)s_hAtomDeviceChange, "", WS_POPUP, 0, 0, 0, 0, 0, 0, s_classDeviceChange.hInstance, 0);
|
---|
1589 | AssertMsg(hwnd, ("CreateWindow failed with %d\n", GetLastError()));
|
---|
1590 | SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
|
---|
1591 |
|
---|
1592 | /*
|
---|
1593 | * Signal the waiting EMT thread that everything went fine.
|
---|
1594 | */
|
---|
1595 | ASMAtomicXchgPtr((void * volatile *)&pThis->hwndDeviceChange, hwnd);
|
---|
1596 | RTThreadUserSignal(ThreadSelf);
|
---|
1597 | if (!hwnd)
|
---|
1598 | {
|
---|
1599 | LogFlow(("%s-%d: drvHostBaseMediaThread: returns VERR_GENERAL_FAILURE\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
1600 | return VERR_GENERAL_FAILURE;
|
---|
1601 | }
|
---|
1602 | LogFlow(("%s-%d: drvHostBaseMediaThread: Created hwndDeviceChange=%p\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, hwnd));
|
---|
1603 |
|
---|
1604 | /*
|
---|
1605 | * Message pump.
|
---|
1606 | */
|
---|
1607 | MSG Msg;
|
---|
1608 | BOOL fRet;
|
---|
1609 | while ((fRet = GetMessage(&Msg, NULL, 0, 0)) != FALSE)
|
---|
1610 | {
|
---|
1611 | if (fRet != -1)
|
---|
1612 | {
|
---|
1613 | TranslateMessage(&Msg);
|
---|
1614 | DispatchMessage(&Msg);
|
---|
1615 | }
|
---|
1616 | //else: handle the error and possibly exit
|
---|
1617 | }
|
---|
1618 | Assert(!pThis->hwndDeviceChange);
|
---|
1619 |
|
---|
1620 | #else /* !RT_OS_WINDOWS */
|
---|
1621 | bool fFirst = true;
|
---|
1622 | int cRetries = 10;
|
---|
1623 | while (!pThis->fShutdownPoller)
|
---|
1624 | {
|
---|
1625 | /*
|
---|
1626 | * Perform the polling (unless we've run out of 50ms retries).
|
---|
1627 | */
|
---|
1628 | if ( pThis->pfnPoll
|
---|
1629 | && cRetries-- > 0)
|
---|
1630 | {
|
---|
1631 |
|
---|
1632 | int rc = pThis->pfnPoll(pThis);
|
---|
1633 | if (RT_FAILURE(rc))
|
---|
1634 | {
|
---|
1635 | RTSemEventWait(pThis->EventPoller, 50);
|
---|
1636 | continue;
|
---|
1637 | }
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | /*
|
---|
1641 | * Signal EMT after the first go.
|
---|
1642 | */
|
---|
1643 | if (fFirst)
|
---|
1644 | {
|
---|
1645 | RTThreadUserSignal(ThreadSelf);
|
---|
1646 | fFirst = false;
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | /*
|
---|
1650 | * Sleep.
|
---|
1651 | */
|
---|
1652 | int rc = RTSemEventWait(pThis->EventPoller, pThis->cMilliesPoller);
|
---|
1653 | if ( RT_FAILURE(rc)
|
---|
1654 | && rc != VERR_TIMEOUT)
|
---|
1655 | {
|
---|
1656 | AssertMsgFailed(("rc=%Rrc\n", rc));
|
---|
1657 | pThis->ThreadPoller = NIL_RTTHREAD;
|
---|
1658 | LogFlow(("drvHostBaseMediaThread: returns %Rrc\n", rc));
|
---|
1659 | return rc;
|
---|
1660 | }
|
---|
1661 | cRetries = 10;
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | #endif /* !RT_OS_WINDOWS */
|
---|
1665 |
|
---|
1666 | /* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
|
---|
1667 | LogFlow(("%s-%d: drvHostBaseMediaThread: returns VINF_SUCCESS\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
1668 | return VINF_SUCCESS;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | /* -=-=-=-=- driver interface -=-=-=-=- */
|
---|
1672 |
|
---|
1673 |
|
---|
1674 | /**
|
---|
1675 | * Done state load operation.
|
---|
1676 | *
|
---|
1677 | * @returns VBox load code.
|
---|
1678 | * @param pDrvIns Driver instance of the driver which registered the data unit.
|
---|
1679 | * @param pSSM SSM operation handle.
|
---|
1680 | */
|
---|
1681 | static DECLCALLBACK(int) drvHostBaseLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
|
---|
1682 | {
|
---|
1683 | RT_NOREF(pSSM);
|
---|
1684 | PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
|
---|
1685 | LogFlow(("%s-%d: drvHostBaseMediaThread:\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
1686 | RTCritSectEnter(&pThis->CritSect);
|
---|
1687 |
|
---|
1688 | /*
|
---|
1689 | * Tell the device/driver above us that the media status is uncertain.
|
---|
1690 | */
|
---|
1691 | if (pThis->pDrvMountNotify)
|
---|
1692 | {
|
---|
1693 | pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
|
---|
1694 | if (pThis->fMediaPresent)
|
---|
1695 | pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | RTCritSectLeave(&pThis->CritSect);
|
---|
1699 | return VINF_SUCCESS;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 |
|
---|
1703 | /** @copydoc FNPDMDRVDESTRUCT */
|
---|
1704 | DECLCALLBACK(void) DRVHostBaseDestruct(PPDMDRVINS pDrvIns)
|
---|
1705 | {
|
---|
1706 | PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
|
---|
1707 | LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
|
---|
1708 |
|
---|
1709 | /*
|
---|
1710 | * Terminate the thread.
|
---|
1711 | */
|
---|
1712 | if (pThis->ThreadPoller != NIL_RTTHREAD)
|
---|
1713 | {
|
---|
1714 | pThis->fShutdownPoller = true;
|
---|
1715 | int rc;
|
---|
1716 | int cTimes = 50;
|
---|
1717 | do
|
---|
1718 | {
|
---|
1719 | #ifdef RT_OS_WINDOWS
|
---|
1720 | if (pThis->hwndDeviceChange)
|
---|
1721 | PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
|
---|
1722 | #else
|
---|
1723 | RTSemEventSignal(pThis->EventPoller);
|
---|
1724 | #endif
|
---|
1725 | rc = RTThreadWait(pThis->ThreadPoller, 100, NULL);
|
---|
1726 | } while (cTimes-- > 0 && rc == VERR_TIMEOUT);
|
---|
1727 |
|
---|
1728 | if (!rc)
|
---|
1729 | pThis->ThreadPoller = NIL_RTTHREAD;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | /*
|
---|
1733 | * Unlock the drive if we've locked it or we're in passthru mode.
|
---|
1734 | */
|
---|
1735 | #ifdef RT_OS_DARWIN
|
---|
1736 | if ( ( pThis->fLocked
|
---|
1737 | || pThis->IMedia.pfnSendCmd)
|
---|
1738 | && pThis->ppScsiTaskDI
|
---|
1739 | #else /** @todo Check if the other guys can mix pfnDoLock with scsi passthru.
|
---|
1740 | * (We're currently not unlocking the device after use. See todo in DevATA.cpp.) */
|
---|
1741 | if ( pThis->fLocked
|
---|
1742 | && pThis->hFileDevice != NIL_RTFILE
|
---|
1743 | #endif
|
---|
1744 | && pThis->pfnDoLock)
|
---|
1745 | {
|
---|
1746 | int rc = pThis->pfnDoLock(pThis, false);
|
---|
1747 | if (RT_SUCCESS(rc))
|
---|
1748 | pThis->fLocked = false;
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | /*
|
---|
1752 | * Cleanup the other resources.
|
---|
1753 | */
|
---|
1754 | #ifdef RT_OS_WINDOWS
|
---|
1755 | if (pThis->hwndDeviceChange)
|
---|
1756 | {
|
---|
1757 | if (SetWindowLongPtr(pThis->hwndDeviceChange, GWLP_USERDATA, 0) == (LONG_PTR)pThis)
|
---|
1758 | PostMessage(pThis->hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
|
---|
1759 | pThis->hwndDeviceChange = NULL;
|
---|
1760 | }
|
---|
1761 | #else
|
---|
1762 | if (pThis->EventPoller != NULL)
|
---|
1763 | {
|
---|
1764 | RTSemEventDestroy(pThis->EventPoller);
|
---|
1765 | pThis->EventPoller = NULL;
|
---|
1766 | }
|
---|
1767 | #endif
|
---|
1768 |
|
---|
1769 | #ifdef RT_OS_DARWIN
|
---|
1770 | /*
|
---|
1771 | * The unclaiming doesn't seem to mean much, the DVD is actually
|
---|
1772 | * remounted when we release exclusive access. I'm not quite sure
|
---|
1773 | * if I should put the unclaim first or not...
|
---|
1774 | *
|
---|
1775 | * Anyway, that it's automatically remounted very good news for us,
|
---|
1776 | * because that means we don't have to mess with that ourselves. Of
|
---|
1777 | * course there is the unlikely scenario that we've succeeded in claiming
|
---|
1778 | * and umount the DVD but somehow failed to gain exclusive scsi access...
|
---|
1779 | */
|
---|
1780 | if (pThis->ppScsiTaskDI)
|
---|
1781 | {
|
---|
1782 | LogFlow(("%s-%d: releasing exclusive scsi access!\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
1783 | (*pThis->ppScsiTaskDI)->ReleaseExclusiveAccess(pThis->ppScsiTaskDI);
|
---|
1784 | (*pThis->ppScsiTaskDI)->Release(pThis->ppScsiTaskDI);
|
---|
1785 | pThis->ppScsiTaskDI = NULL;
|
---|
1786 | }
|
---|
1787 | if (pThis->pDADisk)
|
---|
1788 | {
|
---|
1789 | LogFlow(("%s-%d: unclaiming the disk!\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
1790 | DADiskUnclaim(pThis->pDADisk);
|
---|
1791 | CFRelease(pThis->pDADisk);
|
---|
1792 | pThis->pDADisk = NULL;
|
---|
1793 | }
|
---|
1794 | if (pThis->ppMMCDI)
|
---|
1795 | {
|
---|
1796 | LogFlow(("%s-%d: releasing the MMC object!\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
1797 | (*pThis->ppMMCDI)->Release(pThis->ppMMCDI);
|
---|
1798 | pThis->ppMMCDI = NULL;
|
---|
1799 | }
|
---|
1800 | if (pThis->MasterPort != IO_OBJECT_NULL)
|
---|
1801 | {
|
---|
1802 | mach_port_deallocate(mach_task_self(), pThis->MasterPort);
|
---|
1803 | pThis->MasterPort = IO_OBJECT_NULL;
|
---|
1804 | }
|
---|
1805 | if (pThis->pDASession)
|
---|
1806 | {
|
---|
1807 | LogFlow(("%s-%d: releasing the DA session!\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
1808 | CFRelease(pThis->pDASession);
|
---|
1809 | pThis->pDASession = NULL;
|
---|
1810 | }
|
---|
1811 | #else
|
---|
1812 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
1813 | {
|
---|
1814 | int rc = RTFileClose(pThis->hFileDevice);
|
---|
1815 | AssertRC(rc);
|
---|
1816 | pThis->hFileDevice = NIL_RTFILE;
|
---|
1817 | }
|
---|
1818 | #endif
|
---|
1819 |
|
---|
1820 | #ifdef RT_OS_SOLARIS
|
---|
1821 | if (pThis->hFileRawDevice != NIL_RTFILE)
|
---|
1822 | {
|
---|
1823 | int rc = RTFileClose(pThis->hFileRawDevice);
|
---|
1824 | AssertRC(rc);
|
---|
1825 | pThis->hFileRawDevice = NIL_RTFILE;
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | if (pThis->pszRawDeviceOpen)
|
---|
1829 | {
|
---|
1830 | RTStrFree(pThis->pszRawDeviceOpen);
|
---|
1831 | pThis->pszRawDeviceOpen = NULL;
|
---|
1832 | }
|
---|
1833 | #endif
|
---|
1834 |
|
---|
1835 | if (pThis->pszDevice)
|
---|
1836 | {
|
---|
1837 | MMR3HeapFree(pThis->pszDevice);
|
---|
1838 | pThis->pszDevice = NULL;
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | if (pThis->pszDeviceOpen)
|
---|
1842 | {
|
---|
1843 | RTStrFree(pThis->pszDeviceOpen);
|
---|
1844 | pThis->pszDeviceOpen = NULL;
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | /* Forget about the notifications. */
|
---|
1848 | pThis->pDrvMountNotify = NULL;
|
---|
1849 |
|
---|
1850 | /* Leave the instance operational if this is just a cleanup of the state
|
---|
1851 | * after an attach error happened. So don't destroy the critsect then. */
|
---|
1852 | if (!pThis->fKeepInstance && RTCritSectIsInitialized(&pThis->CritSect))
|
---|
1853 | RTCritSectDelete(&pThis->CritSect);
|
---|
1854 | LogFlow(("%s-%d: drvHostBaseDestruct completed\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 |
|
---|
1858 | /**
|
---|
1859 | * Initializes the instance data (init part 1).
|
---|
1860 | *
|
---|
1861 | * The driver which derives from this base driver will override function pointers after
|
---|
1862 | * calling this method, and complete the construction by calling DRVHostBaseInitFinish().
|
---|
1863 | *
|
---|
1864 | * On failure call DRVHostBaseDestruct().
|
---|
1865 | *
|
---|
1866 | * @returns VBox status code.
|
---|
1867 | * @param pDrvIns Driver instance.
|
---|
1868 | * @param pCfg Configuration handle.
|
---|
1869 | * @param enmType Device type.
|
---|
1870 | */
|
---|
1871 | int DRVHostBaseInitData(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, PDMMEDIATYPE enmType)
|
---|
1872 | {
|
---|
1873 | PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
|
---|
1874 | LogFlow(("%s-%d: DRVHostBaseInitData: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
|
---|
1875 |
|
---|
1876 | /*
|
---|
1877 | * Initialize most of the data members.
|
---|
1878 | */
|
---|
1879 | pThis->pDrvIns = pDrvIns;
|
---|
1880 | pThis->fKeepInstance = false;
|
---|
1881 | pThis->ThreadPoller = NIL_RTTHREAD;
|
---|
1882 | #ifdef RT_OS_DARWIN
|
---|
1883 | pThis->MasterPort = IO_OBJECT_NULL;
|
---|
1884 | pThis->ppMMCDI = NULL;
|
---|
1885 | pThis->ppScsiTaskDI = NULL;
|
---|
1886 | pThis->cbBlock = 0;
|
---|
1887 | pThis->pDADisk = NULL;
|
---|
1888 | pThis->pDASession = NULL;
|
---|
1889 | #else
|
---|
1890 | pThis->hFileDevice = NIL_RTFILE;
|
---|
1891 | #endif
|
---|
1892 | #ifdef RT_OS_SOLARIS
|
---|
1893 | pThis->hFileRawDevice = NIL_RTFILE;
|
---|
1894 | #endif
|
---|
1895 | pThis->enmType = enmType;
|
---|
1896 | //pThis->cErrors = 0;
|
---|
1897 | pThis->fAttachFailError = true; /* It's an error until we've read the config. */
|
---|
1898 |
|
---|
1899 | pThis->pfnGetMediaSize = drvHostBaseGetMediaSize;
|
---|
1900 |
|
---|
1901 | /* IBase. */
|
---|
1902 | pDrvIns->IBase.pfnQueryInterface = drvHostBaseQueryInterface;
|
---|
1903 |
|
---|
1904 | /* IMedia. */
|
---|
1905 | pThis->IMedia.pfnRead = drvHostBaseRead;
|
---|
1906 | pThis->IMedia.pfnWrite = drvHostBaseWrite;
|
---|
1907 | pThis->IMedia.pfnFlush = drvHostBaseFlush;
|
---|
1908 | pThis->IMedia.pfnIsReadOnly = drvHostBaseIsReadOnly;
|
---|
1909 | pThis->IMedia.pfnGetSize = drvHostBaseGetSize;
|
---|
1910 | pThis->IMedia.pfnGetType = drvHostBaseGetType;
|
---|
1911 | pThis->IMedia.pfnGetUuid = drvHostBaseGetUuid;
|
---|
1912 | pThis->IMedia.pfnIoBufAlloc = drvHostBaseIoBufAlloc;
|
---|
1913 | pThis->IMedia.pfnIoBufFree = drvHostBaseIoBufFree;
|
---|
1914 | pThis->IMedia.pfnBiosGetPCHSGeometry = drvHostBaseGetPCHSGeometry;
|
---|
1915 | pThis->IMedia.pfnBiosSetPCHSGeometry = drvHostBaseSetPCHSGeometry;
|
---|
1916 | pThis->IMedia.pfnBiosGetLCHSGeometry = drvHostBaseGetLCHSGeometry;
|
---|
1917 | pThis->IMedia.pfnBiosSetLCHSGeometry = drvHostBaseSetLCHSGeometry;
|
---|
1918 | pThis->IMedia.pfnBiosIsVisible = drvHostBaseIsVisible;
|
---|
1919 |
|
---|
1920 | /* IMount. */
|
---|
1921 | pThis->IMount.pfnUnmount = drvHostBaseUnmount;
|
---|
1922 | pThis->IMount.pfnIsMounted = drvHostBaseIsMounted;
|
---|
1923 | pThis->IMount.pfnLock = drvHostBaseLock;
|
---|
1924 | pThis->IMount.pfnUnlock = drvHostBaseUnlock;
|
---|
1925 | pThis->IMount.pfnIsLocked = drvHostBaseIsLocked;
|
---|
1926 |
|
---|
1927 | /*
|
---|
1928 | * Get the IBlockPort & IMountNotify interfaces of the above driver/device.
|
---|
1929 | */
|
---|
1930 | pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
|
---|
1931 | if (!pThis->pDrvMediaPort)
|
---|
1932 | {
|
---|
1933 | AssertMsgFailed(("Configuration error: No media port interface above!\n"));
|
---|
1934 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
1935 | }
|
---|
1936 | pThis->pDrvMountNotify = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMOUNTNOTIFY);
|
---|
1937 |
|
---|
1938 | /*
|
---|
1939 | * Query configuration.
|
---|
1940 | */
|
---|
1941 | /* Device */
|
---|
1942 | int rc = CFGMR3QueryStringAlloc(pCfg, "Path", &pThis->pszDevice);
|
---|
1943 | if (RT_FAILURE(rc))
|
---|
1944 | {
|
---|
1945 | AssertMsgFailed(("Configuration error: query for \"Path\" string returned %Rra.\n", rc));
|
---|
1946 | return rc;
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | /* Mountable */
|
---|
1950 | uint32_t u32;
|
---|
1951 | rc = CFGMR3QueryU32(pCfg, "Interval", &u32);
|
---|
1952 | if (RT_SUCCESS(rc))
|
---|
1953 | pThis->cMilliesPoller = u32;
|
---|
1954 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1955 | pThis->cMilliesPoller = 1000;
|
---|
1956 | else if (RT_FAILURE(rc))
|
---|
1957 | {
|
---|
1958 | AssertMsgFailed(("Configuration error: Query \"Mountable\" resulted in %Rrc.\n", rc));
|
---|
1959 | return rc;
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | /* ReadOnly */
|
---|
1963 | rc = CFGMR3QueryBool(pCfg, "ReadOnly", &pThis->fReadOnlyConfig);
|
---|
1964 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1965 | pThis->fReadOnlyConfig = enmType == PDMMEDIATYPE_DVD || enmType == PDMMEDIATYPE_CDROM ? true : false;
|
---|
1966 | else if (RT_FAILURE(rc))
|
---|
1967 | {
|
---|
1968 | AssertMsgFailed(("Configuration error: Query \"ReadOnly\" resulted in %Rrc.\n", rc));
|
---|
1969 | return rc;
|
---|
1970 | }
|
---|
1971 |
|
---|
1972 | /* Locked */
|
---|
1973 | rc = CFGMR3QueryBool(pCfg, "Locked", &pThis->fLocked);
|
---|
1974 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1975 | pThis->fLocked = false;
|
---|
1976 | else if (RT_FAILURE(rc))
|
---|
1977 | {
|
---|
1978 | AssertMsgFailed(("Configuration error: Query \"Locked\" resulted in %Rrc.\n", rc));
|
---|
1979 | return rc;
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | /* BIOS visible */
|
---|
1983 | rc = CFGMR3QueryBool(pCfg, "BIOSVisible", &pThis->fBiosVisible);
|
---|
1984 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1985 | pThis->fBiosVisible = true;
|
---|
1986 | else if (RT_FAILURE(rc))
|
---|
1987 | {
|
---|
1988 | AssertMsgFailed(("Configuration error: Query \"BIOSVisible\" resulted in %Rrc.\n", rc));
|
---|
1989 | return rc;
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | /* Uuid */
|
---|
1993 | char *psz;
|
---|
1994 | rc = CFGMR3QueryStringAlloc(pCfg, "Uuid", &psz);
|
---|
1995 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1996 | RTUuidClear(&pThis->Uuid);
|
---|
1997 | else if (RT_SUCCESS(rc))
|
---|
1998 | {
|
---|
1999 | rc = RTUuidFromStr(&pThis->Uuid, psz);
|
---|
2000 | if (RT_FAILURE(rc))
|
---|
2001 | {
|
---|
2002 | AssertMsgFailed(("Configuration error: Uuid from string failed on \"%s\", rc=%Rrc.\n", psz, rc));
|
---|
2003 | MMR3HeapFree(psz);
|
---|
2004 | return rc;
|
---|
2005 | }
|
---|
2006 | MMR3HeapFree(psz);
|
---|
2007 | }
|
---|
2008 | else
|
---|
2009 | {
|
---|
2010 | AssertMsgFailed(("Configuration error: Failed to obtain the uuid, rc=%Rrc.\n", rc));
|
---|
2011 | return rc;
|
---|
2012 | }
|
---|
2013 |
|
---|
2014 | /* Define whether attach failure is an error (default) or not. */
|
---|
2015 | bool fAttachFailError;
|
---|
2016 | rc = CFGMR3QueryBool(pCfg, "AttachFailError", &fAttachFailError);
|
---|
2017 | if (RT_FAILURE(rc))
|
---|
2018 | fAttachFailError = true;
|
---|
2019 | pThis->fAttachFailError = fAttachFailError;
|
---|
2020 |
|
---|
2021 | /* name to open & watch for */
|
---|
2022 | #ifdef RT_OS_WINDOWS
|
---|
2023 | int iBit = RT_C_TO_UPPER(pThis->pszDevice[0]) - 'A';
|
---|
2024 | if ( iBit > 'Z' - 'A'
|
---|
2025 | || pThis->pszDevice[1] != ':'
|
---|
2026 | || pThis->pszDevice[2])
|
---|
2027 | {
|
---|
2028 | AssertMsgFailed(("Configuration error: Invalid drive specification: '%s'\n", pThis->pszDevice));
|
---|
2029 | return VERR_INVALID_PARAMETER;
|
---|
2030 | }
|
---|
2031 | pThis->fUnitMask = 1 << iBit;
|
---|
2032 | RTStrAPrintf(&pThis->pszDeviceOpen, "\\\\.\\%s", pThis->pszDevice);
|
---|
2033 |
|
---|
2034 | #elif defined(RT_OS_SOLARIS)
|
---|
2035 | char *pszBlockDevName = getfullblkname(pThis->pszDevice);
|
---|
2036 | if (!pszBlockDevName)
|
---|
2037 | return VERR_NO_MEMORY;
|
---|
2038 | pThis->pszDeviceOpen = RTStrDup(pszBlockDevName); /* for RTStrFree() */
|
---|
2039 | free(pszBlockDevName);
|
---|
2040 | pThis->pszRawDeviceOpen = RTStrDup(pThis->pszDevice);
|
---|
2041 |
|
---|
2042 | #else
|
---|
2043 | pThis->pszDeviceOpen = RTStrDup(pThis->pszDevice);
|
---|
2044 | #endif
|
---|
2045 |
|
---|
2046 | if (!pThis->pszDeviceOpen)
|
---|
2047 | return VERR_NO_MEMORY;
|
---|
2048 |
|
---|
2049 | return VINF_SUCCESS;
|
---|
2050 | }
|
---|
2051 |
|
---|
2052 |
|
---|
2053 | /**
|
---|
2054 | * Do the 2nd part of the init after the derived driver has overridden the defaults.
|
---|
2055 | *
|
---|
2056 | * On failure call DRVHostBaseDestruct().
|
---|
2057 | *
|
---|
2058 | * @returns VBox status code.
|
---|
2059 | * @param pThis Pointer to the instance data.
|
---|
2060 | */
|
---|
2061 | int DRVHostBaseInitFinish(PDRVHOSTBASE pThis)
|
---|
2062 | {
|
---|
2063 | int src = VINF_SUCCESS;
|
---|
2064 | PPDMDRVINS pDrvIns = pThis->pDrvIns;
|
---|
2065 |
|
---|
2066 | /* log config summary */
|
---|
2067 | Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%RTuuid\n",
|
---|
2068 | pDrvIns->pReg->szName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
|
---|
2069 | pThis->fReadOnlyConfig, pThis->fLocked, pThis->fBiosVisible, &pThis->Uuid));
|
---|
2070 |
|
---|
2071 | /*
|
---|
2072 | * Check that there are no drivers below us.
|
---|
2073 | */
|
---|
2074 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
2075 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
2076 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
2077 |
|
---|
2078 | /*
|
---|
2079 | * Register saved state.
|
---|
2080 | */
|
---|
2081 | int rc = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvHostBaseLoadDone);
|
---|
2082 | if (RT_FAILURE(rc))
|
---|
2083 | return rc;
|
---|
2084 |
|
---|
2085 | /*
|
---|
2086 | * Verify type.
|
---|
2087 | */
|
---|
2088 | #ifdef RT_OS_WINDOWS
|
---|
2089 | UINT uDriveType = GetDriveType(pThis->pszDevice);
|
---|
2090 | switch (pThis->enmType)
|
---|
2091 | {
|
---|
2092 | case PDMMEDIATYPE_FLOPPY_360:
|
---|
2093 | case PDMMEDIATYPE_FLOPPY_720:
|
---|
2094 | case PDMMEDIATYPE_FLOPPY_1_20:
|
---|
2095 | case PDMMEDIATYPE_FLOPPY_1_44:
|
---|
2096 | case PDMMEDIATYPE_FLOPPY_2_88:
|
---|
2097 | case PDMMEDIATYPE_FLOPPY_FAKE_15_6:
|
---|
2098 | case PDMMEDIATYPE_FLOPPY_FAKE_63_5:
|
---|
2099 | if (uDriveType != DRIVE_REMOVABLE)
|
---|
2100 | {
|
---|
2101 | AssertMsgFailed(("Configuration error: '%s' is not a floppy (type=%d)\n",
|
---|
2102 | pThis->pszDevice, uDriveType));
|
---|
2103 | return VERR_INVALID_PARAMETER;
|
---|
2104 | }
|
---|
2105 | break;
|
---|
2106 | case PDMMEDIATYPE_CDROM:
|
---|
2107 | case PDMMEDIATYPE_DVD:
|
---|
2108 | if (uDriveType != DRIVE_CDROM)
|
---|
2109 | {
|
---|
2110 | AssertMsgFailed(("Configuration error: '%s' is not a cdrom (type=%d)\n",
|
---|
2111 | pThis->pszDevice, uDriveType));
|
---|
2112 | return VERR_INVALID_PARAMETER;
|
---|
2113 | }
|
---|
2114 | break;
|
---|
2115 | case PDMMEDIATYPE_HARD_DISK:
|
---|
2116 | default:
|
---|
2117 | AssertMsgFailed(("enmType=%d\n", pThis->enmType));
|
---|
2118 | return VERR_INVALID_PARAMETER;
|
---|
2119 | }
|
---|
2120 | #endif
|
---|
2121 |
|
---|
2122 | /*
|
---|
2123 | * Open the device.
|
---|
2124 | */
|
---|
2125 | #if defined(RT_OS_DARWIN)
|
---|
2126 | rc = drvHostBaseOpen(pThis, NULL, pThis->fReadOnlyConfig);
|
---|
2127 | #else
|
---|
2128 | rc = drvHostBaseReopen(pThis);
|
---|
2129 | #endif
|
---|
2130 | if (RT_FAILURE(rc))
|
---|
2131 | {
|
---|
2132 | char *pszDevice = pThis->pszDevice;
|
---|
2133 | #ifndef RT_OS_DARWIN
|
---|
2134 | char szPathReal[256];
|
---|
2135 | if ( RTPathExists(pszDevice)
|
---|
2136 | && RT_SUCCESS(RTPathReal(pszDevice, szPathReal, sizeof(szPathReal))))
|
---|
2137 | pszDevice = szPathReal;
|
---|
2138 | pThis->hFileDevice = NIL_RTFILE;
|
---|
2139 | #endif
|
---|
2140 | #ifdef RT_OS_SOLARIS
|
---|
2141 | pThis->hFileRawDevice = NIL_RTFILE;
|
---|
2142 | #endif
|
---|
2143 |
|
---|
2144 | /*
|
---|
2145 | * Disable CD/DVD passthrough in case it was enabled. Would cause
|
---|
2146 | * weird failures later when the guest issues commands. These would
|
---|
2147 | * all fail because of the invalid file handle. So use the normal
|
---|
2148 | * virtual CD/DVD code, which deals more gracefully with unavailable
|
---|
2149 | * "media" - actually a complete drive in this case.
|
---|
2150 | */
|
---|
2151 | pThis->IMedia.pfnSendCmd = NULL;
|
---|
2152 | AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pszDevice, rc));
|
---|
2153 | switch (rc)
|
---|
2154 | {
|
---|
2155 | case VERR_ACCESS_DENIED:
|
---|
2156 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
2157 | #ifdef RT_OS_LINUX
|
---|
2158 | N_("Cannot open host device '%s' for %s access. Check the permissions "
|
---|
2159 | "of that device ('/bin/ls -l %s'): Most probably you need to be member "
|
---|
2160 | "of the device group. Make sure that you logout/login after changing "
|
---|
2161 | "the group settings of the current user"),
|
---|
2162 | #else
|
---|
2163 | N_("Cannot open host device '%s' for %s access. Check the permissions "
|
---|
2164 | "of that device"),
|
---|
2165 | #endif
|
---|
2166 | pszDevice, pThis->fReadOnlyConfig ? "readonly" : "read/write",
|
---|
2167 | pszDevice);
|
---|
2168 | default:
|
---|
2169 | {
|
---|
2170 | if (pThis->fAttachFailError)
|
---|
2171 | return rc;
|
---|
2172 | int erc = PDMDrvHlpVMSetRuntimeError(pDrvIns, 0 /*fFlags*/,
|
---|
2173 | "DrvHost_MOUNTFAIL",
|
---|
2174 | N_("Cannot attach to host device '%s'"), pszDevice);
|
---|
2175 | AssertRC(erc);
|
---|
2176 | src = rc;
|
---|
2177 | }
|
---|
2178 | }
|
---|
2179 | }
|
---|
2180 | #ifdef RT_OS_WINDOWS
|
---|
2181 | if (RT_SUCCESS(src))
|
---|
2182 | DRVHostBaseMediaPresent(pThis);
|
---|
2183 | #endif
|
---|
2184 |
|
---|
2185 | /*
|
---|
2186 | * Lock the drive if that's required by the configuration.
|
---|
2187 | */
|
---|
2188 | if (pThis->fLocked)
|
---|
2189 | {
|
---|
2190 | if (pThis->pfnDoLock)
|
---|
2191 | rc = pThis->pfnDoLock(pThis, true);
|
---|
2192 | if (RT_FAILURE(rc))
|
---|
2193 | {
|
---|
2194 | AssertMsgFailed(("Failed to lock the dvd drive. rc=%Rrc\n", rc));
|
---|
2195 | return rc;
|
---|
2196 | }
|
---|
2197 | }
|
---|
2198 |
|
---|
2199 | #ifndef RT_OS_WINDOWS
|
---|
2200 | if (RT_SUCCESS(src))
|
---|
2201 | {
|
---|
2202 | /*
|
---|
2203 | * Create the event semaphore which the poller thread will wait on.
|
---|
2204 | */
|
---|
2205 | rc = RTSemEventCreate(&pThis->EventPoller);
|
---|
2206 | if (RT_FAILURE(rc))
|
---|
2207 | return rc;
|
---|
2208 | }
|
---|
2209 | #endif
|
---|
2210 |
|
---|
2211 | /*
|
---|
2212 | * Initialize the critical section used for serializing the access to the media.
|
---|
2213 | */
|
---|
2214 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
2215 | if (RT_FAILURE(rc))
|
---|
2216 | return rc;
|
---|
2217 |
|
---|
2218 | if (RT_SUCCESS(src))
|
---|
2219 | {
|
---|
2220 | /*
|
---|
2221 | * Start the thread which will poll for the media.
|
---|
2222 | */
|
---|
2223 | rc = RTThreadCreate(&pThis->ThreadPoller, drvHostBaseMediaThread, pThis, 0,
|
---|
2224 | RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "DVDMEDIA");
|
---|
2225 | if (RT_FAILURE(rc))
|
---|
2226 | {
|
---|
2227 | AssertMsgFailed(("Failed to create poller thread. rc=%Rrc\n", rc));
|
---|
2228 | return rc;
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | /*
|
---|
2232 | * Wait for the thread to start up (!w32:) and do one detection loop.
|
---|
2233 | */
|
---|
2234 | rc = RTThreadUserWait(pThis->ThreadPoller, 10000);
|
---|
2235 | AssertRC(rc);
|
---|
2236 | #ifdef RT_OS_WINDOWS
|
---|
2237 | if (!pThis->hwndDeviceChange)
|
---|
2238 | return VERR_GENERAL_FAILURE;
|
---|
2239 | #endif
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | if (RT_FAILURE(src))
|
---|
2243 | return src;
|
---|
2244 | return rc;
|
---|
2245 | }
|
---|
2246 |
|
---|