Flattened Device Tree Python Module
pip install fdtFlattened Device Tree Python Module
This python module is usable for manipulation with Device Tree Data and primary was created for imxsb tool
Some parts in this module have been inspired from: https://github.com/superna9999/pyfdt project.
Installation
pip install fdt
To install the latest version from master branch execute in shell following command:
pip install -U https://github.com/molejar/pyFDT/archive/master.zip
In case of development, install it from cloned sources:
git clone https://github.com/molejar/pyFDT.git
cd pyFDT
pip install -U -e .
NOTE: You may run into a permissions issues running these commands. Here are a few options how to fix it:
- Run with
sudoto installfdtand dependencies globally - Specify the
--useroption to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't). - Run the command in a virtualenv local to a specific project working set.
Usage
fdt module has intuitive and self describing API, what is presented in following example. Many of general requirements for manipulation with FDT Nodes, Properties and dts/dtb files are already implemented.
import fdt
#-----------------------------------------------
# convert *.dtb to *.dts
# ----------------------------------------------
with open("example.dtb", "rb") as f:
dtb_data = f.read()
dt1 = fdt.parse_dtb(dtb_data)
with open("example.dts", "w") as f:
f.write(dt1.to_dts())
#-----------------------------------------------
# convert *.dts to *.dtb
# ----------------------------------------------
with open("example.dts", "r") as f:
dts_text = f.read()
dt2 = fdt.parse_dts(dts_text)
with open("example.dtb", "wb") as f:
f.write(dt2.to_dtb(version=17))
#-----------------------------------------------
# add new Node into dt2
# ----------------------------------------------
# create node instance
node = fdt.Node('test_node1')
# add properties
node.append(fdt.Property('basic_property'))
node.append(fdt.PropStrings('string_property', 'value1', 'value2'))
node.append(fdt.PropWords('words_property', 0x80000000))
node.append(fdt.PropBytes('bytes_property', 0x00, 0x01, 0x02))
# PropBytes constructor take also complex data object as bytes() or bytearray()
node.append(fdt.PropBytes('bytes_property2', data=b"\x00\x01\x02"))
# add created node into root path of dt2
dt2.add_item(node)
# use set_property method to update or create new property
dt2.set_property('words_property', [0, 1], path='/test_node1')
dt2.set_property('bytes_property', b"\x00\x01", path='/test_node1')
dt2.set_property('string_property', ['value1', 'value2', 'value3'], path='/test_node1')
# use search method for find all string properties and then update it
items = dt2.search("", itype=fdt.ItemType.PROP_STRINGS, path="/test_node1")
for item in items:
item.data = ['value1', 'value2']
#-----------------------------------------------
# merge dt2 into dt1
# ----------------------------------------------
dt1.merge(dt2)
with open("merged.dtb", "wb") as f:
f.write(dt1.to_dtb(version=17))
#-----------------------------------------------
# diff two fdt objects
# ----------------------------------------------
out = fdt.diff(dt1, dt2)
print(out[0]) # same in dt1 and dt2
print(out[1]) # specific for dt1
print(out[2]) # specific for dt2