@@ -13,7 +13,7 @@ use bitflags::bitflags;
13
13
use core:: convert:: TryInto ;
14
14
15
15
// This is _almost_ an enum, but there are 'file' selectors
16
- // between 0x20 and 0x7fff that make it impractical to actually
16
+ // between 0x20 and 0x7fff inclusive that make it impractical to actually
17
17
// enumerate the selectors.
18
18
#[ allow( non_snake_case) ]
19
19
pub mod FwCfgSelector {
@@ -174,12 +174,14 @@ impl QemuFwCfgBuilder {
174
174
}
175
175
176
176
fn next_file_selector ( & self ) -> u16 {
177
- //TODO: this should only consider keys below 0x8000
178
177
self . data
179
178
. keys ( )
180
179
. copied ( )
180
+ . filter ( |& s| {
181
+ s >= FwCfgSelector :: FILE_FIRST && s <= FwCfgSelector :: FILE_LAST
182
+ } )
181
183
. max ( )
182
- . unwrap_or ( FwCfgSelector :: FILE_FIRST )
184
+ . unwrap_or ( FwCfgSelector :: FILE_FIRST - 1 )
183
185
+ 1
184
186
}
185
187
@@ -396,3 +398,25 @@ impl EmulatedDevice for QemuFwCfg {
396
398
Ok ( ( ) )
397
399
}
398
400
}
401
+
402
+ #[ cfg( test) ]
403
+ mod test {
404
+ use super :: * ;
405
+
406
+ #[ test]
407
+ fn test_next_file_selector_first ( ) {
408
+ let builder = QemuFwCfgBuilder :: new ( ) ;
409
+ let selector = builder. next_file_selector ( ) ;
410
+ assert ! ( selector >= FwCfgSelector :: FILE_FIRST ) ;
411
+ assert ! ( selector <= FwCfgSelector :: FILE_LAST ) ;
412
+ }
413
+
414
+ #[ test]
415
+ fn test_next_file_selector_last ( ) {
416
+ let mut builder = QemuFwCfgBuilder :: new ( ) ;
417
+ builder. add_i32 ( FwCfgSelector :: FILE_LAST + 1 , 0x0 ) ;
418
+ let selector = builder. next_file_selector ( ) ;
419
+ assert ! ( selector >= FwCfgSelector :: FILE_FIRST ) ;
420
+ assert ! ( selector <= FwCfgSelector :: FILE_LAST ) ;
421
+ }
422
+ }
0 commit comments