From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1K1RdN-0004Tc-0r for qemu-devel@nongnu.org; Wed, 28 May 2008 15:48:29 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1K1RdM-0004TB-2f for qemu-devel@nongnu.org; Wed, 28 May 2008 15:48:28 -0400 Received: from [199.232.76.173] (port=45873 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1K1RdL-0004T3-OT for qemu-devel@nongnu.org; Wed, 28 May 2008 15:48:27 -0400 Received: from g4t0016.houston.hp.com ([15.201.24.19]:20639) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1K1RdL-0002XG-5U for qemu-devel@nongnu.org; Wed, 28 May 2008 15:48:27 -0400 Subject: Re: [Qemu-devel] [PATCH] ide: fix ATAPI read drive structure command From: Alex Williamson In-Reply-To: References: <1211865917.8201.56.camel@bling> Content-Type: text/plain Date: Wed, 28 May 2008 13:48:04 -0600 Message-Id: <1212004084.6821.30.camel@lappy> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alexander Graf Cc: qemu-devel@nongnu.org Hi Alex, Thanks for the comments. On Tue, 2008-05-27 at 09:46 +0200, Alexander Graf wrote: > It might be a good idea to go through all the commands and see if they > really return dynamic lengths if they have to. Apparently this is not > the first one failing to do so. Agreed, but this patch is only focused on this one command for now. > Several parts of the spec are still unimplemented: > > - Different formats. These should at least be defined as a TODO in the > switch clause Done. I added a few more formats listed as required for DVD-ROM support, and re-architected the code to make it easier to fill in others later, with appropriate TODOs. > - Proper allocation length interpretation. I will comment on this below How does this work when the data response also includes a data length? Should I adjust that, or risk a bug in the guest OS indexing off of it's buffer? In the patch below I chose to fix the response data length, it needs some minor changes if that isn't correct. I'm also interpreting the request max_len as including the 4 byte header, but the response length as being only the data part. Please double check, this is the first time I've looked at the MMC spec. > - Check if the media is a CD and fail if true. It might be a good idea > to reuse the detection from the GET_CONFIGURATION command and put it > into a function, so it can be reused in several places. Done. It seems too trivial for a function, so I just made a macro. New version below, I've really only tested media 0/format 0, but I think the rest is correct. Please let me know if you have further comments. Thanks, Alex Fix ATAPI read drive structure command Make use of the allocation length field in the command and only return the number of bytes requested. Fix location of format byte in command. Add comments for more fields as we fill them in. Restructure code and add appropriate TODOs for yet to be implemented functionality. Signed-off-by: Alex Williamson -- diff -r 1f1541286539 trunk/hw/ide.c --- a/trunk/hw/ide.c Sun May 25 15:01:05 2008 -0600 +++ b/trunk/hw/ide.c Wed May 28 13:39:19 2008 -0600 @@ -351,6 +351,7 @@ #define ASC_ILLEGAL_OPCODE 0x20 #define ASC_LOGICAL_BLOCK_OOR 0x21 #define ASC_INV_FIELD_IN_CMD_PACKET 0x24 +#define ASC_INCOMPATIBLE_FORMAT 0x30 #define ASC_MEDIUM_NOT_PRESENT 0x3a #define ASC_SAVING_PARAMETERS_NOT_SUPPORTED 0x39 @@ -433,6 +434,11 @@ typedef struct IDEState { uint8_t *mdata_storage; int media_changed; } IDEState; + +/* XXX: DVDs that could fit on a CD will be reported as a CD */ +#define MEDIA_PRESENT(s) (s->nb_sectors > 0) +#define MEDIA_IS_DVD(s) (MEDIA_PRESENT(s) && (s)->nb_sectors > CD_MAX_SECTORS) +#define MEDIA_IS_CD(s) (MEDIA_PRESENT(s) && (s)->nb_sectors <= CD_MAX_SECTORS) #define BM_STATUS_DMAING 0x01 #define BM_STATUS_ERROR 0x02 @@ -1364,6 +1370,79 @@ static inline uint8_t ide_atapi_set_prof return 4; } +static void ide_dvd_read_structure(IDEState *s) +{ + const uint8_t *packet = s->io_buffer; + uint8_t *buf =s->io_buffer; + int format = packet[7]; + int max_len = ube16_to_cpu(packet + 8); + + switch (format) { + case 0x0: /* Physical format information */ + { + int layer = packet[6]; + uint64_t total_sectors; + + if (layer != 0) { + ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, + ASC_INV_FIELD_IN_CMD_PACKET); + break; + } + + bdrv_get_geometry(s->bs, &total_sectors); + total_sectors >>= 2; + if (total_sectors == 0) { + ide_atapi_cmd_error(s, SENSE_NOT_READY, + ASC_MEDIUM_NOT_PRESENT); + break; + } + + if (max_len > 2048 + 4) + max_len = 2048 + 4; + + memset(buf, 0, max_len); + buf[4] = 1; // DVD-ROM, part version 1 + buf[5] = 0xf; // 120mm disc, maximum rate unspecified + buf[6] = 0; // one layer, embossed data + buf[7] = 0; // default densities + + /* FIXME: 0x30000 per spec? */ + cpu_to_ube32(buf + 8, 0); // start sector + cpu_to_ube32(buf + 12, total_sectors - 1); // end sector + cpu_to_ube32(buf + 16, total_sectors - 1); // l0 end sector + + /* Size of buffer, minus 4 byte header */ + cpu_to_be16wu((uint16_t *)buf, max_len > 4 ? max_len - 4 : 0); + + ide_atapi_cmd_reply(s, 2048 + 4, max_len); + break; + } + case 0x01: /* DVD copyright information */ + if (max_len > 4 + 4) + max_len = 4 + 4; + + memset(buf, 0, max_len); + /* no copyright/region info */ + cpu_to_be16wu((uint16_t *)buf, max_len > 4 ? max_len - 4 : 0); + ide_atapi_cmd_reply(s, 8, max_len); + break; + case 0x04: /* DVD disc manufacturing information */ + if (max_len > 2048 + 4) + max_len = 2048 + 4; + + memset(buf, 0, max_len); + cpu_to_be16wu((uint16_t *)buf, 0); + ide_atapi_cmd_reply(s, 4, max_len); + break; + case 0x03: /* BCA information - invalid field for no BCA info */ + /* TODO: formats beyond DVD-ROM requires */ + default: + ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, + ASC_INV_FIELD_IN_CMD_PACKET); + break; + } +} + static void ide_atapi_cmd(IDEState *s) { const uint8_t *packet; @@ -1651,42 +1730,41 @@ static void ide_atapi_cmd(IDEState *s) case GPCMD_READ_DVD_STRUCTURE: { int media = packet[1]; - int layer = packet[6]; - int format = packet[2]; - uint64_t total_sectors; + int format = packet[7]; - if (media != 0 || layer != 0) - { + /* Disc structure capability list */ + if (format == 0xff) { + /* TODO: media independent, so separate from below */ ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET); + break; + } + + if (!MEDIA_IS_DVD(s)) { + if (format < 0xc0) + ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, + ASC_INCOMPATIBLE_FORMAT); + else + ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, + ASC_INV_FIELD_IN_CMD_PACKET); + break; } switch (format) { - case 0: - bdrv_get_geometry(s->bs, &total_sectors); - total_sectors >>= 2; - if (total_sectors == 0) { - ide_atapi_cmd_error(s, SENSE_NOT_READY, - ASC_MEDIUM_NOT_PRESENT); + case 0x00 ... 0x7f: + if (media == 0) { + ide_dvd_read_structure(s); break; } + /* TODO: BD support, fall through for now */ - memset(buf, 0, 2052); - - buf[4] = 1; // DVD-ROM, part version 1 - buf[5] = 0xf; // 120mm disc, maximum rate unspecified - buf[6] = 0; // one layer, embossed data - buf[7] = 0; - - cpu_to_ube32(buf + 8, 0); - cpu_to_ube32(buf + 12, total_sectors - 1); - cpu_to_ube32(buf + 16, total_sectors - 1); - - cpu_to_be16wu((uint16_t *)buf, 2048 + 4); - - ide_atapi_cmd_reply(s, 2048 + 3, 2048 + 4); - break; - + /* Generic disk structures */ + case 0x80: /* TODO: AACS volume identifier */ + case 0x81: /* TODO: AACS media serial number */ + case 0x82: /* TODO: AACS media identifier */ + case 0x83: /* TODO: AACS media key block */ + case 0x90: /* TODO: List of recognized format layers */ + case 0xc0: /* TODO: Write protection status */ default: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET); @@ -1739,16 +1817,12 @@ static void ide_atapi_cmd(IDEState *s) /* * the number of sectors from the media tells us which profile * to use as current. 0 means there is no media - * - * XXX: fails to detect correctly DVDs with less data burned - * than what a CD can hold */ - if ((s -> nb_sectors)) { - if ((s -> nb_sectors > CD_MAX_SECTORS)) - cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM); - else - cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM); - } + if (MEDIA_IS_DVD(s)) + cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM); + else if (MEDIA_IS_CD(s)) + cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM); + len = 8; /* header completed */ if (max_len > len) {