control.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Creates binary images from input files controlled by a description
  6. #
  7. from collections import OrderedDict
  8. import os
  9. import re
  10. import sys
  11. import tools
  12. import command
  13. import elf
  14. from image import Image
  15. import tout
  16. # List of images we plan to create
  17. # Make this global so that it can be referenced from tests
  18. images = OrderedDict()
  19. # Records the device-tree files known to binman, keyed by filename (e.g.
  20. # 'u-boot-spl.dtb')
  21. fdt_files = {}
  22. # Arguments passed to binman to provide arguments to entries
  23. entry_args = {}
  24. def _ReadImageDesc(binman_node):
  25. """Read the image descriptions from the /binman node
  26. This normally produces a single Image object called 'image'. But if
  27. multiple images are present, they will all be returned.
  28. Args:
  29. binman_node: Node object of the /binman node
  30. Returns:
  31. OrderedDict of Image objects, each of which describes an image
  32. """
  33. images = OrderedDict()
  34. if 'multiple-images' in binman_node.props:
  35. for node in binman_node.subnodes:
  36. images[node.name] = Image(node.name, node)
  37. else:
  38. images['image'] = Image('image', binman_node)
  39. return images
  40. def _FindBinmanNode(dtb):
  41. """Find the 'binman' node in the device tree
  42. Args:
  43. dtb: Fdt object to scan
  44. Returns:
  45. Node object of /binman node, or None if not found
  46. """
  47. for node in dtb.GetRoot().subnodes:
  48. if node.name == 'binman':
  49. return node
  50. return None
  51. def GetFdt(fname):
  52. """Get the Fdt object for a particular device-tree filename
  53. Binman keeps track of at least one device-tree file called u-boot.dtb but
  54. can also have others (e.g. for SPL). This function looks up the given
  55. filename and returns the associated Fdt object.
  56. Args:
  57. fname: Filename to look up (e.g. 'u-boot.dtb').
  58. Returns:
  59. Fdt object associated with the filename
  60. """
  61. return fdt_files[fname]
  62. def GetFdtPath(fname):
  63. return fdt_files[fname]._fname
  64. def SetEntryArgs(args):
  65. global entry_args
  66. entry_args = {}
  67. if args:
  68. for arg in args:
  69. m = re.match('([^=]*)=(.*)', arg)
  70. if not m:
  71. raise ValueError("Invalid entry arguemnt '%s'" % arg)
  72. entry_args[m.group(1)] = m.group(2)
  73. def GetEntryArg(name):
  74. return entry_args.get(name)
  75. def WriteEntryDocs(modules, test_missing=None):
  76. from entry import Entry
  77. Entry.WriteDocs(modules, test_missing)
  78. def Binman(options, args):
  79. """The main control code for binman
  80. This assumes that help and test options have already been dealt with. It
  81. deals with the core task of building images.
  82. Args:
  83. options: Command line options object
  84. args: Command line arguments (list of strings)
  85. """
  86. global images
  87. if options.full_help:
  88. pager = os.getenv('PAGER')
  89. if not pager:
  90. pager = 'more'
  91. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  92. 'README')
  93. command.Run(pager, fname)
  94. return 0
  95. # Try to figure out which device tree contains our image description
  96. if options.dt:
  97. dtb_fname = options.dt
  98. else:
  99. board = options.board
  100. if not board:
  101. raise ValueError('Must provide a board to process (use -b <board>)')
  102. board_pathname = os.path.join(options.build_dir, board)
  103. dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
  104. if not options.indir:
  105. options.indir = ['.']
  106. options.indir.append(board_pathname)
  107. try:
  108. # Import these here in case libfdt.py is not available, in which case
  109. # the above help option still works.
  110. import fdt
  111. import fdt_util
  112. tout.Init(options.verbosity)
  113. elf.debug = options.debug
  114. try:
  115. tools.SetInputDirs(options.indir)
  116. tools.PrepareOutputDir(options.outdir, options.preserve)
  117. SetEntryArgs(options.entry_arg)
  118. # Get the device tree ready by compiling it and copying the compiled
  119. # output into a file in our output directly. Then scan it for use
  120. # in binman.
  121. dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
  122. fname = tools.GetOutputFilename('u-boot-out.dtb')
  123. with open(dtb_fname) as infd:
  124. with open(fname, 'wb') as outfd:
  125. outfd.write(infd.read())
  126. dtb = fdt.FdtScan(fname)
  127. # Note the file so that GetFdt() can find it
  128. fdt_files['u-boot.dtb'] = dtb
  129. node = _FindBinmanNode(dtb)
  130. if not node:
  131. raise ValueError("Device tree '%s' does not have a 'binman' "
  132. "node" % dtb_fname)
  133. images = _ReadImageDesc(node)
  134. if options.image:
  135. skip = []
  136. for name, image in images.iteritems():
  137. if name not in options.image:
  138. del images[name]
  139. skip.append(name)
  140. if skip:
  141. print 'Skipping images: %s\n' % ', '.join(skip)
  142. # Prepare the device tree by making sure that any missing
  143. # properties are added (e.g. 'pos' and 'size'). The values of these
  144. # may not be correct yet, but we add placeholders so that the
  145. # size of the device tree is correct. Later, in
  146. # SetCalculatedProperties() we will insert the correct values
  147. # without changing the device-tree size, thus ensuring that our
  148. # entry offsets remain the same.
  149. for image in images.values():
  150. if options.update_fdt:
  151. image.AddMissingProperties()
  152. image.ProcessFdt(dtb)
  153. dtb.Sync(auto_resize=True)
  154. dtb.Pack()
  155. dtb.Flush()
  156. for image in images.values():
  157. # Perform all steps for this image, including checking and
  158. # writing it. This means that errors found with a later
  159. # image will be reported after earlier images are already
  160. # completed and written, but that does not seem important.
  161. image.GetEntryContents()
  162. image.GetEntryOffsets()
  163. image.PackEntries()
  164. image.CheckSize()
  165. image.CheckEntries()
  166. image.SetImagePos()
  167. if options.update_fdt:
  168. image.SetCalculatedProperties()
  169. dtb.Sync()
  170. image.ProcessEntryContents()
  171. image.WriteSymbols()
  172. image.BuildImage()
  173. if options.map:
  174. image.WriteMap()
  175. with open(fname, 'wb') as outfd:
  176. outfd.write(dtb.GetContents())
  177. finally:
  178. tools.FinaliseOutputDir()
  179. finally:
  180. tout.Uninit()
  181. return 0