1use anyhow::{Error, anyhow, ensure};
6use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
7
8const MAX_PARTITION_ENTRIES: u32 = 128;
9
10pub const GPT_SIGNATURE: [u8; 8] = [0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54];
11pub const GPT_REVISION: u32 = 0x10000;
12pub const GPT_HEADER_SIZE: usize = 92;
13
14#[derive(Clone, Debug, Eq, PartialEq, Immutable, IntoBytes, KnownLayout, FromBytes)]
16#[repr(C)]
17pub struct Header {
18 pub signature: [u8; 8],
20 pub revision: u32,
22 pub header_size: u32,
24 pub crc32: u32,
26 pub reserved: u32,
28 pub current_lba: u64,
30 pub backup_lba: u64,
32 pub first_usable: u64,
34 pub last_usable: u64,
36 pub disk_guid: [u8; 16],
38 pub part_start: u64,
40 pub num_parts: u32,
42 pub part_size: u32,
44 pub crc32_parts: u32,
46 zerocopy_padding: u32,
49}
50
51impl Header {
52 pub fn new(block_count: u64, block_size: u32, num_parts: u32) -> Result<Self, Error> {
53 ensure!(block_size > 0 && block_size.is_power_of_two(), "Invalid block size");
54 let bs = block_size as u64;
55
56 let part_size = std::mem::size_of::<PartitionTableEntry>();
57 let partition_table_len = num_parts as u64 * part_size as u64;
58 let partition_table_blocks = partition_table_len.checked_next_multiple_of(bs).unwrap() / bs;
59
60 ensure!(block_count > 1 + 2 * (1 + partition_table_blocks), "Too few blocks");
63
64 let mut this = Self {
65 signature: GPT_SIGNATURE,
66 revision: GPT_REVISION,
67 header_size: GPT_HEADER_SIZE as u32,
68 crc32: 0,
69 reserved: 0,
70 current_lba: 1,
71 backup_lba: block_count - 1,
72 first_usable: 2 + partition_table_blocks,
73 last_usable: block_count - (2 + partition_table_blocks),
74 disk_guid: uuid::Uuid::new_v4().into_bytes(),
75 part_start: 2,
76 num_parts,
77 part_size: part_size as u32,
78 crc32_parts: 0,
79 zerocopy_padding: 0,
80 };
81 this.update_checksum();
82 Ok(this)
83 }
84
85 pub fn compute_checksum(&self) -> u32 {
87 let mut header_copy = self.clone();
88 header_copy.crc32 = 0;
89 crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC)
90 .checksum(&header_copy.as_bytes()[..GPT_HEADER_SIZE])
91 }
92
93 fn update_checksum(&mut self) {
94 self.crc32 = 0;
95 let crc = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC)
96 .checksum(&self.as_bytes()[..GPT_HEADER_SIZE]);
97 self.crc32 = crc;
98 }
99
100 pub fn ensure_integrity(&self, block_count: u64, block_size: u64) -> Result<(), Error> {
102 ensure!(self.signature == GPT_SIGNATURE, "Bad signature {:x?}", self.signature);
103 ensure!(self.revision == GPT_REVISION, "Bad revision {:x}", self.revision);
104 ensure!(
105 self.header_size as usize == GPT_HEADER_SIZE,
106 "Bad header size {}",
107 self.header_size
108 );
109
110 ensure!(self.crc32 == self.compute_checksum(), "Invalid header checksum");
113
114 ensure!(self.num_parts <= MAX_PARTITION_ENTRIES, "Invalid num_parts {}", self.num_parts);
115 ensure!(
116 self.part_size as usize == std::mem::size_of::<PartitionTableEntry>(),
117 "Invalid part_size {}",
118 self.part_size
119 );
120 let partition_table_blocks = (self
121 .num_parts
122 .checked_mul(self.part_size)
123 .and_then(|v| v.checked_next_multiple_of(block_size as u32))
124 .ok_or_else(|| anyhow!("Partition table size overflow"))?
125 as u64)
126 / block_size;
127 ensure!(partition_table_blocks < block_count, "Invalid partition table size");
128
129 ensure!(
133 self.current_lba == 1 || self.current_lba == block_count - 1,
134 "Invalid current_lba {}",
135 self.current_lba
136 );
137 let (first_lba, second_lba) = if self.current_lba == 1 {
138 (self.current_lba, self.backup_lba)
139 } else {
140 (self.backup_lba, self.current_lba)
141 };
142
143 ensure!(
144 self.first_usable >= first_lba + partition_table_blocks,
145 "Invalid first_usable {}",
146 self.first_usable
147 );
148 ensure!(
149 self.first_usable <= self.last_usable
150 && self.last_usable + partition_table_blocks <= second_lba,
151 "Invalid last_usable {}",
152 self.last_usable
153 );
154
155 if first_lba == self.current_lba {
156 ensure!(self.part_start == first_lba + 1, "Invalid part_start {}", self.part_start);
157 } else {
158 ensure!(
159 self.part_start == self.last_usable + 1,
160 "Invalid part_start {}",
161 self.part_start
162 );
163 }
164
165 Ok(())
166 }
167}
168
169#[derive(Clone, Debug, Eq, PartialEq, Immutable, IntoBytes, KnownLayout, FromBytes)]
170#[repr(C)]
171pub struct PartitionTableEntry {
172 pub type_guid: [u8; 16],
173 pub instance_guid: [u8; 16],
174 pub first_lba: u64,
175 pub last_lba: u64,
176 pub flags: u64,
177 pub name: [u16; 36],
178}
179
180impl PartitionTableEntry {
181 pub fn is_empty(&self) -> bool {
182 self.as_bytes().iter().all(|b| *b == 0)
183 }
184
185 pub fn empty() -> Self {
186 Self {
187 type_guid: [0u8; 16],
188 instance_guid: [0u8; 16],
189 first_lba: 0,
190 last_lba: 0,
191 flags: 0,
192 name: [0u16; 36],
193 }
194 }
195
196 pub fn ensure_integrity(&self) -> Result<(), Error> {
197 ensure!(self.type_guid != [0u8; 16], "Empty type GUID");
198 ensure!(self.instance_guid != [0u8; 16], "Empty instance GUID");
199 ensure!(self.first_lba != 0, "Invalid first LBA");
200 ensure!(self.last_lba != 0 && self.last_lba >= self.first_lba, "Invalid last LBA");
201 Ok(())
202 }
203}
204
205#[derive(Eq, thiserror::Error, Clone, Debug, PartialEq)]
206pub enum FormatError {
207 #[error("Invalid arguments")]
208 InvalidArguments,
209 #[error("No space")]
210 NoSpace,
211}
212
213pub fn serialize_partition_table(
217 header: &mut Header,
218 block_size: usize,
219 num_blocks: u64,
220 entries: &[PartitionTableEntry],
221) -> Result<Vec<u8>, FormatError> {
222 let crc_algo = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
223 let mut digest = crc_algo.digest();
224 let partition_table_len = header.part_size as usize * entries.len();
225 let partition_table_len = partition_table_len
226 .checked_next_multiple_of(block_size)
227 .ok_or(FormatError::InvalidArguments)?;
228 let partition_table_blocks = (partition_table_len / block_size) as u64;
229 let mut partition_table = vec![0u8; partition_table_len];
230 let mut partition_table_view = &mut partition_table[..];
231 let first_usable = partition_table_blocks + 2;
233 let last_usable = num_blocks.saturating_sub(partition_table_blocks + 2);
236 if first_usable > last_usable {
237 return Err(FormatError::NoSpace);
238 }
239 let mut used_ranges = vec![0..first_usable, last_usable + 1..num_blocks];
240 let part_size = header.part_size as usize;
241 for entry in entries {
242 let part_raw = entry.as_bytes();
243 assert!(part_raw.len() == part_size);
244 if !entry.is_empty() {
245 entry.ensure_integrity().map_err(|_| FormatError::InvalidArguments)?;
246 used_ranges.push(entry.first_lba..entry.last_lba + 1);
247 partition_table_view[..part_raw.len()].copy_from_slice(part_raw);
248 }
249 digest.update(part_raw);
250 partition_table_view = &mut partition_table_view[part_size..];
251 }
252 used_ranges.sort_by_key(|range| range.start);
253 for ranges in used_ranges.windows(2) {
254 if ranges[0].end > ranges[1].start {
255 return Err(FormatError::InvalidArguments);
256 }
257 }
258 header.first_usable = first_usable;
259 header.last_usable = last_usable;
260 header.num_parts = entries.len() as u32;
261 header.crc32_parts = digest.finalize();
262 header.crc32 = header.compute_checksum();
263 Ok(partition_table)
264}
265
266#[cfg(test)]
267mod tests {
268 use super::{GPT_HEADER_SIZE, Header};
269
270 #[fuchsia::test]
271 fn header_crc() {
272 let nblocks = 8;
273 let partition_table_nblocks = 1;
274 let mut header = Header {
275 signature: [0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54],
276 revision: 0x10000,
277 header_size: GPT_HEADER_SIZE as u32,
278 crc32: 0,
279 reserved: 0,
280 current_lba: 1,
281 backup_lba: nblocks - 1,
282 first_usable: 2 + partition_table_nblocks,
283 last_usable: nblocks - (2 + partition_table_nblocks),
284 disk_guid: [0u8; 16],
285 part_start: 2,
286 num_parts: 1,
287 part_size: 128,
288 crc32_parts: 0,
289 zerocopy_padding: 0,
290 };
291 header.crc32 = header.compute_checksum();
292
293 header.ensure_integrity(nblocks, 512).expect("Header should be valid");
294
295 header.num_parts = 2;
297
298 header.ensure_integrity(nblocks, 512).expect_err("Header should be invalid");
299 }
300}