1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # Class for an image, the output of binman
8 from __future__ import print_function
10 from collections import OrderedDict
11 from operator import attrgetter
20 """A Image, representing an output from binman
22 An image is comprised of a collection of entries each containing binary
23 data. The image size must be large enough to hold all of this data.
25 This class implements the various operations needed for images.
28 _node: Node object that contains the image definition in device tree
30 _size: Image size in bytes, or None if not known yet
31 _filename: Output filename for image
32 _sections: Sections present in this image (may be one or more)
35 test: True if this is being called from a test of Images. This this case
36 there is no device tree defining the structure of the section, so
37 we create a section manually.
39 def __init__(self, name, node, test=False):
43 self._filename = '%s.bin' % self._name
45 self._section = bsection.Section('main-section', self._node, True)
50 """Read properties from the image node"""
51 self._size = fdt_util.GetInt(self._node, 'size')
52 filename = fdt_util.GetString(self._node, 'filename')
54 self._filename = filename
55 self._section = bsection.Section('main-section', self._node)
57 def AddMissingProperties(self):
58 """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
66 self._section.AddMissingProperties()
68 def ProcessFdt(self, fdt):
69 return self._section.ProcessFdt(fdt)
71 def GetEntryContents(self):
72 """Call ObtainContents() for the section
74 self._section.GetEntryContents()
76 def GetEntryOffsets(self):
77 """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.
82 self._section.GetEntryOffsets()
84 def PackEntries(self):
85 """Pack all entries into the image"""
86 self._section.PackEntries()
89 """Check that the image contents does not exceed its size, etc."""
90 self._size = self._section.CheckSize()
92 def CheckEntries(self):
93 """Check that entries do not overlap or extend outside the image"""
94 self._section.CheckEntries()
96 def SetCalculatedProperties(self):
97 self._section.SetCalculatedProperties()
99 def ProcessEntryContents(self):
100 """Call the ProcessContents() method for each entry
102 This is intended to adjust the contents as needed by the entry type.
104 self._section.ProcessEntryContents()
106 def WriteSymbols(self):
107 """Write symbol values into binary files for access at run time"""
108 self._section.WriteSymbols()
110 def BuildImage(self):
111 """Write the image to a file"""
112 fname = tools.GetOutputFilename(self._filename)
113 with open(fname, 'wb') as fd:
114 self._section.BuildSection(fd, 0)
116 def GetEntries(self):
117 return self._section.GetEntries()
120 """Write a map of the image to a .map file"""
121 filename = '%s.map' % self._name
122 fname = tools.GetOutputFilename(filename)
123 with open(fname, 'w') as fd:
124 print('%8s %8s %s' % ('Offset', 'Size', 'Name'), file=fd)
125 self._section.WriteMap(fd, 0)