1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 02:03:29 +04:00
rCore/kernel/src/fs.rs

82 lines
2.6 KiB
Rust
Raw Normal View History

use simple_filesystem::*;
use alloc::boxed::Box;
2018-05-23 20:25:27 +04:00
use arch::driver::ide;
use spin::Mutex;
use process;
2018-07-13 21:11:47 +04:00
#[cfg(not(feature = "link_user_program"))]
2018-05-23 20:25:27 +04:00
pub fn load_sfs() {
// let slice = unsafe { MemBuf::new(_binary_user_ucore32_img_start, _binary_user_ucore32_img_end) };
let sfs = SimpleFileSystem::open(Box::new(&ide::DISK0)).unwrap();
let root = sfs.root_inode();
let files = root.borrow().list().unwrap();
trace!("Loading programs: {:?}", files);
// for name in files.iter().filter(|&f| f != "." && f != "..") {
for name in files.iter().filter(|&f| f == "sleep") {
static mut BUF: [u8; 64 << 12] = [0; 64 << 12];
let file = root.borrow().lookup(name.as_str()).unwrap();
let len = file.borrow().read_at(0, unsafe { &mut BUF }).unwrap();
process::add_user_process(name, unsafe { &BUF[..len] });
}
process::print();
}
2018-07-13 21:11:47 +04:00
#[cfg(feature = "link_user_program")]
pub fn load_sfs() {
let slice = unsafe {
slice::from_raw_parts(_binary_hello_start as *const u8,
_binary_hello_size as usize)
};
process::add_user_process("hello", slice);
process::print();
}
2018-05-23 20:25:27 +04:00
#[cfg(feature = "link_user_program")]
extern {
2018-07-13 21:11:47 +04:00
fn _binary_hello_start();
fn _binary_hello_size();
}
struct MemBuf(&'static [u8]);
impl MemBuf {
unsafe fn new(begin: unsafe extern fn(), end: unsafe extern fn()) -> Self {
use core::slice;
MemBuf(slice::from_raw_parts(begin as *const u8, end as usize - begin as usize))
}
}
impl Device for MemBuf {
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
let slice = self.0;
let len = buf.len().min(slice.len() - offset);
buf[..len].copy_from_slice(&slice[offset..offset + len]);
Some(len)
}
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
None
}
}
2018-05-23 20:25:27 +04:00
use core::slice;
2018-05-23 20:25:27 +04:00
impl BlockedDevice for &'static ide::DISK0 {
fn block_size_log2(&self) -> u8 {
debug_assert_eq!(ide::BLOCK_SIZE, 512);
9
}
fn read_at(&mut self, block_id: usize, buf: &mut [u8]) -> bool {
assert!(buf.len() >= ide::BLOCK_SIZE);
let buf = unsafe { slice::from_raw_parts_mut(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
self.0.lock().read(block_id as u64, 1, buf).is_ok()
}
fn write_at(&mut self, block_id: usize, buf: &[u8]) -> bool {
assert!(buf.len() >= ide::BLOCK_SIZE);
let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
self.0.lock().write(block_id as u64, 1, buf).is_ok()
}
}