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'])
39 """An Entry in the section
41 An entry corresponds to a single node in the device-tree description
42 of the section. Each entry ends up being a part of the final section.
43 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.
46 This class is not used by itself. All entry objects are subclasses of
50 section: Section object containing this entry
51 node: The node that created this entry
52 offset: Offset of entry within the section, None if not known yet (in
53 which case it will be calculated by Pack())
54 size: Entry size in bytes, None if not known
55 contents_size: Size of contents in bytes, 0 by default
56 align: Entry start offset alignment, or None
57 align_size: Entry size alignment, or None
58 align_end: Entry end offset alignment, or None
59 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)
63 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
64 self.section = section
67 self.name = node and (name_prefix + node.name) or 'none'
71 self.contents_size = 0
73 self.align_size = None
77 self.offset_unset = False
83 def Lookup(section, node_path, etype):
84 """Look up the entry class for a node.
87 section: Section object containing this node
88 node_node: Path name of Node object containing information about
89 the entry to create (used for errors)
90 etype: Entry type to use
93 The entry class object if found, else None
95 # Convert something like 'u-boot@0' to 'u_boot' since we are only
96 # interested in the type.
97 module_name = etype.replace('-', '_')
98 if '@' in module_name:
99 module_name = module_name.split('@')[0]
100 module = modules.get(module_name)
102 # Also allow entry-type modules to be brought in from the etype directory.
104 # Import the module if we have not already done so.
107 sys.path.insert(0, os.path.join(our_path, 'etype'))
110 module = importlib.import_module(module_name)
112 module = __import__(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))
118 modules[module_name] = module
120 # Look up the expected class name
121 return getattr(module, 'Entry_%s' % module_name)
124 def Create(section, node, etype=None):
125 """Create a new entry for a node.
128 section: Section object containing this node
129 node: Node object containing information about the entry to
131 etype: Entry type to use, or None to work it out (used for tests)
134 A new Entry object of the correct type (a subclass of Entry)
137 etype = fdt_util.GetString(node, 'type', node.name)
138 obj = Entry.Lookup(section, node.path, etype)
140 # Call its constructor to get the object we want.
141 return obj(section, etype, node)
144 """Read entry information from the node
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.align = fdt_util.GetInt(self._node, 'align')
153 if tools.NotPowerOfTwo(self.align):
154 raise ValueError("Node '%s': Alignment %s must be a power of two" %
155 (self._node.path, self.align))
156 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
157 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
158 self.align_size = fdt_util.GetInt(self._node, 'align-size')
159 if tools.NotPowerOfTwo(self.align_size):
160 raise ValueError("Node '%s': Alignment size %s must be a power "
161 "of two" % (self._node.path, self.align_size))
162 self.align_end = fdt_util.GetInt(self._node, 'align-end')
163 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
165 def GetDefaultFilename(self):
169 """Get the set of device trees used by this entry
172 Set containing the filename from this entry, if it is a .dtb, else
175 fname = self.GetDefaultFilename()
176 # It would be better to use isinstance(self, Entry_blob_dtb) here but
177 # we cannot access Entry_blob_dtb
178 if fname and fname.endswith('.dtb'):
182 def AddMissingProperties(self):
183 """Add new properties to the device tree as needed for this entry"""
184 for prop in ['offset', 'size', 'image-pos']:
185 if not prop in self._node.props:
186 state.AddZeroProp(self._node, prop)
188 def SetCalculatedProperties(self):
189 """Set the value of device-tree properties calculated by binman"""
190 state.SetInt(self._node, 'offset', self.offset)
191 state.SetInt(self._node, 'size', self.size)
192 state.SetInt(self._node, 'image-pos', self.image_pos)
194 def ProcessFdt(self, fdt):
195 """Allow entries to adjust the device tree
197 Some entries need to adjust the device tree for their purposes. This
198 may involve adding or deleting properties.
201 True if processing is complete
202 False if processing could not be completed due to a dependency.
203 This will cause the entry to be retried after others have been
208 def SetPrefix(self, prefix):
209 """Set the name prefix for a node
212 prefix: Prefix to set, or '' to not use a prefix
215 self.name = prefix + self.name
217 def SetContents(self, data):
218 """Set the contents of an entry
220 This sets both the data and content_size properties
223 data: Data to set to the contents (string)
226 self.contents_size = len(self.data)
228 def ProcessContentsUpdate(self, data):
229 """Update the contens of an entry, after the size is fixed
231 This checks that the new data is the same size as the old.
234 data: Data to set to the contents (string)
237 ValueError if the new data size is not the same as the old
239 if len(data) != self.contents_size:
240 self.Raise('Cannot update entry size from %d to %d' %
241 (len(data), self.contents_size))
242 self.SetContents(data)
244 def ObtainContents(self):
245 """Figure out the contents of an entry.
248 True if the contents were found, False if another call is needed
249 after the other entries are processed.
251 # No contents by default: subclasses can implement this
254 def Pack(self, offset):
255 """Figure out how to pack the entry into the section
257 Most of the time the entries are not fully specified. There may be
258 an alignment but no size. In that case we take the size from the
259 contents of the entry.
261 If an entry has no hard-coded offset, it will be placed at @offset.
263 Once this function is complete, both the offset and size of the
267 Current section offset pointer
270 New section offset pointer (after this entry)
272 if self.offset is None:
273 if self.offset_unset:
274 self.Raise('No offset set with offset-unset: should another '
275 'entry provide this correct offset?')
276 self.offset = tools.Align(offset, self.align)
277 needed = self.pad_before + self.contents_size + self.pad_after
278 needed = tools.Align(needed, self.align_size)
282 new_offset = self.offset + size
283 aligned_offset = tools.Align(new_offset, self.align_end)
284 if aligned_offset != new_offset:
285 size = aligned_offset - self.offset
286 new_offset = aligned_offset
291 if self.size < needed:
292 self.Raise("Entry contents size is %#x (%d) but entry size is "
293 "%#x (%d)" % (needed, needed, self.size, self.size))
294 # Check that the alignment is correct. It could be wrong if the
295 # and offset or size values were provided (i.e. not calculated), but
296 # conflict with the provided alignment values
297 if self.size != tools.Align(self.size, self.align_size):
298 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
299 (self.size, self.size, self.align_size, self.align_size))
300 if self.offset != tools.Align(self.offset, self.align):
301 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
302 (self.offset, self.offset, self.align, self.align))
306 def Raise(self, msg):
307 """Convenience function to raise an error referencing a node"""
308 raise ValueError("Node '%s': %s" % (self._node.path, msg))
310 def GetEntryArgsOrProps(self, props, required=False):
311 """Return the values of a set of properties
314 props: List of EntryArg objects
317 ValueError if a property is not found
322 python_prop = prop.name.replace('-', '_')
323 if hasattr(self, python_prop):
324 value = getattr(self, python_prop)
328 value = self.GetArg(prop.name, prop.datatype)
329 if value is None and required:
330 missing.append(prop.name)
333 self.Raise('Missing required properties/entry args: %s' %
334 (', '.join(missing)))
338 """Get the path of a node
341 Full path of the node for this entry
343 return self._node.path
348 def GetOffsets(self):
351 def SetOffsetSize(self, pos, size):
355 def SetImagePos(self, image_pos):
356 """Set the position in the image
359 image_pos: Position of this entry in the image
361 self.image_pos = image_pos + self.offset
363 def ProcessContents(self):
366 def WriteSymbols(self, section):
367 """Write symbol values into binary files for access at run time
370 section: Section containing the entry
374 def CheckOffset(self):
375 """Check that the entry offsets are correct
377 This is used for entries which have extra offset requirements (other
378 than having to be fully inside their section). Sub-classes can implement
379 this function and raise if there is a problem.
384 def WriteMapLine(fd, indent, name, offset, size, image_pos):
385 print('%08x %s%08x %08x %s' % (image_pos, ' ' * indent, offset,
386 size, name), file=fd)
388 def WriteMap(self, fd, indent):
389 """Write a map of the entry to a .map file
392 fd: File to write the map to
393 indent: Curent indent level of map (0=none, 1=one level, etc.)
395 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
398 def GetEntries(self):
399 """Return a list of entries contained by this entry
402 List of entries, or None if none. A normal entry has no entries
403 within it so will return None
407 def GetArg(self, name, datatype=str):
408 """Get the value of an entry argument or device-tree-node property
410 Some node properties can be provided as arguments to binman. First check
411 the entry arguments, and fall back to the device tree if not found
415 datatype: Data type (str or int)
418 Value of argument as a string or int, or None if no value
421 ValueError if the argument cannot be converted to in
423 value = state.GetEntryArg(name)
424 if value is not None:
429 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
431 elif datatype == str:
434 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
437 value = fdt_util.GetDatatype(self._node, name, datatype)
441 def WriteDocs(modules, test_missing=None):
442 """Write out documentation about the various entry types to stdout
445 modules: List of modules to include
446 test_missing: Used for testing. This is a module to report
449 print('''Binman Entry Documentation
450 ===========================
452 This file describes the entry types supported by binman. These entry types can
453 be placed in an image one by one to build up a final firmware image. It is
454 fairly easy to create new entry types. Just add a new file to the 'etype'
455 directory. You can use the existing entries as examples.
457 Note that some entries are subclasses of others, using and extending their
458 features to produce new behaviours.
462 modules = sorted(modules)
464 # Don't show the test entry
465 if '_testing' in modules:
466 modules.remove('_testing')
469 module = Entry.Lookup(name, name, name)
470 docs = getattr(module, '__doc__')
471 if test_missing == name:
474 lines = docs.splitlines()
475 first_line = lines[0]
476 rest = [line[4:] for line in lines[1:]]
477 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
479 print('-' * len(hdr))
480 print('\n'.join(rest))
487 raise ValueError('Documentation is missing for modules: %s' %
490 def GetUniqueName(self):
491 """Get a unique name for a node
494 String containing a unique name for a node, consisting of the name
495 of all ancestors (starting from within the 'binman' node) separated
496 by a dot ('.'). This can be useful for generating unique filesnames
497 in the output directory.
503 if node.name == 'binman':
505 name = '%s.%s' % (node.name, name)