control.py 6.2 KB

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