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