| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* cdrom_id - optical drive and media information prober |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <fcntl.h> |
| 7 |
#include <linux/cdrom.h> |
| 8 |
#include <scsi/sg.h> |
| 9 |
#include <sys/ioctl.h> |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "build.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "format-table.h" |
| 16 |
#include "help-util.h" |
| 17 |
#include "main-func.h" |
| 18 |
#include "options.h" |
| 19 |
#include "random-util.h" |
| 20 |
#include "sort-util.h" |
| 21 |
#include "string-table.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "time-util.h" |
| 25 |
#include "udev-util.h" |
| 26 |
#include "unaligned.h" |
| 27 |
|
| 28 |
static bool arg_eject = false; |
| 29 |
static bool arg_lock = false; |
| 30 |
static bool arg_unlock = false; |
| 31 |
static const char *arg_node = NULL; |
| 32 |
|
| 33 |
typedef enum Feature { |
| 34 |
FEATURE_RW_NONREMOVABLE = 0x01, |
| 35 |
FEATURE_RW_REMOVABLE = 0x02, |
| 36 |
|
| 37 |
FEATURE_MO_SE = 0x03, /* sector erase */ |
| 38 |
FEATURE_MO_WO = 0x04, /* write once */ |
| 39 |
FEATURE_MO_AS = 0x05, /* advance storage */ |
| 40 |
|
| 41 |
FEATURE_CD_ROM = 0x08, |
| 42 |
FEATURE_CD_R = 0x09, |
| 43 |
FEATURE_CD_RW = 0x0a, |
| 44 |
|
| 45 |
FEATURE_DVD_ROM = 0x10, |
| 46 |
FEATURE_DVD_R = 0x11, |
| 47 |
FEATURE_DVD_RAM = 0x12, |
| 48 |
FEATURE_DVD_RW_RO = 0x13, /* restricted overwrite mode */ |
| 49 |
FEATURE_DVD_RW_SEQ = 0x14, /* sequential mode */ |
| 50 |
FEATURE_DVD_R_DL_SEQ = 0x15, /* sequential recording */ |
| 51 |
FEATURE_DVD_R_DL_JR = 0x16, /* jump recording */ |
| 52 |
FEATURE_DVD_RW_DL = 0x17, |
| 53 |
FEATURE_DVD_R_DDR = 0x18, /* download disc recording - dvd for css managed recording */ |
| 54 |
FEATURE_DVD_PLUS_RW = 0x1a, |
| 55 |
FEATURE_DVD_PLUS_R = 0x1b, |
| 56 |
|
| 57 |
FEATURE_DDCD_ROM = 0x20, |
| 58 |
FEATURE_DDCD_R = 0x21, |
| 59 |
FEATURE_DDCD_RW = 0x22, |
| 60 |
|
| 61 |
FEATURE_DVD_PLUS_RW_DL = 0x2a, |
| 62 |
FEATURE_DVD_PLUS_R_DL = 0x2b, |
| 63 |
|
| 64 |
FEATURE_BD = 0x40, |
| 65 |
FEATURE_BD_R_SRM = 0x41, /* sequential recording mode */ |
| 66 |
FEATURE_BD_R_RRM = 0x42, /* random recording mode */ |
| 67 |
FEATURE_BD_RE = 0x43, |
| 68 |
|
| 69 |
FEATURE_HDDVD = 0x50, |
| 70 |
FEATURE_HDDVD_R = 0x51, |
| 71 |
FEATURE_HDDVD_RAM = 0x52, |
| 72 |
FEATURE_HDDVD_RW = 0x53, |
| 73 |
FEATURE_HDDVD_R_DL = 0x58, |
| 74 |
FEATURE_HDDVD_RW_DL = 0x5a, |
| 75 |
|
| 76 |
FEATURE_MRW, |
| 77 |
FEATURE_MRW_W, |
| 78 |
|
| 79 |
_FEATURE_MAX, |
| 80 |
_FEATURE_INVALID = -EINVAL, |
| 81 |
} Feature; |
| 82 |
|
| 83 |
typedef enum MediaState { |
| 84 |
MEDIA_STATE_BLANK = 0, |
| 85 |
MEDIA_STATE_APPENDABLE = 1, |
| 86 |
MEDIA_STATE_COMPLETE = 2, |
| 87 |
MEDIA_STATE_OTHER = 3, |
| 88 |
_MEDIA_STATE_MAX, |
| 89 |
_MEDIA_STATE_INVALID = -EINVAL, |
| 90 |
} MediaState; |
| 91 |
|
| 92 |
typedef struct Context { |
| 93 |
int fd; |
| 94 |
|
| 95 |
Feature *drive_features; |
| 96 |
size_t n_drive_feature; |
| 97 |
|
| 98 |
Feature media_feature; |
| 99 |
bool has_media; |
| 100 |
|
| 101 |
MediaState media_state; |
| 102 |
unsigned media_session_next; |
| 103 |
unsigned media_session_count; |
| 104 |
unsigned media_track_count; |
| 105 |
unsigned media_track_count_data; |
| 106 |
unsigned media_track_count_audio; |
| 107 |
uint64_t media_session_last_offset; |
| 108 |
} Context; |
| 109 |
|
| 110 |
#define CONTEXT_EMPTY { \ |
| 111 |
.fd = -EBADF, \ |
| 112 |
.media_feature = _FEATURE_INVALID, \ |
| 113 |
.media_state = _MEDIA_STATE_INVALID, \ |
| 114 |
} |
| 115 |
|
| 116 |
static void context_clear(Context *c) { |
| 117 |
if (!c) |
| 118 |
return; |
| 119 |
|
| 120 |
safe_close(c->fd); |
| 121 |
free(c->drive_features); |
| 122 |
} |
| 123 |
|
| 124 |
static bool drive_has_feature(const Context *c, Feature f) { |
| 125 |
assert(c); |
| 126 |
|
| 127 |
for (size_t i = 0; i < c->n_drive_feature; i++) |
| 128 |
if (c->drive_features[i] == f) |
| 129 |
return true; |
| 130 |
|
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
static int set_drive_feature(Context *c, Feature f) { |
| 135 |
assert(c); |
| 136 |
|
| 137 |
if (drive_has_feature(c, f)) |
| 138 |
return 0; |
| 139 |
|
| 140 |
if (!GREEDY_REALLOC(c->drive_features, c->n_drive_feature + 1)) |
| 141 |
return -ENOMEM; |
| 142 |
|
| 143 |
c->drive_features[c->n_drive_feature++] = f; |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
#define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13])) |
| 148 |
#define SK(errcode) (((errcode) >> 16) & 0xFU) |
| 149 |
#define ASC(errcode) (((errcode) >> 8) & 0xFFU) |
| 150 |
#define ASCQ(errcode) ((errcode) & 0xFFU) |
| 151 |
#define CHECK_CONDITION 0x01 |
| 152 |
|
| 153 |
static int log_scsi_debug_errno(int error, const char *msg) { |
| 154 |
assert(error != 0); |
| 155 |
|
| 156 |
/* error < 0 means errno-style error, error > 0 means SCSI error */ |
| 157 |
|
| 158 |
if (error < 0) |
| 159 |
return log_debug_errno(error, "Failed to %s: %m", msg); |
| 160 |
|
| 161 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 162 |
"Failed to %s with SK=%X/ASC=%02X/ACQ=%02X", |
| 163 |
msg, SK(error), ASC(error), ASCQ(error)); |
| 164 |
} |
| 165 |
|
| 166 |
struct scsi_cmd { |
| 167 |
struct cdrom_generic_command cgc; |
| 168 |
union { |
| 169 |
struct request_sense s; |
| 170 |
unsigned char u[18]; |
| 171 |
} _sense; |
| 172 |
struct sg_io_hdr sg_io; |
| 173 |
}; |
| 174 |
|
| 175 |
static void scsi_cmd_init(struct scsi_cmd *cmd) { |
| 176 |
memzero(cmd, sizeof(struct scsi_cmd)); |
| 177 |
cmd->cgc.quiet = 1; |
| 178 |
cmd->cgc.sense = &cmd->_sense.s; |
| 179 |
cmd->sg_io.interface_id = 'S'; |
| 180 |
cmd->sg_io.mx_sb_len = sizeof(cmd->_sense); |
| 181 |
cmd->sg_io.cmdp = cmd->cgc.cmd; |
| 182 |
cmd->sg_io.sbp = cmd->_sense.u; |
| 183 |
cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO; |
| 184 |
} |
| 185 |
|
| 186 |
static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, unsigned char arg) { |
| 187 |
cmd->sg_io.cmd_len = i + 1; |
| 188 |
cmd->cgc.cmd[i] = arg; |
| 189 |
} |
| 190 |
|
| 191 |
static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize) { |
| 192 |
int r; |
| 193 |
|
| 194 |
assert(cmd); |
| 195 |
assert(fd >= 0); |
| 196 |
assert(buf || bufsize == 0); |
| 197 |
|
| 198 |
/* Return 0 on success. On failure, return negative errno or positive error code. */ |
| 199 |
|
| 200 |
if (bufsize > 0) { |
| 201 |
cmd->sg_io.dxferp = buf; |
| 202 |
cmd->sg_io.dxfer_len = bufsize; |
| 203 |
cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV; |
| 204 |
} else |
| 205 |
cmd->sg_io.dxfer_direction = SG_DXFER_NONE; |
| 206 |
|
| 207 |
if (ioctl(fd, SG_IO, &cmd->sg_io) < 0) |
| 208 |
return -errno; |
| 209 |
|
| 210 |
if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) { |
| 211 |
if (cmd->sg_io.masked_status & CHECK_CONDITION) { |
| 212 |
r = ERRCODE(cmd->_sense.u); |
| 213 |
if (r != 0) |
| 214 |
return r; |
| 215 |
} |
| 216 |
return -EIO; |
| 217 |
} |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int scsi_cmd_run_and_log(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize, const char *msg) { |
| 223 |
int r; |
| 224 |
|
| 225 |
assert(msg); |
| 226 |
|
| 227 |
r = scsi_cmd_run(cmd, fd, buf, bufsize); |
| 228 |
if (r != 0) |
| 229 |
return log_scsi_debug_errno(r, msg); |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
static int media_lock(int fd, bool lock) { |
| 235 |
/* disable the kernel's lock logic */ |
| 236 |
if (ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK) < 0) |
| 237 |
log_debug_errno(errno, "Failed to issue ioctl(CDROM_CLEAR_OPTIONS, CDO_LOCK), ignoring: %m"); |
| 238 |
|
| 239 |
if (ioctl(fd, CDROM_LOCKDOOR, lock ? 1 : 0) < 0) |
| 240 |
return log_debug_errno(errno, "Failed to issue ioctl(CDROM_LOCKDOOR): %m"); |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
static int media_eject(int fd) { |
| 246 |
struct scsi_cmd sc; |
| 247 |
|
| 248 |
scsi_cmd_init(&sc); |
| 249 |
scsi_cmd_set(&sc, 0, GPCMD_START_STOP_UNIT); |
| 250 |
scsi_cmd_set(&sc, 4, 0x02); |
| 251 |
scsi_cmd_set(&sc, 5, 0); |
| 252 |
|
| 253 |
return scsi_cmd_run_and_log(&sc, fd, NULL, 0, "start/stop unit"); |
| 254 |
} |
| 255 |
|
| 256 |
static int cd_capability_compat(Context *c) { |
| 257 |
int capability, r; |
| 258 |
|
| 259 |
assert(c); |
| 260 |
|
| 261 |
capability = ioctl(c->fd, CDROM_GET_CAPABILITY, NULL); |
| 262 |
if (capability < 0) |
| 263 |
return log_debug_errno(errno, "CDROM_GET_CAPABILITY failed"); |
| 264 |
|
| 265 |
if (capability & CDC_CD_R) { |
| 266 |
r = set_drive_feature(c, FEATURE_CD_R); |
| 267 |
if (r < 0) |
| 268 |
return log_oom_debug(); |
| 269 |
} |
| 270 |
if (capability & CDC_CD_RW) { |
| 271 |
r = set_drive_feature(c, FEATURE_CD_RW); |
| 272 |
if (r < 0) |
| 273 |
return log_oom_debug(); |
| 274 |
} |
| 275 |
if (capability & CDC_DVD) { |
| 276 |
r = set_drive_feature(c, FEATURE_DVD_ROM); |
| 277 |
if (r < 0) |
| 278 |
return log_oom_debug(); |
| 279 |
} |
| 280 |
if (capability & CDC_DVD_R) { |
| 281 |
r = set_drive_feature(c, FEATURE_DVD_R); |
| 282 |
if (r < 0) |
| 283 |
return log_oom_debug(); |
| 284 |
} |
| 285 |
if (capability & CDC_DVD_RAM) { |
| 286 |
r = set_drive_feature(c, FEATURE_DVD_RAM); |
| 287 |
if (r < 0) |
| 288 |
return log_oom_debug(); |
| 289 |
} |
| 290 |
if (capability & CDC_MRW) { |
| 291 |
r = set_drive_feature(c, FEATURE_MRW); |
| 292 |
if (r < 0) |
| 293 |
return log_oom_debug(); |
| 294 |
} |
| 295 |
if (capability & CDC_MRW_W) { |
| 296 |
r = set_drive_feature(c, FEATURE_MRW_W); |
| 297 |
if (r < 0) |
| 298 |
return log_oom_debug(); |
| 299 |
} |
| 300 |
|
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
static int cd_media_compat(Context *c) { |
| 305 |
int r; |
| 306 |
|
| 307 |
assert(c); |
| 308 |
|
| 309 |
r = ioctl(c->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
| 310 |
if (r < 0) |
| 311 |
return log_debug_errno(errno, "ioctl(CDROM_DRIVE_STATUS) failed: %m"); |
| 312 |
if (r != CDS_DISC_OK) |
| 313 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), |
| 314 |
"ioctl(CDROM_DRIVE_STATUS) → %d (%s), ignoring.", |
| 315 |
r, |
| 316 |
r == CDS_NO_INFO ? "no info" : |
| 317 |
r == CDS_NO_DISC ? "no disc" : |
| 318 |
r == CDS_TRAY_OPEN ? "tray open" : |
| 319 |
r == CDS_DRIVE_NOT_READY ? "drive not ready" : |
| 320 |
"unknown status"); |
| 321 |
|
| 322 |
c->has_media = true; |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
static int cd_inquiry(Context *c) { |
| 327 |
struct scsi_cmd sc; |
| 328 |
unsigned char inq[36]; |
| 329 |
int r; |
| 330 |
|
| 331 |
assert(c); |
| 332 |
|
| 333 |
scsi_cmd_init(&sc); |
| 334 |
scsi_cmd_set(&sc, 0, GPCMD_INQUIRY); |
| 335 |
scsi_cmd_set(&sc, 4, sizeof(inq)); |
| 336 |
scsi_cmd_set(&sc, 5, 0); |
| 337 |
r = scsi_cmd_run_and_log(&sc, c->fd, inq, sizeof(inq), "inquire"); |
| 338 |
if (r < 0) |
| 339 |
return r; |
| 340 |
|
| 341 |
if ((inq[0] & 0x1F) != 5) |
| 342 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Not an MMC unit."); |
| 343 |
|
| 344 |
log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
static int feature_profiles(Context *c, const unsigned char *profiles, size_t size) { |
| 349 |
int r; |
| 350 |
|
| 351 |
assert(c); |
| 352 |
|
| 353 |
for (size_t i = 0; i + 4 <= size; i += 4) { |
| 354 |
r = set_drive_feature(c, (Feature) unaligned_read_be16(&profiles[i])); |
| 355 |
if (r < 0) |
| 356 |
return log_oom_debug(); |
| 357 |
} |
| 358 |
|
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
static int cd_profiles_old_mmc(Context *c) { |
| 363 |
disc_information discinfo; |
| 364 |
struct scsi_cmd sc; |
| 365 |
size_t len; |
| 366 |
int r; |
| 367 |
|
| 368 |
assert(c); |
| 369 |
|
| 370 |
scsi_cmd_init(&sc); |
| 371 |
scsi_cmd_set(&sc, 0, GPCMD_READ_DISC_INFO); |
| 372 |
scsi_cmd_set(&sc, 8, sizeof(discinfo.disc_information_length)); |
| 373 |
scsi_cmd_set(&sc, 9, 0); |
| 374 |
r = scsi_cmd_run_and_log(&sc, c->fd, (unsigned char *)&discinfo.disc_information_length, sizeof(discinfo.disc_information_length), "read disc information"); |
| 375 |
if (r >= 0) { |
| 376 |
/* Not all drives have the same disc_info length, so requeue |
| 377 |
* packet with the length the drive tells us it can supply */ |
| 378 |
len = be16toh(discinfo.disc_information_length) + sizeof(discinfo.disc_information_length); |
| 379 |
if (len > sizeof(discinfo)) |
| 380 |
len = sizeof(discinfo); |
| 381 |
|
| 382 |
scsi_cmd_init(&sc); |
| 383 |
scsi_cmd_set(&sc, 0, GPCMD_READ_DISC_INFO); |
| 384 |
scsi_cmd_set(&sc, 8, len); |
| 385 |
scsi_cmd_set(&sc, 9, 0); |
| 386 |
r = scsi_cmd_run_and_log(&sc, c->fd, (unsigned char *)&discinfo, len, "read disc information"); |
| 387 |
} |
| 388 |
if (r < 0) { |
| 389 |
if (c->has_media) { |
| 390 |
log_debug("No current profile, but disc is present; assuming CD-ROM."); |
| 391 |
c->media_feature = FEATURE_CD_ROM; |
| 392 |
c->media_track_count = 1; |
| 393 |
c->media_track_count_data = 1; |
| 394 |
return 1; |
| 395 |
} else |
| 396 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), |
| 397 |
"no current profile, assuming no media."); |
| 398 |
}; |
| 399 |
|
| 400 |
c->has_media = true; |
| 401 |
|
| 402 |
if (discinfo.erasable) |
| 403 |
c->media_feature = FEATURE_CD_RW; |
| 404 |
else if (discinfo.disc_status < 2 && drive_has_feature(c, FEATURE_CD_R)) |
| 405 |
c->media_feature = FEATURE_CD_R; |
| 406 |
else |
| 407 |
c->media_feature = FEATURE_CD_ROM; |
| 408 |
|
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
static int cd_profiles(Context *c) { |
| 413 |
struct scsi_cmd sc; |
| 414 |
unsigned char features[65530]; |
| 415 |
unsigned cur_profile; |
| 416 |
size_t len; |
| 417 |
int r; |
| 418 |
|
| 419 |
assert(c); |
| 420 |
|
| 421 |
/* First query the current profile */ |
| 422 |
scsi_cmd_init(&sc); |
| 423 |
scsi_cmd_set(&sc, 0, GPCMD_GET_CONFIGURATION); |
| 424 |
scsi_cmd_set(&sc, 8, 8); |
| 425 |
scsi_cmd_set(&sc, 9, 0); |
| 426 |
r = scsi_cmd_run(&sc, c->fd, features, 8); |
| 427 |
if (r != 0) { |
| 428 |
/* handle pre-MMC2 drives which do not support GET CONFIGURATION */ |
| 429 |
if (r > 0 && SK(r) == 0x5 && IN_SET(ASC(r), 0x20, 0x24)) { |
| 430 |
log_debug("Drive is pre-MMC2 and does not support 46h get configuration command; " |
| 431 |
"trying to work around the problem."); |
| 432 |
return cd_profiles_old_mmc(c); |
| 433 |
} |
| 434 |
|
| 435 |
return log_scsi_debug_errno(r, "get configuration"); |
| 436 |
} |
| 437 |
|
| 438 |
cur_profile = unaligned_read_be16(&features[6]); |
| 439 |
if (cur_profile > 0) { |
| 440 |
log_debug("current profile 0x%02x", cur_profile); |
| 441 |
c->media_feature = (Feature) cur_profile; |
| 442 |
c->has_media = true; |
| 443 |
} else { |
| 444 |
log_debug("no current profile, assuming no media"); |
| 445 |
c->has_media = false; |
| 446 |
} |
| 447 |
|
| 448 |
len = unaligned_read_be32(features); |
| 449 |
log_debug("GET CONFIGURATION: size of features buffer %zu", len); |
| 450 |
|
| 451 |
if (len > sizeof(features)) { |
| 452 |
log_debug("Cannot get features in a single query, truncating."); |
| 453 |
len = sizeof(features); |
| 454 |
} else if (len <= 8) |
| 455 |
len = sizeof(features); |
| 456 |
|
| 457 |
/* Now get the full feature buffer */ |
| 458 |
scsi_cmd_init(&sc); |
| 459 |
scsi_cmd_set(&sc, 0, GPCMD_GET_CONFIGURATION); |
| 460 |
scsi_cmd_set(&sc, 7, (len >> 8) & 0xff); |
| 461 |
scsi_cmd_set(&sc, 8, len & 0xff); |
| 462 |
scsi_cmd_set(&sc, 9, 0); |
| 463 |
r = scsi_cmd_run_and_log(&sc, c->fd, features, len, "get configuration"); |
| 464 |
if (r < 0) |
| 465 |
return r; |
| 466 |
|
| 467 |
/* parse the length once more, in case the drive decided to have other features suddenly :) */ |
| 468 |
len = unaligned_read_be32(features); |
| 469 |
log_debug("GET CONFIGURATION: size of features buffer %zu", len); |
| 470 |
|
| 471 |
if (len > sizeof(features)) { |
| 472 |
log_debug("Cannot get features in a single query, truncating."); |
| 473 |
len = sizeof(features); |
| 474 |
} |
| 475 |
|
| 476 |
/* device features */ |
| 477 |
for (size_t i = 8; i + 4 < len; i += 4 + features[i + 3]) { |
| 478 |
unsigned feature; |
| 479 |
|
| 480 |
feature = unaligned_read_be16(&features[i]); |
| 481 |
|
| 482 |
switch (feature) { |
| 483 |
case 0x00: |
| 484 |
log_debug("GET CONFIGURATION: feature 'profiles', with %u entries", features[i + 3] / 4U); |
| 485 |
feature_profiles(c, features + i + 4, MIN(features[i + 3], len - i - 4)); |
| 486 |
break; |
| 487 |
default: |
| 488 |
log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature, features[i + 3]); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
return c->has_media; |
| 493 |
} |
| 494 |
|
| 495 |
static const char * const media_state_table[_MEDIA_STATE_MAX] = { |
| 496 |
[MEDIA_STATE_BLANK] = "blank", |
| 497 |
[MEDIA_STATE_APPENDABLE] = "appendable", |
| 498 |
[MEDIA_STATE_COMPLETE] = "complete", |
| 499 |
[MEDIA_STATE_OTHER] = "other", |
| 500 |
}; |
| 501 |
|
| 502 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(media_state, MediaState); |
| 503 |
|
| 504 |
static int dvd_ram_media_update_state(Context *c) { |
| 505 |
struct scsi_cmd sc; |
| 506 |
unsigned char dvdstruct[8]; |
| 507 |
unsigned char format[12]; |
| 508 |
unsigned char len; |
| 509 |
int r; |
| 510 |
|
| 511 |
assert(c); |
| 512 |
|
| 513 |
/* Return 1 if media state is determined. */ |
| 514 |
|
| 515 |
if (c->media_feature != FEATURE_DVD_RAM) |
| 516 |
return 0; |
| 517 |
|
| 518 |
/* a write protected dvd-ram may report "complete" status */ |
| 519 |
scsi_cmd_init(&sc); |
| 520 |
scsi_cmd_set(&sc, 0, GPCMD_READ_DVD_STRUCTURE); |
| 521 |
scsi_cmd_set(&sc, 7, 0xC0); |
| 522 |
scsi_cmd_set(&sc, 9, sizeof(dvdstruct)); |
| 523 |
scsi_cmd_set(&sc, 11, 0); |
| 524 |
r = scsi_cmd_run_and_log(&sc, c->fd, dvdstruct, sizeof(dvdstruct), "read DVD structure"); |
| 525 |
if (r < 0) |
| 526 |
return r; |
| 527 |
|
| 528 |
if (dvdstruct[4] & 0x02) { |
| 529 |
c->media_state = MEDIA_STATE_COMPLETE; |
| 530 |
log_debug("Write-protected DVD-RAM media inserted"); |
| 531 |
return 1; |
| 532 |
} |
| 533 |
|
| 534 |
/* let's make sure we don't try to read unformatted media */ |
| 535 |
scsi_cmd_init(&sc); |
| 536 |
scsi_cmd_set(&sc, 0, GPCMD_READ_FORMAT_CAPACITIES); |
| 537 |
scsi_cmd_set(&sc, 8, sizeof(format)); |
| 538 |
scsi_cmd_set(&sc, 9, 0); |
| 539 |
r = scsi_cmd_run_and_log(&sc, c->fd, format, sizeof(format), "read DVD format capacities"); |
| 540 |
if (r < 0) |
| 541 |
return r; |
| 542 |
|
| 543 |
len = format[3]; |
| 544 |
if (len & 7 || len < 16) |
| 545 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 546 |
"Invalid format capacities length."); |
| 547 |
|
| 548 |
switch (format[8] & 3) { |
| 549 |
case 1: |
| 550 |
/* This means that last format was interrupted or failed, blank dvd-ram discs are |
| 551 |
* factory formatted. Take no action here as it takes quite a while to reformat a |
| 552 |
* dvd-ram and it's not automatically started. */ |
| 553 |
log_debug("Unformatted DVD-RAM media inserted."); |
| 554 |
return 1; |
| 555 |
|
| 556 |
case 2: |
| 557 |
log_debug("Formatted DVD-RAM media inserted."); |
| 558 |
return 0; |
| 559 |
|
| 560 |
case 3: |
| 561 |
c->has_media = false; |
| 562 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), |
| 563 |
"Format capacities returned no media."); |
| 564 |
} |
| 565 |
|
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
static int dvd_media_update_state(Context *c) { |
| 570 |
struct scsi_cmd sc; |
| 571 |
unsigned char buffer[32 * 2048]; |
| 572 |
int r; |
| 573 |
|
| 574 |
r = dvd_ram_media_update_state(c); |
| 575 |
if (r != 0) |
| 576 |
return r; |
| 577 |
|
| 578 |
/* Take a closer look at formatted media (unformatted DVD+RW |
| 579 |
* has "blank" status", DVD-RAM was examined earlier) and check |
| 580 |
* for ISO and UDF PVDs or a fs superblock presence and do it |
| 581 |
* in one ioctl (we need just sectors 0 and 16) */ |
| 582 |
|
| 583 |
scsi_cmd_init(&sc); |
| 584 |
scsi_cmd_set(&sc, 0, GPCMD_READ_10); |
| 585 |
scsi_cmd_set(&sc, 5, 0); |
| 586 |
scsi_cmd_set(&sc, 8, sizeof(buffer)/2048); |
| 587 |
scsi_cmd_set(&sc, 9, 0); |
| 588 |
r = scsi_cmd_run_and_log(&sc, c->fd, buffer, sizeof(buffer), "read first 32 blocks"); |
| 589 |
if (r < 0) { |
| 590 |
c->has_media = false; |
| 591 |
return r; |
| 592 |
} |
| 593 |
|
| 594 |
/* if any non-zero data is found in sector 16 (iso and udf) or |
| 595 |
* eventually 0 (fat32 boot sector, ext2 superblock, etc), disc |
| 596 |
* is assumed non-blank */ |
| 597 |
|
| 598 |
for (size_t offset = 32768; offset < 32768 + 2048; offset++) |
| 599 |
if (buffer[offset] != 0) { |
| 600 |
log_debug("Data in block 16, assuming complete."); |
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
for (size_t offset = 0; offset < 2048; offset++) |
| 605 |
if (buffer[offset] != 0) { |
| 606 |
log_debug("Data in block 0, assuming complete."); |
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
log_debug("No data in blocks 0 or 16, assuming blank."); |
| 611 |
c->media_state = MEDIA_STATE_BLANK; |
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
static int cd_media_info(Context *c) { |
| 616 |
struct scsi_cmd sc; |
| 617 |
unsigned char header[32]; |
| 618 |
MediaState state; |
| 619 |
int r; |
| 620 |
|
| 621 |
assert(c); |
| 622 |
|
| 623 |
scsi_cmd_init(&sc); |
| 624 |
scsi_cmd_set(&sc, 0, GPCMD_READ_DISC_INFO); |
| 625 |
scsi_cmd_set(&sc, 8, sizeof(header)); |
| 626 |
scsi_cmd_set(&sc, 9, 0); |
| 627 |
r = scsi_cmd_run_and_log(&sc, c->fd, header, sizeof(header), "read disc information"); |
| 628 |
if (r < 0) |
| 629 |
return r; |
| 630 |
|
| 631 |
c->has_media = true; |
| 632 |
log_debug("disk type %02x", header[8]); |
| 633 |
|
| 634 |
state = (MediaState) (header[2] & 0x03); |
| 635 |
log_debug("hardware reported media status: %s", strna(media_state_to_string(state))); |
| 636 |
|
| 637 |
/* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */ |
| 638 |
if (c->media_feature != FEATURE_CD_ROM) |
| 639 |
c->media_state = state; |
| 640 |
|
| 641 |
/* fresh DVD-RW in restricted overwrite mode reports itself as |
| 642 |
* "appendable"; change it to "blank" to make it consistent with what |
| 643 |
* gets reported after blanking, and what userspace expects. */ |
| 644 |
if (c->media_feature == FEATURE_DVD_RW_RO && state == MEDIA_STATE_APPENDABLE) |
| 645 |
c->media_state = MEDIA_STATE_BLANK; |
| 646 |
|
| 647 |
/* DVD+RW discs (and DVD-RW in restricted mode) once formatted are |
| 648 |
* always "complete", DVD-RAM are "other" or "complete" if the disc is |
| 649 |
* write protected; we need to check the contents if it is blank */ |
| 650 |
if (IN_SET(c->media_feature, FEATURE_DVD_RW_RO, FEATURE_DVD_PLUS_RW, FEATURE_DVD_PLUS_RW_DL, FEATURE_DVD_RAM) && |
| 651 |
IN_SET(state, MEDIA_STATE_COMPLETE, MEDIA_STATE_OTHER)) { |
| 652 |
r = dvd_media_update_state(c); |
| 653 |
if (r < 0) |
| 654 |
return r; |
| 655 |
} |
| 656 |
|
| 657 |
/* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in |
| 658 |
* restricted overwrite mode can never append, only in sequential mode */ |
| 659 |
if (c->media_feature != FEATURE_DVD_RW_RO && IN_SET(state, MEDIA_STATE_BLANK, MEDIA_STATE_APPENDABLE)) |
| 660 |
c->media_session_next = header[10] << 8 | header[5]; |
| 661 |
c->media_session_count = header[9] << 8 | header[4]; |
| 662 |
c->media_track_count = header[11] << 8 | header[6]; |
| 663 |
|
| 664 |
return 0; |
| 665 |
} |
| 666 |
|
| 667 |
static int cd_media_toc(Context *c) { |
| 668 |
struct scsi_cmd sc; |
| 669 |
unsigned char header[12]; |
| 670 |
unsigned char toc[65536]; |
| 671 |
unsigned num_tracks; |
| 672 |
size_t len; |
| 673 |
int r; |
| 674 |
|
| 675 |
assert(c); |
| 676 |
|
| 677 |
scsi_cmd_init(&sc); |
| 678 |
scsi_cmd_set(&sc, 0, GPCMD_READ_TOC_PMA_ATIP); |
| 679 |
scsi_cmd_set(&sc, 6, 1); |
| 680 |
scsi_cmd_set(&sc, 8, sizeof(header)); |
| 681 |
scsi_cmd_set(&sc, 9, 0); |
| 682 |
r = scsi_cmd_run_and_log(&sc, c->fd, header, sizeof(header), "read TOC"); |
| 683 |
if (r < 0) |
| 684 |
return r; |
| 685 |
|
| 686 |
len = unaligned_read_be16(header) + 2; |
| 687 |
log_debug("READ TOC: len: %zu, start track: %u, end track: %u", len, header[2], header[3]); |
| 688 |
|
| 689 |
if (len > sizeof(toc)) |
| 690 |
return -1; |
| 691 |
/* empty media has no tracks */ |
| 692 |
if (len < 8) |
| 693 |
return 0; |
| 694 |
|
| 695 |
/* 2: first track, 3: last track */ |
| 696 |
num_tracks = header[3] - header[2] + 1; |
| 697 |
|
| 698 |
scsi_cmd_init(&sc); |
| 699 |
scsi_cmd_set(&sc, 0, GPCMD_READ_TOC_PMA_ATIP); |
| 700 |
scsi_cmd_set(&sc, 6, header[2]); /* First Track/Session Number */ |
| 701 |
scsi_cmd_set(&sc, 7, (len >> 8) & 0xff); |
| 702 |
scsi_cmd_set(&sc, 8, len & 0xff); |
| 703 |
scsi_cmd_set(&sc, 9, 0); |
| 704 |
r = scsi_cmd_run_and_log(&sc, c->fd, toc, len, "read TOC (tracks)"); |
| 705 |
if (r < 0) |
| 706 |
return r; |
| 707 |
|
| 708 |
/* Take care to not iterate beyond the last valid track as specified in |
| 709 |
* the TOC, but also avoid going beyond the TOC length, just in case |
| 710 |
* the last track number is invalidly large */ |
| 711 |
for (size_t i = 4; i + 8 <= len && num_tracks > 0; i += 8, --num_tracks) { |
| 712 |
bool is_data_track; |
| 713 |
uint32_t block; |
| 714 |
|
| 715 |
is_data_track = (toc[i + 1] & 0x04) != 0; |
| 716 |
block = unaligned_read_be32(&toc[i + 4]); |
| 717 |
|
| 718 |
log_debug("track=%u info=0x%x(%s) start_block=%"PRIu32, |
| 719 |
toc[i + 2], toc[i + 1] & 0x0FU, is_data_track ? "data":"audio", block); |
| 720 |
|
| 721 |
if (is_data_track) |
| 722 |
c->media_track_count_data++; |
| 723 |
else |
| 724 |
c->media_track_count_audio++; |
| 725 |
} |
| 726 |
|
| 727 |
scsi_cmd_init(&sc); |
| 728 |
scsi_cmd_set(&sc, 0, GPCMD_READ_TOC_PMA_ATIP); |
| 729 |
scsi_cmd_set(&sc, 2, 1); /* Session Info */ |
| 730 |
scsi_cmd_set(&sc, 8, sizeof(header)); |
| 731 |
scsi_cmd_set(&sc, 9, 0); |
| 732 |
r = scsi_cmd_run_and_log(&sc, c->fd, header, sizeof(header), "read TOC (multi session)"); |
| 733 |
if (r < 0) |
| 734 |
return r; |
| 735 |
|
| 736 |
len = unaligned_read_be32(&header[8]); |
| 737 |
log_debug("last track %u starts at block %zu", header[4+2], len); |
| 738 |
c->media_session_last_offset = (uint64_t) len * 2048; |
| 739 |
|
| 740 |
return 0; |
| 741 |
} |
| 742 |
|
| 743 |
static int open_drive(Context *c) { |
| 744 |
int fd; |
| 745 |
|
| 746 |
assert(c); |
| 747 |
assert(c->fd < 0); |
| 748 |
|
| 749 |
for (int cnt = 0;; cnt++) { |
| 750 |
fd = open(arg_node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); |
| 751 |
if (fd >= 0) |
| 752 |
break; |
| 753 |
if (++cnt >= 20 || errno != EBUSY) |
| 754 |
return log_debug_errno(errno, "Unable to open '%s': %m", arg_node); |
| 755 |
|
| 756 |
(void) usleep_safe(100 * USEC_PER_MSEC + random_u64_range(100 * USEC_PER_MSEC)); |
| 757 |
} |
| 758 |
|
| 759 |
log_debug("probing: '%s'", arg_node); |
| 760 |
c->fd = fd; |
| 761 |
return 0; |
| 762 |
} |
| 763 |
|
| 764 |
typedef struct FeatureToString { |
| 765 |
Feature feature; |
| 766 |
const char *str; |
| 767 |
} FeatureToString; |
| 768 |
|
| 769 |
static const FeatureToString feature_to_string[] = { |
| 770 |
{ .feature = FEATURE_RW_NONREMOVABLE, .str = "RW_NONREMOVABLE", }, |
| 771 |
{ .feature = FEATURE_RW_REMOVABLE, .str = "RW_REMOVABLE", }, |
| 772 |
|
| 773 |
{ .feature = FEATURE_MO_SE, .str = "MO_SE", }, |
| 774 |
{ .feature = FEATURE_MO_WO, .str = "MO_WO", }, |
| 775 |
{ .feature = FEATURE_MO_AS, .str = "MO_AS", }, |
| 776 |
|
| 777 |
{ .feature = FEATURE_CD_ROM, .str = "CD", }, |
| 778 |
{ .feature = FEATURE_CD_R, .str = "CD_R", }, |
| 779 |
{ .feature = FEATURE_CD_RW, .str = "CD_RW", }, |
| 780 |
|
| 781 |
{ .feature = FEATURE_DVD_ROM, .str = "DVD", }, |
| 782 |
{ .feature = FEATURE_DVD_R, .str = "DVD_R", }, |
| 783 |
{ .feature = FEATURE_DVD_RAM, .str = "DVD_RAM", }, |
| 784 |
{ .feature = FEATURE_DVD_RW_RO, .str = "DVD_RW_RO", }, |
| 785 |
{ .feature = FEATURE_DVD_RW_SEQ, .str = "DVD_RW_SEQ", }, |
| 786 |
{ .feature = FEATURE_DVD_R_DL_SEQ, .str = "DVD_R_DL_SEQ", }, |
| 787 |
{ .feature = FEATURE_DVD_R_DL_JR, .str = "DVD_R_DL_JR", }, |
| 788 |
{ .feature = FEATURE_DVD_RW_DL, .str = "DVD_RW_DL", }, |
| 789 |
{ .feature = FEATURE_DVD_R_DDR, .str = "DVD_R_DDR", }, |
| 790 |
{ .feature = FEATURE_DVD_PLUS_RW, .str = "DVD_PLUS_RW", }, |
| 791 |
{ .feature = FEATURE_DVD_PLUS_R, .str = "DVD_PLUS_R", }, |
| 792 |
|
| 793 |
{ .feature = FEATURE_DDCD_ROM, .str = "DDCD", }, |
| 794 |
{ .feature = FEATURE_DDCD_R, .str = "DDCD_R", }, |
| 795 |
{ .feature = FEATURE_DDCD_RW, .str = "DDCD_RW", }, |
| 796 |
|
| 797 |
{ .feature = FEATURE_DVD_PLUS_RW_DL, .str = "DVD_PLUS_RW_DL", }, |
| 798 |
{ .feature = FEATURE_DVD_PLUS_R_DL, .str = "DVD_PLUS_R_DL", }, |
| 799 |
|
| 800 |
{ .feature = FEATURE_BD, .str = "BD", }, |
| 801 |
{ .feature = FEATURE_BD_R_SRM, .str = "BD_R_SRM", }, |
| 802 |
{ .feature = FEATURE_BD_R_RRM, .str = "BD_R_RRM", }, |
| 803 |
{ .feature = FEATURE_BD_RE, .str = "BD_RE", }, |
| 804 |
|
| 805 |
{ .feature = FEATURE_HDDVD, .str = "HDDVD", }, |
| 806 |
{ .feature = FEATURE_HDDVD_R, .str = "HDDVD_R", }, |
| 807 |
{ .feature = FEATURE_HDDVD_RAM, .str = "HDDVD_RAM", }, |
| 808 |
{ .feature = FEATURE_HDDVD_RW, .str = "HDDVD_RW", }, |
| 809 |
{ .feature = FEATURE_HDDVD_R_DL, .str = "HDDVD_R_DL", }, |
| 810 |
{ .feature = FEATURE_HDDVD_RW_DL, .str = "HDDVD_RW_DL", }, |
| 811 |
|
| 812 |
{ .feature = FEATURE_MRW, .str = "MRW", }, |
| 813 |
{ .feature = FEATURE_MRW_W, .str = "MRW_W", }, |
| 814 |
}; |
| 815 |
|
| 816 |
static int feature_to_string_compare_func(const FeatureToString *a, const FeatureToString *b) { |
| 817 |
assert(a); |
| 818 |
assert(b); |
| 819 |
|
| 820 |
return CMP(a->feature, b->feature); |
| 821 |
} |
| 822 |
|
| 823 |
static void print_feature(Feature feature, const char *prefix) { |
| 824 |
const FeatureToString *found, in = { |
| 825 |
.feature = feature, |
| 826 |
}; |
| 827 |
|
| 828 |
assert(prefix); |
| 829 |
|
| 830 |
found = typesafe_bsearch(&in, feature_to_string, ELEMENTSOF(feature_to_string), feature_to_string_compare_func); |
| 831 |
if (!found) |
| 832 |
return (void) log_debug("Unknown feature 0x%02x, ignoring.", (unsigned) feature); |
| 833 |
|
| 834 |
printf("%s_%s=1\n", prefix, found->str); |
| 835 |
} |
| 836 |
|
| 837 |
static void print_properties(const Context *c) { |
| 838 |
const char *state; |
| 839 |
|
| 840 |
assert(c); |
| 841 |
|
| 842 |
printf("ID_CDROM=1\n"); |
| 843 |
for (size_t i = 0; i < c->n_drive_feature; i++) |
| 844 |
print_feature(c->drive_features[i], "ID_CDROM"); |
| 845 |
|
| 846 |
if (drive_has_feature(c, FEATURE_MO_SE) || |
| 847 |
drive_has_feature(c, FEATURE_MO_WO) || |
| 848 |
drive_has_feature(c, FEATURE_MO_AS)) |
| 849 |
printf("ID_CDROM_MO=1\n"); |
| 850 |
|
| 851 |
if (drive_has_feature(c, FEATURE_DVD_RW_RO) || |
| 852 |
drive_has_feature(c, FEATURE_DVD_RW_SEQ)) |
| 853 |
printf("ID_CDROM_DVD_RW=1\n"); |
| 854 |
|
| 855 |
if (drive_has_feature(c, FEATURE_DVD_R_DL_SEQ) || |
| 856 |
drive_has_feature(c, FEATURE_DVD_R_DL_JR)) |
| 857 |
printf("ID_CDROM_DVD_R_DL=1\n"); |
| 858 |
|
| 859 |
if (drive_has_feature(c, FEATURE_DVD_R_DDR)) |
| 860 |
printf("ID_CDROM_DVD_R=1\n"); |
| 861 |
|
| 862 |
if (drive_has_feature(c, FEATURE_BD_R_SRM) || |
| 863 |
drive_has_feature(c, FEATURE_BD_R_RRM)) |
| 864 |
printf("ID_CDROM_BD_R=1\n"); |
| 865 |
|
| 866 |
if (c->has_media) { |
| 867 |
printf("ID_CDROM_MEDIA=1\n"); |
| 868 |
print_feature(c->media_feature, "ID_CDROM_MEDIA"); |
| 869 |
|
| 870 |
if (IN_SET(c->media_feature, FEATURE_MO_SE, FEATURE_MO_WO, FEATURE_MO_AS)) |
| 871 |
printf("ID_CDROM_MEDIA_MO=1\n"); |
| 872 |
|
| 873 |
if (IN_SET(c->media_feature, FEATURE_DVD_RW_RO, FEATURE_DVD_RW_SEQ)) |
| 874 |
printf("ID_CDROM_MEDIA_DVD_RW=1\n"); |
| 875 |
|
| 876 |
if (IN_SET(c->media_feature, FEATURE_DVD_R_DL_SEQ, FEATURE_DVD_R_DL_JR)) |
| 877 |
printf("ID_CDROM_MEDIA_DVD_R_DL=1\n"); |
| 878 |
|
| 879 |
if (c->media_feature == FEATURE_DVD_R_DDR) |
| 880 |
printf("ID_CDROM_MEDIA_DVD_R=1\n"); |
| 881 |
|
| 882 |
if (IN_SET(c->media_feature, FEATURE_BD_R_SRM, FEATURE_BD_R_RRM)) |
| 883 |
printf("ID_CDROM_MEDIA_BD_R=1\n"); |
| 884 |
} |
| 885 |
|
| 886 |
state = media_state_to_string(c->media_state); |
| 887 |
if (state) |
| 888 |
printf("ID_CDROM_MEDIA_STATE=%s\n", state); |
| 889 |
if (c->media_session_next > 0) |
| 890 |
printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", c->media_session_next); |
| 891 |
if (c->media_session_count > 0) |
| 892 |
printf("ID_CDROM_MEDIA_SESSION_COUNT=%u\n", c->media_session_count); |
| 893 |
if (c->media_session_count > 1 && c->media_session_last_offset > 0) |
| 894 |
printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%" PRIu64 "\n", c->media_session_last_offset); |
| 895 |
if (c->media_track_count > 0) |
| 896 |
printf("ID_CDROM_MEDIA_TRACK_COUNT=%u\n", c->media_track_count); |
| 897 |
if (c->media_track_count_audio > 0) |
| 898 |
printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%u\n", c->media_track_count_audio); |
| 899 |
if (c->media_track_count_data > 0) |
| 900 |
printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%u\n", c->media_track_count_data); |
| 901 |
} |
| 902 |
|
| 903 |
static int help(void) { |
| 904 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 905 |
int r; |
| 906 |
|
| 907 |
r = option_parser_get_help_table(&options); |
| 908 |
if (r < 0) |
| 909 |
return r; |
| 910 |
|
| 911 |
help_cmdline("[OPTIONS...] DEVICE"); |
| 912 |
help_section("Options"); |
| 913 |
|
| 914 |
return table_print_or_warn(options); |
| 915 |
} |
| 916 |
|
| 917 |
static int parse_argv(int argc, char *argv[]) { |
| 918 |
assert(argc >= 0); |
| 919 |
assert(argv); |
| 920 |
|
| 921 |
OptionParser opts = { argc, argv }; |
| 922 |
|
| 923 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 924 |
switch (c) { |
| 925 |
|
| 926 |
OPTION_COMMON_HELP: |
| 927 |
return help(); |
| 928 |
|
| 929 |
OPTION_COMMON_VERSION: |
| 930 |
return version(); |
| 931 |
|
| 932 |
OPTION('l', "lock-media", NULL, |
| 933 |
"Lock the media (to enable eject request events)"): |
| 934 |
arg_lock = true; |
| 935 |
break; |
| 936 |
|
| 937 |
OPTION('u', "unlock-media", NULL, |
| 938 |
"Unlock the media"): |
| 939 |
arg_unlock = true; |
| 940 |
break; |
| 941 |
|
| 942 |
OPTION('e', "eject-media", NULL, |
| 943 |
"Eject the media"): |
| 944 |
arg_eject = true; |
| 945 |
break; |
| 946 |
|
| 947 |
OPTION('d', "debug", NULL, |
| 948 |
"Print debug messages to stderr"): |
| 949 |
log_set_target(LOG_TARGET_CONSOLE); |
| 950 |
log_set_max_level(LOG_DEBUG); |
| 951 |
log_open(); |
| 952 |
break; |
| 953 |
} |
| 954 |
|
| 955 |
char **args = option_parser_get_args(&opts); |
| 956 |
if (strv_length(args) != 1) |
| 957 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Need exactly one DEVICE argument."); |
| 958 |
|
| 959 |
arg_node = args[0]; |
| 960 |
return 1; |
| 961 |
} |
| 962 |
|
| 963 |
static int run(int argc, char *argv[]) { |
| 964 |
_cleanup_(context_clear) Context c = CONTEXT_EMPTY; |
| 965 |
int r; |
| 966 |
|
| 967 |
(void) udev_parse_config(); |
| 968 |
log_setup(); |
| 969 |
|
| 970 |
r = parse_argv(argc, argv); |
| 971 |
if (r <= 0) |
| 972 |
return r; |
| 973 |
|
| 974 |
r = open_drive(&c); |
| 975 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 976 |
return 0; |
| 977 |
if (r < 0) |
| 978 |
return log_warning_errno(r, "Failed to open device node '%s': %m", arg_node); |
| 979 |
|
| 980 |
/* same data as original cdrom_id */ |
| 981 |
r = cd_capability_compat(&c); |
| 982 |
if (r < 0) |
| 983 |
return r; |
| 984 |
|
| 985 |
/* check for media - don't bail if there's no media as we still need to |
| 986 |
* to read profiles */ |
| 987 |
(void) cd_media_compat(&c); |
| 988 |
|
| 989 |
/* check if drive talks MMC */ |
| 990 |
if (cd_inquiry(&c) < 0) |
| 991 |
goto work; |
| 992 |
|
| 993 |
r = cd_profiles(&c); /* read drive and possibly current profile */ |
| 994 |
if (r > 0) { |
| 995 |
/* at this point we are guaranteed to have media in the drive - find out more about it */ |
| 996 |
|
| 997 |
/* get session/track info */ |
| 998 |
(void) cd_media_toc(&c); |
| 999 |
|
| 1000 |
/* get writable media state */ |
| 1001 |
(void) cd_media_info(&c); |
| 1002 |
} |
| 1003 |
|
| 1004 |
work: |
| 1005 |
/* lock the media, so we enable eject button events */ |
| 1006 |
if (arg_lock && c.has_media) { |
| 1007 |
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)"); |
| 1008 |
(void) media_lock(c.fd, true); |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (arg_unlock && c.has_media) { |
| 1012 |
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)"); |
| 1013 |
(void) media_lock(c.fd, false); |
| 1014 |
} |
| 1015 |
|
| 1016 |
if (arg_eject) { |
| 1017 |
log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)"); |
| 1018 |
(void) media_lock(c.fd, false); |
| 1019 |
log_debug("START_STOP_UNIT (eject)"); |
| 1020 |
(void) media_eject(c.fd); |
| 1021 |
} |
| 1022 |
|
| 1023 |
print_properties(&c); |
| 1024 |
|
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
|
| 1028 |
DEFINE_MAIN_FUNCTION(run); |