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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! Partition information protocol.

use crate::proto::unsafe_protocol;
use crate::{guid, Char16, Guid};

newtype_enum! {
    /// MBR OS type.
    ///
    /// Only two values are defined in the UEFI specification, other
    /// values are used by legacy operating systems.
    pub enum MbrOsType: u8 => {
        /// A fake partition covering the entire disk.
        GPT_PROTECTIVE = 0xee,

        /// UEFI system partition.
        UEFI_SYSTEM_PARTITION = 0xef,
    }
}

/// Legacy MBR Partition Record.
#[repr(C)]
#[repr(packed)]
#[derive(Clone, Copy, Debug)]
pub struct MbrPartitionRecord {
    /// If 0x80, this is the bootable legacy partition.
    pub boot_indicator: u8,

    /// Start of the partition in CHS address format.
    pub starting_chs: [u8; 3],

    /// Type of partition.
    pub os_type: MbrOsType,

    /// End of the partition in CHS address format.
    pub ending_chs: [u8; 3],

    /// Starting LBA of the partition on the disk.
    pub starting_lba: u32,

    /// Size of the partition in LBA units of logical blocks.
    pub size_in_lba: u32,
}

impl MbrPartitionRecord {
    /// True if the partition is a bootable legacy partition.
    #[must_use]
    pub const fn is_bootable(&self) -> bool {
        self.boot_indicator == 0x80
    }
}

newtype_enum! {
    /// GUID that defines the type of partition. Only three values are
    /// defined in the UEFI specification, OS vendors define their own
    /// Partition Type GUIDs.
    pub enum GptPartitionType: Guid => {
        /// Indicates a partition entry is unused.
        UNUSED_ENTRY = guid!("00000000-0000-0000-0000-000000000000"),

        /// EFI System Partition.
        EFI_SYSTEM_PARTITION = guid!("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"),

        /// Partition containing a legacy MBR.
        LEGACY_MBR = guid!("024dee41-33e7-11d3-9d69-0008c781f39f"),
    }
}

bitflags::bitflags! {

    /// Attributes describing a GPT partition.
    ///
    /// * Bit 0: [`REQUIRED_PARTITION`][Self::REQUIRED_PARTITION]
    /// * Bit 1: [`NO_BLOCK_IO_PROTOCOL`][Self::NO_BLOCK_IO_PROTOCOL]
    /// * Bit 2: [`LEGACY_BIOS_BOOTABLE`][Self::LEGACY_BIOS_BOOTABLE]
    /// * Bits `3..=47`: reserved for future use and must be zero.
    /// * Bits `48..=63`: See
    /// [`type_specific_bits`][Self::type_specific_bits] and
    /// [`RESERVED_FOR_PARTITION_TYPE`][Self::RESERVED_FOR_PARTITION_TYPE].
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
    #[repr(transparent)]
    pub struct GptPartitionAttributes: u64 {
        /// Bit: Partition is required for the platform to function.
        const REQUIRED_PARTITION = 1;
        /// Bit: No [`BlockIO`] protocol will be created for this partition.
        ///
        /// [`BlockIO`]: uefi::proto::media::block::BlockIO
        const NO_BLOCK_IO_PROTOCOL = 1 << 1;

        /// Bit: Indicates that special software on a legacy BIOS system may
        /// treat this partition as bootable. UEFI boot managers must
        /// ignore the partition.
        const LEGACY_BIOS_BOOTABLE = 1 << 2;

        /// Mask for bits `48..=63`. The meaning of these bits depends
        /// on the partition type.
        const RESERVED_FOR_PARTITION_TYPE = 0xffff_0000_0000_0000;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_0 = 1 << 47;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_1 = 1 << 48;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_2 = 1 << 49;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_3 = 1 << 50;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_4 = 1 << 51;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_5 = 1 << 52;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_6 = 1 << 53;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_7 = 1 << 54;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_8 = 1 << 55;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_9 = 1 << 56;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_10 = 1 << 57;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_11 = 1 << 58;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_12 = 1 << 59;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_13 = 1 << 60;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_14 = 1 << 61;

        /// The meaning of this bit depends on the partition type.
        const TYPE_SPECIFIC_BIT_15 = 1 << 62;
    }
}

impl GptPartitionAttributes {
    /// Get bits `48..=63` as a [`u16`]. The meaning of these bits depends
    /// on the partition's type (see [`GptPartitionEntry::partition_type_guid`]).
    #[must_use]
    pub const fn type_specific_bits(&self) -> u16 {
        (self.0.bits() >> 48) as u16
    }
}

/// GPT/EFI Partition Entry.
#[repr(C)]
#[repr(packed)]
#[derive(Clone, Copy, Debug)]
pub struct GptPartitionEntry {
    /// GUID that defines the type of this Partition. A value of zero
    /// indicates that this partition entry is unused.
    pub partition_type_guid: GptPartitionType,

    /// GUID that is unique for every partition entry.
    pub unique_partition_guid: Guid,

    /// Starting LBA of the partition.
    pub starting_lba: u64,

    /// Ending LBA of the partition.
    pub ending_lba: u64,

    /// All attribute bits of the partition.
    pub attributes: GptPartitionAttributes,

    /// Null-terminated string containing a human-readable name of the
    /// partition.
    pub partition_name: [Char16; 36],
}

impl GptPartitionEntry {
    /// Get the number of blocks in the partition. Returns `None` if the
    /// end block is before the start block, or if the number doesn't
    /// fit in a `u64`.
    #[must_use]
    pub fn num_blocks(&self) -> Option<u64> {
        self.ending_lba
            .checked_sub(self.starting_lba)?
            .checked_add(1)
    }
}

newtype_enum! {
    /// Partition type.
    pub enum PartitionType: u32 => {
        /// Partition is not MBR or GPT.
        OTHER = 0x00,
        /// MBR partition.
        MBR = 0x01,
        /// GPT partition.
        GPT = 0x02,
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
union PartitionInfoRecord {
    mbr: MbrPartitionRecord,
    gpt: GptPartitionEntry,
}

newtype_enum! {
    /// Partition info protocol revision.
    pub enum PartitionInfoRevision: u32 => {
        /// Revision of EFI_PARTITION_INFO_PROTOCOL_REVISION.
        PROTOCOL_REVISION = 0x0001000,
    }
}

/// Protocol for accessing partition information.
#[allow(missing_debug_implementations)]
#[repr(C)]
#[repr(packed)]
#[unsafe_protocol("8cf2f62c-bc9b-4821-808d-ec9ec421a1a0")]
pub struct PartitionInfo {
    /// Revision of the partition info protocol.
    pub revision: PartitionInfoRevision,

    /// Type of partition.
    pub partition_type: PartitionType,

    system: u8,
    reserved: [u8; 7],
    record: PartitionInfoRecord,
}

impl PartitionInfo {
    /// True if the partition is an EFI system partition.
    #[must_use]
    pub const fn is_system(&self) -> bool {
        self.system == 1
    }

    /// Get the MBR partition record. Returns None if the partition
    /// type is not MBR.
    #[must_use]
    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
        }
    }

    /// Get the GPT partition entry. Returns None if the partition
    /// type is not GPT.
    #[must_use]
    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
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_partition_attributes() {
        let attr: GptPartitionAttributes =
            GptPartitionAttributes::from_bits_retain(0xabcd_0000_0000_0007);
        assert_eq!(attr.type_specific_bits(), 0xabcd);
    }
}