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
;
60 static unsigned int cd_cd_r
;
61 static unsigned int cd_cd_rw
;
62 static unsigned int cd_dvd_rom
;
63 static unsigned int cd_dvd_r
;
64 static unsigned int cd_dvd_rw
;
65 static unsigned int cd_dvd_ram
;
66 static unsigned int cd_dvd_plus_r
;
67 static unsigned int cd_dvd_plus_rw
;
68 static unsigned int cd_dvd_plus_r_dl
;
69 static unsigned int cd_dvd_plus_rw_dl
;
70 static unsigned int cd_bd
;
71 static unsigned int cd_bd_r
;
72 static unsigned int cd_bd_re
;
73 static unsigned int cd_hddvd
;
74 static unsigned int cd_hddvd_r
;
75 static unsigned int cd_hddvd_rw
;
76 static unsigned int cd_mo
;
77 static unsigned int cd_mrw
;
78 static unsigned int cd_mrw_w
;
81 static unsigned int cd_media
;
82 static unsigned int cd_media_cd_rom
;
83 static unsigned int cd_media_cd_r
;
84 static unsigned int cd_media_cd_rw
;
85 static unsigned int cd_media_dvd_rom
;
86 static unsigned int cd_media_dvd_r
;
87 static unsigned int cd_media_dvd_rw
;
88 static unsigned int cd_media_dvd_rw_ro
; /* restricted overwrite mode */
89 static unsigned int cd_media_dvd_rw_seq
; /* sequential mode */
90 static unsigned int cd_media_dvd_ram
;
91 static unsigned int cd_media_dvd_plus_r
;
92 static unsigned int cd_media_dvd_plus_rw
;
93 static unsigned int cd_media_dvd_plus_r_dl
;
94 static unsigned int cd_media_dvd_plus_rw_dl
;
95 static unsigned int cd_media_bd
;
96 static unsigned int cd_media_bd_r
;
97 static unsigned int cd_media_bd_re
;
98 static unsigned int cd_media_hddvd
;
99 static unsigned int cd_media_hddvd_r
;
100 static unsigned int cd_media_hddvd_rw
;
101 static unsigned int cd_media_mo
;
102 static unsigned int cd_media_mrw
;
103 static unsigned int cd_media_mrw_w
;
105 static const char *cd_media_state
= NULL
;
106 static unsigned int cd_media_session_next
;
107 static unsigned int cd_media_session_count
;
108 static unsigned int cd_media_track_count
;
109 static unsigned int cd_media_track_count_data
;
110 static unsigned int cd_media_track_count_audio
;
111 static unsigned long long int cd_media_session_last_offset
;
113 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
114 #define SK(errcode) (((errcode) >> 16) & 0xF)
115 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
116 #define ASCQ(errcode) ((errcode) & 0xFF)
118 static bool is_mounted(const char *device
)
123 bool mounted
= false;
125 if (stat(device
, &statbuf
) < 0)
128 fp
= fopen("/proc/self/mountinfo", "re");
131 while (fscanf(fp
, "%*s %*s %i:%i %*[^\n]", &maj
, &min
) == 2) {
132 if (makedev(maj
, min
) == statbuf
.st_rdev
) {
141 static void info_scsi_cmd_err(struct udev
*udev
, const char *cmd
, int err
)
144 log_debug("%s failed\n", cmd
);
147 log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd
, SK(err
), ASC(err
), ASCQ(err
));
151 struct cdrom_generic_command cgc
;
153 struct request_sense s
;
156 struct sg_io_hdr sg_io
;
159 static void scsi_cmd_init(struct udev
*udev
, struct scsi_cmd
*cmd
)
161 memset(cmd
, 0x00, sizeof(struct scsi_cmd
));
163 cmd
->cgc
.sense
= &cmd
->_sense
.s
;
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
)
184 cmd
->sg_io
.dxferp
= buf
;
185 cmd
->sg_io
.dxfer_len
= bufsize
;
186 cmd
->sg_io
.dxfer_direction
= SG_DXFER_FROM_DEV
;
188 cmd
->sg_io
.dxfer_direction
= SG_DXFER_NONE
;
190 if (ioctl(fd
, SG_IO
, &cmd
->sg_io
))
193 if ((cmd
->sg_io
.info
& SG_INFO_OK_MASK
) != SG_INFO_OK
) {
196 if (cmd
->sg_io
.masked_status
& CHECK_CONDITION
) {
197 ret
= ERRCODE(cmd
->_sense
.u
);
205 static int media_lock(struct udev
*udev
, int fd
, bool lock
)
209 /* disable the kernel's lock logic */
210 err
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_LOCK
);
212 log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed\n");
214 err
= ioctl(fd
, CDROM_LOCKDOOR
, lock
? 1 : 0);
216 log_debug("CDROM_LOCKDOOR failed\n");
221 static int media_eject(struct udev
*udev
, int fd
)
226 scsi_cmd_init(udev
, &sc
);
227 scsi_cmd_set(udev
, &sc
, 0, 0x1b);
228 scsi_cmd_set(udev
, &sc
, 4, 0x02);
229 scsi_cmd_set(udev
, &sc
, 5, 0);
230 err
= scsi_cmd_run(udev
, &sc
, fd
, NULL
, 0);
232 info_scsi_cmd_err(udev
, "START_STOP_UNIT", err
);
238 static int cd_capability_compat(struct udev
*udev
, int fd
)
242 capability
= ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
);
243 if (capability
< 0) {
244 log_debug("CDROM_GET_CAPABILITY failed\n");
248 if (capability
& CDC_CD_R
)
250 if (capability
& CDC_CD_RW
)
252 if (capability
& CDC_DVD
)
254 if (capability
& CDC_DVD_R
)
256 if (capability
& CDC_DVD_RAM
)
258 if (capability
& CDC_MRW
)
260 if (capability
& CDC_MRW_W
)
265 static int cd_media_compat(struct udev
*udev
, int fd
)
267 if (ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
) != CDS_DISC_OK
) {
268 log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
275 static int cd_inquiry(struct udev
*udev
, int fd
)
278 unsigned char inq
[128];
281 scsi_cmd_init(udev
, &sc
);
282 scsi_cmd_set(udev
, &sc
, 0, 0x12);
283 scsi_cmd_set(udev
, &sc
, 4, 36);
284 scsi_cmd_set(udev
, &sc
, 5, 0);
285 err
= scsi_cmd_run(udev
, &sc
, fd
, inq
, 36);
287 info_scsi_cmd_err(udev
, "INQUIRY", err
);
291 if ((inq
[0] & 0x1F) != 5) {
292 log_debug("not an MMC unit\n");
296 log_debug("INQUIRY: [%.8s][%.16s][%.4s]\n", inq
+ 8, inq
+ 16, inq
+ 32);
300 static void feature_profile_media(struct udev
*udev
, int cur_profile
)
302 switch (cur_profile
) {
306 log_debug("profile 0x%02x \n", cur_profile
);
311 log_debug("profile 0x%02x media_cd_rom\n", cur_profile
);
316 log_debug("profile 0x%02x media_cd_r\n", cur_profile
);
321 log_debug("profile 0x%02x media_cd_rw\n", cur_profile
);
326 log_debug("profile 0x%02x media_dvd_ro\n", cur_profile
);
328 cd_media_dvd_rom
= 1;
331 log_debug("profile 0x%02x media_dvd_r\n", cur_profile
);
336 log_debug("profile 0x%02x media_dvd_ram\n", cur_profile
);
338 cd_media_dvd_ram
= 1;
341 log_debug("profile 0x%02x media_dvd_rw_ro\n", cur_profile
);
344 cd_media_dvd_rw_ro
= 1;
347 log_debug("profile 0x%02x media_dvd_rw_seq\n", cur_profile
);
350 cd_media_dvd_rw_seq
= 1;
353 log_debug("profile 0x%02x media_dvd_plus_r\n", cur_profile
);
355 cd_media_dvd_plus_r
= 1;
358 log_debug("profile 0x%02x media_dvd_plus_rw\n", cur_profile
);
360 cd_media_dvd_plus_rw
= 1;
363 log_debug("profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile
);
365 cd_media_dvd_plus_rw_dl
= 1;
368 log_debug("profile 0x%02x media_dvd_plus_r_dl\n", cur_profile
);
370 cd_media_dvd_plus_r_dl
= 1;
373 log_debug("profile 0x%02x media_bd\n", cur_profile
);
379 log_debug("profile 0x%02x media_bd_r\n", cur_profile
);
384 log_debug("profile 0x%02x media_bd_re\n", cur_profile
);
389 log_debug("profile 0x%02x media_hddvd\n", cur_profile
);
394 log_debug("profile 0x%02x media_hddvd_r\n", cur_profile
);
396 cd_media_hddvd_r
= 1;
399 log_debug("profile 0x%02x media_hddvd_rw\n", cur_profile
);
401 cd_media_hddvd_rw
= 1;
404 log_debug("profile 0x%02x <ignored>\n", cur_profile
);
409 static int feature_profiles(struct udev
*udev
, const unsigned char *profiles
, size_t size
)
413 for (i
= 0; i
+4 <= size
; i
+= 4) {
416 profile
= profiles
[i
] << 8 | profiles
[i
+1];
421 log_debug("profile 0x%02x mo\n", profile
);
425 log_debug("profile 0x%02x cd_rom\n", profile
);
429 log_debug("profile 0x%02x cd_r\n", profile
);
433 log_debug("profile 0x%02x cd_rw\n", profile
);
437 log_debug("profile 0x%02x dvd_rom\n", profile
);
441 log_debug("profile 0x%02x dvd_ram\n", profile
);
446 log_debug("profile 0x%02x dvd_rw\n", profile
);
450 log_debug("profile 0x%02x dvd_plus_r\n", profile
);
454 log_debug("profile 0x%02x dvd_plus_rw\n", profile
);
458 log_debug("profile 0x%02x dvd_plus_rw_dl\n", profile
);
459 cd_dvd_plus_rw_dl
= 1;
462 log_debug("profile 0x%02x dvd_plus_r_dl\n", profile
);
463 cd_dvd_plus_r_dl
= 1;
467 log_debug("profile 0x%02x bd\n", profile
);
472 log_debug("profile 0x%02x bd_r\n", profile
);
476 log_debug("profile 0x%02x bd_re\n", profile
);
480 log_debug("profile 0x%02x hddvd\n", profile
);
484 log_debug("profile 0x%02x hddvd_r\n", profile
);
488 log_debug("profile 0x%02x hddvd_rw\n", profile
);
491 log_debug("profile 0x%02x <ignored>\n", profile
);
498 /* returns 0 if media was detected */
499 static int cd_profiles_old_mmc(struct udev
*udev
, int fd
)
504 unsigned char header
[32];
506 scsi_cmd_init(udev
, &sc
);
507 scsi_cmd_set(udev
, &sc
, 0, 0x51);
508 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
509 scsi_cmd_set(udev
, &sc
, 9, 0);
510 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
512 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
514 log_debug("no current profile, but disc is present; assuming CD-ROM\n");
518 log_debug("no current profile, assuming no media\n");
525 if (header
[2] & 16) {
527 log_debug("profile 0x0a media_cd_rw\n");
528 } else if ((header
[2] & 3) < 2 && cd_cd_r
) {
530 log_debug("profile 0x09 media_cd_r\n");
533 log_debug("profile 0x08 media_cd_rom\n");
538 /* returns 0 if media was detected */
539 static int cd_profiles(struct udev
*udev
, int fd
)
542 unsigned char features
[65530];
543 unsigned int cur_profile
= 0;
551 /* First query the current profile */
552 scsi_cmd_init(udev
, &sc
);
553 scsi_cmd_set(udev
, &sc
, 0, 0x46);
554 scsi_cmd_set(udev
, &sc
, 8, 8);
555 scsi_cmd_set(udev
, &sc
, 9, 0);
556 err
= scsi_cmd_run(udev
, &sc
, fd
, features
, 8);
558 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
559 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
560 if (SK(err
) == 0x5 && ASC(err
) == 0x20) {
561 log_debug("drive is pre-MMC2 and does not support 46h get configuration command\n");
562 log_debug("trying to work around the problem\n");
563 ret
= cd_profiles_old_mmc(udev
, fd
);
568 cur_profile
= features
[6] << 8 | features
[7];
569 if (cur_profile
> 0) {
570 log_debug("current profile 0x%02x\n", cur_profile
);
571 feature_profile_media (udev
, cur_profile
);
572 ret
= 0; /* we have media */
574 log_debug("no current profile, assuming no media\n");
577 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
578 log_debug("GET CONFIGURATION: size of features buffer 0x%04x\n", len
);
580 if (len
> sizeof(features
)) {
581 log_debug("can not get features in a single query, truncating\n");
582 len
= sizeof(features
);
583 } else if (len
<= 8) {
584 len
= sizeof(features
);
587 /* Now get the full feature buffer */
588 scsi_cmd_init(udev
, &sc
);
589 scsi_cmd_set(udev
, &sc
, 0, 0x46);
590 scsi_cmd_set(udev
, &sc
, 7, ( len
>> 8 ) & 0xff);
591 scsi_cmd_set(udev
, &sc
, 8, len
& 0xff);
592 scsi_cmd_set(udev
, &sc
, 9, 0);
593 err
= scsi_cmd_run(udev
, &sc
, fd
, features
, len
);
595 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
599 /* parse the length once more, in case the drive decided to have other features suddenly :) */
600 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
601 log_debug("GET CONFIGURATION: size of features buffer 0x%04x\n", len
);
603 if (len
> sizeof(features
)) {
604 log_debug("can not get features in a single query, truncating\n");
605 len
= sizeof(features
);
608 /* device features */
609 for (i
= 8; i
+4 < len
; i
+= (4 + features
[i
+3])) {
610 unsigned int feature
;
612 feature
= features
[i
] << 8 | features
[i
+1];
616 log_debug("GET CONFIGURATION: feature 'profiles', with %i entries\n", features
[i
+3] / 4);
617 feature_profiles(udev
, &features
[i
]+4, features
[i
+3]);
620 log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature
, features
[i
+3]);
628 static int cd_media_info(struct udev
*udev
, int fd
)
631 unsigned char header
[32];
632 static const char *media_status
[] = {
640 scsi_cmd_init(udev
, &sc
);
641 scsi_cmd_set(udev
, &sc
, 0, 0x51);
642 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
643 scsi_cmd_set(udev
, &sc
, 9, 0);
644 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
646 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
651 log_debug("disk type %02x\n", header
[8]);
652 log_debug("hardware reported media status: %s\n", media_status
[header
[2] & 3]);
654 /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
655 if (!cd_media_cd_rom
)
656 cd_media_state
= media_status
[header
[2] & 3];
658 /* fresh DVD-RW in restricted overwite mode reports itself as
659 * "appendable"; change it to "blank" to make it consistent with what
660 * gets reported after blanking, and what userspace expects */
661 if (cd_media_dvd_rw_ro
&& (header
[2] & 3) == 1)
662 cd_media_state
= media_status
[0];
664 /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
665 * always "complete", DVD-RAM are "other" or "complete" if the disc is
666 * write protected; we need to check the contents if it is blank */
667 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) {
668 unsigned char buffer
[32 * 2048];
669 unsigned char result
, len
;
672 if (cd_media_dvd_ram
) {
673 /* a write protected dvd-ram may report "complete" status */
675 unsigned char dvdstruct
[8];
676 unsigned char format
[12];
678 scsi_cmd_init(udev
, &sc
);
679 scsi_cmd_set(udev
, &sc
, 0, 0xAD);
680 scsi_cmd_set(udev
, &sc
, 7, 0xC0);
681 scsi_cmd_set(udev
, &sc
, 9, sizeof(dvdstruct
));
682 scsi_cmd_set(udev
, &sc
, 11, 0);
683 err
= scsi_cmd_run(udev
, &sc
, fd
, dvdstruct
, sizeof(dvdstruct
));
685 info_scsi_cmd_err(udev
, "READ DVD STRUCTURE", err
);
688 if (dvdstruct
[4] & 0x02) {
689 cd_media_state
= media_status
[2];
690 log_debug("write-protected DVD-RAM media inserted\n");
694 /* let's make sure we don't try to read unformatted media */
695 scsi_cmd_init(udev
, &sc
);
696 scsi_cmd_set(udev
, &sc
, 0, 0x23);
697 scsi_cmd_set(udev
, &sc
, 8, sizeof(format
));
698 scsi_cmd_set(udev
, &sc
, 9, 0);
699 err
= scsi_cmd_run(udev
, &sc
, fd
, format
, sizeof(format
));
701 info_scsi_cmd_err(udev
, "READ DVD FORMAT CAPACITIES", err
);
706 if (len
& 7 || len
< 16) {
707 log_debug("invalid format capacities length\n");
711 switch(format
[8] & 3) {
713 log_debug("unformatted DVD-RAM media inserted\n");
714 /* This means that last format was interrupted
715 * or failed, blank dvd-ram discs are factory
716 * formatted. Take no action here as it takes
717 * quite a while to reformat a dvd-ram and it's
718 * not automatically started */
722 log_debug("formatted DVD-RAM media inserted\n");
726 cd_media
= 0; //return no media
727 log_debug("format capacities returned no media\n");
732 /* Take a closer look at formatted media (unformatted DVD+RW
733 * has "blank" status", DVD-RAM was examined earlier) and check
734 * for ISO and UDF PVDs or a fs superblock presence and do it
735 * in one ioctl (we need just sectors 0 and 16) */
736 scsi_cmd_init(udev
, &sc
);
737 scsi_cmd_set(udev
, &sc
, 0, 0x28);
738 scsi_cmd_set(udev
, &sc
, 5, 0);
739 scsi_cmd_set(udev
, &sc
, 8, 32);
740 scsi_cmd_set(udev
, &sc
, 9, 0);
741 err
= scsi_cmd_run(udev
, &sc
, fd
, buffer
, sizeof(buffer
));
744 info_scsi_cmd_err(udev
, "READ FIRST 32 BLOCKS", err
);
748 /* if any non-zero data is found in sector 16 (iso and udf) or
749 * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
750 * is assumed non-blank */
753 for (block
= 32768; block
>= 0 && !result
; block
-= 32768) {
755 while (offset
< (block
+ 2048) && !result
) {
756 result
= buffer
[offset
];
762 cd_media_state
= media_status
[0];
763 log_debug("no data in blocks 0 or 16, assuming blank\n");
765 log_debug("data in blocks 0 or 16, assuming complete\n");
770 /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
771 * restricted overwrite mode can never append, only in sequential mode */
772 if ((header
[2] & 3) < 2 && !cd_media_dvd_rw_ro
)
773 cd_media_session_next
= header
[10] << 8 | header
[5];
774 cd_media_session_count
= header
[9] << 8 | header
[4];
775 cd_media_track_count
= header
[11] << 8 | header
[6];
780 static int cd_media_toc(struct udev
*udev
, int fd
)
783 unsigned char header
[12];
784 unsigned char toc
[65536];
785 unsigned int len
, i
, num_tracks
;
789 scsi_cmd_init(udev
, &sc
);
790 scsi_cmd_set(udev
, &sc
, 0, 0x43);
791 scsi_cmd_set(udev
, &sc
, 6, 1);
792 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
793 scsi_cmd_set(udev
, &sc
, 9, 0);
794 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
796 info_scsi_cmd_err(udev
, "READ TOC", err
);
800 len
= (header
[0] << 8 | header
[1]) + 2;
801 log_debug("READ TOC: len: %d, start track: %d, end track: %d\n", len
, header
[2], header
[3]);
802 if (len
> sizeof(toc
))
806 /* 2: first track, 3: last track */
807 num_tracks
= header
[3] - header
[2] + 1;
809 /* empty media has no tracks */
813 scsi_cmd_init(udev
, &sc
);
814 scsi_cmd_set(udev
, &sc
, 0, 0x43);
815 scsi_cmd_set(udev
, &sc
, 6, header
[2]); /* First Track/Session Number */
816 scsi_cmd_set(udev
, &sc
, 7, (len
>> 8) & 0xff);
817 scsi_cmd_set(udev
, &sc
, 8, len
& 0xff);
818 scsi_cmd_set(udev
, &sc
, 9, 0);
819 err
= scsi_cmd_run(udev
, &sc
, fd
, toc
, len
);
821 info_scsi_cmd_err(udev
, "READ TOC (tracks)", err
);
825 /* Take care to not iterate beyond the last valid track as specified in
826 * the TOC, but also avoid going beyond the TOC length, just in case
827 * the last track number is invalidly large */
828 for (p
= toc
+4, i
= 4; i
< len
-8 && num_tracks
> 0; i
+= 8, p
+= 8, --num_tracks
) {
830 unsigned int is_data_track
;
832 is_data_track
= (p
[1] & 0x04) != 0;
834 block
= p
[4] << 24 | p
[5] << 16 | p
[6] << 8 | p
[7];
835 log_debug("track=%u info=0x%x(%s) start_block=%u\n",
836 p
[2], p
[1] & 0x0f, is_data_track
? "data":"audio", block
);
839 cd_media_track_count_data
++;
841 cd_media_track_count_audio
++;
844 scsi_cmd_init(udev
, &sc
);
845 scsi_cmd_set(udev
, &sc
, 0, 0x43);
846 scsi_cmd_set(udev
, &sc
, 2, 1); /* Session Info */
847 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
848 scsi_cmd_set(udev
, &sc
, 9, 0);
849 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
851 info_scsi_cmd_err(udev
, "READ TOC (multi session)", err
);
854 len
= header
[4+4] << 24 | header
[4+5] << 16 | header
[4+6] << 8 | header
[4+7];
855 log_debug("last track %u starts at block %u\n", header
[4+2], len
);
856 cd_media_session_last_offset
= (unsigned long long int)len
* 2048;
860 int main(int argc
, char *argv
[])
863 static const struct option options
[] = {
864 { "lock-media", no_argument
, NULL
, 'l' },
865 { "unlock-media", no_argument
, NULL
, 'u' },
866 { "eject-media", no_argument
, NULL
, 'e' },
867 { "debug", no_argument
, NULL
, 'd' },
868 { "help", no_argument
, NULL
, 'h' },
874 const char *node
= NULL
;
884 udev_set_log_fn(udev
, log_fn
);
889 option
= getopt_long(argc
, argv
, "deluh", options
, NULL
);
905 log_set_max_level(LOG_DEBUG
);
906 udev_set_log_priority(udev
, LOG_DEBUG
);
909 printf("Usage: cdrom_id [options] <device>\n"
910 " --lock-media lock the media (to enable eject request events)\n"
911 " --unlock-media unlock the media\n"
912 " --eject-media eject the media\n"
913 " --debug debug to stderr\n"
914 " --help print this help text\n\n");
924 log_error("no device\n");
925 fprintf(stderr
, "no device\n");
930 srand((unsigned int)getpid());
931 for (cnt
= 20; cnt
> 0; cnt
--) {
932 struct timespec duration
;
934 fd
= open(node
, O_RDONLY
|O_NONBLOCK
|(is_mounted(node
) ? 0 : O_EXCL
));
935 if (fd
>= 0 || errno
!= EBUSY
)
938 duration
.tv_nsec
= (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
939 nanosleep(&duration
, NULL
);
942 log_debug("unable to open '%s'\n", node
);
943 fprintf(stderr
, "unable to open '%s'\n", node
);
947 log_debug("probing: '%s'\n", node
);
949 /* same data as original cdrom_id */
950 if (cd_capability_compat(udev
, fd
) < 0) {
955 /* check for media - don't bail if there's no media as we still need to
956 * to read profiles */
957 cd_media_compat(udev
, fd
);
959 /* check if drive talks MMC */
960 if (cd_inquiry(udev
, fd
) < 0)
963 /* read drive and possibly current profile */
964 if (cd_profiles(udev
, fd
) != 0)
967 /* at this point we are guaranteed to have media in the drive - find out more about it */
969 /* get session/track info */
970 cd_media_toc(udev
, fd
);
972 /* get writable media state */
973 cd_media_info(udev
, fd
);
976 /* lock the media, so we enable eject button events */
977 if (lock
&& cd_media
) {
978 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)\n");
979 media_lock(udev
, fd
, true);
982 if (unlock
&& cd_media
) {
983 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
984 media_lock(udev
, fd
, false);
988 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
989 media_lock(udev
, fd
, false);
990 log_debug("START_STOP_UNIT (eject)\n");
991 media_eject(udev
, fd
);
994 printf("ID_CDROM=1\n");
996 printf("ID_CDROM_CD=1\n");
998 printf("ID_CDROM_CD_R=1\n");
1000 printf("ID_CDROM_CD_RW=1\n");
1002 printf("ID_CDROM_DVD=1\n");
1004 printf("ID_CDROM_DVD_R=1\n");
1006 printf("ID_CDROM_DVD_RW=1\n");
1008 printf("ID_CDROM_DVD_RAM=1\n");
1010 printf("ID_CDROM_DVD_PLUS_R=1\n");
1012 printf("ID_CDROM_DVD_PLUS_RW=1\n");
1013 if (cd_dvd_plus_r_dl
)
1014 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
1015 if (cd_dvd_plus_rw_dl
)
1016 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
1018 printf("ID_CDROM_BD=1\n");
1020 printf("ID_CDROM_BD_R=1\n");
1022 printf("ID_CDROM_BD_RE=1\n");
1024 printf("ID_CDROM_HDDVD=1\n");
1026 printf("ID_CDROM_HDDVD_R=1\n");
1028 printf("ID_CDROM_HDDVD_RW=1\n");
1030 printf("ID_CDROM_MO=1\n");
1032 printf("ID_CDROM_MRW=1\n");
1034 printf("ID_CDROM_MRW_W=1\n");
1037 printf("ID_CDROM_MEDIA=1\n");
1039 printf("ID_CDROM_MEDIA_MO=1\n");
1041 printf("ID_CDROM_MEDIA_MRW=1\n");
1043 printf("ID_CDROM_MEDIA_MRW_W=1\n");
1044 if (cd_media_cd_rom
)
1045 printf("ID_CDROM_MEDIA_CD=1\n");
1047 printf("ID_CDROM_MEDIA_CD_R=1\n");
1049 printf("ID_CDROM_MEDIA_CD_RW=1\n");
1050 if (cd_media_dvd_rom
)
1051 printf("ID_CDROM_MEDIA_DVD=1\n");
1053 printf("ID_CDROM_MEDIA_DVD_R=1\n");
1054 if (cd_media_dvd_ram
)
1055 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
1056 if (cd_media_dvd_rw
)
1057 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
1058 if (cd_media_dvd_plus_r
)
1059 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
1060 if (cd_media_dvd_plus_rw
)
1061 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
1062 if (cd_media_dvd_plus_rw_dl
)
1063 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
1064 if (cd_media_dvd_plus_r_dl
)
1065 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
1067 printf("ID_CDROM_MEDIA_BD=1\n");
1069 printf("ID_CDROM_MEDIA_BD_R=1\n");
1071 printf("ID_CDROM_MEDIA_BD_RE=1\n");
1073 printf("ID_CDROM_MEDIA_HDDVD=1\n");
1074 if (cd_media_hddvd_r
)
1075 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
1076 if (cd_media_hddvd_rw
)
1077 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
1079 if (cd_media_state
!= NULL
)
1080 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state
);
1081 if (cd_media_session_next
> 0)
1082 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next
);
1083 if (cd_media_session_count
> 0)
1084 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count
);
1085 if (cd_media_session_count
> 1 && cd_media_session_last_offset
> 0)
1086 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset
);
1087 if (cd_media_track_count
> 0)
1088 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count
);
1089 if (cd_media_track_count_audio
> 0)
1090 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio
);
1091 if (cd_media_track_count_data
> 0)
1092 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data
);