|
| 1 | +use crate::types::EfiGuid; |
| 2 | +use std::fs::{self, ReadDir}; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +const EFIVARFS_PATH: &'static str = "/sys/firmware/efi/efivars"; |
| 6 | + |
| 7 | +// variable file names have 1 or more characters, a dash, then a UUID (36 characters) |
| 8 | +const MIN_VAR_FILE_NAME_LEN: usize = 38; |
| 9 | + |
| 10 | +pub struct EfiVariables { |
| 11 | + path: PathBuf, |
| 12 | +} |
| 13 | + |
| 14 | +pub struct EfiVariablesNameIter { |
| 15 | + dir_entry_iter: Option<ReadDir>, |
| 16 | +} |
| 17 | + |
| 18 | +fn convert_name(name: Option<&str>) -> Result<String, String> { |
| 19 | + return match name { |
| 20 | + Some(n) => { |
| 21 | + if n.len() < MIN_VAR_FILE_NAME_LEN { |
| 22 | + return Err(format!( |
| 23 | + "file name {n} does not represent an EFI variable name" |
| 24 | + )); |
| 25 | + } |
| 26 | + if n.bytes().nth(n.len() - MIN_VAR_FILE_NAME_LEN + 1).unwrap() != b'-' { |
| 27 | + return Err(format!( |
| 28 | + "file name {n} does not represent an EFI variable name" |
| 29 | + )); |
| 30 | + } |
| 31 | + let guid_bytes = &n[n.len() - MIN_VAR_FILE_NAME_LEN + 2..n.len()]; |
| 32 | + let guid: Option<EfiGuid> = match guid_bytes.try_into() { |
| 33 | + Ok(g) => Some(g), |
| 34 | + Err(_) => None, |
| 35 | + }; |
| 36 | + if guid.is_none() { |
| 37 | + return Err(format!( |
| 38 | + "file name {n} does not represent an EFI variable name" |
| 39 | + )); |
| 40 | + } |
| 41 | + let suffix = &n[0..n.len() - MIN_VAR_FILE_NAME_LEN + 1]; |
| 42 | + |
| 43 | + return Ok(String::new() + guid_bytes + &"-" + suffix); |
| 44 | + } |
| 45 | + None => Err("no name provided".to_string()), |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +impl Iterator for EfiVariablesNameIter { |
| 50 | + type Item = String; |
| 51 | + |
| 52 | + fn next(&mut self) -> Option<Self::Item> { |
| 53 | + return match &mut self.dir_entry_iter { |
| 54 | + Some(iter) => { |
| 55 | + for entry in iter { |
| 56 | + let converted_name = match entry { |
| 57 | + Ok(entry) => convert_name(entry.file_name().to_str()), |
| 58 | + Err(e) => Err(e.to_string()), |
| 59 | + }; |
| 60 | + if converted_name.is_ok() { |
| 61 | + return converted_name.ok(); |
| 62 | + } |
| 63 | + } |
| 64 | + return None; |
| 65 | + } |
| 66 | + None => None, |
| 67 | + }; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Default for EfiVariables { |
| 72 | + fn default() -> Self { |
| 73 | + return EfiVariables { |
| 74 | + path: EFIVARFS_PATH.into(), |
| 75 | + }; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl EfiVariables { |
| 80 | + pub fn set_path(&mut self, path: PathBuf) -> &EfiVariables { |
| 81 | + self.path = path; |
| 82 | + return self; |
| 83 | + } |
| 84 | + |
| 85 | + pub fn list(&mut self) -> EfiVariablesNameIter { |
| 86 | + if self.path.is_dir() { |
| 87 | + let iter = fs::read_dir(&self.path); |
| 88 | + return EfiVariablesNameIter { |
| 89 | + dir_entry_iter: match iter { |
| 90 | + Ok(i) => Some(i), |
| 91 | + Err(_) => None, |
| 92 | + }, |
| 93 | + }; |
| 94 | + } |
| 95 | + return EfiVariablesNameIter { |
| 96 | + dir_entry_iter: None, |
| 97 | + }; |
| 98 | + } |
| 99 | +} |
0 commit comments