VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvHostDVD.cpp@ 12727

Last change on this file since 12727 was 12727, checked in by vboxsync, 16 years ago

fixed passthrough on Windows hosts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.0 KB
Line 
1/* $Id: DrvHostDVD.cpp 12727 2008-09-25 12:35:35Z vboxsync $ */
2/** @file
3 * DrvHostDVD - Host DVD block driver.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DRV_HOST_DVD
27#define __STDC_LIMIT_MACROS
28#define __STDC_CONSTANT_MACROS
29#ifdef RT_OS_DARWIN
30# include <mach/mach.h>
31# include <Carbon/Carbon.h>
32# include <IOKit/IOKitLib.h>
33# include <IOKit/IOCFPlugIn.h>
34# include <IOKit/scsi-commands/SCSITaskLib.h>
35# include <IOKit/scsi-commands/SCSICommandOperationCodes.h>
36# include <IOKit/storage/IOStorageDeviceCharacteristics.h>
37# include <mach/mach_error.h>
38# define USE_MEDIA_POLLING
39
40#elif defined(RT_OS_L4)
41/* nothing (yet). */
42
43#elif defined RT_OS_LINUX
44# include <sys/ioctl.h>
45/* This is a hack to work around conflicts between these linux kernel headers
46 * and the GLIBC tcpip headers. They have different declarations of the 4
47 * standard byte order functions. */
48# define _LINUX_BYTEORDER_GENERIC_H
49/* This is another hack for not bothering with C++ unfriendly byteswap macros. */
50# define _LINUX_BYTEORDER_SWAB_H
51/* Those macros that are needed are defined in the header below */
52# include "swab.h"
53# include <linux/cdrom.h>
54# include <sys/fcntl.h>
55# include <errno.h>
56# include <limits.h>
57# define USE_MEDIA_POLLING
58
59#elif defined(RT_OS_SOLARIS)
60# include <stropts.h>
61# include <fcntl.h>
62# include <ctype.h>
63# include <errno.h>
64# include <pwd.h>
65# include <unistd.h>
66# include <syslog.h>
67# ifdef VBOX_WITH_SUID_WRAPPER
68# include <auth_attr.h>
69# endif
70# include <sys/dkio.h>
71# include <sys/sockio.h>
72# include <sys/scsi/scsi.h>
73# define USE_MEDIA_POLLING
74
75#elif defined(RT_OS_WINDOWS)
76# include <Windows.h>
77# include <winioctl.h>
78# include <ntddscsi.h>
79# undef USE_MEDIA_POLLING
80
81#else
82# error "Unsupported Platform."
83#endif
84
85#include <VBox/pdmdrv.h>
86#include <iprt/assert.h>
87#include <iprt/file.h>
88#include <iprt/string.h>
89#include <iprt/thread.h>
90#include <iprt/critsect.h>
91#include <VBox/scsi.h>
92
93#include "Builtins.h"
94#include "DrvHostBase.h"
95
96
97/* Forward declarations. */
98
99static DECLCALLBACK(int) drvHostDvdDoLock(PDRVHOSTBASE pThis, bool fLock);
100#ifdef VBOX_WITH_SUID_WRAPPER
101static int solarisCheckUserAuth();
102static int solarisEnterRootMode(uid_t *pEffUserID);
103static int solarisExitRootMode(uid_t *pEffUserID);
104#endif
105
106
107/** @copydoc PDMIMOUNT::pfnUnmount */
108static DECLCALLBACK(int) drvHostDvdUnmount(PPDMIMOUNT pInterface, bool fForce)
109{
110 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
111 RTCritSectEnter(&pThis->CritSect);
112
113 /*
114 * Validate state.
115 */
116 int rc = VINF_SUCCESS;
117 if (!pThis->fLocked || fForce)
118 {
119 /* Unlock drive if necessary. */
120 if (pThis->fLocked)
121 drvHostDvdDoLock(pThis, false);
122
123 /*
124 * Eject the disc.
125 */
126#ifdef RT_OS_DARWIN
127 uint8_t abCmd[16] =
128 {
129 SCSI_START_STOP_UNIT, 0, 0, 0, 2 /*eject+stop*/, 0,
130 0,0,0,0,0,0,0,0,0,0
131 };
132 rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, NULL, 0, 0);
133
134#elif defined(RT_OS_LINUX)
135 rc = ioctl(pThis->FileDevice, CDROMEJECT, 0);
136 if (rc < 0)
137 {
138 if (errno == EBUSY)
139 rc = VERR_PDM_MEDIA_LOCKED;
140 else if (errno == ENOSYS)
141 rc = VERR_NOT_SUPPORTED;
142 else
143 rc = RTErrConvertFromErrno(errno);
144 }
145
146#elif defined(RT_OS_SOLARIS)
147 rc = ioctl(pThis->FileRawDevice, DKIOCEJECT, 0);
148 if (rc < 0)
149 {
150 if (errno == EBUSY)
151 rc = VERR_PDM_MEDIA_LOCKED;
152 else if (errno == ENOSYS || errno == ENOTSUP)
153 rc = VERR_NOT_SUPPORTED;
154 else if (errno == ENODEV)
155 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
156 else
157 rc = RTErrConvertFromErrno(errno);
158 }
159
160#elif defined(RT_OS_WINDOWS)
161 RTFILE FileDevice = pThis->FileDevice;
162 if (FileDevice == NIL_RTFILE) /* obsolete crap */
163 rc = RTFileOpen(&FileDevice, pThis->pszDeviceOpen, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
164 if (RT_SUCCESS(rc))
165 {
166 /* do ioctl */
167 DWORD cbReturned;
168 if (DeviceIoControl((HANDLE)FileDevice, IOCTL_STORAGE_EJECT_MEDIA,
169 NULL, 0,
170 NULL, 0, &cbReturned,
171 NULL))
172 rc = VINF_SUCCESS;
173 else
174 rc = RTErrConvertFromWin32(GetLastError());
175
176 /* clean up handle */
177 if (FileDevice != pThis->FileDevice)
178 RTFileClose(FileDevice);
179 }
180 else
181 AssertMsgFailed(("Failed to open '%s' for ejecting this tray.\n", rc));
182
183
184#else
185 AssertMsgFailed(("Eject is not implemented!\n"));
186 rc = VINF_SUCCESS;
187#endif
188
189 /*
190 * Media is no longer present.
191 */
192 DRVHostBaseMediaNotPresent(pThis); /** @todo This isn't thread safe! */
193 }
194 else
195 {
196 Log(("drvHostDvdUnmount: Locked\n"));
197 rc = VERR_PDM_MEDIA_LOCKED;
198 }
199
200 RTCritSectLeave(&pThis->CritSect);
201 LogFlow(("drvHostDvdUnmount: returns %Rrc\n", rc));
202 return rc;
203}
204
205
206/**
207 * Locks or unlocks the drive.
208 *
209 * @returns VBox status code.
210 * @param pThis The instance data.
211 * @param fLock True if the request is to lock the drive, false if to unlock.
212 */
213static DECLCALLBACK(int) drvHostDvdDoLock(PDRVHOSTBASE pThis, bool fLock)
214{
215#ifdef RT_OS_DARWIN
216 uint8_t abCmd[16] =
217 {
218 SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL, 0, 0, 0, fLock, 0,
219 0,0,0,0,0,0,0,0,0,0
220 };
221 int rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, NULL, 0, 0);
222
223#elif defined(RT_OS_LINUX)
224 int rc = ioctl(pThis->FileDevice, CDROM_LOCKDOOR, (int)fLock);
225 if (rc < 0)
226 {
227 if (errno == EBUSY)
228 rc = VERR_ACCESS_DENIED;
229 else if (errno == EDRIVE_CANT_DO_THIS)
230 rc = VERR_NOT_SUPPORTED;
231 else
232 rc = RTErrConvertFromErrno(errno);
233 }
234
235#elif defined(RT_OS_SOLARIS)
236 int rc = ioctl(pThis->FileRawDevice, fLock ? DKIOCLOCK : DKIOCUNLOCK, 0);
237 if (rc < 0)
238 {
239 if (errno == EBUSY)
240 rc = VERR_ACCESS_DENIED;
241 else if (errno == ENOTSUP || errno == ENOSYS)
242 rc = VERR_NOT_SUPPORTED;
243 else
244 rc = RTErrConvertFromErrno(errno);
245 }
246
247#elif defined(RT_OS_WINDOWS)
248
249 PREVENT_MEDIA_REMOVAL PreventMediaRemoval = {fLock};
250 DWORD cbReturned;
251 int rc;
252 if (DeviceIoControl((HANDLE)pThis->FileDevice, IOCTL_STORAGE_MEDIA_REMOVAL,
253 &PreventMediaRemoval, sizeof(PreventMediaRemoval),
254 NULL, 0, &cbReturned,
255 NULL))
256 rc = VINF_SUCCESS;
257 else
258 /** @todo figure out the return codes for already locked. */
259 rc = RTErrConvertFromWin32(GetLastError());
260
261#else
262 AssertMsgFailed(("Lock/Unlock is not implemented!\n"));
263 int rc = VINF_SUCCESS;
264
265#endif
266
267 LogFlow(("drvHostDvdDoLock(, fLock=%RTbool): returns %Rrc\n", fLock, rc));
268 return rc;
269}
270
271
272
273#ifdef RT_OS_LINUX
274/**
275 * Get the media size.
276 *
277 * @returns VBox status code.
278 * @param pThis The instance data.
279 * @param pcb Where to store the size.
280 */
281static int drvHostDvdGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
282{
283 /*
284 * Query the media size.
285 */
286 /* Clear the media-changed-since-last-call-thingy just to be on the safe side. */
287 ioctl(pThis->FileDevice, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
288 return RTFileSeek(pThis->FileDevice, 0, RTFILE_SEEK_END, pcb);
289
290}
291#endif /* RT_OS_LINUX */
292
293
294#ifdef USE_MEDIA_POLLING
295/**
296 * Do media change polling.
297 */
298DECLCALLBACK(int) drvHostDvdPoll(PDRVHOSTBASE pThis)
299{
300 /*
301 * Poll for media change.
302 */
303#ifdef RT_OS_DARWIN
304 AssertReturn(pThis->ppScsiTaskDI, VERR_INTERNAL_ERROR);
305
306 /*
307 * Issue a TEST UNIT READY request.
308 */
309 bool fMediaChanged = false;
310 bool fMediaPresent = false;
311 uint8_t abCmd[16] = { SCSI_TEST_UNIT_READY, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
312 uint8_t abSense[32];
313 int rc2 = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, abSense, sizeof(abSense), 0);
314 if (RT_SUCCESS(rc2))
315 fMediaPresent = true;
316 else if ( rc2 == VERR_UNRESOLVED_ERROR
317 && abSense[2] == 6 /* unit attention */
318 && ( (abSense[12] == 0x29 && abSense[13] < 5 /* reset */)
319 || (abSense[12] == 0x2a && abSense[13] == 0 /* parameters changed */) //???
320 || (abSense[12] == 0x3f && abSense[13] == 0 /* target operating conditions have changed */) //???
321 || (abSense[12] == 0x3f && abSense[13] == 2 /* changed operating definition */) //???
322 || (abSense[12] == 0x3f && abSense[13] == 3 /* inquery parameters changed */)
323 || (abSense[12] == 0x3f && abSense[13] == 5 /* device identifier changed */)
324 )
325 )
326 {
327 fMediaPresent = false;
328 fMediaChanged = true;
329 /** @todo check this media chance stuff on Darwin. */
330 }
331
332#elif defined(RT_OS_LINUX)
333 bool fMediaPresent = ioctl(pThis->FileDevice, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
334
335#elif defined(RT_OS_SOLARIS)
336 bool fMediaPresent = false;
337 bool fMediaChanged = false;
338
339 /* Need to pass the previous state and DKIO_NONE for the first time. */
340 static dkio_state s_DeviceState = DKIO_NONE;
341 dkio_state PreviousState = s_DeviceState;
342 int rc2 = ioctl(pThis->FileRawDevice, DKIOCSTATE, &s_DeviceState);
343 if (rc2 == 0)
344 {
345 fMediaPresent = (s_DeviceState == DKIO_INSERTED);
346 if (PreviousState != s_DeviceState)
347 fMediaChanged = true;
348 }
349 else
350 fMediaChanged = true;
351
352#else
353# error "Unsupported platform."
354#endif
355
356 RTCritSectEnter(&pThis->CritSect);
357
358 int rc = VINF_SUCCESS;
359 if (pThis->fMediaPresent != fMediaPresent)
360 {
361 LogFlow(("drvHostDvdPoll: %d -> %d\n", pThis->fMediaPresent, fMediaPresent));
362 pThis->fMediaPresent = false;
363 if (fMediaPresent)
364 rc = DRVHostBaseMediaPresent(pThis);
365 else
366 DRVHostBaseMediaNotPresent(pThis);
367 }
368 else if (fMediaPresent)
369 {
370 /*
371 * Poll for media change.
372 */
373#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
374 /* taken care of above. */
375#elif defined(RT_OS_LINUX)
376 bool fMediaChanged = ioctl(pThis->FileDevice, CDROM_MEDIA_CHANGED, CDSL_CURRENT) == 1;
377#else
378# error "Unsupported platform."
379#endif
380 if (fMediaChanged)
381 {
382 LogFlow(("drvHostDVDMediaThread: Media changed!\n"));
383 DRVHostBaseMediaNotPresent(pThis);
384 rc = DRVHostBaseMediaPresent(pThis);
385 }
386 }
387
388 RTCritSectLeave(&pThis->CritSect);
389 return rc;
390}
391#endif /* USE_MEDIA_POLLING */
392
393
394/** @copydoc PDMIBLOCK::pfnSendCmd */
395static int drvHostDvdSendCmd(PPDMIBLOCK pInterface, const uint8_t *pbCmd,
396 PDMBLOCKTXDIR enmTxDir, void *pvBuf, size_t *pcbBuf,
397 uint8_t *pabSense, size_t cbSense, uint32_t cTimeoutMillies)
398{
399 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
400 int rc;
401 LogFlow(("%s: cmd[0]=%#04x txdir=%d pcbBuf=%d timeout=%d\n", __FUNCTION__, pbCmd[0], enmTxDir, *pcbBuf, cTimeoutMillies));
402
403#ifdef RT_OS_DARWIN
404 /*
405 * Pass the request on to the internal scsi command interface.
406 * The command seems to be 12 bytes long, the docs a bit copy&pasty on the command length point...
407 */
408 if (enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE)
409 memset(pvBuf, '\0', *pcbBuf); /* we got read size, but zero it anyway. */
410 rc = DRVHostBaseScsiCmd(pThis, pbCmd, 12, PDMBLOCKTXDIR_FROM_DEVICE, pvBuf, pcbBuf, pabSense, cbSense, cTimeoutMillies);
411 if (rc == VERR_UNRESOLVED_ERROR)
412 rc = VINF_SUCCESS;
413
414#elif defined(RT_OS_L4)
415 /* Not really ported to L4 yet. */
416 rc = VERR_INTERNAL_ERROR;
417
418#elif defined(RT_OS_LINUX)
419 int direction;
420 struct cdrom_generic_command cgc;
421
422 switch (enmTxDir)
423 {
424 case PDMBLOCKTXDIR_NONE:
425 Assert(*pcbBuf == 0);
426 direction = CGC_DATA_NONE;
427 break;
428 case PDMBLOCKTXDIR_FROM_DEVICE:
429 Assert(*pcbBuf != 0);
430 /* Make sure that the buffer is clear for commands reading
431 * data. The actually received data may be shorter than what
432 * we expect, and due to the unreliable feedback about how much
433 * data the ioctl actually transferred, it's impossible to
434 * prevent that. Returning previous buffer contents may cause
435 * security problems inside the guest OS, if users can issue
436 * commands to the CDROM device. */
437 memset(pvBuf, '\0', *pcbBuf);
438 direction = CGC_DATA_READ;
439 break;
440 case PDMBLOCKTXDIR_TO_DEVICE:
441 Assert(*pcbBuf != 0);
442 direction = CGC_DATA_WRITE;
443 break;
444 default:
445 AssertMsgFailed(("enmTxDir invalid!\n"));
446 direction = CGC_DATA_NONE;
447 }
448 memset(&cgc, '\0', sizeof(cgc));
449 memcpy(cgc.cmd, pbCmd, CDROM_PACKET_SIZE);
450 cgc.buffer = (unsigned char *)pvBuf;
451 cgc.buflen = *pcbBuf;
452 cgc.stat = 0;
453 Assert(cbSense >= sizeof(struct request_sense));
454 cgc.sense = (struct request_sense *)pabSense;
455 cgc.data_direction = direction;
456 cgc.quiet = false;
457 cgc.timeout = cTimeoutMillies;
458 rc = ioctl(pThis->FileDevice, CDROM_SEND_PACKET, &cgc);
459 if (rc < 0)
460 {
461 if (errno == EBUSY)
462 rc = VERR_PDM_MEDIA_LOCKED;
463 else if (errno == ENOSYS)
464 rc = VERR_NOT_SUPPORTED;
465 else
466 {
467 rc = RTErrConvertFromErrno(errno);
468 if (rc == VERR_ACCESS_DENIED && cgc.sense->sense_key == SCSI_SENSE_NONE)
469 cgc.sense->sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
470 Log2(("%s: error status %d, rc=%Rrc\n", __FUNCTION__, cgc.stat, rc));
471 }
472 }
473 Log2(("%s: after ioctl: cgc.buflen=%d txlen=%d\n", __FUNCTION__, cgc.buflen, *pcbBuf));
474 /* The value of cgc.buflen does not reliably reflect the actual amount
475 * of data transferred (for packet commands with little data transfer
476 * it's 0). So just assume that everything worked ok. */
477
478#elif defined(RT_OS_SOLARIS)
479 struct uscsi_cmd usc;
480 union scsi_cdb scdb;
481 memset(&usc, 0, sizeof(struct uscsi_cmd));
482 memset(&scdb, 0, sizeof(scdb));
483
484 switch (enmTxDir)
485 {
486 case PDMBLOCKTXDIR_NONE:
487 Assert(*pcbBuf == 0);
488 usc.uscsi_flags = USCSI_READ;
489 /* nothing to do */
490 break;
491
492 case PDMBLOCKTXDIR_FROM_DEVICE:
493 Assert(*pcbBuf != 0);
494 /* Make sure that the buffer is clear for commands reading
495 * data. The actually received data may be shorter than what
496 * we expect, and due to the unreliable feedback about how much
497 * data the ioctl actually transferred, it's impossible to
498 * prevent that. Returning previous buffer contents may cause
499 * security problems inside the guest OS, if users can issue
500 * commands to the CDROM device. */
501 memset(pvBuf, '\0', *pcbBuf);
502 usc.uscsi_flags = USCSI_READ;
503 break;
504 case PDMBLOCKTXDIR_TO_DEVICE:
505 Assert(*pcbBuf != 0);
506 usc.uscsi_flags = USCSI_WRITE;
507 break;
508 default:
509 AssertMsgFailedReturn(("%d\n", enmTxDir), VERR_INTERNAL_ERROR);
510 }
511 usc.uscsi_flags |= USCSI_RQENABLE;
512 usc.uscsi_rqbuf = (char *)pabSense;
513 usc.uscsi_rqlen = cbSense;
514 usc.uscsi_cdb = (caddr_t)&scdb;
515 usc.uscsi_cdblen = 12;
516 memcpy (usc.uscsi_cdb, pbCmd, usc.uscsi_cdblen);
517 usc.uscsi_bufaddr = (caddr_t)pvBuf;
518 usc.uscsi_buflen = *pcbBuf;
519 usc.uscsi_timeout = (cTimeoutMillies + 999) / 1000;
520
521 /* We need root privileges for user-SCSI under Solaris. */
522#ifdef VBOX_WITH_SUID_WRAPPER
523 uid_t effUserID = geteuid();
524 solarisEnterRootMode(&effUserID); /** @todo check return code when this really works. */
525#endif
526 rc = ioctl(pThis->FileRawDevice, USCSICMD, &usc);
527#ifdef VBOX_WITH_SUID_WRAPPER
528 solarisExitRootMode(&effUserID);
529#endif
530 if (rc < 0)
531 {
532 if (errno == EPERM)
533 return VERR_PERMISSION_DENIED;
534 if (usc.uscsi_status)
535 {
536 rc = RTErrConvertFromErrno(errno);
537 Log2(("%s: error status. rc=%Rrc\n", __FUNCTION__, rc));
538 }
539 }
540 Log2(("%s: after ioctl: residual buflen=%d original buflen=%d\n", __FUNCTION__, usc.uscsi_resid, usc.uscsi_buflen));
541
542#elif defined(RT_OS_WINDOWS)
543 int direction;
544 struct _REQ
545 {
546 SCSI_PASS_THROUGH_DIRECT spt;
547 uint8_t aSense[64];
548 } Req;
549 DWORD cbReturned = 0;
550
551 switch (enmTxDir)
552 {
553 case PDMBLOCKTXDIR_NONE:
554 direction = SCSI_IOCTL_DATA_UNSPECIFIED;
555 break;
556 case PDMBLOCKTXDIR_FROM_DEVICE:
557 Assert(*pcbBuf != 0);
558 /* Make sure that the buffer is clear for commands reading
559 * data. The actually received data may be shorter than what
560 * we expect, and due to the unreliable feedback about how much
561 * data the ioctl actually transferred, it's impossible to
562 * prevent that. Returning previous buffer contents may cause
563 * security problems inside the guest OS, if users can issue
564 * commands to the CDROM device. */
565 memset(pvBuf, '\0', *pcbBuf);
566 direction = SCSI_IOCTL_DATA_IN;
567 break;
568 case PDMBLOCKTXDIR_TO_DEVICE:
569 direction = SCSI_IOCTL_DATA_OUT;
570 break;
571 default:
572 AssertMsgFailed(("enmTxDir invalid!\n"));
573 direction = SCSI_IOCTL_DATA_UNSPECIFIED;
574 }
575 memset(&Req, '\0', sizeof(Req));
576 Req.spt.Length = sizeof(Req.spt);
577 Req.spt.CdbLength = 12;
578 memcpy(Req.spt.Cdb, pbCmd, Req.spt.CdbLength);
579 Req.spt.DataBuffer = pvBuf;
580 Req.spt.DataTransferLength = *pcbBuf;
581 Req.spt.DataIn = direction;
582 Req.spt.TimeOutValue = (cTimeoutMillies + 999) / 1000; /* Convert to seconds */
583 Assert(cbSense <= sizeof(Req.aSense));
584 Req.spt.SenseInfoLength = cbSense;
585 Req.spt.SenseInfoOffset = RT_OFFSETOF(struct _REQ, aSense);
586 if (DeviceIoControl((HANDLE)pThis->FileDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT,
587 &Req, sizeof(Req), &Req, sizeof(Req), &cbReturned, NULL))
588 {
589 if (cbReturned > RT_OFFSETOF(struct _REQ, aSense))
590 memcpy(pabSense, Req.aSense, cbSense);
591 else
592 memset(pabSense, '\0', cbSense);
593 /* Windows shares the property of not properly reflecting the actually
594 * transferred data size. See above. Assume that everything worked ok.
595 * Except if there are sense information. */
596 rc = (pabSense[2] & 0x0f) == SCSI_SENSE_NONE
597 ? VINF_SUCCESS
598 : VERR_DEV_IO_ERROR;
599 }
600 else
601 rc = RTErrConvertFromWin32(GetLastError());
602 Log2(("%s: scsistatus=%d bytes returned=%d tlength=%d\n", __FUNCTION__, Req.spt.ScsiStatus, cbReturned, Req.spt.DataTransferLength));
603
604#else
605# error "Unsupported platform."
606#endif
607 LogFlow(("%s: rc=%Rrc\n", __FUNCTION__, rc));
608 return rc;
609}
610
611#ifdef VBOX_WITH_SUID_WRAPPER
612/* These functions would have to go into a seperate solaris binary with
613 * the setuid permission set, which would run the user-SCSI ioctl and
614 * return the value. BUT... this might be prohibitively slow.
615 */
616#ifdef RT_OS_SOLARIS
617/**
618 * Checks if the current user is authorized using Solaris' role-based access control.
619 * Made as a seperate function with so that it need not be invoked each time we need
620 * to gain root access.
621 *
622 * @returns VBox error code.
623 */
624static int solarisCheckUserAuth()
625{
626 /* Uses Solaris' role-based access control (RBAC).*/
627 struct passwd *pPass = getpwuid(getuid());
628 if (pPass == NULL || chkauthattr("solaris.device.cdrw", pPass->pw_name) == 0)
629 return VERR_PERMISSION_DENIED;
630
631 return VINF_SUCCESS;
632}
633
634/**
635 * Setuid wrapper to gain root access.
636 *
637 * @returns VBox error code.
638 * @param pEffUserID Pointer to effective user ID.
639 */
640static int solarisEnterRootMode(uid_t *pEffUserID)
641{
642 /* Increase privilege if required */
643 if (*pEffUserID != 0)
644 {
645 if (seteuid(0) == 0)
646 {
647 *pEffUserID = 0;
648 return VINF_SUCCESS;
649 }
650 return VERR_PERMISSION_DENIED;
651 }
652 return VINF_SUCCESS;
653}
654
655/**
656 * Setuid wrapper to relinquish root access.
657 *
658 * @returns VBox error code.
659 * @param pEffUserID Pointer to effective user ID.
660 */
661static int solarisExitRootMode(uid_t *pEffUserID)
662{
663 /* Get back to user mode. */
664 if (*pEffUserID == 0)
665 {
666 uid_t realID = getuid();
667 if (seteuid(realID) == 0)
668 {
669 *pEffUserID = realID;
670 return VINF_SUCCESS;
671 }
672 return VERR_PERMISSION_DENIED;
673 }
674 return VINF_SUCCESS;
675}
676#endif /* RT_OS_SOLARIS */
677#endif
678
679/* -=-=-=-=- driver interface -=-=-=-=- */
680
681
682/**
683 * Construct a host dvd drive driver instance.
684 *
685 * @returns VBox status.
686 * @param pDrvIns The driver instance data.
687 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
688 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
689 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
690 * iInstance it's expected to be used a bit in this function.
691 */
692static DECLCALLBACK(int) drvHostDvdConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
693{
694 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
695 LogFlow(("drvHostDvdConstruct: iInstance=%d\n", pDrvIns->iInstance));
696
697 /*
698 * Validate configuration.
699 */
700 if (!CFGMR3AreValuesValid(pCfgHandle, "Path\0Interval\0Locked\0BIOSVisible\0AttachFailError\0Passthrough\0"))
701 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
702
703
704 /*
705 * Init instance data.
706 */
707 int rc = DRVHostBaseInitData(pDrvIns, pCfgHandle, PDMBLOCKTYPE_DVD);
708 if (RT_SUCCESS(rc))
709 {
710 /*
711 * Override stuff.
712 */
713
714#ifndef RT_OS_L4 /* Passthrough is not supported on L4 yet */
715 bool fPassthrough;
716 rc = CFGMR3QueryBool(pCfgHandle, "Passthrough", &fPassthrough);
717 if (RT_SUCCESS(rc) && fPassthrough)
718 {
719 pThis->IBlock.pfnSendCmd = drvHostDvdSendCmd;
720 /* Passthrough requires opening the device in R/W mode. */
721 pThis->fReadOnlyConfig = false;
722# ifdef VBOX_WITH_SUID_WRAPPER /* Solaris setuid for Passthrough mode. */
723 rc = solarisCheckUserAuth();
724 if (RT_FAILURE(rc))
725 {
726 Log(("DVD: solarisCheckUserAuth failed. Permission denied!\n"));
727 return rc;
728 }
729# endif /* VBOX_WITH_SUID_WRAPPER */
730 }
731#endif /* !RT_OS_L4 */
732
733 pThis->IMount.pfnUnmount = drvHostDvdUnmount;
734 pThis->pfnDoLock = drvHostDvdDoLock;
735#ifdef USE_MEDIA_POLLING
736 if (!fPassthrough)
737 pThis->pfnPoll = drvHostDvdPoll;
738 else
739 pThis->pfnPoll = NULL;
740#endif
741#ifdef RT_OS_LINUX
742 pThis->pfnGetMediaSize = drvHostDvdGetMediaSize;
743#endif
744
745 /*
746 * 2nd init part.
747 */
748 rc = DRVHostBaseInitFinish(pThis);
749 }
750 if (RT_FAILURE(rc))
751 {
752 if (!pThis->fAttachFailError)
753 {
754 /* Suppressing the attach failure error must not affect the normal
755 * DRVHostBaseDestruct, so reset this flag below before leaving. */
756 pThis->fKeepInstance = true;
757 rc = VINF_SUCCESS;
758 }
759 DRVHostBaseDestruct(pDrvIns);
760 pThis->fKeepInstance = false;
761 }
762
763 LogFlow(("drvHostDvdConstruct: returns %Rrc\n", rc));
764 return rc;
765}
766
767
768/**
769 * Block driver registration record.
770 */
771const PDMDRVREG g_DrvHostDVD =
772{
773 /* u32Version */
774 PDM_DRVREG_VERSION,
775 /* szDriverName */
776 "HostDVD",
777 /* pszDescription */
778 "Host DVD Block Driver.",
779 /* fFlags */
780 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
781 /* fClass. */
782 PDM_DRVREG_CLASS_BLOCK,
783 /* cMaxInstances */
784 ~0,
785 /* cbInstance */
786 sizeof(DRVHOSTBASE),
787 /* pfnConstruct */
788 drvHostDvdConstruct,
789 /* pfnDestruct */
790 DRVHostBaseDestruct,
791 /* pfnIOCtl */
792 NULL,
793 /* pfnPowerOn */
794 NULL,
795 /* pfnReset */
796 NULL,
797 /* pfnSuspend */
798 NULL,
799 /* pfnResume */
800 NULL,
801 /* pfnDetach */
802 NULL
803};
804
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette