|
6 | 6 |
|
7 | 7 | use alloc::collections::BTreeMap; |
8 | 8 | use alloc::ffi::CString; |
9 | | -use alloc::string::String; |
10 | 9 | use alloc::vec::Vec; |
11 | 10 | use core::cmp::{Ord, Ordering}; |
12 | 11 | use core::convert::TryInto; |
@@ -446,16 +445,20 @@ impl FdtWriter { |
446 | 445 | } |
447 | 446 |
|
448 | 447 | /// Write a string property. |
449 | | - pub fn property_string(&mut self, name: &str, val: &str) -> Result<()> { |
450 | | - let cstr_value = CString::new(val).map_err(|_| Error::InvalidString)?; |
| 448 | + pub fn property_string<S: AsRef<str>>(&mut self, name: &str, val: S) -> Result<()> { |
| 449 | + let cstr_value = CString::new(val.as_ref()).map_err(|_| Error::InvalidString)?; |
451 | 450 | self.property(name, cstr_value.to_bytes_with_nul()) |
452 | 451 | } |
453 | 452 |
|
454 | 453 | /// Write a stringlist property. |
455 | | - pub fn property_string_list(&mut self, name: &str, values: Vec<String>) -> Result<()> { |
| 454 | + pub fn property_string_list<S: AsRef<str>>( |
| 455 | + &mut self, |
| 456 | + name: &str, |
| 457 | + values: Vec<S>, |
| 458 | + ) -> Result<()> { |
456 | 459 | let mut bytes = Vec::new(); |
457 | 460 | for s in values { |
458 | | - let cstr = CString::new(s).map_err(|_| Error::InvalidString)?; |
| 461 | + let cstr = CString::new(s.as_ref()).map_err(|_| Error::InvalidString)?; |
459 | 462 | bytes.extend_from_slice(cstr.to_bytes_with_nul()); |
460 | 463 | } |
461 | 464 | self.property(name, &bytes) |
@@ -706,25 +709,27 @@ mod tests { |
706 | 709 | fdt.property_u32("u32", 0x12345678).unwrap(); |
707 | 710 | fdt.property_u64("u64", 0x1234567887654321).unwrap(); |
708 | 711 | fdt.property_string("str", "hello").unwrap(); |
709 | | - fdt.property_string_list("strlst", vec!["hi".into(), "bye".into()]) |
| 712 | + fdt.property_string_list("strlst", vec!["hi", "bye"]) |
710 | 713 | .unwrap(); |
711 | 714 | fdt.property_array_u32("arru32", &[0x12345678, 0xAABBCCDD]) |
712 | 715 | .unwrap(); |
713 | 716 | fdt.property_array_u64("arru64", &[0x1234567887654321]) |
714 | 717 | .unwrap(); |
| 718 | + fdt.property_string_list("stringlst", vec![String::from("hi"), String::from("bye")]) |
| 719 | + .unwrap(); |
715 | 720 | fdt.end_node(root_node).unwrap(); |
716 | 721 | let actual_fdt = fdt.finish().unwrap(); |
717 | 722 | let expected_fdt = vec![ |
718 | 723 | 0xd0, 0x0d, 0xfe, 0xed, // 0000: magic (0xd00dfeed) |
719 | | - 0x00, 0x00, 0x00, 0xee, // 0004: totalsize (0xEE) |
| 724 | + 0x00, 0x00, 0x01, 0x0c, // 0004: totalsize (0x10C) |
720 | 725 | 0x00, 0x00, 0x00, 0x38, // 0008: off_dt_struct (0x38) |
721 | | - 0x00, 0x00, 0x00, 0xc8, // 000C: off_dt_strings (0xC8) |
| 726 | + 0x00, 0x00, 0x00, 0xdc, // 000C: off_dt_strings (0xDC) |
722 | 727 | 0x00, 0x00, 0x00, 0x28, // 0010: off_mem_rsvmap (0x28) |
723 | 728 | 0x00, 0x00, 0x00, 0x11, // 0014: version (0x11 = 17) |
724 | 729 | 0x00, 0x00, 0x00, 0x10, // 0018: last_comp_version (0x10 = 16) |
725 | 730 | 0x00, 0x00, 0x00, 0x00, // 001C: boot_cpuid_phys (0) |
726 | | - 0x00, 0x00, 0x00, 0x26, // 0020: size_dt_strings (0x26) |
727 | | - 0x00, 0x00, 0x00, 0x90, // 0024: size_dt_struct (0x90) |
| 731 | + 0x00, 0x00, 0x00, 0x30, // 0020: size_dt_strings (0x30) |
| 732 | + 0x00, 0x00, 0x00, 0xa4, // 0024: size_dt_struct (0xA4) |
728 | 733 | 0x00, 0x00, 0x00, 0x00, // 0028: rsvmap terminator (address = 0 high) |
729 | 734 | 0x00, 0x00, 0x00, 0x00, // 002C: rsvmap terminator (address = 0 low) |
730 | 735 | 0x00, 0x00, 0x00, 0x00, // 0030: rsvmap terminator (size = 0 high) |
@@ -763,15 +768,22 @@ mod tests { |
763 | 768 | 0x00, 0x00, 0x00, 0x1f, // 00B4: prop nameoff (0x1F) |
764 | 769 | 0x12, 0x34, 0x56, 0x78, // 00B8: prop u64 value 0 high |
765 | 770 | 0x87, 0x65, 0x43, 0x21, // 00BC: prop u64 value 0 low |
766 | | - 0x00, 0x00, 0x00, 0x02, // 00C0: FDT_END_NODE |
767 | | - 0x00, 0x00, 0x00, 0x09, // 00C4: FDT_END |
768 | | - b'n', b'u', b'l', b'l', 0x00, // 00C8: strings + 0x00: "null"" |
769 | | - b'u', b'3', b'2', 0x00, // 00CD: strings + 0x05: "u32" |
770 | | - b'u', b'6', b'4', 0x00, // 00D1: strings + 0x09: "u64" |
771 | | - b's', b't', b'r', 0x00, // 00D5: strings + 0x0D: "str" |
772 | | - b's', b't', b'r', b'l', b's', b't', 0x00, // 00D9: strings + 0x11: "strlst" |
773 | | - b'a', b'r', b'r', b'u', b'3', b'2', 0x00, // 00E0: strings + 0x18: "arru32" |
774 | | - b'a', b'r', b'r', b'u', b'6', b'4', 0x00, // 00E7: strings + 0x1F: "arru64" |
| 771 | + 0x00, 0x00, 0x00, 0x03, // 00C0: FDT_PROP (string) |
| 772 | + 0x00, 0x00, 0x00, 0x07, // 00C4: prop len (7) |
| 773 | + 0x00, 0x00, 0x00, 0x26, // 00C8: prop nameoff (0x0D) |
| 774 | + b'h', b'i', 0x00, b'b', // 00CC: prop value ("hi", "bye") |
| 775 | + b'y', b'e', 0x00, 0x00, // 00D0: "ye\0" + padding |
| 776 | + 0x00, 0x00, 0x00, 0x02, // 00D4: FDT_END_NODE |
| 777 | + 0x00, 0x00, 0x00, 0x09, // 00D8: FDT_END |
| 778 | + b'n', b'u', b'l', b'l', 0x00, // 00DC: strings + 0x00: "null"" |
| 779 | + b'u', b'3', b'2', 0x00, // 00E1: strings + 0x05: "u32" |
| 780 | + b'u', b'6', b'4', 0x00, // 00E5: strings + 0x09: "u64" |
| 781 | + b's', b't', b'r', 0x00, // 00E9: strings + 0x0D: "str" |
| 782 | + b's', b't', b'r', b'l', b's', b't', 0x00, // 00ED: strings + 0x11: "strlst" |
| 783 | + b'a', b'r', b'r', b'u', b'3', b'2', 0x00, // 00F4: strings + 0x18: "arru32" |
| 784 | + b'a', b'r', b'r', b'u', b'6', b'4', 0x00, // 00FB: strings + 0x1F: "arru64" |
| 785 | + b's', b't', b'r', b'i', b'n', b'g', // 0102: strings + 0x26: "string" |
| 786 | + b'l', b's', b't', 0x00, // 0108: strings + 0x2c: "lst\0" |
775 | 787 | ]; |
776 | 788 | assert_eq!(expected_fdt, actual_fdt); |
777 | 789 | } |
@@ -951,12 +963,22 @@ mod tests { |
951 | 963 | fdt.property_string("mystr", "abc\0def").unwrap_err(), |
952 | 964 | Error::InvalidString |
953 | 965 | ); |
| 966 | + assert_eq!( |
| 967 | + fdt.property_string("mystr", String::from("abc\0def")) |
| 968 | + .unwrap_err(), |
| 969 | + Error::InvalidString |
| 970 | + ); |
954 | 971 | } |
955 | 972 |
|
956 | 973 | #[test] |
957 | 974 | fn invalid_prop_string_list_value_nul() { |
958 | 975 | let mut fdt = FdtWriter::new().unwrap(); |
959 | | - let strs = vec!["test".into(), "abc\0def".into()]; |
| 976 | + let strs = vec!["test", "abc\0def"]; |
| 977 | + assert_eq!( |
| 978 | + fdt.property_string_list("mystr", strs).unwrap_err(), |
| 979 | + Error::InvalidString |
| 980 | + ); |
| 981 | + let strs = vec![String::from("test"), String::from("abc\0def")]; |
960 | 982 | assert_eq!( |
961 | 983 | fdt.property_string_list("mystr", strs).unwrap_err(), |
962 | 984 | Error::InvalidString |
|
0 commit comments