Skip to content

Commit b5f2ba0

Browse files
committed
guid-create: Build from crates.io on non-UEFI
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 73745a3 commit b5f2ba0

File tree

7 files changed

+170
-16
lines changed

7 files changed

+170
-16
lines changed

Cargo.lock

Lines changed: 89 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework_lib/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ spin = { version = "0.9.8" }
3434
no-std-compat = { version = "0.4.1", features = [ "alloc" ] }
3535
hidapi = { version = "2.6.3", features = [ "windows-native" ], optional = true }
3636
rusb = { version = "0.9.4", optional = true }
37-
guid-create = { git = "https://github.com/FrameworkComputer/guid-create", branch = "no-rand", default-features = false }
3837

3938
[target.'cfg(target_os = "uefi")'.dependencies]
4039
uefi = { version = "0.20", features = ["alloc"] }
4140
uefi-services = "0.17"
4241
plain = "0.2.3"
4342
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false }
4443
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std", default-features = false }
44+
guid-create-no-std = { package = "guid-create", git = "https://github.com/FrameworkComputer/guid-create", branch = "no_std", default-features = false }
4545

4646
[target.'cfg(windows)'.dependencies]
4747
wmi = "0.15.0"
@@ -51,6 +51,7 @@ clap = { version = "4.5", features = ["derive", "cargo"] }
5151
clap-num = { version = "1.2.0" }
5252
clap-verbosity-flag = { version = "2.2.1" }
5353
windows-version = "0.1.4"
54+
guid-create = "0.4.1"
5455

5556
[target.'cfg(unix)'.dependencies]
5657
libc = "0.2.155"
@@ -61,6 +62,7 @@ env_logger = "0.11"
6162
clap = { version = "4.5", features = ["derive", "cargo"] }
6263
clap-num = { version = "1.2.0" }
6364
clap-verbosity-flag = { version = "2.2.1" }
65+
guid-create = "0.4.1"
6466

6567
[target.'cfg(windows)'.dependencies.windows]
6668
version = "0.59.0"

framework_lib/src/capsule.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
1111
use std::prelude::v1::*;
1212

