1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![warn(rust_2018_idioms)]

#[macro_use]
extern crate failure_derive;

use efibootnext;
use efivar;

pub use efibootnext::LoadOption;
pub use failure::Error;

mod error;

pub type Result<T> = std::result::Result<T, Error>;

pub struct Backend {
    var_manager: Box<dyn efivar::VarManager>,
}

impl Backend {
    pub fn init() -> Result<Self> {
        Ok(Self {
            var_manager: efivar::system(),
        })
    }

    pub fn load_options<'a>(&'a mut self) -> impl Iterator<Item = Result<LoadOption>> + 'a {
        efibootnext::load_options(self.var_manager.as_mut())
    }

    pub fn reboot_into(&mut self, num: u16) -> Result<()> {
        efibootnext::set_boot_next(self.var_manager.as_mut(), num)
            .map_err(error::RebootIntoErrorKind::SetBootNextError)?;
        simplereboot::reboot()
            .map_err(|e| Error::from(e))
            .map_err(error::RebootIntoErrorKind::RebootError)?;
        Ok(())
    }
}