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
//! Error types for partition operations.
use core::fmt::{self, Debug, Display};
/// Errors that can occur during partition table operations.
#[derive(Debug)]
pub enum Error {
/// An I/O error occurred.
Io(hadris_io::Error),
/// The MBR signature (0x55AA) is invalid.
InvalidMbrSignature {
/// The actual signature bytes found.
found: [u8; 2],
},
/// The GPT signature ("EFI PART") is invalid.
InvalidGptSignature {
/// The actual signature bytes found.
found: [u8; 8],
},
/// The GPT header CRC32 checksum does not match.
GptHeaderCrcMismatch {
/// The expected CRC32 value (from header).
expected: u32,
/// The actual CRC32 value (calculated).
actual: u32,
},
/// The GPT partition entry array CRC32 checksum does not match.
GptEntriesCrcMismatch {
/// The expected CRC32 value (from header).
expected: u32,
/// The actual CRC32 value (calculated).
actual: u32,
},
/// The backup GPT header could not be read from its declared LBA.
BackupHeaderIo {
/// LBA declared by the primary header for the backup header.
lba: u64,
/// Underlying I/O failure.
source: hadris_io::Error,
},
/// The backup GPT header signature is invalid.
InvalidBackupGptSignature {
/// The actual signature bytes found.
found: [u8; 8],
},
/// The backup GPT header CRC32 checksum does not match.
BackupGptHeaderCrcMismatch {
/// The expected CRC32 value stored in the backup header.
expected: u32,
/// The calculated CRC32 value.
actual: u32,
},
/// Two partitions overlap.
PartitionOverlap {
/// Index of the first overlapping partition.
index1: usize,
/// Index of the second overlapping partition.
index2: usize,
/// Starting LBA of the overlap.
overlap_start: u64,
/// Ending LBA of the overlap.
overlap_end: u64,
},
/// Too many partitions requested.
TooManyPartitions {
/// Maximum number of partitions allowed.
max: usize,
/// Number of partitions requested.
requested: usize,
},
/// A partition extends beyond the disk boundary.
PartitionOutOfBounds {
/// Index of the offending partition.
index: usize,
/// Ending LBA of the partition.
partition_end: u64,
/// Last usable LBA of the disk.
disk_end: u64,
},
/// The partition entry size is invalid.
InvalidPartitionEntrySize {
/// The invalid size.
size: u32,
},
/// A logical block is too small to contain the requested structure.
InvalidBlockSize {
/// Supplied logical block size in bytes.
size: u32,
/// Minimum required size in bytes.
minimum: u32,
},
/// The backup GPT header does not match the primary.
BackupHeaderMismatch,
/// No protective MBR found on a GPT disk.
NoProtectiveMbr,
/// Invalid hybrid MBR configuration.
InvalidHybridMbr {
/// Description of the error.
reason: &'static str,
},
/// The disk is too small for the requested partitions.
DiskTooSmall {
/// Required size in sectors.
required: u64,
/// Available size in sectors.
available: u64,
},
/// A required feature is not available.
FeatureNotAvailable(&'static str),
/// A partition is not properly aligned.
MisalignedPartition {
/// The misaligned LBA.
lba: u64,
/// The required alignment in sectors.
required_alignment: u64,
},
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "I/O error: {err}"),
Self::InvalidMbrSignature { found } => {
write!(
f,
"invalid MBR signature: expected 0x55AA, found 0x{:02X}{:02X}",
found[0], found[1]
)
}
Self::InvalidGptSignature { found } => {
write!(
f,
"invalid GPT signature: expected 'EFI PART', found {found:?}"
)
}
Self::GptHeaderCrcMismatch { expected, actual } => {
write!(
f,
"GPT header CRC mismatch: expected 0x{expected:08X}, got 0x{actual:08X}"
)
}
Self::GptEntriesCrcMismatch { expected, actual } => {
write!(
f,
"GPT entries CRC mismatch: expected 0x{expected:08X}, got 0x{actual:08X}"
)
}
Self::InvalidBlockSize { size, minimum } => {
write!(f, "invalid block size {size}; minimum is {minimum} bytes")
}
Self::BackupHeaderIo { lba, source } => {
write!(f, "failed to read backup GPT header at LBA {lba}: {source}")
}
Self::InvalidBackupGptSignature { found } => {
write!(
f,
"invalid backup GPT signature: expected 'EFI PART', found {found:?}"
)
}
Self::BackupGptHeaderCrcMismatch { expected, actual } => {
write!(
f,
"backup GPT header CRC mismatch: expected 0x{expected:08X}, got 0x{actual:08X}"
)
}
Self::PartitionOverlap {
index1,
index2,
overlap_start,
overlap_end,
} => {
write!(
f,
"partitions {index1} and {index2} overlap (LBA {overlap_start}-{overlap_end})"
)
}
Self::TooManyPartitions { max, requested } => {
write!(
f,
"too many partitions: maximum is {max}, requested {requested}"
)
}
Self::PartitionOutOfBounds {
index,
partition_end,
disk_end,
} => {
write!(
f,
"partition {index} extends beyond disk (ends at LBA {partition_end}, disk ends at {disk_end})"
)
}
Self::InvalidPartitionEntrySize { size } => {
write!(
f,
"invalid partition entry size: {size} (must be 128 * 2^n)"
)
}
Self::BackupHeaderMismatch => {
write!(f, "backup GPT header does not match primary")
}
Self::NoProtectiveMbr => {
write!(f, "no protective MBR found on GPT disk")
}
Self::InvalidHybridMbr { reason } => {
write!(f, "invalid hybrid MBR: {reason}")
}
Self::DiskTooSmall {
required,
available,
} => {
write!(
f,
"disk too small: requires {required} sectors, only {available} available"
)
}
Self::FeatureNotAvailable(feature) => {
write!(f, "feature not available: {feature}")
}
Self::MisalignedPartition {
lba,
required_alignment,
} => {
write!(
f,
"partition at LBA {lba} is not aligned to {required_alignment} sectors"
)
}
}
}
}
impl<E: hadris_io::IoError> From<hadris_io::Error<E>> for Error {
fn from(err: hadris_io::Error<E>) -> Self {
Self::Io(err.erase())
}
}
#[cfg(all(feature = "write", any(feature = "sync", feature = "async")))]
impl Error {
/// An on-disk LBA field whose byte offset is not representable.
pub(crate) fn lba_offset_overflow() -> Self {
Self::Io(hadris_io::Error::new(
hadris_io::ErrorKind::InvalidInput,
"LBA value overflows byte offset",
))
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
Self::BackupHeaderIo { source, .. } => Some(source),
_ => None,
}
}
}
/// A specialized `Result` type for partition operations.
pub type Result<T> = core::result::Result<T, Error>;