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