1010use super :: { IpAddress , MacAddress } ;
1111use crate :: data_types:: Event ;
1212use crate :: { Result , Status } ;
13+ use bitflags:: bitflags;
1314use core:: ffi:: c_void;
1415use core:: ptr;
1516use core:: ptr:: NonNull ;
@@ -84,69 +85,70 @@ pub struct SimpleNetwork {
8485}
8586
8687impl 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,50 @@ 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+ #[ must_use]
262+ pub fn wait_for_packet ( & self ) -> & Event {
263+ & self . wait_for_packet
264+ }
265+
266+ /// Returns a reference to the Simple Network mode.
246267 #[ must_use]
247268 pub fn mode ( & self ) -> & NetworkMode {
248269 unsafe { & * self . mode }
249270 }
250271}
251272
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
273+ bitflags ! {
274+ /// Flags to pass to receive_filters to enable/disable reception of some kinds of packets.
275+ pub struct ReceiveFlags : u32 {
276+ /// Receive unicast packets.
277+ const UNICAST = 0x01 ;
278+ /// Receive multicast packets.
279+ const MULTICAST = 0x02 ;
280+ /// Receive broadcast packets.
281+ const BROADCAST = 0x04 ;
282+ /// Receive packets in promiscuous mode.
283+ const PROMISCUOUS = 0x08 ;
284+ /// Receive packets in promiscuous multicast mode.
285+ const PROMISCUOUS_MULTICAST = 0x10 ;
293286 }
294287}
295288
296- impl Default for InterruptStatus {
297- fn default ( ) -> Self {
298- Self :: new ( )
289+ bitflags ! {
290+ /// Flags returned by get_interrupt_status to indicate which interrupts have fired on the
291+ /// interace since the last call.
292+ pub struct InterruptStatus : u32 {
293+ /// Packet received.
294+ const RECEIVE = 0x01 ;
295+ /// Packet transmitted.
296+ const TRANSMIT = 0x02 ;
297+ /// Command interrupt fired.
298+ const COMMAND = 0x04 ;
299+ /// Software interrupt fired.
300+ const SOFTWARE = 0x08 ;
299301 }
300302}
301303
0 commit comments