Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8d2d53c

Browse files
authoredMar 12, 2023
Merge pull request #320 from jasoncouture/simplified_disk_builder
Simplified disk builder
2 parents 5a24373 + e3dd6fc commit 8d2d53c

File tree

10 files changed

+324
-237
lines changed

10 files changed

+324
-237
lines changed
 

‎build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ async fn build_bios_stage_4(out_dir: &Path) -> PathBuf {
266266
convert_elf_to_bin(elf_path).await
267267
}
268268

269+
#[cfg(not(docsrs_dummy_build))]
269270
#[cfg(feature = "bios")]
270271
async fn convert_elf_to_bin(elf_path: PathBuf) -> PathBuf {
271272
let flat_binary_path = elf_path.with_extension("bin");

‎src/bios/mod.rs

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,36 @@
1-
use crate::fat;
2-
use anyhow::Context;
3-
use bootloader_boot_config::BootConfig;
4-
use std::io::Write;
5-
use std::{
6-
collections::BTreeMap,
7-
path::{Path, PathBuf},
8-
};
9-
use tempfile::NamedTempFile;
1+
use std::path::Path;
102

11-
mod mbr;
3+
use bootloader_boot_config::BootConfig;
124

13-
const BIOS_STAGE_3: &str = "boot-stage-3";
14-
const BIOS_STAGE_4: &str = "boot-stage-4";
5+
use crate::DiskImageBuilder;
156

167
/// Create disk images for booting on legacy BIOS systems.
178
pub struct BiosBoot {
18-
kernel: PathBuf,
19-
ramdisk: Option<PathBuf>,
20-
config: Option<String>,
9+
image_builder: DiskImageBuilder,
2110
}
2211

2312
impl BiosBoot {
2413
/// Start creating a disk image for the given bootloader ELF executable.
2514
pub fn new(kernel_path: &Path) -> Self {
2615
Self {
27-
kernel: kernel_path.to_owned(),
28-
ramdisk: None,
29-
config: None,
16+
image_builder: DiskImageBuilder::new(kernel_path.to_owned()),
3017
}
3118
}
3219

3320
/// Add a ramdisk file to the image.
3421
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
35-
self.ramdisk = Some(ramdisk_path.to_owned());
22+
self.image_builder.set_ramdisk(ramdisk_path.to_owned());
3623
self
3724
}
3825

39-
/// Configures the runtime behavior of the bootloader.
26+
/// Creates a configuration file (boot.json) that configures the runtime behavior of the bootloader.
4027
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
41-
self.config = Some(serde_json::to_string(&config).expect("failed to serialize BootConfig"));
28+
self.image_builder.set_boot_config(config);
4229
self
4330
}
4431

4532
/// Create a bootable BIOS disk image at the given path.
4633
pub fn create_disk_image(&self, out_path: &Path) -> anyhow::Result<()> {
47-
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH"));
48-
let stage_2_path = Path::new(env!("BIOS_STAGE_2_PATH"));
49-
50-
let fat_partition = self
51-
.create_fat_partition()
52-
.context("failed to create FAT partition")?;
53-
54-
mbr::create_mbr_disk(
55-
bootsector_path,
56-
stage_2_path,
57-
fat_partition.path(),
58-
out_path,
59-
)
60-
.context("failed to create BIOS MBR disk image")?;
61-
62-
fat_partition
63-
.close()
64-
.context("failed to delete FAT partition after disk image creation")?;
65-
66-
Ok(())
67-
}
68-
69-
/// Creates an BIOS-bootable FAT partition with the kernel.
70-
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
71-
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
72-
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH"));
73-
74-
let mut files = BTreeMap::new();
75-
files.insert(crate::KERNEL_FILE_NAME, self.kernel.as_path());
76-
files.insert(BIOS_STAGE_3, stage_3_path);
77-
files.insert(BIOS_STAGE_4, stage_4_path);
78-
if let Some(ramdisk_path) = &self.ramdisk {
79-
files.insert(crate::RAMDISK_FILE_NAME, ramdisk_path);
80-
}
81-
82-
let mut config_file: NamedTempFile;
83-
84-
if let Some(config_ser) = &self.config {
85-
config_file = NamedTempFile::new()
86-
.context("failed to create temp file")
87-
.unwrap();
88-
writeln!(config_file, "{config_ser}")?;
89-
files.insert(crate::CONFIG_FILE_NAME, config_file.path());
90-
}
91-
92-
let out_file = NamedTempFile::new().context("failed to create temp file")?;
93-
fat::create_fat_filesystem(files, out_file.path())
94-
.context("failed to create BIOS FAT filesystem")?;
95-
96-
Ok(out_file)
34+
self.image_builder.create_bios_image(out_path)
9735
}
9836
}

