2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::{Context, Result};
|
2025-10-01 21:30:43 -07:00
|
|
|
use uefi::CString16;
|
|
|
|
|
use uefi::fs::{FileSystem, Path};
|
2025-10-02 00:24:19 -07:00
|
|
|
use uefi::proto::device_path::text::{AllowShortcuts, DevicePathFromText, DisplayOnly};
|
|
|
|
|
use uefi::proto::device_path::{DevicePath, PoolDevicePath};
|
2025-10-01 21:30:43 -07:00
|
|
|
use uefi::proto::media::fs::SimpleFileSystem;
|
|
|
|
|
|
2025-10-11 14:11:31 -07:00
|
|
|
pub mod framebuffer;
|
|
|
|
|
|
2025-10-11 14:35:29 -07:00
|
|
|
pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
|
|
|
|
|
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
|
2025-10-01 21:30:43 -07:00
|
|
|
let device_path_from_text = uefi::boot::open_protocol_exclusive::<DevicePathFromText>(
|
|
|
|
|
uefi::boot::get_handle_for_protocol::<DevicePathFromText>()
|
2025-10-11 14:35:29 -07:00
|
|
|
.context("no device path from text protocol")?,
|
2025-10-01 21:30:43 -07:00
|
|
|
)
|
2025-10-11 14:35:29 -07:00
|
|
|
.context("unable to open device path from text protocol")?;
|
2025-10-01 21:30:43 -07:00
|
|
|
|
|
|
|
|
device_path_from_text
|
|
|
|
|
.convert_text_to_device_path(&path)
|
2025-10-11 14:35:29 -07:00
|
|
|
.context("unable to convert text to device path")
|
2025-10-01 21:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:35:29 -07:00
|
|
|
pub fn device_path_root(path: &DevicePath) -> Result<String> {
|
2025-10-02 00:24:19 -07:00
|
|
|
let mut path = path
|
|
|
|
|
.node_iter()
|
|
|
|
|
.filter_map(|item| {
|
2025-10-11 14:35:29 -07:00
|
|
|
let item = item.to_string(DisplayOnly(false), AllowShortcuts(false));
|
|
|
|
|
if item
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|item| item.to_string().contains("("))
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
{
|
|
|
|
|
Some(item.unwrap_or_default())
|
2025-10-02 00:24:19 -07:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.map(|item| item.to_string())
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join("/");
|
|
|
|
|
path.push('/');
|
2025-10-11 14:35:29 -07:00
|
|
|
Ok(path)
|
2025-10-02 00:24:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:35:29 -07:00
|
|
|
pub fn read_file_contents(path: &str) -> Result<Vec<u8>> {
|
2025-10-01 21:30:43 -07:00
|
|
|
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(
|
2025-10-11 14:35:29 -07:00
|
|
|
uefi::boot::get_handle_for_protocol::<SimpleFileSystem>()
|
|
|
|
|
.context("no filesystem protocol")?,
|
2025-10-01 21:30:43 -07:00
|
|
|
)
|
2025-10-11 14:35:29 -07:00
|
|
|
.context("unable to open filesystem protocol")?;
|
2025-10-01 21:30:43 -07:00
|
|
|
let mut fs = FileSystem::new(fs);
|
2025-10-11 14:35:29 -07:00
|
|
|
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
|
2025-10-01 21:30:43 -07:00
|
|
|
let content = fs.read(Path::new(&path));
|
2025-10-11 14:35:29 -07:00
|
|
|
content.context("unable to read file contents")
|
2025-10-01 21:30:43 -07:00
|
|
|
}
|