Better virtual file system, keyboard through MMIO&VirtIO
This commit is contained in:
118
src/fs.rs
118
src/fs.rs
@@ -2,44 +2,18 @@
|
||||
//!
|
||||
//! Implements a minimal disk backend and exposes a global FILE_SYSTEM used by
|
||||
//! the kernel to load user binaries.
|
||||
use core::{cell::UnsafeCell, ops::Deref};
|
||||
|
||||
use bffs::{
|
||||
io::{IoBase, Read, Seek},
|
||||
Fat32FileSystem,
|
||||
};
|
||||
use core::fmt::Debug;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use bffs::{Fat32FileSystem, ReadSeek, entry::DirEntry, file::File};
|
||||
|
||||
use io::{IoBase, Read, Seek, Write};
|
||||
|
||||
use crate::virtual_fs::{VirtualFileSystem, VirtualNode};
|
||||
|
||||
const DISK_ADDR: *const u8 = 0x9000_0000 as *const _;
|
||||
|
||||
/// Lazy holder for the kernel's filesystem instance.
|
||||
///
|
||||
/// The inner `UnsafeCell` allows one-time initialization at early boot while
|
||||
/// exposing a shared `&'static` reference through `Deref` once initialized.
|
||||
pub struct FSTemp(UnsafeCell<Option<Fat32FileSystem<Disk>>>);
|
||||
unsafe impl Sync for FSTemp {}
|
||||
|
||||
impl FSTemp {
|
||||
/// Initialize the global filesystem from the in-memory disk image.
|
||||
///
|
||||
/// Safety: must be called exactly once during early kernel initialization
|
||||
/// before any other filesystem operations occur.
|
||||
pub unsafe fn init(&self) {
|
||||
unsafe {
|
||||
*self.0.get() = Some(Fat32FileSystem::new(Disk::new(1024 * 1024 * 16)).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for FSTemp {
|
||||
type Target = Fat32FileSystem<Disk>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { (&*self.0.get()).as_ref().unwrap_unchecked() }
|
||||
}
|
||||
}
|
||||
|
||||
pub static FAT32_FILE_SYSTEM: FSTemp = FSTemp(UnsafeCell::new(None));
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Simple disk backend that reads from a fixed memory region.
|
||||
///
|
||||
@@ -62,11 +36,11 @@ impl IoBase for Disk {
|
||||
}
|
||||
|
||||
impl Seek for Disk {
|
||||
fn seek(&mut self, pos: bffs::io::SeekFrom) -> Result<u64, bffs::error::Error<Self::Error>> {
|
||||
fn seek(&mut self, pos: io::SeekFrom) -> Result<u64, Self::Error> {
|
||||
match pos {
|
||||
bffs::io::SeekFrom::Start(pos) => self.pos = pos,
|
||||
bffs::io::SeekFrom::End(_) => unimplemented!(),
|
||||
bffs::io::SeekFrom::Current(pos) => self.pos = (self.pos as i64 + pos) as u64,
|
||||
io::SeekFrom::Start(pos) => self.pos = pos,
|
||||
io::SeekFrom::End(_) => unimplemented!(),
|
||||
io::SeekFrom::Current(pos) => self.pos = (self.pos as i64 + pos) as u64,
|
||||
}
|
||||
Ok(self.pos)
|
||||
}
|
||||
@@ -74,7 +48,7 @@ impl Seek for Disk {
|
||||
|
||||
impl Read for Disk {
|
||||
/// Read bytes from the in-memory disk image into `buf`.
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, bffs::error::Error<Self::Error>> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
if self.pos >= self.size {
|
||||
return Ok(0);
|
||||
}
|
||||
@@ -90,3 +64,69 @@ impl Read for Disk {
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Fat32VirtualNode<'a, T> {
|
||||
kind: Fat32VirtualNodeType<'a, T>,
|
||||
_entry: DirEntry<'a, T>,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
enum Fat32VirtualNodeType<'a, T> {
|
||||
Dir,
|
||||
File(File<'a, T>),
|
||||
}
|
||||
|
||||
impl<'a, T: ReadSeek> Fat32VirtualNode<'a, T> {
|
||||
pub unsafe fn new(entry: DirEntry<'a, T>) -> Self {
|
||||
let kind = if entry.is_dir() {
|
||||
Fat32VirtualNodeType::Dir
|
||||
} else {
|
||||
Fat32VirtualNodeType::File(entry.to_file())
|
||||
};
|
||||
Self {
|
||||
kind,
|
||||
_entry: entry,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ReadSeek + Debug> VirtualNode for Fat32VirtualNode<'_, T> {}
|
||||
|
||||
impl<T> IoBase for Fat32VirtualNode<'_, T> {
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl<T: ReadSeek + Debug> Seek for Fat32VirtualNode<'_, T> {
|
||||
fn seek(&mut self, pos: io::SeekFrom) -> Result<u64, Self::Error> {
|
||||
match &mut self.kind {
|
||||
Fat32VirtualNodeType::Dir => todo!(),
|
||||
Fat32VirtualNodeType::File(file) => file.seek(pos).map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ReadSeek + Debug> Read for Fat32VirtualNode<'_, T> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
|
||||
match &mut self.kind {
|
||||
Fat32VirtualNodeType::Dir => unimplemented!(),
|
||||
Fat32VirtualNodeType::File(file) => file.read(buf).map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ReadSeek + Debug> Write for Fat32VirtualNode<'_, T> {
|
||||
fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ReadSeek + Debug> VirtualFileSystem for Fat32FileSystem<T> {
|
||||
fn open(&mut self, path: &bffs::path::Path) -> Result<Box<dyn VirtualNode + '_>, ()> {
|
||||
let entry = self.open_entry(path).unwrap();
|
||||
Ok(Box::new(unsafe { Fat32VirtualNode::new(entry) }))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user