‎src/fat.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
use crate::file_data_source::FileDataSource;
12
use anyhow::Context;
2-
use std::{collections::BTreeMap, fs, io, path::Path};
3+
use fatfs::Dir;
4+
use std::fs::File;
5+
use std::{collections::BTreeMap, fs, path::Path};
36

47
use crate::KERNEL_FILE_NAME;
58

69
pub fn create_fat_filesystem(
7-
files: BTreeMap<&str, &Path>,
10+
files: BTreeMap<&str, &FileDataSource>,
811
out_fat_path: &Path,
912
) -> anyhow::Result<()> {
1013
const MB: u64 = 1024 * 1024;
1114

1215
// calculate needed size
1316
let mut needed_size = 0;
14-
for path in files.values() {
15-
let file_size = fs::metadata(path)
16-
.with_context(|| format!("failed to read metadata of file `{}`", path.display()))?
17-
.len();
18-
needed_size += file_size;
17+
for source in files.values() {
18+
needed_size += source.len()?;
1919
}
2020

2121
// create new filesystem image file at the given path and set its length
@@ -31,7 +31,9 @@ pub fn create_fat_filesystem(
3131

3232
// choose a file system label
3333
let mut label = *b"MY_RUST_OS!";
34-
if let Some(path) = files.get(KERNEL_FILE_NAME) {
34+
35+
// This __should__ always be a file, but maybe not. Should we allow the caller to set the volume label instead?
36+
if let Some(FileDataSource::File(path)) = files.get(KERNEL_FILE_NAME) {
3537
if let Some(name) = path.file_stem() {
3638
let converted = name.to_string_lossy();
3739
let name = converted.as_bytes();
@@ -48,10 +50,17 @@ pub fn create_fat_filesystem(
4850
fatfs::format_volume(&fat_file, format_options).context("Failed to format FAT file")?;
4951
let filesystem = fatfs::FileSystem::new(&fat_file, fatfs::FsOptions::new())
5052
.context("Failed to open FAT file system of UEFI FAT file")?;
53+
let root_dir = filesystem.root_dir();
5154

5255
// copy files to file system
53-
let root_dir = filesystem.root_dir();
54-
for (target_path_raw, file_path) in files {
56+
add_files_to_image(&root_dir, files)
57+
}
58+
59+
pub fn add_files_to_image(
60+
root_dir: &Dir<&File>,
61+
files: BTreeMap<&str, &FileDataSource>,
62+
) -> anyhow::Result<()> {
63+
for (target_path_raw, source) in files {
5564
let target_path = Path::new(target_path_raw);
5665
// create parent directories
5766
let ancestors: Vec<_> = target_path.ancestors().skip(1).collect();
@@ -70,12 +79,14 @@ pub fn create_fat_filesystem(
7079
.create_file(target_path_raw)
7180
.with_context(|| format!("failed to create file at `{}`", target_path.display()))?;
7281
new_file.truncate().unwrap();
73-
io::copy(
74-
&mut fs::File::open(file_path)
75-
.with_context(|| format!("failed to open `{}` for copying", file_path.display()))?,
76-
&mut new_file,
77-
)
78-
.with_context(|| format!("failed to copy `{}` to FAT filesystem", file_path.display()))?;
82+
83+
source.copy_to(&mut new_file).with_context(|| {
84+
format!(
85+
"failed to copy source data `{:?}` to file at `{}`",
86+
source,
87+
target_path.display()
88+
)
89+
})?;
7990
}
8091

8192
Ok(())

‎src/file_data_source.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use alloc::vec::Vec;
2+
use anyhow::Context;
3+
use core::fmt::{Debug, Formatter};
4+
5+
use std::io::Cursor;
6+
use std::path::PathBuf;
7+
use std::{fs, io};
8+
9+
#[derive(Clone)]
10+
/// Defines a data source, either a source `std::path::PathBuf`, or a vector of bytes.
11+
pub enum FileDataSource {
12+
File(PathBuf),
13+
Data(Vec<u8>),
14+
}
15+
16+
impl Debug for FileDataSource {
17+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
18+
match self {
19+
FileDataSource::File(file) => {
20+
f.write_fmt(format_args!("data source: File {}", file.display()))
21+
}
22+
FileDataSource::Data(d) => {
23+
f.write_fmt(format_args!("data source: {} raw bytes ", d.len()))
24+
}
25+
}
26+
}
27+
}
28+
29+
impl FileDataSource {
30+
/// Get the length of the inner data source
31+
pub fn len(&self) -> anyhow::Result<u64> {
32+
Ok(match self {
33+
FileDataSource::File(path) => fs::metadata(path)
34+
.with_context(|| format!("failed to read metadata of file `{}`", path.display()))?
35+
.len(),
36+
FileDataSource::Data(v) => v.len() as u64,
37+
})
38+
}
39+
/// Copy this data source to the specified target that implements io::Write
40+
pub fn copy_to(&self, target: &mut dyn io::Write) -> anyhow::Result<()> {
41+
match self {
42+
FileDataSource::File(file_path) => {
43+
io::copy(
44+
&mut fs::File::open(file_path).with_context(|| {
45+
format!("failed to open `{}` for copying", file_path.display())
46+
})?,
47+
target,
48+
)?;
49+
}
50+
FileDataSource::Data(contents) => {
51+
let mut cursor = Cursor::new(contents);
52+
io::copy(&mut cursor, target)?;
53+
}
54+
};
55+
56+
Ok(())
57+
}
58+
}
File renamed without changes.

‎src/lib.rs

Lines changed: 206 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,223 @@ An experimental x86_64 bootloader that works on both BIOS and UEFI systems.
44

55
#![warn(missing_docs)]
66

7+
extern crate alloc;
8+
79
#[cfg(feature = "bios")]
810
mod bios;
9-
mod fat;
11+
#[cfg(feature = "uefi")]
12+
mod gpt;
13+
#[cfg(feature = "bios")]
14+
mod mbr;
1015
#[cfg(feature = "uefi")]
1116
mod uefi;
1217

18+
#[cfg(feature = "uefi")]
19+
pub use uefi::UefiBoot;
20+
1321
#[cfg(feature = "bios")]
1422
pub use bios::BiosBoot;
1523

16-
#[cfg(feature = "uefi")]
17-
pub use uefi::UefiBoot;
24+
mod fat;
25+
mod file_data_source;
1826

27+
use std::{
28+
borrow::Cow,
29+
collections::BTreeMap,
30+
path::{Path, PathBuf},
31+
};
32+
33+
use anyhow::Context;
34+
35+
use tempfile::NamedTempFile;
36+
37+
use crate::file_data_source::FileDataSource;
1938
pub use bootloader_boot_config::BootConfig;
2039

2140
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
2241
const RAMDISK_FILE_NAME: &str = "ramdisk";
2342
const CONFIG_FILE_NAME: &str = "boot.json";
43+
44+
/// Allows creating disk images for a specified set of files.
45+
///
46+
/// It can currently create `MBR` (BIOS), `GPT` (UEFI), and `TFTP` (UEFI) images.
47+
pub struct DiskImageBuilder {
48+
files: BTreeMap<Cow<'static, str>, FileDataSource>,
49+
}
50+
51+
impl DiskImageBuilder {
52+
/// Create a new instance of DiskImageBuilder, with the specified kernel.
53+
pub fn new(kernel: PathBuf) -> Self {
54+
let mut obj = Self::empty();
55+
obj.set_kernel(kernel);
56+
obj
57+
}
58+
59+
/// Create a new, empty instance of DiskImageBuilder
60+
pub fn empty() -> Self {
61+
Self {
62+
files: BTreeMap::new(),
63+
}
64+
}
65+
66+
/// Add or replace a kernel to be included in the final image.
67+
pub fn set_kernel(&mut self, path: PathBuf) -> &mut Self {
68+
self.set_file_source(KERNEL_FILE_NAME.into(), FileDataSource::File(path))
69+
}
70+
71+
/// Add or replace a ramdisk to be included in the final image.
72+
pub fn set_ramdisk(&mut self, path: PathBuf) -> &mut Self {
73+
self.set_file_source(RAMDISK_FILE_NAME.into(), FileDataSource::File(path))
74+
}
75+
76+
/// Configures the runtime behavior of the bootloader.
77+
pub fn set_boot_config(&mut self, boot_config: &BootConfig) -> &mut Self {
78+
let json = serde_json::to_vec_pretty(boot_config).expect("failed to serialize BootConfig");
79+
self.set_file_source(CONFIG_FILE_NAME.into(), FileDataSource::Data(json))
80+
}
81+
82+
/// Add a file with the specified bytes to the disk image
83+
///
84+
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
85+
/// Other files need to be loaded manually by the kernel.
86+
pub fn set_file_contents(&mut self, destination: String, data: Vec<u8>) -> &mut Self {
87+
self.set_file_source(destination.into(), FileDataSource::Data(data))
88+
}
89+
90+
/// Add a file with the specified source file to the disk image
91+
///
92+
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
93+
/// Other files need to be loaded manually by the kernel.
94+
pub fn set_file(&mut self, destination: String, file_path: PathBuf) -> &mut Self {
95+
self.set_file_source(destination.into(), FileDataSource::File(file_path))
96+
}
97+
98+
#[cfg(feature = "bios")]
99+
/// Create an MBR disk image for booting on BIOS systems.
100+
pub fn create_bios_image(&self, image_path: &Path) -> anyhow::Result<()> {
101+
const BIOS_STAGE_3: &str = "boot-stage-3";
102+
const BIOS_STAGE_4: &str = "boot-stage-4";
103+
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH"));
104+
let stage_2_path = Path::new(env!("BIOS_STAGE_2_PATH"));
105+
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
106+
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH"));
107+
let mut internal_files = BTreeMap::new();
108+
internal_files.insert(
109+
BIOS_STAGE_3,
110+
FileDataSource::File(stage_3_path.to_path_buf()),
111+
);
112+
internal_files.insert(
113+
BIOS_STAGE_4,
114+
FileDataSource::File(stage_4_path.to_path_buf()),
115+
);
116+
117+
let fat_partition = self
118+
.create_fat_filesystem_image(internal_files)
119+
.context("failed to create FAT partition")?;
120+
mbr::create_mbr_disk(
121+
bootsector_path,
122+
stage_2_path,
123+
fat_partition.path(),
124+
image_path,
125+
)
126+
.context("failed to create BIOS MBR disk image")?;
127+
128+
fat_partition
129+
.close()
130+
.context("failed to delete FAT partition after disk image creation")?;
131+
Ok(())
132+
}
133+
134+
#[cfg(feature = "uefi")]
135+
/// Create a GPT disk image for booting on UEFI systems.
136+
pub fn create_uefi_image(&self, image_path: &Path) -> anyhow::Result<()> {
137+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
138+
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
139+
let mut internal_files = BTreeMap::new();
140+
internal_files.insert(
141+
UEFI_BOOT_FILENAME,
142+
FileDataSource::File(bootloader_path.to_path_buf()),
143+
);
144+
let fat_partition = self
145+
.create_fat_filesystem_image(internal_files)
146+
.context("failed to create FAT partition")?;
147+
gpt::create_gpt_disk(fat_partition.path(), image_path)
148+
.context("failed to create UEFI GPT disk image")?;
149+
fat_partition
150+
.close()
151+
.context("failed to delete FAT partition after disk image creation")?;
152+
153+
Ok(())
154+
}
155+
156+
#[cfg(feature = "uefi")]
157+
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
158+
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
159+
use std::{fs, ops::Deref};
160+
161+
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader";
162+
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
163+
fs::create_dir_all(tftp_path)
164+
.with_context(|| format!("failed to create out dir at {}", tftp_path.display()))?;
165+
166+
let to = tftp_path.join(UEFI_TFTP_BOOT_FILENAME);
167+
fs::copy(bootloader_path, &to).with_context(|| {
168+
format!(
169+
"failed to copy bootloader from {} to {}",
170+
bootloader_path.display(),
171+
to.display()
172+
)
173+
})?;
174+
175+
for f in &self.files {
176+
let to = tftp_path.join(f.0.deref());
177+
178+
let mut new_file = fs::OpenOptions::new()
179+
.read(true)
180+
.write(true)
181+
.create(true)
182+
.truncate(true)
183+
.open(to)?;
184+
185+
f.1.copy_to(&mut new_file)?;
186+
}
187+
188+
Ok(())
189+
}
190+
191+
/// Add a file source to the disk image
192+
fn set_file_source(
193+
&mut self,
194+
destination: Cow<'static, str>,
195+
source: FileDataSource,
196+
) -> &mut Self {
197+
self.files.insert(destination, source);
198+
self
199+
}
200+
201+
fn create_fat_filesystem_image(
202+
&self,
203+
internal_files: BTreeMap<&str, FileDataSource>,
204+
) -> anyhow::Result<NamedTempFile> {
205+
let mut local_map: BTreeMap<&str, _> = BTreeMap::new();
206+
207+
for (name, source) in &self.files {
208+
local_map.insert(name, source);
209+
}
210+
211+
for k in &internal_files {
212+
if local_map.insert(k.0, k.1).is_some() {
213+
return Err(anyhow::Error::msg(format!(
214+
"Attempted to overwrite internal file: {}",
215+
k.0
216+
)));
217+
}
218+
}
219+
220+
let out_file = NamedTempFile::new().context("failed to create temp file")?;
221+
fat::create_fat_filesystem(local_map, out_file.path())
222+
.context("failed to create BIOS FAT filesystem")?;
223+
224+
Ok(out_file)
225+
}
226+
}
File renamed without changes.

‎src/uefi/mod.rs

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,37 @@
1-
use crate::fat;
2-
use anyhow::Context;
1+
use std::path::Path;
2+
33
use bootloader_boot_config::BootConfig;
4-
use std::io::Write;
5-
use std::{
6-
collections::BTreeMap,
7-
path::{Path, PathBuf},
8-
};
9-
use tempfile::NamedTempFile;
104

11-
mod gpt;
12-
mod pxe;
5+
use crate::DiskImageBuilder;
136

14-
/// Create disk images for booting on UEFI systems.
7+
/// Create disk images for booting on legacy BIOS systems.
158
pub struct UefiBoot {
16-
kernel: PathBuf,
17-
ramdisk: Option<PathBuf>,
18-
config: Option<String>,
9+
image_builder: DiskImageBuilder,
1910
}
2011

2112
impl UefiBoot {
2213
/// Start creating a disk image for the given bootloader ELF executable.
2314
pub fn new(kernel_path: &Path) -> Self {
2415
Self {
25-
kernel: kernel_path.to_owned(),
26-
ramdisk: None,
27-
config: None,
16+
image_builder: DiskImageBuilder::new(kernel_path.to_owned()),
2817
}
2918
}
3019

31-
/// Add a ramdisk file to the disk image.
20+
/// Add a ramdisk file to the image
3221
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
33-
self.ramdisk = Some(ramdisk_path.to_owned());
22+
self.image_builder.set_ramdisk(ramdisk_path.to_owned());
3423
self
3524
}
3625

37-
/// Configures the runtime behavior of the bootloader.
26+
/// Creates a configuration file (boot.json) that configures the runtime behavior of the bootloader.
3827
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
39-
self.config = Some(serde_json::to_string(&config).expect("failed to serialize BootConfig"));
28+
self.image_builder.set_boot_config(config);
4029
self
4130
}
4231

4332
/// Create a bootable UEFI disk image at the given path.
4433
pub fn create_disk_image(&self, out_path: &Path) -> anyhow::Result<()> {
45-
let fat_partition = self
46-
.create_fat_partition()
47-
.context("failed to create FAT partition")?;
48-
49-
gpt::create_gpt_disk(fat_partition.path(), out_path)
50-
.context("failed to create UEFI GPT disk image")?;
51-
52-
fat_partition
53-
.close()
54-
.context("failed to delete FAT partition after disk image creation")?;
55-
56-
Ok(())
34+
self.image_builder.create_uefi_image(out_path)
5735
}
5836

5937
/// Prepare a folder for use with booting over UEFI_PXE.
@@ -62,45 +40,6 @@ impl UefiBoot {
6240
/// DHCP server should set the filename option to that path, otherwise the
6341
/// bootloader won't be found.
6442
pub fn create_pxe_tftp_folder(&self, out_path: &Path) -> anyhow::Result<()> {
65-
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
66-
67-
pxe::create_uefi_tftp_folder(
68-
bootloader_path,
69-
self.kernel.as_path(),
70-
self.ramdisk.as_deref(),
71-
self.config.as_deref(),
72-
out_path,
73-
)
74-
.context("failed to create UEFI PXE tftp folder")?;
75-
76-
Ok(())
77-
}
78-
79-
/// Creates an UEFI-bootable FAT partition with the kernel.
80-
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
81-
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
82-
83-
let mut files = BTreeMap::new();
84-
files.insert("efi/boot/bootx64.efi", bootloader_path);
85-
files.insert(crate::KERNEL_FILE_NAME, self.kernel.as_path());
86-
if let Some(ramdisk_path) = &self.ramdisk {
87-
files.insert(crate::RAMDISK_FILE_NAME, ramdisk_path);
88-
}
89-
90-
let mut config_file: NamedTempFile;
91-
92-
if let Some(config_ser) = &self.config {
93-
config_file = NamedTempFile::new()
94-
.context("failed to create temp file")
95-
.unwrap();
96-
writeln!(config_file, "{config_ser}")?;
97-
files.insert(crate::CONFIG_FILE_NAME, config_file.path());
98-
}
99-
100-
let out_file = NamedTempFile::new().context("failed to create temp file")?;
101-
fat::create_fat_filesystem(files, out_file.path())
102-
.context("failed to create UEFI FAT filesystem")?;
103-
104-
Ok(out_file)
43+
self.image_builder.create_uefi_tftp_folder(out_path)
10544
}
10645
}

‎src/uefi/pxe.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎tests/runner/src/lib.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bootloader::BootConfig;
2+
use bootloader::DiskImageBuilder;
23
use std::{io::Read, path::Path, process::Command};
34

45
const QEMU_ARGS: &[&str] = &[
@@ -31,26 +32,20 @@ pub fn run_test_kernel_internal(
3132
config_file_path: Option<&BootConfig>,
3233
) {
3334
let kernel_path = Path::new(kernel_binary_path);
35+
let mut image_builder = DiskImageBuilder::new(kernel_path.to_owned());
36+
if let Some(rdp) = ramdisk_path {
37+
image_builder.set_ramdisk(rdp.to_owned());
38+
}
39+
if let Some(cfp) = config_file_path {
40+
image_builder.set_boot_config(cfp);
41+
}
3442

3543
#[cfg(feature = "uefi")]
3644
{
37-
// create a GPT disk image for UEFI booting
3845
let gpt_path = kernel_path.with_extension("gpt");
39-
let mut uefi_builder = bootloader::UefiBoot::new(kernel_path);
40-
// Set ramdisk for test, if supplied.
41-
if let Some(rdp) = ramdisk_path {
42-
uefi_builder.set_ramdisk(rdp);
43-
}
44-
if let Some(cfp) = config_file_path {
45-
uefi_builder.set_boot_config(cfp);
46-
}
47-
uefi_builder.create_disk_image(&gpt_path).unwrap();
48-
49-
// create a TFTP folder with the kernel executable and UEFI bootloader for
50-
// UEFI PXE booting
5146
let tftp_path = kernel_path.with_extension("tftp");
52-
uefi_builder.create_pxe_tftp_folder(&tftp_path).unwrap();
53-
47+
image_builder.create_uefi_image(&gpt_path).unwrap();
48+
image_builder.create_uefi_tftp_folder(&tftp_path).unwrap();
5449
run_test_kernel_on_uefi(&gpt_path);
5550
run_test_kernel_on_uefi_pxe(&tftp_path);
5651
}
@@ -59,15 +54,7 @@ pub fn run_test_kernel_internal(
5954
{
6055
// create an MBR disk image for legacy BIOS booting
6156
let mbr_path = kernel_path.with_extension("mbr");
62-
let mut bios_builder = bootloader::BiosBoot::new(kernel_path);
63-
// Set ramdisk for test, if supplied.
64-
if let Some(rdp) = ramdisk_path {
65-
bios_builder.set_ramdisk(rdp);
66-
}
67-
if let Some(cfp) = config_file_path {
68-
bios_builder.set_boot_config(cfp);
69-
}
70-
bios_builder.create_disk_image(&mbr_path).unwrap();
57+
image_builder.create_bios_image(mbr_path.as_path()).unwrap();
7158

7259
run_test_kernel_on_bios(&mbr_path);
7360
}

0 commit comments

Comments
 (0)
Please sign in to comment.