-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embed bios and uefi binaries #395
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69596a1
Initial commit
mysteriouslyseeing 8be2588
Run cargo fmt
mysteriouslyseeing 12e3331
Removed unnecessary reference
mysteriouslyseeing 0f90f10
Removed feature gate
mysteriouslyseeing 0f5e3d9
Changed static to const and removed unnecessary lifetime
mysteriouslyseeing 580cb42
Added new variant for FileDataSource
mysteriouslyseeing 3b312fc
Ran cargo fmt
mysteriouslyseeing 7bedb9c
Re-added multiple mistakenly deleted #[cfg(feature)]
mysteriouslyseeing 99a6e54
Changed build.rs to place dummy files in out_dir when under the "docs…
mysteriouslyseeing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,18 @@ const KERNEL_FILE_NAME: &str = "kernel-x86_64"; | |
const RAMDISK_FILE_NAME: &str = "ramdisk"; | ||
const CONFIG_FILE_NAME: &str = "boot.json"; | ||
|
||
#[cfg(all(feature = "embedded_binaries", feature = "uefi"))] | ||
static UEFI_BOOTLOADER: &'static [u8] = include_bytes!(env!("UEFI_BOOTLOADER_PATH")); | ||
|
||
#[cfg(all(feature = "embedded_binaries", feature = "bios"))] | ||
static BIOS_BOOT_SECTOR: &'static [u8] = include_bytes!(env!("BIOS_BOOT_SECTOR_PATH")); | ||
#[cfg(all(feature = "embedded_binaries", feature = "bios"))] | ||
static BIOS_STAGE_2: &'static [u8] = include_bytes!(env!("BIOS_STAGE_2_PATH")); | ||
#[cfg(all(feature = "embedded_binaries", feature = "bios"))] | ||
static BIOS_STAGE_3: &'static [u8] = include_bytes!(env!("BIOS_STAGE_3_PATH")); | ||
#[cfg(all(feature = "embedded_binaries", feature = "bios"))] | ||
static BIOS_STAGE_4: &'static [u8] = include_bytes!(env!("BIOS_STAGE_4_PATH")); | ||
|
||
/// Allows creating disk images for a specified set of files. | ||
/// | ||
/// It can currently create `MBR` (BIOS), `GPT` (UEFI), and `TFTP` (UEFI) images. | ||
|
@@ -95,22 +107,22 @@ impl DiskImageBuilder { | |
self.set_file_source(destination.into(), FileDataSource::File(file_path)) | ||
} | ||
|
||
#[cfg(feature = "bios")] | ||
#[cfg(all(feature = "bios", not(feature = "embedded_binaries")))] | ||
/// Create an MBR disk image for booting on BIOS systems. | ||
pub fn create_bios_image(&self, image_path: &Path) -> anyhow::Result<()> { | ||
const BIOS_STAGE_3: &str = "boot-stage-3"; | ||
const BIOS_STAGE_4: &str = "boot-stage-4"; | ||
const BIOS_STAGE_3_NAME: &str = "boot-stage-3"; | ||
const BIOS_STAGE_4_NAME: &str = "boot-stage-4"; | ||
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH")); | ||
let stage_2_path = Path::new(env!("BIOS_STAGE_2_PATH")); | ||
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH")); | ||
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH")); | ||
let mut internal_files = BTreeMap::new(); | ||
internal_files.insert( | ||
BIOS_STAGE_3, | ||
BIOS_STAGE_3_NAME, | ||
FileDataSource::File(stage_3_path.to_path_buf()), | ||
); | ||
internal_files.insert( | ||
BIOS_STAGE_4, | ||
BIOS_STAGE_4_NAME, | ||
FileDataSource::File(stage_4_path.to_path_buf()), | ||
); | ||
|
||
|
@@ -131,16 +143,50 @@ impl DiskImageBuilder { | |
Ok(()) | ||
} | ||
|
||
#[cfg(all(feature = "bios", feature = "embedded_binaries"))] | ||
/// Create an MBR disk image for booting on BIOS systems. | ||
pub fn create_bios_image(&self, image_path: &Path) -> anyhow::Result<()> { | ||
const BIOS_STAGE_3_NAME: &str = "boot-stage-3"; | ||
const BIOS_STAGE_4_NAME: &str = "boot-stage-4"; | ||
let stage_3 = FileDataSource::Data(BIOS_STAGE_3.to_vec()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
let stage_4 = FileDataSource::Data(BIOS_STAGE_4.to_vec()); | ||
let mut internal_files = BTreeMap::new(); | ||
internal_files.insert(BIOS_STAGE_3_NAME, stage_3); | ||
internal_files.insert(BIOS_STAGE_4_NAME, stage_4); | ||
let fat_partition = self | ||
.create_fat_filesystem_image(internal_files) | ||
.context("failed to create FAT partition")?; | ||
mbr::create_mbr_disk( | ||
BIOS_BOOT_SECTOR, | ||
BIOS_STAGE_2, | ||
fat_partition.path(), | ||
image_path, | ||
) | ||
.context("failed to create BIOS MBR disk image")?; | ||
|
||
fat_partition | ||
.close() | ||
.context("failed to delete FAT partition after disk image creation")?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "uefi")] | ||
/// Create a GPT disk image for booting on UEFI systems. | ||
pub fn create_uefi_image(&self, image_path: &Path) -> anyhow::Result<()> { | ||
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi"; | ||
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH")); | ||
|
||
#[cfg(feature = "embedded_binaries")] | ||
fn get_uefi_bootloader() -> FileDataSource { | ||
FileDataSource::Data(UEFI_BOOTLOADER.to_vec()) | ||
} | ||
#[cfg(not(feature = "embedded_binaries"))] | ||
fn get_uefi_bootloader() -> FileDataSource { | ||
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH")); | ||
FileDataSource::File(bootloader_path.to_path_buf()) | ||
} | ||
|
||
let mut internal_files = BTreeMap::new(); | ||
internal_files.insert( | ||
UEFI_BOOT_FILENAME, | ||
FileDataSource::File(bootloader_path.to_path_buf()), | ||
); | ||
internal_files.insert(UEFI_BOOT_FILENAME, get_uefi_bootloader()); | ||
let fat_partition = self | ||
.create_fat_filesystem_image(internal_files) | ||
.context("failed to create FAT partition")?; | ||
|
@@ -158,19 +204,33 @@ impl DiskImageBuilder { | |
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> { | ||
use std::{fs, ops::Deref}; | ||
|
||
#[cfg(feature = "embedded_binaries")] | ||
fn write_uefi_bootloader(to: &PathBuf) -> anyhow::Result<()> { | ||
fs::write(to, UEFI_BOOTLOADER).with_context(|| { | ||
format!( | ||
"failed to copy bootloader from the embedded binary to {}", | ||
to.display() | ||
) | ||
}) | ||
} | ||
#[cfg(not(feature = "embedded_binaries"))] | ||
fn write_uefi_bootloader(to: &PathBuf) -> anyhow::Result<()> { | ||
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH")); | ||
fs::copy(bootloader_path, to).map(|_| ()).with_context(|| { | ||
format!( | ||
"failed to copy bootloader from {} to {}", | ||
bootloader_path.display(), | ||
to.display() | ||
) | ||
}) | ||
} | ||
|
||
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader"; | ||
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH")); | ||
fs::create_dir_all(tftp_path) | ||
.with_context(|| format!("failed to create out dir at {}", tftp_path.display()))?; | ||
|
||
let to = tftp_path.join(UEFI_TFTP_BOOT_FILENAME); | ||
fs::copy(bootloader_path, &to).with_context(|| { | ||
format!( | ||
"failed to copy bootloader from {} to {}", | ||
bootloader_path.display(), | ||
to.display() | ||
) | ||
})?; | ||
write_uefi_bootloader(&to)?; | ||
|
||
for f in &self.files { | ||
let to = tftp_path.join(f.0.deref()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of statics, I think we can use consts here. E.g.
const BIOS_BOOT_SECTOR: &'static [u8] = ...
.