hadris-part 1.0.0

Partition table support for MBR, GPT, and Hybrid MBR
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Unified partition scheme handling.
//!
//! This module provides a unified API for working with different partition schemes:
//! - MBR (Master Boot Record)
//! - GPT (GUID Partition Table)
//! - Hybrid MBR (GPT with MBR entries for BIOS compatibility)

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "alloc")]
use crate::error::{PartitionError, Result};
use crate::gpt::Guid;
#[cfg(feature = "alloc")]
use crate::gpt::{GptHeader, GptPartitionEntry};
use crate::hybrid::is_hybrid_mbr;
use crate::mbr::MasterBootRecord;

/// The type of partition scheme detected or to be created.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PartitionSchemeType {
    /// Master Boot Record (legacy BIOS).
    Mbr,
    /// GUID Partition Table (UEFI).
    Gpt,
    /// Hybrid MBR (GPT with MBR entries for dual BIOS/UEFI boot).
    Hybrid,
}

impl core::fmt::Display for PartitionSchemeType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Mbr => write!(f, "MBR"),
            Self::Gpt => write!(f, "GPT"),
            Self::Hybrid => write!(f, "Hybrid MBR"),
        }
    }
}

/// Information about a partition, independent of the underlying scheme.
#[derive(Debug, Clone, Copy)]
pub struct PartitionInfo {
    /// Partition index (0-based).
    pub index: usize,
    /// Starting LBA.
    pub start_lba: u64,
    /// Ending LBA (inclusive).
    pub end_lba: u64,
    /// Size in sectors.
    pub size_sectors: u64,
    /// Whether the partition is bootable/active.
    pub bootable: bool,
    /// Partition type (MBR type code or GPT type GUID).
    pub partition_type: PartitionType,
}

/// Partition type information.
#[derive(Debug, Clone, Copy)]
pub enum PartitionType {
    /// MBR partition type code.
    Mbr(u8),
    /// GPT partition type GUID.
    Gpt(Guid),
}

impl PartitionInfo {
    /// Returns the size in bytes (assuming 512-byte sectors).
    pub const fn size_bytes(&self) -> u64 {
        self.size_sectors * 512
    }

    /// Returns the size in bytes for a given sector size.
    pub const fn size_bytes_with_sector_size(&self, sector_size: u32) -> u64 {
        self.size_sectors * sector_size as u64
    }
}

/// A complete GPT disk structure.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub struct GptDisk {
    /// The primary GPT header (at LBA 1).
    pub primary_header: GptHeader,
    /// The backup GPT header (at last LBA).
    pub backup_header: GptHeader,
    /// Partition entries.
    pub entries: Vec<GptPartitionEntry>,
    /// Logical block size in bytes.
    pub block_size: u32,
}

#[cfg(feature = "alloc")]
impl GptDisk {
    /// Default number of partition entries.
    pub const DEFAULT_ENTRY_COUNT: u32 = 128;

    /// Creates a new empty GPT disk structure.
    pub fn new(disk_sectors: u64, block_size: u32) -> Self {
        let entry_count = Self::DEFAULT_ENTRY_COUNT;
        let entry_size = core::mem::size_of::<GptPartitionEntry>() as u32;
        let entries_per_sector = block_size / entry_size;
        let entry_sectors = entry_count.div_ceil(entries_per_sector);

        // First usable LBA is after: MBR (1) + GPT header (1) + entries
        let first_usable = 2 + entry_sectors as u64;
        // Last usable LBA is before: backup entries + backup header (1)
        let last_usable = disk_sectors - 2 - entry_sectors as u64;

        let disk_guid = {
            #[cfg(feature = "rand")]
            {
                Guid::generate_v4()
            }
            #[cfg(not(feature = "rand"))]
            {
                Guid::UNUSED
            }
        };

        #[cfg_attr(not(feature = "crc"), allow(unused_mut))]
        let mut primary_header = GptHeader {
            signature: GptHeader::SIGNATURE,
            revision: GptHeader::REVISION_1_0,
            header_size: GptHeader::STANDARD_HEADER_SIZE,
            header_crc32: 0,
            reserved: 0,
            my_lba: 1,
            alternate_lba: disk_sectors - 1,
            first_usable_lba: first_usable,
            last_usable_lba: last_usable,
            disk_guid,
            partition_entry_lba: 2,
            num_partition_entries: entry_count,
            size_of_partition_entry: entry_size,
            partition_entry_array_crc32: 0,
        };

        #[cfg_attr(not(feature = "crc"), allow(unused_mut))]
        let mut backup_header = GptHeader {
            my_lba: disk_sectors - 1,
            alternate_lba: 1,
            partition_entry_lba: disk_sectors - 1 - entry_sectors as u64,
            ..primary_header
        };

        let entries = alloc::vec![GptPartitionEntry::default(); entry_count as usize];

        // Update CRCs
        #[cfg(feature = "crc")]
        {
            let entries_crc = crate::gpt::calculate_partition_array_crc32(&entries);
            primary_header.partition_entry_array_crc32 = entries_crc;
            backup_header.partition_entry_array_crc32 = entries_crc;
            primary_header.update_crc32();
            backup_header.update_crc32();
        }

        Self {
            primary_header,
            backup_header,
            entries,
            block_size,
        }
    }

