libfdt: Don't search for the terminator in fdt_get_string()

Unless FDT_ASSUME_MASK includes ASSUME_VALID_INPUT, fdt_get_string()
finds the end of the string with memchr(), bounded by the end of the
string block, which also checks that the string is terminated. A
string block normally ends with a NUL, and then every string in it is
terminated. When the caller does not ask for the string's length,
check that byte instead, so that fdt_string() doesn't scan the string
at all.

This helps callers that look up many property names. With a 612 KiB
tree on x86-64, built with gcc -O2 and the default FDT_ASSUME_MASK,
calling fdt_string() for the name of each of its 17,899 properties
takes 108 us instead of 160 us, not counting the walk that finds
them. libufdt, which Android bootloaders use to apply overlays, calls
fdt_string() at least once for every property it reads: tens of
thousands of times to apply one overlay to a 445 KiB tree.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Kelvin Zhang <zhangkelvin@google.com>
Message-ID: <CAOSmRzixCf479QyhmGuwBBZrRLTYqoaSUw88HKZnkPyEh6=gcw@mail.gmail.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
1 file changed
tree: 0701a643d36ab966697dcbc0a9d503bb573fca84
  1. .github/
  2. Documentation/
  3. libfdt/
  4. pylibfdt/
  5. scripts/
  6. tests/
  7. .clang-format
  8. .editorconfig
  9. .gitignore
  10. .gitlab-ci.yml
  11. AGENTS.md
  12. BSD-2-Clause
  13. checks.c
  14. CLAUDE.md
  15. CONTRIBUTING.md
  16. convert-dtsv0-lexer.l
  17. data.c
  18. dtc-lexer.l
  19. dtc-parser.y
  20. dtc.c
  21. dtc.h
  22. dtdiff
  23. fdtdump.c
  24. fdtget.c
  25. fdtoverlay.c
  26. fdtput.c
  27. flattree.c
  28. fstree.c
  29. GPL
  30. livetree.c
  31. Makefile
  32. Makefile.convert-dtsv0
  33. Makefile.dtc
  34. Makefile.utils
  35. meson.build
  36. meson_options.txt
  37. pyproject.toml
  38. README.license
  39. README.md
  40. srcpos.c
  41. srcpos.h
  42. TODO
  43. treesource.c
  44. util.c
  45. util.h
  46. VERSION.txt
  47. version_gen.h.in
  48. yamltree.c
README.md

Device Tree Compiler and libfdt

The source tree contains the Device Tree Compiler (dtc) toolchain for working with device tree source and binary files and also libfdt, a utility library for reading and manipulating the binary format.

dtc and libfdt are maintained by:

Building

dtc and libfdt are built with Meson:

$ meson setup build
$ meson compile -C build
$ meson test -C build
$ meson install -C build

meson configure build lists the build options, e.g. -Dpython=disabled to skip the Python library. See the Meson quick guide for more, such as installing with --destdir. scripts/install-deps.sh installs the packages needed on common systems.

Building with make still works but is deprecated.

Python library

A Python library wrapping libfdt is also available. To build this you will need to install swig and Python development files. On Debian distributions:

$ sudo apt-get install swig python3-dev

The library provides an Fdt class which you can use like this, from build/tests after running the tests:

$ PYTHONPATH=../pylibfdt python3
>>> import libfdt
>>> fdt = libfdt.Fdt(open('test_tree1.dtb', mode='rb').read())
>>> node = fdt.path_offset('/subnode@1')
>>> print(node)
124
>>> prop_offset = fdt.first_property_offset(node)
>>> prop = fdt.get_property_by_offset(prop_offset)
>>> print('%s=%s' % (prop.name, prop.as_str()))
compatible=subnode1
>>> node2 = fdt.path_offset('/')
>>> print(fdt.getprop(node2, 'compatible').as_str())
test_tree1

You will find tests in tests/pylibfdt_tests.py showing how to use each method. Help is available using the Python help command, e.g.:

$ cd build/pylibfdt
$ python3 -c "import libfdt; help(libfdt)"

If you add new features, please check code coverage:

$ sudo apt-get install python3-coverage
$ cd build/tests
# It's just 'coverage' on most other distributions
$ python3-coverage run ../../tests/pylibfdt_tests.py
$ python3-coverage html
# Open 'htmlcov/index.html' in your browser

The library can be installed with pip from a local source tree:

$ pip install . [--user|--prefix=/path/to/install_dir]

Or directly from a remote git repo:

$ pip install git+git://git.kernel.org/pub/scm/utils/dtc/dtc.git@main

The install depends on libfdt shared library being installed on the host system first. Generally, using --user or --prefix is not necessary and pip will use the default location for the Python installation which varies if the user is root or not.

You can also install everything with meson if you like, but pip is recommended.

To install both libfdt and pylibfdt you can use:

$ meson setup [--prefix=/path/to/install_dir] build
$ meson install -C build

To disable building the python library, even if swig and Python are available, use:

$ meson setup -Dpython=disabled build

More work remains to support all of libfdt, including access to numeric values.

Mailing lists