#![doc(html_root_url = "https://docs.rs/efivar/2.0.0")]
pub mod efi;
#[cfg(feature = "store")]
pub mod store;
pub mod boot;
mod enumerator;
mod error;
pub mod push;
mod reader;
mod sys;
pub mod test_utils;
pub mod utils;
mod writer;
use boot::{BootVarReader, BootVarWriter};
pub use crate::enumerator::VarEnumerator;
pub use crate::reader::*;
pub use crate::writer::VarWriter;
pub use crate::error::Error;
pub type Result<T> = std::result::Result<T, Error>;
pub trait VarManager:
VarEnumerator + VarReader + VarWriter + BootVarReader + BootVarWriter
{
}
#[derive(Debug, thiserror::Error)]
pub enum VarManagerInitError {
#[error("EFI variables not available (did you boot in BIOS/MBR mode ?)")]
EFIVariablesNotAvailable,
}
use crate::sys::SystemManager;
pub fn system() -> std::result::Result<Box<dyn VarManager>, VarManagerInitError> {
SystemManager::new().map(|m| Box::new(m) as Box<dyn VarManager>)
}
#[cfg(feature = "store")]
pub fn file_store<P: Into<std::path::PathBuf>>(filename: P) -> Box<dyn VarManager> {
Box::new(store::FileStore::new(filename.into()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "store")]
fn file_store_roundtrip() {
use crate::efi::{Variable, VariableFlags};
{
let mut store = file_store("doc-test.toml");
let value = vec![1, 2, 3, 4];
store
.write(
&Variable::new("BootOrder"),
VariableFlags::NON_VOLATILE,
&value,
)
.expect("Failed to write value in store");
let (data, attributes) = store.read(&Variable::new("BootOrder")).unwrap();
assert_eq!(attributes, VariableFlags::NON_VOLATILE);
assert_eq!(data, value);
}
std::fs::remove_file("doc-test.toml")
.expect("Failed to remove temporary file doc-test.toml");
}
}