VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VSCSI/VSCSILunMmc.cpp@ 70688

Last change on this file since 70688 was 70688, checked in by vboxsync, 7 years ago

VSCSI,DrvSCSI: Add method to query INQUIRY related data from the outside to overwrite defaults

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 70.4 KB
Line 
1/* $Id: VSCSILunMmc.cpp 70688 2018-01-22 19:38:45Z vboxsync $ */
2/** @file
3 * Virtual SCSI driver: MMC LUN implementation (CD/DVD-ROM)
4 */
5
6/*
7 * Copyright (C) 2006-2017 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_VSCSI
23#include <VBox/log.h>
24#include <VBox/err.h>
25#include <VBox/types.h>
26#include <VBox/vscsi.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/mem.h>
30#include <iprt/string.h>
31
32#include "VSCSIInternal.h"
33
34
35/*********************************************************************************************************************************
36* Structures and Typedefs *
37*********************************************************************************************************************************/
38
39/**
40 * Different event status types.
41 */
42typedef enum MMCEVENTSTATUSTYPE
43{
44 /** Medium event status not changed. */
45 MMCEVENTSTATUSTYPE_UNCHANGED = 0,
46 /** New medium inserted. */
47 MMCEVENTSTATUSTYPE_MEDIA_NEW,
48 /** Medium removed. */
49 MMCEVENTSTATUSTYPE_MEDIA_REMOVED,
50 /** Medium was removed + new medium was inserted. */
51 MMCEVENTSTATUSTYPE_MEDIA_CHANGED,
52 /** Medium eject requested (eject button pressed). */
53 MMCEVENTSTATUSTYPE_MEDIA_EJECT_REQUESTED,
54 /** 32bit hack. */
55 MMCEVENTSTATUSTYPE_32BIT_HACK = 0x7fffffff
56} MMCEVENTSTATUSTYPE;
57
58/** @name Media track types.
59 * @{ */
60/** Unknown media type. */
61#define MMC_MEDIA_TYPE_UNKNOWN 0
62/** Door closed, no media. */
63#define MMC_MEDIA_TYPE_NO_DISC 0x70
64/** @} */
65
66
67/**
68 * MMC LUN instance
69 */
70typedef struct VSCSILUNMMC
71{
72 /** Core LUN structure */
73 VSCSILUNINT Core;
74 /** Size of the virtual disk. */
75 uint64_t cSectors;
76 /** Medium locked indicator. */
77 bool fLocked;
78 /** Media event status. */
79 volatile MMCEVENTSTATUSTYPE MediaEventStatus;
80 /** Media track type. */
81 volatile uint32_t u32MediaTrackType;
82} VSCSILUNMMC, *PVSCSILUNMMC;
83
84
85/**
86 * Callback to fill a feature for a GET CONFIGURATION request.
87 *
88 * @returns Number of bytes used for this feature in the buffer.
89 * @param pbBuf The buffer to use.
90 * @param cbBuf Size of the buffer.
91 */
92typedef DECLCALLBACK(size_t) FNVSCSILUNMMCFILLFEATURE(uint8_t *pbBuf, size_t cbBuf);
93/** Pointer to a fill feature callback. */
94typedef FNVSCSILUNMMCFILLFEATURE *PFNVSCSILUNMMCFILLFEATURE;
95
96/**
97 * VSCSI MMC feature descriptor.
98 */
99typedef struct VSCSILUNMMCFEATURE
100{
101 /** The feature number. */
102 uint16_t u16Feat;
103 /** The callback to call for this feature. */
104 PFNVSCSILUNMMCFILLFEATURE pfnFeatureFill;
105} VSCSILUNMMCFEATURE;
106/** Pointer to a VSCSI MMC feature descriptor. */
107typedef VSCSILUNMMCFEATURE *PVSCSILUNMMCFEATURE;
108/** Pointer to a const VSCSI MMC feature descriptor. */
109typedef const VSCSILUNMMCFEATURE *PCVSCSILUNMMCFEATURE;
110
111
112
113/*********************************************************************************************************************************
114* Internal Functions *
115*********************************************************************************************************************************/
116RT_C_DECLS_BEGIN
117static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureListProfiles(uint8_t *pbBuf, size_t cbBuf);
118static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureCore(uint8_t *pbBuf, size_t cbBuf);
119static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureMorphing(uint8_t *pbBuf, size_t cbBuf);
120static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureRemovableMedium(uint8_t *pbBuf, size_t cbBuf);
121static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureRandomReadable(uint8_t *pbBuf, size_t cbBuf);
122static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureCDRead(uint8_t *pbBuf, size_t cbBuf);
123static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeaturePowerManagement(uint8_t *pbBuf, size_t cbBuf);
124static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureTimeout(uint8_t *pbBuf, size_t cbBuf);
125RT_C_DECLS_END
126
127/**
128 * List of supported MMC features.
129 */
130static const VSCSILUNMMCFEATURE g_aVScsiMmcFeatures[] =
131{
132 { 0x0000, vscsiLunMmcGetConfigurationFillFeatureListProfiles},
133 { 0x0001, vscsiLunMmcGetConfigurationFillFeatureCore},
134 { 0x0002, vscsiLunMmcGetConfigurationFillFeatureMorphing},
135 { 0x0003, vscsiLunMmcGetConfigurationFillFeatureRemovableMedium},
136 { 0x0010, vscsiLunMmcGetConfigurationFillFeatureRandomReadable},
137 { 0x001e, vscsiLunMmcGetConfigurationFillFeatureCDRead},
138 { 0x0100, vscsiLunMmcGetConfigurationFillFeaturePowerManagement},
139 { 0x0105, vscsiLunMmcGetConfigurationFillFeatureTimeout}
140};
141
142/* Fabricate normal TOC information. */
143static int mmcReadTOCNormal(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
144{
145 uint8_t aReply[2+99*8 + 32]; RT_ZERO(aReply); /* Maximum possible reply plus some safety. */
146 uint8_t *pbBuf = aReply;
147 uint8_t *q;
148 uint8_t iStartTrack;
149 uint32_t cbSize;
150 uint32_t cTracks = vscsiLunMediumGetRegionCount(pVScsiLun);
151
152 iStartTrack = pVScsiReq->pbCDB[6];
153 if (iStartTrack == 0)
154 iStartTrack = 1;
155 if (iStartTrack > cTracks && iStartTrack != 0xaa)
156 return vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
157
158 q = pbBuf + 2;
159 *q++ = iStartTrack; /* first track number */
160 *q++ = cTracks; /* last track number */
161 for (uint32_t iTrack = iStartTrack; iTrack <= cTracks; iTrack++)
162 {
163 uint64_t uLbaStart = 0;
164 VDREGIONDATAFORM enmDataForm = VDREGIONDATAFORM_MODE1_2048;
165
166 int rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, iTrack - 1, &uLbaStart,
167 NULL, NULL, &enmDataForm);
168 if (rc == VERR_NOT_FOUND || rc == VERR_MEDIA_NOT_PRESENT)
169 return vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY,
170 SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
171 else
172 AssertRC(rc);
173
174 *q++ = 0; /* reserved */
175
176 if (enmDataForm == VDREGIONDATAFORM_CDDA)
177 *q++ = 0x10; /* ADR, control */
178 else
179 *q++ = 0x14; /* ADR, control */
180
181 *q++ = (uint8_t)iTrack; /* track number */
182 *q++ = 0; /* reserved */
183 if (fMSF)
184 {
185 *q++ = 0; /* reserved */
186 scsiLBA2MSF(q, (uint32_t)uLbaStart);
187 q += 3;
188 }
189 else
190 {
191 /* sector 0 */
192 scsiH2BE_U32(q, (uint32_t)uLbaStart);
193 q += 4;
194 }
195 }
196 /* lead out track */
197 *q++ = 0; /* reserved */
198 *q++ = 0x14; /* ADR, control */
199 *q++ = 0xaa; /* track number */
200 *q++ = 0; /* reserved */
201
202 /* Query start and length of last track to get the start of the lead out track. */
203 uint64_t uLbaStart = 0;
204 uint64_t cBlocks = 0;
205
206 int rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, cTracks - 1, &uLbaStart,
207 &cBlocks, NULL, NULL);
208 if (rc == VERR_NOT_FOUND || rc == VERR_MEDIA_NOT_PRESENT)
209 return vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY,
210 SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
211 else
212 AssertRC(rc);
213
214 uLbaStart += cBlocks;
215 if (fMSF)
216 {
217 *q++ = 0; /* reserved */
218 scsiLBA2MSF(q, (uint32_t)uLbaStart);
219 q += 3;
220 }
221 else
222 {
223 scsiH2BE_U32(q, (uint32_t)uLbaStart);
224 q += 4;
225 }
226 cbSize = q - pbBuf;
227 Assert(cbSize <= sizeof(aReply));
228 scsiH2BE_U16(pbBuf, cbSize - 2);
229 if (cbSize < cbMaxTransfer)
230 cbMaxTransfer = cbSize;
231
232 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, cbMaxTransfer);
233 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
234}
235
236/* Fabricate session information. */
237static int mmcReadTOCMulti(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
238{
239 RT_NOREF1(cbMaxTransfer);
240 uint8_t aReply[32];
241 uint8_t *pbBuf = aReply;
242
243 /* multi session: only a single session defined */
244 memset(pbBuf, 0, 12);
245 pbBuf[1] = 0x0a;
246 pbBuf[2] = 0x01; /* first complete session number */
247 pbBuf[3] = 0x01; /* last complete session number */
248
249 VDREGIONDATAFORM enmDataForm = VDREGIONDATAFORM_MODE1_2048;
250 int rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, 0, NULL,
251 NULL, NULL, &enmDataForm);
252 if (rc == VERR_NOT_FOUND || rc == VERR_MEDIA_NOT_PRESENT)
253 return vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY,
254 SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
255 else
256 AssertRC(rc);
257
258 if (enmDataForm == VDREGIONDATAFORM_CDDA)
259 pbBuf[5] = 0x10; /* ADR, control */
260 else
261 pbBuf[5] = 0x14; /* ADR, control */
262
263 pbBuf[6] = 1; /* first track in last complete session */
264
265 if (fMSF)
266 {
267 pbBuf[8] = 0; /* reserved */
268 scsiLBA2MSF(pbBuf + 8, 0);
269 }
270 else
271 {
272 /* sector 0 */
273 scsiH2BE_U32(pbBuf + 8, 0);
274 }
275
276 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, 12);
277
278 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
279}
280
281/**
282 * Create raw TOC data information.
283 *
284 * @returns SCSI status code.
285 * @param pVScsiLun The LUN instance.
286 * @param pVScsiReq The VSCSI request.
287 * @param cbMaxTransfer The maximum transfer size.
288 * @param fMSF Flag whether to use MSF format to encode sector numbers.
289 */
290static int mmcReadTOCRaw(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
291{
292 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
293 uint8_t aReply[50]; /* Counted a maximum of 45 bytes but better be on the safe side. */
294 uint32_t cbSize;
295 uint8_t *pbBuf = &aReply[0] + 2;
296
297 *pbBuf++ = 1; /* first session */
298 *pbBuf++ = 1; /* last session */
299
300 *pbBuf++ = 1; /* session number */
301 *pbBuf++ = 0x14; /* data track */
302 *pbBuf++ = 0; /* track number */
303 *pbBuf++ = 0xa0; /* first track in program area */
304 *pbBuf++ = 0; /* min */
305 *pbBuf++ = 0; /* sec */
306 *pbBuf++ = 0; /* frame */
307 *pbBuf++ = 0;
308 *pbBuf++ = 1; /* first track */
309 *pbBuf++ = 0x00; /* disk type CD-DA or CD data */
310 *pbBuf++ = 0;
311
312 *pbBuf++ = 1; /* session number */
313 *pbBuf++ = 0x14; /* data track */
314 *pbBuf++ = 0; /* track number */
315 *pbBuf++ = 0xa1; /* last track in program area */
316 *pbBuf++ = 0; /* min */
317 *pbBuf++ = 0; /* sec */
318 *pbBuf++ = 0; /* frame */
319 *pbBuf++ = 0;
320 *pbBuf++ = 1; /* last track */
321 *pbBuf++ = 0;
322 *pbBuf++ = 0;
323
324 *pbBuf++ = 1; /* session number */
325 *pbBuf++ = 0x14; /* data track */
326 *pbBuf++ = 0; /* track number */
327 *pbBuf++ = 0xa2; /* lead-out */
328 *pbBuf++ = 0; /* min */
329 *pbBuf++ = 0; /* sec */
330 *pbBuf++ = 0; /* frame */
331 if (fMSF)
332 {
333 *pbBuf++ = 0; /* reserved */
334 scsiLBA2MSF(pbBuf, pVScsiLunMmc->cSectors);
335 pbBuf += 3;
336 }
337 else
338 {
339 scsiH2BE_U32(pbBuf, pVScsiLunMmc->cSectors);
340 pbBuf += 4;
341 }
342
343 *pbBuf++ = 1; /* session number */
344 *pbBuf++ = 0x14; /* ADR, control */
345 *pbBuf++ = 0; /* track number */
346 *pbBuf++ = 1; /* point */
347 *pbBuf++ = 0; /* min */
348 *pbBuf++ = 0; /* sec */
349 *pbBuf++ = 0; /* frame */
350 if (fMSF)
351 {
352 *pbBuf++ = 0; /* reserved */
353 scsiLBA2MSF(pbBuf, 0);
354 pbBuf += 3;
355 }
356 else
357 {
358 /* sector 0 */
359 scsiH2BE_U32(pbBuf, 0);
360 pbBuf += 4;
361 }
362
363 cbSize = pbBuf - aReply;
364 scsiH2BE_U16(&aReply[0], cbSize - 2);
365
366 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, cbSize));
367 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
368}
369
370static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureListProfiles(uint8_t *pbBuf, size_t cbBuf)
371{
372 if (cbBuf < 3*4)
373 return 0;
374
375 scsiH2BE_U16(pbBuf, 0x0); /* feature 0: list of profiles supported */
376 pbBuf[2] = (0 << 2) | (1 << 1) | (1 << 0); /* version 0, persistent, current */
377 pbBuf[3] = 8; /* additional bytes for profiles */
378 /* The MMC-3 spec says that DVD-ROM read capability should be reported
379 * before CD-ROM read capability. */
380 scsiH2BE_U16(pbBuf + 4, 0x10); /* profile: read-only DVD */
381 pbBuf[6] = (0 << 0); /* NOT current profile */
382 scsiH2BE_U16(pbBuf + 8, 0x08); /* profile: read only CD */
383 pbBuf[10] = (1 << 0); /* current profile */
384
385 return 3*4; /* Header + 2 profiles entries */
386}
387
388static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureCore(uint8_t *pbBuf, size_t cbBuf)
389{
390 if (cbBuf < 12)
391 return 0;
392
393 scsiH2BE_U16(pbBuf, 0x1); /* feature 0001h: Core Feature */
394 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
395 pbBuf[3] = 8; /* Additional length */
396 scsiH2BE_U16(pbBuf + 4, 0x00000002); /* Physical interface ATAPI. */
397 pbBuf[8] = RT_BIT(0); /* DBE */
398 /* Rest is reserved. */
399
400 return 12;
401}
402
403static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureMorphing(uint8_t *pbBuf, size_t cbBuf)
404{
405 if (cbBuf < 8)
406 return 0;
407
408 scsiH2BE_U16(pbBuf, 0x2); /* feature 0002h: Morphing Feature */
409 pbBuf[2] = (0x1 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
410 pbBuf[3] = 4; /* Additional length */
411 pbBuf[4] = RT_BIT(1) | 0x0; /* OCEvent | !ASYNC */
412 /* Rest is reserved. */
413
414 return 8;
415}
416
417static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureRemovableMedium(uint8_t *pbBuf, size_t cbBuf)
418{
419 if (cbBuf < 8)
420 return 0;
421
422 scsiH2BE_U16(pbBuf, 0x3); /* feature 0003h: Removable Medium Feature */
423 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
424 pbBuf[3] = 4; /* Additional length */
425 /* Tray type loading | Load | Eject | !Pvnt Jmpr | !DBML | Lock */
426 pbBuf[4] = (0x2 << 5) | RT_BIT(4) | RT_BIT(3) | (0x0 << 2) | (0x0 << 1) | RT_BIT(0);
427 /* Rest is reserved. */
428
429 return 8;
430}
431
432static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureRandomReadable(uint8_t *pbBuf, size_t cbBuf)
433{
434 if (cbBuf < 12)
435 return 0;
436
437 scsiH2BE_U16(pbBuf, 0x10); /* feature 0010h: Random Readable Feature */
438 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
439 pbBuf[3] = 8; /* Additional length */
440 scsiH2BE_U32(pbBuf + 4, 2048); /* Logical block size. */
441 scsiH2BE_U16(pbBuf + 8, 0x10); /* Blocking (0x10 for DVD, CD is not defined). */
442 pbBuf[10] = 0; /* PP not present */
443 /* Rest is reserved. */
444
445 return 12;
446}
447
448static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureCDRead(uint8_t *pbBuf, size_t cbBuf)
449{
450 if (cbBuf < 8)
451 return 0;
452
453 scsiH2BE_U16(pbBuf, 0x1e); /* feature 001Eh: CD Read Feature */
454 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
455 pbBuf[3] = 0; /* Additional length */
456 pbBuf[4] = (0x0 << 7) | (0x0 << 1) | 0x0; /* !DAP | !C2-Flags | !CD-Text. */
457 /* Rest is reserved. */
458
459 return 8;
460}
461
462static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeaturePowerManagement(uint8_t *pbBuf, size_t cbBuf)
463{
464 if (cbBuf < 4)
465 return 0;
466
467 scsiH2BE_U16(pbBuf, 0x100); /* feature 0100h: Power Management Feature */
468 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
469 pbBuf[3] = 0; /* Additional length */
470
471 return 4;
472}
473
474static DECLCALLBACK(size_t) vscsiLunMmcGetConfigurationFillFeatureTimeout(uint8_t *pbBuf, size_t cbBuf)
475{
476 if (cbBuf < 8)
477 return 0;
478
479 scsiH2BE_U16(pbBuf, 0x105); /* feature 0105h: Timeout Feature */
480 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
481 pbBuf[3] = 4; /* Additional length */
482 pbBuf[4] = 0x0; /* !Group3 */
483
484 return 8;
485}
486
487/**
488 * Processes the GET CONFIGURATION SCSI request.
489 *
490 * @returns SCSI status code.
491 * @param pVScsiLunMmc The MMC LUN instance.
492 * @param pVScsiReq The VSCSI request.
493 * @param cbMaxTransfer The maximum transfer size.
494 */
495static int vscsiLunMmcGetConfiguration(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
496{
497 uint8_t aReply[80];
498 uint8_t *pbBuf = &aReply[0];
499 size_t cbBuf = sizeof(aReply);
500 size_t cbCopied = 0;
501 uint16_t u16Sfn = scsiBE2H_U16(&pVScsiReq->pbCDB[2]);
502 uint8_t u8Rt = pVScsiReq->pbCDB[1] & 0x03;
503
504 /* Accept valid request types only. */
505 if (u8Rt == 3)
506 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
507 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
508
509 /** @todo implement switching between CD-ROM and DVD-ROM profile (the only
510 * way to differentiate them right now is based on the image size). */
511 if (pVScsiLunMmc->cSectors)
512 scsiH2BE_U16(pbBuf + 6, 0x08); /* current profile: read-only CD */
513 else
514 scsiH2BE_U16(pbBuf + 6, 0x00); /* current profile: none -> no media */
515 cbBuf -= 8;
516 pbBuf += 8;
517
518 if (u8Rt == 0x2)
519 {
520 for (uint32_t i = 0; i < RT_ELEMENTS(g_aVScsiMmcFeatures); i++)
521 {
522 if (g_aVScsiMmcFeatures[i].u16Feat == u16Sfn)
523 {
524 cbCopied = g_aVScsiMmcFeatures[i].pfnFeatureFill(pbBuf, cbBuf);
525 cbBuf -= cbCopied;
526 pbBuf += cbCopied;
527 break;
528 }
529 }
530 }
531 else
532 {
533 for (uint32_t i = 0; i < RT_ELEMENTS(g_aVScsiMmcFeatures); i++)
534 {
535 if (g_aVScsiMmcFeatures[i].u16Feat > u16Sfn)
536 {
537 cbCopied = g_aVScsiMmcFeatures[i].pfnFeatureFill(pbBuf, cbBuf);
538 cbBuf -= cbCopied;
539 pbBuf += cbCopied;
540 }
541 }
542 }
543
544 /* Set data length now. */
545 scsiH2BE_U32(&aReply[0], (uint32_t)(sizeof(aReply) - cbBuf));
546
547 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply) - cbBuf));
548 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
549}
550
551/**
552 * Processes the READ DVD STRUCTURE SCSI request.
553 *
554 * @returns SCSI status code.
555 * @param pVScsiLunMmc The MMC LUN instance.
556 * @param pVScsiReq The VSCSI request.
557 * @param cbMaxTransfer The maximum transfer size.
558 */
559static int vscsiLunMmcReadDvdStructure(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
560{
561 uint8_t aReply[25]; /* Counted a maximum of 20 bytes but better be on the safe side. */
562
563 RT_ZERO(aReply);
564
565 /* Act according to the indicated format. */
566 switch (pVScsiReq->pbCDB[7])
567 {
568 case 0x00:
569 case 0x01:
570 case 0x02:
571 case 0x03:
572 case 0x04:
573 case 0x05:
574 case 0x06:
575 case 0x07:
576 case 0x08:
577 case 0x09:
578 case 0x0a:
579 case 0x0b:
580 case 0x0c:
581 case 0x0d:
582 case 0x0e:
583 case 0x0f:
584 case 0x10:
585 case 0x11:
586 case 0x30:
587 case 0x31:
588 case 0xff:
589 if (pVScsiReq->pbCDB[1] == 0)
590 {
591 int uASC = SCSI_ASC_NONE;
592
593 switch (pVScsiReq->pbCDB[7])
594 {
595 case 0x0: /* Physical format information */
596 {
597 uint8_t uLayer = pVScsiReq->pbCDB[6];
598 uint64_t cTotalSectors;
599
600 if (uLayer != 0)
601 {
602 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
603 break;
604 }
605
606 cTotalSectors = pVScsiLunMmc->cSectors;
607 cTotalSectors >>= 2;
608 if (cTotalSectors == 0)
609 {
610 uASC = -SCSI_ASC_MEDIUM_NOT_PRESENT;
611 break;
612 }
613
614 aReply[4] = 1; /* DVD-ROM, part version 1 */
615 aReply[5] = 0xf; /* 120mm disc, minimum rate unspecified */
616 aReply[6] = 1; /* one layer, read-only (per MMC-2 spec) */
617 aReply[7] = 0; /* default densities */
618
619 /* FIXME: 0x30000 per spec? */
620 scsiH2BE_U32(&aReply[8], 0); /* start sector */
621 scsiH2BE_U32(&aReply[12], cTotalSectors - 1); /* end sector */
622 scsiH2BE_U32(&aReply[16], cTotalSectors - 1); /* l0 end sector */
623
624 /* Size of buffer, not including 2 byte size field */
625 scsiH2BE_U32(&aReply[0], 2048 + 2);
626
627 /* 2k data + 4 byte header */
628 uASC = (2048 + 4);
629 break;
630 }
631 case 0x01: /* DVD copyright information */
632 aReply[4] = 0; /* no copyright data */
633 aReply[5] = 0; /* no region restrictions */
634
635 /* Size of buffer, not including 2 byte size field */
636 scsiH2BE_U16(&aReply[0], 4 + 2);
637
638 /* 4 byte header + 4 byte data */
639 uASC = (4 + 4);
640 break;
641
642 case 0x03: /* BCA information - invalid field for no BCA info */
643 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
644 break;
645
646 case 0x04: /* DVD disc manufacturing information */
647 /* Size of buffer, not including 2 byte size field */
648 scsiH2BE_U16(&aReply[0], 2048 + 2);
649
650 /* 2k data + 4 byte header */
651 uASC = (2048 + 4);
652 break;
653 case 0xff:
654 /*
655 * This lists all the command capabilities above. Add new ones
656 * in order and update the length and buffer return values.
657 */
658
659 aReply[4] = 0x00; /* Physical format */
660 aReply[5] = 0x40; /* Not writable, is readable */
661 scsiH2BE_U16(&aReply[6], 2048 + 4);
662
663 aReply[8] = 0x01; /* Copyright info */
664 aReply[9] = 0x40; /* Not writable, is readable */
665 scsiH2BE_U16(&aReply[10], 4 + 4);
666
667 aReply[12] = 0x03; /* BCA info */
668 aReply[13] = 0x40; /* Not writable, is readable */
669 scsiH2BE_U16(&aReply[14], 188 + 4);
670
671 aReply[16] = 0x04; /* Manufacturing info */
672 aReply[17] = 0x40; /* Not writable, is readable */
673 scsiH2BE_U16(&aReply[18], 2048 + 4);
674
675 /* Size of buffer, not including 2 byte size field */
676 scsiH2BE_U16(&aReply[0], 16 + 2);
677
678 /* data written + 4 byte header */
679 uASC = (16 + 4);
680 break;
681 default: /** @todo formats beyond DVD-ROM requires */
682 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
683 }
684
685 if (uASC < 0)
686 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
687 -uASC, 0x00);
688 break;
689 }
690 /** @todo BD support, fall through for now */
691 RT_FALL_THRU();
692
693 /* Generic disk structures */
694 case 0x80: /** @todo AACS volume identifier */
695 case 0x81: /** @todo AACS media serial number */
696 case 0x82: /** @todo AACS media identifier */
697 case 0x83: /** @todo AACS media key block */
698 case 0x90: /** @todo List of recognized format layers */
699 case 0xc0: /** @todo Write protection status */
700 default:
701 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
702 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
703 }
704
705 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
706 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
707}
708
709/**
710 * Processes the MODE SENSE 10 SCSI request.
711 *
712 * @returns SCSI status code.
713 * @param pVScsiLunMmc The MMC LUN instance.
714 * @param pVScsiReq The VSCSI request.
715 * @param cbMaxTransfer The maximum transfer size.
716 */
717static int vscsiLunMmcModeSense10(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
718{
719 int rcReq;
720 uint8_t uPageControl = pVScsiReq->pbCDB[2] >> 6;
721 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
722
723 switch (uPageControl)
724 {
725 case SCSI_PAGECONTROL_CURRENT:
726 switch (uPageCode)
727 {
728 case SCSI_MODEPAGE_ERROR_RECOVERY:
729 {
730 uint8_t aReply[16];
731
732 scsiH2BE_U16(&aReply[0], 16 + 6);
733 aReply[2] = (uint8_t)pVScsiLunMmc->u32MediaTrackType;
734 aReply[3] = 0;
735 aReply[4] = 0;
736 aReply[5] = 0;
737 aReply[6] = 0;
738 aReply[7] = 0;
739
740 aReply[8] = 0x01;
741 aReply[9] = 0x06;
742 aReply[10] = 0x00;
743 aReply[11] = 0x05;
744 aReply[12] = 0x00;
745 aReply[13] = 0x00;
746 aReply[14] = 0x00;
747 aReply[15] = 0x00;
748 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
749 rcReq = vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
750 break;
751 }
752 case SCSI_MODEPAGE_CD_STATUS:
753 {
754 uint8_t aReply[40];
755
756 scsiH2BE_U16(&aReply[0], 38);
757 aReply[2] = (uint8_t)pVScsiLunMmc->u32MediaTrackType;
758 aReply[3] = 0;
759 aReply[4] = 0;
760 aReply[5] = 0;
761 aReply[6] = 0;
762 aReply[7] = 0;
763
764 aReply[8] = 0x2a;
765 aReply[9] = 30; /* page length */
766 aReply[10] = 0x08; /* DVD-ROM read support */
767 aReply[11] = 0x00; /* no write support */
768 /* The following claims we support audio play. This is obviously false,
769 * but the Linux generic CDROM support makes many features depend on this
770 * capability. If it's not set, this causes many things to be disabled. */
771 aReply[12] = 0x71; /* multisession support, mode 2 form 1/2 support, audio play */
772 aReply[13] = 0x00; /* no subchannel reads supported */
773 aReply[14] = (1 << 0) | (1 << 3) | (1 << 5); /* lock supported, eject supported, tray type loading mechanism */
774 if (pVScsiLunMmc->fLocked)
775 aReply[14] |= 1 << 1; /* report lock state */
776 aReply[15] = 0; /* no subchannel reads supported, no separate audio volume control, no changer etc. */
777 scsiH2BE_U16(&aReply[16], 5632); /* (obsolete) claim 32x speed support */
778 scsiH2BE_U16(&aReply[18], 2); /* number of audio volume levels */
779 scsiH2BE_U16(&aReply[20], 128); /* buffer size supported in Kbyte - We don't have a buffer because we write directly into guest memory.
780 Just write some dummy value. */
781 scsiH2BE_U16(&aReply[22], 5632); /* (obsolete) current read speed 32x */
782 aReply[24] = 0; /* reserved */
783 aReply[25] = 0; /* reserved for digital audio (see idx 15) */
784 scsiH2BE_U16(&aReply[26], 0); /* (obsolete) maximum write speed */
785 scsiH2BE_U16(&aReply[28], 0); /* (obsolete) current write speed */
786 scsiH2BE_U16(&aReply[30], 0); /* copy management revision supported 0=no CSS */
787 aReply[32] = 0; /* reserved */
788 aReply[33] = 0; /* reserved */
789 aReply[34] = 0; /* reserved */
790 aReply[35] = 1; /* rotation control CAV */
791 scsiH2BE_U16(&aReply[36], 0); /* current write speed */
792 scsiH2BE_U16(&aReply[38], 0); /* number of write speed performance descriptors */
793 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
794 rcReq = vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
795 break;
796 }
797 default:
798 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
799 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
800 break;
801 }
802 break;
803 case SCSI_PAGECONTROL_CHANGEABLE:
804 case SCSI_PAGECONTROL_DEFAULT:
805 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
806 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
807 break;
808 default:
809 case SCSI_PAGECONTROL_SAVED:
810 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
811 SCSI_ASC_SAVING_PARAMETERS_NOT_SUPPORTED, 0x00);
812 break;
813 }
814
815 return rcReq;
816}
817
818/**
819 * Processes the GET EVENT STATUS NOTIFICATION SCSI request.
820 *
821 * @returns SCSI status code.
822 * @param pVScsiLunMmc The MMC LUN instance.
823 * @param pVScsiReq The VSCSI request.
824 * @param cbMaxTransfer The maximum transfer size.
825 */
826static int vscsiLunMmcGetEventStatusNotification(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq,
827 size_t cbMaxTransfer)
828{
829 uint32_t OldStatus;
830 uint32_t NewStatus;
831 uint8_t aReply[8];
832 RT_ZERO(aReply);
833
834 LogFlowFunc(("pVScsiLunMmc=%#p pVScsiReq=%#p cbMaxTransfer=%zu\n",
835 pVScsiLunMmc, pVScsiReq, cbMaxTransfer));
836
837 do
838 {
839 OldStatus = ASMAtomicReadU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus);
840 NewStatus = MMCEVENTSTATUSTYPE_UNCHANGED;
841
842 switch (OldStatus)
843 {
844 case MMCEVENTSTATUSTYPE_MEDIA_NEW:
845 /* mount */
846 scsiH2BE_U16(&aReply[0], 6);
847 aReply[2] = 0x04; /* media */
848 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
849 aReply[4] = 0x02; /* new medium */
850 aReply[5] = 0x02; /* medium present / door closed */
851 aReply[6] = 0x00;
852 aReply[7] = 0x00;
853 pVScsiLunMmc->Core.fReady = true;
854 break;
855
856 case MMCEVENTSTATUSTYPE_MEDIA_CHANGED:
857 case MMCEVENTSTATUSTYPE_MEDIA_REMOVED:
858 /* umount */
859 scsiH2BE_U16(&aReply[0], 6);
860 aReply[2] = 0x04; /* media */
861 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
862 aReply[4] = 0x03; /* media removal */
863 aReply[5] = 0x00; /* medium absent / door closed */
864 aReply[6] = 0x00;
865 aReply[7] = 0x00;
866 if (OldStatus == MMCEVENTSTATUSTYPE_MEDIA_CHANGED)
867 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_NEW;
868 break;
869
870 case MMCEVENTSTATUSTYPE_MEDIA_EJECT_REQUESTED: /* currently unused */
871 scsiH2BE_U16(&aReply[0], 6);
872 aReply[2] = 0x04; /* media */
873 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
874 aReply[4] = 0x01; /* eject requested (eject button pressed) */
875 aReply[5] = 0x02; /* medium present / door closed */
876 aReply[6] = 0x00;
877 aReply[7] = 0x00;
878 break;
879
880 case MMCEVENTSTATUSTYPE_UNCHANGED:
881 default:
882 scsiH2BE_U16(&aReply[0], 6);
883 aReply[2] = 0x01; /* operational change request / notification */
884 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
885 aReply[4] = 0x00;
886 aReply[5] = 0x00;
887 aReply[6] = 0x00;
888 aReply[7] = 0x00;
889 break;
890 }
891
892 LogFlowFunc(("OldStatus=%u NewStatus=%u\n", OldStatus, NewStatus));
893
894 } while (!ASMAtomicCmpXchgU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, NewStatus, OldStatus));
895
896 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
897 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
898}
899
900/**
901 * Processes a READ TRACK INFORMATION SCSI request.
902 *
903 * @returns SCSI status code.
904 * @param pVScsiLunMmc The MMC LUN instance.
905 * @param pVScsiReq The VSCSI request.
906 * @param cbMaxTransfer The maximum transfer size.
907 */
908static int vscsiLunMmcReadTrackInformation(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq,
909 size_t cbMaxTransfer)
910{
911 int rcReq;
912 uint32_t u32LogAddr = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
913 uint8_t u8LogAddrType = pVScsiReq->pbCDB[1] & 0x03;
914
915 int rc = VINF_SUCCESS;
916 uint64_t u64LbaStart = 0;
917 uint32_t uRegion = 0;
918 uint64_t cBlocks = 0;
919 uint64_t cbBlock = 0;
920 VDREGIONDATAFORM enmDataForm = VDREGIONDATAFORM_INVALID;
921
922 switch (u8LogAddrType)
923 {
924 case 0x00:
925 rc = vscsiLunMediumQueryRegionPropertiesForLba(&pVScsiLunMmc->Core, u32LogAddr, &uRegion,
926 NULL, NULL, NULL);
927 if (RT_SUCCESS(rc))
928 rc = vscsiLunMediumQueryRegionProperties(&pVScsiLunMmc->Core, uRegion, &u64LbaStart,
929 &cBlocks, &cbBlock, &enmDataForm);
930 break;
931 case 0x01:
932 {
933 if (u32LogAddr >= 1)
934 {
935 uRegion = u32LogAddr - 1;
936 rc = vscsiLunMediumQueryRegionProperties(&pVScsiLunMmc->Core, uRegion, &u64LbaStart,
937 &cBlocks, &cbBlock, &enmDataForm);
938 }
939 else
940 rc = VERR_NOT_FOUND; /** @todo Return lead-in information. */
941 break;
942 }
943 case 0x02:
944 default:
945 rc = VERR_INVALID_PARAMETER;
946 }
947
948 if (RT_SUCCESS(rc))
949 {
950 uint8_t u8DataMode = 0xf; /* Unknown data mode. */
951 uint8_t u8TrackMode = 0;
952 uint8_t aReply[36];
953 RT_ZERO(aReply);
954
955 switch (enmDataForm)
956 {
957 case VDREGIONDATAFORM_MODE1_2048:
958 case VDREGIONDATAFORM_MODE1_2352:
959 case VDREGIONDATAFORM_MODE1_0:
960 u8DataMode = 1;
961 break;
962 case VDREGIONDATAFORM_XA_2336:
963 case VDREGIONDATAFORM_XA_2352:
964 case VDREGIONDATAFORM_XA_0:
965 case VDREGIONDATAFORM_MODE2_2336:
966 case VDREGIONDATAFORM_MODE2_2352:
967 case VDREGIONDATAFORM_MODE2_0:
968 u8DataMode = 2;
969 break;
970 default:
971 u8DataMode = 0xf;
972 }
973
974 if (enmDataForm == VDREGIONDATAFORM_CDDA)
975 u8TrackMode = 0x0;
976 else
977 u8TrackMode = 0x4;
978
979 scsiH2BE_U16(&aReply[0], 34);
980 aReply[2] = uRegion + 1; /* track number (LSB) */
981 aReply[3] = 1; /* session number (LSB) */
982 aReply[5] = (0 << 5) | (0 << 4) | u8TrackMode; /* not damaged, primary copy, data track */
983 aReply[6] = (0 << 7) | (0 << 6) | (0 << 5) | (0 << 6) | u8DataMode; /* not reserved track, not blank, not packet writing, not fixed packet, data mode 1 */
984 aReply[7] = (0 << 1) | (0 << 0); /* last recorded address not valid, next recordable address not valid */
985 scsiH2BE_U32(&aReply[8], (uint32_t)u64LbaStart); /* track start address is 0 */
986 scsiH2BE_U32(&aReply[24], (uint32_t)cBlocks); /* track size */
987 aReply[32] = 0; /* track number (MSB) */
988 aReply[33] = 0; /* session number (MSB) */
989
990 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMaxTransfer));
991 rcReq = vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
992 }
993 else
994 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
995 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
996
997 return rcReq;
998}
999
1000static DECLCALLBACK(int) vscsiLunMmcInit(PVSCSILUNINT pVScsiLun)
1001{
1002 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1003 int rc = VINF_SUCCESS;
1004
1005 ASMAtomicWriteU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, MMCEVENTSTATUSTYPE_UNCHANGED);
1006 pVScsiLunMmc->u32MediaTrackType = MMC_MEDIA_TYPE_UNKNOWN;
1007 pVScsiLunMmc->cSectors = 0;
1008
1009 uint32_t cTracks = vscsiLunMediumGetRegionCount(pVScsiLun);
1010 if (cTracks)
1011 {
1012 for (uint32_t i = 0; i < cTracks; i++)
1013 {
1014 uint64_t cBlocks = 0;
1015 rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, i, NULL, &cBlocks,
1016 NULL, NULL);
1017 AssertRC(rc);
1018
1019 pVScsiLunMmc->cSectors += cBlocks;
1020 }
1021
1022 pVScsiLunMmc->Core.fMediaPresent = true;
1023 pVScsiLunMmc->Core.fReady = false;
1024 }
1025 else
1026 {
1027 pVScsiLunMmc->Core.fMediaPresent = false;
1028 pVScsiLunMmc->Core.fReady = false;
1029 }
1030
1031 return rc;
1032}
1033
1034static DECLCALLBACK(int) vscsiLunMmcDestroy(PVSCSILUNINT pVScsiLun)
1035{
1036 RT_NOREF1(pVScsiLun);
1037 return VINF_SUCCESS;
1038}
1039
1040static DECLCALLBACK(int) vscsiLunMmcReqProcess(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq)
1041{
1042 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1043 VSCSIIOREQTXDIR enmTxDir = VSCSIIOREQTXDIR_INVALID;
1044 uint64_t uLbaStart = 0;
1045 uint32_t cSectorTransfer = 0;
1046 size_t cbSector = 0;
1047 int rc = VINF_SUCCESS;
1048 int rcReq = SCSI_STATUS_OK;
1049 unsigned uCmd = pVScsiReq->pbCDB[0];
1050 PCRTSGSEG paSegs = pVScsiReq->SgBuf.paSegs;
1051 unsigned cSegs = pVScsiReq->SgBuf.cSegs;
1052
1053 LogFlowFunc(("pVScsiLun=%#p{.fReady=%RTbool, .fMediaPresent=%RTbool} pVScsiReq=%#p{.pbCdb[0]=%#x}\n",
1054 pVScsiLun, pVScsiLun->fReady, pVScsiLun->fMediaPresent, pVScsiReq, uCmd));
1055
1056 /*
1057 * GET CONFIGURATION, GET EVENT/STATUS NOTIFICATION, INQUIRY, and REQUEST SENSE commands
1058 * operate even when a unit attention condition exists for initiator; every other command
1059 * needs to report CHECK CONDITION in that case.
1060 */
1061 if ( !pVScsiLunMmc->Core.fReady
1062 && uCmd != SCSI_INQUIRY
1063 && uCmd != SCSI_GET_CONFIGURATION
1064 && uCmd != SCSI_GET_EVENT_STATUS_NOTIFICATION)
1065 {
1066 /*
1067 * A note on media changes: As long as a medium is not present, the unit remains in
1068 * the 'not ready' state. Technically the unit becomes 'ready' soon after a medium
1069 * is inserted; however, we internally keep the 'not ready' state until we've had
1070 * a chance to report the UNIT ATTENTION status indicating a media change.
1071 */
1072 if (pVScsiLunMmc->Core.fMediaPresent)
1073 {
1074 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_UNIT_ATTENTION,
1075 SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED, 0x00);
1076 pVScsiLunMmc->Core.fReady = true;
1077 }
1078 else
1079 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY,
1080 SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
1081 }
1082 else
1083 {
1084 switch (uCmd)
1085 {
1086 case SCSI_TEST_UNIT_READY:
1087 Assert(!pVScsiLunMmc->Core.fReady); /* Only should get here if LUN isn't ready. */
1088 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY, SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
1089 break;
1090
1091 case SCSI_INQUIRY:
1092 {
1093 SCSIINQUIRYDATA ScsiInquiryReply;
1094
1095 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[3]));
1096 memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
1097
1098 ScsiInquiryReply.cbAdditional = 31;
1099 ScsiInquiryReply.fRMB = 1; /* Removable. */
1100 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_CD_DVD;
1101 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
1102 ScsiInquiryReply.u3AnsiVersion = 0x05; /* MMC-?? compliant */
1103 ScsiInquiryReply.fCmdQue = 1; /* Command queuing supported. */
1104 ScsiInquiryReply.fWBus16 = 1;
1105
1106 const char *pszVendorId = "VBOX";
1107 const char *pszProductId = "CD-ROM";
1108 const char *pszProductLevel = "1.0";
1109 int rcTmp = vscsiLunQueryInqStrings(pVScsiLun, &pszVendorId, &pszProductId, &pszProductLevel);
1110 Assert(RT_SUCCESS(rcTmp) || rcTmp == VERR_NOT_FOUND);
1111
1112 scsiPadStrS(ScsiInquiryReply.achVendorId, pszVendorId, 8);
1113 scsiPadStrS(ScsiInquiryReply.achProductId, pszProductId, 16);
1114 scsiPadStrS(ScsiInquiryReply.achProductLevel, pszProductLevel, 4);
1115
1116 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, (uint8_t *)&ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
1117 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1118 break;
1119 }
1120 case SCSI_READ_CAPACITY:
1121 {
1122 uint8_t aReply[8];
1123 memset(aReply, 0, sizeof(aReply));
1124 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
1125
1126 /*
1127 * If sector size exceeds the maximum value that is
1128 * able to be stored in 4 bytes return 0xffffffff in this field
1129 */
1130 if (pVScsiLunMmc->cSectors > UINT32_C(0xffffffff))
1131 scsiH2BE_U32(aReply, UINT32_C(0xffffffff));
1132 else
1133 scsiH2BE_U32(aReply, pVScsiLunMmc->cSectors - 1);
1134 scsiH2BE_U32(&aReply[4], _2K);
1135 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1136 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1137 break;
1138 }
1139 case SCSI_MODE_SENSE_6:
1140 {
1141 uint8_t uModePage = pVScsiReq->pbCDB[2] & 0x3f;
1142 uint8_t aReply[24];
1143 uint8_t *pu8ReplyPos;
1144 bool fValid = false;
1145
1146 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
1147 memset(aReply, 0, sizeof(aReply));
1148 aReply[0] = 4; /* Reply length 4. */
1149 aReply[1] = 0; /* Default media type. */
1150 aReply[2] = RT_BIT(4); /* Caching supported. */
1151 aReply[3] = 0; /* Block descriptor length. */
1152
1153 pu8ReplyPos = aReply + 4;
1154
1155 if ((uModePage == 0x08) || (uModePage == 0x3f))
1156 {
1157 memset(pu8ReplyPos, 0, 20);
1158 *pu8ReplyPos++ = 0x08; /* Page code. */
1159 *pu8ReplyPos++ = 0x12; /* Size of the page. */
1160 *pu8ReplyPos++ = 0x4; /* Write cache enabled. */
1161 fValid = true;
1162 } else if (uModePage == 0) {
1163 fValid = true;
1164 }
1165
1166 /* Querying unknown pages must fail. */
1167 if (fValid) {
1168 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1169 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1170 } else {
1171 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1172 }
1173 break;
1174 }
1175 case SCSI_MODE_SENSE_10:
1176 {
1177 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1178 vscsiReqSetXferSize(pVScsiReq, cbMax);
1179 rcReq = vscsiLunMmcModeSense10(pVScsiLunMmc, pVScsiReq, cbMax);
1180 break;
1181 }
1182 case SCSI_SEEK_10:
1183 {
1184 uint32_t uLba = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1185 if (uLba > pVScsiLunMmc->cSectors)
1186 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1187 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1188 else
1189 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1190 break;
1191 }
1192 case SCSI_MODE_SELECT_6:
1193 {
1194 /** @todo implement!! */
1195 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
1196 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1197 break;
1198 }
1199 case SCSI_READ_6:
1200 {
1201 enmTxDir = VSCSIIOREQTXDIR_READ;
1202 uLbaStart = ((uint64_t) pVScsiReq->pbCDB[3]
1203 | (pVScsiReq->pbCDB[2] << 8)
1204 | ((pVScsiReq->pbCDB[1] & 0x1f) << 16));
1205 cSectorTransfer = pVScsiReq->pbCDB[4];
1206 cbSector = _2K;
1207 break;
1208 }
1209 case SCSI_READ_10:
1210 {
1211 enmTxDir = VSCSIIOREQTXDIR_READ;
1212 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1213 cSectorTransfer = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1214 cbSector = _2K;
1215 break;
1216 }
1217 case SCSI_READ_12:
1218 {
1219 enmTxDir = VSCSIIOREQTXDIR_READ;
1220 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1221 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[6]);
1222 cbSector = _2K;
1223 break;
1224 }
1225 case SCSI_READ_16:
1226 {
1227 enmTxDir = VSCSIIOREQTXDIR_READ;
1228 uLbaStart = scsiBE2H_U64(&pVScsiReq->pbCDB[2]);
1229 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[10]);
1230 cbSector = _2K;
1231 break;
1232 }
1233 case SCSI_READ_CD:
1234 {
1235 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1236 cSectorTransfer = (pVScsiReq->pbCDB[6] << 16) | (pVScsiReq->pbCDB[7] << 8) | pVScsiReq->pbCDB[8];
1237
1238 /*
1239 * If the LBA is in an audio track we are required to ignore pretty much all
1240 * of the channel selection values (except 0x00) and map everything to 0x10
1241 * which means read user data with a sector size of 2352 bytes.
1242 *
1243 * (MMC-6 chapter 6.19.2.6)
1244 */
1245 uint8_t uChnSel = pVScsiReq->pbCDB[9] & 0xf8;
1246 VDREGIONDATAFORM enmDataForm;
1247 uint64_t cbSectorRegion = 0;
1248 rc = vscsiLunMediumQueryRegionPropertiesForLba(pVScsiLun, uLbaStart,
1249 NULL, NULL, &cbSectorRegion,
1250 &enmDataForm);
1251 AssertRC(rc);
1252
1253 if (enmDataForm == VDREGIONDATAFORM_CDDA)
1254 {
1255 if (uChnSel == 0)
1256 {
1257 /* nothing */
1258 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1259 }
1260 else
1261 {
1262 enmTxDir = VSCSIIOREQTXDIR_READ;
1263 cbSector = 2352;
1264 }
1265 }
1266 else
1267 {
1268 switch (uChnSel)
1269 {
1270 case 0x00:
1271 /* nothing */
1272 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1273 break;
1274 case 0x10:
1275 /* normal read */
1276 enmTxDir = VSCSIIOREQTXDIR_READ;
1277 cbSector = _2K;
1278 break;
1279 case 0xf8:
1280 {
1281 if (cbSectorRegion == 2048)
1282 {
1283 /*
1284 * Read all data, sector size is 2352.
1285 * Rearrange the buffer and fill the gaps with the sync bytes.
1286 */
1287 /* Count the number of segments for the buffer we require. */
1288 RTSGBUF SgBuf;
1289 bool fBufTooSmall = false;
1290 uint32_t cSegsNew = 0;
1291 RTSgBufClone(&SgBuf, &pVScsiReq->SgBuf);
1292 for (uint32_t uLba = (uint32_t)uLbaStart; uLba < uLbaStart + cSectorTransfer; uLba++)
1293 {
1294 size_t cbTmp = RTSgBufAdvance(&SgBuf, 16);
1295 if (cbTmp < 16)
1296 {
1297 fBufTooSmall = true;
1298 break;
1299 }
1300
1301 cbTmp = 2048;
1302 while (cbTmp)
1303 {
1304 size_t cbBuf = cbTmp;
1305 RTSgBufGetNextSegment(&SgBuf, &cbBuf);
1306 if (!cbBuf)
1307 {
1308 fBufTooSmall = true;
1309 break;
1310 }
1311
1312 cbTmp -= cbBuf;
1313 cSegsNew++;
1314 }
1315
1316 cbTmp = RTSgBufAdvance(&SgBuf, 280);
1317 if (cbTmp < 280)
1318 {
1319 fBufTooSmall = true;
1320 break;
1321 }
1322 }
1323
1324 if (!fBufTooSmall)
1325 {
1326 PRTSGSEG paSegsNew = (PRTSGSEG)RTMemAllocZ(cSegsNew * sizeof(RTSGSEG));
1327 if (paSegsNew)
1328 {
1329 enmTxDir = VSCSIIOREQTXDIR_READ;
1330
1331 uint32_t idxSeg = 0;
1332 for (uint32_t uLba = (uint32_t)uLbaStart; uLba < uLbaStart + cSectorTransfer; uLba++)
1333 {
1334 /* Sync bytes, see 4.2.3.8 CD Main Channel Block Formats */
1335 uint8_t abBuf[16];
1336 abBuf[0] = 0x00;
1337 memset(&abBuf[1], 0xff, 10);
1338 abBuf[11] = 0x00;
1339 /* MSF */
1340 scsiLBA2MSF(&abBuf[12], uLba);
1341 abBuf[15] = 0x01; /* mode 1 data */
1342 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, &abBuf[0], sizeof(abBuf));
1343
1344 size_t cbTmp = 2048;
1345 while (cbTmp)
1346 {
1347 size_t cbBuf = cbTmp;
1348 paSegsNew[idxSeg].pvSeg = RTSgBufGetNextSegment(&pVScsiReq->SgBuf, &cbBuf);
1349 paSegsNew[idxSeg].cbSeg = cbBuf;
1350 idxSeg++;
1351
1352 cbTmp -= cbBuf;
1353 }
1354
1355 /**
1356 * @todo: maybe compute ECC and parity, layout is:
1357 * 2072 4 EDC
1358 * 2076 172 P parity symbols
1359 * 2248 104 Q parity symbols
1360 */
1361 RTSgBufSet(&pVScsiReq->SgBuf, 0, 280);
1362 }
1363
1364 paSegs = paSegsNew;
1365 cSegs = cSegsNew;
1366 pVScsiReq->pvLun = paSegsNew;
1367 }
1368 else
1369 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1370 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1371 }
1372 else
1373 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1374 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1375 }
1376 else if (cbSectorRegion == 2352)
1377 {
1378 /* Sector size matches what is read. */
1379 cbSector = cbSectorRegion;
1380 enmTxDir = VSCSIIOREQTXDIR_READ;
1381 }
1382 break;
1383 }
1384 default:
1385 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1386 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1387 break;
1388 }
1389 }
1390 break;
1391 }
1392 case SCSI_READ_BUFFER:
1393 {
1394 uint8_t uDataMode = pVScsiReq->pbCDB[1] & 0x1f;
1395
1396 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[6]));
1397
1398 switch (uDataMode)
1399 {
1400 case 0x00:
1401 case 0x01:
1402 case 0x02:
1403 case 0x03:
1404 case 0x0a:
1405 break;
1406 case 0x0b:
1407 {
1408 uint8_t aReply[4];
1409 RT_ZERO(aReply);
1410
1411 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1412 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1413 break;
1414 }
1415 case 0x1a:
1416 case 0x1c:
1417 break;
1418 default:
1419 AssertMsgFailed(("Invalid data mode\n"));
1420 }
1421 break;
1422 }
1423 case SCSI_VERIFY_10:
1424 case SCSI_START_STOP_UNIT:
1425 {
1426 int rc2 = VINF_SUCCESS;
1427 switch (pVScsiReq->pbCDB[4] & 3)
1428 {
1429 case 0: /* 00 - Stop motor */
1430 case 1: /* 01 - Start motor */
1431 break;
1432 case 2: /* 10 - Eject media */
1433 rc2 = vscsiLunMediumEject(pVScsiLun);
1434 break;
1435 case 3: /* 11 - Load media */
1436 /** @todo */
1437 break;
1438 }
1439 if (RT_SUCCESS(rc2))
1440 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1441 else
1442 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_MEDIA_LOAD_OR_EJECT_FAILED, 0x02);
1443 break;
1444 }
1445 case SCSI_LOG_SENSE:
1446 {
1447 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
1448 uint8_t uSubPageCode = pVScsiReq->pbCDB[3];
1449
1450 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[7]));
1451
1452 switch (uPageCode)
1453 {
1454 case 0x00:
1455 {
1456 if (uSubPageCode == 0)
1457 {
1458 uint8_t aReply[4];
1459
1460 aReply[0] = 0;
1461 aReply[1] = 0;
1462 aReply[2] = 0;
1463 aReply[3] = 0;
1464
1465 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1466 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1467 break;
1468 }
1469 }
1470 RT_FALL_THRU();
1471 default:
1472 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1473 }
1474 break;
1475 }
1476 case SCSI_SERVICE_ACTION_IN_16:
1477 {
1478 switch (pVScsiReq->pbCDB[1] & 0x1f)
1479 {
1480 case SCSI_SVC_ACTION_IN_READ_CAPACITY_16:
1481 {
1482 uint8_t aReply[32];
1483
1484 memset(aReply, 0, sizeof(aReply));
1485 scsiH2BE_U64(aReply, pVScsiLunMmc->cSectors - 1);
1486 scsiH2BE_U32(&aReply[8], _2K);
1487 /* Leave the rest 0 */
1488
1489 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
1490 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1491 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1492 break;
1493 }
1494 default:
1495 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1496 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00); /* Don't know if this is correct */
1497 }
1498 break;
1499 }
1500 case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
1501 {
1502 pVScsiLunMmc->fLocked = RT_BOOL(pVScsiReq->pbCDB[4] & 0x01);
1503 vscsiLunMediumSetLock(pVScsiLun, pVScsiLunMmc->fLocked);
1504 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1505 break;
1506 }
1507 case SCSI_READ_TOC_PMA_ATIP:
1508 {
1509 uint8_t format;
1510 uint16_t cbMax;
1511 bool fMSF;
1512
1513 format = pVScsiReq->pbCDB[2] & 0x0f;
1514 cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1515 fMSF = (pVScsiReq->pbCDB[1] >> 1) & 1;
1516
1517 vscsiReqSetXferSize(pVScsiReq, cbMax);
1518 switch (format)
1519 {
1520 case 0x00:
1521 rcReq = mmcReadTOCNormal(pVScsiLun, pVScsiReq, cbMax, fMSF);
1522 break;
1523 case 0x01:
1524 rcReq = mmcReadTOCMulti(pVScsiLun, pVScsiReq, cbMax, fMSF);
1525 break;
1526 case 0x02:
1527 rcReq = mmcReadTOCRaw(pVScsiLun, pVScsiReq, cbMax, fMSF);
1528 break;
1529 default:
1530 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1531 }
1532 break;
1533 }
1534 case SCSI_GET_EVENT_STATUS_NOTIFICATION:
1535 {
1536 /* Only supporting polled mode at the moment. */
1537 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1538
1539 vscsiReqSetXferSize(pVScsiReq, cbMax);
1540 if (pVScsiReq->pbCDB[1] & 0x1)
1541 rcReq = vscsiLunMmcGetEventStatusNotification(pVScsiLunMmc, pVScsiReq, cbMax);
1542 else
1543 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1544 break;
1545 }
1546 case SCSI_MECHANISM_STATUS:
1547 {
1548 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1549 uint8_t aReply[8];
1550
1551 vscsiReqSetXferSize(pVScsiReq, cbMax);
1552 scsiH2BE_U16(&aReply[0], 0);
1553 /* no current LBA */
1554 aReply[2] = 0;
1555 aReply[3] = 0;
1556 aReply[4] = 0;
1557 aReply[5] = 1;
1558 scsiH2BE_U16(&aReply[6], 0);
1559 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1560 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1561 break;
1562 }
1563 case SCSI_READ_DISC_INFORMATION:
1564 {
1565 uint8_t aReply[34];
1566 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1567
1568 vscsiReqSetXferSize(pVScsiReq, cbMax);
1569 memset(aReply, '\0', sizeof(aReply));
1570 scsiH2BE_U16(&aReply[0], 32);
1571 aReply[2] = (0 << 4) | (3 << 2) | (2 << 0); /* not erasable, complete session, complete disc */
1572 aReply[3] = 1; /* number of first track */
1573 aReply[4] = 1; /* number of sessions (LSB) */
1574 aReply[5] = 1; /* first track number in last session (LSB) */
1575 aReply[6] = (uint8_t)vscsiLunMediumGetRegionCount(pVScsiLun); /* last track number in last session (LSB) */
1576 aReply[7] = (0 << 7) | (0 << 6) | (1 << 5) | (0 << 2) | (0 << 0); /* disc id not valid, disc bar code not valid, unrestricted use, not dirty, not RW medium */
1577 aReply[8] = 0; /* disc type = CD-ROM */
1578 aReply[9] = 0; /* number of sessions (MSB) */
1579 aReply[10] = 0; /* number of sessions (MSB) */
1580 aReply[11] = 0; /* number of sessions (MSB) */
1581 scsiH2BE_U32(&aReply[16], 0x00ffffff); /* last session lead-in start time is not available */
1582 scsiH2BE_U32(&aReply[20], 0x00ffffff); /* last possible start time for lead-out is not available */
1583 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1584 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1585 break;
1586 }
1587 case SCSI_READ_TRACK_INFORMATION:
1588 {
1589 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1590 vscsiReqSetXferSize(pVScsiReq, cbMax);
1591 rcReq = vscsiLunMmcReadTrackInformation(pVScsiLunMmc, pVScsiReq, cbMax);
1592 break;
1593 }
1594 case SCSI_GET_CONFIGURATION:
1595 {
1596 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1597 vscsiReqSetXferSize(pVScsiReq, cbMax);
1598 rcReq = vscsiLunMmcGetConfiguration(pVScsiLunMmc, pVScsiReq, cbMax);
1599 break;
1600 }
1601 case SCSI_READ_DVD_STRUCTURE:
1602 {
1603 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1604 vscsiReqSetXferSize(pVScsiReq, cbMax);
1605 rcReq = vscsiLunMmcReadDvdStructure(pVScsiLunMmc, pVScsiReq, cbMax);
1606 break;
1607 }
1608 default:
1609 //AssertMsgFailed(("Command %#x [%s] not implemented\n", pVScsiReq->pbCDB[0], SCSICmdText(pVScsiReq->pbCDB[0])));
1610 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE, 0x00);
1611 }
1612 }
1613
1614 if (enmTxDir != VSCSIIOREQTXDIR_INVALID)
1615 {
1616 LogFlow(("%s: uLbaStart=%llu cSectorTransfer=%u\n",
1617 __FUNCTION__, uLbaStart, cSectorTransfer));
1618
1619 vscsiReqSetXferSize(pVScsiReq, cSectorTransfer * cbSector);
1620 if (RT_UNLIKELY(uLbaStart + cSectorTransfer > pVScsiLunMmc->cSectors))
1621 {
1622 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1623 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1624 }
1625 else if (!cSectorTransfer)
1626 {
1627 /* A 0 transfer length is not an error. */
1628 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1629 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1630 }
1631 else
1632 {
1633 /* Check that the sector size is valid. */
1634 VDREGIONDATAFORM enmDataForm = VDREGIONDATAFORM_INVALID;
1635 rc = vscsiLunMediumQueryRegionPropertiesForLba(pVScsiLun, uLbaStart,
1636 NULL, NULL, NULL, &enmDataForm);
1637 if (RT_FAILURE(rc))
1638 {
1639 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1640 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1641 }
1642 else if ( enmDataForm != VDREGIONDATAFORM_MODE1_2048
1643 && enmDataForm != VDREGIONDATAFORM_MODE1_2352
1644 && enmDataForm != VDREGIONDATAFORM_MODE2_2336
1645 && enmDataForm != VDREGIONDATAFORM_MODE2_2352
1646 && enmDataForm != VDREGIONDATAFORM_RAW
1647 && cbSector == _2K)
1648 {
1649 rcReq = vscsiLunReqSenseErrorInfoSet(pVScsiLun, pVScsiReq,
1650 SCSI_SENSE_ILLEGAL_REQUEST | SCSI_SENSE_FLAG_ILI,
1651 SCSI_ASC_ILLEGAL_MODE_FOR_THIS_TRACK, 0, uLbaStart);
1652 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1653 }
1654 else
1655 {
1656 /* Enqueue new I/O request */
1657 rc = vscsiIoReqTransferEnqueueEx(pVScsiLun, pVScsiReq, enmTxDir,
1658 uLbaStart * cbSector,
1659 paSegs, cSegs, cSectorTransfer * cbSector);
1660 }
1661 }
1662 }
1663 else /* Request completed */
1664 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1665
1666 return rc;
1667}
1668
1669/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunReqFree} */
1670static DECLCALLBACK(void) vscsiLunMmcReqFree(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq,
1671 void *pvLun)
1672{
1673 RT_NOREF2(pVScsiLun, pVScsiReq);
1674 RTMemFree(pvLun);
1675}
1676
1677/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumInserted} */
1678static DECLCALLBACK(int) vscsiLunMmcMediumInserted(PVSCSILUNINT pVScsiLun)
1679{
1680 int rc = VINF_SUCCESS;
1681 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1682
1683 pVScsiLunMmc->cSectors = 0;
1684 uint32_t cTracks = vscsiLunMediumGetRegionCount(pVScsiLun);
1685 for (uint32_t i = 0; i < cTracks && RT_SUCCESS(rc); i++)
1686 {
1687 uint64_t cBlocks = 0;
1688 rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, i, NULL, &cBlocks,
1689 NULL, NULL);
1690 if (RT_FAILURE(rc))
1691 break;
1692 pVScsiLunMmc->cSectors += cBlocks;
1693 }
1694
1695 if (RT_SUCCESS(rc))
1696 {
1697 uint32_t OldStatus, NewStatus;
1698 do
1699 {
1700 OldStatus = ASMAtomicReadU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus);
1701 switch (OldStatus)
1702 {
1703 case MMCEVENTSTATUSTYPE_MEDIA_CHANGED:
1704 case MMCEVENTSTATUSTYPE_MEDIA_REMOVED:
1705 /* no change, we will send "medium removed" + "medium inserted" */
1706 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_CHANGED;
1707 break;
1708 default:
1709 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_NEW;
1710 break;
1711 }
1712 } while (!ASMAtomicCmpXchgU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus,
1713 NewStatus, OldStatus));
1714
1715 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_UNKNOWN);
1716 }
1717
1718 return rc;
1719}
1720
1721/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumRemoved} */
1722static DECLCALLBACK(int) vscsiLunMmcMediumRemoved(PVSCSILUNINT pVScsiLun)
1723{
1724 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1725
1726 ASMAtomicWriteU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, MMCEVENTSTATUSTYPE_MEDIA_REMOVED);
1727 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_NO_DISC);
1728 pVScsiLunMmc->cSectors = 0;
1729 return VINF_SUCCESS;
1730}
1731
1732
1733VSCSILUNDESC g_VScsiLunTypeMmc =
1734{
1735 /** enmLunType */
1736 VSCSILUNTYPE_MMC,
1737 /** pcszDescName */
1738 "MMC",
1739 /** cbLun */
1740 sizeof(VSCSILUNMMC),
1741 /** cSupOpcInfo */
1742 0,
1743 /** paSupOpcInfo */
1744 NULL,
1745 /** pfnVScsiLunInit */
1746 vscsiLunMmcInit,
1747 /** pfnVScsiLunDestroy */
1748 vscsiLunMmcDestroy,
1749 /** pfnVScsiLunReqProcess */
1750 vscsiLunMmcReqProcess,
1751 /** pfnVScsiLunReqFree */
1752 vscsiLunMmcReqFree,
1753 /** pfnVScsiLunMediumInserted */
1754 vscsiLunMmcMediumInserted,
1755 /** pfnVScsiLunMediumRemoved */
1756 vscsiLunMmcMediumRemoved
1757};
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