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