fdt-edit
A high-level Rust library for creating, editing, and encoding Flattened Device Tree (FDT) structures.
Overview
fdt-edit is a feature-rich device tree manipulation library built on top of fdt-raw. It provides comprehensive functionality for creating new device trees from scratch, modifying existing device trees, and encoding the edited device trees into standard DTB format.
Features
- Complete device tree editing: Full CRUD operations for nodes and properties
- Type-safe node operations: Specialized node types (clocks, memory, PCI, interrupt controllers, etc.)
- Efficient encoder: Converts in-memory device tree structures to standard DTB format
- phandle management: Automatic phandle allocation and reference management
- Memory reservation support: Complete memory reservation region operations
no_stdcompatible: Suitable for embedded environments
Core Components
Fdt Structure
An editable device tree container that:
- Parses from raw DTB data
- Creates new empty device trees
- Manages phandle cache
- Encodes to DTB format
Node System
Supports multiple specialized node types:
- Clock nodes: Clock sources and clock consumers
- Memory nodes: Memory region definitions
- PCI nodes: PCI buses and devices
- Interrupt controllers: Interrupt mapping and management
- Generic nodes: Customizable node types
Property System
- Type-safe properties: Support for various data types
- Automatic property management: Intelligent property CRUD operations
- Formatted display: Friendly node and property display
Quick Start
use Fdt;
// Parse existing DTB from bytes
let raw_data = include_bytes!;
let fdt = from_bytes?;
// Access nodes by path
let node = fdt.get_by_path;
if let Some = node
// Encode back to DTB format
let dtb_data = fdt.encode;
write?;
Node Traversal and Searching
use ;
let fdt = from_bytes?;
// Iterate through all nodes
for node in fdt.all_nodes
// Find nodes by path pattern
let virtio_nodes: = fdt.find_by_path.collect;
println!;
Node Modification and Creation
use ;
let mut fdt = from_bytes?;
// Create new node manually
let mut new_node = new;
// Add properties (API in development)
// new_node.add_property("compatible", &["vendor,test-device"]);
// new_node.add_property("reg", &[0x12340000u64, 0x1000u64]);
// Add to root node
fdt.root.add_child;
// Remove existing node
if fdt.get_by_path.is_some
// Save the modified device tree
let modified_dtb = fdt.encode;
write?;
Specialized Node Access
use ;
let fdt = from_bytes?;
// Find and work with memory nodes
for node in fdt.all_nodes
// Find clock nodes
let mut clock_count = 0;
for node in fdt.all_nodes
Display as Device Tree Source
use Fdt;
let fdt = from_bytes?;
// Display as DTS format (including memory reservations)
println!;
// Output will show:
// /dts-v1/;
// /memreserve/ 0x80000000 0x100000;
// / {
// #address-cells = <0x2>;
// #size-cells = <0x2>;
// compatible = "qemu,arm64";
// ...
// };
Current Status
This library is under active development. Currently supported features:
- ✅ Parse DTB files into editable structures
- ✅ Encode device trees back to DTB format
- ✅ Display device trees in DTS format
- ✅ Access to memory reservations
- 🚧 Node editing APIs (in development)
Dependencies
fdt-raw- Low-level FDT parsing librarylog = "0.4"- Logging supportenum_dispatch = "0.3.13"- Enum dispatch optimization
Dev Dependencies
dtb-file- Test dataenv_logger = "0.11"- Logger implementation
Testing
The library includes comprehensive tests that verify round-trip compatibility:
The main test (test_parse_and_rebuild) ensures that:
- A DTB file can be parsed successfully
- The parsed structure can be encoded back to DTB
- The original and rebuilt DTB files produce identical DTS output when using
dtc
License
This project is licensed under open source licenses. Please see the LICENSE file in the project root for specific license types.
Contributing
Issues and Pull Requests are welcome. Please ensure:
- Code follows the project's formatting standards (
cargo fmt) - All tests pass (
cargo test) - Clippy checks pass (
cargo clippy) - New features include appropriate test cases
Related Projects
- fdt-raw - Low-level FDT parsing library
- fdt-parser - High-level cached FDT parser
- dtb-tool - DTB file inspection tool
- dtb-file - Test data package
Examples
More usage examples can be found in the source code test files, particularly in tests/edit.rs.