VirtualBox

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

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

backed out r118835 as it incorrectly updated the 'This file is based on' file headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 70.0 KB
Line 
1/* $Id: VSCSILunMmc.cpp 69498 2017-10-28 15:07:25Z vboxsync $ */
2/** @file
3 * Virtual SCSI driver: MMC LUN implementation (CD/DVD-ROM)
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 scsiPadStrS(ScsiInquiryReply.achVendorId, "VBOX", 8);
1106 scsiPadStrS(ScsiInquiryReply.achProductId, "CD-ROM", 16);
1107 scsiPadStrS(ScsiInquiryReply.achProductLevel, "1.0", 4);
1108
1109 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, (uint8_t *)&ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
1110 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1111 break;
1112 }
1113 case SCSI_READ_CAPACITY:
1114 {
1115 uint8_t aReply[8];
1116 memset(aReply, 0, sizeof(aReply));
1117 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
1118
1119 /*
1120 * If sector size exceeds the maximum value that is
1121 * able to be stored in 4 bytes return 0xffffffff in this field
1122 */
1123 if (pVScsiLunMmc->cSectors > UINT32_C(0xffffffff))
1124 scsiH2BE_U32(aReply, UINT32_C(0xffffffff));
1125 else
1126 scsiH2BE_U32(aReply, pVScsiLunMmc->cSectors - 1);
1127 scsiH2BE_U32(&aReply[4], _2K);
1128 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1129 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1130 break;
1131 }
1132 case SCSI_MODE_SENSE_6:
1133 {
1134 uint8_t uModePage = pVScsiReq->pbCDB[2] & 0x3f;
1135 uint8_t aReply[24];
1136 uint8_t *pu8ReplyPos;
1137 bool fValid = false;
1138
1139 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
1140 memset(aReply, 0, sizeof(aReply));
1141 aReply[0] = 4; /* Reply length 4. */
1142 aReply[1] = 0; /* Default media type. */
1143 aReply[2] = RT_BIT(4); /* Caching supported. */
1144 aReply[3] = 0; /* Block descriptor length. */
1145
1146 pu8ReplyPos = aReply + 4;
1147
1148 if ((uModePage == 0x08) || (uModePage == 0x3f))
1149 {
1150 memset(pu8ReplyPos, 0, 20);
1151 *pu8ReplyPos++ = 0x08; /* Page code. */
1152 *pu8ReplyPos++ = 0x12; /* Size of the page. */
1153 *pu8ReplyPos++ = 0x4; /* Write cache enabled. */
1154 fValid = true;
1155 } else if (uModePage == 0) {
1156 fValid = true;
1157 }
1158
1159 /* Querying unknown pages must fail. */
1160 if (fValid) {
1161 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1162 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1163 } else {
1164 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1165 }
1166 break;
1167 }
1168 case SCSI_MODE_SENSE_10:
1169 {
1170 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1171 vscsiReqSetXferSize(pVScsiReq, cbMax);
1172 rcReq = vscsiLunMmcModeSense10(pVScsiLunMmc, pVScsiReq, cbMax);
1173 break;
1174 }
1175 case SCSI_SEEK_10:
1176 {
1177 uint32_t uLba = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1178 if (uLba > pVScsiLunMmc->cSectors)
1179 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1180 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1181 else
1182 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1183 break;
1184 }
1185 case SCSI_MODE_SELECT_6:
1186 {
1187 /** @todo implement!! */
1188 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
1189 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1190 break;
1191 }
1192 case SCSI_READ_6:
1193 {
1194 enmTxDir = VSCSIIOREQTXDIR_READ;
1195 uLbaStart = ((uint64_t) pVScsiReq->pbCDB[3]
1196 | (pVScsiReq->pbCDB[2] << 8)
1197 | ((pVScsiReq->pbCDB[1] & 0x1f) << 16));
1198 cSectorTransfer = pVScsiReq->pbCDB[4];
1199 cbSector = _2K;
1200 break;
1201 }
1202 case SCSI_READ_10:
1203 {
1204 enmTxDir = VSCSIIOREQTXDIR_READ;
1205 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1206 cSectorTransfer = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1207 cbSector = _2K;
1208 break;
1209 }
1210 case SCSI_READ_12:
1211 {
1212 enmTxDir = VSCSIIOREQTXDIR_READ;
1213 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1214 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[6]);
1215 cbSector = _2K;
1216 break;
1217 }
1218 case SCSI_READ_16:
1219 {
1220 enmTxDir = VSCSIIOREQTXDIR_READ;
1221 uLbaStart = scsiBE2H_U64(&pVScsiReq->pbCDB[2]);
1222 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[10]);
1223 cbSector = _2K;
1224 break;
1225 }
1226 case SCSI_READ_CD:
1227 {
1228 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
1229 cSectorTransfer = (pVScsiReq->pbCDB[6] << 16) | (pVScsiReq->pbCDB[7] << 8) | pVScsiReq->pbCDB[8];
1230
1231 /*
1232 * If the LBA is in an audio track we are required to ignore pretty much all
1233 * of the channel selection values (except 0x00) and map everything to 0x10
1234 * which means read user data with a sector size of 2352 bytes.
1235 *
1236 * (MMC-6 chapter 6.19.2.6)
1237 */
1238 uint8_t uChnSel = pVScsiReq->pbCDB[9] & 0xf8;
1239 VDREGIONDATAFORM enmDataForm;
1240 uint64_t cbSectorRegion = 0;
1241 rc = vscsiLunMediumQueryRegionPropertiesForLba(pVScsiLun, uLbaStart,
1242 NULL, NULL, &cbSectorRegion,
1243 &enmDataForm);
1244 AssertRC(rc);
1245
1246 if (enmDataForm == VDREGIONDATAFORM_CDDA)
1247 {
1248 if (uChnSel == 0)
1249 {
1250 /* nothing */
1251 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1252 }
1253 else
1254 {
1255 enmTxDir = VSCSIIOREQTXDIR_READ;
1256 cbSector = 2352;
1257 }
1258 }
1259 else
1260 {
1261 switch (uChnSel)
1262 {
1263 case 0x00:
1264 /* nothing */
1265 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1266 break;
1267 case 0x10:
1268 /* normal read */
1269 enmTxDir = VSCSIIOREQTXDIR_READ;
1270 cbSector = _2K;
1271 break;
1272 case 0xf8:
1273 {
1274 if (cbSectorRegion == 2048)
1275 {
1276 /*
1277 * Read all data, sector size is 2352.
1278 * Rearrange the buffer and fill the gaps with the sync bytes.
1279 */
1280 /* Count the number of segments for the buffer we require. */
1281 RTSGBUF SgBuf;
1282 bool fBufTooSmall = false;
1283 uint32_t cSegsNew = 0;
1284 RTSgBufClone(&SgBuf, &pVScsiReq->SgBuf);
1285 for (uint32_t uLba = (uint32_t)uLbaStart; uLba < uLbaStart + cSectorTransfer; uLba++)
1286 {
1287 size_t cbTmp = RTSgBufAdvance(&SgBuf, 16);
1288 if (cbTmp < 16)
1289 {
1290 fBufTooSmall = true;
1291 break;
1292 }
1293
1294 cbTmp = 2048;
1295 while (cbTmp)
1296 {
1297 size_t cbBuf = cbTmp;
1298 RTSgBufGetNextSegment(&SgBuf, &cbBuf);
1299 if (!cbBuf)
1300 {
1301 fBufTooSmall = true;
1302 break;
1303 }
1304
1305 cbTmp -= cbBuf;
1306 cSegsNew++;
1307 }
1308
1309 cbTmp = RTSgBufAdvance(&SgBuf, 280);
1310 if (cbTmp < 280)
1311 {
1312 fBufTooSmall = true;
1313 break;
1314 }
1315 }
1316
1317 if (!fBufTooSmall)
1318 {
1319 PRTSGSEG paSegsNew = (PRTSGSEG)RTMemAllocZ(cSegsNew * sizeof(RTSGSEG));
1320 if (paSegsNew)
1321 {
1322 enmTxDir = VSCSIIOREQTXDIR_READ;
1323
1324 uint32_t idxSeg = 0;
1325 for (uint32_t uLba = (uint32_t)uLbaStart; uLba < uLbaStart + cSectorTransfer; uLba++)
1326 {
1327 /* Sync bytes, see 4.2.3.8 CD Main Channel Block Formats */
1328 uint8_t abBuf[16];
1329 abBuf[0] = 0x00;
1330 memset(&abBuf[1], 0xff, 10);
1331 abBuf[11] = 0x00;
1332 /* MSF */
1333 scsiLBA2MSF(&abBuf[12], uLba);
1334 abBuf[15] = 0x01; /* mode 1 data */
1335 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, &abBuf[0], sizeof(abBuf));
1336
1337 size_t cbTmp = 2048;
1338 while (cbTmp)
1339 {
1340 size_t cbBuf = cbTmp;
1341 paSegsNew[idxSeg].pvSeg = RTSgBufGetNextSegment(&pVScsiReq->SgBuf, &cbBuf);
1342 paSegsNew[idxSeg].cbSeg = cbBuf;
1343 idxSeg++;
1344
1345 cbTmp -= cbBuf;
1346 }
1347
1348 /**
1349 * @todo: maybe compute ECC and parity, layout is:
1350 * 2072 4 EDC
1351 * 2076 172 P parity symbols
1352 * 2248 104 Q parity symbols
1353 */
1354 RTSgBufSet(&pVScsiReq->SgBuf, 0, 280);
1355 }
1356
1357 paSegs = paSegsNew;
1358 cSegs = cSegsNew;
1359 pVScsiReq->pvLun = paSegsNew;
1360 }
1361 else
1362 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1363 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1364 }
1365 else
1366 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1367 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1368 }
1369 else if (cbSectorRegion == 2352)
1370 {
1371 /* Sector size matches what is read. */
1372 cbSector = cbSectorRegion;
1373 enmTxDir = VSCSIIOREQTXDIR_READ;
1374 }
1375 break;
1376 }
1377 default:
1378 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1379 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1380 break;
1381 }
1382 }
1383 break;
1384 }
1385 case SCSI_READ_BUFFER:
1386 {
1387 uint8_t uDataMode = pVScsiReq->pbCDB[1] & 0x1f;
1388
1389 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[6]));
1390
1391 switch (uDataMode)
1392 {
1393 case 0x00:
1394 case 0x01:
1395 case 0x02:
1396 case 0x03:
1397 case 0x0a:
1398 break;
1399 case 0x0b:
1400 {
1401 uint8_t aReply[4];
1402 RT_ZERO(aReply);
1403
1404 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1405 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1406 break;
1407 }
1408 case 0x1a:
1409 case 0x1c:
1410 break;
1411 default:
1412 AssertMsgFailed(("Invalid data mode\n"));
1413 }
1414 break;
1415 }
1416 case SCSI_VERIFY_10:
1417 case SCSI_START_STOP_UNIT:
1418 {
1419 int rc2 = VINF_SUCCESS;
1420 switch (pVScsiReq->pbCDB[4] & 3)
1421 {
1422 case 0: /* 00 - Stop motor */
1423 case 1: /* 01 - Start motor */
1424 break;
1425 case 2: /* 10 - Eject media */
1426 rc2 = vscsiLunMediumEject(pVScsiLun);
1427 break;
1428 case 3: /* 11 - Load media */
1429 /** @todo */
1430 break;
1431 }
1432 if (RT_SUCCESS(rc2))
1433 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1434 else
1435 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_MEDIA_LOAD_OR_EJECT_FAILED, 0x02);
1436 break;
1437 }
1438 case SCSI_LOG_SENSE:
1439 {
1440 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
1441 uint8_t uSubPageCode = pVScsiReq->pbCDB[3];
1442
1443 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[7]));
1444
1445 switch (uPageCode)
1446 {
1447 case 0x00:
1448 {
1449 if (uSubPageCode == 0)
1450 {
1451 uint8_t aReply[4];
1452
1453 aReply[0] = 0;
1454 aReply[1] = 0;
1455 aReply[2] = 0;
1456 aReply[3] = 0;
1457
1458 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1459 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1460 break;
1461 }
1462 }
1463 RT_FALL_THRU();
1464 default:
1465 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1466 }
1467 break;
1468 }
1469 case SCSI_SERVICE_ACTION_IN_16:
1470 {
1471 switch (pVScsiReq->pbCDB[1] & 0x1f)
1472 {
1473 case SCSI_SVC_ACTION_IN_READ_CAPACITY_16:
1474 {
1475 uint8_t aReply[32];
1476
1477 memset(aReply, 0, sizeof(aReply));
1478 scsiH2BE_U64(aReply, pVScsiLunMmc->cSectors - 1);
1479 scsiH2BE_U32(&aReply[8], _2K);
1480 /* Leave the rest 0 */
1481
1482 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
1483 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1484 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1485 break;
1486 }
1487 default:
1488 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1489 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00); /* Don't know if this is correct */
1490 }
1491 break;
1492 }
1493 case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
1494 {
1495 pVScsiLunMmc->fLocked = RT_BOOL(pVScsiReq->pbCDB[4] & 0x01);
1496 vscsiLunMediumSetLock(pVScsiLun, pVScsiLunMmc->fLocked);
1497 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1498 break;
1499 }
1500 case SCSI_READ_TOC_PMA_ATIP:
1501 {
1502 uint8_t format;
1503 uint16_t cbMax;
1504 bool fMSF;
1505
1506 format = pVScsiReq->pbCDB[2] & 0x0f;
1507 cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1508 fMSF = (pVScsiReq->pbCDB[1] >> 1) & 1;
1509
1510 vscsiReqSetXferSize(pVScsiReq, cbMax);
1511 switch (format)
1512 {
1513 case 0x00:
1514 rcReq = mmcReadTOCNormal(pVScsiLun, pVScsiReq, cbMax, fMSF);
1515 break;
1516 case 0x01:
1517 rcReq = mmcReadTOCMulti(pVScsiLun, pVScsiReq, cbMax, fMSF);
1518 break;
1519 case 0x02:
1520 rcReq = mmcReadTOCRaw(pVScsiLun, pVScsiReq, cbMax, fMSF);
1521 break;
1522 default:
1523 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1524 }
1525 break;
1526 }
1527 case SCSI_GET_EVENT_STATUS_NOTIFICATION:
1528 {
1529 /* Only supporting polled mode at the moment. */
1530 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1531
1532 vscsiReqSetXferSize(pVScsiReq, cbMax);
1533 if (pVScsiReq->pbCDB[1] & 0x1)
1534 rcReq = vscsiLunMmcGetEventStatusNotification(pVScsiLunMmc, pVScsiReq, cbMax);
1535 else
1536 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1537 break;
1538 }
1539 case SCSI_MECHANISM_STATUS:
1540 {
1541 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1542 uint8_t aReply[8];
1543
1544 vscsiReqSetXferSize(pVScsiReq, cbMax);
1545 scsiH2BE_U16(&aReply[0], 0);
1546 /* no current LBA */
1547 aReply[2] = 0;
1548 aReply[3] = 0;
1549 aReply[4] = 0;
1550 aReply[5] = 1;
1551 scsiH2BE_U16(&aReply[6], 0);
1552 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1553 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1554 break;
1555 }
1556 case SCSI_READ_DISC_INFORMATION:
1557 {
1558 uint8_t aReply[34];
1559 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1560
1561 vscsiReqSetXferSize(pVScsiReq, cbMax);
1562 memset(aReply, '\0', sizeof(aReply));
1563 scsiH2BE_U16(&aReply[0], 32);
1564 aReply[2] = (0 << 4) | (3 << 2) | (2 << 0); /* not erasable, complete session, complete disc */
1565 aReply[3] = 1; /* number of first track */
1566 aReply[4] = 1; /* number of sessions (LSB) */
1567 aReply[5] = 1; /* first track number in last session (LSB) */
1568 aReply[6] = (uint8_t)vscsiLunMediumGetRegionCount(pVScsiLun); /* last track number in last session (LSB) */
1569 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 */
1570 aReply[8] = 0; /* disc type = CD-ROM */
1571 aReply[9] = 0; /* number of sessions (MSB) */
1572 aReply[10] = 0; /* number of sessions (MSB) */
1573 aReply[11] = 0; /* number of sessions (MSB) */
1574 scsiH2BE_U32(&aReply[16], 0x00ffffff); /* last session lead-in start time is not available */
1575 scsiH2BE_U32(&aReply[20], 0x00ffffff); /* last possible start time for lead-out is not available */
1576 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1577 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1578 break;
1579 }
1580 case SCSI_READ_TRACK_INFORMATION:
1581 {
1582 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1583 vscsiReqSetXferSize(pVScsiReq, cbMax);
1584 rcReq = vscsiLunMmcReadTrackInformation(pVScsiLunMmc, pVScsiReq, cbMax);
1585 break;
1586 }
1587 case SCSI_GET_CONFIGURATION:
1588 {
1589 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1590 vscsiReqSetXferSize(pVScsiReq, cbMax);
1591 rcReq = vscsiLunMmcGetConfiguration(pVScsiLunMmc, pVScsiReq, cbMax);
1592 break;
1593 }
1594 case SCSI_READ_DVD_STRUCTURE:
1595 {
1596 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1597 vscsiReqSetXferSize(pVScsiReq, cbMax);
1598 rcReq = vscsiLunMmcReadDvdStructure(pVScsiLunMmc, pVScsiReq, cbMax);
1599 break;
1600 }
1601 default:
1602 //AssertMsgFailed(("Command %#x [%s] not implemented\n", pVScsiReq->pbCDB[0], SCSICmdText(pVScsiReq->pbCDB[0])));
1603 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE, 0x00);
1604 }
1605 }
1606
1607 if (enmTxDir != VSCSIIOREQTXDIR_INVALID)
1608 {
1609 LogFlow(("%s: uLbaStart=%llu cSectorTransfer=%u\n",
1610 __FUNCTION__, uLbaStart, cSectorTransfer));
1611
1612 vscsiReqSetXferSize(pVScsiReq, cSectorTransfer * cbSector);
1613 if (RT_UNLIKELY(uLbaStart + cSectorTransfer > pVScsiLunMmc->cSectors))
1614 {
1615 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1616 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1617 }
1618 else if (!cSectorTransfer)
1619 {
1620 /* A 0 transfer length is not an error. */
1621 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1622 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1623 }
1624 else
1625 {
1626 /* Check that the sector size is valid. */
1627 VDREGIONDATAFORM enmDataForm = VDREGIONDATAFORM_INVALID;
1628 rc = vscsiLunMediumQueryRegionPropertiesForLba(pVScsiLun, uLbaStart,
1629 NULL, NULL, NULL, &enmDataForm);
1630 if (RT_FAILURE(rc))
1631 {
1632 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1633 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1634 }
1635 else if ( enmDataForm != VDREGIONDATAFORM_MODE1_2048
1636 && enmDataForm != VDREGIONDATAFORM_MODE1_2352
1637 && enmDataForm != VDREGIONDATAFORM_MODE2_2336
1638 && enmDataForm != VDREGIONDATAFORM_MODE2_2352
1639 && enmDataForm != VDREGIONDATAFORM_RAW
1640 && cbSector == _2K)
1641 {
1642 rcReq = vscsiLunReqSenseErrorInfoSet(pVScsiLun, pVScsiReq,
1643 SCSI_SENSE_ILLEGAL_REQUEST | SCSI_SENSE_FLAG_ILI,
1644 SCSI_ASC_ILLEGAL_MODE_FOR_THIS_TRACK, 0, uLbaStart);
1645 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1646 }
1647 else
1648 {
1649 /* Enqueue new I/O request */
1650 rc = vscsiIoReqTransferEnqueueEx(pVScsiLun, pVScsiReq, enmTxDir,
1651 uLbaStart * cbSector,
1652 paSegs, cSegs, cSectorTransfer * cbSector);
1653 }
1654 }
1655 }
1656 else /* Request completed */
1657 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1658
1659 return rc;
1660}
1661
1662/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunReqFree} */
1663static DECLCALLBACK(void) vscsiLunMmcReqFree(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq,
1664 void *pvLun)
1665{
1666 RT_NOREF2(pVScsiLun, pVScsiReq);
1667 RTMemFree(pvLun);
1668}
1669
1670/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumInserted} */
1671static DECLCALLBACK(int) vscsiLunMmcMediumInserted(PVSCSILUNINT pVScsiLun)
1672{
1673 int rc = VINF_SUCCESS;
1674 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1675
1676 pVScsiLunMmc->cSectors = 0;
1677 uint32_t cTracks = vscsiLunMediumGetRegionCount(pVScsiLun);
1678 for (uint32_t i = 0; i < cTracks && RT_SUCCESS(rc); i++)
1679 {
1680 uint64_t cBlocks = 0;
1681 rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, i, NULL, &cBlocks,
1682 NULL, NULL);
1683 if (RT_FAILURE(rc))
1684 break;
1685 pVScsiLunMmc->cSectors += cBlocks;
1686 }
1687
1688 if (RT_SUCCESS(rc))
1689 {
1690 uint32_t OldStatus, NewStatus;
1691 do
1692 {
1693 OldStatus = ASMAtomicReadU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus);
1694 switch (OldStatus)
1695 {
1696 case MMCEVENTSTATUSTYPE_MEDIA_CHANGED:
1697 case MMCEVENTSTATUSTYPE_MEDIA_REMOVED:
1698 /* no change, we will send "medium removed" + "medium inserted" */
1699 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_CHANGED;
1700 break;
1701 default:
1702 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_NEW;
1703 break;
1704 }
1705 } while (!ASMAtomicCmpXchgU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus,
1706 NewStatus, OldStatus));
1707
1708 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_UNKNOWN);
1709 }
1710
1711 return rc;
1712}
1713
1714/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumRemoved} */
1715static DECLCALLBACK(int) vscsiLunMmcMediumRemoved(PVSCSILUNINT pVScsiLun)
1716{
1717 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1718
1719 ASMAtomicWriteU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, MMCEVENTSTATUSTYPE_MEDIA_REMOVED);
1720 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_NO_DISC);
1721 pVScsiLunMmc->cSectors = 0;
1722 return VINF_SUCCESS;
1723}
1724
1725
1726VSCSILUNDESC g_VScsiLunTypeMmc =
1727{
1728 /** enmLunType */
1729 VSCSILUNTYPE_MMC,
1730 /** pcszDescName */
1731 "MMC",
1732 /** cbLun */
1733 sizeof(VSCSILUNMMC),
1734 /** cSupOpcInfo */
1735 0,
1736 /** paSupOpcInfo */
1737 NULL,
1738 /** pfnVScsiLunInit */
1739 vscsiLunMmcInit,
1740 /** pfnVScsiLunDestroy */
1741 vscsiLunMmcDestroy,
1742 /** pfnVScsiLunReqProcess */
1743 vscsiLunMmcReqProcess,
1744 /** pfnVScsiLunReqFree */
1745 vscsiLunMmcReqFree,
1746 /** pfnVScsiLunMediumInserted */
1747 vscsiLunMmcMediumInserted,
1748 /** pfnVScsiLunMediumRemoved */
1749 vscsiLunMmcMediumRemoved
1750};
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