chiark / gitweb /
use correct format types
[elogind.git] / src / udev / cdrom_id / cdrom_id.c
1 /*
2  * cdrom_id - optical drive and media information prober
3  *
4  * Copyright (C) 2008-2010 Kay Sievers <kay@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <time.h>
30 #include <scsi/sg.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/ioctl.h>
35 #include <linux/cdrom.h>
36
37 #include "libudev.h"
38 #include "libudev-private.h"
39
40 /* device info */
41 static unsigned int cd_cd_rom;
42 static unsigned int cd_cd_r;
43 static unsigned int cd_cd_rw;
44 static unsigned int cd_dvd_rom;
45 static unsigned int cd_dvd_r;
46 static unsigned int cd_dvd_rw;
47 static unsigned int cd_dvd_ram;
48 static unsigned int cd_dvd_plus_r;
49 static unsigned int cd_dvd_plus_rw;
50 static unsigned int cd_dvd_plus_r_dl;
51 static unsigned int cd_dvd_plus_rw_dl;
52 static unsigned int cd_bd;
53 static unsigned int cd_bd_r;
54 static unsigned int cd_bd_re;
55 static unsigned int cd_hddvd;
56 static unsigned int cd_hddvd_r;
57 static unsigned int cd_hddvd_rw;
58 static unsigned int cd_mo;
59 static unsigned int cd_mrw;
60 static unsigned int cd_mrw_w;
61
62 /* media info */
63 static unsigned int cd_media;
64 static unsigned int cd_media_cd_rom;
65 static unsigned int cd_media_cd_r;
66 static unsigned int cd_media_cd_rw;
67 static unsigned int cd_media_dvd_rom;
68 static unsigned int cd_media_dvd_r;
69 static unsigned int cd_media_dvd_rw;
70 static unsigned int cd_media_dvd_rw_ro; /* restricted overwrite mode */
71 static unsigned int cd_media_dvd_rw_seq; /* sequential mode */
72 static unsigned int cd_media_dvd_ram;
73 static unsigned int cd_media_dvd_plus_r;
74 static unsigned int cd_media_dvd_plus_rw;
75 static unsigned int cd_media_dvd_plus_r_dl;
76 static unsigned int cd_media_dvd_plus_rw_dl;
77 static unsigned int cd_media_bd;
78 static unsigned int cd_media_bd_r;
79 static unsigned int cd_media_bd_re;
80 static unsigned int cd_media_hddvd;
81 static unsigned int cd_media_hddvd_r;
82 static unsigned int cd_media_hddvd_rw;
83 static unsigned int cd_media_mo;
84 static unsigned int cd_media_mrw;
85 static unsigned int cd_media_mrw_w;
86
87 static const char *cd_media_state = NULL;
88 static unsigned int cd_media_session_next;
89 static unsigned int cd_media_session_count;
90 static unsigned int cd_media_track_count;
91 static unsigned int cd_media_track_count_data;
92 static unsigned int cd_media_track_count_audio;
93 static unsigned long long int cd_media_session_last_offset;
94
95 #define ERRCODE(s)        ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
96 #define SK(errcode)        (((errcode) >> 16) & 0xF)
97 #define ASC(errcode)        (((errcode) >> 8) & 0xFF)
98 #define ASCQ(errcode)        ((errcode) & 0xFF)
99
100 static bool is_mounted(const char *device)
101 {
102         struct stat statbuf;
103         FILE *fp;
104         int maj, min;
105         bool mounted = false;
106
107         if (stat(device, &statbuf) < 0)
108                 return -ENODEV;
109
110         fp = fopen("/proc/self/mountinfo", "re");
111         if (fp == NULL)
112                 return -ENOSYS;
113         while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
114                 if (makedev(maj, min) == statbuf.st_rdev) {
115                         mounted = true;
116                         break;
117                 }
118         }
119         fclose(fp);
120         return mounted;
121 }
122
123 static void info_scsi_cmd_err(struct udev *udev, const char *cmd, int err)
124 {
125         if (err == -1) {
126                 log_debug("%s failed", cmd);
127                 return;
128         }
129         log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
130 }
131
132 struct scsi_cmd {
133         struct cdrom_generic_command cgc;
134         union {
135                 struct request_sense s;
136                 unsigned char u[18];
137         } _sense;
138         struct sg_io_hdr sg_io;
139 };
140
141 static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd)
142 {
143         memzero(cmd, sizeof(struct scsi_cmd));
144         cmd->cgc.quiet = 1;
145         cmd->cgc.sense = &cmd->_sense.s;
146         cmd->sg_io.interface_id = 'S';
147         cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
148         cmd->sg_io.cmdp = cmd->cgc.cmd;
149         cmd->sg_io.sbp = cmd->_sense.u;
150         cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
151 }
152
153 static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
154 {
155         cmd->sg_io.cmd_len = i + 1;
156         cmd->cgc.cmd[i] = arg;
157 }
158
159 #define CHECK_CONDITION 0x01
160
161 static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
162 {
163         int ret = 0;
164
165         if (bufsize > 0) {
166                 cmd->sg_io.dxferp = buf;
167                 cmd->sg_io.dxfer_len = bufsize;
168                 cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
169         } else {
170                 cmd->sg_io.dxfer_direction = SG_DXFER_NONE;
171         }
172         if (ioctl(fd, SG_IO, &cmd->sg_io))
173                 return -1;
174
175         if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
176                 errno = EIO;
177                 ret = -1;
178                 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
179                         ret = ERRCODE(cmd->_sense.u);
180                         if (ret == 0)
181                                 ret = -1;
182                 }
183         }
184         return ret;
185 }
186
187 static int media_lock(struct udev *udev, int fd, bool lock)
188 {
189         int err;
190
191         /* disable the kernel's lock logic */
192         err = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK);
193         if (err < 0)
194                 log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed");
195
196         err = ioctl(fd, CDROM_LOCKDOOR, lock ? 1 : 0);
197         if (err < 0)
198                 log_debug("CDROM_LOCKDOOR failed");
199
200         return err;
201 }
202
203 static int media_eject(struct udev *udev, int fd)
204 {
205         struct scsi_cmd sc;
206         int err;
207
208         scsi_cmd_init(udev, &sc);
209         scsi_cmd_set(udev, &sc, 0, 0x1b);
210         scsi_cmd_set(udev, &sc, 4, 0x02);
211         scsi_cmd_set(udev, &sc, 5, 0);
212         err = scsi_cmd_run(udev, &sc, fd, NULL, 0);
213         if ((err != 0)) {
214                 info_scsi_cmd_err(udev, "START_STOP_UNIT", err);
215                 return -1;
216         }
217         return 0;
218 }
219
220 static int cd_capability_compat(struct udev *udev, int fd)
221 {
222         int capability;
223
224         capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
225         if (capability < 0) {
226                 log_debug("CDROM_GET_CAPABILITY failed");
227                 return -1;
228         }
229
230         if (capability & CDC_CD_R)
231                 cd_cd_r = 1;
232         if (capability & CDC_CD_RW)
233                 cd_cd_rw = 1;
234         if (capability & CDC_DVD)
235                 cd_dvd_rom = 1;
236         if (capability & CDC_DVD_R)
237                 cd_dvd_r = 1;
238         if (capability & CDC_DVD_RAM)
239                 cd_dvd_ram = 1;
240         if (capability & CDC_MRW)
241                 cd_mrw = 1;
242         if (capability & CDC_MRW_W)
243                 cd_mrw_w = 1;
244         return 0;
245 }
246
247 static int cd_media_compat(struct udev *udev, int fd)
248 {
249         if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
250                 log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
251                 return -1;
252         }
253         cd_media = 1;
254         return 0;
255 }
256
257 static int cd_inquiry(struct udev *udev, int fd)
258 {
259         struct scsi_cmd sc;
260         unsigned char inq[128];
261         int err;
262
263         scsi_cmd_init(udev, &sc);
264         scsi_cmd_set(udev, &sc, 0, 0x12);
265         scsi_cmd_set(udev, &sc, 4, 36);
266         scsi_cmd_set(udev, &sc, 5, 0);
267         err = scsi_cmd_run(udev, &sc, fd, inq, 36);
268         if ((err != 0)) {
269                 info_scsi_cmd_err(udev, "INQUIRY", err);
270                 return -1;
271         }
272
273         if ((inq[0] & 0x1F) != 5) {
274                 log_debug("not an MMC unit");
275                 return -1;
276         }
277
278         log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
279         return 0;
280 }
281
282 static void feature_profile_media(struct udev *udev, int cur_profile)
283 {
284         switch (cur_profile) {
285         case 0x03:
286         case 0x04:
287         case 0x05:
288                 log_debug("profile 0x%02x ", cur_profile);
289                 cd_media = 1;
290                 cd_media_mo = 1;
291                 break;
292         case 0x08:
293                 log_debug("profile 0x%02x media_cd_rom", cur_profile);
294                 cd_media = 1;
295                 cd_media_cd_rom = 1;
296                 break;
297         case 0x09:
298                 log_debug("profile 0x%02x media_cd_r", cur_profile);
299                 cd_media = 1;
300                 cd_media_cd_r = 1;
301                 break;
302         case 0x0a:
303                 log_debug("profile 0x%02x media_cd_rw", cur_profile);
304                 cd_media = 1;
305                 cd_media_cd_rw = 1;
306                 break;
307         case 0x10:
308                 log_debug("profile 0x%02x media_dvd_ro", cur_profile);
309                 cd_media = 1;
310                 cd_media_dvd_rom = 1;
311                 break;
312         case 0x11:
313                 log_debug("profile 0x%02x media_dvd_r", cur_profile);
314                 cd_media = 1;
315                 cd_media_dvd_r = 1;
316                 break;
317         case 0x12:
318                 log_debug("profile 0x%02x media_dvd_ram", cur_profile);
319                 cd_media = 1;
320                 cd_media_dvd_ram = 1;
321                 break;
322         case 0x13:
323                 log_debug("profile 0x%02x media_dvd_rw_ro", cur_profile);
324                 cd_media = 1;
325                 cd_media_dvd_rw = 1;
326                 cd_media_dvd_rw_ro = 1;
327                 break;
328         case 0x14:
329                 log_debug("profile 0x%02x media_dvd_rw_seq", cur_profile);
330                 cd_media = 1;
331                 cd_media_dvd_rw = 1;
332                 cd_media_dvd_rw_seq = 1;
333                 break;
334         case 0x1B:
335                 log_debug("profile 0x%02x media_dvd_plus_r", cur_profile);
336                 cd_media = 1;
337                 cd_media_dvd_plus_r = 1;
338                 break;
339         case 0x1A:
340                 log_debug("profile 0x%02x media_dvd_plus_rw", cur_profile);
341                 cd_media = 1;
342                 cd_media_dvd_plus_rw = 1;
343                 break;
344         case 0x2A:
345                 log_debug("profile 0x%02x media_dvd_plus_rw_dl", cur_profile);
346                 cd_media = 1;
347                 cd_media_dvd_plus_rw_dl = 1;
348                 break;
349         case 0x2B:
350                 log_debug("profile 0x%02x media_dvd_plus_r_dl", cur_profile);
351                 cd_media = 1;
352                 cd_media_dvd_plus_r_dl = 1;
353                 break;
354         case 0x40:
355                 log_debug("profile 0x%02x media_bd", cur_profile);
356                 cd_media = 1;
357                 cd_media_bd = 1;
358                 break;
359         case 0x41:
360         case 0x42:
361                 log_debug("profile 0x%02x media_bd_r", cur_profile);
362                 cd_media = 1;
363                 cd_media_bd_r = 1;
364                 break;
365         case 0x43:
366                 log_debug("profile 0x%02x media_bd_re", cur_profile);
367                 cd_media = 1;
368                 cd_media_bd_re = 1;
369                 break;
370         case 0x50:
371                 log_debug("profile 0x%02x media_hddvd", cur_profile);
372                 cd_media = 1;
373                 cd_media_hddvd = 1;
374                 break;
375         case 0x51:
376                 log_debug("profile 0x%02x media_hddvd_r", cur_profile);
377                 cd_media = 1;
378                 cd_media_hddvd_r = 1;
379                 break;
380         case 0x52:
381                 log_debug("profile 0x%02x media_hddvd_rw", cur_profile);
382                 cd_media = 1;
383                 cd_media_hddvd_rw = 1;
384                 break;
385         default:
386                 log_debug("profile 0x%02x <ignored>", cur_profile);
387                 break;
388         }
389 }
390
391 static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
392 {
393         unsigned int i;
394
395         for (i = 0; i+4 <= size; i += 4) {
396                 int profile;
397
398                 profile = profiles[i] << 8 | profiles[i+1];
399                 switch (profile) {
400                 case 0x03:
401                 case 0x04:
402                 case 0x05:
403                         log_debug("profile 0x%02x mo", profile);
404                         cd_mo = 1;
405                         break;
406                 case 0x08:
407                         log_debug("profile 0x%02x cd_rom", profile);
408                         cd_cd_rom = 1;
409                         break;
410                 case 0x09:
411                         log_debug("profile 0x%02x cd_r", profile);
412                         cd_cd_r = 1;
413                         break;
414                 case 0x0A:
415                         log_debug("profile 0x%02x cd_rw", profile);
416                         cd_cd_rw = 1;
417                         break;
418                 case 0x10:
419                         log_debug("profile 0x%02x dvd_rom", profile);
420                         cd_dvd_rom = 1;
421                         break;
422                 case 0x12:
423                         log_debug("profile 0x%02x dvd_ram", profile);
424                         cd_dvd_ram = 1;
425                         break;
426                 case 0x13:
427                 case 0x14:
428                         log_debug("profile 0x%02x dvd_rw", profile);
429                         cd_dvd_rw = 1;
430                         break;
431                 case 0x1B:
432                         log_debug("profile 0x%02x dvd_plus_r", profile);
433                         cd_dvd_plus_r = 1;
434                         break;
435                 case 0x1A:
436                         log_debug("profile 0x%02x dvd_plus_rw", profile);
437                         cd_dvd_plus_rw = 1;
438                         break;
439                 case 0x2A:
440                         log_debug("profile 0x%02x dvd_plus_rw_dl", profile);
441                         cd_dvd_plus_rw_dl = 1;
442                         break;
443                 case 0x2B:
444                         log_debug("profile 0x%02x dvd_plus_r_dl", profile);
445                         cd_dvd_plus_r_dl = 1;
446                         break;
447                 case 0x40:
448                         cd_bd = 1;
449                         log_debug("profile 0x%02x bd", profile);
450                         break;
451                 case 0x41:
452                 case 0x42:
453                         cd_bd_r = 1;
454                         log_debug("profile 0x%02x bd_r", profile);
455                         break;
456                 case 0x43:
457                         cd_bd_re = 1;
458                         log_debug("profile 0x%02x bd_re", profile);
459                         break;
460                 case 0x50:
461                         cd_hddvd = 1;
462                         log_debug("profile 0x%02x hddvd", profile);
463                         break;
464                 case 0x51:
465                         cd_hddvd_r = 1;
466                         log_debug("profile 0x%02x hddvd_r", profile);
467                         break;
468                 case 0x52:
469                         cd_hddvd_rw = 1;
470                         log_debug("profile 0x%02x hddvd_rw", profile);
471                         break;
472                 default:
473                         log_debug("profile 0x%02x <ignored>", profile);
474                         break;
475                 }
476         }
477         return 0;
478 }
479
480 /* returns 0 if media was detected */
481 static int cd_profiles_old_mmc(struct udev *udev, int fd)
482 {
483         struct scsi_cmd sc;
484         int err;
485
486         unsigned char header[32];
487
488         scsi_cmd_init(udev, &sc);
489         scsi_cmd_set(udev, &sc, 0, 0x51);
490         scsi_cmd_set(udev, &sc, 8, sizeof(header));
491         scsi_cmd_set(udev, &sc, 9, 0);
492         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
493         if ((err != 0)) {
494                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
495                 if (cd_media == 1) {
496                         log_debug("no current profile, but disc is present; assuming CD-ROM");
497                         cd_media_cd_rom = 1;
498                         cd_media_track_count = 1;
499                         cd_media_track_count_data = 1;
500                         return 0;
501                 } else {
502                         log_debug("no current profile, assuming no media");
503                         return -1;
504                 }
505         };
506
507         cd_media = 1;
508
509         if (header[2] & 16) {
510                 cd_media_cd_rw = 1;
511                 log_debug("profile 0x0a media_cd_rw");
512         } else if ((header[2] & 3) < 2 && cd_cd_r) {
513                 cd_media_cd_r = 1;
514                 log_debug("profile 0x09 media_cd_r");
515         } else {
516                 cd_media_cd_rom = 1;
517                 log_debug("profile 0x08 media_cd_rom");
518         }
519         return 0;
520 }
521
522 /* returns 0 if media was detected */
523 static int cd_profiles(struct udev *udev, int fd)
524 {
525         struct scsi_cmd sc;
526         unsigned char features[65530];
527         unsigned int cur_profile = 0;
528         unsigned int len;
529         unsigned int i;
530         int err;
531         int ret;
532
533         ret = -1;
534
535         /* First query the current profile */
536         scsi_cmd_init(udev, &sc);
537         scsi_cmd_set(udev, &sc, 0, 0x46);
538         scsi_cmd_set(udev, &sc, 8, 8);
539         scsi_cmd_set(udev, &sc, 9, 0);
540         err = scsi_cmd_run(udev, &sc, fd, features, 8);
541         if ((err != 0)) {
542                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
543                 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
544                 if (SK(err) == 0x5 && (ASC(err) == 0x20 || ASC(err) == 0x24)) {
545                         log_debug("drive is pre-MMC2 and does not support 46h get configuration command");
546                         log_debug("trying to work around the problem");
547                         ret = cd_profiles_old_mmc(udev, fd);
548                 }
549                 goto out;
550         }
551
552         cur_profile = features[6] << 8 | features[7];
553         if (cur_profile > 0) {
554                 log_debug("current profile 0x%02x", cur_profile);
555                 feature_profile_media (udev, cur_profile);
556                 ret = 0; /* we have media */
557         } else {
558                 log_debug("no current profile, assuming no media");
559         }
560
561         len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
562         log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
563
564         if (len > sizeof(features)) {
565                 log_debug("can not get features in a single query, truncating");
566                 len = sizeof(features);
567         } else if (len <= 8) {
568                 len = sizeof(features);
569         }
570
571         /* Now get the full feature buffer */
572         scsi_cmd_init(udev, &sc);
573         scsi_cmd_set(udev, &sc, 0, 0x46);
574         scsi_cmd_set(udev, &sc, 7, ( len >> 8 ) & 0xff);
575         scsi_cmd_set(udev, &sc, 8, len & 0xff);
576         scsi_cmd_set(udev, &sc, 9, 0);
577         err = scsi_cmd_run(udev, &sc, fd, features, len);
578         if ((err != 0)) {
579                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
580                 return -1;
581         }
582
583         /* parse the length once more, in case the drive decided to have other features suddenly :) */
584         len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
585         log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
586
587         if (len > sizeof(features)) {
588                 log_debug("can not get features in a single query, truncating");
589                 len = sizeof(features);
590         }
591
592         /* device features */
593         for (i = 8; i+4 < len; i += (4 + features[i+3])) {
594                 unsigned int feature;
595
596                 feature = features[i] << 8 | features[i+1];
597
598                 switch (feature) {
599                 case 0x00:
600                         log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features[i+3] / 4);
601                         feature_profiles(udev, &features[i]+4, MIN(features[i+3], len - i - 4));
602                         break;
603                 default:
604                         log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature, features[i+3]);
605                         break;
606                 }
607         }
608 out:
609         return ret;
610 }
611
612 static int cd_media_info(struct udev *udev, int fd)
613 {
614         struct scsi_cmd sc;
615         unsigned char header[32];
616         static const char *media_status[] = {
617                 "blank",
618                 "appendable",
619                 "complete",
620                 "other"
621         };
622         int err;
623
624         scsi_cmd_init(udev, &sc);
625         scsi_cmd_set(udev, &sc, 0, 0x51);
626         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
627         scsi_cmd_set(udev, &sc, 9, 0);
628         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
629         if ((err != 0)) {
630                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
631                 return -1;
632         };
633
634         cd_media = 1;
635         log_debug("disk type %02x", header[8]);
636         log_debug("hardware reported media status: %s", media_status[header[2] & 3]);
637
638         /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
639         if (!cd_media_cd_rom)
640                 cd_media_state = media_status[header[2] & 3];
641
642         /* fresh DVD-RW in restricted overwite mode reports itself as
643          * "appendable"; change it to "blank" to make it consistent with what
644          * gets reported after blanking, and what userspace expects  */
645         if (cd_media_dvd_rw_ro && (header[2] & 3) == 1)
646                 cd_media_state = media_status[0];
647
648         /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
649          * always "complete", DVD-RAM are "other" or "complete" if the disc is
650          * write protected; we need to check the contents if it is blank */
651         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) {
652                 unsigned char buffer[32 * 2048];
653                 unsigned char result, len;
654                 int block, offset;
655
656                 if (cd_media_dvd_ram) {
657                         /* a write protected dvd-ram may report "complete" status */
658
659                         unsigned char dvdstruct[8];
660                         unsigned char format[12];
661
662                         scsi_cmd_init(udev, &sc);
663                         scsi_cmd_set(udev, &sc, 0, 0xAD);
664                         scsi_cmd_set(udev, &sc, 7, 0xC0);
665                         scsi_cmd_set(udev, &sc, 9, sizeof(dvdstruct));
666                         scsi_cmd_set(udev, &sc, 11, 0);
667                         err = scsi_cmd_run(udev, &sc, fd, dvdstruct, sizeof(dvdstruct));
668                         if ((err != 0)) {
669                                 info_scsi_cmd_err(udev, "READ DVD STRUCTURE", err);
670                                 return -1;
671                         }
672                         if (dvdstruct[4] & 0x02) {
673                                 cd_media_state = media_status[2];
674                                 log_debug("write-protected DVD-RAM media inserted");
675                                 goto determined;
676                         }
677
678                         /* let's make sure we don't try to read unformatted media */
679                         scsi_cmd_init(udev, &sc);
680                         scsi_cmd_set(udev, &sc, 0, 0x23);
681                         scsi_cmd_set(udev, &sc, 8, sizeof(format));
682                         scsi_cmd_set(udev, &sc, 9, 0);
683                         err = scsi_cmd_run(udev, &sc, fd, format, sizeof(format));
684                         if ((err != 0)) {
685                                 info_scsi_cmd_err(udev, "READ DVD FORMAT CAPACITIES", err);
686                                 return -1;
687                         }
688
689                         len = format[3];
690                         if (len & 7 || len < 16) {
691                                 log_debug("invalid format capacities length");
692                                 return -1;
693                         }
694
695                         switch(format[8] & 3) {
696                             case 1:
697                                 log_debug("unformatted DVD-RAM media inserted");
698                                 /* This means that last format was interrupted
699                                  * or failed, blank dvd-ram discs are factory
700                                  * formatted. Take no action here as it takes
701                                  * quite a while to reformat a dvd-ram and it's
702                                  * not automatically started */
703                                 goto determined;
704
705                             case 2:
706                                 log_debug("formatted DVD-RAM media inserted");
707                                 break;
708
709                             case 3:
710                                 cd_media = 0; //return no media
711                                 log_debug("format capacities returned no media");
712                                 return -1;
713                         }
714                 }
715
716                 /* Take a closer look at formatted media (unformatted DVD+RW
717                  * has "blank" status", DVD-RAM was examined earlier) and check
718                  * for ISO and UDF PVDs or a fs superblock presence and do it
719                  * in one ioctl (we need just sectors 0 and 16) */
720                 scsi_cmd_init(udev, &sc);
721                 scsi_cmd_set(udev, &sc, 0, 0x28);
722                 scsi_cmd_set(udev, &sc, 5, 0);
723                 scsi_cmd_set(udev, &sc, 8, 32);
724                 scsi_cmd_set(udev, &sc, 9, 0);
725                 err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
726                 if ((err != 0)) {
727                         cd_media = 0;
728                         info_scsi_cmd_err(udev, "READ FIRST 32 BLOCKS", err);
729                         return -1;
730                 }
731
732                 /* if any non-zero data is found in sector 16 (iso and udf) or
733                  * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
734                  * is assumed non-blank */
735                 result = 0;
736
737                 for (block = 32768; block >= 0 && !result; block -= 32768) {
738                         offset = block;
739                         while (offset < (block + 2048) && !result) {
740                                 result = buffer [offset];
741                                 offset++;
742                         }
743                 }
744
745                 if (!result) {
746                         cd_media_state = media_status[0];
747                         log_debug("no data in blocks 0 or 16, assuming blank");
748                 } else {
749                         log_debug("data in blocks 0 or 16, assuming complete");
750                 }
751         }
752
753 determined:
754         /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
755          * restricted overwrite mode can never append, only in sequential mode */
756         if ((header[2] & 3) < 2 && !cd_media_dvd_rw_ro)
757                 cd_media_session_next = header[10] << 8 | header[5];
758         cd_media_session_count = header[9] << 8 | header[4];
759         cd_media_track_count = header[11] << 8 | header[6];
760
761         return 0;
762 }
763
764 static int cd_media_toc(struct udev *udev, int fd)
765 {
766         struct scsi_cmd sc;
767         unsigned char header[12];
768         unsigned char toc[65536];
769         unsigned int len, i, num_tracks;
770         unsigned char *p;
771         int err;
772
773         scsi_cmd_init(udev, &sc);
774         scsi_cmd_set(udev, &sc, 0, 0x43);
775         scsi_cmd_set(udev, &sc, 6, 1);
776         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
777         scsi_cmd_set(udev, &sc, 9, 0);
778         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
779         if ((err != 0)) {
780                 info_scsi_cmd_err(udev, "READ TOC", err);
781                 return -1;
782         }
783
784         len = (header[0] << 8 | header[1]) + 2;
785         log_debug("READ TOC: len: %d, start track: %d, end track: %d", len, header[2], header[3]);
786         if (len > sizeof(toc))
787                 return -1;
788         if (len < 2)
789                 return -1;
790         /* 2: first track, 3: last track */
791         num_tracks = header[3] - header[2] + 1;
792
793         /* empty media has no tracks */
794         if (len < 8)
795                 return 0;
796
797         scsi_cmd_init(udev, &sc);
798         scsi_cmd_set(udev, &sc, 0, 0x43);
799         scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
800         scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
801         scsi_cmd_set(udev, &sc, 8, len & 0xff);
802         scsi_cmd_set(udev, &sc, 9, 0);
803         err = scsi_cmd_run(udev, &sc, fd, toc, len);
804         if ((err != 0)) {
805                 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
806                 return -1;
807         }
808
809         /* Take care to not iterate beyond the last valid track as specified in
810          * the TOC, but also avoid going beyond the TOC length, just in case
811          * the last track number is invalidly large */
812         for (p = toc+4, i = 4; i < len-8 && num_tracks > 0; i += 8, p += 8, --num_tracks) {
813                 unsigned int block;
814                 unsigned int is_data_track;
815
816                 is_data_track = (p[1] & 0x04) != 0;
817
818                 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
819                 log_debug("track=%u info=0x%x(%s) start_block=%u",
820                      p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
821
822                 if (is_data_track)
823                         cd_media_track_count_data++;
824                 else
825                         cd_media_track_count_audio++;
826         }
827
828         scsi_cmd_init(udev, &sc);
829         scsi_cmd_set(udev, &sc, 0, 0x43);
830         scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
831         scsi_cmd_set(udev, &sc, 8, sizeof(header));
832         scsi_cmd_set(udev, &sc, 9, 0);
833         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
834         if ((err != 0)) {
835                 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
836                 return -1;
837         }
838         len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
839         log_debug("last track %u starts at block %u", header[4+2], len);
840         cd_media_session_last_offset = (unsigned long long int)len * 2048;
841         return 0;
842 }
843
844 int main(int argc, char *argv[])
845 {
846         struct udev *udev;
847         static const struct option options[] = {
848                 { "lock-media", no_argument, NULL, 'l' },
849                 { "unlock-media", no_argument, NULL, 'u' },
850                 { "eject-media", no_argument, NULL, 'e' },
851                 { "debug", no_argument, NULL, 'd' },
852                 { "help", no_argument, NULL, 'h' },
853                 {}
854         };
855         bool eject = false;
856         bool lock = false;
857         bool unlock = false;
858         const char *node = NULL;
859         int fd = -1;
860         int cnt;
861         int rc = 0;
862
863         log_parse_environment();
864         log_open();
865
866         udev = udev_new();
867         if (udev == NULL)
868                 goto exit;
869
870         while (1) {
871                 int option;
872
873                 option = getopt_long(argc, argv, "deluh", options, NULL);
874                 if (option == -1)
875                         break;
876
877                 switch (option) {
878                 case 'l':
879                         lock = true;
880                         break;
881                 case 'u':
882                         unlock = true;
883                         break;
884                 case 'e':
885                         eject = true;
886                         break;
887                 case 'd':
888                         log_set_target(LOG_TARGET_CONSOLE);
889                         log_set_max_level(LOG_DEBUG);
890                         log_open();
891                         break;
892                 case 'h':
893                         printf("Usage: cdrom_id [options] <device>\n"
894                                "  --lock-media    lock the media (to enable eject request events)\n"
895                                "  --unlock-media  unlock the media\n"
896                                "  --eject-media   eject the media\n"
897                                "  --debug         debug to stderr\n"
898                                "  --help          print this help text\n\n");
899                         goto exit;
900                 default:
901                         rc = 1;
902                         goto exit;
903                 }
904         }
905
906         node = argv[optind];
907         if (!node) {
908                 log_error("no device");
909                 fprintf(stderr, "no device\n");
910                 rc = 1;
911                 goto exit;
912         }
913
914         initialize_srand();
915         for (cnt = 20; cnt > 0; cnt--) {
916                 struct timespec duration;
917
918                 fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|(is_mounted(node) ? 0 : O_EXCL));
919                 if (fd >= 0 || errno != EBUSY)
920                         break;
921                 duration.tv_sec = 0;
922                 duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
923                 nanosleep(&duration, NULL);
924         }
925         if (fd < 0) {
926                 log_debug("unable to open '%s'", node);
927                 fprintf(stderr, "unable to open '%s'\n", node);
928                 rc = 1;
929                 goto exit;
930         }
931         log_debug("probing: '%s'", node);
932
933         /* same data as original cdrom_id */
934         if (cd_capability_compat(udev, fd) < 0) {
935                 rc = 1;
936                 goto exit;
937         }
938
939         /* check for media - don't bail if there's no media as we still need to
940          * to read profiles */
941         cd_media_compat(udev, fd);
942
943         /* check if drive talks MMC */
944         if (cd_inquiry(udev, fd) < 0)
945                 goto work;
946
947         /* read drive and possibly current profile */
948         if (cd_profiles(udev, fd) != 0)
949                 goto work;
950
951         /* at this point we are guaranteed to have media in the drive - find out more about it */
952
953         /* get session/track info */
954         cd_media_toc(udev, fd);
955
956         /* get writable media state */
957         cd_media_info(udev, fd);
958
959 work:
960         /* lock the media, so we enable eject button events */
961         if (lock && cd_media) {
962                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)");
963                 media_lock(udev, fd, true);
964         }
965
966         if (unlock && cd_media) {
967                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
968                 media_lock(udev, fd, false);
969         }
970
971         if (eject) {
972                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
973                 media_lock(udev, fd, false);
974                 log_debug("START_STOP_UNIT (eject)");
975                 media_eject(udev, fd);
976         }
977
978         printf("ID_CDROM=1\n");
979         if (cd_cd_rom)
980                 printf("ID_CDROM_CD=1\n");
981         if (cd_cd_r)
982                 printf("ID_CDROM_CD_R=1\n");
983         if (cd_cd_rw)
984                 printf("ID_CDROM_CD_RW=1\n");
985         if (cd_dvd_rom)
986                 printf("ID_CDROM_DVD=1\n");
987         if (cd_dvd_r)
988                 printf("ID_CDROM_DVD_R=1\n");
989         if (cd_dvd_rw)
990                 printf("ID_CDROM_DVD_RW=1\n");
991         if (cd_dvd_ram)
992                 printf("ID_CDROM_DVD_RAM=1\n");
993         if (cd_dvd_plus_r)
994                 printf("ID_CDROM_DVD_PLUS_R=1\n");
995         if (cd_dvd_plus_rw)
996                 printf("ID_CDROM_DVD_PLUS_RW=1\n");
997         if (cd_dvd_plus_r_dl)
998                 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
999         if (cd_dvd_plus_rw_dl)
1000                 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
1001         if (cd_bd)
1002                 printf("ID_CDROM_BD=1\n");
1003         if (cd_bd_r)
1004                 printf("ID_CDROM_BD_R=1\n");
1005         if (cd_bd_re)
1006                 printf("ID_CDROM_BD_RE=1\n");
1007         if (cd_hddvd)
1008                 printf("ID_CDROM_HDDVD=1\n");
1009         if (cd_hddvd_r)
1010                 printf("ID_CDROM_HDDVD_R=1\n");
1011         if (cd_hddvd_rw)
1012                 printf("ID_CDROM_HDDVD_RW=1\n");
1013         if (cd_mo)
1014                 printf("ID_CDROM_MO=1\n");
1015         if (cd_mrw)
1016                 printf("ID_CDROM_MRW=1\n");
1017         if (cd_mrw_w)
1018                 printf("ID_CDROM_MRW_W=1\n");
1019
1020         if (cd_media)
1021                 printf("ID_CDROM_MEDIA=1\n");
1022         if (cd_media_mo)
1023                 printf("ID_CDROM_MEDIA_MO=1\n");
1024         if (cd_media_mrw)
1025                 printf("ID_CDROM_MEDIA_MRW=1\n");
1026         if (cd_media_mrw_w)
1027                 printf("ID_CDROM_MEDIA_MRW_W=1\n");
1028         if (cd_media_cd_rom)
1029                 printf("ID_CDROM_MEDIA_CD=1\n");
1030         if (cd_media_cd_r)
1031                 printf("ID_CDROM_MEDIA_CD_R=1\n");
1032         if (cd_media_cd_rw)
1033                 printf("ID_CDROM_MEDIA_CD_RW=1\n");
1034         if (cd_media_dvd_rom)
1035                 printf("ID_CDROM_MEDIA_DVD=1\n");
1036         if (cd_media_dvd_r)
1037                 printf("ID_CDROM_MEDIA_DVD_R=1\n");
1038         if (cd_media_dvd_ram)
1039                 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
1040         if (cd_media_dvd_rw)
1041                 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
1042         if (cd_media_dvd_plus_r)
1043                 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
1044         if (cd_media_dvd_plus_rw)
1045                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
1046         if (cd_media_dvd_plus_rw_dl)
1047                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
1048         if (cd_media_dvd_plus_r_dl)
1049                 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
1050         if (cd_media_bd)
1051                 printf("ID_CDROM_MEDIA_BD=1\n");
1052         if (cd_media_bd_r)
1053                 printf("ID_CDROM_MEDIA_BD_R=1\n");
1054         if (cd_media_bd_re)
1055                 printf("ID_CDROM_MEDIA_BD_RE=1\n");
1056         if (cd_media_hddvd)
1057                 printf("ID_CDROM_MEDIA_HDDVD=1\n");
1058         if (cd_media_hddvd_r)
1059                 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
1060         if (cd_media_hddvd_rw)
1061                 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
1062
1063         if (cd_media_state != NULL)
1064                 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
1065         if (cd_media_session_next > 0)
1066                 printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next);
1067         if (cd_media_session_count > 0)
1068                 printf("ID_CDROM_MEDIA_SESSION_COUNT=%u\n", cd_media_session_count);
1069         if (cd_media_session_count > 1 && cd_media_session_last_offset > 0)
1070                 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
1071         if (cd_media_track_count > 0)
1072                 printf("ID_CDROM_MEDIA_TRACK_COUNT=%u\n", cd_media_track_count);
1073         if (cd_media_track_count_audio > 0)
1074                 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%u\n", cd_media_track_count_audio);
1075         if (cd_media_track_count_data > 0)
1076                 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%u\n", cd_media_track_count_data);
1077 exit:
1078         if (fd >= 0)
1079                 close(fd);
1080         udev_unref(udev);
1081         log_close();
1082         return rc;
1083 }