blob: fd7223477eb02bbb142b8f50ff75245bf9573062 [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 Glass53af22a2018-07-17 13:25:32 -06009from collections import namedtuple
10
Simon Glassbf7fd502016-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
Simon Glassbadf0ec2018-06-01 09:38:15 -060020import os
Simon Glass539aece2018-09-14 04:57:22 -060021from sets import Set
Simon Glassbadf0ec2018-06-01 09:38:15 -060022import sys
Simon Glassc55a50f2018-09-14 04:57:19 -060023
24import fdt_util
25import state
Simon Glassbf7fd502016-11-25 20:15:51 -070026import tools
27
28modules = {}
29
Simon Glassbadf0ec2018-06-01 09:38:15 -060030our_path = os.path.dirname(os.path.realpath(__file__))
31
Simon Glass53af22a2018-07-17 13:25:32 -060032
33# An argument which can be passed to entries on the command line, in lieu of
34# device-tree properties.
35EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
36
37
Simon Glassbf7fd502016-11-25 20:15:51 -070038class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060039 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070040
41 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060042 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070043 Entries can be placed either right next to each other, or with padding
44 between them. The type of the entry determines the data that is in it.
45
46 This class is not used by itself. All entry objects are subclasses of
47 Entry.
48
49 Attributes:
Simon Glass8122f392018-07-17 13:25:28 -060050 section: Section object containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070051 node: The node that created this entry
Simon Glass3ab95982018-08-01 15:22:37 -060052 offset: Offset of entry within the section, None if not known yet (in
53 which case it will be calculated by Pack())
Simon Glassbf7fd502016-11-25 20:15:51 -070054 size: Entry size in bytes, None if not known
55 contents_size: Size of contents in bytes, 0 by default
Simon Glass3ab95982018-08-01 15:22:37 -060056 align: Entry start offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070057 align_size: Entry size alignment, or None
Simon Glass3ab95982018-08-01 15:22:37 -060058 align_end: Entry end offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070059 pad_before: Number of pad bytes before the contents, 0 if none
60 pad_after: Number of pad bytes after the contents, 0 if none
61 data: Contents of entry (string of bytes)
62 """
Simon Glassc8d48ef2018-06-01 09:38:21 -060063 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glass25ac0e62018-06-01 09:38:14 -060064 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070065 self.etype = etype
66 self._node = node
Simon Glassc8d48ef2018-06-01 09:38:21 -060067 self.name = node and (name_prefix + node.name) or 'none'
Simon Glass3ab95982018-08-01 15:22:37 -060068 self.offset = None
Simon Glassbf7fd502016-11-25 20:15:51 -070069 self.size = None
Simon Glass24d0d3c2018-07-17 13:25:47 -060070 self.data = None
Simon Glassbf7fd502016-11-25 20:15:51 -070071 self.contents_size = 0
72 self.align = None
73 self.align_size = None
74 self.align_end = None
75 self.pad_before = 0
76 self.pad_after = 0
Simon Glass3ab95982018-08-01 15:22:37 -060077 self.offset_unset = False
Simon Glassdbf6be92018-08-01 15:22:42 -060078 self.image_pos = None
Simon Glassba64a0b2018-09-14 04:57:29 -060079 self._expand_size = False
Simon Glassbf7fd502016-11-25 20:15:51 -070080 if read_node:
81 self.ReadNode()
82
83 @staticmethod
Simon Glassfd8d1f72018-07-17 13:25:36 -060084 def Lookup(section, node_path, etype):
85 """Look up the entry class for a node.
Simon Glassbf7fd502016-11-25 20:15:51 -070086
87 Args:
Simon Glassfd8d1f72018-07-17 13:25:36 -060088 section: Section object containing this node
89 node_node: Path name of Node object containing information about
90 the entry to create (used for errors)
91 etype: Entry type to use
Simon Glassbf7fd502016-11-25 20:15:51 -070092
93 Returns:
Simon Glassfd8d1f72018-07-17 13:25:36 -060094 The entry class object if found, else None
Simon Glassbf7fd502016-11-25 20:15:51 -070095 """
Simon Glassdd57c132018-06-01 09:38:11 -060096 # Convert something like 'u-boot@0' to 'u_boot' since we are only
97 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -070098 module_name = etype.replace('-', '_')
Simon Glassdd57c132018-06-01 09:38:11 -060099 if '@' in module_name:
100 module_name = module_name.split('@')[0]
Simon Glassbf7fd502016-11-25 20:15:51 -0700101 module = modules.get(module_name)
102
Simon Glassbadf0ec2018-06-01 09:38:15 -0600103 # Also allow entry-type modules to be brought in from the etype directory.
104
Simon Glassbf7fd502016-11-25 20:15:51 -0700105 # Import the module if we have not already done so.
106 if not module:
Simon Glassbadf0ec2018-06-01 09:38:15 -0600107 old_path = sys.path
108 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -0700109 try:
110 if have_importlib:
111 module = importlib.import_module(module_name)
112 else:
113 module = __import__(module_name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600114 except ImportError as e:
115 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
116 (etype, node_path, module_name, e))
Simon Glassbadf0ec2018-06-01 09:38:15 -0600117 finally:
118 sys.path = old_path
Simon Glassbf7fd502016-11-25 20:15:51 -0700119 modules[module_name] = module
120
Simon Glassfd8d1f72018-07-17 13:25:36 -0600121 # Look up the expected class name
122 return getattr(module, 'Entry_%s' % module_name)
123
124 @staticmethod
125 def Create(section, node, etype=None):
126 """Create a new entry for a node.
127
128 Args:
129 section: Section object containing this node
130 node: Node object containing information about the entry to
131 create
132 etype: Entry type to use, or None to work it out (used for tests)
133
134 Returns:
135 A new Entry object of the correct type (a subclass of Entry)
136 """
137 if not etype:
138 etype = fdt_util.GetString(node, 'type', node.name)
139 obj = Entry.Lookup(section, node.path, etype)
140
Simon Glassbf7fd502016-11-25 20:15:51 -0700141 # Call its constructor to get the object we want.
Simon Glass25ac0e62018-06-01 09:38:14 -0600142 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700143
144 def ReadNode(self):
145 """Read entry information from the node
146
147 This reads all the fields we recognise from the node, ready for use.
148 """
Simon Glass15a587c2018-07-17 13:25:51 -0600149 if 'pos' in self._node.props:
150 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glass3ab95982018-08-01 15:22:37 -0600151 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700152 self.size = fdt_util.GetInt(self._node, 'size')
153 self.align = fdt_util.GetInt(self._node, 'align')
154 if tools.NotPowerOfTwo(self.align):
155 raise ValueError("Node '%s': Alignment %s must be a power of two" %
156 (self._node.path, self.align))
157 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
158 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
159 self.align_size = fdt_util.GetInt(self._node, 'align-size')
160 if tools.NotPowerOfTwo(self.align_size):
161 raise ValueError("Node '%s': Alignment size %s must be a power "
162 "of two" % (self._node.path, self.align_size))
163 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glass3ab95982018-08-01 15:22:37 -0600164 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassba64a0b2018-09-14 04:57:29 -0600165 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
Simon Glassbf7fd502016-11-25 20:15:51 -0700166
Simon Glass6c234bf2018-09-14 04:57:18 -0600167 def GetDefaultFilename(self):
168 return None
169
Simon Glass539aece2018-09-14 04:57:22 -0600170 def GetFdtSet(self):
171 """Get the set of device trees used by this entry
172
173 Returns:
174 Set containing the filename from this entry, if it is a .dtb, else
175 an empty set
176 """
177 fname = self.GetDefaultFilename()
178 # It would be better to use isinstance(self, Entry_blob_dtb) here but
179 # we cannot access Entry_blob_dtb
180 if fname and fname.endswith('.dtb'):
181 return Set([fname])
182 return Set()
183
Simon Glass0a98b282018-09-14 04:57:28 -0600184 def ExpandEntries(self):
185 pass
186
Simon Glass078ab1a2018-07-06 10:27:41 -0600187 def AddMissingProperties(self):
188 """Add new properties to the device tree as needed for this entry"""
Simon Glassdbf6be92018-08-01 15:22:42 -0600189 for prop in ['offset', 'size', 'image-pos']:
Simon Glass078ab1a2018-07-06 10:27:41 -0600190 if not prop in self._node.props:
Simon Glassf46621d2018-09-14 04:57:21 -0600191 state.AddZeroProp(self._node, prop)
Simon Glasse0e5df92018-09-14 04:57:31 -0600192 err = state.CheckAddHashProp(self._node)
193 if err:
194 self.Raise(err)
Simon Glass078ab1a2018-07-06 10:27:41 -0600195
196 def SetCalculatedProperties(self):
197 """Set the value of device-tree properties calculated by binman"""
Simon Glassf46621d2018-09-14 04:57:21 -0600198 state.SetInt(self._node, 'offset', self.offset)
199 state.SetInt(self._node, 'size', self.size)
200 state.SetInt(self._node, 'image-pos', self.image_pos)
Simon Glasse0e5df92018-09-14 04:57:31 -0600201 state.CheckSetHashValue(self._node, self.GetData)
Simon Glass078ab1a2018-07-06 10:27:41 -0600202
Simon Glassecab8972018-07-06 10:27:40 -0600203 def ProcessFdt(self, fdt):
Simon Glass6ed45ba2018-09-14 04:57:24 -0600204 """Allow entries to adjust the device tree
205
206 Some entries need to adjust the device tree for their purposes. This
207 may involve adding or deleting properties.
208
209 Returns:
210 True if processing is complete
211 False if processing could not be completed due to a dependency.
212 This will cause the entry to be retried after others have been
213 called
214 """
Simon Glassecab8972018-07-06 10:27:40 -0600215 return True
216
Simon Glassc8d48ef2018-06-01 09:38:21 -0600217 def SetPrefix(self, prefix):
218 """Set the name prefix for a node
219
220 Args:
221 prefix: Prefix to set, or '' to not use a prefix
222 """
223 if prefix:
224 self.name = prefix + self.name
225
Simon Glass5c890232018-07-06 10:27:19 -0600226 def SetContents(self, data):
227 """Set the contents of an entry
228
229 This sets both the data and content_size properties
230
231 Args:
232 data: Data to set to the contents (string)
233 """
234 self.data = data
235 self.contents_size = len(self.data)
236
237 def ProcessContentsUpdate(self, data):
238 """Update the contens of an entry, after the size is fixed
239
240 This checks that the new data is the same size as the old.
241
242 Args:
243 data: Data to set to the contents (string)
244
245 Raises:
246 ValueError if the new data size is not the same as the old
247 """
248 if len(data) != self.contents_size:
249 self.Raise('Cannot update entry size from %d to %d' %
250 (len(data), self.contents_size))
251 self.SetContents(data)
252
Simon Glassbf7fd502016-11-25 20:15:51 -0700253 def ObtainContents(self):
254 """Figure out the contents of an entry.
255
256 Returns:
257 True if the contents were found, False if another call is needed
258 after the other entries are processed.
259 """
260 # No contents by default: subclasses can implement this
261 return True
262
Simon Glass3ab95982018-08-01 15:22:37 -0600263 def Pack(self, offset):
Simon Glass25ac0e62018-06-01 09:38:14 -0600264 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700265
266 Most of the time the entries are not fully specified. There may be
267 an alignment but no size. In that case we take the size from the
268 contents of the entry.
269
Simon Glass3ab95982018-08-01 15:22:37 -0600270 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glassbf7fd502016-11-25 20:15:51 -0700271
Simon Glass3ab95982018-08-01 15:22:37 -0600272 Once this function is complete, both the offset and size of the
Simon Glassbf7fd502016-11-25 20:15:51 -0700273 entry will be know.
274
275 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600276 Current section offset pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700277
278 Returns:
Simon Glass3ab95982018-08-01 15:22:37 -0600279 New section offset pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700280 """
Simon Glass3ab95982018-08-01 15:22:37 -0600281 if self.offset is None:
282 if self.offset_unset:
283 self.Raise('No offset set with offset-unset: should another '
284 'entry provide this correct offset?')
285 self.offset = tools.Align(offset, self.align)
Simon Glassbf7fd502016-11-25 20:15:51 -0700286 needed = self.pad_before + self.contents_size + self.pad_after
287 needed = tools.Align(needed, self.align_size)
288 size = self.size
289 if not size:
290 size = needed
Simon Glass3ab95982018-08-01 15:22:37 -0600291 new_offset = self.offset + size
292 aligned_offset = tools.Align(new_offset, self.align_end)
293 if aligned_offset != new_offset:
294 size = aligned_offset - self.offset
295 new_offset = aligned_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700296
297 if not self.size:
298 self.size = size
299
300 if self.size < needed:
301 self.Raise("Entry contents size is %#x (%d) but entry size is "
302 "%#x (%d)" % (needed, needed, self.size, self.size))
303 # Check that the alignment is correct. It could be wrong if the
Simon Glass3ab95982018-08-01 15:22:37 -0600304 # and offset or size values were provided (i.e. not calculated), but
Simon Glassbf7fd502016-11-25 20:15:51 -0700305 # conflict with the provided alignment values
306 if self.size != tools.Align(self.size, self.align_size):
307 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
308 (self.size, self.size, self.align_size, self.align_size))
Simon Glass3ab95982018-08-01 15:22:37 -0600309 if self.offset != tools.Align(self.offset, self.align):
310 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
311 (self.offset, self.offset, self.align, self.align))
Simon Glassbf7fd502016-11-25 20:15:51 -0700312
Simon Glass3ab95982018-08-01 15:22:37 -0600313 return new_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700314
315 def Raise(self, msg):
316 """Convenience function to raise an error referencing a node"""
317 raise ValueError("Node '%s': %s" % (self._node.path, msg))
318
Simon Glass53af22a2018-07-17 13:25:32 -0600319 def GetEntryArgsOrProps(self, props, required=False):
320 """Return the values of a set of properties
321
322 Args:
323 props: List of EntryArg objects
324
325 Raises:
326 ValueError if a property is not found
327 """
328 values = []
329 missing = []
330 for prop in props:
331 python_prop = prop.name.replace('-', '_')
332 if hasattr(self, python_prop):
333 value = getattr(self, python_prop)
334 else:
335 value = None
336 if value is None:
337 value = self.GetArg(prop.name, prop.datatype)
338 if value is None and required:
339 missing.append(prop.name)
340 values.append(value)
341 if missing:
342 self.Raise('Missing required properties/entry args: %s' %
343 (', '.join(missing)))
344 return values
345
Simon Glassbf7fd502016-11-25 20:15:51 -0700346 def GetPath(self):
347 """Get the path of a node
348
349 Returns:
350 Full path of the node for this entry
351 """
352 return self._node.path
353
354 def GetData(self):
355 return self.data
356
Simon Glass3ab95982018-08-01 15:22:37 -0600357 def GetOffsets(self):
Simon Glassbf7fd502016-11-25 20:15:51 -0700358 return {}
359
Simon Glass3ab95982018-08-01 15:22:37 -0600360 def SetOffsetSize(self, pos, size):
361 self.offset = pos
Simon Glassbf7fd502016-11-25 20:15:51 -0700362 self.size = size
363
Simon Glassdbf6be92018-08-01 15:22:42 -0600364 def SetImagePos(self, image_pos):
365 """Set the position in the image
366
367 Args:
368 image_pos: Position of this entry in the image
369 """
370 self.image_pos = image_pos + self.offset
371
Simon Glassbf7fd502016-11-25 20:15:51 -0700372 def ProcessContents(self):
373 pass
Simon Glass19790632017-11-13 18:55:01 -0700374
Simon Glassf55382b2018-06-01 09:38:13 -0600375 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700376 """Write symbol values into binary files for access at run time
377
378 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600379 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700380 """
381 pass
Simon Glass18546952018-06-01 09:38:16 -0600382
Simon Glass3ab95982018-08-01 15:22:37 -0600383 def CheckOffset(self):
384 """Check that the entry offsets are correct
Simon Glass18546952018-06-01 09:38:16 -0600385
Simon Glass3ab95982018-08-01 15:22:37 -0600386 This is used for entries which have extra offset requirements (other
Simon Glass18546952018-06-01 09:38:16 -0600387 than having to be fully inside their section). Sub-classes can implement
388 this function and raise if there is a problem.
389 """
390 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600391
Simon Glass8122f392018-07-17 13:25:28 -0600392 @staticmethod
Simon Glass1be70d22018-07-17 13:25:49 -0600393 def WriteMapLine(fd, indent, name, offset, size, image_pos):
394 print('%08x %s%08x %08x %s' % (image_pos, ' ' * indent, offset,
395 size, name), file=fd)
Simon Glass8122f392018-07-17 13:25:28 -0600396
Simon Glass3b0c3822018-06-01 09:38:20 -0600397 def WriteMap(self, fd, indent):
398 """Write a map of the entry to a .map file
399
400 Args:
401 fd: File to write the map to
402 indent: Curent indent level of map (0=none, 1=one level, etc.)
403 """
Simon Glass1be70d22018-07-17 13:25:49 -0600404 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
405 self.image_pos)
Simon Glass53af22a2018-07-17 13:25:32 -0600406
Simon Glass11e36cc2018-07-17 13:25:38 -0600407 def GetEntries(self):
408 """Return a list of entries contained by this entry
409
410 Returns:
411 List of entries, or None if none. A normal entry has no entries
412 within it so will return None
413 """
414 return None
415
Simon Glass53af22a2018-07-17 13:25:32 -0600416 def GetArg(self, name, datatype=str):
417 """Get the value of an entry argument or device-tree-node property
418
419 Some node properties can be provided as arguments to binman. First check
420 the entry arguments, and fall back to the device tree if not found
421
422 Args:
423 name: Argument name
424 datatype: Data type (str or int)
425
426 Returns:
427 Value of argument as a string or int, or None if no value
428
429 Raises:
430 ValueError if the argument cannot be converted to in
431 """
Simon Glassc55a50f2018-09-14 04:57:19 -0600432 value = state.GetEntryArg(name)
Simon Glass53af22a2018-07-17 13:25:32 -0600433 if value is not None:
434 if datatype == int:
435 try:
436 value = int(value)
437 except ValueError:
438 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
439 (name, value))
440 elif datatype == str:
441 pass
442 else:
443 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
444 datatype)
445 else:
446 value = fdt_util.GetDatatype(self._node, name, datatype)
447 return value
Simon Glassfd8d1f72018-07-17 13:25:36 -0600448
449 @staticmethod
450 def WriteDocs(modules, test_missing=None):
451 """Write out documentation about the various entry types to stdout
452
453 Args:
454 modules: List of modules to include
455 test_missing: Used for testing. This is a module to report
456 as missing
457 """
458 print('''Binman Entry Documentation
459===========================
460
461This file describes the entry types supported by binman. These entry types can
462be placed in an image one by one to build up a final firmware image. It is
463fairly easy to create new entry types. Just add a new file to the 'etype'
464directory. You can use the existing entries as examples.
465
466Note that some entries are subclasses of others, using and extending their
467features to produce new behaviours.
468
469
470''')
471 modules = sorted(modules)
472
473 # Don't show the test entry
474 if '_testing' in modules:
475 modules.remove('_testing')
476 missing = []
477 for name in modules:
478 module = Entry.Lookup(name, name, name)
479 docs = getattr(module, '__doc__')
480 if test_missing == name:
481 docs = None
482 if docs:
483 lines = docs.splitlines()
484 first_line = lines[0]
485 rest = [line[4:] for line in lines[1:]]
486 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
487 print(hdr)
488 print('-' * len(hdr))
489 print('\n'.join(rest))
490 print()
491 print()
492 else:
493 missing.append(name)
494
495 if missing:
496 raise ValueError('Documentation is missing for modules: %s' %
497 ', '.join(missing))
Simon Glassa326b492018-09-14 04:57:11 -0600498
499 def GetUniqueName(self):
500 """Get a unique name for a node
501
502 Returns:
503 String containing a unique name for a node, consisting of the name
504 of all ancestors (starting from within the 'binman' node) separated
505 by a dot ('.'). This can be useful for generating unique filesnames
506 in the output directory.
507 """
508 name = self.name
509 node = self._node
510 while node.parent:
511 node = node.parent
512 if node.name == 'binman':
513 break
514 name = '%s.%s' % (node.name, name)
515 return name
Simon Glassba64a0b2018-09-14 04:57:29 -0600516
517 def ExpandToLimit(self, limit):
518 """Expand an entry so that it ends at the given offset limit"""
519 if self.offset + self.size < limit:
520 self.size = limit - self.offset
521 # Request the contents again, since changing the size requires that
522 # the data grows. This should not fail, but check it to be sure.
523 if not self.ObtainContents():
524 self.Raise('Cannot obtain contents when expanding entry')