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 / device_path / util.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Protocol with utility functions for working with device paths. |
| 4 | |
| 5 | use super::{DevicePath, DevicePathNode, PoolDevicePath}; |
| 6 | use crate::mem::PoolAllocation; |
| 7 | use core::ptr::NonNull; |
| 8 | use uefi_macros::unsafe_protocol; |
| 9 | use uefi_raw::Status; |
| 10 | use uefi_raw::protocol::device_path::DevicePathUtilitiesProtocol; |
| 11 | |
| 12 | /// Protocol with utility functions for working with device paths. |
| 13 | #[derive(Debug)] |
| 14 | #[repr(transparent)] |
| 15 | #[unsafe_protocol(DevicePathUtilitiesProtocol::GUID)] |
| 16 | pub struct DevicePathUtilities(DevicePathUtilitiesProtocol); |
| 17 | |
| 18 | impl DevicePathUtilities { |
| 19 | /// Retrieves the size of the specified device path in bytes, including the |
| 20 | /// end-of-device-path node. |
| 21 | /// |
| 22 | /// # Arguments |
| 23 | /// - `device_path`: A reference to the [`DevicePath`] whose size is to be determined. |
| 24 | /// |
| 25 | /// # Returns |
| 26 | /// The size of the specified device path in bytes. |
| 27 | #[must_use] |
| 28 | pub fn get_size(&self, device_path: &DevicePath) -> usize { |
| 29 | // SAFETY: The memory is valid. |
| 30 | unsafe { (self.0.get_device_path_size)(device_path.as_ffi_ptr().cast()) } |
| 31 | } |
| 32 | |
| 33 | /// Create a new device path by cloning the given `path` into newly allocated memory. |
| 34 | /// |
| 35 | /// # Arguments |
| 36 | /// - `path`: A reference to the device path to clone. |
| 37 | /// |
| 38 | /// # Returns |
| 39 | /// A [`PoolDevicePath`] instance created by cloning the given `path`. |
| 40 | pub fn duplicate_path(&self, path: &DevicePath) -> crate::Result<PoolDevicePath> { |
| 41 | // SAFETY: The memory is valid. |
| 42 | unsafe { |
| 43 | let ptr = (self.0.duplicate_device_path)(path.as_ffi_ptr().cast()); |
| 44 | NonNull::new(ptr) |
| 45 | .map(|p| PoolDevicePath(PoolAllocation::new(p.cast()))) |
| 46 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Creates a new device path by appending the second device path to the first. |
| 51 | /// |
| 52 | /// # Arguments |
| 53 | /// - `path0`: A reference to the base device path. |
| 54 | /// - `path1`: A reference to the device path to append. |
| 55 | /// |
| 56 | /// # Returns |
| 57 | /// A [`PoolDevicePath`] instance containing the newly created device path, |
| 58 | /// or an error if memory could not be allocated. |
| 59 | pub fn append_path( |
| 60 | &self, |
| 61 | path0: &DevicePath, |
| 62 | path1: &DevicePath, |
| 63 | ) -> crate::Result<PoolDevicePath> { |
| 64 | // SAFETY: The memory is valid. |
| 65 | unsafe { |
| 66 | let ptr = |
| 67 | (self.0.append_device_path)(path0.as_ffi_ptr().cast(), path1.as_ffi_ptr().cast()); |
| 68 | NonNull::new(ptr) |
| 69 | .map(|p| PoolDevicePath(PoolAllocation::new(p.cast()))) |
| 70 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Creates a new device path by appending a device node to the base device path. |
| 75 | /// |
| 76 | /// # Arguments |
| 77 | /// - `basepath`: A reference to the base device path. |
| 78 | /// - `node`: A reference to the device node to append. |
| 79 | /// |
| 80 | /// # Returns |
| 81 | /// A [`PoolDevicePath`] instance containing the newly created device path, |
| 82 | /// or an error if memory could not be allocated. |
| 83 | pub fn append_node( |
| 84 | &self, |
| 85 | basepath: &DevicePath, |
| 86 | node: &DevicePathNode, |
| 87 | ) -> crate::Result<PoolDevicePath> { |
| 88 | // SAFETY: The memory is valid. |
| 89 | unsafe { |
| 90 | let ptr = |
| 91 | (self.0.append_device_node)(basepath.as_ffi_ptr().cast(), node.as_ffi_ptr().cast()); |
| 92 | NonNull::new(ptr) |
| 93 | .map(|p| PoolDevicePath(PoolAllocation::new(p.cast()))) |
| 94 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Creates a new device path by appending the specified device path instance to the base path. |
| 99 | /// |
| 100 | /// # Arguments |
| 101 | /// - `basepath`: A reference to the base device path. |
| 102 | /// - `instance`: A reference to the device path instance to append. |
| 103 | /// |
| 104 | /// # Returns |
| 105 | /// A [`PoolDevicePath`] instance containing the newly created device path, |
| 106 | /// or an error if memory could not be allocated. |
| 107 | pub fn append_instance( |
| 108 | &self, |
| 109 | basepath: &DevicePath, |
| 110 | instance: &DevicePath, |
| 111 | ) -> crate::Result<PoolDevicePath> { |
| 112 | // SAFETY: The memory is valid. |
| 113 | unsafe { |
| 114 | let ptr = (self.0.append_device_path_instance)( |
| 115 | basepath.as_ffi_ptr().cast(), |
| 116 | instance.as_ffi_ptr().cast(), |
| 117 | ); |
| 118 | NonNull::new(ptr) |
| 119 | .map(|p| PoolDevicePath(PoolAllocation::new(p.cast()))) |
| 120 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 121 | } |
| 122 | } |
| 123 | } |