blob: de07f2721599d502856e4d83ebdea367473b0c24 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3#
Simon Glass2574ef62016-11-25 20:15:51 -07004# Base class for all entries
5#
6
Simon Glass30732662018-06-01 09:38:20 -06007from __future__ import print_function
8
Simon Glass91710b32018-07-17 13:25:32 -06009from collections import namedtuple
10
Simon Glass2574ef62016-11-25 20:15:51 -070011# importlib was introduced in Python 2.7 but there was a report of it not
12# working in 2.7.12, so we work around this:
13# http://lists.denx.de/pipermail/u-boot/2016-October/269729.html
14try:
15 import importlib
16 have_importlib = True
17except:
18 have_importlib = False
19
20import fdt_util
Simon Glass91710b32018-07-17 13:25:32 -060021import control
Simon Glass691198c2018-06-01 09:38:15 -060022import os
23import sys
Simon Glass2574ef62016-11-25 20:15:51 -070024import tools
25
26modules = {}
27
Simon Glass691198c2018-06-01 09:38:15 -060028our_path = os.path.dirname(os.path.realpath(__file__))
29
Simon Glass91710b32018-07-17 13:25:32 -060030
31# An argument which can be passed to entries on the command line, in lieu of
32# device-tree properties.
33EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
34
35
Simon Glass2574ef62016-11-25 20:15:51 -070036class Entry(object):
Simon Glassad5a7712018-06-01 09:38:14 -060037 """An Entry in the section
Simon Glass2574ef62016-11-25 20:15:51 -070038
39 An entry corresponds to a single node in the device-tree description
Simon Glassad5a7712018-06-01 09:38:14 -060040 of the section. Each entry ends up being a part of the final section.
Simon Glass2574ef62016-11-25 20:15:51 -070041 Entries can be placed either right next to each other, or with padding
42 between them. The type of the entry determines the data that is in it.
43
44 This class is not used by itself. All entry objects are subclasses of
45 Entry.
46
47 Attributes:
Simon Glass3a9a2b82018-07-17 13:25:28 -060048 section: Section object containing this entry
Simon Glass2574ef62016-11-25 20:15:51 -070049 node: The node that created this entry
Simon Glasse8561af2018-08-01 15:22:37 -060050 offset: Offset of entry within the section, None if not known yet (in
51 which case it will be calculated by Pack())
Simon Glass2574ef62016-11-25 20:15:51 -070052 size: Entry size in bytes, None if not known
53 contents_size: Size of contents in bytes, 0 by default
Simon Glasse8561af2018-08-01 15:22:37 -060054 align: Entry start offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070055 align_size: Entry size alignment, or None
Simon Glasse8561af2018-08-01 15:22:37 -060056 align_end: Entry end offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070057 pad_before: Number of pad bytes before the contents, 0 if none
58 pad_after: Number of pad bytes after the contents, 0 if none
59 data: Contents of entry (string of bytes)
60 """
Simon Glass3b78d532018-06-01 09:38:21 -060061 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glassad5a7712018-06-01 09:38:14 -060062 self.section = section
Simon Glass2574ef62016-11-25 20:15:51 -070063 self.etype = etype
64 self._node = node
Simon Glass3b78d532018-06-01 09:38:21 -060065 self.name = node and (name_prefix + node.name) or 'none'
Simon Glasse8561af2018-08-01 15:22:37 -060066 self.offset = None
Simon Glass2574ef62016-11-25 20:15:51 -070067 self.size = None
Simon Glass2e1169f2018-07-06 10:27:19 -060068 self.data = ''
Simon Glass2574ef62016-11-25 20:15:51 -070069 self.contents_size = 0
70 self.align = None
71 self.align_size = None
72 self.align_end = None
73 self.pad_before = 0
74 self.pad_after = 0
Simon Glasse8561af2018-08-01 15:22:37 -060075 self.offset_unset = False
Simon Glass9dcc8612018-08-01 15:22:42 -060076 self.image_pos = None
Simon Glass2574ef62016-11-25 20:15:51 -070077 if read_node:
78 self.ReadNode()
79
80 @staticmethod
Simon Glassad5a7712018-06-01 09:38:14 -060081 def Create(section, node, etype=None):
Simon Glass2574ef62016-11-25 20:15:51 -070082 """Create a new entry for a node.
83
84 Args:
Simon Glass3a9a2b82018-07-17 13:25:28 -060085 section: Section object containing this node
Simon Glass2574ef62016-11-25 20:15:51 -070086 node: Node object containing information about the entry to create
87 etype: Entry type to use, or None to work it out (used for tests)
88
89 Returns:
90 A new Entry object of the correct type (a subclass of Entry)
91 """
92 if not etype:
93 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glasse76a3e62018-06-01 09:38:11 -060094
95 # Convert something like 'u-boot@0' to 'u_boot' since we are only
96 # interested in the type.
Simon Glass2574ef62016-11-25 20:15:51 -070097 module_name = etype.replace('-', '_')
Simon Glasse76a3e62018-06-01 09:38:11 -060098 if '@' in module_name:
99 module_name = module_name.split('@')[0]
Simon Glass2574ef62016-11-25 20:15:51 -0700100 module = modules.get(module_name)
101
Simon Glass691198c2018-06-01 09:38:15 -0600102 # Also allow entry-type modules to be brought in from the etype directory.
103
Simon Glass2574ef62016-11-25 20:15:51 -0700104 # Import the module if we have not already done so.
105 if not module:
Simon Glass691198c2018-06-01 09:38:15 -0600106 old_path = sys.path
107 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glass2574ef62016-11-25 20:15:51 -0700108 try:
109 if have_importlib:
110 module = importlib.import_module(module_name)
111 else:
112 module = __import__(module_name)
113 except ImportError:
114 raise ValueError("Unknown entry type '%s' in node '%s'" %
115 (etype, node.path))
Simon Glass691198c2018-06-01 09:38:15 -0600116 finally:
117 sys.path = old_path
Simon Glass2574ef62016-11-25 20:15:51 -0700118 modules[module_name] = module
119
120 # Call its constructor to get the object we want.
121 obj = getattr(module, 'Entry_%s' % module_name)
Simon Glassad5a7712018-06-01 09:38:14 -0600122 return obj(section, etype, node)
Simon Glass2574ef62016-11-25 20:15:51 -0700123
124 def ReadNode(self):
125 """Read entry information from the node
126
127 This reads all the fields we recognise from the node, ready for use.
128 """
Simon Glasse8561af2018-08-01 15:22:37 -0600129 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glass2574ef62016-11-25 20:15:51 -0700130 self.size = fdt_util.GetInt(self._node, 'size')
131 self.align = fdt_util.GetInt(self._node, 'align')
132 if tools.NotPowerOfTwo(self.align):
133 raise ValueError("Node '%s': Alignment %s must be a power of two" %
134 (self._node.path, self.align))
135 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
136 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
137 self.align_size = fdt_util.GetInt(self._node, 'align-size')
138 if tools.NotPowerOfTwo(self.align_size):
139 raise ValueError("Node '%s': Alignment size %s must be a power "
140 "of two" % (self._node.path, self.align_size))
141 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glasse8561af2018-08-01 15:22:37 -0600142 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glass2574ef62016-11-25 20:15:51 -0700143
Simon Glasse22f8fa2018-07-06 10:27:41 -0600144 def AddMissingProperties(self):
145 """Add new properties to the device tree as needed for this entry"""
Simon Glass9dcc8612018-08-01 15:22:42 -0600146 for prop in ['offset', 'size', 'image-pos']:
Simon Glasse22f8fa2018-07-06 10:27:41 -0600147 if not prop in self._node.props:
148 self._node.AddZeroProp(prop)
149
150 def SetCalculatedProperties(self):
151 """Set the value of device-tree properties calculated by binman"""
Simon Glasse8561af2018-08-01 15:22:37 -0600152 self._node.SetInt('offset', self.offset)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600153 self._node.SetInt('size', self.size)
Simon Glass9dcc8612018-08-01 15:22:42 -0600154 self._node.SetInt('image-pos', self.image_pos)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600155
Simon Glass92307732018-07-06 10:27:40 -0600156 def ProcessFdt(self, fdt):
157 return True
158
Simon Glass3b78d532018-06-01 09:38:21 -0600159 def SetPrefix(self, prefix):
160 """Set the name prefix for a node
161
162 Args:
163 prefix: Prefix to set, or '' to not use a prefix
164 """
165 if prefix:
166 self.name = prefix + self.name
167
Simon Glass2e1169f2018-07-06 10:27:19 -0600168 def SetContents(self, data):
169 """Set the contents of an entry
170
171 This sets both the data and content_size properties
172
173 Args:
174 data: Data to set to the contents (string)
175 """
176 self.data = data
177 self.contents_size = len(self.data)
178
179 def ProcessContentsUpdate(self, data):
180 """Update the contens of an entry, after the size is fixed
181
182 This checks that the new data is the same size as the old.
183
184 Args:
185 data: Data to set to the contents (string)
186
187 Raises:
188 ValueError if the new data size is not the same as the old
189 """
190 if len(data) != self.contents_size:
191 self.Raise('Cannot update entry size from %d to %d' %
192 (len(data), self.contents_size))
193 self.SetContents(data)
194
Simon Glass2574ef62016-11-25 20:15:51 -0700195 def ObtainContents(self):
196 """Figure out the contents of an entry.
197
198 Returns:
199 True if the contents were found, False if another call is needed
200 after the other entries are processed.
201 """
202 # No contents by default: subclasses can implement this
203 return True
204
Simon Glasse8561af2018-08-01 15:22:37 -0600205 def Pack(self, offset):
Simon Glassad5a7712018-06-01 09:38:14 -0600206 """Figure out how to pack the entry into the section
Simon Glass2574ef62016-11-25 20:15:51 -0700207
208 Most of the time the entries are not fully specified. There may be
209 an alignment but no size. In that case we take the size from the
210 contents of the entry.
211
Simon Glasse8561af2018-08-01 15:22:37 -0600212 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glass2574ef62016-11-25 20:15:51 -0700213
Simon Glasse8561af2018-08-01 15:22:37 -0600214 Once this function is complete, both the offset and size of the
Simon Glass2574ef62016-11-25 20:15:51 -0700215 entry will be know.
216
217 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600218 Current section offset pointer
Simon Glass2574ef62016-11-25 20:15:51 -0700219
220 Returns:
Simon Glasse8561af2018-08-01 15:22:37 -0600221 New section offset pointer (after this entry)
Simon Glass2574ef62016-11-25 20:15:51 -0700222 """
Simon Glasse8561af2018-08-01 15:22:37 -0600223 if self.offset is None:
224 if self.offset_unset:
225 self.Raise('No offset set with offset-unset: should another '
226 'entry provide this correct offset?')
227 self.offset = tools.Align(offset, self.align)
Simon Glass2574ef62016-11-25 20:15:51 -0700228 needed = self.pad_before + self.contents_size + self.pad_after
229 needed = tools.Align(needed, self.align_size)
230 size = self.size
231 if not size:
232 size = needed
Simon Glasse8561af2018-08-01 15:22:37 -0600233 new_offset = self.offset + size
234 aligned_offset = tools.Align(new_offset, self.align_end)
235 if aligned_offset != new_offset:
236 size = aligned_offset - self.offset
237 new_offset = aligned_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700238
239 if not self.size:
240 self.size = size
241
242 if self.size < needed:
243 self.Raise("Entry contents size is %#x (%d) but entry size is "
244 "%#x (%d)" % (needed, needed, self.size, self.size))
245 # Check that the alignment is correct. It could be wrong if the
Simon Glasse8561af2018-08-01 15:22:37 -0600246 # and offset or size values were provided (i.e. not calculated), but
Simon Glass2574ef62016-11-25 20:15:51 -0700247 # conflict with the provided alignment values
248 if self.size != tools.Align(self.size, self.align_size):
249 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
250 (self.size, self.size, self.align_size, self.align_size))
Simon Glasse8561af2018-08-01 15:22:37 -0600251 if self.offset != tools.Align(self.offset, self.align):
252 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
253 (self.offset, self.offset, self.align, self.align))
Simon Glass2574ef62016-11-25 20:15:51 -0700254
Simon Glasse8561af2018-08-01 15:22:37 -0600255 return new_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700256
257 def Raise(self, msg):
258 """Convenience function to raise an error referencing a node"""
259 raise ValueError("Node '%s': %s" % (self._node.path, msg))
260
Simon Glass91710b32018-07-17 13:25:32 -0600261 def GetEntryArgsOrProps(self, props, required=False):
262 """Return the values of a set of properties
263
264 Args:
265 props: List of EntryArg objects
266
267 Raises:
268 ValueError if a property is not found
269 """
270 values = []
271 missing = []
272 for prop in props:
273 python_prop = prop.name.replace('-', '_')
274 if hasattr(self, python_prop):
275 value = getattr(self, python_prop)
276 else:
277 value = None
278 if value is None:
279 value = self.GetArg(prop.name, prop.datatype)
280 if value is None and required:
281 missing.append(prop.name)
282 values.append(value)
283 if missing:
284 self.Raise('Missing required properties/entry args: %s' %
285 (', '.join(missing)))
286 return values
287
Simon Glass2574ef62016-11-25 20:15:51 -0700288 def GetPath(self):
289 """Get the path of a node
290
291 Returns:
292 Full path of the node for this entry
293 """
294 return self._node.path
295
296 def GetData(self):
297 return self.data
298
Simon Glasse8561af2018-08-01 15:22:37 -0600299 def GetOffsets(self):
Simon Glass2574ef62016-11-25 20:15:51 -0700300 return {}
301
Simon Glasse8561af2018-08-01 15:22:37 -0600302 def SetOffsetSize(self, pos, size):
303 self.offset = pos
Simon Glass2574ef62016-11-25 20:15:51 -0700304 self.size = size
305
Simon Glass9dcc8612018-08-01 15:22:42 -0600306 def SetImagePos(self, image_pos):
307 """Set the position in the image
308
309 Args:
310 image_pos: Position of this entry in the image
311 """
312 self.image_pos = image_pos + self.offset
313
Simon Glass2574ef62016-11-25 20:15:51 -0700314 def ProcessContents(self):
315 pass
Simon Glass4ca8e042017-11-13 18:55:01 -0700316
Simon Glass8a6f56e2018-06-01 09:38:13 -0600317 def WriteSymbols(self, section):
Simon Glass4ca8e042017-11-13 18:55:01 -0700318 """Write symbol values into binary files for access at run time
319
320 Args:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600321 section: Section containing the entry
Simon Glass4ca8e042017-11-13 18:55:01 -0700322 """
323 pass
Simon Glassa91e1152018-06-01 09:38:16 -0600324
Simon Glasse8561af2018-08-01 15:22:37 -0600325 def CheckOffset(self):
326 """Check that the entry offsets are correct
Simon Glassa91e1152018-06-01 09:38:16 -0600327
Simon Glasse8561af2018-08-01 15:22:37 -0600328 This is used for entries which have extra offset requirements (other
Simon Glassa91e1152018-06-01 09:38:16 -0600329 than having to be fully inside their section). Sub-classes can implement
330 this function and raise if there is a problem.
331 """
332 pass
Simon Glass30732662018-06-01 09:38:20 -0600333
Simon Glass3a9a2b82018-07-17 13:25:28 -0600334 @staticmethod
335 def WriteMapLine(fd, indent, name, offset, size):
336 print('%s%08x %08x %s' % (' ' * indent, offset, size, name), file=fd)
337
Simon Glass30732662018-06-01 09:38:20 -0600338 def WriteMap(self, fd, indent):
339 """Write a map of the entry to a .map file
340
341 Args:
342 fd: File to write the map to
343 indent: Curent indent level of map (0=none, 1=one level, etc.)
344 """
Simon Glass3a9a2b82018-07-17 13:25:28 -0600345 self.WriteMapLine(fd, indent, self.name, self.offset, self.size)
Simon Glass91710b32018-07-17 13:25:32 -0600346
347 def GetArg(self, name, datatype=str):
348 """Get the value of an entry argument or device-tree-node property
349
350 Some node properties can be provided as arguments to binman. First check
351 the entry arguments, and fall back to the device tree if not found
352
353 Args:
354 name: Argument name
355 datatype: Data type (str or int)
356
357 Returns:
358 Value of argument as a string or int, or None if no value
359
360 Raises:
361 ValueError if the argument cannot be converted to in
362 """
363 value = control.GetEntryArg(name)
364 if value is not None:
365 if datatype == int:
366 try:
367 value = int(value)
368 except ValueError:
369 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
370 (name, value))
371 elif datatype == str:
372 pass
373 else:
374 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
375 datatype)
376 else:
377 value = fdt_util.GetDatatype(self._node, name, datatype)
378 return value