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