Commit graph

35 commits

Author SHA1 Message Date
Simon Glass
9005f4108e pylibfdt: Allow delprop() to return errors
At present this method always raised an exception when an error occurs.
Add a 'quiet' argument so it matches the other methods.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-09-13 10:16:34 +10:00
Dan Horák
0fd1c8c783 pylibfdt: fdt_get_mem_rsv returns 2 uint64_t values
Fix typemap for fdt_get_mem_rsv so it returns 64-bit values.

Fixes https://github.com/dgibson/dtc/issues/15.

Signed-off-by: Dan Horák <dan@danny.cz>
[dwg: Adjusted commit message for typo and context]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-09-02 15:35:59 +10:00
David Gibson
04853cad18 pylibfdt: Don't incorrectly / unnecessarily override uint64_t typemap
In libfdt.i we set the handling of uint64_t parameters to use
PyLong_AsUnsignedLong.  But for 32-bit platforms, where an unsigned long
is 32-bits, this will truncate the value we need.

It turns out swig's default typemapping for uint64_t correctly handles
conversions both to python ints and python longs, so we don't need this
typemap at all.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-08-31 11:15:41 +10:00
Simon Glass
853649acce pylibfdt: Support the sequential-write interface
It is useful to be able to create a device tree from scratch using
software. This is supported in libfdt but not currently available in the
Python bindings.

Add a new FdtSw class to handle this, with various methods corresponding
to the libfdt functions. When the tree is complete, calling AsFdt() will
return the completed device-tree object.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-07-11 16:21:20 +10:00
Simon Glass
b770f3d1c1 pylibfdt: Support setting the name of a node
Add a method to call fdt_set_name().

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-14 14:08:32 +10:00
Simon Glass
2f0d07e678 pylibfdt: Add functions to set and get properties as strings
It is common to want to set a property to a nul-terminated string in a
device tree. Add python methods to handle this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-14 14:07:40 +10:00
Simon Glass
354d3dc559 pylibfdt: Update the bytearray size with pack()
At present pack() calls fdt_pack() which may well reduce the size of the
device-tree data. However this does not currently update the size of the
bytearray to take account of any reduction. This means that there may be
unused data at the end of the bytearray and any users of as_bytearray()
will see this extra data.

Fix this by resizing the bytearray after packing.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-13 22:02:06 +10:00
Simon Glass
3c374d46ac pylibfdt: Allow reading integer values from properties
Extend the Properties class with some functions to read a single integer
property. Add a new getprop_obj() function to return a Property object
instead of the raw data.

This suggested approach can be extended to handle other types, as well as
arrays.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-13 16:59:18 +10:00
Simon Glass
49d32ce40b pylibfdt: Use an unsigned type for fdt32_t
The members of struct fdt_header are declared as fdt32_t which is a
32-bit, big-endian, unsigned integer. These fields are accessed by macros
in libfdt.h so no return type is declared. But the correct return type is
uint32_t, not fdt32_t, since the endianness conversion is done within the
macro before returning the value.

The macros are re-declared as normal functions in pylibfdt since swig does
not support macros. The return type is currently int. Change it to
uint32_t, which allows us to drop the work-around mask in Fdt.magic().

Also change the typedef for fdt32_t to uint32_t. The currently has no
obvious effect, since use of big-endian values should always be internal
to pylibfdt, but it is more correct.

Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-13 16:32:55 +10:00
Simon Glass
9aafa33d99 pylibfdt: Add functions to update properties
Allow updating and creating properties, including special methods for
integers.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-12 14:44:20 +10:00
Simon Glass
5a598671fd pylibfdt: Support device-tree creation/expansion
Add support for fdt_open_into() and fdt_create_empty_tree() from the
Python library. The former is named resize() since it better fits with
what the Python binding actually does.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-09 23:36:33 +10:00
Simon Glass
483e170625 pylibfdt: Add support for reading the memory reserve map
Add a way to access this information from Python.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-09 23:35:35 +10:00
Simon Glass
29bb05aa42 pylibfdt: Add support for the rest of the header functions
Export all of these through Python.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-09 23:33:41 +10:00
Simon Glass
582a7159a5 pylibfdt: Add support for fdt_next_node()
This function requires a bit of typemap effort to get the depth parameter
to work correctly. Add support for it, along with a test.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-08 22:26:03 +10:00
Simon Glass
f0f8c91698 pylibfdt: Reorder functions to match libfdt.h
The ordering of the Python functions loosely matches the corresponding
function in the C header file, but not exactly. As we add more functions
it is easier to track what is missing if they are in the same order.

Move some functions around to achieve this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-08 22:25:02 +10:00
Simon Glass
64a69d1239 pylibfdt: Return string instead of bytearray from getprop()
The return value is not actually mutable, so it seems more correct to
return bytes rather than a bytearray.

Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-08 13:43:52 +10:00
Simon Glass
f0be81bd8d Make Property a subclass of bytearray
It is annoying to have to add .value when we want the value of a Property.
Make Property a subclass of bytearray so that it can be used directly when
the value is required.

