cdrom_id.c revision a0ec302b9309bc56f6bed6162e47ad6c27165747
/*
* cdrom_id - optical drive and media information prober
*
* Copyright (C) 2008-2010 Kay Sievers <kay@vrfy.org>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#include "libudev.h"
#include "libudev-private.h"
static bool debug;
{
if (debug) {
} else {
}
}
/* device info */
static unsigned int cd_cd_rom;
static unsigned int cd_cd_r;
static unsigned int cd_cd_rw;
static unsigned int cd_dvd_rom;
static unsigned int cd_dvd_r;
static unsigned int cd_dvd_rw;
static unsigned int cd_dvd_ram;
static unsigned int cd_dvd_plus_r;
static unsigned int cd_dvd_plus_rw;
static unsigned int cd_dvd_plus_r_dl;
static unsigned int cd_dvd_plus_rw_dl;
static unsigned int cd_bd;
static unsigned int cd_bd_r;
static unsigned int cd_bd_re;
static unsigned int cd_hddvd;
static unsigned int cd_hddvd_r;
static unsigned int cd_hddvd_rw;
static unsigned int cd_mo;
static unsigned int cd_mrw;
static unsigned int cd_mrw_w;
/* media info */
static unsigned int cd_media;
static unsigned int cd_media_cd_rom;
static unsigned int cd_media_cd_r;
static unsigned int cd_media_cd_rw;
static unsigned int cd_media_dvd_rom;
static unsigned int cd_media_dvd_r;
static unsigned int cd_media_dvd_rw;
static unsigned int cd_media_dvd_rw_ro; /* restricted overwrite mode */
static unsigned int cd_media_dvd_rw_seq; /* sequential mode */
static unsigned int cd_media_dvd_ram;
static unsigned int cd_media_dvd_plus_r;
static unsigned int cd_media_dvd_plus_rw;
static unsigned int cd_media_dvd_plus_r_dl;
static unsigned int cd_media_dvd_plus_rw_dl;
static unsigned int cd_media_bd;
static unsigned int cd_media_bd_r;
static unsigned int cd_media_bd_re;
static unsigned int cd_media_hddvd;
static unsigned int cd_media_hddvd_r;
static unsigned int cd_media_hddvd_rw;
static unsigned int cd_media_mo;
static unsigned int cd_media_mrw;
static unsigned int cd_media_mrw_w;
static const char *cd_media_state = NULL;
static unsigned int cd_media_session_next;
static unsigned int cd_media_session_count;
static unsigned int cd_media_track_count;
static unsigned int cd_media_track_count_data;
static unsigned int cd_media_track_count_audio;
static unsigned long long int cd_media_session_last_offset;
static bool is_mounted(const char *device)
{
bool mounted = false;
return -ENODEV;
return -ENOSYS;
mounted = true;
break;
}
}
return mounted;
}
{
if (err == -1) {
return;
}
}
struct scsi_cmd {
struct cdrom_generic_command cgc;
union {
struct request_sense s;
unsigned char u[18];
} _sense;
};
{
}
{
}
#define CHECK_CONDITION 0x01
static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
{
int ret = 0;
if (bufsize > 0) {
} else {
}
return -1;
ret = -1;
if (ret == 0)
ret = -1;
}
}
return ret;
}
{
int err;
/* disable the kernel's lock logic */
if (err < 0)
log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed\n");
if (err < 0)
log_debug("CDROM_LOCKDOOR failed\n");
return err;
}
{
int err;
if ((err != 0)) {
return -1;
}
return 0;
}
{
int capability;
if (capability < 0) {
log_debug("CDROM_GET_CAPABILITY failed\n");
return -1;
}
if (capability & CDC_CD_R)
cd_cd_r = 1;
if (capability & CDC_CD_RW)
cd_cd_rw = 1;
if (capability & CDC_DVD)
cd_dvd_rom = 1;
if (capability & CDC_DVD_R)
cd_dvd_r = 1;
if (capability & CDC_DVD_RAM)
cd_dvd_ram = 1;
if (capability & CDC_MRW)
cd_mrw = 1;
if (capability & CDC_MRW_W)
cd_mrw_w = 1;
return 0;
}
{
log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
return -1;
}
cd_media = 1;
return 0;
}
{
unsigned char inq[128];
int err;
if ((err != 0)) {
return -1;
}
log_debug("not an MMC unit\n");
return -1;
}
return 0;
}
{
switch (cur_profile) {
case 0x03:
case 0x04:
case 0x05:
cd_media = 1;
cd_media_mo = 1;
break;
case 0x08:
cd_media = 1;
cd_media_cd_rom = 1;
break;
case 0x09:
cd_media = 1;
cd_media_cd_r = 1;
break;
case 0x0a:
cd_media = 1;
cd_media_cd_rw = 1;
break;
case 0x10:
cd_media = 1;
cd_media_dvd_rom = 1;
break;
case 0x11:
cd_media = 1;
cd_media_dvd_r = 1;
break;
case 0x12:
cd_media = 1;
cd_media_dvd_ram = 1;
break;
case 0x13:
cd_media = 1;
cd_media_dvd_rw = 1;
cd_media_dvd_rw_ro = 1;
break;
case 0x14:
cd_media = 1;
cd_media_dvd_rw = 1;
cd_media_dvd_rw_seq = 1;
break;
case 0x1B:
cd_media = 1;
cd_media_dvd_plus_r = 1;
break;
case 0x1A:
cd_media = 1;
cd_media_dvd_plus_rw = 1;
break;
case 0x2A:
cd_media = 1;
break;
case 0x2B:
cd_media = 1;
break;
case 0x40:
cd_media = 1;
cd_media_bd = 1;
break;
case 0x41:
case 0x42:
cd_media = 1;
cd_media_bd_r = 1;
break;
case 0x43:
cd_media = 1;
cd_media_bd_re = 1;
break;
case 0x50:
cd_media = 1;
cd_media_hddvd = 1;
break;
case 0x51:
cd_media = 1;
cd_media_hddvd_r = 1;
break;
case 0x52:
cd_media = 1;
cd_media_hddvd_rw = 1;
break;
default:
break;
}
}
{
unsigned int i;
int profile;
switch (profile) {
case 0x03:
case 0x04:
case 0x05:
cd_mo = 1;
break;
case 0x08:
cd_cd_rom = 1;
break;
case 0x09:
cd_cd_r = 1;
break;
case 0x0A:
cd_cd_rw = 1;
break;
case 0x10:
cd_dvd_rom = 1;
break;
case 0x12:
cd_dvd_ram = 1;
break;
case 0x13:
case 0x14:
cd_dvd_rw = 1;
break;
case 0x1B:
cd_dvd_plus_r = 1;
break;
case 0x1A:
cd_dvd_plus_rw = 1;
break;
case 0x2A:
cd_dvd_plus_rw_dl = 1;
break;
case 0x2B:
cd_dvd_plus_r_dl = 1;
break;
case 0x40:
cd_bd = 1;
break;
case 0x41:
case 0x42:
cd_bd_r = 1;
break;
case 0x43:
cd_bd_re = 1;
break;
case 0x50:
cd_hddvd = 1;
break;
case 0x51:
cd_hddvd_r = 1;
break;
case 0x52:
cd_hddvd_rw = 1;
break;
default:
break;
}
}
return 0;
}
/* returns 0 if media was detected */
{
int err;
unsigned char header[32];
if ((err != 0)) {
if (cd_media == 1) {
log_debug("no current profile, but disc is present; assuming CD-ROM\n");
cd_media_cd_rom = 1;
cd_media_track_count = 1;
return 0;
} else {
log_debug("no current profile, assuming no media\n");
return -1;
}
};
cd_media = 1;
cd_media_cd_rw = 1;
log_debug("profile 0x0a media_cd_rw\n");
cd_media_cd_r = 1;
log_debug("profile 0x09 media_cd_r\n");
} else {
cd_media_cd_rom = 1;
log_debug("profile 0x08 media_cd_rom\n");
}
return 0;
}
/* returns 0 if media was detected */
{
unsigned char features[65530];
unsigned int cur_profile = 0;
unsigned int len;
unsigned int i;
int err;
int ret;
ret = -1;
/* First query the current profile */
if ((err != 0)) {
/* handle pre-MMC2 drives which do not support GET CONFIGURATION */
log_debug("drive is pre-MMC2 and does not support 46h get configuration command\n");
log_debug("trying to work around the problem\n");
}
goto out;
}
if (cur_profile > 0) {
ret = 0; /* we have media */
} else {
log_debug("no current profile, assuming no media\n");
}
log_debug("can not get features in a single query, truncating\n");
} else if (len <= 8) {
}
/* Now get the full feature buffer */
if ((err != 0)) {
return -1;
}
/* parse the length once more, in case the drive decided to have other features suddenly :) */
log_debug("can not get features in a single query, truncating\n");
}
/* device features */
unsigned int feature;
switch (feature) {
case 0x00:
break;
default:
log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
break;
}
}
out:
return ret;
}
{
unsigned char header[32];
static const char *media_status[] = {
"blank",
"appendable",
"complete",
"other"
};
int err;
if ((err != 0)) {
return -1;
};
cd_media = 1;
/* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
if (!cd_media_cd_rom)
/* fresh DVD-RW in restricted overwite mode reports itself as
* "appendable"; change it to "blank" to make it consistent with what
* gets reported after blanking, and what userspace expects */
cd_media_state = media_status[0];
/* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
* always "complete", DVD-RAM are "other" or "complete" if the disc is
* write protected; we need to check the contents if it is blank */
if ((cd_media_dvd_rw_ro || cd_media_dvd_plus_rw || cd_media_dvd_plus_rw_dl || cd_media_dvd_ram) && (header[2] & 3) > 1) {
if (cd_media_dvd_ram) {
/* a write protected dvd-ram may report "complete" status */
unsigned char dvdstruct[8];
unsigned char format[12];
if ((err != 0)) {
return -1;
}
log_debug("write-protected DVD-RAM media inserted\n");
goto determined;
}
/* let's make sure we don't try to read unformatted media */
if ((err != 0)) {
return -1;
}
log_debug("invalid format capacities length\n");
return -1;
}
case 1:
log_debug("unformatted DVD-RAM media inserted\n");
/* This means that last format was interrupted
* or failed, blank dvd-ram discs are factory
* formatted. Take no action here as it takes
* quite a while to reformat a dvd-ram and it's
* not automatically started */
goto determined;
case 2:
log_debug("formatted DVD-RAM media inserted\n");
break;
case 3:
cd_media = 0; //return no media
log_debug("format capacities returned no media\n");
return -1;
}
}
/* Take a closer look at formatted media (unformatted DVD+RW
* has "blank" status", DVD-RAM was examined earlier) and check
* for ISO and UDF PVDs or a fs superblock presence and do it
* in one ioctl (we need just sectors 0 and 16) */
if ((err != 0)) {
cd_media = 0;
return -1;
}
/* if any non-zero data is found in sector 16 (iso and udf) or
* eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
* is assumed non-blank */
result = 0;
offset++;
}
}
if (!result) {
cd_media_state = media_status[0];
log_debug("no data in blocks 0 or 16, assuming blank\n");
} else {
log_debug("data in blocks 0 or 16, assuming complete\n");
}
}
/* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
* restricted overwrite mode can never append, only in sequential mode */
return 0;
}
{
unsigned char header[12];
unsigned char toc[65536];
unsigned int len, i, num_tracks;
unsigned char *p;
int err;
if ((err != 0)) {
return -1;
}
return -1;
if (len < 2)
return -1;
/* 2: first track, 3: last track */
/* empty media has no tracks */
if (len < 8)
return 0;
if ((err != 0)) {
return -1;
}
/* Take care to not iterate beyond the last valid track as specified in
* the TOC, but also avoid going beyond the TOC length, just in case
* the last track number is invalidly large */
unsigned int block;
unsigned int is_data_track;
log_debug("track=%u info=0x%x(%s) start_block=%u\n",
if (is_data_track)
else
}
if ((err != 0)) {
return -1;
}
return 0;
}
{
{}
};
bool eject = false;
bool lock = false;
bool unlock = false;
int fd = -1;
int cnt;
int rc = 0;
goto exit;
log_open();
while (1) {
int option;
if (option == -1)
break;
switch (option) {
case 'l':
lock = true;
break;
case 'u':
unlock = true;
break;
case 'e':
eject = true;
break;
case 'd':
debug = true;
break;
case 'h':
printf("Usage: cdrom_id [options] <device>\n"
" --lock-media lock the media (to enable eject request events)\n"
" --unlock-media unlock the media\n"
" --eject-media eject the media\n"
" --debug debug to stderr\n"
" --help print this help text\n\n");
goto exit;
default:
rc = 1;
goto exit;
}
}
if (!node) {
log_error("no device\n");
rc = 1;
goto exit;
}
break;
}
if (fd < 0) {
rc = 1;
goto exit;
}
/* same data as original cdrom_id */
rc = 1;
goto exit;
}
/* check for media - don't bail if there's no media as we still need to
* to read profiles */
/* check if drive talks MMC */
goto work;
/* read drive and possibly current profile */
goto work;
/* at this point we are guaranteed to have media in the drive - find out more about it */
/* get writable media state */
work:
/* lock the media, so we enable eject button events */
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)\n");
}
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
}
if (eject) {
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
log_debug("START_STOP_UNIT (eject)\n");
}
printf("ID_CDROM=1\n");
if (cd_cd_rom)
printf("ID_CDROM_CD=1\n");
if (cd_cd_r)
printf("ID_CDROM_CD_R=1\n");
if (cd_cd_rw)
printf("ID_CDROM_CD_RW=1\n");
if (cd_dvd_rom)
printf("ID_CDROM_DVD=1\n");
if (cd_dvd_r)
printf("ID_CDROM_DVD_R=1\n");
if (cd_dvd_rw)
printf("ID_CDROM_DVD_RW=1\n");
if (cd_dvd_ram)
printf("ID_CDROM_DVD_RAM=1\n");
if (cd_dvd_plus_r)
printf("ID_CDROM_DVD_PLUS_R=1\n");
if (cd_dvd_plus_rw)
printf("ID_CDROM_DVD_PLUS_RW=1\n");
if (cd_dvd_plus_r_dl)
printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
if (cd_dvd_plus_rw_dl)
printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
if (cd_bd)
printf("ID_CDROM_BD=1\n");
if (cd_bd_r)
printf("ID_CDROM_BD_R=1\n");
if (cd_bd_re)
printf("ID_CDROM_BD_RE=1\n");
if (cd_hddvd)
printf("ID_CDROM_HDDVD=1\n");
if (cd_hddvd_r)
printf("ID_CDROM_HDDVD_R=1\n");
if (cd_hddvd_rw)
printf("ID_CDROM_HDDVD_RW=1\n");
if (cd_mo)
printf("ID_CDROM_MO=1\n");
if (cd_mrw)
printf("ID_CDROM_MRW=1\n");
if (cd_mrw_w)
printf("ID_CDROM_MRW_W=1\n");
if (cd_media)
printf("ID_CDROM_MEDIA=1\n");
if (cd_media_mo)
printf("ID_CDROM_MEDIA_MO=1\n");
if (cd_media_mrw)
printf("ID_CDROM_MEDIA_MRW=1\n");
if (cd_media_mrw_w)
printf("ID_CDROM_MEDIA_MRW_W=1\n");
if (cd_media_cd_rom)
printf("ID_CDROM_MEDIA_CD=1\n");
if (cd_media_cd_r)
printf("ID_CDROM_MEDIA_CD_R=1\n");
if (cd_media_cd_rw)
printf("ID_CDROM_MEDIA_CD_RW=1\n");
if (cd_media_dvd_rom)
printf("ID_CDROM_MEDIA_DVD=1\n");
if (cd_media_dvd_r)
printf("ID_CDROM_MEDIA_DVD_R=1\n");
if (cd_media_dvd_ram)
printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
if (cd_media_dvd_rw)
printf("ID_CDROM_MEDIA_DVD_RW=1\n");
if (cd_media_dvd_plus_r)
printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
if (cd_media_dvd_plus_rw)
printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
if (cd_media_bd)
printf("ID_CDROM_MEDIA_BD=1\n");
if (cd_media_bd_r)
printf("ID_CDROM_MEDIA_BD_R=1\n");
if (cd_media_bd_re)
printf("ID_CDROM_MEDIA_BD_RE=1\n");
if (cd_media_hddvd)
printf("ID_CDROM_MEDIA_HDDVD=1\n");
if (cd_media_hddvd_r)
printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
if (cd_media_hddvd_rw)
printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
if (cd_media_state != NULL)
if (cd_media_session_next > 0)
if (cd_media_session_count > 0)
if (cd_media_track_count > 0)
if (cd_media_track_count_audio > 0)
if (cd_media_track_count_data > 0)
exit:
if (fd >= 0)
log_close();
return rc;
}