10
10
use super :: { IpAddress , MacAddress } ;
11
11
use crate :: data_types:: Event ;
12
12
use crate :: { Result , Status } ;
13
+ use bitflags:: bitflags;
13
14
use core:: ffi:: c_void;
14
15
use core:: ptr;
15
16
use core:: ptr:: NonNull ;
@@ -84,69 +85,70 @@ pub struct SimpleNetwork {
84
85
}
85
86
86
87
impl SimpleNetwork {
87
- /// Changes the state of a network from "Stopped" to "Started"
88
+ /// Change the state of a network from "Stopped" to "Started".
88
89
pub fn start ( & self ) -> Result {
89
90
( self . start ) ( self ) . into ( )
90
91
}
91
92
92
- /// Changes the state of a network interface from "Started" to "Stopped"
93
+ /// Change the state of a network interface from "Started" to "Stopped".
93
94
pub fn stop ( & self ) -> Result {
94
95
( self . stop ) ( self ) . into ( )
95
96
}
96
97
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.
100
101
pub fn initialize ( & self , extra_rx_buffer_size : usize , extra_tx_buffer_size : usize ) -> Result {
101
102
( self . initialize ) ( self , extra_rx_buffer_size, extra_tx_buffer_size) . into ( )
102
103
}
103
104
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`.
106
107
pub fn reset ( & self , extended_verification : bool ) -> Result {
107
108
( self . reset ) ( self , extended_verification) . into ( )
108
109
}
109
110
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
111
112
/// for another driver to initialize
112
113
pub fn shutdown ( & self ) -> Result {
113
114
( self . shutdown ) ( self ) . into ( )
114
115
}
115
116
116
- /// Manages the multicast receive filters of a network
117
+ /// Manage the multicast receive filters of a network.
117
118
pub fn receive_filters (
118
119
& self ,
119
- enable : u32 ,
120
- disable : u32 ,
120
+ enable : ReceiveFlags ,
121
+ disable : ReceiveFlags ,
121
122
reset_mcast_filter : bool ,
122
123
mcast_filter : Option < & [ MacAddress ] > ,
123
124
) -> Result {
124
125
if let Some ( mcast_filter) = mcast_filter {
125
126
( self . receive_filters ) (
126
127
self ,
127
- enable,
128
- disable,
128
+ enable. bits ,
129
+ disable. bits ,
129
130
reset_mcast_filter,
130
131
mcast_filter. len ( ) ,
131
132
NonNull :: new ( mcast_filter. as_ptr ( ) as * mut _ ) ,
132
133
)
133
134
. into ( )
134
135
} 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 ( )
136
138
}
137
139
}
138
140
139
- /// Modifies or resets the current station address, if supported
141
+ /// Modify or reset the current station address, if supported.
140
142
pub fn station_address ( & self , reset : bool , new : Option < & MacAddress > ) -> Result {
141
143
( self . station_address ) ( self , reset, new) . into ( )
142
144
}
143
145
144
- /// Resets statistics on a network interface
146
+ /// Reset statistics on a network interface.
145
147
pub fn reset_statistics ( & self ) -> Result {
146
148
( self . statistics ) ( self , true , None , None ) . into ( )
147
149
}
148
150
149
- /// Collects statistics on a network interface
151
+ /// Collect statistics on a network interface.
150
152
pub fn collect_statistics ( & self ) -> Result < NetworkStats > {
151
153
let mut stats_table: NetworkStats = Default :: default ( ) ;
152
154
let mut stats_size = core:: mem:: size_of :: < NetworkStats > ( ) ;
@@ -155,71 +157,81 @@ impl SimpleNetwork {
155
157
Ok ( stats_table)
156
158
}
157
159
158
- /// Converts a multicast IP address to a multicast HW MAC Address
160
+ /// Convert a multicast IP address to a multicast HW MAC Address.
159
161
pub fn mcast_ip_to_mac ( & self , ipv6 : bool , ip : IpAddress ) -> Result < MacAddress > {
160
162
let mut mac_address = MacAddress ( [ 0 ; 32 ] ) ;
161
163
let status = ( self . mcast_ip_to_mac ) ( self , ipv6, & ip, & mut mac_address) ;
162
164
Result :: from ( status) ?;
163
165
Ok ( mac_address)
164
166
}
165
167
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 ( )
170
179
}
171
180
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 ( )
175
191
}
176
192
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.
179
195
pub fn get_interrupt_status ( & self ) -> Result < InterruptStatus > {
180
- let mut interrupt_status = InterruptStatus :: new ( ) ;
196
+ let mut interrupt_status = InterruptStatus :: empty ( ) ;
181
197
let status = ( self . get_status ) ( self , Some ( & mut interrupt_status) , None ) ;
182
198
Result :: from ( status) ?;
183
199
Ok ( interrupt_status)
184
200
}
185
201
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 > > > {
189
205
let mut tx_buf: * mut c_void = ptr:: null_mut ( ) ;
190
206
let status = ( self . get_status ) ( self , None , Some ( & mut tx_buf) ) ;
191
207
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 ( ) ) )
197
209
}
198
210
199
- /// Places a packet in the transmit queue of a network interface
211
+ /// Place a packet in the transmit queue of a network interface.
200
212
pub fn transmit (
201
213
& self ,
202
214
header_size : usize ,
203
215
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 > ,
207
219
) -> Result {
208
220
( self . transmit ) (
209
221
self ,
210
222
header_size,
211
223
buffer. len ( ) + header_size,
212
224
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 ( ) ,
216
228
)
217
229
. into ( )
218
230
}
219
231
220
- /// Receives a packet from a network interface
232
+ /// Receive a packet from a network interface.
221
233
///
222
- /// On success, returns the size of bytes of the received packet
234
+ /// On success, returns the size of bytes of the received packet.
223
235
pub fn receive (
224
236
& self ,
225
237
buffer : & mut [ u8 ] ,
@@ -242,60 +254,50 @@ impl SimpleNetwork {
242
254
Ok ( buffer_size)
243
255
}
244
256
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.
246
267
#[ must_use]
247
268
pub fn mode ( & self ) -> & NetworkMode {
248
269
unsafe { & * self . mode }
249
270
}
250
271
}
251
272
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 ;
293
286
}
294
287
}
295
288
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 ;
299
301
}
300
302
}
301
303
0 commit comments