blob: dd2daadf16f143ae5251d442a0b916bdaf6445e1 [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
Simon Glasseea264e2019-07-08 14:25:49 -060026import tout
Simon Glassbf7fd502016-11-25 20:15:51 -070027
28modules = {}
29
Simon Glassbadf0ec2018-06-01 09:38:15 -060030our_path = os.path.dirname(os.path.realpath(__file__))
31
Simon Glass53af22a2018-07-17 13:25:32 -060032
33# An argument which can be passed to entries on the command line, in lieu of
34# device-tree properties.
35EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
36
Simon Glass41b8ba02019-07-08 14:25:43 -060037# Information about an entry for use when displaying summaries
38EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
39 'image_pos', 'uncomp_size', 'offset',
40 'entry'])
Simon Glass53af22a2018-07-17 13:25:32 -060041
Simon Glassbf7fd502016-11-25 20:15:51 -070042class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060043 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070044
45 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060046 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070047 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.
49
50 This class is not used by itself. All entry objects are subclasses of
51 Entry.
52
53 Attributes:
Simon Glass8122f392018-07-17 13:25:28 -060054 section: Section object containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070055 node: The node that created this entry
Simon Glass3ab95982018-08-01 15:22:37 -060056 offset: Offset of entry within the section, None if not known yet (in
57 which case it will be calculated by Pack())
Simon Glassbf7fd502016-11-25 20:15:51 -070058 size: Entry size in bytes, None if not known
Simon Glass8287ee82019-07-08 14:25:30 -060059 uncomp_size: Size of uncompressed data in bytes, if the entry is
60 compressed, else None
Simon Glassbf7fd502016-11-25 20:15:51 -070061 contents_size: Size of contents in bytes, 0 by default
Simon Glass3ab95982018-08-01 15:22:37 -060062 align: Entry start offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070063 align_size: Entry size alignment, or None
Simon Glass3ab95982018-08-01 15:22:37 -060064 align_end: Entry end offset alignment, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070065 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)
Simon Glass8287ee82019-07-08 14:25:30 -060068 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
Simon Glassc52c9e72019-07-08 14:25:37 -060069 orig_offset: Original offset value read from node
70 orig_size: Original size value read from node
Simon Glassbf7fd502016-11-25 20:15:51 -070071 """
Simon Glassc8d48ef2018-06-01 09:38:21 -060072 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glass25ac0e62018-06-01 09:38:14 -060073 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070074 self.etype = etype
75 self._node = node
Simon Glassc8d48ef2018-06-01 09:38:21 -060076 self.name = node and (name_prefix + node.name) or 'none'
Simon Glass3ab95982018-08-01 15:22:37 -060077 self.offset = None
Simon Glassbf7fd502016-11-25 20:15:51 -070078 self.size = None
Simon Glass8287ee82019-07-08 14:25:30 -060079 self.uncomp_size = None
Simon Glass24d0d3c2018-07-17 13:25:47 -060080 self.data = None
Simon Glassbf7fd502016-11-25 20:15:51 -070081 self.contents_size = 0
82 self.align = None
83 self.align_size = None
84 self.align_end = None
85 self.pad_before = 0
86 self.pad_after = 0
Simon Glass3ab95982018-08-01 15:22:37 -060087 self.offset_unset = False
Simon Glassdbf6be92018-08-01 15:22:42 -060088 self.image_pos = None
Simon Glassba64a0b2018-09-14 04:57:29 -060089 self._expand_size = False
Simon Glass8287ee82019-07-08 14:25:30 -060090 self.compress = 'none'
Simon Glassbf7fd502016-11-25 20:15:51 -070091 if read_node:
92 self.ReadNode()
93
94 @staticmethod
Simon Glassc073ced2019-07-08 14:25:31 -060095 def Lookup(node_path, etype):
Simon Glassfd8d1f72018-07-17 13:25:36 -060096 """Look up the entry class for a node.
Simon Glassbf7fd502016-11-25 20:15:51 -070097
98 Args:
Simon Glassfd8d1f72018-07-17 13:25:36 -060099 node_node: Path name of Node object containing information about
100 the entry to create (used for errors)
101 etype: Entry type to use
Simon Glassbf7fd502016-11-25 20:15:51 -0700102
103 Returns:
Simon Glassfd8d1f72018-07-17 13:25:36 -0600104 The entry class object if found, else None
Simon Glassbf7fd502016-11-25 20:15:51 -0700105 """
Simon Glassdd57c132018-06-01 09:38:11 -0600106 # Convert something like 'u-boot@0' to 'u_boot' since we are only
107 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -0700108 module_name = etype.replace('-', '_')
Simon Glassdd57c132018-06-01 09:38:11 -0600109 if '@' in module_name:
110 module_name = module_name.split('@')[0]
Simon Glassbf7fd502016-11-25 20:15:51 -0700111 module = modules.get(module_name)
112
Simon Glassbadf0ec2018-06-01 09:38:15 -0600113 # Also allow entry-type modules to be brought in from the etype directory.
114
Simon Glassbf7fd502016-11-25 20:15:51 -0700115 # Import the module if we have not already done so.
116 if not module:
Simon Glassbadf0ec2018-06-01 09:38:15 -0600117 old_path = sys.path
118 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -0700119 try:
120 if have_importlib:
121 module = importlib.import_module(module_name)
122 else:
123 module = __import__(module_name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600124 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))
Simon Glassbadf0ec2018-06-01 09:38:15 -0600127 finally:
128 sys.path = old_path
Simon Glassbf7fd502016-11-25 20:15:51 -0700129 modules[module_name] = module
130
Simon Glassfd8d1f72018-07-17 13:25:36 -0600131 # Look up the expected class name
132 return getattr(module, 'Entry_%s' % module_name)
133
134 @staticmethod
135 def Create(section, node, etype=None):
136 """Create a new entry for a node.
137
138 Args:
139 section: Section object containing this node
140 node: Node object containing information about the entry to
141 create
142 etype: Entry type to use, or None to work it out (used for tests)
143
144 Returns:
145 A new Entry object of the correct type (a subclass of Entry)
146 """
147 if not etype:
148 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glassc073ced2019-07-08 14:25:31 -0600149 obj = Entry.Lookup(node.path, etype)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600150
Simon Glassbf7fd502016-11-25 20:15:51 -0700151 # Call its constructor to get the object we want.
Simon Glass25ac0e62018-06-01 09:38:14 -0600152 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700153
154 def ReadNode(self):
155 """Read entry information from the node
156
157 This reads all the fields we recognise from the node, ready for use.
158 """
Simon Glass15a587c2018-07-17 13:25:51 -0600159 if 'pos' in self._node.props:
160 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glass3ab95982018-08-01 15:22:37 -0600161 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700162 self.size = fdt_util.GetInt(self._node, 'size')
Simon Glassc52c9e72019-07-08 14:25:37 -0600163 self.orig_offset = self.offset
164 self.orig_size = self.size
165
Simon Glassffded752019-07-08 14:25:46 -0600166 # 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')
170
Simon Glassbf7fd502016-11-25 20:15:51 -0700171 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):
Simon Glass8beb11e2019-07-08 14:25:47 -0600179 self.Raise("Alignment size %s must be a power of two" %
180 self.align_size)
Simon Glassbf7fd502016-11-25 20:15:51 -0700181 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glass3ab95982018-08-01 15:22:37 -0600182 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassba64a0b2018-09-14 04:57:29 -0600183 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
Simon Glassbf7fd502016-11-25 20:15:51 -0700184
Simon Glass6c234bf2018-09-14 04:57:18 -0600185 def GetDefaultFilename(self):
186 return None
187
Simon Glassa8adb6d2019-07-20 12:23:28 -0600188 def GetFdts(self):
189 """Get the device trees used by this entry
Simon Glass539aece2018-09-14 04:57:22 -0600190
191 Returns:
Simon Glassa8adb6d2019-07-20 12:23:28 -0600192 Empty dict, if this entry is not a .dtb, otherwise:
193 Dict:
194 key: Filename from this entry (without the path)
Simon Glass4bdd3002019-07-20 12:23:31 -0600195 value: Tuple:
196 Fdt object for this dtb, or None if not available
197 Filename of file containing this dtb
Simon Glass539aece2018-09-14 04:57:22 -0600198 """
Simon Glassa8adb6d2019-07-20 12:23:28 -0600199 return {}
Simon Glass539aece2018-09-14 04:57:22 -0600200
Simon Glass0a98b282018-09-14 04:57:28 -0600201 def ExpandEntries(self):
202 pass
203
Simon Glass078ab1a2018-07-06 10:27:41 -0600204 def AddMissingProperties(self):
205 """Add new properties to the device tree as needed for this entry"""
Simon Glassdbf6be92018-08-01 15:22:42 -0600206 for prop in ['offset', 'size', 'image-pos']:
Simon Glass078ab1a2018-07-06 10:27:41 -0600207 if not prop in self._node.props:
Simon Glassf46621d2018-09-14 04:57:21 -0600208 state.AddZeroProp(self._node, prop)
Simon Glass8287ee82019-07-08 14:25:30 -0600209 if self.compress != 'none':
210 state.AddZeroProp(self._node, 'uncomp-size')
Simon Glasse0e5df92018-09-14 04:57:31 -0600211 err = state.CheckAddHashProp(self._node)
212 if err:
213 self.Raise(err)
Simon Glass078ab1a2018-07-06 10:27:41 -0600214
215 def SetCalculatedProperties(self):
216 """Set the value of device-tree properties calculated by binman"""
Simon Glassf46621d2018-09-14 04:57:21 -0600217 state.SetInt(self._node, 'offset', self.offset)
218 state.SetInt(self._node, 'size', self.size)
Simon Glass8beb11e2019-07-08 14:25:47 -0600219 base = self.section.GetRootSkipAtStart() if self.section else 0
220 state.SetInt(self._node, 'image-pos', self.image_pos - base)
Simon Glass8287ee82019-07-08 14:25:30 -0600221 if self.uncomp_size is not None:
222 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
Simon Glasse0e5df92018-09-14 04:57:31 -0600223 state.CheckSetHashValue(self._node, self.GetData)
Simon Glass078ab1a2018-07-06 10:27:41 -0600224
Simon Glassecab8972018-07-06 10:27:40 -0600225 def ProcessFdt(self, fdt):
Simon Glass6ed45ba2018-09-14 04:57:24 -0600226 """Allow entries to adjust the device tree
227
228 Some entries need to adjust the device tree for their purposes. This
229 may involve adding or deleting properties.
230
231 Returns:
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
235 called
236 """
Simon Glassecab8972018-07-06 10:27:40 -0600237 return True
238
Simon Glassc8d48ef2018-06-01 09:38:21 -0600239 def SetPrefix(self, prefix):
240 """Set the name prefix for a node
241
242 Args:
243 prefix: Prefix to set, or '' to not use a prefix
244 """
245 if prefix:
246 self.name = prefix + self.name
247
Simon Glass5c890232018-07-06 10:27:19 -0600248 def SetContents(self, data):
249 """Set the contents of an entry
250
251 This sets both the data and content_size properties
252
253 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600254 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600255 """
256 self.data = data
257 self.contents_size = len(self.data)
258
259 def ProcessContentsUpdate(self, data):
Simon Glass5b463fc2019-07-08 14:25:33 -0600260 """Update the contents of an entry, after the size is fixed
Simon Glass5c890232018-07-06 10:27:19 -0600261
Simon Glassa0dcaf22019-07-08 14:25:35 -0600262 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.
Simon Glass5c890232018-07-06 10:27:19 -0600264
265 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600266 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600267
268 Raises:
269 ValueError if the new data size is not the same as the old
270 """
Simon Glassa0dcaf22019-07-08 14:25:35 -0600271 size_ok = True
Simon Glassc52c9e72019-07-08 14:25:37 -0600272 new_size = len(data)
273 if state.AllowEntryExpansion():
274 if new_size > self.contents_size:
Simon Glasseea264e2019-07-08 14:25:49 -0600275 tout.Debug("Entry '%s' size change from %#x to %#x" % (
Simon Glassc52c9e72019-07-08 14:25:37 -0600276 self._node.path, self.contents_size, new_size))
277 # self.data will indicate the new size needed
278 size_ok = False
279 elif new_size != self.contents_size:
Simon Glass5c890232018-07-06 10:27:19 -0600280 self.Raise('Cannot update entry size from %d to %d' %
Simon Glassc52c9e72019-07-08 14:25:37 -0600281 (self.contents_size, new_size))
Simon Glass5c890232018-07-06 10:27:19 -0600282 self.SetContents(data)
Simon Glassa0dcaf22019-07-08 14:25:35 -0600283 return size_ok
Simon Glass5c890232018-07-06 10:27:19 -0600284
Simon Glassbf7fd502016-11-25 20:15:51 -0700285 def ObtainContents(self):
286 """Figure out the contents of an entry.
287
288 Returns:
289 True if the contents were found, False if another call is needed
290 after the other entries are processed.
291 """
292 # No contents by default: subclasses can implement this
293 return True
294
Simon Glassc52c9e72019-07-08 14:25:37 -0600295 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
299
Simon Glass3ab95982018-08-01 15:22:37 -0600300 def Pack(self, offset):
Simon Glass25ac0e62018-06-01 09:38:14 -0600301 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700302
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.
306
Simon Glass3ab95982018-08-01 15:22:37 -0600307 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glassbf7fd502016-11-25 20:15:51 -0700308
Simon Glass3ab95982018-08-01 15:22:37 -0600309 Once this function is complete, both the offset and size of the
Simon Glassbf7fd502016-11-25 20:15:51 -0700310 entry will be know.
311
312 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600313 Current section offset pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700314
315 Returns:
Simon Glass3ab95982018-08-01 15:22:37 -0600316 New section offset pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700317 """
Simon Glass3ab95982018-08-01 15:22:37 -0600318 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)
Simon Glassbf7fd502016-11-25 20:15:51 -0700323 needed = self.pad_before + self.contents_size + self.pad_after
324 needed = tools.Align(needed, self.align_size)
325 size = self.size
326 if not size:
327 size = needed
Simon Glass3ab95982018-08-01 15:22:37 -0600328 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
Simon Glassbf7fd502016-11-25 20:15:51 -0700333
334 if not self.size:
335 self.size = size
336
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
Simon Glass3ab95982018-08-01 15:22:37 -0600341 # and offset or size values were provided (i.e. not calculated), but
Simon Glassbf7fd502016-11-25 20:15:51 -0700342 # 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))
Simon Glass3ab95982018-08-01 15:22:37 -0600346 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))
Simon Glassbf7fd502016-11-25 20:15:51 -0700349
Simon Glass3ab95982018-08-01 15:22:37 -0600350 return new_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700351
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))
355
Simon Glass53af22a2018-07-17 13:25:32 -0600356 def GetEntryArgsOrProps(self, props, required=False):
357 """Return the values of a set of properties
358
359 Args:
360 props: List of EntryArg objects
361
362 Raises:
363 ValueError if a property is not found
364 """
365 values = []
366 missing = []
367 for prop in props:
368 python_prop = prop.name.replace('-', '_')
369 if hasattr(self, python_prop):
370 value = getattr(self, python_prop)
371 else:
372 value = None
373 if value is None:
374 value = self.GetArg(prop.name, prop.datatype)
375 if value is None and required:
376 missing.append(prop.name)
377 values.append(value)
378 if missing:
379 self.Raise('Missing required properties/entry args: %s' %
380 (', '.join(missing)))
381 return values
382
Simon Glassbf7fd502016-11-25 20:15:51 -0700383 def GetPath(self):
384 """Get the path of a node
385
386 Returns:
387 Full path of the node for this entry
388 """
389 return self._node.path
390
391 def GetData(self):
392 return self.data
393
Simon Glass3ab95982018-08-01 15:22:37 -0600394 def GetOffsets(self):
Simon Glassed7dd5e2019-07-08 13:18:30 -0600395 """Get the offsets for siblings
396
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.
400
401 If this entry knows about the position of other entries, it can specify
402 this by returning values here
403
404 Returns:
405 Dict:
406 key: Entry type
407 value: List containing position and size of the given entry
Simon Glasscf549042019-07-08 13:18:39 -0600408 type. Either can be None if not known
Simon Glassed7dd5e2019-07-08 13:18:30 -0600409 """
Simon Glassbf7fd502016-11-25 20:15:51 -0700410 return {}
411
Simon Glasscf549042019-07-08 13:18:39 -0600412 def SetOffsetSize(self, offset, size):
413 """Set the offset and/or size of an entry
414
415 Args:
416 offset: New offset, or None to leave alone
417 size: New size, or None to leave alone
418 """
419 if offset is not None:
420 self.offset = offset
421 if size is not None:
422 self.size = size
Simon Glassbf7fd502016-11-25 20:15:51 -0700423
Simon Glassdbf6be92018-08-01 15:22:42 -0600424 def SetImagePos(self, image_pos):
425 """Set the position in the image
426
427 Args:
428 image_pos: Position of this entry in the image
429 """
430 self.image_pos = image_pos + self.offset
431
Simon Glassbf7fd502016-11-25 20:15:51 -0700432 def ProcessContents(self):
Simon Glassa0dcaf22019-07-08 14:25:35 -0600433 """Do any post-packing updates of entry contents
434
435 This function should call ProcessContentsUpdate() to update the entry
436 contents, if necessary, returning its return value here.
437
438 Args:
439 data: Data to set to the contents (bytes)
440
441 Returns:
442 True if the new data size is OK, False if expansion is needed
443
444 Raises:
445 ValueError if the new data size is not the same as the old and
446 state.AllowEntryExpansion() is False
447 """
448 return True
Simon Glass19790632017-11-13 18:55:01 -0700449
Simon Glassf55382b2018-06-01 09:38:13 -0600450 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700451 """Write symbol values into binary files for access at run time
452
453 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600454 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700455 """
456 pass
Simon Glass18546952018-06-01 09:38:16 -0600457
Simon Glass3ab95982018-08-01 15:22:37 -0600458 def CheckOffset(self):
459 """Check that the entry offsets are correct
Simon Glass18546952018-06-01 09:38:16 -0600460
Simon Glass3ab95982018-08-01 15:22:37 -0600461 This is used for entries which have extra offset requirements (other
Simon Glass18546952018-06-01 09:38:16 -0600462 than having to be fully inside their section). Sub-classes can implement
463 this function and raise if there is a problem.
464 """
465 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600466
Simon Glass8122f392018-07-17 13:25:28 -0600467 @staticmethod
Simon Glass163ed6c2018-09-14 04:57:36 -0600468 def GetStr(value):
469 if value is None:
470 return '<none> '
471 return '%08x' % value
472
473 @staticmethod
Simon Glass1be70d22018-07-17 13:25:49 -0600474 def WriteMapLine(fd, indent, name, offset, size, image_pos):
Simon Glass163ed6c2018-09-14 04:57:36 -0600475 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
476 Entry.GetStr(offset), Entry.GetStr(size),
477 name), file=fd)
Simon Glass8122f392018-07-17 13:25:28 -0600478
Simon Glass3b0c3822018-06-01 09:38:20 -0600479 def WriteMap(self, fd, indent):
480 """Write a map of the entry to a .map file
481
482 Args:
483 fd: File to write the map to
484 indent: Curent indent level of map (0=none, 1=one level, etc.)
485 """
Simon Glass1be70d22018-07-17 13:25:49 -0600486 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
487 self.image_pos)
Simon Glass53af22a2018-07-17 13:25:32 -0600488
Simon Glass11e36cc2018-07-17 13:25:38 -0600489 def GetEntries(self):
490 """Return a list of entries contained by this entry
491
492 Returns:
493 List of entries, or None if none. A normal entry has no entries
494 within it so will return None
495 """
496 return None
497
Simon Glass53af22a2018-07-17 13:25:32 -0600498 def GetArg(self, name, datatype=str):
499 """Get the value of an entry argument or device-tree-node property
500
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
503
504 Args:
505 name: Argument name
506 datatype: Data type (str or int)
507
508 Returns:
509 Value of argument as a string or int, or None if no value
510
511 Raises:
512 ValueError if the argument cannot be converted to in
513 """
Simon Glassc55a50f2018-09-14 04:57:19 -0600514 value = state.GetEntryArg(name)
Simon Glass53af22a2018-07-17 13:25:32 -0600515 if value is not None:
516 if datatype == int:
517 try:
518 value = int(value)
519 except ValueError:
520 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
521 (name, value))
522 elif datatype == str:
523 pass
524 else:
525 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
526 datatype)
527 else:
528 value = fdt_util.GetDatatype(self._node, name, datatype)
529 return value
Simon Glassfd8d1f72018-07-17 13:25:36 -0600530
531 @staticmethod
532 def WriteDocs(modules, test_missing=None):
533 """Write out documentation about the various entry types to stdout
534
535 Args:
536 modules: List of modules to include
537 test_missing: Used for testing. This is a module to report
538 as missing
539 """
540 print('''Binman Entry Documentation
541===========================
542
543This file describes the entry types supported by binman. These entry types can
544be placed in an image one by one to build up a final firmware image. It is
545fairly easy to create new entry types. Just add a new file to the 'etype'
546directory. You can use the existing entries as examples.
547
548Note that some entries are subclasses of others, using and extending their
549features to produce new behaviours.
550
551
552''')
553 modules = sorted(modules)
554
555 # Don't show the test entry
556 if '_testing' in modules:
557 modules.remove('_testing')
558 missing = []
559 for name in modules:
Simon Glasse4304402019-07-08 14:25:32 -0600560 if name.startswith('__'):
561 continue
Simon Glassc073ced2019-07-08 14:25:31 -0600562 module = Entry.Lookup(name, name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600563 docs = getattr(module, '__doc__')
564 if test_missing == name:
565 docs = None
566 if docs:
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)
571 print(hdr)
572 print('-' * len(hdr))
573 print('\n'.join(rest))
574 print()
575 print()
576 else:
577 missing.append(name)
578
579 if missing:
580 raise ValueError('Documentation is missing for modules: %s' %
581 ', '.join(missing))
Simon Glassa326b492018-09-14 04:57:11 -0600582
583 def GetUniqueName(self):
584 """Get a unique name for a node
585
586 Returns:
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.
591 """
592 name = self.name
593 node = self._node
594 while node.parent:
595 node = node.parent
596 if node.name == 'binman':
597 break
598 name = '%s.%s' % (node.name, name)
599 return name
Simon Glassba64a0b2018-09-14 04:57:29 -0600600
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')
Simon Glassfa1c9372019-07-08 13:18:38 -0600609
610 def HasSibling(self, name):
611 """Check if there is a sibling of a given name
612
613 Returns:
614 True if there is an entry with this name in the the same section,
615 else False
616 """
617 return name in self.section.GetEntries()
Simon Glasscf228942019-07-08 14:25:28 -0600618
619 def GetSiblingImagePos(self, name):
620 """Return the image position of the given sibling
621
622 Returns:
623 Image position of sibling, or None if the sibling has no position,
624 or False if there is no such sibling
625 """
626 if not self.HasSibling(name):
627 return False
628 return self.section.GetEntries()[name].image_pos
Simon Glass41b8ba02019-07-08 14:25:43 -0600629
630 @staticmethod
631 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
632 uncomp_size, offset, entry):
633 """Add a new entry to the entries list
634
635 Args:
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
643 None
644 offset: Entry offset within parent in bytes (int)
645 entry: Entry object
646 """
647 entries.append(EntryInfo(indent, name, etype, size, image_pos,
648 uncomp_size, offset, entry))
649
650 def ListEntries(self, entries, indent):
651 """Add files in this entry to the list of entries
652
653 This can be overridden by subclasses which need different behaviour.
654
655 Args:
656 entries: List (of EntryInfo objects) to add to
657 indent: Current indent level to add to list
658 """
659 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
660 self.image_pos, self.uncomp_size, self.offset, self)
Simon Glassf667e452019-07-08 14:25:50 -0600661
662 def ReadData(self, decomp=True):
663 """Read the data for an entry from the image
664
665 This is used when the image has been read in and we want to extract the
666 data for a particular entry from that image.
667
668 Args:
669 decomp: True to decompress any compressed data before returning it;
670 False to return the raw, uncompressed data
671
672 Returns:
673 Entry data (bytes)
674 """
675 # Use True here so that we get an uncompressed section to work from,
676 # although compressed sections are currently not supported
677 data = self.section.ReadData(True)
678 tout.Info('%s: Reading data from offset %#x-%#x, size %#x (avail %#x)' %
679 (self.GetPath(), self.offset, self.offset + self.size,
680 self.size, len(data)))
681 return data[self.offset:self.offset + self.size]