control.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) 2016 Google, Inc
  2. # Written by Simon Glass <sjg@chromium.org>
  3. #
  4. # SPDX-License-Identifier: GPL-2.0+
  5. #
  6. # Creates binary images from input files controlled by a description
  7. #
  8. from collections import OrderedDict
  9. import os
  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. def _ReadImageDesc(binman_node):
  22. """Read the image descriptions from the /binman node
  23. This normally produces a single Image object called 'image'. But if
  24. multiple images are present, they will all be returned.
  25. Args:
  26. binman_node: Node object of the /binman node
  27. Returns:
  28. OrderedDict of Image objects, each of which describes an image
  29. """
  30. images = OrderedDict()
  31. if 'multiple-images' in binman_node.props:
  32. for node in binman_node.subnodes:
  33. images[node.name] = Image(node.name, node)
  34. else:
  35. images['image'] = Image('image', binman_node)
  36. return images
  37. def _FindBinmanNode(dtb):
  38. """Find the 'binman' node in the device tree
  39. Args:
  40. dtb: Fdt object to scan
  41. Returns:
  42. Node object of /binman node, or None if not found
  43. """
  44. for node in dtb.GetRoot().subnodes:
  45. if node.name == 'binman':
  46. return node
  47. return None
  48. def Binman(options, args):
  49. """The main control code for binman
  50. This assumes that help and test options have already been dealt with. It
  51. deals with the core task of building images.
  52. Args:
  53. options: Command line options object
  54. args: Command line arguments (list of strings)
  55. """
  56. global images
  57. if options.full_help:
  58. pager = os.getenv('PAGER')
  59. if not pager:
  60. pager = 'more'
  61. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  62. 'README')
  63. command.Run(pager, fname)
  64. return 0
  65. # Try to figure out which device tree contains our image description
  66. if options.dt:
  67. dtb_fname = options.dt
  68. else:
  69. board = options.board
  70. if not board:
  71. raise ValueError('Must provide a board to process (use -b <board>)')
  72. board_pathname = os.path.join(options.build_dir, board)
  73. dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
  74. if not options.indir:
  75. options.indir = ['.']
  76. options.indir.append(board_pathname)
  77. try:
  78. tout.Init(options.verbosity)
  79. elf.debug = options.debug
  80. try:
  81. tools.SetInputDirs(options.indir)
  82. tools.PrepareOutputDir(options.outdir, options.preserve)
  83. dtb = fdt.FdtScan(dtb_fname)
  84. node = _FindBinmanNode(dtb)
  85. if not node:
  86. raise ValueError("Device tree '%s' does not have a 'binman' "
  87. "node" % dtb_fname)
  88. images = _ReadImageDesc(node)
  89. for image in images.values():
  90. # Perform all steps for this image, including checking and
  91. # writing it. This means that errors found with a later
  92. # image will be reported after earlier images are already
  93. # completed and written, but that does not seem important.
  94. image.GetEntryContents()
  95. image.GetEntryPositions()
  96. image.PackEntries()
  97. image.CheckSize()
  98. image.CheckEntries()
  99. image.ProcessEntryContents()
  100. image.WriteSymbols()
  101. image.BuildImage()
  102. finally:
  103. tools.FinaliseOutputDir()
  104. finally:
  105. tout.Uninit()
  106. return 0