Flattened Device Tree Python Module
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
-
FileNotFoundError: [Errno 2] No such file or directory: 'dtc'
cause You are attempting to parse a DTS string using `FDT.from_dts()`, but the `dtc` (Device Tree Compiler) utility is not installed or not found in your system's PATH.fixInstall `dtc` on your operating system. For Debian/Ubuntu, use `sudo apt install device-tree-compiler`. For other systems, consult your distribution's package manager or the official `dtc` documentation. -
fdt.FdtException: Error parsing DTB data: 'Invalid FDT magic'
cause The input provided to the `FDT()` constructor is not valid binary Flattened Device Tree (DTB) data. This often happens if you pass a DTS string, malformed data, or an incorrect file format.fixEnsure the input to `FDT()` is valid binary DTB `bytes` data. If you intend to parse a DTS string, use `FDT.from_dts(your_dts_string)` instead (and ensure `dtc` is installed). -
fdt.FdtException: Error parsing DTS data: 'Command '['dtc', '-I', 'dts', '-O', 'dtb', '-o', ...
cause While `dtc` was found, it failed to parse the provided DTS string. This indicates a syntax error or invalid structure within your DTS input.fixCarefully review your DTS string for syntax errors, missing semicolons, incorrect property formats, or malformed nodes. You can try running `dtc -I dts -O dtb -o /dev/null your_file.dts` directly in your terminal to get more specific error messages from `dtc`.
Warnings
- gotcha Parsing DTS (Device Tree Source) strings requires the `dtc` (Device Tree Compiler) utility to be installed on your system and available in the PATH. The `fdt` library executes `dtc` as a subprocess for this conversion.
- gotcha The `FDT` constructor directly accepts binary DTB data (`bytes`), while `FDT.from_dts()` is a class method specifically for parsing DTS string data (`str`). Mixing these can lead to parsing errors.
Install
-
pip install fdt
Imports
- FDT
from fdt import FDT
Quickstart
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}")