@@ -13,7 +13,7 @@ use bitflags::bitflags;
1313use core:: convert:: TryInto ;
1414
1515// 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
1717// enumerate the selectors.
1818#[ allow( non_snake_case) ]
1919pub mod FwCfgSelector {
@@ -174,12 +174,14 @@ impl QemuFwCfgBuilder {
174174 }
175175
176176 fn next_file_selector ( & self ) -> u16 {
177- //TODO: this should only consider keys below 0x8000
178177 self . data
179178 . keys ( )
180179 . copied ( )
180+ . filter ( |& s| {
181+ s >= FwCfgSelector :: FILE_FIRST && s <= FwCfgSelector :: FILE_LAST
182+ } )
181183 . max ( )
182- . unwrap_or ( FwCfgSelector :: FILE_FIRST )
184+ . unwrap_or ( FwCfgSelector :: FILE_FIRST - 1 )
183185 + 1
184186 }
185187
@@ -396,3 +398,25 @@ impl EmulatedDevice for QemuFwCfg {
396398 Ok ( ( ) )
397399 }
398400}
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