Skip to content

Commit 4a92872

Browse files
committed
Review comments.
1 parent d704933 commit 4a92872

File tree

2 files changed

+99
-102
lines changed
  • uefi/src/proto/network
  • uefi-test-runner/src/proto/network

2 files changed

+99
-102
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use uefi::prelude::BootServices;
2-
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;
2+
use uefi::proto::network::snp::{InterruptStatus, ReceiveFlags, SimpleNetwork};
63
use uefi::proto::network::MacAddress;
74
use uefi::Status;
85

@@ -46,10 +43,8 @@ pub fn test(bt: &BootServices) {
4643
// Set receive filters
4744
simple_network
4845
.receive_filters(
49-
EFI_SIMPLE_NETWORK_RECEIVE_UNICAST
50-
| EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST
51-
| EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST,
52-
0,
46+
ReceiveFlags::UNICAST | ReceiveFlags::MULTICAST | ReceiveFlags::BROADCAST,
47+
ReceiveFlags::empty(),
5348
false,
5449
None,
5550
)
@@ -80,23 +75,24 @@ pub fn test(bt: &BootServices) {
8075
assert!(!simple_network
8176
.get_interrupt_status()
8277
.unwrap()
83-
.transmit_interrupt());
78+
.contains(InterruptStatus::TRANSMIT));
79+
8480
// Send the frame
8581
simple_network
8682
.transmit(
8783
simple_network.mode().media_header_size as usize,
8884
payload,
8985
None,
90-
Some(&dest_addr),
91-
Some(&0x0800),
86+
Some(dest_addr),
87+
Some(0x0800),
9288
)
9389
.expect("Failed to transmit frame");
9490

9591
info!("Waiting for the transmit");
9692
while !simple_network
9793
.get_interrupt_status()
9894
.unwrap()
99-
.transmit_interrupt()
95+
.contains(InterruptStatus::TRANSMIT)
10096
{}
10197

10298
// Attempt to receive a frame

uefi/src/proto/network/snp.rs

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use super::{IpAddress, MacAddress};
1111
use crate::data_types::Event;
1212
use crate::{Result, Status};
13+
use bitflags::bitflags;
1314
use core::ffi::c_void;
1415
use core::ptr;
1516
use core::ptr::NonNull;
@@ -84,69 +85,70 @@ pub struct SimpleNetwork {
8485
}
8586

8687
impl SimpleNetwork {
87-
/// Changes the state of a network from "Stopped" to "Started"
88+
/// Change the state of a network from "Stopped" to "Started".
8889
pub fn start(&self) -> Result {
8990
(self.start)(self).into()
9091
}
9192

92-
/// Changes the state of a network interface from "Started" to "Stopped"
93+
/// Change the state of a network interface from "Started" to "Stopped".
9394
pub fn stop(&self) -> Result {
9495
(self.stop)(self).into()
9596
}
9697

97-
/// Resets a network adapter and allocates the transmit and receive buffers
98-
/// required by the network interface; optionally, also requests allocation of
99-
/// additional transmit and receive buffers
98+
/// Reset a network adapter and allocate the transmit and receive buffers
99+
/// required by the network interface; optionally, also request allocation of
100+
/// additional transmit and receive buffers.
100101
pub fn initialize(&self, extra_rx_buffer_size: usize, extra_tx_buffer_size: usize) -> Result {
101102
(self.initialize)(self, extra_rx_buffer_size, extra_tx_buffer_size).into()
102103
}
103104

104-
/// Resets a network adapter and reinitializes it with the parameters that were
105-
/// provided in the previous call to `initialize`
105+
/// Reset a network adapter and reinitialize it with the parameters that were
106+
/// provided in the previous call to `initialize`.
106107
pub fn reset(&self, extended_verification: bool) -> Result {
107108
(self.reset)(self, extended_verification).into()
108109
}
109110

110-
/// Resets a network adapter and leaves it in a state that is safe
111+
/// Reset a network adapter, leaving it in a state that is safe
111112
/// for another driver to initialize
112113
pub fn shutdown(&self) -> Result {
113114
(self.shutdown)(self).into()
114115
}
115116

116-
/// Manages the multicast receive filters of a network
117+
/// Manage the multicast receive filters of a network.
117118
pub fn receive_filters(
118119
&self,
119-
enable: u32,
120-
disable: u32,
120+
enable: ReceiveFlags,
121+
disable: ReceiveFlags,
121122
reset_mcast_filter: bool,
122123
mcast_filter: Option<&[MacAddress]>,
123124
) -> Result {
124125
if let Some(mcast_filter) = mcast_filter {
125126
(self.receive_filters)(
126127
self,
127-
enable,
128-
disable,
128+
enable.bits,
129+
disable.bits,
129130
reset_mcast_filter,
130131
mcast_filter.len(),
131132
NonNull::new(mcast_filter.as_ptr() as *mut _),
132133
)
133134
.into()
134135
} else {
135-
(self.receive_filters)(self, enable, disable, reset_mcast_filter, 0, None).into()
136+
(self.receive_filters)(self, enable.bits, disable.bits, reset_mcast_filter, 0, None)
137+
.into()
136138
}
137139
}
138140

139-
/// Modifies or resets the current station address, if supported
141+
/// Modify or reset the current station address, if supported.
140142
pub fn station_address(&self, reset: bool, new: Option<&MacAddress>) -> Result {
141143
(self.station_address)(self, reset, new).into()
142144
}
143145

144-
/// Resets statistics on a network interface
146+
/// Reset statistics on a network interface.
145147
pub fn reset_statistics(&self) -> Result {
146148
(self.statistics)(self, true, None, None).into()
147149
}
148150

149-
/// Collects statistics on a network interface
151+
/// Collect statistics on a network interface.
150152
pub fn collect_statistics(&self) -> Result<NetworkStats> {
151153
let mut stats_table: NetworkStats = Default::default();
152154
let mut stats_size = core::mem::size_of::<NetworkStats>();
@@ -155,71 +157,81 @@ impl SimpleNetwork {
155157
Ok(stats_table)
156158
}
157159

158-
/// Converts a multicast IP address to a multicast HW MAC Address
160+
/// Convert a multicast IP address to a multicast HW MAC Address.
159161
pub fn mcast_ip_to_mac(&self, ipv6: bool, ip: IpAddress) -> Result<MacAddress> {
160162
let mut mac_address = MacAddress([0; 32]);
161163
let status = (self.mcast_ip_to_mac)(self, ipv6, &ip, &mut mac_address);
162164
Result::from(status)?;
163165
Ok(mac_address)
164166
}
165167

166-
/// Performs read operations on the NVRAM device attached to
167-
/// a network interface
168-
pub fn read_nv_data(&self, offset: usize, buffer_size: usize, buffer: *mut c_void) -> Result {
169-
(self.nv_data)(self, true, offset, buffer_size, buffer).into()
168+
/// Perform read operations on the NVRAM device attached to
169+
/// a network interface.
170+
pub fn read_nv_data(&self, offset: usize, buffer: &[u8]) -> Result {
171+
(self.nv_data)(
172+
self,
173+
true,
174+
offset,
175+
buffer.len(),
176+
buffer.as_ptr() as *mut c_void,
177+
)
178+
.into()
170179
}
171180

172-
/// Performs write operations on the NVRAM device attached to a network interface
173-
pub fn write_nv_data(&self, offset: usize, buffer_size: usize, buffer: *mut c_void) -> Result {
174-
(self.nv_data)(self, false, offset, buffer_size, buffer).into()
181+
/// Perform write operations on the NVRAM device attached to a network interface.
182+
pub fn write_nv_data(&self, offset: usize, buffer: &mut [u8]) -> Result {
183+
(self.nv_data)(
184+
self,
185+
false,
186+
offset,
187+
buffer.len(),
188+
buffer.as_mut_ptr().cast(),
189+
)
190+
.into()
175191
}
176192

177-
/// Reads the current interrupt status and recycled transmit buffer
178-
/// status from a network interface
193+
/// Read the current interrupt status and recycled transmit buffer
194+
/// status from a network interface.
179195
pub fn get_interrupt_status(&self) -> Result<InterruptStatus> {
180-
let mut interrupt_status = InterruptStatus::new();
196+
let mut interrupt_status = InterruptStatus::empty();
181197
let status = (self.get_status)(self, Some(&mut interrupt_status), None);
182198
Result::from(status)?;
183199
Ok(interrupt_status)
184200
}
185201

186-
/// Reads the current recycled transmit buffer status from a
187-
/// network interface
188-
pub fn get_recycled_transmit_buffer_status(&self) -> Result<Option<*mut u8>> {
202+
/// Read the current recycled transmit buffer status from a
203+
/// network interface.
204+
pub fn get_recycled_transmit_buffer_status(&self) -> Result<Option<NonNull<u8>>> {
189205
let mut tx_buf: *mut c_void = ptr::null_mut();
190206
let status = (self.get_status)(self, None, Some(&mut tx_buf));
191207
Result::from(status)?;
192-
if tx_buf.is_null() {
193-
Ok(None)
194-
} else {
195-
Ok(Some(tx_buf.cast()))
196-
}
208+
Ok(NonNull::new(tx_buf.cast()))
197209
}
198210

199-
/// Places a packet in the transmit queue of a network interface
211+
/// Place a packet in the transmit queue of a network interface.
200212
pub fn transmit(
201213
&self,
202214
header_size: usize,
203215
buffer: &[u8],
204-
src_addr: Option<&MacAddress>,
205-
dest_addr: Option<&MacAddress>,
206-
protocol: Option<&u16>,
216+
src_addr: Option<MacAddress>,
217+
dest_addr: Option<MacAddress>,
218+
protocol: Option<u16>,
207219
) -> Result {
208220
(self.transmit)(
209221
self,
210222
header_size,
211223
buffer.len() + header_size,
212224
buffer.as_ptr().cast(),
213-
src_addr,
214-
dest_addr,
215-
protocol,
225+
src_addr.as_ref(),
226+
dest_addr.as_ref(),
227+
protocol.as_ref(),
216228
)
217229
.into()
218230
}
219231

220-
/// Receives a packet from a network interface
232+
/// Receive a packet from a network interface.
221233
///
222-
/// On success, returns the size of bytes of the received packet
234+
/// On success, returns the size of bytes of the received packet.
223235
pub fn receive(
224236
&self,
225237
buffer: &mut [u8],
@@ -242,60 +254,49 @@ impl SimpleNetwork {
242254
Ok(buffer_size)
243255
}
244256

245-
/// Returns a reference to the Simple Network mode
257+
/// Event that fires once a packet is available to be received.
258+
///
259+
/// On QEMU, this event seems to never fire; it is suggested to verify that your implementation
260+
/// of UEFI properly implements this event before using it.
261+
pub fn wait_for_packet(&self) -> &Event {
262+
&self.wait_for_packet
263+
}
264+
265+
/// Returns a reference to the Simple Network mode.
246266
#[must_use]
247267
pub fn mode(&self) -> &NetworkMode {
248268
unsafe { &*self.mode }
249269
}
250270
}
251271

252-
/// Receive unicast packets.
253-
pub const EFI_SIMPLE_NETWORK_RECEIVE_UNICAST: u32 = 0x01;
254-
/// Receive multicast packets.
255-
pub const EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST: u32 = 0x02;
256-
/// Receive broadcast packets.
257-
pub const EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST: u32 = 0x04;
258-
/// Receive packets in promiscuous mode.
259-
pub const EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS: u32 = 0x08;
260-
/// Receive packets in promiscuous multicast mode.
261-
pub const EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST: u32 = 0x10;
262-
263-
/// A bitmask of currently active interrupts
264-
#[derive(Debug)]
265-
#[repr(transparent)]
266-
pub struct InterruptStatus(u32);
267-
268-
impl InterruptStatus {
269-
/// Creates a new InterruptStatus instance with all bits unset
270-
#[must_use]
271-
pub fn new() -> Self {
272-
Self(0)
273-
}
274-
/// The receive interrupt bit
275-
#[must_use]
276-
pub fn receive_interrupt(&self) -> bool {
277-
self.0 & 0x01 != 0
278-
}
279-
/// The transmit interrupt bit
280-
#[must_use]
281-
pub fn transmit_interrupt(&self) -> bool {
282-
self.0 & 0x02 != 0
283-
}
284-
/// The command interrupt bit
285-
#[must_use]
286-
pub fn command_interrupt(&self) -> bool {
287-
self.0 & 0x04 != 0
288-
}
289-
/// The software interrupt bit
290-
#[must_use]
291-
pub fn software_interrupt(&self) -> bool {
292-
self.0 & 0x08 != 0
272+
bitflags! {
273+
/// Flags to pass to receive_filters to enable/disable reception of some kinds of packets.
274+
pub struct ReceiveFlags : u32 {
275+
/// Receive unicast packets.
276+
const UNICAST = 0x01;
277+
/// Receive multicast packets.
278+
const MULTICAST = 0x02;
279+
/// Receive broadcast packets.
280+
const BROADCAST = 0x04;
281+
/// Receive packets in promiscuous mode.
282+
const PROMISCUOUS = 0x08;
283+
/// Receive packets in promiscuous multicast mode.
284+
const PROMISCUOUS_MULTICAST = 0x10;
293285
}
294286
}
295287

296-
impl Default for InterruptStatus {
297-
fn default() -> Self {
298-
Self::new()
288+
bitflags! {
289+
/// Flags returned by get_interrupt_status to indicate which interrupts have fired on the
290+
/// interace since the last call.
291+
pub struct InterruptStatus : u32 {
292+
/// Packet received.
293+
const RECEIVE = 0x01;
294+
/// Packet transmitted.
295+
const TRANSMIT = 0x02;
296+
/// Command interrupt fired.
297+
const COMMAND = 0x04;
298+
/// Software interrupt fired.
299+
const SOFTWARE = 0x08;
299300
}
300301
}
301302

0 commit comments

Comments
 (0)