Refactor
This commit is contained in:
22
src/fs.rs
22
src/fs.rs
@@ -1,3 +1,7 @@
|
||||
//! Simple wrapper around a FAT32 image exposed to the kernel.
|
||||
//!
|
||||
//! 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::{
|
||||
@@ -7,10 +11,18 @@ use bffs::{
|
||||
|
||||
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());
|
||||
@@ -29,12 +41,17 @@ impl Deref for FSTemp {
|
||||
pub static FILE_SYSTEM: FSTemp = FSTemp(UnsafeCell::new(None));
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Simple disk backend that reads from a fixed memory region.
|
||||
///
|
||||
/// The `Disk` struct provides `Read` and `Seek` implementations over a
|
||||
/// contiguous in-memory image exposed at DISK_ADDR.
|
||||
pub struct Disk {
|
||||
pos: u64,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
impl Disk {
|
||||
/// Create a new `Disk` representing an in-memory image of `size` bytes.
|
||||
pub fn new(size: u64) -> Self {
|
||||
Self { pos: 0, size }
|
||||
}
|
||||
@@ -56,12 +73,15 @@ 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>> {
|
||||
if self.pos >= self.size {
|
||||
return Ok(0);
|
||||
}
|
||||
let size = usize::min(buf.len(), (self.size - self.pos) as usize);
|
||||
(0..size).for_each(|i| buf[i] = unsafe { *DISK_ADDR.byte_add(i + self.pos as usize) });
|
||||
for i in 0..size {
|
||||
buf[i] = unsafe { *DISK_ADDR.byte_add(i + self.pos as usize) };
|
||||
}
|
||||
self.pos += size as u64;
|
||||
Ok(size)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user