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