Flattened Device Tree Python Module

0.3.3 · active · verified Fri Apr 17

The `fdt` Python library (version 0.3.3) provides utilities for parsing, manipulating, and generating Flattened Device Tree (FDT) structures. It supports both binary DTB and textual DTS formats, allowing programmatic interaction with device tree data commonly used in embedded systems. Releases are infrequent, focusing on bug fixes and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates loading Flattened Device Tree data from both a binary DTB blob and a DTS string. Note the requirement for the `dtc` utility for DTS parsing.

import os
from fdt import FDT

# Example 1: Parsing a binary DTB blob
# (Often read from a .dtb file)
dtb_blob = b'\xd0\x0d\xfe\xed\x00\x00\x00\x28\x00\x00\x00\x18\x00\x00\x00\x08\x00\x00\x00\x01\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00'

try:
    fdt_dtb = FDT(dtb_blob)
    print("\n--- Parsed DTB ---")
    print(fdt_dtb.get_dts()) # Convert to DTS string for display
except Exception as e:
    print(f"Error parsing DTB: {e}")

# Example 2: Parsing a DTS string
# Requires 'dtc' (Device Tree Compiler) to be installed on the system path.
# On Debian/Ubuntu: sudo apt install device-tree-compiler
dts_string = """
/dts-v1/;
/ {
    compatible = "example,device";
    prop1 = <0x12345678>;
    node1 {
        prop2 = "hello world";
    };
};
"""

try:
    fdt_dts = FDT.from_dts(dts_string)
    print("\n--- Parsed DTS ---")
    print(fdt_dts.get_dts()) # Display DTS string
    node = fdt_dts.get_node("/node1")
    if node:
        print(f"Value of prop2 in node1: {node.get_property('prop2').get_value()}")
except FileNotFoundError:
    print("\n--- WARNING: 'dtc' not found. Cannot parse DTS string. ---")
    print("Please install 'dtc' (Device Tree Compiler) on your system.")
except Exception as e:
    print(f"Error parsing DTS: {e}")

view raw JSON →