{"id":9743,"library":"fdt","title":"Flattened Device Tree Python Module","description":"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.","status":"active","version":"0.3.3","language":"en","source_language":"en","source_url":"https://github.com/molejar/pyFDT","tags":["device tree","dtb","dts","parsing","embedded"],"install":[{"cmd":"pip install fdt","lang":"bash","label":"Install fdt"}],"dependencies":[{"reason":"Required system-wide for parsing DTS (Device Tree Source) files using `FDT.from_dts()`. This is typically installed via your system's package manager (e.g., `apt install device-tree-compiler` on Debian/Ubuntu).","package":"dtc (Device Tree Compiler)","optional":true}],"imports":[{"symbol":"FDT","correct":"from fdt import FDT"}],"quickstart":{"code":"import os\nfrom fdt import FDT\n\n# Example 1: Parsing a binary DTB blob\n# (Often read from a .dtb file)\ndtb_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'\n\ntry:\n    fdt_dtb = FDT(dtb_blob)\n    print(\"\\n--- Parsed DTB ---\")\n    print(fdt_dtb.get_dts()) # Convert to DTS string for display\nexcept Exception as e:\n    print(f\"Error parsing DTB: {e}\")\n\n# Example 2: Parsing a DTS string\n# Requires 'dtc' (Device Tree Compiler) to be installed on the system path.\n# On Debian/Ubuntu: sudo apt install device-tree-compiler\ndts_string = \"\"\"\n/dts-v1/;\n/ {\n    compatible = \"example,device\";\n    prop1 = <0x12345678>;\n    node1 {\n        prop2 = \"hello world\";\n    };\n};\n\"\"\"\n\ntry:\n    fdt_dts = FDT.from_dts(dts_string)\n    print(\"\\n--- Parsed DTS ---\")\n    print(fdt_dts.get_dts()) # Display DTS string\n    node = fdt_dts.get_node(\"/node1\")\n    if node:\n        print(f\"Value of prop2 in node1: {node.get_property('prop2').get_value()}\")\nexcept FileNotFoundError:\n    print(\"\\n--- WARNING: 'dtc' not found. Cannot parse DTS string. ---\")\n    print(\"Please install 'dtc' (Device Tree Compiler) on your system.\")\nexcept Exception as e:\n    print(f\"Error parsing DTS: {e}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Install `dtc` via your system's package manager (e.g., `sudo apt install device-tree-compiler` on Debian/Ubuntu, or check your distribution's documentation).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you use `FDT(binary_dtb_data)` for DTB and `FDT.from_dts(dts_string_data)` for DTS. Do not pass DTS strings directly to the `FDT` constructor.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install `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.","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.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'dtc'"},{"fix":"Ensure 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).","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.","error":"fdt.FdtException: Error parsing DTB data: 'Invalid FDT magic'"},{"fix":"Carefully 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`.","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.","error":"fdt.FdtException: Error parsing DTS data: 'Command '['dtc', '-I', 'dts', '-O', 'dtb', '-o', ..."}]}