1use crate::readers::{Reader, ReaderError};
36use std::collections::HashMap;
37use std::mem::size_of;
38use std::{fmt, str};
39use thiserror::Error;
40use zerocopy::byteorder::little_endian::{U16 as LEU16, U32 as LEU32, U64 as LEU64};
41use zerocopy::{FromBytes, Immutable, KnownLayout, Ref, SplitByteSlice, Unaligned};
42
43pub const FIRST_BG_PADDING: u64 = 1024;
45pub const ROOT_INODE_NUM: u32 = 2;
47pub const SB_MAGIC: u16 = 0xEF53;
49pub const EH_MAGIC: u16 = 0xF30A;
51pub const MIN_EXT4_SIZE: u64 = FIRST_BG_PADDING + size_of::<SuperBlock>() as u64;
53pub const MINIMUM_INODE_SIZE: u64 = 128;
55
56#[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
57#[repr(C)]
58pub struct ExtentHeader {
59 pub eh_magic: LEU16,
61 pub eh_ecount: LEU16,
63 pub eh_max: LEU16,
65 pub eh_depth: LEU16,
68 pub eh_gen: LEU32,
70}
71assert_eq_size!(ExtentHeader, [u8; 12]);
74
75#[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
76#[repr(C)]
77pub struct ExtentIndex {
78 pub ei_blk: LEU32,
80 pub ei_leaf_lo: LEU32,
82 pub ei_leaf_hi: LEU16,
84 pub ei_unused: LEU16,
85}
86assert_eq_size!(ExtentIndex, [u8; 12]);
89
90#[derive(Clone, KnownLayout, FromBytes, Immutable, Unaligned)]
91#[repr(C)]
92pub struct Extent {
93 pub e_blk: LEU32,
95 pub e_len: LEU16,
97 pub e_start_hi: LEU16,
99 pub e_start_lo: LEU32,
101}
102assert_eq_size!(Extent, [u8; 12]);
105
106#[derive(std::fmt::Debug)]
107#[repr(C)]
108pub struct DirEntry2 {
109 pub e2d_ino: LEU32,
111 pub e2d_reclen: LEU16,
113 pub e2d_namlen: u8,
115 pub e2d_type: u8,
117 pub e2d_name: [u8; 255],
119}
120
121#[derive(KnownLayout, FromBytes, Immutable, Unaligned, std::fmt::Debug)]
122#[repr(C)]
123pub struct DirEntryHeader {
124 pub e2d_ino: LEU32,
126 pub e2d_reclen: LEU16,
128 pub e2d_namlen: u8,
130 pub e2d_type: u8,
132}
133assert_eq_size!(DirEntryHeader, [u8; 8]);
136
137#[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
138#[repr(C)]
139pub struct SuperBlock {
140 pub e2fs_icount: LEU32,
142 pub e2fs_bcount: LEU32,
144 pub e2fs_rbcount: LEU32,
146 pub e2fs_fbcount: LEU32,
148 pub e2fs_ficount: LEU32,
150 pub e2fs_first_dblock: LEU32,
152 pub e2fs_log_bsize: LEU32,
154 pub e2fs_log_fsize: LEU32,
156 pub e2fs_bpg: LEU32,
158 pub e2fs_fpg: LEU32,
160 pub e2fs_ipg: LEU32,
162 pub e2fs_mtime: LEU32,
164 pub e2fs_wtime: LEU32,
166 pub e2fs_mnt_count: LEU16,
168 pub e2fs_max_mnt_count: LEU16,
170 pub e2fs_magic: LEU16,
172 pub e2fs_state: LEU16,
174 pub e2fs_beh: LEU16,
176 pub e2fs_minrev: LEU16,
178 pub e2fs_lastfsck: LEU32,
180 pub e2fs_fsckintv: LEU32,
182 pub e2fs_creator: LEU32,
184 pub e2fs_rev: LEU32,
186 pub e2fs_ruid: LEU16,
188 pub e2fs_rgid: LEU16,
190 pub e2fs_first_ino: LEU32,
192 pub e2fs_inode_size: LEU16,
194 pub e2fs_block_group_nr: LEU16,
196 pub e2fs_features_compat: LEU32,
198 pub e2fs_features_incompat: LEU32,
200 pub e2fs_features_rocompat: LEU32,
202 pub e2fs_uuid: [u8; 16],
204 pub e2fs_vname: [u8; 16],
206 pub e2fs_fsmnt: [u8; 64],
208 pub e2fs_algo: LEU32,
210 pub e2fs_prealloc: u8,
212 pub e2fs_dir_prealloc: u8,
214 pub e2fs_reserved_ngdb: LEU16,
216 pub e3fs_journal_uuid: [u8; 16],
218 pub e3fs_journal_inum: LEU32,
220 pub e3fs_journal_dev: LEU32,
222 pub e3fs_last_orphan: LEU32,
224 pub e3fs_hash_seed: [LEU32; 4],
226 pub e3fs_def_hash_version: u8,
228 pub e3fs_jnl_backup_type: u8,
230 pub e3fs_desc_size: LEU16,
232 pub e3fs_default_mount_opts: LEU32,
234 pub e3fs_first_meta_bg: LEU32,
236 pub e3fs_mkfs_time: LEU32,
238 pub e3fs_jnl_blks: [LEU32; 17],
240 pub e4fs_bcount_hi: LEU32,
242 pub e4fs_rbcount_hi: LEU32,
244 pub e4fs_fbcount_hi: LEU32,
246 pub e4fs_min_extra_isize: LEU16,
248 pub e4fs_want_extra_isize: LEU16,
250 pub e4fs_flags: LEU32,
252 pub e4fs_raid_stride: LEU16,
254 pub e4fs_mmpintv: LEU16,
256 pub e4fs_mmpblk: LEU64,
258 pub e4fs_raid_stripe_wid: LEU32,
260 pub e4fs_log_gpf: u8,
262 pub e4fs_chksum_type: u8,
264 pub e4fs_encrypt: u8,
266 pub e4fs_reserved_pad: u8,
267 pub e4fs_kbytes_written: LEU64,
269 pub e4fs_snapinum: LEU32,
271 pub e4fs_snapid: LEU32,
273 pub e4fs_snaprbcount: LEU64,
275 pub e4fs_snaplist: LEU32,
277 pub e4fs_errcount: LEU32,
279 pub e4fs_first_errtime: LEU32,
281 pub e4fs_first_errino: LEU32,
283 pub e4fs_first_errblk: LEU64,
285 pub e4fs_first_errfunc: [u8; 32],
287 pub e4fs_first_errline: LEU32,
289 pub e4fs_last_errtime: LEU32,
291 pub e4fs_last_errino: LEU32,
293 pub e4fs_last_errline: LEU32,
295 pub e4fs_last_errblk: LEU64,
297 pub e4fs_last_errfunc: [u8; 32],
299 pub e4fs_mount_opts: [u8; 64],
301 pub e4fs_usrquota_inum: LEU32,
303 pub e4fs_grpquota_inum: LEU32,
305 pub e4fs_overhead_clusters: LEU32,
307 pub e4fs_backup_bgs: [LEU32; 2],
309 pub e4fs_encrypt_algos: [u8; 4],
311 pub e4fs_encrypt_pw_salt: [u8; 16],
313 pub e4fs_lpf_ino: LEU32,
315 pub e4fs_proj_quota_inum: LEU32,
317 pub e4fs_chksum_seed: LEU32,
319 pub e4fs_reserved: [LEU32; 98],
321 pub e4fs_sbchksum: LEU32,
323}
324assert_eq_size!(SuperBlock, [u8; 1024]);
327
328#[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
329#[repr(C)]
330pub struct BlockGroupDesc32 {
331 pub ext2bgd_b_bitmap: LEU32,
333 pub ext2bgd_i_bitmap: LEU32,
335 pub ext2bgd_i_tables: LEU32,
337 pub ext2bgd_nbfree: LEU16,
339 pub ext2bgd_nifree: LEU16,
341 pub ext2bgd_ndirs: LEU16,
343 pub ext4bgd_flags: LEU16,
345 pub ext4bgd_x_bitmap: LEU32,
347 pub ext4bgd_b_bmap_csum: LEU16,
349 pub ext4bgd_i_bmap_csum: LEU16,
351 pub ext4bgd_i_unused: LEU16,
353 pub ext4bgd_csum: LEU16,
355}
356assert_eq_size!(BlockGroupDesc32, [u8; 32]);
359
360#[derive(KnownLayout, FromBytes, Immutable)]
383#[repr(C)]
384pub struct ExtentTreeNode<B: SplitByteSlice> {
385 pub header: Ref<B, ExtentHeader>,
386 pub entries: B,
387}
388
389#[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
390#[repr(C)]
391pub struct INode {
392 pub e2di_mode: LEU16,
394 pub e2di_uid: LEU16,
396 pub e2di_size: LEU32,
398 pub e2di_atime: LEU32,
400 pub e2di_ctime: LEU32,
402 pub e2di_mtime: LEU32,
404 pub e2di_dtime: LEU32,
406 pub e2di_gid: LEU16,
408 pub e2di_nlink: LEU16,
410 pub e2di_nblock: LEU32,
412 pub e2di_flags: LEU32,
414 pub e2di_version: [u8; 4],
416 pub e2di_blocks: [u8; 60],
418 pub e2di_gen: LEU32,
420 pub e2di_facl: LEU32,
422 pub e2di_size_high: LEU32,
424 pub e2di_faddr: LEU32,
426 pub e2di_nblock_high: LEU16,
428 pub e2di_facl_high: LEU16,
430 pub e2di_uid_high: LEU16,
432 pub e2di_gid_high: LEU16,
434 pub e2di_chksum_lo: LEU16,
436 pub e2di_lx_reserved: LEU16,
437 e4di_extra_isize: LEU16,
441 e4di_chksum_hi: LEU16,
442 e4di_ctime_extra: LEU32,
443 e4di_mtime_extra: LEU32,
444 e4di_atime_extra: LEU32,
445 e4di_crtime: LEU32,
446 e4di_crtime_extra: LEU32,
447 e4di_version_hi: LEU32,
448 e4di_projid: LEU32,
449}
450assert_eq_size!(INode, [u8; 160]);
453
454#[derive(KnownLayout, FromBytes, Immutable, Unaligned, Debug)]
455#[repr(C)]
456pub struct XattrHeader {
457 pub e_magic: LEU32,
458 pub e_refcount: LEU32,
459 pub e_blocks: LEU32,
460 pub e_hash: LEU32,
461 pub e_checksum: LEU32,
462 e_reserved: [u8; 8],
463}
464
465#[derive(KnownLayout, FromBytes, Immutable, Unaligned, Debug)]
466#[repr(C)]
467pub struct XattrEntryHeader {
468 pub e_name_len: u8,
469 pub e_name_index: u8,
470 pub e_value_offs: LEU16,
471 pub e_value_inum: LEU32,
472 pub e_value_size: LEU32,
473 pub e_hash: LEU32,
474}
475
476#[derive(Debug, PartialEq)]
477pub enum InvalidAddressErrorType {
478 Lower,
479 Upper,
480}
481
482impl fmt::Display for InvalidAddressErrorType {
483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484 match *self {
485 InvalidAddressErrorType::Lower => write!(f, "lower"),
486 InvalidAddressErrorType::Upper => write!(f, "upper"),
487 }
488 }
489}
490
491#[derive(Error, Debug, PartialEq)]
492pub enum ParsingError {
493 #[error("Unable to parse Super Block at 0x{:X}", _0)]
494 InvalidSuperBlock(u64),
495 #[error("Invalid Super Block magic number {} should be 0xEF53", _0)]
496 InvalidSuperBlockMagic(u16),
497 #[error("Invalid Super Block inode size {} should be {}", _0, std::mem::size_of::<INode>())]
498 InvalidInodeSize(u16),
499 #[error("Block number {} out of bounds.", _0)]
500 BlockNumberOutOfBounds(u64),
501 #[error("SuperBlock e2fs_log_bsize value invalid: {}", _0)]
502 BlockSizeInvalid(u32),
503
504 #[error("Unable to parse Block Group Description at 0x{:X}", _0)]
505 InvalidBlockGroupDesc(u64),
506 #[error("Unable to parse INode {}", _0)]
507 InvalidInode(u32),
508
509 #[error("Unable to parse ExtentHeader from INode")]
511 InvalidExtentHeader,
512 #[error("Invalid Extent Header magic number {} should be 0xF30A", _0)]
513 InvalidExtentHeaderMagic(u16),
514 #[error("Unable to parse Extent at 0x{:X}", _0)]
515 InvalidExtent(u64),
516 #[error("Extent has more data {} than expected {}", _0, _1)]
517 ExtentUnexpectedLength(u64, u64),
518
519 #[error("Invalid Directory Entry at 0x{:X}", _0)]
520 InvalidDirEntry2(u64),
521 #[error("Directory Entry has invalid string in name field: {:?}", _0)]
522 DirEntry2NonUtf8(Vec<u8>),
523 #[error("Requested path contains invalid string")]
524 InvalidInputPath,
525 #[error("Non-existent path: {}", _0)]
526 PathNotFound(String),
527 #[error("Entry Type {} unknown", _0)]
528 BadEntryType(u8),
529
530 #[error("Incompatible feature flags (feature_incompat): 0x{:X}", _0)]
532 BannedFeatureIncompat(u32),
533 #[error("Required feature flags (feature_incompat): 0x{:X}", _0)]
534 RequiredFeatureIncompat(u32),
535
536 #[error("{}", _0)]
538 Incompatible(String),
539 #[error("Bad file at {}", _0)]
540 BadFile(String),
541 #[error("Bad directory at {}", _0)]
542 BadDirectory(String),
543
544 #[error("Attempted to access at 0x{:X} when the {} bound is 0x{:X}", _1, _0, _2)]
545 InvalidAddress(InvalidAddressErrorType, u64, u64),
546
547 #[error("Reader failed to read at 0x{:X}", _0)]
548 SourceReadError(u64),
549
550 #[error("Not a file")]
551 NotFile,
552}
553
554impl From<ReaderError> for ParsingError {
555 fn from(err: ReaderError) -> ParsingError {
556 match err {
557 ReaderError::Read(addr) => ParsingError::SourceReadError(addr),
558 ReaderError::OutOfBounds(addr, max) => {
559 ParsingError::InvalidAddress(InvalidAddressErrorType::Upper, addr, max)
560 }
561 }
562 }
563}
564
565#[derive(Debug, PartialEq)]
567#[repr(u8)]
568pub enum EntryType {
569 Unknown = 0x0,
570 RegularFile = 0x1,
571 Directory = 0x2,
572 CharacterDevice = 0x3,
573 BlockDevice = 0x4,
574 FIFO = 0x5,
575 Socket = 0x6,
576 SymLink = 0x7,
577}
578
579impl EntryType {
580 pub fn from_u8(value: u8) -> Result<EntryType, ParsingError> {
581 match value {
582 0x0 => Ok(EntryType::Unknown),
583 0x1 => Ok(EntryType::RegularFile),
584 0x2 => Ok(EntryType::Directory),
585 0x3 => Ok(EntryType::CharacterDevice),
586 0x4 => Ok(EntryType::BlockDevice),
587 0x5 => Ok(EntryType::FIFO),
588 0x6 => Ok(EntryType::Socket),
589 0x7 => Ok(EntryType::SymLink),
590 _ => Err(ParsingError::BadEntryType(value)),
591 }
592 }
593}
594
595#[derive(PartialEq)]
605#[repr(u32)]
606pub enum FeatureIncompat {
607 Compression = 0x1,
608 EntryHasFileType = 0x2,
610
611 HasJournal = 0x4,
618 JournalSeparate = 0x8,
619 MetaBlockGroups = 0x10,
620 Extents = 0x40,
623 Is64Bit = 0x80,
624 MultiMountProtection = 0x100,
625
626 FlexibleBlockGroups = 0x200,
629 ExtendedAttributeINodes = 0x400,
630 ExtendedDirectoryEntry = 0x1000,
631 MetadataChecksum = 0x2000,
633 LargeDirectory = 0x4000,
634 SmallFilesInINode = 0x8000,
635 EncryptedINodes = 0x10000,
636}
637
638pub const REQUIRED_FEATURE_INCOMPAT: u32 =
640 FeatureIncompat::Extents as u32 | FeatureIncompat::EntryHasFileType as u32;
641
642pub const BANNED_FEATURE_INCOMPAT: u32 = FeatureIncompat::Compression as u32 |
644 FeatureIncompat::Is64Bit as u32 |
646 FeatureIncompat::MultiMountProtection as u32 |
647 FeatureIncompat::ExtendedAttributeINodes as u32 |
648 FeatureIncompat::ExtendedDirectoryEntry as u32 |
649 FeatureIncompat::SmallFilesInINode as u32 |
650 FeatureIncompat::EncryptedINodes as u32;
651
652pub trait ParseToStruct: FromBytes + KnownLayout + Immutable + Unaligned + Sized {
655 fn from_reader_with_offset(reader: &dyn Reader, offset: u64) -> Result<Self, ParsingError> {
656 if offset < FIRST_BG_PADDING {
657 return Err(ParsingError::InvalidAddress(
658 InvalidAddressErrorType::Lower,
659 offset,
660 FIRST_BG_PADDING,
661 ));
662 }
663 let mut object = Self::new_zeroed();
664 let buffer = unsafe {
669 std::slice::from_raw_parts_mut(&mut object as *mut Self as *mut u8, size_of::<Self>())
670 };
671 reader.read(offset, buffer)?;
672 Ok(object)
673 }
674
675 fn to_struct_ref(data: &[u8], error_type: ParsingError) -> Result<&Self, ParsingError> {
679 Ref::<&[u8], Self>::from_bytes(data).map(|res| Ref::into_ref(res)).map_err(|_| error_type)
680 }
681}
682
683impl<T: FromBytes + KnownLayout + Immutable + Unaligned> ParseToStruct for T {}
685
686impl<B: SplitByteSlice> ExtentTreeNode<B> {
687 pub fn parse(data: B) -> Option<Self> {
692 Ref::<B, ExtentHeader>::from_prefix(data)
693 .ok()
694 .map(|(header, entries)| Self { header, entries })
695 }
696}
697
698impl SuperBlock {
699 pub fn parse(reader: &dyn Reader) -> Result<SuperBlock, ParsingError> {
701 let sb = SuperBlock::from_reader_with_offset(reader, FIRST_BG_PADDING)?;
705 sb.check_magic()?;
706 sb.feature_check()?;
707 sb.check_inode_size()?;
708 Ok(sb)
709 }
710
711 pub fn check_magic(&self) -> Result<(), ParsingError> {
712 if self.e2fs_magic.get() == SB_MAGIC {
713 Ok(())
714 } else {
715 Err(ParsingError::InvalidSuperBlockMagic(self.e2fs_magic.get()))
716 }
717 }
718
719 pub fn check_inode_size(&self) -> Result<(), ParsingError> {
720 let inode_size: u64 = self.e2fs_inode_size.into();
721 if inode_size < MINIMUM_INODE_SIZE {
722 Err(ParsingError::InvalidInodeSize(self.e2fs_inode_size.get()))
723 } else {
724 Ok(())
725 }
726 }
727
728 pub fn block_size(&self) -> Result<u64, ParsingError> {
733 let bs = 2u64
734 .checked_pow(self.e2fs_log_bsize.get() + 10)
735 .ok_or_else(|| ParsingError::BlockSizeInvalid(self.e2fs_log_bsize.get()))?;
736 if bs == 1024 || bs == 2048 || bs == 4096 || bs == 65536 {
737 Ok(bs)
738 } else {
739 Err(ParsingError::BlockSizeInvalid(self.e2fs_log_bsize.get()))
740 }
741 }
742
743 fn feature_check(&self) -> Result<(), ParsingError> {
744 let banned = self.e2fs_features_incompat.get() & BANNED_FEATURE_INCOMPAT;
745 if banned > 0 {
746 return Err(ParsingError::BannedFeatureIncompat(banned));
747 }
748 let required = self.e2fs_features_incompat.get() & REQUIRED_FEATURE_INCOMPAT;
749 if required != REQUIRED_FEATURE_INCOMPAT {
750 return Err(ParsingError::RequiredFeatureIncompat(
751 required ^ REQUIRED_FEATURE_INCOMPAT,
752 ));
753 }
754 Ok(())
755 }
756}
757
758impl INode {
759 pub fn extent_tree_node(&self) -> Result<ExtentTreeNode<&[u8]>, ParsingError> {
762 let eh = ExtentTreeNode::<&[u8]>::parse(
763 &self.e2di_blocks,
766 )
767 .ok_or(ParsingError::InvalidExtentHeader)?;
768 eh.header.check_magic()?;
769 Ok(eh)
770 }
771
772 pub fn size(&self) -> u64 {
774 (self.e2di_size_high.get() as u64) << 32 | self.e2di_size.get() as u64
775 }
776
777 pub fn facl(&self) -> u64 {
778 (self.e2di_facl_high.get() as u64) << 32 | self.e2di_facl.get() as u64
779 }
780
781 fn check_inode_size(sb: &SuperBlock) -> Result<(), ParsingError> {
782 let inode_size: usize = sb.e2fs_inode_size.into();
783 if inode_size < std::mem::size_of::<INode>() {
784 Err(ParsingError::Incompatible("Inode size too small.".to_string()))
785 } else {
786 Ok(())
787 }
788 }
789
790 pub fn e4di_extra_isize(&self, sb: &SuperBlock) -> Result<LEU16, ParsingError> {
791 INode::check_inode_size(sb)?;
792 Ok(self.e4di_extra_isize)
793 }
794
795 pub fn e4di_chksum_hi(&self, sb: &SuperBlock) -> Result<LEU16, ParsingError> {
796 INode::check_inode_size(sb)?;
797 Ok(self.e4di_chksum_hi)
798 }
799
800 pub fn e4di_ctime_extra(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
801 INode::check_inode_size(sb)?;
802 Ok(self.e4di_ctime_extra)
803 }
804
805 pub fn e4di_mtime_extra(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
806 INode::check_inode_size(sb)?;
807 Ok(self.e4di_mtime_extra)
808 }
809
810 pub fn e4di_atime_extra(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
811 INode::check_inode_size(sb)?;
812 Ok(self.e4di_atime_extra)
813 }
814
815 pub fn e4di_crtime(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
816 INode::check_inode_size(sb)?;
817 Ok(self.e4di_crtime)
818 }
819
820 pub fn e4di_crtime_extra(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
821 INode::check_inode_size(sb)?;
822 Ok(self.e4di_crtime_extra)
823 }
824
825 pub fn e4di_version_hi(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
826 INode::check_inode_size(sb)?;
827 Ok(self.e4di_version_hi)
828 }
829
830 pub fn e4di_projid(&self, sb: &SuperBlock) -> Result<LEU32, ParsingError> {
831 INode::check_inode_size(sb)?;
832 Ok(self.e4di_projid)
833 }
834}
835
836impl ExtentHeader {
837 pub fn check_magic(&self) -> Result<(), ParsingError> {
838 if self.eh_magic.get() == EH_MAGIC {
839 Ok(())
840 } else {
841 Err(ParsingError::InvalidExtentHeaderMagic(self.eh_magic.get()))
842 }
843 }
844}
845
846impl DirEntry2 {
847 pub fn name(&self) -> Result<&str, ParsingError> {
849 str::from_utf8(&self.e2d_name[0..self.e2d_namlen as usize]).map_err(|_| {
850 ParsingError::DirEntry2NonUtf8(self.e2d_name[0..self.e2d_namlen as usize].to_vec())
851 })
852 }
853
854 pub fn name_bytes(&self) -> &[u8] {
855 &self.e2d_name[0..self.e2d_namlen as usize]
856 }
857
858 pub fn as_hash_map(
863 entries: Vec<DirEntry2>,
864 ) -> Result<HashMap<String, DirEntry2>, ParsingError> {
865 let mut entry_map: HashMap<String, DirEntry2> = HashMap::with_capacity(entries.len());
866
867 for entry in entries {
868 entry_map.insert(entry.name()?.to_string(), entry);
869 }
870 Ok(entry_map)
871 }
872}
873
874impl Extent {
875 pub fn target_block_num(&self) -> u64 {
877 (self.e_start_hi.get() as u64) << 32 | self.e_start_lo.get() as u64
878 }
879}
880
881impl ExtentIndex {
882 pub fn target_block_num(&self) -> u64 {
884 (self.ei_leaf_hi.get() as u64) << 32 | self.ei_leaf_lo.get() as u64
885 }
886}
887
888#[cfg(test)]
889mod test {
890 use super::{
891 Extent, ExtentHeader, ExtentIndex, FeatureIncompat, ParseToStruct, SuperBlock, EH_MAGIC,
892 FIRST_BG_PADDING, LEU16, LEU32, LEU64, REQUIRED_FEATURE_INCOMPAT, SB_MAGIC,
893 };
894 use crate::readers::VecReader;
895 use std::fs;
896
897 impl Default for SuperBlock {
898 fn default() -> SuperBlock {
899 SuperBlock {
900 e2fs_icount: LEU32::new(0),
901 e2fs_bcount: LEU32::new(0),
902 e2fs_rbcount: LEU32::new(0),
903 e2fs_fbcount: LEU32::new(0),
904 e2fs_ficount: LEU32::new(0),
905 e2fs_first_dblock: LEU32::new(0),
906 e2fs_log_bsize: LEU32::new(0),
907 e2fs_log_fsize: LEU32::new(0),
908 e2fs_bpg: LEU32::new(0),
909 e2fs_fpg: LEU32::new(0),
910 e2fs_ipg: LEU32::new(0),
911 e2fs_mtime: LEU32::new(0),
912 e2fs_wtime: LEU32::new(0),
913 e2fs_mnt_count: LEU16::new(0),
914 e2fs_max_mnt_count: LEU16::new(0),
915 e2fs_magic: LEU16::new(0),
916 e2fs_state: LEU16::new(0),
917 e2fs_beh: LEU16::new(0),
918 e2fs_minrev: LEU16::new(0),
919 e2fs_lastfsck: LEU32::new(0),
920 e2fs_fsckintv: LEU32::new(0),
921 e2fs_creator: LEU32::new(0),
922 e2fs_rev: LEU32::new(0),
923 e2fs_ruid: LEU16::new(0),
924 e2fs_rgid: LEU16::new(0),
925 e2fs_first_ino: LEU32::new(0),
926 e2fs_inode_size: LEU16::new(0),
927 e2fs_block_group_nr: LEU16::new(0),
928 e2fs_features_compat: LEU32::new(0),
929 e2fs_features_incompat: LEU32::new(0),
930 e2fs_features_rocompat: LEU32::new(0),
931 e2fs_uuid: [0; 16],
932 e2fs_vname: [0; 16],
933 e2fs_fsmnt: [0; 64],
934 e2fs_algo: LEU32::new(0),
935 e2fs_prealloc: 0,
936 e2fs_dir_prealloc: 0,
937 e2fs_reserved_ngdb: LEU16::new(0),
938 e3fs_journal_uuid: [0; 16],
939 e3fs_journal_inum: LEU32::new(0),
940 e3fs_journal_dev: LEU32::new(0),
941 e3fs_last_orphan: LEU32::new(0),
942 e3fs_hash_seed: [LEU32::new(0); 4],
943 e3fs_def_hash_version: 0,
944 e3fs_jnl_backup_type: 0,
945 e3fs_desc_size: LEU16::new(0),
946 e3fs_default_mount_opts: LEU32::new(0),
947 e3fs_first_meta_bg: LEU32::new(0),
948 e3fs_mkfs_time: LEU32::new(0),
949 e3fs_jnl_blks: [LEU32::new(0); 17],
950 e4fs_bcount_hi: LEU32::new(0),
951 e4fs_rbcount_hi: LEU32::new(0),
952 e4fs_fbcount_hi: LEU32::new(0),
953 e4fs_min_extra_isize: LEU16::new(0),
954 e4fs_want_extra_isize: LEU16::new(0),
955 e4fs_flags: LEU32::new(0),
956 e4fs_raid_stride: LEU16::new(0),
957 e4fs_mmpintv: LEU16::new(0),
958 e4fs_mmpblk: LEU64::new(0),
959 e4fs_raid_stripe_wid: LEU32::new(0),
960 e4fs_log_gpf: 0,
961 e4fs_chksum_type: 0,
962 e4fs_encrypt: 0,
963 e4fs_reserved_pad: 0,
964 e4fs_kbytes_written: LEU64::new(0),
965 e4fs_snapinum: LEU32::new(0),
966 e4fs_snapid: LEU32::new(0),
967 e4fs_snaprbcount: LEU64::new(0),
968 e4fs_snaplist: LEU32::new(0),
969 e4fs_errcount: LEU32::new(0),
970 e4fs_first_errtime: LEU32::new(0),
971 e4fs_first_errino: LEU32::new(0),
972 e4fs_first_errblk: LEU64::new(0),
973 e4fs_first_errfunc: [0; 32],
974 e4fs_first_errline: LEU32::new(0),
975 e4fs_last_errtime: LEU32::new(0),
976 e4fs_last_errino: LEU32::new(0),
977 e4fs_last_errline: LEU32::new(0),
978 e4fs_last_errblk: LEU64::new(0),
979 e4fs_last_errfunc: [0; 32],
980 e4fs_mount_opts: [0; 64],
981 e4fs_usrquota_inum: LEU32::new(0),
982 e4fs_grpquota_inum: LEU32::new(0),
983 e4fs_overhead_clusters: LEU32::new(0),
984 e4fs_backup_bgs: [LEU32::new(0); 2],
985 e4fs_encrypt_algos: [0; 4],
986 e4fs_encrypt_pw_salt: [0; 16],
987 e4fs_lpf_ino: LEU32::new(0),
988 e4fs_proj_quota_inum: LEU32::new(0),
989 e4fs_chksum_seed: LEU32::new(0),
990 e4fs_reserved: [LEU32::new(0); 98],
991 e4fs_sbchksum: LEU32::new(0),
992 }
993 }
994 }
995
996 #[fuchsia::test]
1005 fn parse_superblock() {
1006 let data = fs::read("/pkg/data/1file.img").expect("Unable to read file");
1007 let reader = VecReader::new(data);
1008 let sb = SuperBlock::parse(&reader).expect("Parsed Super Block");
1009 assert_eq!(sb.block_size().unwrap(), FIRST_BG_PADDING);
1011
1012 assert!(sb.check_magic().is_ok());
1014
1015 let mut sb = SuperBlock::default();
1016 assert!(sb.check_magic().is_err());
1017
1018 sb.e2fs_magic = LEU16::new(SB_MAGIC);
1019 assert!(sb.check_magic().is_ok());
1020
1021 sb.e2fs_log_bsize = LEU32::new(0); assert!(sb.block_size().is_ok());
1024 sb.e2fs_log_bsize = LEU32::new(1); assert!(sb.block_size().is_ok());
1026 sb.e2fs_log_bsize = LEU32::new(2); assert!(sb.block_size().is_ok());
1028 sb.e2fs_log_bsize = LEU32::new(6); assert!(sb.block_size().is_ok());
1030
1031 sb.e2fs_log_bsize = LEU32::new(3);
1033 assert!(sb.block_size().is_err());
1034 sb.e2fs_log_bsize = LEU32::new(5);
1035 assert!(sb.block_size().is_err());
1036 sb.e2fs_log_bsize = LEU32::new(7);
1037 assert!(sb.block_size().is_err());
1038 sb.e2fs_log_bsize = LEU32::new(20);
1040 assert!(sb.block_size().is_err());
1041 }
1042
1043 #[fuchsia::test]
1045 fn parse_to_struct_from_reader_with_offset() {
1046 let data = fs::read("/pkg/data/1file.img").expect("Unable to read file");
1047 let reader = VecReader::new(data);
1048 let sb = SuperBlock::from_reader_with_offset(&reader, FIRST_BG_PADDING)
1049 .expect("Parsed Super Block");
1050 assert!(sb.check_magic().is_ok());
1051 }
1052
1053 #[fuchsia::test]
1055 fn incompatible_feature_flags() {
1056 let data = fs::read("/pkg/data/1file.img").expect("Unable to read file");
1057 let reader = VecReader::new(data);
1058 let sb = SuperBlock::parse(&reader).expect("Parsed Super Block");
1059 assert_eq!(sb.e2fs_magic.get(), SB_MAGIC);
1060 assert!(sb.feature_check().is_ok());
1061
1062 let mut sb = SuperBlock::default();
1063 match sb.feature_check() {
1064 Ok(_) => assert!(false, "Feature flags should be incorrect."),
1065 Err(e) => assert_eq!(
1066 format!("{}", e),
1067 format!(
1068 "Required feature flags (feature_incompat): 0x{:X}",
1069 REQUIRED_FEATURE_INCOMPAT
1070 )
1071 ),
1072 }
1073
1074 sb.e2fs_features_incompat = LEU32::new(REQUIRED_FEATURE_INCOMPAT | 0xF00000);
1076 assert!(sb.feature_check().is_ok());
1077
1078 sb.e2fs_features_incompat = LEU32::new(FeatureIncompat::Extents as u32);
1080 match sb.feature_check() {
1081 Ok(_) => assert!(false, "Feature flags should be incorrect."),
1082 Err(e) => assert_eq!(
1083 format!("{}", e),
1084 format!(
1085 "Required feature flags (feature_incompat): 0x{:X}",
1086 REQUIRED_FEATURE_INCOMPAT ^ FeatureIncompat::Extents as u32
1087 )
1088 ),
1089 }
1090
1091 sb.e2fs_features_incompat = LEU32::new(FeatureIncompat::Is64Bit as u32);
1093 match sb.feature_check() {
1094 Ok(_) => assert!(false, "Feature flags should be incorrect."),
1095 Err(e) => assert_eq!(
1096 format!("{}", e),
1097 format!(
1098 "Incompatible feature flags (feature_incompat): 0x{:X}",
1099 FeatureIncompat::Is64Bit as u32
1100 )
1101 ),
1102 }
1103 }
1104
1105 #[fuchsia::test]
1107 fn extent_target_block_num() {
1108 let e = Extent {
1109 e_blk: LEU32::new(0),
1110 e_len: LEU16::new(0),
1111 e_start_hi: LEU16::new(0x4444),
1112 e_start_lo: LEU32::new(0x6666_8888),
1113 };
1114 assert_eq!(e.target_block_num(), 0x4444_6666_8888);
1115 }
1116
1117 #[fuchsia::test]
1119 fn extent_index_target_block_num() {
1120 let e = ExtentIndex {
1121 ei_blk: LEU32::new(0),
1122 ei_leaf_lo: LEU32::new(0x6666_8888),
1123 ei_leaf_hi: LEU16::new(0x4444),
1124 ei_unused: LEU16::new(0),
1125 };
1126 assert_eq!(e.target_block_num(), 0x4444_6666_8888);
1127 }
1128
1129 #[fuchsia::test]
1131 fn extent_header_check_magic() {
1132 let e = ExtentHeader {
1133 eh_magic: LEU16::new(EH_MAGIC),
1134 eh_ecount: LEU16::new(0),
1135 eh_max: LEU16::new(0),
1136 eh_depth: LEU16::new(0),
1137 eh_gen: LEU32::new(0),
1138 };
1139 assert!(e.check_magic().is_ok());
1140
1141 let e = ExtentHeader {
1142 eh_magic: LEU16::new(0x1234),
1143 eh_ecount: LEU16::new(0),
1144 eh_max: LEU16::new(0),
1145 eh_depth: LEU16::new(0),
1146 eh_gen: LEU32::new(0),
1147 };
1148 assert!(e.check_magic().is_err());
1149 }
1150}