2 * cdrom_id - optical drive and media information prober
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <linux/cdrom.h>
42 #include "libudev-private.h"
46 static void log_fn(struct udev *udev, int priority,
47 const char *file, int line, const char *fn,
48 const char *format, va_list args)
51 fprintf(stderr, "%s: ", fn);
52 vfprintf(stderr, format, args);
54 vsyslog(priority, format, args);
59 static unsigned int cd_cd_rom = 0;
60 static unsigned int cd_cd_r = 0;
61 static unsigned int cd_cd_rw = 0;
62 static unsigned int cd_dvd_rom = 0;
63 static unsigned int cd_dvd_r = 0;
64 static unsigned int cd_dvd_rw = 0;
65 static unsigned int cd_dvd_ram = 0;
66 static unsigned int cd_dvd_plus_r = 0;
67 static unsigned int cd_dvd_plus_rw = 0;
68 static unsigned int cd_dvd_plus_r_dl = 0;
69 static unsigned int cd_dvd_plus_rw_dl = 0;
70 static unsigned int cd_bd = 0;
71 static unsigned int cd_bd_r = 0;
72 static unsigned int cd_bd_re = 0;
73 static unsigned int cd_hddvd = 0;
74 static unsigned int cd_hddvd_r = 0;
75 static unsigned int cd_hddvd_rw = 0;
76 static unsigned int cd_mo = 0;
77 static unsigned int cd_mrw = 0;
78 static unsigned int cd_mrw_w = 0;
81 static unsigned int cd_media = 0;
82 static unsigned int cd_media_cd_rom = 0;
83 static unsigned int cd_media_cd_r = 0;
84 static unsigned int cd_media_cd_rw = 0;
85 static unsigned int cd_media_dvd_rom = 0;
86 static unsigned int cd_media_dvd_r = 0;
87 static unsigned int cd_media_dvd_rw = 0;
88 static unsigned int cd_media_dvd_ram = 0;
89 static unsigned int cd_media_dvd_plus_r = 0;
90 static unsigned int cd_media_dvd_plus_rw = 0;
91 static unsigned int cd_media_dvd_plus_r_dl = 0;
92 static unsigned int cd_media_dvd_plus_rw_dl = 0;
93 static unsigned int cd_media_bd = 0;
94 static unsigned int cd_media_bd_r = 0;
95 static unsigned int cd_media_bd_re = 0;
96 static unsigned int cd_media_hddvd = 0;
97 static unsigned int cd_media_hddvd_r = 0;
98 static unsigned int cd_media_hddvd_rw = 0;
99 static unsigned int cd_media_mo = 0;
100 static unsigned int cd_media_mrw = 0;
101 static unsigned int cd_media_mrw_w = 0;
103 static const char *cd_media_state = NULL;
104 static unsigned int cd_media_session_next = 0;
105 static unsigned int cd_media_session_count = 0;
106 static unsigned int cd_media_track_count = 0;
107 static unsigned int cd_media_track_count_data = 0;
108 static unsigned int cd_media_track_count_audio = 0;
109 static unsigned long long int cd_media_session_last_offset = 0;
111 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
112 #define SK(errcode) (((errcode) >> 16) & 0xF)
113 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
114 #define ASCQ(errcode) ((errcode) & 0xFF)
116 static int is_mounted(const char *device)
123 if (stat(device, &statbuf) < 0)
126 fp = fopen("/proc/self/mountinfo", "r");
129 while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
130 if (makedev(maj, min) == statbuf.st_rdev) {
139 static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
142 info(udev, "%s failed\n", cmd);
145 info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
149 struct cdrom_generic_command cgc;
151 struct request_sense s;
154 struct sg_io_hdr sg_io;
157 static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd, unsigned char *buf, size_t bufsize)
159 memset(cmd, 0x00, sizeof(struct scsi_cmd));
160 memset(buf, 0x00, bufsize);
162 cmd->cgc.sense = &cmd->_sense.s;
163 memset(&cmd->sg_io, 0, sizeof(cmd->sg_io));
164 cmd->sg_io.interface_id = 'S';
165 cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
166 cmd->sg_io.cmdp = cmd->cgc.cmd;
167 cmd->sg_io.sbp = cmd->_sense.u;
168 cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
171 static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
173 cmd->sg_io.cmd_len = i + 1;
174 cmd->cgc.cmd[i] = arg;
177 #define CHECK_CONDITION 0x01
179 static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
183 cmd->sg_io.dxferp = buf;
184 cmd->sg_io.dxfer_len = bufsize;
185 cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
186 if (ioctl(fd, SG_IO, &cmd->sg_io))
189 if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
192 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
193 ret = ERRCODE(cmd->_sense.u);
201 static int cd_capability_compat(struct udev *udev, int fd)
205 capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
206 if (capability < 0) {
207 info(udev, "CDROM_GET_CAPABILITY failed\n");
211 if (capability & CDC_CD_R)
213 if (capability & CDC_CD_RW)
215 if (capability & CDC_DVD)
217 if (capability & CDC_DVD_R)
219 if (capability & CDC_DVD_RAM)
221 if (capability & CDC_MRW)
223 if (capability & CDC_MRW_W)
228 static int cd_media_compat(struct udev *udev, int fd)
230 if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
231 info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
238 static int cd_inquiry(struct udev *udev, int fd) {
240 unsigned char inq[128];
243 scsi_cmd_init(udev, &sc, inq, sizeof(inq));
244 scsi_cmd_set(udev, &sc, 0, 0x12);
245 scsi_cmd_set(udev, &sc, 4, 36);
246 scsi_cmd_set(udev, &sc, 5, 0);
247 err = scsi_cmd_run(udev, &sc, fd, inq, 36);
249 info_scsi_cmd_err(udev, "INQUIRY", err);
253 if ((inq[0] & 0x1F) != 5) {
254 info(udev, "not an MMC unit\n");
258 info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
262 static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
266 for (i = 0; i+4 <= size; i += 4) {
269 profile = profiles[i] << 8 | profiles[i+1];
274 info(udev, "profile 0x%02x mo\n", profile);
278 info(udev, "profile 0x%02x cd_rom\n", profile);
282 info(udev, "profile 0x%02x cd_r\n", profile);
286 info(udev, "profile 0x%02x cd_rw\n", profile);
290 info(udev, "profile 0x%02x dvd_rom\n", profile);
294 info(udev, "profile 0x%02x dvd_ram\n", profile);
299 info(udev, "profile 0x%02x dvd_rw\n", profile);
303 info(udev, "profile 0x%02x dvd_plus_r\n", profile);
307 info(udev, "profile 0x%02x dvd_plus_rw\n", profile);
311 info(udev, "profile 0x%02x dvd_plus_rw_dl\n", profile);
312 cd_dvd_plus_rw_dl = 1;
315 info(udev, "profile 0x%02x dvd_plus_r_dl\n", profile);
316 cd_dvd_plus_r_dl = 1;
320 info(udev, "profile 0x%02x bd\n", profile);
325 info(udev, "profile 0x%02x bd_r\n", profile);
329 info(udev, "profile 0x%02x bd_re\n", profile);
333 info(udev, "profile 0x%02x hddvd\n", profile);
337 info(udev, "profile 0x%02x hddvd_r\n", profile);
341 info(udev, "profile 0x%02x hddvd_rw\n", profile);
344 info(udev, "profile 0x%02x <ignored>\n", profile);
351 static int cd_profiles(struct udev *udev, int fd)
354 unsigned char features[65530];
355 unsigned int cur_profile = 0;
360 scsi_cmd_init(udev, &sc, features, sizeof(features));
361 scsi_cmd_set(udev, &sc, 0, 0x46);
362 scsi_cmd_set(udev, &sc, 7, (sizeof(features) >> 8) & 0xff);
363 scsi_cmd_set(udev, &sc, 8, sizeof(features) & 0xff);
364 scsi_cmd_set(udev, &sc, 9, 0);
365 err = scsi_cmd_run(udev, &sc, fd, features, sizeof(features));
367 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
371 len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
372 info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);
374 if (len > sizeof(features)) {
375 info(udev, "can not get features in a single query, truncating\n");
376 len = sizeof(features);
379 /* device features */
380 for (i = 8; i+4 < len; i += (4 + features[i+3])) {
381 unsigned int feature;
383 feature = features[i] << 8 | features[i+1];
387 info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
388 feature_profiles(udev, &features[i]+4, features[i+3]);
391 info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
396 cur_profile = features[6] << 8 | features[7];
397 if (cur_profile > 0) {
398 info(udev, "current profile 0x%02x\n", cur_profile);
400 info(udev, "no current profile, assuming no media\n");
404 switch (cur_profile) {
408 info(udev, "profile 0x%02x \n", cur_profile);
413 info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
418 info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
423 info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
428 info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
430 cd_media_dvd_rom = 1;
433 info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
438 info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
440 cd_media_dvd_ram = 1;
444 info(udev, "profile 0x%02x media_dvd_rw\n", cur_profile);
449 info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
451 cd_media_dvd_plus_r = 1;
454 info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
456 cd_media_dvd_plus_rw = 1;
459 info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
461 cd_media_dvd_plus_rw_dl = 1;
464 info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
466 cd_media_dvd_plus_r_dl = 1;
469 info(udev, "profile 0x%02x media_bd\n", cur_profile);
475 info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
480 info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
485 info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
490 info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
492 cd_media_hddvd_r = 1;
495 info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
497 cd_media_hddvd_rw = 1;
500 info(udev, "profile 0x%02x <ignored>\n", cur_profile);
506 static int cd_media_info(struct udev *udev, int fd)
509 unsigned char header[32];
510 static const char *media_status[] = {
518 scsi_cmd_init(udev, &sc, header, sizeof(header));
519 scsi_cmd_set(udev, &sc, 0, 0x51);
520 scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
521 scsi_cmd_set(udev, &sc, 9, 0);
522 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
524 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
529 info(udev, "disk type %02x\n", header[8]);
531 /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
532 if (!cd_media_cd_rom)
533 cd_media_state = media_status[header[2] & 3];
535 if ((header[2] & 3) != 2)
536 cd_media_session_next = header[10] << 8 | header[5];
537 cd_media_session_count = header[9] << 8 | header[4];
538 cd_media_track_count = header[11] << 8 | header[6];
543 static int cd_media_toc(struct udev *udev, int fd)
546 unsigned char header[12];
547 unsigned char toc[2048];
552 scsi_cmd_init(udev, &sc, header, sizeof(header));
553 scsi_cmd_set(udev, &sc, 0, 0x43);
554 scsi_cmd_set(udev, &sc, 6, 1);
555 scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
556 scsi_cmd_set(udev, &sc, 9, 0);
557 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
559 info_scsi_cmd_err(udev, "READ TOC", err);
563 len = (header[0] << 8 | header[1]) + 2;
564 info(udev, "READ TOC: len: %d\n", len);
565 if (len > sizeof(toc))
570 /* empty media has no tracks */
574 scsi_cmd_init(udev, &sc, toc, sizeof(toc));
575 scsi_cmd_set(udev, &sc, 0, 0x43);
576 scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
577 scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
578 scsi_cmd_set(udev, &sc, 8, len & 0xff);
579 scsi_cmd_set(udev, &sc, 9, 0);
580 err = scsi_cmd_run(udev, &sc, fd, toc, len);
582 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
586 for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
588 unsigned int is_data_track;
590 is_data_track = (p[1] & 0x04) != 0;
592 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
593 info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
594 p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
597 cd_media_track_count_data++;
599 cd_media_track_count_audio++;
602 scsi_cmd_init(udev, &sc, header, sizeof(header));
603 scsi_cmd_set(udev, &sc, 0, 0x43);
604 scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
605 scsi_cmd_set(udev, &sc, 8, sizeof(header));
606 scsi_cmd_set(udev, &sc, 9, 0);
607 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
609 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
612 len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
613 info(udev, "last track %u starts at block %u\n", header[4+2], len);
614 cd_media_session_last_offset = (unsigned long long int)len * 2048;
618 int main(int argc, char *argv[])
621 static const struct option options[] = {
622 { "export", no_argument, NULL, 'x' },
623 { "debug", no_argument, NULL, 'd' },
624 { "help", no_argument, NULL, 'h' },
627 const char *node = NULL;
637 udev_log_init("cdrom_id");
638 udev_set_log_fn(udev, log_fn);
643 option = getopt_long(argc, argv, "dxh", options, NULL);
650 if (udev_get_log_priority(udev) < LOG_INFO)
651 udev_set_log_priority(udev, LOG_INFO);
657 printf("Usage: cdrom_id [options] <device>\n"
658 " --export export key/value pairs\n"
659 " --debug debug to stderr\n"
660 " --help print this help text\n\n");
670 err(udev, "no device\n");
671 fprintf(stderr, "no device\n");
676 srand((unsigned int)getpid());
677 for (cnt = 20; cnt > 0; cnt--) {
678 struct timespec duration;
680 fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
681 if (fd >= 0 || errno != EBUSY)
684 duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
685 nanosleep(&duration, NULL);
688 info(udev, "unable to open '%s'\n", node);
689 fprintf(stderr, "unable to open '%s'\n", node);
693 info(udev, "probing: '%s'\n", node);
695 /* same data as original cdrom_id */
696 if (cd_capability_compat(udev, fd) < 0) {
701 /* check for media - don't bail if there's no media as we still need to
702 * to read profiles */
703 cd_media_compat(udev, fd);
705 /* check if drive talks MMC */
706 if (cd_inquiry(udev, fd) < 0)
709 /* read drive and possibly current profile */
710 if (cd_profiles(udev, fd) < 0)
713 /* get writable media state */
714 if (cd_media_info(udev, fd) < 0)
717 /* get session/track info */
718 if (cd_media_toc(udev, fd) < 0)
722 printf("ID_CDROM=1\n");
724 printf("ID_CDROM_CD=1\n");
726 printf("ID_CDROM_CD_R=1\n");
728 printf("ID_CDROM_CD_RW=1\n");
730 printf("ID_CDROM_DVD=1\n");
732 printf("ID_CDROM_DVD_R=1\n");
734 printf("ID_CDROM_DVD_RW=1\n");
736 printf("ID_CDROM_DVD_RAM=1\n");
738 printf("ID_CDROM_DVD_PLUS_R=1\n");
740 printf("ID_CDROM_DVD_PLUS_RW=1\n");
741 if (cd_dvd_plus_r_dl)
742 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
743 if (cd_dvd_plus_rw_dl)
744 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
746 printf("ID_CDROM_BD=1\n");
748 printf("ID_CDROM_BD_R=1\n");
750 printf("ID_CDROM_BD_RE=1\n");
752 printf("ID_CDROM_HDDVD=1\n");
754 printf("ID_CDROM_HDDVD_R=1\n");
756 printf("ID_CDROM_HDDVD_RW=1\n");
758 printf("ID_CDROM_MO=1\n");
760 printf("ID_CDROM_MRW=1\n");
762 printf("ID_CDROM_MRW_W=1\n");
765 printf("ID_CDROM_MEDIA=1\n");
767 printf("ID_CDROM_MEDIA_MO=1\n");
769 printf("ID_CDROM_MEDIA_MRW=1\n");
771 printf("ID_CDROM_MEDIA_MRW_W=1\n");
773 printf("ID_CDROM_MEDIA_CD=1\n");
775 printf("ID_CDROM_MEDIA_CD_R=1\n");
777 printf("ID_CDROM_MEDIA_CD_RW=1\n");
778 if (cd_media_dvd_rom)
779 printf("ID_CDROM_MEDIA_DVD=1\n");
781 printf("ID_CDROM_MEDIA_DVD_R=1\n");
782 if (cd_media_dvd_ram)
783 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
785 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
786 if (cd_media_dvd_plus_r)
787 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
788 if (cd_media_dvd_plus_rw)
789 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
790 if (cd_media_dvd_plus_rw_dl)
791 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
792 if (cd_media_dvd_plus_r_dl)
793 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
795 printf("ID_CDROM_MEDIA_BD=1\n");
797 printf("ID_CDROM_MEDIA_BD_R=1\n");
799 printf("ID_CDROM_MEDIA_BD_RE=1\n");
801 printf("ID_CDROM_MEDIA_HDDVD=1\n");
802 if (cd_media_hddvd_r)
803 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
804 if (cd_media_hddvd_rw)
805 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
807 if (cd_media_state != NULL)
808 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
809 if (cd_media_session_next > 0)
810 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
811 if (cd_media_session_count > 0)
812 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
813 if (cd_media_track_count > 0)
814 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
815 if (cd_media_track_count_audio > 0)
816 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
817 if (cd_media_track_count_data > 0)
818 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
819 if (cd_media_session_last_offset > 0)
820 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);