66
77use alloc:: collections:: BTreeMap ;
88use alloc:: ffi:: CString ;
9- use alloc:: string:: String ;
109use alloc:: vec:: Vec ;
1110use core:: cmp:: { Ord , Ordering } ;
1211use core:: convert:: TryInto ;
@@ -452,7 +451,11 @@ impl FdtWriter {
452451 }
453452
454453 /// Write a stringlist property.
455- pub fn property_string_list ( & mut self , name : & str , values : Vec < String > ) -> Result < ( ) > {
454+ pub fn property_string_list < Iter , T > ( & mut self , name : & str , values : Iter ) -> Result < ( ) >
455+ where
456+ Iter : IntoIterator < Item = T > ,
457+ T : Into < Vec < u8 > > ,
458+ {
456459 let mut bytes = Vec :: new ( ) ;
457460 for s in values {
458461 let cstr = CString :: new ( s) . map_err ( |_| Error :: InvalidString ) ?;
@@ -461,6 +464,18 @@ impl FdtWriter {
461464 self . property ( name, & bytes)
462465 }
463466
467+ /// Write a stringlist property.
468+ pub fn property_cstring_list < Iter > ( & mut self , name : & str , values : Iter ) -> Result < ( ) >
469+ where
470+ Iter : IntoIterator < Item = CString > ,
471+ {
472+ let mut bytes = Vec :: new ( ) ;
473+ for cstr in values {
474+ bytes. extend_from_slice ( cstr. to_bytes_with_nul ( ) ) ;
475+ }
476+ self . property ( name, & bytes)
477+ }
478+
464479 /// Write a 32-bit unsigned integer property.
465480 pub fn property_u32 ( & mut self , name : & str , val : u32 ) -> Result < ( ) > {
466481 self . property ( name, & val. to_be_bytes ( ) )
@@ -706,7 +721,9 @@ mod tests {
706721 fdt. property_u32 ( "u32" , 0x12345678 ) . unwrap ( ) ;
707722 fdt. property_u64 ( "u64" , 0x1234567887654321 ) . unwrap ( ) ;
708723 fdt. property_string ( "str" , "hello" ) . unwrap ( ) ;
709- fdt. property_string_list ( "strlst" , vec ! [ "hi" . into( ) , "bye" . into( ) ] )
724+ fdt. property_string_list ( "strlst" , vec ! [ "hi" , "bye" ] )
725+ . unwrap ( ) ;
726+ fdt. property_cstring_list ( "strlst2" , vec ! [ CString :: new( "hi" ) . unwrap( ) , CString :: new( "bye" ) . unwrap( ) ] )
710727 . unwrap ( ) ;
711728 fdt. property_array_u32 ( "arru32" , & [ 0x12345678 , 0xAABBCCDD ] )
712729 . unwrap ( ) ;
@@ -716,14 +733,14 @@ mod tests {
716733 let actual_fdt = fdt. finish ( ) . unwrap ( ) ;
717734 let expected_fdt = vec ! [
718735 0xd0 , 0x0d , 0xfe , 0xed , // 0000: magic (0xd00dfeed)
719- 0x00 , 0x00 , 0x00 , 0xee , // 0004: totalsize (0xEE )
736+ 0x00 , 0x00 , 0x01 , 0x0a , // 0004: totalsize (0x10A )
720737 0x00 , 0x00 , 0x00 , 0x38 , // 0008: off_dt_struct (0x38)
721- 0x00 , 0x00 , 0x00 , 0xc8 , // 000C: off_dt_strings (0xC8 )
738+ 0x00 , 0x00 , 0x00 , 0xdc , // 000C: off_dt_strings (0xDC )
722739 0x00 , 0x00 , 0x00 , 0x28 , // 0010: off_mem_rsvmap (0x28)
723740 0x00 , 0x00 , 0x00 , 0x11 , // 0014: version (0x11 = 17)
724741 0x00 , 0x00 , 0x00 , 0x10 , // 0018: last_comp_version (0x10 = 16)
725742 0x00 , 0x00 , 0x00 , 0x00 , // 001C: boot_cpuid_phys (0)
726- 0x00 , 0x00 , 0x00 , 0x26 , // 0020: size_dt_strings (0x26 )
743+ 0x00 , 0x00 , 0x00 , 0x2e , // 0020: size_dt_strings (0x2E )
727744 0x00 , 0x00 , 0x00 , 0x90 , // 0024: size_dt_struct (0x90)
728745 0x00 , 0x00 , 0x00 , 0x00 , // 0028: rsvmap terminator (address = 0 high)
729746 0x00 , 0x00 , 0x00 , 0x00 , // 002C: rsvmap terminator (address = 0 low)
@@ -956,7 +973,7 @@ mod tests {
956973 #[ test]
957974 fn invalid_prop_string_list_value_nul ( ) {
958975 let mut fdt = FdtWriter :: new ( ) . unwrap ( ) ;
959- let strs = vec ! [ "test" . into ( ) , "abc\0 def" . into ( ) ] ;
976+ let strs = vec ! [ "test" , "abc\0 def" ] ;
960977 assert_eq ! (
961978 fdt. property_string_list( "mystr" , strs) . unwrap_err( ) ,
962979 Error :: InvalidString
0 commit comments