From 8956c8b986d44dede08fdf650fd69097c873dc4d Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sun, 15 Mar 2026 15:45:35 +0100 Subject: [PATCH] uefi: add `time03` feature and integrate time types with struct Time --- Cargo.lock | 40 +++++++ Cargo.toml | 1 + uefi/CHANGELOG.md | 3 + uefi/Cargo.toml | 3 + uefi/src/lib.rs | 3 + uefi/src/runtime/time/integration_common.rs | 7 ++ .../runtime/time/integration_time_crate.rs | 109 ++++++++++++++++++ uefi/src/runtime/time/mod.rs | 17 ++- xtask/src/cargo.rs | 12 +- 9 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 uefi/src/runtime/time/integration_time_crate.rs diff --git a/Cargo.lock b/Cargo.lock index fef5cb7e..eaa9201d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -200,6 +200,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" +dependencies = [ + "powerfmt", +] + [[package]] name = "digest" version = "0.10.7" @@ -486,6 +495,12 @@ dependencies = [ "libc", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "once_cell" version = "1.21.4" @@ -511,6 +526,12 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "prettyplease" version = "0.2.37" @@ -862,6 +883,24 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9e442fc33d7fdb45aa9bfeb312c095964abdf596f7567261062b2a7107aaabd" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b36ee98fd31ec7426d599183e8fe26932a8dc1fb76ddb6214d05493377d34ca" + [[package]] name = "toml" version = "1.1.2+spec-1.1.0" @@ -940,6 +979,7 @@ dependencies = [ "log", "ptr_meta", "qemu-exit", + "time", "ucs2", "uefi-macros", "uefi-raw", diff --git a/Cargo.toml b/Cargo.toml index 24ab1fb6..e6879dbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ bitflags = "2.0.0" log = { version = "0.4.5", default-features = false } ptr_meta = { version = "0.3.0", default-features = false, features = ["derive"] } qemu-exit = { version = "4.0.0" } +time = { version = "0.3", default-features = false } uguid = "2.2.1" [patch.crates-io] diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index 4a6da7f5..1e600c29 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -34,6 +34,9 @@ - Added `Serial::read_exact()` and `Serial::write_exact()` - `CStr16::from_bytes_with_nul()`: This is especially useful to transform the retrieved value from a UEFI variable into a UCS2 (CStr16) string. +- Integration of `Time` with `time` crate + - `TryFrom`: `time::PrimitiveDateTime <--> Time` (without timezone) + - `TryFrom`: `time::OffsetDateTime <--> Time` (with timezone) ## Changed - export all `text::{input, output}::*` types diff --git a/uefi/Cargo.toml b/uefi/Cargo.toml index 05fc2b75..4330fcb8 100644 --- a/uefi/Cargo.toml +++ b/uefi/Cargo.toml @@ -20,6 +20,8 @@ rust-version.workspace = true # KEEP this feature list in sync with doc in uefi/lib.rs! default = [ ] alloc = [] +# Integration with time crate `>= v0.3` +time03 = ["dep:time"] # Generic gate to code that uses unstable features of Rust, needing a nightly # toolchain. @@ -39,6 +41,7 @@ log-debugcon = [] bitflags.workspace = true log.workspace = true ptr_meta.workspace = true +time = { workspace = true, optional = true } qemu-exit = { workspace = true, optional = true } uguid.workspace = true cfg-if = "1.0.0" diff --git a/uefi/src/lib.rs b/uefi/src/lib.rs index 32f40549..fe74a4b8 100644 --- a/uefi/src/lib.rs +++ b/uefi/src/lib.rs @@ -148,6 +148,9 @@ //! - `log-debugcon`: Whether the logger set up by `logger` should also log //! to the debugcon device (available in QEMU or Cloud Hypervisor on x86). //! - `panic_handler`: Add a default panic handler that logs to `stdout`. +//! - `time03`: Integration of [`runtime::Time`] with the `time` crate +//! (version 0.3 and possible above). Specifically, it integrates the time +//! struct with `PrimitiveDateTime` and `OffsetDateTime` via `TryFrom`. //! - `unstable`: Enable functionality that depends on [unstable features] in //! the Rust compiler (nightly version). //! - `qemu`: Enable some code paths to adapt their execution when executed diff --git a/uefi/src/runtime/time/integration_common.rs b/uefi/src/runtime/time/integration_common.rs index 69f03242..5865e070 100644 --- a/uefi/src/runtime/time/integration_common.rs +++ b/uefi/src/runtime/time/integration_common.rs @@ -40,6 +40,9 @@ pub(super) enum ConversionErrorInner { /// /// [`Time::UNSPECIFIED_TIMEZONE`]: super::Time::UNSPECIFIED_TIMEZONE UnspecifiedTimezone, + /// Errors raised in the [`time`] crate. + #[cfg(feature = "time03")] + TimeCrateError(time::Error), } impl Display for ConversionErrorInner { @@ -48,6 +51,8 @@ impl Display for ConversionErrorInner { Self::InvalidComponent => write!(f, "Invalid component"), Self::InvalidUefiTime(e) => write!(f, "Invalid UEFI time: {e}"), Self::UnspecifiedTimezone => write!(f, "Unspecified timezone"), + #[cfg(feature = "time03")] + Self::TimeCrateError(e) => write!(f, "Time crate error: {}", e), } } } @@ -58,6 +63,8 @@ impl Error for ConversionErrorInner { Self::InvalidComponent => None, Self::InvalidUefiTime(e) => Some(e), Self::UnspecifiedTimezone => None, + #[cfg(feature = "time03")] + Self::TimeCrateError(e) => Some(e), } } } diff --git a/uefi/src/runtime/time/integration_time_crate.rs b/uefi/src/runtime/time/integration_time_crate.rs new file mode 100644 index 00000000..3deaac64 --- /dev/null +++ b/uefi/src/runtime/time/integration_time_crate.rs @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +//! Integration of the UEFI [`Time`] type with the [`time`] crate. + +use super::Time; +use super::integration_common::{ConversionError, ConversionErrorInner}; +use crate::runtime::TimeParams; +use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; + +impl TryFrom