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