Skip to content

Commit

Permalink
refactor(env): replace get_cmdline, get_cmdsize with args
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Aug 31, 2023
1 parent 2cb192e commit ec651ca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 60 deletions.
23 changes: 4 additions & 19 deletions src/arch/aarch64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod switch;
pub mod systemtime;

use core::arch::global_asm;
use core::str;
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};

use hermit_entry::boot_info::{BootInfo, RawBootInfo};
Expand Down Expand Up @@ -90,30 +91,14 @@ pub fn get_processor_count() -> u32 {
1
}

pub fn get_cmdsize() -> usize {
pub fn args() -> Option<&'static str> {
let dtb = unsafe {
hermit_dtb::Dtb::from_raw(boot_info().hardware_info.device_tree.unwrap().get() as *const u8)
.expect(".dtb file has invalid header")
};

if let Some(cmd) = dtb.get_property("/chosen", "bootargs") {
cmd.len()
} else {
0
}
}

pub fn get_cmdline() -> VirtAddr {
let dtb = unsafe {
hermit_dtb::Dtb::from_raw(boot_info().hardware_info.device_tree.unwrap().get() as *const u8)
.expect(".dtb file has invalid header")
};

if let Some(cmd) = dtb.get_property("/chosen", "bootargs") {
VirtAddr(cmd.as_ptr() as u64)
} else {
VirtAddr::zero()
}
dtb.get_property("/chosen", "bootargs")
.map(|property| str::from_utf8(property).unwrap())
}

/// Earliest initialization function called by the Boot Processor.
Expand Down
28 changes: 4 additions & 24 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,11 @@ pub fn is_uhyve_with_pci() -> bool {
}
}

pub fn get_cmdsize() -> usize {
pub fn args() -> Option<&'static str> {
match boot_info().platform_info {
PlatformInfo::Multiboot { command_line, .. } => command_line
.map(|command_line| command_line.len())
.unwrap_or_default(),
PlatformInfo::LinuxBootParams { command_line, .. } => command_line
.map(|command_line| command_line.len())
.unwrap_or_default(),
PlatformInfo::Uhyve { .. } => 0,
}
}

pub fn get_cmdline() -> VirtAddr {
match boot_info().platform_info {
PlatformInfo::Multiboot { command_line, .. } => VirtAddr(
command_line
.map(|command_line| command_line.as_ptr() as u64)
.unwrap_or_default(),
),
PlatformInfo::LinuxBootParams { command_line, .. } => VirtAddr(
command_line
.map(|command_line| command_line.as_ptr() as u64)
.unwrap_or_default(),
),
PlatformInfo::Uhyve { .. } => VirtAddr(0),
PlatformInfo::Multiboot { command_line, .. } => command_line,
PlatformInfo::LinuxBootParams { command_line, .. } => command_line,
PlatformInfo::Uhyve { .. } => None,
}
}

Expand Down
20 changes: 3 additions & 17 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::{slice, str};
use core::str;

use ahash::RandomState;
use hashbrown::hash_map::Iter;
use hashbrown::HashMap;
use hermit_entry::boot_info::PlatformInfo;
use hermit_sync::OnceCell;

pub use crate::arch::kernel::{
get_base_address, get_cmdline, get_cmdsize, get_image_size, get_ram_address,
};
pub use crate::arch::kernel::{self, get_base_address, get_image_size, get_ram_address};
use crate::kernel::boot_info;

static CLI: OnceCell<Cli> = OnceCell::new();
Expand All @@ -35,18 +33,6 @@ pub fn is_uhyve() -> bool {
matches!(boot_info().platform_info, PlatformInfo::Uhyve { .. })
}

fn get_cmdline_str() -> &'static str {
let cmdsize = get_cmdsize();
let cmdline = get_cmdline().as_ptr::<u8>();
if cmdline.is_null() {
""
} else {
// SAFETY: cmdline and cmdsize are valid forever.
let slice = unsafe { slice::from_raw_parts(cmdline, cmdsize) };
str::from_utf8(slice).unwrap().trim_matches(char::from(0))
}
}

impl Default for Cli {
fn default() -> Self {
let mut image_path = None;
Expand All @@ -56,7 +42,7 @@ impl Default for Cli {
);
let mut args = Vec::new();

let words = shell_words::split(get_cmdline_str()).unwrap();
let words = shell_words::split(kernel::args().unwrap_or_default()).unwrap();
debug!("cli_words = {words:?}");

let mut words = words.into_iter();
Expand Down

0 comments on commit ec651ca

Please sign in to comment.