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