|
| 1 | +//! A module for custom IOCTL objects |
| 2 | +
|
| 3 | +use alloc::boxed::Box; |
| 4 | +use alloc::sync::Arc; |
| 5 | +use alloc::vec::Vec; |
| 6 | +use core::fmt::Debug; |
| 7 | + |
| 8 | +use bitfield_struct::bitfield; |
| 9 | + |
| 10 | +use crate::fd::{AccessPermission, ObjectInterface}; |
| 11 | +use crate::fs::{NodeKind, VfsNode}; |
| 12 | +use crate::io; |
| 13 | + |
| 14 | +/// Encoding for an IOCTL command, as done in the Linux Kernel. |
| 15 | +/// |
| 16 | +/// See [relevant kernel header](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/ioctl.h?h=v6.15) for reference. |
| 17 | +/// |
| 18 | +/// The goal of this interface is to easily support linux applications that communicate via IOCTL, |
| 19 | +/// so linux compatibility is an intended and explicit goal here. |
| 20 | +#[bitfield(u32)] |
| 21 | +pub struct IoCtlCall { |
| 22 | + call_nr: u8, |
| 23 | + |
| 24 | + call_type: u8, |
| 25 | + |
| 26 | + #[bits(2, from = IoCtlDirection::from_bits_truncate, default = IoCtlDirection::empty())] |
| 27 | + call_dir: IoCtlDirection, |
| 28 | + |
| 29 | + #[bits(14)] |
| 30 | + call_size: u16, |
| 31 | +} |
| 32 | + |
| 33 | +bitflags! { |
| 34 | + #[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] |
| 35 | + pub struct IoCtlDirection: u8 { |
| 36 | + const IOC_WRITE = 1; |
| 37 | + const IOC_READ = 2; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl IoCtlDirection { |
| 42 | + // Required for IoCtlCall |
| 43 | + const fn into_bits(self) -> u8 { |
| 44 | + self.bits() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug)] |
| 49 | +struct IoCtlNode(Arc<dyn ObjectInterface>); |
| 50 | + |
| 51 | +impl VfsNode for IoCtlNode { |
| 52 | + fn get_kind(&self) -> NodeKind { |
| 53 | + NodeKind::File |
| 54 | + } |
| 55 | + |
| 56 | + fn get_object(&self) -> io::Result<Arc<dyn ObjectInterface>> { |
| 57 | + Ok(self.0.clone()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Register a custom object to handle IOCTLS at a given path, |
| 62 | +/// |
| 63 | +/// This call mounts a trivial VfsNode that opens to the provided ioctl_object at the given path. |
| 64 | +#[allow(dead_code)] |
| 65 | +pub(crate) fn register_ioctl(path: &str, ioctl_object: Arc<dyn ObjectInterface>) { |
| 66 | + assert!(path.starts_with("/")); |
| 67 | + let mut path: Vec<&str> = path.split("/").skip(1).collect(); |
| 68 | + assert!(!path.is_empty()); |
| 69 | + |
| 70 | + let fs = super::FILESYSTEM |
| 71 | + .get() |
| 72 | + .expect("Failed to mount ioctl: filesystem is not yet initialized"); |
| 73 | + |
| 74 | + // Create parent directory |
| 75 | + let mut directory: Vec<&str> = path.clone(); |
| 76 | + directory.pop().unwrap(); // remove file name |
| 77 | + directory.reverse(); |
| 78 | + |
| 79 | + if !directory.is_empty() { |
| 80 | + let _ = fs |
| 81 | + .root |
| 82 | + .traverse_mkdir(&mut directory, AccessPermission::all()); // ignore possible errors at this step |
| 83 | + } |
| 84 | + |
| 85 | + // Mount the file |
| 86 | + path.reverse(); |
| 87 | + fs.root |
| 88 | + .traverse_mount(&mut path, Box::new(IoCtlNode(ioctl_object))) |
| 89 | + .expect("Failed to mount ioctl: filesystem error"); |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(all(test, not(target_os = "none")))] |
| 93 | +mod tests { |
| 94 | + use super::{IoCtlCall, IoCtlDirection}; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn ioctl_call_correctly_written() { |
| 98 | + let call_nr = 0x12u8; |
| 99 | + let call_type = 0x78u8; |
| 100 | + let call_dir = IoCtlDirection::IOC_WRITE; |
| 101 | + let call_size = 0x423u16; |
| 102 | + |
| 103 | + let ioctl_call_number = (u32::from(call_size) << 18) |
| 104 | + | (u32::from(call_dir.bits()) << 16) |
| 105 | + | (u32::from(call_type) << 8) |
| 106 | + | u32::from(call_nr); |
| 107 | + |
| 108 | + let call = IoCtlCall::new() |
| 109 | + .with_call_nr(call_nr) |
| 110 | + .with_call_type(call_type) |
| 111 | + .with_call_dir(call_dir) |
| 112 | + .with_call_size(call_size); |
| 113 | + |
| 114 | + assert_eq!(ioctl_call_number, call.into_bits()); |
| 115 | + } |
| 116 | + #[test] |
| 117 | + fn ioctl_call_correctly_parsed() { |
| 118 | + let call_nr = 0x12u8; |
| 119 | + let call_type = 0x78u8; |
| 120 | + let call_dir = IoCtlDirection::IOC_WRITE; |
| 121 | + let call_size = 0x423u16; |
| 122 | + |
| 123 | + let ioctl_call_number = (u32::from(call_size) << 18) |
| 124 | + | (u32::from(call_dir.bits()) << 16) |
| 125 | + | (u32::from(call_type) << 8) |
| 126 | + | u32::from(call_nr); |
| 127 | + |
| 128 | + let parsed = IoCtlCall::from_bits(ioctl_call_number); |
| 129 | + |
| 130 | + assert_eq!(call_nr, parsed.call_nr()); |
| 131 | + assert_eq!(call_type, parsed.call_type()); |
| 132 | + assert_eq!(call_dir, parsed.call_dir()); |
| 133 | + assert_eq!(call_size, parsed.call_size()); |
| 134 | + } |
| 135 | +} |
0 commit comments