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 / ata / pass_thru.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! ATA Pass Thru Protocol. |
| 4 | |
| 5 | use super::{AtaRequest, AtaResponse}; |
| 6 | use crate::StatusExt; |
| 7 | use crate::mem::{AlignedBuffer, PoolAllocation}; |
| 8 | use crate::proto::device_path::PoolDevicePathNode; |
| 9 | use core::alloc::LayoutError; |
| 10 | use core::cell::UnsafeCell; |
| 11 | use core::ptr::{self, NonNull}; |
| 12 | use uefi_macros::unsafe_protocol; |
| 13 | use uefi_raw::Status; |
| 14 | use uefi_raw::protocol::ata::AtaPassThruProtocol; |
| 15 | use uefi_raw::protocol::device_path::DevicePathProtocol; |
| 16 | |
| 17 | /// Mode structure with controller-specific information. |
| 18 | pub use uefi_raw::protocol::ata::AtaPassThruMode; |
| 19 | |
| 20 | /// The ATA Pass Thru Protocol. |
| 21 | /// |
| 22 | /// One protocol instance represents one ATA controller connected to the machine. |
| 23 | /// |
| 24 | /// This API offers a safe and convenient, yet still low-level interface to ATA devices. |
| 25 | /// It is designed as a foundational layer, leaving higher-level abstractions responsible for implementing |
| 26 | /// richer storage semantics, device-specific commands, and advanced use cases. |
| 27 | /// |
| 28 | /// # UEFI Specification |
| 29 | /// Provides services that allow ATA commands to be sent to ATA Devices attached to an ATA controller. Packet- |
| 30 | /// based commands would be sent to ATAPI devices only through the Extended SCSI Pass Thru Protocol. While |
| 31 | /// the ATA_PASS_THRU interface would expose an interface to the underlying ATA devices on an ATA controller, |
| 32 | /// EXT_SCSI_PASS_THRU is responsible for exposing a packet-based command interface for the ATAPI devices on |
| 33 | /// the same ATA controller. |
| 34 | #[derive(Debug)] |
| 35 | #[repr(transparent)] |
| 36 | #[unsafe_protocol(AtaPassThruProtocol::GUID)] |
| 37 | pub struct AtaPassThru(UnsafeCell<AtaPassThruProtocol>); |
| 38 | |
| 39 | impl AtaPassThru { |
| 40 | /// Retrieves the mode structure for the Extended SCSI Pass Thru protocol. |
| 41 | /// |
| 42 | /// # Returns |
| 43 | /// The [`AtaPassThruMode`] structure containing configuration details of the protocol. |
| 44 | #[must_use] |
| 45 | pub fn mode(&self) -> AtaPassThruMode { |
| 46 | // SAFETY: The memory is valid. |
| 47 | let mut mode = unsafe { (*(*self.0.get()).mode).clone() }; |
| 48 | mode.io_align = mode.io_align.max(1); // 0 and 1 is the same, says UEFI spec |
| 49 | mode |
| 50 | } |
| 51 | |
| 52 | /// Retrieves the I/O buffer alignment required by this SCSI channel. |
| 53 | /// |
| 54 | /// # Returns |
| 55 | /// - A `u32` value representing the required I/O alignment in bytes. |
| 56 | #[must_use] |
| 57 | pub fn io_align(&self) -> u32 { |
| 58 | self.mode().io_align |
| 59 | } |
| 60 | |
| 61 | /// Allocates an I/O buffer with the necessary alignment for this ATA Controller. |
| 62 | /// |
| 63 | /// You can alternatively do this yourself using the [`AlignedBuffer`] helper directly. |
| 64 | /// The `ata` api will validate that your buffers have the correct alignment and error |
| 65 | /// if they don't. |
| 66 | /// |
| 67 | /// # Arguments |
| 68 | /// - `len`: The size (in bytes) of the buffer to allocate. |
| 69 | /// |
| 70 | /// # Returns |
| 71 | /// [`AlignedBuffer`] containing the allocated memory. |
| 72 | /// |
| 73 | /// # Errors |
| 74 | /// This method can fail due to alignment or memory allocation issues. |
| 75 | pub fn alloc_io_buffer(&self, len: usize) -> Result<AlignedBuffer, LayoutError> { |
| 76 | AlignedBuffer::from_size_align(len, self.io_align() as usize) |
| 77 | } |
| 78 | |
| 79 | /// Iterate over all potential ATA devices on this channel. |
| 80 | /// |
| 81 | /// # Warnings |
| 82 | /// Depending on the UEFI implementation, this does not only return all actually available devices. |
| 83 | /// Most implementations instead return a list of all possible fully-qualified device addresses. |
| 84 | /// You have to probe for availability yourself, using [`AtaDevice::execute_command`]. |
| 85 | /// |
| 86 | /// # Returns |
| 87 | /// [`AtaDeviceIterator`] to iterate through connected ATA devices. |
| 88 | #[must_use] |
| 89 | pub const fn iter_devices(&self) -> AtaDeviceIterator<'_> { |
| 90 | AtaDeviceIterator { |
| 91 | proto: &self.0, |
| 92 | end_of_port: true, |
| 93 | prev_port: 0xFFFF, |
| 94 | prev_pmp: 0xFFFF, |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Represents an ATA device on a controller. |
| 100 | /// |
| 101 | /// # Warnings |
| 102 | /// This is only a potentially valid device address. Verify it by probing for an actually |
| 103 | /// available / connected device using [`AtaDevice::execute_command`] before doing anything meaningful. |
| 104 | #[derive(Debug)] |
| 105 | pub struct AtaDevice<'a> { |
| 106 | proto: &'a UnsafeCell<AtaPassThruProtocol>, |
| 107 | port: u16, |
| 108 | pmp: u16, |
| 109 | } |
| 110 | |
| 111 | impl AtaDevice<'_> { |
| 112 | /// Returns the port number of the device. |
| 113 | /// |
| 114 | /// # Details |
| 115 | /// - For SATA: This is the port number on the motherboard or controller. |
| 116 | /// - For IDE: This is `0` for the primary bus and `1` for the secondary bus. |
| 117 | #[must_use] |
| 118 | pub const fn port(&self) -> u16 { |
| 119 | self.port |
| 120 | } |
| 121 | |
| 122 | /// Returns the port multiplier port (PMP) number for the device. |
| 123 | /// |
| 124 | /// # Details |
| 125 | /// - For SATA: `0xFFFF` indicates a direct connection to the port, while other values |
| 126 | /// indicate the port number on a port-multiplier device. |
| 127 | /// - For IDE: `0` represents the master device, and `1` represents the slave device. |
| 128 | #[must_use] |
| 129 | pub const fn port_multiplier_port(&self) -> u16 { |
| 130 | self.pmp |
| 131 | } |
| 132 | |
| 133 | /// Resets the ATA device. |
| 134 | /// |
| 135 | /// This method attempts to reset the specified ATA device, restoring it to its default state. |
| 136 | /// |
| 137 | /// # Errors |
| 138 | /// - [`Status::UNSUPPORTED`] The ATA controller does not support a device reset operation. |
| 139 | /// - [`Status::INVALID_PARAMETER`] The `Port` or `PortMultiplierPort` values are invalid. |
| 140 | /// - [`Status::DEVICE_ERROR`] A device error occurred while attempting to reset the specified ATA device. |
| 141 | /// - [`Status::TIMEOUT`] A timeout occurred while attempting to reset the specified ATA device. |
| 142 | pub fn reset(&mut self) -> crate::Result<()> { |
| 143 | // SAFETY: The memory is valid. |
| 144 | unsafe { |
| 145 | ((*self.proto.get()).reset_device)(self.proto.get(), self.port, self.pmp).to_result() |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// Get the final device path node for this device. |
| 150 | /// |
| 151 | /// For a full [`crate::proto::device_path::DevicePath`] pointing to this device, this needs to be appended to |
| 152 | /// the controller's device path. |
| 153 | pub fn path_node(&self) -> crate::Result<PoolDevicePathNode> { |
| 154 | // SAFETY: The memory is valid. |
| 155 | unsafe { |
| 156 | let mut path_ptr: *mut DevicePathProtocol = ptr::null_mut(); |
| 157 | ((*self.proto.get()).build_device_path)( |
| 158 | self.proto.get(), |
| 159 | self.port, |
| 160 | self.pmp, |
| 161 | &mut path_ptr, |
| 162 | ) |
| 163 | .to_result()?; |
| 164 | NonNull::new(path_ptr) |
| 165 | .map(|p| PoolDevicePathNode(PoolAllocation::new(p.cast()))) |
| 166 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// Executes a command on the device. |
| 171 | /// |
| 172 | /// # Arguments |
| 173 | /// - `req`: The request structure containing details about the command to execute. |
| 174 | /// |
| 175 | /// # Returns |
| 176 | /// [`AtaResponse`] containing the results of the operation, such as data and status. |
| 177 | /// |
| 178 | /// # Errors |
| 179 | /// - [`Status::BAD_BUFFER_SIZE`] The ATA command was not executed because the buffer size exceeded the allowed transfer size. |
| 180 | /// The number of bytes that could be transferred is returned in `InTransferLength` or `OutTransferLength`. |
| 181 | /// - [`Status::NOT_READY`] The ATA command could not be sent because too many commands are already queued. Retry the operation later. |
| 182 | /// - [`Status::DEVICE_ERROR`] A device error occurred while attempting to send the ATA command. Refer to `Asb` for additional status details. |
| 183 | /// - [`Status::INVALID_PARAMETER`] The `Port`, `PortMultiplierPort`, or the contents of `Acb` are invalid. |
| 184 | /// The command was not sent, and no additional status information is available. |
| 185 | /// - [`Status::UNSUPPORTED`] The host adapter does not support the command described by the ATA command. |
| 186 | /// The command was not sent, and no additional status information is available. |
| 187 | /// - [`Status::TIMEOUT`] A timeout occurred while waiting for the ATA command to execute. Refer to `Asb` for additional status details. |
| 188 | #[expect(clippy::result_large_err)] |
| 189 | pub fn execute_command<'req>( |
| 190 | &mut self, |
| 191 | mut req: AtaRequest<'req>, |
| 192 | ) -> crate::Result<AtaResponse<'req>, AtaResponse<'req>> { |
| 193 | req.packet.acb = &req.acb; |
| 194 | // SAFETY: The memory is valid. |
| 195 | let result = unsafe { |
| 196 | ((*self.proto.get()).pass_thru)( |
| 197 | self.proto.get(), |
| 198 | self.port, |
| 199 | self.pmp, |
| 200 | &mut req.packet, |
| 201 | ptr::null_mut(), |
| 202 | ) |
| 203 | .to_result() |
| 204 | }; |
| 205 | match result { |
| 206 | Ok(_) => Ok(AtaResponse { req }), |
| 207 | Err(s) => Err(crate::Error::new(s.status(), AtaResponse { req })), |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// An iterator over the drives connected to an ATA controller. |
| 213 | /// |
| 214 | /// The iterator yields [`AtaDevice`] instances, each representing one *potential* |
| 215 | /// drive connected to the ATA controller. You have to probe whether the drive |
| 216 | /// is actually available and connected! |
| 217 | #[derive(Debug)] |
| 218 | pub struct AtaDeviceIterator<'a> { |
| 219 | proto: &'a UnsafeCell<AtaPassThruProtocol>, |
| 220 | // when there are no more devices on this port -> get next port |
| 221 | end_of_port: bool, |
| 222 | prev_port: u16, |
| 223 | prev_pmp: u16, |
| 224 | } |
| 225 | |
| 226 | impl<'a> Iterator for AtaDeviceIterator<'a> { |
| 227 | type Item = AtaDevice<'a>; |
| 228 | |
| 229 | fn next(&mut self) -> Option<Self::Item> { |
| 230 | loop { |
| 231 | if self.end_of_port { |
| 232 | // SAFETY: The memory is valid. |
| 233 | let result = unsafe { |
| 234 | ((*self.proto.get()).get_next_port)(self.proto.get(), &mut self.prev_port) |
| 235 | }; |
| 236 | match result { |
| 237 | Status::SUCCESS => self.end_of_port = false, |
| 238 | Status::NOT_FOUND => return None, // no more ports / devices. End of list |
| 239 | _ => panic!("Must not happen according to spec!"), |
| 240 | } |
| 241 | } |
| 242 | // get next device on port |
| 243 | // The UEFI spec states, that: |
| 244 | // If there is no port multiplier detected on the given port, the initial query of get_next_device() |
| 245 | // is allowed to return either of: |
| 246 | // - EFI_SUCCESS & PMP = 0xFFFF |
| 247 | // - EFI_NOT_FOUND |
| 248 | // But even when there is no detected port multiplier, there might be a device directly connected |
| 249 | // to the port! A port where the device is directly connected uses a pmp-value of 0xFFFF. |
| 250 | let was_first = self.prev_pmp == 0xFFFF; |
| 251 | // SAFETY: The memory is valid. |
| 252 | let result = unsafe { |
| 253 | ((*self.proto.get()).get_next_device)( |
| 254 | self.proto.get(), |
| 255 | self.prev_port, |
| 256 | &mut self.prev_pmp, |
| 257 | ) |
| 258 | }; |
| 259 | match result { |
| 260 | Status::SUCCESS => { |
| 261 | if self.prev_pmp == 0xFFFF { |
| 262 | self.end_of_port = true; |
| 263 | } |
| 264 | return Some(AtaDevice { |
| 265 | proto: self.proto, |
| 266 | port: self.prev_port, |
| 267 | pmp: self.prev_pmp, |
| 268 | }); |
| 269 | } |
| 270 | Status::NOT_FOUND => { |
| 271 | self.end_of_port = true; |
| 272 | self.prev_pmp = 0xFFFF; |
| 273 | if was_first { |
| 274 | // no port multiplier on port, return valid device anyway. |
| 275 | return Some(AtaDevice { |
| 276 | proto: self.proto, |
| 277 | port: self.prev_port, |
| 278 | pmp: 0xFFFF, |
| 279 | }); |
| 280 | } |
| 281 | } |
| 282 | _ => panic!("Must not happen according to spec!"), |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |