Skip to content

uefi: Add safe EFI_USB_IO_PROTOCOL bindings #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions uefi-test-runner/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn test() {
rng::test();
shell_params::test();
string::test();
usb::test();
misc::test();

// disable the ATA test on aarch64 for now. The aarch64 UEFI Firmware does not yet seem
Expand Down Expand Up @@ -96,3 +97,4 @@ mod shell_params;
mod shim;
mod string;
mod tcg;
mod usb;
82 changes: 82 additions & 0 deletions uefi-test-runner/src/proto/usb/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::mem;
use uefi::proto::usb::DeviceDescriptor;
use uefi::proto::usb::io::{ControlTransfer, UsbIo};
use uefi::{Status, boot};

const DEVICE_TO_HOST: u8 = 1 << 7;
const STANDARD_REQUEST: u8 = 0b00 << 5;
const DEVICE_RECIPIENT: u8 = 0b0_0000;
const GET_DESCRIPTOR_REQUEST: u8 = 6;
const DEVICE_DESCRIPTOR: u8 = 1;

/// This test iterates through all of the exposed active USB interfaces and
/// performs checks on each to validate that descriptor acquisition and control
/// transfers work correctly.
pub fn test() {
info!("Testing USB I/O protocol");

let handles = boot::locate_handle_buffer(boot::SearchType::from_proto::<UsbIo>())
.expect("failed to acquire USB I/O handles");

for handle in handles.iter().copied() {
let mut io = boot::open_protocol_exclusive::<UsbIo>(handle)
.expect("failed to open USB I/O protocol");

let device = io
.device_descriptor()
.expect("failed to acquire USB device descriptor");
io.config_descriptor()
.expect("failed to acquire USB config descriptor");
io.interface_descriptor()
.expect("failed to acquire USB interface descriptor");

for endpoint_index in 0..16 {
let result = io.endpoint_descriptor(endpoint_index);
if result
.as_ref()
.is_err_and(|error| error.status() == Status::NOT_FOUND)
{
continue;
}

result.expect("failed to acquire USB endpoint descriptor");
}

let supported_languages = io
.supported_languages()
.expect("failed to acquire supported language list");
let test_language = supported_languages[0];

for string_index in 0..=u8::MAX {
let result = io.string_descriptor(test_language, string_index);
if result
.as_ref()
.is_err_and(|error| error.status() == Status::NOT_FOUND)
{
continue;
}

result.expect("failed to acquire string descriptor");
}

let mut buffer = [0u8; mem::size_of::<DeviceDescriptor>()];

io.control_transfer(
DEVICE_TO_HOST | STANDARD_REQUEST | DEVICE_RECIPIENT,
GET_DESCRIPTOR_REQUEST,
u16::from(DEVICE_DESCRIPTOR) << 8,
0,
ControlTransfer::DataIn(&mut buffer[..mem::size_of::<DeviceDescriptor>()]),
0,
)
.expect("failed control transfer");
unsafe {
assert_eq!(
device,
buffer.as_ptr().cast::<DeviceDescriptor>().read_unaligned()
)
}
}
}
9 changes: 9 additions & 0 deletions uefi-test-runner/src/proto/usb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

pub fn test() {
info!("Testing USB protocols");

io::test();
}

mod io;
1 change: 1 addition & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Added
- Added `ConfigTableEntry::MEMORY_ATTRIBUTES_GUID` and `ConfigTableEntry::IMAGE_SECURITY_DATABASE_GUID`.
- Added `proto::usb::io::UsbIo`.

## Changed
- **Breaking:** `boot::stall` now take `core::time::Duration` instead of `usize`.
Expand Down
1 change: 1 addition & 0 deletions uefi/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod shell_params;
pub mod shim;
pub mod string;
pub mod tcg;
pub mod usb;

mod boot_policy;

Expand Down
Loading