2 * cdrom_id - optical drive and media information prober
4 * Copyright (C) 2008-2010 Kay Sievers <kay@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/>.
24 #include <linux/cdrom.h>
30 #include <sys/ioctl.h>
33 #include <sys/types.h>
39 #include "libudev-private.h"
40 #include "random-util.h"
43 static unsigned int cd_cd_rom
;
44 static unsigned int cd_cd_r
;
45 static unsigned int cd_cd_rw
;
46 static unsigned int cd_dvd_rom
;
47 static unsigned int cd_dvd_r
;
48 static unsigned int cd_dvd_rw
;
49 static unsigned int cd_dvd_ram
;
50 static unsigned int cd_dvd_plus_r
;
51 static unsigned int cd_dvd_plus_rw
;
52 static unsigned int cd_dvd_plus_r_dl
;
53 static unsigned int cd_dvd_plus_rw_dl
;
54 static unsigned int cd_bd
;
55 static unsigned int cd_bd_r
;
56 static unsigned int cd_bd_re
;
57 static unsigned int cd_hddvd
;
58 static unsigned int cd_hddvd_r
;
59 static unsigned int cd_hddvd_rw
;
60 static unsigned int cd_mo
;
61 static unsigned int cd_mrw
;
62 static unsigned int cd_mrw_w
;
65 static unsigned int cd_media
;
66 static unsigned int cd_media_cd_rom
;
67 static unsigned int cd_media_cd_r
;
68 static unsigned int cd_media_cd_rw
;
69 static unsigned int cd_media_dvd_rom
;
70 static unsigned int cd_media_dvd_r
;
71 static unsigned int cd_media_dvd_rw
;
72 static unsigned int cd_media_dvd_rw_ro
; /* restricted overwrite mode */
73 static unsigned int cd_media_dvd_rw_seq
; /* sequential mode */
74 static unsigned int cd_media_dvd_ram
;
75 static unsigned int cd_media_dvd_plus_r
;
76 static unsigned int cd_media_dvd_plus_rw
;
77 static unsigned int cd_media_dvd_plus_r_dl
;
78 static unsigned int cd_media_dvd_plus_rw_dl
;
79 static unsigned int cd_media_bd
;
80 static unsigned int cd_media_bd_r
;
81 static unsigned int cd_media_bd_re
;
82 static unsigned int cd_media_hddvd
;
83 static unsigned int cd_media_hddvd_r
;
84 static unsigned int cd_media_hddvd_rw
;
85 static unsigned int cd_media_mo
;
86 static unsigned int cd_media_mrw
;
87 static unsigned int cd_media_mrw_w
;
89 static const char *cd_media_state
= NULL
;
90 static unsigned int cd_media_session_next
;
91 static unsigned int cd_media_session_count
;
92 static unsigned int cd_media_track_count
;
93 static unsigned int cd_media_track_count_data
;
94 static unsigned int cd_media_track_count_audio
;
95 static unsigned long long int cd_media_session_last_offset
;
97 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
98 #define SK(errcode) (((errcode) >> 16) & 0xF)
99 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
100 #define ASCQ(errcode) ((errcode) & 0xFF)
102 static bool is_mounted(const char *device
)
107 bool mounted
= false;
109 if (stat(device
, &statbuf
) < 0)
112 fp
= fopen("/proc/self/mountinfo", "re");
115 while (fscanf(fp
, "%*s %*s %i:%i %*[^\n]", &maj
, &min
) == 2) {
116 if (makedev(maj
, min
) == statbuf
.st_rdev
) {
125 static void info_scsi_cmd_err(struct udev
*udev
, const char *cmd
, int err
)
128 log_debug("%s failed", cmd
);
131 log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd
, SK(err
), ASC(err
), ASCQ(err
));
135 struct cdrom_generic_command cgc
;
137 struct request_sense s
;
140 struct sg_io_hdr sg_io
;
143 static void scsi_cmd_init(struct udev
*udev
, struct scsi_cmd
*cmd
)
145 memzero(cmd
, sizeof(struct scsi_cmd
));
147 cmd
->cgc
.sense
= &cmd
->_sense
.s
;
148 cmd
->sg_io
.interface_id
= 'S';
149 cmd
->sg_io
.mx_sb_len
= sizeof(cmd
->_sense
);
150 cmd
->sg_io
.cmdp
= cmd
->cgc
.cmd
;
151 cmd
->sg_io
.sbp
= cmd
->_sense
.u
;
152 cmd
->sg_io
.flags
= SG_FLAG_LUN_INHIBIT
| SG_FLAG_DIRECT_IO
;
155 static void scsi_cmd_set(struct udev
*udev
, struct scsi_cmd
*cmd
, size_t i
, unsigned char arg
)
157 cmd
->sg_io
.cmd_len
= i
+ 1;
158 cmd
->cgc
.cmd
[i
] = arg
;
161 #define CHECK_CONDITION 0x01
163 static int scsi_cmd_run(struct udev
*udev
, struct scsi_cmd
*cmd
, int fd
, unsigned char *buf
, size_t bufsize
)
168 cmd
->sg_io
.dxferp
= buf
;
169 cmd
->sg_io
.dxfer_len
= bufsize
;
170 cmd
->sg_io
.dxfer_direction
= SG_DXFER_FROM_DEV
;
172 cmd
->sg_io
.dxfer_direction
= SG_DXFER_NONE
;
174 if (ioctl(fd
, SG_IO
, &cmd
->sg_io
))
177 if ((cmd
->sg_io
.info
& SG_INFO_OK_MASK
) != SG_INFO_OK
) {
180 if (cmd
->sg_io
.masked_status
& CHECK_CONDITION
) {
181 ret
= ERRCODE(cmd
->_sense
.u
);
189 static int media_lock(struct udev
*udev
, int fd
, bool lock
)
193 /* disable the kernel's lock logic */
194 err
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_LOCK
);
196 log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed");
198 err
= ioctl(fd
, CDROM_LOCKDOOR
, lock
? 1 : 0);
200 log_debug("CDROM_LOCKDOOR failed");
205 static int media_eject(struct udev
*udev
, int fd
)
210 scsi_cmd_init(udev
, &sc
);
211 scsi_cmd_set(udev
, &sc
, 0, 0x1b);
212 scsi_cmd_set(udev
, &sc
, 4, 0x02);
213 scsi_cmd_set(udev
, &sc
, 5, 0);
214 err
= scsi_cmd_run(udev
, &sc
, fd
, NULL
, 0);
216 info_scsi_cmd_err(udev
, "START_STOP_UNIT", err
);
222 static int cd_capability_compat(struct udev
*udev
, int fd
)
226 capability
= ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
);
227 if (capability
< 0) {
228 log_debug("CDROM_GET_CAPABILITY failed");
232 if (capability
& CDC_CD_R
)
234 if (capability
& CDC_CD_RW
)
236 if (capability
& CDC_DVD
)
238 if (capability
& CDC_DVD_R
)
240 if (capability
& CDC_DVD_RAM
)
242 if (capability
& CDC_MRW
)
244 if (capability
& CDC_MRW_W
)
249 static int cd_media_compat(struct udev
*udev
, int fd
)
251 if (ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
) != CDS_DISC_OK
) {
252 log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
259 static int cd_inquiry(struct udev
*udev
, int fd
)
262 unsigned char inq
[128];
265 scsi_cmd_init(udev
, &sc
);
266 scsi_cmd_set(udev
, &sc
, 0, 0x12);
267 scsi_cmd_set(udev
, &sc
, 4, 36);
268 scsi_cmd_set(udev
, &sc
, 5, 0);
269 err
= scsi_cmd_run(udev
, &sc
, fd
, inq
, 36);
271 info_scsi_cmd_err(udev
, "INQUIRY", err
);
275 if ((inq
[0] & 0x1F) != 5) {
276 log_debug("not an MMC unit");
280 log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq
+ 8, inq
+ 16, inq
+ 32);
284 static void feature_profile_media(struct udev
*udev
, int cur_profile
)
286 switch (cur_profile
) {
290 log_debug("profile 0x%02x ", cur_profile
);
295 log_debug("profile 0x%02x media_cd_rom", cur_profile
);
300 log_debug("profile 0x%02x media_cd_r", cur_profile
);
305 log_debug("profile 0x%02x media_cd_rw", cur_profile
);
310 log_debug("profile 0x%02x media_dvd_ro", cur_profile
);
312 cd_media_dvd_rom
= 1;
315 log_debug("profile 0x%02x media_dvd_r", cur_profile
);
320 log_debug("profile 0x%02x media_dvd_ram", cur_profile
);
322 cd_media_dvd_ram
= 1;
325 log_debug("profile 0x%02x media_dvd_rw_ro", cur_profile
);
328 cd_media_dvd_rw_ro
= 1;
331 log_debug("profile 0x%02x media_dvd_rw_seq", cur_profile
);
334 cd_media_dvd_rw_seq
= 1;
337 log_debug("profile 0x%02x media_dvd_plus_r", cur_profile
);
339 cd_media_dvd_plus_r
= 1;
342 log_debug("profile 0x%02x media_dvd_plus_rw", cur_profile
);
344 cd_media_dvd_plus_rw
= 1;
347 log_debug("profile 0x%02x media_dvd_plus_rw_dl", cur_profile
);
349 cd_media_dvd_plus_rw_dl
= 1;
352 log_debug("profile 0x%02x media_dvd_plus_r_dl", cur_profile
);
354 cd_media_dvd_plus_r_dl
= 1;
357 log_debug("profile 0x%02x media_bd", cur_profile
);
363 log_debug("profile 0x%02x media_bd_r", cur_profile
);
368 log_debug("profile 0x%02x media_bd_re", cur_profile
);
373 log_debug("profile 0x%02x media_hddvd", cur_profile
);
378 log_debug("profile 0x%02x media_hddvd_r", cur_profile
);
380 cd_media_hddvd_r
= 1;
383 log_debug("profile 0x%02x media_hddvd_rw", cur_profile
);
385 cd_media_hddvd_rw
= 1;
388 log_debug("profile 0x%02x <ignored>", cur_profile
);
393 static int feature_profiles(struct udev
*udev
, const unsigned char *profiles
, size_t size
)
397 for (i
= 0; i
+4 <= size
; i
+= 4) {
400 profile
= profiles
[i
] << 8 | profiles
[i
+1];
405 log_debug("profile 0x%02x mo", profile
);
409 log_debug("profile 0x%02x cd_rom", profile
);
413 log_debug("profile 0x%02x cd_r", profile
);
417 log_debug("profile 0x%02x cd_rw", profile
);
421 log_debug("profile 0x%02x dvd_rom", profile
);
425 log_debug("profile 0x%02x dvd_ram", profile
);
430 log_debug("profile 0x%02x dvd_rw", profile
);
434 log_debug("profile 0x%02x dvd_plus_r", profile
);
438 log_debug("profile 0x%02x dvd_plus_rw", profile
);
442 log_debug("profile 0x%02x dvd_plus_rw_dl", profile
);
443 cd_dvd_plus_rw_dl
= 1;
446 log_debug("profile 0x%02x dvd_plus_r_dl", profile
);
447 cd_dvd_plus_r_dl
= 1;
451 log_debug("profile 0x%02x bd", profile
);
456 log_debug("profile 0x%02x bd_r", profile
);
460 log_debug("profile 0x%02x bd_re", profile
);
464 log_debug("profile 0x%02x hddvd", profile
);
468 log_debug("profile 0x%02x hddvd_r", profile
);
472 log_debug("profile 0x%02x hddvd_rw", profile
);
475 log_debug("profile 0x%02x <ignored>", profile
);
482 /* returns 0 if media was detected */
483 static int cd_profiles_old_mmc(struct udev
*udev
, int fd
)
488 unsigned char header
[32];
490 scsi_cmd_init(udev
, &sc
);
491 scsi_cmd_set(udev
, &sc
, 0, 0x51);
492 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
493 scsi_cmd_set(udev
, &sc
, 9, 0);
494 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
496 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
498 log_debug("no current profile, but disc is present; assuming CD-ROM");
500 cd_media_track_count
= 1;
501 cd_media_track_count_data
= 1;
504 log_debug("no current profile, assuming no media");
511 if (header
[2] & 16) {
513 log_debug("profile 0x0a media_cd_rw");
514 } else if ((header
[2] & 3) < 2 && cd_cd_r
) {
516 log_debug("profile 0x09 media_cd_r");
519 log_debug("profile 0x08 media_cd_rom");
524 /* returns 0 if media was detected */
525 static int cd_profiles(struct udev
*udev
, int fd
)
528 unsigned char features
[65530];
529 unsigned int cur_profile
= 0;
537 /* First query the current profile */
538 scsi_cmd_init(udev
, &sc
);
539 scsi_cmd_set(udev
, &sc
, 0, 0x46);
540 scsi_cmd_set(udev
, &sc
, 8, 8);
541 scsi_cmd_set(udev
, &sc
, 9, 0);
542 err
= scsi_cmd_run(udev
, &sc
, fd
, features
, 8);
544 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
545 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
546 if (SK(err
) == 0x5 && (ASC(err
) == 0x20 || ASC(err
) == 0x24)) {
547 log_debug("drive is pre-MMC2 and does not support 46h get configuration command");
548 log_debug("trying to work around the problem");
549 ret
= cd_profiles_old_mmc(udev
, fd
);
554 cur_profile
= features
[6] << 8 | features
[7];
555 if (cur_profile
> 0) {
556 log_debug("current profile 0x%02x", cur_profile
);
557 feature_profile_media (udev
, cur_profile
);
558 ret
= 0; /* we have media */
560 log_debug("no current profile, assuming no media");
563 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
564 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len
);
566 if (len
> sizeof(features
)) {
567 log_debug("can not get features in a single query, truncating");
568 len
= sizeof(features
);
570 len
= sizeof(features
);
572 /* Now get the full feature buffer */
573 scsi_cmd_init(udev
, &sc
);
574 scsi_cmd_set(udev
, &sc
, 0, 0x46);
575 scsi_cmd_set(udev
, &sc
, 7, ( len
>> 8 ) & 0xff);
576 scsi_cmd_set(udev
, &sc
, 8, len
& 0xff);
577 scsi_cmd_set(udev
, &sc
, 9, 0);
578 err
= scsi_cmd_run(udev
, &sc
, fd
, features
, len
);
580 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
584 /* parse the length once more, in case the drive decided to have other features suddenly :) */
585 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
586 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len
);
588 if (len
> sizeof(features
)) {
589 log_debug("can not get features in a single query, truncating");
590 len
= sizeof(features
);
593 /* device features */
594 for (i
= 8; i
+4 < len
; i
+= (4 + features
[i
+3])) {
595 unsigned int feature
;
597 feature
= features
[i
] << 8 | features
[i
+1];
601 log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features
[i
+3] / 4);
602 feature_profiles(udev
, &features
[i
]+4, MIN(features
[i
+3], len
- i
- 4));
605 log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature
, features
[i
+3]);
613 static int cd_media_info(struct udev
*udev
, int fd
)
616 unsigned char header
[32];
617 static const char *media_status
[] = {
625 scsi_cmd_init(udev
, &sc
);
626 scsi_cmd_set(udev
, &sc
, 0, 0x51);
627 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
628 scsi_cmd_set(udev
, &sc
, 9, 0);
629 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
631 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
636 log_debug("disk type %02x", header
[8]);
637 log_debug("hardware reported media status: %s", media_status
[header
[2] & 3]);
639 /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
640 if (!cd_media_cd_rom
)
641 cd_media_state
= media_status
[header
[2] & 3];
643 /* fresh DVD-RW in restricted overwite mode reports itself as
644 * "appendable"; change it to "blank" to make it consistent with what
645 * gets reported after blanking, and what userspace expects */
646 if (cd_media_dvd_rw_ro
&& (header
[2] & 3) == 1)
647 cd_media_state
= media_status
[0];
649 /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
650 * always "complete", DVD-RAM are "other" or "complete" if the disc is
651 * write protected; we need to check the contents if it is blank */
652 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) {
653 unsigned char buffer
[32 * 2048];
657 if (cd_media_dvd_ram
) {
658 /* a write protected dvd-ram may report "complete" status */
660 unsigned char dvdstruct
[8];
661 unsigned char format
[12];
663 scsi_cmd_init(udev
, &sc
);
664 scsi_cmd_set(udev
, &sc
, 0, 0xAD);
665 scsi_cmd_set(udev
, &sc
, 7, 0xC0);
666 scsi_cmd_set(udev
, &sc
, 9, sizeof(dvdstruct
));
667 scsi_cmd_set(udev
, &sc
, 11, 0);
668 err
= scsi_cmd_run(udev
, &sc
, fd
, dvdstruct
, sizeof(dvdstruct
));
670 info_scsi_cmd_err(udev
, "READ DVD STRUCTURE", err
);
673 if (dvdstruct
[4] & 0x02) {
674 cd_media_state
= media_status
[2];
675 log_debug("write-protected DVD-RAM media inserted");
679 /* let's make sure we don't try to read unformatted media */
680 scsi_cmd_init(udev
, &sc
);
681 scsi_cmd_set(udev
, &sc
, 0, 0x23);
682 scsi_cmd_set(udev
, &sc
, 8, sizeof(format
));
683 scsi_cmd_set(udev
, &sc
, 9, 0);
684 err
= scsi_cmd_run(udev
, &sc
, fd
, format
, sizeof(format
));
686 info_scsi_cmd_err(udev
, "READ DVD FORMAT CAPACITIES", err
);
691 if (len
& 7 || len
< 16) {
692 log_debug("invalid format capacities length");
696 switch(format
[8] & 3) {
698 log_debug("unformatted DVD-RAM media inserted");
699 /* This means that last format was interrupted
700 * or failed, blank dvd-ram discs are factory
701 * formatted. Take no action here as it takes
702 * quite a while to reformat a dvd-ram and it's
703 * not automatically started */
707 log_debug("formatted DVD-RAM media inserted");
711 cd_media
= 0; //return no media
712 log_debug("format capacities returned no media");
717 /* Take a closer look at formatted media (unformatted DVD+RW
718 * has "blank" status", DVD-RAM was examined earlier) and check
719 * for ISO and UDF PVDs or a fs superblock presence and do it
720 * in one ioctl (we need just sectors 0 and 16) */
721 scsi_cmd_init(udev
, &sc
);
722 scsi_cmd_set(udev
, &sc
, 0, 0x28);
723 scsi_cmd_set(udev
, &sc
, 5, 0);
724 scsi_cmd_set(udev
, &sc
, 8, 32);
725 scsi_cmd_set(udev
, &sc
, 9, 0);
726 err
= scsi_cmd_run(udev
, &sc
, fd
, buffer
, sizeof(buffer
));
729 info_scsi_cmd_err(udev
, "READ FIRST 32 BLOCKS", err
);
733 /* if any non-zero data is found in sector 16 (iso and udf) or
734 * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
735 * is assumed non-blank */
737 for (offset
= 32768; offset
< (32768 + 2048); offset
++) {
738 if (buffer
[offset
]) {
739 log_debug("data in block 16, assuming complete");
744 for (offset
= 0; offset
< 2048; offset
++) {
745 if (buffer
[offset
]) {
746 log_debug("data in block 0, assuming complete");
751 cd_media_state
= media_status
[0];
752 log_debug("no data in blocks 0 or 16, assuming blank");
756 /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
757 * restricted overwrite mode can never append, only in sequential mode */
758 if ((header
[2] & 3) < 2 && !cd_media_dvd_rw_ro
)
759 cd_media_session_next
= header
[10] << 8 | header
[5];
760 cd_media_session_count
= header
[9] << 8 | header
[4];
761 cd_media_track_count
= header
[11] << 8 | header
[6];
766 static int cd_media_toc(struct udev
*udev
, int fd
)
769 unsigned char header
[12];
770 unsigned char toc
[65536];
771 unsigned int len
, i
, num_tracks
;
775 scsi_cmd_init(udev
, &sc
);
776 scsi_cmd_set(udev
, &sc
, 0, 0x43);
777 scsi_cmd_set(udev
, &sc
, 6, 1);
778 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
779 scsi_cmd_set(udev
, &sc
, 9, 0);
780 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
782 info_scsi_cmd_err(udev
, "READ TOC", err
);
786 len
= (header
[0] << 8 | header
[1]) + 2;
787 log_debug("READ TOC: len: %d, start track: %d, end track: %d", len
, header
[2], header
[3]);
788 if (len
> sizeof(toc
))
792 /* 2: first track, 3: last track */
793 num_tracks
= header
[3] - header
[2] + 1;
795 /* empty media has no tracks */
799 scsi_cmd_init(udev
, &sc
);
800 scsi_cmd_set(udev
, &sc
, 0, 0x43);
801 scsi_cmd_set(udev
, &sc
, 6, header
[2]); /* First Track/Session Number */
802 scsi_cmd_set(udev
, &sc
, 7, (len
>> 8) & 0xff);
803 scsi_cmd_set(udev
, &sc
, 8, len
& 0xff);
804 scsi_cmd_set(udev
, &sc
, 9, 0);
805 err
= scsi_cmd_run(udev
, &sc
, fd
, toc
, len
);
807 info_scsi_cmd_err(udev
, "READ TOC (tracks)", err
);
811 /* Take care to not iterate beyond the last valid track as specified in
812 * the TOC, but also avoid going beyond the TOC length, just in case
813 * the last track number is invalidly large */
814 for (p
= toc
+4, i
= 4; i
< len
-8 && num_tracks
> 0; i
+= 8, p
+= 8, --num_tracks
) {
816 unsigned int is_data_track
;
818 is_data_track
= (p
[1] & 0x04) != 0;
820 block
= p
[4] << 24 | p
[5] << 16 | p
[6] << 8 | p
[7];
821 log_debug("track=%u info=0x%x(%s) start_block=%u",
822 p
[2], p
[1] & 0x0f, is_data_track
? "data":"audio", block
);
825 cd_media_track_count_data
++;
827 cd_media_track_count_audio
++;
830 scsi_cmd_init(udev
, &sc
);
831 scsi_cmd_set(udev
, &sc
, 0, 0x43);
832 scsi_cmd_set(udev
, &sc
, 2, 1); /* Session Info */
833 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
834 scsi_cmd_set(udev
, &sc
, 9, 0);
835 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
837 info_scsi_cmd_err(udev
, "READ TOC (multi session)", err
);
840 len
= header
[4+4] << 24 | header
[4+5] << 16 | header
[4+6] << 8 | header
[4+7];
841 log_debug("last track %u starts at block %u", header
[4+2], len
);
842 cd_media_session_last_offset
= (unsigned long long int)len
* 2048;
846 int main(int argc
, char *argv
[])
849 static const struct option options
[] = {
850 { "lock-media", no_argument
, NULL
, 'l' },
851 { "unlock-media", no_argument
, NULL
, 'u' },
852 { "eject-media", no_argument
, NULL
, 'e' },
853 { "debug", no_argument
, NULL
, 'd' },
854 { "help", no_argument
, NULL
, 'h' },
860 const char *node
= NULL
;
865 log_parse_environment();
875 option
= getopt_long(argc
, argv
, "deluh", options
, NULL
);
890 log_set_target(LOG_TARGET_CONSOLE
);
891 log_set_max_level(LOG_DEBUG
);
895 printf("Usage: cdrom_id [options] <device>\n"
896 " -l,--lock-media lock the media (to enable eject request events)\n"
897 " -u,--unlock-media unlock the media\n"
898 " -e,--eject-media eject the media\n"
899 " -d,--debug debug to stderr\n"
900 " -h,--help print this help text\n\n");
910 log_error("no device");
911 fprintf(stderr
, "no device\n");
917 for (cnt
= 20; cnt
> 0; cnt
--) {
918 struct timespec duration
;
920 fd
= open(node
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|(is_mounted(node
) ? 0 : O_EXCL
));
921 if (fd
>= 0 || errno
!= EBUSY
)
924 duration
.tv_nsec
= (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
925 nanosleep(&duration
, NULL
);
928 log_debug("unable to open '%s'", node
);
929 fprintf(stderr
, "unable to open '%s'\n", node
);
933 log_debug("probing: '%s'", node
);
935 /* same data as original cdrom_id */
936 if (cd_capability_compat(udev
, fd
) < 0) {
941 /* check for media - don't bail if there's no media as we still need to
942 * to read profiles */
943 cd_media_compat(udev
, fd
);
945 /* check if drive talks MMC */
946 if (cd_inquiry(udev
, fd
) < 0)
949 /* read drive and possibly current profile */
950 if (cd_profiles(udev
, fd
) != 0)
953 /* at this point we are guaranteed to have media in the drive - find out more about it */
955 /* get session/track info */
956 cd_media_toc(udev
, fd
);
958 /* get writable media state */
959 cd_media_info(udev
, fd
);
962 /* lock the media, so we enable eject button events */
963 if (lock
&& cd_media
) {
964 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)");
965 media_lock(udev
, fd
, true);
968 if (unlock
&& cd_media
) {
969 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
970 media_lock(udev
, fd
, false);
974 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
975 media_lock(udev
, fd
, false);
976 log_debug("START_STOP_UNIT (eject)");
977 media_eject(udev
, fd
);
980 printf("ID_CDROM=1\n");
982 printf("ID_CDROM_CD=1\n");
984 printf("ID_CDROM_CD_R=1\n");
986 printf("ID_CDROM_CD_RW=1\n");
988 printf("ID_CDROM_DVD=1\n");
990 printf("ID_CDROM_DVD_R=1\n");
992 printf("ID_CDROM_DVD_RW=1\n");
994 printf("ID_CDROM_DVD_RAM=1\n");
996 printf("ID_CDROM_DVD_PLUS_R=1\n");
998 printf("ID_CDROM_DVD_PLUS_RW=1\n");
999 if (cd_dvd_plus_r_dl
)
1000 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
1001 if (cd_dvd_plus_rw_dl
)
1002 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
1004 printf("ID_CDROM_BD=1\n");
1006 printf("ID_CDROM_BD_R=1\n");
1008 printf("ID_CDROM_BD_RE=1\n");
1010 printf("ID_CDROM_HDDVD=1\n");
1012 printf("ID_CDROM_HDDVD_R=1\n");
1014 printf("ID_CDROM_HDDVD_RW=1\n");
1016 printf("ID_CDROM_MO=1\n");
1018 printf("ID_CDROM_MRW=1\n");
1020 printf("ID_CDROM_MRW_W=1\n");
1023 printf("ID_CDROM_MEDIA=1\n");
1025 printf("ID_CDROM_MEDIA_MO=1\n");
1027 printf("ID_CDROM_MEDIA_MRW=1\n");
1029 printf("ID_CDROM_MEDIA_MRW_W=1\n");
1030 if (cd_media_cd_rom
)
1031 printf("ID_CDROM_MEDIA_CD=1\n");
1033 printf("ID_CDROM_MEDIA_CD_R=1\n");
1035 printf("ID_CDROM_MEDIA_CD_RW=1\n");
1036 if (cd_media_dvd_rom
)
1037 printf("ID_CDROM_MEDIA_DVD=1\n");
1039 printf("ID_CDROM_MEDIA_DVD_R=1\n");
1040 if (cd_media_dvd_ram
)
1041 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
1042 if (cd_media_dvd_rw
)
1043 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
1044 if (cd_media_dvd_plus_r
)
1045 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
1046 if (cd_media_dvd_plus_rw
)
1047 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
1048 if (cd_media_dvd_plus_rw_dl
)
1049 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
1050 if (cd_media_dvd_plus_r_dl
)
1051 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
1053 printf("ID_CDROM_MEDIA_BD=1\n");
1055 printf("ID_CDROM_MEDIA_BD_R=1\n");
1057 printf("ID_CDROM_MEDIA_BD_RE=1\n");
1059 printf("ID_CDROM_MEDIA_HDDVD=1\n");
1060 if (cd_media_hddvd_r
)
1061 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
1062 if (cd_media_hddvd_rw
)
1063 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
1065 if (cd_media_state
!= NULL
)
1066 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state
);
1067 if (cd_media_session_next
> 0)
1068 printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next
);
1069 if (cd_media_session_count
> 0)
1070 printf("ID_CDROM_MEDIA_SESSION_COUNT=%u\n", cd_media_session_count
);
1071 if (cd_media_session_count
> 1 && cd_media_session_last_offset
> 0)
1072 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset
);
1073 if (cd_media_track_count
> 0)
1074 printf("ID_CDROM_MEDIA_TRACK_COUNT=%u\n", cd_media_track_count
);
1075 if (cd_media_track_count_audio
> 0)
1076 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%u\n", cd_media_track_count_audio
);
1077 if (cd_media_track_count_data
> 0)
1078 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%u\n", cd_media_track_count_data
);