add locating Superblock and [BlockGroupDescriptor]

This commit is contained in:
Szymon Walter 2018-03-18 18:38:54 +01:00
parent b211d95807
commit 096cc865a4
3 changed files with 60 additions and 0 deletions

View file

@ -11,6 +11,7 @@
/// Remember that blocks are numbered starting at 0, and that block numbers /// Remember that blocks are numbered starting at 0, and that block numbers
/// don't usually correspond to physical block addresses. /// don't usually correspond to physical block addresses.
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BlockGroupDescriptor { pub struct BlockGroupDescriptor {
/// Block address of block usage bitmap /// Block address of block usage bitmap
block_usage_addr: u32, block_usage_addr: u32,

View file

@ -1,4 +1,5 @@
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Inode { pub struct Inode {
/// Type and Permissions (see below) /// Type and Permissions (see below)
type_perm: u16, type_perm: u16,

View file

@ -1,3 +1,16 @@
#[cfg(test)]
use std::mem;
#[cfg(test)]
use std::slice;
#[cfg(not(test))]
use core::mem;
#[cfg(not(test))]
use core::slice;
use error::Error;
use block_group::BlockGroupDescriptor;
/// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a /// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a
/// volume /// volume
pub const EXT2_MAGIC: u16 = 0xef53; pub const EXT2_MAGIC: u16 = 0xef53;
@ -34,6 +47,7 @@ pub const OS_LITE: u32 = 4;
/// 512 byte sectors, the Superblock will begin at LBA 2 and will occupy all of /// 512 byte sectors, the Superblock will begin at LBA 2 and will occupy all of
/// sector 2 and 3. /// sector 2 and 3.
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Superblock { pub struct Superblock {
// taken from https://wiki.osdev.org/Ext2 // taken from https://wiki.osdev.org/Ext2
/// Total number of inodes in file system /// Total number of inodes in file system
@ -139,6 +153,50 @@ pub struct Superblock {
} }
impl Superblock { impl Superblock {
pub fn find<'a>(
haystack: &'a mut [u8],
) -> Result<&'a mut Superblock, Error> {
let offset = 1024;
let end = offset + mem::size_of::<Superblock>();
if haystack.len() < end {
return Err(Error::OutOfBounds(end));
}
let superblock: &mut Superblock = unsafe {
let ptr =
haystack.as_ptr().offset(offset as isize) as *mut Superblock;
ptr.as_mut().unwrap()
};
if superblock.magic != EXT2_MAGIC {
Err(Error::BadMagic(superblock.magic))
} else {
Ok(superblock)
}
}
pub fn find_block_table<'a>(
&self,
haystack: &'a mut [u8],
) -> Result<&'a mut [BlockGroupDescriptor], Error> {
let count = self.block_group_count()
.map_err(|(a, b)| Error::BadBlockGroupCount(a, b))?
as usize;
let offset = 2048;
let end = offset + count * mem::size_of::<BlockGroupDescriptor>();
if haystack.len() < end {
return Err(Error::OutOfBounds(end));
}
let ptr = unsafe {
haystack.as_ptr().offset(offset as isize)
as *mut BlockGroupDescriptor
};
let slice = unsafe { slice::from_raw_parts_mut(ptr, count) };
Ok(slice)
}
#[inline] #[inline]
pub fn block_size(&self) -> usize { pub fn block_size(&self) -> usize {
1024 << self.log_block_size 1024 << self.log_block_size