]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/cdrom_id/cdrom_id.c
Merge pull request #6910 from ssahani/issue-6359
[thirdparty/systemd.git] / src / udev / cdrom_id / cdrom_id.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
8f6919e6 2/*
fd7a285e 3 * cdrom_id - optical drive and media information prober
8f6919e6 4 *
1298001e 5 * Copyright (C) 2008-2010 Kay Sievers <kay@vrfy.org>
8f6919e6 6 *
55e9959b
KS
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.
8f6919e6 11 *
55e9959b
KS
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.
fd7a285e 16 *
55e9959b
KS
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/>.
8f6919e6
GKH
19 */
20
8f6919e6 21#include <errno.h>
07630cea 22#include <fcntl.h>
fd7a285e 23#include <getopt.h>
07630cea
LP
24#include <limits.h>
25#include <linux/cdrom.h>
fd7a285e 26#include <scsi/sg.h>
07630cea
LP
27#include <stddef.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/ioctl.h>
8f6919e6 32#include <sys/stat.h>
fd7a285e 33#include <sys/time.h>
07630cea
LP
34#include <sys/types.h>
35#include <time.h>
36#include <unistd.h>
01618658 37
cf0fbc49 38#include "libudev.h"
b4bbcaa9
TA
39
40#include "libudev-private.h"
3df3e884 41#include "random-util.h"
b237a168 42#include "udev-util.h"
8f6919e6 43
fd7a285e 44/* device info */
db8f32f6
KS
45static unsigned int cd_cd_rom;
46static unsigned int cd_cd_r;
47static unsigned int cd_cd_rw;
48static unsigned int cd_dvd_rom;
49static unsigned int cd_dvd_r;
50static unsigned int cd_dvd_rw;
51static unsigned int cd_dvd_ram;
52static unsigned int cd_dvd_plus_r;
53static unsigned int cd_dvd_plus_rw;
54static unsigned int cd_dvd_plus_r_dl;
55static unsigned int cd_dvd_plus_rw_dl;
56static unsigned int cd_bd;
57static unsigned int cd_bd_r;
58static unsigned int cd_bd_re;
59static unsigned int cd_hddvd;
60static unsigned int cd_hddvd_r;
61static unsigned int cd_hddvd_rw;
62static unsigned int cd_mo;
63static unsigned int cd_mrw;
64static unsigned int cd_mrw_w;
fd7a285e
KS
65
66/* media info */
db8f32f6
KS
67static unsigned int cd_media;
68static unsigned int cd_media_cd_rom;
69static unsigned int cd_media_cd_r;
70static unsigned int cd_media_cd_rw;
71static unsigned int cd_media_dvd_rom;
72static unsigned int cd_media_dvd_r;
73static unsigned int cd_media_dvd_rw;
74static unsigned int cd_media_dvd_rw_ro; /* restricted overwrite mode */
75static unsigned int cd_media_dvd_rw_seq; /* sequential mode */
76static unsigned int cd_media_dvd_ram;
77static unsigned int cd_media_dvd_plus_r;
78static unsigned int cd_media_dvd_plus_rw;
79static unsigned int cd_media_dvd_plus_r_dl;
80static unsigned int cd_media_dvd_plus_rw_dl;
81static unsigned int cd_media_bd;
82static unsigned int cd_media_bd_r;
83static unsigned int cd_media_bd_re;
84static unsigned int cd_media_hddvd;
85static unsigned int cd_media_hddvd_r;
86static unsigned int cd_media_hddvd_rw;
87static unsigned int cd_media_mo;
88static unsigned int cd_media_mrw;
89static unsigned int cd_media_mrw_w;
fd7a285e 90
816e6bf0 91static const char *cd_media_state = NULL;
db8f32f6
KS
92static unsigned int cd_media_session_next;
93static unsigned int cd_media_session_count;
94static unsigned int cd_media_track_count;
95static unsigned int cd_media_track_count_data;
96static unsigned int cd_media_track_count_audio;
97static unsigned long long int cd_media_session_last_offset;
fd7a285e 98
912541b0
KS
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)
fd7a285e 103
db8f32f6 104static bool is_mounted(const char *device)
38a3cde1 105{
912541b0
KS
106 struct stat statbuf;
107 FILE *fp;
108 int maj, min;
db8f32f6 109 bool mounted = false;
912541b0
KS
110
111 if (stat(device, &statbuf) < 0)
75677581 112 return false;
912541b0 113
47ef94ac 114 fp = fopen("/proc/self/mountinfo", "re");
912541b0 115 if (fp == NULL)
75677581 116 return false;
912541b0
KS
117 while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
118 if (makedev(maj, min) == statbuf.st_rdev) {
db8f32f6 119 mounted = true;
912541b0
KS
120 break;
121 }
122 }
123 fclose(fp);
124 return mounted;
38a3cde1
KS
125}
126
fc863dea 127static void info_scsi_cmd_err(struct udev *udev, const char *cmd, int err)
fd7a285e 128{
912541b0 129 if (err == -1) {
9f6445e3 130 log_debug("%s failed", cmd);
912541b0
KS
131 return;
132 }
9f6445e3 133 log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
fd7a285e
KS
134}
135
136struct scsi_cmd {
912541b0
KS
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;
fd7a285e
KS
143};
144
b20fd3cb 145static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd)
0413a47e 146{
29804cc1 147 memzero(cmd, sizeof(struct scsi_cmd));
912541b0
KS
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;
0413a47e
KS
155}
156
157static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
fd7a285e 158{
912541b0
KS
159 cmd->sg_io.cmd_len = i + 1;
160 cmd->cgc.cmd[i] = arg;
fd7a285e
KS
161}
162
163#define CHECK_CONDITION 0x01
164
7d563a17 165static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
fd7a285e 166{
912541b0
KS
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;
fd7a285e
KS
189}
190
b20fd3cb
KS
191static int media_lock(struct udev *udev, int fd, bool lock)
192{
912541b0 193 int err;
b20fd3cb 194
912541b0
KS
195 /* disable the kernel's lock logic */
196 err = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK);
197 if (err < 0)
9f6445e3 198 log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed");
b20fd3cb 199
912541b0
KS
200 err = ioctl(fd, CDROM_LOCKDOOR, lock ? 1 : 0);
201 if (err < 0)
9f6445e3 202 log_debug("CDROM_LOCKDOOR failed");
b20fd3cb 203
912541b0 204 return err;
b20fd3cb
KS
205}
206
207static int media_eject(struct udev *udev, int fd)
208{
912541b0
KS
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;
b20fd3cb
KS
222}
223
7d563a17 224static int cd_capability_compat(struct udev *udev, int fd)
fd7a285e 225{
912541b0
KS
226 int capability;
227
228 capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
229 if (capability < 0) {
9f6445e3 230 log_debug("CDROM_GET_CAPABILITY failed");
912541b0
KS
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;
fd7a285e
KS
249}
250
d6f0b22d
KS
251static int cd_media_compat(struct udev *udev, int fd)
252{
912541b0 253 if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
9f6445e3 254 log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
912541b0
KS
255 return -1;
256 }
257 cd_media = 1;
258 return 0;
d6f0b22d
KS
259}
260
b20fd3cb
KS
261static int cd_inquiry(struct udev *udev, int fd)
262{
912541b0
KS
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) {
9f6445e3 278 log_debug("not an MMC unit");
912541b0
KS
279 return -1;
280 }
281
9f6445e3 282 log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
912541b0 283 return 0;
fd7a285e
KS
284}
285
98ab6a37
DZ
286static void feature_profile_media(struct udev *udev, int cur_profile)
287{
912541b0
KS
288 switch (cur_profile) {
289 case 0x03:
290 case 0x04:
291 case 0x05:
9f6445e3 292 log_debug("profile 0x%02x ", cur_profile);
912541b0
KS
293 cd_media = 1;
294 cd_media_mo = 1;
295 break;
296 case 0x08:
9f6445e3 297 log_debug("profile 0x%02x media_cd_rom", cur_profile);
912541b0
KS
298 cd_media = 1;
299 cd_media_cd_rom = 1;
300 break;
301 case 0x09:
9f6445e3 302 log_debug("profile 0x%02x media_cd_r", cur_profile);
912541b0
KS
303 cd_media = 1;
304 cd_media_cd_r = 1;
305 break;
306 case 0x0a:
9f6445e3 307 log_debug("profile 0x%02x media_cd_rw", cur_profile);
912541b0
KS
308 cd_media = 1;
309 cd_media_cd_rw = 1;
310 break;
311 case 0x10:
9f6445e3 312 log_debug("profile 0x%02x media_dvd_ro", cur_profile);
912541b0
KS
313 cd_media = 1;
314 cd_media_dvd_rom = 1;
315 break;
316 case 0x11:
9f6445e3 317 log_debug("profile 0x%02x media_dvd_r", cur_profile);
912541b0
KS
318 cd_media = 1;
319 cd_media_dvd_r = 1;
320 break;
321 case 0x12:
9f6445e3 322 log_debug("profile 0x%02x media_dvd_ram", cur_profile);
912541b0
KS
323 cd_media = 1;
324 cd_media_dvd_ram = 1;
325 break;
326 case 0x13:
9f6445e3 327 log_debug("profile 0x%02x media_dvd_rw_ro", cur_profile);
912541b0
KS
328 cd_media = 1;
329 cd_media_dvd_rw = 1;
330 cd_media_dvd_rw_ro = 1;
331 break;
332 case 0x14:
9f6445e3 333 log_debug("profile 0x%02x media_dvd_rw_seq", cur_profile);
912541b0
KS
334 cd_media = 1;
335 cd_media_dvd_rw = 1;
336 cd_media_dvd_rw_seq = 1;
337 break;
338 case 0x1B:
9f6445e3 339 log_debug("profile 0x%02x media_dvd_plus_r", cur_profile);
912541b0
KS
340 cd_media = 1;
341 cd_media_dvd_plus_r = 1;
342 break;
343 case 0x1A:
9f6445e3 344 log_debug("profile 0x%02x media_dvd_plus_rw", cur_profile);
912541b0
KS
345 cd_media = 1;
346 cd_media_dvd_plus_rw = 1;
347 break;
348 case 0x2A:
9f6445e3 349 log_debug("profile 0x%02x media_dvd_plus_rw_dl", cur_profile);
912541b0
KS
350 cd_media = 1;
351 cd_media_dvd_plus_rw_dl = 1;
352 break;
353 case 0x2B:
9f6445e3 354 log_debug("profile 0x%02x media_dvd_plus_r_dl", cur_profile);
912541b0
KS
355 cd_media = 1;
356 cd_media_dvd_plus_r_dl = 1;
357 break;
358 case 0x40:
9f6445e3 359 log_debug("profile 0x%02x media_bd", cur_profile);
912541b0
KS
360 cd_media = 1;
361 cd_media_bd = 1;
362 break;
363 case 0x41:
364 case 0x42:
9f6445e3 365 log_debug("profile 0x%02x media_bd_r", cur_profile);
912541b0
KS
366 cd_media = 1;
367 cd_media_bd_r = 1;
368 break;
369 case 0x43:
9f6445e3 370 log_debug("profile 0x%02x media_bd_re", cur_profile);
912541b0
KS
371 cd_media = 1;
372 cd_media_bd_re = 1;
373 break;
374 case 0x50:
9f6445e3 375 log_debug("profile 0x%02x media_hddvd", cur_profile);
912541b0
KS
376 cd_media = 1;
377 cd_media_hddvd = 1;
378 break;
379 case 0x51:
9f6445e3 380 log_debug("profile 0x%02x media_hddvd_r", cur_profile);
912541b0
KS
381 cd_media = 1;
382 cd_media_hddvd_r = 1;
383 break;
384 case 0x52:
9f6445e3 385 log_debug("profile 0x%02x media_hddvd_rw", cur_profile);
912541b0
KS
386 cd_media = 1;
387 cd_media_hddvd_rw = 1;
388 break;
389 default:
9f6445e3 390 log_debug("profile 0x%02x <ignored>", cur_profile);
912541b0
KS
391 break;
392 }
98ab6a37
DZ
393}
394
0413a47e 395static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
fd7a285e 396{
912541b0
KS
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:
9f6445e3 407 log_debug("profile 0x%02x mo", profile);
912541b0
KS
408 cd_mo = 1;
409 break;
410 case 0x08:
9f6445e3 411 log_debug("profile 0x%02x cd_rom", profile);
912541b0
KS
412 cd_cd_rom = 1;
413 break;
414 case 0x09:
9f6445e3 415 log_debug("profile 0x%02x cd_r", profile);
912541b0
KS
416 cd_cd_r = 1;
417 break;
418 case 0x0A:
9f6445e3 419 log_debug("profile 0x%02x cd_rw", profile);
912541b0
KS
420 cd_cd_rw = 1;
421 break;
422 case 0x10:
9f6445e3 423 log_debug("profile 0x%02x dvd_rom", profile);
912541b0
KS
424 cd_dvd_rom = 1;
425 break;
426 case 0x12:
9f6445e3 427 log_debug("profile 0x%02x dvd_ram", profile);
912541b0
KS
428 cd_dvd_ram = 1;
429 break;
430 case 0x13:
431 case 0x14:
9f6445e3 432 log_debug("profile 0x%02x dvd_rw", profile);
912541b0
KS
433 cd_dvd_rw = 1;
434 break;
435 case 0x1B:
9f6445e3 436 log_debug("profile 0x%02x dvd_plus_r", profile);
912541b0
KS
437 cd_dvd_plus_r = 1;
438 break;
439 case 0x1A:
9f6445e3 440 log_debug("profile 0x%02x dvd_plus_rw", profile);
912541b0
KS
441 cd_dvd_plus_rw = 1;
442 break;
443 case 0x2A:
9f6445e3 444 log_debug("profile 0x%02x dvd_plus_rw_dl", profile);
912541b0
KS
445 cd_dvd_plus_rw_dl = 1;
446 break;
447 case 0x2B:
9f6445e3 448 log_debug("profile 0x%02x dvd_plus_r_dl", profile);
912541b0
KS
449 cd_dvd_plus_r_dl = 1;
450 break;
451 case 0x40:
452 cd_bd = 1;
9f6445e3 453 log_debug("profile 0x%02x bd", profile);
912541b0
KS
454 break;
455 case 0x41:
456 case 0x42:
457 cd_bd_r = 1;
9f6445e3 458 log_debug("profile 0x%02x bd_r", profile);
912541b0
KS
459 break;
460 case 0x43:
461 cd_bd_re = 1;
9f6445e3 462 log_debug("profile 0x%02x bd_re", profile);
912541b0
KS
463 break;
464 case 0x50:
465 cd_hddvd = 1;
9f6445e3 466 log_debug("profile 0x%02x hddvd", profile);
912541b0
KS
467 break;
468 case 0x51:
469 cd_hddvd_r = 1;
9f6445e3 470 log_debug("profile 0x%02x hddvd_r", profile);
912541b0
KS
471 break;
472 case 0x52:
473 cd_hddvd_rw = 1;
9f6445e3 474 log_debug("profile 0x%02x hddvd_rw", profile);
912541b0
KS
475 break;
476 default:
9f6445e3 477 log_debug("profile 0x%02x <ignored>", profile);
912541b0
KS
478 break;
479 }
480 }
481 return 0;
0413a47e
KS
482}
483
98ab6a37 484/* returns 0 if media was detected */
d83dfabe
JD
485static int cd_profiles_old_mmc(struct udev *udev, int fd)
486{
912541b0
KS
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) {
9f6445e3 500 log_debug("no current profile, but disc is present; assuming CD-ROM");
912541b0 501 cd_media_cd_rom = 1;
a0ec302b
HH
502 cd_media_track_count = 1;
503 cd_media_track_count_data = 1;
912541b0
KS
504 return 0;
505 } else {
9f6445e3 506 log_debug("no current profile, assuming no media");
912541b0
KS
507 return -1;
508 }
509 };
510
511 cd_media = 1;
512
513 if (header[2] & 16) {
514 cd_media_cd_rw = 1;
9f6445e3 515 log_debug("profile 0x0a media_cd_rw");
912541b0
KS
516 } else if ((header[2] & 3) < 2 && cd_cd_r) {
517 cd_media_cd_r = 1;
9f6445e3 518 log_debug("profile 0x09 media_cd_r");
912541b0
KS
519 } else {
520 cd_media_cd_rom = 1;
9f6445e3 521 log_debug("profile 0x08 media_cd_rom");
912541b0
KS
522 }
523 return 0;
d83dfabe
JD
524}
525
98ab6a37 526/* returns 0 if media was detected */
0413a47e
KS
527static int cd_profiles(struct udev *udev, int fd)
528{
912541b0
KS
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 */
4c701096 548 if (SK(err) == 0x5 && IN_SET(ASC(err), 0x20, 0x24)) {
9f6445e3
LP
549 log_debug("drive is pre-MMC2 and does not support 46h get configuration command");
550 log_debug("trying to work around the problem");
912541b0
KS
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) {
9f6445e3 558 log_debug("current profile 0x%02x", cur_profile);
912541b0
KS
559 feature_profile_media (udev, cur_profile);
560 ret = 0; /* we have media */
561 } else {
9f6445e3 562 log_debug("no current profile, assuming no media");
912541b0
KS
563 }
564
565 len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
9f6445e3 566 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
912541b0
KS
567
568 if (len > sizeof(features)) {
9f6445e3 569 log_debug("can not get features in a single query, truncating");
912541b0 570 len = sizeof(features);
1f6b4113 571 } else if (len <= 8)
912541b0 572 len = sizeof(features);
912541b0
KS
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];
9f6445e3 588 log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
912541b0
KS
589
590 if (len > sizeof(features)) {
9f6445e3 591 log_debug("can not get features in a single query, truncating");
912541b0
KS
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:
9f6445e3 603 log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features[i+3] / 4);
3fcd0960 604 feature_profiles(udev, &features[i]+4, MIN(features[i+3], len - i - 4));
912541b0
KS
605 break;
606 default:
9f6445e3 607 log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature, features[i+3]);
912541b0
KS
608 break;
609 }
610 }
98ab6a37 611out:
912541b0 612 return ret;
fd7a285e
KS
613}
614
7d563a17 615static int cd_media_info(struct udev *udev, int fd)
fd7a285e 616{
912541b0
KS
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;
9f6445e3
LP
638 log_debug("disk type %02x", header[8]);
639 log_debug("hardware reported media status: %s", media_status[header[2] & 3]);
912541b0
KS
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];
cb80d06c
HH
656 unsigned char len;
657 int offset;
912541b0
KS
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];
9f6445e3 677 log_debug("write-protected DVD-RAM media inserted");
912541b0
KS
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) {
9f6445e3 694 log_debug("invalid format capacities length");
912541b0
KS
695 return -1;
696 }
697
698 switch(format[8] & 3) {
699 case 1:
9f6445e3 700 log_debug("unformatted DVD-RAM media inserted");
912541b0
KS
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:
9f6445e3 709 log_debug("formatted DVD-RAM media inserted");
912541b0
KS
710 break;
711
712 case 3:
713 cd_media = 0; //return no media
9f6445e3 714 log_debug("format capacities returned no media");
912541b0
KS
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 */
912541b0 738
cb80d06c
HH
739 for (offset = 32768; offset < (32768 + 2048); offset++) {
740 if (buffer [offset]) {
741 log_debug("data in block 16, assuming complete");
742 goto determined;
912541b0
KS
743 }
744 }
745
cb80d06c
HH
746 for (offset = 0; offset < 2048; offset++) {
747 if (buffer [offset]) {
748 log_debug("data in block 0, assuming complete");
749 goto determined;
750 }
912541b0 751 }
cb80d06c
HH
752
753 cd_media_state = media_status[0];
754 log_debug("no data in blocks 0 or 16, assuming blank");
912541b0 755 }
1ef6c9e9
JD
756
757determined:
912541b0
KS
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;
fd7a285e
KS
766}
767
7d563a17 768static int cd_media_toc(struct udev *udev, int fd)
fd7a285e 769{
912541b0
KS
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;
9f6445e3 789 log_debug("READ TOC: len: %d, start track: %d, end track: %d", len, header[2], header[3]);
912541b0
KS
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];
9f6445e3 823 log_debug("track=%u info=0x%x(%s) start_block=%u",
912541b0
KS
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];
9f6445e3 843 log_debug("last track %u starts at block %u", header[4+2], len);
912541b0
KS
844 cd_media_session_last_offset = (unsigned long long int)len * 2048;
845 return 0;
fd7a285e
KS
846}
847
b237a168 848int main(int argc, char *argv[]) {
912541b0
KS
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
b237a168
ZJS
866 log_set_target(LOG_TARGET_AUTO);
867 udev_parse_config();
5168f84a
LP
868 log_parse_environment();
869 log_open();
870
912541b0
KS
871 udev = udev_new();
872 if (udev == NULL)
873 goto exit;
874
57255510 875 for (;;) {
912541b0
KS
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':
5168f84a 893 log_set_target(LOG_TARGET_CONSOLE);
e239cd8d 894 log_set_max_level(LOG_DEBUG);
5168f84a 895 log_open();
912541b0
KS
896 break;
897 case 'h':
898 printf("Usage: cdrom_id [options] <device>\n"
257e968d
RM
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");
912541b0
KS
904 goto exit;
905 default:
906 rc = 1;
907 goto exit;
908 }
909 }
910
911 node = argv[optind];
912 if (!node) {
9f6445e3 913 log_error("no device");
912541b0
KS
914 fprintf(stderr, "no device\n");
915 rc = 1;
916 goto exit;
917 }
918
ef309a68 919 initialize_srand();
912541b0
KS
920 for (cnt = 20; cnt > 0; cnt--) {
921 struct timespec duration;
922
c8a202b7 923 fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|(is_mounted(node) ? 0 : O_EXCL));
912541b0
KS
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) {
9f6445e3 931 log_debug("unable to open '%s'", node);
912541b0
KS
932 fprintf(stderr, "unable to open '%s'\n", node);
933 rc = 1;
934 goto exit;
935 }
9f6445e3 936 log_debug("probing: '%s'", node);
912541b0
KS
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
7505831b 945 * to read profiles */
912541b0 946 cd_media_compat(udev, fd);
d6f0b22d 947
912541b0
KS
948 /* check if drive talks MMC */
949 if (cd_inquiry(udev, fd) < 0)
950 goto work;
8f6919e6 951
912541b0
KS
952 /* read drive and possibly current profile */
953 if (cd_profiles(udev, fd) != 0)
954 goto work;
fd7a285e 955
912541b0 956 /* at this point we are guaranteed to have media in the drive - find out more about it */
98ab6a37 957
912541b0
KS
958 /* get session/track info */
959 cd_media_toc(udev, fd);
30e3b1a0 960
912541b0
KS
961 /* get writable media state */
962 cd_media_info(udev, fd);
1b3b6c2d 963
b20fd3cb 964work:
912541b0
KS
965 /* lock the media, so we enable eject button events */
966 if (lock && cd_media) {
9f6445e3 967 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)");
912541b0
KS
968 media_lock(udev, fd, true);
969 }
970
971 if (unlock && cd_media) {
9f6445e3 972 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
912541b0
KS
973 media_lock(udev, fd, false);
974 }
975
976 if (eject) {
9f6445e3 977 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
912541b0 978 media_lock(udev, fd, false);
9f6445e3 979 log_debug("START_STOP_UNIT (eject)");
912541b0
KS
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)
c4ef0548 1071 printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next);
912541b0 1072 if (cd_media_session_count > 0)
c4ef0548 1073 printf("ID_CDROM_MEDIA_SESSION_COUNT=%u\n", cd_media_session_count);
912541b0
KS
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)
c4ef0548 1077 printf("ID_CDROM_MEDIA_TRACK_COUNT=%u\n", cd_media_track_count);
912541b0 1078 if (cd_media_track_count_audio > 0)
c4ef0548 1079 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%u\n", cd_media_track_count_audio);
912541b0 1080 if (cd_media_track_count_data > 0)
c4ef0548 1081 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%u\n", cd_media_track_count_data);
8f6919e6 1082exit:
912541b0
KS
1083 if (fd >= 0)
1084 close(fd);
1085 udev_unref(udev);
baa30fbc 1086 log_close();
912541b0 1087 return rc;
8f6919e6 1088}