image.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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', self._node, True)
  38. else:
  39. self._ReadNode()
  40. def _ReadNode(self):
  41. """Read properties from the image node"""
  42. self._size = fdt_util.GetInt(self._node, 'size')
  43. filename = fdt_util.GetString(self._node, 'filename')
  44. if filename:
  45. self._filename = filename
  46. self._section = bsection.Section('main-section', self._node)
  47. def AddMissingProperties(self):
  48. """Add properties that are not present in the device tree
  49. When binman has completed packing the entries the offset and size of
  50. each entry are known. But before this the device tree may not specify
  51. these. Add any missing properties, with a dummy value, so that the
  52. size of the entry is correct. That way we can insert the correct values
  53. later.
  54. """
  55. self._section.AddMissingProperties()
  56. def ProcessFdt(self, fdt):
  57. return self._section.ProcessFdt(fdt)
  58. def GetEntryContents(self):
  59. """Call ObtainContents() for the section
  60. """
  61. self._section.GetEntryContents()
  62. def GetEntryOffsets(self):
  63. """Handle entries that want to set the offset/size of other entries
  64. This calls each entry's GetOffsets() method. If it returns a list
  65. of entries to update, it updates them.
  66. """
  67. self._section.GetEntryOffsets()
  68. def PackEntries(self):
  69. """Pack all entries into the image"""
  70. self._section.PackEntries()
  71. def CheckSize(self):
  72. """Check that the image contents does not exceed its size, etc."""
  73. self._size = self._section.CheckSize()
  74. def CheckEntries(self):
  75. """Check that entries do not overlap or extend outside the image"""
  76. self._section.CheckEntries()
  77. def SetCalculatedProperties(self):
  78. self._section.SetCalculatedProperties()
  79. def SetImagePos(self):
  80. self._section.SetImagePos(0)
  81. def ProcessEntryContents(self):
  82. """Call the ProcessContents() method for each entry
  83. This is intended to adjust the contents as needed by the entry type.
  84. """
  85. self._section.ProcessEntryContents()
  86. def WriteSymbols(self):
  87. """Write symbol values into binary files for access at run time"""
  88. self._section.WriteSymbols()
  89. def BuildImage(self):
  90. """Write the image to a file"""
  91. fname = tools.GetOutputFilename(self._filename)
  92. with open(fname, 'wb') as fd:
  93. self._section.BuildSection(fd, 0)
  94. def GetEntries(self):
  95. return self._section.GetEntries()
  96. def WriteMap(self):
  97. """Write a map of the image to a .map file"""
  98. filename = '%s.map' % self._name
  99. fname = tools.GetOutputFilename(filename)
  100. with open(fname, 'w') as fd:
  101. print('%8s %8s %s' % ('Offset', 'Size', 'Name'), file=fd)
  102. self._section.WriteMap(fd, 0)