Skip to content

Commit 4ac2ce8

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

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/lib.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,11 @@ impl DiskImageBuilder {
109109
#[cfg(feature = "bios")]
110110
/// Create an MBR disk image for booting on BIOS systems.
111111
pub fn create_bios_image(&self, image_path: &Path) -> anyhow::Result<()> {
112-
const BIOS_STAGE_3_NAME: &str = "boot-stage-3";
113-
const BIOS_STAGE_4_NAME: &str = "boot-stage-4";
114-
let stage_3 = FileDataSource::Bytes(BIOS_STAGE_3);
115-
let stage_4 = FileDataSource::Bytes(BIOS_STAGE_4);
116-
let mut internal_files = BTreeMap::new();
117-
internal_files.insert(BIOS_STAGE_3_NAME, stage_3);
118-
internal_files.insert(BIOS_STAGE_4_NAME, stage_4);
119-
let fat_partition = self
120-
.create_fat_filesystem_image(internal_files)
112+
let fat_partition = NamedTempFile::new().context("failed to create temp file")?;
113+
114+
self.create_bios_fat_partition(fat_partition.path())
121115
.context("failed to create FAT partition")?;
116+
122117
mbr::create_mbr_disk(
123118
BIOS_BOOT_SECTOR,
124119
BIOS_STAGE_2,
@@ -133,16 +128,33 @@ impl DiskImageBuilder {
133128
Ok(())
134129
}
135130

131+
#[cfg(feature = "bios")]
132+
/// create a bootable fat-partition for a BIOS system.
133+
///
134+
/// Note that for the partition to work as a boot-partition [BIOS_BOOT_SECTOR] and [BIOS_STAGE_2]
135+
/// have to be properly loaded into the MBR disk image.
136+
pub fn create_bios_fat_partition(&self, partition_path: &Path) -> anyhow::Result<()> {
137+
const BIOS_STAGE_3_NAME: &str = "boot-stage-3";
138+
const BIOS_STAGE_4_NAME: &str = "boot-stage-4";
139+
let stage_3 = FileDataSource::Bytes(BIOS_STAGE_3);
140+
let stage_4 = FileDataSource::Bytes(BIOS_STAGE_4);
141+
let mut internal_files = BTreeMap::new();
142+
internal_files.insert(BIOS_STAGE_3_NAME, stage_3);
143+
internal_files.insert(BIOS_STAGE_4_NAME, stage_4);
144+
145+
self.create_fat_filesystem_image(internal_files, partition_path)
146+
.context("failed to create FAT partition")?;
147+
148+
Ok(())
149+
}
150+
136151
#[cfg(feature = "uefi")]
137152
/// Create a GPT disk image for booting on UEFI systems.
138153
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)
154+
let fat_partition = NamedTempFile::new().context("failed to create temp file")?;
155+
self.create_uefi_fat_partition(fat_partition.path())
145156
.context("failed to create FAT partition")?;
157+
146158
gpt::create_gpt_disk(fat_partition.path(), image_path)
147159
.context("failed to create UEFI GPT disk image")?;
148160
fat_partition
@@ -152,6 +164,19 @@ impl DiskImageBuilder {
152164
Ok(())
153165
}
154166

167+
#[cfg(feature = "uefi")]
168+
/// create a bootable fat-partition for a UEFI system.
169+
pub fn create_uefi_fat_partition(&self, partition_path: &Path) -> anyhow::Result<()> {
170+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
171+
172+
let mut internal_files = BTreeMap::new();
173+
internal_files.insert(UEFI_BOOT_FILENAME, FileDataSource::Bytes(UEFI_BOOTLOADER));
174+
self.create_fat_filesystem_image(internal_files, partition_path)
175+
.context("failed to create FAT partition")?;
176+
177+
Ok(())
178+
}
179+
155180
#[cfg(feature = "uefi")]
156181
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
157182
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
@@ -198,7 +223,8 @@ impl DiskImageBuilder {
198223
fn create_fat_filesystem_image(
199224
&self,
200225
internal_files: BTreeMap<&str, FileDataSource>,
201-
) -> anyhow::Result<NamedTempFile> {
226+
partition_path: &Path,
227+
) -> anyhow::Result<()> {
202228
let mut local_map: BTreeMap<&str, _> = BTreeMap::new();
203229

204230
for (name, source) in &self.files {
@@ -214,10 +240,9 @@ impl DiskImageBuilder {
214240
}
215241
}
216242

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

221-
Ok(out_file)
246+
Ok(())
222247
}
223248
}

0 commit comments

Comments
 (0)