Skip to content

Commit e567622

Browse files
authored
Merge pull request #535 from Wasabi375/disk_image
Expose data for custom boot image creation
2 parents e791ee5 + dc8b3cf commit e567622

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/lib.rs

Lines changed: 24 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,
@@ -136,13 +138,10 @@ impl DiskImageBuilder {
136138
#[cfg(feature = "uefi")]
137139
/// Create a GPT disk image for booting on UEFI systems.
138140
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)
141+
let fat_partition = NamedTempFile::new().context("failed to create temp file")?;
142+
self.create_uefi_fat_partition(fat_partition.path())
145143
.context("failed to create FAT partition")?;
144+
146145
gpt::create_gpt_disk(fat_partition.path(), image_path)
147146
.context("failed to create UEFI GPT disk image")?;
148147
fat_partition
@@ -152,6 +151,19 @@ impl DiskImageBuilder {
152151
Ok(())
153152
}
154153

154+
#[cfg(feature = "uefi")]
155+
/// Create a bootable FAT partition for a UEFI system.
156+
pub fn create_uefi_fat_partition(&self, partition_path: &Path) -> anyhow::Result<()> {
157+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
158+
159+
let mut internal_files = BTreeMap::new();
160+
internal_files.insert(UEFI_BOOT_FILENAME, FileDataSource::Bytes(UEFI_BOOTLOADER));
161+
self.create_fat_filesystem_image(internal_files, partition_path)
162+
.context("failed to create FAT partition")?;
163+
164+
Ok(())
165+
}
166+
155167
#[cfg(feature = "uefi")]
156168
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
157169
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
@@ -198,7 +210,8 @@ impl DiskImageBuilder {
198210
fn create_fat_filesystem_image(
199211
&self,
200212
internal_files: BTreeMap<&str, FileDataSource>,
201-
) -> anyhow::Result<NamedTempFile> {
213+
partition_path: &Path,
214+
) -> anyhow::Result<()> {
202215
let mut local_map: BTreeMap<&str, _> = BTreeMap::new();
203216

204217
for (name, source) in &self.files {
@@ -214,10 +227,9 @@ impl DiskImageBuilder {
214227
}
215228
}
216229

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

221-
Ok(out_file)
233+
Ok(())
222234
}
223235
}

0 commit comments

Comments
 (0)