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