control.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Binman(options, args):
  76. """The main control code for binman
  77. This assumes that help and test options have already been dealt with. It
  78. deals with the core task of building images.
  79. Args:
  80. options: Command line options object
  81. args: Command line arguments (list of strings)
  82. """
  83. global images
  84. if options.full_help:
  85. pager = os.getenv('PAGER')
  86. if not pager:
  87. pager = 'more'
  88. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  89. 'README')
  90. command.Run(pager, fname)
  91. return 0
  92. # Try to figure out which device tree contains our image description
  93. if options.dt:
  94. dtb_fname = options.dt
  95. else:
  96. board = options.board
  97. if not board:
  98. raise ValueError('Must provide a board to process (use -b <board>)')
  99. board_pathname = os.path.join(options.build_dir, board)
  100. dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
  101. if not options.indir:
  102. options.indir = ['.']
  103. options.indir.append(board_pathname)
  104. try:
  105. # Import these here in case libfdt.py is not available, in which case
  106. # the above help option still works.
  107. import fdt
  108. import fdt_util
  109. tout.Init(options.verbosity)
  110. elf.debug = options.debug
  111. try:
  112. tools.SetInputDirs(options.indir)
  113. tools.PrepareOutputDir(options.outdir, options.preserve)
  114. SetEntryArgs(options.entry_arg)
  115. # Get the device tree ready by compiling it and copying the compiled
  116. # output into a file in our output directly. Then scan it for use
  117. # in binman.
  118. dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
  119. fname = tools.GetOutputFilename('u-boot-out.dtb')
  120. with open(dtb_fname) as infd:
  121. with open(fname, 'wb') as outfd:
  122. outfd.write(infd.read())
  123. dtb = fdt.FdtScan(fname)
  124. # Note the file so that GetFdt() can find it
  125. fdt_files['u-boot.dtb'] = dtb
  126. node = _FindBinmanNode(dtb)
  127. if not node:
  128. raise ValueError("Device tree '%s' does not have a 'binman' "
  129. "node" % dtb_fname)
  130. images = _ReadImageDesc(node)
  131. # Prepare the device tree by making sure that any missing
  132. # properties are added (e.g. 'pos' and 'size'). The values of these
  133. # may not be correct yet, but we add placeholders so that the
  134. # size of the device tree is correct. Later, in
  135. # SetCalculatedProperties() we will insert the correct values
  136. # without changing the device-tree size, thus ensuring that our
  137. # entry offsets remain the same.
  138. for image in images.values():
  139. if options.update_fdt:
  140. image.AddMissingProperties()
  141. image.ProcessFdt(dtb)
  142. dtb.Pack()
  143. dtb.Flush()
  144. for image in images.values():
  145. # Perform all steps for this image, including checking and
  146. # writing it. This means that errors found with a later
  147. # image will be reported after earlier images are already
  148. # completed and written, but that does not seem important.
  149. image.GetEntryContents()
  150. image.GetEntryOffsets()
  151. image.PackEntries()
  152. image.CheckSize()
  153. image.CheckEntries()
  154. image.SetImagePos()
  155. if options.update_fdt:
  156. image.SetCalculatedProperties()
  157. image.ProcessEntryContents()
  158. image.WriteSymbols()
  159. image.BuildImage()
  160. if options.map:
  161. image.WriteMap()
  162. with open(fname, 'wb') as outfd:
  163. outfd.write(dtb.GetContents())
  164. finally:
  165. tools.FinaliseOutputDir()
  166. finally:
  167. tout.Uninit()
  168. return 0