1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Base class for all entries
7 from __future__ import print_function
9 from collections import namedtuple
11 # 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
18 have_importlib = False
29 our_path = os.path.dirname(os.path.realpath(__file__))
32 # An argument which can be passed to entries on the command line, in lieu of
33 # device-tree properties.
34 EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
36 # Information about an entry for use when displaying summaries
37 EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
38 'image_pos', 'uncomp_size', 'offset',
42 """An Entry in the section
44 An entry corresponds to a single node in the device-tree description
45 of the section. Each entry ends up being a part of the final section.
46 Entries can be placed either right next to each other, or with padding
47 between them. The type of the entry determines the data that is in it.
49 This class is not used by itself. All entry objects are subclasses of
53 section: Section object containing this entry
54 node: The node that created this entry
55 offset: Offset of entry within the section, None if not known yet (in
56 which case it will be calculated by Pack())
57 size: Entry size in bytes, None if not known
58 uncomp_size: Size of uncompressed data in bytes, if the entry is
60 contents_size: Size of contents in bytes, 0 by default
61 align: Entry start offset alignment, or None
62 align_size: Entry size alignment, or None
63 align_end: Entry end offset alignment, or None
64 pad_before: Number of pad bytes before the contents, 0 if none
65 pad_after: Number of pad bytes after the contents, 0 if none
66 data: Contents of entry (string of bytes)
67 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
68 orig_offset: Original offset value read from node
69 orig_size: Original size value read from node
71 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
72 self.section = section
75 self.name = node and (name_prefix + node.name) or 'none'
78 self.uncomp_size = None
80 self.contents_size = 0
82 self.align_size = None
86 self.offset_unset = False
88 self._expand_size = False
89 self.compress = 'none'
94 def Lookup(node_path, etype):
95 """Look up the entry class for a node.
98 node_node: Path name of Node object containing information about
99 the entry to create (used for errors)
100 etype: Entry type to use
103 The entry class object if found, else None
105 # Convert something like 'u-boot@0' to 'u_boot' since we are only
106 # interested in the type.
107 module_name = etype.replace('-', '_')
108 if '@' in module_name:
109 module_name = module_name.split('@')[0]
110 module = modules.get(module_name)
112 # Also allow entry-type modules to be brought in from the etype directory.
114 # Import the module if we have not already done so.
117 sys.path.insert(0, os.path.join(our_path, 'etype'))
120 module = importlib.import_module(module_name)
122 module = __import__(module_name)
123 except ImportError as e:
124 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
125 (etype, node_path, module_name, e))
128 modules[module_name] = module
130 # Look up the expected class name
131 return getattr(module, 'Entry_%s' % module_name)
134 def Create(section, node, etype=None):
135 """Create a new entry for a node.
138 section: Section object containing this node
139 node: Node object containing information about the entry to
141 etype: Entry type to use, or None to work it out (used for tests)
144 A new Entry object of the correct type (a subclass of Entry)
147 etype = fdt_util.GetString(node, 'type', node.name)
148 obj = Entry.Lookup(node.path, etype)
150 # Call its constructor to get the object we want.
151 return obj(section, etype, node)
154 """Read entry information from the node
156 This reads all the fields we recognise from the node, ready for use.
158 if 'pos' in self._node.props:
159 self.Raise("Please use 'offset' instead of 'pos'")
160 self.offset = fdt_util.GetInt(self._node, 'offset')
161 self.size = fdt_util.GetInt(self._node, 'size')
162 self.orig_offset = self.offset
163 self.orig_size = self.size
165 # These should not be set in input files, but are set in an FDT map,
166 # which is also read by this code.
167 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
168 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
170 self.align = fdt_util.GetInt(self._node, 'align')
171 if tools.NotPowerOfTwo(self.align):
172 raise ValueError("Node '%s': Alignment %s must be a power of two" %
173 (self._node.path, self.align))
174 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
175 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
176 self.align_size = fdt_util.GetInt(self._node, 'align-size')
177 if tools.NotPowerOfTwo(self.align_size):
178 raise ValueError("Node '%s': Alignment size %s must be a power "
179 "of two" % (self._node.path, self.align_size))
180 self.align_end = fdt_util.GetInt(self._node, 'align-end')
181 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
182 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
184 def GetDefaultFilename(self):
188 """Get the set of device trees used by this entry
191 Set containing the filename from this entry, if it is a .dtb, else
194 fname = self.GetDefaultFilename()
195 # It would be better to use isinstance(self, Entry_blob_dtb) here but
196 # we cannot access Entry_blob_dtb
197 if fname and fname.endswith('.dtb'):
201 def ExpandEntries(self):
204 def AddMissingProperties(self):
205 """Add new properties to the device tree as needed for this entry"""
206 for prop in ['offset', 'size', 'image-pos']:
207 if not prop in self._node.props:
208 state.AddZeroProp(self._node, prop)
209 if self.compress != 'none':
210 state.AddZeroProp(self._node, 'uncomp-size')
211 err = state.CheckAddHashProp(self._node)
215 def SetCalculatedProperties(self):
216 """Set the value of device-tree properties calculated by binman"""
217 state.SetInt(self._node, 'offset', self.offset)
218 state.SetInt(self._node, 'size', self.size)
219 state.SetInt(self._node, 'image-pos',
220 self.image_pos - self.section.GetRootSkipAtStart())
221 if self.uncomp_size is not None:
222 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
223 state.CheckSetHashValue(self._node, self.GetData)
225 def ProcessFdt(self, fdt):
226 """Allow entries to adjust the device tree
228 Some entries need to adjust the device tree for their purposes. This
229 may involve adding or deleting properties.
232 True if processing is complete
233 False if processing could not be completed due to a dependency.
234 This will cause the entry to be retried after others have been
239 def SetPrefix(self, prefix):
240 """Set the name prefix for a node
243 prefix: Prefix to set, or '' to not use a prefix
246 self.name = prefix + self.name
248 def SetContents(self, data):
249 """Set the contents of an entry
251 This sets both the data and content_size properties
254 data: Data to set to the contents (bytes)
257 self.contents_size = len(self.data)
259 def ProcessContentsUpdate(self, data):
260 """Update the contents of an entry, after the size is fixed
262 This checks that the new data is the same size as the old. If the size
263 has changed, this triggers a re-run of the packing algorithm.
266 data: Data to set to the contents (bytes)
269 ValueError if the new data size is not the same as the old
273 if state.AllowEntryExpansion():
274 if new_size > self.contents_size:
275 print("Entry '%s' size change from %#x to %#x" % (
276 self._node.path, self.contents_size, new_size))
277 # self.data will indicate the new size needed
279 elif new_size != self.contents_size:
280 self.Raise('Cannot update entry size from %d to %d' %
281 (self.contents_size, new_size))
282 self.SetContents(data)
285 def ObtainContents(self):
286 """Figure out the contents of an entry.
289 True if the contents were found, False if another call is needed
290 after the other entries are processed.
292 # No contents by default: subclasses can implement this
295 def ResetForPack(self):
296 """Reset offset/size fields so that packing can be done again"""
297 self.offset = self.orig_offset
298 self.size = self.orig_size
300 def Pack(self, offset):
301 """Figure out how to pack the entry into the section
303 Most of the time the entries are not fully specified. There may be
304 an alignment but no size. In that case we take the size from the
305 contents of the entry.
307 If an entry has no hard-coded offset, it will be placed at @offset.
309 Once this function is complete, both the offset and size of the
313 Current section offset pointer
316 New section offset pointer (after this entry)
318 if self.offset is None:
319 if self.offset_unset:
320 self.Raise('No offset set with offset-unset: should another '
321 'entry provide this correct offset?')
322 self.offset = tools.Align(offset, self.align)
323 needed = self.pad_before + self.contents_size + self.pad_after
324 needed = tools.Align(needed, self.align_size)
328 new_offset = self.offset + size
329 aligned_offset = tools.Align(new_offset, self.align_end)
330 if aligned_offset != new_offset:
331 size = aligned_offset - self.offset
332 new_offset = aligned_offset
337 if self.size < needed:
338 self.Raise("Entry contents size is %#x (%d) but entry size is "
339 "%#x (%d)" % (needed, needed, self.size, self.size))
340 # Check that the alignment is correct. It could be wrong if the
341 # and offset or size values were provided (i.e. not calculated), but
342 # conflict with the provided alignment values
343 if self.size != tools.Align(self.size, self.align_size):
344 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
345 (self.size, self.size, self.align_size, self.align_size))
346 if self.offset != tools.Align(self.offset, self.align):
347 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
348 (self.offset, self.offset, self.align, self.align))
352 def Raise(self, msg):
353 """Convenience function to raise an error referencing a node"""
354 raise ValueError("Node '%s': %s" % (self._node.path, msg))
356 def GetEntryArgsOrProps(self, props, required=False):
357 """Return the values of a set of properties
360 props: List of EntryArg objects
363 ValueError if a property is not found
368 python_prop = prop.name.replace('-', '_')
369 if hasattr(self, python_prop):
370 value = getattr(self, python_prop)
374 value = self.GetArg(prop.name, prop.datatype)
375 if value is None and required:
376 missing.append(prop.name)
379 self.Raise('Missing required properties/entry args: %s' %
380 (', '.join(missing)))
384 """Get the path of a node
387 Full path of the node for this entry
389 return self._node.path
394 def GetOffsets(self):
395 """Get the offsets for siblings
397 Some entry types can contain information about the position or size of
398 other entries. An example of this is the Intel Flash Descriptor, which
399 knows where the Intel Management Engine section should go.
401 If this entry knows about the position of other entries, it can specify
402 this by returning values here
407 value: List containing position and size of the given entry
408 type. Either can be None if not known
412 def SetOffsetSize(self, offset, size):
413 """Set the offset and/or size of an entry
416 offset: New offset, or None to leave alone
417 size: New size, or None to leave alone
419 if offset is not None:
424 def SetImagePos(self, image_pos):
425 """Set the position in the image
428 image_pos: Position of this entry in the image
430 self.image_pos = image_pos + self.offset
432 def ProcessContents(self):
433 """Do any post-packing updates of entry contents
435 This function should call ProcessContentsUpdate() to update the entry
436 contents, if necessary, returning its return value here.
439 data: Data to set to the contents (bytes)
442 True if the new data size is OK, False if expansion is needed
445 ValueError if the new data size is not the same as the old and
446 state.AllowEntryExpansion() is False
450 def WriteSymbols(self, section):
451 """Write symbol values into binary files for access at run time
454 section: Section containing the entry
458 def CheckOffset(self):
459 """Check that the entry offsets are correct
461 This is used for entries which have extra offset requirements (other
462 than having to be fully inside their section). Sub-classes can implement
463 this function and raise if there is a problem.
471 return '%08x' % value
474 def WriteMapLine(fd, indent, name, offset, size, image_pos):
475 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
476 Entry.GetStr(offset), Entry.GetStr(size),
479 def WriteMap(self, fd, indent):
480 """Write a map of the entry to a .map file
483 fd: File to write the map to
484 indent: Curent indent level of map (0=none, 1=one level, etc.)
486 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
489 def GetEntries(self):
490 """Return a list of entries contained by this entry
493 List of entries, or None if none. A normal entry has no entries
494 within it so will return None
498 def GetArg(self, name, datatype=str):
499 """Get the value of an entry argument or device-tree-node property
501 Some node properties can be provided as arguments to binman. First check
502 the entry arguments, and fall back to the device tree if not found
506 datatype: Data type (str or int)
509 Value of argument as a string or int, or None if no value
512 ValueError if the argument cannot be converted to in
514 value = state.GetEntryArg(name)
515 if value is not None:
520 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
522 elif datatype == str:
525 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
528 value = fdt_util.GetDatatype(self._node, name, datatype)
532 def WriteDocs(modules, test_missing=None):
533 """Write out documentation about the various entry types to stdout
536 modules: List of modules to include
537 test_missing: Used for testing. This is a module to report
540 print('''Binman Entry Documentation
541 ===========================
543 This file describes the entry types supported by binman. These entry types can
544 be placed in an image one by one to build up a final firmware image. It is
545 fairly easy to create new entry types. Just add a new file to the 'etype'
546 directory. You can use the existing entries as examples.
548 Note that some entries are subclasses of others, using and extending their
549 features to produce new behaviours.
553 modules = sorted(modules)
555 # Don't show the test entry
556 if '_testing' in modules:
557 modules.remove('_testing')
560 if name.startswith('__'):
562 module = Entry.Lookup(name, name)
563 docs = getattr(module, '__doc__')
564 if test_missing == name:
567 lines = docs.splitlines()
568 first_line = lines[0]
569 rest = [line[4:] for line in lines[1:]]
570 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
572 print('-' * len(hdr))
573 print('\n'.join(rest))
580 raise ValueError('Documentation is missing for modules: %s' %
583 def GetUniqueName(self):
584 """Get a unique name for a node
587 String containing a unique name for a node, consisting of the name
588 of all ancestors (starting from within the 'binman' node) separated
589 by a dot ('.'). This can be useful for generating unique filesnames
590 in the output directory.
596 if node.name == 'binman':
598 name = '%s.%s' % (node.name, name)
601 def ExpandToLimit(self, limit):
602 """Expand an entry so that it ends at the given offset limit"""
603 if self.offset + self.size < limit:
604 self.size = limit - self.offset
605 # Request the contents again, since changing the size requires that
606 # the data grows. This should not fail, but check it to be sure.
607 if not self.ObtainContents():
608 self.Raise('Cannot obtain contents when expanding entry')
610 def HasSibling(self, name):
611 """Check if there is a sibling of a given name
614 True if there is an entry with this name in the the same section,
617 return name in self.section.GetEntries()
619 def GetSiblingImagePos(self, name):
620 """Return the image position of the given sibling
623 Image position of sibling, or None if the sibling has no position,
624 or False if there is no such sibling
626 if not self.HasSibling(name):
628 return self.section.GetEntries()[name].image_pos
631 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
632 uncomp_size, offset, entry):
633 """Add a new entry to the entries list
636 entries: List (of EntryInfo objects) to add to
637 indent: Current indent level to add to list
638 name: Entry name (string)
639 etype: Entry type (string)
640 size: Entry size in bytes (int)
641 image_pos: Position within image in bytes (int)
642 uncomp_size: Uncompressed size if the entry uses compression, else
644 offset: Entry offset within parent in bytes (int)
647 entries.append(EntryInfo(indent, name, etype, size, image_pos,
648 uncomp_size, offset, entry))
650 def ListEntries(self, entries, indent):
651 """Add files in this entry to the list of entries
653 This can be overridden by subclasses which need different behaviour.
656 entries: List (of EntryInfo objects) to add to
657 indent: Current indent level to add to list
659 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
660 self.image_pos, self.uncomp_size, self.offset, self)