1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
//! UEFI services available at runtime, even after the OS boots. use super::Header; use crate::table::boot::MemoryDescriptor; use crate::{Result, Status}; use bitflags::bitflags; use core::fmt; use core::mem::MaybeUninit; use core::ptr; /// Contains pointers to all of the runtime services. /// /// This table, and the function pointers it contains are valid /// even after the UEFI OS loader and OS have taken control of the platform. #[repr(C)] pub struct RuntimeServices { header: Header, get_time: unsafe extern "efiapi" fn(time: *mut Time, capabilities: *mut TimeCapabilities) -> Status, set_time: unsafe extern "efiapi" fn(time: &Time) -> Status, // Skip some useless functions. _pad: [usize; 2], set_virtual_address_map: unsafe extern "efiapi" fn( map_size: usize, desc_size: usize, desc_version: u32, virtual_map: *mut MemoryDescriptor, ) -> Status, _pad2: [usize; 5], reset: unsafe extern "efiapi" fn( rt: ResetType, status: Status, data_size: usize, data: *const u8, ) -> !, } impl RuntimeServices { /// Query the current time and date information pub fn get_time(&self) -> Result<Time> { let mut time = MaybeUninit::<Time>::uninit(); unsafe { (self.get_time)(time.as_mut_ptr(), ptr::null_mut()) } .into_with_val(|| unsafe { time.assume_init() }) } /// Query the current time and date information and the RTC capabilities pub fn get_time_and_caps(&self) -> Result<(Time, TimeCapabilities)> { let mut time = MaybeUninit::<Time>::uninit(); let mut caps = MaybeUninit::<TimeCapabilities>::uninit(); unsafe { (self.get_time)(time.as_mut_ptr(), caps.as_mut_ptr()) } .into_with_val(|| unsafe { (time.assume_init(), caps.assume_init()) }) } /// Sets the current local time and date information /// /// During runtime, if a PC-AT CMOS device is present in the platform, the /// caller must synchronize access to the device before calling `set_time`. /// /// # Safety /// /// Undefined behavior could happen if multiple tasks try to /// use this function at the same time without synchronisation. pub unsafe fn set_time(&mut self, time: &Time) -> Result { (self.set_time)(time).into() } /// Changes the runtime addressing mode of EFI firmware from physical to virtual. /// /// # Safety /// /// Setting new virtual memory map is unsafe and may cause undefined behaviors. pub unsafe fn set_virtual_address_map(&self, map: &mut [MemoryDescriptor]) -> Result { // Unsafe Code Guidelines guarantees that there is no padding in an array or a slice // between its elements if the element type is `repr(C)`, which is our case. // // See https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html let map_size = core::mem::size_of_val(map); let entry_size = core::mem::size_of::<MemoryDescriptor>(); let entry_version = crate::table::boot::MEMORY_DESCRIPTOR_VERSION; let map_ptr = map.as_mut_ptr(); (self.set_virtual_address_map)(map_size, entry_size, entry_version, map_ptr).into() } /// Resets the computer. pub fn reset(&self, rt: ResetType, status: Status, data: Option<&[u8]>) -> ! { let (size, data) = match data { // FIXME: The UEFI spec states that the data must start with a NUL- // terminated string, which we should check... but it does not // specify if that string should be Latin-1 or UCS-2! // // PlatformSpecific resets should also insert a GUID after the // NUL-terminated string. Some(data) => (data.len(), data.as_ptr()), None => (0, ptr::null()), }; unsafe { (self.reset)(rt, status, size, data) } } } impl super::Table for RuntimeServices { const SIGNATURE: u64 = 0x5652_4553_544e_5552; } /// The current time information #[derive(Copy, Clone)] #[repr(C)] pub struct Time { year: u16, // 1900 - 9999 month: u8, // 1 - 12 day: u8, // 1 - 31 hour: u8, // 0 - 23 minute: u8, // 0 - 59 second: u8, // 0 - 59 _pad1: u8, nanosecond: u32, // 0 - 999_999_999 time_zone: i16, // -1440 to 1440, or 2047 if unspecified daylight: Daylight, _pad2: u8, } bitflags! { /// Flags describing the capabilities of a memory range. pub struct Daylight: u8 { /// Time is affected by daylight savings time const ADJUST_DAYLIGHT = 0x01; /// Time has been adjusted for daylight savings time const IN_DAYLIGHT = 0x02; } } impl Time { /// Build an UEFI time struct #[allow(clippy::too_many_arguments)] pub fn new( year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8, nanosecond: u32, time_zone: i16, daylight: Daylight, ) -> Self { assert!(year >= 1900 && year <= 9999); assert!(month >= 1 && month <= 12); assert!(day >= 1 && day <= 31); assert!(hour <= 23); assert!(minute <= 59); assert!(second <= 59); assert!(nanosecond <= 999_999_999); assert!((time_zone >= -1440 && time_zone <= 1440) || time_zone == 2047); Self { year, month, day, hour, minute, second, _pad1: 0, nanosecond, time_zone, daylight, _pad2: 0, } } /// Query the year pub fn year(&self) -> u16 { self.year } /// Query the month pub fn month(&self) -> u8 { self.month } /// Query the day pub fn day(&self) -> u8 { self.day } /// Query the hour pub fn hour(&self) -> u8 { self.hour } /// Query the minute pub fn minute(&self) -> u8 { self.minute } /// Query the second pub fn second(&self) -> u8 { self.second } /// Query the nanosecond pub fn nanosecond(&self) -> u32 { self.nanosecond } /// Query the time offset in minutes from UTC, or None if using local time pub fn time_zone(&self) -> Option<i16> { if self.time_zone == 2047 { None } else { Some(self.time_zone) } } /// Query the daylight savings time information pub fn daylight(&self) -> Daylight { self.daylight } } impl fmt::Debug for Time { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}-{}-{} ", self.year, self.month, self.day)?; write!(f, "{}:{}:{}.{} ", self.hour, self.minute, self.second, self.nanosecond)?; write!(f, "{} {:?}", self.time_zone, self.daylight) } } /// Real time clock capabilities #[derive(Debug, Copy, Clone, Eq, PartialEq)] #[repr(C)] pub struct TimeCapabilities { /// Reporting resolution of the clock in counts per second. 1 for a normal /// PC-AT CMOS RTC device, which reports the time with 1-second resolution. pub resolution: u32, /// Timekeeping accuracy in units of 1e-6 parts per million. pub accuracy: u32, /// Whether a time set operation clears the device's time below the /// "resolution" reporting level. False for normal PC-AT CMOS RTC devices. pub sets_to_zero: bool, } /// The type of system reset. #[derive(Debug, Copy, Clone, Eq, PartialEq)] #[repr(u32)] pub enum ResetType { /// Resets all the internal circuitry to its initial state. /// /// This is analogous to power cycling the device. Cold = 0, /// The processor is reset to its initial state. Warm, /// The components are powered off. Shutdown, /// A platform-specific reset type. /// /// The additional data must be a pointer to /// a null-terminated string followed by an UUID. PlatformSpecific, // SAFETY: This enum is never exposed to the user, but only fed as input to // the firmware. Therefore, unexpected values can never come from // the firmware, and modeling this as a Rust enum seems safe. }