VirtualBox

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

Last change on this file since 65620 was 65111, checked in by vboxsync, 8 years ago

Storage/Devices/VSCSI: Make use of the new SCSI inline helpers and remove the the local inlines

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.7 KB
Line 
1/* $Id: VSCSILunMmc.cpp 65111 2017-01-04 14:07:58Z 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 * Different event status types.
36 */
37typedef enum MMCEVENTSTATUSTYPE
38{
39 /** Medium event status not changed. */
40 MMCEVENTSTATUSTYPE_UNCHANGED = 0,
41 /** New medium inserted. */
42 MMCEVENTSTATUSTYPE_MEDIA_NEW,
43 /** Medium removed. */
44 MMCEVENTSTATUSTYPE_MEDIA_REMOVED,
45 /** Medium was removed + new medium was inserted. */
46 MMCEVENTSTATUSTYPE_MEDIA_CHANGED,
47 /** Medium eject requested (eject button pressed). */
48 MMCEVENTSTATUSTYPE_MEDIA_EJECT_REQUESTED,
49 /** 32bit hack. */
50 MMCEVENTSTATUSTYPE_32BIT_HACK = 0x7fffffff
51} MMCEVENTSTATUSTYPE;
52
53/** @name Media track types.
54 * @{ */
55/** Unknown media type. */
56#define MMC_MEDIA_TYPE_UNKNOWN 0
57/** Door closed, no media. */
58#define MMC_MEDIA_TYPE_NO_DISC 0x70
59/** @} */
60
61
62/**
63 * MMC LUN instance
64 */
65typedef struct VSCSILUNMMC
66{
67 /** Core LUN structure */
68 VSCSILUNINT Core;
69 /** Size of the virtual disk. */
70 uint64_t cSectors;
71 /** Sector size. */
72 uint32_t cbSector;
73 /** Medium locked indicator. */
74 bool fLocked;
75 /** Media event status. */
76 volatile MMCEVENTSTATUSTYPE MediaEventStatus;
77 /** Media track type. */
78 volatile uint32_t u32MediaTrackType;
79} VSCSILUNMMC, *PVSCSILUNMMC;
80
81
82/* Fabricate normal TOC information. */
83static int mmcReadTOCNormal(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
84{
85 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
86 uint8_t aReply[32];
87 uint8_t *pbBuf = aReply;
88 uint8_t *q;
89 uint8_t iStartTrack;
90 uint32_t cbSize;
91
92 iStartTrack = pVScsiReq->pbCDB[6];
93 if (iStartTrack > 1 && iStartTrack != 0xaa)
94 {
95 return vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
96 }
97 q = pbBuf + 2;
98 *q++ = 1; /* first session */
99 *q++ = 1; /* last session */
100 if (iStartTrack <= 1)
101 {
102 *q++ = 0; /* reserved */
103 *q++ = 0x14; /* ADR, CONTROL */
104 *q++ = 1; /* track number */
105 *q++ = 0; /* reserved */
106 if (fMSF)
107 {
108 *q++ = 0; /* reserved */
109 scsiLBA2MSF(q, 0);
110 q += 3;
111 }
112 else
113 {
114 /* sector 0 */
115 scsiH2BE_U32(q, 0);
116 q += 4;
117 }
118 }
119 /* lead out track */
120 *q++ = 0; /* reserved */
121 *q++ = 0x14; /* ADR, CONTROL */
122 *q++ = 0xaa; /* track number */
123 *q++ = 0; /* reserved */
124 if (fMSF)
125 {
126 *q++ = 0; /* reserved */
127 scsiLBA2MSF(q, pVScsiLunMmc->cSectors);
128 q += 3;
129 }
130 else
131 {
132 scsiH2BE_U32(q, pVScsiLunMmc->cSectors);
133 q += 4;
134 }
135 cbSize = q - pbBuf;
136 Assert(cbSize <= sizeof(aReply));
137 scsiH2BE_U16(pbBuf, cbSize - 2);
138 if (cbSize < cbMaxTransfer)
139 cbMaxTransfer = cbSize;
140
141 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, cbMaxTransfer);
142
143 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
144}
145
146/* Fabricate session information. */
147static int mmcReadTOCMulti(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
148{
149 RT_NOREF1(cbMaxTransfer);
150 uint8_t aReply[32];
151 uint8_t *pbBuf = aReply;
152
153 /* multi session: only a single session defined */
154 memset(pbBuf, 0, 12);
155 pbBuf[1] = 0x0a;
156 pbBuf[2] = 0x01; /* first complete session number */
157 pbBuf[3] = 0x01; /* last complete session number */
158 pbBuf[5] = 0x14; /* ADR, CONTROL */
159 pbBuf[6] = 1; /* first track in last complete session */
160
161 if (fMSF)
162 {
163 pbBuf[8] = 0; /* reserved */
164 scsiLBA2MSF(pbBuf + 8, 0);
165 }
166 else
167 {
168 /* sector 0 */
169 scsiH2BE_U32(pbBuf + 8, 0);
170 }
171
172 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, 12);
173
174 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
175}
176
177/**
178 * Create raw TOC data information.
179 *
180 * @returns SCSI status code.
181 * @param pVScsiLun The LUN instance.
182 * @param pVScsiReq The VSCSI request.
183 * @param cbMaxTransfer The maximum transfer size.
184 * @param fMSF Flag whether to use MSF format to encode sector numbers.
185 */
186static int mmcReadTOCRaw(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq, uint16_t cbMaxTransfer, bool fMSF)
187{
188 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
189 uint8_t aReply[50]; /* Counted a maximum of 45 bytes but better be on the safe side. */
190 uint32_t cbSize;
191 uint8_t *pbBuf = &aReply[0] + 2;
192
193 *pbBuf++ = 1; /* first session */
194 *pbBuf++ = 1; /* last session */
195
196 *pbBuf++ = 1; /* session number */
197 *pbBuf++ = 0x14; /* data track */
198 *pbBuf++ = 0; /* track number */
199 *pbBuf++ = 0xa0; /* first track in program area */
200 *pbBuf++ = 0; /* min */
201 *pbBuf++ = 0; /* sec */
202 *pbBuf++ = 0; /* frame */
203 *pbBuf++ = 0;
204 *pbBuf++ = 1; /* first track */
205 *pbBuf++ = 0x00; /* disk type CD-DA or CD data */
206 *pbBuf++ = 0;
207
208 *pbBuf++ = 1; /* session number */
209 *pbBuf++ = 0x14; /* data track */
210 *pbBuf++ = 0; /* track number */
211 *pbBuf++ = 0xa1; /* last track in program area */
212 *pbBuf++ = 0; /* min */
213 *pbBuf++ = 0; /* sec */
214 *pbBuf++ = 0; /* frame */
215 *pbBuf++ = 0;
216 *pbBuf++ = 1; /* last track */
217 *pbBuf++ = 0;
218 *pbBuf++ = 0;
219
220 *pbBuf++ = 1; /* session number */
221 *pbBuf++ = 0x14; /* data track */
222 *pbBuf++ = 0; /* track number */
223 *pbBuf++ = 0xa2; /* lead-out */
224 *pbBuf++ = 0; /* min */
225 *pbBuf++ = 0; /* sec */
226 *pbBuf++ = 0; /* frame */
227 if (fMSF)
228 {
229 *pbBuf++ = 0; /* reserved */
230 scsiLBA2MSF(pbBuf, pVScsiLunMmc->cSectors);
231 pbBuf += 3;
232 }
233 else
234 {
235 scsiH2BE_U32(pbBuf, pVScsiLunMmc->cSectors);
236 pbBuf += 4;
237 }
238
239 *pbBuf++ = 1; /* session number */
240 *pbBuf++ = 0x14; /* ADR, control */
241 *pbBuf++ = 0; /* track number */
242 *pbBuf++ = 1; /* point */
243 *pbBuf++ = 0; /* min */
244 *pbBuf++ = 0; /* sec */
245 *pbBuf++ = 0; /* frame */
246 if (fMSF)
247 {
248 *pbBuf++ = 0; /* reserved */
249 scsiLBA2MSF(pbBuf, 0);
250 pbBuf += 3;
251 }
252 else
253 {
254 /* sector 0 */
255 scsiH2BE_U32(pbBuf, 0);
256 pbBuf += 4;
257 }
258
259 cbSize = pbBuf - aReply;
260 scsiH2BE_U16(&aReply[0], cbSize - 2);
261
262 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, cbSize));
263 return vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
264}
265
266static size_t vscsiLunMmcGetConfigurationFillFeatureListProfiles(uint8_t *pbBuf, size_t cbBuf)
267{
268 if (cbBuf < 3*4)
269 return 0;
270
271 scsiH2BE_U16(pbBuf, 0x0); /* feature 0: list of profiles supported */
272 pbBuf[2] = (0 << 2) | (1 << 1) | (1 << 0); /* version 0, persistent, current */
273 pbBuf[3] = 8; /* additional bytes for profiles */
274 /* The MMC-3 spec says that DVD-ROM read capability should be reported
275 * before CD-ROM read capability. */
276 scsiH2BE_U16(pbBuf + 4, 0x10); /* profile: read-only DVD */
277 pbBuf[6] = (0 << 0); /* NOT current profile */
278 scsiH2BE_U16(pbBuf + 8, 0x08); /* profile: read only CD */
279 pbBuf[10] = (1 << 0); /* current profile */
280
281 return 3*4; /* Header + 2 profiles entries */
282}
283
284static size_t vscsiLunMmcGetConfigurationFillFeatureCore(uint8_t *pbBuf, size_t cbBuf)
285{
286 if (cbBuf < 12)
287 return 0;
288
289 scsiH2BE_U16(pbBuf, 0x1); /* feature 0001h: Core Feature */
290 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
291 pbBuf[3] = 8; /* Additional length */
292 scsiH2BE_U16(pbBuf + 4, 0x00000002); /* Physical interface ATAPI. */
293 pbBuf[8] = RT_BIT(0); /* DBE */
294 /* Rest is reserved. */
295
296 return 12;
297}
298
299static size_t vscsiLunMmcGetConfigurationFillFeatureMorphing(uint8_t *pbBuf, size_t cbBuf)
300{
301 if (cbBuf < 8)
302 return 0;
303
304 scsiH2BE_U16(pbBuf, 0x2); /* feature 0002h: Morphing Feature */
305 pbBuf[2] = (0x1 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
306 pbBuf[3] = 4; /* Additional length */
307 pbBuf[4] = RT_BIT(1) | 0x0; /* OCEvent | !ASYNC */
308 /* Rest is reserved. */
309
310 return 8;
311}
312
313static size_t vscsiLunMmcGetConfigurationFillFeatureRemovableMedium(uint8_t *pbBuf, size_t cbBuf)
314{
315 if (cbBuf < 8)
316 return 0;
317
318 scsiH2BE_U16(pbBuf, 0x3); /* feature 0003h: Removable Medium Feature */
319 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
320 pbBuf[3] = 4; /* Additional length */
321 /* Tray type loading | Load | Eject | !Pvnt Jmpr | !DBML | Lock */
322 pbBuf[4] = (0x2 << 5) | RT_BIT(4) | RT_BIT(3) | (0x0 << 2) | (0x0 << 1) | RT_BIT(0);
323 /* Rest is reserved. */
324
325 return 8;
326}
327
328static size_t vscsiLunMmcGetConfigurationFillFeatureRandomReadable(uint8_t *pbBuf, size_t cbBuf)
329{
330 if (cbBuf < 12)
331 return 0;
332
333 scsiH2BE_U16(pbBuf, 0x10); /* feature 0010h: Random Readable Feature */
334 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
335 pbBuf[3] = 8; /* Additional length */
336 scsiH2BE_U32(pbBuf + 4, 2048); /* Logical block size. */
337 scsiH2BE_U16(pbBuf + 8, 0x10); /* Blocking (0x10 for DVD, CD is not defined). */
338 pbBuf[10] = 0; /* PP not present */
339 /* Rest is reserved. */
340
341 return 12;
342}
343
344static size_t vscsiLunMmcGetConfigurationFillFeatureCDRead(uint8_t *pbBuf, size_t cbBuf)
345{
346 if (cbBuf < 8)
347 return 0;
348
349 scsiH2BE_U16(pbBuf, 0x1e); /* feature 001Eh: CD Read Feature */
350 pbBuf[2] = (0x2 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
351 pbBuf[3] = 0; /* Additional length */
352 pbBuf[4] = (0x0 << 7) | (0x0 << 1) | 0x0; /* !DAP | !C2-Flags | !CD-Text. */
353 /* Rest is reserved. */
354
355 return 8;
356}
357
358static size_t vscsiLunMmcGetConfigurationFillFeaturePowerManagement(uint8_t *pbBuf, size_t cbBuf)
359{
360 if (cbBuf < 4)
361 return 0;
362
363 scsiH2BE_U16(pbBuf, 0x100); /* feature 0100h: Power Management Feature */
364 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
365 pbBuf[3] = 0; /* Additional length */
366
367 return 4;
368}
369
370static size_t vscsiLunMmcGetConfigurationFillFeatureTimeout(uint8_t *pbBuf, size_t cbBuf)
371{
372 if (cbBuf < 8)
373 return 0;
374
375 scsiH2BE_U16(pbBuf, 0x105); /* feature 0105h: Timeout Feature */
376 pbBuf[2] = (0x0 << 2) | RT_BIT(1) | RT_BIT(0); /* Version | Persistent | Current */
377 pbBuf[3] = 4; /* Additional length */
378 pbBuf[4] = 0x0; /* !Group3 */
379
380 return 8;
381}
382
383/**
384 * Processes the GET CONFIGURATION SCSI request.
385 *
386 * @returns SCSI status code.
387 * @param pVScsiLunMmc The MMC LUN instance.
388 * @param pVScsiReq The VSCSI request.
389 * @param cbMaxTransfer The maximum transfer size.
390 */
391static int vscsiLunMmcGetConfiguration(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
392{
393 uint8_t aReply[80];
394 uint8_t *pbBuf = &aReply[0];
395 size_t cbBuf = sizeof(aReply);
396 size_t cbCopied = 0;
397
398 /* Accept valid request types only, and only starting feature 0. */
399 if ((pVScsiReq->pbCDB[1] & 0x03) == 3 || scsiBE2H_U16(&pVScsiReq->pbCDB[2]) != 0)
400 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
401 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
402
403 /** @todo implement switching between CD-ROM and DVD-ROM profile (the only
404 * way to differentiate them right now is based on the image size). */
405 if (pVScsiLunMmc->cSectors)
406 scsiH2BE_U16(pbBuf + 6, 0x08); /* current profile: read-only CD */
407 else
408 scsiH2BE_U16(pbBuf + 6, 0x00); /* current profile: none -> no media */
409 cbBuf -= 8;
410 pbBuf += 8;
411
412 cbCopied = vscsiLunMmcGetConfigurationFillFeatureListProfiles(pbBuf, cbBuf);
413 cbBuf -= cbCopied;
414 pbBuf += cbCopied;
415
416 cbCopied = vscsiLunMmcGetConfigurationFillFeatureCore(pbBuf, cbBuf);
417 cbBuf -= cbCopied;
418 pbBuf += cbCopied;
419
420 cbCopied = vscsiLunMmcGetConfigurationFillFeatureMorphing(pbBuf, cbBuf);
421 cbBuf -= cbCopied;
422 pbBuf += cbCopied;
423
424 cbCopied = vscsiLunMmcGetConfigurationFillFeatureRemovableMedium(pbBuf, cbBuf);
425 cbBuf -= cbCopied;
426 pbBuf += cbCopied;
427
428 cbCopied = vscsiLunMmcGetConfigurationFillFeatureRandomReadable(pbBuf, cbBuf);
429 cbBuf -= cbCopied;
430 pbBuf += cbCopied;
431
432 cbCopied = vscsiLunMmcGetConfigurationFillFeatureCDRead(pbBuf, cbBuf);
433 cbBuf -= cbCopied;
434 pbBuf += cbCopied;
435
436 cbCopied = vscsiLunMmcGetConfigurationFillFeaturePowerManagement(pbBuf, cbBuf);
437 cbBuf -= cbCopied;
438 pbBuf += cbCopied;
439
440 cbCopied = vscsiLunMmcGetConfigurationFillFeatureTimeout(pbBuf, cbBuf);
441 cbBuf -= cbCopied;
442 pbBuf += cbCopied;
443
444 /* Set data length now. */
445 scsiH2BE_U32(&aReply[0], (uint32_t)(sizeof(aReply) - cbBuf));
446
447 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply) - cbBuf));
448 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
449}
450
451/**
452 * Processes the READ DVD STRUCTURE SCSI request.
453 *
454 * @returns SCSI status code.
455 * @param pVScsiLunMmc The MMC LUN instance.
456 * @param pVScsiReq The VSCSI request.
457 * @param cbMaxTransfer The maximum transfer size.
458 */
459static int vscsiLunMmcReadDvdStructure(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
460{
461 uint8_t aReply[25]; /* Counted a maximum of 20 bytes but better be on the safe side. */
462
463 RT_ZERO(aReply);
464
465 /* Act according to the indicated format. */
466 switch (pVScsiReq->pbCDB[7])
467 {
468 case 0x00:
469 case 0x01:
470 case 0x02:
471 case 0x03:
472 case 0x04:
473 case 0x05:
474 case 0x06:
475 case 0x07:
476 case 0x08:
477 case 0x09:
478 case 0x0a:
479 case 0x0b:
480 case 0x0c:
481 case 0x0d:
482 case 0x0e:
483 case 0x0f:
484 case 0x10:
485 case 0x11:
486 case 0x30:
487 case 0x31:
488 case 0xff:
489 if (pVScsiReq->pbCDB[1] == 0)
490 {
491 int uASC = SCSI_ASC_NONE;
492
493 switch (pVScsiReq->pbCDB[7])
494 {
495 case 0x0: /* Physical format information */
496 {
497 uint8_t uLayer = pVScsiReq->pbCDB[6];
498 uint64_t cTotalSectors;
499
500 if (uLayer != 0)
501 {
502 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
503 break;
504 }
505
506 cTotalSectors = pVScsiLunMmc->cSectors;
507 cTotalSectors >>= 2;
508 if (cTotalSectors == 0)
509 {
510 uASC = -SCSI_ASC_MEDIUM_NOT_PRESENT;
511 break;
512 }
513
514 aReply[4] = 1; /* DVD-ROM, part version 1 */
515 aReply[5] = 0xf; /* 120mm disc, minimum rate unspecified */
516 aReply[6] = 1; /* one layer, read-only (per MMC-2 spec) */
517 aReply[7] = 0; /* default densities */
518
519 /* FIXME: 0x30000 per spec? */
520 scsiH2BE_U32(&aReply[8], 0); /* start sector */
521 scsiH2BE_U32(&aReply[12], cTotalSectors - 1); /* end sector */
522 scsiH2BE_U32(&aReply[16], cTotalSectors - 1); /* l0 end sector */
523
524 /* Size of buffer, not including 2 byte size field */
525 scsiH2BE_U32(&aReply[0], 2048 + 2);
526
527 /* 2k data + 4 byte header */
528 uASC = (2048 + 4);
529 break;
530 }
531 case 0x01: /* DVD copyright information */
532 aReply[4] = 0; /* no copyright data */
533 aReply[5] = 0; /* no region restrictions */
534
535 /* Size of buffer, not including 2 byte size field */
536 scsiH2BE_U16(&aReply[0], 4 + 2);
537
538 /* 4 byte header + 4 byte data */
539 uASC = (4 + 4);
540 break;
541
542 case 0x03: /* BCA information - invalid field for no BCA info */
543 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
544 break;
545
546 case 0x04: /* DVD disc manufacturing information */
547 /* Size of buffer, not including 2 byte size field */
548 scsiH2BE_U16(&aReply[0], 2048 + 2);
549
550 /* 2k data + 4 byte header */
551 uASC = (2048 + 4);
552 break;
553 case 0xff:
554 /*
555 * This lists all the command capabilities above. Add new ones
556 * in order and update the length and buffer return values.
557 */
558
559 aReply[4] = 0x00; /* Physical format */
560 aReply[5] = 0x40; /* Not writable, is readable */
561 scsiH2BE_U16(&aReply[6], 2048 + 4);
562
563 aReply[8] = 0x01; /* Copyright info */
564 aReply[9] = 0x40; /* Not writable, is readable */
565 scsiH2BE_U16(&aReply[10], 4 + 4);
566
567 aReply[12] = 0x03; /* BCA info */
568 aReply[13] = 0x40; /* Not writable, is readable */
569 scsiH2BE_U16(&aReply[14], 188 + 4);
570
571 aReply[16] = 0x04; /* Manufacturing info */
572 aReply[17] = 0x40; /* Not writable, is readable */
573 scsiH2BE_U16(&aReply[18], 2048 + 4);
574
575 /* Size of buffer, not including 2 byte size field */
576 scsiH2BE_U16(&aReply[0], 16 + 2);
577
578 /* data written + 4 byte header */
579 uASC = (16 + 4);
580 break;
581 default: /** @todo formats beyond DVD-ROM requires */
582 uASC = -SCSI_ASC_INV_FIELD_IN_CMD_PACKET;
583 }
584
585 if (uASC < 0)
586 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
587 -uASC, 0x00);
588 break;
589 }
590 /** @todo BD support, fall through for now */
591
592 /* Generic disk structures */
593 case 0x80: /** @todo AACS volume identifier */
594 case 0x81: /** @todo AACS media serial number */
595 case 0x82: /** @todo AACS media identifier */
596 case 0x83: /** @todo AACS media key block */
597 case 0x90: /** @todo List of recognized format layers */
598 case 0xc0: /** @todo Write protection status */
599 default:
600 return vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
601 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
602 }
603
604 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
605 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
606}
607
608/**
609 * Processes the MODE SENSE 10 SCSI request.
610 *
611 * @returns SCSI status code.
612 * @param pVScsiLunMmc The MMC LUN instance.
613 * @param pVScsiReq The VSCSI request.
614 * @param cbMaxTransfer The maximum transfer size.
615 */
616static int vscsiLunMmcModeSense10(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq, size_t cbMaxTransfer)
617{
618 int rcReq;
619 uint8_t uPageControl = pVScsiReq->pbCDB[2] >> 6;
620 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
621
622 switch (uPageControl)
623 {
624 case SCSI_PAGECONTROL_CURRENT:
625 switch (uPageCode)
626 {
627 case SCSI_MODEPAGE_ERROR_RECOVERY:
628 {
629 uint8_t aReply[16];
630
631 scsiH2BE_U16(&aReply[0], 16 + 6);
632 aReply[2] = (uint8_t)pVScsiLunMmc->u32MediaTrackType;
633 aReply[3] = 0;
634 aReply[4] = 0;
635 aReply[5] = 0;
636 aReply[6] = 0;
637 aReply[7] = 0;
638
639 aReply[8] = 0x01;
640 aReply[9] = 0x06;
641 aReply[10] = 0x00;
642 aReply[11] = 0x05;
643 aReply[12] = 0x00;
644 aReply[13] = 0x00;
645 aReply[14] = 0x00;
646 aReply[15] = 0x00;
647 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
648 rcReq = vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
649 break;
650 }
651 case SCSI_MODEPAGE_CD_STATUS:
652 {
653 uint8_t aReply[40];
654
655 scsiH2BE_U16(&aReply[0], 38);
656 aReply[2] = (uint8_t)pVScsiLunMmc->u32MediaTrackType;
657 aReply[3] = 0;
658 aReply[4] = 0;
659 aReply[5] = 0;
660 aReply[6] = 0;
661 aReply[7] = 0;
662
663 aReply[8] = 0x2a;
664 aReply[9] = 30; /* page length */
665 aReply[10] = 0x08; /* DVD-ROM read support */
666 aReply[11] = 0x00; /* no write support */
667 /* The following claims we support audio play. This is obviously false,
668 * but the Linux generic CDROM support makes many features depend on this
669 * capability. If it's not set, this causes many things to be disabled. */
670 aReply[12] = 0x71; /* multisession support, mode 2 form 1/2 support, audio play */
671 aReply[13] = 0x00; /* no subchannel reads supported */
672 aReply[14] = (1 << 0) | (1 << 3) | (1 << 5); /* lock supported, eject supported, tray type loading mechanism */
673 if (pVScsiLunMmc->fLocked)
674 aReply[14] |= 1 << 1; /* report lock state */
675 aReply[15] = 0; /* no subchannel reads supported, no separate audio volume control, no changer etc. */
676 scsiH2BE_U16(&aReply[16], 5632); /* (obsolete) claim 32x speed support */
677 scsiH2BE_U16(&aReply[18], 2); /* number of audio volume levels */
678 scsiH2BE_U16(&aReply[20], 128); /* buffer size supported in Kbyte - We don't have a buffer because we write directly into guest memory.
679 Just write some dummy value. */
680 scsiH2BE_U16(&aReply[22], 5632); /* (obsolete) current read speed 32x */
681 aReply[24] = 0; /* reserved */
682 aReply[25] = 0; /* reserved for digital audio (see idx 15) */
683 scsiH2BE_U16(&aReply[26], 0); /* (obsolete) maximum write speed */
684 scsiH2BE_U16(&aReply[28], 0); /* (obsolete) current write speed */
685 scsiH2BE_U16(&aReply[30], 0); /* copy management revision supported 0=no CSS */
686 aReply[32] = 0; /* reserved */
687 aReply[33] = 0; /* reserved */
688 aReply[34] = 0; /* reserved */
689 aReply[35] = 1; /* rotation control CAV */
690 scsiH2BE_U16(&aReply[36], 0); /* current write speed */
691 scsiH2BE_U16(&aReply[38], 0); /* number of write speed performance descriptors */
692 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
693 rcReq = vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
694 break;
695 }
696 default:
697 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
698 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
699 break;
700 }
701 break;
702 case SCSI_PAGECONTROL_CHANGEABLE:
703 case SCSI_PAGECONTROL_DEFAULT:
704 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
705 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
706 break;
707 default:
708 case SCSI_PAGECONTROL_SAVED:
709 rcReq = vscsiLunReqSenseErrorSet(&pVScsiLunMmc->Core, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
710 SCSI_ASC_SAVING_PARAMETERS_NOT_SUPPORTED, 0x00);
711 break;
712 }
713
714 return rcReq;
715}
716
717/**
718 * Processes the GET EVENT STATUS NOTIFICATION SCSI request.
719 *
720 * @returns SCSI status code.
721 * @param pVScsiLunMmc The MMC LUN instance.
722 * @param pVScsiReq The VSCSI request.
723 * @param cbMaxTransfer The maximum transfer size.
724 */
725static int vscsiLunMmcGetEventStatusNotification(PVSCSILUNMMC pVScsiLunMmc, PVSCSIREQINT pVScsiReq,
726 size_t cbMaxTransfer)
727{
728 uint32_t OldStatus;
729 uint32_t NewStatus;
730 uint8_t aReply[8];
731 RT_ZERO(aReply);
732
733 LogFlowFunc(("pVScsiLunMmc=%#p pVScsiReq=%#p cbMaxTransfer=%zu\n",
734 pVScsiLunMmc, pVScsiReq, cbMaxTransfer));
735
736 do
737 {
738 OldStatus = ASMAtomicReadU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus);
739 NewStatus = MMCEVENTSTATUSTYPE_UNCHANGED;
740
741 switch (OldStatus)
742 {
743 case MMCEVENTSTATUSTYPE_MEDIA_NEW:
744 /* mount */
745 scsiH2BE_U16(&aReply[0], 6);
746 aReply[2] = 0x04; /* media */
747 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
748 aReply[4] = 0x02; /* new medium */
749 aReply[5] = 0x02; /* medium present / door closed */
750 aReply[6] = 0x00;
751 aReply[7] = 0x00;
752 pVScsiLunMmc->Core.fReady = true;
753 break;
754
755 case MMCEVENTSTATUSTYPE_MEDIA_CHANGED:
756 case MMCEVENTSTATUSTYPE_MEDIA_REMOVED:
757 /* umount */
758 scsiH2BE_U16(&aReply[0], 6);
759 aReply[2] = 0x04; /* media */
760 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
761 aReply[4] = 0x03; /* media removal */
762 aReply[5] = 0x00; /* medium absent / door closed */
763 aReply[6] = 0x00;
764 aReply[7] = 0x00;
765 if (OldStatus == MMCEVENTSTATUSTYPE_MEDIA_CHANGED)
766 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_NEW;
767 break;
768
769 case MMCEVENTSTATUSTYPE_MEDIA_EJECT_REQUESTED: /* currently unused */
770 scsiH2BE_U16(&aReply[0], 6);
771 aReply[2] = 0x04; /* media */
772 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
773 aReply[4] = 0x01; /* eject requested (eject button pressed) */
774 aReply[5] = 0x02; /* medium present / door closed */
775 aReply[6] = 0x00;
776 aReply[7] = 0x00;
777 break;
778
779 case MMCEVENTSTATUSTYPE_UNCHANGED:
780 default:
781 scsiH2BE_U16(&aReply[0], 6);
782 aReply[2] = 0x01; /* operational change request / notification */
783 aReply[3] = 0x5e; /* supported = busy|media|external|power|operational */
784 aReply[4] = 0x00;
785 aReply[5] = 0x00;
786 aReply[6] = 0x00;
787 aReply[7] = 0x00;
788 break;
789 }
790
791 LogFlowFunc(("OldStatus=%u NewStatus=%u\n", OldStatus, NewStatus));
792
793 } while (!ASMAtomicCmpXchgU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, NewStatus, OldStatus));
794
795 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(cbMaxTransfer, sizeof(aReply)));
796 return vscsiLunReqSenseOkSet(&pVScsiLunMmc->Core, pVScsiReq);
797}
798
799static DECLCALLBACK(int) vscsiLunMmcInit(PVSCSILUNINT pVScsiLun)
800{
801 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
802 uint64_t cbDisk = 0;
803 int rc = VINF_SUCCESS;
804
805 ASMAtomicWriteU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, MMCEVENTSTATUSTYPE_UNCHANGED);
806 pVScsiLunMmc->u32MediaTrackType = MMC_MEDIA_TYPE_UNKNOWN;
807 pVScsiLunMmc->cbSector = 2048; /* Default to 2K sectors. */
808 rc = vscsiLunMediumGetSize(pVScsiLun, &cbDisk);
809 if (RT_SUCCESS(rc))
810 pVScsiLunMmc->cSectors = cbDisk / pVScsiLunMmc->cbSector;
811
812 return rc;
813}
814
815static DECLCALLBACK(int) vscsiLunMmcDestroy(PVSCSILUNINT pVScsiLun)
816{
817 RT_NOREF1(pVScsiLun);
818 return VINF_SUCCESS;
819}
820
821static DECLCALLBACK(int) vscsiLunMmcReqProcess(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq)
822{
823 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
824 VSCSIIOREQTXDIR enmTxDir = VSCSIIOREQTXDIR_INVALID;
825 uint64_t uLbaStart = 0;
826 uint32_t cSectorTransfer = 0;
827 int rc = VINF_SUCCESS;
828 int rcReq = SCSI_STATUS_OK;
829 unsigned uCmd = pVScsiReq->pbCDB[0];
830
831 LogFlowFunc(("pVScsiLun=%#p{.fReady=%RTbool, .fMediaPresent=%RTbool} pVScsiReq=%#p{.pbCdb[0]=%#x}\n",
832 pVScsiLun, pVScsiLun->fReady, pVScsiLun->fMediaPresent, pVScsiReq, uCmd));
833
834 /*
835 * GET CONFIGURATION, GET EVENT/STATUS NOTIFICATION, INQUIRY, and REQUEST SENSE commands
836 * operate even when a unit attention condition exists for initiator; every other command
837 * needs to report CHECK CONDITION in that case.
838 */
839 if ( !pVScsiLunMmc->Core.fReady
840 && uCmd != SCSI_INQUIRY
841 && uCmd != SCSI_GET_CONFIGURATION
842 && uCmd != SCSI_GET_EVENT_STATUS_NOTIFICATION)
843 {
844 /*
845 * A note on media changes: As long as a medium is not present, the unit remains in
846 * the 'not ready' state. Technically the unit becomes 'ready' soon after a medium
847 * is inserted; however, we internally keep the 'not ready' state until we've had
848 * a chance to report the UNIT ATTENTION status indicating a media change.
849 */
850 if (pVScsiLunMmc->Core.fMediaPresent)
851 {
852 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_UNIT_ATTENTION,
853 SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED, 0x00);
854 pVScsiLunMmc->Core.fReady = true;
855 }
856 else
857 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY,
858 SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
859 }
860 else
861 {
862 switch (uCmd)
863 {
864 case SCSI_TEST_UNIT_READY:
865 Assert(!pVScsiLunMmc->Core.fReady); /* Only should get here if LUN isn't ready. */
866 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_NOT_READY, SCSI_ASC_MEDIUM_NOT_PRESENT, 0x00);
867 break;
868
869 case SCSI_INQUIRY:
870 {
871 SCSIINQUIRYDATA ScsiInquiryReply;
872
873 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[3]));
874 memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
875
876 ScsiInquiryReply.cbAdditional = 31;
877 ScsiInquiryReply.fRMB = 1; /* Removable. */
878 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_CD_DVD;
879 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
880 ScsiInquiryReply.u3AnsiVersion = 0x05; /* MMC-?? compliant */
881 ScsiInquiryReply.fCmdQue = 1; /* Command queuing supported. */
882 ScsiInquiryReply.fWBus16 = 1;
883 scsiPadStrS(ScsiInquiryReply.achVendorId, "VBOX", 8);
884 scsiPadStrS(ScsiInquiryReply.achProductId, "CD-ROM", 16);
885 scsiPadStrS(ScsiInquiryReply.achProductLevel, "1.0", 4);
886
887 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, (uint8_t *)&ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
888 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
889 break;
890 }
891 case SCSI_READ_CAPACITY:
892 {
893 uint8_t aReply[8];
894 memset(aReply, 0, sizeof(aReply));
895 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
896
897 /*
898 * If sector size exceeds the maximum value that is
899 * able to be stored in 4 bytes return 0xffffffff in this field
900 */
901 if (pVScsiLunMmc->cSectors > UINT32_C(0xffffffff))
902 scsiH2BE_U32(aReply, UINT32_C(0xffffffff));
903 else
904 scsiH2BE_U32(aReply, pVScsiLunMmc->cSectors - 1);
905 scsiH2BE_U32(&aReply[4], pVScsiLunMmc->cbSector);
906 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
907 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
908 break;
909 }
910 case SCSI_MODE_SENSE_6:
911 {
912 uint8_t uModePage = pVScsiReq->pbCDB[2] & 0x3f;
913 uint8_t aReply[24];
914 uint8_t *pu8ReplyPos;
915 bool fValid = false;
916
917 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
918 memset(aReply, 0, sizeof(aReply));
919 aReply[0] = 4; /* Reply length 4. */
920 aReply[1] = 0; /* Default media type. */
921 aReply[2] = RT_BIT(4); /* Caching supported. */
922 aReply[3] = 0; /* Block descriptor length. */
923
924 pu8ReplyPos = aReply + 4;
925
926 if ((uModePage == 0x08) || (uModePage == 0x3f))
927 {
928 memset(pu8ReplyPos, 0, 20);
929 *pu8ReplyPos++ = 0x08; /* Page code. */
930 *pu8ReplyPos++ = 0x12; /* Size of the page. */
931 *pu8ReplyPos++ = 0x4; /* Write cache enabled. */
932 fValid = true;
933 } else if (uModePage == 0) {
934 fValid = true;
935 }
936
937 /* Querying unknown pages must fail. */
938 if (fValid) {
939 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
940 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
941 } else {
942 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
943 }
944 break;
945 }
946 case SCSI_MODE_SENSE_10:
947 {
948 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
949 vscsiReqSetXferSize(pVScsiReq, cbMax);
950 rcReq = vscsiLunMmcModeSense10(pVScsiLunMmc, pVScsiReq, cbMax);
951 break;
952 }
953 case SCSI_SEEK_10:
954 {
955 uint32_t uLba = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
956 if (uLba > pVScsiLunMmc->cSectors)
957 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
958 SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
959 else
960 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
961 break;
962 }
963 case SCSI_MODE_SELECT_6:
964 {
965 /** @todo implement!! */
966 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
967 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
968 break;
969 }
970 case SCSI_READ_6:
971 {
972 enmTxDir = VSCSIIOREQTXDIR_READ;
973 uLbaStart = ((uint64_t) pVScsiReq->pbCDB[3]
974 | (pVScsiReq->pbCDB[2] << 8)
975 | ((pVScsiReq->pbCDB[1] & 0x1f) << 16));
976 cSectorTransfer = pVScsiReq->pbCDB[4];
977 break;
978 }
979 case SCSI_READ_10:
980 {
981 enmTxDir = VSCSIIOREQTXDIR_READ;
982 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
983 cSectorTransfer = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
984 break;
985 }
986 case SCSI_READ_12:
987 {
988 enmTxDir = VSCSIIOREQTXDIR_READ;
989 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
990 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[6]);
991 break;
992 }
993 case SCSI_READ_16:
994 {
995 enmTxDir = VSCSIIOREQTXDIR_READ;
996 uLbaStart = scsiBE2H_U64(&pVScsiReq->pbCDB[2]);
997 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[10]);
998 break;
999 }
1000 case SCSI_READ_BUFFER:
1001 {
1002 uint8_t uDataMode = pVScsiReq->pbCDB[1] & 0x1f;
1003
1004 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[6]));
1005
1006 switch (uDataMode)
1007 {
1008 case 0x00:
1009 case 0x01:
1010 case 0x02:
1011 case 0x03:
1012 case 0x0a:
1013 break;
1014 case 0x0b:
1015 {
1016 uint8_t aReply[4];
1017 RT_ZERO(aReply);
1018
1019 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1020 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1021 break;
1022 }
1023 case 0x1a:
1024 case 0x1c:
1025 break;
1026 default:
1027 AssertMsgFailed(("Invalid data mode\n"));
1028 }
1029 break;
1030 }
1031 case SCSI_VERIFY_10:
1032 case SCSI_START_STOP_UNIT:
1033 {
1034 int rc2 = VINF_SUCCESS;
1035 switch (pVScsiReq->pbCDB[4] & 3)
1036 {
1037 case 0: /* 00 - Stop motor */
1038 case 1: /* 01 - Start motor */
1039 break;
1040 case 2: /* 10 - Eject media */
1041 rc2 = vscsiLunMediumEject(pVScsiLun);
1042 break;
1043 case 3: /* 11 - Load media */
1044 /** @todo */
1045 break;
1046 }
1047 if (RT_SUCCESS(rc2))
1048 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1049 else
1050 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_MEDIA_LOAD_OR_EJECT_FAILED, 0x02);
1051 break;
1052 }
1053 case SCSI_LOG_SENSE:
1054 {
1055 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
1056 uint8_t uSubPageCode = pVScsiReq->pbCDB[3];
1057
1058 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[7]));
1059
1060 switch (uPageCode)
1061 {
1062 case 0x00:
1063 {
1064 if (uSubPageCode == 0)
1065 {
1066 uint8_t aReply[4];
1067
1068 aReply[0] = 0;
1069 aReply[1] = 0;
1070 aReply[2] = 0;
1071 aReply[3] = 0;
1072
1073 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1074 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1075 break;
1076 }
1077 }
1078 default:
1079 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1080 }
1081 break;
1082 }
1083 case SCSI_SERVICE_ACTION_IN_16:
1084 {
1085 switch (pVScsiReq->pbCDB[1] & 0x1f)
1086 {
1087 case SCSI_SVC_ACTION_IN_READ_CAPACITY_16:
1088 {
1089 uint8_t aReply[32];
1090
1091 memset(aReply, 0, sizeof(aReply));
1092 scsiH2BE_U64(aReply, pVScsiLunMmc->cSectors - 1);
1093 scsiH2BE_U32(&aReply[8], pVScsiLunMmc->cbSector);
1094 /* Leave the rest 0 */
1095
1096 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
1097 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
1098 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1099 break;
1100 }
1101 default:
1102 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1103 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00); /* Don't know if this is correct */
1104 }
1105 break;
1106 }
1107 case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
1108 {
1109 pVScsiLunMmc->fLocked = RT_BOOL(pVScsiReq->pbCDB[4] & 0x01);
1110 vscsiLunMediumSetLock(pVScsiLun, pVScsiLunMmc->fLocked);
1111 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1112 break;
1113 }
1114 case SCSI_READ_TOC_PMA_ATIP:
1115 {
1116 uint8_t format;
1117 uint16_t cbMax;
1118 bool fMSF;
1119
1120 format = pVScsiReq->pbCDB[2] & 0x0f;
1121 cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1122 fMSF = (pVScsiReq->pbCDB[1] >> 1) & 1;
1123
1124 vscsiReqSetXferSize(pVScsiReq, cbMax);
1125 switch (format)
1126 {
1127 case 0x00:
1128 rcReq = mmcReadTOCNormal(pVScsiLun, pVScsiReq, cbMax, fMSF);
1129 break;
1130 case 0x01:
1131 rcReq = mmcReadTOCMulti(pVScsiLun, pVScsiReq, cbMax, fMSF);
1132 break;
1133 case 0x02:
1134 rcReq = mmcReadTOCRaw(pVScsiLun, pVScsiReq, cbMax, fMSF);
1135 break;
1136 default:
1137 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1138 }
1139 break;
1140 }
1141 case SCSI_GET_EVENT_STATUS_NOTIFICATION:
1142 {
1143 /* Only supporting polled mode at the moment. */
1144 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1145
1146 vscsiReqSetXferSize(pVScsiReq, cbMax);
1147 if (pVScsiReq->pbCDB[1] & 0x1)
1148 rcReq = vscsiLunMmcGetEventStatusNotification(pVScsiLunMmc, pVScsiReq, cbMax);
1149 else
1150 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1151 break;
1152 }
1153 case SCSI_MECHANISM_STATUS:
1154 {
1155 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1156 uint8_t aReply[8];
1157
1158 vscsiReqSetXferSize(pVScsiReq, cbMax);
1159 scsiH2BE_U16(&aReply[0], 0);
1160 /* no current LBA */
1161 aReply[2] = 0;
1162 aReply[3] = 0;
1163 aReply[4] = 0;
1164 aReply[5] = 1;
1165 scsiH2BE_U16(&aReply[6], 0);
1166 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1167 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1168 break;
1169 }
1170 case SCSI_READ_DISC_INFORMATION:
1171 {
1172 uint8_t aReply[34];
1173 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1174
1175 vscsiReqSetXferSize(pVScsiReq, cbMax);
1176 memset(aReply, '\0', sizeof(aReply));
1177 scsiH2BE_U16(&aReply[0], 32);
1178 aReply[2] = (0 << 4) | (3 << 2) | (2 << 0); /* not erasable, complete session, complete disc */
1179 aReply[3] = 1; /* number of first track */
1180 aReply[4] = 1; /* number of sessions (LSB) */
1181 aReply[5] = 1; /* first track number in last session (LSB) */
1182 aReply[6] = 1; /* last track number in last session (LSB) */
1183 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 */
1184 aReply[8] = 0; /* disc type = CD-ROM */
1185 aReply[9] = 0; /* number of sessions (MSB) */
1186 aReply[10] = 0; /* number of sessions (MSB) */
1187 aReply[11] = 0; /* number of sessions (MSB) */
1188 scsiH2BE_U32(&aReply[16], 0x00ffffff); /* last session lead-in start time is not available */
1189 scsiH2BE_U32(&aReply[20], 0x00ffffff); /* last possible start time for lead-out is not available */
1190 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1191 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1192 break;
1193 }
1194 case SCSI_READ_TRACK_INFORMATION:
1195 {
1196 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1197
1198 vscsiReqSetXferSize(pVScsiReq, cbMax);
1199 /* Accept address/number type of 1 only, and only track 1 exists. */
1200 if ((pVScsiReq->pbCDB[1] & 0x03) != 1 || scsiBE2H_U32(&pVScsiReq->pbCDB[2]) != 1)
1201 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
1202 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
1203 else
1204 {
1205 uint8_t aReply[36];
1206 RT_ZERO(aReply);
1207
1208 scsiH2BE_U16(&aReply[0], 34);
1209 aReply[2] = 1; /* track number (LSB) */
1210 aReply[3] = 1; /* session number (LSB) */
1211 aReply[5] = (0 << 5) | (0 << 4) | (4 << 0); /* not damaged, primary copy, data track */
1212 aReply[6] = (0 << 7) | (0 << 6) | (0 << 5) | (0 << 6) | (1 << 0); /* not reserved track, not blank, not packet writing, not fixed packet, data mode 1 */
1213 aReply[7] = (0 << 1) | (0 << 0); /* last recorded address not valid, next recordable address not valid */
1214 scsiH2BE_U32(&aReply[8], 0); /* track start address is 0 */
1215 scsiH2BE_U32(&aReply[24], pVScsiLunMmc->cSectors); /* track size */
1216 aReply[32] = 0; /* track number (MSB) */
1217 aReply[33] = 0; /* session number (MSB) */
1218
1219 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, RT_MIN(sizeof(aReply), cbMax));
1220 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1221 }
1222 break;
1223 }
1224 case SCSI_GET_CONFIGURATION:
1225 {
1226 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
1227 vscsiReqSetXferSize(pVScsiReq, cbMax);
1228 rcReq = vscsiLunMmcGetConfiguration(pVScsiLunMmc, pVScsiReq, cbMax);
1229 break;
1230 }
1231 case SCSI_READ_DVD_STRUCTURE:
1232 {
1233 size_t cbMax = scsiBE2H_U16(&pVScsiReq->pbCDB[8]);
1234 vscsiReqSetXferSize(pVScsiReq, cbMax);
1235 rcReq = vscsiLunMmcReadDvdStructure(pVScsiLunMmc, pVScsiReq, cbMax);
1236 break;
1237 }
1238 default:
1239 //AssertMsgFailed(("Command %#x [%s] not implemented\n", pVScsiReq->pbCDB[0], SCSICmdText(pVScsiReq->pbCDB[0])));
1240 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE, 0x00);
1241 }
1242 }
1243
1244 if (enmTxDir != VSCSIIOREQTXDIR_INVALID)
1245 {
1246 LogFlow(("%s: uLbaStart=%llu cSectorTransfer=%u\n",
1247 __FUNCTION__, uLbaStart, cSectorTransfer));
1248
1249 vscsiReqSetXferSize(pVScsiReq, cSectorTransfer * pVScsiLunMmc->cbSector);
1250 if (RT_UNLIKELY(uLbaStart + cSectorTransfer > pVScsiLunMmc->cSectors))
1251 {
1252 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
1253 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1254 }
1255 else if (!cSectorTransfer)
1256 {
1257 /* A 0 transfer length is not an error. */
1258 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
1259 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1260 }
1261 else
1262 {
1263 /* Enqueue new I/O request */
1264 rc = vscsiIoReqTransferEnqueue(pVScsiLun, pVScsiReq, enmTxDir,
1265 uLbaStart * pVScsiLunMmc->cbSector,
1266 cSectorTransfer * pVScsiLunMmc->cbSector);
1267 }
1268 }
1269 else /* Request completed */
1270 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
1271
1272 return rc;
1273}
1274
1275/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumInserted} */
1276static DECLCALLBACK(int) vscsiLunMmcMediumInserted(PVSCSILUNINT pVScsiLun)
1277{
1278 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1279 uint64_t cbDisk = 0;
1280 int rc = vscsiLunMediumGetSize(pVScsiLun, &cbDisk);
1281 if (RT_SUCCESS(rc))
1282 {
1283 pVScsiLunMmc->cSectors = cbDisk / pVScsiLunMmc->cbSector;
1284
1285 uint32_t OldStatus, NewStatus;
1286 do
1287 {
1288 OldStatus = ASMAtomicReadU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus);
1289 switch (OldStatus)
1290 {
1291 case MMCEVENTSTATUSTYPE_MEDIA_CHANGED:
1292 case MMCEVENTSTATUSTYPE_MEDIA_REMOVED:
1293 /* no change, we will send "medium removed" + "medium inserted" */
1294 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_CHANGED;
1295 break;
1296 default:
1297 NewStatus = MMCEVENTSTATUSTYPE_MEDIA_NEW;
1298 break;
1299 }
1300 } while (!ASMAtomicCmpXchgU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus,
1301 NewStatus, OldStatus));
1302
1303 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_UNKNOWN);
1304 }
1305
1306 return rc;
1307}
1308
1309/** @interface_method_impl{VSCSILUNDESC,pfnVScsiLunMediumRemoved} */
1310static DECLCALLBACK(int) vscsiLunMmcMediumRemoved(PVSCSILUNINT pVScsiLun)
1311{
1312 PVSCSILUNMMC pVScsiLunMmc = (PVSCSILUNMMC)pVScsiLun;
1313
1314 ASMAtomicWriteU32((volatile uint32_t *)&pVScsiLunMmc->MediaEventStatus, MMCEVENTSTATUSTYPE_MEDIA_REMOVED);
1315 ASMAtomicXchgU32(&pVScsiLunMmc->u32MediaTrackType, MMC_MEDIA_TYPE_NO_DISC);
1316 return VINF_SUCCESS;
1317}
1318
1319
1320VSCSILUNDESC g_VScsiLunTypeMmc =
1321{
1322 /** enmLunType */
1323 VSCSILUNTYPE_MMC,
1324 /** pcszDescName */
1325 "MMC",
1326 /** cbLun */
1327 sizeof(VSCSILUNMMC),
1328 /** cSupOpcInfo */
1329 0,
1330 /** paSupOpcInfo */
1331 NULL,
1332 /** pfnVScsiLunInit */
1333 vscsiLunMmcInit,
1334 /** pfnVScsiLunDestroy */
1335 vscsiLunMmcDestroy,
1336 /** pfnVScsiLunReqProcess */
1337 vscsiLunMmcReqProcess,
1338 /** pfnVScsiLunMediumInserted */
1339 vscsiLunMmcMediumInserted,
1340 /** pfnVScsiLunMediumRemoved */
1341 vscsiLunMmcMediumRemoved
1342};
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