control.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 sys
  10. import tools
  11. import command
  12. import elf
  13. from image import Image
  14. import state
  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. def _ReadImageDesc(binman_node):
  20. """Read the image descriptions from the /binman node
  21. This normally produces a single Image object called 'image'. But if
  22. multiple images are present, they will all be returned.
  23. Args:
  24. binman_node: Node object of the /binman node
  25. Returns:
  26. OrderedDict of Image objects, each of which describes an image
  27. """
  28. images = OrderedDict()
  29. if 'multiple-images' in binman_node.props:
  30. for node in binman_node.subnodes:
  31. images[node.name] = Image(node.name, node)
  32. else:
  33. images['image'] = Image('image', binman_node)
  34. return images
  35. def _FindBinmanNode(dtb):
  36. """Find the 'binman' node in the device tree
  37. Args:
  38. dtb: Fdt object to scan
  39. Returns:
  40. Node object of /binman node, or None if not found
  41. """
  42. for node in dtb.GetRoot().subnodes:
  43. if node.name == 'binman':
  44. return node
  45. return None
  46. def WriteEntryDocs(modules, test_missing=None):
  47. """Write out documentation for all entries
  48. Args:
  49. modules: List of Module objects to get docs for
  50. test_missing: Used for testing only, to force an entry's documeentation
  51. to show as missing even if it is present. Should be set to None in
  52. normal use.
  53. """
  54. from entry import Entry
  55. Entry.WriteDocs(modules, test_missing)
  56. def Binman(options, args):
  57. """The main control code for binman
  58. This assumes that help and test options have already been dealt with. It
  59. deals with the core task of building images.
  60. Args:
  61. options: Command line options object
  62. args: Command line arguments (list of strings)
  63. """
  64. global images
  65. if options.full_help:
  66. pager = os.getenv('PAGER')
  67. if not pager:
  68. pager = 'more'
  69. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  70. 'README')
  71. command.Run(pager, fname)
  72. return 0
  73. # Try to figure out which device tree contains our image description
  74. if options.dt:
  75. dtb_fname = options.dt
  76. else:
  77. board = options.board
  78. if not board:
  79. raise ValueError('Must provide a board to process (use -b <board>)')
  80. board_pathname = os.path.join(options.build_dir, board)
  81. dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
  82. if not options.indir:
  83. options.indir = ['.']
  84. options.indir.append(board_pathname)
  85. try:
  86. # Import these here in case libfdt.py is not available, in which case
  87. # the above help option still works.
  88. import fdt
  89. import fdt_util
  90. tout.Init(options.verbosity)
  91. elf.debug = options.debug
  92. try:
  93. tools.SetInputDirs(options.indir)
  94. tools.PrepareOutputDir(options.outdir, options.preserve)
  95. state.SetEntryArgs(options.entry_arg)
  96. # Get the device tree ready by compiling it and copying the compiled
  97. # output into a file in our output directly. Then scan it for use
  98. # in binman.
  99. dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
  100. fname = tools.GetOutputFilename('u-boot-out.dtb')
  101. with open(dtb_fname) as infd:
  102. with open(fname, 'wb') as outfd:
  103. outfd.write(infd.read())
  104. dtb = fdt.FdtScan(fname)
  105. node = _FindBinmanNode(dtb)
  106. if not node:
  107. raise ValueError("Device tree '%s' does not have a 'binman' "
  108. "node" % dtb_fname)
  109. images = _ReadImageDesc(node)
  110. if options.image:
  111. skip = []
  112. for name, image in images.iteritems():
  113. if name not in options.image:
  114. del images[name]
  115. skip.append(name)
  116. if skip:
  117. print 'Skipping images: %s\n' % ', '.join(skip)
  118. state.Prepare(images, dtb)
  119. # Prepare the device tree by making sure that any missing
  120. # properties are added (e.g. 'pos' and 'size'). The values of these
  121. # may not be correct yet, but we add placeholders so that the
  122. # size of the device tree is correct. Later, in
  123. # SetCalculatedProperties() we will insert the correct values
  124. # without changing the device-tree size, thus ensuring that our
  125. # entry offsets remain the same.
  126. for image in images.values():
  127. if options.update_fdt:
  128. image.AddMissingProperties()
  129. image.ProcessFdt(dtb)
  130. for dtb_item in state.GetFdts():
  131. dtb_item.Sync(auto_resize=True)
  132. dtb_item.Pack()
  133. dtb_item.Flush()
  134. for image in images.values():
  135. # Perform all steps for this image, including checking and
  136. # writing it. This means that errors found with a later
  137. # image will be reported after earlier images are already
  138. # completed and written, but that does not seem important.
  139. image.GetEntryContents()
  140. image.GetEntryOffsets()
  141. image.PackEntries()
  142. image.CheckSize()
  143. image.CheckEntries()
  144. image.SetImagePos()
  145. if options.update_fdt:
  146. image.SetCalculatedProperties()
  147. for dtb_item in state.GetFdts():
  148. dtb_item.Sync()
  149. image.ProcessEntryContents()
  150. image.WriteSymbols()
  151. image.BuildImage()
  152. if options.map:
  153. image.WriteMap()
  154. # Write the updated FDTs to our output files
  155. for dtb_item in state.GetFdts():
  156. tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
  157. finally:
  158. tools.FinaliseOutputDir()
  159. finally:
  160. tout.Uninit()
  161. return 0