Skip to main content

efivar_fix/boot/
writer.rs

1//! This module contains functions to write boot entries
2
3use crate::{
4    boot::BootVarFormat,
5    efi::{Variable, VariableFlags},
6    VarWriter,
7};
8
9use super::BootEntry;
10
11pub trait BootVarWriter {
12    fn create_boot_entry(&mut self, id: u16, entry: BootEntry) -> crate::Result<()>;
13    fn set_boot_order(&mut self, ids: Vec<u16>) -> crate::Result<()>;
14}
15
16impl<T: VarWriter> BootVarWriter for T {
17    fn set_boot_order(&mut self, ids: Vec<u16>) -> crate::Result<()> {
18        let bytes: Vec<u8> = ids.iter().flat_map(|id| id.to_le_bytes()).collect();
19
20        self.write(
21            &Variable::new("BootOrder"),
22            VariableFlags::default(),
23            &bytes,
24        )?;
25
26        log::debug!("Set BootOrder to {ids:?}");
27        Ok(())
28    }
29
30    /// Creates an EFI variable for a boot entry.
31    /// You then need to add it to the boot order using [`BootVarWriter::set_boot_order`].
32    fn create_boot_entry(&mut self, id: u16, entry: BootEntry) -> crate::Result<()> {
33        let bytes = entry.to_bytes();
34
35        self.write(
36            &Variable::new(&id.boot_var_format()),
37            VariableFlags::default(),
38            &bytes,
39        )?;
40
41        log::debug!("Created boot entry for ID {id}: {entry:?}");
42        Ok(())
43    }
44}