libcdio_rs/mmc/prevent_allow_medium_removal.rs
1// Copyright (C) 2026 Shiva Kiran Koninty <shiva@skran.xyz>
2//
3// This file is part of libcdio-rs.
4//
5// libcdio-rs is free software: you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation, either version 3 of the
8// License, or (at your option) any later version.
9//
10// libcdio-rs is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with libcdio-rs. If not, see <https://www.gnu.org/licenses/>.
17
18//! Routines based on MMC `PREVENT ALLOW MEDIUM REMOVAL`.
19
20use displaydoc::Display;
21use thiserror::Error;
22
23use crate::{
24 Mmc,
25 mmc::{Cdb, MmcCommand, MmcDirection, MmcError},
26};
27
28/// Routines based on MMC `PREVENT ALLOW MEDIUM REMOVAL`
29impl Mmc {
30 /// Allow media removal.
31 pub fn allow_media_removal(&self) -> Result<(), MmcMediaRemovalError> {
32 self.prevent_allow_medium_removal(PreventOption::Clear)
33 }
34
35 /// Prevent media removal.
36 pub fn prevent_media_removal(&self) -> Result<(), MmcMediaRemovalError> {
37 self.prevent_allow_medium_removal(PreventOption::Set)
38 }
39
40 fn prevent_allow_medium_removal(
41 &self,
42 prevent: PreventOption,
43 ) -> Result<(), MmcMediaRemovalError> {
44 let mut cdb = Cdb::default();
45 cdb[0] = MmcCommand::PreventAllowMediumRemoval as u8;
46 cdb[4] = prevent as u8;
47
48 self.run_command(Some(MmcDirection::Write), &mut [], cdb)?;
49
50 Ok(())
51 }
52}
53
54/// error from a `PREVENT ALLOW MEDIUM REMOVAL` command.
55#[non_exhaustive]
56#[derive(Debug, Display, Error)]
57pub struct MmcMediaRemovalError {
58 #[from]
59 pub source: MmcError,
60}
61
62/// Operations of the `PREVENT ALLOW MEDIUM REMOVAL` command
63#[allow(unused)]
64#[repr(u8)]
65#[derive(Clone, Copy, Debug)]
66enum PreventOption {
67 Clear = 0b00,
68 Set = 0b01,
69 ClearPersistent = 0b10,
70 SetPersistent = 0b11,
71}
72
73// see tests/media_removal.rs