Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/uefi/src/proto/ata/mod.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / uefi / src / proto / ata / mod.rs` 383 lines ยท 14356 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! ATA Protocols. 4 5 use crate::mem::{AlignedBuffer, AlignmentError}; 6 use crate::util::usize_from_u32; 7 use core::alloc::LayoutError; 8 use core::marker::PhantomData; 9 use core::time::Duration; 10 use core::{ptr, slice}; 11 use uefi_raw::protocol::ata::{ 12 AtaCommandBlock, AtaPassThruCommandPacket, AtaPassThruLength, AtaStatusBlock, 13 }; 14 15 pub mod pass_thru; 16 17 /// Represents the protocol for ATA Pass Thru command handling. 18 /// 19 /// This type defines the protocols supported for passing ATA commands through to an 20 /// ATA compliant controller. Over time, multiple possible transports for ATA commands 21 /// have evolved. The UEFI spec generically abstracts all of these transports below 22 /// this one protocol, so old PATA drives and controllers, as well as modern AHCI-only 23 /// SATA controllers are supported with the same set of APIs. 24 /// see: 25 pub use uefi_raw::protocol::ata::AtaPassThruCommandProtocol; 26 27 /// Represents an ATA request built for execution on an ATA controller. 28 #[derive(Debug)] 29 pub struct AtaRequest<'a> { 30 io_align: u32, 31 acb: AtaCommandBlock, 32 packet: AtaPassThruCommandPacket, 33 /// Capacity of the buffer the firmware was given. The firmware reports 34 /// how much it transferred in the packet, but a bogus value must not 35 /// produce an out-of-bounds slice. 36 in_data_capacity: usize, 37 in_data_buffer: Option, 38 out_data_buffer: Option, 39 asb: AlignedBuffer, 40 _phantom: PhantomData<&'a u8>, 41 } 42 43 /// Builder for creating and configuring an [`AtaRequest`]. 44 /// 45 /// This builder simplifies the creation of an [`AtaRequest`] by providing chainable methods for 46 /// configuring fields like timeout, buffers, and ATA command details. 47 #[derive(Debug)] 48 pub struct AtaRequestBuilder<'a> { 49 req: AtaRequest<'a>, 50 } 51 52 impl<'a> AtaRequestBuilder<'a> { 53 /// Creates a new [`AtaRequestBuilder`] with the specified alignment, command, and protocol. 54 /// 55 /// # Arguments 56 /// - `io_align`: The I/O buffer alignment required for the ATA controller. 57 /// - `command`: The ATA command byte specifying the operation to execute. 58 /// - `protocol`: The protocol type for the command (e.g., DMA, UDMA, etc.). 59 /// 60 /// # Returns 61 /// `Result` indicating success or memory allocation failure. 62 /// 63 /// # Errors 64 /// This method can fail due to alignment or memory allocation issues. 65 fn new( 66 io_align: u32, 67 command: u8, 68 protocol: AtaPassThruCommandProtocol, 69 ) -> Result { 70 // status block has alignment requirements! 71 let mut asb = 72 AlignedBuffer::from_size_align(size_of::(), usize_from_u32(io_align))?; 73 Ok(Self { 74 req: AtaRequest { 75 io_align, 76 acb: AtaCommandBlock { 77 command, 78 ..Default::default() 79 }, 80 packet: AtaPassThruCommandPacket { 81 asb: asb.ptr_mut().cast(), 82 acb: ptr::null(), // filled during execution 83 timeout: 0, 84 in_data_buffer: ptr::null_mut(), 85 out_data_buffer: ptr::null(), 86 in_transfer_length: 0, 87 out_transfer_length: 0, 88 protocol, 89 length: AtaPassThruLength::BYTES, 90 }, 91 in_data_capacity: 0, 92 in_data_buffer: None, 93 out_data_buffer: None, 94 asb, 95 _phantom: PhantomData, 96 }, 97 }) 98 } 99 100 // # PIO 101 // ######################################################################## 102 103 /// Creates a builder for a PIO write operation. 104 /// 105 /// Since the ATA specification mandates the support for PIO mode for all 106 /// compliant drives and controllers, this is the protocol variant with the 107 /// highest compatibility in the field. 108 /// So probing, (sending ATA IDENTIFY commands to device ports to find out 109 /// whether there is actually a device connected to it) should probably be 110 /// done using this method most of the time. 111 /// If this errors with Status "UNSUPPORTED", try UDMA next. 112 /// 113 /// # Arguments 114 /// - `io_align`: The I/O buffer alignment required for the ATA controller. 115 /// - `command`: The ATA command byte specifying the write operation. 116 /// 117 /// # Returns 118 /// `Result` indicating success or memory allocation failure. 119 /// 120 /// # Errors 121 /// This method can fail due to alignment or memory allocation issues. 122 pub fn read_pio(io_align: u32, command: u8) -> Result { 123 Self::new(io_align, command, AtaPassThruCommandProtocol::PIO_DATA_IN) 124 } 125 126 // # UDMA 127 // ######################################################################## 128 129 /// Creates a builder for a UDMA read operation. 130 /// 131 /// # Arguments 132 /// - `io_align`: The I/O buffer alignment required for the ATA controller. 133 /// - `command`: The ATA command byte specifying the read operation. 134 /// 135 /// # Returns 136 /// `Result` indicating success or memory allocation failure. 137 /// 138 /// # Errors 139 /// This method can fail due to alignment or memory allocation issues. 140 pub fn read_udma(io_align: u32, command: u8) -> Result { 141 Self::new(io_align, command, AtaPassThruCommandProtocol::UDMA_DATA_IN) 142 } 143 144 /// Creates a builder for a UDMA write operation. 145 /// 146 /// # Arguments 147 /// - `io_align`: The I/O buffer alignment required for the ATA controller. 148 /// - `command`: The ATA command byte specifying the write operation. 149 /// 150 /// # Returns 151 /// `Result` indicating success or memory allocation failure. 152 /// 153 /// # Errors 154 /// This method can fail due to alignment or memory allocation issues. 155 pub fn write_udma(io_align: u32, command: u8) -> Result { 156 Self::new(io_align, command, AtaPassThruCommandProtocol::UDMA_DATA_OUT) 157 } 158 159 // ######################################################################## 160 161 /// Configure the given timeout for this request. 162 #[must_use] 163 pub const fn with_timeout(mut self, timeout: Duration) -> Self { 164 self.req.packet.timeout = (timeout.as_nanos() / 100) as u64; 165 self 166 } 167 168 /// Configure the `features` field. 169 /// FEATURES (7:0) 170 #[must_use] 171 pub const fn with_features(mut self, features: u8) -> Self { 172 self.req.acb.features = features; 173 self 174 } 175 176 /// Configure the `sector_number` field. 177 /// LBA (7:0) 178 #[must_use] 179 pub const fn with_sector_number(mut self, sector_number: u8) -> Self { 180 self.req.acb.sector_number = sector_number; 181 self 182 } 183 184 /// Configure the `cylinder` fields (low and high combined). 185 /// low: LBA (15:8) 186 /// high: LBA (23:16) 187 #[must_use] 188 pub const fn with_cylinder(mut self, low: u8, high: u8) -> Self { 189 self.req.acb.cylinder_low = low; 190 self.req.acb.cylinder_high = high; 191 self 192 } 193 194 /// Configure the `device_head` field. 195 /// DEVICE 196 #[must_use] 197 pub const fn with_device_head(mut self, device_head: u8) -> Self { 198 self.req.acb.device_head = device_head; 199 self 200 } 201 202 /// Configure the `sector_number_exp` field. 203 /// LBA (31:24) 204 #[must_use] 205 pub const fn with_sector_number_exp(mut self, sector_number_exp: u8) -> Self { 206 self.req.acb.sector_number_exp = sector_number_exp; 207 self 208 } 209 210 /// Configure the `cylinder_exp` fields (low and high combined). 211 /// low_exp: LBA (39:32) 212 /// high_exp: LBA (47:40) 213 #[must_use] 214 pub const fn with_cylinder_exp(mut self, low_exp: u8, high_exp: u8) -> Self { 215 self.req.acb.cylinder_low_exp = low_exp; 216 self.req.acb.cylinder_high_exp = high_exp; 217 self 218 } 219 220 /// Configure the `features_exp` field. 221 /// FEATURES (15:8) 222 #[must_use] 223 pub const fn with_features_exp(mut self, features_exp: u8) -> Self { 224 self.req.acb.features_exp = features_exp; 225 self 226 } 227 228 /// Configure the `sector_count` field. 229 /// COUNT (7:0) 230 #[must_use] 231 pub const fn with_sector_count(mut self, sector_count: u8) -> Self { 232 self.req.acb.sector_count = sector_count; 233 self 234 } 235 236 /// Configure the `sector_count_exp` field. 237 /// COUNT (15:8) 238 #[must_use] 239 pub const fn with_sector_count_exp(mut self, sector_count_exp: u8) -> Self { 240 self.req.acb.sector_count_exp = sector_count_exp; 241 self 242 } 243 244 // # READ BUFFER 245 // ######################################################################################## 246 247 /// Uses a user-supplied buffer for reading data from the device. 248 /// 249 /// # Arguments 250 /// - `bfr`: A mutable reference to an [`AlignedBuffer`] that will be used to store data read from the device. 251 /// 252 /// # Returns 253 /// `Result` indicating success or an alignment issue with the provided buffer. 254 /// 255 /// # Description 256 /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to 257 /// the `in_data_buffer` of the underlying [`AtaRequest`]. 258 pub fn use_read_buffer(mut self, bfr: &'a mut AlignedBuffer) -> Result { 259 // check alignment of externally supplied buffer 260 bfr.check_alignment(self.req.io_align as usize)?; 261 self.req.in_data_buffer = None; 262 self.req.in_data_capacity = bfr.size(); 263 self.req.packet.in_data_buffer = bfr.ptr_mut().cast(); 264 self.req.packet.in_transfer_length = bfr.size() as u32; 265 Ok(self) 266 } 267 268 /// Adds a newly allocated read buffer to the built ATA request. 269 /// 270 /// # Arguments 271 /// - `len`: The size of the buffer (in bytes) to allocate for receiving data. 272 /// 273 /// # Returns 274 /// `Result` indicating success or a memory allocation error. 275 pub fn with_read_buffer(mut self, len: usize) -> Result { 276 let mut bfr = AlignedBuffer::from_size_align(len, self.req.io_align as usize)?; 277 self.req.in_data_capacity = bfr.size(); 278 self.req.packet.in_data_buffer = bfr.ptr_mut().cast(); 279 self.req.packet.in_transfer_length = bfr.size() as u32; 280 self.req.in_data_buffer = Some(bfr); 281 Ok(self) 282 } 283 284 // # WRITE BUFFER 285 // ######################################################################################## 286 287 /// Uses a user-supplied buffer for writing data to the device. 288 /// 289 /// # Arguments 290 /// - `bfr`: A mutable reference to an [`AlignedBuffer`] containing the data to be written to the device. 291 /// 292 /// # Returns 293 /// `Result` indicating success or an alignment issue with the provided buffer. 294 /// 295 /// # Description 296 /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to 297 /// the `out_data_buffer` of the underlying [`AtaRequest`]. 298 pub fn use_write_buffer(mut self, bfr: &'a mut AlignedBuffer) -> Result { 299 // check alignment of externally supplied buffer 300 bfr.check_alignment(self.req.io_align as usize)?; 301 self.req.out_data_buffer = None; 302 self.req.packet.out_data_buffer = bfr.ptr_mut().cast(); 303 self.req.packet.out_transfer_length = bfr.size() as u32; 304 Ok(self) 305 } 306 307 /// Adds a newly allocated write buffer to the built ATA request that is filled from the 308 /// given data buffer. (Done for memory alignment and lifetime purposes) 309 /// 310 /// # Arguments 311 /// - `data`: A slice of bytes representing the data to be written. 312 /// 313 /// # Returns 314 /// `Result` indicating success or a memory allocation error. 315 pub fn with_write_data(mut self, data: &[u8]) -> Result { 316 let mut bfr = AlignedBuffer::from_size_align(data.len(), self.req.io_align as usize)?; 317 bfr.copy_from_slice(data); 318 self.req.packet.out_data_buffer = bfr.ptr_mut().cast(); 319 self.req.packet.out_transfer_length = bfr.size() as u32; 320 self.req.out_data_buffer = Some(bfr); 321 Ok(self) 322 } 323 324 /// Build the final [`AtaRequest`]. 325 /// 326 /// # Returns 327 /// A fully-configured [`AtaRequest`] ready for execution. 328 #[must_use] 329 pub fn build(self) -> AtaRequest<'a> { 330 self.req 331 } 332 } 333 334 /// Represents a response from an ATA request. 335 /// 336 /// This structure provides access to the status block, read buffer, and other 337 /// details returned by the ATA controller after executing a request. 338 #[derive(Debug)] 339 pub struct AtaResponse<'a> { 340 req: AtaRequest<'a>, 341 } 342 343 impl AtaResponse<'_> { 344 /// Retrieves the status block from the response. 345 /// 346 /// # Returns 347 /// A reference to the [`AtaStatusBlock`] containing details about the status of the executed operation. 348 #[must_use] 349 pub const fn status(&self) -> &AtaStatusBlock { 350 // SAFETY: The memory is valid. 351 unsafe { 352 self.req 353 .asb 354 .ptr() 355 .cast::() 356 .as_ref() 357 .unwrap() 358 } 359 } 360 361 /// Retrieves the buffer containing data read from the device (if available). 362 /// 363 /// # Returns 364 /// `Option<&[u8]>`: A slice of the data read from the device, or `None` if no read buffer was used. 365 #[must_use] 366 pub const fn read_buffer(&self) -> Option<&[u8]> { 367 if self.req.packet.in_data_buffer.is_null() { 368 return None; 369 } 370 // The firmware reports the transferred length; clamp it so that a 371 // bogus value cannot produce an out-of-bounds slice. 372 let reported = self.req.packet.in_transfer_length as usize; 373 let cap = self.req.in_data_capacity; 374 let len = if reported < cap { reported } else { cap }; 375 // SAFETY: The buffer holds at least `len` initialized bytes. 376 unsafe { 377 Some(slice::from_raw_parts( 378 self.req.packet.in_data_buffer.cast(), 379 len, 380 )) 381 } 382 } 383 }