Fix the Property class comment while we are here.

Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-04 18:20:02 +10:00
Simon Glass
24b1f3f064 pylibfdt: Add a method to access the device tree directly
When calling libfdt functions which are not supported by the Fdt class it
is necessary to get direct access to the device tree data. At present this
requries using the internal _fdt member. Add a new method to provide
public access to this, without allowing the data to be changed.

Note that a bytearray type is returned rather than str, since the swig
types are set up for bytearray to map correctly to const void *.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-04 18:18:38 +10:00
David Gibson
8c1eb1526d pylibfdt: Use Python2 explicitly
The pylibfdt code is written for Python2, not Python3.  So, it's safer to
explicitly request Python2 in our scripts and when checking pkg-config.

On Arch Linux at least, there isn't actually a plain "python" link, just
"python2" and "python3", so the current setup won't work at all.

According to https://www.python.org/dev/peps/pep-0394/ using "python2"
should work, and is preferred.

Updating pylibfdt to work with Python3 would be nice, but is a problem for
another day.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-09-27 18:10:02 +10:00
Simon Glass
5bed86aee9 pylibfdt: Add support for fdt_subnode_offset()
Add this into the class to simplify use of this function.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-21 10:17:57 +10:00
Simon Glass
46f31b65b3 pylibfdt: Add support for fdt_node_offset_by_phandle()
Add this into the class to simplify use of this function.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-21 10:00:44 +10:00
Simon Glass
a3ae437236 pylibfdt: Add support for fdt_parent_offset()
Add this into the class to simplify use of this function.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-21 10:00:44 +10:00
Simon Glass
a198af8034 pylibfdt: Add support for fdt_get_phandle()
Add this into the class to simplify use of this function.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-21 10:00:44 +10:00
Simon Glass
ab78860f09 pylibfdt: Add stdint include to fix uint32_t
Some types used by libfdt.h are not understood the Python module since
they are defined outside it. An example is an attempt to do this:

   import libfdt
   result = libfdt.fdt_node_offset_by_phandle(fdt, phandle)

This results in:

TypeError: in method 'fdt_node_offset_by_phandle', argument 2 of
	type 'uint32_t'

Include the standard integer header file to fix this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-15 13:47:24 +10:00
Simon Glass
e56f2b07be pylibfdt: Use setup.py to build the swig file
Since we are using the standard .i extension for the swig file, we can use
setup.py to build the wrapper. Drop the existing build code since it is
not needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-08 11:54:07 +10:00
Simon Glass
896f1c1332 pylibfdt: Use Makefile constructs to implement NO_PYTHON
The current mechanism uses a shell construct, but it seems better to use
a Makefile approach.

Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-08 11:54:07 +10:00
Simon Glass
90db6d9989 pylibfdt: Allow setup.py to operate stand-alone
At present we require that setup.py is executed from the Makefile, which
sets up various important things like the list of files to build and the
version number.

However many installation systems expect to be able to change to the
directory containing setup.py and run it. This allows them to support (for
example) building/installing for multiple Python versions, varying
installation paths, particular C flags, etc.

The problem in implementing this is that we don't want to duplicate the
information in the Makefile. A common solution (so I am told) is to parse
the Makefile to obtain the required information.

Update the setup.py script to read a few Makefiles when it does not see
the required information in its environment. This allows installation
using:

   ./pylibfdt/setup.py install

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-08 11:54:07 +10:00
Simon Glass
b04a2cf088 pylibfdt: Fix code style in setup.py
We should follow PEP8 even for our setup() call.

Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-07 15:43:34 +10:00
Simon Glass
1c5170d3a4 pylibfdt: Rename libfdt.swig to libfdt.i
The .i extension allows Python distutils to automatically handle the swig
file. Rename it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-07 15:43:34 +10:00
Simon Glass
ab15256d8d pylibfdt: Use the call function to simplify the Makefile
This is in a separate patch since I not sure if GNU make features
are permitted in the Makefile.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-29 16:13:58 +11:00
Simon Glass
9f2e3a3a1f pylibfdt: Use the correct libfdt version in the module
Use the same version number in the module as with the rest of libfdt. This
can be examined with:

   import pkg_resources
   print pkg_resources.require('libfdt')[0].version

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-29 16:13:58 +11:00
Simon Glass
e91c652af2 pylibfdt: Enable installation of Python module
Adjust the setup script to support installation, and call it from the
Makefile if enabled. It will be disabled if we were unable to build the
module (e.g. due to swig being missing), or the NO_PYTHON environment
variable is set.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-29 16:13:58 +11:00
Simon Glass
14c4171f4f pylibfdt: Use package_dir to set the package directory
At present we manually move _libfdt.so into the correct place. Provide a
package directory so we can avoid needing to do this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-27 15:33:25 +11:00
Simon Glass
89a5062ab2 pylibfdt: Use environment to pass C flags and files
At present setup.py adjusts its command line when running, so that the
C flags and file list can be passed as arguments. Pass them in environment
variables instead, so we can avoid this messiness. It also allows us to
support the 'install' command.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-27 15:33:25 +11:00
Simon Glass
50f2507016 Add an initial Python library for libfdt
Add Python bindings for a bare-bones set of libfdt functions. These allow
navigating the tree and reading node names and properties.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-21 16:21:58 +11:00