No description
Find a file
Rob Herring abbd523bae pylibfdt: Work-around SWIG limitations with flexible arrays
Commit a41509bea3 ("libfdt: Replace deprecated 0-length arrays with
proper flexible arrays") fails to build pylibfdt:

./pylibfdt/libfdt_wrap.c: In function ‘_wrap_fdt_node_header_name_set’:
./pylibfdt/libfdt_wrap.c:4350:18: error: cast specifies array type
 4350 |     arg1->name = (char [])(char *)memcpy(malloc((size)*sizeof(char)), (const char *)(arg2), sizeof(char)*(size));
      |                  ^
./pylibfdt/libfdt_wrap.c:4350:16: error: invalid use of flexible array member
 4350 |     arg1->name = (char [])(char *)memcpy(malloc((size)*sizeof(char)), (const char *)(arg2), sizeof(char)*(size));
      |                ^
./pylibfdt/libfdt_wrap.c:4352:16: error: invalid use of flexible array member
 4352 |     arg1->name = 0;
      |                ^
./pylibfdt/libfdt_wrap.c: In function ‘_wrap_fdt_property_data_set’:
./pylibfdt/libfdt_wrap.c:4613:18: error: cast specifies array type
 4613 |     arg1->data = (char [])(char *)memcpy(malloc((size)*sizeof(char)), (const char *)(arg2), sizeof(char)*(size));
      |                  ^
./pylibfdt/libfdt_wrap.c:4613:16: error: invalid use of flexible array member
 4613 |     arg1->data = (char [])(char *)memcpy(malloc((size)*sizeof(char)), (const char *)(arg2), sizeof(char)*(size));
      |                ^
./pylibfdt/libfdt_wrap.c:4615:16: error: invalid use of flexible array member
 4615 |     arg1->data = 0;
      |                ^

Turns out this is known issue with SWIG: https://github.com/swig/swig/issues/1699

Implement the work-around to ignore the flexible array member.

Fixes: a41509bea3 ("libfdt: Replace deprecated 0-length arrays with proper flexible arrays")
Cc: Kees Cook <keescook@chromium.org>
Cc: Simon Glass <sjg@chromium.org>
Signed-off-by: Rob Herring <robh@kernel.org>
Message-Id: <20230201224441.305757-1-robh@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-02 16:32:47 +11:00
Documentation manual.txt: Follow README.md and remove Jon 2022-07-30 13:50:27 +10:00
libfdt libfdt: Replace deprecated 0-length arrays with proper flexible arrays 2023-01-29 14:42:57 +11:00
pylibfdt pylibfdt: Work-around SWIG limitations with flexible arrays 2023-02-02 16:32:47 +11:00
scripts dtc: Add GPLv2 SPDX tags to files missing license text 2019-06-21 21:06:10 +10:00
tests dtc: Warning rather than error on possible truncation of cell values 2022-11-21 14:18:44 +11:00
.cirrus.yml CI: Cirrus: bump used FreeBSD from 12.1 to 13.0 2021-11-01 14:02:44 +11:00
.editorconfig Add .editorconfig 2019-10-09 22:41:44 +11:00
.gitignore pylibfdt: Move setup.py to the top level 2021-11-11 14:34:51 +11:00
.travis.yml travis.yml: Run tests on the non-x86 builders, too 2019-12-04 16:54:04 +11:00
BSD-2-Clause README.license: Update to reflect SPDX tag usage 2019-06-21 21:06:10 +10:00
checks.c Handle integer overflow in check_property_phandle_args() 2021-12-29 17:04:18 +11:00
CONTRIBUTING.md Add description of Signed-off-by lines 2022-07-29 12:55:15 +10:00
convert-dtsv0-lexer.l convert-dtsv0: Fix signedness comparisons warning 2020-10-13 15:48:40 +11:00
data.c dtc: Fix signedness comparisons warnings: Wrap (-1) 2020-10-13 15:58:53 +11:00
dtc-lexer.l dtc: introduce label relative path references 2022-01-25 17:10:47 +11:00
dtc-parser.y dtc: Warning rather than error on possible truncation of cell values 2022-11-21 14:18:44 +11:00
dtc.c dtc: Fix signedness comparisons warnings: reservednum 2021-06-15 12:48:25 +10:00
dtc.h asm: Use .asciz and .ascii instead of .string 2021-09-25 16:11:41 +10:00
dtdiff dtc: Add GPLv2 SPDX tags to files missing license text 2019-06-21 21:06:10 +10:00
fdtdump.c fdtdump: fix -Werror=int-to-pointer-cast 2021-08-26 10:58:26 +10:00
fdtget.c Support 'r' format for printing raw bytes with fdtget 2021-12-09 17:48:58 +11:00
fdtoverlay.c fdtoverlay: Return non-zero exit code if overlays can't be applied 2019-10-11 15:58:18 +11:00
fdtput.c Support 'r' format for printing raw bytes with fdtget 2021-12-09 17:48:58 +11:00
flattree.c flattree: Use '\n', not ';' to separate asm pseudo-ops 2021-09-27 11:54:56 +10:00
fstree.c fstree: replace lstat with stat 2019-10-14 17:07:10 +11:00
GPL Update the GPL2 text to the latest revision 2019-05-21 09:58:37 +10:00
livetree.c Don't generate erroneous fixups from reference to path 2022-07-31 22:10:05 +10:00
Makefile Makefile: fix infinite recursion by dropping non-existent %.output 2022-09-26 15:22:05 +10:00
Makefile.convert-dtsv0 dtc: Add GPLv2 SPDX tags to files missing license text 2019-06-21 21:06:10 +10:00
Makefile.dtc dtc: Replace GPLv2 boilerplate/reference with SPDX tags 2019-06-21 21:06:10 +10:00
Makefile.utils dtc: Add GPLv2 SPDX tags to files missing license text 2019-06-21 21:06:10 +10:00
MANIFEST.in Update README in MANIFEST.in and setup.py to README.md 2022-07-30 13:42:39 +10:00
meson.build Allow static building with meson 2022-07-27 16:38:45 +10:00
meson_options.txt Allow static building with meson 2022-07-27 16:38:45 +10:00
README.license README.license: Update to reflect SPDX tag usage 2019-06-21 21:06:10 +10:00
README.md Split out information for contributors to CONTRIBUTING.md 2022-07-28 16:25:56 +10:00
setup.py Update README in MANIFEST.in and setup.py to README.md 2022-07-30 13:42:39 +10:00
srcpos.c srcpos: increase MAX_SRCFILE_DEPTH 2021-01-13 12:05:26 +11:00
srcpos.h dtc: Replace GPLv2 boilerplate/reference with SPDX tags 2019-06-21 21:06:10 +10:00
TODO dtc: Update TODO files 2007-12-19 08:20:26 -06:00
treesource.c treesource: Maintain phandle label/path on output 2021-10-11 16:20:53 +11:00
util.c util: introduce xstrndup helper 2022-01-25 17:10:47 +11:00
util.h util: introduce xstrndup helper 2022-01-25 17:10:47 +11:00
version_gen.h.in build-sys: add meson build 2020-10-21 14:36:07 +11:00
yamltree.c yamltree: Remove marker ordering dependency 2021-06-08 11:53:19 +10:00

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:

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:

$ 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 pylibfdt
$ python3 -c "import libfdt; help(libfdt)"

If you add new features, please check code coverage:

$ sudo apt-get install python3-coverage
$ cd tests
# It's just 'coverage' on most other distributions
$ python3-coverage run 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 via make if you like, but pip is recommended.

To install both libfdt and pylibfdt you can use:

$ make install [PREFIX=/path/to/install_dir]

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

$ make NO_PYTHON=1

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

Mailing lists