interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / proto / media / partition.rs

318 lines · 10208 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Partition information protocol.
4
5use crate::proto::unsafe_protocol;
6use crate::{Char16, Guid, guid};
7
8newtype_enum! {
9 /// MBR OS type.
10 ///
11 /// Only two values are defined in the UEFI specification, other
12 /// values are used by legacy operating systems.
13 pub enum MbrOsType: u8 => {
14 /// A fake partition covering the entire disk.
15 GPT_PROTECTIVE = 0xee,
16
17 /// UEFI system partition.
18 UEFI_SYSTEM_PARTITION = 0xef,
19 }
20}
21
22/// Legacy MBR Partition Record.
23#[repr(C)]
24#[repr(packed)]
25#[derive(Clone, Copy, Debug)]
26pub struct MbrPartitionRecord {
27 /// If 0x80, this is the bootable legacy partition.
28 pub boot_indicator: u8,
29
30 /// Start of the partition in CHS address format.
31 pub starting_chs: [u8; 3],
32
33 /// Type of partition.
34 pub os_type: MbrOsType,
35
36 /// End of the partition in CHS address format.
37 pub ending_chs: [u8; 3],
38
39 /// Starting LBA of the partition on the disk.
40 pub starting_lba: u32,
41
42 /// Size of the partition in LBA units of logical blocks.
43 pub size_in_lba: u32,
44}
45
46impl MbrPartitionRecord {
47 /// True if the partition is a bootable legacy partition.
48 #[must_use]
49 pub const fn is_bootable(&self) -> bool {
50 self.boot_indicator == 0x80
51 }
52}
53
54newtype_enum! {
55 /// GUID that defines the type of partition. Only three values are
56 /// defined in the UEFI specification, OS vendors define their own
57 /// Partition Type GUIDs.
58 pub enum GptPartitionType: Guid => {
59 /// Indicates a partition entry is unused.
60 UNUSED_ENTRY = guid!("00000000-0000-0000-0000-000000000000"),
61
62 /// EFI System Partition.
63 EFI_SYSTEM_PARTITION = guid!("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"),
64
65 /// Partition containing a legacy MBR.
66 LEGACY_MBR = guid!("024dee41-33e7-11d3-9d69-0008c781f39f"),
67 }
68}
69
70bitflags::bitflags! {
71
72 /// Attributes describing a GPT partition.
73 ///
74 /// * Bit 0: [`REQUIRED_PARTITION`][Self::REQUIRED_PARTITION]
75 /// * Bit 1: [`NO_BLOCK_IO_PROTOCOL`][Self::NO_BLOCK_IO_PROTOCOL]
76 /// * Bit 2: [`LEGACY_BIOS_BOOTABLE`][Self::LEGACY_BIOS_BOOTABLE]
77 /// * Bits `3..=47`: reserved for future use and must be zero.
78 /// * Bits `48..=63`: See
79 /// [`type_specific_bits`][Self::type_specific_bits] and
80 /// [`RESERVED_FOR_PARTITION_TYPE`][Self::RESERVED_FOR_PARTITION_TYPE].
81 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
82 #[repr(transparent)]
83 pub struct GptPartitionAttributes: u64 {
84 /// Bit: Partition is required for the platform to function.
85 const REQUIRED_PARTITION = 1;
86 /// Bit: No [`BlockIO`] protocol will be created for this partition.
87 ///
88 /// [`BlockIO`]: uefi::proto::media::block::BlockIO
89 const NO_BLOCK_IO_PROTOCOL = 1 << 1;
90
91 /// Bit: Indicates that special software on a legacy BIOS system may
92 /// treat this partition as bootable. UEFI boot managers must
93 /// ignore the partition.
94 const LEGACY_BIOS_BOOTABLE = 1 << 2;
95
96 /// Mask for bits `48..=63`. The meaning of these bits depends
97 /// on the partition type.
98 const RESERVED_FOR_PARTITION_TYPE = 0xffff_0000_0000_0000;
99
100 /// The meaning of this bit depends on the partition type.
101 const TYPE_SPECIFIC_BIT_0 = 1 << 48;
102
103 /// The meaning of this bit depends on the partition type.
104 const TYPE_SPECIFIC_BIT_1 = 1 << 49;
105
106 /// The meaning of this bit depends on the partition type.
107 const TYPE_SPECIFIC_BIT_2 = 1 << 50;
108
109 /// The meaning of this bit depends on the partition type.
110 const TYPE_SPECIFIC_BIT_3 = 1 << 51;
111
112 /// The meaning of this bit depends on the partition type.
113 const TYPE_SPECIFIC_BIT_4 = 1 << 52;
114
115 /// The meaning of this bit depends on the partition type.
116 const TYPE_SPECIFIC_BIT_5 = 1 << 53;
117
118 /// The meaning of this bit depends on the partition type.
119 const TYPE_SPECIFIC_BIT_6 = 1 << 54;
120
121 /// The meaning of this bit depends on the partition type.
122 const TYPE_SPECIFIC_BIT_7 = 1 << 55;
123
124 /// The meaning of this bit depends on the partition type.
125 const TYPE_SPECIFIC_BIT_8 = 1 << 56;
126
127 /// The meaning of this bit depends on the partition type.
128 const TYPE_SPECIFIC_BIT_9 = 1 << 57;
129
130 /// The meaning of this bit depends on the partition type.
131 const TYPE_SPECIFIC_BIT_10 = 1 << 58;
132
133 /// The meaning of this bit depends on the partition type.
134 const TYPE_SPECIFIC_BIT_11 = 1 << 59;
135
136 /// The meaning of this bit depends on the partition type.
137 const TYPE_SPECIFIC_BIT_12 = 1 << 60;
138
139 /// The meaning of this bit depends on the partition type.
140 const TYPE_SPECIFIC_BIT_13 = 1 << 61;
141
142 /// The meaning of this bit depends on the partition type.
143 const TYPE_SPECIFIC_BIT_14 = 1 << 62;
144
145 /// The meaning of this bit depends on the partition type.
146 const TYPE_SPECIFIC_BIT_15 = 1 << 63;
147 }
148}
149
150impl GptPartitionAttributes {
151 /// Get bits `48..=63` as a [`u16`]. The meaning of these bits depends
152 /// on the partition's type (see [`GptPartitionEntry::partition_type_guid`]).
153 #[must_use]
154 pub const fn type_specific_bits(&self) -> u16 {
155 (self.0.bits() >> 48) as u16
156 }
157}
158
159/// GPT/EFI Partition Entry.
160#[repr(C)]
161#[repr(packed)]
162#[derive(Clone, Copy, Debug, Eq, PartialEq)]
163pub struct GptPartitionEntry {
164 /// GUID that defines the type of this Partition. A value of zero
165 /// indicates that this partition entry is unused.
166 pub partition_type_guid: GptPartitionType,
167
168 /// GUID that is unique for every partition entry.
169 pub unique_partition_guid: Guid,
170
171 /// Starting LBA of the partition.
172 pub starting_lba: u64,
173
174 /// Ending LBA of the partition.
175 pub ending_lba: u64,
176
177 /// All attribute bits of the partition.
178 pub attributes: GptPartitionAttributes,
179
180 /// Null-terminated string containing a human-readable name of the
181 /// partition.
182 pub partition_name: [Char16; 36],
183}
184
185impl GptPartitionEntry {
186 /// Get the number of blocks in the partition. Returns `None` if the
187 /// end block is before the start block, or if the number doesn't
188 /// fit in a `u64`.
189 #[must_use]
190 pub fn num_blocks(&self) -> Option<u64> {
191 self.ending_lba
192 .checked_sub(self.starting_lba)?
193 .checked_add(1)
194 }
195}
196
197newtype_enum! {
198 /// Partition type.
199 pub enum PartitionType: u32 => {
200 /// Partition is not MBR or GPT.
201 OTHER = 0x00,
202 /// MBR partition.
203 MBR = 0x01,
204 /// GPT partition.
205 GPT = 0x02,
206 }
207}
208
209#[repr(C)]
210#[derive(Clone, Copy)]
211union PartitionInfoRecord {
212 mbr: MbrPartitionRecord,
213 gpt: GptPartitionEntry,
214}
215
216newtype_enum! {
217 /// Partition info protocol revision.
218 pub enum PartitionInfoRevision: u32 => {
219 /// Revision of EFI_PARTITION_INFO_PROTOCOL_REVISION.
220 PROTOCOL_REVISION = 0x0001000,
221 }
222}
223
224/// Partition Info [`Protocol`].
225///
226/// Protocol for accessing partition information.
227///
228/// [`Protocol`]: uefi::proto::Protocol
229#[expect(missing_debug_implementations)]
230#[repr(C)]
231#[repr(packed)]
232#[unsafe_protocol("8cf2f62c-bc9b-4821-808d-ec9ec421a1a0")]
233pub struct PartitionInfo {
234 /// Revision of the partition info protocol.
235 pub revision: PartitionInfoRevision,
236
237 /// Type of partition.
238 pub partition_type: PartitionType,
239
240 system: u8,
241 reserved: [u8; 7],
242 record: PartitionInfoRecord,
243}
244
245impl PartitionInfo {
246 /// True if the partition is an EFI system partition.
247 #[must_use]
248 pub const fn is_system(&self) -> bool {
249 self.system == 1
250 }
251
252 /// Get the MBR partition record. Returns None if the partition
253 /// type is not MBR.
254 #[must_use]
255 pub fn mbr_partition_record(&self) -> Option<&MbrPartitionRecord> {
256 if { self.revision } != PartitionInfoRevision::PROTOCOL_REVISION {
257 return None;
258 }
259
260 if { self.partition_type } == PartitionType::MBR {
261 // SAFETY: The memory is valid.
262 Some(unsafe { &self.record.mbr })
263 } else {
264 None
265 }
266 }
267
268 /// Get the GPT partition entry. Returns None if the partition
269 /// type is not GPT.
270 #[must_use]
271 pub fn gpt_partition_entry(&self) -> Option<&GptPartitionEntry> {
272 if { self.revision } != PartitionInfoRevision::PROTOCOL_REVISION {
273 return None;
274 }
275
276 if { self.partition_type } == PartitionType::GPT {
277 // SAFETY: The memory is valid.
278 Some(unsafe { &self.record.gpt })
279 } else {
280 None
281 }
282 }
283}
284
285#[cfg(test)]
286mod tests {
287 use super::*;
288
289 #[test]
290 fn test_partition_attributes() {
291 let attr: GptPartitionAttributes =
292 GptPartitionAttributes::from_bits_retain(0xabcd_0000_0000_0007);
293 assert_eq!(attr.type_specific_bits(), 0xabcd);
294 }
295
296 /// Check that the type-specific bit constants cover exactly the
297 /// reserved range, bits 48 to 63.
298 #[test]
299 fn test_type_specific_bits() {
300 let all = GptPartitionAttributes::TYPE_SPECIFIC_BIT_0
301 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_1
302 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_2
303 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_3
304 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_4
305 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_5
306 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_6
307 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_7
308 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_8
309 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_9
310 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_10
311 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_11
312 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_12
313 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_13
314 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_14
315 | GptPartitionAttributes::TYPE_SPECIFIC_BIT_15;
316 assert_eq!(all, GptPartitionAttributes::RESERVED_FOR_PARTITION_TYPE);
317 }
318}