Skip to content

Commit 2824a46

Browse files
committed
extract fat-partition creation into separate pub fn
1 parent 62884a3 commit 2824a46

1 file changed

Lines changed: 44 additions & 12 deletions

File tree

src/lib.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ impl DiskImageBuilder {
111111
pub fn create_bios_image(&self, image_path: &Path) -> anyhow::Result<()> {
112112
const BIOS_STAGE_3_NAME: &str = "boot-stage-3";
113113
const BIOS_STAGE_4_NAME: &str = "boot-stage-4";
114+
115+
let fat_partition = NamedTempFile::new().context("failed to create temp file")?;
116+
114117
let stage_3 = FileDataSource::Bytes(BIOS_STAGE_3);
115118
let stage_4 = FileDataSource::Bytes(BIOS_STAGE_4);
116119
let mut internal_files = BTreeMap::new();
117120
internal_files.insert(BIOS_STAGE_3_NAME, stage_3);
118121
internal_files.insert(BIOS_STAGE_4_NAME, stage_4);
119-
let fat_partition = self
120-
.create_fat_filesystem_image(internal_files)
122+
self.create_fat_filesystem_image(internal_files, fat_partition.path())
121123
.context("failed to create FAT partition")?;
122124
mbr::create_mbr_disk(
123125
BIOS_BOOT_SECTOR,
@@ -133,16 +135,33 @@ impl DiskImageBuilder {
133135
Ok(())
134136
}
135137

138+
#[cfg(feature = "bios")]
139+
/// Create a bootable fat-partition for a BIOS system.
140+
///
141+
/// Note that for the partition to work as a boot-partition [BIOS_BOOT_SECTOR] and [BIOS_STAGE_2]
142+
/// have to be properly loaded into the MBR disk image.
143+
pub fn create_bios_fat_partition(&self, partition_path: &Path) -> anyhow::Result<()> {
144+
const BIOS_STAGE_3_NAME: &str = "boot-stage-3";
145+
const BIOS_STAGE_4_NAME: &str = "boot-stage-4";
146+
let stage_3 = FileDataSource::Bytes(BIOS_STAGE_3);
147+
let stage_4 = FileDataSource::Bytes(BIOS_STAGE_4);
148+
let mut internal_files = BTreeMap::new();
149+
internal_files.insert(BIOS_STAGE_3_NAME, stage_3);
150+
internal_files.insert(BIOS_STAGE_4_NAME, stage_4);
151+
152+
self.create_fat_filesystem_image(internal_files, partition_path)
153+
.context("failed to create FAT partition")?;
154+
155+
Ok(())
156+
}
157+
136158
#[cfg(feature = "uefi")]
137159
/// Create a GPT disk image for booting on UEFI systems.
138160
pub fn create_uefi_image(&self, image_path: &Path) -> anyhow::Result<()> {
139-
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
140-
141-
let mut internal_files = BTreeMap::new();
142-
internal_files.insert(UEFI_BOOT_FILENAME, FileDataSource::Bytes(UEFI_BOOTLOADER));
143-
let fat_partition = self
144-
.create_fat_filesystem_image(internal_files)
161+
let fat_partition = NamedTempFile::new().context("failed to create temp file")?;
162+
self.create_uefi_fat_partition(fat_partition.path())
145163
.context("failed to create FAT partition")?;
164+
146165
gpt::create_gpt_disk(fat_partition.path(), image_path)
147166
.context("failed to create UEFI GPT disk image")?;
148167
fat_partition
@@ -152,6 +171,19 @@ impl DiskImageBuilder {
152171
Ok(())
153172
}
154173

174+
#[cfg(feature = "uefi")]
175+
/// create a bootable fat-partition for a UEFI system.
176+
pub fn create_uefi_fat_partition(&self, partition_path: &Path) -> anyhow::Result<()> {
177+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
178+
179+
let mut internal_files = BTreeMap::new();
180+
internal_files.insert(UEFI_BOOT_FILENAME, FileDataSource::Bytes(UEFI_BOOTLOADER));
181+
self.create_fat_filesystem_image(internal_files, partition_path)
182+
.context("failed to create FAT partition")?;
183+
184+
Ok(())
185+
}
186+
155187
#[cfg(feature = "uefi")]
156188
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
157189
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
@@ -198,7 +230,8 @@ impl DiskImageBuilder {
198230
fn create_fat_filesystem_image(
199231
&self,
200232
internal_files: BTreeMap<&str, FileDataSource>,
201-
) -> anyhow::Result<NamedTempFile> {
233+
partition_path: &Path,
234+
) -> anyhow::Result<()> {
202235
let mut local_map: BTreeMap<&str, _> = BTreeMap::new();
203236

204237
for (name, source) in &self.files {
@@ -214,10 +247,9 @@ impl DiskImageBuilder {
214247
}
215248
}
216249

217-
let out_file = NamedTempFile::new().context("failed to create temp file")?;
218-
fat::create_fat_filesystem(local_map, out_file.path())
250+
fat::create_fat_filesystem(local_map, partition_path)
219251
.context("failed to create FAT filesystem")?;
220252

221-
Ok(out_file)
253+
Ok(())
222254
}
223255
}

0 commit comments

Comments
 (0)