Skip to content

Commit 0e003b2

Browse files
committed
Clean up implementation of SNP and fix test code.
1 parent 1975acf commit 0e003b2

File tree

3 files changed

+174
-102
lines changed

3 files changed

+174
-102
lines changed

uefi-test-runner/src/proto/network/snp.rs

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
use uefi::prelude::BootServices;
22
use uefi::proto::network::snp::SimpleNetwork;
3+
use uefi::proto::network::snp::EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
4+
use uefi::proto::network::snp::EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
5+
use uefi::proto::network::snp::EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
36
use uefi::proto::network::MacAddress;
4-
7+
use uefi::Status;
58

69
pub fn test(bt: &BootServices) {
710
info!("Testing the simple network protocol");
811

9-
let handles = bt
10-
.find_handles::<SimpleNetwork>()
11-
.expect("Failed to get handles for `SimpleNetwork` protocol");
12+
let handles = bt.find_handles::<SimpleNetwork>().unwrap_or_default();
1213

1314
for handle in handles {
14-
1515
let simple_network = bt.open_protocol_exclusive::<SimpleNetwork>(handle);
16-
if simple_network.is_err() { continue; }
16+
if simple_network.is_err() {
17+
continue;
18+
}
1719
let simple_network = simple_network.unwrap();
1820

1921
// Check shutdown
20-
simple_network.shutdown().expect("Failed to shutdown Simple Network");
22+
simple_network
23+
.shutdown()
24+
.expect("Failed to shutdown Simple Network");
2125

2226
// Check stop
23-
simple_network.stop().expect("Failed to stop Simple Network");
27+
simple_network
28+
.stop()
29+
.expect("Failed to stop Simple Network");
2430

2531
// Check start
26-
simple_network.start().expect("Failed to start Simple Network");
32+
simple_network
33+
.start()
34+
.expect("Failed to start Simple Network");
2735

2836
// Check initialize
29-
simple_network.initialize(None, None)
37+
simple_network
38+
.initialize(0, 0)
3039
.expect("Failed to initialize Simple Network");
3140

3241
simple_network.reset_statistics().unwrap();
@@ -35,51 +44,81 @@ pub fn test(bt: &BootServices) {
3544
simple_network.get_interrupt_status().unwrap();
3645

3746
// Set receive filters
38-
simple_network.receive_filters(0x01 | 0x02 | 0x04 | 0x08 | 0x10, 0, false, None, None)
47+
simple_network
48+
.receive_filters(
49+
EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
50+
| EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
51+
| EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST,
52+
0,
53+
false,
54+
None,
55+
)
3956
.expect("Failed to set receive filters");
4057

4158
// Check media
4259
if !simple_network.mode().media_present_supported || !simple_network.mode().media_present {
4360
continue;
4461
}
4562

46-
let payload = &[0u8; 46];
47-
48-
let dest_addr = MacAddress([0xffu8;32]);
49-
assert!(!simple_network.get_interrupt_status().unwrap().transmit_interrupt());
63+
let payload = b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
64+
\x45\x00\
65+
\x00\x21\
66+
\x00\x01\
67+
\x00\x00\
68+
\x10\
69+
\x11\
70+
\x07\x6a\
71+
\xc0\xa8\x11\x0f\
72+
\xc0\xa8\x11\x02\
73+
\x54\x45\
74+
\x54\x44\
75+
\x00\x0d\
76+
\xa9\xe4\
77+
\x04\x01\x02\x03\x04";
78+
79+
let dest_addr = MacAddress([0xffu8; 32]);
80+
assert!(!simple_network
81+
.get_interrupt_status()
82+
.unwrap()
83+
.transmit_interrupt());
5084
// Send the frame
51-
simple_network.transmit(
52-
simple_network.mode().media_header_size as usize,
53-
payload,
54-
None,
55-
Some(&dest_addr),
56-
Some(&0x0800),
57-
)
58-
.expect("Failed to transmit frame");
85+
simple_network
86+
.transmit(
87+
simple_network.mode().media_header_size as usize,
88+
payload,
89+
None,
90+
Some(&dest_addr),
91+
Some(&0x0800),
92+
)
93+
.expect("Failed to transmit frame");
5994

6095
info!("Waiting for the transmit");
61-
while !simple_network.get_interrupt_status().unwrap().transmit_interrupt() {}
96+
while !simple_network
97+
.get_interrupt_status()
98+
.unwrap()
99+
.transmit_interrupt()
100+
{}
62101

63102
// Attempt to receive a frame
64103
let mut buffer = [0u8; 1500];
65-
66-
let mut count = 0;
67-
104+
68105
info!("Waiting for the reception");
69-
while count < 1_000 {
70-
let result = simple_network.receive(
71-
&mut buffer,
72-
None,
73-
None,
74-
None,
75-
None
76-
);
77-
if result.is_ok() { break; }
78-
count += 1;
106+
if simple_network.receive(&mut buffer, None, None, None, None)
107+
== Err(Status::NOT_READY.into())
108+
{
109+
bt.stall(1_000_000);
110+
111+
simple_network
112+
.receive(&mut buffer, None, None, None, None)
113+
.unwrap();
79114
}
80115

116+
assert_eq!(buffer[42..47], [4, 4, 3, 2, 1]);
117+
81118
// Get stats
82-
let stats = simple_network.collect_statistics().expect("Failed to collect statistics");
119+
let stats = simple_network
120+
.collect_statistics()
121+
.expect("Failed to collect statistics");
83122
info!("Stats: {:?}", stats);
84123

85124
// One frame should have been transmitted and one received

0 commit comments

Comments
 (0)