-
-
Notifications
You must be signed in to change notification settings - Fork 170
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ pub mod shell_params; | |
pub mod shim; | ||
pub mod string; | ||
pub mod tcg; | ||
pub mod usb; | ||
|
||
mod boot_policy; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.