Skip to content

Commit 7886c18

Browse files
committed
Fix build errors due to rebase
1 parent d19228d commit 7886c18

File tree

4 files changed

+63
-46
lines changed

4 files changed

+63
-46
lines changed

src/bios/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::path::Path;
22

3-
use crate::DiskImageBuilder;
3+
use bootloader_boot_config::BootConfig;
44

5-
const BIOS_STAGE_3: &str = "boot-stage-3";
6-
const BIOS_STAGE_4: &str = "boot-stage-4";
5+
use crate::DiskImageBuilder;
76

87
/// Create disk images for booting on legacy BIOS systems.
98
pub struct BiosBoot {
@@ -18,12 +17,18 @@ impl BiosBoot {
1817
}
1918
}
2019

21-
/// Add a ramdisk file to the image
20+
/// Add a ramdisk file to the image.
2221
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
2322
self.image_builder.set_ramdisk(ramdisk_path);
2423
self
2524
}
2625

26+
/// Creates a configuration file (boot.json) that configures the runtime behavior of the bootloader.
27+
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
28+
self.image_builder.set_boot_config(config);
29+
self
30+
}
31+
2732
/// Create a bootable BIOS disk image at the given path.
2833
pub fn create_disk_image(&self, out_path: &Path) -> anyhow::Result<()> {
2934
self.image_builder.create_bios_image(out_path)

src/lib.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ mod mbr;
1313
#[cfg(feature = "uefi")]
1414
mod uefi;
1515

16+
#[cfg(feature = "uefi")]
17+
pub use uefi::UefiBoot;
18+
19+
#[cfg(feature = "bios")]
20+
pub use bios::BiosBoot;
21+
1622
mod fat;
1723

1824
use std::{
1925
collections::BTreeMap,
26+
env::temp_dir,
27+
fs,
28+
io::Write,
2029
path::{Path, PathBuf},
2130
};
2231

2332
use anyhow::Context;
2433

25-
use tempfile::NamedTempFile;
34+
use tempfile::{tempfile, NamedTempFile};
2635

2736
pub use bootloader_boot_config::BootConfig;
2837

@@ -65,6 +74,31 @@ impl DiskImageBuilder {
6574
self.add_or_replace_file(&path, RAMDISK_FILE_NAME)
6675
}
6776

77+
/// Configures the runtime behavior of the bootloader.
78+
pub fn set_boot_config(&mut self, boot_config: &BootConfig) -> &mut Self {
79+
let json =
80+
serde_json::to_string_pretty(boot_config).expect("failed to serialize BootConfig");
81+
let bytes = json.as_bytes();
82+
self.add_or_replace_file_byte_content(bytes, CONFIG_FILE_NAME)
83+
}
84+
85+
/// Add or replace a file from a byte array
86+
pub fn add_or_replace_file_byte_content(&mut self, data: &[u8], target: &str) -> &mut Self {
87+
let temp_path = temp_dir();
88+
let file_name = temp_path.join("bytes.tmp");
89+
fs::create_dir_all(temp_path).expect("Failed to create temp directory");
90+
let mut temp_file =
91+
fs::File::create(file_name.clone()).expect("Failed to create temp file");
92+
temp_file
93+
.write_all(data)
94+
.expect("Failed to write data to temp file");
95+
temp_file
96+
.sync_all()
97+
.expect("Failed to flush temp file to disk");
98+
99+
self.add_or_replace_file(&file_name, target)
100+
}
101+
68102
/// Add or replace arbitrary files.
69103
/// NOTE: You can overwrite internal files if you choose, such as EFI/BOOT/BOOTX64.EFI
70104
/// This can be useful in situations where you want to generate an image, but not use the provided bootloader.

src/uefi/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::path::Path;
22

3-
use crate::DiskImageBuilder;
3+
use bootloader_boot_config::BootConfig;
44

5-
const BIOS_STAGE_3: &str = "boot-stage-3";
6-
const BIOS_STAGE_4: &str = "boot-stage-4";
5+
use crate::DiskImageBuilder;
76

87
/// Create disk images for booting on legacy BIOS systems.
98
pub struct UefiBoot {
@@ -24,6 +23,12 @@ impl UefiBoot {
2423
self
2524
}
2625

26+
/// Creates a configuration file (boot.json) that configures the runtime behavior of the bootloader.
27+
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
28+
self.image_builder.set_boot_config(config);
29+
self
30+
}
31+
2732
/// Create a bootable UEFI disk image at the given path.
2833
pub fn create_disk_image(&self, out_path: &Path) -> anyhow::Result<()> {
2934
self.image_builder.create_uefi_image(out_path)

tests/runner/src/lib.rs

+11-38
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);
36+
if let Some(rdp) = ramdisk_path {
37+
image_builder.set_ramdisk(rdp);
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,32 +54,10 @@ 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
}
74-
75-
#[cfg(feature = "uefi")]
76-
{
77-
let gpt_path = kernel_path_buf.with_extension("gpt");
78-
let tftp_path = kernel_path_buf.with_extension("tftp");
79-
image_builder.create_uefi_image(&gpt_path).unwrap();
80-
image_builder.create_uefi_tftp_folder(&tftp_path).unwrap();
81-
run_test_kernel_on_uefi(&gpt_path);
82-
run_test_kernel_on_uefi_pxe(&tftp_path);
83-
}
84-
}
85-
86-
pub fn run_test_kernel(kernel_binary_path: &str) {
87-
run_test_kernel_with_ramdisk(kernel_binary_path, None);
8861
}
8962

9063
#[cfg(feature = "uefi")]

0 commit comments

Comments
 (0)