    /// Returns the number of used (non-empty) partition entries.
    pub fn partition_count(&self) -> usize {
        self.entries.iter().filter(|e| !e.is_unused()).count()
    }

    /// Returns an iterator over non-empty partition entries.
    pub fn partitions(&self) -> impl Iterator<Item = (usize, &GptPartitionEntry)> {
        self.entries
            .iter()
            .enumerate()
            .filter(|(_, e)| !e.is_unused())
    }

    /// Adds a partition to the first available slot.
    ///
    /// Returns the index of the new partition, or an error if no slots are available.
    pub fn add_partition(&mut self, entry: GptPartitionEntry) -> Result<usize> {
        for (i, slot) in self.entries.iter_mut().enumerate() {
            if slot.is_unused() {
                *slot = entry;
                self.update_crcs();
                return Ok(i);
            }
        }
        Err(PartitionError::TooManyPartitions {
            max: self.entries.len(),
            requested: self.entries.len() + 1,
        })
    }

    /// Validates the GPT structure.
    pub fn validate(&self) -> Result<()> {
        // Check signature
        if !self.primary_header.has_valid_signature() {
            return Err(PartitionError::InvalidGptSignature {
                found: self.primary_header.signature,
            });
        }

        // Check CRCs
        #[cfg(feature = "crc")]
        {
            if !self.primary_header.verify_crc32() {
                return Err(PartitionError::GptHeaderCrcMismatch {
                    expected: self.primary_header.header_crc32,
                    actual: self.primary_header.calculate_crc32(),
                });
            }

            let entries_crc = crate::gpt::calculate_partition_array_crc32(&self.entries);
            if self.primary_header.partition_entry_array_crc32 != entries_crc {
                return Err(PartitionError::GptEntriesCrcMismatch {
                    expected: self.primary_header.partition_entry_array_crc32,
                    actual: entries_crc,
                });
            }
        }

        // Check for overlapping partitions
        let used: Vec<_> = self.partitions().collect();
        for i in 0..used.len() {
            for j in (i + 1)..used.len() {
                let (idx1, p1) = used[i];
                let (idx2, p2) = used[j];
                if p1.first_lba <= p2.last_lba && p2.first_lba <= p1.last_lba {
                    let overlap_start = p1.first_lba.max(p2.first_lba);
                    let overlap_end = p1.last_lba.min(p2.last_lba);
                    return Err(PartitionError::PartitionOverlap {
                        index1: idx1,
                        index2: idx2,
                        overlap_start,
                        overlap_end,
                    });
                }
            }
        }

        // Check partitions are within usable area
        for (idx, entry) in self.partitions() {
            if entry.first_lba < self.primary_header.first_usable_lba
                || entry.last_lba > self.primary_header.last_usable_lba
            {
                return Err(PartitionError::PartitionOutOfBounds {
                    index: idx,
                    partition_end: entry.last_lba,
                    disk_end: self.primary_header.last_usable_lba,
                });
            }
        }

        Ok(())
    }

    /// Updates all CRCs in the headers.
    #[cfg(feature = "crc")]
    pub fn update_crcs(&mut self) {
        let entries_crc = crate::gpt::calculate_partition_array_crc32(&self.entries);
        self.primary_header.partition_entry_array_crc32 = entries_crc;
        self.backup_header.partition_entry_array_crc32 = entries_crc;
        self.primary_header.update_crc32();
        self.backup_header.update_crc32();
    }

    #[cfg(not(feature = "crc"))]
    pub fn update_crcs(&mut self) {
        // No-op without CRC feature
    }

    /// Creates a protective MBR for this GPT disk.
    pub fn create_protective_mbr(&self) -> MasterBootRecord {
        let disk_sectors = self.backup_header.my_lba + 1;
        MasterBootRecord::protective(disk_sectors)
    }
}

/// A unified partition scheme that can be MBR, GPT, or Hybrid.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub enum DiskPartitionScheme {
    /// Pure MBR partitioning.
    Mbr(MasterBootRecord),
    /// GPT partitioning with protective MBR.
    Gpt {
        /// The protective MBR.
        protective_mbr: MasterBootRecord,
        /// The GPT disk structure.
        gpt: GptDisk,
    },
    /// Hybrid MBR + GPT partitioning.
    Hybrid {
        /// The hybrid MBR.
        hybrid_mbr: MasterBootRecord,
        /// The GPT disk structure.
        gpt: GptDisk,
    },
}

#[cfg(feature = "alloc")]
impl DiskPartitionScheme {
    /// Creates a new MBR-only partition scheme.
    pub fn new_mbr() -> Self {
        Self::Mbr(MasterBootRecord::default())
    }

    /// Creates a new GPT partition scheme.
    pub fn new_gpt(disk_sectors: u64, block_size: u32) -> Self {
        let gpt = GptDisk::new(disk_sectors, block_size);
        let protective_mbr = gpt.create_protective_mbr();
        Self::Gpt {
            protective_mbr,
            gpt,
        }
    }

