Skip to content

Commit 5740c14

Browse files
authored
Merge pull request #62 from edera-dev/chore/code-cleanup-stamp-iter
chore(sprout): implement iterator stamping to cleanup code
2 parents 95e0360 + c295de4 commit 5740c14

7 files changed

Lines changed: 30 additions & 27 deletions

File tree

crates/boot/src/actions/chainload.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfigurat
2121
// Resolve the path to the image to chainload.
2222
let resolved = eficore::path::resolve_path(
2323
Some(context.root().loaded_image_path()?),
24-
&context.stamp(&configuration.path),
24+
context.stamp(&configuration.path),
2525
)
2626
.context("unable to resolve chainload path")?;
2727

@@ -38,8 +38,7 @@ pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfigurat
3838
.context("unable to open loaded image protocol")?;
3939

4040
// Stamp and combine the options to pass to the image.
41-
let options =
42-
utils::combine_options(configuration.options.iter().map(|item| context.stamp(item)));
41+
let options = utils::combine_options(context.stamp_iter(configuration.options.iter()));
4342

4443
// Pass the load options to the image.
4544
// If no options are provided, the resulting string will be empty.
@@ -50,6 +49,7 @@ pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfigurat
5049
.context("unable to convert chainloader options to CString16")?,
5150
);
5251

52+
// Ensure the chainloader options limit is not exceeded.
5353
if options.num_bytes() > u32::MAX as usize {
5454
bail!("chainloader options too large");
5555
}

crates/boot/src/actions/edera.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,11 @@ use uefi::Guid;
2020
/// Builds a configuration string for the Xen EFI stub using the specified `configuration`.
2121
fn build_xen_config(context: Rc<SproutContext>, configuration: &EderaConfiguration) -> String {
2222
// Stamp xen options and combine them.
23-
let xen_options = utils::combine_options(
24-
configuration
25-
.xen_options
26-
.iter()
27-
.map(|item| context.stamp(item)),
28-
);
23+
let xen_options = utils::combine_options(context.stamp_iter(configuration.xen_options.iter()));
2924

3025
// Stamp kernel options and combine them.
31-
let kernel_options = utils::combine_options(
32-
configuration
33-
.kernel_options
34-
.iter()
35-
.map(|item| context.stamp(item)),
36-
);
26+
let kernel_options =
27+
utils::combine_options(context.stamp_iter(configuration.kernel_options.iter()));
3728

3829
// xen config file format is ini-like
3930
[

crates/boot/src/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ impl SproutContext {
269269
Self::stamp_values(&self.all_values(), text.as_ref()).1
270270
}
271271

272+
/// Stamps all the items from the iterator `input` with all the values in this [SproutContext]
273+
/// and it's parents. This calls [self.stamp] on each item.
274+
pub fn stamp_iter(
275+
&self,
276+
input: impl Iterator<Item = impl AsRef<str>>,
277+
) -> impl Iterator<Item = String> {
278+
input.map(|item| self.stamp(item))
279+
}
280+
272281
/// Unloads a [SproutContext] back into an owned context. This
273282
/// may not succeed if something else is holding onto the value.
274283
pub fn unload(self: Rc<SproutContext>) -> Option<SproutContext> {

crates/boot/src/drivers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn load_driver(context: Rc<SproutContext>, driver: &DriverDeclaration) -> Result
1818
// Resolve the path to the driver image.
1919
let resolved = eficore::path::resolve_path(
2020
Some(context.root().loaded_image_path()?),
21-
&context.stamp(&driver.path),
21+
context.stamp(&driver.path),
2222
)
2323
.context("unable to resolve path to driver")?;
2424

crates/boot/src/generators/list.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ pub fn generate(
2222

2323
// Stamp the entry title and actions from the template.
2424
let mut entry = list.entry.clone();
25-
entry.actions = entry
26-
.actions
27-
.into_iter()
28-
.map(|action| context.stamp(action))
29-
.collect();
25+
26+
// Stamp all the actions this entry references.
27+
entry.actions = context.stamp_iter(entry.actions.into_iter()).collect();
28+
3029
// Push the entry into the list with the new context.
3130
entries.push(BootableEntry::new(
3231
index.to_string(),

crates/eficore/src/path.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fn cstring16_contains_char(string: &CString16, c: char) -> bool {
4848

4949
/// Parses the input `path` as a [DevicePath].
5050
/// Uses the [DevicePathFromText] protocol exclusively, and will fail if it cannot acquire the protocol.
51-
pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
52-
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
51+
pub fn text_to_device_path(path: impl AsRef<str>) -> Result<PoolDevicePath> {
52+
let path = CString16::try_from(path.as_ref()).context("unable to convert path to CString16")?;
5353
let device_path_from_text = uefi::boot::open_protocol_exclusive::<DevicePathFromText>(
5454
uefi::boot::get_handle_for_protocol::<DevicePathFromText>()
5555
.context("no device path from text protocol")?,
@@ -113,8 +113,13 @@ pub fn device_path_subpath(path: &DevicePath) -> Result<String> {
113113
/// Resolve a path specified by `input` to its various components.
114114
/// Uses `default_root_path` as the base root if one is not specified in the path.
115115
/// Returns [ResolvedPath] which contains the resolved components.
116-
pub fn resolve_path(default_root_path: Option<&DevicePath>, input: &str) -> Result<ResolvedPath> {
117-
let mut path = text_to_device_path(input).context("unable to convert text to path")?;
116+
pub fn resolve_path(
117+
default_root_path: Option<&DevicePath>,
118+
input: impl ToString,
119+
) -> Result<ResolvedPath> {
120+
let mut input = input.to_string();
121+
122+
let mut path = text_to_device_path(&input).context("unable to convert text to path")?;
118123
let path_has_device = path
119124
.node_iter()
120125
.next()
@@ -125,7 +130,6 @@ pub fn resolve_path(default_root_path: Option<&DevicePath>, input: &str) -> Resu
125130
.map(|it| it.to_string().contains('('))
126131
.unwrap_or(false);
127132
if !path_has_device {
128-
let mut input = input.to_string();
129133
if !input.starts_with('\\') {
130134
input.insert(0, '\\');
131135
}

crates/eficore/src/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a> ShimInput<'a> {
8484
let path = path
8585
.to_string(DisplayOnly(false), AllowShortcuts(false))
8686
.context("unable to convert device path to string")?;
87-
let path = crate::path::resolve_path(None, &path.to_string())
87+
let path = crate::path::resolve_path(None, path.to_string())
8888
.context("unable to resolve path")?;
8989
// Read the file path.
9090
let data = path.read_file()?;

0 commit comments

Comments
 (0)