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