diff --git a/Cargo.lock b/Cargo.lock index 8a41533..ada3b68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -256,6 +256,16 @@ dependencies = [ "libc", ] +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys 0.8.7", + "libc", +] + [[package]] name = "core-foundation-sys" version = "0.6.2" @@ -264,9 +274,9 @@ checksum = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" [[package]] name = "core-foundation-sys" -version = "0.8.4" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "cpufeatures" @@ -415,7 +425,8 @@ dependencies = [ "clap-num", "clap-verbosity-flag", "env_logger", - "guid-create", + "guid-create 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "guid-create 0.4.1 (git+https://github.com/FrameworkComputer/guid-create?branch=no_std)", "hidapi", "lazy_static", "libc", @@ -426,11 +437,13 @@ dependencies = [ "num-derive", "num-traits", "plain", - "redox_hwio", + "redox_hwio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_hwio 0.1.6 (git+https://github.com/FrameworkComputer/rust-hwio?branch=freebsd)", "regex", "rusb", "sha2", - "smbios-lib", + "smbios-lib 0.9.1", + "smbios-lib 0.9.2", "spin 0.9.8", "uefi", "uefi-services", @@ -568,6 +581,17 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "git2" version = "0.15.0" @@ -584,7 +608,17 @@ dependencies = [ [[package]] name = "guid-create" version = "0.4.1" -source = "git+https://github.com/FrameworkComputer/guid-create?branch=no-rand#84c3ad2e8b64a12beebb460804a65da55434cfd9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "738068c48cd875ab1ff0bb5de7fa448cb88769ce74783f4eb964257e693f8a19" +dependencies = [ + "rand", + "winapi", +] + +[[package]] +name = "guid-create" +version = "0.4.1" +source = "git+https://github.com/FrameworkComputer/guid-create?branch=no_std#f1d82853cb3679689f9b3f09644082321558145d" dependencies = [ "winapi", ] @@ -627,7 +661,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" dependencies = [ "android_system_properties", - "core-foundation-sys 0.8.4", + "core-foundation-sys 0.8.7", "iana-time-zone-haiku", "js-sys", "wasm-bindgen", @@ -674,6 +708,16 @@ dependencies = [ "mach 0.2.3", ] +[[package]] +name = "io-kit-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b" +dependencies = [ + "core-foundation-sys 0.8.7", + "mach2", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -798,6 +842,15 @@ dependencies = [ "libc", ] +[[package]] +name = "mach2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d640282b302c0bb0a2a8e0233ead9035e3bed871f0b7e81fe4a1ec829765db44" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.7.2" @@ -955,6 +1008,15 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.93" @@ -993,6 +1055,45 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_hwio" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eb516ad341a84372b5b15a5a35cf136ba901a639c8536f521b108253d7fce74" +dependencies = [ + "lazy_static", +] + [[package]] name = "redox_hwio" version = "0.1.6" @@ -1142,10 +1243,9 @@ name = "smbios-lib" version = "0.9.1" source = "git+https://github.com/FrameworkComputer/smbios-lib.git?branch=no-std#b3e2fff8a6f4b8c2d729467cbbf0c8c41974cd1c" dependencies = [ - "core-foundation", + "core-foundation 0.6.4", "core-foundation-sys 0.6.2", - "getopts", - "io-kit-sys", + "io-kit-sys 0.1.0", "libc", "mach 0.3.2", "no-std-compat", @@ -1153,6 +1253,22 @@ dependencies = [ "serde_json", ] +[[package]] +name = "smbios-lib" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c18320ad3d997a100cb948fc020111936c530eddfde947f467083730e39e72" +dependencies = [ + "core-foundation 0.10.1", + "core-foundation-sys 0.8.7", + "getopts", + "io-kit-sys 0.4.1", + "libc", + "mach2", + "serde", + "serde_json", +] + [[package]] name = "spin" version = "0.5.2" @@ -1421,6 +1537,12 @@ dependencies = [ "libc", ] +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "wasm-bindgen" version = "0.2.84" @@ -1839,3 +1961,23 @@ dependencies = [ "windows 0.59.0", "windows-core", ] + +[[package]] +name = "zerocopy" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.98", +] diff --git a/framework_lib/Cargo.toml b/framework_lib/Cargo.toml index 348b6bc..9d5b4f5 100644 --- a/framework_lib/Cargo.toml +++ b/framework_lib/Cargo.toml @@ -34,33 +34,40 @@ spin = { version = "0.9.8" } no-std-compat = { version = "0.4.1", features = [ "alloc" ] } hidapi = { version = "2.6.3", features = [ "windows-native" ], optional = true } rusb = { version = "0.9.4", optional = true } -guid-create = { git = "https://github.com/FrameworkComputer/guid-create", branch = "no-rand", default-features = false } [target.'cfg(target_os = "uefi")'.dependencies] uefi = { version = "0.20", features = ["alloc"] } uefi-services = "0.17" plain = "0.2.3" -redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false } -smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std", default-features = false } +redox_hwio = { version = "0.1.6", default-features = false } +smbios-lib-no-std = { package = "smbios-lib", git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std", default-features = false } +guid-create-no-std = { package = "guid-create", git = "https://github.com/FrameworkComputer/guid-create", branch = "no_std", default-features = false } [target.'cfg(windows)'.dependencies] wmi = "0.15.0" -smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" } +smbios-lib = "0.9.2" env_logger = "0.11" clap = { version = "4.5", features = ["derive", "cargo"] } clap-num = { version = "1.2.0" } clap-verbosity-flag = { version = "2.2.1" } windows-version = "0.1.4" +guid-create = "0.4.1" [target.'cfg(unix)'.dependencies] libc = "0.2.155" nix = { version = "0.29.0", features = ["ioctl", "user"] } -redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd" } -smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" } +smbios-lib = "0.9.2" env_logger = "0.11" clap = { version = "4.5", features = ["derive", "cargo"] } clap-num = { version = "1.2.0" } clap-verbosity-flag = { version = "2.2.1" } +guid-create = "0.4.1" + +[target.'cfg(target_os="linux")'.dependencies] +redox_hwio = "0.1.6" + +[target.'cfg(target_os="freebsd")'.dependencies] +redox_hwio_freebsd = { package = "redox_hwio", git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd" } [target.'cfg(windows)'.dependencies.windows] version = "0.59.0" diff --git a/framework_lib/src/capsule.rs b/framework_lib/src/capsule.rs index e2dff32..a222968 100644 --- a/framework_lib/src/capsule.rs +++ b/framework_lib/src/capsule.rs @@ -10,8 +10,8 @@ use std::prelude::v1::*; +use crate::guid::CGuid; use core::prelude::rust_2021::derive; -use guid_create::Guid; #[cfg(not(feature = "uefi"))] use std::fs::File; #[cfg(not(feature = "uefi"))] @@ -21,7 +21,7 @@ use std::io::prelude::*; #[repr(C)] pub struct EfiCapsuleHeader { /// A GUID that defines the contents of a capsule. - pub capsule_guid: Guid, + pub capsule_guid: CGuid, /// The size of the capsule header. This may be larger than the size of /// the EFI_CAPSULE_HEADER since CapsuleGuid may imply @@ -205,14 +205,14 @@ mod tests { let data = fs::read(capsule_path).unwrap(); let cap = parse_capsule_header(&data).unwrap(); let expected_header = EfiCapsuleHeader { - capsule_guid: Guid::from(esrt::WINUX_GUID), + capsule_guid: CGuid::from(esrt::WINUX_GUID), header_size: 28, flags: 65536, capsule_image_size: 676898, }; assert_eq!(cap, expected_header); - assert_eq!(cap.capsule_guid, Guid::from(esrt::WINUX_GUID)); + assert_eq!(cap.capsule_guid, CGuid::from(esrt::WINUX_GUID)); let ux_header = parse_ux_header(&data); assert_eq!( ux_header, diff --git a/framework_lib/src/chromium_ec/portio.rs b/framework_lib/src/chromium_ec/portio.rs index d76f8a6..3b55dda 100644 --- a/framework_lib/src/chromium_ec/portio.rs +++ b/framework_lib/src/chromium_ec/portio.rs @@ -4,8 +4,10 @@ use alloc::string::ToString; use alloc::vec; use alloc::vec::Vec; use core::convert::TryInto; -#[cfg(not(windows))] +#[cfg(all(not(windows), not(target_os = "freebsd")))] use hwio::{Io, Pio}; +#[cfg(target_os = "freebsd")] +use hwio_freebsd::{Io, Pio}; #[cfg(target_os = "linux")] use libc::ioperm; use log::Level; diff --git a/framework_lib/src/chromium_ec/portio_mec.rs b/framework_lib/src/chromium_ec/portio_mec.rs index 1f855d0..0cbd3bc 100644 --- a/framework_lib/src/chromium_ec/portio_mec.rs +++ b/framework_lib/src/chromium_ec/portio_mec.rs @@ -4,7 +4,10 @@ use alloc::vec::Vec; use log::Level; +#[cfg(all(not(windows), not(target_os = "freebsd")))] use hwio::{Io, Pio}; +#[cfg(target_os = "freebsd")] +use hwio_freebsd::{Io, Pio}; #[cfg(target_os = "linux")] use libc::ioperm; diff --git a/framework_lib/src/commandline/mod.rs b/framework_lib/src/commandline/mod.rs index 8644026..8fd3fb2 100644 --- a/framework_lib/src/commandline/mod.rs +++ b/framework_lib/src/commandline/mod.rs @@ -3,11 +3,15 @@ //! Can be easily re-used from any OS or UEFI shell. //! We have implemented both in the `framework_tool` and `framework_uefi` crates. +use crate::guid::CGuid; use alloc::format; use alloc::string::String; use alloc::string::ToString; use alloc::vec::Vec; -use guid_create::{Guid, GUID}; +#[cfg(not(feature = "uefi"))] +use guid_create::GUID; +#[cfg(feature = "uefi")] +use guid_create_no_std::GUID; use log::Level; use num_traits::FromPrimitive; @@ -65,6 +69,9 @@ use crate::util::{self, Config, Platform, PlatformFamily}; use hidapi::HidApi; use sha2::{Digest, Sha256, Sha384, Sha512}; //use smbioslib::*; +#[cfg(feature = "uefi")] +use smbios_lib_no_std::{DefinedStruct, SMBiosInformation}; +#[cfg(not(feature = "uefi"))] use smbioslib::{DefinedStruct, SMBiosInformation}; use crate::chromium_ec::{CrosEc, CrosEcDriverType, HardwareDeviceType}; @@ -1270,7 +1277,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 { println!(" Size: {:>20} B", data.len()); println!(" Size: {:>20} KB", data.len() / 1024); if let Some(header) = analyze_capsule(&data) { - if header.capsule_guid == Guid::from(esrt::WINUX_GUID) { + if header.capsule_guid == CGuid::from(esrt::WINUX_GUID) { let ux_header = capsule::parse_ux_header(&data); if let Some(dump_path) = &args.dump { // TODO: Better error handling, rather than just panicking diff --git a/framework_lib/src/esrt/mod.rs b/framework_lib/src/esrt/mod.rs index aa517bf..e555f40 100644 --- a/framework_lib/src/esrt/mod.rs +++ b/framework_lib/src/esrt/mod.rs @@ -14,8 +14,12 @@ use log::{debug, error, info, trace}; use std::prelude::v1::*; +use crate::guid::CGuid; use core::prelude::v1::derive; -use guid_create::{Guid, GUID}; +#[cfg(not(feature = "uefi"))] +use guid_create::GUID; +#[cfg(feature = "uefi")] +use guid_create_no_std::GUID; #[cfg(target_os = "linux")] use std::fs; @@ -193,7 +197,7 @@ pub enum FrameworkGuidKind { Unknown, } -pub fn match_guid_kind(guid: &Guid) -> FrameworkGuidKind { +pub fn match_guid_kind(guid: &CGuid) -> FrameworkGuidKind { match GUID::from(*guid) { TGL_BIOS_GUID => FrameworkGuidKind::TglBios, ADL_BIOS_GUID => FrameworkGuidKind::AdlBios, @@ -292,7 +296,7 @@ impl UpdateStatus { // TODO: Decode into proper Rust types #[derive(Clone)] pub struct EsrtResourceEntry { - pub fw_class: Guid, + pub fw_class: CGuid, pub fw_type: u32, // ResourceType pub fw_version: u32, pub lowest_supported_fw_version: u32, @@ -364,7 +368,7 @@ fn esrt_from_sysfs(dir: &Path) -> io::Result { let last_attempt_version = fs::read_to_string(path.join("last_attempt_version"))?; let last_attempt_status = fs::read_to_string(path.join("last_attempt_status"))?; let esrt = EsrtResourceEntry { - fw_class: Guid::from( + fw_class: CGuid::from( GUID::parse(fw_class.trim()).expect("Kernel provided wrong value"), ), fw_type: fw_type @@ -436,7 +440,7 @@ pub fn get_esrt() -> Option { let ver_str = caps.get(2).unwrap().as_str().to_string(); let guid = GUID::parse(guid_str.trim()).expect("Kernel provided wrong value"); - let guid_kind = match_guid_kind(&Guid::from(guid)); + let guid_kind = match_guid_kind(&CGuid::from(guid)); let ver = u32::from_str_radix(&ver_str, 16).unwrap(); debug!("ESRT Entry {}", i); debug!(" Name: {:?}", guid_kind); @@ -456,7 +460,7 @@ pub fn get_esrt() -> Option { // TODO: The missing fields are present in Device Manager // So there must be a way to get at them let esrt = EsrtResourceEntry { - fw_class: Guid::from(guid), + fw_class: CGuid::from(guid), fw_type, fw_version: ver, // TODO: Not exposed by windows @@ -543,7 +547,7 @@ pub fn get_esrt() -> Option { for table in config_tables { // TODO: Why aren't they the same type? //debug!("Table: {:?}", table); - let table_guid: Guid = unsafe { std::mem::transmute(table.guid) }; + let table_guid: CGuid = unsafe { std::mem::transmute(table.guid) }; let table_guid = GUID::from(table_guid); match table_guid { SYSTEM_RESOURCE_TABLE_GUID => unsafe { diff --git a/framework_lib/src/guid.rs b/framework_lib/src/guid.rs new file mode 100644 index 0000000..e52de03 --- /dev/null +++ b/framework_lib/src/guid.rs @@ -0,0 +1,56 @@ +use alloc::fmt; + +#[cfg(not(feature = "uefi"))] +use guid_create::GUID; +#[cfg(feature = "uefi")] +use guid_create_no_std::GUID; + +// Until https://github.com/kurtlawrence/guid-create/pull/20 is merged + +impl From for GUID { + fn from(item: CGuid) -> Self { + GUID::build_from_components(item.a, item.b, item.c, &item.d) + } +} + +impl From for CGuid { + fn from(item: GUID) -> Self { + CGuid { + a: item.data1(), + b: item.data2(), + c: item.data3(), + d: item.data4(), + } + } +} + +impl fmt::Display for CGuid { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{:08X}-{:04X}-{:04X}-{:04X}-{:08X}{:04X}", + self.a, + self.b, + self.c, + u16::from_be_bytes(self.d[0..2].try_into().unwrap()), + u32::from_be_bytes(self.d[2..6].try_into().unwrap()), + u16::from_be_bytes(self.d[6..8].try_into().unwrap()), + ) + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Hash)] +#[repr(C)] +pub struct CGuid { + /// The low field of the timestamp. + a: u32, + /// The middle field of the timestamp. + b: u16, + /// The high field of the timestamp multiplexed with the version number. + c: u16, + /// Contains, in this order: + /// - The high field of the clock sequence multiplexed with the variant. + /// - The low field of the clock sequence. + /// - The spatially unique node identifier. + d: [u8; 8], +} diff --git a/framework_lib/src/lib.rs b/framework_lib/src/lib.rs index 05adf8f..bfb8b5a 100644 --- a/framework_lib/src/lib.rs +++ b/framework_lib/src/lib.rs @@ -38,6 +38,7 @@ pub mod commandline; pub mod csme; pub mod ec_binary; pub mod esrt; +pub mod guid; mod os_specific; pub mod power; pub mod smbios; diff --git a/framework_lib/src/smbios.rs b/framework_lib/src/smbios.rs index 144ad21..08fe2af 100644 --- a/framework_lib/src/smbios.rs +++ b/framework_lib/src/smbios.rs @@ -9,6 +9,9 @@ use crate::util::Config; pub use crate::util::{Platform, PlatformFamily}; use num_derive::FromPrimitive; use num_traits::FromPrimitive; +#[cfg(feature = "uefi")] +use smbios_lib_no_std::*; +#[cfg(not(feature = "uefi"))] use smbioslib::*; #[cfg(feature = "uefi")] use spin::Mutex;