|
| 1 | +//! Disk I/O protocols. |
| 2 | +
|
| 3 | +use crate::proto::Protocol; |
| 4 | +use crate::{unsafe_guid, Event, Result, Status}; |
| 5 | +use core::ptr::NonNull; |
| 6 | + |
| 7 | +/// The disk I/O protocol. |
| 8 | +/// |
| 9 | +/// This protocol is used to abstract the block accesses of the block I/O |
| 10 | +/// protocol to a more general offset-length protocol. Firmware is |
| 11 | +/// reponsible for adding this protocol to any block I/O interface that |
| 12 | +/// appears in the system that does not already have a disk I/O protocol. |
| 13 | +#[repr(C)] |
| 14 | +#[unsafe_guid("ce345171-ba0b-11d2-8e4f-00a0c969723b")] |
| 15 | +#[derive(Protocol)] |
| 16 | +pub struct DiskIo { |
| 17 | + revision: u64, |
| 18 | + read_disk: extern "efiapi" fn( |
| 19 | + this: &DiskIo, |
| 20 | + media_id: u32, |
| 21 | + offset: u64, |
| 22 | + len: usize, |
| 23 | + buffer: *mut u8, |
| 24 | + ) -> Status, |
| 25 | + write_disk: extern "efiapi" fn( |
| 26 | + this: &mut DiskIo, |
| 27 | + media_id: u32, |
| 28 | + offset: u64, |
| 29 | + len: usize, |
| 30 | + buffer: *const u8, |
| 31 | + ) -> Status, |
| 32 | +} |
| 33 | + |
| 34 | +impl DiskIo { |
| 35 | + /// Reads bytes from the disk device. |
| 36 | + /// |
| 37 | + /// # Arguments: |
| 38 | + /// * `media_id` - ID of the medium to be read. |
| 39 | + /// * `offset` - Starting byte offset on the logical block I/O device to read from. |
| 40 | + /// * `buffer` - Pointer to a buffer to read into. |
| 41 | + /// |
| 42 | + /// # Errors: |
| 43 | + /// * `uefi::status::INVALID_PARAMETER` The read request contains device addresses that |
| 44 | + /// are not valid for the device. |
| 45 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 46 | + /// the read operation. |
| 47 | + /// * `uefi::status::NO_MEDIA` There is no medium in the device. |
| 48 | + /// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium. |
| 49 | + pub fn read_disk(&self, media_id: u32, offset: u64, buffer: &mut [u8]) -> Result { |
| 50 | + (self.read_disk)(self, media_id, offset, buffer.len(), buffer.as_mut_ptr()).into() |
| 51 | + } |
| 52 | + |
| 53 | + /// Writes bytes to the disk device. |
| 54 | + /// |
| 55 | + /// # Arguments: |
| 56 | + /// * `media_id` - ID of the medium to be written. |
| 57 | + /// * `offset` - Starting byte offset on the logical block I/O device to write to. |
| 58 | + /// * `buffer` - Pointer to a buffer to write from. |
| 59 | + /// |
| 60 | + /// # Errors: |
| 61 | + /// * `uefi::status::INVALID_PARAMETER` The write request contains device addresses that |
| 62 | + /// are not valid for the device. |
| 63 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 64 | + /// the write operation. |
| 65 | + /// * `uefi::status::NO_MEDIA` There is no medium in the device. |
| 66 | + /// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium. |
| 67 | + /// * `uefi::status::WRITE_PROTECTED` The device cannot be written to. |
| 68 | + pub fn write_disk(&mut self, media_id: u32, offset: u64, buffer: &[u8]) -> Result { |
| 69 | + (self.write_disk)(self, media_id, offset, buffer.len(), buffer.as_ptr()).into() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Asynchronous transaction token for disk I/O 2 operations. |
| 74 | +#[repr(C)] |
| 75 | +pub struct DiskIo2Token { |
| 76 | + /// Event to be signalled when an asynchronous disk I/O operation completes. |
| 77 | + pub event: Event, |
| 78 | + /// Transaction status code. |
| 79 | + pub transaction_status: Status, |
| 80 | +} |
| 81 | + |
| 82 | +/// The disk I/O 2 protocol. |
| 83 | +/// |
| 84 | +/// This protocol provides an extension to the disk I/O protocol to enable |
| 85 | +/// non-blocking / asynchronous byte-oriented disk operation. |
| 86 | +#[repr(C)] |
| 87 | +#[unsafe_guid("151c8eae-7f2c-472c-9e54-9828194f6a88")] |
| 88 | +#[derive(Protocol)] |
| 89 | +pub struct DiskIo2 { |
| 90 | + revision: u64, |
| 91 | + cancel: extern "efiapi" fn(this: &mut DiskIo2) -> Status, |
| 92 | + read_disk_ex: extern "efiapi" fn( |
| 93 | + this: &DiskIo2, |
| 94 | + media_id: u32, |
| 95 | + offset: u64, |
| 96 | + token: Option<NonNull<DiskIo2Token>>, |
| 97 | + len: usize, |
| 98 | + buffer: *mut u8, |
| 99 | + ) -> Status, |
| 100 | + write_disk_ex: extern "efiapi" fn( |
| 101 | + this: &mut DiskIo2, |
| 102 | + media_id: u32, |
| 103 | + offset: u64, |
| 104 | + token: Option<NonNull<DiskIo2Token>>, |
| 105 | + len: usize, |
| 106 | + buffer: *const u8, |
| 107 | + ) -> Status, |
| 108 | + flush_disk_ex: |
| 109 | + extern "efiapi" fn(this: &mut DiskIo2, token: Option<NonNull<DiskIo2Token>>) -> Status, |
| 110 | +} |
| 111 | + |
| 112 | +impl DiskIo2 { |
| 113 | + /// Terminates outstanding asynchronous requests to the device. |
| 114 | + /// |
| 115 | + /// # Errors: |
| 116 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 117 | + /// the cancel operation. |
| 118 | + pub fn cancel(&mut self) -> Result { |
| 119 | + (self.cancel)(self).into() |
| 120 | + } |
| 121 | + |
| 122 | + /// Reads bytes from the disk device. |
| 123 | + /// |
| 124 | + /// # Arguments: |
| 125 | + /// * `media_id` - ID of the medium to be read from. |
| 126 | + /// * `offset` - Starting byte offset on the logical block I/O device to read from. |
| 127 | + /// * `token` - Transaction token for asynchronous read. |
| 128 | + /// * `len` - Buffer size. |
| 129 | + /// * `buffer` - Buffer to read into. |
| 130 | + /// |
| 131 | + /// # Safety |
| 132 | + /// |
| 133 | + /// Because of the asynchronous nature of the disk transaction, manual lifetime |
| 134 | + /// tracking is required. |
| 135 | + /// |
| 136 | + /// # Errors: |
| 137 | + /// * `uefi::status::INVALID_PARAMETER` The read request contains device addresses |
| 138 | + /// that are not valid for the device. |
| 139 | + /// * `uefi::status::OUT_OF_RESOURCES` The request could not be completed due to |
| 140 | + /// a lack of resources. |
| 141 | + /// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium. |
| 142 | + /// * `uefi::status::NO_MEDIA` There is no medium in the device. |
| 143 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 144 | + /// the read operation. |
| 145 | + pub unsafe fn read_disk_raw( |
| 146 | + &self, |
| 147 | + media_id: u32, |
| 148 | + offset: u64, |
| 149 | + token: Option<NonNull<DiskIo2Token>>, |
| 150 | + len: usize, |
| 151 | + buffer: *mut u8, |
| 152 | + ) -> Result { |
| 153 | + (self.read_disk_ex)(self, media_id, offset, token, len, buffer).into() |
| 154 | + } |
| 155 | + |
| 156 | + /// Writes bytes to the disk device. |
| 157 | + /// |
| 158 | + /// # Arguments: |
| 159 | + /// * `media_id` - ID of the medium to write to. |
| 160 | + /// * `offset` - Starting byte offset on the logical block I/O device to write to. |
| 161 | + /// * `token` - Transaction token for asynchronous write. |
| 162 | + /// * `len` - Buffer size. |
| 163 | + /// * `buffer` - Buffer to write from. |
| 164 | + /// |
| 165 | + /// # Safety |
| 166 | + /// |
| 167 | + /// Because of the asynchronous nature of the disk transaction, manual lifetime |
| 168 | + /// tracking is required. |
| 169 | + /// |
| 170 | + /// # Errors: |
| 171 | + /// * `uefi::status::INVALID_PARAMETER` The write request contains device addresses |
| 172 | + /// that are not valid for the device. |
| 173 | + /// * `uefi::status::OUT_OF_RESOURCES` The request could not be completed due to |
| 174 | + /// a lack of resources. |
| 175 | + /// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium. |
| 176 | + /// * `uefi::status::NO_MEDIA` There is no medium in the device. |
| 177 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 178 | + /// the write operation. |
| 179 | + /// * `uefi::status::WRITE_PROTECTED` The device cannot be written to. |
| 180 | + pub unsafe fn write_disk_raw( |
| 181 | + &mut self, |
| 182 | + media_id: u32, |
| 183 | + offset: u64, |
| 184 | + token: Option<NonNull<DiskIo2Token>>, |
| 185 | + len: usize, |
| 186 | + buffer: *const u8, |
| 187 | + ) -> Result { |
| 188 | + (self.write_disk_ex)(self, media_id, offset, token, len, buffer).into() |
| 189 | + } |
| 190 | + |
| 191 | + /// Flushes all modified data to the physical device. |
| 192 | + /// |
| 193 | + /// # Arguments: |
| 194 | + /// * `token` - Transaction token for the asynchronous flush. |
| 195 | + /// |
| 196 | + /// # Errors: |
| 197 | + /// * `uefi::status::OUT_OF_RESOURCES` The request could not be completed due to |
| 198 | + /// a lack of resources. |
| 199 | + /// * `uefi::status::MEDIA_CHANGED` The medium in the device has changed since |
| 200 | + /// the last access. |
| 201 | + /// * `uefi::status::NO_MEDIA` There is no medium in the device. |
| 202 | + /// * `uefi::status::DEVICE_ERROR` The device reported an error while performing |
| 203 | + /// the flush operation. |
| 204 | + /// * `uefi::status::WRITE_PROTECTED` The device cannot be written to. |
| 205 | + pub fn flush_disk(&mut self, token: Option<NonNull<DiskIo2Token>>) -> Result { |
| 206 | + (self.flush_disk_ex)(self, token).into() |
| 207 | + } |
| 208 | +} |
0 commit comments