-
Notifications
You must be signed in to change notification settings - Fork 141
efivars: Parse efivar as UTF-16 LE bytes #1633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -188,6 +188,63 @@ pub(crate) fn digested_pullspec(image: &str, digest: &str) -> String { | |||||||||||||
format!("{image}@{digest}") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(feature = "composefs-backend")] | ||||||||||||||
#[derive(Debug)] | ||||||||||||||
pub enum EfiError { | ||||||||||||||
SystemNotUEFI, | ||||||||||||||
MissingVar, | ||||||||||||||
#[allow(dead_code)] | ||||||||||||||
InvalidData(&'static str), | ||||||||||||||
#[allow(dead_code)] | ||||||||||||||
Io(std::io::Error), | ||||||||||||||
Comment on lines
+196
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(feature = "composefs-backend")] | ||||||||||||||
impl From<std::io::Error> for EfiError { | ||||||||||||||
fn from(e: std::io::Error) -> Self { | ||||||||||||||
EfiError::Io(e) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(feature = "composefs-backend")] | ||||||||||||||
pub fn read_uefi_var(var_name: &str) -> Result<String, EfiError> { | ||||||||||||||
use crate::install::EFIVARFS; | ||||||||||||||
use cap_std_ext::cap_std::ambient_authority; | ||||||||||||||
|
||||||||||||||
let efivarfs = match Dir::open_ambient_dir(EFIVARFS, ambient_authority()) { | ||||||||||||||
Ok(dir) => dir, | ||||||||||||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Err(EfiError::SystemNotUEFI), | ||||||||||||||
Err(e) => Err(e)?, | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
match efivarfs.read(var_name) { | ||||||||||||||
Ok(loader_bytes) => { | ||||||||||||||
if loader_bytes.len() % 2 != 0 { | ||||||||||||||
return Err(EfiError::InvalidData( | ||||||||||||||
"EFI var length is not valid UTF-16 LE", | ||||||||||||||
)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// EFI vars are UTF-16 LE | ||||||||||||||
let loader_u16_bytes: Vec<u16> = loader_bytes | ||||||||||||||
.chunks_exact(2) | ||||||||||||||
.map(|x| u16::from_le_bytes([x[0], x[1]])) | ||||||||||||||
.collect(); | ||||||||||||||
|
||||||||||||||
let loader = String::from_utf16(&loader_u16_bytes) | ||||||||||||||
.map_err(|_| EfiError::InvalidData("EFI var is not UTF-16"))?; | ||||||||||||||
|
||||||||||||||
return Ok(loader); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did coreos/cap-std-ext#78 motivated by this btw |
||||||||||||||
return Err(EfiError::MissingVar); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Err(e) => Err(e)?, | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
Comment on lines
+210
to
+246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pub fn read_uefi_var(var_name: &str) -> Result<String, EfiError> {
use crate::install::EFIVARFS;
use cap_std_ext::cap_std::ambient_authority;
let efivarfs = Dir::open_ambient_dir(EFIVARFS, ambient_authority()).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
EfiError::SystemNotUEFI
} else {
e.into()
}
})?;
let loader_bytes = efivarfs.read(var_name).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
EfiError::MissingVar
} else {
e.into()
}
})?;
if loader_bytes.len() % 2 != 0 {
return Err(EfiError::InvalidData(
"EFI var length is not valid UTF-16 LE",
));
}
// EFI vars are UTF-16 LE
let loader_u16_bytes: Vec<u16> = loader_bytes
.chunks_exact(2)
.map(|x| u16::from_le_bytes([x[0], x[1]]))
.collect();
String::from_utf16(&loader_u16_bytes)
.map_err(|_| EfiError::InvalidData("EFI var is not UTF-16"))
} |
||||||||||||||
|
||||||||||||||
/// Computes a relative path from `from` to `to`. | ||||||||||||||
/// | ||||||||||||||
/// Both `from` and `to` must be absolute paths. | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The match arms for
EfiError::SystemNotUEFI
andEfiError::MissingVar
have the same logic. They can be combined into a single arm using the|
operator for conciseness and better readability.