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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::proto::Protocol;
use crate::{unsafe_guid, Char16, Guid};
newtype_enum! {
pub enum MbrOsType: u8 => {
GPT_PROTECTIVE = 0xee,
UEFI_SYSTEM_PARTITION = 0xef,
}
}
#[repr(C)]
#[repr(packed)]
#[derive(Clone, Copy, Debug)]
pub struct MbrPartitionRecord {
pub boot_indicator: u8,
pub starting_chs: [u8; 3],
pub os_type: MbrOsType,
pub ending_chs: [u8; 3],
pub starting_lba: u32,
pub size_in_lba: u32,
}
impl MbrPartitionRecord {
pub fn is_bootable(&self) -> bool {
self.boot_indicator == 0x80
}
}
newtype_enum! {
pub enum GptPartitionType: Guid => {
UNUSED_ENTRY = Guid::from_values(
0x00000000,
0x0000,
0x0000,
0x0000,
0x000000000000,
),
EFI_SYSTEM_PARTITION = Guid::from_values(
0xc12a7328,
0xf81f,
0x11d2,
0xba4b,
0x00a0c93ec93b,
),
LEGACY_MBR = Guid::from_values(
0x024dee41,
0x33e7,
0x11d3,
0x9d69,
0x0008c781f39f,
),
}
}
#[repr(C)]
#[repr(packed)]
#[derive(Clone, Copy, Debug)]
pub struct GptPartitionEntry {
pub partition_type_guid: GptPartitionType,
pub unique_partition_guid: Guid,
pub starting_lba: u64,
pub ending_lba: u64,
pub attributes: u64,
pub partition_name: [Char16; 36],
}
impl GptPartitionEntry {
pub fn num_blocks(&self) -> Option<u64> {
self.ending_lba
.checked_sub(self.starting_lba)?
.checked_add(1)
}
}
newtype_enum! {
pub enum PartitionType: u32 => {
OTHER = 0x00,
MBR = 0x01,
GPT = 0x02,
}
}
#[repr(C)]
#[derive(Clone, Copy)]
union PartitionInfoRecord {
mbr: MbrPartitionRecord,
gpt: GptPartitionEntry,
}
newtype_enum! {
pub enum PartitionInfoRevision: u32 => {
PROTOCOL_REVISION = 0x0001000,
}
}
#[repr(C)]
#[repr(packed)]
#[unsafe_guid("8cf2f62c-bc9b-4821-808d-ec9ec421a1a0")]
#[derive(Clone, Copy, Protocol)]
pub struct PartitionInfo {
pub revision: PartitionInfoRevision,
pub partition_type: PartitionType,
system: u8,
reserved: [u8; 7],
record: PartitionInfoRecord,
}
impl PartitionInfo {
pub fn is_system(&self) -> bool {
self.system == 1
}
pub fn mbr_partition_record(&self) -> Option<&MbrPartitionRecord> {
if { self.revision } != PartitionInfoRevision::PROTOCOL_REVISION {
return None;
}
if { self.partition_type } == PartitionType::MBR {
Some(unsafe { &self.record.mbr })
} else {
None
}
}
pub fn gpt_partition_entry(&self) -> Option<&GptPartitionEntry> {
if { self.revision } != PartitionInfoRevision::PROTOCOL_REVISION {
return None;
}
if { self.partition_type } == PartitionType::GPT {
Some(unsafe { &self.record.gpt })
} else {
None
}
}
}