use alloc::{format, vec::Vec};
use uefi::{CStr16, Handle, cstr16};
use crate::{
config::{
Config,
builder::ConfigBuilder,
parsers::{ConfigParser, Parsers},
},
system::{
fs::UefiFileSystem,
helper::{get_arch, get_path_cstr, str_to_cstr},
},
};
const FALLBACK_PREFIX: &CStr16 = cstr16!("\\EFI\\BOOT");
const FALLBACK_SUFFIX: &str = ".efi";
pub struct FallbackConfig;
impl ConfigParser for FallbackConfig {
fn parse_configs(fs: &mut UefiFileSystem, handle: Handle, configs: &mut Vec<Config>) {
let filename = match get_arch().as_deref().map(alloc::string::String::as_str) {
Some("x86") => "BOOTia32.efi",
Some("x64") => "BOOTx64.efi",
Some("arm") => "BOOTaa32.efi",
Some("aa64") => "BOOTaa64.efi",
_ => return,
};
let Ok(filename) = str_to_cstr(filename) else {
return; };
let Ok(path) = get_path_cstr(FALLBACK_PREFIX, &filename) else {
return; };
if fs.exists(&path)
&& let Ok(volume_label) = fs.get_volume_label()
{
let efi_path = format!("{FALLBACK_PREFIX}\\{filename}");
let title = if volume_label.is_empty() {
&filename
} else {
&volume_label };
let config = ConfigBuilder::new(&filename, FALLBACK_SUFFIX)
.efi_path(efi_path)
.title(title)
.sort_key("fallback")
.fs_handle(handle)
.origin(Parsers::Fallback);
configs.push(config.build());
}
}
}