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