1use std::{io, path::PathBuf};
45
46use thiserror::Error;
47
48pub(crate) mod customization;
49mod flashing;
50mod helpers;
51pub(crate) mod pal;
52
53pub use customization::{Customization, ParitionType};
54pub use flashing::flash;
55
56pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
57
58#[derive(Error, Debug)]
59pub enum Error {
61 #[error("Partition table of image not valid.")]
63 InvalidPartitionTable,
64 #[error("Only FAT BOOT partitions are supported.")]
65 InvalidBootPartition,
66 #[error("Failed to create customization {file}")]
67 CustomizationFileCreateFail {
68 #[source]
69 source: io::Error,
70 file: Box<str>,
71 },
72 #[error("Unknown Error during IO. Please check logs for more information.")]
74 IoError {
75 #[from]
76 #[source]
77 source: io::Error,
78 },
79 #[error("Aborted before completing.")]
81 Aborted,
82 #[error("Failed to format SD Card.")]
83 FailedToFormat {
84 #[source]
85 source: io::Error,
86 },
87 #[error("Failed to open SD Card.")]
88 FailedToOpenDestination {
89 #[source]
90 source: anyhow::Error,
91 },
92 #[error("Invalid bmap for the image.")]
93 InvalidBmap,
94 #[error("Writer thread has been closed.")]
95 WriterClosed,
96
97 #[cfg(windows)]
98 #[error("Failed to clear SD Card.")]
99 WindowsCleanError(std::process::Output),
100}
101
102pub fn devices(filter: bool) -> std::collections::HashSet<Device> {
104 bb_drivelist::drive_list()
105 .expect("Unsupported OS for Sd Card")
106 .into_iter()
107 .filter(|x| {
108 if filter {
109 x.is_removable && !x.is_virtual
110 } else {
111 true
112 }
113 })
114 .map(|x| Device::new(x.description, x.raw.into(), x.size.unwrap_or_default()))
115 .collect()
116}
117
118#[derive(Hash, Debug, PartialEq, Eq, Clone)]
119pub struct Device {
121 pub name: String,
122 pub path: PathBuf,
123 pub size: u64,
124}
125
126impl Device {
127 const fn new(name: String, path: PathBuf, size: u64) -> Self {
128 Self { name, path, size }
129 }
130}
131
132pub async fn format(dst: &std::path::Path) -> Result<()> {
134 crate::pal::format(dst).await
135}