1use crate::BootloaderType;
6use anyhow::{Context as _, Error};
7use block_client::{BlockClient, MutableBufferSlice, RemoteBlockClient};
8use fidl::endpoints::Proxy;
9use fidl_fuchsia_mem::Buffer;
10use fidl_fuchsia_paver::{Asset, Configuration, DynamicDataSinkProxy};
11use fidl_fuchsia_storage_block::{BlockMarker, BlockProxy};
12
13use recovery_util_block::BlockDevice;
14use std::cmp::min;
15use std::fmt;
16
17#[derive(Debug, PartialEq)]
18pub enum PartitionPaveType {
19 Asset { r#type: Asset, config: Configuration },
20 Volume,
21 Bootloader,
22}
23
24pub struct Partition {
26 pave_type: PartitionPaveType,
27 src: String,
28 size: u64,
29 block_size: u64,
30}
31
32static WORKSTATION_INSTALLER_GPT: [u8; 16] = [
36 0xce, 0x98, 0xce, 0x4d, 0x7e, 0xe7, 0xc1, 0x45, 0xa8, 0x63, 0xca, 0xf9, 0x2f, 0x13, 0x30, 0xc1,
37];
38
39static WORKSTATION_PARTITION_GPTS: [[u8; 16]; 5] = [
43 [
44 0xfe, 0x94, 0xce, 0x5e, 0x86, 0x4c, 0xe8, 0x11, 0xa1, 0x5b, 0x48, 0x0f, 0xcf, 0x35, 0xf8,
45 0xe6,
46 ], [
48 0x6b, 0xe1, 0x09, 0xa4, 0xaa, 0x78, 0xcc, 0x4a, 0x5c, 0x99, 0x41, 0x1a, 0x62, 0x52, 0x23,
49 0x30,
50 ], [
52 0xf6, 0xff, 0x37, 0x9b, 0x58, 0x2e, 0x6a, 0x46, 0x3a, 0x98, 0xe0, 0x04, 0x0b, 0x6d, 0x92,
53 0xf7,
54 ], [
56 0xf6, 0xff, 0x37, 0x9b, 0x58, 0x2e, 0x6a, 0x46, 0x3a, 0x98, 0xe0, 0x04, 0x0b, 0x6d, 0x92,
57 0xf7,
58 ], [
60 0xf6, 0xff, 0x37, 0x9b, 0x58, 0x2e, 0x6a, 0x46, 0x3a, 0x98, 0xe0, 0x04, 0x0b, 0x6d, 0x92,
61 0xf7,
62 ], ];
64
65impl Partition {
66 async fn new(
75 src: String,
76 part: BlockProxy,
77 bootloader: BootloaderType,
78 ) -> Result<Option<Self>, Error> {
79 let (status, guid) = part.get_type_guid().await.context("Get type guid failed")?;
80 if let None = guid {
81 return Err(Error::new(zx::Status::from_raw(status)));
82 }
83
84 let (_status, name) = part.get_name().await.context("Get name failed")?;
85 let pave_type;
86 if let Some(string) = name {
87 let guid = guid.unwrap();
88 if guid.value != WORKSTATION_INSTALLER_GPT
89 && !(src.contains("usb-bus") && WORKSTATION_PARTITION_GPTS.contains(&guid.value))
90 {
91 return Ok(None);
92 }
93 if string == "storage-sparse" {
95 pave_type = Some(PartitionPaveType::Volume);
96 } else if bootloader == BootloaderType::Efi {
97 pave_type = Partition::get_efi_pave_type(&string.to_lowercase());
98 } else if bootloader == BootloaderType::Coreboot {
99 pave_type = Partition::get_coreboot_pave_type(&string);
100 } else {
101 pave_type = None;
102 }
103 } else {
104 return Ok(None);
105 }
106
107 if let Some(pave_type) = pave_type {
108 let info =
109 part.get_info().await.context("Get info failed")?.map_err(zx::Status::from_raw)?;
110 let block_size = info.block_size.into();
111 let size = info.block_count * block_size;
112
113 Ok(Some(Partition { pave_type, src, size, block_size }))
114 } else {
115 Ok(None)
116 }
117 }
118
119 fn get_efi_pave_type(label: &str) -> Option<PartitionPaveType> {
120 if label.starts_with("zircon_") && label.len() == "zircon_x".len() {
121 let configuration = Partition::letter_to_configuration(label.chars().last().unwrap());
122 Some(PartitionPaveType::Asset { r#type: Asset::Kernel, config: configuration })
123 } else if label.starts_with("vbmeta_") && label.len() == "vbmeta_x".len() {
124 let configuration = Partition::letter_to_configuration(label.chars().last().unwrap());
125 Some(PartitionPaveType::Asset {
126 r#type: Asset::VerifiedBootMetadata,
127 config: configuration,
128 })
129 } else if label.starts_with("efi")
130 || label.starts_with("fuchsia.esp")
131 || label.starts_with("bootloader")
132 {
133 Some(PartitionPaveType::Bootloader)
134 } else {
135 None
136 }
137 }
138
139 fn get_coreboot_pave_type(label: &str) -> Option<PartitionPaveType> {
140 if let Ok(re) = regex_lite::Regex::new(r"^zircon_(.)\.signed$") {
141 if let Some(captures) = re.captures(label) {
142 let config = Partition::letter_to_configuration(
143 captures.get(1).unwrap().as_str().chars().last().unwrap(),
144 );
145 Some(PartitionPaveType::Asset { r#type: Asset::Kernel, config: config })
146 } else {
147 None
148 }
149 } else {
150 None
151 }
152 }
153
154 pub async fn get_partitions(
162 block_device: &BlockDevice,
163 all_devices: &Vec<BlockDevice>,
164 bootloader: BootloaderType,
165 ) -> Result<Vec<Self>, Error> {
166 let mut partitions = Vec::new();
167
168 for entry in all_devices {
169 if !entry.topo_path.starts_with(&block_device.topo_path) || entry == block_device {
170 continue;
173 }
174 let (local, remote) = zx::Channel::create();
175 fdio::service_connect(&entry.class_path, remote).context("Connecting to partition")?;
176 let local = fidl::AsyncChannel::from_channel(local);
177
178 let proxy = BlockProxy::from_channel(local);
179 if let Some(partition) = Partition::new(entry.class_path.clone(), proxy, bootloader)
180 .await
181 .context(format!(
182 "Creating partition for block device at {} ({})",
183 entry.topo_path, entry.class_path
184 ))?
185 {
186 partitions.push(partition);
187 }
188 }
189 Ok(partitions)
190 }
191
192 pub async fn pave<F>(
194 &self,
195 data_sink: &DynamicDataSinkProxy,
196 progress_callback: &F,
197 ) -> Result<(), Error>
198 where
199 F: Send + Sync + Fn(usize, usize) -> (),
200 {
201 match self.pave_type {
202 PartitionPaveType::Asset { r#type: asset, config } => {
203 let fidl_buf = self.read_data().await?;
204 data_sink.write_asset(config, asset, fidl_buf).await?;
205 }
206 PartitionPaveType::Bootloader => {
207 let fidl_buf = self.read_data().await?;
208 data_sink.write_firmware(Configuration::A, "", fidl_buf).await?;
210 }
211 PartitionPaveType::Volume => {
212 self.pave_volume(data_sink, progress_callback).await?;
213 }
214 };
215 Ok(())
216 }
217
218 async fn pave_volume<F>(
219 &self,
220 _data_sink: &DynamicDataSinkProxy,
221 _progress_callback: &F,
222 ) -> Result<(), Error>
223 where
224 F: Send + Sync + Fn(usize, usize) -> (),
225 {
226 Err(Error::from(zx::Status::NOT_SUPPORTED))
227 }
228
229 pub async fn pave_b(&self, data_sink: &DynamicDataSinkProxy) -> Result<(), Error> {
232 if !self.is_ab() {
233 return Err(Error::from(zx::Status::NOT_SUPPORTED));
234 }
235
236 let fidl_buf = self.read_data().await?;
237 match self.pave_type {
238 PartitionPaveType::Asset { r#type: asset, config: _ } => {
239 data_sink.write_asset(Configuration::B, asset, fidl_buf).await?;
243 Ok(())
244 }
245 _ => Err(Error::from(zx::Status::NOT_SUPPORTED)),
246 }
247 }
248
249 pub fn is_ab(&self) -> bool {
251 if let PartitionPaveType::Asset { r#type: _, config } = self.pave_type {
252 return config == Configuration::A;
255 }
256 return false;
257 }
258
259 async fn read_data(&self) -> Result<Buffer, Error> {
261 let mut rounded_size = self.size;
262 let page_size = u64::from(zx::system_get_page_size());
263 if rounded_size % page_size != 0 {
264 rounded_size += page_size;
265 rounded_size -= rounded_size % page_size;
266 }
267
268 let vmo = zx::Vmo::create_with_opts(zx::VmoOptions::RESIZABLE, rounded_size)?;
269
270 let proxy =
271 fuchsia_component::client::connect_to_protocol_at_path::<BlockMarker>(&self.src)
272 .with_context(|| format!("Connecting to block device {}", &self.src))?;
273 let block_device = RemoteBlockClient::new(proxy).await?;
274 let vmo_id = block_device.attach_vmo(&vmo).await?;
275
276 let max_read_length: u64 = self.block_size * 100;
278 let mut read: u64 = 0;
279 while read < self.size {
280 let read_size = min(self.size - read, max_read_length);
281 if let Err(e) = block_device
282 .read_at(MutableBufferSlice::new_with_vmo_id(&vmo_id, read, read_size), read)
283 .await
284 .context("Reading from partition to VMO")
285 {
286 block_device.detach_vmo(vmo_id).await?;
288 return Err(e);
289 }
290
291 read += read_size;
292 }
293
294 block_device.detach_vmo(vmo_id).await?;
295
296 return Ok(Buffer { vmo: fidl::Vmo::from(vmo), size: self.size });
297 }
298
299 fn letter_to_configuration(letter: char) -> Configuration {
303 match letter {
306 'A' | 'a' => Configuration::A,
307 'B' | 'b' => Configuration::A,
308 'R' | 'r' => Configuration::Recovery,
309 _ => Configuration::A,
310 }
311 }
312}
313
314impl fmt::Debug for Partition {
315 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
316 match self.pave_type {
317 PartitionPaveType::Asset { r#type, config } => write!(
318 f,
319 "Partition[src={}, pave_type={:?}, asset={:?}, config={:?}]",
320 self.src, self.pave_type, r#type, config
321 ),
322 _ => write!(f, "Partition[src={}, pave_type={:?}]", self.src, self.pave_type),
323 }
324 }
325}
326
327#[cfg(test)]
328mod tests {
329 use super::*;
330 use fidl_fuchsia_storage_block::{
331 BlockInfo, BlockMarker, BlockRequest, BlockRequestStream, DeviceFlag, Guid,
332 };
333 use fuchsia_async as fasync;
334 use futures::{TryFutureExt, TryStreamExt};
335
336 async fn serve_partition(
337 label: &str,
338 block_size: u32,
339 block_count: u64,
340 guid: [u8; 16],
341 mut stream: BlockRequestStream,
342 ) -> Result<(), Error> {
343 while let Some(req) = stream.try_next().await? {
344 match req {
345 BlockRequest::GetName { responder } => responder.send(0, Some(label))?,
346 BlockRequest::GetInfo { responder } => responder.send(Ok(&BlockInfo {
347 block_count,
348 block_size,
349 max_transfer_size: 0,
350 flags: DeviceFlag::empty(),
351 }))?,
352 BlockRequest::GetTypeGuid { responder } => {
353 responder.send(0, Some(&Guid { value: guid }))?
354 }
355 _ => panic!("Expected a GetInfo/GetName request, but did not get one."),
356 }
357 }
358 Ok(())
359 }
360
361 fn mock_partition(
362 label: &'static str,
363 block_size: usize,
364 block_count: usize,
365 guid: [u8; 16],
366 ) -> Result<BlockProxy, Error> {
367 let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<BlockMarker>();
368 fasync::Task::local(
369 serve_partition(
370 label,
371 block_size.try_into().unwrap(),
372 block_count.try_into().unwrap(),
373 guid,
374 stream,
375 )
376 .unwrap_or_else(|e| panic!("Error while serving fake block device: {}", e)),
377 )
378 .detach();
379 Ok(proxy)
380 }
381
382 #[fasync::run_singlethreaded(test)]
383 async fn test_new_partition_bad_guid() -> Result<(), Error> {
384 let proxy = mock_partition("zircon_a", 512, 1000, [0xaa; 16])?;
385 let part = Partition::new("zircon_a".to_string(), proxy, BootloaderType::Efi).await?;
386 assert!(part.is_none());
387 Ok(())
388 }
389
390 #[fasync::run_singlethreaded(test)]
391 async fn test_new_partition_zircona() -> Result<(), Error> {
392 let proxy = mock_partition("zircon_a", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
393 let part = Partition::new("zircon_a".to_string(), proxy, BootloaderType::Efi).await?;
394 assert!(part.is_some());
395 let part = part.unwrap();
396 assert_eq!(
397 part.pave_type,
398 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::A }
399 );
400 assert_eq!(part.size, 512 * 1000);
401 assert_eq!(part.src, "zircon_a");
402 assert!(part.is_ab());
403 Ok(())
404 }
405
406 #[fasync::run_singlethreaded(test)]
407 async fn test_new_partition_zirconb() -> Result<(), Error> {
408 let proxy = mock_partition("zircon_b", 20, 1000, WORKSTATION_INSTALLER_GPT)?;
409 let part = Partition::new("zircon_b".to_string(), proxy, BootloaderType::Efi).await?;
410 assert!(part.is_some());
411 let part = part.unwrap();
412 assert_eq!(
413 part.pave_type,
414 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::A }
415 );
416 assert_eq!(part.size, 20 * 1000);
417 assert_eq!(part.src, "zircon_b");
418 assert!(part.is_ab());
419 Ok(())
420 }
421
422 #[fasync::run_singlethreaded(test)]
423 async fn test_new_partition_zirconr() -> Result<(), Error> {
424 let proxy = mock_partition("zircon_r", 40, 200, WORKSTATION_INSTALLER_GPT)?;
425 let part = Partition::new("zircon_r".to_string(), proxy, BootloaderType::Efi).await?;
426 assert!(part.is_some());
427 let part = part.unwrap();
428 assert_eq!(
429 part.pave_type,
430 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::Recovery }
431 );
432 assert_eq!(part.size, 40 * 200);
433 assert_eq!(part.src, "zircon_r");
434 assert!(!part.is_ab());
435 Ok(())
436 }
437
438 async fn new_partition_vbmetax_test_helper(
439 name: &'static str,
440 expected_config: Configuration,
441 ) -> Result<(), Error> {
442 let proxy = mock_partition(name, 40, 200, WORKSTATION_INSTALLER_GPT)?;
443 let part = Partition::new(name.to_string(), proxy, BootloaderType::Efi).await?;
444 assert!(part.is_some());
445 let part = part.unwrap();
446 assert_eq!(
447 part.pave_type,
448 PartitionPaveType::Asset {
449 r#type: Asset::VerifiedBootMetadata,
450 config: expected_config
451 }
452 );
453 assert_eq!(part.size, 40 * 200);
454 assert_eq!(part.src, name);
455 Ok(())
456 }
457
458 #[fasync::run_singlethreaded(test)]
459 async fn test_new_partition_vbmetaa() -> Result<(), Error> {
460 new_partition_vbmetax_test_helper("vbmeta_a", Configuration::A).await
461 }
462
463 #[fasync::run_singlethreaded(test)]
464 async fn test_new_partition_vbmetab() -> Result<(), Error> {
465 new_partition_vbmetax_test_helper("vbmeta_b", Configuration::A).await
468 }
469
470 #[fasync::run_singlethreaded(test)]
471 async fn test_new_partition_vbmetar() -> Result<(), Error> {
472 new_partition_vbmetax_test_helper("vbmeta_r", Configuration::Recovery).await
473 }
474
475 #[fasync::run_singlethreaded(test)]
476 async fn test_new_partition_efi() -> Result<(), Error> {
477 let proxy = mock_partition("efi", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
478 let part = Partition::new("efi".to_string(), proxy, BootloaderType::Efi).await?;
479 assert!(part.is_some());
480 let part = part.unwrap();
481 assert_eq!(part.pave_type, PartitionPaveType::Bootloader);
482 assert_eq!(part.size, 512 * 1000);
483 assert_eq!(part.src, "efi");
484 assert!(!part.is_ab());
485 Ok(())
486 }
487
488 #[fasync::run_singlethreaded(test)]
489 async fn test_new_partition_fvm() -> Result<(), Error> {
490 let proxy = mock_partition("storage-sparse", 2048, 4097, WORKSTATION_INSTALLER_GPT)?;
491 let part = Partition::new("storage-sparse".to_string(), proxy, BootloaderType::Efi).await?;
492 assert!(part.is_some());
493 let part = part.unwrap();
494 assert_eq!(part.pave_type, PartitionPaveType::Volume);
495 assert_eq!(part.size, 2048 * 4097);
496 assert_eq!(part.src, "storage-sparse");
497 assert!(!part.is_ab());
498 Ok(())
499 }
500
501 #[fasync::run_singlethreaded(test)]
502 async fn test_zircona_unsigned_coreboot() -> Result<(), Error> {
503 let proxy = mock_partition("zircon_a", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
504 let part = Partition::new("zircon_a".to_string(), proxy, BootloaderType::Coreboot).await?;
505 assert!(part.is_none());
506 Ok(())
507 }
508
509 #[fasync::run_singlethreaded(test)]
510 async fn test_zircona_signed_coreboot() -> Result<(), Error> {
511 let proxy = mock_partition("zircon_a.signed", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
512 let part =
513 Partition::new("zircon_a.signed".to_string(), proxy, BootloaderType::Coreboot).await?;
514 assert!(part.is_some());
515 let part = part.unwrap();
516 assert_eq!(
517 part.pave_type,
518 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::A }
519 );
520 assert_eq!(part.size, 512 * 1000);
521 assert_eq!(part.src, "zircon_a.signed");
522 assert!(part.is_ab());
523 Ok(())
524 }
525
526 #[fasync::run_singlethreaded(test)]
527 async fn test_new_partition_unknown() -> Result<(), Error> {
528 let proxy = mock_partition("unknown-label", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
529 let part = Partition::new("unknown-label".to_string(), proxy, BootloaderType::Efi).await?;
530 assert!(part.is_none());
531 Ok(())
532 }
533
534 #[fasync::run_singlethreaded(test)]
535 async fn test_new_partition_zedboot_efi() -> Result<(), Error> {
536 let proxy = mock_partition("zedboot-efi", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
537 let part = Partition::new("zedboot-efi".to_string(), proxy, BootloaderType::Efi).await?;
538 assert!(part.is_none());
539 Ok(())
540 }
541
542 #[fasync::run_singlethreaded(test)]
543 async fn test_invalid_partitions_coreboot() -> Result<(), Error> {
544 let proxy = mock_partition("zircon_.signed", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
545 let part =
546 Partition::new("zircon_.signed".to_string(), proxy, BootloaderType::Coreboot).await?;
547 assert!(part.is_none());
548
549 let proxy = mock_partition("zircon_aa.signed", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
550 let part =
551 Partition::new("zircon_aa.signed".to_string(), proxy, BootloaderType::Coreboot).await?;
552 assert!(part.is_none());
553
554 Ok(())
555 }
556
557 #[fasync::run_singlethreaded(test)]
558 async fn test_invalid_partitions_efi() -> Result<(), Error> {
559 let proxy = mock_partition("zircon_", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
560 let part = Partition::new("zircon_".to_string(), proxy, BootloaderType::Efi).await?;
561 assert!(part.is_none());
562
563 let proxy = mock_partition("zircon_aa", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
564 let part = Partition::new("zircon_aa".to_string(), proxy, BootloaderType::Efi).await?;
565 assert!(part.is_none());
566
567 let proxy = mock_partition("zircon_a.signed", 512, 1000, WORKSTATION_INSTALLER_GPT)?;
568 let part =
569 Partition::new("zircon_a.signed".to_string(), proxy, BootloaderType::Efi).await?;
570 assert!(part.is_none());
571 Ok(())
572 }
573
574 #[fasync::run_singlethreaded(test)]
575 async fn test_new_partition_usb_bad_guid() -> Result<(), Error> {
576 let proxy = mock_partition("zircon_a", 512, 1000, [0xaa; 16])?;
577 let part = Partition::new("/dev/usb-bus".to_string(), proxy, BootloaderType::Efi).await?;
578 assert!(part.is_none());
579 Ok(())
580 }
581
582 #[fasync::run_singlethreaded(test)]
583 async fn test_new_partition_usb_zircona() -> Result<(), Error> {
584 let proxy = mock_partition("zircon_a", 512, 1000, WORKSTATION_PARTITION_GPTS[2])?;
585 let part = Partition::new("/dev/usb-bus".to_string(), proxy, BootloaderType::Efi).await?;
586 assert!(part.is_some());
587 let part = part.unwrap();
588 assert_eq!(
589 part.pave_type,
590 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::A }
591 );
592 assert_eq!(part.size, 512 * 1000);
593 assert_eq!(part.src, "/dev/usb-bus");
594 assert!(part.is_ab());
595 Ok(())
596 }
597
598 #[fasync::run_singlethreaded(test)]
599 async fn test_new_partition_usb_zirconb() -> Result<(), Error> {
600 let proxy = mock_partition("zircon_b", 20, 1000, WORKSTATION_PARTITION_GPTS[3])?;
601 let part = Partition::new("/dev/usb-bus".to_string(), proxy, BootloaderType::Efi).await?;
602 assert!(part.is_some());
603 let part = part.unwrap();
604 assert_eq!(
605 part.pave_type,
606 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::A }
607 );
608 assert_eq!(part.size, 20 * 1000);
609 assert_eq!(part.src, "/dev/usb-bus");
610 assert!(part.is_ab());
611 Ok(())
612 }
613
614 #[fasync::run_singlethreaded(test)]
615 async fn test_new_partition_usb_zirconr() -> Result<(), Error> {
616 let proxy = mock_partition("zircon_r", 40, 200, WORKSTATION_PARTITION_GPTS[4])?;
617 let part = Partition::new("/dev/usb-bus".to_string(), proxy, BootloaderType::Efi).await?;
618 assert!(part.is_some());
619 let part = part.unwrap();
620 assert_eq!(
621 part.pave_type,
622 PartitionPaveType::Asset { r#type: Asset::Kernel, config: Configuration::Recovery }
623 );
624 assert_eq!(part.size, 40 * 200);
625 assert_eq!(part.src, "/dev/usb-bus");
626 assert!(!part.is_ab());
627 Ok(())
628 }
629
630 #[fasync::run_singlethreaded(test)]
631 async fn test_new_partition_usb_efi() -> Result<(), Error> {
632 let proxy = mock_partition("efi-system", 512, 1000, WORKSTATION_PARTITION_GPTS[0])?;
633 let part = Partition::new("/dev/usb-bus".to_string(), proxy, BootloaderType::Efi).await?;
634 assert!(part.is_some());
635 let part = part.unwrap();
636 assert_eq!(part.pave_type, PartitionPaveType::Bootloader);
637 assert_eq!(part.size, 512 * 1000);
638 assert_eq!(part.src, "/dev/usb-bus");
639 assert!(!part.is_ab());
640 Ok(())
641 }
642}