Skip to content

Commit 53fc5f4

Browse files
committed
Add pxe, bios, and uefi features, all enabled by default
1 parent 4e8bcd7 commit 53fc5f4

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

Diff for: Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ bootloader_api = { version = "0.11.0", path = "api" }
3838
bootloader-x86_64-common = { version = "0.11.0", path = "common" }
3939
bootloader-x86_64-bios-common = { version = "0.11.0", path = "bios/common" }
4040

41+
42+
[features]
43+
default = ["bios", "uefi", "pxe"]
44+
bios = ["mbrman"]
45+
uefi = ["gpt"]
46+
pxe = ["uefi"]
47+
4148
[dependencies]
4249
anyhow = "1.0.32"
4350
fatfs = "0.3.4"
44-
gpt = "3.0.0"
45-
mbrman = "0.5.1"
4651
tempfile = "3.3.0"
52+
mbrman = { version = "0.5.1", optional = true }
53+
gpt = { version = "3.0.0", optional = true }
4754

4855
[dev-dependencies]
4956
bootloader_test_runner = { path = "tests/runner" }

Diff for: build.rs

+30-40
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ use std::{
33
process::Command,
44
};
55

6-
const BOOTLOADER_X86_64_UEFI_VERSION: &str = env!("CARGO_PKG_VERSION");
7-
8-
const BOOTLOADER_X86_64_BIOS_BOOT_SECTOR_VERSION: &str = env!("CARGO_PKG_VERSION");
9-
const BOOTLOADER_X86_64_BIOS_STAGE_2_VERSION: &str = env!("CARGO_PKG_VERSION");
10-
const BOOTLOADER_X86_64_BIOS_STAGE_3_VERSION: &str = env!("CARGO_PKG_VERSION");
11-
const BOOTLOADER_X86_64_BIOS_STAGE_4_VERSION: &str = env!("CARGO_PKG_VERSION");
6+
const BOOTLOADER_VERSION: &str = env!("CARGO_PKG_VERSION");
127

138
fn main() {
14-
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
9+
#[cfg(any(feature = "uefi", feature = "pxe"))]
10+
uefi_main();
11+
#[cfg(feature = "bios")]
12+
bios_main();
13+
}
1514

15+
#[cfg(all(any(feature = "uefi", feature = "pxe"), not(docsrs_dummy_build)))]
16+
fn uefi_main() {
17+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
1618
let uefi_path = build_uefi_bootloader(&out_dir);
1719
println!(
1820
"cargo:rustc-env=UEFI_BOOTLOADER_PATH={}",
1921
uefi_path.display()
2022
);
23+
}
2124

