Skip to content

Commit aaed894

Browse files
committed
disks: Add MMC detection
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 0326002 commit aaed894

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

crates/disks/src/disk.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ use std::{
99
path::{Path, PathBuf},
1010
};
1111

12-
use crate::{nvme, partition::Partition, scsi, sysfs, DEVFS_DIR};
12+
use crate::{mmc, nvme, partition::Partition, scsi, sysfs, DEVFS_DIR};
1313

1414
/// Represents the type of disk device.
1515
#[derive(Debug)]
1616
pub enum Disk {
1717
/// SCSI disk device (e.g. sda, sdb)
1818
Scsi(scsi::Disk),
19+
/// MMC disk device (e.g. mmcblk0)
20+
Mmc(mmc::Disk),
1921
/// NVMe disk device (e.g. nvme0n1)
2022
Nvme(nvme::Disk),
2123
}
@@ -26,8 +28,9 @@ impl Deref for Disk {
2628
// Let scsi and nvme disks deref to BasicDisk
2729
fn deref(&self) -> &Self::Target {
2830
match self {
29-
Disk::Scsi(disk) => disk,
31+
Disk::Mmc(disk) => disk,
3032
Disk::Nvme(disk) => disk,
33+
Disk::Scsi(disk) => disk,
3134
}
3235
}
3336
}

crates/disks/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod disk;
66
use std::{fs, io, path::PathBuf};
77

88
pub use disk::*;
9+
pub mod mmc;
910
pub mod nvme;
1011
pub mod partition;
1112
pub mod scsi;
@@ -58,6 +59,8 @@ impl BlockDevice {
5859
Disk::Scsi(disk)
5960
} else if let Some(disk) = nvme::Disk::from_sysfs_path(&sysfs_dir, &entry) {
6061
Disk::Nvme(disk)
62+
} else if let Some(disk) = mmc::Disk::from_sysfs_path(&sysfs_dir, &entry) {
63+
Disk::Mmc(disk)
6164
} else {
6265
continue;
6366
};

crates/disks/src/mmc.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
//! MMC device enumeration and handling
6+
//!
7+
//! This module provides functionality to enumerate and handle MMC (MultiMediaCard)
8+
//! storage devices by parsing sysfs paths and device names.
9+
10+
use crate::{BasicDisk, DiskInit};
11+
use regex::Regex;
12+
use std::{ops::Deref, path::Path, sync::OnceLock};
13+
14+
/// Regex pattern to match valid MMC device names (e.g. mmcblk0)
15+
static MMC_PATTERN: OnceLock<Regex> = OnceLock::new();
16+
17+
/// Represents an MMC disk device
18+
#[derive(Debug)]
19+
pub struct Disk(pub BasicDisk);
20+
21+
impl Deref for Disk {
22+
type Target = BasicDisk;
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.0
26+
}
27+
}
28+
29+
impl DiskInit for Disk {
30+
/// Creates a new MMC disk from a sysfs path and device name
31+
///
32+
/// # Arguments
33+
/// * `sysroot` - The sysfs root path
34+
/// * `name` - The device name to check
35+
///
36+
/// # Returns
37+
/// * `Some(Disk)` if the device name matches MMC pattern
38+
/// * `None` if name doesn't match or basic disk creation fails
39+
fn from_sysfs_path(sysroot: &Path, name: &str) -> Option<Self> {
40+
let regex =
41+
MMC_PATTERN.get_or_init(|| Regex::new(r"^mmcblk\d+$").expect("Failed to initialise known-working regex"));
42+
if regex.is_match(name) {
43+
Some(Self(BasicDisk::from_sysfs_path(sysroot, name)?))
44+
} else {
45+
None
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)