move some code to utils and move around functions

This commit is contained in:
2025-10-01 21:30:43 -07:00
parent 3b8243d401
commit 8a2c08bb57
5 changed files with 45 additions and 39 deletions

29
src/utils.rs Normal file
View File

@@ -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::<DevicePathFromText>(
uefi::boot::get_handle_for_protocol::<DevicePathFromText>()
.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<u8> {
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(
uefi::boot::get_handle_for_protocol::<SimpleFileSystem>().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")
}