Skip to content

Commit 6ef7454

Browse files
authored
Merge pull request #220 from wedsonaf/sample
Add `rust_random` sample that uses `module_misc_device`.
2 parents 5525db3 + c828d82 commit 6ef7454

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ config SAMPLE_RUST_SEMAPHORE_C
100100

101101
If unsure, say N.
102102

103+
config SAMPLE_RUST_RANDOM
104+
tristate "Random"
105+
help
106+
This option builds the Rust random sample.
107+
108+
To compile this as a module, choose M here:
109+
the module will be called rust_random.
110+
111+
If unsure, say N.
112+
103113
endif # SAMPLES_RUST

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ obj-$(CONFIG_SAMPLE_RUST_MISCDEV) += rust_miscdev.o
99
obj-$(CONFIG_SAMPLE_RUST_STACK_PROBING) += rust_stack_probing.o
1010
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE) += rust_semaphore.o
1111
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE_C) += rust_semaphore_c.o
12+
obj-$(CONFIG_SAMPLE_RUST_RANDOM) += rust_random.o

samples/rust/rust_random.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust random device
4+
//!
5+
//! Adapted from Alex Gaynor's original available at
6+
//! <https://github.com/alex/just-use/blob/master/src/lib.rs>.
7+
8+
#![no_std]
9+
#![feature(allocator_api, global_asm)]
10+
11+
use kernel::{
12+
file_operations::{File, FileOperations},
13+
prelude::*,
14+
user_ptr::{UserSlicePtrReader, UserSlicePtrWriter},
15+
};
16+
17+
#[derive(Default)]
18+
struct RandomFile;
19+
20+
impl FileOperations for RandomFile {
21+
kernel::declare_file_operations!(read, write);
22+
23+
fn read(&self, file: &File, buf: &mut UserSlicePtrWriter, _offset: u64) -> KernelResult<usize> {
24+
let total_len = buf.len();
25+
let mut chunkbuf = [0; 256];
26+
27+
while !buf.is_empty() {
28+
let len = chunkbuf.len().min(buf.len());
29+
let chunk = &mut chunkbuf[0..len];
30+
31+
if file.is_blocking() {
32+
kernel::random::getrandom(chunk)?;
33+
} else {
34+
kernel::random::getrandom_nonblock(chunk)?;
35+
}
36+
buf.write_slice(chunk)?;
37+
}
38+
Ok(total_len)
39+
}
40+
41+
fn write(&self, buf: &mut UserSlicePtrReader, _offset: u64) -> KernelResult<usize> {
42+
let total_len = buf.len();
43+
let mut chunkbuf = [0; 256];
44+
while !buf.is_empty() {
45+
let len = chunkbuf.len().min(buf.len());
46+
let chunk = &mut chunkbuf[0..len];
47+
buf.read_slice(chunk)?;
48+
kernel::random::add_randomness(chunk);
49+
}
50+
Ok(total_len)
51+
}
52+
}
53+
54+
module_misc_device! {
55+
type: RandomFile,
56+
name: b"rust_random",
57+
author: b"Rust for Linux Contributors",
58+
description: b"Just use /dev/urandom: Now with early-boot safety",
59+
license: b"GPL v2",
60+
}

0 commit comments

Comments
 (0)