TOC support for raw cdrom block devices
PATCH 1/1 TOC support for raw cdrom block devices
The current raw block drvice code fakes a TOC with one track covering the entire disc. With this patch it attempts to read the TOC from a cdrom device and make that information available to the ide driver when the guest issues an ATAPI READ TOC/PMA/ATIP type 0 (read TOC) command.
There is only support for reading the TOC on the BSDs, MacOS X, and Linux, Win32 and other OSes will still fake one large data track. All tracks are reported as data, no attempt was made to support audio tracks. The ATAPI READ TOC/PMA/ATIP type 2 (read full TOC) command is unchanged, I did not have a way of testing it.
The purpose of this patch, in case anyone was wondering, was to get the BeOS R5 CD booting in qemu.
-jre
Index: block-raw.c
===================================================================
RCS file: /sources/qemu/qemu/block-raw.c,v
retrieving revision 1.10
block-raw.c +115 -0
@@ -39,6 +39,7 @@#include <IOKit/storage/IOMediaBSDClient.h> #include <IOKit/storage/IOMedia.h> #include <IOKit/storage/IOCDMedia.h> +#include <IOKit/storage/IOCDMediaBSDClient.h> //#include <IOKit/storage/IOCDTypes.h> #include <CoreFoundation/CoreFoundation.h> #endif
@@ -51,6 +52,10 @@#include <linux/cdrom.h> #include <linux/fd.h> #endif +#if defined(_BSD) && !defined(CONFIG_COCOA ) +#include <sys/cdio.h> +#include <arpa/inet.h> +#endif //#define DEBUG_FLOPPY
@@ -675,6 +680,114 @@ }#endif +static void raw_gettracks (BlockDriverState *bs,int *start,int *end) +{ +BDRVRawState *s = bs->opaque; +int fd = s->fd; +#ifdef CDROMREADTOCHDR +struct cdrom_tochdr hdr; +#endif +#ifdef CDIOREADTOCHEADER +struct ioc_toc_header hdr; +#endif +#ifdef DKIOCCDREADTOC +dk_cd_read_toc_t hdr; +uint8_t buf[4 ]; +#endif +int ret; + + *start =-1 ; + *end =-1 ; + ret =fd_open (bs); +if (ret <0 ) +return ; + +#if defined(CDROMREADTOCHDR ) +if (!ioctl (fd,CDROMREADTOCHDR , &hdr)) { + *start = hdr.cdth_trk0; + *end = hdr.cdth_trk1; + } +#endif +#ifdef CDIOREADTOCHEADER +if (!ioctl (fd,CDIOREADTOCHEADER , &hdr)) { + *start = hdr.starting_track; + *end = hdr.ending_track; + } +#endif +#if defined(DKIOCCDREADTOC ) +memset (&hdr,0 ,sizeof (hdr)); + hdr.bufferLength =sizeof (buf); + hdr.buffer = &buf; +if (!ioctl (fd,DKIOCCDREADTOC , &hdr) &&sizeof (buf) == hdr.bufferLength) { +/* buf is the same format as an ATAPI READ TOC/PMA/ATIP response */ + *start = buf[2 ]; + *end = buf[3 ]; + } +#endif + +return ; +} + +static int64_t raw_gettrackoff (BlockDriverState *bs,int track) +{ +BDRVRawState *s = bs->opaque; +int fd = s->fd; +int64_t off; +#if defined(CDIOREADTOCENTRY ) +struct ioc_read_toc_single_entry entry; +#elif defined(CDIOREADTOCENTRIES ) +struct ioc_read_toc_entry entry; +struct cd_toc_entry buf; +#elif defined(CDROMREADTOCENTRY ) +struct cdrom_tocentry entry; +#elif defined(DKIOCCDREADTOC ) +dk_cd_read_toc_t hdr; +uint8_t buf[804 ]; +int skip, max; +#endif +int ret; + + off =-1 ; + ret =fd_open (bs); +if (ret <0 ) +return off; + +#if defined(CDIOREADTOCENTRY ) + entry.address_format =CD_LBA_FORMAT ; + entry.track = track; +if (!ioctl (fd,CDIOREADTOCENTRY , &entry)) + off =ntohl (entry.entry.addr.lba); +#elif defined(CDIOREADTOCENTRIES ) + entry.address_format =CD_LBA_FORMAT ; + entry.starting_track = track; + entry.data_len =sizeof (buf); + entry.data = &buf; +if (!ioctl (fd,CDIOREADTOCENTRIES , &entry)) + off = buf.addr.lba; +#elif defined(CDROMREADTOCENTRY ) + entry.cdte_track = track; + entry.cdte_format =CDROM_LBA ; +if (!ioctl (fd,CDROMREADTOCENTRY , &entry)) + off = entry.cdte_addr.lba; +#elif defined(DKIOCCDREADTOC ) +memset (&hdr,0 ,sizeof (hdr)); + hdr.address.track = track; + hdr.bufferLength =sizeof (buf); + hdr.buffer = &buf; +if (!ioctl (fd,DKIOCCDREADTOC , &hdr) &&4 <= hdr.bufferLength) { +/* buf is the same format as an ATAPI READ TOC/PMA/ATIP response */ + max =OSReadBigInt16 (buf,0 ) +2 ; +if (sizeof (buf) < max) + max =sizeof (buf); +for (skip =4 ;0 > off && skip +8 <= max; skip +=8 ) +if (track == buf[skip +2 ]) + off =OSReadBigInt32 (buf, skip +4 ); + } +#endif + +return off; +} +#if defined(__linux__)static int raw_is_inserted (BlockDriverState *bs)
@@ -818,6 +931,8 @@ .bdrv_pread = raw_pread, .bdrv_pwrite = raw_pwrite, .bdrv_getlength = raw_getlength, + .bdrv_gettracks = raw_gettracks, + .bdrv_gettrackoff = raw_gettrackoff,/* removable device support */ .bdrv_is_inserted = raw_is_inserted,
Index: block.c
===================================================================
RCS file: /sources/qemu/qemu/block.c,v
retrieving revision 1.37
block.c +32 -0
@@ -705,6 +705,38 @@ *nb_sectors_ptr = length; } +void bdrv_get_track_count (BlockDriverState *bs,int *start,int *end) +{ +BlockDriver *drv = bs->drv; +if (!drv) + *start = *end =0 ; +else if (!drv->bdrv_gettracks) + *start = *end =1 ; +else + drv->bdrv_gettracks (bs, start, end); +} + +int64_t bdrv_get_track_offset (BlockDriverState *bs,int track) +{ +BlockDriver *drv = bs->drv; +int64_t ret; + +if (!drv) + ret =-1 ; +else if (!drv->bdrv_gettrackoff) { +if (1 == track) + ret =0 ; +else if (0xaa == track) +bdrv_get_geometry (bs, &ret); +else + ret =-1 ; + } +else + ret = drv->bdrv_gettrackoff (bs, track); + +return ret; +} +/* force a given boot sector. */ void bdrv_set_boot_sector (BlockDriverState *bs,const uint8_t *data,int size) {
Index: block_int.h
===================================================================
RCS file: /sources/qemu/qemu/block_int.h,v
retrieving revision 1.10
block_int.h +2 -0
@@ -58,6 +58,8 @@ const uint8_t *buf, int count);int (*bdrv_truncate)(BlockDriverState *bs,int64_t offset);int64_t (*bdrv_getlength)(BlockDriverState *bs); +void (*bdrv_gettracks)(BlockDriverState *bs,int *start,int *end); +int64_t (*bdrv_gettrackoff)(BlockDriverState *bs,int track);int (*bdrv_write_compressed)(BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors);
Index: vl.h
===================================================================
RCS file: /sources/qemu/qemu/vl.h,v
retrieving revision 1.154
vl.h +3 -1
@@ -564,6 +564,8 @@int bdrv_truncate (BlockDriverState *bs,int64_t offset);int64_t bdrv_getlength (BlockDriverState *bs);void bdrv_get_geometry (BlockDriverState *bs,int64_t *nb_sectors_ptr); +void bdrv_get_track_count (BlockDriverState *bs,int *start,int *end); +int64_t bdrv_get_track_offset (BlockDriverState *bs,int track);int bdrv_commit (BlockDriverState *bs);void bdrv_set_boot_sector (BlockDriverState *bs,const uint8_t *data,int size);/* async block I/O */
@@ -882,7 +884,7 @@ SetIRQFunc *set_irq, void *irq_opaque, int irq);/* cdrom.c */ -int cdrom_read_toc (int nb_sectors,uint8_t *buf,int msf,int start_track); +int cdrom_read_toc (BlockDriverState *bs,uint8_t *buf,int msf,int start_track);int cdrom_read_toc_raw (int nb_sectors,uint8_t *buf,int msf,int session_num);/* es1370.c */
Index: hw/cdrom.c
===================================================================
RCS file: /sources/qemu/qemu/hw/cdrom.c,v
retrieving revision 1.1
cdrom.c +27 -14
69 lines not on this page · raw message
Index: hw/ide.c
===================================================================
RCS file: /sources/qemu/qemu/hw/ide.c,v
retrieving revision 1.48
ide.c +1 -1
@@ -1415,7 +1415,7 @@ start_track = packet[6 ];switch (format) {case 0 : - len =cdrom_read_toc (total_sectors, buf, msf, start_track); + len =cdrom_read_toc (s->bs, buf, msf, start_track);if (len <0 ) goto error_cmd;ide_atapi_cmd_reply (s, len, max_len);
Quoted 5 lines · Josh Elsasser
The current raw block drvice code fakes a TOC with one track covering the entire disc. With this patch it attempts to read the TOC from a cdrom device and make that information available to the ide driver when the guest issues an ATAPI READ TOC/PMA/ATIP type 0 (read TOC) command.
I recently sent a patch to add this for linux but then I thought of a better way to do it.
I the TOC was stored in a struct somewhere, built when starting (or changing media) using either .toc, .cue, real CD or maybe even a list of track files this would allow to use such methods. Would be really useful for me actually to be able to use a list of tracks but generating toc/cue is quite easy.
Quoted 7 lines · Pascal Terjan
I recently sent a patch to add this for linux but then I thought of a better way to do it.
I the TOC was stored in a struct somewhere, built when starting (or changing media) using either .toc, .cue, real CD or maybe even a list of track files this would allow to use such methods. Would be really useful for me actually to be able to use a list of tracks but generating toc/cue is quite easy.
That would probably be better than reading the TOC from the CD each time the guest requests it. I had thought of using a .toc or .cue file as well, however it wasn't required for my purposes.
-jre