state.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Holds and modifies the state information held by binman
  6. #
  7. import re
  8. from sets import Set
  9. import os
  10. import tools
  11. # Records the device-tree files known to binman, keyed by filename (e.g.
  12. # 'u-boot-spl.dtb')
  13. fdt_files = {}
  14. # Arguments passed to binman to provide arguments to entries
  15. entry_args = {}
  16. # Set of all device tree files references by images
  17. fdt_set = Set()
  18. # Same as above, but excluding the main one
  19. fdt_subset = Set()
  20. # The DTB which contains the full image information
  21. main_dtb = None
  22. def GetFdt(fname):
  23. """Get the Fdt object for a particular device-tree filename
  24. Binman keeps track of at least one device-tree file called u-boot.dtb but
  25. can also have others (e.g. for SPL). This function looks up the given
  26. filename and returns the associated Fdt object.
  27. Args:
  28. fname: Filename to look up (e.g. 'u-boot.dtb').
  29. Returns:
  30. Fdt object associated with the filename
  31. """
  32. return fdt_files[fname]
  33. def GetFdtPath(fname):
  34. """Get the full pathname of a particular Fdt object
  35. Similar to GetFdt() but returns the pathname associated with the Fdt.
  36. Args:
  37. fname: Filename to look up (e.g. 'u-boot.dtb').
  38. Returns:
  39. Full path name to the associated Fdt
  40. """
  41. return fdt_files[fname]._fname
  42. def SetEntryArgs(args):
  43. """Set the value of the entry args
  44. This sets up the entry_args dict which is used to supply entry arguments to
  45. entries.
  46. Args:
  47. args: List of entry arguments, each in the format "name=value"
  48. """
  49. global entry_args
  50. entry_args = {}
  51. if args:
  52. for arg in args:
  53. m = re.match('([^=]*)=(.*)', arg)
  54. if not m:
  55. raise ValueError("Invalid entry arguemnt '%s'" % arg)
  56. entry_args[m.group(1)] = m.group(2)
  57. def GetEntryArg(name):
  58. """Get the value of an entry argument
  59. Args:
  60. name: Name of argument to retrieve
  61. Returns:
  62. String value of argument
  63. """
  64. return entry_args.get(name)
  65. def Prepare(dtb):
  66. """Get device tree files ready for use
  67. This sets up a set of device tree files that can be retrieved by GetFdts().
  68. At present there is only one, that for U-Boot proper.
  69. Args:
  70. dtb: Main dtb
  71. """
  72. global fdt_set, fdt_subset, fdt_files, main_dtb
  73. # Import these here in case libfdt.py is not available, in which case
  74. # the above help option still works.
  75. import fdt
  76. import fdt_util
  77. # If we are updating the DTBs we need to put these updated versions
  78. # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb'
  79. # since it is assumed to be the one passed in with options.dt, and
  80. # was handled just above.
  81. main_dtb = dtb
  82. fdt_files.clear()
  83. fdt_files['u-boot.dtb'] = dtb
  84. fdt_set = Set()
  85. fdt_subset = Set()
  86. def GetFdts():
  87. """Yield all device tree files being used by binman
  88. Yields:
  89. Device trees being used (U-Boot proper, SPL, TPL)
  90. """
  91. yield main_dtb
  92. def GetUpdateNodes(node):
  93. """Yield all the nodes that need to be updated in all device trees
  94. The property referenced by this node is added to any device trees which
  95. have the given node. Due to removable of unwanted notes, SPL and TPL may
  96. not have this node.
  97. Args:
  98. node: Node object in the main device tree to look up
  99. Yields:
  100. Node objects in each device tree that is in use (U-Boot proper, which
  101. is node, SPL and TPL)
  102. """
  103. yield node
  104. def AddZeroProp(node, prop):
  105. """Add a new property to affected device trees with an integer value of 0.
  106. Args:
  107. prop_name: Name of property
  108. """
  109. for n in GetUpdateNodes(node):
  110. n.AddZeroProp(prop)
  111. def SetInt(node, prop, value):
  112. """Update an integer property in affected device trees with an integer value
  113. This is not allowed to change the size of the FDT.
  114. Args:
  115. prop_name: Name of property
  116. """
  117. for n in GetUpdateNodes(node):
  118. n.SetInt(prop, value)