Skip to content

Add newtype enum for variable vendors and add IMAGE_SECURITY_DATABASE to it #273

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

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions src/table/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ impl RuntimeServices {

/// Get the size (in bytes) of a variable. This can be used to find out how
/// big of a buffer should be passed in to `get_variable`.
pub fn get_variable_size(&self, name: &CStr16, vendor: &Guid) -> Result<usize> {
pub fn get_variable_size(&self, name: &CStr16, vendor: &VariableVendor) -> Result<usize> {
let mut data_size = 0;
let status = unsafe {
(self.get_variable)(
name.as_ptr(),
vendor,
&vendor.0,
ptr::null_mut(),
&mut data_size,
ptr::null_mut(),
Expand All @@ -139,15 +139,15 @@ impl RuntimeServices {
pub fn get_variable<'a>(
&self,
name: &CStr16,
vendor: &Guid,
vendor: &VariableVendor,
buf: &'a mut [u8],
) -> Result<(&'a [u8], VariableAttributes)> {
let mut attributes = VariableAttributes::empty();
let mut data_size = buf.len();
unsafe {
(self.get_variable)(
name.as_ptr(),
vendor,
&vendor.0,
&mut attributes,
&mut data_size,
buf.as_mut_ptr(),
Expand Down Expand Up @@ -189,7 +189,10 @@ impl RuntimeServices {
break;
};

all_variables.push(VariableKey { name, vendor });
all_variables.push(VariableKey {
name,
vendor: VariableVendor(vendor),
});
}
Status::BUFFER_TOO_SMALL => {
// The name buffer passed in was too small, resize it to be
Expand Down Expand Up @@ -220,12 +223,19 @@ impl RuntimeServices {
pub fn set_variable(
&self,
name: &CStr16,
vendor: &Guid,
vendor: &VariableVendor,
attributes: VariableAttributes,
data: &[u8],
) -> Result {
unsafe {
(self.set_variable)(name.as_ptr(), vendor, attributes, data.len(), data.as_ptr()).into()
(self.set_variable)(
name.as_ptr(),
&vendor.0,
attributes,
data.len(),
data.as_ptr(),
)
.into()
}
}

Expand Down Expand Up @@ -482,22 +492,38 @@ bitflags! {
}
}

/// Vendor GUID used to access global variables.
pub const GLOBAL_VARIABLE: Guid = Guid::from_values(
0x8be4df61,
0x93ca,
0x11d2,
0xaa0d,
[0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c],
);
newtype_enum! {
/// Variable vendor GUID. This serves as a namespace for variables to
/// avoid naming conflicts between vendors. The UEFI specification
/// defines some special values, and vendors will define their own.
pub enum VariableVendor: Guid => {
/// Used to access global variables.
GLOBAL_VARIABLE = Guid::from_values(
0x8be4df61,
0x93ca,
0x11d2,
0xaa0d,
[0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c],
),

/// Used to access EFI signature database variables.
IMAGE_SECURITY_DATABASE = Guid::from_values(
0xd719b2cb,
0x3d3a,
0x4596,
0xa3bc,
[0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f],
),
}
}

/// Unique key for a variable.
#[cfg(feature = "exts")]
#[derive(Debug)]
pub struct VariableKey {
name: Vec<u16>,
/// Unique identifier for the vendor.
pub vendor: Guid,
pub vendor: VariableVendor,
}

#[cfg(feature = "exts")]
Expand All @@ -520,10 +546,10 @@ impl fmt::Display for VariableKey {

write!(f, ", vendor: ")?;

if self.vendor == GLOBAL_VARIABLE {
if self.vendor == VariableVendor::GLOBAL_VARIABLE {
write!(f, "GLOBAL_VARIABLE")?;
} else {
write!(f, "{}", self.vendor)?;
write!(f, "{}", self.vendor.0)?;
}

write!(f, " }}")
Expand Down
6 changes: 3 additions & 3 deletions uefi-test-runner/src/runtime/vars.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;
use log::info;
use uefi::prelude::*;
use uefi::table::runtime::VariableAttributes;
use uefi::table::runtime::{VariableAttributes, VariableVendor};
use uefi::{CStr16, Guid};

struct CString16(Vec<u16>);
Expand All @@ -27,13 +27,13 @@ fn test_variables(rt: &RuntimeServices) {
let test_attrs = VariableAttributes::BOOTSERVICE_ACCESS | VariableAttributes::RUNTIME_ACCESS;

// Arbitrary GUID generated for this test.
let vendor = Guid::from_values(
let vendor = VariableVendor(Guid::from_values(
0x9baf21cf,
0xe187,
0x497e,
0xae77,
[0x5b, 0xd8, 0xb0, 0xe0, 0x97, 0x03],
);
));

info!("Testing set_variable");
rt.set_variable(name.as_cstr16(), &vendor, test_attrs, test_value)
Expand Down