Skip to content

Commit 52494a1

Browse files
committed
Add --set-uefi-var
Not working yet Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 4c1d00a commit 52494a1

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

framework_lib/src/commandline/clap_std.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ struct ClapCli {
254254
#[arg(long)]
255255
s0ix_counter: bool,
256256

257+
/// Set UEFI variable (name, file path)
258+
#[arg(long)]
259+
set_uefi_var: Option<std::path::PathBuf>,
260+
257261
/// Hash a file of arbitrary data
258262
#[arg(long)]
259263
hash: Option<std::path::PathBuf>,
@@ -528,6 +532,7 @@ pub fn parse(args: &[String]) -> Cli {
528532
ec_hib_delay: args.ec_hib_delay,
529533
uptimeinfo: args.uptimeinfo,
530534
s0ix_counter: args.s0ix_counter,
535+
set_uefi_var: args.set_uefi_var,
531536
hash: args.hash.map(|x| x.into_os_string().into_string().unwrap()),
532537
driver: args.driver,
533538
pd_addrs,

framework_lib/src/commandline/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub struct Cli {
216216
pub ec_hib_delay: Option<Option<u32>>,
217217
pub uptimeinfo: bool,
218218
pub s0ix_counter: bool,
219+
pub set_uefi_var: Option<std::path::PathBuf>,
219220
pub hash: Option<String>,
220221
pub pd_addrs: Option<(u16, u16, u16)>,
221222
pub pd_ports: Option<(u8, u8, u8)>,
@@ -1510,6 +1511,24 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
15101511
} else {
15111512
println!("s0ix_counter: Unknown");
15121513
}
1514+
} else if let Some(filepath) = &args.set_uefi_var {
1515+
#[cfg(feature = "uefi")]
1516+
let data = crate::uefi::fs::shell_read_file(filepath);
1517+
#[cfg(not(feature = "uefi"))]
1518+
let data = match fs::read(filepath) {
1519+
Ok(data) => Some(data),
1520+
// TODO: Perhaps a more user-friendly error
1521+
Err(e) => {
1522+
println!("Error {:?}", e);
1523+
None
1524+
}
1525+
};
1526+
if let Some(data) = data {
1527+
println!("File");
1528+
println!(" Size: {:>20} B", data.len());
1529+
println!(" Size: {:>20} KB", data.len() / 1024);
1530+
os_specific::set_dbx(&data);
1531+
}
15131532
} else if args.test {
15141533
println!("Self-Test");
15151534
let result = selftest(&ec);

framework_lib/src/os_specific.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,26 @@ pub fn sleep(micros: u64) {
5555
}
5656
}
5757

58-
#[cfg(windows)]
59-
pub fn set_dbx() -> Option<()> {
60-
set_uefi_var("dbx", "d719b2cb-3d3a-4596-a3bc-dad00e67656f", &[], 0)
58+
pub const EFI_VARIABLE_NON_VOLATILE: u32 = 0x00000001;
59+
pub const EFI_VARIABLE_BOOTSERVICE_ACCESS: u32 = 0x00000002;
60+
pub const EFI_VARIABLE_RUNTIME_ACCESS: u32 = 0x00000004;
61+
//pub const EFI_VARIABLE_HARDWARE_ERROR_RECORD: u32 = 0x00000008;
62+
pub const EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS: u32 = 0x00000010;
63+
//pub const EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS: u32 = 0x00000020;
64+
pub const EFI_VARIABLE_APPEND_WRITE: u32 = 0x00000040;
65+
66+
pub fn set_dbx(data: &[u8]) -> Option<()> {
67+
let attrs = EFI_VARIABLE_NON_VOLATILE
68+
| EFI_VARIABLE_BOOTSERVICE_ACCESS
69+
| EFI_VARIABLE_RUNTIME_ACCESS
70+
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
71+
| EFI_VARIABLE_APPEND_WRITE;
72+
set_uefi_var("dbx", "d719b2cb-3d3a-4596-a3bc-dad00e67656f", data, attrs)
6173
}
6274

6375
#[cfg(windows)]
6476
pub fn set_uefi_var(name: &str, guid: &str, value: &[u8], attributes: u32) -> Option<()> {
65-
unsafe {
77+
let res = unsafe {
6678
SetFirmwareEnvironmentVariableExW(
6779
// PCWSTR
6880
&HSTRING::from(name),
@@ -72,6 +84,13 @@ pub fn set_uefi_var(name: &str, guid: &str, value: &[u8], attributes: u32) -> Op
7284
value.len() as u32,
7385
attributes,
7486
)
75-
.ok()
76-
}
87+
};
88+
println!("{:?}", res);
89+
res.ok()
90+
}
91+
92+
#[cfg(not(windows))]
93+
pub fn set_uefi_var(name: &str, guid: &str, value: &[u8], attributes: u32) -> Option<()> {
94+
error!("Setting UEFI variable not supported on this OS");
95+
None
7796
}

0 commit comments

Comments
 (0)