1 | /* $Id: DrvHostBase-win.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvHostBase - Host base drive access driver, Windows specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | #include <iprt/nt/nt-and-windows.h>
|
---|
24 | #include <dbt.h>
|
---|
25 | #include <ntddscsi.h>
|
---|
26 |
|
---|
27 | #include <iprt/ctype.h>
|
---|
28 | #include <iprt/file.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/scsi.h>
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Host backend specific data.
|
---|
35 | */
|
---|
36 | typedef struct DRVHOSTBASEOS
|
---|
37 | {
|
---|
38 | /** The filehandle of the device. */
|
---|
39 | RTFILE hFileDevice;
|
---|
40 | /** Handle to the window we use to catch the device change broadcast messages. */
|
---|
41 | volatile HWND hwndDeviceChange;
|
---|
42 | /** The unit mask. */
|
---|
43 | DWORD fUnitMask;
|
---|
44 | /** Handle of the poller thread. */
|
---|
45 | RTTHREAD hThrdMediaChange;
|
---|
46 | } DRVHOSTBASEOS;
|
---|
47 | /** Pointer to the host backend specific data. */
|
---|
48 | typedef DRVHOSTBASEOS *PDRVHOSBASEOS;
|
---|
49 | AssertCompile(sizeof(DRVHOSTBASEOS) <= 64);
|
---|
50 |
|
---|
51 | #define DRVHOSTBASE_OS_INT_DECLARED
|
---|
52 | #include "DrvHostBase.h"
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Defined Constants And Macros *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | /** Maximum buffer size we support, check whether darwin has some real upper limit. */
|
---|
59 | #define WIN_SCSI_MAX_BUFFER_SIZE (100 * _1K)
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Window procedure for the invisible window used to catch the WM_DEVICECHANGE broadcasts.
|
---|
65 | */
|
---|
66 | static LRESULT CALLBACK DeviceChangeWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
67 | {
|
---|
68 | Log2(("DeviceChangeWindowProc: hwnd=%08x uMsg=%08x\n", hwnd, uMsg));
|
---|
69 | if (uMsg == WM_DESTROY)
|
---|
70 | {
|
---|
71 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
---|
72 | if (pThis)
|
---|
73 | ASMAtomicXchgSize(&pThis->Os.hwndDeviceChange, NULL);
|
---|
74 | PostQuitMessage(0);
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (uMsg != WM_DEVICECHANGE)
|
---|
78 | return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
---|
79 |
|
---|
80 | PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
|
---|
81 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
---|
82 | Assert(pThis);
|
---|
83 | if (pThis == NULL)
|
---|
84 | return 0;
|
---|
85 |
|
---|
86 | switch (wParam)
|
---|
87 | {
|
---|
88 | case DBT_DEVICEARRIVAL:
|
---|
89 | case DBT_DEVICEREMOVECOMPLETE:
|
---|
90 | // Check whether a CD or DVD was inserted into or removed from a drive.
|
---|
91 | if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
|
---|
92 | {
|
---|
93 | PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
---|
94 | if ( (lpdbv->dbcv_flags & DBTF_MEDIA)
|
---|
95 | && (pThis->Os.fUnitMask & lpdbv->dbcv_unitmask))
|
---|
96 | {
|
---|
97 | RTCritSectEnter(&pThis->CritSect);
|
---|
98 | if (wParam == DBT_DEVICEARRIVAL)
|
---|
99 | {
|
---|
100 | int cRetries = 10;
|
---|
101 | int rc = DRVHostBaseMediaPresent(pThis);
|
---|
102 | while (RT_FAILURE(rc) && cRetries-- > 0)
|
---|
103 | {
|
---|
104 | RTThreadSleep(50);
|
---|
105 | rc = DRVHostBaseMediaPresent(pThis);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | else
|
---|
109 | DRVHostBaseMediaNotPresent(pThis);
|
---|
110 | RTCritSectLeave(&pThis->CritSect);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | return TRUE;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * This thread will wait for changed media notificatons.
|
---|
121 | *
|
---|
122 | * @returns Ignored.
|
---|
123 | * @param ThreadSelf Handle of this thread. Ignored.
|
---|
124 | * @param pvUser Pointer to the driver instance structure.
|
---|
125 | */
|
---|
126 | static DECLCALLBACK(int) drvHostBaseMediaThreadWin(RTTHREAD ThreadSelf, void *pvUser)
|
---|
127 | {
|
---|
128 | PDRVHOSTBASE pThis = (PDRVHOSTBASE)pvUser;
|
---|
129 | LogFlow(("%s-%d: drvHostBaseMediaThreadWin: ThreadSelf=%p pvUser=%p\n",
|
---|
130 | pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, ThreadSelf, pvUser));
|
---|
131 | static WNDCLASS s_classDeviceChange = {0};
|
---|
132 | static ATOM s_hAtomDeviceChange = 0;
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Register custom window class.
|
---|
136 | */
|
---|
137 | if (s_hAtomDeviceChange == 0)
|
---|
138 | {
|
---|
139 | memset(&s_classDeviceChange, 0, sizeof(s_classDeviceChange));
|
---|
140 | s_classDeviceChange.lpfnWndProc = DeviceChangeWindowProc;
|
---|
141 | s_classDeviceChange.lpszClassName = "VBOX_DeviceChangeClass";
|
---|
142 | s_classDeviceChange.hInstance = GetModuleHandle("VBoxDD.dll");
|
---|
143 | Assert(s_classDeviceChange.hInstance);
|
---|
144 | s_hAtomDeviceChange = RegisterClassA(&s_classDeviceChange);
|
---|
145 | Assert(s_hAtomDeviceChange);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Create Window w/ the pThis as user data.
|
---|
150 | */
|
---|
151 | HWND hwnd = CreateWindow((LPCTSTR)s_hAtomDeviceChange, "", WS_POPUP, 0, 0, 0, 0, 0, 0, s_classDeviceChange.hInstance, 0);
|
---|
152 | AssertMsg(hwnd, ("CreateWindow failed with %d\n", GetLastError()));
|
---|
153 | SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Signal the waiting EMT thread that everything went fine.
|
---|
157 | */
|
---|
158 | ASMAtomicXchgPtr((void * volatile *)&pThis->Os.hwndDeviceChange, hwnd);
|
---|
159 | RTThreadUserSignal(ThreadSelf);
|
---|
160 | if (!hwnd)
|
---|
161 | {
|
---|
162 | LogFlow(("%s-%d: drvHostBaseMediaThreadWin: returns VERR_GENERAL_FAILURE\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
163 | return VERR_GENERAL_FAILURE;
|
---|
164 | }
|
---|
165 | LogFlow(("%s-%d: drvHostBaseMediaThreadWin: Created hwndDeviceChange=%p\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, hwnd));
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Message pump.
|
---|
169 | */
|
---|
170 | MSG Msg;
|
---|
171 | BOOL fRet;
|
---|
172 | while ((fRet = GetMessage(&Msg, NULL, 0, 0)) != FALSE)
|
---|
173 | {
|
---|
174 | if (fRet != -1)
|
---|
175 | {
|
---|
176 | TranslateMessage(&Msg);
|
---|
177 | DispatchMessage(&Msg);
|
---|
178 | }
|
---|
179 | //else: handle the error and possibly exit
|
---|
180 | }
|
---|
181 | Assert(!pThis->Os.hwndDeviceChange);
|
---|
182 | /* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
|
---|
183 | LogFlow(("%s-%d: drvHostBaseMediaThreadWin: returns VINF_SUCCESS\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | DECLHIDDEN(int) drvHostBaseScsiCmdOs(PDRVHOSTBASE pThis, const uint8_t *pbCmd, size_t cbCmd, PDMMEDIATXDIR enmTxDir,
|
---|
189 | void *pvBuf, uint32_t *pcbBuf, uint8_t *pbSense, size_t cbSense, uint32_t cTimeoutMillies)
|
---|
190 | {
|
---|
191 | /*
|
---|
192 | * Minimal input validation.
|
---|
193 | */
|
---|
194 | Assert(enmTxDir == PDMMEDIATXDIR_NONE || enmTxDir == PDMMEDIATXDIR_FROM_DEVICE || enmTxDir == PDMMEDIATXDIR_TO_DEVICE);
|
---|
195 | Assert(!pvBuf || pcbBuf);
|
---|
196 | Assert(pvBuf || enmTxDir == PDMMEDIATXDIR_NONE);
|
---|
197 | Assert(pbSense || !cbSense);
|
---|
198 | AssertPtr(pbCmd);
|
---|
199 | Assert(cbCmd <= 16 && cbCmd >= 1); RT_NOREF(cbCmd);
|
---|
200 |
|
---|
201 | int rc = VERR_GENERAL_FAILURE;
|
---|
202 | int direction;
|
---|
203 | struct _REQ
|
---|
204 | {
|
---|
205 | SCSI_PASS_THROUGH_DIRECT spt;
|
---|
206 | uint8_t aSense[64];
|
---|
207 | } Req;
|
---|
208 | DWORD cbReturned = 0;
|
---|
209 |
|
---|
210 | switch (enmTxDir)
|
---|
211 | {
|
---|
212 | case PDMMEDIATXDIR_NONE:
|
---|
213 | direction = SCSI_IOCTL_DATA_UNSPECIFIED;
|
---|
214 | break;
|
---|
215 | case PDMMEDIATXDIR_FROM_DEVICE:
|
---|
216 | Assert(*pcbBuf != 0);
|
---|
217 | /* Make sure that the buffer is clear for commands reading
|
---|
218 | * data. The actually received data may be shorter than what
|
---|
219 | * we expect, and due to the unreliable feedback about how much
|
---|
220 | * data the ioctl actually transferred, it's impossible to
|
---|
221 | * prevent that. Returning previous buffer contents may cause
|
---|
222 | * security problems inside the guest OS, if users can issue
|
---|
223 | * commands to the CDROM device. */
|
---|
224 | memset(pvBuf, '\0', *pcbBuf);
|
---|
225 | direction = SCSI_IOCTL_DATA_IN;
|
---|
226 | break;
|
---|
227 | case PDMMEDIATXDIR_TO_DEVICE:
|
---|
228 | direction = SCSI_IOCTL_DATA_OUT;
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | AssertMsgFailed(("enmTxDir invalid!\n"));
|
---|
232 | direction = SCSI_IOCTL_DATA_UNSPECIFIED;
|
---|
233 | }
|
---|
234 | memset(&Req, '\0', sizeof(Req));
|
---|
235 | Req.spt.Length = sizeof(Req.spt);
|
---|
236 | Req.spt.CdbLength = 12;
|
---|
237 | memcpy(Req.spt.Cdb, pbCmd, Req.spt.CdbLength);
|
---|
238 | Req.spt.DataBuffer = pvBuf;
|
---|
239 | Req.spt.DataTransferLength = *pcbBuf;
|
---|
240 | Req.spt.DataIn = direction;
|
---|
241 | Req.spt.TimeOutValue = (cTimeoutMillies + 999) / 1000; /* Convert to seconds */
|
---|
242 | Assert(cbSense <= sizeof(Req.aSense));
|
---|
243 | Req.spt.SenseInfoLength = (UCHAR)RT_MIN(sizeof(Req.aSense), cbSense);
|
---|
244 | Req.spt.SenseInfoOffset = RT_UOFFSETOF(struct _REQ, aSense);
|
---|
245 | if (DeviceIoControl((HANDLE)RTFileToNative(pThis->Os.hFileDevice), IOCTL_SCSI_PASS_THROUGH_DIRECT,
|
---|
246 | &Req, sizeof(Req), &Req, sizeof(Req), &cbReturned, NULL))
|
---|
247 | {
|
---|
248 | if (cbReturned > RT_UOFFSETOF(struct _REQ, aSense))
|
---|
249 | memcpy(pbSense, Req.aSense, cbSense);
|
---|
250 | else
|
---|
251 | memset(pbSense, '\0', cbSense);
|
---|
252 | /* Windows shares the property of not properly reflecting the actually
|
---|
253 | * transferred data size. See above. Assume that everything worked ok.
|
---|
254 | * Except if there are sense information. */
|
---|
255 | rc = (pbSense[2] & 0x0f) == SCSI_SENSE_NONE
|
---|
256 | ? VINF_SUCCESS
|
---|
257 | : VERR_DEV_IO_ERROR;
|
---|
258 | }
|
---|
259 | else
|
---|
260 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
261 | Log2(("%s: scsistatus=%d bytes returned=%d tlength=%d\n", __FUNCTION__, Req.spt.ScsiStatus, cbReturned, Req.spt.DataTransferLength));
|
---|
262 |
|
---|
263 | return rc;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | DECLHIDDEN(size_t) drvHostBaseScsiCmdGetBufLimitOs(PDRVHOSTBASE pThis)
|
---|
268 | {
|
---|
269 | RT_NOREF(pThis);
|
---|
270 |
|
---|
271 | return WIN_SCSI_MAX_BUFFER_SIZE;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | DECLHIDDEN(int) drvHostBaseGetMediaSizeOs(PDRVHOSTBASE pThis, uint64_t *pcb)
|
---|
276 | {
|
---|
277 | int rc = VERR_GENERAL_FAILURE;
|
---|
278 |
|
---|
279 | if (PDMMEDIATYPE_IS_FLOPPY(pThis->enmType))
|
---|
280 | {
|
---|
281 | DISK_GEOMETRY geom;
|
---|
282 | DWORD cbBytesReturned;
|
---|
283 | int cbSectors;
|
---|
284 |
|
---|
285 | memset(&geom, 0, sizeof(geom));
|
---|
286 | rc = DeviceIoControl((HANDLE)RTFileToNative(pThis->Os.hFileDevice), IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
---|
287 | NULL, 0, &geom, sizeof(geom), &cbBytesReturned, NULL);
|
---|
288 | if (rc) {
|
---|
289 | cbSectors = geom.Cylinders.QuadPart * geom.TracksPerCylinder * geom.SectorsPerTrack;
|
---|
290 | *pcb = cbSectors * geom.BytesPerSector;
|
---|
291 | rc = VINF_SUCCESS;
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | DWORD dwLastError;
|
---|
296 |
|
---|
297 | dwLastError = GetLastError();
|
---|
298 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
299 | Log(("DrvHostFloppy: IOCTL_DISK_GET_DRIVE_GEOMETRY(%s) failed, LastError=%d rc=%Rrc\n",
|
---|
300 | pThis->pszDevice, dwLastError, rc));
|
---|
301 | return rc;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | /* use NT api, retry a few times if the media is being verified. */
|
---|
307 | IO_STATUS_BLOCK IoStatusBlock = {0};
|
---|
308 | FILE_FS_SIZE_INFORMATION FsSize = {0};
|
---|
309 | NTSTATUS rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(pThis->Os.hFileDevice), &IoStatusBlock,
|
---|
310 | &FsSize, sizeof(FsSize), FileFsSizeInformation);
|
---|
311 | int cRetries = 5;
|
---|
312 | while (rcNt == STATUS_VERIFY_REQUIRED && cRetries-- > 0)
|
---|
313 | {
|
---|
314 | RTThreadSleep(10);
|
---|
315 | rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(pThis->Os.hFileDevice), &IoStatusBlock,
|
---|
316 | &FsSize, sizeof(FsSize), FileFsSizeInformation);
|
---|
317 | }
|
---|
318 | if (rcNt >= 0)
|
---|
319 | {
|
---|
320 | *pcb = FsSize.TotalAllocationUnits.QuadPart * FsSize.BytesPerSector;
|
---|
321 | return VINF_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* convert nt status code to VBox status code. */
|
---|
325 | /** @todo Make conversion function!. */
|
---|
326 | switch (rcNt)
|
---|
327 | {
|
---|
328 | case STATUS_NO_MEDIA_IN_DEVICE: rc = VERR_MEDIA_NOT_PRESENT; break;
|
---|
329 | case STATUS_VERIFY_REQUIRED: rc = VERR_TRY_AGAIN; break;
|
---|
330 | }
|
---|
331 | LogFlow(("drvHostBaseGetMediaSize: NtQueryVolumeInformationFile -> %#lx\n", rcNt, rc));
|
---|
332 | }
|
---|
333 | return rc;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | DECLHIDDEN(int) drvHostBaseReadOs(PDRVHOSTBASE pThis, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
338 | {
|
---|
339 | return RTFileReadAt(pThis->Os.hFileDevice, off, pvBuf, cbRead, NULL);
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | DECLHIDDEN(int) drvHostBaseWriteOs(PDRVHOSTBASE pThis, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
344 | {
|
---|
345 | return RTFileWriteAt(pThis->Os.hFileDevice, off, pvBuf, cbWrite, NULL);
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | DECLHIDDEN(int) drvHostBaseFlushOs(PDRVHOSTBASE pThis)
|
---|
350 | {
|
---|
351 | return RTFileFlush(pThis->Os.hFileDevice);
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | DECLHIDDEN(int) drvHostBaseDoLockOs(PDRVHOSTBASE pThis, bool fLock)
|
---|
356 | {
|
---|
357 | PREVENT_MEDIA_REMOVAL PreventMediaRemoval = {fLock};
|
---|
358 | DWORD cbReturned;
|
---|
359 | int rc;
|
---|
360 | if (DeviceIoControl((HANDLE)RTFileToNative(pThis->Os.hFileDevice), IOCTL_STORAGE_MEDIA_REMOVAL,
|
---|
361 | &PreventMediaRemoval, sizeof(PreventMediaRemoval),
|
---|
362 | NULL, 0, &cbReturned,
|
---|
363 | NULL))
|
---|
364 | rc = VINF_SUCCESS;
|
---|
365 | else
|
---|
366 | /** @todo figure out the return codes for already locked. */
|
---|
367 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
368 |
|
---|
369 | return rc;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | DECLHIDDEN(int) drvHostBaseEjectOs(PDRVHOSTBASE pThis)
|
---|
374 | {
|
---|
375 | int rc = VINF_SUCCESS;
|
---|
376 | RTFILE hFileDevice = pThis->Os.hFileDevice;
|
---|
377 | if (hFileDevice == NIL_RTFILE) /* obsolete crap */
|
---|
378 | rc = RTFileOpen(&hFileDevice, pThis->pszDeviceOpen, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
379 | if (RT_SUCCESS(rc))
|
---|
380 | {
|
---|
381 | /* do ioctl */
|
---|
382 | DWORD cbReturned;
|
---|
383 | if (DeviceIoControl((HANDLE)RTFileToNative(hFileDevice), IOCTL_STORAGE_EJECT_MEDIA,
|
---|
384 | NULL, 0,
|
---|
385 | NULL, 0, &cbReturned,
|
---|
386 | NULL))
|
---|
387 | rc = VINF_SUCCESS;
|
---|
388 | else
|
---|
389 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
390 |
|
---|
391 | /* clean up handle */
|
---|
392 | if (hFileDevice != pThis->Os.hFileDevice)
|
---|
393 | RTFileClose(hFileDevice);
|
---|
394 | }
|
---|
395 | else
|
---|
396 | AssertMsgFailed(("Failed to open '%s' for ejecting this tray.\n", rc));
|
---|
397 |
|
---|
398 | return rc;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | DECLHIDDEN(void) drvHostBaseInitOs(PDRVHOSTBASE pThis)
|
---|
403 | {
|
---|
404 | pThis->Os.hFileDevice = NIL_RTFILE;
|
---|
405 | pThis->Os.hwndDeviceChange = NULL;
|
---|
406 | pThis->Os.hThrdMediaChange = NIL_RTTHREAD;
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | DECLHIDDEN(int) drvHostBaseOpenOs(PDRVHOSTBASE pThis, bool fReadOnly)
|
---|
411 | {
|
---|
412 | UINT uDriveType = GetDriveType(pThis->pszDevice);
|
---|
413 | switch (pThis->enmType)
|
---|
414 | {
|
---|
415 | case PDMMEDIATYPE_FLOPPY_360:
|
---|
416 | case PDMMEDIATYPE_FLOPPY_720:
|
---|
417 | case PDMMEDIATYPE_FLOPPY_1_20:
|
---|
418 | case PDMMEDIATYPE_FLOPPY_1_44:
|
---|
419 | case PDMMEDIATYPE_FLOPPY_2_88:
|
---|
420 | case PDMMEDIATYPE_FLOPPY_FAKE_15_6:
|
---|
421 | case PDMMEDIATYPE_FLOPPY_FAKE_63_5:
|
---|
422 | if (uDriveType != DRIVE_REMOVABLE)
|
---|
423 | {
|
---|
424 | AssertMsgFailed(("Configuration error: '%s' is not a floppy (type=%d)\n",
|
---|
425 | pThis->pszDevice, uDriveType));
|
---|
426 | return VERR_INVALID_PARAMETER;
|
---|
427 | }
|
---|
428 | break;
|
---|
429 | case PDMMEDIATYPE_CDROM:
|
---|
430 | case PDMMEDIATYPE_DVD:
|
---|
431 | if (uDriveType != DRIVE_CDROM)
|
---|
432 | {
|
---|
433 | AssertMsgFailed(("Configuration error: '%s' is not a cdrom (type=%d)\n",
|
---|
434 | pThis->pszDevice, uDriveType));
|
---|
435 | return VERR_INVALID_PARAMETER;
|
---|
436 | }
|
---|
437 | break;
|
---|
438 | case PDMMEDIATYPE_HARD_DISK:
|
---|
439 | default:
|
---|
440 | AssertMsgFailed(("enmType=%d\n", pThis->enmType));
|
---|
441 | return VERR_INVALID_PARAMETER;
|
---|
442 | }
|
---|
443 |
|
---|
444 | int iBit = RT_C_TO_UPPER(pThis->pszDevice[0]) - 'A';
|
---|
445 | if ( iBit > 'Z' - 'A'
|
---|
446 | || pThis->pszDevice[1] != ':'
|
---|
447 | || pThis->pszDevice[2])
|
---|
448 | {
|
---|
449 | AssertMsgFailed(("Configuration error: Invalid drive specification: '%s'\n", pThis->pszDevice));
|
---|
450 | return VERR_INVALID_PARAMETER;
|
---|
451 | }
|
---|
452 | pThis->Os.fUnitMask = 1 << iBit;
|
---|
453 | RTStrAPrintf(&pThis->pszDeviceOpen, "\\\\.\\%s", pThis->pszDevice);
|
---|
454 | if (!pThis->pszDeviceOpen)
|
---|
455 | return VERR_NO_MEMORY;
|
---|
456 |
|
---|
457 | uint32_t fFlags = (fReadOnly ? RTFILE_O_READ : RTFILE_O_READWRITE) | RTFILE_O_OPEN | RTFILE_O_DENY_NONE;
|
---|
458 | int rc = RTFileOpen(&pThis->Os.hFileDevice, pThis->pszDeviceOpen, fFlags);
|
---|
459 |
|
---|
460 | if (RT_SUCCESS(rc))
|
---|
461 | {
|
---|
462 | /*
|
---|
463 | * Start the thread which will wait for the media change events.
|
---|
464 | */
|
---|
465 | rc = RTThreadCreate(&pThis->Os.hThrdMediaChange, drvHostBaseMediaThreadWin, pThis, 0,
|
---|
466 | RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "DVDMEDIA");
|
---|
467 | if (RT_FAILURE(rc))
|
---|
468 | {
|
---|
469 | AssertMsgFailed(("Failed to create poller thread. rc=%Rrc\n", rc));
|
---|
470 | return rc;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * Wait for the thread to start up (!w32:) and do one detection loop.
|
---|
475 | */
|
---|
476 | rc = RTThreadUserWait(pThis->Os.hThrdMediaChange, 10000);
|
---|
477 | AssertRC(rc);
|
---|
478 |
|
---|
479 | if (!pThis->Os.hwndDeviceChange)
|
---|
480 | return VERR_GENERAL_FAILURE;
|
---|
481 |
|
---|
482 | DRVHostBaseMediaPresent(pThis);
|
---|
483 | }
|
---|
484 |
|
---|
485 | return rc;
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | DECLHIDDEN(int) drvHostBaseMediaRefreshOs(PDRVHOSTBASE pThis)
|
---|
490 | {
|
---|
491 | RT_NOREF(pThis);
|
---|
492 | return VINF_SUCCESS;
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | DECLHIDDEN(int) drvHostBaseQueryMediaStatusOs(PDRVHOSTBASE pThis, bool *pfMediaChanged, bool *pfMediaPresent)
|
---|
497 | {
|
---|
498 | RT_NOREF3(pThis, pfMediaChanged, pfMediaPresent); /* We don't support the polling method. */
|
---|
499 | return VERR_NOT_SUPPORTED;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | DECLHIDDEN(bool) drvHostBaseIsMediaPollingRequiredOs(PDRVHOSTBASE pThis)
|
---|
504 | {
|
---|
505 | /* For Windows we alwys use an internal approach. */
|
---|
506 | RT_NOREF(pThis);
|
---|
507 | return false;
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | DECLHIDDEN(void) drvHostBaseDestructOs(PDRVHOSTBASE pThis)
|
---|
512 | {
|
---|
513 | /*
|
---|
514 | * Terminate the thread.
|
---|
515 | */
|
---|
516 | if (pThis->Os.hThrdMediaChange != NIL_RTTHREAD)
|
---|
517 | {
|
---|
518 | int rc;
|
---|
519 | int cTimes = 50;
|
---|
520 | do
|
---|
521 | {
|
---|
522 | if (pThis->Os.hwndDeviceChange)
|
---|
523 | PostMessage(pThis->Os.hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
|
---|
524 |
|
---|
525 | rc = RTThreadWait(pThis->Os.hThrdMediaChange, 100, NULL);
|
---|
526 | } while (cTimes-- > 0 && rc == VERR_TIMEOUT);
|
---|
527 |
|
---|
528 | if (RT_SUCCESS(rc))
|
---|
529 | pThis->Os.hThrdMediaChange = NIL_RTTHREAD;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Unlock the drive if we've locked it or we're in passthru mode.
|
---|
534 | */
|
---|
535 | if ( pThis->fLocked
|
---|
536 | && pThis->Os.hFileDevice != NIL_RTFILE
|
---|
537 | && pThis->pfnDoLock)
|
---|
538 | {
|
---|
539 | int rc = pThis->pfnDoLock(pThis, false);
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | pThis->fLocked = false;
|
---|
542 | }
|
---|
543 |
|
---|
544 | if (pThis->Os.hwndDeviceChange)
|
---|
545 | {
|
---|
546 | if (SetWindowLongPtr(pThis->Os.hwndDeviceChange, GWLP_USERDATA, 0) == (LONG_PTR)pThis)
|
---|
547 | PostMessage(pThis->Os.hwndDeviceChange, WM_CLOSE, 0, 0); /* default win proc will destroy the window */
|
---|
548 | pThis->Os.hwndDeviceChange = NULL;
|
---|
549 | }
|
---|
550 |
|
---|
551 | if (pThis->Os.hFileDevice != NIL_RTFILE)
|
---|
552 | {
|
---|
553 | int rc = RTFileClose(pThis->Os.hFileDevice);
|
---|
554 | AssertRC(rc);
|
---|
555 | pThis->Os.hFileDevice = NIL_RTFILE;
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|