Skip to content

Commit f1b0d4b

Browse files
committed
uefi: Add integration test for the USB I/O protocol
1 parent e5b16ee commit f1b0d4b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

uefi-test-runner/src/proto/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn test() {
2424
rng::test();
2525
shell_params::test();
2626
string::test();
27+
usb::test();
2728
misc::test();
2829

2930
// disable the ATA test on aarch64 for now. The aarch64 UEFI Firmware does not yet seem
@@ -96,3 +97,4 @@ mod shell_params;
9697
mod shim;
9798
mod string;
9899
mod tcg;
100+
mod usb;

uefi-test-runner/src/proto/usb/io.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
use core::mem;
4+
use uefi::{
5+
Status, boot,
6+
proto::usb::{
7+
DeviceDescriptor,
8+
io::{ControlTransfer, UsbIo},
9+
},
10+
};
11+
12+
pub fn test() {
13+
info!("Testing USB I/O protocol");
14+
15+
let handles = boot::locate_handle_buffer(boot::SearchType::from_proto::<UsbIo>())
16+
.expect("failed to acquire USB I/O handles");
17+
18+
for handle in handles.iter().copied() {
19+
let mut io = boot::open_protocol_exclusive::<UsbIo>(handle)
20+
.expect("failed to open USB I/O protocol");
21+
22+
let device = io
23+
.device_descriptor()
24+
.expect("failed to acquire USB device descriptor");
25+
let config = io
26+
.config_descriptor()
27+
.expect("failed to acquire USB config descriptor");
28+
let interface = io
29+
.interface_descriptor()
30+
.expect("failed to acquire USB interface descriptor");
31+
for endpoint_index in 0..16 {
32+
let result = io.endpoint_descriptor(endpoint_index);
33+
if result
34+
.as_ref()
35+
.is_err_and(|error| error.status() == Status::NOT_FOUND)
36+
{
37+
continue;
38+
}
39+
40+
result.expect("failed to acquire USB endpoint descriptor");
41+
}
42+
43+
let mut buffer = [0u8; mem::size_of::<DeviceDescriptor>()];
44+
45+
io.control_transfer(
46+
0b1000_0000,
47+
6,
48+
1u16 << 8,
49+
0,
50+
ControlTransfer::DataIn(&mut buffer[..mem::size_of::<DeviceDescriptor>()]),
51+
0,
52+
)
53+
.expect("failed control transfer");
54+
unsafe {
55+
assert_eq!(
56+
device,
57+
buffer.as_ptr().cast::<DeviceDescriptor>().read_unaligned()
58+
)
59+
}
60+
}
61+
}

uefi-test-runner/src/proto/usb/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
pub fn test() {
4+
info!("Testing USB protocols");
5+
6+
io::test();
7+
}
8+
9+
mod io;

xtask/src/qemu.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ pub fn run_qemu(arch: UefiArch, opt: &QemuOpt) -> Result<()> {
438438
cmd.args(["-display", "none"]);
439439
}
440440

441+
// Configure USB
442+
cmd.args(["-device", "qemu-xhci"]);
443+
444+
cmd.args(["-device", "usb-net"]);
445+
441446
// Second (FAT) disk
442447
let test_disk = tmp_dir.join("test_disk.fat.img");
443448
create_mbr_test_disk(&test_disk)?;

0 commit comments

Comments
 (0)