self.props = self._fdt.GetProps(self)
phandle = fdt_obj.get_phandle(self.Offset())
if phandle:
+ dup = self._fdt.phandle_to_node.get(phandle)
+ if dup:
+ raise ValueError(
+ f'Duplicate phandle {phandle} in nodes {dup.path} and {self.path}')
+
self._fdt.phandle_to_node[phandle] = self
offset = fdt_obj.first_subnode(self.Offset(), QUIET_NOTFOUND)
prop.Sync(auto_resize)
return added
- def merge_props(self, src):
+ def merge_props(self, src, copy_phandles):
"""Copy missing properties (except 'phandle') from another node
Args:
src (Node): Node containing properties to copy
+ copy_phandles (bool): True to copy phandle properties in nodes
Adds properties which are present in src but not in this node. Any
'phandle' property is not copied since this might result in two nodes
tout.debug(f'copy to {self.path}: {src.path}')
for name, src_prop in src.props.items():
done = False
- if name != 'phandle' and name not in self.props:
- self.props[name] = Prop(self, None, name, src_prop.bytes)
- done = True
+ if name not in self.props:
+ if copy_phandles or name != 'phandle':
+ self.props[name] = Prop(self, None, name, src_prop.bytes)
+ done = True
tout.debug(f" {name}{'' if done else ' - ignored'}")
- def copy_node(self, src):
+ def copy_node(self, src, copy_phandles=False):
"""Copy a node and all its subnodes into this node
Args:
src (Node): Node to copy
+ copy_phandles (bool): True to copy phandle properties in nodes
Returns:
Node: Resulting destination node
- This works recursively.
+ This works recursively, with copy_phandles being set to True for the
+ recursive calls
The new node is put before all other nodes. If the node already
exists, just its subnodes and properties are copied, placing them before
dst.move_to_first()
else:
dst = self.insert_subnode(src.name)
- dst.merge_props(src)
+ dst.merge_props(src, copy_phandles)
# Process in reverse order so that they appear correctly in the result,
# since copy_node() puts the node first in the list
for node in reversed(src.subnodes):
- dst.copy_node(node)
+ dst.copy_node(node, True)
return dst
def copy_subnodes_from_phandles(self, phandle_list):
dst = self.copy_node(node)
tout.debug(f'merge props from {parent.path} to {dst.path}')
- self.merge_props(parent)
+ self.merge_props(parent, False)
class Fdt:
TODO(sjg@chromium.org): Implement the 'root' parameter
"""
+ self.phandle_to_node = {}
self._cached_offsets = True
self._root = self.Node(self, None, 0, '/', '/')
self._root.Scan()
over = dtb.GetNode('/dest/base/over')
self.assertTrue(over)
- # Make sure that the phandle for 'over' is not copied
- self.assertNotIn('phandle', over.props.keys())
+ # Make sure that the phandle for 'over' is copied
+ self.assertIn('phandle', over.props.keys())
second = dtb.GetNode('/dest/base/second')
self.assertTrue(second)
[n.name for n in chk.subnodes])
self.assertEqual(chk, over.parent)
self.assertEqual(
- {'bootph-all', 'compatible', 'reg', 'low-power'},
+ {'bootph-all', 'compatible', 'reg', 'low-power', 'phandle'},
over.props.keys())
if expect_none:
dtb.Sync(auto_resize=True)
- # Now check that the FDT looks correct
+ # Now check the resulting FDT. It should have duplicate phandles since
+ # 'over' has been copied to 'dest/base/over' but still exists in its old
+ # place
new_dtb = fdt.Fdt.FromData(dtb.GetContents())
+ with self.assertRaises(ValueError) as exc:
+ new_dtb.Scan()
+ self.assertIn(
+ 'Duplicate phandle 1 in nodes /dest/base/over and /base/over',
+ str(exc.exception))
+
+ # Remove the source nodes for the copy
+ new_dtb.GetNode('/base').Delete()
+
+ # Now it should scan OK
new_dtb.Scan()
+
dst = new_dtb.GetNode('/dest')
do_copy_checks(new_dtb, dst, second1_ph_val, expect_none=False)