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