Skip to content

fdt/builder: enforce BE datatypes on add_prop_array with new add_be_array method #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 119 additions & 2 deletions support/fdt/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,25 @@ impl<'a, T> Builder<'a, Nest<T>> {
Ok(self)
}

/// Adds an array of properties. The caller must ensure these are Big Endian
/// slices.
/// Adds an array of raw byte properties. Use this for unstructured byte data
/// such as entropy or empty arrays. For structured data that needs proper
/// byte ordering, prefer `add_be_array`, `add_u32_array`, or `add_u64_array`.
pub fn add_prop_array(mut self, name: StringId, data_array: &[&[u8]]) -> Result<Self, Error> {
self.inner.prop_array_iter(name, data_array.iter())?;
Ok(self)
}

/// Adds an array of Big Endian typed properties. This method ensures that
/// the data is properly byte-ordered for FDT requirements. Use this for
/// structured data types that implement `IntoBytes` and `Immutable`.
pub fn add_be_array<U>(mut self, name: StringId, data_array: &[U]) -> Result<Self, Error>
where
U: IntoBytes + zerocopy::Immutable,
{
self.inner.prop_array_iter(name, data_array.iter().map(|item| item.as_bytes()))?;
Ok(self)
}

/// Adds a string property.
pub fn add_str(mut self, name: StringId, data: &str) -> Result<Self, Error> {
self.inner
Expand Down Expand Up @@ -425,6 +437,9 @@ impl<'a> Iterator for StringBytesWithZeroIter<'a> {

#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
use super::*;

fn create_entry(address: u64, size: u64) -> spec::ReserveEntry {
Expand Down Expand Up @@ -492,4 +507,106 @@ mod tests {
Ok(())
);
}

#[test]
fn test_add_be_array() {
use zerocopy::byteorder::BigEndian;
use zerocopy::U32;

let mut buf = vec![0; 1024];
let mut builder = Builder::new(BuilderConfig {
blob_buffer: buf.as_mut_slice(),
string_table_cap: 128,
memory_reservations: &[],
}).unwrap();

let prop_name = builder.add_string("test-prop").unwrap();

// Test with BE u32 values
let be_values = [
U32::<BigEndian>::new(0x12345678),
U32::<BigEndian>::new(0xDEADBEEF),
];

let result = builder
.start_node("test")
.unwrap()
.add_be_array(prop_name, &be_values)
.unwrap()
.end_node()
.unwrap()
.build(0);

assert!(result.is_ok());
}

#[test]
fn test_add_be_array_with_u64() {
use zerocopy::byteorder::BigEndian;
use zerocopy::U64;

let mut buf = vec![0; 1024];
let mut builder = Builder::new(BuilderConfig {
blob_buffer: buf.as_mut_slice(),
string_table_cap: 128,
memory_reservations: &[],
}).unwrap();

let prop_name = builder.add_string("test-prop-u64").unwrap();

// Test with BE u64 values - similar to what the parser test was doing
let native_values = [0x1122334455667788u64, 0xAABBCCDDEEFF0011u64];
let be_values: Vec<U64<BigEndian>> = native_values
.iter()
.map(|v| U64::<BigEndian>::new(*v))
.collect();

let result = builder
.start_node("test")
.unwrap()
.add_be_array(prop_name, &be_values)
.unwrap()
.end_node()
.unwrap()
.build(0);

assert!(result.is_ok());
}

// This test demonstrates that add_be_array enforces type safety.
// Uncommenting the code below would cause a compilation error,
// which is exactly the behavior we want.
#[test]
fn test_add_be_array_type_safety_documentation() {
// The following would NOT compile, demonstrating type safety:
//
// let native_u32s = [0x12345678u32, 0xDEADBEEF];
// builder.add_be_array(prop_name, &native_u32s); // ERROR: u32 doesn't implement required traits
//
// Instead, you must use BE types:
// let be_u32s = [U32::<BigEndian>::new(0x12345678), U32::<BigEndian>::new(0xDEADBEEF)];
// builder.add_be_array(prop_name, &be_u32s); // OK

// This test just verifies that empty arrays work fine
let mut buf = vec![0; 1024];
let mut builder = Builder::new(BuilderConfig {
blob_buffer: buf.as_mut_slice(),
string_table_cap: 128,
memory_reservations: &[],
}).unwrap();

let prop_name = builder.add_string("empty-prop").unwrap();
let empty_array: &[u32] = &[];

let result = builder
.start_node("test")
.unwrap()
.add_be_array(prop_name, empty_array)
.unwrap()
.end_node()
.unwrap()
.build(0);

assert!(result.is_ok());
}
}
10 changes: 1 addition & 9 deletions support/fdt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ mod test {
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use zerocopy::IntoBytes;

#[derive(Debug, Clone, PartialEq, Eq)]
enum DtProp {
Expand Down Expand Up @@ -825,14 +824,7 @@ mod test {
.collect::<Vec<_>>();

new_builder
.add_prop_array(
$ids.proplist,
big_endians
.iter()
.map(|v| v.as_bytes())
.collect::<Vec<_>>()
.as_slice(),
)
.add_be_array($ids.proplist, &big_endians)
.unwrap()
}
};
Expand Down