blob: 7db1402b84fb2e24e94322d29b18bc253bbaad71 [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
21import sys
Simon Glassc55a50f2018-09-14 04:57:19 -060022
23import fdt_util
24import state
Simon Glassbf7fd502016-11-25 20:15:51 -070025import tools
26
27modules = {}
28
Simon Glassbadf0ec2018-06-01 09:38:15 -060029our_path = os.path.dirname(os.path.realpath(__file__))
30
Simon Glass53af22a2018-07-17 13:25:32 -060031
32# An argument which can be passed to entries on the command line, in lieu of
33# device-tree properties.
34EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
35
36
Simon Glassbf7fd502016-11-25 20:15:51 -070037class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060038 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070039
40 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060041 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070042 Entries can be placed either right next to each other, or with padding
43 between them. The type of the entry determines the data that is in it.
44
45 This class is not used by itself. All entry objects are subclasses of
46 Entry.
47
48 Attributes:
Simon Glass8122f392018-07-17 13:25:28 -060049 section: Section object containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070050 node: The node that created this entry
Simon Glass3ab95982018-08-01 15:22:37 -060051 offset: Offset of entry within the section, None if not known yet (in
52 which case it will be calculated by Pack())
Simon Glassbf7fd502016-11-25 20:15:51 -070053 size: Entry size in bytes, None if not known
Simon Glass8287ee82019-07-08 14:25:30 -060054 uncomp_size: Size of uncompressed data in bytes, if the entry is
55 compressed, else None
Simon Glassbf7fd502016-11-25 20:15:51 -070056 contents_size: Size of contents in bytes, 0 by default
Simon Glass3ab95982018-08-01 15:22:37 -060057 align: Entry start offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070058 align_size: Entry size alignment, or None
Simon Glass3ab95982018-08-01 15:22:37 -060059 align_end: Entry end offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070060 pad_before: Number of pad bytes before the contents, 0 if none
61 pad_after: Number of pad bytes after the contents, 0 if none
62 data: Contents of entry (string of bytes)
Simon Glass8287ee82019-07-08 14:25:30 -060063 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
Simon Glassbf7fd502016-11-25 20:15:51 -070064 """
Simon Glassc8d48ef2018-06-01 09:38:21 -060065 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glass25ac0e62018-06-01 09:38:14 -060066 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070067 self.etype = etype
68 self._node = node
Simon Glassc8d48ef2018-06-01 09:38:21 -060069 self.name = node and (name_prefix + node.name) or 'none'
Simon Glass3ab95982018-08-01 15:22:37 -060070 self.offset = None
Simon Glassbf7fd502016-11-25 20:15:51 -070071 self.size = None
Simon Glass8287ee82019-07-08 14:25:30 -060072 self.uncomp_size = None
Simon Glass24d0d3c2018-07-17 13:25:47 -060073 self.data = None
Simon Glassbf7fd502016-11-25 20:15:51 -070074 self.contents_size = 0
75 self.align = None
76 self.align_size = None
77 self.align_end = None
78 self.pad_before = 0
79 self.pad_after = 0
Simon Glass3ab95982018-08-01 15:22:37 -060080 self.offset_unset = False
Simon Glassdbf6be92018-08-01 15:22:42 -060081 self.image_pos = None
Simon Glassba64a0b2018-09-14 04:57:29 -060082 self._expand_size = False
Simon Glass8287ee82019-07-08 14:25:30 -060083 self.compress = 'none'
Simon Glassbf7fd502016-11-25 20:15:51 -070084 if read_node:
85 self.ReadNode()
86
87 @staticmethod
Simon Glassc073ced2019-07-08 14:25:31 -060088 def Lookup(node_path, etype):
Simon Glassfd8d1f72018-07-17 13:25:36 -060089 """Look up the entry class for a node.
Simon Glassbf7fd502016-11-25 20:15:51 -070090
91 Args:
Simon Glassfd8d1f72018-07-17 13:25:36 -060092 node_node: Path name of Node object containing information about
93 the entry to create (used for errors)
94 etype: Entry type to use
Simon Glassbf7fd502016-11-25 20:15:51 -070095
96 Returns:
Simon Glassfd8d1f72018-07-17 13:25:36 -060097 The entry class object if found, else None
Simon Glassbf7fd502016-11-25 20:15:51 -070098 """
Simon Glassdd57c132018-06-01 09:38:11 -060099 # Convert something like 'u-boot@0' to 'u_boot' since we are only
100 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -0700101 module_name = etype.replace('-', '_')
Simon Glassdd57c132018-06-01 09:38:11 -0600102 if '@' in module_name:
103 module_name = module_name.split('@')[0]
Simon Glassbf7fd502016-11-25 20:15:51 -0700104 module = modules.get(module_name)
105
Simon Glassbadf0ec2018-06-01 09:38:15 -0600106 # Also allow entry-type modules to be brought in from the etype directory.
107
Simon Glassbf7fd502016-11-25 20:15:51 -0700108 # Import the module if we have not already done so.
109 if not module:
Simon Glassbadf0ec2018-06-01 09:38:15 -0600110 old_path = sys.path
111 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -0700112 try:
113 if have_importlib:
114 module = importlib.import_module(module_name)
115 else:
116 module = __import__(module_name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600117 except ImportError as e:
118 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
119 (etype, node_path, module_name, e))
Simon Glassbadf0ec2018-06-01 09:38:15 -0600120 finally:
121 sys.path = old_path
Simon Glassbf7fd502016-11-25 20:15:51 -0700122 modules[module_name] = module
123
Simon Glassfd8d1f72018-07-17 13:25:36 -0600124 # Look up the expected class name
125 return getattr(module, 'Entry_%s' % module_name)
126
127 @staticmethod
128 def Create(section, node, etype=None):
129 """Create a new entry for a node.
130
131 Args:
132 section: Section object containing this node
133 node: Node object containing information about the entry to
134 create
135 etype: Entry type to use, or None to work it out (used for tests)
136
137 Returns:
138 A new Entry object of the correct type (a subclass of Entry)
139 """
140 if not etype:
141 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glassc073ced2019-07-08 14:25:31 -0600142 obj = Entry.Lookup(node.path, etype)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600143
Simon Glassbf7fd502016-11-25 20:15:51 -0700144 # Call its constructor to get the object we want.
Simon Glass25ac0e62018-06-01 09:38:14 -0600145 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700146
147 def ReadNode(self):
148 """Read entry information from the node
149
150 This reads all the fields we recognise from the node, ready for use.
151 """
Simon Glass15a587c2018-07-17 13:25:51 -0600152 if 'pos' in self._node.props:
153 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glass3ab95982018-08-01 15:22:37 -0600154 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700155 self.size = fdt_util.GetInt(self._node, 'size')
156 self.align = fdt_util.GetInt(self._node, 'align')
157 if tools.NotPowerOfTwo(self.align):
158 raise ValueError("Node '%s': Alignment %s must be a power of two" %
159 (self._node.path, self.align))
160 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
161 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
162 self.align_size = fdt_util.GetInt(self._node, 'align-size')
163 if tools.NotPowerOfTwo(self.align_size):
164 raise ValueError("Node '%s': Alignment size %s must be a power "
165 "of two" % (self._node.path, self.align_size))
166 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glass3ab95982018-08-01 15:22:37 -0600167 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassba64a0b2018-09-14 04:57:29 -0600168 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
Simon Glassbf7fd502016-11-25 20:15:51 -0700169
Simon Glass6c234bf2018-09-14 04:57:18 -0600170 def GetDefaultFilename(self):
171 return None
172
Simon Glass539aece2018-09-14 04:57:22 -0600173 def GetFdtSet(self):
174 """Get the set of device trees used by this entry
175
176 Returns:
177 Set containing the filename from this entry, if it is a .dtb, else
178 an empty set
179 """
180 fname = self.GetDefaultFilename()
181 # It would be better to use isinstance(self, Entry_blob_dtb) here but
182 # we cannot access Entry_blob_dtb
183 if fname and fname.endswith('.dtb'):
Simon Glassd141f6c2019-05-14 15:53:39 -0600184 return set([fname])
185 return set()
Simon Glass539aece2018-09-14 04:57:22 -0600186
Simon Glass0a98b282018-09-14 04:57:28 -0600187 def ExpandEntries(self):
188 pass
189
Simon Glass078ab1a2018-07-06 10:27:41 -0600190 def AddMissingProperties(self):
191 """Add new properties to the device tree as needed for this entry"""
Simon Glassdbf6be92018-08-01 15:22:42 -0600192 for prop in ['offset', 'size', 'image-pos']:
Simon Glass078ab1a2018-07-06 10:27:41 -0600193 if not prop in self._node.props:
Simon Glassf46621d2018-09-14 04:57:21 -0600194 state.AddZeroProp(self._node, prop)
Simon Glass8287ee82019-07-08 14:25:30 -0600195 if self.compress != 'none':
196 state.AddZeroProp(self._node, 'uncomp-size')
Simon Glasse0e5df92018-09-14 04:57:31 -0600197 err = state.CheckAddHashProp(self._node)
198 if err:
199 self.Raise(err)
Simon Glass078ab1a2018-07-06 10:27:41 -0600200
201 def SetCalculatedProperties(self):
202 """Set the value of device-tree properties calculated by binman"""
Simon Glassf46621d2018-09-14 04:57:21 -0600203 state.SetInt(self._node, 'offset', self.offset)
204 state.SetInt(self._node, 'size', self.size)
Simon Glassf8f8df62018-09-14 04:57:34 -0600205 state.SetInt(self._node, 'image-pos',
206 self.image_pos - self.section.GetRootSkipAtStart())
Simon Glass8287ee82019-07-08 14:25:30 -0600207 if self.uncomp_size is not None:
208 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
Simon Glasse0e5df92018-09-14 04:57:31 -0600209 state.CheckSetHashValue(self._node, self.GetData)
Simon Glass078ab1a2018-07-06 10:27:41 -0600210
Simon Glassecab8972018-07-06 10:27:40 -0600211 def ProcessFdt(self, fdt):
Simon Glass6ed45ba2018-09-14 04:57:24 -0600212 """Allow entries to adjust the device tree
213
214 Some entries need to adjust the device tree for their purposes. This
215 may involve adding or deleting properties.
216
217 Returns:
218 True if processing is complete
219 False if processing could not be completed due to a dependency.
220 This will cause the entry to be retried after others have been
221 called
222 """
Simon Glassecab8972018-07-06 10:27:40 -0600223 return True
224
Simon Glassc8d48ef2018-06-01 09:38:21 -0600225 def SetPrefix(self, prefix):
226 """Set the name prefix for a node
227
228 Args:
229 prefix: Prefix to set, or '' to not use a prefix
230 """
231 if prefix:
232 self.name = prefix + self.name
233
Simon Glass5c890232018-07-06 10:27:19 -0600234 def SetContents(self, data):
235 """Set the contents of an entry
236
237 This sets both the data and content_size properties
238
239 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600240 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600241 """
242 self.data = data
243 self.contents_size = len(self.data)
244
245 def ProcessContentsUpdate(self, data):
Simon Glass5b463fc2019-07-08 14:25:33 -0600246 """Update the contents of an entry, after the size is fixed
Simon Glass5c890232018-07-06 10:27:19 -0600247
Simon Glassa0dcaf22019-07-08 14:25:35 -0600248 This checks that the new data is the same size as the old. If the size
249 has changed, this triggers a re-run of the packing algorithm.
Simon Glass5c890232018-07-06 10:27:19 -0600250
251 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600252 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600253
254 Raises:
255 ValueError if the new data size is not the same as the old
256 """
Simon Glassa0dcaf22019-07-08 14:25:35 -0600257 size_ok = True
Simon Glass5c890232018-07-06 10:27:19 -0600258 if len(data) != self.contents_size:
259 self.Raise('Cannot update entry size from %d to %d' %
Simon Glass5b463fc2019-07-08 14:25:33 -0600260 (self.contents_size, len(data)))
Simon Glass5c890232018-07-06 10:27:19 -0600261 self.SetContents(data)
Simon Glassa0dcaf22019-07-08 14:25:35 -0600262 return size_ok
Simon Glass5c890232018-07-06 10:27:19 -0600263
Simon Glassbf7fd502016-11-25 20:15:51 -0700264 def ObtainContents(self):
265 """Figure out the contents of an entry.
266
267 Returns:
268 True if the contents were found, False if another call is needed
269 after the other entries are processed.
270 """
271 # No contents by default: subclasses can implement this
272 return True
273
Simon Glass3ab95982018-08-01 15:22:37 -0600274 def Pack(self, offset):
Simon Glass25ac0e62018-06-01 09:38:14 -0600275 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700276
277 Most of the time the entries are not fully specified. There may be
278 an alignment but no size. In that case we take the size from the
279 contents of the entry.
280
Simon Glass3ab95982018-08-01 15:22:37 -0600281 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glassbf7fd502016-11-25 20:15:51 -0700282
Simon Glass3ab95982018-08-01 15:22:37 -0600283 Once this function is complete, both the offset and size of the
Simon Glassbf7fd502016-11-25 20:15:51 -0700284 entry will be know.
285
286 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600287 Current section offset pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700288
289 Returns:
Simon Glass3ab95982018-08-01 15:22:37 -0600290 New section offset pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700291 """
Simon Glass3ab95982018-08-01 15:22:37 -0600292 if self.offset is None:
293 if self.offset_unset:
294 self.Raise('No offset set with offset-unset: should another '
295 'entry provide this correct offset?')
296 self.offset = tools.Align(offset, self.align)
Simon Glassbf7fd502016-11-25 20:15:51 -0700297 needed = self.pad_before + self.contents_size + self.pad_after
298 needed = tools.Align(needed, self.align_size)
299 size = self.size
300 if not size:
301 size = needed
Simon Glass3ab95982018-08-01 15:22:37 -0600302 new_offset = self.offset + size
303 aligned_offset = tools.Align(new_offset, self.align_end)
304 if aligned_offset != new_offset:
305 size = aligned_offset - self.offset
306 new_offset = aligned_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700307
308 if not self.size:
309 self.size = size
310
311 if self.size < needed:
312 self.Raise("Entry contents size is %#x (%d) but entry size is "
313 "%#x (%d)" % (needed, needed, self.size, self.size))
314 # Check that the alignment is correct. It could be wrong if the
Simon Glass3ab95982018-08-01 15:22:37 -0600315 # and offset or size values were provided (i.e. not calculated), but
Simon Glassbf7fd502016-11-25 20:15:51 -0700316 # conflict with the provided alignment values
317 if self.size != tools.Align(self.size, self.align_size):
318 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
319 (self.size, self.size, self.align_size, self.align_size))
Simon Glass3ab95982018-08-01 15:22:37 -0600320 if self.offset != tools.Align(self.offset, self.align):
321 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
322 (self.offset, self.offset, self.align, self.align))
Simon Glassbf7fd502016-11-25 20:15:51 -0700323
Simon Glass3ab95982018-08-01 15:22:37 -0600324 return new_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700325
326 def Raise(self, msg):
327 """Convenience function to raise an error referencing a node"""
328 raise ValueError("Node '%s': %s" % (self._node.path, msg))
329
Simon Glass53af22a2018-07-17 13:25:32 -0600330 def GetEntryArgsOrProps(self, props, required=False):
331 """Return the values of a set of properties
332
333 Args:
334 props: List of EntryArg objects
335
336 Raises:
337 ValueError if a property is not found
338 """
339 values = []
340 missing = []
341 for prop in props:
342 python_prop = prop.name.replace('-', '_')
343 if hasattr(self, python_prop):
344 value = getattr(self, python_prop)
345 else:
346 value = None
347 if value is None:
348 value = self.GetArg(prop.name, prop.datatype)
349 if value is None and required:
350 missing.append(prop.name)
351 values.append(value)
352 if missing:
353 self.Raise('Missing required properties/entry args: %s' %
354 (', '.join(missing)))
355 return values
356
Simon Glassbf7fd502016-11-25 20:15:51 -0700357 def GetPath(self):
358 """Get the path of a node
359
360 Returns:
361 Full path of the node for this entry
362 """
363 return self._node.path
364
365 def GetData(self):
366 return self.data
367
Simon Glass3ab95982018-08-01 15:22:37 -0600368 def GetOffsets(self):
Simon Glassed7dd5e2019-07-08 13:18:30 -0600369 """Get the offsets for siblings
370
371 Some entry types can contain information about the position or size of
372 other entries. An example of this is the Intel Flash Descriptor, which
373 knows where the Intel Management Engine section should go.
374
375 If this entry knows about the position of other entries, it can specify
376 this by returning values here
377
378 Returns:
379 Dict:
380 key: Entry type
381 value: List containing position and size of the given entry
Simon Glasscf549042019-07-08 13:18:39 -0600382 type. Either can be None if not known
Simon Glassed7dd5e2019-07-08 13:18:30 -0600383 """
Simon Glassbf7fd502016-11-25 20:15:51 -0700384 return {}
385
Simon Glasscf549042019-07-08 13:18:39 -0600386 def SetOffsetSize(self, offset, size):
387 """Set the offset and/or size of an entry
388
389 Args:
390 offset: New offset, or None to leave alone
391 size: New size, or None to leave alone
392 """
393 if offset is not None:
394 self.offset = offset
395 if size is not None:
396 self.size = size
Simon Glassbf7fd502016-11-25 20:15:51 -0700397
Simon Glassdbf6be92018-08-01 15:22:42 -0600398 def SetImagePos(self, image_pos):
399 """Set the position in the image
400
401 Args:
402 image_pos: Position of this entry in the image
403 """
404 self.image_pos = image_pos + self.offset
405
Simon Glassbf7fd502016-11-25 20:15:51 -0700406 def ProcessContents(self):
Simon Glassa0dcaf22019-07-08 14:25:35 -0600407 """Do any post-packing updates of entry contents
408
409 This function should call ProcessContentsUpdate() to update the entry
410 contents, if necessary, returning its return value here.
411
412 Args:
413 data: Data to set to the contents (bytes)
414
415 Returns:
416 True if the new data size is OK, False if expansion is needed
417
418 Raises:
419 ValueError if the new data size is not the same as the old and
420 state.AllowEntryExpansion() is False
421 """
422 return True
Simon Glass19790632017-11-13 18:55:01 -0700423
Simon Glassf55382b2018-06-01 09:38:13 -0600424 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700425 """Write symbol values into binary files for access at run time
426
427 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600428 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700429 """
430 pass
Simon Glass18546952018-06-01 09:38:16 -0600431
Simon Glass3ab95982018-08-01 15:22:37 -0600432 def CheckOffset(self):
433 """Check that the entry offsets are correct
Simon Glass18546952018-06-01 09:38:16 -0600434
Simon Glass3ab95982018-08-01 15:22:37 -0600435 This is used for entries which have extra offset requirements (other
Simon Glass18546952018-06-01 09:38:16 -0600436 than having to be fully inside their section). Sub-classes can implement
437 this function and raise if there is a problem.
438 """
439 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600440
Simon Glass8122f392018-07-17 13:25:28 -0600441 @staticmethod
Simon Glass163ed6c2018-09-14 04:57:36 -0600442 def GetStr(value):
443 if value is None:
444 return '<none> '
445 return '%08x' % value
446
447 @staticmethod
Simon Glass1be70d22018-07-17 13:25:49 -0600448 def WriteMapLine(fd, indent, name, offset, size, image_pos):
Simon Glass163ed6c2018-09-14 04:57:36 -0600449 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
450 Entry.GetStr(offset), Entry.GetStr(size),
451 name), file=fd)
Simon Glass8122f392018-07-17 13:25:28 -0600452
Simon Glass3b0c3822018-06-01 09:38:20 -0600453 def WriteMap(self, fd, indent):
454 """Write a map of the entry to a .map file
455
456 Args:
457 fd: File to write the map to
458 indent: Curent indent level of map (0=none, 1=one level, etc.)
459 """
Simon Glass1be70d22018-07-17 13:25:49 -0600460 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
461 self.image_pos)
Simon Glass53af22a2018-07-17 13:25:32 -0600462
Simon Glass11e36cc2018-07-17 13:25:38 -0600463 def GetEntries(self):
464 """Return a list of entries contained by this entry
465
466 Returns:
467 List of entries, or None if none. A normal entry has no entries
468 within it so will return None
469 """
470 return None
471
Simon Glass53af22a2018-07-17 13:25:32 -0600472 def GetArg(self, name, datatype=str):
473 """Get the value of an entry argument or device-tree-node property
474
475 Some node properties can be provided as arguments to binman. First check
476 the entry arguments, and fall back to the device tree if not found
477
478 Args:
479 name: Argument name
480 datatype: Data type (str or int)
481
482 Returns:
483 Value of argument as a string or int, or None if no value
484
485 Raises:
486 ValueError if the argument cannot be converted to in
487 """
Simon Glassc55a50f2018-09-14 04:57:19 -0600488 value = state.GetEntryArg(name)
Simon Glass53af22a2018-07-17 13:25:32 -0600489 if value is not None:
490 if datatype == int:
491 try:
492 value = int(value)
493 except ValueError:
494 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
495 (name, value))
496 elif datatype == str:
497 pass
498 else:
499 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
500 datatype)
501 else:
502 value = fdt_util.GetDatatype(self._node, name, datatype)
503 return value
Simon Glassfd8d1f72018-07-17 13:25:36 -0600504
505 @staticmethod
506 def WriteDocs(modules, test_missing=None):
507 """Write out documentation about the various entry types to stdout
508
509 Args:
510 modules: List of modules to include
511 test_missing: Used for testing. This is a module to report
512 as missing
513 """
514 print('''Binman Entry Documentation
515===========================
516
517This file describes the entry types supported by binman. These entry types can
518be placed in an image one by one to build up a final firmware image. It is
519fairly easy to create new entry types. Just add a new file to the 'etype'
520directory. You can use the existing entries as examples.
521
522Note that some entries are subclasses of others, using and extending their
523features to produce new behaviours.
524
525
526''')
527 modules = sorted(modules)
528
529 # Don't show the test entry
530 if '_testing' in modules:
531 modules.remove('_testing')
532 missing = []
533 for name in modules:
Simon Glasse4304402019-07-08 14:25:32 -0600534 if name.startswith('__'):
535 continue
Simon Glassc073ced2019-07-08 14:25:31 -0600536 module = Entry.Lookup(name, name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600537 docs = getattr(module, '__doc__')
538 if test_missing == name:
539 docs = None
540 if docs:
541 lines = docs.splitlines()
542 first_line = lines[0]
543 rest = [line[4:] for line in lines[1:]]
544 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
545 print(hdr)
546 print('-' * len(hdr))
547 print('\n'.join(rest))
548 print()
549 print()
550 else:
551 missing.append(name)
552
553 if missing:
554 raise ValueError('Documentation is missing for modules: %s' %
555 ', '.join(missing))
Simon Glassa326b492018-09-14 04:57:11 -0600556
557 def GetUniqueName(self):
558 """Get a unique name for a node
559
560 Returns:
561 String containing a unique name for a node, consisting of the name
562 of all ancestors (starting from within the 'binman' node) separated
563 by a dot ('.'). This can be useful for generating unique filesnames
564 in the output directory.
565 """
566 name = self.name
567 node = self._node
568 while node.parent:
569 node = node.parent
570 if node.name == 'binman':
571 break
572 name = '%s.%s' % (node.name, name)
573 return name
Simon Glassba64a0b2018-09-14 04:57:29 -0600574
575 def ExpandToLimit(self, limit):
576 """Expand an entry so that it ends at the given offset limit"""
577 if self.offset + self.size < limit:
578 self.size = limit - self.offset
579 # Request the contents again, since changing the size requires that
580 # the data grows. This should not fail, but check it to be sure.
581 if not self.ObtainContents():
582 self.Raise('Cannot obtain contents when expanding entry')
Simon Glassfa1c9372019-07-08 13:18:38 -0600583
584 def HasSibling(self, name):
585 """Check if there is a sibling of a given name
586
587 Returns:
588 True if there is an entry with this name in the the same section,
589 else False
590 """
591 return name in self.section.GetEntries()
Simon Glasscf228942019-07-08 14:25:28 -0600592
593 def GetSiblingImagePos(self, name):
594 """Return the image position of the given sibling
595
596 Returns:
597 Image position of sibling, or None if the sibling has no position,
598 or False if there is no such sibling
599 """
600 if not self.HasSibling(name):
601 return False
602 return self.section.GetEntries()[name].image_pos