use alloc::vec::Vec;
use const_format::formatcp;
use uefi::{CStr16, Handle, cstr16};
use crate::{
config::{
Config,
builder::ConfigBuilder,
parsers::{ConfigParser, Parsers},
},
system::{
fs::UefiFileSystem,
helper::{get_path_cstr, str_to_cstr},
},
};
const FALLBACK_PREFIX: &CStr16 = cstr16!("\\EFI\\BOOT");
const FALLBACK_PREFIX_STR: &str = "\\EFI\\BOOT";
const FALLBACK_SUFFIX: &str = ".efi";
const FILENAME: &str = get_filename();
pub struct FallbackConfig;
impl ConfigParser for FallbackConfig {
fn parse_configs(fs: &mut UefiFileSystem, handle: Handle, configs: &mut Vec<Config>) {
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 = formatcp!("{FALLBACK_PREFIX_STR}\\{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());
}
}
}
const fn get_filename() -> &'static str {
if cfg!(target_arch = "x86") {
"BOOTia32.efi"
} else if cfg!(target_arch = "x86_64") {
"BOOTx64.efi"
} else if cfg!(target_arch = "arm") {
"BOOTaa32.efi"
} else if cfg!(target_arch = "aarch64") {
"BOOTaa64.efi"
} else {
""
}
}