morpheus_core/disk/gpt_ops/
create_modify.rs

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
// GPT operations using gpt-disk-rs

use super::{mb_to_lba, GptError};
use crate::disk::partition::PartitionType;
use gpt_disk_io::{BlockIo, Disk};
use gpt_disk_types::{
    guid, BlockSize, GptHeader, GptPartitionEntry, GptPartitionEntryArray, LbaLe, U32Le,
};

/// Scan disk for GPT and populate partition table
pub fn create_gpt<B: BlockIo>(block_io: B, num_blocks: u64) -> Result<(), GptError> {
    let mut disk = Disk::new(block_io).map_err(|_| GptError::IoError)?;

    // Create GPT header
    let mut header = GptHeader {
        my_lba: LbaLe::from_u64(1),
        alternate_lba: LbaLe::from_u64(num_blocks - 1),
        first_usable_lba: LbaLe::from_u64(34),
        last_usable_lba: LbaLe::from_u64(num_blocks - 34),
        disk_guid: guid!("12345678-1234-1234-1234-123456789012"),
        partition_entry_lba: LbaLe::from_u64(2),
        number_of_partition_entries: U32Le::from_u32(128),
        ..Default::default()
    };

    header.update_header_crc32();

    // Write protective MBR
    let mut buf = [0u8; 512];
    disk.write_protective_mbr(&mut buf)
        .map_err(|_| GptError::IoError)?;

    // Write primary GPT header
    disk.write_primary_gpt_header(&header, &mut buf)
        .map_err(|_| GptError::IoError)?;

    // Write empty partition array
    let layout = header
        .get_partition_entry_array_layout()
        .map_err(|_| GptError::InvalidHeader)?;

    let block_size = BlockSize::BS_512;

    let mut entry_buf = [0u8; 16384];
    let entry_array = GptPartitionEntryArray::new(layout, block_size, &mut entry_buf)
        .map_err(|_| GptError::IoError)?;

    disk.write_gpt_partition_entry_array(&entry_array)
        .map_err(|_| GptError::IoError)?;

    // Flush to ensure writes are committed
    disk.flush().map_err(|_| GptError::IoError)?;

    Ok(())
}

/// Find all free space regions on disk
pub fn create_partition<B: BlockIo>(
    block_io: B,
    partition_type: PartitionType,
    start_lba: u64,
    end_lba: u64,
) -> Result<(), GptError> {
    let mut disk = Disk::new(block_io).map_err(|_| GptError::IoError)?;

    // Read existing header
    let mut header = disk
        .read_primary_gpt_header(&mut [0u8; 512])
        .map_err(|_| GptError::InvalidHeader)?;

    // Validate range
    let first_usable = header.first_usable_lba.to_u64();
    let last_usable = header.last_usable_lba.to_u64();

    if start_lba < first_usable || end_lba > last_usable || start_lba >= end_lba {
        return Err(GptError::InvalidSize);
    }

    // Read partition array
    let layout = header
        .get_partition_entry_array_layout()
        .map_err(|_| GptError::InvalidHeader)?;

    let mut entry_buf = [0u8; 16384];
    let mut entry_array = disk
        .read_gpt_partition_entry_array(layout, &mut entry_buf)
        .map_err(|_| GptError::IoError)?;

    // Find first empty slot
    let mut slot_index = None;
    let num_entries = layout.num_entries as usize;

    for i in 0..num_entries {
        if let Some(entry) = entry_array.get_partition_entry(i.try_into().unwrap()) {
            if !entry.is_used() {
                slot_index = Some(i);
                break;
            }
        }
    }

    let slot = slot_index.ok_or(GptError::NoSpace)?;

    // Create new entry directly in buffer
    let entry = entry_array
        .get_partition_entry_mut(slot.try_into().unwrap())
        .ok_or(GptError::NoSpace)?;

    entry.partition_type_guid = partition_type.to_gpt_guid();
    entry.unique_partition_guid = guid!("12345678-1234-5678-1234-567812345678"); // TODO: generate unique
    entry.starting_lba = LbaLe::from_u64(start_lba);
    entry.ending_lba = LbaLe::from_u64(end_lba);
    entry.attributes = Default::default();

    // Name is already zeroed in default entry

    // Update CRC in header
    header.partition_entry_array_crc32 = entry_array.calculate_crc32();
    header.update_header_crc32();

    // Write everything back
    disk.write_primary_gpt_header(&header, &mut [0u8; 512])
        .map_err(|_| GptError::IoError)?;

    disk.write_gpt_partition_entry_array(&entry_array)
        .map_err(|_| GptError::IoError)?;

    Ok(())
}

