aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / misc.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Miscellaneous protocols. |
| 4 | |
| 5 | use uefi_raw::protocol::misc::{ |
| 6 | ResetNotificationProtocol, ResetSystemFn, TimestampProperties, TimestampProtocol, |
| 7 | }; |
| 8 | |
| 9 | use crate::proto::unsafe_protocol; |
| 10 | use crate::{Result, StatusExt}; |
| 11 | |
| 12 | /// Timestamp [`Protocol`]. |
| 13 | /// |
| 14 | /// Protocol for retrieving a high-resolution timestamp counter. |
| 15 | /// |
| 16 | /// # Note |
| 17 | /// If your UEFI firmware not support timestamp protocol which first added at |
| 18 | /// UEFI spec 2.4 2013. you also could use `RDTSC` in rust, here is a demo |
| 19 | /// [Slint-UI](https://github.com/slint-ui/slint/blob/2c0ba2bc0f151eba8d1fa17839fa2ac58832ca80/examples/uefi-demo/main.rs#L28-L62) |
| 20 | /// who use uefi-rs. |
| 21 | /// |
| 22 | /// [`Protocol`]: uefi::proto::Protocol |
| 23 | #[derive(Debug)] |
| 24 | #[repr(transparent)] |
| 25 | #[unsafe_protocol(TimestampProtocol::GUID)] |
| 26 | pub struct Timestamp(TimestampProtocol); |
| 27 | |
| 28 | impl Timestamp { |
| 29 | /// Get the current value of the timestamp counter. |
| 30 | #[must_use] |
| 31 | pub fn get_timestamp(&self) -> u64 { |
| 32 | // SAFETY: The memory is valid. |
| 33 | unsafe { (self.0.get_timestamp)() } |
| 34 | } |
| 35 | |
| 36 | /// Get the properties of the timestamp counter. |
| 37 | pub fn get_properties(&self) -> Result<TimestampProperties> { |
| 38 | let mut properties = TimestampProperties::default(); |
| 39 | // SAFETY: The memory is valid. |
| 40 | unsafe { (self.0.get_properties)(&mut properties) }.to_result_with_val(|| properties) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// Reset Notification [`Protocol`]. |
| 45 | /// |
| 46 | /// Protocol to register for a notification when ResetSystem is called. |
| 47 | /// |
| 48 | /// [`Protocol`]: uefi::proto::Protocol |
| 49 | #[derive(Debug)] |
| 50 | #[repr(transparent)] |
| 51 | #[unsafe_protocol(ResetNotificationProtocol::GUID)] |
| 52 | pub struct ResetNotification(ResetNotificationProtocol); |
| 53 | |
| 54 | impl ResetNotification { |
| 55 | /// Register a notification function to be called when ResetSystem() is called. |
| 56 | /// |
| 57 | /// |
| 58 | /// # Example |
| 59 | /// |
| 60 | /// ```rust |
| 61 | /// use log::info; |
| 62 | /// use uefi::{boot, Handle}; |
| 63 | /// use uefi::proto::misc::{ResetNotification}; |
| 64 | /// use uefi_raw::Status; |
| 65 | /// use uefi_raw::table::runtime; |
| 66 | /// |
| 67 | /// |
| 68 | /// // value efi_reset_fn is the type of ResetSystemFn, a function pointer |
| 69 | /// unsafe extern "efiapi" fn efi_reset_fn( |
| 70 | /// rt: runtime::ResetType, |
| 71 | /// status: Status, |
| 72 | /// data_size: usize, |
| 73 | /// data: *const u8, |
| 74 | /// ){ |
| 75 | /// info!("Inside the event callback"); |
| 76 | /// info!("do what you want"); |
| 77 | /// } |
| 78 | /// |
| 79 | /// pub fn test(image: Handle) { |
| 80 | /// |
| 81 | /// let mut rn = boot::open_protocol_exclusive::<ResetNotification>(image) |
| 82 | /// .expect("Failed to open Timestamp protocol"); |
| 83 | /// |
| 84 | /// rn.register_reset_notify(efi_reset_fn) |
| 85 | /// .expect("Failed to register a reset notification function!"); |
| 86 | /// } |
| 87 | /// ``` |
| 88 | pub fn register_reset_notify(&mut self, reset_function: ResetSystemFn) -> Result { |
| 89 | // SAFETY: The memory is valid. |
| 90 | unsafe { (self.0.register_reset_notify)(&mut self.0, reset_function) }.to_result() |
| 91 | } |
| 92 | |
| 93 | /// Remove a reset notification function that was previously registered with [`ResetNotification::register_reset_notify`]. |
| 94 | pub fn unregister_reset_notify(&mut self, reset_function: ResetSystemFn) -> Result { |
| 95 | // SAFETY: The memory is valid. |
| 96 | unsafe { (self.0.unregister_reset_notify)(&mut self.0, reset_function) }.to_result() |
| 97 | } |
| 98 | } |