mirror of
https://github.com/Fishwaldo/u-boot.git
synced 2025-03-28 01:51:33 +00:00
dtoc: Add a function to obtain a list of phandles
Add a function which can decode a property containing a list of phandles. This is useful for finding nodes linked to a property. Also provide a way to look up a single phandle and get the Fdt object from a Node. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
4f5dea4543
commit
94a7c603b4
3 changed files with 57 additions and 0 deletions
|
@ -181,6 +181,14 @@ class Node:
|
||||||
self.subnodes = []
|
self.subnodes = []
|
||||||
self.props = {}
|
self.props = {}
|
||||||
|
|
||||||
|
def GetFdt(self):
|
||||||
|
"""Get the Fdt object for this node
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Fdt object
|
||||||
|
"""
|
||||||
|
return self._fdt
|
||||||
|
|
||||||
def FindNode(self, name):
|
def FindNode(self, name):
|
||||||
"""Find a node given its name
|
"""Find a node given its name
|
||||||
|
|
||||||
|
@ -314,6 +322,17 @@ class Fdt:
|
||||||
with open(self._fname) as fd:
|
with open(self._fname) as fd:
|
||||||
self._fdt_obj = libfdt.Fdt(fd.read())
|
self._fdt_obj = libfdt.Fdt(fd.read())
|
||||||
|
|
||||||
|
def LookupPhandle(self, phandle):
|
||||||
|
"""Look up a phandle
|
||||||
|
|
||||||
|
Args:
|
||||||
|
phandle: Phandle to look up (int)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Node object the phandle points to
|
||||||
|
"""
|
||||||
|
return self.phandle_to_node.get(phandle)
|
||||||
|
|
||||||
def Scan(self, root='/'):
|
def Scan(self, root='/'):
|
||||||
"""Scan a device tree, building up a tree of Node objects
|
"""Scan a device tree, building up a tree of Node objects
|
||||||
|
|
||||||
|
|
|
@ -171,6 +171,24 @@ def GetByte(node, propname, default=None):
|
||||||
(node.name, propname, len(value), 1))
|
(node.name, propname, len(value), 1))
|
||||||
return ord(value[0])
|
return ord(value[0])
|
||||||
|
|
||||||
|
def GetPhandleList(node, propname):
|
||||||
|
"""Get a list of phandles from a property
|
||||||
|
|
||||||
|
Args:
|
||||||
|
node: Node object to read from
|
||||||
|
propname: property name to read
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of phandles read, each an integer
|
||||||
|
"""
|
||||||
|
prop = node.props.get(propname)
|
||||||
|
if not prop:
|
||||||
|
return None
|
||||||
|
value = prop.value
|
||||||
|
if not isinstance(value, list):
|
||||||
|
value = [value]
|
||||||
|
return [fdt32_to_cpu(v) for v in value]
|
||||||
|
|
||||||
def GetDatatype(node, propname, datatype):
|
def GetDatatype(node, propname, datatype):
|
||||||
"""Get a value of a given type from a property
|
"""Get a value of a given type from a property
|
||||||
|
|
||||||
|
|
|
@ -115,6 +115,9 @@ class TestFdt(unittest.TestCase):
|
||||||
fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
|
fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
|
||||||
self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
|
self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
|
||||||
|
|
||||||
|
def testGetFdt(self):
|
||||||
|
node = self.dtb.GetNode('/spl-test')
|
||||||
|
self.assertEqual(self.dtb, node.GetFdt())
|
||||||
|
|
||||||
class TestNode(unittest.TestCase):
|
class TestNode(unittest.TestCase):
|
||||||
"""Test operation of the Node class"""
|
"""Test operation of the Node class"""
|
||||||
|
@ -188,6 +191,14 @@ class TestNode(unittest.TestCase):
|
||||||
self.assertIn("Internal error, property 'notstring' missing, offset ",
|
self.assertIn("Internal error, property 'notstring' missing, offset ",
|
||||||
str(e.exception))
|
str(e.exception))
|
||||||
|
|
||||||
|
def testLookupPhandle(self):
|
||||||
|
"""Test looking up a single phandle"""
|
||||||
|
dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
|
||||||
|
node = dtb.GetNode('/phandle-source2')
|
||||||
|
prop = node.props['clocks']
|
||||||
|
target = dtb.GetNode('/phandle-target')
|
||||||
|
self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value)))
|
||||||
|
|
||||||
|
|
||||||
class TestProp(unittest.TestCase):
|
class TestProp(unittest.TestCase):
|
||||||
"""Test operation of the Prop class"""
|
"""Test operation of the Prop class"""
|
||||||
|
@ -394,6 +405,15 @@ class TestFdtUtil(unittest.TestCase):
|
||||||
self.assertIn("property 'intval' has length 4, expecting 1",
|
self.assertIn("property 'intval' has length 4, expecting 1",
|
||||||
str(e.exception))
|
str(e.exception))
|
||||||
|
|
||||||
|
def testGetPhandleList(self):
|
||||||
|
dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
|
||||||
|
node = dtb.GetNode('/phandle-source2')
|
||||||
|
self.assertEqual([1], fdt_util.GetPhandleList(node, 'clocks'))
|
||||||
|
node = dtb.GetNode('/phandle-source')
|
||||||
|
self.assertEqual([1, 2, 11, 3, 12, 13, 1],
|
||||||
|
fdt_util.GetPhandleList(node, 'clocks'))
|
||||||
|
self.assertEqual(None, fdt_util.GetPhandleList(node, 'missing'))
|
||||||
|
|
||||||
def testGetDataType(self):
|
def testGetDataType(self):
|
||||||
self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int))
|
self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int))
|
||||||
self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval',
|
self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval',
|
||||||
|
|
Loading…
Add table
Reference in a new issue