chiark / gitweb /
cdrom_id: Also apply format check to DVD-RW
[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_old_mmc(struct udev *udev, int fd)
352 {
353         struct scsi_cmd sc;
354         int err;
355
356         unsigned char header[32];
357
358         scsi_cmd_init(udev, &sc, header, sizeof(header));
359         scsi_cmd_set(udev, &sc, 0, 0x51);
360         scsi_cmd_set(udev, &sc, 8, sizeof(header));
361         scsi_cmd_set(udev, &sc, 9, 0);
362         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
363         if ((err != 0)) {
364                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
365                 info(udev, "no current profile, assuming no media\n");
366                 return -1;
367         };
368
369         cd_media = 1;
370
371         if (header[2] & 16) {
372                 cd_media_cd_rw = 1;
373                 info(udev, "profile 0x0a media_cd_rw\n");
374         }       
375         else if ((header[2] & 3) < 2 && cd_cd_r) { 
376                 cd_media_cd_r = 1;
377                 info(udev, "profile 0x09 media_cd_r\n");
378         }
379         else {
380         cd_media_cd_rom = 1;
381         info(udev, "profile 0x08 media_cd_rom\n");
382         }
383         return 0;
384 }
385
386 static int cd_profiles(struct udev *udev, int fd)
387 {
388         struct scsi_cmd sc;
389         unsigned char features[65530];
390         unsigned int cur_profile = 0;
391         unsigned int len;
392         unsigned int i;
393         int err;
394
395         scsi_cmd_init(udev, &sc, features, sizeof(features));
396         scsi_cmd_set(udev, &sc, 0, 0x46);
397         scsi_cmd_set(udev, &sc, 7, (sizeof(features) >> 8) & 0xff);
398         scsi_cmd_set(udev, &sc, 8, sizeof(features) & 0xff);
399         scsi_cmd_set(udev, &sc, 9, 0);
400         err = scsi_cmd_run(udev, &sc, fd, features, sizeof(features));
401         if ((err != 0)) {
402                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
403                 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
404                 if (SK(err) == 0x5 && ASC(err) == 0x20) {
405                         info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n");
406                         info(udev, "trying to work around the problem\n");
407                         return cd_profiles_old_mmc(udev, fd);
408                 }
409                 return -1;
410         }
411
412         len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
413         info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);
414
415         if (len > sizeof(features)) {
416                 info(udev, "can not get features in a single query, truncating\n");
417                 len = sizeof(features);
418         }
419
420         /* device features */
421         for (i = 8; i+4 < len; i += (4 + features[i+3])) {
422                 unsigned int feature;
423
424                 feature = features[i] << 8 | features[i+1];
425
426                 switch (feature) {
427                 case 0x00:
428                         info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
429                         feature_profiles(udev, &features[i]+4, features[i+3]);
430                         break;
431                 default:
432                         info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
433                         break;
434                 }
435         }
436
437         cur_profile = features[6] << 8 | features[7];
438         if (cur_profile > 0) {
439                 info(udev, "current profile 0x%02x\n", cur_profile);
440         } else {
441                 info(udev, "no current profile, assuming no media\n");
442                 return -1;
443         }
444
445         switch (cur_profile) {
446         case 0x03:
447         case 0x04:
448         case 0x05:
449                 info(udev, "profile 0x%02x \n", cur_profile);
450                 cd_media = 1;
451                 cd_media_mo = 1;
452                 break;
453         case 0x08:
454                 info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
455                 cd_media = 1;
456                 cd_media_cd_rom = 1;
457                 break;
458         case 0x09:
459                 info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
460                 cd_media = 1;
461                 cd_media_cd_r = 1;
462                 break;
463         case 0x0a:
464                 info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
465                 cd_media = 1;
466                 cd_media_cd_rw = 1;
467                 break;
468         case 0x10:
469                 info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
470                 cd_media = 1;
471                 cd_media_dvd_rom = 1;
472                 break;
473         case 0x11:
474                 info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
475                 cd_media = 1;
476                 cd_media_dvd_r = 1;
477                 break;
478         case 0x12:
479                 info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
480                 cd_media = 1;
481                 cd_media_dvd_ram = 1;
482                 break;
483         case 0x13:
484         case 0x14:
485                 info(udev, "profile 0x%02x media_dvd_rw\n", cur_profile);
486                 cd_media = 1;
487                 cd_media_dvd_rw = 1;
488                 break;
489         case 0x1B:
490                 info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
491                 cd_media = 1;
492                 cd_media_dvd_plus_r = 1;
493                 break;
494         case 0x1A:
495                 info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
496                 cd_media = 1;
497                 cd_media_dvd_plus_rw = 1;
498                 break;
499         case 0x2A:
500                 info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
501                 cd_media = 1;
502                 cd_media_dvd_plus_rw_dl = 1;
503                 break;
504         case 0x2B:
505                 info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
506                 cd_media = 1;
507                 cd_media_dvd_plus_r_dl = 1;
508                 break;
509         case 0x40:
510                 info(udev, "profile 0x%02x media_bd\n", cur_profile);
511                 cd_media = 1;
512                 cd_media_bd = 1;
513                 break;
514         case 0x41:
515         case 0x42:
516                 info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
517                 cd_media = 1;
518                 cd_media_bd_r = 1;
519                 break;
520         case 0x43:
521                 info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
522                 cd_media = 1;
523                 cd_media_bd_re = 1;
524                 break;
525         case 0x50:
526                 info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
527                 cd_media = 1;
528                 cd_media_hddvd = 1;
529                 break;
530         case 0x51:
531                 info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
532                 cd_media = 1;
533                 cd_media_hddvd_r = 1;
534                 break;
535         case 0x52:
536                 info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
537                 cd_media = 1;
538                 cd_media_hddvd_rw = 1;
539                 break;
540         default:
541                 info(udev, "profile 0x%02x <ignored>\n", cur_profile);
542                 break;
543         }
544         return 0;
545 }
546
547 static int cd_media_info(struct udev *udev, int fd)
548 {
549         struct scsi_cmd sc;
550         unsigned char header[32];
551         static const char *media_status[] = {
552                 "blank",
553                 "appendable",
554                 "complete",
555                 "other"
556         };
557         int err;
558
559         scsi_cmd_init(udev, &sc, header, sizeof(header));
560         scsi_cmd_set(udev, &sc, 0, 0x51);
561         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
562         scsi_cmd_set(udev, &sc, 9, 0);
563         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
564         if ((err != 0)) {
565                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
566                 return -1;
567         };
568
569         cd_media = 1;
570         info(udev, "disk type %02x\n", header[8]);
571
572         /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
573         if (!cd_media_cd_rom)
574                 cd_media_state = media_status[header[2] & 3];
575
576         /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
577          * always "complete", DVD-RAM are "other" or "complete" if the disc is
578          * write protected; we need to check the contents if it is blank */
579         if ((cd_media_dvd_rw || cd_media_dvd_plus_rw || cd_media_dvd_plus_rw_dl || cd_media_dvd_ram) && (header[2] & 3) > 1) {
580                 unsigned char buffer[17 * 2048];
581                 unsigned char result, len;
582                 int block, offset;
583
584                 if (cd_media_dvd_ram) {
585                         /* a write protected dvd-ram may report "complete" status */
586
587                         unsigned char dvdstruct[8];
588                         unsigned char format[12];
589
590                         scsi_cmd_init(udev, &sc, dvdstruct, sizeof(dvdstruct));
591                         scsi_cmd_set(udev, &sc, 0, 0xAD);
592                         scsi_cmd_set(udev, &sc, 7, 0xC0);
593                         scsi_cmd_set(udev, &sc, 9, sizeof(dvdstruct));
594                         scsi_cmd_set(udev, &sc, 11, 0);
595                         err = scsi_cmd_run(udev, &sc, fd, dvdstruct, sizeof(dvdstruct));
596                         if ((err != 0)) {
597                                 info_scsi_cmd_err(udev, "READ DVD STRUCTURE", err);
598                                 return -1;
599                         }
600                         if (dvdstruct[4] & 0x02) {
601                                 cd_media_state = media_status[2];
602                                 info(udev, "write-protected DVD-RAM media inserted\n");
603                                 goto determined;
604                         }
605
606                         /* let's make sure we don't try to read unformatted media */
607                         scsi_cmd_init(udev, &sc, format, sizeof(format));
608                         scsi_cmd_set(udev, &sc, 0, 0x23);
609                         scsi_cmd_set(udev, &sc, 8, sizeof(format));
610                         scsi_cmd_set(udev, &sc, 9, 0);
611                         err = scsi_cmd_run(udev, &sc, fd, format, sizeof(format));
612                         if ((err != 0)) {
613                                 info_scsi_cmd_err(udev, "READ DVD FORMAT CAPACITIES", err);
614                                 return -1;
615                         }
616
617                         len = format[3];
618                         if (len & 7 || len < 16) {
619                                 info(udev, "invalid format capacities length\n");
620                                 return -1;
621                         }
622
623                         switch(format[8] & 3) {
624                             case 1:
625                                 info(udev, "unformatted DVD-RAM media inserted\n");
626                                 /* This means that last format was interrupted
627                                  * or failed, blank dvd-ram discs are factory
628                                  * formatted. Take no action here as it takes
629                                  * quite a while to reformat a dvd-ram and it's
630                                  * not automatically started */
631                                 goto determined;
632
633                             case 2:
634                                 info(udev, "formatted DVD-RAM media inserted\n");
635                                 break;
636
637                             case 3:
638                                 cd_media = 0; //return no media
639                                 info(udev, "format capacities returned no media\n");
640                                 return -1;
641                         }
642                 }
643
644                 /* Take a closer look at formatted media (unformatted DVD+RW
645                  * has "blank" status", DVD-RAM was examined earlier) and check
646                  * for ISO and UDF PVDs or a fs superblock presence and do it
647                  * in one ioctl (we need just sectors 0 and 16) */
648                 scsi_cmd_init(udev, &sc, buffer, sizeof(buffer));
649                 scsi_cmd_set(udev, &sc, 0, 0x28);
650                 scsi_cmd_set(udev, &sc, 5, 0);
651                 scsi_cmd_set(udev, &sc, 8, 17);
652                 scsi_cmd_set(udev, &sc, 9, 0);
653                 err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
654                 if ((err != 0)) {
655                         info_scsi_cmd_err(udev, "READ FIRST 32 BLOCKS", err);
656                         return -1;
657                 }
658
659                 /* if any non-zero data is found in sector 16 (iso and udf) or
660                  * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
661                  * is assumed non-blank */
662                 result = 0;
663
664                 for (block = 32768; block >= 0 && !result; block -= 32768) {
665                         offset = block;
666                         while (offset < (block + 2048) && !result) {
667                                 result = buffer [offset];
668                                 offset++;
669                         }
670                 }
671
672                 if (!result) {
673                         cd_media_state = media_status[0];
674                         info(udev, "no data in blocks 0 or 16, assuming blank\n");
675                 } else {
676                         info(udev, "data in blocks 0 or 16, assuming complete\n");
677                 }
678         }
679
680 determined:
681         if ((header[2] & 3) != 2)
682                 cd_media_session_next = header[10] << 8 | header[5];
683         cd_media_session_count = header[9] << 8 | header[4];
684         cd_media_track_count = header[11] << 8 | header[6];
685
686         return 0;
687 }
688
689 static int cd_media_toc(struct udev *udev, int fd)
690 {
691         struct scsi_cmd sc;
692         unsigned char header[12];
693         unsigned char toc[2048];
694         unsigned int len, i;
695         unsigned char *p;
696         int err;
697
698         scsi_cmd_init(udev, &sc, header, sizeof(header));
699         scsi_cmd_set(udev, &sc, 0, 0x43);
700         scsi_cmd_set(udev, &sc, 6, 1);
701         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
702         scsi_cmd_set(udev, &sc, 9, 0);
703         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
704         if ((err != 0)) {
705                 info_scsi_cmd_err(udev, "READ TOC", err);
706                 return -1;
707         }
708
709         len = (header[0] << 8 | header[1]) + 2;
710         info(udev, "READ TOC: len: %d\n", len);
711         if (len > sizeof(toc))
712                 return -1;
713         if (len < 2)
714                 return -1;
715
716         /* empty media has no tracks */
717         if (len < 8)
718                 return 0;
719
720         scsi_cmd_init(udev, &sc, toc, sizeof(toc));
721         scsi_cmd_set(udev, &sc, 0, 0x43);
722         scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
723         scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
724         scsi_cmd_set(udev, &sc, 8, len & 0xff);
725         scsi_cmd_set(udev, &sc, 9, 0);
726         err = scsi_cmd_run(udev, &sc, fd, toc, len);
727         if ((err != 0)) {
728                 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
729                 return -1;
730         }
731
732         for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
733                 unsigned int block;
734                 unsigned int is_data_track;
735
736                 is_data_track = (p[1] & 0x04) != 0;
737
738                 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
739                 info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
740                      p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
741
742                 if (is_data_track)
743                         cd_media_track_count_data++;
744                 else
745                         cd_media_track_count_audio++;
746         }
747
748         scsi_cmd_init(udev, &sc, header, sizeof(header));
749         scsi_cmd_set(udev, &sc, 0, 0x43);
750         scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
751         scsi_cmd_set(udev, &sc, 8, sizeof(header));
752         scsi_cmd_set(udev, &sc, 9, 0);
753         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
754         if ((err != 0)) {
755                 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
756                 return -1;
757         }
758         len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
759         info(udev, "last track %u starts at block %u\n", header[4+2], len);
760         cd_media_session_last_offset = (unsigned long long int)len * 2048;
761         return 0;
762 }
763
764 int main(int argc, char *argv[])
765 {
766         struct udev *udev;
767         static const struct option options[] = {
768                 { "export", no_argument, NULL, 'x' },
769                 { "debug", no_argument, NULL, 'd' },
770                 { "help", no_argument, NULL, 'h' },
771                 {}
772         };
773         const char *node = NULL;
774         int export = 0;
775         int fd = -1;
776         int cnt;
777         int rc = 0;
778
779         udev = udev_new();
780         if (udev == NULL)
781                 goto exit;
782
783         udev_log_init("cdrom_id");
784         udev_set_log_fn(udev, log_fn);
785
786         while (1) {
787                 int option;
788
789                 option = getopt_long(argc, argv, "dxh", options, NULL);
790                 if (option == -1)
791                         break;
792
793                 switch (option) {
794                 case 'd':
795                         debug = 1;
796                         if (udev_get_log_priority(udev) < LOG_INFO)
797                                 udev_set_log_priority(udev, LOG_INFO);
798                         break;
799                 case 'x':
800                         export = 1;
801                         break;
802                 case 'h':
803                         printf("Usage: cdrom_id [options] <device>\n"
804                                "  --export        export key/value pairs\n"
805                                "  --debug         debug to stderr\n"
806                                "  --help          print this help text\n\n");
807                         goto exit;
808                 default:
809                         rc = 1;
810                         goto exit;
811                 }
812         }
813
814         node = argv[optind];
815         if (!node) {
816                 err(udev, "no device\n");
817                 fprintf(stderr, "no device\n");
818                 rc = 1;
819                 goto exit;
820         }
821
822         srand((unsigned int)getpid());
823         for (cnt = 20; cnt > 0; cnt--) {
824                 struct timespec duration;
825
826                 fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
827                 if (fd >= 0 || errno != EBUSY)
828                         break;
829                 duration.tv_sec = 0;
830                 duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
831                 nanosleep(&duration, NULL);
832         }
833         if (fd < 0) {
834                 info(udev, "unable to open '%s'\n", node);
835                 fprintf(stderr, "unable to open '%s'\n", node);
836                 rc = 1;
837                 goto exit;
838         }
839         info(udev, "probing: '%s'\n", node);
840
841         /* same data as original cdrom_id */
842         if (cd_capability_compat(udev, fd) < 0) {
843                 rc = 1;
844                 goto exit;
845         }
846
847         /* check for media - don't bail if there's no media as we still need to
848          * to read profiles */
849         cd_media_compat(udev, fd);
850
851         /* check if drive talks MMC */
852         if (cd_inquiry(udev, fd) < 0)
853                 goto print;
854
855         /* read drive and possibly current profile */
856         if (cd_profiles(udev, fd) < 0)
857                 goto print;
858
859         /* get writable media state */
860         if (cd_media_info(udev, fd) < 0)
861                 goto print;
862
863         /* get session/track info */
864         if (cd_media_toc(udev, fd) < 0)
865                 goto print;
866
867 print:
868         printf("ID_CDROM=1\n");
869         if (cd_cd_rom)
870                 printf("ID_CDROM_CD=1\n");
871         if (cd_cd_r)
872                 printf("ID_CDROM_CD_R=1\n");
873         if (cd_cd_rw)
874                 printf("ID_CDROM_CD_RW=1\n");
875         if (cd_dvd_rom)
876                 printf("ID_CDROM_DVD=1\n");
877         if (cd_dvd_r)
878                 printf("ID_CDROM_DVD_R=1\n");
879         if (cd_dvd_rw)
880                 printf("ID_CDROM_DVD_RW=1\n");
881         if (cd_dvd_ram)
882                 printf("ID_CDROM_DVD_RAM=1\n");
883         if (cd_dvd_plus_r)
884                 printf("ID_CDROM_DVD_PLUS_R=1\n");
885         if (cd_dvd_plus_rw)
886                 printf("ID_CDROM_DVD_PLUS_RW=1\n");
887         if (cd_dvd_plus_r_dl)
888                 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
889         if (cd_dvd_plus_rw_dl)
890                 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
891         if (cd_bd)
892                 printf("ID_CDROM_BD=1\n");
893         if (cd_bd_r)
894                 printf("ID_CDROM_BD_R=1\n");
895         if (cd_bd_re)
896                 printf("ID_CDROM_BD_RE=1\n");
897         if (cd_hddvd)
898                 printf("ID_CDROM_HDDVD=1\n");
899         if (cd_hddvd_r)
900                 printf("ID_CDROM_HDDVD_R=1\n");
901         if (cd_hddvd_rw)
902                 printf("ID_CDROM_HDDVD_RW=1\n");
903         if (cd_mo)
904                 printf("ID_CDROM_MO=1\n");
905         if (cd_mrw)
906                 printf("ID_CDROM_MRW=1\n");
907         if (cd_mrw_w)
908                 printf("ID_CDROM_MRW_W=1\n");
909
910         if (cd_media)
911                 printf("ID_CDROM_MEDIA=1\n");
912         if (cd_media_mo)
913                 printf("ID_CDROM_MEDIA_MO=1\n");
914         if (cd_media_mrw)
915                 printf("ID_CDROM_MEDIA_MRW=1\n");
916         if (cd_media_mrw_w)
917                 printf("ID_CDROM_MEDIA_MRW_W=1\n");
918         if (cd_media_cd_rom)
919                 printf("ID_CDROM_MEDIA_CD=1\n");
920         if (cd_media_cd_r)
921                 printf("ID_CDROM_MEDIA_CD_R=1\n");
922         if (cd_media_cd_rw)
923                 printf("ID_CDROM_MEDIA_CD_RW=1\n");
924         if (cd_media_dvd_rom)
925                 printf("ID_CDROM_MEDIA_DVD=1\n");
926         if (cd_media_dvd_r)
927                 printf("ID_CDROM_MEDIA_DVD_R=1\n");
928         if (cd_media_dvd_ram)
929                 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
930         if (cd_media_dvd_rw)
931                 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
932         if (cd_media_dvd_plus_r)
933                 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
934         if (cd_media_dvd_plus_rw)
935                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
936         if (cd_media_dvd_plus_rw_dl)
937                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
938         if (cd_media_dvd_plus_r_dl)
939                 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
940         if (cd_media_bd)
941                 printf("ID_CDROM_MEDIA_BD=1\n");
942         if (cd_media_bd_r)
943                 printf("ID_CDROM_MEDIA_BD_R=1\n");
944         if (cd_media_bd_re)
945                 printf("ID_CDROM_MEDIA_BD_RE=1\n");
946         if (cd_media_hddvd)
947                 printf("ID_CDROM_MEDIA_HDDVD=1\n");
948         if (cd_media_hddvd_r)
949                 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
950         if (cd_media_hddvd_rw)
951                 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
952
953         if (cd_media_state != NULL)
954                 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
955         if (cd_media_session_next > 0)
956                 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
957         if (cd_media_session_count > 0)
958                 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
959         if (cd_media_session_count > 1 && cd_media_session_last_offset > 0)
960                 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
961         if (cd_media_track_count > 0)
962                 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
963         if (cd_media_track_count_audio > 0)
964                 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
965         if (cd_media_track_count_data > 0)
966                 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
967 exit:
968         if (fd >= 0)
969                 close(fd);
970         udev_unref(udev);
971         udev_log_close();
972         return rc;
973 }
974