1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Base class for all entries
7 from collections import namedtuple
12 from dtoc import fdt_util
13 from patman import tools
14 from patman.tools import ToHex, ToHexSize
15 from patman import tout
19 our_path = os.path.dirname(os.path.realpath(__file__))
22 # An argument which can be passed to entries on the command line, in lieu of
23 # device-tree properties.
24 EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
26 # Information about an entry for use when displaying summaries
27 EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
28 'image_pos', 'uncomp_size', 'offset',
32 """An Entry in the section
34 An entry corresponds to a single node in the device-tree description
35 of the section. Each entry ends up being a part of the final section.
36 Entries can be placed either right next to each other, or with padding
37 between them. The type of the entry determines the data that is in it.
39 This class is not used by itself. All entry objects are subclasses of
43 section: Section object containing this entry
44 node: The node that created this entry
45 offset: Offset of entry within the section, None if not known yet (in
46 which case it will be calculated by Pack())
47 size: Entry size in bytes, None if not known
48 pre_reset_size: size as it was before ResetForPack(). This allows us to
49 keep track of the size we started with and detect size changes
50 uncomp_size: Size of uncompressed data in bytes, if the entry is
52 contents_size: Size of contents in bytes, 0 by default
53 align: Entry start offset alignment, or None
54 align_size: Entry size alignment, or None
55 align_end: Entry end offset alignment, or None
56 pad_before: Number of pad bytes before the contents, 0 if none
57 pad_after: Number of pad bytes after the contents, 0 if none
58 data: Contents of entry (string of bytes)
59 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
60 orig_offset: Original offset value read from node
61 orig_size: Original size value read from node
63 def __init__(self, section, etype, node, name_prefix=''):
64 # Put this here to allow entry-docs and help to work without libfdt
66 from binman import state
68 self.section = section
71 self.name = node and (name_prefix + node.name) or 'none'
74 self.pre_reset_size = None
75 self.uncomp_size = None
77 self.contents_size = 0
79 self.align_size = None
83 self.offset_unset = False
85 self._expand_size = False
86 self.compress = 'none'
89 def Lookup(node_path, etype):
90 """Look up the entry class for a node.
93 node_node: Path name of Node object containing information about
94 the entry to create (used for errors)
95 etype: Entry type to use
98 The entry class object if found, else None
100 # Convert something like 'u-boot@0' to 'u_boot' since we are only
101 # interested in the type.
102 module_name = etype.replace('-', '_')
103 if '@' in module_name:
104 module_name = module_name.split('@')[0]
105 module = modules.get(module_name)
107 # Also allow entry-type modules to be brought in from the etype directory.
109 # Import the module if we have not already done so.
112 module = importlib.import_module('binman.etype.' + module_name)
113 except ImportError as e:
114 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
115 (etype, node_path, module_name, e))
116 modules[module_name] = module
118 # Look up the expected class name
119 return getattr(module, 'Entry_%s' % module_name)
122 def Create(section, node, etype=None):
123 """Create a new entry for a node.
126 section: Section object containing this node
127 node: Node object containing information about the entry to
129 etype: Entry type to use, or None to work it out (used for tests)
132 A new Entry object of the correct type (a subclass of Entry)
135 etype = fdt_util.GetString(node, 'type', node.name)
136 obj = Entry.Lookup(node.path, etype)
138 # Call its constructor to get the object we want.
139 return obj(section, etype, node)
142 """Read entry information from the node
144 This must be called as the first thing after the Entry is created.
146 This reads all the fields we recognise from the node, ready for use.
148 if 'pos' in self._node.props:
149 self.Raise("Please use 'offset' instead of 'pos'")
150 self.offset = fdt_util.GetInt(self._node, 'offset')
151 self.size = fdt_util.GetInt(self._node, 'size')
152 self.orig_offset = fdt_util.GetInt(self._node, 'orig-offset')
153 self.orig_size = fdt_util.GetInt(self._node, 'orig-size')
154 if self.GetImage().copy_to_orig:
155 self.orig_offset = self.offset
156 self.orig_size = self.size
158 # These should not be set in input files, but are set in an FDT map,
159 # which is also read by this code.
160 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
161 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
163 self.align = fdt_util.GetInt(self._node, 'align')
164 if tools.NotPowerOfTwo(self.align):
165 raise ValueError("Node '%s': Alignment %s must be a power of two" %
166 (self._node.path, self.align))
167 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
168 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
169 self.align_size = fdt_util.GetInt(self._node, 'align-size')
170 if tools.NotPowerOfTwo(self.align_size):
171 self.Raise("Alignment size %s must be a power of two" %
173 self.align_end = fdt_util.GetInt(self._node, 'align-end')
174 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
175 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
177 def GetDefaultFilename(self):
181 """Get the device trees used by this entry
184 Empty dict, if this entry is not a .dtb, otherwise:
186 key: Filename from this entry (without the path)
188 Fdt object for this dtb, or None if not available
189 Filename of file containing this dtb
193 def ExpandEntries(self):
196 def AddMissingProperties(self):
197 """Add new properties to the device tree as needed for this entry"""
198 for prop in ['offset', 'size', 'image-pos']:
199 if not prop in self._node.props:
200 state.AddZeroProp(self._node, prop)
201 if self.GetImage().allow_repack:
202 if self.orig_offset is not None:
203 state.AddZeroProp(self._node, 'orig-offset', True)
204 if self.orig_size is not None:
205 state.AddZeroProp(self._node, 'orig-size', True)
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.GetImage().allow_repack:
220 if self.orig_offset is not None:
221 state.SetInt(self._node, 'orig-offset', self.orig_offset, True)
222 if self.orig_size is not None:
223 state.SetInt(self._node, 'orig-size', self.orig_size, True)
224 if self.uncomp_size is not None:
225 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
226 state.CheckSetHashValue(self._node, self.GetData)
228 def ProcessFdt(self, fdt):
229 """Allow entries to adjust the device tree
231 Some entries need to adjust the device tree for their purposes. This
232 may involve adding or deleting properties.
235 True if processing is complete
236 False if processing could not be completed due to a dependency.
237 This will cause the entry to be retried after others have been
242 def SetPrefix(self, prefix):
243 """Set the name prefix for a node
246 prefix: Prefix to set, or '' to not use a prefix
249 self.name = prefix + self.name
251 def SetContents(self, data):
252 """Set the contents of an entry
254 This sets both the data and content_size properties
257 data: Data to set to the contents (bytes)
260 self.contents_size = len(self.data)
262 def ProcessContentsUpdate(self, data):
263 """Update the contents of an entry, after the size is fixed
265 This checks that the new data is the same size as the old. If the size
266 has changed, this triggers a re-run of the packing algorithm.
269 data: Data to set to the contents (bytes)
272 ValueError if the new data size is not the same as the old
276 if state.AllowEntryExpansion() and new_size > self.contents_size:
277 # self.data will indicate the new size needed
279 elif state.AllowEntryContraction() and new_size < self.contents_size:
282 # If not allowed to change, try to deal with it or give up
284 if new_size > self.contents_size:
285 self.Raise('Cannot update entry size from %d to %d' %
286 (self.contents_size, new_size))
288 # Don't let the data shrink. Pad it if necessary
289 if size_ok and new_size < self.contents_size:
290 data += tools.GetBytes(0, self.contents_size - new_size)
293 tout.Debug("Entry '%s' size change from %s to %s" % (
294 self._node.path, ToHex(self.contents_size),
296 self.SetContents(data)
299 def ObtainContents(self):
300 """Figure out the contents of an entry.
303 True if the contents were found, False if another call is needed
304 after the other entries are processed.
306 # No contents by default: subclasses can implement this
309 def ResetForPack(self):
310 """Reset offset/size fields so that packing can be done again"""
311 self.Detail('ResetForPack: offset %s->%s, size %s->%s' %
312 (ToHex(self.offset), ToHex(self.orig_offset),
313 ToHex(self.size), ToHex(self.orig_size)))
314 self.pre_reset_size = self.size
315 self.offset = self.orig_offset
316 self.size = self.orig_size
318 def Pack(self, offset):
319 """Figure out how to pack the entry into the section
321 Most of the time the entries are not fully specified. There may be
322 an alignment but no size. In that case we take the size from the
323 contents of the entry.
325 If an entry has no hard-coded offset, it will be placed at @offset.
327 Once this function is complete, both the offset and size of the
331 Current section offset pointer
334 New section offset pointer (after this entry)
336 self.Detail('Packing: offset=%s, size=%s, content_size=%x' %
337 (ToHex(self.offset), ToHex(self.size),
339 if self.offset is None:
340 if self.offset_unset:
341 self.Raise('No offset set with offset-unset: should another '
342 'entry provide this correct offset?')
343 self.offset = tools.Align(offset, self.align)
344 needed = self.pad_before + self.contents_size + self.pad_after
345 needed = tools.Align(needed, self.align_size)
349 new_offset = self.offset + size
350 aligned_offset = tools.Align(new_offset, self.align_end)
351 if aligned_offset != new_offset:
352 size = aligned_offset - self.offset
353 new_offset = aligned_offset
358 if self.size < needed:
359 self.Raise("Entry contents size is %#x (%d) but entry size is "
360 "%#x (%d)" % (needed, needed, self.size, self.size))
361 # Check that the alignment is correct. It could be wrong if the
362 # and offset or size values were provided (i.e. not calculated), but
363 # conflict with the provided alignment values
364 if self.size != tools.Align(self.size, self.align_size):
365 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
366 (self.size, self.size, self.align_size, self.align_size))
367 if self.offset != tools.Align(self.offset, self.align):
368 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
369 (self.offset, self.offset, self.align, self.align))
370 self.Detail(' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' %
371 (self.offset, self.size, self.contents_size, new_offset))
375 def Raise(self, msg):
376 """Convenience function to raise an error referencing a node"""
377 raise ValueError("Node '%s': %s" % (self._node.path, msg))
379 def Detail(self, msg):
380 """Convenience function to log detail referencing a node"""
381 tag = "Node '%s'" % self._node.path
382 tout.Detail('%30s: %s' % (tag, msg))
384 def GetEntryArgsOrProps(self, props, required=False):
385 """Return the values of a set of properties
388 props: List of EntryArg objects
391 ValueError if a property is not found
396 python_prop = prop.name.replace('-', '_')
397 if hasattr(self, python_prop):
398 value = getattr(self, python_prop)
402 value = self.GetArg(prop.name, prop.datatype)
403 if value is None and required:
404 missing.append(prop.name)
407 self.Raise('Missing required properties/entry args: %s' %
408 (', '.join(missing)))
412 """Get the path of a node
415 Full path of the node for this entry
417 return self._node.path
420 self.Detail('GetData: size %s' % ToHexSize(self.data))
423 def GetOffsets(self):
424 """Get the offsets for siblings
426 Some entry types can contain information about the position or size of
427 other entries. An example of this is the Intel Flash Descriptor, which
428 knows where the Intel Management Engine section should go.
430 If this entry knows about the position of other entries, it can specify
431 this by returning values here
436 value: List containing position and size of the given entry
437 type. Either can be None if not known
441 def SetOffsetSize(self, offset, size):
442 """Set the offset and/or size of an entry
445 offset: New offset, or None to leave alone
446 size: New size, or None to leave alone
448 if offset is not None:
453 def SetImagePos(self, image_pos):
454 """Set the position in the image
457 image_pos: Position of this entry in the image
459 self.image_pos = image_pos + self.offset
461 def ProcessContents(self):
462 """Do any post-packing updates of entry contents
464 This function should call ProcessContentsUpdate() to update the entry
465 contents, if necessary, returning its return value here.
468 data: Data to set to the contents (bytes)
471 True if the new data size is OK, False if expansion is needed
474 ValueError if the new data size is not the same as the old and
475 state.AllowEntryExpansion() is False
479 def WriteSymbols(self, section):
480 """Write symbol values into binary files for access at run time
483 section: Section containing the entry
487 def CheckOffset(self):
488 """Check that the entry offsets are correct
490 This is used for entries which have extra offset requirements (other
491 than having to be fully inside their section). Sub-classes can implement
492 this function and raise if there is a problem.
500 return '%08x' % value
503 def WriteMapLine(fd, indent, name, offset, size, image_pos):
504 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
505 Entry.GetStr(offset), Entry.GetStr(size),
508 def WriteMap(self, fd, indent):
509 """Write a map of the entry to a .map file
512 fd: File to write the map to
513 indent: Curent indent level of map (0=none, 1=one level, etc.)
515 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
518 def GetEntries(self):
519 """Return a list of entries contained by this entry
522 List of entries, or None if none. A normal entry has no entries
523 within it so will return None
527 def GetArg(self, name, datatype=str):
528 """Get the value of an entry argument or device-tree-node property
530 Some node properties can be provided as arguments to binman. First check
531 the entry arguments, and fall back to the device tree if not found
535 datatype: Data type (str or int)
538 Value of argument as a string or int, or None if no value
541 ValueError if the argument cannot be converted to in
543 value = state.GetEntryArg(name)
544 if value is not None:
549 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
551 elif datatype == str:
554 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
557 value = fdt_util.GetDatatype(self._node, name, datatype)
561 def WriteDocs(modules, test_missing=None):
562 """Write out documentation about the various entry types to stdout
565 modules: List of modules to include
566 test_missing: Used for testing. This is a module to report
569 print('''Binman Entry Documentation
570 ===========================
572 This file describes the entry types supported by binman. These entry types can
573 be placed in an image one by one to build up a final firmware image. It is
574 fairly easy to create new entry types. Just add a new file to the 'etype'
575 directory. You can use the existing entries as examples.
577 Note that some entries are subclasses of others, using and extending their
578 features to produce new behaviours.
582 modules = sorted(modules)
584 # Don't show the test entry
585 if '_testing' in modules:
586 modules.remove('_testing')
589 module = Entry.Lookup('WriteDocs', name)
590 docs = getattr(module, '__doc__')
591 if test_missing == name:
594 lines = docs.splitlines()
595 first_line = lines[0]
596 rest = [line[4:] for line in lines[1:]]
597 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
599 print('-' * len(hdr))
600 print('\n'.join(rest))
607 raise ValueError('Documentation is missing for modules: %s' %
610 def GetUniqueName(self):
611 """Get a unique name for a node
614 String containing a unique name for a node, consisting of the name
615 of all ancestors (starting from within the 'binman' node) separated
616 by a dot ('.'). This can be useful for generating unique filesnames
617 in the output directory.
623 if node.name == 'binman':
625 name = '%s.%s' % (node.name, name)
628 def ExpandToLimit(self, limit):
629 """Expand an entry so that it ends at the given offset limit"""
630 if self.offset + self.size < limit:
631 self.size = limit - self.offset
632 # Request the contents again, since changing the size requires that
633 # the data grows. This should not fail, but check it to be sure.
634 if not self.ObtainContents():
635 self.Raise('Cannot obtain contents when expanding entry')
637 def HasSibling(self, name):
638 """Check if there is a sibling of a given name
641 True if there is an entry with this name in the the same section,
644 return name in self.section.GetEntries()
646 def GetSiblingImagePos(self, name):
647 """Return the image position of the given sibling
650 Image position of sibling, or None if the sibling has no position,
651 or False if there is no such sibling
653 if not self.HasSibling(name):
655 return self.section.GetEntries()[name].image_pos
658 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
659 uncomp_size, offset, entry):
660 """Add a new entry to the entries list
663 entries: List (of EntryInfo objects) to add to
664 indent: Current indent level to add to list
665 name: Entry name (string)
666 etype: Entry type (string)
667 size: Entry size in bytes (int)
668 image_pos: Position within image in bytes (int)
669 uncomp_size: Uncompressed size if the entry uses compression, else
671 offset: Entry offset within parent in bytes (int)
674 entries.append(EntryInfo(indent, name, etype, size, image_pos,
675 uncomp_size, offset, entry))
677 def ListEntries(self, entries, indent):
678 """Add files in this entry to the list of entries
680 This can be overridden by subclasses which need different behaviour.
683 entries: List (of EntryInfo objects) to add to
684 indent: Current indent level to add to list
686 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
687 self.image_pos, self.uncomp_size, self.offset, self)
689 def ReadData(self, decomp=True):
690 """Read the data for an entry from the image
692 This is used when the image has been read in and we want to extract the
693 data for a particular entry from that image.
696 decomp: True to decompress any compressed data before returning it;
697 False to return the raw, uncompressed data
702 # Use True here so that we get an uncompressed section to work from,
703 # although compressed sections are currently not supported
704 tout.Debug("ReadChildData section '%s', entry '%s'" %
705 (self.section.GetPath(), self.GetPath()))
706 data = self.section.ReadChildData(self, decomp)
709 def ReadChildData(self, child, decomp=True):
710 """Read the data for a particular child entry
712 This reads data from the parent and extracts the piece that relates to
716 child: Child entry to read data for (must be valid)
717 decomp: True to decompress any compressed data before returning it;
718 False to return the raw, uncompressed data
721 Data for the child (bytes)
725 def LoadData(self, decomp=True):
726 data = self.ReadData(decomp)
727 self.contents_size = len(data)
728 self.ProcessContentsUpdate(data)
729 self.Detail('Loaded data size %x' % len(data))
732 """Get the image containing this entry
735 Image object containing this entry
737 return self.section.GetImage()
739 def WriteData(self, data, decomp=True):
740 """Write the data to an entry in the image
742 This is used when the image has been read in and we want to replace the
743 data for a particular entry in that image.
745 The image must be re-packed and written out afterwards.
748 data: Data to replace it with
749 decomp: True to compress the data if needed, False if data is
750 already compressed so should be used as is
753 True if the data did not result in a resize of this entry, False if
754 the entry must be resized
756 if self.size is not None:
757 self.contents_size = self.size
759 self.contents_size = self.pre_reset_size
760 ok = self.ProcessContentsUpdate(data)
761 self.Detail('WriteData: size=%x, ok=%s' % (len(data), ok))
762 section_ok = self.section.WriteChildData(self)
763 return ok and section_ok
765 def WriteChildData(self, child):
766 """Handle writing the data in a child entry
768 This should be called on the child's parent section after the child's
769 data has been updated. It
771 This base-class implementation does nothing, since the base Entry object
772 does not have any children.
775 child: Child Entry that was written
778 True if the section could be updated successfully, False if the
779 data is such that the section could not updat
783 def GetSiblingOrder(self):
784 """Get the relative order of an entry amoung its siblings
787 'start' if this entry is first among siblings, 'end' if last,
790 entries = list(self.section.GetEntries().values())
792 if self == entries[0]:
794 elif self == entries[-1]: