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>main
parent
49d32ce40b
commit
3c374d46ac
|
@ -488,7 +488,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 of bytes, 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)
|
||||||
|
@ -497,8 +499,7 @@ class Fdt:
|
||||||
quiet)
|
quiet)
|
||||||
if isinstance(pdata, (int)):
|
if isinstance(pdata, (int)):
|
||||||
return pdata
|
return pdata
|
||||||
# Use bytes() rather than string(). This works on both Python 2 and 3
|
return Property(prop_name, bytearray(pdata[0]))
|
||||||
return bytes(pdata[0])
|
|
||||||
|
|
||||||
def get_phandle(self, nodeoffset):
|
def get_phandle(self, nodeoffset):
|
||||||
"""Get the phandle of a node
|
"""Get the phandle of a node
|
||||||
|
@ -623,6 +624,21 @@ class Property(bytearray):
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
bytearray.__init__(self, value)
|
bytearray.__init__(self, value)
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
def as_cell(self, fmt):
|
||||||
|
return struct.unpack('>' + fmt, self)[0]
|
||||||
|
|
||||||
|
def as_uint32(self):
|
||||||
|
return self.as_cell('L')
|
||||||
|
|
||||||
|
def as_int32(self):
|
||||||
|
return self.as_cell('l')
|
||||||
|
|
||||||
|
def as_uint64(self):
|
||||||
|
return self.as_cell('Q')
|
||||||
|
|
||||||
|
def as_int64(self):
|
||||||
|
return self.as_cell('q')
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%rename(fdt_property) fdt_property_func;
|
%rename(fdt_property) fdt_property_func;
|
||||||
|
|
|
@ -102,6 +102,7 @@ class PyLibfdtTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Read in the device tree we use for testing"""
|
"""Read in the device tree we use for testing"""
|
||||||
self.fdt = _ReadFdt('test_tree1.dtb')
|
self.fdt = _ReadFdt('test_tree1.dtb')
|
||||||
|
self.fdt2 = _ReadFdt('test_props.dtb')
|
||||||
|
|
||||||
def GetPropList(self, node_path):
|
def GetPropList(self, node_path):
|
||||||
"""Read a list of properties from a node
|
"""Read a list of properties from a node
|
||||||
|
@ -368,6 +369,17 @@ class PyLibfdtTests(unittest.TestCase):
|
||||||
node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
|
node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
|
||||||
self.assertEquals(node2, self.fdt.node_offset_by_phandle(0x2001))
|
self.assertEquals(node2, self.fdt.node_offset_by_phandle(0x2001))
|
||||||
|
|
||||||
|
def get_prop(self, name):
|
||||||
|
return self.fdt2.getprop(0, name)
|
||||||
|
|
||||||
|
def testGetIntProperties(self):
|
||||||
|
"""Test that we can access properties as integers"""
|
||||||
|
self.assertEquals(0xdeadbeef, self.get_prop("prop-hex32").as_uint32())
|
||||||
|
self.assertEquals(123, self.get_prop("prop-uint32").as_uint32())
|
||||||
|
self.assertEquals(-2, self.get_prop("prop-int32").as_int32())
|
||||||
|
self.assertEquals(9223372036854775807,
|
||||||
|
self.get_prop("prop-uint64").as_uint64())
|
||||||
|
self.assertEquals(-2, self.get_prop("prop-int64").as_int64())
|
||||||
|
|
||||||
def testReserveMap(self):
|
def testReserveMap(self):
|
||||||
"""Test that we can access the memory reserve map"""
|
"""Test that we can access the memory reserve map"""
|
||||||
|
|
|
@ -888,6 +888,7 @@ fdtoverlay_tests() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pylibfdt_tests () {
|
pylibfdt_tests () {
|
||||||
|
run_dtc_test -I dts -O dtb -o test_props.dtb test_props.dts
|
||||||
TMP=/tmp/tests.stderr.$$
|
TMP=/tmp/tests.stderr.$$
|
||||||
python pylibfdt_tests.py -v 2> $TMP
|
python pylibfdt_tests.py -v 2> $TMP
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
/dts-v1/;
|
||||||
|
|
||||||
|
/ {
|
||||||
|
compatible = "test_props";
|
||||||
|
prop-hex32 = <0xdeadbeef>;
|
||||||
|
prop-uint32 = <123>;
|
||||||
|
prop-int32 = <0xfffffffe>;
|
||||||
|
prop-hex64 = /bits/ 64 <0xdeadbeef01abcdef>;
|
||||||
|
prop-uint64 = /bits/ 64 <9223372036854775807>;
|
||||||
|
prop-int64 = /bits/ 64 <0xfffffffffffffffe>;
|
||||||
|
};
|
Loading…
Reference in New Issue