Skip to content

Commit dbca725

Browse files
author
lhw
committed
feat: add virtio-rng into fs random
1 parent 83ff1a4 commit dbca725

8 files changed

Lines changed: 466 additions & 274 deletions

File tree

Cargo.lock

Lines changed: 402 additions & 272 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/ruxfeat/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ display = [
7777
]
7878

7979
#virtio random number generator
80-
virtio-rng = ["ruxdriver/virtio-rng","ruxruntime/rng"]
80+
virtio-rng = ["ruxdriver/virtio-rng","ruxruntime/rng","ruxfs/rng"]
8181

8282
# 9P
8383
virtio-9p = [

modules/ruxdriver/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl AllDevices {
139139
/// Probes all supported devices.
140140
fn probe(&mut self) {
141141
for_each_drivers!(type Driver, {
142+
info!("Probing for {:?} devices...", core::any::type_name::<Driver>());
142143
if let Some(dev) = Driver::probe_global() {
143144
info!(
144145
"registered a new {:?} device: {:?}",

modules/ruxfs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ myfs = []
2828
use-ramdisk = []
2929
alloc = ["axalloc"]
3030
fp_simd = []
31+
rng = ["dep:ruxrand"]
3132

3233
default = ["devfs", "ramfs", "procfs", "sysfs", "etcfs", "mntfs"]
3334

@@ -49,6 +50,7 @@ ruxfifo = { path = "../../crates/ruxfifo" }
4950
crate_interface = { version = "0.1.1" }
5051
ruxfdtable = { path = "../ruxfdtable" }
5152
ruxdriver = { path = "../ruxdriver", features = ["block"] }
53+
ruxrand = { path = "../ruxrand", optional = true }
5254
axalloc = { path = "../axalloc", optional = true }
5355
memory_addr = "0.1.0"
5456
# lwext4_rust = { git = "https://github.com/elliott10/lwext4_rust", optional = true }

modules/ruxfs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub mod fifo;
4848
mod file;
4949
pub mod fops;
5050
pub mod fuse_st;
51+
#[cfg(feature = "rng")]
52+
pub mod rng_rand;
5153
pub mod root;
5254

5355
pub use directory::Directory;

modules/ruxfs/src/mounts.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ use crate::fs;
1818
pub(crate) fn devfs() -> Arc<fs::devfs::DeviceFileSystem> {
1919
let null = fs::devfs::NullDev;
2020
let zero = fs::devfs::ZeroDev;
21+
#[cfg(feature = "rng")]
22+
let random = crate::rng_rand::RandomDev;
23+
#[cfg(feature = "rng")]
24+
let urandom = crate::rng_rand::RandomDev;
25+
#[cfg(not(feature = "rng"))]
2126
let random = fs::devfs::RandomDev;
27+
#[cfg(not(feature = "rng"))]
2228
let urandom = fs::devfs::RandomDev;
2329
let fuse = crate::devfuse::FuseDev::new();
2430
let pts = fs::devfs::init_pts();

modules/ruxfs/src/rng_rand.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (c) [2023] [Syswonder Community]
2+
* [Rukos] is licensed under Mulan PSL v2.
3+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
* You may obtain a copy of Mulan PSL v2 at:
5+
* http://license.coscl.org.cn/MulanPSL2
6+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7+
* See the Mulan PSL v2 for more details.
8+
*/
9+
//! random number generator from ruxrand for `/dev/random` and `/dev/urandom`
10+
11+
use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodePerm, VfsNodeType, VfsResult};
12+
13+
use axerrno::AxError;
14+
15+
use ruxrand::request_entropy;
16+
17+
/// A random device behaves like `/dev/random`.
18+
///
19+
/// It always returns a chunk of random bytes when read, and all writes are discarded.
20+
pub struct RandomDev;
21+
22+
impl VfsNodeOps for RandomDev {
23+
fn get_attr(&self) -> VfsResult<VfsNodeAttr> {
24+
Ok(VfsNodeAttr::new(
25+
6,
26+
VfsNodePerm::default_file(),
27+
VfsNodeType::CharDevice,
28+
0,
29+
0,
30+
))
31+
}
32+
33+
fn set_mode(&self, _mode: VfsNodePerm) -> VfsResult {
34+
Ok(())
35+
}
36+
37+
fn read_at(&self, _offset: u64, buf: &mut [u8]) -> VfsResult<usize> {
38+
request_entropy(buf).map_err(|_e| AxError::Io)?;
39+
Ok(buf.len())
40+
}
41+
42+
fn write_at(&self, _offset: u64, buf: &[u8]) -> VfsResult<usize> {
43+
Ok(buf.len())
44+
}
45+
46+
fn truncate(&self, _size: u64) -> VfsResult {
47+
Ok(())
48+
}
49+
50+
axfs_vfs::impl_vfs_non_dir_default! {}
51+
}

modules/ruxhal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ tty = { path = "../../crates/tty" }
6666

6767
[target.'cfg(target_arch = "x86_64")'.dependencies]
6868
x86 = "0.52"
69-
x86_64 = "0.14.13"
69+
x86_64 = "0.15.2"
7070
x2apic = "0.5"
7171
raw-cpuid = "11.0"
7272

0 commit comments

Comments
 (0)