state.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in
  17. # ftest.py)
  18. use_fake_dtb = True
  19. # Set of all device tree files references by images
  20. fdt_set = Set()
  21. # Same as above, but excluding the main one
  22. fdt_subset = Set()
  23. # The DTB which contains the full image information
  24. main_dtb = None
  25. def GetFdt(fname):
  26. """Get the Fdt object for a particular device-tree filename
  27. Binman keeps track of at least one device-tree file called u-boot.dtb but
  28. can also have others (e.g. for SPL). This function looks up the given
  29. filename and returns the associated Fdt object.
  30. Args:
  31. fname: Filename to look up (e.g. 'u-boot.dtb').
  32. Returns:
  33. Fdt object associated with the filename
  34. """
  35. return fdt_files[fname]
  36. def GetFdtPath(fname):
  37. """Get the full pathname of a particular Fdt object
  38. Similar to GetFdt() but returns the pathname associated with the Fdt.
  39. Args:
  40. fname: Filename to look up (e.g. 'u-boot.dtb').
  41. Returns:
  42. Full path name to the associated Fdt
  43. """
  44. return fdt_files[fname]._fname
  45. def SetEntryArgs(args):
  46. """Set the value of the entry args
  47. This sets up the entry_args dict which is used to supply entry arguments to
  48. entries.
  49. Args:
  50. args: List of entry arguments, each in the format "name=value"
  51. """
  52. global entry_args
  53. entry_args = {}
  54. if args:
  55. for arg in args:
  56. m = re.match('([^=]*)=(.*)', arg)
  57. if not m:
  58. raise ValueError("Invalid entry arguemnt '%s'" % arg)
  59. entry_args[m.group(1)] = m.group(2)
  60. def GetEntryArg(name):
  61. """Get the value of an entry argument
  62. Args:
  63. name: Name of argument to retrieve
  64. Returns:
  65. String value of argument
  66. """
  67. return entry_args.get(name)
  68. def Prepare(images, dtb):
  69. """Get device tree files ready for use
  70. This sets up a set of device tree files that can be retrieved by GetFdts().
  71. At present there is only one, that for U-Boot proper.
  72. Args:
  73. images: List of images being used
  74. dtb: Main dtb
  75. """
  76. global fdt_set, fdt_subset, fdt_files, main_dtb
  77. # Import these here in case libfdt.py is not available, in which case
  78. # the above help option still works.
  79. import fdt
  80. import fdt_util
  81. # If we are updating the DTBs we need to put these updated versions
  82. # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb'
  83. # since it is assumed to be the one passed in with options.dt, and
  84. # was handled just above.
  85. main_dtb = dtb
  86. fdt_files.clear()
  87. fdt_files['u-boot.dtb'] = dtb
  88. fdt_subset = Set()
  89. if not use_fake_dtb:
  90. for image in images.values():
  91. fdt_subset.update(image.GetFdtSet())
  92. fdt_subset.discard('u-boot.dtb')
  93. for other_fname in fdt_subset:
  94. infile = tools.GetInputFilename(other_fname)
  95. other_fname_dtb = fdt_util.EnsureCompiled(infile)
  96. out_fname = tools.GetOutputFilename('%s.out' %
  97. os.path.split(other_fname)[1])
  98. tools.WriteFile(out_fname, tools.ReadFile(other_fname_dtb))
  99. other_dtb = fdt.FdtScan(out_fname)
  100. fdt_files[other_fname] = other_dtb
  101. def GetFdts():
  102. """Yield all device tree files being used by binman
  103. Yields:
  104. Device trees being used (U-Boot proper, SPL, TPL)
  105. """
  106. yield main_dtb
  107. def GetUpdateNodes(node):
  108. """Yield all the nodes that need to be updated in all device trees
  109. The property referenced by this node is added to any device trees which
  110. have the given node. Due to removable of unwanted notes, SPL and TPL may
  111. not have this node.
  112. Args:
  113. node: Node object in the main device tree to look up
  114. Yields:
  115. Node objects in each device tree that is in use (U-Boot proper, which
  116. is node, SPL and TPL)
  117. """
  118. yield node
  119. def AddZeroProp(node, prop):
  120. """Add a new property to affected device trees with an integer value of 0.
  121. Args:
  122. prop_name: Name of property
  123. """
  124. for n in GetUpdateNodes(node):
  125. n.AddZeroProp(prop)
  126. def SetInt(node, prop, value):
  127. """Update an integer property in affected device trees with an integer value
  128. This is not allowed to change the size of the FDT.
  129. Args:
  130. prop_name: Name of property
  131. """
  132. for n in GetUpdateNodes(node):
  133. n.SetInt(prop, value)