///Delete a partition by index
pub fn delete_partition<B: BlockIo>(block_io: B, partition_index: usize) -> Result<(), GptError> {
    let mut disk = Disk::new(block_io).map_err(|_| GptError::IoError)?;

    let mut header = disk
        .read_primary_gpt_header(&mut [0u8; 512])
        .map_err(|_| GptError::InvalidHeader)?;

    let layout = header
        .get_partition_entry_array_layout()
        .map_err(|_| GptError::InvalidHeader)?;

    let mut entry_buf = [0u8; 16384];
    let mut entry_array = disk
        .read_gpt_partition_entry_array(layout, &mut entry_buf)
        .map_err(|_| GptError::IoError)?;

    // Clear the entry
    if let Some(entry) = entry_array.get_partition_entry_mut(partition_index.try_into().unwrap()) {
        *entry = GptPartitionEntry::default(); // Zero out the entry
    } else {
        return Err(GptError::PartitionNotFound);
    }

    // Update CRCs
    header.partition_entry_array_crc32 = entry_array.calculate_crc32();
    header.update_header_crc32();

    // Write back
    disk.write_primary_gpt_header(&header, &mut [0u8; 512])
        .map_err(|_| GptError::IoError)?;

    disk.write_gpt_partition_entry_array(&entry_array)
        .map_err(|_| GptError::IoError)?;

    Ok(())
}

/// Shrink a partition to a new smaller size
/// partition_index: GPT entry index (0-127)
/// new_size_mb: New size in megabytes (must be smaller than current)
pub fn shrink_partition<B: BlockIo>(
    block_io: B,
    partition_index: usize,
    new_size_mb: u64,
) -> Result<(), GptError> {
    let mut disk = Disk::new(block_io).map_err(|_| GptError::IoError)?;

    let mut header = disk
        .read_primary_gpt_header(&mut [0u8; 512])
        .map_err(|_| GptError::InvalidHeader)?;

    let layout = header
        .get_partition_entry_array_layout()
        .map_err(|_| GptError::InvalidHeader)?;

    let mut entry_buf = [0u8; 16384];
    let mut entry_array = disk
        .read_gpt_partition_entry_array(layout, &mut entry_buf)
        .map_err(|_| GptError::IoError)?;

    // Get the partition entry
    let entry = entry_array
        .get_partition_entry_mut(partition_index.try_into().unwrap())
        .ok_or(GptError::PartitionNotFound)?;

    // Check if partition is used
    if !entry.is_used() {
        return Err(GptError::PartitionNotFound);
    }

    let start_lba = entry.starting_lba.to_u64();
    let current_end_lba = entry.ending_lba.to_u64();
    let current_size_lba = current_end_lba - start_lba + 1;

    // Calculate new size in LBA (assume 512-byte blocks)
    let new_size_lba = mb_to_lba(new_size_mb, 512);

    if new_size_lba == 0 {
        return Err(GptError::InvalidSize);
    }

    if new_size_lba >= current_size_lba {
        return Err(GptError::InvalidSize); // Can only shrink
    }

    // Calculate new end LBA
    let new_end_lba = start_lba + new_size_lba - 1;

    // Update the partition entry
    entry.ending_lba = LbaLe::from_u64(new_end_lba);

    // Update CRCs
    header.partition_entry_array_crc32 = entry_array.calculate_crc32();
    header.update_header_crc32();

    // Write back
    disk.write_primary_gpt_header(&header, &mut [0u8; 512])
        .map_err(|_| GptError::IoError)?;

    disk.write_gpt_partition_entry_array(&entry_array)
        .map_err(|_| GptError::IoError)?;

    Ok(())
}