Skip to main content

starnix_core/device/
block.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::device::DeviceMode;
6use crate::device::kobject::DeviceMetadata;
7use crate::fs::sysfs::{BlockDeviceInfo, build_block_device_directory};
8use crate::task::{CurrentTask, Kernel};
9use crate::vfs::{FileOps, FsString, NamespaceNode};
10use starnix_logging::track_stub;
11use starnix_uapi::device_id::DeviceId;
12use starnix_uapi::errors::Errno;
13use starnix_uapi::open_flags::OpenFlags;
14use starnix_uapi::user_address::ArchSpecific;
15use starnix_uapi::{errno, uapi};
16use std::sync::Arc;
17
18pub fn canonicalize_ioctl_request(current_task: &CurrentTask, request: u32) -> u32 {
19    if current_task.is_arch32() {
20        match request {
21            uapi::arch32::BLKGETSIZE64 => uapi::BLKGETSIZE64,
22            _ => request,
23        }
24    } else {
25        request
26    }
27}
28
29pub struct MmcBlockDevice;
30
31impl BlockDeviceInfo for MmcBlockDevice {
32    fn size(&self) -> Result<usize, Errno> {
33        track_stub!(TODO("https://fxbug.dev/488067251"), "mmcblk query size");
34        Err(errno!(ENOTSUP))
35    }
36}
37
38fn open_mmc_block_device(
39    _current_task: &CurrentTask,
40    _id: DeviceId,
41    _node: &NamespaceNode,
42    _flags: OpenFlags,
43) -> Result<Box<dyn FileOps>, Errno> {
44    track_stub!(TODO("https://fxbug.dev/488067251"), "mmcblk open device");
45    Err(errno!(ENOTSUP))
46}
47
48/// Adds an mmc block device at /dev/block/mmcblk0. The current implementation is just a stub that
49/// exports the typical sysfs layout for block devices, but cannot be read from or written to.
50pub fn add_mmc_block_device(kernel: &Kernel) -> Result<Arc<MmcBlockDevice>, Errno> {
51    let name = FsString::from("mmcblk0");
52    let class = kernel.device_registry.objects.virtual_block_class();
53    let device = Arc::new(MmcBlockDevice);
54    let device_weak = Arc::downgrade(&device);
55    kernel.device_registry.register_device_with_dir(
56        kernel,
57        name.as_ref(),
58        DeviceMetadata::new(name.clone(), DeviceId::MMCBLK0, DeviceMode::Block),
59        class,
60        |device, dir| build_block_device_directory(device, device_weak, dir),
61        open_mmc_block_device,
62    )?;
63    Ok(device)
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::testing::{anon_test_file, spawn_kernel_and_run};
70    use crate::vfs::VecOutputBuffer;
71    use starnix_uapi::open_flags::OpenFlags;
72
73    #[::fuchsia::test]
74    async fn test_mmc_block_device() {
75        spawn_kernel_and_run(async |current_task| {
76            let _device = add_mmc_block_device(current_task.kernel()).unwrap();
77            let class = current_task.kernel().device_registry.objects.virtual_block_class();
78            // The device should have a typical sysfs layout for block devices.
79            assert!(class.dir.lookup(b"mmcblk0/holders".into()).is_some());
80            // We should be able to open the size node of the stub device, but reading it will fail
81            // since right now it is just a stub implementation.
82            let size_node = class.dir.lookup(b"mmcblk0/size".into()).unwrap();
83            let file_ops = size_node.create_file_ops(&current_task, OpenFlags::RDONLY).unwrap();
84            let file = anon_test_file(&current_task, file_ops, OpenFlags::RDONLY);
85            let mut buf = VecOutputBuffer::new(10);
86            assert_eq!(file.read(&current_task, &mut buf).unwrap_err(), errno!(ENOTSUP));
87        })
88        .await;
89    }
90}