image.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 ProcessFdt(self, fdt):
  48. return self._section.ProcessFdt(fdt)
  49. def GetEntryContents(self):
  50. """Call ObtainContents() for the section
  51. """
  52. self._section.GetEntryContents()
  53. def GetEntryPositions(self):
  54. """Handle entries that want to set the position/size of other entries
  55. This calls each entry's GetPositions() method. If it returns a list
  56. of entries to update, it updates them.
  57. """
  58. self._section.GetEntryPositions()
  59. def PackEntries(self):
  60. """Pack all entries into the image"""
  61. self._section.PackEntries()
  62. def CheckSize(self):
  63. """Check that the image contents does not exceed its size, etc."""
  64. self._size = self._section.CheckSize()
  65. def CheckEntries(self):
  66. """Check that entries do not overlap or extend outside the image"""
  67. self._section.CheckEntries()
  68. def ProcessEntryContents(self):
  69. """Call the ProcessContents() method for each entry
  70. This is intended to adjust the contents as needed by the entry type.
  71. """
  72. self._section.ProcessEntryContents()
  73. def WriteSymbols(self):
  74. """Write symbol values into binary files for access at run time"""
  75. self._section.WriteSymbols()
  76. def BuildImage(self):
  77. """Write the image to a file"""
  78. fname = tools.GetOutputFilename(self._filename)
  79. with open(fname, 'wb') as fd:
  80. self._section.BuildSection(fd, 0)
  81. def GetEntries(self):
  82. return self._section.GetEntries()
  83. def WriteMap(self):
  84. """Write a map of the image to a .map file"""
  85. filename = '%s.map' % self._name
  86. fname = tools.GetOutputFilename(filename)
  87. with open(fname, 'w') as fd:
  88. print('%8s %8s %s' % ('Position', 'Size', 'Name'), file=fd)
  89. self._section.WriteMap(fd, 0)