25+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
26+
fn bios_main() {
27+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
2228
let bios_boot_sector_path = build_bios_boot_sector(&out_dir);
2329
println!(
2430
"cargo:rustc-env=BIOS_BOOT_SECTOR_PATH={}",
@@ -43,7 +49,7 @@ fn main() {
4349
);
4450
}
4551

46-
#[cfg(not(docsrs_dummy_build))]
52+
#[cfg(all(any(feature = "uefi", feature = "pxe"), not(docsrs_dummy_build)))]
4753
fn build_uefi_bootloader(out_dir: &Path) -> PathBuf {
4854
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
4955
let mut cmd = Command::new(cargo);
@@ -53,7 +59,7 @@ fn build_uefi_bootloader(out_dir: &Path) -> PathBuf {
5359
cmd.arg("--path").arg("uefi");
5460
println!("cargo:rerun-if-changed=uefi");
5561
} else {
56-
cmd.arg("--version").arg(BOOTLOADER_X86_64_UEFI_VERSION);
62+
cmd.arg("--version").arg(BOOTLOADER_VERSION);
5763
}
5864
cmd.arg("--locked");
5965
cmd.arg("--target").arg("x86_64-unknown-uefi");
@@ -77,7 +83,7 @@ fn build_uefi_bootloader(out_dir: &Path) -> PathBuf {
7783
}
7884
}
7985

80-
#[cfg(not(docsrs_dummy_build))]
86+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
8187
fn build_bios_boot_sector(out_dir: &Path) -> PathBuf {
8288
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
8389
let mut cmd = Command::new(cargo);
@@ -90,8 +96,7 @@ fn build_bios_boot_sector(out_dir: &Path) -> PathBuf {
9096
cmd.arg("--path").arg(&local_path);
9197
println!("cargo:rerun-if-changed={}", local_path.display());
9298
} else {
93-
cmd.arg("--version")
94-
.arg(BOOTLOADER_X86_64_BIOS_BOOT_SECTOR_VERSION);
99+
cmd.arg("--version").arg(BOOTLOADER_VERSION);
95100
}
96101
cmd.arg("--locked");
97102
cmd.arg("--target").arg("i386-code16-boot-sector.json");
@@ -120,7 +125,7 @@ fn build_bios_boot_sector(out_dir: &Path) -> PathBuf {
120125
convert_elf_to_bin(elf_path)
121126
}
122127

123-
#[cfg(not(docsrs_dummy_build))]
128+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
124129
fn build_bios_stage_2(out_dir: &Path) -> PathBuf {
125130
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
126131
let mut cmd = Command::new(cargo);
@@ -133,8 +138,7 @@ fn build_bios_stage_2(out_dir: &Path) -> PathBuf {
133138
cmd.arg("--path").arg(&local_path);
134139
println!("cargo:rerun-if-changed={}", local_path.display());
135140
} else {
136-
cmd.arg("--version")
137-
.arg(BOOTLOADER_X86_64_BIOS_STAGE_2_VERSION);
141+
cmd.arg("--version").arg(BOOTLOADER_VERSION);
138142
}
139143
cmd.arg("--locked");
140144
cmd.arg("--target").arg("i386-code16-stage-2.json");
@@ -161,7 +165,7 @@ fn build_bios_stage_2(out_dir: &Path) -> PathBuf {
161165
convert_elf_to_bin(elf_path)
162166
}
163167

164-
#[cfg(not(docsrs_dummy_build))]
168+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
165169
fn build_bios_stage_3(out_dir: &Path) -> PathBuf {
166170
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
167171
let mut cmd = Command::new(cargo);
@@ -174,8 +178,7 @@ fn build_bios_stage_3(out_dir: &Path) -> PathBuf {
174178
cmd.arg("--path").arg(&local_path);
175179
println!("cargo:rerun-if-changed={}", local_path.display());
176180
} else {
177-
cmd.arg("--version")
178-
.arg(BOOTLOADER_X86_64_BIOS_STAGE_3_VERSION);
181+
cmd.arg("--version").arg(BOOTLOADER_VERSION);
179182
}
180183
cmd.arg("--locked");
181184
cmd.arg("--target").arg("i686-stage-3.json");
@@ -202,7 +205,7 @@ fn build_bios_stage_3(out_dir: &Path) -> PathBuf {
202205
convert_elf_to_bin(elf_path)
203206
}
204207

205-
#[cfg(not(docsrs_dummy_build))]
208+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
206209
fn build_bios_stage_4(out_dir: &Path) -> PathBuf {
207210
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
208211
let mut cmd = Command::new(cargo);
@@ -215,8 +218,7 @@ fn build_bios_stage_4(out_dir: &Path) -> PathBuf {
215218
cmd.arg("--path").arg(&local_path);
216219
println!("cargo:rerun-if-changed={}", local_path.display());
217220
} else {
218-
cmd.arg("--version")
219-
.arg(BOOTLOADER_X86_64_BIOS_STAGE_4_VERSION);
221+
cmd.arg("--version").arg(BOOTLOADER_VERSION);
220222
}
221223
cmd.arg("--locked");
222224
cmd.arg("--target").arg("x86_64-stage-4.json");
@@ -244,6 +246,7 @@ fn build_bios_stage_4(out_dir: &Path) -> PathBuf {
244246
convert_elf_to_bin(elf_path)
245247
}
246248

249+
#[cfg(all(feature = "bios", not(docsrs_dummy_build)))]
247250
fn convert_elf_to_bin(elf_path: PathBuf) -> PathBuf {
248251
let flat_binary_path = elf_path.with_extension("bin");
249252

@@ -273,23 +276,10 @@ fn convert_elf_to_bin(elf_path: PathBuf) -> PathBuf {
273276

274277
// dummy implementations because docsrs builds have no network access
275278

276-
#[cfg(docsrs_dummy_build)]
277-
fn build_uefi_bootloader(_out_dir: &Path) -> PathBuf {
278-
PathBuf::new()
279-
}
280-
#[cfg(docsrs_dummy_build)]
281-
fn build_bios_boot_sector(_out_dir: &Path) -> PathBuf {
282-
PathBuf::new()
283-
}
284-
#[cfg(docsrs_dummy_build)]
285-
fn build_bios_stage_2(_out_dir: &Path) -> PathBuf {
286-
PathBuf::new()
287-
}
288-
#[cfg(docsrs_dummy_build)]
289-
fn build_bios_stage_3(_out_dir: &Path) -> PathBuf {
290-
PathBuf::new()
291-
}
292-
#[cfg(docsrs_dummy_build)]
293-
fn build_bios_stage_4(_out_dir: &Path) -> PathBuf {
294-
PathBuf::new()
279+
#[cfg(all(feature = "uefi", docsrs_dummy_build))]
280+
fn uefi_main() {
281+
// stub
295282
}
283+
284+
#[cfg(all(feature = "bios", docsrs_dummy_build))]
285+
fn bios_main() {}

Diff for: src/lib.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ use std::{
1212
use tempfile::NamedTempFile;
1313

1414
mod fat;
15+
#[cfg(feature = "uefi")]
1516
mod gpt;
17+
18+
#[cfg(feature = "bios")]
1619
mod mbr;
1720

1821
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
1922
const RAMDISK_FILE_NAME: &str = "ramdisk";
20-
const BIOS_STAGE_3: &str = "boot-stage-3";
21-
const BIOS_STAGE_4: &str = "boot-stage-4";
22-
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
23-
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader";
2423

2524
struct DiskImageFile<'a> {
2625
source: &'a PathBuf,
@@ -87,9 +86,11 @@ impl<'a> DiskImageBuilder<'a> {
8786

8887
Ok(out_file)
8988
}
90-
89+
#[cfg(feature = "bios")]
9190
/// Create an MBR disk image for booting on BIOS systems.
9291
pub fn create_bios_image(&self, image_filename: &Path) -> anyhow::Result<()> {
92+
const BIOS_STAGE_3: &str = "boot-stage-3";
93+
const BIOS_STAGE_4: &str = "boot-stage-4";
9394
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH"));
9495
let stage_2_path = Path::new(env!("BIOS_STAGE_2_PATH"));
9596
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
@@ -114,8 +115,11 @@ impl<'a> DiskImageBuilder<'a> {
114115
.context("failed to delete FAT partition after disk image creation")?;
115116
Ok(())
116117
}
118+
119+
#[cfg(feature = "uefi")]
117120
/// Create a GPT disk image for booting on UEFI systems.
118121
pub fn create_uefi_image(&self, image_filename: &Path) -> anyhow::Result<()> {
122+
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
119123
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
120124
let mut internal_files = BTreeMap::new();
121125
internal_files.insert(UEFI_BOOT_FILENAME, bootloader_path);
@@ -131,8 +135,10 @@ impl<'a> DiskImageBuilder<'a> {
131135
Ok(())
132136
}
133137

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

0 commit comments

Comments
 (0)