image.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Class for an image, the output of binman
  6. #
  7. from __future__ import print_function
  8. from collections import OrderedDict
  9. from operator import attrgetter
  10. import re
  11. import sys
  12. import fdt_util
  13. import bsection
  14. import tools
  15. class Image:
  16. """A Image, representing an output from binman
  17. An image is comprised of a collection of entries each containing binary
  18. data. The image size must be large enough to hold all of this data.
  19. This class implements the various operations needed for images.
  20. Atrtributes:
  21. _node: Node object that contains the image definition in device tree
  22. _name: Image name
  23. _size: Image size in bytes, or None if not known yet
  24. _filename: Output filename for image
  25. _sections: Sections present in this image (may be one or more)
  26. Args:
  27. test: True if this is being called from a test of Images. This this case
  28. there is no device tree defining the structure of the section, so
  29. we create a section manually.
  30. """
  31. def __init__(self, name, node, test=False):
  32. self._node = node
  33. self._name = name
  34. self._size = None
  35. self._filename = '%s.bin' % self._name
  36. if test:
  37. self._section = bsection.Section('main-section', None, self._node,
  38. self, True)
  39. else:
  40. self._ReadNode()
  41. def _ReadNode(self):
  42. """Read properties from the image node"""
  43. self._size = fdt_util.GetInt(self._node, 'size')
  44. filename = fdt_util.GetString(self._node, 'filename')
  45. if filename:
  46. self._filename = filename
  47. self._section = bsection.Section('main-section', None, self._node, self)
  48. def GetFdtSet(self):
  49. """Get the set of device tree files used by this image"""
  50. return self._section.GetFdtSet()
  51. def ExpandEntries(self):
  52. """Expand out any entries which have calculated sub-entries
  53. Some entries are expanded out at runtime, e.g. 'files', which produces
  54. a section containing a list of files. Process these entries so that
  55. this information is added to the device tree.
  56. """
  57. self._section.ExpandEntries()
  58. def AddMissingProperties(self):
  59. """Add properties that are not present in the device tree
  60. When binman has completed packing the entries the offset and size of
  61. each entry are known. But before this the device tree may not specify
  62. these. Add any missing properties, with a dummy value, so that the
  63. size of the entry is correct. That way we can insert the correct values
  64. later.
  65. """
  66. self._section.AddMissingProperties()
  67. def ProcessFdt(self, fdt):
  68. """Allow entries to adjust the device tree
  69. Some entries need to adjust the device tree for their purposes. This
  70. may involve adding or deleting properties.
  71. """
  72. return self._section.ProcessFdt(fdt)
  73. def GetEntryContents(self):
  74. """Call ObtainContents() for the section
  75. """
  76. self._section.GetEntryContents()
  77. def GetEntryOffsets(self):
  78. """Handle entries that want to set the offset/size of other entries
  79. This calls each entry's GetOffsets() method. If it returns a list
  80. of entries to update, it updates them.
  81. """
  82. self._section.GetEntryOffsets()
  83. def PackEntries(self):
  84. """Pack all entries into the image"""
  85. self._section.PackEntries()
  86. def CheckSize(self):
  87. """Check that the image contents does not exceed its size, etc."""
  88. self._size = self._section.CheckSize()
  89. def CheckEntries(self):
  90. """Check that entries do not overlap or extend outside the image"""
  91. self._section.CheckEntries()
  92. def SetCalculatedProperties(self):
  93. self._section.SetCalculatedProperties()
  94. def SetImagePos(self):
  95. self._section.SetImagePos(0)
  96. def ProcessEntryContents(self):
  97. """Call the ProcessContents() method for each entry
  98. This is intended to adjust the contents as needed by the entry type.
  99. """
  100. self._section.ProcessEntryContents()
  101. def WriteSymbols(self):
  102. """Write symbol values into binary files for access at run time"""
  103. self._section.WriteSymbols()
  104. def BuildImage(self):
  105. """Write the image to a file"""
  106. fname = tools.GetOutputFilename(self._filename)
  107. with open(fname, 'wb') as fd:
  108. self._section.BuildSection(fd, 0)
  109. def GetEntries(self):
  110. return self._section.GetEntries()
  111. def WriteMap(self):
  112. """Write a map of the image to a .map file
  113. Returns:
  114. Filename of map file written
  115. """
  116. filename = '%s.map' % self._name
  117. fname = tools.GetOutputFilename(filename)
  118. with open(fname, 'w') as fd:
  119. print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
  120. file=fd)
  121. self._section.WriteMap(fd, 0)
  122. return fname