fdt.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # SPDX-License-Identifier: GPL-2.0+
  7. #
  8. import struct
  9. import sys
  10. import fdt_util
  11. # This deals with a device tree, presenting it as an assortment of Node and
  12. # Prop objects, representing nodes and properties, respectively. This file
  13. # contains the base classes and defines the high-level API. Most of the
  14. # implementation is in the FdtFallback and FdtNormal subclasses. See
  15. # fdt_select.py for how to create an Fdt object.
  16. def CheckErr(errnum, msg):
  17. if errnum:
  18. raise ValueError('Error %d: %s: %s' %
  19. (errnum, libfdt.fdt_strerror(errnum), msg))
  20. class PropBase:
  21. """A device tree property
  22. Properties:
  23. name: Property name (as per the device tree)
  24. value: Property value as a string of bytes, or a list of strings of
  25. bytes
  26. type: Value type
  27. """
  28. def __init__(self, node, offset, name):
  29. self._node = node
  30. self._offset = offset
  31. self.name = name
  32. self.value = None
  33. class NodeBase:
  34. """A device tree node
  35. Properties:
  36. offset: Integer offset in the device tree
  37. name: Device tree node tname
  38. path: Full path to node, along with the node name itself
  39. _fdt: Device tree object
  40. subnodes: A list of subnodes for this node, each a Node object
  41. props: A dict of properties for this node, each a Prop object.
  42. Keyed by property name
  43. """
  44. def __init__(self, fdt, offset, name, path):
  45. self._fdt = fdt
  46. self._offset = offset
  47. self.name = name
  48. self.path = path
  49. self.subnodes = []
  50. self.props = {}
  51. class Fdt:
  52. """Provides simple access to a flat device tree blob.
  53. Properties:
  54. fname: Filename of fdt
  55. _root: Root of device tree (a Node object)
  56. """
  57. def __init__(self, fname):
  58. self._fname = fname