blob: cb693c938204c01b223b349a189e97cb3f505a4e [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#
Simon Glassbf7fd502016-11-25 20:15:51 -07004# Base class for all entries
5#
6
Simon Glass3b0c3822018-06-01 09:38:20 -06007from __future__ import print_function
8
Simon Glassbf7fd502016-11-25 20:15:51 -07009# importlib was introduced in Python 2.7 but there was a report of it not
10# working in 2.7.12, so we work around this:
11# http://lists.denx.de/pipermail/u-boot/2016-October/269729.html
12try:
13 import importlib
14 have_importlib = True
15except:
16 have_importlib = False
17
18import fdt_util
Simon Glassbadf0ec2018-06-01 09:38:15 -060019import os
20import sys
Simon Glassbf7fd502016-11-25 20:15:51 -070021import tools
22
23modules = {}
24
Simon Glassbadf0ec2018-06-01 09:38:15 -060025our_path = os.path.dirname(os.path.realpath(__file__))
26
Simon Glassbf7fd502016-11-25 20:15:51 -070027class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060028 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070029
30 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060031 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070032 Entries can be placed either right next to each other, or with padding
33 between them. The type of the entry determines the data that is in it.
34
35 This class is not used by itself. All entry objects are subclasses of
36 Entry.
37
38 Attributes:
Simon Glass8122f392018-07-17 13:25:28 -060039 section: Section object containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070040 node: The node that created this entry
Simon Glass3ab95982018-08-01 15:22:37 -060041 offset: Offset of entry within the section, None if not known yet (in
42 which case it will be calculated by Pack())
Simon Glassbf7fd502016-11-25 20:15:51 -070043 size: Entry size in bytes, None if not known
44 contents_size: Size of contents in bytes, 0 by default
Simon Glass3ab95982018-08-01 15:22:37 -060045 align: Entry start offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070046 align_size: Entry size alignment, or None
Simon Glass3ab95982018-08-01 15:22:37 -060047 align_end: Entry end offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070048 pad_before: Number of pad bytes before the contents, 0 if none
49 pad_after: Number of pad bytes after the contents, 0 if none
50 data: Contents of entry (string of bytes)
51 """
Simon Glassc8d48ef2018-06-01 09:38:21 -060052 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glass25ac0e62018-06-01 09:38:14 -060053 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070054 self.etype = etype
55 self._node = node
Simon Glassc8d48ef2018-06-01 09:38:21 -060056 self.name = node and (name_prefix + node.name) or 'none'
Simon Glass3ab95982018-08-01 15:22:37 -060057 self.offset = None
Simon Glassbf7fd502016-11-25 20:15:51 -070058 self.size = None
Simon Glass5c890232018-07-06 10:27:19 -060059 self.data = ''
Simon Glassbf7fd502016-11-25 20:15:51 -070060 self.contents_size = 0
61 self.align = None
62 self.align_size = None
63 self.align_end = None
64 self.pad_before = 0
65 self.pad_after = 0
Simon Glass3ab95982018-08-01 15:22:37 -060066 self.offset_unset = False
Simon Glassbf7fd502016-11-25 20:15:51 -070067 if read_node:
68 self.ReadNode()
69
70 @staticmethod
Simon Glass25ac0e62018-06-01 09:38:14 -060071 def Create(section, node, etype=None):
Simon Glassbf7fd502016-11-25 20:15:51 -070072 """Create a new entry for a node.
73
74 Args:
Simon Glass8122f392018-07-17 13:25:28 -060075 section: Section object containing this node
Simon Glassbf7fd502016-11-25 20:15:51 -070076 node: Node object containing information about the entry to create
77 etype: Entry type to use, or None to work it out (used for tests)
78
79 Returns:
80 A new Entry object of the correct type (a subclass of Entry)
81 """
82 if not etype:
83 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glassdd57c132018-06-01 09:38:11 -060084
85 # Convert something like 'u-boot@0' to 'u_boot' since we are only
86 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -070087 module_name = etype.replace('-', '_')
Simon Glassdd57c132018-06-01 09:38:11 -060088 if '@' in module_name:
89 module_name = module_name.split('@')[0]
Simon Glassbf7fd502016-11-25 20:15:51 -070090 module = modules.get(module_name)
91
Simon Glassbadf0ec2018-06-01 09:38:15 -060092 # Also allow entry-type modules to be brought in from the etype directory.
93
Simon Glassbf7fd502016-11-25 20:15:51 -070094 # Import the module if we have not already done so.
95 if not module:
Simon Glassbadf0ec2018-06-01 09:38:15 -060096 old_path = sys.path
97 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -070098 try:
99 if have_importlib:
100 module = importlib.import_module(module_name)
101 else:
102 module = __import__(module_name)
103 except ImportError:
104 raise ValueError("Unknown entry type '%s' in node '%s'" %
105 (etype, node.path))
Simon Glassbadf0ec2018-06-01 09:38:15 -0600106 finally:
107 sys.path = old_path
Simon Glassbf7fd502016-11-25 20:15:51 -0700108 modules[module_name] = module
109
110 # Call its constructor to get the object we want.
111 obj = getattr(module, 'Entry_%s' % module_name)
Simon Glass25ac0e62018-06-01 09:38:14 -0600112 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700113
114 def ReadNode(self):
115 """Read entry information from the node
116
117 This reads all the fields we recognise from the node, ready for use.
118 """
Simon Glass3ab95982018-08-01 15:22:37 -0600119 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700120 self.size = fdt_util.GetInt(self._node, 'size')
121 self.align = fdt_util.GetInt(self._node, 'align')
122 if tools.NotPowerOfTwo(self.align):
123 raise ValueError("Node '%s': Alignment %s must be a power of two" %
124 (self._node.path, self.align))
125 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
126 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
127 self.align_size = fdt_util.GetInt(self._node, 'align-size')
128 if tools.NotPowerOfTwo(self.align_size):
129 raise ValueError("Node '%s': Alignment size %s must be a power "
130 "of two" % (self._node.path, self.align_size))
131 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glass3ab95982018-08-01 15:22:37 -0600132 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700133
Simon Glass078ab1a2018-07-06 10:27:41 -0600134 def AddMissingProperties(self):
135 """Add new properties to the device tree as needed for this entry"""
Simon Glass8122f392018-07-17 13:25:28 -0600136 for prop in ['offset', 'size']:
Simon Glass078ab1a2018-07-06 10:27:41 -0600137 if not prop in self._node.props:
138 self._node.AddZeroProp(prop)
139
140 def SetCalculatedProperties(self):
141 """Set the value of device-tree properties calculated by binman"""
Simon Glass3ab95982018-08-01 15:22:37 -0600142 self._node.SetInt('offset', self.offset)
Simon Glass078ab1a2018-07-06 10:27:41 -0600143 self._node.SetInt('size', self.size)
144
Simon Glassecab8972018-07-06 10:27:40 -0600145 def ProcessFdt(self, fdt):
146 return True
147
Simon Glassc8d48ef2018-06-01 09:38:21 -0600148 def SetPrefix(self, prefix):
149 """Set the name prefix for a node
150
151 Args:
152 prefix: Prefix to set, or '' to not use a prefix
153 """
154 if prefix:
155 self.name = prefix + self.name
156
Simon Glass5c890232018-07-06 10:27:19 -0600157 def SetContents(self, data):
158 """Set the contents of an entry
159
160 This sets both the data and content_size properties
161
162 Args:
163 data: Data to set to the contents (string)
164 """
165 self.data = data
166 self.contents_size = len(self.data)
167
168 def ProcessContentsUpdate(self, data):
169 """Update the contens of an entry, after the size is fixed
170
171 This checks that the new data is the same size as the old.
172
173 Args:
174 data: Data to set to the contents (string)
175
176 Raises:
177 ValueError if the new data size is not the same as the old
178 """
179 if len(data) != self.contents_size:
180 self.Raise('Cannot update entry size from %d to %d' %
181 (len(data), self.contents_size))
182 self.SetContents(data)
183
Simon Glassbf7fd502016-11-25 20:15:51 -0700184 def ObtainContents(self):
185 """Figure out the contents of an entry.
186
187 Returns:
188 True if the contents were found, False if another call is needed
189 after the other entries are processed.
190 """
191 # No contents by default: subclasses can implement this
192 return True
193
Simon Glass3ab95982018-08-01 15:22:37 -0600194 def Pack(self, offset):
Simon Glass25ac0e62018-06-01 09:38:14 -0600195 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700196
197 Most of the time the entries are not fully specified. There may be
198 an alignment but no size. In that case we take the size from the
199 contents of the entry.
200
Simon Glass3ab95982018-08-01 15:22:37 -0600201 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glassbf7fd502016-11-25 20:15:51 -0700202
Simon Glass3ab95982018-08-01 15:22:37 -0600203 Once this function is complete, both the offset and size of the
Simon Glassbf7fd502016-11-25 20:15:51 -0700204 entry will be know.
205
206 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600207 Current section offset pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700208
209 Returns:
Simon Glass3ab95982018-08-01 15:22:37 -0600210 New section offset pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700211 """
Simon Glass3ab95982018-08-01 15:22:37 -0600212 if self.offset is None:
213 if self.offset_unset:
214 self.Raise('No offset set with offset-unset: should another '
215 'entry provide this correct offset?')
216 self.offset = tools.Align(offset, self.align)
Simon Glassbf7fd502016-11-25 20:15:51 -0700217 needed = self.pad_before + self.contents_size + self.pad_after
218 needed = tools.Align(needed, self.align_size)
219 size = self.size
220 if not size:
221 size = needed
Simon Glass3ab95982018-08-01 15:22:37 -0600222 new_offset = self.offset + size
223 aligned_offset = tools.Align(new_offset, self.align_end)
224 if aligned_offset != new_offset:
225 size = aligned_offset - self.offset
226 new_offset = aligned_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700227
228 if not self.size:
229 self.size = size
230
231 if self.size < needed:
232 self.Raise("Entry contents size is %#x (%d) but entry size is "
233 "%#x (%d)" % (needed, needed, self.size, self.size))
234 # Check that the alignment is correct. It could be wrong if the
Simon Glass3ab95982018-08-01 15:22:37 -0600235 # and offset or size values were provided (i.e. not calculated), but
Simon Glassbf7fd502016-11-25 20:15:51 -0700236 # conflict with the provided alignment values
237 if self.size != tools.Align(self.size, self.align_size):
238 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
239 (self.size, self.size, self.align_size, self.align_size))
Simon Glass3ab95982018-08-01 15:22:37 -0600240 if self.offset != tools.Align(self.offset, self.align):
241 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
242 (self.offset, self.offset, self.align, self.align))
Simon Glassbf7fd502016-11-25 20:15:51 -0700243
Simon Glass3ab95982018-08-01 15:22:37 -0600244 return new_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700245
246 def Raise(self, msg):
247 """Convenience function to raise an error referencing a node"""
248 raise ValueError("Node '%s': %s" % (self._node.path, msg))
249
250 def GetPath(self):
251 """Get the path of a node
252
253 Returns:
254 Full path of the node for this entry
255 """
256 return self._node.path
257
258 def GetData(self):
259 return self.data
260
Simon Glass3ab95982018-08-01 15:22:37 -0600261 def GetOffsets(self):
Simon Glassbf7fd502016-11-25 20:15:51 -0700262 return {}
263
Simon Glass3ab95982018-08-01 15:22:37 -0600264 def SetOffsetSize(self, pos, size):
265 self.offset = pos
Simon Glassbf7fd502016-11-25 20:15:51 -0700266 self.size = size
267
268 def ProcessContents(self):
269 pass
Simon Glass19790632017-11-13 18:55:01 -0700270
Simon Glassf55382b2018-06-01 09:38:13 -0600271 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700272 """Write symbol values into binary files for access at run time
273
274 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600275 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700276 """
277 pass
Simon Glass18546952018-06-01 09:38:16 -0600278
Simon Glass3ab95982018-08-01 15:22:37 -0600279 def CheckOffset(self):
280 """Check that the entry offsets are correct
Simon Glass18546952018-06-01 09:38:16 -0600281
Simon Glass3ab95982018-08-01 15:22:37 -0600282 This is used for entries which have extra offset requirements (other
Simon Glass18546952018-06-01 09:38:16 -0600283 than having to be fully inside their section). Sub-classes can implement
284 this function and raise if there is a problem.
285 """
286 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600287
Simon Glass8122f392018-07-17 13:25:28 -0600288 @staticmethod
289 def WriteMapLine(fd, indent, name, offset, size):
290 print('%s%08x %08x %s' % (' ' * indent, offset, size, name), file=fd)
291
Simon Glass3b0c3822018-06-01 09:38:20 -0600292 def WriteMap(self, fd, indent):
293 """Write a map of the entry to a .map file
294
295 Args:
296 fd: File to write the map to
297 indent: Curent indent level of map (0=none, 1=one level, etc.)
298 """
Simon Glass8122f392018-07-17 13:25:28 -0600299 self.WriteMapLine(fd, indent, self.name, self.offset, self.size)