-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdfdd3a
commit 7305bff
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
pub use virtio::*; | ||
|
||
use alloc::sync::Arc; | ||
use core::ptr::NonNull; | ||
use ext4_rs::Ext4; | ||
use virtio_drivers::{device::blk::VirtIOBlk, transport::mmio::{MmioTransport, VirtIOHeader}}; | ||
use crate::{board::VIRT_IO, drivers::VirtIOHal}; | ||
|
||
mod virtio; | ||
|
||
// region Ext4Fs begin | ||
pub struct Ext4Fs { | ||
pub inner: Ext4, | ||
} | ||
|
||
impl Ext4Fs { | ||
pub fn new(device_id: usize) -> Self { | ||
let addr = VIRT_IO + device_id * 0x1000; | ||
let header = NonNull::new(addr as *mut VirtIOHeader).unwrap(); | ||
let transport = | ||
unsafe { MmioTransport::new(header).expect("Failed to create mmio transport") }; | ||
let blk = VirtIOBlk::<VirtIOHal, MmioTransport>::new(transport) | ||
.expect("Failed to create VirtIOBlk"); | ||
|
||
let device = VirtIODisk::new(blk); | ||
let ext4 = Ext4::open(Arc::new(device)); | ||
Self { inner: ext4 } | ||
} | ||
} | ||
// region Ext4Fs end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use core::cell::RefMut; | ||
use crate::{drivers::VirtIOHal, sync::UPSafeCell}; | ||
use alloc::vec; | ||
use ext4_rs::BlockDevice; | ||
use virtio_drivers::{device::blk::VirtIOBlk, transport::mmio::MmioTransport}; | ||
|
||
const BLOCK_SIZE: usize = 512; | ||
|
||
// region VirtIODisk begin | ||
pub struct VirtIODisk { | ||
inner: UPSafeCell<VirtIODiskInner>, | ||
} | ||
|
||
impl VirtIODisk { | ||
pub fn new(virt_io_blk: VirtIODiskInner) -> Self { | ||
VirtIODisk { | ||
inner: unsafe { UPSafeCell::new(virt_io_blk) }, | ||
} | ||
} | ||
|
||
fn inner_mut(&self) -> RefMut<VirtIODiskInner> { | ||
self.inner.exclusive_access() | ||
} | ||
} | ||
|
||
impl BlockDevice for VirtIODisk { | ||
fn read_offset(&self, offset: usize) -> alloc::vec::Vec<u8> { | ||
let mut inner = self.inner_mut(); | ||
let mut buf = vec![0; BLOCK_SIZE]; | ||
inner | ||
.read_blocks(offset, &mut buf) | ||
.expect("Error occurred when reading VirtIOBlk"); | ||
buf | ||
} | ||
|
||
fn write_offset(&self, offset: usize, data: &[u8]) { | ||
let mut inner = self.inner_mut(); | ||
inner | ||
.write_blocks(offset, &data) | ||
.expect("Error occurred when writing VirtIOBlk"); | ||
} | ||
} | ||
// region VirtIODisk end | ||
|
||
type VirtIODiskInner = VirtIOBlk<VirtIOHal, MmioTransport>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters