Skip to content

Commit a03e4b2

Browse files
committed
Add pxe, bios, and uefi features, all enabled by default
1 parent 80a448b commit a03e4b2

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

build.rs

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@ use std::{
66
const BOOTLOADER_VERSION: &str = env!("CARGO_PKG_VERSION");
77

88
fn main() {
9-
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
10-
119
#[cfg(feature = "uefi")]
12-
{
13-
let uefi_path = build_uefi_bootloader(&out_dir);
14-
println!(
15-
"cargo:rustc-env=UEFI_BOOTLOADER_PATH={}",
16-
uefi_path.display()
17-
);
18-
}
19-
10+
uefi_main();
2011
#[cfg(feature = "bios")]
21-
{
22-
let bios_boot_sector_path = build_bios_boot_sector(&out_dir);
23-
println!(
24-
"cargo:rustc-env=BIOS_BOOT_SECTOR_PATH={}",
25-
bios_boot_sector_path.display()
26-
);
27-
let bios_stage_2_path = build_bios_stage_2(&out_dir);
28-
println!(
29-
"cargo:rustc-env=BIOS_STAGE_2_PATH={}",
30-
bios_stage_2_path.display()
31-
);
12+
bios_main();
13+
}
14+
15+
#[cfg(not(docsrs_dummy_build))]
16+
#[cfg(feature = "uefi")]
17+
fn uefi_main() {
18+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
19+
let uefi_path = build_uefi_bootloader(&out_dir);
20+
println!(
21+
"cargo:rustc-env=UEFI_BOOTLOADER_PATH={}",
22+
uefi_path.display()
23+
);
24+
}
25+
26+
#[cfg(not(docsrs_dummy_build))]
27+
#[cfg(feature = "bios")]
28+
fn bios_main() {
29+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
30+
let bios_boot_sector_path = build_bios_boot_sector(&out_dir);
31+
println!(
32+
"cargo:rustc-env=BIOS_BOOT_SECTOR_PATH={}",
33+
bios_boot_sector_path.display()
34+
);
35+
let bios_stage_2_path = build_bios_stage_2(&out_dir);
36+
println!(
37+
"cargo:rustc-env=BIOS_STAGE_2_PATH={}",
38+
bios_stage_2_path.display()
39+
);
3240

3341
let bios_stage_3_path = build_bios_stage_3(&out_dir);
3442
println!(
@@ -41,10 +49,8 @@ fn main() {
4149
"cargo:rustc-env=BIOS_STAGE_4_PATH={}",
4250
bios_stage_4_path.display()
4351
);
44-
}
4552
}
4653

47-
#[cfg(not(docsrs_dummy_build))]
4854
#[cfg(feature = "uefi")]
4955
fn build_uefi_bootloader(out_dir: &Path) -> PathBuf {
5056
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
@@ -79,7 +85,6 @@ fn build_uefi_bootloader(out_dir: &Path) -> PathBuf {
7985
}
8086
}
8187

82-
#[cfg(not(docsrs_dummy_build))]
8388
#[cfg(feature = "bios")]
8489
fn build_bios_boot_sector(out_dir: &Path) -> PathBuf {
8590
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
@@ -122,7 +127,6 @@ fn build_bios_boot_sector(out_dir: &Path) -> PathBuf {
122127
convert_elf_to_bin(elf_path)
123128
}
124129

125-
#[cfg(not(docsrs_dummy_build))]
126130
#[cfg(feature = "bios")]
127131
fn build_bios_stage_2(out_dir: &Path) -> PathBuf {
128132
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
@@ -163,7 +167,6 @@ fn build_bios_stage_2(out_dir: &Path) -> PathBuf {
163167
convert_elf_to_bin(elf_path)
164168
}
165169

166-
#[cfg(not(docsrs_dummy_build))]
167170
#[cfg(feature = "bios")]
168171
fn build_bios_stage_3(out_dir: &Path) -> PathBuf {
169172
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
@@ -204,7 +207,6 @@ fn build_bios_stage_3(out_dir: &Path) -> PathBuf {
204207
convert_elf_to_bin(elf_path)
205208
}
206209

207-
#[cfg(not(docsrs_dummy_build))]
208210
#[cfg(feature = "bios")]
209211
fn build_bios_stage_4(out_dir: &Path) -> PathBuf {
210212
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
@@ -277,22 +279,9 @@ fn convert_elf_to_bin(elf_path: PathBuf) -> PathBuf {
277279
// dummy implementations because docsrs builds have no network access
278280

279281
#[cfg(docsrs_dummy_build)]
280-
fn build_uefi_bootloader(_out_dir: &Path) -> PathBuf {
281-
PathBuf::new()
282-
}
283-
#[cfg(docsrs_dummy_build)]
284-
fn build_bios_boot_sector(_out_dir: &Path) -> PathBuf {
285-
PathBuf::new()
286-
}
287-
#[cfg(docsrs_dummy_build)]
288-
fn build_bios_stage_2(_out_dir: &Path) -> PathBuf {
289-
PathBuf::new()
290-
}
291-
#[cfg(docsrs_dummy_build)]
292-
fn build_bios_stage_3(_out_dir: &Path) -> PathBuf {
293-
PathBuf::new()
294-
}
282+
#[cfg(feature = "uefi")]
283+
fn uefi_main() {}
284+
295285
#[cfg(docsrs_dummy_build)]
296-
fn build_bios_stage_4(_out_dir: &Path) -> PathBuf {
297-
PathBuf::new()
298-
}
286+
#[cfg(feature = "bios")]
287+
fn bios_main() {}

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ use anyhow::Context;
2121
use tempfile::NamedTempFile;
2222

2323
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
24-
const BIOS_STAGE_3: &str = "boot-stage-3";
25-
const BIOS_STAGE_4: &str = "boot-stage-4";
26-
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
27-
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader";
2824

2925
struct DiskImageFile<'a> {
3026
source: &'a PathBuf,
@@ -87,9 +83,11 @@ impl<'a> DiskImageBuilder<'a> {
8783

8884
Ok(out_file)
8985
}
90-
86+
#[cfg(feature = "bios")]
9187
/// Create an MBR disk image for booting on BIOS systems.
9288
pub fn create_bios_image(&self, image_filename: &Path) -> anyhow::Result<()> {
89+
const BIOS_STAGE_3: &str = "boot-stage-3";
90+
const BIOS_STAGE_4: &str = "boot-stage-4";
9391
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH"));
9492
let stage_2_path = Path::new(env!("BIOS_STAGE_2_PATH"));
9593
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
@@ -114,8 +112,11 @@ impl<'a> DiskImageBuilder<'a> {
114112
.context("failed to delete FAT partition after disk image creation")?;
115113
Ok(())
116114
}
115+
116+
#[cfg(feature = "uefi")]
117117
/// Create a GPT disk image for booting on UEFI systems.
118118
pub fn create_uefi_image(&self, image_filename: &Path) -> anyhow::Result<()> {
119+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
119120
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
120121
let mut internal_files = BTreeMap::new();
121122
internal_files.insert(UEFI_BOOT_FILENAME, bootloader_path);
@@ -131,8 +132,10 @@ impl<'a> DiskImageBuilder<'a> {
131132
Ok(())
132133
}
133134

135+
#[cfg(feature = "uefi")]
134136
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
135137
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
138+
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader";
136139
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
137140
std::fs::create_dir_all(tftp_path)
138141
.with_context(|| format!("failed to create out dir at {}", tftp_path.display()))?;

0 commit comments

Comments
 (0)