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 / runtime / mod.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! UEFI runtime services. |
| 4 | //! |
| 5 | //! These services are available both before and after exiting boot |
| 6 | //! services. Note that various restrictions apply when calling runtime services |
| 7 | //! functions after exiting boot services; see the "Calling Convention" section |
| 8 | //! of the UEFI specification for details. |
| 9 | |
| 10 | mod time; |
| 11 | |
| 12 | pub use time::*; |
| 13 | |
| 14 | use crate::data_types::PhysicalAddress; |
| 15 | use crate::table::{self, Revision}; |
| 16 | use crate::{CStr16, Error, Result, Status, StatusExt}; |
| 17 | use core::fmt::Debug; |
| 18 | #[cfg(feature = "alloc")] |
| 19 | use core::fmt::{self, Display, Formatter}; |
| 20 | use core::ptr::{self, NonNull}; |
| 21 | use uefi_raw::table::boot::MemoryDescriptor; |
| 22 | use uefi_raw::table::runtime::RuntimeServices; |
| 23 | use uefi_raw::table::system::SystemTable; |
| 24 | |
| 25 | #[cfg(feature = "alloc")] |
| 26 | use { |
| 27 | crate::CString16, |
| 28 | crate::Guid, |
| 29 | crate::mem::make_boxed, |
| 30 | alloc::borrow::ToOwned, |
| 31 | alloc::boxed::Box, |
| 32 | alloc::{vec, vec::Vec}, |
| 33 | }; |
| 34 | |
| 35 | pub use uefi_raw::capsule::{CapsuleBlockDescriptor, CapsuleFlags, CapsuleHeader}; |
| 36 | pub use uefi_raw::table::runtime::{ |
| 37 | ResetType, TimeCapabilities, VariableAttributes, VariableVendor, |
| 38 | }; |
| 39 | pub use uefi_raw::time::Daylight; |
| 40 | |
| 41 | fn runtime_services_raw_panicking() -> NonNull<RuntimeServices> { |
| 42 | let st = table::system_table_raw_panicking(); |
| 43 | // SAFETY: valid per requirements of `set_system_table`. |
| 44 | let st = unsafe { st.as_ref() }; |
| 45 | NonNull::new(st.runtime_services).expect("runtime services are not active") |
| 46 | } |
| 47 | |
| 48 | /// Query the current time and date information. |
| 49 | pub fn get_time() -> Result<Time> { |
| 50 | let rt = runtime_services_raw_panicking(); |
| 51 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 52 | let rt = unsafe { rt.as_ref() }; |
| 53 | |
| 54 | let mut time = Time::invalid(); |
| 55 | let time_ptr: *mut Time = &mut time; |
| 56 | // SAFETY: The memory is valid. |
| 57 | unsafe { (rt.get_time)(time_ptr.cast(), ptr::null_mut()) }.to_result_with_val(|| time) |
| 58 | } |
| 59 | |
| 60 | /// Query the current time and date information and the RTC capabilities. |
| 61 | pub fn get_time_and_caps() -> Result<(Time, TimeCapabilities)> { |
| 62 | let rt = runtime_services_raw_panicking(); |
| 63 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 64 | let rt = unsafe { rt.as_ref() }; |
| 65 | |
| 66 | let mut time = Time::invalid(); |
| 67 | let time_ptr: *mut Time = &mut time; |
| 68 | let mut caps = TimeCapabilities::default(); |
| 69 | // SAFETY: The memory is valid. |
| 70 | unsafe { (rt.get_time)(time_ptr.cast(), &mut caps) }.to_result_with_val(|| (time, caps)) |
| 71 | } |
| 72 | |
| 73 | /// Sets the current local time and date information |
| 74 | /// |
| 75 | /// During runtime, if a PC-AT CMOS device is present in the platform, the |
| 76 | /// caller must synchronize access to the device before calling `set_time`. |
| 77 | /// |
| 78 | /// # Safety |
| 79 | /// |
| 80 | /// Undefined behavior could happen if multiple tasks try to |
| 81 | /// use this function at the same time without synchronisation. |
| 82 | pub unsafe fn set_time(time: &Time) -> Result { |
| 83 | let rt = runtime_services_raw_panicking(); |
| 84 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 85 | let rt = unsafe { rt.as_ref() }; |
| 86 | |
| 87 | let time: *const Time = time; |
| 88 | // SAFETY: The memory is valid. |
| 89 | unsafe { (rt.set_time)(time.cast()) }.to_result() |
| 90 | } |
| 91 | |
| 92 | /// Checks if a variable exists. |
| 93 | /// |
| 94 | /// Returns `Ok(true)` if the variable exists, `Ok(false)` if the variable does |
| 95 | /// not exist, or `Err` if the existence of the variable could not be determined. |
| 96 | /// |
| 97 | /// # Errors |
| 98 | /// |
| 99 | /// * [`Status::DEVICE_ERROR`]: variable could not be read due to a hardware error. |
| 100 | /// * [`Status::SECURITY_VIOLATION`]: variable could not be read due to an |
| 101 | /// authentication error. |
| 102 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 103 | /// after exiting boot services. |
| 104 | pub fn variable_exists(name: &CStr16, vendor: &VariableVendor) -> Result<bool> { |
| 105 | let rt = runtime_services_raw_panicking(); |
| 106 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 107 | let rt = unsafe { rt.as_ref() }; |
| 108 | |
| 109 | let attributes = ptr::null_mut(); |
| 110 | let data = ptr::null_mut(); |
| 111 | let mut data_size = 0; |
| 112 | |
| 113 | // SAFETY: The memory is valid. |
| 114 | let status = unsafe { |
| 115 | (rt.get_variable)( |
| 116 | name.as_ptr().cast(), |
| 117 | &vendor.0, |
| 118 | attributes, |
| 119 | &mut data_size, |
| 120 | data, |
| 121 | ) |
| 122 | }; |
| 123 | |
| 124 | match status { |
| 125 | // If the variable exists, the status will be BUFFER_TOO_SMALL because |
| 126 | // data_size is 0. Empty variables do not exist, because setting a |
| 127 | // variable with empty data deletes the variable. In other words, the |
| 128 | // status will never be SUCCESS. |
| 129 | Status::BUFFER_TOO_SMALL => Ok(true), |
| 130 | Status::NOT_FOUND => Ok(false), |
| 131 | _ => Err(Error::from(status)), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Gets the contents and attributes of a variable. The size of `buf` must be at |
| 136 | /// least as big as the variable's size, although it can be larger. |
| 137 | /// |
| 138 | /// On success, returns a tuple containing the variable's value (a slice of |
| 139 | /// `buf`) and the variable's attributes. |
| 140 | /// |
| 141 | /// # Errors |
| 142 | /// |
| 143 | /// * [`Status::NOT_FOUND`]: variable was not found. |
| 144 | /// * [`Status::BUFFER_TOO_SMALL`]: `buf` is not large enough. The required size |
| 145 | /// will be returned in the error data. |
| 146 | /// * [`Status::DEVICE_ERROR`]: variable could not be read due to a hardware error. |
| 147 | /// * [`Status::SECURITY_VIOLATION`]: variable could not be read due to an |
| 148 | /// authentication error. |
| 149 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 150 | /// after exiting boot services. |
| 151 | pub fn get_variable<'buf>( |
| 152 | name: &CStr16, |
| 153 | vendor: &VariableVendor, |
| 154 | buf: &'buf mut [u8], |
| 155 | ) -> Result<(&'buf mut [u8], VariableAttributes), Option<usize>> { |
| 156 | let rt = runtime_services_raw_panicking(); |
| 157 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 158 | let rt = unsafe { rt.as_ref() }; |
| 159 | |
| 160 | let mut attributes = VariableAttributes::empty(); |
| 161 | let mut data_size = buf.len(); |
| 162 | // SAFETY: The memory is valid. |
| 163 | let status = unsafe { |
| 164 | (rt.get_variable)( |
| 165 | name.as_ptr().cast(), |
| 166 | &vendor.0, |
| 167 | &mut attributes, |
| 168 | &mut data_size, |
| 169 | buf.as_mut_ptr(), |
| 170 | ) |
| 171 | }; |
| 172 | |
| 173 | match status { |
| 174 | Status::SUCCESS => Ok((&mut buf[..data_size], attributes)), |
| 175 | Status::BUFFER_TOO_SMALL => Err(Error::new(status, Some(data_size))), |
| 176 | _ => Err(Error::new(status, None)), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Gets the contents and attributes of a variable. |
| 181 | /// |
| 182 | /// # Errors |
| 183 | /// |
| 184 | /// * [`Status::NOT_FOUND`]: variable was not found. |
| 185 | /// * [`Status::DEVICE_ERROR`]: variable could not be read due to a hardware error. |
| 186 | /// * [`Status::SECURITY_VIOLATION`]: variable could not be read due to an |
| 187 | /// authentication error. |
| 188 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 189 | /// after exiting boot services. |
| 190 | #[cfg(feature = "alloc")] |
| 191 | pub fn get_variable_boxed( |
| 192 | name: &CStr16, |
| 193 | vendor: &VariableVendor, |
| 194 | ) -> Result<(Box<[u8]>, VariableAttributes)> { |
| 195 | let mut out_attr = VariableAttributes::empty(); |
| 196 | let get_var = |buf| { |
| 197 | get_variable(name, vendor, buf).map(|(val, attr)| { |
| 198 | // `make_boxed` expects only a DST value to be returned (`val` in |
| 199 | // this case), so smuggle the `attr` value out via a separate |
| 200 | // variable. |
| 201 | out_attr = attr; |
| 202 | val |
| 203 | }) |
| 204 | }; |
| 205 | make_boxed(get_var).map(|val| (val, out_attr)) |
| 206 | } |
| 207 | |
| 208 | /// Gets each variable key (name and vendor) one at a time. |
| 209 | /// |
| 210 | /// This is used to iterate over variable keys. See [`variable_keys`] for a more |
| 211 | /// convenient interface that requires the `alloc` feature. |
| 212 | /// |
| 213 | /// To get the first variable key, `name` must be initialized to start with a |
| 214 | /// null character. The `vendor` value is arbitrary. On success, the first |
| 215 | /// variable's name and vendor will be written out to `name` and `vendor`. Keep |
| 216 | /// calling `get_next_variable_key` with the same `name` and `vendor` references |
| 217 | /// to get the remaining variable keys. |
| 218 | /// |
| 219 | /// All variable names should be valid strings, but this may not be enforced by |
| 220 | /// firmware. To convert to a string, truncate at the first null and call |
| 221 | /// [`CStr16::from_u16_with_nul`]. |
| 222 | /// |
| 223 | /// # Errors |
| 224 | /// |
| 225 | /// * [`Status::NOT_FOUND`]: indicates end of iteration, the last variable keys |
| 226 | /// was retrieved by the previous call to `get_next_variable_key`. |
| 227 | /// * [`Status::BUFFER_TOO_SMALL`]: `name` is not large enough. The required |
| 228 | /// size (in `u16` characters, not bytes) will be returned in the error data. |
| 229 | /// * [`Status::INVALID_PARAMETER`]: `name` does not contain a null character, or |
| 230 | /// the `name` and `vendor` are not an existing variable. |
| 231 | /// * [`Status::DEVICE_ERROR`]: variable could not be read due to a hardware error. |
| 232 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 233 | /// after exiting boot services. |
| 234 | pub fn get_next_variable_key( |
| 235 | name: &mut [u16], |
| 236 | vendor: &mut VariableVendor, |
| 237 | ) -> Result<(), Option<usize>> { |
| 238 | let rt = runtime_services_raw_panicking(); |
| 239 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 240 | let rt = unsafe { rt.as_ref() }; |
| 241 | |
| 242 | let mut name_size_in_bytes = size_of_val(name); |
| 243 | |
| 244 | // SAFETY: The memory is valid. |
| 245 | let status = unsafe { |
| 246 | (rt.get_next_variable_name)(&mut name_size_in_bytes, name.as_mut_ptr(), &mut vendor.0) |
| 247 | }; |
| 248 | match status { |
| 249 | Status::SUCCESS => Ok(()), |
| 250 | Status::BUFFER_TOO_SMALL => Err(Error::new( |
| 251 | status, |
| 252 | Some(name_size_in_bytes / size_of::<u16>()), |
| 253 | )), |
| 254 | _ => Err(Error::new(status, None)), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /// Get an iterator over all UEFI variables. |
| 259 | /// |
| 260 | /// See [`VariableKeys`] for details. |
| 261 | #[cfg(feature = "alloc")] |
| 262 | #[must_use] |
| 263 | pub fn variable_keys() -> VariableKeys { |
| 264 | VariableKeys::new() |
| 265 | } |
| 266 | |
| 267 | /// Iterator over all UEFI variables. |
| 268 | /// |
| 269 | /// Each iteration yields a <code>Result<`[`VariableKey`]`></code>. Error values: |
| 270 | /// |
| 271 | /// * [`Status::DEVICE_ERROR`]: variable could not be read due to a hardware error. |
| 272 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 273 | /// after exiting boot services. |
| 274 | #[cfg(feature = "alloc")] |
| 275 | #[derive(Debug)] |
| 276 | pub struct VariableKeys { |
| 277 | name: Vec<u16>, |
| 278 | vendor: VariableVendor, |
| 279 | is_done: bool, |
| 280 | } |
| 281 | |
| 282 | #[cfg(feature = "alloc")] |
| 283 | impl VariableKeys { |
| 284 | fn new() -> Self { |
| 285 | // Create a name buffer with a large default size and zero |
| 286 | // initialize it. A Toshiba Satellite Pro R50-B-12P was found |
| 287 | // to not correctly update the VariableNameSize passed into |
| 288 | // GetNextVariableName and starting with a large buffer works |
| 289 | // around this issue. |
| 290 | let name = vec![0; 512]; |
| 291 | |
| 292 | Self { |
| 293 | name, |
| 294 | // The initial vendor GUID is arbitrary. |
| 295 | vendor: VariableVendor(Guid::default()), |
| 296 | is_done: false, |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | #[cfg(feature = "alloc")] |
| 302 | impl Iterator for VariableKeys { |
| 303 | type Item = Result<VariableKey>; |
| 304 | |
| 305 | fn next(&mut self) -> Option<Result<VariableKey>> { |
| 306 | if self.is_done { |
| 307 | return None; |
| 308 | } |
| 309 | |
| 310 | let mut result = get_next_variable_key(&mut self.name, &mut self.vendor); |
| 311 | |
| 312 | // If the name buffer was too small, resize it to be big enough and call |
| 313 | // `get_next_variable_key` again. |
| 314 | if let Err(err) = &result |
| 315 | && let Some(required_size) = err.data() |
| 316 | { |
| 317 | self.name.resize(*required_size, 0u16); |
| 318 | result = get_next_variable_key(&mut self.name, &mut self.vendor); |
| 319 | } |
| 320 | |
| 321 | match result { |
| 322 | Ok(()) => { |
| 323 | // Convert the name to a `CStr16`, yielding an error if invalid. |
| 324 | let Ok(name) = CStr16::from_u16_until_nul(&self.name) else { |
| 325 | return Some(Err(Status::UNSUPPORTED.into())); |
| 326 | }; |
| 327 | |
| 328 | Some(Ok(VariableKey { |
| 329 | name: name.to_owned(), |
| 330 | vendor: self.vendor, |
| 331 | })) |
| 332 | } |
| 333 | Err(err) => { |
| 334 | self.is_done = true; |
| 335 | if err.status() == Status::NOT_FOUND { |
| 336 | // This status indicates the end of the list. The final variable |
| 337 | // has already been yielded at this point, so return `None`. |
| 338 | None |
| 339 | } else { |
| 340 | // Return the error and end iteration. |
| 341 | Some(Err(err.to_err_without_payload())) |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /// Sets the value of a variable. This can be used to create a new variable, |
| 349 | /// update an existing variable, or (when the size of `data` is zero) |
| 350 | /// delete a variable. |
| 351 | /// |
| 352 | /// # Warnings |
| 353 | /// |
| 354 | /// The [`Status::WARN_RESET_REQUIRED`] warning will be returned when using |
| 355 | /// this function to transition the Secure Boot mode to setup mode or audit |
| 356 | /// mode if the firmware requires a reboot for that operation. |
| 357 | /// |
| 358 | /// # Errors |
| 359 | /// |
| 360 | /// * [`Status::INVALID_PARAMETER`]: invalid attributes, name, or vendor. |
| 361 | /// * [`Status::OUT_OF_RESOURCES`]: not enough storage is available to hold |
| 362 | /// the variable. |
| 363 | /// * [`Status::WRITE_PROTECTED`]: variable is read-only. |
| 364 | /// * [`Status::SECURITY_VIOLATION`]: variable could not be written due to an |
| 365 | /// authentication error. |
| 366 | /// * [`Status::NOT_FOUND`]: attempted to update a non-existent variable. |
| 367 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 368 | /// after exiting boot services. |
| 369 | pub fn set_variable( |
| 370 | name: &CStr16, |
| 371 | vendor: &VariableVendor, |
| 372 | attributes: VariableAttributes, |
| 373 | data: &[u8], |
| 374 | ) -> Result { |
| 375 | let rt = runtime_services_raw_panicking(); |
| 376 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 377 | let rt = unsafe { rt.as_ref() }; |
| 378 | |
| 379 | // SAFETY: The memory is valid. |
| 380 | unsafe { |
| 381 | (rt.set_variable)( |
| 382 | name.as_ptr().cast(), |
| 383 | &vendor.0, |
| 384 | attributes, |
| 385 | data.len(), |
| 386 | data.as_ptr(), |
| 387 | ) |
| 388 | .to_result() |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /// Deletes a UEFI variable. |
| 393 | /// |
| 394 | /// # Errors |
| 395 | /// |
| 396 | /// * [`Status::INVALID_PARAMETER`]: invalid name or vendor. |
| 397 | /// * [`Status::WRITE_PROTECTED`]: variable is read-only. |
| 398 | /// * [`Status::SECURITY_VIOLATION`]: variable could not be deleted due to an |
| 399 | /// authentication error. |
| 400 | /// * [`Status::NOT_FOUND`]: attempted to delete a non-existent variable. |
| 401 | /// * [`Status::UNSUPPORTED`]: this platform does not support variable storage |
| 402 | /// after exiting boot services. |
| 403 | pub fn delete_variable(name: &CStr16, vendor: &VariableVendor) -> Result { |
| 404 | set_variable(name, vendor, VariableAttributes::empty(), &[]) |
| 405 | } |
| 406 | |
| 407 | /// Get information about UEFI variable storage space for the type |
| 408 | /// of variable specified in `attributes`. |
| 409 | /// |
| 410 | /// This operation is only supported starting with UEFI 2.0. |
| 411 | /// |
| 412 | /// See [`VariableStorageInfo`] for details of the information returned. |
| 413 | /// |
| 414 | /// # Errors |
| 415 | /// |
| 416 | /// * [`Status::INVALID_PARAMETER`]: invalid combination of variable attributes. |
| 417 | /// * [`Status::UNSUPPORTED`]: the combination of variable attributes is not |
| 418 | /// supported on this platform, or the UEFI version is less than 2.0. |
| 419 | pub fn query_variable_info(attributes: VariableAttributes) -> Result<VariableStorageInfo> { |
| 420 | let rt = runtime_services_raw_panicking(); |
| 421 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 422 | let rt = unsafe { rt.as_ref() }; |
| 423 | |
| 424 | if rt.header.revision < Revision::EFI_2_00 { |
| 425 | return Err(Status::UNSUPPORTED.into()); |
| 426 | } |
| 427 | |
| 428 | let mut info = VariableStorageInfo::default(); |
| 429 | // SAFETY: The memory is valid. |
| 430 | unsafe { |
| 431 | (rt.query_variable_info)( |
| 432 | attributes, |
| 433 | &mut info.maximum_variable_storage_size, |
| 434 | &mut info.remaining_variable_storage_size, |
| 435 | &mut info.maximum_variable_size, |
| 436 | ) |
| 437 | .to_result_with_val(|| info) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /// Passes capsules to the firmware. |
| 442 | /// |
| 443 | /// Capsules are most commonly used to update system firmware. |
| 444 | /// |
| 445 | /// # Errors |
| 446 | /// |
| 447 | /// * [`Status::INVALID_PARAMETER`]: zero capsules were provided, or the |
| 448 | /// capsules are invalid. |
| 449 | /// * [`Status::DEVICE_ERROR`]: the capsule update was started but failed to a |
| 450 | /// device error. |
| 451 | /// * [`Status::OUT_OF_RESOURCES`]: before exiting boot services, indicates the |
| 452 | /// capsule is compatible with the platform but there are insufficient |
| 453 | /// resources to complete the update. After exiting boot services, indicates |
| 454 | /// the capsule is compatible with the platform but can only be processed |
| 455 | /// before exiting boot services. |
| 456 | /// * [`Status::UNSUPPORTED`]: this platform does not support capsule updates |
| 457 | /// after exiting boot services. |
| 458 | pub fn update_capsule( |
| 459 | capsule_header_array: &[&CapsuleHeader], |
| 460 | capsule_block_descriptors: &[CapsuleBlockDescriptor], |
| 461 | ) -> Result { |
| 462 | let rt = runtime_services_raw_panicking(); |
| 463 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 464 | let rt = unsafe { rt.as_ref() }; |
| 465 | |
| 466 | // SAFETY: The memory is valid. |
| 467 | unsafe { |
| 468 | (rt.update_capsule)( |
| 469 | capsule_header_array.as_ptr().cast(), |
| 470 | capsule_header_array.len(), |
| 471 | capsule_block_descriptors.as_ptr() as PhysicalAddress, |
| 472 | ) |
| 473 | .to_result() |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /// Tests whether a capsule or capsules can be updated via [`update_capsule`]. |
| 478 | /// |
| 479 | /// See [`CapsuleInfo`] for details of the information returned. |
| 480 | /// |
| 481 | /// # Errors |
| 482 | /// |
| 483 | /// * [`Status::OUT_OF_RESOURCES`]: before exiting boot services, indicates the |
| 484 | /// capsule is compatible with the platform but there are insufficient |
| 485 | /// resources to complete the update. After exiting boot services, indicates |
| 486 | /// the capsule is compatible with the platform but can only be processed |
| 487 | /// before exiting boot services. |
| 488 | /// * [`Status::UNSUPPORTED`]: either the capsule type is not supported by this |
| 489 | /// platform, or the platform does not support capsule updates after exiting |
| 490 | /// boot services. |
| 491 | pub fn query_capsule_capabilities(capsule_header_array: &[&CapsuleHeader]) -> Result<CapsuleInfo> { |
| 492 | let rt = runtime_services_raw_panicking(); |
| 493 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 494 | let rt = unsafe { rt.as_ref() }; |
| 495 | |
| 496 | let mut info = CapsuleInfo::default(); |
| 497 | // SAFETY: The memory is valid. |
| 498 | unsafe { |
| 499 | (rt.query_capsule_capabilities)( |
| 500 | capsule_header_array.as_ptr().cast(), |
| 501 | capsule_header_array.len(), |
| 502 | &mut info.maximum_capsule_size, |
| 503 | &mut info.reset_type, |
| 504 | ) |
| 505 | .to_result_with_val(|| info) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /// Resets the computer. |
| 510 | /// |
| 511 | /// See [`ResetType`] for details of the various reset types. |
| 512 | /// |
| 513 | /// For a normal reset the value of `status` should be |
| 514 | /// [`Status::SUCCESS`]. Otherwise, an error code can be used. |
| 515 | /// |
| 516 | /// The `data` arg is usually `None`. Otherwise, it must contain a UCS-2 |
| 517 | /// null-terminated string followed by additional binary data. For |
| 518 | /// [`ResetType::PLATFORM_SPECIFIC`], the binary data must be a vendor-specific |
| 519 | /// [`Guid`] that indicates the type of reset to perform. |
| 520 | /// |
| 521 | /// This function never returns. |
| 522 | pub fn reset(reset_type: ResetType, status: Status, data: Option<&[u8]>) -> ! { |
| 523 | let rt = runtime_services_raw_panicking(); |
| 524 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 525 | let rt = unsafe { rt.as_ref() }; |
| 526 | |
| 527 | let (size, data) = data |
| 528 | .map(|data| (data.len(), data.as_ptr())) |
| 529 | .unwrap_or((0, ptr::null())); |
| 530 | |
| 531 | // SAFETY: The memory is valid. |
| 532 | unsafe { (rt.reset_system)(reset_type, status, size, data) } |
| 533 | } |
| 534 | |
| 535 | /// Changes the runtime addressing mode of EFI firmware from physical to |
| 536 | /// virtual. |
| 537 | /// |
| 538 | /// It is up to the caller to translate the old system table address |
| 539 | /// to a new virtual address and provide it for this function. |
| 540 | /// |
| 541 | /// If successful, this function will call [`set_system_table`] with |
| 542 | /// `new_system_table_virtual_addr`. |
| 543 | /// |
| 544 | /// [`set_system_table`]: table::set_system_table |
| 545 | /// |
| 546 | /// # Safety |
| 547 | /// |
| 548 | /// The caller must ensure the memory map is valid. |
| 549 | /// |
| 550 | /// # Errors |
| 551 | /// |
| 552 | /// * [`Status::UNSUPPORTED`]: either boot services haven't been exited, the |
| 553 | /// firmware's addressing mode is already virtual, or the firmware does not |
| 554 | /// support this operation. |
| 555 | /// * [`Status::NO_MAPPING`]: `map` is missing a required range. |
| 556 | /// * [`Status::NOT_FOUND`]: `map` contains an address that is not in the |
| 557 | /// current memory map. |
| 558 | pub unsafe fn set_virtual_address_map( |
| 559 | map: &[MemoryDescriptor], |
| 560 | new_system_table_virtual_addr: *const SystemTable, |
| 561 | ) -> Result { |
| 562 | let rt = runtime_services_raw_panicking(); |
| 563 | // SAFETY: The pointer is not null and we assume it to be initialized. |
| 564 | let rt = unsafe { rt.as_ref() }; |
| 565 | |
| 566 | // Unsafe Code Guidelines guarantees that there is no padding in an array or a slice |
| 567 | // between its elements if the element type is `repr(C)`, which is our case. |
| 568 | // |
| 569 | // See https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html |
| 570 | let map_size = size_of_val(map); |
| 571 | let entry_size = size_of::<MemoryDescriptor>(); |
| 572 | let entry_version = MemoryDescriptor::VERSION; |
| 573 | let map_ptr = map.as_ptr(); |
| 574 | // SAFETY: The memory is valid. |
| 575 | unsafe { (rt.set_virtual_address_map)(map_size, entry_size, entry_version, map_ptr) } |
| 576 | .to_result()?; |
| 577 | |
| 578 | // Update the global system table pointer. |
| 579 | // SAFETY: The memory is valid. |
| 580 | unsafe { table::set_system_table(new_system_table_virtual_addr) }; |
| 581 | |
| 582 | Ok(()) |
| 583 | } |
| 584 | |
| 585 | /// Unique key for a variable. |
| 586 | #[cfg(feature = "alloc")] |
| 587 | #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 588 | pub struct VariableKey { |
| 589 | /// Unique identifier for the vendor. |
| 590 | pub vendor: VariableVendor, |
| 591 | |
| 592 | /// Name of the variable, unique with the vendor namespace. |
| 593 | pub name: CString16, |
| 594 | } |
| 595 | |
| 596 | #[cfg(feature = "alloc")] |
| 597 | impl Display for VariableKey { |
| 598 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 599 | write!(f, "VariableKey {{ name: \"{}\", vendor: ", self.name)?; |
| 600 | |
| 601 | if self.vendor == VariableVendor::GLOBAL_VARIABLE { |
| 602 | write!(f, "GLOBAL_VARIABLE")?; |
| 603 | } else { |
| 604 | write!(f, "{}", self.vendor.0)?; |
| 605 | } |
| 606 | |
| 607 | write!(f, " }}") |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /// Information about UEFI variable storage space returned by |
| 612 | /// [`query_variable_info`]. |
| 613 | /// |
| 614 | /// Note that the data here is limited to a specific type of variable (as |
| 615 | /// specified by the `attributes` argument to `query_variable_info`). |
| 616 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 617 | pub struct VariableStorageInfo { |
| 618 | /// Maximum size in bytes of the storage space available for |
| 619 | /// variables of the specified type. |
| 620 | pub maximum_variable_storage_size: u64, |
| 621 | |
| 622 | /// Remaining size in bytes of the storage space available for |
| 623 | /// variables of the specified type. |
| 624 | pub remaining_variable_storage_size: u64, |
| 625 | |
| 626 | /// Maximum size of an individual variable of the specified type. |
| 627 | pub maximum_variable_size: u64, |
| 628 | } |
| 629 | |
| 630 | /// Information about UEFI variable storage space returned by |
| 631 | /// [`query_capsule_capabilities`]. |
| 632 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 633 | pub struct CapsuleInfo { |
| 634 | /// The maximum size in bytes that [`update_capsule`] |
| 635 | /// can support as input. Note that the size of an update capsule is composed of |
| 636 | /// all [`CapsuleHeader`]s and [CapsuleBlockDescriptor]s. |
| 637 | pub maximum_capsule_size: u64, |
| 638 | |
| 639 | /// The type of reset required for the capsule update. |
| 640 | pub reset_type: ResetType, |
| 641 | } |