Skip to content

Commit

Permalink
disks: Add MMC detection
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Jan 20, 2025
1 parent 0326002 commit aaed894
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/disks/src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use std::{
path::{Path, PathBuf},
};

use crate::{nvme, partition::Partition, scsi, sysfs, DEVFS_DIR};
use crate::{mmc, nvme, partition::Partition, scsi, sysfs, DEVFS_DIR};

/// Represents the type of disk device.
#[derive(Debug)]
pub enum Disk {
/// SCSI disk device (e.g. sda, sdb)
Scsi(scsi::Disk),
/// MMC disk device (e.g. mmcblk0)
Mmc(mmc::Disk),
/// NVMe disk device (e.g. nvme0n1)
Nvme(nvme::Disk),
}
Expand All @@ -26,8 +28,9 @@ impl Deref for Disk {
// Let scsi and nvme disks deref to BasicDisk
fn deref(&self) -> &Self::Target {
match self {
Disk::Scsi(disk) => disk,
Disk::Mmc(disk) => disk,
Disk::Nvme(disk) => disk,
Disk::Scsi(disk) => disk,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/disks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod disk;
use std::{fs, io, path::PathBuf};

pub use disk::*;
pub mod mmc;
pub mod nvme;
pub mod partition;
pub mod scsi;
Expand Down Expand Up @@ -58,6 +59,8 @@ impl BlockDevice {
Disk::Scsi(disk)
} else if let Some(disk) = nvme::Disk::from_sysfs_path(&sysfs_dir, &entry) {
Disk::Nvme(disk)
} else if let Some(disk) = mmc::Disk::from_sysfs_path(&sysfs_dir, &entry) {
Disk::Mmc(disk)
} else {
continue;
};
Expand Down
48 changes: 48 additions & 0 deletions crates/disks/src/mmc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0

//! MMC device enumeration and handling
//!
//! This module provides functionality to enumerate and handle MMC (MultiMediaCard)
//! storage devices by parsing sysfs paths and device names.
use crate::{BasicDisk, DiskInit};
use regex::Regex;
use std::{ops::Deref, path::Path, sync::OnceLock};

/// Regex pattern to match valid MMC device names (e.g. mmcblk0)
static MMC_PATTERN: OnceLock<Regex> = OnceLock::new();

/// Represents an MMC disk device
#[derive(Debug)]
pub struct Disk(pub BasicDisk);

impl Deref for Disk {
type Target = BasicDisk;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DiskInit for Disk {
/// Creates a new MMC disk from a sysfs path and device name
///
/// # Arguments
/// * `sysroot` - The sysfs root path
/// * `name` - The device name to check
///
/// # Returns
/// * `Some(Disk)` if the device name matches MMC pattern
/// * `None` if name doesn't match or basic disk creation fails
fn from_sysfs_path(sysroot: &Path, name: &str) -> Option<Self> {
let regex =
MMC_PATTERN.get_or_init(|| Regex::new(r"^mmcblk\d+$").expect("Failed to initialise known-working regex"));
if regex.is_match(name) {
Some(Self(BasicDisk::from_sysfs_path(sysroot, name)?))
} else {
None
}
}
}

0 comments on commit aaed894

Please sign in to comment.