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