13+
use crate::guid::CGuid;
1314
use core::prelude::rust_2021::derive;
14-
use guid_create::Guid;
1515
#[cfg(not(feature = "uefi"))]
1616
use std::fs::File;
1717
#[cfg(not(feature = "uefi"))]
@@ -21,7 +21,7 @@ use std::io::prelude::*;
2121
#[repr(C)]
2222
pub struct EfiCapsuleHeader {
2323
/// A GUID that defines the contents of a capsule.
24-
pub capsule_guid: Guid,
24+
pub capsule_guid: CGuid,
2525

2626
/// The size of the capsule header. This may be larger than the size of
2727
/// the EFI_CAPSULE_HEADER since CapsuleGuid may imply
@@ -205,14 +205,14 @@ mod tests {
205205
let data = fs::read(capsule_path).unwrap();
206206
let cap = parse_capsule_header(&data).unwrap();
207207
let expected_header = EfiCapsuleHeader {
208-
capsule_guid: Guid::from(esrt::WINUX_GUID),
208+
capsule_guid: CGuid::from(esrt::WINUX_GUID),
209209
header_size: 28,
210210
flags: 65536,
211211
capsule_image_size: 676898,
212212
};
213213
assert_eq!(cap, expected_header);
214214

215-
assert_eq!(cap.capsule_guid, Guid::from(esrt::WINUX_GUID));
215+
assert_eq!(cap.capsule_guid, CGuid::from(esrt::WINUX_GUID));
216216
let ux_header = parse_ux_header(&data);
217217
assert_eq!(
218218
ux_header,

framework_lib/src/commandline/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
//! Can be easily re-used from any OS or UEFI shell.
44
//! We have implemented both in the `framework_tool` and `framework_uefi` crates.
55
6+
use crate::guid::CGuid;
67
use alloc::format;
78
use alloc::string::String;
89
use alloc::string::ToString;
910
use alloc::vec::Vec;
10-
use guid_create::{Guid, GUID};
11+
#[cfg(not(feature = "uefi"))]
12+
use guid_create::GUID;
13+
#[cfg(feature = "uefi")]
14+
use guid_create_no_std::GUID;
1115
use log::Level;
1216
use num_traits::FromPrimitive;
1317

@@ -1270,7 +1274,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
12701274
println!(" Size: {:>20} B", data.len());
12711275
println!(" Size: {:>20} KB", data.len() / 1024);
12721276
if let Some(header) = analyze_capsule(&data) {
1273-
if header.capsule_guid == Guid::from(esrt::WINUX_GUID) {
1277+
if header.capsule_guid == CGuid::from(esrt::WINUX_GUID) {
12741278
let ux_header = capsule::parse_ux_header(&data);
12751279
if let Some(dump_path) = &args.dump {
12761280
// TODO: Better error handling, rather than just panicking

framework_lib/src/esrt/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
use log::{debug, error, info, trace};
1515
use std::prelude::v1::*;
1616

17+
use crate::guid::CGuid;
1718
use core::prelude::v1::derive;
18-
use guid_create::{Guid, GUID};
19+
#[cfg(not(feature = "uefi"))]
20+
use guid_create::GUID;
21+
#[cfg(feature = "uefi")]
22+
use guid_create_no_std::GUID;
1923

2024
#[cfg(target_os = "linux")]
2125
use std::fs;
@@ -193,7 +197,7 @@ pub enum FrameworkGuidKind {
193197
Unknown,
194198
}
195199

196-
pub fn match_guid_kind(guid: &Guid) -> FrameworkGuidKind {
200+
pub fn match_guid_kind(guid: &CGuid) -> FrameworkGuidKind {
197201
match GUID::from(*guid) {
198202
TGL_BIOS_GUID => FrameworkGuidKind::TglBios,
199203
ADL_BIOS_GUID => FrameworkGuidKind::AdlBios,
@@ -292,7 +296,7 @@ impl UpdateStatus {
292296
// TODO: Decode into proper Rust types
293297
#[derive(Clone)]
294298
pub struct EsrtResourceEntry {
295-
pub fw_class: Guid,
299+
pub fw_class: CGuid,
296300
pub fw_type: u32, // ResourceType
297301
pub fw_version: u32,
298302
pub lowest_supported_fw_version: u32,
@@ -364,7 +368,7 @@ fn esrt_from_sysfs(dir: &Path) -> io::Result<Esrt> {
364368
let last_attempt_version = fs::read_to_string(path.join("last_attempt_version"))?;
365369
let last_attempt_status = fs::read_to_string(path.join("last_attempt_status"))?;
366370
let esrt = EsrtResourceEntry {
367-
fw_class: Guid::from(
371+
fw_class: CGuid::from(
368372
GUID::parse(fw_class.trim()).expect("Kernel provided wrong value"),
369373
),
370374
fw_type: fw_type
@@ -436,7 +440,7 @@ pub fn get_esrt() -> Option<Esrt> {
436440
let ver_str = caps.get(2).unwrap().as_str().to_string();
437441

438442
let guid = GUID::parse(guid_str.trim()).expect("Kernel provided wrong value");
439-
let guid_kind = match_guid_kind(&Guid::from(guid));
443+
let guid_kind = match_guid_kind(&CGuid::from(guid));
440444
let ver = u32::from_str_radix(&ver_str, 16).unwrap();
441445
debug!("ESRT Entry {}", i);
442446
debug!(" Name: {:?}", guid_kind);
@@ -456,7 +460,7 @@ pub fn get_esrt() -> Option<Esrt> {
456460
// TODO: The missing fields are present in Device Manager
457461
// So there must be a way to get at them
458462
let esrt = EsrtResourceEntry {
459-
fw_class: Guid::from(guid),
463+
fw_class: CGuid::from(guid),
460464
fw_type,
461465
fw_version: ver,
462466
// TODO: Not exposed by windows
@@ -543,7 +547,7 @@ pub fn get_esrt() -> Option<Esrt> {
543547
for table in config_tables {
544548
// TODO: Why aren't they the same type?
545549
//debug!("Table: {:?}", table);
546-
let table_guid: Guid = unsafe { std::mem::transmute(table.guid) };
550+
let table_guid: CGuid = unsafe { std::mem::transmute(table.guid) };
547551
let table_guid = GUID::from(table_guid);
548552
match table_guid {
549553
SYSTEM_RESOURCE_TABLE_GUID => unsafe {

framework_lib/src/guid.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use alloc::fmt;
2+
3+
#[cfg(not(feature = "uefi"))]
4+
use guid_create::GUID;
5+
#[cfg(feature = "uefi")]
6+
use guid_create_no_std::GUID;
7+
8+
// Until https://github.com/kurtlawrence/guid-create/pull/20 is merged
9+
10+
impl From<CGuid> for GUID {
11+
fn from(item: CGuid) -> Self {
12+
GUID::build_from_components(item.a, item.b, item.c, &item.d)
13+
}
14+
}
15+
16+
impl From<GUID> for CGuid {
17+
fn from(item: GUID) -> Self {
18+
CGuid {
19+
a: item.data1(),
20+
b: item.data2(),
21+
c: item.data3(),
22+
d: item.data4(),
23+
}
24+
}
25+
}
26+
27+
impl fmt::Display for CGuid {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
write!(
30+
f,
31+
"{:08X}-{:04X}-{:04X}-{:04X}-{:08X}{:04X}",
32+
self.a,
33+
self.b,
34+
self.c,
35+
u16::from_be_bytes(self.d[0..2].try_into().unwrap()),
36+
u32::from_be_bytes(self.d[2..6].try_into().unwrap()),
37+
u16::from_be_bytes(self.d[6..8].try_into().unwrap()),
38+
)
39+
}
40+
}
41+
42+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Hash)]
43+
#[repr(C)]
44+
pub struct CGuid {
45+
/// The low field of the timestamp.
46+
a: u32,
47+
/// The middle field of the timestamp.
48+
b: u16,
49+
/// The high field of the timestamp multiplexed with the version number.
50+
c: u16,
51+
/// Contains, in this order:
52+
/// - The high field of the clock sequence multiplexed with the variant.
53+
/// - The low field of the clock sequence.
54+
/// - The spatially unique node identifier.
55+
d: [u8; 8],
56+
}

framework_lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub mod commandline;
3838
pub mod csme;
3939
pub mod ec_binary;
4040
pub mod esrt;
41+
pub mod guid;
4142
mod os_specific;
4243
pub mod power;
4344
pub mod smbios;

0 commit comments

Comments
 (0)