xref: /aosp_15_r20/external/dtc/README.md (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1# Device Tree Compiler and libfdt
2
3The source tree contains the Device Tree Compiler (dtc) toolchain for
4working with device tree source and binary files and also libfdt, a
5utility library for reading and manipulating the binary format.
6
7dtc and libfdt are maintained by:
8
9* [David Gibson `<[email protected]>`](mailto:[email protected])
10
11## Python library
12
13A Python library wrapping libfdt is also available. To build this you
14will need to install `swig` and Python development files. On Debian
15distributions:
16
17```
18$ sudo apt-get install swig python3-dev
19```
20
21The library provides an `Fdt` class which you can use like this:
22
23```
24$ PYTHONPATH=../pylibfdt python3
25>>> import libfdt
26>>> fdt = libfdt.Fdt(open('test_tree1.dtb', mode='rb').read())
27>>> node = fdt.path_offset('/subnode@1')
28>>> print(node)
29124
30>>> prop_offset = fdt.first_property_offset(node)
31>>> prop = fdt.get_property_by_offset(prop_offset)
32>>> print('%s=%s' % (prop.name, prop.as_str()))
33compatible=subnode1
34>>> node2 = fdt.path_offset('/')
35>>> print(fdt.getprop(node2, 'compatible').as_str())
36test_tree1
37```
38
39You will find tests in `tests/pylibfdt_tests.py` showing how to use each
40method. Help is available using the Python help command, e.g.:
41
42```
43$ cd pylibfdt
44$ python3 -c "import libfdt; help(libfdt)"
45```
46
47If you add new features, please check code coverage:
48
49```
50$ sudo apt-get install python3-coverage
51$ cd tests
52# It's just 'coverage' on most other distributions
53$ python3-coverage run pylibfdt_tests.py
54$ python3-coverage html
55# Open 'htmlcov/index.html' in your browser
56```
57
58The library can be installed with pip from a local source tree:
59
60```
61$ pip install . [--user|--prefix=/path/to/install_dir]
62```
63
64Or directly from a remote git repo:
65
66```
67$ pip install git+git://git.kernel.org/pub/scm/utils/dtc/dtc.git@main
68```
69
70The install depends on libfdt shared library being installed on the
71host system first. Generally, using `--user` or `--prefix` is not
72necessary and pip will use the default location for the Python
73installation which varies if the user is root or not.
74
75You can also install everything via make if you like, but pip is
76recommended.
77
78To install both libfdt and pylibfdt you can use:
79
80```
81$ make install [PREFIX=/path/to/install_dir]
82```
83
84To disable building the python library, even if swig and Python are available,
85use:
86
87```
88$ make NO_PYTHON=1
89```
90
91More work remains to support all of libfdt, including access to numeric
92values.
93
94## Mailing lists
95
96* The [devicetree-compiler](mailto:[email protected])
97  list is for discussion about dtc and libfdt implementation.
98* Core device tree bindings are discussed on the
99  [devicetree-spec](mailto:[email protected]) list.
100
101