mirror of
https://github.com/edera-dev/sprout.git
synced 2025-12-19 11:10:17 +00:00
chore(code): move load options parsing to crates/eficore
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -75,7 +75,6 @@ dependencies = [
|
|||||||
"hex",
|
"hex",
|
||||||
"log",
|
"log",
|
||||||
"sha2",
|
"sha2",
|
||||||
"shlex",
|
|
||||||
"toml",
|
"toml",
|
||||||
"uefi",
|
"uefi",
|
||||||
"uefi-raw",
|
"uefi-raw",
|
||||||
@@ -99,6 +98,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"log",
|
"log",
|
||||||
|
"shlex",
|
||||||
"spin",
|
"spin",
|
||||||
"uefi",
|
"uefi",
|
||||||
"uefi-raw",
|
"uefi-raw",
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ edera-sprout-config.path = "../config"
|
|||||||
edera-sprout-eficore.path = "../eficore"
|
edera-sprout-eficore.path = "../eficore"
|
||||||
hex.workspace = true
|
hex.workspace = true
|
||||||
sha2.workspace = true
|
sha2.workspace = true
|
||||||
shlex.workspace = true
|
|
||||||
toml.workspace = true
|
toml.workspace = true
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
uefi.workspace = true
|
uefi.workspace = true
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ use alloc::collections::BTreeMap;
|
|||||||
use alloc::string::{String, ToString};
|
use alloc::string::{String, ToString};
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
|
|
||||||
/// Acquire arguments from UEFI environment.
|
|
||||||
pub mod env;
|
|
||||||
|
|
||||||
/// The Sprout options parser.
|
/// The Sprout options parser.
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use crate::options::env;
|
|
||||||
use alloc::collections::BTreeMap;
|
use alloc::collections::BTreeMap;
|
||||||
use alloc::string::{String, ToString};
|
use alloc::string::{String, ToString};
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use core::ptr::null_mut;
|
use core::ptr::null_mut;
|
||||||
|
use eficore::env;
|
||||||
use log::info;
|
use log::info;
|
||||||
use uefi_raw::Status;
|
use uefi_raw::Status;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ pub enum OptionForm {
|
|||||||
Help,
|
Help,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The description of an option, used in the options parser
|
/// The description of an option, used in the option parser
|
||||||
/// to make decisions about how to progress.
|
/// to make decisions about how to progress.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct OptionDescription<'a> {
|
pub struct OptionDescription<'a> {
|
||||||
@@ -35,8 +35,8 @@ pub trait OptionsRepresentable {
|
|||||||
type Output;
|
type Output;
|
||||||
|
|
||||||
/// The configured options for this type. This should describe all the options
|
/// The configured options for this type. This should describe all the options
|
||||||
/// that are valid to produce the type. The left hand side is the name of the option,
|
/// that are valid to produce the type. The left-hand side is the name of the option,
|
||||||
/// and the right hand side is the description.
|
/// and the right-hand side is the description.
|
||||||
fn options() -> &'static [(&'static str, OptionDescription<'static>)];
|
fn options() -> &'static [(&'static str, OptionDescription<'static>)];
|
||||||
|
|
||||||
/// Produces the type by taking the `options` and processing it into the output.
|
/// Produces the type by taking the `options` and processing it into the output.
|
||||||
@@ -44,7 +44,7 @@ pub trait OptionsRepresentable {
|
|||||||
|
|
||||||
/// For minimalism, we don't want a full argument parser. Instead, we use
|
/// For minimalism, we don't want a full argument parser. Instead, we use
|
||||||
/// a simple --xyz = xyz: None and --abc 123 = abc: Some("123") format.
|
/// a simple --xyz = xyz: None and --abc 123 = abc: Some("123") format.
|
||||||
/// We also support --abc=123 = abc: Some("123") format.
|
/// We also support the format: --abc=123
|
||||||
fn parse_raw() -> Result<BTreeMap<String, Option<String>>> {
|
fn parse_raw() -> Result<BTreeMap<String, Option<String>>> {
|
||||||
// Access the configured options for this type.
|
// Access the configured options for this type.
|
||||||
let configured: BTreeMap<_, _> = BTreeMap::from_iter(Self::options().to_vec());
|
let configured: BTreeMap<_, _> = BTreeMap::from_iter(Self::options().to_vec());
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ edition.workspace = true
|
|||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
bitflags.workspace = true
|
bitflags.workspace = true
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
|
shlex.workspace = true
|
||||||
spin.workspace = true
|
spin.workspace = true
|
||||||
uefi.workspace = true
|
uefi.workspace = true
|
||||||
uefi-raw.workspace = true
|
uefi-raw.workspace = true
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ use alloc::vec::Vec;
|
|||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use uefi::proto::loaded_image::{LoadOptionsError, LoadedImage};
|
use uefi::proto::loaded_image::{LoadOptionsError, LoadedImage};
|
||||||
|
|
||||||
/// Loads the command-line arguments passed to Sprout.
|
/// Loads the command-line arguments passed to the current image.
|
||||||
pub fn args() -> Result<Vec<String>> {
|
pub fn args() -> Result<Vec<String>> {
|
||||||
// Acquire the image handle of Sprout.
|
// Acquire the current image handle.
|
||||||
let handle = uefi::boot::image_handle();
|
let handle = uefi::boot::image_handle();
|
||||||
|
|
||||||
// Open the LoadedImage protocol for Sprout.
|
// Open the LoadedImage protocol for the current image.
|
||||||
let loaded_image = uefi::boot::open_protocol_exclusive::<LoadedImage>(handle)
|
let loaded_image = uefi::boot::open_protocol_exclusive::<LoadedImage>(handle)
|
||||||
.context("unable to open loaded image protocol for sprout")?;
|
.context("unable to open loaded image protocol for current image")?;
|
||||||
|
|
||||||
// Load the command-line argument string.
|
// Load the command-line argument string.
|
||||||
let options = match loaded_image.load_options_as_cstr16() {
|
let options = match loaded_image.load_options_as_cstr16() {
|
||||||
@@ -35,7 +35,7 @@ pub fn args() -> Result<Vec<String>> {
|
|||||||
let options = options.to_string();
|
let options = options.to_string();
|
||||||
|
|
||||||
// Use shlex to parse the options.
|
// Use shlex to parse the options.
|
||||||
// If shlex fails, we will fall back to a simple whitespace split.
|
// If shlex fails, we will perform a simple whitespace split.
|
||||||
let mut args = shlex::split(&options).unwrap_or_else(|| {
|
let mut args = shlex::split(&options).unwrap_or_else(|| {
|
||||||
options
|
options
|
||||||
.split_ascii_whitespace()
|
.split_ascii_whitespace()
|
||||||
@@ -43,14 +43,6 @@ pub fn args() -> Result<Vec<String>> {
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
});
|
});
|
||||||
|
|
||||||
// If there is a first argument, check if it is not an option.
|
|
||||||
// If it is not, we will assume it is the path to the executable and remove it.
|
|
||||||
if let Some(arg) = args.first()
|
|
||||||
&& !arg.starts_with('-')
|
|
||||||
{
|
|
||||||
args.remove(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Correct firmware that may add invalid arguments at the start.
|
// Correct firmware that may add invalid arguments at the start.
|
||||||
// Witnessed this on a Dell Precision 5690 when direct booting.
|
// Witnessed this on a Dell Precision 5690 when direct booting.
|
||||||
loop {
|
loop {
|
||||||
@@ -73,5 +65,13 @@ pub fn args() -> Result<Vec<String>> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there is a first argument, check if it is not an option.
|
||||||
|
// If it is not, we will assume it is the path to the executable and remove it.
|
||||||
|
if let Some(arg) = args.first()
|
||||||
|
&& !arg.starts_with('-')
|
||||||
|
{
|
||||||
|
args.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(args)
|
Ok(args)
|
||||||
}
|
}
|
||||||
@@ -29,6 +29,8 @@ pub mod strings;
|
|||||||
|
|
||||||
/// Implements support for the bootloader interface specification.
|
/// Implements support for the bootloader interface specification.
|
||||||
pub mod bootloader_interface;
|
pub mod bootloader_interface;
|
||||||
|
/// Acquire arguments from UEFI environment.
|
||||||
|
pub mod env;
|
||||||
/// Support code for the EFI framebuffer.
|
/// Support code for the EFI framebuffer.
|
||||||
pub mod framebuffer;
|
pub mod framebuffer;
|
||||||
/// Support code for the media loader protocol.
|
/// Support code for the media loader protocol.
|
||||||
|
|||||||
Reference in New Issue
Block a user