diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index b14eb09ec..d8c669f7a 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -1,5 +1,8 @@ # uefi-raw - [Unreleased] +## Added +- Added `TimestampProtocol`. + # uefi-raw - 0.5.1 (2024-03-17) ## Added diff --git a/uefi-raw/src/protocol/misc.rs b/uefi-raw/src/protocol/misc.rs new file mode 100644 index 000000000..a7a3a905a --- /dev/null +++ b/uefi-raw/src/protocol/misc.rs @@ -0,0 +1,24 @@ +use crate::{guid, Guid, Status}; + +#[derive(Debug)] +#[repr(C)] +pub struct TimestampProtocol { + pub get_timestamp: unsafe extern "efiapi" fn() -> u64, + pub get_properties: unsafe extern "efiapi" fn(*mut TimestampProperties) -> Status, +} + +impl TimestampProtocol { + pub const GUID: Guid = guid!("afbfde41-2e6e-4262-ba65-62b9236e5495"); +} + +/// Properties of the timestamp counter. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[repr(C)] +pub struct TimestampProperties { + /// Timestamp counter frequency, in Hz. + pub frequency: u64, + + /// The maximum value of the timestamp counter before it rolls over. For + /// example, a 24-bit counter would have an end value of `0xff_ffff`. + pub end_value: u64, +} diff --git a/uefi-raw/src/protocol/mod.rs b/uefi-raw/src/protocol/mod.rs index ba1e54f62..16616ca0a 100644 --- a/uefi-raw/src/protocol/mod.rs +++ b/uefi-raw/src/protocol/mod.rs @@ -13,6 +13,7 @@ pub mod file_system; pub mod loaded_image; pub mod media; pub mod memory_protection; +pub mod misc; pub mod network; pub mod rng; pub mod shell_params; diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index 2e517c549..e3486f78d 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -1,5 +1,8 @@ # uefi - [Unreleased] +## Added +- Added `Timestamp` protocol. + # uefi - 0.27.0 (2024-03-17) ## Added diff --git a/uefi/src/proto/misc.rs b/uefi/src/proto/misc.rs new file mode 100644 index 000000000..339c8ea6e --- /dev/null +++ b/uefi/src/proto/misc.rs @@ -0,0 +1,25 @@ +//! Miscellaneous protocols. + +use crate::proto::unsafe_protocol; +use crate::{Result, StatusExt}; +use uefi_raw::protocol::misc::{TimestampProperties, TimestampProtocol}; + +/// Protocol for retrieving a high-resolution timestamp counter. +#[derive(Debug)] +#[repr(transparent)] +#[unsafe_protocol(TimestampProtocol::GUID)] +pub struct Timestamp(TimestampProtocol); + +impl Timestamp { + /// Get the current value of the timestamp counter. + #[must_use] + pub fn get_timestamp(&self) -> u64 { + unsafe { (self.0.get_timestamp)() } + } + + /// Get the properties of the timestamp counter. + pub fn get_properties(&self) -> Result { + let mut properties = TimestampProperties::default(); + unsafe { (self.0.get_properties)(&mut properties) }.to_result_with_val(|| properties) + } +} diff --git a/uefi/src/proto/mod.rs b/uefi/src/proto/mod.rs index 6f2663944..5919b7afc 100644 --- a/uefi/src/proto/mod.rs +++ b/uefi/src/proto/mod.rs @@ -72,6 +72,7 @@ pub mod device_path; pub mod driver; pub mod loaded_image; pub mod media; +pub mod misc; pub mod network; pub mod pi; pub mod rng;