    /// Returns the partition scheme type.
    pub fn scheme_type(&self) -> PartitionSchemeType {
        match self {
            Self::Mbr(_) => PartitionSchemeType::Mbr,
            Self::Gpt { .. } => PartitionSchemeType::Gpt,
            Self::Hybrid { .. } => PartitionSchemeType::Hybrid,
        }
    }

    /// Returns partition information for all partitions.
    pub fn partitions(&self) -> Vec<PartitionInfo> {
        match self {
            Self::Mbr(mbr) => {
                let pt = mbr.get_partition_table();
                pt.partitions
                    .iter()
                    .enumerate()
                    .filter(|(_, p)| !p.is_empty())
                    .map(|(i, p)| PartitionInfo {
                        index: i,
                        start_lba: p.start_lba as u64,
                        end_lba: p.end_lba() as u64,
                        size_sectors: p.sector_count as u64,
                        bootable: p.is_bootable(),
                        partition_type: PartitionType::Mbr(p.part_type),
                    })
                    .collect()
            }
            Self::Gpt { gpt, .. } | Self::Hybrid { gpt, .. } => gpt
                .partitions()
                .map(|(i, e)| PartitionInfo {
                    index: i,
                    start_lba: e.first_lba,
                    end_lba: e.last_lba,
                    size_sectors: e.size_sectors(),
                    bootable: e.attributes.is_legacy_bios_bootable(),
                    partition_type: PartitionType::Gpt(e.type_guid),
                })
                .collect(),
        }
    }

    /// Validates the partition scheme.
    pub fn validate(&self) -> Result<()> {
        match self {
            Self::Mbr(mbr) => {
                if !mbr.has_valid_signature() {
                    return Err(PartitionError::InvalidMbrSignature {
                        found: mbr.signature,
                    });
                }
                let pt = mbr.get_partition_table();
                if !pt.is_valid() {
                    return Err(PartitionError::InvalidHybridMbr {
                        reason: "invalid MBR partition table",
                    });
                }
                Ok(())
            }
            Self::Gpt {
                protective_mbr,
                gpt,
            } => {
                if !protective_mbr.has_valid_signature() {
                    return Err(PartitionError::InvalidMbrSignature {
                        found: protective_mbr.signature,
                    });
                }
                let pt = protective_mbr.get_partition_table();
                if !pt.is_protective() {
                    return Err(PartitionError::NoProtectiveMbr);
                }
                gpt.validate()
            }
            Self::Hybrid { hybrid_mbr, gpt } => {
                if !hybrid_mbr.has_valid_signature() {
                    return Err(PartitionError::InvalidMbrSignature {
                        found: hybrid_mbr.signature,
                    });
                }
                if !is_hybrid_mbr(hybrid_mbr) {
                    return Err(PartitionError::InvalidHybridMbr {
                        reason: "not a valid hybrid MBR",
                    });
                }
                gpt.validate()
            }
        }
    }
}

/// Detects the partition scheme type from an MBR.
///
/// This is a preliminary detection based only on the MBR.
/// To fully detect GPT, you need to also read and validate the GPT header.
pub fn detect_scheme_from_mbr(mbr: &MasterBootRecord) -> PartitionSchemeType {
    if !mbr.has_valid_signature() {
        return PartitionSchemeType::Mbr;
    }

    let pt = mbr.get_partition_table();
    if is_hybrid_mbr(mbr) {
        PartitionSchemeType::Hybrid
    } else if pt.is_protective() {
        PartitionSchemeType::Gpt
    } else {
        PartitionSchemeType::Mbr
    }
}

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

    #[cfg(feature = "alloc")]
    #[test]
    fn test_gpt_disk_creation() {
        let disk = GptDisk::new(1000000, 512);
        assert!(disk.primary_header.has_valid_signature());
        assert_eq!(disk.entries.len(), 128);
        assert_eq!(disk.partition_count(), 0);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_scheme_detection() {
        // MBR
        let mbr = MasterBootRecord::default();
        assert_eq!(detect_scheme_from_mbr(&mbr), PartitionSchemeType::Mbr);

        // Protective MBR (GPT)
        let protective = MasterBootRecord::protective(1000000);
        assert_eq!(
            detect_scheme_from_mbr(&protective),
            PartitionSchemeType::Gpt
        );

        // Hybrid MBR
        let mut hybrid = protective;
        hybrid.with_partition_table(|pt| {
            pt[1] = MbrPartition::new(crate::mbr::MbrPartitionType::Fat32, 2048, 100000);
        });
        assert_eq!(detect_scheme_from_mbr(&hybrid), PartitionSchemeType::Hybrid);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_partition_scheme_new_gpt() {
        let scheme = DiskPartitionScheme::new_gpt(1000000, 512);
        assert_eq!(scheme.scheme_type(), PartitionSchemeType::Gpt);
        assert!(scheme.validate().is_ok());
        assert!(scheme.partitions().is_empty());
    }
}