Last active
October 8, 2023 07:11
-
-
Save officialcjunior/c05614c521eb78af160d72a396891311 to your computer and use it in GitHub Desktop.
rust_blr.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Virtual Device Module | |
use kernel::prelude::*; | |
use kernel::file::{File, Operations}; | |
use kernel::io_buffer::{IoBufferReader, IoBufferWriter}; | |
use kernel::sync::smutex::Mutex; | |
use kernel::sync::{Arc, ArcBorrow}; | |
use kernel::{miscdev, Module}; | |
module! { | |
type: RustBlrDev, | |
name: "rustblrdev", | |
license: "GPL", | |
} | |
struct Device { | |
number: usize, | |
contents: Mutex<Vec<u8>>, | |
} | |
struct RustBlrDev { | |
_dev: Pin<Box<miscdev::Registration<RustBlrDev>>>, | |
} | |
#[vtable] | |
impl Operations for RustBlrDev { | |
type OpenData = Arc<Device>; | |
type Data = Arc<Device>; | |
fn open(context: &Arc<Device>, _file: &File) -> Result<Arc<Device>> { | |
pr_info!("Rust BLR device was opened!\n"); | |
Ok(context.clone()) | |
} | |
fn read( | |
data: ArcBorrow<'_, Device>, | |
_file: &File, | |
writer: &mut impl IoBufferWriter, | |
offset: u64, | |
) -> Result<usize> { | |
pr_info!("File for device {} was read\n", data.number); | |
let offset = offset.try_into()?; | |
let vec = data.contents.lock(); | |
let len = core::cmp::min(writer.len(), vec.len().saturating_sub(offset)); | |
writer.write_slice(&vec[offset..][..len])?; | |
Ok(len) | |
} | |
fn write( | |
data: ArcBorrow<'_, Device>, | |
_file: &File, | |
reader: &mut impl IoBufferReader, | |
_offset: u64, | |
) -> Result<usize> { | |
pr_info!("File for device {} was written\n", data.number); | |
let copy = reader.read_all()?; | |
let len = copy.len(); | |
*data.contents.lock() = copy; | |
/* Acquire a lock on the contents Mutex | |
let contents_lock = data.contents.lock(); | |
Access the Vec<u8> inside the Mutex | |
let contents = &*contents_lock; | |
pr_info!("{:#?}", contents); | |
*/ | |
Ok(len) | |
} | |
} | |
impl Module for RustBlrDev { | |
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> { | |
pr_info!("initialize rustblrdev module!\n"); | |
let mut vec1 = Vec::new(); | |
vec1.try_push(65)?; | |
vec1.try_push(116)?; | |
vec1.try_push(32)?; | |
vec1.try_push(108)?; | |
vec1.try_push(101)?; | |
vec1.try_push(97)?; | |
vec1.try_push(115)?; | |
vec1.try_push(116)?; | |
vec1.try_push(32)?; | |
vec1.try_push(116)?; | |
vec1.try_push(104)?; | |
vec1.try_push(101)?; | |
vec1.try_push(32)?; | |
vec1.try_push(119)?; | |
vec1.try_push(101)?; | |
vec1.try_push(97)?; | |
vec1.try_push(116)?; | |
vec1.try_push(104)?; | |
vec1.try_push(101)?; | |
vec1.try_push(114)?; | |
vec1.try_push(32)?; | |
vec1.try_push(105)?; | |
vec1.try_push(115)?; | |
vec1.try_push(32)?; | |
vec1.try_push(103)?; | |
vec1.try_push(111)?; | |
vec1.try_push(111)?; | |
vec1.try_push(100)?; | |
vec1.try_push(33)?; | |
vec1.try_push(0x0a)?; | |
let dev = Arc::try_new(Device { | |
number: 0, | |
contents: Mutex::new(vec1), | |
})?; | |
let reg = miscdev::Registration::new_pinned(fmt!("rustblrdev"), dev)?; | |
Ok(Self { _dev: reg }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment