Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 21, 2025

This PR addresses the lack of Big Endian enforcement in the FDT builder by adding a new type-safe method for structured data while preserving backward compatibility.

Problem

The existing add_prop_array method accepts raw byte slices (&[&[u8]]) with only a comment that "The caller must ensure these are Big Endian slices" - there was no compile-time enforcement. This violated FDT specification requirements and led to error-prone manual byte conversion in tests:

// Before: Manual and error-prone BE conversion
let big_endians = val
    .iter()
    .map(|v| zerocopy::byteorder::U64::<zerocopy::byteorder::BigEndian>::new(*v))
    .collect::<Vec<_>>();
new_builder.add_prop_array(
    prop_name,
    big_endians.iter().map(|v| v.as_bytes()).collect::<Vec<_>>().as_slice(),
)

Solution

Added a new add_be_array<U> method that enforces Big Endian types at compile time using zerocopy traits:

// After: Type-safe and clean
let big_endians = val
    .iter()
    .map(|v| zerocopy::byteorder::U64::<zerocopy::byteorder::BigEndian>::new(*v))
    .collect::<Vec<_>>();
new_builder.add_be_array(prop_name, &big_endians)

Changes

  1. Added add_be_array<U> method: Accepts types that implement IntoBytes + Immutable, providing compile-time BE enforcement
  2. Updated documentation: Clarified when to use add_prop_array (raw bytes like entropy) vs add_be_array (structured data)
  3. Simplified test code: Updated parser tests to use the new type-safe method
  4. Comprehensive testing: Added tests demonstrating both functionality and type safety
  5. Backward compatibility: Preserved existing add_prop_array for legitimate raw byte use cases (empty arrays, entropy data)

Verification

  • All existing usage of add_prop_array remains appropriate (empty arrays and entropy data)
  • New method prevents accidental use of native-endian types at compile time
  • All tests pass including dependent modules (openhcl_boot, host_fdt_parser)

Fixes #777.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] fdt/builder: enforce BE datatypes on add_prop_array fdt/builder: enforce BE datatypes on add_prop_array with new add_be_array method Jun 21, 2025
Copilot AI requested a review from benhillis June 21, 2025 01:12
@benhillis benhillis closed this Nov 18, 2025
@benhillis benhillis deleted the copilot/fix-777 branch November 18, 2025 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fdt/builder: enforce BE datatypes on add_prop_array

2 participants