blob: 6f4bd5d37b252a707d7e8efeaa52fa1fb1693b82 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassbf7fd502016-11-25 20:15:51 -07005# Class for an image, the output of binman
6#
7
Simon Glass19790632017-11-13 18:55:01 -07008from __future__ import print_function
9
Simon Glassbf7fd502016-11-25 20:15:51 -070010from collections import OrderedDict
11from operator import attrgetter
Simon Glass19790632017-11-13 18:55:01 -070012import re
13import sys
Simon Glassbf7fd502016-11-25 20:15:51 -070014
Simon Glassbf7fd502016-11-25 20:15:51 -070015import fdt_util
Simon Glass8f1da502018-06-01 09:38:12 -060016import bsection
Simon Glassbf7fd502016-11-25 20:15:51 -070017import tools
18
19class Image:
20 """A Image, representing an output from binman
21
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.
24
25 This class implements the various operations needed for images.
26
27 Atrtributes:
28 _node: Node object that contains the image definition in device tree
29 _name: Image name
30 _size: Image size in bytes, or None if not known yet
Simon Glassbf7fd502016-11-25 20:15:51 -070031 _filename: Output filename for image
Simon Glass8f1da502018-06-01 09:38:12 -060032 _sections: Sections present in this image (may be one or more)
Simon Glass7ae5f312018-06-01 09:38:19 -060033
34 Args:
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.
Simon Glassbf7fd502016-11-25 20:15:51 -070038 """
Simon Glass19790632017-11-13 18:55:01 -070039 def __init__(self, name, node, test=False):
Simon Glassbf7fd502016-11-25 20:15:51 -070040 self._node = node
41 self._name = name
42 self._size = None
Simon Glassbf7fd502016-11-25 20:15:51 -070043 self._filename = '%s.bin' % self._name
Simon Glass8f1da502018-06-01 09:38:12 -060044 if test:
Simon Glass08723a72018-09-14 04:57:33 -060045 self._section = bsection.Section('main-section', None, self._node,
46 self, True)
Simon Glass8f1da502018-06-01 09:38:12 -060047 else:
Simon Glass19790632017-11-13 18:55:01 -070048 self._ReadNode()
Simon Glassbf7fd502016-11-25 20:15:51 -070049
50 def _ReadNode(self):
51 """Read properties from the image node"""
52 self._size = fdt_util.GetInt(self._node, 'size')
Simon Glassbf7fd502016-11-25 20:15:51 -070053 filename = fdt_util.GetString(self._node, 'filename')
54 if filename:
55 self._filename = filename
Simon Glass08723a72018-09-14 04:57:33 -060056 self._section = bsection.Section('main-section', None, self._node, self)
Simon Glassbf7fd502016-11-25 20:15:51 -070057
Simon Glassc52c9e72019-07-08 14:25:37 -060058 def Raise(self, msg):
59 """Convenience function to raise an error referencing an image"""
60 raise ValueError("Image '%s': %s" % (self._node.path, msg))
61
Simon Glass539aece2018-09-14 04:57:22 -060062 def GetFdtSet(self):
63 """Get the set of device tree files used by this image"""
64 return self._section.GetFdtSet()
65
Simon Glass0a98b282018-09-14 04:57:28 -060066 def ExpandEntries(self):
67 """Expand out any entries which have calculated sub-entries
68
69 Some entries are expanded out at runtime, e.g. 'files', which produces
70 a section containing a list of files. Process these entries so that
71 this information is added to the device tree.
72 """
73 self._section.ExpandEntries()
74
Simon Glass078ab1a2018-07-06 10:27:41 -060075 def AddMissingProperties(self):
76 """Add properties that are not present in the device tree
77
Simon Glass3ab95982018-08-01 15:22:37 -060078 When binman has completed packing the entries the offset and size of
Simon Glass078ab1a2018-07-06 10:27:41 -060079 each entry are known. But before this the device tree may not specify
80 these. Add any missing properties, with a dummy value, so that the
81 size of the entry is correct. That way we can insert the correct values
82 later.
83 """
84 self._section.AddMissingProperties()
85
Simon Glassecab8972018-07-06 10:27:40 -060086 def ProcessFdt(self, fdt):
Simon Glass6ed45ba2018-09-14 04:57:24 -060087 """Allow entries to adjust the device tree
88
89 Some entries need to adjust the device tree for their purposes. This
90 may involve adding or deleting properties.
91 """
Simon Glassecab8972018-07-06 10:27:40 -060092 return self._section.ProcessFdt(fdt)
93
Simon Glassbf7fd502016-11-25 20:15:51 -070094 def GetEntryContents(self):
Simon Glass8f1da502018-06-01 09:38:12 -060095 """Call ObtainContents() for the section
Simon Glassbf7fd502016-11-25 20:15:51 -070096 """
Simon Glass8f1da502018-06-01 09:38:12 -060097 self._section.GetEntryContents()
Simon Glassbf7fd502016-11-25 20:15:51 -070098
Simon Glass3ab95982018-08-01 15:22:37 -060099 def GetEntryOffsets(self):
100 """Handle entries that want to set the offset/size of other entries
Simon Glassbf7fd502016-11-25 20:15:51 -0700101
Simon Glass3ab95982018-08-01 15:22:37 -0600102 This calls each entry's GetOffsets() method. If it returns a list
Simon Glassbf7fd502016-11-25 20:15:51 -0700103 of entries to update, it updates them.
104 """
Simon Glass3ab95982018-08-01 15:22:37 -0600105 self._section.GetEntryOffsets()
Simon Glassbf7fd502016-11-25 20:15:51 -0700106
Simon Glassc52c9e72019-07-08 14:25:37 -0600107 def ResetForPack(self):
108 """Reset offset/size fields so that packing can be done again"""
109 self._section.ResetForPack()
110
Simon Glassbf7fd502016-11-25 20:15:51 -0700111 def PackEntries(self):
112 """Pack all entries into the image"""
Simon Glass8f1da502018-06-01 09:38:12 -0600113 self._section.PackEntries()
Simon Glassbf7fd502016-11-25 20:15:51 -0700114
Simon Glass8f1da502018-06-01 09:38:12 -0600115 def CheckSize(self):
116 """Check that the image contents does not exceed its size, etc."""
117 self._size = self._section.CheckSize()
Simon Glassbf7fd502016-11-25 20:15:51 -0700118
119 def CheckEntries(self):
120 """Check that entries do not overlap or extend outside the image"""
Simon Glass8f1da502018-06-01 09:38:12 -0600121 self._section.CheckEntries()
Simon Glassbf7fd502016-11-25 20:15:51 -0700122
Simon Glass078ab1a2018-07-06 10:27:41 -0600123 def SetCalculatedProperties(self):
124 self._section.SetCalculatedProperties()
125
Simon Glassdbf6be92018-08-01 15:22:42 -0600126 def SetImagePos(self):
127 self._section.SetImagePos(0)
128
Simon Glassbf7fd502016-11-25 20:15:51 -0700129 def ProcessEntryContents(self):
130 """Call the ProcessContents() method for each entry
131
132 This is intended to adjust the contents as needed by the entry type.
Simon Glassa0dcaf22019-07-08 14:25:35 -0600133
134 Returns:
135 True if the new data size is OK, False if expansion is needed
Simon Glassbf7fd502016-11-25 20:15:51 -0700136 """
Simon Glassa0dcaf22019-07-08 14:25:35 -0600137 return self._section.ProcessEntryContents()
Simon Glassbf7fd502016-11-25 20:15:51 -0700138
Simon Glass19790632017-11-13 18:55:01 -0700139 def WriteSymbols(self):
140 """Write symbol values into binary files for access at run time"""
Simon Glass8f1da502018-06-01 09:38:12 -0600141 self._section.WriteSymbols()
Simon Glass19790632017-11-13 18:55:01 -0700142
Simon Glassbf7fd502016-11-25 20:15:51 -0700143 def BuildImage(self):
144 """Write the image to a file"""
145 fname = tools.GetOutputFilename(self._filename)
146 with open(fname, 'wb') as fd:
Simon Glass8f1da502018-06-01 09:38:12 -0600147 self._section.BuildSection(fd, 0)
Simon Glassbf7fd502016-11-25 20:15:51 -0700148
Simon Glass8f1da502018-06-01 09:38:12 -0600149 def GetEntries(self):
150 return self._section.GetEntries()
Simon Glass3b0c38212018-06-01 09:38:20 -0600151
152 def WriteMap(self):
Simon Glass163ed6c2018-09-14 04:57:36 -0600153 """Write a map of the image to a .map file
154
155 Returns:
156 Filename of map file written
157 """
Simon Glass3b0c38212018-06-01 09:38:20 -0600158 filename = '%s.map' % self._name
159 fname = tools.GetOutputFilename(filename)
160 with open(fname, 'w') as fd:
Simon Glass1be70d22018-07-17 13:25:49 -0600161 print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
162 file=fd)
Simon Glass3b0c38212018-06-01 09:38:20 -0600163 self._section.WriteMap(fd, 0)
Simon Glass163ed6c2018-09-14 04:57:36 -0600164 return fname
Simon Glass41b8ba02019-07-08 14:25:43 -0600165
166 def BuildEntryList(self):
167 """List the files in an image
168
169 Returns:
170 List of entry.EntryInfo objects describing all entries in the image
171 """
172 entries = []
173 self._section.ListEntries(entries, 0)
174 return entries