Skip to content

Commit 4e732f5

Browse files
authored
Merge pull request #490 from zyuiop/feat/boot-args-file
feat: add boot args via file for EFI loader
2 parents b4ad862 + 809128f commit 4e732f5

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/arch/x86_64/firecracker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::borrow::ToOwned;
12
use core::ptr::write_bytes;
23
use core::{ptr, slice};
34

@@ -217,7 +218,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
217218
info!("Found available RAM: [{start_address:#x} - {end_address:#x}]");
218219

219220
if let Some(command_line) = command_line {
220-
fdt = fdt.bootargs(command_line).unwrap();
221+
fdt = fdt.bootargs(command_line.to_owned()).unwrap();
221222
}
222223

223224
let fdt = fdt.finish().unwrap();

src/arch/x86_64/multiboot.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::borrow::ToOwned;
12
use core::ptr::write_bytes;
23
use core::{mem, ptr, slice};
34

@@ -65,7 +66,7 @@ impl DeviceTree {
6566
let mut fdt = Fdt::new("multiboot")?.memory_regions(memory_regions)?;
6667

6768
if let Some(cmdline) = multiboot.command_line() {
68-
fdt = fdt.bootargs(cmdline)?;
69+
fdt = fdt.bootargs(cmdline.to_owned())?;
6970
}
7071

7172
let fdt = fdt.finish()?;

src/fdt.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use alloc::format;
2+
use alloc::string::String;
23
use alloc::vec::Vec;
34
use core::ops::Range;
45

56
use vm_fdt::{FdtWriter, FdtWriterNode, FdtWriterResult};
67

7-
pub struct Fdt<'a> {
8+
pub struct Fdt {
89
writer: FdtWriter,
910
root_node: FdtWriterNode,
10-
bootargs: Option<&'a str>,
11+
bootargs: Option<String>,
1112
}
1213

13-
impl<'a> Fdt<'a> {
14+
impl Fdt {
1415
pub fn new(platform: &str) -> FdtWriterResult<Self> {
1516
let mut writer = FdtWriter::new()?;
1617

@@ -30,7 +31,7 @@ impl<'a> Fdt<'a> {
3031

3132
pub fn finish(mut self) -> FdtWriterResult<Vec<u8>> {
3233
let chosen_node = self.writer.begin_node("chosen")?;
33-
if let Some(bootargs) = self.bootargs {
34+
if let Some(bootargs) = &self.bootargs {
3435
self.writer.property_string("bootargs", bootargs)?;
3536
}
3637
self.writer.end_node(chosen_node)?;
@@ -40,8 +41,7 @@ impl<'a> Fdt<'a> {
4041
self.writer.finish()
4142
}
4243

43-
#[cfg_attr(target_os = "uefi", expect(unused))]
44-
pub fn bootargs(mut self, bootargs: &'a str) -> FdtWriterResult<Self> {
44+
pub fn bootargs(mut self, bootargs: String) -> FdtWriterResult<Self> {
4545
assert!(self.bootargs.is_none());
4646
self.bootargs = Some(bootargs);
4747

@@ -75,7 +75,7 @@ mod x86_64 {
7575
use multiboot::information::{MemoryMapIter, MemoryType};
7676
use vm_fdt::FdtWriterResult;
7777

78-
impl super::Fdt<'_> {
78+
impl super::Fdt {
7979
pub fn memory_regions(
8080
mut self,
8181
memory_regions: MemoryMapIter<'_, '_>,
@@ -105,7 +105,7 @@ mod uefi {
105105
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
106106
use vm_fdt::FdtWriterResult;
107107

108-
impl super::Fdt<'_> {
108+
impl super::Fdt {
109109
pub fn memory_map(mut self, memory_map: &mut impl MemoryMapMut) -> FdtWriterResult<Self> {
110110
memory_map.sort();
111111
info!("Memory map:\n{}", memory_map.display());

src/os/uefi/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod allocator;
22
mod console;
33

4+
use alloc::string::String;
45
use alloc::vec::Vec;
56
use core::ffi::c_void;
67
use core::mem::MaybeUninit;
@@ -14,7 +15,7 @@ use hermit_entry::elf::{KernelObject, LoadedKernel};
1415
use log::info;
1516
use sptr::Strict;
1617
use uefi::boot::{AllocateType, MemoryType, PAGE_SIZE};
17-
use uefi::fs::{FileSystem, Path};
18+
use uefi::fs::{self, FileSystem, Path};
1819
use uefi::prelude::*;
1920
use uefi::table::cfg;
2021

@@ -40,11 +41,15 @@ fn main() -> Status {
4041

4142
drop(kernel_image);
4243

43-
let fdt = Fdt::new("uefi")
44+
let mut fdt = Fdt::new("uefi")
4445
.unwrap()
4546
.rsdp(u64::try_from(rsdp.expose_addr()).unwrap())
4647
.unwrap();
4748

49+
if let Some(bootargs) = read_bootargs() {
50+
fdt = fdt.bootargs(bootargs).unwrap();
51+
}
52+
4853
allocator::exit_boot_services();
4954
let mut memory_map = unsafe { boot::exit_boot_services(None) };
5055

@@ -69,6 +74,25 @@ fn read_app() -> Vec<u8> {
6974
data
7075
}
7176

77+
fn read_bootargs() -> Option<String> {
78+
let image_handle = boot::image_handle();
79+
let fs = boot::get_image_file_system(image_handle).expect("should open file system");
80+
81+
let path = Path::new(cstr16!(r"\efi\boot\hermit-bootargs"));
82+
83+
match FileSystem::new(fs).read_to_string(path) {
84+
Ok(bootargs) => {
85+
info!("Read Hermit bootargs from from \"{path}\": {bootargs}");
86+
Some(bootargs)
87+
}
88+
Err(fs::Error::Io(err)) if err.uefi_error.status() == Status::NOT_FOUND => {
89+
info!("Hermit bootargs file does not exist: \"{path}\"");
90+
None
91+
}
92+
Err(err) => panic!("{err:?}"),
93+
}
94+
}
95+
7296
pub unsafe fn boot_kernel(kernel_info: LoadedKernel, fdt: Vec<u8>) -> ! {
7397
let LoadedKernel {
7498
load_info,

0 commit comments

Comments
 (0)