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 / media / block.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Block I/O protocols [`BlockIO`] and [`BlockIO2`]. |
| 4 | |
| 5 | use core::ptr::NonNull; |
| 6 | |
| 7 | use crate::proto::unsafe_protocol; |
| 8 | use crate::util::opt_nonnull_to_ptr; |
| 9 | use crate::{Event, Result, Status, StatusExt}; |
| 10 | use uefi_raw::protocol::block::BlockIoMedia; |
| 11 | |
| 12 | pub use uefi_raw::protocol::block::{BlockIo2Protocol, BlockIoProtocol, Lba}; |
| 13 | |
| 14 | /// Block I/O [`Protocol`]. |
| 15 | /// |
| 16 | /// [`Protocol`]: uefi::proto::Protocol |
| 17 | #[derive(Debug)] |
| 18 | #[repr(transparent)] |
| 19 | #[unsafe_protocol(BlockIoProtocol::GUID)] |
| 20 | pub struct BlockIO(BlockIoProtocol); |
| 21 | |
| 22 | impl BlockIO { |
| 23 | /// Pointer for block IO media. |
| 24 | #[must_use] |
| 25 | pub const fn media(&self) -> &BlockIOMedia { |
| 26 | // SAFETY: The memory is valid. |
| 27 | unsafe { &*self.0.media.cast::<BlockIOMedia>() } |
| 28 | } |
| 29 | |
| 30 | /// Returns the revision of this protocol. |
| 31 | #[must_use] |
| 32 | pub const fn revision(&self) -> u64 { |
| 33 | self.0.revision |
| 34 | } |
| 35 | |
| 36 | /// Returns the first LBA that is aligned to a physical block boundary. |
| 37 | /// |
| 38 | /// Returns `None` for protocol revisions below 2, where this media |
| 39 | /// field is not present. |
| 40 | #[must_use] |
| 41 | pub const fn lowest_aligned_lba(&self) -> Option<Lba> { |
| 42 | if self.0.revision >= BlockIoProtocol::REVISION_2 { |
| 43 | Some(self.media().0.lowest_aligned_lba) |
| 44 | } else { |
| 45 | None |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Returns the number of logical blocks per physical block. |
| 50 | /// |
| 51 | /// Returns `None` for protocol revisions below 2, where this media |
| 52 | /// field is not present. |
| 53 | #[must_use] |
| 54 | pub const fn logical_blocks_per_physical_block(&self) -> Option<u32> { |
| 55 | if self.0.revision >= BlockIoProtocol::REVISION_2 { |
| 56 | Some(self.media().0.logical_blocks_per_physical_block) |
| 57 | } else { |
| 58 | None |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Returns the optimal transfer length granularity as a number of |
| 63 | /// logical blocks. |
| 64 | /// |
| 65 | /// Returns `None` for protocol revisions below 3, where this media |
| 66 | /// field is not present. |
| 67 | #[must_use] |
| 68 | pub const fn optimal_transfer_length_granularity(&self) -> Option<u32> { |
| 69 | if self.0.revision >= BlockIoProtocol::REVISION_3 { |
| 70 | Some(self.media().0.optimal_transfer_length_granularity) |
| 71 | } else { |
| 72 | None |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Resets the block device hardware. |
| 77 | /// |
| 78 | /// # Arguments |
| 79 | /// * `extended_verification` Indicates that the driver may perform a more |
| 80 | /// exhaustive verification operation of the device during reset. |
| 81 | /// |
| 82 | /// # Errors |
| 83 | /// * `Status::DEVICE_ERROR` The block device is not functioning |
| 84 | /// correctly and could not be reset. |
| 85 | pub fn reset(&mut self, extended_verification: bool) -> Result { |
| 86 | // SAFETY: The memory is valid. |
| 87 | unsafe { (self.0.reset)(&mut self.0, extended_verification.into()) }.to_result() |
| 88 | } |
| 89 | |
| 90 | /// Read the requested number of blocks from the device. |
| 91 | /// |
| 92 | /// # Arguments |
| 93 | /// * `media_id` - The media ID that the read request is for. |
| 94 | /// * `lba` - The starting logical block address to read from on the device. |
| 95 | /// * `buffer` - The target buffer of the read operation |
| 96 | /// |
| 97 | /// # Errors |
| 98 | /// * `Status::DEVICE_ERROR` The device reported an error while attempting to perform the read |
| 99 | /// operation. |
| 100 | /// * `Status::NO_MEDIA` There is no media in the device. |
| 101 | /// * `Status::MEDIA_CHANGED` The `media_id` is not for the current media. |
| 102 | /// * `Status::BAD_BUFFER_SIZE` The buffer size parameter is not a multiple of the intrinsic block size of |
| 103 | /// the device. |
| 104 | /// * `Status::INVALID_PARAMETER` The read request contains LBAs that are not valid, or the buffer is not on |
| 105 | /// proper alignment. |
| 106 | pub fn read_blocks(&mut self, media_id: u32, lba: Lba, buffer: &mut [u8]) -> Result { |
| 107 | let buffer_size = buffer.len(); |
| 108 | // SAFETY: The memory is valid. |
| 109 | unsafe { |
| 110 | (self.0.read_blocks)( |
| 111 | &self.0, |
| 112 | media_id, |
| 113 | lba, |
| 114 | buffer_size, |
| 115 | buffer.as_mut_ptr().cast(), |
| 116 | ) |
| 117 | } |
| 118 | .to_result() |
| 119 | } |
| 120 | |
| 121 | /// Writes the requested number of blocks to the device. |
| 122 | /// |
| 123 | /// # Arguments |
| 124 | /// * `media_id` The media ID that the write request is for. |
| 125 | /// * `lba` The starting logical block address to be written. |
| 126 | /// * `buffer` Buffer to be written |
| 127 | /// |
| 128 | /// # Errors |
| 129 | /// * `Status::WRITE_PROTECTED` The device cannot be written to. |
| 130 | /// * `Status::NO_MEDIA` There is no media in the device. |
| 131 | /// * `Status::MEDIA_CHANGED` The `media_id` is not for the current media. |
| 132 | /// * `Status::DEVICE_ERROR` The device reported an error while attempting to perform the write |
| 133 | /// operation. |
| 134 | /// * `Status::BAD_BUFFER_SIZE` The buffer size parameter is not a multiple of the intrinsic block size |
| 135 | /// of the device. |
| 136 | /// * `Status::INVALID_PARAMETER` The write request contains LBAs that are not valid, or the buffer is not |
| 137 | /// on proper alignment. |
| 138 | pub fn write_blocks(&mut self, media_id: u32, lba: Lba, buffer: &[u8]) -> Result { |
| 139 | let buffer_size = buffer.len(); |
| 140 | // SAFETY: The memory is valid. |
| 141 | unsafe { |
| 142 | (self.0.write_blocks)( |
| 143 | &mut self.0, |
| 144 | media_id, |
| 145 | lba, |
| 146 | buffer_size, |
| 147 | buffer.as_ptr().cast(), |
| 148 | ) |
| 149 | } |
| 150 | .to_result() |
| 151 | } |
| 152 | |
| 153 | /// Flushes all modified data to a physical block device. |
| 154 | /// |
| 155 | /// # Errors |
| 156 | /// * `Status::DEVICE_ERROR` The device reported an error while attempting to write data. |
| 157 | /// * `Status::NO_MEDIA` There is no media in the device. |
| 158 | pub fn flush_blocks(&mut self) -> Result { |
| 159 | // SAFETY: The memory is valid. |
| 160 | unsafe { (self.0.flush_blocks)(&mut self.0) }.to_result() |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// Media information structure |
| 165 | #[repr(transparent)] |
| 166 | #[derive(Debug)] |
| 167 | pub struct BlockIOMedia(BlockIoMedia); |
| 168 | |
| 169 | impl BlockIOMedia { |
| 170 | /// The current media ID. |
| 171 | #[must_use] |
| 172 | pub const fn media_id(&self) -> u32 { |
| 173 | self.0.media_id |
| 174 | } |
| 175 | |
| 176 | /// True if the media is removable. |
| 177 | #[must_use] |
| 178 | pub fn is_removable_media(&self) -> bool { |
| 179 | self.0.removable_media.into() |
| 180 | } |
| 181 | |
| 182 | /// True if there is a media currently present in the device. |
| 183 | #[must_use] |
| 184 | pub fn is_media_present(&self) -> bool { |
| 185 | self.0.media_present.into() |
| 186 | } |
| 187 | |
| 188 | /// True if block IO was produced to abstract partition structure. |
| 189 | #[must_use] |
| 190 | pub fn is_logical_partition(&self) -> bool { |
| 191 | self.0.logical_partition.into() |
| 192 | } |
| 193 | |
| 194 | /// True if the media is marked read-only. |
| 195 | #[must_use] |
| 196 | pub fn is_read_only(&self) -> bool { |
| 197 | self.0.read_only.into() |
| 198 | } |
| 199 | |
| 200 | /// True if `writeBlocks` function writes data. |
| 201 | #[must_use] |
| 202 | pub fn is_write_caching(&self) -> bool { |
| 203 | self.0.write_caching.into() |
| 204 | } |
| 205 | |
| 206 | /// The intrinsic block size of the device. |
| 207 | /// |
| 208 | /// If the media changes, then this field is updated. Returns the number of bytes per logical block. |
| 209 | #[must_use] |
| 210 | pub const fn block_size(&self) -> u32 { |
| 211 | self.0.block_size |
| 212 | } |
| 213 | |
| 214 | /// Supplies the alignment requirement for any buffer used in a data transfer. |
| 215 | #[must_use] |
| 216 | pub const fn io_align(&self) -> u32 { |
| 217 | self.0.io_align |
| 218 | } |
| 219 | |
| 220 | /// The last LBA on the device. If the media changes, then this field is updated. |
| 221 | #[must_use] |
| 222 | pub const fn last_block(&self) -> Lba { |
| 223 | self.0.last_block |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /// Asynchronous transaction token for Block I/O 2 operations. |
| 228 | #[repr(C)] |
| 229 | #[derive(Debug)] |
| 230 | pub struct BlockIO2Token { |
| 231 | /// Event to be signalled when an asynchronous block I/O operation |
| 232 | /// completes. |
| 233 | pub event: Option<Event>, |
| 234 | /// Transaction status code. |
| 235 | pub transaction_status: Status, |
| 236 | } |
| 237 | |
| 238 | /// Block I/O 2 [`Protocol`]. |
| 239 | /// |
| 240 | /// The Block I/O 2 protocol defines an extension to the Block I/O protocol |
| 241 | /// which enables the ability to read and write data at a block level in a |
| 242 | /// non-blocking manner. |
| 243 | /// |
| 244 | /// [`Protocol`]: uefi::proto::Protocol |
| 245 | #[derive(Debug)] |
| 246 | #[repr(transparent)] |
| 247 | #[unsafe_protocol(BlockIo2Protocol::GUID)] |
| 248 | pub struct BlockIO2(BlockIo2Protocol); |
| 249 | |
| 250 | impl BlockIO2 { |
| 251 | /// Pointer for block IO media. |
| 252 | #[must_use] |
| 253 | pub const fn media(&self) -> &BlockIOMedia { |
| 254 | // SAFETY: The memory is valid. |
| 255 | unsafe { &*self.0.media.cast::<BlockIOMedia>() } |
| 256 | } |
| 257 | |
| 258 | /// Resets the block device hardware. |
| 259 | /// |
| 260 | /// # Arguments |
| 261 | /// * `extended_verification` - Indicates that the driver may perform a more exhaustive verification operation of the device during reset. |
| 262 | /// |
| 263 | /// # Errors |
| 264 | /// * [`Status::DEVICE_ERROR`] The block device is not functioning correctly and could not be reset. |
| 265 | pub fn reset(&mut self, extended_verification: bool) -> Result { |
| 266 | // SAFETY: The memory is valid. |
| 267 | unsafe { (self.0.reset)(&mut self.0, extended_verification.into()) }.to_result() |
| 268 | } |
| 269 | |
| 270 | /// Reads the requested number of blocks from the device. |
| 271 | /// |
| 272 | /// # Arguments |
| 273 | /// * `media_id` - The media ID that the read request is for. |
| 274 | /// * `lba` - The starting logical block address to read from on the device. |
| 275 | /// * `token` - Transaction token for asynchronous read. |
| 276 | /// * `len` - Buffer size. |
| 277 | /// * `buffer` - The target buffer of the read operation |
| 278 | /// |
| 279 | /// # Safety |
| 280 | /// Because of the asynchronous nature of the block transaction, manual lifetime |
| 281 | /// tracking is required. |
| 282 | /// |
| 283 | /// # Errors |
| 284 | /// * [`Status::INVALID_PARAMETER`] The read request contains LBAs that are not valid, or the buffer is not on proper alignment. |
| 285 | /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources. |
| 286 | /// * [`Status::MEDIA_CHANGED`] The `media_id` is not for the current media. |
| 287 | /// * [`Status::NO_MEDIA`] There is no media in the device. |
| 288 | /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the read operation. |
| 289 | /// * [`Status::BAD_BUFFER_SIZE`] The buffer size parameter is not a multiple of the intrinsic block size of the device. |
| 290 | pub unsafe fn read_blocks_ex( |
| 291 | &mut self, |
| 292 | media_id: u32, |
| 293 | lba: Lba, |
| 294 | token: Option<NonNull<BlockIO2Token>>, |
| 295 | len: usize, |
| 296 | buffer: *mut u8, |
| 297 | ) -> Result { |
| 298 | let token = opt_nonnull_to_ptr(token); |
| 299 | // SAFETY: The memory is valid. |
| 300 | unsafe { (self.0.read_blocks_ex)(&self.0, media_id, lba, token.cast(), len, buffer.cast()) } |
| 301 | .to_result() |
| 302 | } |
| 303 | |
| 304 | /// Writes a specified number of blocks to the device. |
| 305 | /// |
| 306 | /// # Arguments |
| 307 | /// * `media_id` - The media ID that the write request is for. |
| 308 | /// * `lba` - The starting logical block address to be written. |
| 309 | /// * `token` - Transaction token for asynchronous write. |
| 310 | /// * `len` - Buffer size. |
| 311 | /// * `buffer` - Buffer to be written from. |
| 312 | /// |
| 313 | /// # Safety |
| 314 | /// Because of the asynchronous nature of the block transaction, manual |
| 315 | /// lifetime tracking is required. |
| 316 | /// |
| 317 | /// # Errors |
| 318 | /// * [`Status::INVALID_PARAMETER`] The write request contains LBAs that are not valid, or the buffer is not on proper alignment. |
| 319 | /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources. |
| 320 | /// * [`Status::MEDIA_CHANGED`] The `media_id` is not for the current media. |
| 321 | /// * [`Status::NO_MEDIA`] There is no media in the device. |
| 322 | /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the write operation. |
| 323 | /// * [`Status::WRITE_PROTECTED`] The device cannot be written to. |
| 324 | /// * [`Status::BAD_BUFFER_SIZE`] The buffer size parameter is not a multiple of the intrinsic block size of the device. |
| 325 | pub unsafe fn write_blocks_ex( |
| 326 | &mut self, |
| 327 | media_id: u32, |
| 328 | lba: Lba, |
| 329 | token: Option<NonNull<BlockIO2Token>>, |
| 330 | len: usize, |
| 331 | buffer: *const u8, |
| 332 | ) -> Result { |
| 333 | let token = opt_nonnull_to_ptr(token); |
| 334 | // SAFETY: The memory is valid. |
| 335 | unsafe { |
| 336 | (self.0.write_blocks_ex)(&mut self.0, media_id, lba, token.cast(), len, buffer.cast()) |
| 337 | } |
| 338 | .to_result() |
| 339 | } |
| 340 | |
| 341 | /// Flushes all modified data to the physical device. |
| 342 | /// |
| 343 | /// # Arguments |
| 344 | /// * `token` - Transaction token for asynchronous flush. |
| 345 | /// |
| 346 | /// # Errors |
| 347 | /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources. |
| 348 | /// * [`Status::MEDIA_CHANGED`] The media in the device has changed since the last access. |
| 349 | /// * [`Status::NO_MEDIA`] There is no media in the device. |
| 350 | /// * [`Status::DEVICE_ERROR`] The `media_id` is not for the current media. |
| 351 | /// * [`Status::WRITE_PROTECTED`] The device cannot be written to. |
| 352 | pub fn flush_blocks_ex(&mut self, token: Option<NonNull<BlockIO2Token>>) -> Result { |
| 353 | let token = opt_nonnull_to_ptr(token); |
| 354 | // SAFETY: The memory is valid. |
| 355 | unsafe { (self.0.flush_blocks_ex)(&mut self.0, token.cast()) }.to_result() |
| 356 | } |
| 357 | } |