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