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 / usb / io.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! USB I/O protocol. |
| 4 | |
| 5 | use core::{ffi, mem, ptr, slice}; |
| 6 | |
| 7 | use uefi_macros::unsafe_protocol; |
| 8 | use uefi_raw::protocol::usb::io::UsbIoProtocol; |
| 9 | use uefi_raw::protocol::usb::{ |
| 10 | ConfigDescriptor, DataDirection, DeviceDescriptor, DeviceRequest, EndpointDescriptor, |
| 11 | InterfaceDescriptor, UsbTransferStatus, |
| 12 | }; |
| 13 | |
| 14 | use crate::data_types::PoolString; |
| 15 | use crate::{Char16, Result, StatusExt}; |
| 16 | |
| 17 | /// USB I/O protocol. |
| 18 | #[derive(Debug)] |
| 19 | #[repr(transparent)] |
| 20 | #[unsafe_protocol(UsbIoProtocol::GUID)] |
| 21 | pub struct UsbIo(UsbIoProtocol); |
| 22 | |
| 23 | impl UsbIo { |
| 24 | /// Performs a USB Control transfer, allowing the driver to communicate with the USB device. |
| 25 | pub fn control_transfer( |
| 26 | &mut self, |
| 27 | request_type: u8, |
| 28 | request: u8, |
| 29 | value: u16, |
| 30 | index: u16, |
| 31 | transfer: ControlTransfer, |
| 32 | timeout: u32, |
| 33 | ) -> Result<(), UsbTransferStatus> { |
| 34 | let (direction, buffer_ptr, length) = match transfer { |
| 35 | ControlTransfer::None => (DataDirection::NO_DATA, ptr::null_mut(), 0), |
| 36 | ControlTransfer::DataIn(buffer) => { |
| 37 | (DataDirection::DATA_IN, buffer.as_mut_ptr(), buffer.len()) |
| 38 | } |
| 39 | ControlTransfer::DataOut(buffer) => ( |
| 40 | DataDirection::DATA_OUT, |
| 41 | buffer.as_ptr().cast_mut(), |
| 42 | buffer.len(), |
| 43 | ), |
| 44 | }; |
| 45 | |
| 46 | let request_type = if direction == DataDirection::DATA_IN { |
| 47 | request_type | 0x80 |
| 48 | } else if direction == DataDirection::DATA_OUT { |
| 49 | request_type & !0x80 |
| 50 | } else { |
| 51 | request_type |
| 52 | }; |
| 53 | |
| 54 | let device_request = DeviceRequest { |
| 55 | request_type, |
| 56 | request, |
| 57 | value, |
| 58 | index, |
| 59 | length: length as u16, |
| 60 | }; |
| 61 | let mut status = UsbTransferStatus::default(); |
| 62 | |
| 63 | // SAFETY: The memory is valid. |
| 64 | unsafe { |
| 65 | (self.0.control_transfer)( |
| 66 | &mut self.0, |
| 67 | &device_request, |
| 68 | direction, |
| 69 | timeout, |
| 70 | buffer_ptr.cast::<ffi::c_void>(), |
| 71 | length, |
| 72 | &mut status, |
| 73 | ) |
| 74 | } |
| 75 | .to_result_with_err(|_| status) |
| 76 | } |
| 77 | |
| 78 | /// Sends the provided buffer to a USB device over a bulk transfer pipe. |
| 79 | /// |
| 80 | /// Returns the number of bytes that were actually sent to the device. |
| 81 | pub fn sync_bulk_send( |
| 82 | &mut self, |
| 83 | endpoint: u8, |
| 84 | buffer: &[u8], |
| 85 | timeout: usize, |
| 86 | ) -> Result<usize, UsbTransferStatus> { |
| 87 | let mut status = UsbTransferStatus::default(); |
| 88 | let mut length = buffer.len(); |
| 89 | |
| 90 | // SAFETY: The memory is valid. |
| 91 | unsafe { |
| 92 | (self.0.bulk_transfer)( |
| 93 | &mut self.0, |
| 94 | endpoint & !0x80, |
| 95 | buffer.as_ptr().cast_mut().cast::<ffi::c_void>(), |
| 96 | &mut length, |
| 97 | timeout, |
| 98 | &mut status, |
| 99 | ) |
| 100 | } |
| 101 | .to_result_with_err(|_| status) |
| 102 | .map(|()| length) |
| 103 | } |
| 104 | |
| 105 | /// Fills the provided buffer with data from a USB device over a bulk transfer pipe. |
| 106 | /// |
| 107 | /// Returns the number of bytes that were actually received from the device. |
| 108 | pub fn sync_bulk_receive( |
| 109 | &mut self, |
| 110 | endpoint: u8, |
| 111 | buffer: &mut [u8], |
| 112 | timeout: usize, |
| 113 | ) -> Result<usize, UsbTransferStatus> { |
| 114 | let mut status = UsbTransferStatus::default(); |
| 115 | let mut length = buffer.len(); |
| 116 | |
| 117 | // SAFETY: The memory is valid. |
| 118 | unsafe { |
| 119 | (self.0.bulk_transfer)( |
| 120 | &mut self.0, |
| 121 | endpoint | 0x80, |
| 122 | buffer.as_mut_ptr().cast::<ffi::c_void>(), |
| 123 | &mut length, |
| 124 | timeout, |
| 125 | &mut status, |
| 126 | ) |
| 127 | } |
| 128 | .to_result_with_err(|_| status) |
| 129 | .map(|()| length) |
| 130 | } |
| 131 | |
| 132 | /// Sends the provided buffer to a USB device through a synchronous interrupt transfer. |
| 133 | pub fn sync_interrupt_send( |
| 134 | &mut self, |
| 135 | endpoint: u8, |
| 136 | buffer: &[u8], |
| 137 | timeout: usize, |
| 138 | ) -> Result<usize, UsbTransferStatus> { |
| 139 | let mut status = UsbTransferStatus::default(); |
| 140 | let mut length = buffer.len(); |
| 141 | |
| 142 | // SAFETY: The memory is valid. |
| 143 | unsafe { |
| 144 | (self.0.sync_interrupt_transfer)( |
| 145 | &mut self.0, |
| 146 | endpoint & !0x80, |
| 147 | buffer.as_ptr().cast_mut().cast::<ffi::c_void>(), |
| 148 | &mut length, |
| 149 | timeout, |
| 150 | &mut status, |
| 151 | ) |
| 152 | } |
| 153 | .to_result_with_err(|_| status) |
| 154 | .map(|()| length) |
| 155 | } |
| 156 | |
| 157 | /// Fills the provided buffer with data from a USB device through a synchronous interrupt |
| 158 | /// transfer. |
| 159 | pub fn sync_interrupt_receive( |
| 160 | &mut self, |
| 161 | endpoint: u8, |
| 162 | buffer: &mut [u8], |
| 163 | timeout: usize, |
| 164 | ) -> Result<usize, UsbTransferStatus> { |
| 165 | let mut status = UsbTransferStatus::default(); |
| 166 | let mut length = buffer.len(); |
| 167 | |
| 168 | // SAFETY: The memory is valid. |
| 169 | unsafe { |
| 170 | (self.0.sync_interrupt_transfer)( |
| 171 | &mut self.0, |
| 172 | endpoint | 0x80, |
| 173 | buffer.as_mut_ptr().cast::<ffi::c_void>(), |
| 174 | &mut length, |
| 175 | timeout, |
| 176 | &mut status, |
| 177 | ) |
| 178 | } |
| 179 | .to_result_with_err(|_| status) |
| 180 | .map(|()| length) |
| 181 | } |
| 182 | |
| 183 | /// Sends the provided buffer to a USB device over an isochronous transfer pipe. |
| 184 | pub fn sync_isochronous_send( |
| 185 | &mut self, |
| 186 | endpoint: u8, |
| 187 | buffer: &[u8], |
| 188 | ) -> Result<(), UsbTransferStatus> { |
| 189 | let mut status = UsbTransferStatus::default(); |
| 190 | |
| 191 | // SAFETY: The memory is valid. |
| 192 | unsafe { |
| 193 | (self.0.isochronous_transfer)( |
| 194 | &mut self.0, |
| 195 | endpoint & !0x80, |
| 196 | buffer.as_ptr().cast_mut().cast::<ffi::c_void>(), |
| 197 | buffer.len(), |
| 198 | &mut status, |
| 199 | ) |
| 200 | } |
| 201 | .to_result_with_err(|_| status) |
| 202 | } |
| 203 | |
| 204 | /// Fills the provided buffer with data from a USB device over an isochronous transfer pipe. |
| 205 | pub fn sync_isochronous_receive( |
| 206 | &mut self, |
| 207 | endpoint: u8, |
| 208 | buffer: &mut [u8], |
| 209 | ) -> Result<(), UsbTransferStatus> { |
| 210 | let mut status = UsbTransferStatus::default(); |
| 211 | |
| 212 | // SAFETY: The memory is valid. |
| 213 | unsafe { |
| 214 | (self.0.isochronous_transfer)( |
| 215 | &mut self.0, |
| 216 | endpoint | 0x80, |
| 217 | buffer.as_mut_ptr().cast::<ffi::c_void>(), |
| 218 | buffer.len(), |
| 219 | &mut status, |
| 220 | ) |
| 221 | } |
| 222 | .to_result_with_err(|_| status) |
| 223 | } |
| 224 | |
| 225 | /// Returns information about USB devices, including the device's class, subclass, and number |
| 226 | /// of configurations. |
| 227 | pub fn device_descriptor(&mut self) -> Result<DeviceDescriptor> { |
| 228 | // SAFETY: The C descriptor is immediately filled by firmware. |
| 229 | let mut device_descriptor = unsafe { mem::zeroed() }; |
| 230 | |
| 231 | // SAFETY: The memory is valid. |
| 232 | unsafe { (self.0.get_device_descriptor)(&mut self.0, &mut device_descriptor) } |
| 233 | .to_result_with_val(|| device_descriptor) |
| 234 | } |
| 235 | |
| 236 | /// Returns information about the active configuration of the USB device. |
| 237 | pub fn config_descriptor(&mut self) -> Result<ConfigDescriptor> { |
| 238 | // SAFETY: The C descriptor is immediately filled by firmware. |
| 239 | let mut config_descriptor = unsafe { mem::zeroed() }; |
| 240 | |
| 241 | // SAFETY: The memory is valid. |
| 242 | unsafe { (self.0.get_config_descriptor)(&mut self.0, &mut config_descriptor) } |
| 243 | .to_result_with_val(|| config_descriptor) |
| 244 | } |
| 245 | |
| 246 | /// Returns information about the interface of the USB device. |
| 247 | pub fn interface_descriptor(&mut self) -> Result<InterfaceDescriptor> { |
| 248 | // SAFETY: The C descriptor is immediately filled by firmware. |
| 249 | let mut interface_descriptor = unsafe { mem::zeroed() }; |
| 250 | |
| 251 | // SAFETY: The memory is valid. |
| 252 | unsafe { (self.0.get_interface_descriptor)(&mut self.0, &mut interface_descriptor) } |
| 253 | .to_result_with_val(|| interface_descriptor) |
| 254 | } |
| 255 | |
| 256 | /// Returns information about the interface of the USB device. |
| 257 | pub fn endpoint_descriptor(&mut self, endpoint: u8) -> Result<EndpointDescriptor> { |
| 258 | // SAFETY: The C descriptor is immediately filled by firmware. |
| 259 | let mut endpoint_descriptor = unsafe { mem::zeroed() }; |
| 260 | |
| 261 | // SAFETY: The memory is valid. |
| 262 | unsafe { (self.0.get_endpoint_descriptor)(&mut self.0, endpoint, &mut endpoint_descriptor) } |
| 263 | .to_result_with_val(|| endpoint_descriptor) |
| 264 | } |
| 265 | |
| 266 | /// Returns the string associated with `string_id` in the language associated with `lang_id`. |
| 267 | pub fn string_descriptor(&mut self, lang_id: u16, string_id: u8) -> Result<PoolString> { |
| 268 | let mut string_ptr = ptr::null_mut(); |
| 269 | |
| 270 | // SAFETY: The memory is valid. |
| 271 | unsafe { (self.0.get_string_descriptor)(&mut self.0, lang_id, string_id, &mut string_ptr) } |
| 272 | .to_result()?; |
| 273 | // SAFETY: The memory is valid. |
| 274 | unsafe { PoolString::new(string_ptr.cast::<Char16>()) } |
| 275 | } |
| 276 | |
| 277 | /// Returns all of the language ID codes that the USB device supports. |
| 278 | pub fn supported_languages(&mut self) -> Result<&[u16]> { |
| 279 | let mut lang_id_table_ptr = ptr::null_mut(); |
| 280 | let mut lang_id_table_size = 0; |
| 281 | |
| 282 | // SAFETY: The memory is valid. |
| 283 | unsafe { |
| 284 | (self.0.get_supported_languages)( |
| 285 | &mut self.0, |
| 286 | &mut lang_id_table_ptr, |
| 287 | &mut lang_id_table_size, |
| 288 | ) |
| 289 | } |
| 290 | .to_result_with_val(|| { |
| 291 | let lang_count = usize::from(lang_id_table_size) / size_of::<u16>(); |
| 292 | // A device without string descriptors has no language table and |
| 293 | // the firmware may report it as NULL. |
| 294 | if lang_id_table_ptr.is_null() || lang_count == 0 { |
| 295 | &[] |
| 296 | } else { |
| 297 | // SAFETY: The memory is valid. |
| 298 | unsafe { slice::from_raw_parts(lang_id_table_ptr, lang_count) } |
| 299 | } |
| 300 | }) |
| 301 | } |
| 302 | |
| 303 | /// Resets and reconfigures the USB controller. |
| 304 | /// |
| 305 | /// This function should work for all USB devices except USB Hub Controllers. |
| 306 | pub fn port_reset(&mut self) -> Result { |
| 307 | // SAFETY: The memory is valid. |
| 308 | unsafe { (self.0.port_reset)(&mut self.0) }.to_result() |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /// Controls what type of USB control transfer operation should occur. |
| 313 | #[derive(Debug)] |
| 314 | pub enum ControlTransfer<'buffer> { |
| 315 | /// The USB control transfer has no data phase. |
| 316 | None, |
| 317 | /// The USB control transfer has an input data phase. |
| 318 | DataIn(&'buffer mut [u8]), |
| 319 | /// The USB control transfer has an output data phase. |
| 320 | DataOut(&'buffer [u8]), |
| 321 | } |
| 322 | |
| 323 | #[cfg(test)] |
| 324 | mod tests { |
| 325 | use super::*; |
| 326 | use crate::Status; |
| 327 | use core::ptr; |
| 328 | use uefi_raw::Boolean; |
| 329 | use uefi_raw::protocol::usb::AsyncUsbTransferCallback; |
| 330 | |
| 331 | /// Byte the mocked device sends on every receive transfer. |
| 332 | const RX_BYTE: u8 = 0xAB; |
| 333 | |
| 334 | /// Mock of `control_transfer`. Fills the whole data buffer for DATA_IN |
| 335 | /// transfers. |
| 336 | /// |
| 337 | /// # Safety |
| 338 | /// `data` must be valid for writing `data_length` bytes. |
| 339 | unsafe extern "efiapi" fn mock_control_transfer( |
| 340 | _: *mut UsbIoProtocol, |
| 341 | _: *const DeviceRequest, |
| 342 | direction: DataDirection, |
| 343 | _: u32, |
| 344 | data: *mut ffi::c_void, |
| 345 | data_length: usize, |
| 346 | _: *mut UsbTransferStatus, |
| 347 | ) -> Status { |
| 348 | if direction == DataDirection::DATA_IN { |
| 349 | // SAFETY: Guaranteed by the caller. |
| 350 | unsafe { data.cast::<u8>().write_bytes(RX_BYTE, data_length) }; |
| 351 | } |
| 352 | Status::SUCCESS |
| 353 | } |
| 354 | |
| 355 | /// Mock of `bulk_transfer` and `sync_interrupt_transfer`. Fills the |
| 356 | /// whole data buffer for receive transfers (endpoint bit 7 set). |
| 357 | /// |
| 358 | /// # Safety |
| 359 | /// `data` must be valid for writing `*data_length` bytes. |
| 360 | unsafe extern "efiapi" fn mock_sync_transfer( |
| 361 | _: *mut UsbIoProtocol, |
| 362 | endpoint: u8, |
| 363 | data: *mut ffi::c_void, |
| 364 | data_length: *mut usize, |
| 365 | _: usize, |
| 366 | _: *mut UsbTransferStatus, |
| 367 | ) -> Status { |
| 368 | if endpoint & 0x80 != 0 { |
| 369 | // SAFETY: Guaranteed by the caller. |
| 370 | unsafe { data.cast::<u8>().write_bytes(RX_BYTE, *data_length) }; |
| 371 | } |
| 372 | Status::SUCCESS |
| 373 | } |
| 374 | |
| 375 | /// Mock of `get_supported_languages` for a device without string |
| 376 | /// descriptors: no language table at all. |
| 377 | /// |
| 378 | /// # Safety |
| 379 | /// `lang_id_table` and `table_size` must be valid for writing. |
| 380 | unsafe extern "efiapi" fn mock_no_supported_languages( |
| 381 | _: *mut UsbIoProtocol, |
| 382 | lang_id_table: *mut *mut u16, |
| 383 | table_size: *mut u16, |
| 384 | ) -> Status { |
| 385 | // SAFETY: Guaranteed by the caller. |
| 386 | unsafe { |
| 387 | *lang_id_table = ptr::null_mut(); |
| 388 | *table_size = 0; |
| 389 | } |
| 390 | Status::SUCCESS |
| 391 | } |
| 392 | |
| 393 | // Stubs for the operations the tests do not exercise. |
| 394 | |
| 395 | extern "efiapi" fn stub_async_interrupt_transfer( |
| 396 | _: *mut UsbIoProtocol, |
| 397 | _: u8, |
| 398 | _: Boolean, |
| 399 | _: usize, |
| 400 | _: usize, |
| 401 | _: Option<AsyncUsbTransferCallback>, |
| 402 | _: *const ffi::c_void, |
| 403 | ) -> Status { |
| 404 | unimplemented!() |
| 405 | } |
| 406 | |
| 407 | extern "efiapi" fn stub_isochronous_transfer( |
| 408 | _: *mut UsbIoProtocol, |
| 409 | _: u8, |
| 410 | _: *mut ffi::c_void, |
| 411 | _: usize, |
| 412 | _: *mut UsbTransferStatus, |
| 413 | ) -> Status { |
| 414 | unimplemented!() |
| 415 | } |
| 416 | |
| 417 | extern "efiapi" fn stub_async_isochronous_transfer( |
| 418 | _: *mut UsbIoProtocol, |
| 419 | _: u8, |
| 420 | _: *mut ffi::c_void, |
| 421 | _: usize, |
| 422 | _: AsyncUsbTransferCallback, |
| 423 | _: *const ffi::c_void, |
| 424 | ) -> Status { |
| 425 | unimplemented!() |
| 426 | } |
| 427 | |
| 428 | extern "efiapi" fn stub_get_device_descriptor( |
| 429 | _: *mut UsbIoProtocol, |
| 430 | _: *mut DeviceDescriptor, |
| 431 | ) -> Status { |
| 432 | unimplemented!() |
| 433 | } |
| 434 | |
| 435 | extern "efiapi" fn stub_get_config_descriptor( |
| 436 | _: *mut UsbIoProtocol, |
| 437 | _: *mut ConfigDescriptor, |
| 438 | ) -> Status { |
| 439 | unimplemented!() |
| 440 | } |
| 441 | |
| 442 | extern "efiapi" fn stub_get_interface_descriptor( |
| 443 | _: *mut UsbIoProtocol, |
| 444 | _: *mut InterfaceDescriptor, |
| 445 | ) -> Status { |
| 446 | unimplemented!() |
| 447 | } |
| 448 | |
| 449 | extern "efiapi" fn stub_get_endpoint_descriptor( |
| 450 | _: *mut UsbIoProtocol, |
| 451 | _: u8, |
| 452 | _: *mut EndpointDescriptor, |
| 453 | ) -> Status { |
| 454 | unimplemented!() |
| 455 | } |
| 456 | |
| 457 | extern "efiapi" fn stub_get_string_descriptor( |
| 458 | _: *mut UsbIoProtocol, |
| 459 | _: u16, |
| 460 | _: u8, |
| 461 | _: *mut *mut uefi_raw::Char16, |
| 462 | ) -> Status { |
| 463 | unimplemented!() |
| 464 | } |
| 465 | |
| 466 | extern "efiapi" fn stub_port_reset(_: *mut UsbIoProtocol) -> Status { |
| 467 | unimplemented!() |
| 468 | } |
| 469 | |
| 470 | const fn mock_protocol() -> UsbIoProtocol { |
| 471 | UsbIoProtocol { |
| 472 | control_transfer: mock_control_transfer, |
| 473 | bulk_transfer: mock_sync_transfer, |
| 474 | async_interrupt_transfer: stub_async_interrupt_transfer, |
| 475 | sync_interrupt_transfer: mock_sync_transfer, |
| 476 | isochronous_transfer: stub_isochronous_transfer, |
| 477 | async_isochronous_transfer: stub_async_isochronous_transfer, |
| 478 | get_device_descriptor: stub_get_device_descriptor, |
| 479 | get_config_descriptor: stub_get_config_descriptor, |
| 480 | get_interface_descriptor: stub_get_interface_descriptor, |
| 481 | get_endpoint_descriptor: stub_get_endpoint_descriptor, |
| 482 | get_string_descriptor: stub_get_string_descriptor, |
| 483 | get_supported_languages: mock_no_supported_languages, |
| 484 | port_reset: stub_port_reset, |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// The receive transfers hand the firmware a pointer into the caller's |
| 489 | /// buffer, which the firmware writes through. This test is mainly useful |
| 490 | /// under Miri, which checks that the pointer grants write access. |
| 491 | #[test] |
| 492 | fn test_receive_writes_through_buffer_ptr() { |
| 493 | let mut usb = UsbIo(mock_protocol()); |
| 494 | |
| 495 | let mut buffer = [0; 4]; |
| 496 | usb.control_transfer(0, 0, 0, 0, ControlTransfer::DataIn(&mut buffer), 0) |
| 497 | .unwrap(); |
| 498 | assert_eq!(buffer, [RX_BYTE; 4]); |
| 499 | |
| 500 | let mut buffer = [0; 4]; |
| 501 | assert_eq!(usb.sync_bulk_receive(1, &mut buffer, 0).unwrap(), 4); |
| 502 | assert_eq!(buffer, [RX_BYTE; 4]); |
| 503 | |
| 504 | let mut buffer = [0; 4]; |
| 505 | assert_eq!(usb.sync_interrupt_receive(1, &mut buffer, 0).unwrap(), 4); |
| 506 | assert_eq!(buffer, [RX_BYTE; 4]); |
| 507 | } |
| 508 | |
| 509 | /// A device without string descriptors reports no language table. |
| 510 | #[test] |
| 511 | fn test_supported_languages_empty() { |
| 512 | let mut usb = UsbIo(mock_protocol()); |
| 513 | assert_eq!(usb.supported_languages().unwrap(), &[]); |
| 514 | } |
| 515 | } |