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