diff --git a/lib/_cdio_linux.c b/lib/_cdio_linux.c index e8fcb335..d91384df 100644 --- a/lib/_cdio_linux.c +++ b/lib/_cdio_linux.c @@ -1,5 +1,5 @@ /* - $Id: _cdio_linux.c,v 1.76 2004/07/25 03:05:18 rocky Exp $ + $Id: _cdio_linux.c,v 1.77 2004/07/25 15:29:51 rocky Exp $ Copyright (C) 2001 Herbert Valerio Riedel Copyright (C) 2002, 2003, 2004 Rocky Bernstein @@ -27,7 +27,7 @@ # include "config.h" #endif -static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.76 2004/07/25 03:05:18 rocky Exp $"; +static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.77 2004/07/25 15:29:51 rocky Exp $"; #include @@ -36,6 +36,7 @@ static const char _rcsid[] = "$Id: _cdio_linux.c,v 1.76 2004/07/25 03:05:18 rock #include #include #include +#include #include "cdtext_private.h" #include "cdio_assert.h" #include "cdio_private.h" @@ -108,7 +109,18 @@ typedef struct { #define ERRNO_TRAYEMPTY(errno) \ ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL)) +/**** prototypes for static functions ****/ +static bool is_cdrom_linux(const char *drive, char *mnttype); +static bool read_toc_linux (_img_private_t *p_env); +static int scsi_mmc_run_cmd_linux( const void *p_user_data, int i_timeout, + unsigned int i_cdb, + const scsi_mmc_cdb_t *p_cdb, + scsi_mmc_direction_t e_direction, + unsigned int i_buf, + /*in/out*/ void *p_buf ); +static int set_bsize (_img_private_t *p_env, unsigned int bsize); static access_mode_t + str_to_access_mode_linux(const char *psz_access_mode) { const access_mode_t default_access_mode = _AM_IOCTL; @@ -128,91 +140,8 @@ str_to_access_mode_linux(const char *psz_access_mode) } } -/* Check a drive to see if it is a CD-ROM - Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive - and -1 if no device exists . -*/ -static bool -cdio_is_cdrom(char *drive, char *mnttype) -{ - bool is_cd=false; - int cdfd; - struct cdrom_tochdr tochdr; - - /* If it doesn't exist, return -1 */ - if ( !cdio_is_device_quiet_generic(drive) ) { - return(false); - } - - /* If it does exist, verify that it's an available CD-ROM */ - cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0); - if ( cdfd >= 0 ) { - if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) { - is_cd = true; - } - close(cdfd); - } - /* Even if we can't read it, it might be mounted */ - else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) { - is_cd = true; - } - return(is_cd); -} - -/*! - Get disc tyhpe associated with cd_obj. -*/ -static discmode_t -_get_discmode_linux (void *p_user_data) -{ - _img_private_t *p_env = p_user_data; - - int32_t i_discmode; - - /* See if this is a DVD. */ - dvd_struct dvd; /* DVD READ STRUCT for layer 0. */ - - dvd.physical.type = DVD_STRUCT_PHYSICAL; - dvd.physical.layer_num = 0; - if (0 == ioctl (p_env->gen.fd, DVD_READ_STRUCT, &dvd)) { - switch(dvd.physical.layer[0].book_type) { - case 0: return CDIO_DISC_MODE_DVD_ROM; - case 1: return CDIO_DISC_MODE_DVD_RAM; - case 2: return CDIO_DISC_MODE_DVD_R; - case 3: return CDIO_DISC_MODE_DVD_RW; - case 8: return CDIO_DISC_MODE_DVD_RW; - case 9: return CDIO_DISC_MODE_DVD_RW; - default: return CDIO_DISC_MODE_DVD_OTHER; - } - } - - i_discmode = ioctl (p_env->gen.fd, CDROM_DISC_STATUS); - - if (i_discmode < 0) return CDIO_DISC_MODE_ERROR; - - /* FIXME Need to add getting DVD types. */ - switch(i_discmode) { - case CDS_AUDIO: - return CDIO_DISC_MODE_CD_DA; - case CDS_DATA_1: - return CDIO_DISC_MODE_CD_DATA_1; - case CDS_DATA_2: - return CDIO_DISC_MODE_CD_DATA_2; - case CDS_MIXED: - return CDIO_DISC_MODE_CD_MIXED; - case CDS_XA_2_1: - return CDIO_DISC_MODE_CD_XA_2_1; - case CDS_XA_2_2: - return CDIO_DISC_MODE_CD_XA_2_2; - case CDS_NO_INFO: - return CDIO_DISC_MODE_NO_INFO; - default: - return CDIO_DISC_MODE_ERROR; - } -} - static char * -cdio_check_mounts(const char *mtab) +check_mounts_linux(const char *mtab) { FILE *mntfp; struct mntent *mntent; @@ -263,7 +192,7 @@ cdio_check_mounts(const char *mtab) } } if ( strcmp(mnt_type, "iso9660") == 0 ) { - if (cdio_is_cdrom(mnt_dev, mnt_type) > 0) { + if (is_cdrom_linux(mnt_dev, mnt_type) > 0) { free(mnt_type); endmntent(mntfp); return mnt_dev; @@ -278,78 +207,401 @@ cdio_check_mounts(const char *mtab) } /*! - Run a SCSI MMC command. - - cdio CD structure set by cdio_open(). - i_timeout time in milliseconds we will wait for the command - to complete. If this value is -1, use the default - time-out value. - p_buf Buffer for data, both sending and receiving - i_buf Size of buffer - e_direction direction the transfer is to go. - cdb CDB bytes. All values that are needed should be set on - input. We'll figure out what the right CDB length should be. - - We return true if command completed successfully and false if not. - */ -static int -scsi_mmc_run_cmd_linux( const void *p_user_data, int i_timeout, - unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb, - scsi_mmc_direction_t e_direction, - unsigned int i_buf, /*out*/ void *p_buf ) + Return the value associated with the key "arg". +*/ +static const char * +get_arg_linux (void *env, const char key[]) { - const _img_private_t *p_env = p_user_data; - struct cdrom_generic_command cgc; - memset (&cgc, 0, sizeof (struct cdrom_generic_command)); - memcpy(&cgc.cmd, p_cdb, i_cdb); - cgc.buflen = i_buf; - cgc.buffer = p_buf; - cgc.data_direction = (SCSI_MMC_DATA_READ == cgc.data_direction) - ? CGC_DATA_READ : CGC_DATA_WRITE; + _img_private_t *_obj = env; -#ifdef HAVE_LINUX_CDROM_TIMEOUT - if (i_timeout >= 0) - cgc.timeout = i_timeout; -#endif - - return ioctl (p_env->gen.fd, CDROM_SEND_PACKET, &cgc); + if (!strcmp (key, "source")) { + return _obj->gen.source_name; + } else if (!strcmp (key, "access-mode")) { + switch (_obj->access_mode) { + case _AM_IOCTL: + return "ioctl"; + case _AM_READ_CD: + return "READ_CD"; + case _AM_READ_10: + return "READ_10"; + case _AM_NONE: + return "no access method"; + } + } + return NULL; } -static int -_set_bsize (_img_private_t *p_env, unsigned int bsize) +/*! + Return the number of of the first track. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_t +get_first_track_num_linux(void *p_user_data) { - scsi_mmc_cdb_t cdb = {{0, }}; - - struct - { - uint8_t reserved1; - uint8_t medium; - uint8_t reserved2; - uint8_t block_desc_length; - uint8_t density; - uint8_t number_of_blocks_hi; - uint8_t number_of_blocks_med; - uint8_t number_of_blocks_lo; - uint8_t reserved3; - uint8_t block_length_hi; - uint8_t block_length_med; - uint8_t block_length_lo; - } mh; - - memset (&mh, 0, sizeof (mh)); - mh.block_desc_length = 0x08; - mh.block_length_hi = (bsize >> 16) & 0xff; - mh.block_length_med = (bsize >> 8) & 0xff; - mh.block_length_lo = (bsize >> 0) & 0xff; - - CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SELECT_6); - - cdb.field[1] = 1 << 4; - cdb.field[4] = 12; + _img_private_t *p_env = p_user_data; - return scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, + if (!p_env->gen.toc_init) read_toc_linux (p_env) ; + + return p_env->gen.toc_init ? FIRST_TRACK_NUM : CDIO_INVALID_TRACK; +} + +/*! + Return the the kind of drive capabilities of device. + + Note: string is malloc'd so caller should free() then returned + string when done with it. + + */ +static void +get_drive_cap_linux (const void *env, + cdio_drive_read_cap_t *p_read_cap, + cdio_drive_write_cap_t *p_write_cap, + cdio_drive_misc_cap_t *p_misc_cap) +{ + const _img_private_t *_obj = env; + int32_t i_drivetype; + + i_drivetype = ioctl (_obj->gen.fd, CDROM_GET_CAPABILITY, CDSL_CURRENT); + + if (i_drivetype < 0) { + *p_read_cap = CDIO_DRIVE_CAP_ERROR; + *p_write_cap = CDIO_DRIVE_CAP_ERROR; + *p_misc_cap = CDIO_DRIVE_CAP_ERROR; + return; + } + + *p_read_cap = 0; + *p_write_cap = 0; + *p_misc_cap = 0; + + /* Reader */ + if (i_drivetype & CDC_PLAY_AUDIO) + *p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO; + if (i_drivetype & CDC_CD_R) + *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_R; + if (i_drivetype & CDC_CD_RW) + *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_RW; + if (i_drivetype & CDC_DVD) + *p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM; + + /* Writer */ + if (i_drivetype & CDC_CD_RW) + *p_read_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW; + if (i_drivetype & CDC_DVD_R) + *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R; + if (i_drivetype & CDC_DVD_RAM) + *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM; + + /* Misc */ + if (i_drivetype & CDC_CLOSE_TRAY) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_CLOSE_TRAY; + if (i_drivetype & CDC_OPEN_TRAY) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_EJECT; + if (i_drivetype & CDC_LOCK) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_LOCK; + if (i_drivetype & CDC_SELECT_SPEED) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_SPEED; + if (i_drivetype & CDC_SELECT_DISC) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_DISC; + if (i_drivetype & CDC_MULTI_SESSION) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MULTI_SESSION; + if (i_drivetype & CDC_MEDIA_CHANGED) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED; + if (i_drivetype & CDC_RESET) + *p_misc_cap |= CDIO_DRIVE_CAP_MISC_RESET; +} + +/*! + Return the media catalog number MCN. + + Note: string is malloc'd so caller should free() then returned + string when done with it. + + */ +static char * +_get_mcn_linux (const void *env) { + + struct cdrom_mcn mcn; + const _img_private_t *_obj = env; + memset(&mcn, 0, sizeof(mcn)); + if (ioctl(_obj->gen.fd, CDROM_GET_MCN, &mcn) != 0) + return NULL; + return strdup(mcn.medium_catalog_number); +} + +/*! + Return the number of tracks in the current medium. + CDIO_INVALID_TRACK is returned on error. +*/ +static track_t +_get_num_tracks_linux(void *p_user_data) +{ + _img_private_t *p_env = p_user_data; + + if (!p_env->gen.toc_init) read_toc_linux (p_env) ; + + return p_env->gen.toc_init ? TOTAL_TRACKS : CDIO_INVALID_TRACK; +} + +/*! + Get format of track. +*/ +static track_format_t +_get_track_format_linux(void *p_user_data, track_t i_track) +{ + _img_private_t *p_env = p_user_data; + + if (i_track > (TOTAL_TRACKS+FIRST_TRACK_NUM) || i_track < FIRST_TRACK_NUM) + return TRACK_FORMAT_ERROR; + + i_track -= FIRST_TRACK_NUM; + + /* This is pretty much copied from the "badly broken" cdrom_count_tracks + in linux/cdrom.c. + */ + if (p_env->tocent[i_track].cdte_ctrl & CDIO_CDROM_DATA_TRACK) { + if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK) + return TRACK_FORMAT_CDI; + else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK) + return TRACK_FORMAT_XA; + else + return TRACK_FORMAT_DATA; + } else + return TRACK_FORMAT_AUDIO; + +} + +/*! + Return true if we have XA data (green, mode2 form1) or + XA data (green, mode2 form2). That is track begins: + sync - header - subheader + 12 4 - 8 + + FIXME: there's gotta be a better design for this and get_track_format? +*/ +static bool +_get_track_green_linux(void *p_user_data, track_t i_track) +{ + _img_private_t *p_env = p_user_data; + + if (!p_env->gen.toc_init) read_toc_linux (p_env) ; + + if (i_track >= (TOTAL_TRACKS+FIRST_TRACK_NUM) || i_track < FIRST_TRACK_NUM) + return false; + + i_track -= FIRST_TRACK_NUM; + + /* FIXME: Dunno if this is the right way, but it's what + I was using in cd-info for a while. + */ + return ((p_env->tocent[i_track].cdte_ctrl & 2) != 0); +} + +/*! + Return the starting MSF (minutes/secs/frames) for track number + track_num in obj. Track numbers usually start at something + greater than 0, usually 1. + + The "leadout" track is specified either by + using i_track LEADOUT_TRACK or the total tracks+1. + False is returned if there is no track entry. +*/ +static bool +_get_track_msf_linux(void *p_user_data, track_t i_track, msf_t *msf) +{ + _img_private_t *p_env = p_user_data; + + if (NULL == msf) return false; + + if (!p_env->gen.toc_init) read_toc_linux (p_env) ; + + if (i_track == CDIO_CDROM_LEADOUT_TRACK) + i_track = TOTAL_TRACKS + FIRST_TRACK_NUM; + + if (i_track > (TOTAL_TRACKS+FIRST_TRACK_NUM) + || i_track < FIRST_TRACK_NUM) { + return false; + } else { + struct cdrom_msf0 *msf0= + &p_env->tocent[i_track-FIRST_TRACK_NUM].cdte_addr.msf; + msf->m = to_bcd8(msf0->minute); + msf->s = to_bcd8(msf0->second); + msf->f = to_bcd8(msf0->frame); + return true; + } +} + +/*! + * Eject using SCSI MMC commands. Return 0 if successful. + */ +static int +eject_media_mmc(_img_private_t *p_env) +{ + int i_status; + scsi_mmc_cdb_t cdb = {{0, }}; + uint8_t buf[1]; + + CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL); + + i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, - SCSI_MMC_DATA_WRITE, sizeof(mh), &mh); + SCSI_MMC_DATA_WRITE, 0, &buf); + if (0 != i_status) + return i_status; + + CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP); + cdb.field[4] = 1; + i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, + scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, + SCSI_MMC_DATA_WRITE, 0, &buf); + if (0 != i_status) + return i_status; + + CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP); + cdb.field[4] = 2; /* eject */ + + i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, + scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, + SCSI_MMC_DATA_WRITE, 0, &buf); + if (0 != i_status) + return i_status; + + /* force kernel to reread partition table when new disc inserted */ + i_status = ioctl(p_env->gen.fd, BLKRRPART); + return (i_status); + +} + +/*! + Eject media in CD drive. + Return 0 if success and 1 for failure, and 2 if no routine. + */ +static int +eject_media_linux (void *p_user_data) { + + _img_private_t *p_env = p_user_data; + int ret=2; + int status; + int fd; + + if ((fd = open (p_env->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) { + if((status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) > 0) { + switch(status) { + case CDS_TRAY_OPEN: + if((ret = ioctl(fd, CDROMCLOSETRAY)) != 0) { + cdio_warn ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno)); + ret = 1; + } + break; + case CDS_DISC_OK: + if((ret = ioctl(fd, CDROMEJECT)) != 0) { + int eject_error = errno; + /* Try ejecting the MMC way... */ + ret = eject_media_mmc(p_env); + if (0 != ret) { + cdio_warn("ioctl CDROMEJECT failed: %s\n", + strerror(eject_error)); + ret = 1; + } + } + break; + default: + cdio_warn ("Unknown CD-ROM (%d)\n", status); + ret = 1; + } + } else { + cdio_warn ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno)); + ret=1; + } + close(fd); + } else + ret = 2; + close(p_env->gen.fd); + p_env->gen.fd = -1; + return ret; +} + +/*! + Get disc tyhpe associated with cd_obj. +*/ +static discmode_t +get_discmode_linux (void *p_user_data) +{ + _img_private_t *p_env = p_user_data; + + int32_t i_discmode; + + /* See if this is a DVD. */ + cdio_dvd_struct_t dvd; /* DVD READ STRUCT for layer 0. */ + + dvd.physical.type = DVD_STRUCT_PHYSICAL; + dvd.physical.layer_num = 0; + if (0 == ioctl (p_env->gen.fd, DVD_READ_STRUCT, &dvd)) { + switch(dvd.physical.layer[0].book_type) { + case CDIO_DVD_BOOT_DVD_ROM: return CDIO_DISC_MODE_DVD_ROM; + case CDIO_DVD_BOOK_DVD_RAM: return CDIO_DISC_MODE_DVD_RAM; + case CDIO_DVD_BOOK_DVD_R: return CDIO_DISC_MODE_DVD_R; + case CDIO_DVD_BOOK_DVD_RW: return CDIO_DISC_MODE_DVD_RW; + case CDIO_DVD_BOOK_DVD_PW: return CDIO_DISC_MODE_DVD_PR; + case CDIO_DVD_BOOK_DVD_PRW: return CDIO_DISC_MODE_DVD_PRW; + default: return CDIO_DISC_MODE_DVD_OTHER; + } + } + + i_discmode = ioctl (p_env->gen.fd, CDROM_DISC_STATUS); + + if (i_discmode < 0) return CDIO_DISC_MODE_ERROR; + + /* FIXME Need to add getting DVD types. */ + switch(i_discmode) { + case CDS_AUDIO: + return CDIO_DISC_MODE_CD_DA; + case CDS_DATA_1: + return CDIO_DISC_MODE_CD_DATA_1; + case CDS_DATA_2: + return CDIO_DISC_MODE_CD_DATA_2; + case CDS_MIXED: + return CDIO_DISC_MODE_CD_MIXED; + case CDS_XA_2_1: + return CDIO_DISC_MODE_CD_XA_2_1; + case CDS_XA_2_2: + return CDIO_DISC_MODE_CD_XA_2_2; + case CDS_NO_INFO: + return CDIO_DISC_MODE_NO_INFO; + default: + return CDIO_DISC_MODE_ERROR; + } +} + +/* Check a drive to see if it is a CD-ROM + Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive + and -1 if no device exists . +*/ +static bool +is_cdrom_linux(const char *drive, char *mnttype) +{ + bool is_cd=false; + int cdfd; + struct cdrom_tochdr tochdr; + + /* If it doesn't exist, return -1 */ + if ( !cdio_is_device_quiet_generic(drive) ) { + return(false); + } + + /* If it does exist, verify that it's an available CD-ROM */ + cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0); + if ( cdfd >= 0 ) { + if ( ioctl(cdfd, CDROMREADTOCHDR, &tochdr) != -1 ) { + is_cd = true; + } + close(cdfd); + } + /* Even if we can't read it, it might be mounted */ + else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) { + is_cd = true; + } + return(is_cd); } /* Packet driver to read mode2 sectors. @@ -410,19 +662,21 @@ _read_mode2_sectors_mmc (_img_private_t *p_env, void *p_buf, lba_t lba, if (b_read_10) { int retval; - if ((retval = _set_bsize (p_env, M2RAW_SECTOR_SIZE))) + if ((retval = set_bsize (p_env, M2RAW_SECTOR_SIZE))) return retval; if ((retval = scsi_mmc_run_cmd_linux (p_env, 0, - scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, - SCSI_MMC_DATA_READ, - M2RAW_SECTOR_SIZE * nblocks, p_buf))) + scsi_mmc_get_cmd_len(cdb.field[0]), + &cdb, + SCSI_MMC_DATA_READ, + M2RAW_SECTOR_SIZE * nblocks, + p_buf))) { - _set_bsize (p_env, CDIO_CD_FRAMESIZE); + set_bsize (p_env, CDIO_CD_FRAMESIZE); return retval; } - if ((retval = _set_bsize (p_env, CDIO_CD_FRAMESIZE))) + if ((retval = set_bsize (p_env, CDIO_CD_FRAMESIZE))) return retval; } else return scsi_mmc_run_cmd_linux (p_env, 0, @@ -544,7 +798,7 @@ _read_mode1_sectors_linux (void *p_user_data, void *p_data, lsn_t lsn, for (i = 0; i < nblocks; i++) { if ( (retval = _read_mode1_sector_linux (p_env, - ((char *)p_data) + (blocksize * i), + ((char *)p_data) + (blocksize*i), lsn + i, b_form2)) ) return retval; } @@ -635,75 +889,19 @@ _read_mode2_sectors_linux (void *p_user_data, void *data, lsn_t lsn, for (i = 0; i < nblocks; i++) { int retval; if ( (retval = _read_mode2_sector_linux (p_env, - ((char *)data) + (i_blocksize * i), + ((char *)data) + (i_blocksize*i), lsn + i, b_form2)) ) return retval; } return 0; } -/*! - Return the size of the CD in logical block address (LBA) units. - */ -static uint32_t -_stat_size_linux (void *p_user_data) -{ - _img_private_t *p_env = p_user_data; - - struct cdrom_tocentry tocent; - uint32_t size; - - tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK; - tocent.cdte_format = CDROM_LBA; - if (ioctl (p_env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1) - { - perror ("ioctl(CDROMREADTOCENTRY)"); - exit (EXIT_FAILURE); - } - - size = tocent.cdte_addr.lba; - - return size; -} - -/*! - Set the arg "key" with "value" in the source device. - Currently "source" and "access-mode" are valid keys. - "source" sets the source device in I/O operations - "access-mode" sets the the method of CD access - - 0 is returned if no error was found, and nonzero if there as an error. -*/ -static int -_set_arg_linux (void *p_user_data, const char key[], const char value[]) -{ - _img_private_t *p_env = p_user_data; - - if (!strcmp (key, "source")) - { - if (!value) - return -2; - - free (p_env->gen.source_name); - - p_env->gen.source_name = strdup (value); - } - else if (!strcmp (key, "access-mode")) - { - return str_to_access_mode_linux(value); - } - else - return -1; - - return 0; -} - /*! Read and cache the CD's Track Table of Contents and track info. Return false if successful or true if an error. */ static bool -_cdio_read_toc (_img_private_t *p_env) +read_toc_linux (_img_private_t *p_env) { int i; @@ -757,6 +955,137 @@ _cdio_read_toc (_img_private_t *p_env) return true; } +/*! + Run a SCSI MMC command. + + cdio CD structure set by cdio_open(). + i_timeout time in milliseconds we will wait for the command + to complete. If this value is -1, use the default + time-out value. + p_buf Buffer for data, both sending and receiving + i_buf Size of buffer + e_direction direction the transfer is to go. + cdb CDB bytes. All values that are needed should be set on + input. We'll figure out what the right CDB length should be. + + We return true if command completed successfully and false if not. + */ +static int +scsi_mmc_run_cmd_linux( const void *p_user_data, int i_timeout, + unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb, + scsi_mmc_direction_t e_direction, + unsigned int i_buf, /*in/out*/ void *p_buf ) +{ + const _img_private_t *p_env = p_user_data; + struct cdrom_generic_command cgc; + memset (&cgc, 0, sizeof (struct cdrom_generic_command)); + memcpy(&cgc.cmd, p_cdb, i_cdb); + cgc.buflen = i_buf; + cgc.buffer = p_buf; + cgc.data_direction = (SCSI_MMC_DATA_READ == cgc.data_direction) + ? CGC_DATA_READ : CGC_DATA_WRITE; + +#ifdef HAVE_LINUX_CDROM_TIMEOUT + if (i_timeout >= 0) + cgc.timeout = i_timeout; +#endif + + return ioctl (p_env->gen.fd, CDROM_SEND_PACKET, &cgc); +} + +static int +set_bsize (_img_private_t *p_env, unsigned int bsize) +{ + scsi_mmc_cdb_t cdb = {{0, }}; + + struct + { + uint8_t reserved1; + uint8_t medium; + uint8_t reserved2; + uint8_t block_desc_length; + uint8_t density; + uint8_t number_of_blocks_hi; + uint8_t number_of_blocks_med; + uint8_t number_of_blocks_lo; + uint8_t reserved3; + uint8_t block_length_hi; + uint8_t block_length_med; + uint8_t block_length_lo; + } mh; + + memset (&mh, 0, sizeof (mh)); + mh.block_desc_length = 0x08; + mh.block_length_hi = (bsize >> 16) & 0xff; + mh.block_length_med = (bsize >> 8) & 0xff; + mh.block_length_lo = (bsize >> 0) & 0xff; + + CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SELECT_6); + + cdb.field[1] = 1 << 4; + cdb.field[4] = 12; + + return scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, + scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, + SCSI_MMC_DATA_WRITE, sizeof(mh), &mh); +} + +/*! + Return the size of the CD in logical block address (LBA) units. + */ +static uint32_t +stat_size_linux (void *p_user_data) +{ + _img_private_t *p_env = p_user_data; + + struct cdrom_tocentry tocent; + uint32_t size; + + tocent.cdte_track = CDIO_CDROM_LEADOUT_TRACK; + tocent.cdte_format = CDROM_LBA; + if (ioctl (p_env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1) + { + perror ("ioctl(CDROMREADTOCENTRY)"); + exit (EXIT_FAILURE); + } + + size = tocent.cdte_addr.lba; + + return size; +} + +/*! + Set the arg "key" with "value" in the source device. + Currently "source" and "access-mode" are valid keys. + "source" sets the source device in I/O operations + "access-mode" sets the the method of CD access + + 0 is returned if no error was found, and nonzero if there as an error. +*/ +static int +set_arg_linux (void *p_user_data, const char key[], const char value[]) +{ + _img_private_t *p_env = p_user_data; + + if (!strcmp (key, "source")) + { + if (!value) + return -2; + + free (p_env->gen.source_name); + + p_env->gen.source_name = strdup (value); + } + else if (!strcmp (key, "access-mode")) + { + return str_to_access_mode_linux(value); + } + else + return -1; + + return 0; +} + static void set_cdtext_field_linux(void *user_data, track_t i_track, track_t i_first_track, @@ -781,7 +1110,7 @@ set_cdtext_field_linux(void *user_data, track_t i_track, not exist. */ static bool -_init_cdtext_linux (_img_private_t *p_env) +init_cdtext_linux (_img_private_t *p_env) { scsi_mmc_cdb_t cdb = {{0, }}; @@ -817,7 +1146,7 @@ _init_cdtext_linux (_img_private_t *p_env) or CD-TEXT information does not exist. */ static const cdtext_t * -_get_cdtext_linux (void *p_user_data, track_t i_track) +get_cdtext_linux (void *p_user_data, track_t i_track) { _img_private_t *p_env = p_user_data; @@ -826,7 +1155,7 @@ _get_cdtext_linux (void *p_user_data, track_t i_track) && i_track >= TOTAL_TRACKS+FIRST_TRACK_NUM ) ) return NULL; - p_env->b_cdtext_init = _init_cdtext_linux(p_env); + p_env->b_cdtext_init = init_cdtext_linux(p_env); if (!p_env->b_cdtext_init || p_env->b_cdtext_error) return NULL; if (0 == i_track) @@ -836,319 +1165,6 @@ _get_cdtext_linux (void *p_user_data, track_t i_track) } -/*! - * Eject using SCSI MMC commands. Return 0 if successful. - */ -static int -_eject_media_mmc(_img_private_t *p_env) -{ - int i_status; - scsi_mmc_cdb_t cdb = {{0, }}; - uint8_t buf[1]; - - CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL); - - i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, - scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, - SCSI_MMC_DATA_WRITE, 0, &buf); - if (0 != i_status) - return i_status; - - CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP); - cdb.field[4] = 1; - i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, - scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, - SCSI_MMC_DATA_WRITE, 0, &buf); - if (0 != i_status) - return i_status; - - CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_START_STOP); - cdb.field[4] = 2; /* eject */ - - i_status = scsi_mmc_run_cmd_linux (p_env, DEFAULT_TIMEOUT, - scsi_mmc_get_cmd_len(cdb.field[0]), &cdb, - SCSI_MMC_DATA_WRITE, 0, &buf); - if (0 != i_status) - return i_status; - - /* force kernel to reread partition table when new disc inserted */ - i_status = ioctl(p_env->gen.fd, BLKRRPART); - return (i_status); - -} - -/*! - Eject media in CD drive. - Return 0 if success and 1 for failure, and 2 if no routine. - */ -static int -_eject_media_linux (void *p_user_data) { - - _img_private_t *p_env = p_user_data; - int ret=2; - int status; - int fd; - - if ((fd = open (p_env->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) { - if((status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) > 0) { - switch(status) { - case CDS_TRAY_OPEN: - if((ret = ioctl(fd, CDROMCLOSETRAY)) != 0) { - cdio_warn ("ioctl CDROMCLOSETRAY failed: %s\n", strerror(errno)); - ret = 1; - } - break; - case CDS_DISC_OK: - if((ret = ioctl(fd, CDROMEJECT)) != 0) { - int eject_error = errno; - /* Try ejecting the MMC way... */ - ret = _eject_media_mmc(p_env); - if (0 != ret) { - cdio_warn("ioctl CDROMEJECT failed: %s\n", strerror(eject_error)); - ret = 1; - } - } - break; - default: - cdio_warn ("Unknown CD-ROM (%d)\n", status); - ret = 1; - } - } else { - cdio_warn ("CDROM_DRIVE_STATUS failed: %s\n", strerror(errno)); - ret=1; - } - close(fd); - } else - ret = 2; - close(p_env->gen.fd); - p_env->gen.fd = -1; - return ret; -} - -/*! - Return the value associated with the key "arg". -*/ -static const char * -_get_arg_linux (void *env, const char key[]) -{ - _img_private_t *_obj = env; - - if (!strcmp (key, "source")) { - return _obj->gen.source_name; - } else if (!strcmp (key, "access-mode")) { - switch (_obj->access_mode) { - case _AM_IOCTL: - return "ioctl"; - case _AM_READ_CD: - return "READ_CD"; - case _AM_READ_10: - return "READ_10"; - case _AM_NONE: - return "no access method"; - } - } - return NULL; -} - -/*! - Return the number of of the first track. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_t -_get_first_track_num_linux(void *p_user_data) -{ - _img_private_t *p_env = p_user_data; - - if (!p_env->gen.toc_init) _cdio_read_toc (p_env) ; - - return FIRST_TRACK_NUM; -} - -/*! - Return the media catalog number MCN. - - Note: string is malloc'd so caller should free() then returned - string when done with it. - - */ -static char * -_get_mcn_linux (const void *env) { - - struct cdrom_mcn mcn; - const _img_private_t *_obj = env; - memset(&mcn, 0, sizeof(mcn)); - if (ioctl(_obj->gen.fd, CDROM_GET_MCN, &mcn) != 0) - return NULL; - return strdup(mcn.medium_catalog_number); -} - -/*! - Return the the kind of drive capabilities of device. - - Note: string is malloc'd so caller should free() then returned - string when done with it. - - */ -static void -_get_drive_cap_linux (const void *env, - cdio_drive_read_cap_t *p_read_cap, - cdio_drive_write_cap_t *p_write_cap, - cdio_drive_misc_cap_t *p_misc_cap) -{ - const _img_private_t *_obj = env; - int32_t i_drivetype; - - i_drivetype = ioctl (_obj->gen.fd, CDROM_GET_CAPABILITY, CDSL_CURRENT); - - if (i_drivetype < 0) { - *p_read_cap = CDIO_DRIVE_CAP_ERROR; - *p_write_cap = CDIO_DRIVE_CAP_ERROR; - *p_misc_cap = CDIO_DRIVE_CAP_ERROR; - return; - } - - *p_read_cap = 0; - *p_write_cap = 0; - *p_misc_cap = 0; - - /* Reader */ - if (i_drivetype & CDC_PLAY_AUDIO) - *p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO; - if (i_drivetype & CDC_CD_R) - *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_R; - if (i_drivetype & CDC_CD_RW) - *p_read_cap |= CDIO_DRIVE_CAP_READ_CD_RW; - if (i_drivetype & CDC_DVD) - *p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM; - - /* Writer */ - if (i_drivetype & CDC_CD_RW) - *p_read_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW; - if (i_drivetype & CDC_DVD_R) - *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R; - if (i_drivetype & CDC_DVD_RAM) - *p_read_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM; - - /* Misc */ - if (i_drivetype & CDC_CLOSE_TRAY) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_CLOSE_TRAY; - if (i_drivetype & CDC_OPEN_TRAY) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_EJECT; - if (i_drivetype & CDC_LOCK) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_LOCK; - if (i_drivetype & CDC_SELECT_SPEED) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_SPEED; - if (i_drivetype & CDC_SELECT_DISC) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_SELECT_DISC; - if (i_drivetype & CDC_MULTI_SESSION) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MULTI_SESSION; - if (i_drivetype & CDC_MEDIA_CHANGED) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED; - if (i_drivetype & CDC_RESET) - *p_misc_cap |= CDIO_DRIVE_CAP_MISC_RESET; -} - -/*! - Return the number of tracks in the current medium. - CDIO_INVALID_TRACK is returned on error. -*/ -static track_t -_get_num_tracks_linux(void *p_user_data) -{ - _img_private_t *p_env = p_user_data; - - if (!p_env->gen.toc_init) _cdio_read_toc (p_env) ; - - return TOTAL_TRACKS; -} - -/*! - Get format of track. -*/ -static track_format_t -_get_track_format_linux(void *p_user_data, track_t i_track) -{ - _img_private_t *p_env = p_user_data; - - if (i_track > (TOTAL_TRACKS+FIRST_TRACK_NUM) || i_track < FIRST_TRACK_NUM) - return TRACK_FORMAT_ERROR; - - i_track -= FIRST_TRACK_NUM; - - /* This is pretty much copied from the "badly broken" cdrom_count_tracks - in linux/cdrom.c. - */ - if (p_env->tocent[i_track].cdte_ctrl & CDIO_CDROM_DATA_TRACK) { - if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK) - return TRACK_FORMAT_CDI; - else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK) - return TRACK_FORMAT_XA; - else - return TRACK_FORMAT_DATA; - } else - return TRACK_FORMAT_AUDIO; - -} - -/*! - Return true if we have XA data (green, mode2 form1) or - XA data (green, mode2 form2). That is track begins: - sync - header - subheader - 12 4 - 8 - - FIXME: there's gotta be a better design for this and get_track_format? -*/ -static bool -_get_track_green_linux(void *p_user_data, track_t i_track) -{ - _img_private_t *p_env = p_user_data; - - if (!p_env->gen.toc_init) _cdio_read_toc (p_env) ; - - if (i_track >= (TOTAL_TRACKS+FIRST_TRACK_NUM) || i_track < FIRST_TRACK_NUM) - return false; - - i_track -= FIRST_TRACK_NUM; - - /* FIXME: Dunno if this is the right way, but it's what - I was using in cd-info for a while. - */ - return ((p_env->tocent[i_track].cdte_ctrl & 2) != 0); -} - -/*! - Return the starting MSF (minutes/secs/frames) for track number - track_num in obj. Track numbers usually start at something - greater than 0, usually 1. - - The "leadout" track is specified either by - using i_track LEADOUT_TRACK or the total tracks+1. - False is returned if there is no track entry. -*/ -static bool -_get_track_msf_linux(void *p_user_data, track_t i_track, msf_t *msf) -{ - _img_private_t *p_env = p_user_data; - - if (NULL == msf) return false; - - if (!p_env->gen.toc_init) _cdio_read_toc (p_env) ; - - if (i_track == CDIO_CDROM_LEADOUT_TRACK) - i_track = TOTAL_TRACKS + FIRST_TRACK_NUM; - - if (i_track > (TOTAL_TRACKS+FIRST_TRACK_NUM) || i_track < FIRST_TRACK_NUM) { - return false; - } else { - struct cdrom_msf0 *msf0= - &p_env->tocent[i_track-FIRST_TRACK_NUM].cdte_addr.msf; - msf->m = to_bcd8(msf0->minute); - msf->s = to_bcd8(msf0->second); - msf->f = to_bcd8(msf0->frame); - return true; - } -} - /* checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr? */ static char checklist1[][40] = { {"cdrom"}, {"dvd"}, {""} @@ -1179,19 +1195,19 @@ cdio_get_devices_linux (void) */ for ( i=0; strlen(checklist1[i]) > 0; ++i ) { sprintf(drive, "/dev/%s", checklist1[i]); - if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) { + if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) { cdio_add_device_list(&drives, drive, &num_drives); } } /* Now check the currently mounted CD drives */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) { + if (NULL != (ret_drive = check_mounts_linux("/etc/mtab"))) { cdio_add_device_list(&drives, ret_drive, &num_drives); free(ret_drive); } /* Finally check possible mountable drives in /etc/fstab */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) { + if (NULL != (ret_drive = check_mounts_linux("/etc/fstab"))) { cdio_add_device_list(&drives, ret_drive, &num_drives); free(ret_drive); } @@ -1209,7 +1225,7 @@ cdio_get_devices_linux (void) if ( insert != NULL ) { *insert = j; } - if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) { + if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) { cdio_add_device_list(&drives, drive, &num_drives); } } @@ -1238,17 +1254,17 @@ cdio_get_default_device_linux(void) */ for ( i=0; strlen(checklist1[i]) > 0; ++i ) { sprintf(drive, "/dev/%s", checklist1[i]); - if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) { + if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) { return strdup(drive); } } /* Now check the currently mounted CD drives */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) + if (NULL != (ret_drive = check_mounts_linux("/etc/mtab"))) return ret_drive; /* Finally check possible mountable drives in /etc/fstab */ - if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) + if (NULL != (ret_drive = check_mounts_linux("/etc/fstab"))) return ret_drive; /* Scan the system for CD-ROM drives. @@ -1264,7 +1280,7 @@ cdio_get_default_device_linux(void) if ( insert != NULL ) { *insert = j; } - if ( (exists=cdio_is_cdrom(drive, NULL)) > 0 ) { + if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) { return(strdup(drive)); } } @@ -1298,15 +1314,15 @@ cdio_open_am_linux (const char *psz_orig_source, const char *access_mode) char *psz_source; cdio_funcs _funcs = { - .eject_media = _eject_media_linux, + .eject_media = eject_media_linux, .free = cdio_generic_free, - .get_arg = _get_arg_linux, - .get_cdtext = _get_cdtext_linux, + .get_arg = get_arg_linux, + .get_cdtext = get_cdtext_linux, .get_devices = cdio_get_devices_linux, .get_default_device = cdio_get_default_device_linux, - .get_discmode = _get_discmode_linux, - .get_drive_cap = _get_drive_cap_linux, - .get_first_track_num= _get_first_track_num_linux, + .get_discmode = get_discmode_linux, + .get_drive_cap = get_drive_cap_linux, + .get_first_track_num= get_first_track_num_linux, .get_mcn = _get_mcn_linux, .get_num_tracks = _get_num_tracks_linux, .get_track_format = _get_track_format_linux, @@ -1321,8 +1337,8 @@ cdio_open_am_linux (const char *psz_orig_source, const char *access_mode) .read_mode2_sector = _read_mode2_sector_linux, .read_mode2_sectors = _read_mode2_sectors_linux, .run_scsi_mmc_cmd = scsi_mmc_run_cmd_linux, - .set_arg = _set_arg_linux, - .stat_size = _stat_size_linux + .set_arg = set_arg_linux, + .stat_size = stat_size_linux }; _data = _cdio_malloc (sizeof (_img_private_t)); @@ -1336,11 +1352,11 @@ cdio_open_am_linux (const char *psz_orig_source, const char *access_mode) if (NULL == psz_orig_source) { psz_source=cdio_get_default_device_linux(); if (NULL == psz_source) return NULL; - _set_arg_linux(_data, "source", psz_source); + set_arg_linux(_data, "source", psz_source); free(psz_source); } else { if (cdio_is_device_generic(psz_orig_source)) - _set_arg_linux(_data, "source", psz_orig_source); + set_arg_linux(_data, "source", psz_orig_source); else { /* The below would be okay if all device drivers worked this way. */ cdio_info ("source %s is a not a device", psz_orig_source); @@ -1373,5 +1389,3 @@ cdio_have_linux (void) return false; #endif /* HAVE_LINUX_CDROM */ } - -