aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSimon Glass2018-09-14 05:57:16 -0500
committerSimon Glass2018-09-28 12:09:01 -0500
commit6434961b2b2099b458e7dc9dcced5d450b45cbb4 (patch)
tree27018b698249bbc4b82d0a922384f11bdad0f3ae /tools
parente21c27af47183619c7ec7097abb1dec34410e775 (diff)
downloadu-boot-6434961b2b2099b458e7dc9dcced5d450b45cbb4.tar.gz
u-boot-6434961b2b2099b458e7dc9dcced5d450b45cbb4.tar.xz
u-boot-6434961b2b2099b458e7dc9dcced5d450b45cbb4.zip
dtoc: Add methods for adding and updating properties
Add a few more functions which allow creating and modifying property values. If only we could do this so easily in the real world. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/dtoc/fdt.py70
-rwxr-xr-xtools/dtoc/test_fdt.py43
2 files changed, 113 insertions, 0 deletions
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index 26f4a4ee56..c5054e8cc9 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -175,6 +175,16 @@ class Prop:
175 self.type = TYPE_INT 175 self.type = TYPE_INT
176 self.dirty = True 176 self.dirty = True
177 177
178 def SetData(self, bytes):
179 """Set the value of a property as bytes
180
181 Args:
182 bytes: New property value to set
183 """
184 self.bytes = str(bytes)
185 self.type, self.value = self.BytesToValue(bytes)
186 self.dirty = True
187
178 def Sync(self, auto_resize=False): 188 def Sync(self, auto_resize=False):
179 """Sync property changes back to the device tree 189 """Sync property changes back to the device tree
180 190
@@ -326,18 +336,78 @@ class Node:
326 """ 336 """
327 self.props[prop_name] = Prop(self, None, prop_name, '\0' * 4) 337 self.props[prop_name] = Prop(self, None, prop_name, '\0' * 4)
328 338
339 def AddEmptyProp(self, prop_name, len):
340 """Add a property with a fixed data size, for filling in later
341
342 The device tree is marked dirty so that the value will be written to
343 the blob on the next sync.
344
345 Args:
346 prop_name: Name of property
347 len: Length of data in property
348 """
349 value = chr(0) * len
350 self.props[prop_name] = Prop(self, None, prop_name, value)
351
329 def SetInt(self, prop_name, val): 352 def SetInt(self, prop_name, val):
330 """Update an integer property int the device tree. 353 """Update an integer property int the device tree.
331 354
332 This is not allowed to change the size of the FDT. 355 This is not allowed to change the size of the FDT.
333 356
357 The device tree is marked dirty so that the value will be written to
358 the blob on the next sync.
359
334 Args: 360 Args:
335 prop_name: Name of property 361 prop_name: Name of property
336 val: Value to set 362 val: Value to set
337 """ 363 """
338 self.props[prop_name].SetInt(val) 364 self.props[prop_name].SetInt(val)
339 365
366 def SetData(self, prop_name, val):
367 """Set the data value of a property
368
369 The device tree is marked dirty so that the value will be written to
370 the blob on the next sync.
371
372 Args:
373 prop_name: Name of property to set
374 val: Data value to set
375 """
376 self.props[prop_name].SetData(val)
377
378 def SetString(self, prop_name, val):
379 """Set the string value of a property
380
381 The device tree is marked dirty so that the value will be written to
382 the blob on the next sync.
383
384 Args:
385 prop_name: Name of property to set
386 val: String value to set (will be \0-terminated in DT)
387 """
388 self.props[prop_name].SetData(val + chr(0))
389
390 def AddString(self, prop_name, val):
391 """Add a new string property to a node
392
393 The device tree is marked dirty so that the value will be written to
394 the blob on the next sync.
395
396 Args:
397 prop_name: Name of property to add
398 val: String value of property
399 """
400 self.props[prop_name] = Prop(self, None, prop_name, val + chr(0))
401
340 def AddSubnode(self, name): 402 def AddSubnode(self, name):
403 """Add a new subnode to the node
404
405 Args:
406 name: name of node to add
407
408 Returns:
409 New subnode that was created
410 """
341 path = self.path + '/' + name 411 path = self.path + '/' + name
342 subnode = Node(self._fdt, self, None, name, path) 412 subnode = Node(self._fdt, self, None, name, path)
343 self.subnodes.append(subnode) 413 self.subnodes.append(subnode)
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index c94e455d12..22a075d6c5 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -352,6 +352,7 @@ class TestProp(unittest.TestCase):
352 with self.assertRaises(libfdt.FdtException) as e: 352 with self.assertRaises(libfdt.FdtException) as e:
353 self.dtb.Sync(auto_resize=False) 353 self.dtb.Sync(auto_resize=False)
354 self.assertIn('FDT_ERR_NOSPACE', str(e.exception)) 354 self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
355 self.dtb.Sync(auto_resize=True)
355 356
356 def testAddNode(self): 357 def testAddNode(self):
357 self.fdt.pack() 358 self.fdt.pack()
@@ -364,6 +365,48 @@ class TestProp(unittest.TestCase):
364 offset = self.fdt.path_offset('/spl-test/subnode') 365 offset = self.fdt.path_offset('/spl-test/subnode')
365 self.assertTrue(offset > 0) 366 self.assertTrue(offset > 0)
366 367
368 def testAddMore(self):
369 """Test various other methods for adding and setting properties"""
370 self.node.AddZeroProp('one')
371 self.dtb.Sync(auto_resize=True)
372 data = self.fdt.getprop(self.node.Offset(), 'one')
373 self.assertEqual(0, fdt32_to_cpu(data))
374
375 self.node.SetInt('one', 1)
376 self.dtb.Sync(auto_resize=False)
377 data = self.fdt.getprop(self.node.Offset(), 'one')
378 self.assertEqual(1, fdt32_to_cpu(data))
379
380 val = '123' + chr(0) + '456'
381 self.node.AddString('string', val)
382 self.dtb.Sync(auto_resize=True)
383 data = self.fdt.getprop(self.node.Offset(), 'string')
384 self.assertEqual(val + '\0', data)
385
386 self.fdt.pack()
387 self.node.SetString('string', val + 'x')
388 with self.assertRaises(libfdt.FdtException) as e:
389 self.dtb.Sync(auto_resize=False)
390 self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
391 self.node.SetString('string', val[:-1])
392
393 prop = self.node.props['string']
394 prop.SetData(val)
395 self.dtb.Sync(auto_resize=False)
396 data = self.fdt.getprop(self.node.Offset(), 'string')
397 self.assertEqual(val, data)
398
399 self.node.AddEmptyProp('empty', 5)
400 self.dtb.Sync(auto_resize=True)
401 prop = self.node.props['empty']
402 prop.SetData(val)
403 self.dtb.Sync(auto_resize=False)
404 data = self.fdt.getprop(self.node.Offset(), 'empty')
405 self.assertEqual(val, data)
406
407 self.node.SetData('empty', '123')
408 self.assertEqual('123', prop.bytes)
409
367 410
368class TestFdtUtil(unittest.TestCase): 411class TestFdtUtil(unittest.TestCase):
369 """Tests for the fdt_util module 412 """Tests for the fdt_util module