1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{GPTError, Result};
#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct MBRPartitionRecord {
pub boot_indicator: u8,
pub start_head: u8,
pub start_sector: u8,
pub start_track: u8,
pub os_indicator: u8,
pub end_head: u8,
pub end_sector: u8,
pub end_track: u8,
pub starting_lba: [u8; 4],
pub size_in_lba: [u8; 4],
}
impl MBRPartitionRecord {
/// Check if the partition is to be considered empty.
///
/// A partition is considered empty if the [`Self::os_indicator`] or the [`Self::size_in_lba`]
/// is 0.
pub fn is_empty(&self) -> bool {
self.os_indicator == 0 || self.size_in_lba() == 0
}
/// Return the starting logical block as u32.
pub fn starting_lba(&self) -> u32 {
u32::from_le_bytes(self.starting_lba)
}
/// Return the size in logical blocks as u32.
pub fn size_in_lba(&self) -> u32 {
u32::from_le_bytes(self.size_in_lba)
}
/// Helper to calculate [`Self::starting_lba`] + [`Self::size_in_lba`] to get the ending lba.
pub fn ending_lba(&self) -> u32 {
self.starting_lba() + self.size_in_lba()
}
/// Defines a UEFI system partition.
pub const UEFI_SYSTEM_OS_TYPE: u8 = 0xef;
/// Is used by a protective MBR to define a fake partition covering the entire disk.
pub const GPT_PROTECTIVE_OS_TYPE: u8 = 0xee;
}
#[derive(Clone, Debug)]
#[repr(C, packed)]
pub struct MasterBootRecord {
/// x86 code used on a non-UEFI system to select an MBR partition record and load the first
/// logical block of that partition . This code shall not be executed on UEFI systems.
pub bootstrapcode: [u8; 440],
/// Unique Disk Signature This may be used by the OS to identify the disk from other disks in
/// the system. This value is always written by the OS and is never written by EFI firmware.
pub unique_mbr_signature: [u8; 4],
/// Unknown. This field shall not be used by UEFI firmware.
pub unknown: [u8; 2],
/// Array of four legacy MBR partition records [`MBRPartitionRecord`].
pub partition: [MBRPartitionRecord; 4],
pub signature: [u8; 2],
}
impl MasterBootRecord {
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `buf` must be valid for 512 reads.
///
/// * `buf` must be properly aligned.
///
pub unsafe fn from_buf(buf: &[u8]) -> Result<Self> {
if buf.len() < core::mem::size_of::<Self>() {
return Err(GPTError::UnexpectedEOF);
}
let ret: MasterBootRecord = unsafe { core::ptr::read(buf.as_ptr() as _) };
Ok(ret)
}
/// Return the signature as u16
pub fn signature(&self) -> u16 {
u16::from_le_bytes(self.signature)
}
pub fn verify(&self, last_lba: Option<u32>) -> Result<()> {
if self.signature() != 0xaa55 {
return Err(GPTError::InvalidData);
}
self.verify_partitions(last_lba)?;
Ok(())
}
pub fn verify_partitions(&self, last_lba: Option<u32>) -> Result<()> {
// FIXME: 1..3 does not have to be empty, just to lazy right now
if !self.partition[1].is_empty()
|| !self.partition[2].is_empty()
|| !self.partition[3].is_empty()
{
return Err(GPTError::InvalidMbr);
}
/*(0..=2)
.fold([0u8, 1, 2, 3], |order, left| {
(left + 1..=3).fold(order, |mut order, right| {
let swap = |mut this: [u8; 4], left, right| {
this.swap(left, right);
this
};
if self.partition[left].starting_lba() > self.partition[right].starting_lba() {
swap(order, left, right)
} else {
order
}
})
})
.iter()
.take(3)
.try_for_each(|index| {
let index = *index as usize;
let left = self.partition[index];
let right = self.partition[index + 1];
if left.is_empty() || right.is_empty() || left.ending_lba() < right.starting_lba() {
Ok(())
} else {
Err(GptError::OverlappingPartitions)
}
});*/
let last_lba_p = self.partition[0].ending_lba();
if let Some(last_lba) = last_lba {
if last_lba_p > last_lba {
return Err(GPTError::InvalidMbr);
}
}
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::mbr::{MBRPartitionRecord, MasterBootRecord};
#[test]
fn size() {
assert_eq!(core::mem::size_of::<MBRPartitionRecord>(), 16);
assert_eq!(core::mem::size_of::<MasterBootRecord>(), 512)
}
}