#devices #tree #tree-node #fdt #dtb #no-std

no-std fdt-rs

A flattened device tree parser for embedded no-std environments

9 releases

0.4.5 Feb 25, 2024
0.4.4 Feb 24, 2024
0.4.3 Jul 6, 2021
0.4.2 Aug 15, 2020
0.1.1 May 28, 2020

#156 in Embedded development

Download history 323/week @ 2024-01-01 117/week @ 2024-01-08 352/week @ 2024-01-15 205/week @ 2024-01-22 111/week @ 2024-01-29 47/week @ 2024-02-05 54/week @ 2024-02-12 452/week @ 2024-02-19 441/week @ 2024-02-26 457/week @ 2024-03-04 424/week @ 2024-03-11 616/week @ 2024-03-18 591/week @ 2024-03-25 331/week @ 2024-04-01 623/week @ 2024-04-08 233/week @ 2024-04-15

1,843 downloads per month
Used in fdtdump

MIT license

77KB
1.5K SLoC

fdt-rs

crates.io downloads docs.rs master coveralls.io

A Flattened Device Tree parser for embedded no-std environments

Usage

Add this to your Cargo.toml:

[dependencies.fdt-rs]
version = "0.4"

and this to your crate root:

extern crate fdt_rs;

Features

This crate can be used without the standard library (#![no_std]) by disabling the default std feature. Use this in Cargo.toml:

[dependencies.fdt-rs]
version = "0.4"
default-features = false

Example

The following example stashes a flattened device tree in memory, parses that device tree into a fdt_rs::DevTree object, searches the device tree for "ns16550a" compatible nodes, and (if found) prints each nodes' name.

extern crate fdt_rs;
use fdt_rs::prelude::*;
use fdt_rs::base::*;

// Place a device tree image into the rust binary and
// align it to a 32-byte boundary by using a wrapper struct.
#[repr(align(4))] struct _Wrapper<T>(T);
pub const FDT: &[u8] = &_Wrapper(*include_bytes!("../tests/riscv64-virt.dtb")).0;

fn main() {
    // Initialize the devtree using an &[u8] array.
    let devtree = unsafe {

        // Get the actual size of the device tree after reading its header.
        let size = DevTree::read_totalsize(FDT).unwrap();
        let buf = &FDT[..size];

        // Create the device tree handle
        DevTree::new(buf).unwrap()
    };

    // Iterate through all "ns16550a" compatible nodes within the device tree.
    // If found, print the name of each node (including unit address).
    let mut node_iter = devtree.compatible_nodes("ns16550a");
    while let Some(node) = node_iter.next().unwrap() {
        println!("{}", node.name().unwrap());
    }
}

Also check out fdtdump for an example implementation of the fdtdump Device Tree utility using this library.

Dependencies

~1.5MB
~40K SLoC