mbr_nostd/
partitions.rs

1
2/// The type of a particular partition.
3#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
4pub enum PartitionType {
5    Unused,
6    Unknown(u8),
7    Fat12(u8),
8    Fat16(u8),
9    Fat32(u8),
10    LinuxExt(u8),
11    HfsPlus(u8),
12    ISO9660(u8),
13    NtfsExfat(u8),
14}
15
16impl PartitionType {
17
18    /// Parses a partition type from the type byte in the MBR's table.
19    pub fn from_mbr_tag_byte(tag: u8) -> PartitionType {
20        match tag {
21            0x0 => PartitionType::Unused,
22            0x01 => PartitionType::Fat12(tag),
23            0x04 | 0x06 | 0x0e => PartitionType::Fat16(tag),
24            0x0b | 0x0c | 0x1b | 0x1c => PartitionType::Fat32(tag),
25            0x83 => PartitionType::LinuxExt(tag),
26            0x07 => PartitionType::NtfsExfat(tag),
27            0xaf => PartitionType::HfsPlus(tag),
28            _ => PartitionType::Unknown(tag),
29        }
30    }
31
32    /// Retrieves the associated type byte for this partition type.
33    pub fn to_mbr_tag_byte(&self) -> u8 {
34        match *self {
35            PartitionType::Unused => 0,
36            PartitionType::Unknown(t) => t,
37            PartitionType::Fat12(t) => t,
38            PartitionType::Fat16(t) => t,
39            PartitionType::Fat32(t) => t,
40            PartitionType::LinuxExt(t) => t,
41            PartitionType::HfsPlus(t) => t,
42            PartitionType::ISO9660(t) => t,
43            PartitionType::NtfsExfat(t) => t,
44        }
45    }
46}
47
48/// An entry in a partition table.
49#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
50pub struct PartitionTableEntry {
51
52    /// The type of partition in this entry.
53    pub partition_type: PartitionType,
54
55    /// The index of the first block of this entry.
56    pub logical_block_address: u32,
57    
58    /// The total number of blocks in this entry.
59    pub sector_count: u32,
60}
61
62impl PartitionTableEntry {
63    pub fn new(
64        partition_type: PartitionType,
65        logical_block_address: u32,
66        sector_count: u32,
67    ) -> PartitionTableEntry {
68        PartitionTableEntry {
69            partition_type,
70            logical_block_address,
71            sector_count,
72        }
73    }
74
75    pub fn empty() -> PartitionTableEntry {
76        PartitionTableEntry::new(PartitionType::Unused, 0, 0)
77    }
78}
79
80/// A general trait for partition table types. 
81pub trait PartitionTable {
82
83    /// The size of the partition table itself, in bytes. 
84    fn size(&self) -> usize;
85
86    /// The entries in this table.
87    fn partition_table_entries(&self) -> &[PartitionTableEntry];
88}