|
| 1 | +//! UEFI runtime services. |
| 2 | +//! |
| 3 | +//! These services are available both before and after exiting boot |
| 4 | +//! services. Note that various restrictions apply when calling runtime services |
| 5 | +//! functions after exiting boot services; see the "Calling Convention" section |
| 6 | +//! of the UEFI specification for details. |
| 7 | +
|
| 8 | +use crate::table::{self}; |
| 9 | +use crate::{Result, StatusExt}; |
| 10 | +use core::ptr::{self, NonNull}; |
| 11 | + |
| 12 | +pub use crate::table::runtime::{Daylight, Time, TimeCapabilities, TimeError, TimeParams}; |
| 13 | + |
| 14 | +fn runtime_services_raw_panicking() -> NonNull<uefi_raw::table::runtime::RuntimeServices> { |
| 15 | + let st = table::system_table_raw_panicking(); |
| 16 | + // SAFETY: valid per requirements of `set_system_table`. |
| 17 | + let st = unsafe { st.as_ref() }; |
| 18 | + NonNull::new(st.runtime_services).expect("runtime services are not active") |
| 19 | +} |
| 20 | + |
| 21 | +/// Query the current time and date information. |
| 22 | +pub fn get_time() -> Result<Time> { |
| 23 | + let rt = runtime_services_raw_panicking(); |
| 24 | + let rt = unsafe { rt.as_ref() }; |
| 25 | + |
| 26 | + let mut time = Time::invalid(); |
| 27 | + let time_ptr: *mut Time = &mut time; |
| 28 | + unsafe { (rt.get_time)(time_ptr.cast(), ptr::null_mut()) }.to_result_with_val(|| time) |
| 29 | +} |
| 30 | + |
| 31 | +/// Query the current time and date information and the RTC capabilities. |
| 32 | +pub fn get_time_and_caps() -> Result<(Time, TimeCapabilities)> { |
| 33 | + let rt = runtime_services_raw_panicking(); |
| 34 | + let rt = unsafe { rt.as_ref() }; |
| 35 | + |
| 36 | + let mut time = Time::invalid(); |
| 37 | + let time_ptr: *mut Time = &mut time; |
| 38 | + let mut caps = TimeCapabilities::default(); |
| 39 | + unsafe { (rt.get_time)(time_ptr.cast(), &mut caps) }.to_result_with_val(|| (time, caps)) |
| 40 | +} |
| 41 | + |
| 42 | +/// Sets the current local time and date information |
| 43 | +/// |
| 44 | +/// During runtime, if a PC-AT CMOS device is present in the platform, the |
| 45 | +/// caller must synchronize access to the device before calling `set_time`. |
| 46 | +/// |
| 47 | +/// # Safety |
| 48 | +/// |
| 49 | +/// Undefined behavior could happen if multiple tasks try to |
| 50 | +/// use this function at the same time without synchronisation. |
| 51 | +pub unsafe fn set_time(time: &Time) -> Result { |
| 52 | + let rt = runtime_services_raw_panicking(); |
| 53 | + let rt = unsafe { rt.as_ref() }; |
| 54 | + |
| 55 | + let time: *const Time = time; |
| 56 | + (rt.set_time)(time.cast()).to_result() |
| 57 | +} |
0 commit comments