Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion libcdio-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ name = "iso-read-rs"
path = "src/iso-read/main.rs"
required-features = ["iso-read"]

[[bin]]
name = "mmc-tool-rs"
path = "src/mmc-tool/main.rs"
required-features = ["mmc-tool"]

[features]
default = ["cd-drive", "iso-info", "iso-read"]
default = ["cd-drive", "iso-info", "iso-read", "mmc-tool"]
cd-drive = []
iso-info = ["libcdio-rs/iso9660", "libcdio-rs/udf", "dep:time"]
iso-read = ["libcdio-rs/iso9660", "libcdio-rs/udf"]
mmc-tool = []

[dev-dependencies]
assert_cmd = { version = "2.2.2", features = ["color"] }
Expand Down
71 changes: 71 additions & 0 deletions libcdio-cli/src/mmc-tool/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (C) 2026 Shiva Kiran Koninty <shiva@skran.xyz>
//
// This file is part of libcdio-cli.
//
// libcdio-cli is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// libcdio-cli is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libcdio-cli. If not, see <https://www.gnu.org/licenses/>.

use std::path::PathBuf;

use clap::{Args, Parser};

#[derive(Debug, Parser)]
#[command(arg_required_else_help = true, long_about = libcdio_cli::HEADER, version)]
pub struct Cli {
/// Show debugging information (1 = Error, 2 = Warn, 3 = Info, 4 = Debug)
#[arg(
default_value = "2",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default_value = "2" doesn't seem to be working:

$ ./mmc-tool-rs -d -c
error: a value is required for '--debug <LEVEL>' but none was supplied

For more information, try '--help'.

$ ./mmc-tool-rs --help
libcdio-cli version 0.1.0

..

Options:
  -d, --debug <LEVEL>
          Show debugging information (1 = Error, 2 = Warn, 3 = Info, 4 = Debug)

          [default: 2]

@skr4n skr4n Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is used when the option is not provided.

On a side note for later, I think using an env variable is more appropriate to accept log levels, rather than a dedicated user-facing option.
Example:
LOG_LEVEL=debug mmc-tool --close-tray
instead of
mmc-tool -d4 --close-tray

short,
long,
value_name = "LEVEL",
value_parser = clap::value_parser!(u8).range(1..=4),
)]
pub debug: u8,

/// Path to an MMC device.
pub device: Option<PathBuf>,

#[command(flatten)]
pub actions: MmcActions,
}

#[derive(Args, Debug)]
#[group(required = true, multiple = false)]
pub struct MmcActions {
/// Eject the drive
#[arg(short, long)]
pub eject: bool,

/// Close the tray, if present
#[arg(short, long)]
pub close_tray: bool,

/// Put the device into standby
#[arg(short, long)]
pub standby: bool,

/// Get the MCN (Media Catalog Number) of the media
#[arg(short, long)]
pub mcn: bool,

/// Get hardware identifiers (Product, Vendor and Revision)
#[arg(short, long)]
pub inquiry: bool,

/// Set the drive read and write speed in KB/s.
///
/// Falls back to the nearest supported value if the provided value is not
/// supported.
#[arg(short = 'S', long)]
pub speed: Option<u16>,
}
64 changes: 64 additions & 0 deletions libcdio-cli/src/mmc-tool/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2026 Shiva Kiran Koninty <shiva@skran.xyz>
//
// This file is part of libcdio-cli.
//
// libcdio-cli is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// libcdio-cli is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libcdio-cli. If not, see <https://www.gnu.org/licenses/>.

use anyhow::{Context, Result};
use clap::Parser;
use libcdio_rs::{
Mmc,
mmc::{PowerCondition, RotationMode},
};

use crate::cli::Cli;

mod cli;

fn main() -> Result<()> {
let cli = Cli::parse();
libcdio_cli::setup_logs(cli.debug);
let mmc = if let Some(device) = cli.device {
Mmc::with_device(device)?
} else {
Mmc::new()?
};

if cli.actions.eject {
mmc.allow_media_removal()?;
mmc.eject()?;
} else if cli.actions.close_tray {
mmc.close_tray()?;
Comment thread
skr4n marked this conversation as resolved.
} else if cli.actions.standby {
mmc.set_power_state(PowerCondition::Standby)?;
} else if cli.actions.mcn {
let mcn = mmc
.media_catalog_number()
.context("could not get MCN")?
.context("current media does not have a Media Catalog Number")?;
println!("{}", mcn);
} else if cli.actions.inquiry {
let ident = mmc.hardware_identifiers()?;
println!("Product: {}", ident.product);
println!("Vendor: {}", ident.vendor);
println!("Revision: {}", ident.revision);
} else if let Some(speed) = cli.actions.speed {
// some drives may not support CLV (Constant Linear Velocity)
if mmc.set_cd_speed(RotationMode::Clv, speed, speed).is_err() {
mmc.set_cd_speed(RotationMode::Cav, speed, speed)?;
}
}

Ok(())
}
31 changes: 27 additions & 4 deletions libcdio-rs/src/mmc/start_stop_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,40 @@ impl Mmc {
Ok(())
}

/// Set power state.
pub fn set_power_state(&self, state: PowerCondition) -> Result<(), MmcSetPowerStateError> {
self.start_stop_unit(StartStopOperation::Power(state))?;
Ok(())
}

fn start_stop_unit(&self, operation: StartStopOperation) -> Result<(), MmcStartStopError> {
let mut cdb = Cdb::default();

cdb[0] = MmcCommand::StartStopUnit as u8;
cdb[1] = 0; // not using the immediate bit for now
if let StartStopOperation::Jump { layer_number } = operation {
cdb[3] = layer_number & 0b11
cdb[3] = layer_number & LAYER_NUM_BITMASK;
cdb[4] |= 1 << FORMAT_LAYER_BITPOS;
}
cdb[4] = match operation {
// loej and start
cdb[4] |= match operation {
StartStopOperation::StartDisc => 0b01,
StartStopOperation::EjectDisc => 0b10,
StartStopOperation::LoadStartDisc | StartStopOperation::Jump { .. } => 0b11,
_ => 0b00,
};
if let StartStopOperation::Power(pow_cond) = operation {
cdb[4] |= (pow_cond as u8 & POWER_COND_BITMASK) << POWER_COND_BITPOS;
}

self.run_command(Some(MmcDirection::Write), &mut [], cdb)?;

Ok(())
return Ok(());

const LAYER_NUM_BITMASK: u8 = 0b11;
const FORMAT_LAYER_BITPOS: usize = 2;
const POWER_COND_BITMASK: u8 = 0b1111;
const POWER_COND_BITPOS: usize = 4;
}
}

Expand All @@ -76,6 +92,13 @@ pub struct MmcCloseTrayError {
pub source: MmcStartStopError,
}

/// could not set power state of MMC device
#[derive(Debug, Display, Error)]
pub struct MmcSetPowerStateError {
#[from]
pub source: MmcStartStopError,
}

/// error from a `START STOP UNIT` command
#[derive(Debug, Display, Error)]
pub struct MmcStartStopError {
Expand Down Expand Up @@ -103,7 +126,7 @@ enum StartStopOperation {

/// A power state as defined under MMC `START STOP UNIT`
#[allow(unused)]
enum PowerCondition {
pub enum PowerCondition {
Idle = 0x2,
Standby = 0x3,
Sleep = 0x5,
Expand Down