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