1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * cdrom_id - optical drive and media information prober
5 * Copyright (C) 2008-2010 Kay Sievers <kay@vrfy.org>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/cdrom.h>
31 #include <sys/ioctl.h>
34 #include <sys/types.h>
40 #include "libudev-private.h"
41 #include "random-util.h"
42 #include "udev-util.h"
45 static unsigned int cd_cd_rom
;
46 static unsigned int cd_cd_r
;
47 static unsigned int cd_cd_rw
;
48 static unsigned int cd_dvd_rom
;
49 static unsigned int cd_dvd_r
;
50 static unsigned int cd_dvd_rw
;
51 static unsigned int cd_dvd_ram
;
52 static unsigned int cd_dvd_plus_r
;
53 static unsigned int cd_dvd_plus_rw
;
54 static unsigned int cd_dvd_plus_r_dl
;
55 static unsigned int cd_dvd_plus_rw_dl
;
56 static unsigned int cd_bd
;
57 static unsigned int cd_bd_r
;
58 static unsigned int cd_bd_re
;
59 static unsigned int cd_hddvd
;
60 static unsigned int cd_hddvd_r
;
61 static unsigned int cd_hddvd_rw
;
62 static unsigned int cd_mo
;
63 static unsigned int cd_mrw
;
64 static unsigned int cd_mrw_w
;
67 static unsigned int cd_media
;
68 static unsigned int cd_media_cd_rom
;
69 static unsigned int cd_media_cd_r
;
70 static unsigned int cd_media_cd_rw
;
71 static unsigned int cd_media_dvd_rom
;
72 static unsigned int cd_media_dvd_r
;
73 static unsigned int cd_media_dvd_rw
;
74 static unsigned int cd_media_dvd_rw_ro
; /* restricted overwrite mode */
75 static unsigned int cd_media_dvd_rw_seq
; /* sequential mode */
76 static unsigned int cd_media_dvd_ram
;
77 static unsigned int cd_media_dvd_plus_r
;
78 static unsigned int cd_media_dvd_plus_rw
;
79 static unsigned int cd_media_dvd_plus_r_dl
;
80 static unsigned int cd_media_dvd_plus_rw_dl
;
81 static unsigned int cd_media_bd
;
82 static unsigned int cd_media_bd_r
;
83 static unsigned int cd_media_bd_re
;
84 static unsigned int cd_media_hddvd
;
85 static unsigned int cd_media_hddvd_r
;
86 static unsigned int cd_media_hddvd_rw
;
87 static unsigned int cd_media_mo
;
88 static unsigned int cd_media_mrw
;
89 static unsigned int cd_media_mrw_w
;
91 static const char *cd_media_state
= NULL
;
92 static unsigned int cd_media_session_next
;
93 static unsigned int cd_media_session_count
;
94 static unsigned int cd_media_track_count
;
95 static unsigned int cd_media_track_count_data
;
96 static unsigned int cd_media_track_count_audio
;
97 static unsigned long long int cd_media_session_last_offset
;
99 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
100 #define SK(errcode) (((errcode) >> 16) & 0xF)
101 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
102 #define ASCQ(errcode) ((errcode) & 0xFF)
104 static bool is_mounted(const char *device
)
109 bool mounted
= false;
111 if (stat(device
, &statbuf
) < 0)
114 fp
= fopen("/proc/self/mountinfo", "re");
117 while (fscanf(fp
, "%*s %*s %i:%i %*[^\n]", &maj
, &min
) == 2) {
118 if (makedev(maj
, min
) == statbuf
.st_rdev
) {
127 static void info_scsi_cmd_err(struct udev
*udev
, const char *cmd
, int err
)
130 log_debug("%s failed", cmd
);
133 log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd
, SK(err
), ASC(err
), ASCQ(err
));
137 struct cdrom_generic_command cgc
;
139 struct request_sense s
;
142 struct sg_io_hdr sg_io
;
145 static void scsi_cmd_init(struct udev
*udev
, struct scsi_cmd
*cmd
)
147 memzero(cmd
, sizeof(struct scsi_cmd
));
149 cmd
->cgc
.sense
= &cmd
->_sense
.s
;
150 cmd
->sg_io
.interface_id
= 'S';
151 cmd
->sg_io
.mx_sb_len
= sizeof(cmd
->_sense
);
152 cmd
->sg_io
.cmdp
= cmd
->cgc
.cmd
;
153 cmd
->sg_io
.sbp
= cmd
->_sense
.u
;
154 cmd
->sg_io
.flags
= SG_FLAG_LUN_INHIBIT
| SG_FLAG_DIRECT_IO
;
157 static void scsi_cmd_set(struct udev
*udev
, struct scsi_cmd
*cmd
, size_t i
, unsigned char arg
)
159 cmd
->sg_io
.cmd_len
= i
+ 1;
160 cmd
->cgc
.cmd
[i
] = arg
;
163 #define CHECK_CONDITION 0x01
165 static int scsi_cmd_run(struct udev
*udev
, struct scsi_cmd
*cmd
, int fd
, unsigned char *buf
, size_t bufsize
)
170 cmd
->sg_io
.dxferp
= buf
;
171 cmd
->sg_io
.dxfer_len
= bufsize
;
172 cmd
->sg_io
.dxfer_direction
= SG_DXFER_FROM_DEV
;
174 cmd
->sg_io
.dxfer_direction
= SG_DXFER_NONE
;
176 if (ioctl(fd
, SG_IO
, &cmd
->sg_io
))
179 if ((cmd
->sg_io
.info
& SG_INFO_OK_MASK
) != SG_INFO_OK
) {
182 if (cmd
->sg_io
.masked_status
& CHECK_CONDITION
) {
183 ret
= ERRCODE(cmd
->_sense
.u
);
191 static int media_lock(struct udev
*udev
, int fd
, bool lock
)
195 /* disable the kernel's lock logic */
196 err
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_LOCK
);
198 log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed");
200 err
= ioctl(fd
, CDROM_LOCKDOOR
, lock
? 1 : 0);
202 log_debug("CDROM_LOCKDOOR failed");
207 static int media_eject(struct udev
*udev
, int fd
)
212 scsi_cmd_init(udev
, &sc
);
213 scsi_cmd_set(udev
, &sc
, 0, 0x1b);
214 scsi_cmd_set(udev
, &sc
, 4, 0x02);
215 scsi_cmd_set(udev
, &sc
, 5, 0);
216 err
= scsi_cmd_run(udev
, &sc
, fd
, NULL
, 0);
218 info_scsi_cmd_err(udev
, "START_STOP_UNIT", err
);
224 static int cd_capability_compat(struct udev
*udev
, int fd
)
228 capability
= ioctl(fd
, CDROM_GET_CAPABILITY
, NULL
);
229 if (capability
< 0) {
230 log_debug("CDROM_GET_CAPABILITY failed");
234 if (capability
& CDC_CD_R
)
236 if (capability
& CDC_CD_RW
)
238 if (capability
& CDC_DVD
)
240 if (capability
& CDC_DVD_R
)
242 if (capability
& CDC_DVD_RAM
)
244 if (capability
& CDC_MRW
)
246 if (capability
& CDC_MRW_W
)
251 static int cd_media_compat(struct udev
*udev
, int fd
)
253 if (ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
) != CDS_DISC_OK
) {
254 log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
261 static int cd_inquiry(struct udev
*udev
, int fd
)
264 unsigned char inq
[128];
267 scsi_cmd_init(udev
, &sc
);
268 scsi_cmd_set(udev
, &sc
, 0, 0x12);
269 scsi_cmd_set(udev
, &sc
, 4, 36);
270 scsi_cmd_set(udev
, &sc
, 5, 0);
271 err
= scsi_cmd_run(udev
, &sc
, fd
, inq
, 36);
273 info_scsi_cmd_err(udev
, "INQUIRY", err
);
277 if ((inq
[0] & 0x1F) != 5) {
278 log_debug("not an MMC unit");
282 log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq
+ 8, inq
+ 16, inq
+ 32);
286 static void feature_profile_media(struct udev
*udev
, int cur_profile
)
288 switch (cur_profile
) {
292 log_debug("profile 0x%02x ", cur_profile
);
297 log_debug("profile 0x%02x media_cd_rom", cur_profile
);
302 log_debug("profile 0x%02x media_cd_r", cur_profile
);
307 log_debug("profile 0x%02x media_cd_rw", cur_profile
);
312 log_debug("profile 0x%02x media_dvd_ro", cur_profile
);
314 cd_media_dvd_rom
= 1;
317 log_debug("profile 0x%02x media_dvd_r", cur_profile
);
322 log_debug("profile 0x%02x media_dvd_ram", cur_profile
);
324 cd_media_dvd_ram
= 1;
327 log_debug("profile 0x%02x media_dvd_rw_ro", cur_profile
);
330 cd_media_dvd_rw_ro
= 1;
333 log_debug("profile 0x%02x media_dvd_rw_seq", cur_profile
);
336 cd_media_dvd_rw_seq
= 1;
339 log_debug("profile 0x%02x media_dvd_plus_r", cur_profile
);
341 cd_media_dvd_plus_r
= 1;
344 log_debug("profile 0x%02x media_dvd_plus_rw", cur_profile
);
346 cd_media_dvd_plus_rw
= 1;
349 log_debug("profile 0x%02x media_dvd_plus_rw_dl", cur_profile
);
351 cd_media_dvd_plus_rw_dl
= 1;
354 log_debug("profile 0x%02x media_dvd_plus_r_dl", cur_profile
);
356 cd_media_dvd_plus_r_dl
= 1;
359 log_debug("profile 0x%02x media_bd", cur_profile
);
365 log_debug("profile 0x%02x media_bd_r", cur_profile
);
370 log_debug("profile 0x%02x media_bd_re", cur_profile
);
375 log_debug("profile 0x%02x media_hddvd", cur_profile
);
380 log_debug("profile 0x%02x media_hddvd_r", cur_profile
);
382 cd_media_hddvd_r
= 1;
385 log_debug("profile 0x%02x media_hddvd_rw", cur_profile
);
387 cd_media_hddvd_rw
= 1;
390 log_debug("profile 0x%02x <ignored>", cur_profile
);
395 static int feature_profiles(struct udev
*udev
, const unsigned char *profiles
, size_t size
)
399 for (i
= 0; i
+4 <= size
; i
+= 4) {
402 profile
= profiles
[i
] << 8 | profiles
[i
+1];
407 log_debug("profile 0x%02x mo", profile
);
411 log_debug("profile 0x%02x cd_rom", profile
);
415 log_debug("profile 0x%02x cd_r", profile
);
419 log_debug("profile 0x%02x cd_rw", profile
);
423 log_debug("profile 0x%02x dvd_rom", profile
);
427 log_debug("profile 0x%02x dvd_ram", profile
);
432 log_debug("profile 0x%02x dvd_rw", profile
);
436 log_debug("profile 0x%02x dvd_plus_r", profile
);
440 log_debug("profile 0x%02x dvd_plus_rw", profile
);
444 log_debug("profile 0x%02x dvd_plus_rw_dl", profile
);
445 cd_dvd_plus_rw_dl
= 1;
448 log_debug("profile 0x%02x dvd_plus_r_dl", profile
);
449 cd_dvd_plus_r_dl
= 1;
453 log_debug("profile 0x%02x bd", profile
);
458 log_debug("profile 0x%02x bd_r", profile
);
462 log_debug("profile 0x%02x bd_re", profile
);
466 log_debug("profile 0x%02x hddvd", profile
);
470 log_debug("profile 0x%02x hddvd_r", profile
);
474 log_debug("profile 0x%02x hddvd_rw", profile
);
477 log_debug("profile 0x%02x <ignored>", profile
);
484 /* returns 0 if media was detected */
485 static int cd_profiles_old_mmc(struct udev
*udev
, int fd
)
490 unsigned char header
[32];
492 scsi_cmd_init(udev
, &sc
);
493 scsi_cmd_set(udev
, &sc
, 0, 0x51);
494 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
495 scsi_cmd_set(udev
, &sc
, 9, 0);
496 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
498 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
500 log_debug("no current profile, but disc is present; assuming CD-ROM");
502 cd_media_track_count
= 1;
503 cd_media_track_count_data
= 1;
506 log_debug("no current profile, assuming no media");
513 if (header
[2] & 16) {
515 log_debug("profile 0x0a media_cd_rw");
516 } else if ((header
[2] & 3) < 2 && cd_cd_r
) {
518 log_debug("profile 0x09 media_cd_r");
521 log_debug("profile 0x08 media_cd_rom");
526 /* returns 0 if media was detected */
527 static int cd_profiles(struct udev
*udev
, int fd
)
530 unsigned char features
[65530];
531 unsigned int cur_profile
= 0;
539 /* First query the current profile */
540 scsi_cmd_init(udev
, &sc
);
541 scsi_cmd_set(udev
, &sc
, 0, 0x46);
542 scsi_cmd_set(udev
, &sc
, 8, 8);
543 scsi_cmd_set(udev
, &sc
, 9, 0);
544 err
= scsi_cmd_run(udev
, &sc
, fd
, features
, 8);
546 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
547 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
548 if (SK(err
) == 0x5 && IN_SET(ASC(err
), 0x20, 0x24)) {
549 log_debug("drive is pre-MMC2 and does not support 46h get configuration command");
550 log_debug("trying to work around the problem");
551 ret
= cd_profiles_old_mmc(udev
, fd
);
556 cur_profile
= features
[6] << 8 | features
[7];
557 if (cur_profile
> 0) {
558 log_debug("current profile 0x%02x", cur_profile
);
559 feature_profile_media (udev
, cur_profile
);
560 ret
= 0; /* we have media */
562 log_debug("no current profile, assuming no media");
565 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
566 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len
);
568 if (len
> sizeof(features
)) {
569 log_debug("cannot get features in a single query, truncating");
570 len
= sizeof(features
);
572 len
= sizeof(features
);
574 /* Now get the full feature buffer */
575 scsi_cmd_init(udev
, &sc
);
576 scsi_cmd_set(udev
, &sc
, 0, 0x46);
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
, features
, len
);
582 info_scsi_cmd_err(udev
, "GET CONFIGURATION", err
);
586 /* parse the length once more, in case the drive decided to have other features suddenly :) */
587 len
= features
[0] << 24 | features
[1] << 16 | features
[2] << 8 | features
[3];
588 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len
);
590 if (len
> sizeof(features
)) {
591 log_debug("cannot get features in a single query, truncating");
592 len
= sizeof(features
);
595 /* device features */
596 for (i
= 8; i
+4 < len
; i
+= (4 + features
[i
+3])) {
597 unsigned int feature
;
599 feature
= features
[i
] << 8 | features
[i
+1];
603 log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features
[i
+3] / 4);
604 feature_profiles(udev
, &features
[i
]+4, MIN(features
[i
+3], len
- i
- 4));
607 log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature
, features
[i
+3]);
615 static int cd_media_info(struct udev
*udev
, int fd
)
618 unsigned char header
[32];
619 static const char *media_status
[] = {
627 scsi_cmd_init(udev
, &sc
);
628 scsi_cmd_set(udev
, &sc
, 0, 0x51);
629 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
630 scsi_cmd_set(udev
, &sc
, 9, 0);
631 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
633 info_scsi_cmd_err(udev
, "READ DISC INFORMATION", err
);
638 log_debug("disk type %02x", header
[8]);
639 log_debug("hardware reported media status: %s", media_status
[header
[2] & 3]);
641 /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
642 if (!cd_media_cd_rom
)
643 cd_media_state
= media_status
[header
[2] & 3];
645 /* fresh DVD-RW in restricted overwite mode reports itself as
646 * "appendable"; change it to "blank" to make it consistent with what
647 * gets reported after blanking, and what userspace expects */
648 if (cd_media_dvd_rw_ro
&& (header
[2] & 3) == 1)
649 cd_media_state
= media_status
[0];
651 /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
652 * always "complete", DVD-RAM are "other" or "complete" if the disc is
653 * write protected; we need to check the contents if it is blank */
654 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) {
655 unsigned char buffer
[32 * 2048];
659 if (cd_media_dvd_ram
) {
660 /* a write protected dvd-ram may report "complete" status */
662 unsigned char dvdstruct
[8];
663 unsigned char format
[12];
665 scsi_cmd_init(udev
, &sc
);
666 scsi_cmd_set(udev
, &sc
, 0, 0xAD);
667 scsi_cmd_set(udev
, &sc
, 7, 0xC0);
668 scsi_cmd_set(udev
, &sc
, 9, sizeof(dvdstruct
));
669 scsi_cmd_set(udev
, &sc
, 11, 0);
670 err
= scsi_cmd_run(udev
, &sc
, fd
, dvdstruct
, sizeof(dvdstruct
));
672 info_scsi_cmd_err(udev
, "READ DVD STRUCTURE", err
);
675 if (dvdstruct
[4] & 0x02) {
676 cd_media_state
= media_status
[2];
677 log_debug("write-protected DVD-RAM media inserted");
681 /* let's make sure we don't try to read unformatted media */
682 scsi_cmd_init(udev
, &sc
);
683 scsi_cmd_set(udev
, &sc
, 0, 0x23);
684 scsi_cmd_set(udev
, &sc
, 8, sizeof(format
));
685 scsi_cmd_set(udev
, &sc
, 9, 0);
686 err
= scsi_cmd_run(udev
, &sc
, fd
, format
, sizeof(format
));
688 info_scsi_cmd_err(udev
, "READ DVD FORMAT CAPACITIES", err
);
693 if (len
& 7 || len
< 16) {
694 log_debug("invalid format capacities length");
698 switch(format
[8] & 3) {
700 log_debug("unformatted DVD-RAM media inserted");
701 /* This means that last format was interrupted
702 * or failed, blank dvd-ram discs are factory
703 * formatted. Take no action here as it takes
704 * quite a while to reformat a dvd-ram and it's
705 * not automatically started */
709 log_debug("formatted DVD-RAM media inserted");
713 cd_media
= 0; //return no media
714 log_debug("format capacities returned no media");
719 /* Take a closer look at formatted media (unformatted DVD+RW
720 * has "blank" status", DVD-RAM was examined earlier) and check
721 * for ISO and UDF PVDs or a fs superblock presence and do it
722 * in one ioctl (we need just sectors 0 and 16) */
723 scsi_cmd_init(udev
, &sc
);
724 scsi_cmd_set(udev
, &sc
, 0, 0x28);
725 scsi_cmd_set(udev
, &sc
, 5, 0);
726 scsi_cmd_set(udev
, &sc
, 8, 32);
727 scsi_cmd_set(udev
, &sc
, 9, 0);
728 err
= scsi_cmd_run(udev
, &sc
, fd
, buffer
, sizeof(buffer
));
731 info_scsi_cmd_err(udev
, "READ FIRST 32 BLOCKS", err
);
735 /* if any non-zero data is found in sector 16 (iso and udf) or
736 * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
737 * is assumed non-blank */
739 for (offset
= 32768; offset
< (32768 + 2048); offset
++) {
740 if (buffer
[offset
]) {
741 log_debug("data in block 16, assuming complete");
746 for (offset
= 0; offset
< 2048; offset
++) {
747 if (buffer
[offset
]) {
748 log_debug("data in block 0, assuming complete");
753 cd_media_state
= media_status
[0];
754 log_debug("no data in blocks 0 or 16, assuming blank");
758 /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
759 * restricted overwrite mode can never append, only in sequential mode */
760 if ((header
[2] & 3) < 2 && !cd_media_dvd_rw_ro
)
761 cd_media_session_next
= header
[10] << 8 | header
[5];
762 cd_media_session_count
= header
[9] << 8 | header
[4];
763 cd_media_track_count
= header
[11] << 8 | header
[6];
768 static int cd_media_toc(struct udev
*udev
, int fd
)
771 unsigned char header
[12];
772 unsigned char toc
[65536];
773 unsigned int len
, i
, num_tracks
;
777 scsi_cmd_init(udev
, &sc
);
778 scsi_cmd_set(udev
, &sc
, 0, 0x43);
779 scsi_cmd_set(udev
, &sc
, 6, 1);
780 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
) & 0xff);
781 scsi_cmd_set(udev
, &sc
, 9, 0);
782 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
784 info_scsi_cmd_err(udev
, "READ TOC", err
);
788 len
= (header
[0] << 8 | header
[1]) + 2;
789 log_debug("READ TOC: len: %d, start track: %d, end track: %d", len
, header
[2], header
[3]);
790 if (len
> sizeof(toc
))
794 /* 2: first track, 3: last track */
795 num_tracks
= header
[3] - header
[2] + 1;
797 /* empty media has no tracks */
801 scsi_cmd_init(udev
, &sc
);
802 scsi_cmd_set(udev
, &sc
, 0, 0x43);
803 scsi_cmd_set(udev
, &sc
, 6, header
[2]); /* First Track/Session Number */
804 scsi_cmd_set(udev
, &sc
, 7, (len
>> 8) & 0xff);
805 scsi_cmd_set(udev
, &sc
, 8, len
& 0xff);
806 scsi_cmd_set(udev
, &sc
, 9, 0);
807 err
= scsi_cmd_run(udev
, &sc
, fd
, toc
, len
);
809 info_scsi_cmd_err(udev
, "READ TOC (tracks)", err
);
813 /* Take care to not iterate beyond the last valid track as specified in
814 * the TOC, but also avoid going beyond the TOC length, just in case
815 * the last track number is invalidly large */
816 for (p
= toc
+4, i
= 4; i
< len
-8 && num_tracks
> 0; i
+= 8, p
+= 8, --num_tracks
) {
818 unsigned int is_data_track
;
820 is_data_track
= (p
[1] & 0x04) != 0;
822 block
= p
[4] << 24 | p
[5] << 16 | p
[6] << 8 | p
[7];
823 log_debug("track=%u info=0x%x(%s) start_block=%u",
824 p
[2], p
[1] & 0x0f, is_data_track
? "data":"audio", block
);
827 cd_media_track_count_data
++;
829 cd_media_track_count_audio
++;
832 scsi_cmd_init(udev
, &sc
);
833 scsi_cmd_set(udev
, &sc
, 0, 0x43);
834 scsi_cmd_set(udev
, &sc
, 2, 1); /* Session Info */
835 scsi_cmd_set(udev
, &sc
, 8, sizeof(header
));
836 scsi_cmd_set(udev
, &sc
, 9, 0);
837 err
= scsi_cmd_run(udev
, &sc
, fd
, header
, sizeof(header
));
839 info_scsi_cmd_err(udev
, "READ TOC (multi session)", err
);
842 len
= header
[4+4] << 24 | header
[4+5] << 16 | header
[4+6] << 8 | header
[4+7];
843 log_debug("last track %u starts at block %u", header
[4+2], len
);
844 cd_media_session_last_offset
= (unsigned long long int)len
* 2048;
848 int main(int argc
, char *argv
[]) {
850 static const struct option options
[] = {
851 { "lock-media", no_argument
, NULL
, 'l' },
852 { "unlock-media", no_argument
, NULL
, 'u' },
853 { "eject-media", no_argument
, NULL
, 'e' },
854 { "debug", no_argument
, NULL
, 'd' },
855 { "help", no_argument
, NULL
, 'h' },
861 const char *node
= NULL
;
866 log_set_target(LOG_TARGET_AUTO
);
868 log_parse_environment();
878 option
= getopt_long(argc
, argv
, "deluh", options
, NULL
);
893 log_set_target(LOG_TARGET_CONSOLE
);
894 log_set_max_level(LOG_DEBUG
);
898 printf("Usage: cdrom_id [options] <device>\n"
899 " -l,--lock-media lock the media (to enable eject request events)\n"
900 " -u,--unlock-media unlock the media\n"
901 " -e,--eject-media eject the media\n"
902 " -d,--debug debug to stderr\n"
903 " -h,--help print this help text\n\n");
913 log_error("no device");
914 fprintf(stderr
, "no device\n");
920 for (cnt
= 20; cnt
> 0; cnt
--) {
921 struct timespec duration
;
923 fd
= open(node
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|(is_mounted(node
) ? 0 : O_EXCL
));
924 if (fd
>= 0 || errno
!= EBUSY
)
927 duration
.tv_nsec
= (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
928 nanosleep(&duration
, NULL
);
931 log_debug("unable to open '%s'", node
);
932 fprintf(stderr
, "unable to open '%s'\n", node
);
936 log_debug("probing: '%s'", node
);
938 /* same data as original cdrom_id */
939 if (cd_capability_compat(udev
, fd
) < 0) {
944 /* check for media - don't bail if there's no media as we still need to
945 * to read profiles */
946 cd_media_compat(udev
, fd
);
948 /* check if drive talks MMC */
949 if (cd_inquiry(udev
, fd
) < 0)
952 /* read drive and possibly current profile */
953 if (cd_profiles(udev
, fd
) != 0)
956 /* at this point we are guaranteed to have media in the drive - find out more about it */
958 /* get session/track info */
959 cd_media_toc(udev
, fd
);
961 /* get writable media state */
962 cd_media_info(udev
, fd
);
965 /* lock the media, so we enable eject button events */
966 if (lock
&& cd_media
) {
967 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)");
968 media_lock(udev
, fd
, true);
971 if (unlock
&& cd_media
) {
972 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
973 media_lock(udev
, fd
, false);
977 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
978 media_lock(udev
, fd
, false);
979 log_debug("START_STOP_UNIT (eject)");
980 media_eject(udev
, fd
);
983 printf("ID_CDROM=1\n");
985 printf("ID_CDROM_CD=1\n");
987 printf("ID_CDROM_CD_R=1\n");
989 printf("ID_CDROM_CD_RW=1\n");
991 printf("ID_CDROM_DVD=1\n");
993 printf("ID_CDROM_DVD_R=1\n");
995 printf("ID_CDROM_DVD_RW=1\n");
997 printf("ID_CDROM_DVD_RAM=1\n");
999 printf("ID_CDROM_DVD_PLUS_R=1\n");
1001 printf("ID_CDROM_DVD_PLUS_RW=1\n");
1002 if (cd_dvd_plus_r_dl
)
1003 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
1004 if (cd_dvd_plus_rw_dl
)
1005 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
1007 printf("ID_CDROM_BD=1\n");
1009 printf("ID_CDROM_BD_R=1\n");
1011 printf("ID_CDROM_BD_RE=1\n");
1013 printf("ID_CDROM_HDDVD=1\n");
1015 printf("ID_CDROM_HDDVD_R=1\n");
1017 printf("ID_CDROM_HDDVD_RW=1\n");
1019 printf("ID_CDROM_MO=1\n");
1021 printf("ID_CDROM_MRW=1\n");
1023 printf("ID_CDROM_MRW_W=1\n");
1026 printf("ID_CDROM_MEDIA=1\n");
1028 printf("ID_CDROM_MEDIA_MO=1\n");
1030 printf("ID_CDROM_MEDIA_MRW=1\n");
1032 printf("ID_CDROM_MEDIA_MRW_W=1\n");
1033 if (cd_media_cd_rom
)
1034 printf("ID_CDROM_MEDIA_CD=1\n");
1036 printf("ID_CDROM_MEDIA_CD_R=1\n");
1038 printf("ID_CDROM_MEDIA_CD_RW=1\n");
1039 if (cd_media_dvd_rom
)
1040 printf("ID_CDROM_MEDIA_DVD=1\n");
1042 printf("ID_CDROM_MEDIA_DVD_R=1\n");
1043 if (cd_media_dvd_ram
)
1044 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
1045 if (cd_media_dvd_rw
)
1046 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
1047 if (cd_media_dvd_plus_r
)
1048 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
1049 if (cd_media_dvd_plus_rw
)
1050 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
1051 if (cd_media_dvd_plus_rw_dl
)
1052 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
1053 if (cd_media_dvd_plus_r_dl
)
1054 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
1056 printf("ID_CDROM_MEDIA_BD=1\n");
1058 printf("ID_CDROM_MEDIA_BD_R=1\n");
1060 printf("ID_CDROM_MEDIA_BD_RE=1\n");
1062 printf("ID_CDROM_MEDIA_HDDVD=1\n");
1063 if (cd_media_hddvd_r
)
1064 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
1065 if (cd_media_hddvd_rw
)
1066 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
1068 if (cd_media_state
!= NULL
)
1069 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state
);
1070 if (cd_media_session_next
> 0)
1071 printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next
);
1072 if (cd_media_session_count
> 0)
1073 printf("ID_CDROM_MEDIA_SESSION_COUNT=%u\n", cd_media_session_count
);
1074 if (cd_media_session_count
> 1 && cd_media_session_last_offset
> 0)
1075 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset
);
1076 if (cd_media_track_count
> 0)
1077 printf("ID_CDROM_MEDIA_TRACK_COUNT=%u\n", cd_media_track_count
);
1078 if (cd_media_track_count_audio
> 0)
1079 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%u\n", cd_media_track_count_audio
);
1080 if (cd_media_track_count_data
> 0)
1081 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%u\n", cd_media_track_count_data
);