mirror of
https://github.com/AsahiLinux/u-boot
synced 2025-02-26 12:27:12 +00:00
libfdt: Update to latest pylibfdt implementation
The enhanced pylibfdt support in U-Boot needed for binman was a placeholder while upstreaming of this work continued. This is now complete, so bring in the changes and update the tools as needed. There are quite a few changes since we decided to split the implementation into three fdt classes instead of two. The Fdt.del_node() method was unfortunately missed in this process and will be dealt with later. It exists in U-Boot but not upstream. Further syncing of libfdt probably needs to wait until we assess the code-size impact of all the new checking code on SPL and possibly provide a way to disable it. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
95e11069b5
commit
50c59522c2
3 changed files with 254 additions and 240 deletions
|
@ -18,7 +18,8 @@
|
||||||
* a struct called fdt_property. That struct causes swig to create a class in
|
* a struct called fdt_property. That struct causes swig to create a class in
|
||||||
* libfdt.py called fdt_property(), which confuses things.
|
* libfdt.py called fdt_property(), which confuses things.
|
||||||
*/
|
*/
|
||||||
static int _fdt_property(void *fdt, const char *name, const char *val, int len)
|
static int fdt_property_stub(void *fdt, const char *name, const char *val,
|
||||||
|
int len)
|
||||||
{
|
{
|
||||||
return fdt_property(fdt, name, val, len);
|
return fdt_property(fdt, name, val, len);
|
||||||
}
|
}
|
||||||
|
@ -54,6 +55,7 @@ import struct
|
||||||
# Pass this as the 'quiet' parameter to return -ENOTFOUND on NOTFOUND errors,
|
# Pass this as the 'quiet' parameter to return -ENOTFOUND on NOTFOUND errors,
|
||||||
# instead of raising an exception.
|
# instead of raising an exception.
|
||||||
QUIET_NOTFOUND = (NOTFOUND,)
|
QUIET_NOTFOUND = (NOTFOUND,)
|
||||||
|
QUIET_NOSPACE = (NOSPACE,)
|
||||||
|
|
||||||
|
|
||||||
class FdtException(Exception):
|
class FdtException(Exception):
|
||||||
|
@ -119,23 +121,18 @@ def check_err_null(val, quiet=()):
|
||||||
raise FdtException(val)
|
raise FdtException(val)
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
class FdtRo(object):
|
||||||
|
"""Class for a read-only device-tree
|
||||||
|
|
||||||
class Fdt:
|
This is a base class used by FdtRw (read-write access) and FdtSw
|
||||||
"""Device tree class, supporting all operations
|
(sequential-write access). It implements read-only access to the
|
||||||
|
device tree.
|
||||||
|
|
||||||
The Fdt object is created is created from a device tree binary file,
|
Here are the three classes and when you should use them:
|
||||||
e.g. with something like:
|
|
||||||
|
|
||||||
fdt = Fdt(open("filename.dtb").read())
|
FdtRo - read-only access to an existing FDT
|
||||||
|
FdtRw - read-write access to an existing FDT (most common case)
|
||||||
Operations can then be performed using the methods in this class. Each
|
FdtSw - for creating a new FDT, as well as allowing read-only access
|
||||||
method xxx(args...) corresponds to a libfdt function fdt_xxx(fdt, args...).
|
|
||||||
|
|
||||||
All methods raise an FdtException if an error occurs. To avoid this
|
|
||||||
behaviour a 'quiet' parameter is provided for some functions. This
|
|
||||||
defaults to empty, but you can pass a list of errors that you expect.
|
|
||||||
If one of these errors occurs, the function will return an error number
|
|
||||||
(e.g. -NOTFOUND).
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self._fdt = bytearray(data)
|
self._fdt = bytearray(data)
|
||||||
|
@ -157,12 +154,14 @@ class Fdt:
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
nodeoffset: Node offset of previous node
|
nodeoffset: Node offset of previous node
|
||||||
depth: On input, the depth of the node at nodeoffset. On output, the
|
depth: The depth of the node at nodeoffset. This is used to
|
||||||
depth of the returned node
|
calculate the depth of the returned node
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The offset of the next node, if any
|
Typle:
|
||||||
|
Offset of the next node, if any, else a -ve error
|
||||||
|
Depth of the returned node, if any, else undefined
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no more nodes found or other error occurs
|
FdtException if no more nodes found or other error occurs
|
||||||
|
@ -205,7 +204,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Magic word
|
Magic word
|
||||||
"""
|
"""
|
||||||
return fdt_magic(self._fdt) & 0xffffffff
|
return fdt_magic(self._fdt)
|
||||||
|
|
||||||
def totalsize(self):
|
def totalsize(self):
|
||||||
"""Return the total size of the device tree
|
"""Return the total size of the device tree
|
||||||
|
@ -213,7 +212,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Total tree size in bytes
|
Total tree size in bytes
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_totalsize(self._fdt))
|
return fdt_totalsize(self._fdt)
|
||||||
|
|
||||||
def off_dt_struct(self):
|
def off_dt_struct(self):
|
||||||
"""Return the start of the device-tree struct area
|
"""Return the start of the device-tree struct area
|
||||||
|
@ -221,7 +220,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Start offset of struct area
|
Start offset of struct area
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_off_dt_struct(self._fdt))
|
return fdt_off_dt_struct(self._fdt)
|
||||||
|
|
||||||
def off_dt_strings(self):
|
def off_dt_strings(self):
|
||||||
"""Return the start of the device-tree string area
|
"""Return the start of the device-tree string area
|
||||||
|
@ -229,7 +228,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Start offset of string area
|
Start offset of string area
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_off_dt_strings(self._fdt))
|
return fdt_off_dt_strings(self._fdt)
|
||||||
|
|
||||||
def off_mem_rsvmap(self):
|
def off_mem_rsvmap(self):
|
||||||
"""Return the start of the memory reserve map
|
"""Return the start of the memory reserve map
|
||||||
|
@ -237,7 +236,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Start offset of memory reserve map
|
Start offset of memory reserve map
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_off_mem_rsvmap(self._fdt))
|
return fdt_off_mem_rsvmap(self._fdt)
|
||||||
|
|
||||||
def version(self):
|
def version(self):
|
||||||
"""Return the version of the device tree
|
"""Return the version of the device tree
|
||||||
|
@ -245,7 +244,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Version number of the device tree
|
Version number of the device tree
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_version(self._fdt))
|
return fdt_version(self._fdt)
|
||||||
|
|
||||||
def last_comp_version(self):
|
def last_comp_version(self):
|
||||||
"""Return the last compatible version of the device tree
|
"""Return the last compatible version of the device tree
|
||||||
|
@ -253,7 +252,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Last compatible version number of the device tree
|
Last compatible version number of the device tree
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_last_comp_version(self._fdt))
|
return fdt_last_comp_version(self._fdt)
|
||||||
|
|
||||||
def boot_cpuid_phys(self):
|
def boot_cpuid_phys(self):
|
||||||
"""Return the physical boot CPU ID
|
"""Return the physical boot CPU ID
|
||||||
|
@ -261,7 +260,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Physical boot CPU ID
|
Physical boot CPU ID
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_boot_cpuid_phys(self._fdt))
|
return fdt_boot_cpuid_phys(self._fdt)
|
||||||
|
|
||||||
def size_dt_strings(self):
|
def size_dt_strings(self):
|
||||||
"""Return the start of the device-tree string area
|
"""Return the start of the device-tree string area
|
||||||
|
@ -269,7 +268,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Start offset of string area
|
Start offset of string area
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_size_dt_strings(self._fdt))
|
return fdt_size_dt_strings(self._fdt)
|
||||||
|
|
||||||
def size_dt_struct(self):
|
def size_dt_struct(self):
|
||||||
"""Return the start of the device-tree struct area
|
"""Return the start of the device-tree struct area
|
||||||
|
@ -277,7 +276,7 @@ class Fdt:
|
||||||
Returns:
|
Returns:
|
||||||
Start offset of struct area
|
Start offset of struct area
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_size_dt_struct(self._fdt))
|
return fdt_size_dt_struct(self._fdt)
|
||||||
|
|
||||||
def num_mem_rsv(self, quiet=()):
|
def num_mem_rsv(self, quiet=()):
|
||||||
"""Return the number of memory reserve-map records
|
"""Return the number of memory reserve-map records
|
||||||
|
@ -398,77 +397,6 @@ class Fdt:
|
||||||
return pdata
|
return pdata
|
||||||
return Property(pdata[0], pdata[1])
|
return Property(pdata[0], pdata[1])
|
||||||
|
|
||||||
def get_property(self, nodeoffset, prop_name, quiet=()):
|
|
||||||
"""Obtains a property by name
|
|
||||||
|
|
||||||
Args:
|
|
||||||
nodeoffset: Offset to the node to check
|
|
||||||
prop_name: Name of property to get
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Property object, or None if not found
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
FdtException on error (e.g. invalid prop_offset or device
|
|
||||||
tree format)
|
|
||||||
"""
|
|
||||||
pdata = check_err_null(
|
|
||||||
fdt_get_property(self._fdt, nodeoffset, prop_name), quiet)
|
|
||||||
if isinstance(pdata, (int)):
|
|
||||||
return pdata
|
|
||||||
return Property(pdata[0], pdata[1])
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def create_empty_tree(size, quiet=()):
|
|
||||||
"""Create an empty device tree ready for use
|
|
||||||
|
|
||||||
Args:
|
|
||||||
size: Size of device tree in bytes
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Fdt object containing the device tree
|
|
||||||
"""
|
|
||||||
data = bytearray(size)
|
|
||||||
err = check_err(fdt_create_empty_tree(data, size), quiet)
|
|
||||||
if err:
|
|
||||||
return err
|
|
||||||
return Fdt(data)
|
|
||||||
|
|
||||||
def open_into(self, size, quiet=()):
|
|
||||||
"""Move the device tree into a larger or smaller space
|
|
||||||
|
|
||||||
This creates a new device tree of size @size and moves the existing
|
|
||||||
device tree contents over to that. It can be used to create more space
|
|
||||||
in a device tree.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
size: Required new size of device tree in bytes
|
|
||||||
"""
|
|
||||||
fdt = bytearray(size)
|
|
||||||
fdt[:len(self._fdt)] = self._fdt
|
|
||||||
err = check_err(fdt_open_into(self._fdt, fdt, size), quiet)
|
|
||||||
if err:
|
|
||||||
return err
|
|
||||||
self._fdt = fdt
|
|
||||||
|
|
||||||
def pack(self, quiet=()):
|
|
||||||
"""Pack the device tree to remove unused space
|
|
||||||
|
|
||||||
This adjusts the tree in place.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
FdtException if any error occurs
|
|
||||||
"""
|
|
||||||
err = check_err(fdt_pack(self._fdt), quiet)
|
|
||||||
if err:
|
|
||||||
return err
|
|
||||||
del self._fdt[self.totalsize():]
|
|
||||||
return err
|
|
||||||
|
|
||||||
def getprop(self, nodeoffset, prop_name, quiet=()):
|
def getprop(self, nodeoffset, prop_name, quiet=()):
|
||||||
"""Get a property from a node
|
"""Get a property from a node
|
||||||
|
|
||||||
|
@ -478,7 +406,9 @@ class Fdt:
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Value of property as a string, or -ve error number
|
Value of property as a Property object (which can be used as a
|
||||||
|
bytearray/string), or -ve error number. On failure, returns an
|
||||||
|
integer error
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtError if any error occurs (e.g. the property is not found)
|
FdtError if any error occurs (e.g. the property is not found)
|
||||||
|
@ -487,26 +417,6 @@ class Fdt:
|
||||||
quiet)
|
quiet)
|
||||||
if isinstance(pdata, (int)):
|
if isinstance(pdata, (int)):
|
||||||
return pdata
|
return pdata
|
||||||
return str(pdata[0])
|
|
||||||
|
|
||||||
def getprop_obj(self, nodeoffset, prop_name, quiet=()):
|
|
||||||
"""Get a property from a node as a Property object
|
|
||||||
|
|
||||||
Args:
|
|
||||||
nodeoffset: Node offset containing property to get
|
|
||||||
prop_name: Name of property to get
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Property object, or None if not found
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
FdtError if any error occurs (e.g. the property is not found)
|
|
||||||
"""
|
|
||||||
pdata = check_err_null(fdt_getprop(self._fdt, nodeoffset, prop_name),
|
|
||||||
quiet)
|
|
||||||
if isinstance(pdata, (int)):
|
|
||||||
return None
|
|
||||||
return Property(prop_name, bytearray(pdata[0]))
|
return Property(prop_name, bytearray(pdata[0]))
|
||||||
|
|
||||||
def get_phandle(self, nodeoffset):
|
def get_phandle(self, nodeoffset):
|
||||||
|
@ -536,12 +446,101 @@ class Fdt:
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet)
|
return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet)
|
||||||
|
|
||||||
|
def node_offset_by_phandle(self, phandle, quiet=()):
|
||||||
|
"""Get the offset of a node with the given phandle
|
||||||
|
|
||||||
|
Args:
|
||||||
|
phandle: Phandle to search for
|
||||||
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The offset of node with that phandle, if any
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
FdtException if no node found or other error occurs
|
||||||
|
"""
|
||||||
|
return check_err(fdt_node_offset_by_phandle(self._fdt, phandle), quiet)
|
||||||
|
|
||||||
|
|
||||||
|
class Fdt(FdtRo):
|
||||||
|
"""Device tree class, supporting all operations
|
||||||
|
|
||||||
|
The Fdt object is created is created from a device tree binary file,
|
||||||
|
e.g. with something like:
|
||||||
|
|
||||||
|
fdt = Fdt(open("filename.dtb").read())
|
||||||
|
|
||||||
|
Operations can then be performed using the methods in this class. Each
|
||||||
|
method xxx(args...) corresponds to a libfdt function fdt_xxx(fdt, args...).
|
||||||
|
|
||||||
|
All methods raise an FdtException if an error occurs. To avoid this
|
||||||
|
behaviour a 'quiet' parameter is provided for some functions. This
|
||||||
|
defaults to empty, but you can pass a list of errors that you expect.
|
||||||
|
If one of these errors occurs, the function will return an error number
|
||||||
|
(e.g. -NOTFOUND).
|
||||||
|
"""
|
||||||
|
def __init__(self, data):
|
||||||
|
FdtRo.__init__(self, data)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create_empty_tree(size, quiet=()):
|
||||||
|
"""Create an empty device tree ready for use
|
||||||
|
|
||||||
|
Args:
|
||||||
|
size: Size of device tree in bytes
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Fdt object containing the device tree
|
||||||
|
"""
|
||||||
|
data = bytearray(size)
|
||||||
|
err = check_err(fdt_create_empty_tree(data, size), quiet)
|
||||||
|
if err:
|
||||||
|
return err
|
||||||
|
return Fdt(data)
|
||||||
|
|
||||||
|
def resize(self, size, quiet=()):
|
||||||
|
"""Move the device tree into a larger or smaller space
|
||||||
|
|
||||||
|
This creates a new device tree of size @size and moves the existing
|
||||||
|
device tree contents over to that. It can be used to create more space
|
||||||
|
in a device tree. Note that the Fdt object remains the same, but it
|
||||||
|
now has a new bytearray holding the contents.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
size: Required new size of device tree in bytes
|
||||||
|
"""
|
||||||
|
fdt = bytearray(size)
|
||||||
|
err = check_err(fdt_open_into(self._fdt, fdt, size), quiet)
|
||||||
|
if err:
|
||||||
|
return err
|
||||||
|
self._fdt = fdt
|
||||||
|
|
||||||
|
def pack(self, quiet=()):
|
||||||
|
"""Pack the device tree to remove unused space
|
||||||
|
|
||||||
|
This adjusts the tree in place.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Error code, or 0 if OK
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
FdtException if any error occurs
|
||||||
|
"""
|
||||||
|
err = check_err(fdt_pack(self._fdt), quiet)
|
||||||
|
if err:
|
||||||
|
return err
|
||||||
|
del self._fdt[self.totalsize():]
|
||||||
|
return err
|
||||||
|
|
||||||
def set_name(self, nodeoffset, name, quiet=()):
|
def set_name(self, nodeoffset, name, quiet=()):
|
||||||
"""Set the name of a node
|
"""Set the name of a node
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
nodeoffset: Node offset of node to update
|
nodeoffset: Node offset of node to update
|
||||||
name: New node name
|
name: New node name (string without \0)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Error code, or 0 if OK
|
Error code, or 0 if OK
|
||||||
|
@ -549,6 +548,8 @@ class Fdt:
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no parent found or other error occurs
|
FdtException if no parent found or other error occurs
|
||||||
"""
|
"""
|
||||||
|
if chr(0) in name:
|
||||||
|
raise ValueError('Property contains embedded nul characters')
|
||||||
return check_err(fdt_set_name(self._fdt, nodeoffset, name), quiet)
|
return check_err(fdt_set_name(self._fdt, nodeoffset, name), quiet)
|
||||||
|
|
||||||
def setprop(self, nodeoffset, prop_name, val, quiet=()):
|
def setprop(self, nodeoffset, prop_name, val, quiet=()):
|
||||||
|
@ -613,7 +614,8 @@ class Fdt:
|
||||||
Args:
|
Args:
|
||||||
nodeoffset: Node offset containing the property to create/update
|
nodeoffset: Node offset containing the property to create/update
|
||||||
prop_name: Name of property
|
prop_name: Name of property
|
||||||
val: Value to write (string without nul terminator)
|
val: Value to write (string without nul terminator). Unicode is
|
||||||
|
supposed by encoding to UTF-8
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
@ -622,7 +624,7 @@ class Fdt:
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no parent found or other error occurs
|
FdtException if no parent found or other error occurs
|
||||||
"""
|
"""
|
||||||
val += '\0'
|
val = val.encode('utf-8') + '\0'
|
||||||
return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
|
return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
|
||||||
val, len(val)), quiet)
|
val, len(val)), quiet)
|
||||||
|
|
||||||
|
@ -638,21 +640,6 @@ class Fdt:
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_delprop(self._fdt, nodeoffset, prop_name))
|
return check_err(fdt_delprop(self._fdt, nodeoffset, prop_name))
|
||||||
|
|
||||||
def node_offset_by_phandle(self, phandle, quiet=()):
|
|
||||||
"""Get the offset of a node with the given phandle
|
|
||||||
|
|
||||||
Args:
|
|
||||||
phandle: Phandle to search for
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The offset of node with that phandle, if any
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
FdtException if no node found or other error occurs
|
|
||||||
"""
|
|
||||||
return check_err(fdt_node_offset_by_phandle(self._fdt, phandle), quiet)
|
|
||||||
|
|
||||||
def del_node(self, nodeoffset):
|
def del_node(self, nodeoffset):
|
||||||
"""Delete a node
|
"""Delete a node
|
||||||
|
|
||||||
|
@ -696,16 +683,20 @@ class Property(bytearray):
|
||||||
return self.as_cell('q')
|
return self.as_cell('q')
|
||||||
|
|
||||||
def as_str(self):
|
def as_str(self):
|
||||||
return self[:-1]
|
"""Unicode is supported by decoding from UTF-8"""
|
||||||
|
if self[-1] != 0:
|
||||||
|
raise ValueError('Property lacks nul termination')
|
||||||
|
if 0 in self[:-1]:
|
||||||
|
raise ValueError('Property contains embedded nul characters')
|
||||||
|
return self[:-1].decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
class FdtSw(object):
|
class FdtSw(FdtRo):
|
||||||
"""Software interface to create a device tree from scratch
|
"""Software interface to create a device tree from scratch
|
||||||
|
|
||||||
The methods in this class work by adding to an existing 'partial' device
|
The methods in this class work by adding to an existing 'partial' device
|
||||||
tree buffer of a fixed size created by instantiating this class. When the
|
tree buffer of a fixed size created by instantiating this class. When the
|
||||||
tree is complete, call finish() to complete the device tree so that it can
|
tree is complete, call as_fdt() to obtain a device tree ready to be used.
|
||||||
be used.
|
|
||||||
|
|
||||||
Similarly with nodes, a new node is started with begin_node() and finished
|
Similarly with nodes, a new node is started with begin_node() and finished
|
||||||
with end_node().
|
with end_node().
|
||||||
|
@ -713,58 +704,87 @@ class FdtSw(object):
|
||||||
The context manager functions can be used to make this a bit easier:
|
The context manager functions can be used to make this a bit easier:
|
||||||
|
|
||||||
# First create the device tree with a node and property:
|
# First create the device tree with a node and property:
|
||||||
with FdtSw(small_size) as sw:
|
sw = FdtSw()
|
||||||
with sw.AddNode('node'):
|
with sw.add_node('node'):
|
||||||
sw.property_u32('reg', 2)
|
sw.property_u32('reg', 2)
|
||||||
fdt = sw.AsFdt()
|
fdt = sw.as_fdt()
|
||||||
|
|
||||||
# Now we can use it as a real device tree
|
# Now we can use it as a real device tree
|
||||||
fdt.setprop_u32(0, 'reg', 3)
|
fdt.setprop_u32(0, 'reg', 3)
|
||||||
|
|
||||||
|
The size hint provides a starting size for the space to be used by the
|
||||||
|
device tree. This will be increased automatically as needed as new items
|
||||||
|
are added to the tree.
|
||||||
"""
|
"""
|
||||||
def __init__(self, size, quiet=()):
|
INC_SIZE = 1024 # Expand size by this much when out of space
|
||||||
fdtrw = bytearray(size)
|
|
||||||
err = check_err(fdt_create(fdtrw, size))
|
def __init__(self, size_hint=None):
|
||||||
|
"""Create a new FdtSw object
|
||||||
|
|
||||||
|
Args:
|
||||||
|
size_hint: A hint as to the initial size to use
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError if size_hint is negative
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
FdtSw object on success, else integer error code (if not raising)
|
||||||
|
"""
|
||||||
|
if not size_hint:
|
||||||
|
size_hint = self.INC_SIZE
|
||||||
|
fdtsw = bytearray(size_hint)
|
||||||
|
err = check_err(fdt_create(fdtsw, size_hint))
|
||||||
if err:
|
if err:
|
||||||
return err
|
return err
|
||||||
self._fdtrw = fdtrw
|
self._fdt = fdtsw
|
||||||
|
|
||||||
def __enter__(self):
|
def as_fdt(self):
|
||||||
"""Contact manager to use to create a device tree via software"""
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
|
||||||
check_err(fdt_finish(self._fdtrw))
|
|
||||||
|
|
||||||
def AsFdt(self):
|
|
||||||
"""Convert a FdtSw into an Fdt so it can be accessed as normal
|
"""Convert a FdtSw into an Fdt so it can be accessed as normal
|
||||||
|
|
||||||
Note that finish() must be called before this function will work. If
|
Creates a new Fdt object from the work-in-progress device tree. This
|
||||||
you are using the context manager (see 'with' code in the FdtSw class
|
does not call fdt_finish() on the current object, so it is possible to
|
||||||
comment) then this will happen automatically.
|
add more nodes/properties and call as_fdt() again to get an updated
|
||||||
|
tree.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Fdt object allowing access to the newly created device tree
|
Fdt object allowing access to the newly created device tree
|
||||||
"""
|
"""
|
||||||
return Fdt(self._fdtrw)
|
fdtsw = bytearray(self._fdt)
|
||||||
|
check_err(fdt_finish(fdtsw))
|
||||||
|
return Fdt(fdtsw)
|
||||||
|
|
||||||
def resize(self, size, quiet=()):
|
def check_space(self, val):
|
||||||
|
"""Check if we need to add more space to the FDT
|
||||||
|
|
||||||
|
This should be called with the error code from an operation. If this is
|
||||||
|
-NOSPACE then the FDT will be expanded to have more space, and True will
|
||||||
|
be returned, indicating that the operation needs to be tried again.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
val: Return value from the operation that was attempted
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if the operation must be retried, else False
|
||||||
|
"""
|
||||||
|
if check_err(val, QUIET_NOSPACE) < 0:
|
||||||
|
self.resize(len(self._fdt) + self.INC_SIZE)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def resize(self, size):
|
||||||
"""Resize the buffer to accommodate a larger tree
|
"""Resize the buffer to accommodate a larger tree
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
size: New size of tree
|
size: New size of tree
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
fdt = bytearray(size)
|
fdt = bytearray(size)
|
||||||
fdt[:len(self._fdtrw)] = self._fdtrw
|
err = check_err(fdt_resize(self._fdt, fdt, size))
|
||||||
err = check_err(fdt_resize(self._fdtrw, fdt, size), quiet)
|
self._fdt = fdt
|
||||||
if err:
|
|
||||||
return err
|
|
||||||
self._fdtrw = fdt
|
|
||||||
|
|
||||||
def add_reservemap_entry(self, addr, size, quiet=()):
|
def add_reservemap_entry(self, addr, size):
|
||||||
"""Add a new memory reserve map entry
|
"""Add a new memory reserve map entry
|
||||||
|
|
||||||
Once finished adding, you must call finish_reservemap().
|
Once finished adding, you must call finish_reservemap().
|
||||||
|
@ -772,26 +792,24 @@ class FdtSw(object):
|
||||||
Args:
|
Args:
|
||||||
addr: 64-bit start address
|
addr: 64-bit start address
|
||||||
size: 64-bit size
|
size: 64-bit size
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_add_reservemap_entry(self._fdtrw, addr, size),
|
while self.check_space(fdt_add_reservemap_entry(self._fdt, addr,
|
||||||
quiet)
|
size)):
|
||||||
|
pass
|
||||||
|
|
||||||
def finish_reservemap(self, quiet=()):
|
def finish_reservemap(self):
|
||||||
"""Indicate that there are no more reserve map entries to add
|
"""Indicate that there are no more reserve map entries to add
|
||||||
|
|
||||||
Args:
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_finish_reservemap(self._fdtrw), quiet)
|
while self.check_space(fdt_finish_reservemap(self._fdt)):
|
||||||
|
pass
|
||||||
|
|
||||||
def begin_node(self, name, quiet=()):
|
def begin_node(self, name):
|
||||||
"""Begin a new node
|
"""Begin a new node
|
||||||
|
|
||||||
Use this before adding properties to the node. Then call end_node() to
|
Use this before adding properties to the node. Then call end_node() to
|
||||||
|
@ -800,14 +818,14 @@ class FdtSw(object):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: Name of node to begin
|
name: Name of node to begin
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_begin_node(self._fdtrw, name), quiet)
|
while self.check_space(fdt_begin_node(self._fdt, name)):
|
||||||
|
pass
|
||||||
|
|
||||||
def property_string(self, name, string, quiet=()):
|
def property_string(self, name, string):
|
||||||
"""Add a property with a string value
|
"""Add a property with a string value
|
||||||
|
|
||||||
The string will be nul-terminated when written to the device tree
|
The string will be nul-terminated when written to the device tree
|
||||||
|
@ -815,14 +833,14 @@ class FdtSw(object):
|
||||||
Args:
|
Args:
|
||||||
name: Name of property to add
|
name: Name of property to add
|
||||||
string: String value of property
|
string: String value of property
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_property_string(self._fdtrw, name, string), quiet)
|
while self.check_space(fdt_property_string(self._fdt, name, string)):
|
||||||
|
pass
|
||||||
|
|
||||||
def property_u32(self, name, val, quiet=()):
|
def property_u32(self, name, val):
|
||||||
"""Add a property with a 32-bit value
|
"""Add a property with a 32-bit value
|
||||||
|
|
||||||
Write a single-cell value to the device tree
|
Write a single-cell value to the device tree
|
||||||
|
@ -830,14 +848,14 @@ class FdtSw(object):
|
||||||
Args:
|
Args:
|
||||||
name: Name of property to add
|
name: Name of property to add
|
||||||
val: Value of property
|
val: Value of property
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_property_u32(self._fdtrw, name, val), quiet)
|
while self.check_space(fdt_property_u32(self._fdt, name, val)):
|
||||||
|
pass
|
||||||
|
|
||||||
def property_u64(self, name, val, quiet=()):
|
def property_u64(self, name, val):
|
||||||
"""Add a property with a 64-bit value
|
"""Add a property with a 64-bit value
|
||||||
|
|
||||||
Write a double-cell value to the device tree in big-endian format
|
Write a double-cell value to the device tree in big-endian format
|
||||||
|
@ -845,14 +863,14 @@ class FdtSw(object):
|
||||||
Args:
|
Args:
|
||||||
name: Name of property to add
|
name: Name of property to add
|
||||||
val: Value of property
|
val: Value of property
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_property_u64(self._fdtrw, name, val), quiet)
|
while self.check_space(fdt_property_u64(self._fdt, name, val)):
|
||||||
|
pass
|
||||||
|
|
||||||
def property_cell(self, name, val, quiet=()):
|
def property_cell(self, name, val):
|
||||||
"""Add a property with a single-cell value
|
"""Add a property with a single-cell value
|
||||||
|
|
||||||
Write a single-cell value to the device tree
|
Write a single-cell value to the device tree
|
||||||
|
@ -863,11 +881,12 @@ class FdtSw(object):
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_property_cell(self._fdtrw, name, val), quiet)
|
while self.check_space(fdt_property_cell(self._fdt, name, val)):
|
||||||
|
pass
|
||||||
|
|
||||||
def property(self, name, val, quiet=()):
|
def property(self, name, val):
|
||||||
"""Add a property
|
"""Add a property
|
||||||
|
|
||||||
Write a new property with the given value to the device tree. The value
|
Write a new property with the given value to the device tree. The value
|
||||||
|
@ -879,11 +898,13 @@ class FdtSw(object):
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(_fdt_property(self._fdtrw, name, val, len(val)), quiet)
|
while self.check_space(fdt_property_stub(self._fdt, name, val,
|
||||||
|
len(val))):
|
||||||
|
pass
|
||||||
|
|
||||||
def end_node(self, quiet=()):
|
def end_node(self):
|
||||||
"""End a node
|
"""End a node
|
||||||
|
|
||||||
Use this after adding properties to a node to close it off. You can also
|
Use this after adding properties to a node to close it off. You can also
|
||||||
|
@ -893,24 +914,12 @@ class FdtSw(object):
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
quiet: Errors to ignore (empty to raise on all errors)
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FdtException if no node found or other error occurs
|
FdtException on any error
|
||||||
"""
|
"""
|
||||||
return check_err(fdt_end_node(self._fdtrw), quiet)
|
while self.check_space(fdt_end_node(self._fdt)):
|
||||||
|
pass
|
||||||
|
|
||||||
def finish(self, quiet=()):
|
def add_node(self, name):
|
||||||
"""Finish writing the device tree
|
|
||||||
|
|
||||||
This closes off the device tree ready for use
|
|
||||||
|
|
||||||
Args:
|
|
||||||
quiet: Errors to ignore (empty to raise on all errors)
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
FdtException if no node found or other error occurs
|
|
||||||
"""
|
|
||||||
return check_err(fdt_finish(self._fdtrw), quiet)
|
|
||||||
|
|
||||||
def AddNode(self, name):
|
|
||||||
"""Create a new context for adding a node
|
"""Create a new context for adding a node
|
||||||
|
|
||||||
When used in a 'with' clause this starts a new node and finishes it
|
When used in a 'with' clause this starts a new node and finishes it
|
||||||
|
@ -919,7 +928,7 @@ class FdtSw(object):
|
||||||
Args:
|
Args:
|
||||||
name: Name of node to add
|
name: Name of node to add
|
||||||
"""
|
"""
|
||||||
return NodeAdder(self._fdtrw, name)
|
return NodeAdder(self, name)
|
||||||
|
|
||||||
|
|
||||||
class NodeAdder():
|
class NodeAdder():
|
||||||
|
@ -927,26 +936,30 @@ class NodeAdder():
|
||||||
|
|
||||||
This allows you to add nodes in a more natural way:
|
This allows you to add nodes in a more natural way:
|
||||||
|
|
||||||
with fdtsw.AddNode('name'):
|
with fdtsw.add_node('name'):
|
||||||
fdtsw.property_string('test', 'value')
|
fdtsw.property_string('test', 'value')
|
||||||
|
|
||||||
The node is automatically completed with a call to end_node() when the
|
The node is automatically completed with a call to end_node() when the
|
||||||
context exits.
|
context exits.
|
||||||
"""
|
"""
|
||||||
def __init__(self, fdt, name):
|
def __init__(self, fdtsw, name):
|
||||||
self._fdt = fdt
|
self._fdt = fdtsw
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
check_err(fdt_begin_node(self._fdt, self._name))
|
self._fdt.begin_node(self._name)
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
check_err(fdt_end_node(self._fdt))
|
self._fdt.end_node()
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%rename(fdt_property) fdt_property_func;
|
%rename(fdt_property) fdt_property_func;
|
||||||
|
|
||||||
typedef int fdt32_t;
|
/*
|
||||||
|
* fdt32_t is a big-endian 32-bit value defined to uint32_t in libfdt_env.h
|
||||||
|
* so use the same type here.
|
||||||
|
*/
|
||||||
|
typedef uint32_t fdt32_t;
|
||||||
|
|
||||||
%include "libfdt/fdt.h"
|
%include "libfdt/fdt.h"
|
||||||
|
|
||||||
|
@ -1039,16 +1052,17 @@ typedef int fdt32_t;
|
||||||
%warnfilter(302) fdt_property;
|
%warnfilter(302) fdt_property;
|
||||||
|
|
||||||
/* These are macros in the header so have to be redefined here */
|
/* These are macros in the header so have to be redefined here */
|
||||||
int fdt_magic(const void *fdt);
|
uint32_t fdt_magic(const void *fdt);
|
||||||
int fdt_totalsize(const void *fdt);
|
uint32_t fdt_totalsize(const void *fdt);
|
||||||
int fdt_off_dt_struct(const void *fdt);
|
uint32_t fdt_off_dt_struct(const void *fdt);
|
||||||
int fdt_off_dt_strings(const void *fdt);
|
uint32_t fdt_off_dt_strings(const void *fdt);
|
||||||
int fdt_off_mem_rsvmap(const void *fdt);
|
uint32_t fdt_off_mem_rsvmap(const void *fdt);
|
||||||
int fdt_version(const void *fdt);
|
uint32_t fdt_version(const void *fdt);
|
||||||
int fdt_last_comp_version(const void *fdt);
|
uint32_t fdt_last_comp_version(const void *fdt);
|
||||||
int fdt_boot_cpuid_phys(const void *fdt);
|
uint32_t fdt_boot_cpuid_phys(const void *fdt);
|
||||||
int fdt_size_dt_strings(const void *fdt);
|
uint32_t fdt_size_dt_strings(const void *fdt);
|
||||||
int fdt_size_dt_struct(const void *fdt);
|
uint32_t fdt_size_dt_struct(const void *fdt);
|
||||||
|
|
||||||
int fdt_property_string(void *fdt, const char *name, const char *val);
|
int fdt_property_string(void *fdt, const char *name, const char *val);
|
||||||
int fdt_property_cell(void *fdt, const char *name, uint32_t val);
|
int fdt_property_cell(void *fdt, const char *name, uint32_t val);
|
||||||
|
|
||||||
|
@ -1056,6 +1070,6 @@ int fdt_property_cell(void *fdt, const char *name, uint32_t val);
|
||||||
* This function has a stub since the name fdt_property is used for both a
|
* This function has a stub since the name fdt_property is used for both a
|
||||||
* function and a struct, which confuses SWIG.
|
* function and a struct, which confuses SWIG.
|
||||||
*/
|
*/
|
||||||
int _fdt_property(void *fdt, const char *name, const char *val, int len);
|
int fdt_property_stub(void *fdt, const char *name, const char *val, int len);
|
||||||
|
|
||||||
%include <../libfdt/libfdt.h>
|
%include <../libfdt/libfdt.h>
|
||||||
|
|
|
@ -287,7 +287,7 @@ class Node:
|
||||||
fdt_obj = self._fdt._fdt_obj
|
fdt_obj = self._fdt._fdt_obj
|
||||||
if fdt_obj.setprop_u32(self.Offset(), prop_name, 0,
|
if fdt_obj.setprop_u32(self.Offset(), prop_name, 0,
|
||||||
(libfdt.NOSPACE,)) == -libfdt.NOSPACE:
|
(libfdt.NOSPACE,)) == -libfdt.NOSPACE:
|
||||||
fdt_obj.open_into(fdt_obj.totalsize() + 1024)
|
fdt_obj.resize(fdt_obj.totalsize() + 1024)
|
||||||
fdt_obj.setprop_u32(self.Offset(), prop_name, 0)
|
fdt_obj.setprop_u32(self.Offset(), prop_name, 0)
|
||||||
self.props[prop_name] = Prop(self, -1, prop_name, '\0' * 4)
|
self.props[prop_name] = Prop(self, -1, prop_name, '\0' * 4)
|
||||||
self._fdt.Invalidate()
|
self._fdt.Invalidate()
|
||||||
|
|
|
@ -233,7 +233,7 @@ class TestProp(unittest.TestCase):
|
||||||
|
|
||||||
Return fdt.Prop object for this property
|
Return fdt.Prop object for this property
|
||||||
"""
|
"""
|
||||||
p = self.fdt.get_property(self.node.Offset(), prop_name)
|
p = self.fdt.getprop(self.node.Offset(), prop_name)
|
||||||
return fdt.Prop(self.node, -1, prop_name, p)
|
return fdt.Prop(self.node, -1, prop_name, p)
|
||||||
|
|
||||||
def testMakeProp(self):
|
def testMakeProp(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue