feat(vfs): file read from initrd in kernel

This commit is contained in:
2021-10-25 16:41:30 +03:00
parent 0dbadd52d0
commit 6d8f0d01ef
17 changed files with 1212 additions and 11 deletions
+21
View File
@@ -0,0 +1,21 @@
#![allow(missing_docs)]
use crate::mem::{self, phys::{self, PageUsage}};
use memfs::BlockAllocator;
#[derive(Clone, Copy)]
pub struct MemfsBlockAlloc;
unsafe impl BlockAllocator for MemfsBlockAlloc {
fn alloc(&self) -> *mut u8 {
if let Ok(page) = phys::alloc_page(PageUsage::Kernel) {
mem::virtualize(page) as *mut u8
} else {
core::ptr::null_mut()
}
}
unsafe fn dealloc(&self, data: *mut u8) {
todo!()
}
}
+3 -2
View File
@@ -10,7 +10,7 @@
const_panic,
panic_info_message,
alloc_error_handler,
const_btree_new,
const_btree_new
)]
#![no_std]
#![no_main]
@@ -27,12 +27,13 @@ pub mod debug;
pub mod arch;
pub mod dev;
pub mod fs;
pub mod mem;
pub mod proc;
pub mod sync;
pub mod util;
#[allow(missing_docs)]
pub mod syscall;
pub mod util;
#[panic_handler]
fn panic_handler(pi: &core::panic::PanicInfo) -> ! {
+24 -8
View File
@@ -57,15 +57,31 @@ pub unsafe fn enter(initrd: Option<(usize, usize)>) -> ! {
if let Some((start, end)) = initrd {
let initrd = Box::into_raw(Box::new((mem::virtualize(start), mem::virtualize(end))));
for _ in 0..4 {
spawn!(fn (initrd_ptr: usize) {
debugln!("Running kernel init process");
spawn!(fn (initrd_ptr: usize) {
use memfs::Ramfs;
use vfs::{Filesystem, Ioctx};
use crate::fs::MemfsBlockAlloc;
debugln!("Running kernel init process");
let (start, _end) = unsafe { *(initrd_ptr as *const (usize, usize)) };
Process::execve(|space| elf::load_elf(space, start as *const u8), 0).unwrap();
panic!("This code should not run");
}, initrd as usize);
}
let (start, end) = unsafe { *(initrd_ptr as *const (usize, usize)) };
let size = end - start;
let fs = Ramfs::open(start as *mut u8, size, MemfsBlockAlloc {}).unwrap();
let root = fs.root().unwrap();
let ioctx = Ioctx::new(root);
// Open a test file
let node = ioctx.find(None, "/test.txt").unwrap();
let mut file = node.open().unwrap();
let mut buf = [0u8; 16];
while let Ok(count) = file.read(&mut buf) {
if count == 0 {
break;
}
debugln!("Read {} bytes: {:?}", count, &buf[0..count]);
}
}, initrd as usize);
}
SCHED.enter();
}