VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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