This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
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.
* refactor(error): begin use of anyhow
* feat(dev): begin of devices implementations
- Loading branch information
Showing
8 changed files
with
173 additions
and
50 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,3 @@ | ||
//! Everything related to the devices | ||
|
||
pub mod sector; |
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,124 @@ | ||
//! General description of sectors. | ||
|
||
use core::fmt::{self, Debug}; | ||
use core::marker::PhantomData; | ||
|
||
use anyhow::{anyhow, Error, Result}; | ||
|
||
/// General interface for sector sizes that are device-dependant. | ||
pub trait Size: Clone + Copy + PartialEq + Eq { | ||
/// Logarithm in base 2 of the sector size. | ||
const LOG_SIZE: u32; | ||
|
||
/// Size of a sector. | ||
const SIZE: u32 = 1 << Self::LOG_SIZE; | ||
|
||
/// Offset mask of the sector size. | ||
const OFFSET_MASK: u32 = Self::SIZE - 1; | ||
} | ||
|
||
/// Size sector of 512 bytes. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Size512; | ||
|
||
impl Size for Size512 { | ||
const LOG_SIZE: u32 = 9; | ||
} | ||
|
||
/// Size sector of 1024 bytes. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Size1024; | ||
|
||
impl Size for Size1024 { | ||
const LOG_SIZE: u32 = 10; | ||
} | ||
|
||
/// Size sector of 2048 bytes. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Size2048; | ||
|
||
impl Size for Size2048 { | ||
const LOG_SIZE: u32 = 11; | ||
} | ||
|
||
/// Size sector of 4096 bytes. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct Size4096; | ||
|
||
impl Size for Size4096 { | ||
const LOG_SIZE: u32 = 12; | ||
} | ||
|
||
/// Address of a physical sector | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Address<S: Size> { | ||
/// Sector in which the address is located. | ||
sector: u32, | ||
|
||
/// Offset of this address in the sector. | ||
offset: u32, | ||
|
||
/// Phantom data to store the sector size. | ||
_phantom: PhantomData<S>, | ||
} | ||
|
||
impl<S: Size> Debug for Address<S> { | ||
#[inline] | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
formatter | ||
.debug_struct("Address") | ||
.field("sector", &self.sector) | ||
.field("offset", &self.offset) | ||
.finish() | ||
} | ||
} | ||
|
||
impl<S: Size> Address<S> { | ||
/// Returns a new [`Address`] with an offset such that `0 <= offset < S::SIZE` | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an [`Error`](anyhow::Error) | ||
#[inline] | ||
pub fn new(sector: u32, offset: i32) -> Result<Self> { | ||
let real_sector = TryInto::<u32>::try_into(TryInto::<i32>::try_into(sector).map_err(Error::msg)? + (offset >> S::LOG_SIZE)) | ||
.map_err(Error::msg)?; | ||
let real_offset = offset.unsigned_abs() & S::OFFSET_MASK; | ||
if real_offset >= S::SIZE { | ||
Err(anyhow!("Offset Out of Bounds: the offset {real_offset} is greater than the sector size {}", S::SIZE)) | ||
} else { | ||
Ok(Self { | ||
sector: real_sector, | ||
offset: real_offset, | ||
_phantom: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
/// Returns the sector containing this address. | ||
#[inline] | ||
#[must_use] | ||
pub const fn sector(&self) -> u32 { | ||
self.sector | ||
} | ||
|
||
/// Returns the offset of this address in its sector. | ||
#[inline] | ||
#[must_use] | ||
pub const fn offset(&self) -> u32 { | ||
self.offset | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::{Size, Size1024, Size2048, Size4096, Size512}; | ||
|
||
#[test] | ||
fn sizes() { | ||
assert_eq!(Size512::SIZE, 512); | ||
assert_eq!(Size1024::SIZE, 1024); | ||
assert_eq!(Size2048::SIZE, 2048); | ||
assert_eq!(Size4096::SIZE, 4096); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
//! Errors related to Ext2 manipulation. | ||
|
||
use core::error; | ||
use core::fmt::{self, Display}; | ||
|
||
/// Enumeration of possible errors encountered with Ext2's manipulation. | ||
#[allow(clippy::module_name_repetitions)] | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Ext2Error { | ||
/// A bad magic number has been found during the superblock parsing. | ||
BadMagic(u16), | ||
} | ||
|
||
impl Display for Ext2Error { | ||
#[inline] | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::BadMagic(magic) => write!(formatter, "Bad Magic: {magic} has been found while TODO was expected"), | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for Ext2Error {} |
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,5 @@ | ||
//! Implementation of the Second Extended Filesystem (ext2fs) filesystem. | ||
//! | ||
//! See [its Wikipedia page](https://fr.wikipedia.org/wiki/Ext2) or [its OSDev page](https://wiki.osdev.org/Ext2) for more informations. | ||
|
||
pub mod error; |
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