diff --git a/crates/kshell/src/lib.rs b/crates/kshell/src/lib.rs index 9f3df3d9c..2fac32971 100644 --- a/crates/kshell/src/lib.rs +++ b/crates/kshell/src/lib.rs @@ -13,7 +13,7 @@ use hyperion_futures::keyboard::KeyboardEvents; use hyperion_kernel_impl::VFS_ROOT; use hyperion_log::*; use hyperion_scheduler::lock::Mutex; -use hyperion_vfs::{error::IoError, path::PathBuf, ramdisk}; +use hyperion_vfs::{error::IoError, path::PathBuf, ramdisk::StaticRoFile}; use snafu::Snafu; use self::{shell::Shell, term::Term}; @@ -53,18 +53,14 @@ pub async fn kshell() { for asset in ASSETS { let (path, bytes): (&str, &[u8]) = *asset; - // TODO: no copy - VFS_ROOT.install_dev(path, ramdisk::File::new(bytes.into())); + VFS_ROOT.install_dev(path, StaticRoFile::new(bytes)); } - let bin = load_elf!("SAMPLE_ELF").into(); - VFS_ROOT.install_dev("/bin/run", ramdisk::File::new(bin)); - let bin = load_elf!("FBTEST").into(); - VFS_ROOT.install_dev("/bin/fbtest", ramdisk::File::new(bin)); + VFS_ROOT.install_dev("/bin/run", StaticRoFile::new(load_elf!("SAMPLE_ELF"))); + VFS_ROOT.install_dev("/bin/fbtest", StaticRoFile::new(load_elf!("FBTEST"))); // everything is the same file - let bin = load_elf!("COREUTILS").into(); - let bin = Arc::new(Mutex::new(ramdisk::File::new(bin))); + let bin = Arc::new(Mutex::new(StaticRoFile::new(load_elf!("COREUTILS")))); VFS_ROOT.install_dev_ref("/bin/coreutils", bin.clone()); VFS_ROOT.install_dev_ref("/bin/cat", bin.clone()); VFS_ROOT.install_dev_ref("/bin/ls", bin.clone()); diff --git a/crates/vfs/src/ramdisk.rs b/crates/vfs/src/ramdisk.rs index f97151f83..d0a1ed162 100644 --- a/crates/vfs/src/ramdisk.rs +++ b/crates/vfs/src/ramdisk.rs @@ -3,6 +3,7 @@ use alloc::{ sync::Arc, vec::Vec, }; +use core::any::Any; use lock_api::Mutex; @@ -23,6 +24,20 @@ impl File { pub fn new(bytes: Vec) -> Self { Self { bytes } } + + pub fn new_empty() -> FileRef { + Arc::new(Mutex::new(Self { bytes: Vec::new() })) as _ + } +} + +pub struct StaticRoFile { + bytes: &'static [u8], +} + +impl StaticRoFile { + pub const fn new(bytes: &'static [u8]) -> Self { + Self { bytes } + } } pub struct Directory { @@ -36,7 +51,7 @@ pub struct Directory { // impl FileDevice for File { - fn as_any(&self) -> &dyn core::any::Any { + fn as_any(&self) -> &dyn Any { self } @@ -45,19 +60,31 @@ impl FileDevice for File { } fn read(&self, offset: usize, buf: &mut [u8]) -> IoResult { - FileDevice::read(&self.bytes[..], offset, buf) + self.bytes.read(offset, buf) } fn write(&mut self, offset: usize, buf: &[u8]) -> IoResult { self.bytes .resize(self.bytes.len().max(buf.len() + offset), b'?'); - FileDevice::write(&mut self.bytes[..], offset, buf) + self.bytes.write(offset, buf) } } -impl File { - pub fn new_empty() -> FileRef { - Arc::new(Mutex::new(Self { bytes: Vec::new() })) as _ +impl FileDevice for StaticRoFile { + fn as_any(&self) -> &dyn Any { + self + } + + fn len(&self) -> usize { + self.bytes.len() + } + + fn read(&self, offset: usize, buf: &mut [u8]) -> IoResult { + self.bytes.read(offset, buf) + } + + fn write(&mut self, _: usize, _: &[u8]) -> IoResult { + Err(IoError::PermissionDenied) } }