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
30 our_path = os.path.dirname(os.path.realpath(__file__))
33 # An argument which can be passed to entries on the command line, in lieu of
34 # device-tree properties.
35 EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
37 # Information about an entry for use when displaying summaries
38 EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
39 'image_pos', 'uncomp_size', 'offset',
43 """An Entry in the section
45 An entry corresponds to a single node in the device-tree description
46 of the section. Each entry ends up being a part of the final section.
47 Entries can be placed either right next to each other, or with padding
48 between them. The type of the entry determines the data that is in it.
50 This class is not used by itself. All entry objects are subclasses of
54 section: Section object containing this entry
55 node: The node that created this entry
56 offset: Offset of entry within the section, None if not known yet (in
57 which case it will be calculated by Pack())
58 size: Entry size in bytes, None if not known
59 uncomp_size: Size of uncompressed data in bytes, if the entry is
61 contents_size: Size of contents in bytes, 0 by default
62 align: Entry start offset alignment, or None
63 align_size: Entry size alignment, or None
64 align_end: Entry end offset alignment, or None
65 pad_before: Number of pad bytes before the contents, 0 if none
66 pad_after: Number of pad bytes after the contents, 0 if none
67 data: Contents of entry (string of bytes)
68 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
69 orig_offset: Original offset value read from node
70 orig_size: Original size value read from node
72 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
73 self.section = section
76 self.name = node and (name_prefix + node.name) or 'none'
79 self.uncomp_size = None
81 self.contents_size = 0
83 self.align_size = None
87 self.offset_unset = False
89 self._expand_size = False
90 self.compress = 'none'
95 def Lookup(node_path, etype):
96 """Look up the entry class for a node.
99 node_node: Path name of Node object containing information about
100 the entry to create (used for errors)
101 etype: Entry type to use
104 The entry class object if found, else None
106 # Convert something like 'u-boot@0' to 'u_boot' since we are only
107 # interested in the type.
108 module_name = etype.replace('-', '_')
109 if '@' in module_name:
110 module_name = module_name.split('@')[0]
111 module = modules.get(module_name)
113 # Also allow entry-type modules to be brought in from the etype directory.
115 # Import the module if we have not already done so.
118 sys.path.insert(0, os.path.join(our_path, 'etype'))
121 module = importlib.import_module(module_name)
123 module = __import__(module_name)
124 except ImportError as e:
125 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
126 (etype, node_path, module_name, e))
129 modules[module_name] = module
131 # Look up the expected class name
132 return getattr(module, 'Entry_%s' % module_name)
135 def Create(section, node, etype=None):
136 """Create a new entry for a node.
139 section: Section object containing this node
140 node: Node object containing information about the entry to
142 etype: Entry type to use, or None to work it out (used for tests)
145 A new Entry object of the correct type (a subclass of Entry)
148 etype = fdt_util.GetString(node, 'type', node.name)
149 obj = Entry.Lookup(node.path, etype)
151 # Call its constructor to get the object we want.
152 return obj(section, etype, node)
155 """Read entry information from the node
157 This reads all the fields we recognise from the node, ready for use.
159 if 'pos' in self._node.props:
160 self.Raise("Please use 'offset' instead of 'pos'")
161 self.offset = fdt_util.GetInt(self._node, 'offset')
162 self.size = fdt_util.GetInt(self._node, 'size')
163 self.orig_offset = self.offset
164 self.orig_size = self.size
166 # These should not be set in input files, but are set in an FDT map,
167 # which is also read by this code.
168 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
169 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
171 self.align = fdt_util.GetInt(self._node, 'align')
172 if tools.NotPowerOfTwo(self.align):
173 raise ValueError("Node '%s': Alignment %s must be a power of two" %
174 (self._node.path, self.align))
175 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
176 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
177 self.align_size = fdt_util.GetInt(self._node, 'align-size')
178 if tools.NotPowerOfTwo(self.align_size):
179 self.Raise("Alignment size %s must be a power of two" %
181 self.align_end = fdt_util.GetInt(self._node, 'align-end')
182 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
183 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
185 def GetDefaultFilename(self):
189 """Get the device trees used by this entry
192 Empty dict, if this entry is not a .dtb, otherwise:
194 key: Filename from this entry (without the path)
195 value: Fdt object for this dtb, or None if not available
199 def ExpandEntries(self):
202 def AddMissingProperties(self):
203 """Add new properties to the device tree as needed for this entry"""
204 for prop in ['offset', 'size', 'image-pos']:
205 if not prop in self._node.props:
206 state.AddZeroProp(self._node, prop)
207 if self.compress != 'none':
208 state.AddZeroProp(self._node, 'uncomp-size')
209 err = state.CheckAddHashProp(self._node)
213 def SetCalculatedProperties(self):
214 """Set the value of device-tree properties calculated by binman"""
215 state.SetInt(self._node, 'offset', self.offset)
216 state.SetInt(self._node, 'size', self.size)
217 base = self.section.GetRootSkipAtStart() if self.section else 0
218 state.SetInt(self._node, 'image-pos', self.image_pos - base)
219 if self.uncomp_size is not None:
220 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
221 state.CheckSetHashValue(self._node, self.GetData)
223 def ProcessFdt(self, fdt):
224 """Allow entries to adjust the device tree
226 Some entries need to adjust the device tree for their purposes. This
227 may involve adding or deleting properties.
230 True if processing is complete
231 False if processing could not be completed due to a dependency.
232 This will cause the entry to be retried after others have been
237 def SetPrefix(self, prefix):
238 """Set the name prefix for a node
241 prefix: Prefix to set, or '' to not use a prefix
244 self.name = prefix + self.name
246 def SetContents(self, data):
247 """Set the contents of an entry
249 This sets both the data and content_size properties
252 data: Data to set to the contents (bytes)
255 self.contents_size = len(self.data)
257 def ProcessContentsUpdate(self, data):
258 """Update the contents of an entry, after the size is fixed
260 This checks that the new data is the same size as the old. If the size
261 has changed, this triggers a re-run of the packing algorithm.
264 data: Data to set to the contents (bytes)
267 ValueError if the new data size is not the same as the old
271 if state.AllowEntryExpansion():
272 if new_size > self.contents_size:
273 tout.Debug("Entry '%s' size change from %#x to %#x" % (
274 self._node.path, self.contents_size, new_size))
275 # self.data will indicate the new size needed
277 elif new_size != self.contents_size:
278 self.Raise('Cannot update entry size from %d to %d' %
279 (self.contents_size, new_size))
280 self.SetContents(data)
283 def ObtainContents(self):
284 """Figure out the contents of an entry.
287 True if the contents were found, False if another call is needed
288 after the other entries are processed.
290 # No contents by default: subclasses can implement this
293 def ResetForPack(self):
294 """Reset offset/size fields so that packing can be done again"""
295 self.offset = self.orig_offset
296 self.size = self.orig_size
298 def Pack(self, offset):
299 """Figure out how to pack the entry into the section
301 Most of the time the entries are not fully specified. There may be
302 an alignment but no size. In that case we take the size from the
303 contents of the entry.
305 If an entry has no hard-coded offset, it will be placed at @offset.
307 Once this function is complete, both the offset and size of the
311 Current section offset pointer
314 New section offset pointer (after this entry)
316 if self.offset is None:
317 if self.offset_unset:
318 self.Raise('No offset set with offset-unset: should another '
319 'entry provide this correct offset?')
320 self.offset = tools.Align(offset, self.align)
321 needed = self.pad_before + self.contents_size + self.pad_after
322 needed = tools.Align(needed, self.align_size)
326 new_offset = self.offset + size
327 aligned_offset = tools.Align(new_offset, self.align_end)
328 if aligned_offset != new_offset:
329 size = aligned_offset - self.offset
330 new_offset = aligned_offset
335 if self.size < needed:
336 self.Raise("Entry contents size is %#x (%d) but entry size is "
337 "%#x (%d)" % (needed, needed, self.size, self.size))
338 # Check that the alignment is correct. It could be wrong if the
339 # and offset or size values were provided (i.e. not calculated), but
340 # conflict with the provided alignment values
341 if self.size != tools.Align(self.size, self.align_size):
342 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
343 (self.size, self.size, self.align_size, self.align_size))
344 if self.offset != tools.Align(self.offset, self.align):
345 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
346 (self.offset, self.offset, self.align, self.align))
350 def Raise(self, msg):
351 """Convenience function to raise an error referencing a node"""
352 raise ValueError("Node '%s': %s" % (self._node.path, msg))
354 def GetEntryArgsOrProps(self, props, required=False):
355 """Return the values of a set of properties
358 props: List of EntryArg objects
361 ValueError if a property is not found
366 python_prop = prop.name.replace('-', '_')
367 if hasattr(self, python_prop):
368 value = getattr(self, python_prop)
372 value = self.GetArg(prop.name, prop.datatype)
373 if value is None and required:
374 missing.append(prop.name)
377 self.Raise('Missing required properties/entry args: %s' %
378 (', '.join(missing)))
382 """Get the path of a node
385 Full path of the node for this entry
387 return self._node.path
392 def GetOffsets(self):
393 """Get the offsets for siblings
395 Some entry types can contain information about the position or size of
396 other entries. An example of this is the Intel Flash Descriptor, which
397 knows where the Intel Management Engine section should go.
399 If this entry knows about the position of other entries, it can specify
400 this by returning values here
405 value: List containing position and size of the given entry
406 type. Either can be None if not known
410 def SetOffsetSize(self, offset, size):
411 """Set the offset and/or size of an entry
414 offset: New offset, or None to leave alone
415 size: New size, or None to leave alone
417 if offset is not None:
422 def SetImagePos(self, image_pos):
423 """Set the position in the image
426 image_pos: Position of this entry in the image
428 self.image_pos = image_pos + self.offset
430 def ProcessContents(self):
431 """Do any post-packing updates of entry contents
433 This function should call ProcessContentsUpdate() to update the entry
434 contents, if necessary, returning its return value here.
437 data: Data to set to the contents (bytes)
440 True if the new data size is OK, False if expansion is needed
443 ValueError if the new data size is not the same as the old and
444 state.AllowEntryExpansion() is False
448 def WriteSymbols(self, section):
449 """Write symbol values into binary files for access at run time
452 section: Section containing the entry
456 def CheckOffset(self):
457 """Check that the entry offsets are correct
459 This is used for entries which have extra offset requirements (other
460 than having to be fully inside their section). Sub-classes can implement
461 this function and raise if there is a problem.
469 return '%08x' % value
472 def WriteMapLine(fd, indent, name, offset, size, image_pos):
473 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
474 Entry.GetStr(offset), Entry.GetStr(size),
477 def WriteMap(self, fd, indent):
478 """Write a map of the entry to a .map file
481 fd: File to write the map to
482 indent: Curent indent level of map (0=none, 1=one level, etc.)
484 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
487 def GetEntries(self):
488 """Return a list of entries contained by this entry
491 List of entries, or None if none. A normal entry has no entries
492 within it so will return None
496 def GetArg(self, name, datatype=str):
497 """Get the value of an entry argument or device-tree-node property
499 Some node properties can be provided as arguments to binman. First check
500 the entry arguments, and fall back to the device tree if not found
504 datatype: Data type (str or int)
507 Value of argument as a string or int, or None if no value
510 ValueError if the argument cannot be converted to in
512 value = state.GetEntryArg(name)
513 if value is not None:
518 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
520 elif datatype == str:
523 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
526 value = fdt_util.GetDatatype(self._node, name, datatype)
530 def WriteDocs(modules, test_missing=None):
531 """Write out documentation about the various entry types to stdout
534 modules: List of modules to include
535 test_missing: Used for testing. This is a module to report
538 print('''Binman Entry Documentation
539 ===========================
541 This file describes the entry types supported by binman. These entry types can
542 be placed in an image one by one to build up a final firmware image. It is
543 fairly easy to create new entry types. Just add a new file to the 'etype'
544 directory. You can use the existing entries as examples.
546 Note that some entries are subclasses of others, using and extending their
547 features to produce new behaviours.
551 modules = sorted(modules)
553 # Don't show the test entry
554 if '_testing' in modules:
555 modules.remove('_testing')
558 if name.startswith('__'):
560 module = Entry.Lookup(name, name)
561 docs = getattr(module, '__doc__')
562 if test_missing == name:
565 lines = docs.splitlines()
566 first_line = lines[0]
567 rest = [line[4:] for line in lines[1:]]
568 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
570 print('-' * len(hdr))
571 print('\n'.join(rest))
578 raise ValueError('Documentation is missing for modules: %s' %
581 def GetUniqueName(self):
582 """Get a unique name for a node
585 String containing a unique name for a node, consisting of the name
586 of all ancestors (starting from within the 'binman' node) separated
587 by a dot ('.'). This can be useful for generating unique filesnames
588 in the output directory.
594 if node.name == 'binman':
596 name = '%s.%s' % (node.name, name)
599 def ExpandToLimit(self, limit):
600 """Expand an entry so that it ends at the given offset limit"""
601 if self.offset + self.size < limit:
602 self.size = limit - self.offset
603 # Request the contents again, since changing the size requires that
604 # the data grows. This should not fail, but check it to be sure.
605 if not self.ObtainContents():
606 self.Raise('Cannot obtain contents when expanding entry')
608 def HasSibling(self, name):
609 """Check if there is a sibling of a given name
612 True if there is an entry with this name in the the same section,
615 return name in self.section.GetEntries()
617 def GetSiblingImagePos(self, name):
618 """Return the image position of the given sibling
621 Image position of sibling, or None if the sibling has no position,
622 or False if there is no such sibling
624 if not self.HasSibling(name):
626 return self.section.GetEntries()[name].image_pos
629 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
630 uncomp_size, offset, entry):
631 """Add a new entry to the entries list
634 entries: List (of EntryInfo objects) to add to
635 indent: Current indent level to add to list
636 name: Entry name (string)
637 etype: Entry type (string)
638 size: Entry size in bytes (int)
639 image_pos: Position within image in bytes (int)
640 uncomp_size: Uncompressed size if the entry uses compression, else
642 offset: Entry offset within parent in bytes (int)
645 entries.append(EntryInfo(indent, name, etype, size, image_pos,
646 uncomp_size, offset, entry))
648 def ListEntries(self, entries, indent):
649 """Add files in this entry to the list of entries
651 This can be overridden by subclasses which need different behaviour.
654 entries: List (of EntryInfo objects) to add to
655 indent: Current indent level to add to list
657 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
658 self.image_pos, self.uncomp_size, self.offset, self)
660 def ReadData(self, decomp=True):
661 """Read the data for an entry from the image
663 This is used when the image has been read in and we want to extract the
664 data for a particular entry from that image.
667 decomp: True to decompress any compressed data before returning it;
668 False to return the raw, uncompressed data
673 # Use True here so that we get an uncompressed section to work from,
674 # although compressed sections are currently not supported
675 data = self.section.ReadData(True)
676 tout.Info('%s: Reading data from offset %#x-%#x, size %#x (avail %#x)' %
677 (self.GetPath(), self.offset, self.offset + self.size,
678 self.size, len(data)))
679 return data[self.offset:self.offset + self.size]