From 8a2c08bb57477939e52e8e83be8b840d0d2a4077 Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Wed, 1 Oct 2025 21:30:43 -0700 Subject: [PATCH] move some code to utils and move around functions --- src/config.rs | 13 ++----------- src/main.rs | 5 ++++- src/modules.rs | 12 +++++------- src/modules/chainloader.rs | 25 +++++-------------------- src/utils.rs | 29 +++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 src/utils.rs diff --git a/src/config.rs b/src/config.rs index 803de11..11cb8d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,5 @@ +use crate::utils; use serde::{Deserialize, Serialize}; -use uefi::cstr16; -use uefi::fs::{FileSystem, Path}; -use uefi::proto::media::fs::SimpleFileSystem; #[derive(Serialize, Deserialize, Default)] pub struct RootConfiguration { @@ -21,13 +19,6 @@ pub struct ChainloaderConfiguration { } pub fn load() -> RootConfiguration { - let fs = uefi::boot::open_protocol_exclusive::( - uefi::boot::get_handle_for_protocol::().expect("no filesystem protocol"), - ) - .expect("unable to open filesystem protocol"); - let mut fs = FileSystem::new(fs); - let content = fs - .read(Path::new(cstr16!("sprout.toml"))) - .expect("unable to read sprout.toml file"); + let content = utils::read_file_contents("sprout.toml"); toml::from_slice(&content).expect("unable to parse sprout.toml file") } diff --git a/src/main.rs b/src/main.rs index ccb751b..0b7ed06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,13 @@ pub mod config; pub mod modules; pub mod setup; +pub mod utils; fn main() { setup::init(); let config = config::load(); - modules::execute(config.modules); + for module in config.modules { + modules::execute(module); + } } diff --git a/src/modules.rs b/src/modules.rs index 1f23c47..7bdee76 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -2,12 +2,10 @@ use crate::config::ModuleConfiguration; pub mod chainloader; -pub fn execute(modules: Vec) { - for module in modules { - if let Some(chainloader) = module.chainloader { - chainloader::chainloader(chainloader); - } else { - panic!("unknown module configuration"); - } +pub fn execute(module: ModuleConfiguration) { + if let Some(chainloader) = module.chainloader { + chainloader::chainloader(chainloader); + } else { + panic!("unknown module configuration"); } } diff --git a/src/modules/chainloader.rs b/src/modules/chainloader.rs index 8cbb0a5..4bf6b37 100644 --- a/src/modules/chainloader.rs +++ b/src/modules/chainloader.rs @@ -1,26 +1,11 @@ use crate::config::ChainloaderConfiguration; +use crate::utils::text_to_device_path; use log::info; -use uefi::proto::loaded_image::LoadedImage; -use uefi::{ - CString16, - proto::device_path::{ - DevicePath, LoadedImageDevicePath, PoolDevicePath, - text::{AllowShortcuts, DevicePathFromText, DisplayOnly}, - }, +use uefi::proto::device_path::{ + DevicePath, LoadedImageDevicePath, + text::{AllowShortcuts, DisplayOnly}, }; - -fn text_to_device_path(path: &str) -> PoolDevicePath { - let path = CString16::try_from(path).expect("unable to convert path to CString16"); - let device_path_from_text = uefi::boot::open_protocol_exclusive::( - uefi::boot::get_handle_for_protocol::() - .expect("no device path from text protocol"), - ) - .expect("unable to open device path from text protocol"); - - device_path_from_text - .convert_text_to_device_path(&path) - .expect("unable to convert text to device path") -} +use uefi::proto::loaded_image::LoadedImage; pub fn chainloader(configuration: ChainloaderConfiguration) { let sprout_image = uefi::boot::image_handle(); diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..1bea02e --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,29 @@ +use uefi::CString16; +use uefi::fs::{FileSystem, Path}; +use uefi::proto::device_path::PoolDevicePath; +use uefi::proto::device_path::text::DevicePathFromText; +use uefi::proto::media::fs::SimpleFileSystem; + +pub fn text_to_device_path(path: &str) -> PoolDevicePath { + let path = CString16::try_from(path).expect("unable to convert path to CString16"); + let device_path_from_text = uefi::boot::open_protocol_exclusive::( + uefi::boot::get_handle_for_protocol::() + .expect("no device path from text protocol"), + ) + .expect("unable to open device path from text protocol"); + + device_path_from_text + .convert_text_to_device_path(&path) + .expect("unable to convert text to device path") +} + +pub fn read_file_contents(path: &str) -> Vec { + let fs = uefi::boot::open_protocol_exclusive::( + uefi::boot::get_handle_for_protocol::().expect("no filesystem protocol"), + ) + .expect("unable to open filesystem protocol"); + let mut fs = FileSystem::new(fs); + let path = CString16::try_from(path).expect("unable to convert path to CString16"); + let content = fs.read(Path::new(&path)); + content.expect("unable to read file contents") +}