blob: 631215dfc88abfc0e12cf1210a5c95b83a98d6c6 [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 Glass53af22a2018-07-17 13:25:32 -06007from collections import namedtuple
Simon Glassb4cf5f12019-10-31 07:42:59 -06008import importlib
Simon Glassbadf0ec2018-06-01 09:38:15 -06009import os
Simon Glass790ba9f2022-01-12 13:10:36 -070010import pathlib
Simon Glassbadf0ec2018-06-01 09:38:15 -060011import sys
Simon Glassc55a50f2018-09-14 04:57:19 -060012
Simon Glass386c63c2022-01-09 20:13:50 -070013from binman import bintool
Simon Glassad35ce52022-01-09 20:14:03 -070014from binman import comp_util
Simon Glass16287932020-04-17 18:09:03 -060015from dtoc import fdt_util
Simon Glassbf776672020-04-17 18:09:04 -060016from patman import tools
Simon Glassc1aa66e2022-01-29 14:14:04 -070017from patman.tools import to_hex, to_hex_size
Simon Glassbf776672020-04-17 18:09:04 -060018from patman import tout
Simon Glassbf7fd502016-11-25 20:15:51 -070019
20modules = {}
21
Simon Glass53af22a2018-07-17 13:25:32 -060022
23# An argument which can be passed to entries on the command line, in lieu of
24# device-tree properties.
25EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
26
Simon Glass41b8ba02019-07-08 14:25:43 -060027# Information about an entry for use when displaying summaries
28EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
29 'image_pos', 'uncomp_size', 'offset',
30 'entry'])
Simon Glass53af22a2018-07-17 13:25:32 -060031
Simon Glassbf7fd502016-11-25 20:15:51 -070032class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060033 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070034
35 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060036 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070037 Entries can be placed either right next to each other, or with padding
38 between them. The type of the entry determines the data that is in it.
39
40 This class is not used by itself. All entry objects are subclasses of
41 Entry.
42
43 Attributes:
Simon Glass8122f392018-07-17 13:25:28 -060044 section: Section object containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070045 node: The node that created this entry
Simon Glass3ab95982018-08-01 15:22:37 -060046 offset: Offset of entry within the section, None if not known yet (in
47 which case it will be calculated by Pack())
Simon Glassbf7fd502016-11-25 20:15:51 -070048 size: Entry size in bytes, None if not known
Simon Glass9a5d3dc2019-10-31 07:43:02 -060049 pre_reset_size: size as it was before ResetForPack(). This allows us to
50 keep track of the size we started with and detect size changes
Simon Glass8287ee82019-07-08 14:25:30 -060051 uncomp_size: Size of uncompressed data in bytes, if the entry is
52 compressed, else None
Simon Glassbf7fd502016-11-25 20:15:51 -070053 contents_size: Size of contents in bytes, 0 by default
Simon Glass4eec34c2020-10-26 17:40:10 -060054 align: Entry start offset alignment relative to the start of the
55 containing section, or None
Simon Glassbf7fd502016-11-25 20:15:51 -070056 align_size: Entry size alignment, or None
Simon Glass4eec34c2020-10-26 17:40:10 -060057 align_end: Entry end offset alignment relative to the start of the
58 containing section, or None
Simon Glassf90d9062020-10-26 17:40:09 -060059 pad_before: Number of pad bytes before the contents when it is placed
60 in the containing section, 0 if none. The pad bytes become part of
61 the entry.
62 pad_after: Number of pad bytes after the contents when it is placed in
63 the containing section, 0 if none. The pad bytes become part of
64 the entry.
65 data: Contents of entry (string of bytes). This does not include
Simon Glass97c3e9a2020-10-26 17:40:15 -060066 padding created by pad_before or pad_after. If the entry is
67 compressed, this contains the compressed data.
68 uncomp_data: Original uncompressed data, if this entry is compressed,
69 else None
Simon Glass8287ee82019-07-08 14:25:30 -060070 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
Simon Glassc52c9e72019-07-08 14:25:37 -060071 orig_offset: Original offset value read from node
72 orig_size: Original size value read from node
Simon Glass87958982020-09-01 05:13:57 -060073 missing: True if this entry is missing its contents
74 allow_missing: Allow children of this entry to be missing (used by
75 subclasses such as Entry_section)
Heiko Thierya89c8f22022-01-06 11:49:41 +010076 allow_fake: Allow creating a dummy fake file if the blob file is not
77 available. This is mainly used for testing.
Simon Glass87958982020-09-01 05:13:57 -060078 external: True if this entry contains an external binary blob
Simon Glass386c63c2022-01-09 20:13:50 -070079 bintools: Bintools used by this entry (only populated for Image)
Simon Glass4f9ee832022-01-09 20:14:09 -070080 missing_bintools: List of missing bintools for this entry
Alper Nebi Yasakee813c82022-02-09 22:02:35 +030081 update_hash: True if this entry's "hash" subnode should be
82 updated with a hash of the entry contents
Simon Glassbf7fd502016-11-25 20:15:51 -070083 """
Simon Glassc6bd6e22019-07-20 12:23:45 -060084 def __init__(self, section, etype, node, name_prefix=''):
Simon Glass8dbb7442019-08-24 07:22:44 -060085 # Put this here to allow entry-docs and help to work without libfdt
86 global state
Simon Glass16287932020-04-17 18:09:03 -060087 from binman import state
Simon Glass8dbb7442019-08-24 07:22:44 -060088
Simon Glass25ac0e62018-06-01 09:38:14 -060089 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070090 self.etype = etype
91 self._node = node
Simon Glassc8d48ef2018-06-01 09:38:21 -060092 self.name = node and (name_prefix + node.name) or 'none'
Simon Glass3ab95982018-08-01 15:22:37 -060093 self.offset = None
Simon Glassbf7fd502016-11-25 20:15:51 -070094 self.size = None
Simon Glass9a5d3dc2019-10-31 07:43:02 -060095 self.pre_reset_size = None
Simon Glass8287ee82019-07-08 14:25:30 -060096 self.uncomp_size = None
Simon Glass24d0d3c2018-07-17 13:25:47 -060097 self.data = None
Simon Glass97c3e9a2020-10-26 17:40:15 -060098 self.uncomp_data = None
Simon Glassbf7fd502016-11-25 20:15:51 -070099 self.contents_size = 0
100 self.align = None
101 self.align_size = None
102 self.align_end = None
103 self.pad_before = 0
104 self.pad_after = 0
Simon Glass3ab95982018-08-01 15:22:37 -0600105 self.offset_unset = False
Simon Glassdbf6be92018-08-01 15:22:42 -0600106 self.image_pos = None
Simon Glassc4738312021-11-23 11:03:43 -0700107 self.expand_size = False
Simon Glass8287ee82019-07-08 14:25:30 -0600108 self.compress = 'none'
Simon Glassb1cca952020-07-09 18:39:40 -0600109 self.missing = False
Heiko Thierya89c8f22022-01-06 11:49:41 +0100110 self.faked = False
Simon Glass87958982020-09-01 05:13:57 -0600111 self.external = False
112 self.allow_missing = False
Heiko Thierya89c8f22022-01-06 11:49:41 +0100113 self.allow_fake = False
Simon Glass386c63c2022-01-09 20:13:50 -0700114 self.bintools = {}
Simon Glass4f9ee832022-01-09 20:14:09 -0700115 self.missing_bintools = []
Alper Nebi Yasakee813c82022-02-09 22:02:35 +0300116 self.update_hash = True
Simon Glassbf7fd502016-11-25 20:15:51 -0700117
118 @staticmethod
Simon Glass858436d2021-11-23 21:09:49 -0700119 def FindEntryClass(etype, expanded):
Simon Glassfd8d1f72018-07-17 13:25:36 -0600120 """Look up the entry class for a node.
Simon Glassbf7fd502016-11-25 20:15:51 -0700121
122 Args:
Simon Glassfd8d1f72018-07-17 13:25:36 -0600123 node_node: Path name of Node object containing information about
124 the entry to create (used for errors)
125 etype: Entry type to use
Simon Glassb35fb172021-03-18 20:25:04 +1300126 expanded: Use the expanded version of etype
Simon Glassbf7fd502016-11-25 20:15:51 -0700127
128 Returns:
Simon Glassb35fb172021-03-18 20:25:04 +1300129 The entry class object if found, else None if not found and expanded
Simon Glass858436d2021-11-23 21:09:49 -0700130 is True, else a tuple:
131 module name that could not be found
132 exception received
Simon Glassbf7fd502016-11-25 20:15:51 -0700133 """
Simon Glassdd57c132018-06-01 09:38:11 -0600134 # Convert something like 'u-boot@0' to 'u_boot' since we are only
135 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -0700136 module_name = etype.replace('-', '_')
Simon Glassb35fb172021-03-18 20:25:04 +1300137
Simon Glassdd57c132018-06-01 09:38:11 -0600138 if '@' in module_name:
139 module_name = module_name.split('@')[0]
Simon Glassb35fb172021-03-18 20:25:04 +1300140 if expanded:
141 module_name += '_expanded'
Simon Glassbf7fd502016-11-25 20:15:51 -0700142 module = modules.get(module_name)
143
Simon Glassbadf0ec2018-06-01 09:38:15 -0600144 # Also allow entry-type modules to be brought in from the etype directory.
145
Simon Glassbf7fd502016-11-25 20:15:51 -0700146 # Import the module if we have not already done so.
147 if not module:
148 try:
Simon Glass16287932020-04-17 18:09:03 -0600149 module = importlib.import_module('binman.etype.' + module_name)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600150 except ImportError as e:
Simon Glassb35fb172021-03-18 20:25:04 +1300151 if expanded:
152 return None
Simon Glass858436d2021-11-23 21:09:49 -0700153 return module_name, e
Simon Glassbf7fd502016-11-25 20:15:51 -0700154 modules[module_name] = module
155
Simon Glassfd8d1f72018-07-17 13:25:36 -0600156 # Look up the expected class name
157 return getattr(module, 'Entry_%s' % module_name)
158
159 @staticmethod
Simon Glass858436d2021-11-23 21:09:49 -0700160 def Lookup(node_path, etype, expanded, missing_etype=False):
161 """Look up the entry class for a node.
162
163 Args:
164 node_node (str): Path name of Node object containing information
165 about the entry to create (used for errors)
166 etype (str): Entry type to use
167 expanded (bool): Use the expanded version of etype
168 missing_etype (bool): True to default to a blob etype if the
169 requested etype is not found
170
171 Returns:
172 The entry class object if found, else None if not found and expanded
173 is True
174
175 Raise:
176 ValueError if expanded is False and the class is not found
177 """
178 # Convert something like 'u-boot@0' to 'u_boot' since we are only
179 # interested in the type.
180 cls = Entry.FindEntryClass(etype, expanded)
181 if cls is None:
182 return None
183 elif isinstance(cls, tuple):
184 if missing_etype:
185 cls = Entry.FindEntryClass('blob', False)
186 if isinstance(cls, tuple): # This should not fail
187 module_name, e = cls
188 raise ValueError(
189 "Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
190 (etype, node_path, module_name, e))
191 return cls
192
193 @staticmethod
194 def Create(section, node, etype=None, expanded=False, missing_etype=False):
Simon Glassfd8d1f72018-07-17 13:25:36 -0600195 """Create a new entry for a node.
196
197 Args:
Simon Glass858436d2021-11-23 21:09:49 -0700198 section (entry_Section): Section object containing this node
199 node (Node): Node object containing information about the entry to
200 create
201 etype (str): Entry type to use, or None to work it out (used for
202 tests)
203 expanded (bool): Use the expanded version of etype
204 missing_etype (bool): True to default to a blob etype if the
205 requested etype is not found
Simon Glassfd8d1f72018-07-17 13:25:36 -0600206
207 Returns:
208 A new Entry object of the correct type (a subclass of Entry)
209 """
210 if not etype:
211 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glass858436d2021-11-23 21:09:49 -0700212 obj = Entry.Lookup(node.path, etype, expanded, missing_etype)
Simon Glassb35fb172021-03-18 20:25:04 +1300213 if obj and expanded:
214 # Check whether to use the expanded entry
215 new_etype = etype + '-expanded'
Simon Glass3d433382021-03-21 18:24:30 +1300216 can_expand = not fdt_util.GetBool(node, 'no-expanded')
217 if can_expand and obj.UseExpanded(node, etype, new_etype):
Simon Glassb35fb172021-03-18 20:25:04 +1300218 etype = new_etype
219 else:
220 obj = None
221 if not obj:
Simon Glass858436d2021-11-23 21:09:49 -0700222 obj = Entry.Lookup(node.path, etype, False, missing_etype)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600223
Simon Glassbf7fd502016-11-25 20:15:51 -0700224 # Call its constructor to get the object we want.
Simon Glass25ac0e62018-06-01 09:38:14 -0600225 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700226
227 def ReadNode(self):
228 """Read entry information from the node
229
Simon Glassc6bd6e22019-07-20 12:23:45 -0600230 This must be called as the first thing after the Entry is created.
231
Simon Glassbf7fd502016-11-25 20:15:51 -0700232 This reads all the fields we recognise from the node, ready for use.
233 """
Simon Glass15a587c2018-07-17 13:25:51 -0600234 if 'pos' in self._node.props:
235 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glass3ab95982018-08-01 15:22:37 -0600236 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glassbf7fd502016-11-25 20:15:51 -0700237 self.size = fdt_util.GetInt(self._node, 'size')
Simon Glass12bb1a92019-07-20 12:23:51 -0600238 self.orig_offset = fdt_util.GetInt(self._node, 'orig-offset')
239 self.orig_size = fdt_util.GetInt(self._node, 'orig-size')
240 if self.GetImage().copy_to_orig:
241 self.orig_offset = self.offset
242 self.orig_size = self.size
Simon Glassc52c9e72019-07-08 14:25:37 -0600243
Simon Glassffded752019-07-08 14:25:46 -0600244 # These should not be set in input files, but are set in an FDT map,
245 # which is also read by this code.
246 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
247 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
248
Simon Glassbf7fd502016-11-25 20:15:51 -0700249 self.align = fdt_util.GetInt(self._node, 'align')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700250 if tools.not_power_of_two(self.align):
Simon Glassbf7fd502016-11-25 20:15:51 -0700251 raise ValueError("Node '%s': Alignment %s must be a power of two" %
252 (self._node.path, self.align))
Simon Glass5ff9fed2021-03-21 18:24:33 +1300253 if self.section and self.align is None:
254 self.align = self.section.align_default
Simon Glassbf7fd502016-11-25 20:15:51 -0700255 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
256 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
257 self.align_size = fdt_util.GetInt(self._node, 'align-size')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700258 if tools.not_power_of_two(self.align_size):
Simon Glass8beb11e2019-07-08 14:25:47 -0600259 self.Raise("Alignment size %s must be a power of two" %
260 self.align_size)
Simon Glassbf7fd502016-11-25 20:15:51 -0700261 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glass3ab95982018-08-01 15:22:37 -0600262 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassba64a0b2018-09-14 04:57:29 -0600263 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
Simon Glassb2381432020-09-06 10:39:09 -0600264 self.missing_msg = fdt_util.GetString(self._node, 'missing-msg')
Simon Glassbf7fd502016-11-25 20:15:51 -0700265
Simon Glass87c96292020-10-26 17:40:06 -0600266 # This is only supported by blobs and sections at present
267 self.compress = fdt_util.GetString(self._node, 'compress', 'none')
268
Simon Glass6c234bf2018-09-14 04:57:18 -0600269 def GetDefaultFilename(self):
270 return None
271
Simon Glassa8adb6d2019-07-20 12:23:28 -0600272 def GetFdts(self):
273 """Get the device trees used by this entry
Simon Glass539aece2018-09-14 04:57:22 -0600274
275 Returns:
Simon Glassa8adb6d2019-07-20 12:23:28 -0600276 Empty dict, if this entry is not a .dtb, otherwise:
277 Dict:
278 key: Filename from this entry (without the path)
Simon Glass4bdd3002019-07-20 12:23:31 -0600279 value: Tuple:
Simon Glassadb67bb2021-03-18 20:25:02 +1300280 Entry object for this dtb
Simon Glass4bdd3002019-07-20 12:23:31 -0600281 Filename of file containing this dtb
Simon Glass539aece2018-09-14 04:57:22 -0600282 """
Simon Glassa8adb6d2019-07-20 12:23:28 -0600283 return {}
Simon Glass539aece2018-09-14 04:57:22 -0600284
Simon Glass0a98b282018-09-14 04:57:28 -0600285 def ExpandEntries(self):
Simon Glassa01d1a22021-03-18 20:24:52 +1300286 """Expand out entries which produce other entries
287
288 Some entries generate subnodes automatically, from which sub-entries
289 are then created. This method allows those to be added to the binman
290 definition for the current image. An entry which implements this method
291 should call state.AddSubnode() to add a subnode and can add properties
292 with state.AddString(), etc.
293
294 An example is 'files', which produces a section containing a list of
295 files.
296 """
Simon Glass0a98b282018-09-14 04:57:28 -0600297 pass
298
Simon Glassa9fad072020-10-26 17:40:17 -0600299 def AddMissingProperties(self, have_image_pos):
300 """Add new properties to the device tree as needed for this entry
301
302 Args:
303 have_image_pos: True if this entry has an image position. This can
304 be False if its parent section is compressed, since compression
305 groups all entries together into a compressed block of data,
306 obscuring the start of each individual child entry
307 """
308 for prop in ['offset', 'size']:
Simon Glass078ab1a2018-07-06 10:27:41 -0600309 if not prop in self._node.props:
Simon Glassf46621d2018-09-14 04:57:21 -0600310 state.AddZeroProp(self._node, prop)
Simon Glassa9fad072020-10-26 17:40:17 -0600311 if have_image_pos and 'image-pos' not in self._node.props:
312 state.AddZeroProp(self._node, 'image-pos')
Simon Glass12bb1a92019-07-20 12:23:51 -0600313 if self.GetImage().allow_repack:
314 if self.orig_offset is not None:
315 state.AddZeroProp(self._node, 'orig-offset', True)
316 if self.orig_size is not None:
317 state.AddZeroProp(self._node, 'orig-size', True)
318
Simon Glass8287ee82019-07-08 14:25:30 -0600319 if self.compress != 'none':
320 state.AddZeroProp(self._node, 'uncomp-size')
Alper Nebi Yasakee813c82022-02-09 22:02:35 +0300321
322 if self.update_hash:
323 err = state.CheckAddHashProp(self._node)
324 if err:
325 self.Raise(err)
Simon Glass078ab1a2018-07-06 10:27:41 -0600326
327 def SetCalculatedProperties(self):
328 """Set the value of device-tree properties calculated by binman"""
Simon Glassf46621d2018-09-14 04:57:21 -0600329 state.SetInt(self._node, 'offset', self.offset)
330 state.SetInt(self._node, 'size', self.size)
Simon Glass8beb11e2019-07-08 14:25:47 -0600331 base = self.section.GetRootSkipAtStart() if self.section else 0
Simon Glassa9fad072020-10-26 17:40:17 -0600332 if self.image_pos is not None:
Simon Glass08594d42020-11-02 12:55:44 -0700333 state.SetInt(self._node, 'image-pos', self.image_pos - base)
Simon Glass12bb1a92019-07-20 12:23:51 -0600334 if self.GetImage().allow_repack:
335 if self.orig_offset is not None:
336 state.SetInt(self._node, 'orig-offset', self.orig_offset, True)
337 if self.orig_size is not None:
338 state.SetInt(self._node, 'orig-size', self.orig_size, True)
Simon Glass8287ee82019-07-08 14:25:30 -0600339 if self.uncomp_size is not None:
340 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
Alper Nebi Yasakee813c82022-02-09 22:02:35 +0300341
342 if self.update_hash:
343 state.CheckSetHashValue(self._node, self.GetData)
Simon Glass078ab1a2018-07-06 10:27:41 -0600344
Simon Glassecab8972018-07-06 10:27:40 -0600345 def ProcessFdt(self, fdt):
Simon Glass6ed45ba2018-09-14 04:57:24 -0600346 """Allow entries to adjust the device tree
347
348 Some entries need to adjust the device tree for their purposes. This
349 may involve adding or deleting properties.
350
351 Returns:
352 True if processing is complete
353 False if processing could not be completed due to a dependency.
354 This will cause the entry to be retried after others have been
355 called
356 """
Simon Glassecab8972018-07-06 10:27:40 -0600357 return True
358
Simon Glassc8d48ef2018-06-01 09:38:21 -0600359 def SetPrefix(self, prefix):
360 """Set the name prefix for a node
361
362 Args:
363 prefix: Prefix to set, or '' to not use a prefix
364 """
365 if prefix:
366 self.name = prefix + self.name
367
Simon Glass5c890232018-07-06 10:27:19 -0600368 def SetContents(self, data):
369 """Set the contents of an entry
370
371 This sets both the data and content_size properties
372
373 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600374 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600375 """
376 self.data = data
377 self.contents_size = len(self.data)
378
379 def ProcessContentsUpdate(self, data):
Simon Glass5b463fc2019-07-08 14:25:33 -0600380 """Update the contents of an entry, after the size is fixed
Simon Glass5c890232018-07-06 10:27:19 -0600381
Simon Glassa0dcaf22019-07-08 14:25:35 -0600382 This checks that the new data is the same size as the old. If the size
383 has changed, this triggers a re-run of the packing algorithm.
Simon Glass5c890232018-07-06 10:27:19 -0600384
385 Args:
Simon Glass5b463fc2019-07-08 14:25:33 -0600386 data: Data to set to the contents (bytes)
Simon Glass5c890232018-07-06 10:27:19 -0600387
388 Raises:
389 ValueError if the new data size is not the same as the old
390 """
Simon Glassa0dcaf22019-07-08 14:25:35 -0600391 size_ok = True
Simon Glassc52c9e72019-07-08 14:25:37 -0600392 new_size = len(data)
Simon Glass61ec04f2019-07-20 12:23:58 -0600393 if state.AllowEntryExpansion() and new_size > self.contents_size:
394 # self.data will indicate the new size needed
395 size_ok = False
396 elif state.AllowEntryContraction() and new_size < self.contents_size:
397 size_ok = False
398
399 # If not allowed to change, try to deal with it or give up
400 if size_ok:
Simon Glassc52c9e72019-07-08 14:25:37 -0600401 if new_size > self.contents_size:
Simon Glass61ec04f2019-07-20 12:23:58 -0600402 self.Raise('Cannot update entry size from %d to %d' %
403 (self.contents_size, new_size))
404
405 # Don't let the data shrink. Pad it if necessary
406 if size_ok and new_size < self.contents_size:
Simon Glassc1aa66e2022-01-29 14:14:04 -0700407 data += tools.get_bytes(0, self.contents_size - new_size)
Simon Glass61ec04f2019-07-20 12:23:58 -0600408
409 if not size_ok:
Simon Glassf3385a52022-01-29 14:14:15 -0700410 tout.debug("Entry '%s' size change from %s to %s" % (
Simon Glassc1aa66e2022-01-29 14:14:04 -0700411 self._node.path, to_hex(self.contents_size),
412 to_hex(new_size)))
Simon Glass5c890232018-07-06 10:27:19 -0600413 self.SetContents(data)
Simon Glassa0dcaf22019-07-08 14:25:35 -0600414 return size_ok
Simon Glass5c890232018-07-06 10:27:19 -0600415
Simon Glassbf7fd502016-11-25 20:15:51 -0700416 def ObtainContents(self):
417 """Figure out the contents of an entry.
418
419 Returns:
420 True if the contents were found, False if another call is needed
421 after the other entries are processed.
422 """
423 # No contents by default: subclasses can implement this
424 return True
425
Simon Glassc52c9e72019-07-08 14:25:37 -0600426 def ResetForPack(self):
427 """Reset offset/size fields so that packing can be done again"""
Simon Glass9f297b02019-07-20 12:23:36 -0600428 self.Detail('ResetForPack: offset %s->%s, size %s->%s' %
Simon Glassc1aa66e2022-01-29 14:14:04 -0700429 (to_hex(self.offset), to_hex(self.orig_offset),
430 to_hex(self.size), to_hex(self.orig_size)))
Simon Glass9a5d3dc2019-10-31 07:43:02 -0600431 self.pre_reset_size = self.size
Simon Glassc52c9e72019-07-08 14:25:37 -0600432 self.offset = self.orig_offset
433 self.size = self.orig_size
434
Simon Glass3ab95982018-08-01 15:22:37 -0600435 def Pack(self, offset):
Simon Glass25ac0e62018-06-01 09:38:14 -0600436 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700437
438 Most of the time the entries are not fully specified. There may be
439 an alignment but no size. In that case we take the size from the
440 contents of the entry.
441
Simon Glass3ab95982018-08-01 15:22:37 -0600442 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glassbf7fd502016-11-25 20:15:51 -0700443
Simon Glass3ab95982018-08-01 15:22:37 -0600444 Once this function is complete, both the offset and size of the
Simon Glassbf7fd502016-11-25 20:15:51 -0700445 entry will be know.
446
447 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600448 Current section offset pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700449
450 Returns:
Simon Glass3ab95982018-08-01 15:22:37 -0600451 New section offset pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700452 """
Simon Glass9f297b02019-07-20 12:23:36 -0600453 self.Detail('Packing: offset=%s, size=%s, content_size=%x' %
Simon Glassc1aa66e2022-01-29 14:14:04 -0700454 (to_hex(self.offset), to_hex(self.size),
Simon Glass9f297b02019-07-20 12:23:36 -0600455 self.contents_size))
Simon Glass3ab95982018-08-01 15:22:37 -0600456 if self.offset is None:
457 if self.offset_unset:
458 self.Raise('No offset set with offset-unset: should another '
459 'entry provide this correct offset?')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700460 self.offset = tools.align(offset, self.align)
Simon Glassbf7fd502016-11-25 20:15:51 -0700461 needed = self.pad_before + self.contents_size + self.pad_after
Simon Glassc1aa66e2022-01-29 14:14:04 -0700462 needed = tools.align(needed, self.align_size)
Simon Glassbf7fd502016-11-25 20:15:51 -0700463 size = self.size
464 if not size:
465 size = needed
Simon Glass3ab95982018-08-01 15:22:37 -0600466 new_offset = self.offset + size
Simon Glassc1aa66e2022-01-29 14:14:04 -0700467 aligned_offset = tools.align(new_offset, self.align_end)
Simon Glass3ab95982018-08-01 15:22:37 -0600468 if aligned_offset != new_offset:
469 size = aligned_offset - self.offset
470 new_offset = aligned_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700471
472 if not self.size:
473 self.size = size
474
475 if self.size < needed:
476 self.Raise("Entry contents size is %#x (%d) but entry size is "
477 "%#x (%d)" % (needed, needed, self.size, self.size))
478 # Check that the alignment is correct. It could be wrong if the
Simon Glass3ab95982018-08-01 15:22:37 -0600479 # and offset or size values were provided (i.e. not calculated), but
Simon Glassbf7fd502016-11-25 20:15:51 -0700480 # conflict with the provided alignment values
Simon Glassc1aa66e2022-01-29 14:14:04 -0700481 if self.size != tools.align(self.size, self.align_size):
Simon Glassbf7fd502016-11-25 20:15:51 -0700482 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
483 (self.size, self.size, self.align_size, self.align_size))
Simon Glassc1aa66e2022-01-29 14:14:04 -0700484 if self.offset != tools.align(self.offset, self.align):
Simon Glass3ab95982018-08-01 15:22:37 -0600485 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
486 (self.offset, self.offset, self.align, self.align))
Simon Glass9f297b02019-07-20 12:23:36 -0600487 self.Detail(' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' %
488 (self.offset, self.size, self.contents_size, new_offset))
Simon Glassbf7fd502016-11-25 20:15:51 -0700489
Simon Glass3ab95982018-08-01 15:22:37 -0600490 return new_offset
Simon Glassbf7fd502016-11-25 20:15:51 -0700491
492 def Raise(self, msg):
493 """Convenience function to raise an error referencing a node"""
494 raise ValueError("Node '%s': %s" % (self._node.path, msg))
495
Simon Glass189f2912021-03-21 18:24:31 +1300496 def Info(self, msg):
497 """Convenience function to log info referencing a node"""
498 tag = "Info '%s'" % self._node.path
Simon Glassf3385a52022-01-29 14:14:15 -0700499 tout.detail('%30s: %s' % (tag, msg))
Simon Glass189f2912021-03-21 18:24:31 +1300500
Simon Glass9f297b02019-07-20 12:23:36 -0600501 def Detail(self, msg):
502 """Convenience function to log detail referencing a node"""
503 tag = "Node '%s'" % self._node.path
Simon Glassf3385a52022-01-29 14:14:15 -0700504 tout.detail('%30s: %s' % (tag, msg))
Simon Glass9f297b02019-07-20 12:23:36 -0600505
Simon Glass53af22a2018-07-17 13:25:32 -0600506 def GetEntryArgsOrProps(self, props, required=False):
507 """Return the values of a set of properties
508
509 Args:
510 props: List of EntryArg objects
511
512 Raises:
513 ValueError if a property is not found
514 """
515 values = []
516 missing = []
517 for prop in props:
518 python_prop = prop.name.replace('-', '_')
519 if hasattr(self, python_prop):
520 value = getattr(self, python_prop)
521 else:
522 value = None
523 if value is None:
524 value = self.GetArg(prop.name, prop.datatype)
525 if value is None and required:
526 missing.append(prop.name)
527 values.append(value)
528 if missing:
Simon Glass939d1062021-01-06 21:35:16 -0700529 self.GetImage().MissingArgs(self, missing)
Simon Glass53af22a2018-07-17 13:25:32 -0600530 return values
531
Simon Glassbf7fd502016-11-25 20:15:51 -0700532 def GetPath(self):
533 """Get the path of a node
534
535 Returns:
536 Full path of the node for this entry
537 """
538 return self._node.path
539
Simon Glass631f7522021-03-21 18:24:32 +1300540 def GetData(self, required=True):
Simon Glass63e7ba62020-10-26 17:40:16 -0600541 """Get the contents of an entry
542
Simon Glass631f7522021-03-21 18:24:32 +1300543 Args:
544 required: True if the data must be present, False if it is OK to
545 return None
546
Simon Glass63e7ba62020-10-26 17:40:16 -0600547 Returns:
548 bytes content of the entry, excluding any padding. If the entry is
549 compressed, the compressed data is returned
550 """
Simon Glassc1aa66e2022-01-29 14:14:04 -0700551 self.Detail('GetData: size %s' % to_hex_size(self.data))
Simon Glassbf7fd502016-11-25 20:15:51 -0700552 return self.data
553
Simon Glass271a0832020-11-02 12:55:43 -0700554 def GetPaddedData(self, data=None):
555 """Get the data for an entry including any padding
556
557 Gets the entry data and uses its section's pad-byte value to add padding
558 before and after as defined by the pad-before and pad-after properties.
559
560 This does not consider alignment.
561
562 Returns:
563 Contents of the entry along with any pad bytes before and
564 after it (bytes)
565 """
566 if data is None:
567 data = self.GetData()
568 return self.section.GetPaddedDataForEntry(self, data)
569
Simon Glass3ab95982018-08-01 15:22:37 -0600570 def GetOffsets(self):
Simon Glassed7dd5e2019-07-08 13:18:30 -0600571 """Get the offsets for siblings
572
573 Some entry types can contain information about the position or size of
574 other entries. An example of this is the Intel Flash Descriptor, which
575 knows where the Intel Management Engine section should go.
576
577 If this entry knows about the position of other entries, it can specify
578 this by returning values here
579
580 Returns:
581 Dict:
582 key: Entry type
583 value: List containing position and size of the given entry
Simon Glasscf549042019-07-08 13:18:39 -0600584 type. Either can be None if not known
Simon Glassed7dd5e2019-07-08 13:18:30 -0600585 """
Simon Glassbf7fd502016-11-25 20:15:51 -0700586 return {}
587
Simon Glasscf549042019-07-08 13:18:39 -0600588 def SetOffsetSize(self, offset, size):
589 """Set the offset and/or size of an entry
590
591 Args:
592 offset: New offset, or None to leave alone
593 size: New size, or None to leave alone
594 """
595 if offset is not None:
596 self.offset = offset
597 if size is not None:
598 self.size = size
Simon Glassbf7fd502016-11-25 20:15:51 -0700599
Simon Glassdbf6be92018-08-01 15:22:42 -0600600 def SetImagePos(self, image_pos):
601 """Set the position in the image
602
603 Args:
604 image_pos: Position of this entry in the image
605 """
606 self.image_pos = image_pos + self.offset
607
Simon Glassbf7fd502016-11-25 20:15:51 -0700608 def ProcessContents(self):
Simon Glassa0dcaf22019-07-08 14:25:35 -0600609 """Do any post-packing updates of entry contents
610
611 This function should call ProcessContentsUpdate() to update the entry
612 contents, if necessary, returning its return value here.
613
614 Args:
615 data: Data to set to the contents (bytes)
616
617 Returns:
618 True if the new data size is OK, False if expansion is needed
619
620 Raises:
621 ValueError if the new data size is not the same as the old and
622 state.AllowEntryExpansion() is False
623 """
624 return True
Simon Glass19790632017-11-13 18:55:01 -0700625
Simon Glassf55382b2018-06-01 09:38:13 -0600626 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700627 """Write symbol values into binary files for access at run time
628
629 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600630 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700631 """
632 pass
Simon Glass18546952018-06-01 09:38:16 -0600633
Simon Glass6ddd6112020-10-26 17:40:18 -0600634 def CheckEntries(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600635 """Check that the entry offsets are correct
Simon Glass18546952018-06-01 09:38:16 -0600636
Simon Glass3ab95982018-08-01 15:22:37 -0600637 This is used for entries which have extra offset requirements (other
Simon Glass18546952018-06-01 09:38:16 -0600638 than having to be fully inside their section). Sub-classes can implement
639 this function and raise if there is a problem.
640 """
641 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600642
Simon Glass8122f392018-07-17 13:25:28 -0600643 @staticmethod
Simon Glass163ed6c2018-09-14 04:57:36 -0600644 def GetStr(value):
645 if value is None:
646 return '<none> '
647 return '%08x' % value
648
649 @staticmethod
Simon Glass1be70d22018-07-17 13:25:49 -0600650 def WriteMapLine(fd, indent, name, offset, size, image_pos):
Simon Glass163ed6c2018-09-14 04:57:36 -0600651 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
652 Entry.GetStr(offset), Entry.GetStr(size),
653 name), file=fd)
Simon Glass8122f392018-07-17 13:25:28 -0600654
Simon Glass3b0c3822018-06-01 09:38:20 -0600655 def WriteMap(self, fd, indent):
656 """Write a map of the entry to a .map file
657
658 Args:
659 fd: File to write the map to
660 indent: Curent indent level of map (0=none, 1=one level, etc.)
661 """
Simon Glass1be70d22018-07-17 13:25:49 -0600662 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
663 self.image_pos)
Simon Glass53af22a2018-07-17 13:25:32 -0600664
Simon Glass11e36cc2018-07-17 13:25:38 -0600665 def GetEntries(self):
666 """Return a list of entries contained by this entry
667
668 Returns:
669 List of entries, or None if none. A normal entry has no entries
670 within it so will return None
671 """
672 return None
673
Simon Glass53af22a2018-07-17 13:25:32 -0600674 def GetArg(self, name, datatype=str):
675 """Get the value of an entry argument or device-tree-node property
676
677 Some node properties can be provided as arguments to binman. First check
678 the entry arguments, and fall back to the device tree if not found
679
680 Args:
681 name: Argument name
682 datatype: Data type (str or int)
683
684 Returns:
685 Value of argument as a string or int, or None if no value
686
687 Raises:
688 ValueError if the argument cannot be converted to in
689 """
Simon Glassc55a50f2018-09-14 04:57:19 -0600690 value = state.GetEntryArg(name)
Simon Glass53af22a2018-07-17 13:25:32 -0600691 if value is not None:
692 if datatype == int:
693 try:
694 value = int(value)
695 except ValueError:
696 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
697 (name, value))
698 elif datatype == str:
699 pass
700 else:
701 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
702 datatype)
703 else:
704 value = fdt_util.GetDatatype(self._node, name, datatype)
705 return value
Simon Glassfd8d1f72018-07-17 13:25:36 -0600706
707 @staticmethod
708 def WriteDocs(modules, test_missing=None):
709 """Write out documentation about the various entry types to stdout
710
711 Args:
712 modules: List of modules to include
713 test_missing: Used for testing. This is a module to report
714 as missing
715 """
716 print('''Binman Entry Documentation
717===========================
718
719This file describes the entry types supported by binman. These entry types can
720be placed in an image one by one to build up a final firmware image. It is
721fairly easy to create new entry types. Just add a new file to the 'etype'
722directory. You can use the existing entries as examples.
723
724Note that some entries are subclasses of others, using and extending their
725features to produce new behaviours.
726
727
728''')
729 modules = sorted(modules)
730
731 # Don't show the test entry
732 if '_testing' in modules:
733 modules.remove('_testing')
734 missing = []
735 for name in modules:
Simon Glassb35fb172021-03-18 20:25:04 +1300736 module = Entry.Lookup('WriteDocs', name, False)
Simon Glassfd8d1f72018-07-17 13:25:36 -0600737 docs = getattr(module, '__doc__')
738 if test_missing == name:
739 docs = None
740 if docs:
741 lines = docs.splitlines()
742 first_line = lines[0]
743 rest = [line[4:] for line in lines[1:]]
744 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
745 print(hdr)
746 print('-' * len(hdr))
747 print('\n'.join(rest))
748 print()
749 print()
750 else:
751 missing.append(name)
752
753 if missing:
754 raise ValueError('Documentation is missing for modules: %s' %
755 ', '.join(missing))
Simon Glassa326b492018-09-14 04:57:11 -0600756
757 def GetUniqueName(self):
758 """Get a unique name for a node
759
760 Returns:
761 String containing a unique name for a node, consisting of the name
762 of all ancestors (starting from within the 'binman' node) separated
763 by a dot ('.'). This can be useful for generating unique filesnames
764 in the output directory.
765 """
766 name = self.name
767 node = self._node
768 while node.parent:
769 node = node.parent
770 if node.name == 'binman':
771 break
772 name = '%s.%s' % (node.name, name)
773 return name
Simon Glassba64a0b2018-09-14 04:57:29 -0600774
775 def ExpandToLimit(self, limit):
776 """Expand an entry so that it ends at the given offset limit"""
777 if self.offset + self.size < limit:
778 self.size = limit - self.offset
779 # Request the contents again, since changing the size requires that
780 # the data grows. This should not fail, but check it to be sure.
781 if not self.ObtainContents():
782 self.Raise('Cannot obtain contents when expanding entry')
Simon Glassfa1c9372019-07-08 13:18:38 -0600783
784 def HasSibling(self, name):
785 """Check if there is a sibling of a given name
786
787 Returns:
788 True if there is an entry with this name in the the same section,
789 else False
790 """
791 return name in self.section.GetEntries()
Simon Glasscf228942019-07-08 14:25:28 -0600792
793 def GetSiblingImagePos(self, name):
794 """Return the image position of the given sibling
795
796 Returns:
797 Image position of sibling, or None if the sibling has no position,
798 or False if there is no such sibling
799 """
800 if not self.HasSibling(name):
801 return False
802 return self.section.GetEntries()[name].image_pos
Simon Glass41b8ba02019-07-08 14:25:43 -0600803
804 @staticmethod
805 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
806 uncomp_size, offset, entry):
807 """Add a new entry to the entries list
808
809 Args:
810 entries: List (of EntryInfo objects) to add to
811 indent: Current indent level to add to list
812 name: Entry name (string)
813 etype: Entry type (string)
814 size: Entry size in bytes (int)
815 image_pos: Position within image in bytes (int)
816 uncomp_size: Uncompressed size if the entry uses compression, else
817 None
818 offset: Entry offset within parent in bytes (int)
819 entry: Entry object
820 """
821 entries.append(EntryInfo(indent, name, etype, size, image_pos,
822 uncomp_size, offset, entry))
823
824 def ListEntries(self, entries, indent):
825 """Add files in this entry to the list of entries
826
827 This can be overridden by subclasses which need different behaviour.
828
829 Args:
830 entries: List (of EntryInfo objects) to add to
831 indent: Current indent level to add to list
832 """
833 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
834 self.image_pos, self.uncomp_size, self.offset, self)
Simon Glassf667e452019-07-08 14:25:50 -0600835
Simon Glass943bf782021-11-23 21:09:50 -0700836 def ReadData(self, decomp=True, alt_format=None):
Simon Glassf667e452019-07-08 14:25:50 -0600837 """Read the data for an entry from the image
838
839 This is used when the image has been read in and we want to extract the
840 data for a particular entry from that image.
841
842 Args:
843 decomp: True to decompress any compressed data before returning it;
844 False to return the raw, uncompressed data
845
846 Returns:
847 Entry data (bytes)
848 """
849 # Use True here so that we get an uncompressed section to work from,
850 # although compressed sections are currently not supported
Simon Glassf3385a52022-01-29 14:14:15 -0700851 tout.debug("ReadChildData section '%s', entry '%s'" %
Simon Glass2d553c02019-09-25 08:56:21 -0600852 (self.section.GetPath(), self.GetPath()))
Simon Glass943bf782021-11-23 21:09:50 -0700853 data = self.section.ReadChildData(self, decomp, alt_format)
Simon Glassa9cd39e2019-07-20 12:24:04 -0600854 return data
Simon Glassd5079332019-07-20 12:23:41 -0600855
Simon Glass943bf782021-11-23 21:09:50 -0700856 def ReadChildData(self, child, decomp=True, alt_format=None):
Simon Glass2d553c02019-09-25 08:56:21 -0600857 """Read the data for a particular child entry
Simon Glass4e185e82019-09-25 08:56:20 -0600858
859 This reads data from the parent and extracts the piece that relates to
860 the given child.
861
862 Args:
Simon Glass943bf782021-11-23 21:09:50 -0700863 child (Entry): Child entry to read data for (must be valid)
864 decomp (bool): True to decompress any compressed data before
865 returning it; False to return the raw, uncompressed data
866 alt_format (str): Alternative format to read in, or None
Simon Glass4e185e82019-09-25 08:56:20 -0600867
868 Returns:
869 Data for the child (bytes)
870 """
871 pass
872
Simon Glassd5079332019-07-20 12:23:41 -0600873 def LoadData(self, decomp=True):
874 data = self.ReadData(decomp)
Simon Glass10f9d002019-07-20 12:23:50 -0600875 self.contents_size = len(data)
Simon Glassd5079332019-07-20 12:23:41 -0600876 self.ProcessContentsUpdate(data)
877 self.Detail('Loaded data size %x' % len(data))
Simon Glassc5ad04b2019-07-20 12:23:46 -0600878
Simon Glass943bf782021-11-23 21:09:50 -0700879 def GetAltFormat(self, data, alt_format):
880 """Read the data for an extry in an alternative format
881
882 Supported formats are list in the documentation for each entry. An
883 example is fdtmap which provides .
884
885 Args:
886 data (bytes): Data to convert (this should have been produced by the
887 entry)
888 alt_format (str): Format to use
889
890 """
891 pass
892
Simon Glassc5ad04b2019-07-20 12:23:46 -0600893 def GetImage(self):
894 """Get the image containing this entry
895
896 Returns:
897 Image object containing this entry
898 """
899 return self.section.GetImage()
Simon Glass10f9d002019-07-20 12:23:50 -0600900
901 def WriteData(self, data, decomp=True):
902 """Write the data to an entry in the image
903
904 This is used when the image has been read in and we want to replace the
905 data for a particular entry in that image.
906
907 The image must be re-packed and written out afterwards.
908
909 Args:
910 data: Data to replace it with
911 decomp: True to compress the data if needed, False if data is
912 already compressed so should be used as is
913
914 Returns:
915 True if the data did not result in a resize of this entry, False if
916 the entry must be resized
917 """
Simon Glass9a5d3dc2019-10-31 07:43:02 -0600918 if self.size is not None:
919 self.contents_size = self.size
920 else:
921 self.contents_size = self.pre_reset_size
Simon Glass10f9d002019-07-20 12:23:50 -0600922 ok = self.ProcessContentsUpdate(data)
923 self.Detail('WriteData: size=%x, ok=%s' % (len(data), ok))
Simon Glass7210c892019-07-20 12:24:05 -0600924 section_ok = self.section.WriteChildData(self)
925 return ok and section_ok
926
927 def WriteChildData(self, child):
928 """Handle writing the data in a child entry
929
930 This should be called on the child's parent section after the child's
Simon Glass557693e2021-11-23 11:03:44 -0700931 data has been updated. It should update any data structures needed to
932 validate that the update is successful.
Simon Glass7210c892019-07-20 12:24:05 -0600933
934 This base-class implementation does nothing, since the base Entry object
935 does not have any children.
936
937 Args:
938 child: Child Entry that was written
939
940 Returns:
941 True if the section could be updated successfully, False if the
Simon Glass557693e2021-11-23 11:03:44 -0700942 data is such that the section could not update
Simon Glass7210c892019-07-20 12:24:05 -0600943 """
944 return True
Simon Glasseba1f0c2019-07-20 12:23:55 -0600945
946 def GetSiblingOrder(self):
947 """Get the relative order of an entry amoung its siblings
948
949 Returns:
950 'start' if this entry is first among siblings, 'end' if last,
951 otherwise None
952 """
953 entries = list(self.section.GetEntries().values())
954 if entries:
955 if self == entries[0]:
956 return 'start'
957 elif self == entries[-1]:
958 return 'end'
959 return 'middle'
Simon Glass4f9f1052020-07-09 18:39:38 -0600960
961 def SetAllowMissing(self, allow_missing):
962 """Set whether a section allows missing external blobs
963
964 Args:
965 allow_missing: True if allowed, False if not allowed
966 """
967 # This is meaningless for anything other than sections
968 pass
Simon Glassb1cca952020-07-09 18:39:40 -0600969
Heiko Thierya89c8f22022-01-06 11:49:41 +0100970 def SetAllowFakeBlob(self, allow_fake):
971 """Set whether a section allows to create a fake blob
972
973 Args:
974 allow_fake: True if allowed, False if not allowed
975 """
Simon Glassf4590e02022-01-09 20:13:46 -0700976 self.allow_fake = allow_fake
Heiko Thierya89c8f22022-01-06 11:49:41 +0100977
Simon Glassb1cca952020-07-09 18:39:40 -0600978 def CheckMissing(self, missing_list):
979 """Check if any entries in this section have missing external blobs
980
981 If there are missing blobs, the entries are added to the list
982
983 Args:
984 missing_list: List of Entry objects to be added to
985 """
986 if self.missing:
987 missing_list.append(self)
Simon Glass87958982020-09-01 05:13:57 -0600988
Simon Glass790ba9f2022-01-12 13:10:36 -0700989 def check_fake_fname(self, fname):
990 """If the file is missing and the entry allows fake blobs, fake it
991
992 Sets self.faked to True if faked
993
994 Args:
995 fname (str): Filename to check
996
997 Returns:
998 fname (str): Filename of faked file
999 """
1000 if self.allow_fake and not pathlib.Path(fname).is_file():
Simon Glassc1aa66e2022-01-29 14:14:04 -07001001 outfname = tools.get_output_filename(os.path.basename(fname))
Simon Glass790ba9f2022-01-12 13:10:36 -07001002 with open(outfname, "wb") as out:
1003 out.truncate(1024)
1004 self.faked = True
1005 return outfname
1006 return fname
1007
Heiko Thierya89c8f22022-01-06 11:49:41 +01001008 def CheckFakedBlobs(self, faked_blobs_list):
1009 """Check if any entries in this section have faked external blobs
1010
1011 If there are faked blobs, the entries are added to the list
1012
1013 Args:
1014 fake_blobs_list: List of Entry objects to be added to
1015 """
1016 # This is meaningless for anything other than blobs
1017 pass
1018
Simon Glass87958982020-09-01 05:13:57 -06001019 def GetAllowMissing(self):
1020 """Get whether a section allows missing external blobs
1021
1022 Returns:
1023 True if allowed, False if not allowed
1024 """
1025 return self.allow_missing
Simon Glassb2381432020-09-06 10:39:09 -06001026
Simon Glass4f9ee832022-01-09 20:14:09 -07001027 def record_missing_bintool(self, bintool):
1028 """Record a missing bintool that was needed to produce this entry
1029
1030 Args:
1031 bintool (Bintool): Bintool that was missing
1032 """
1033 self.missing_bintools.append(bintool)
1034
1035 def check_missing_bintools(self, missing_list):
1036 """Check if any entries in this section have missing bintools
1037
1038 If there are missing bintools, these are added to the list
1039
1040 Args:
1041 missing_list: List of Bintool objects to be added to
1042 """
1043 missing_list += self.missing_bintools
1044
Simon Glassb2381432020-09-06 10:39:09 -06001045 def GetHelpTags(self):
1046 """Get the tags use for missing-blob help
1047
1048 Returns:
1049 list of possible tags, most desirable first
1050 """
1051 return list(filter(None, [self.missing_msg, self.name, self.etype]))
Simon Glass87c96292020-10-26 17:40:06 -06001052
1053 def CompressData(self, indata):
1054 """Compress data according to the entry's compression method
1055
1056 Args:
1057 indata: Data to compress
1058
1059 Returns:
1060 Compressed data (first word is the compressed size)
1061 """
Simon Glass97c3e9a2020-10-26 17:40:15 -06001062 self.uncomp_data = indata
Simon Glass87c96292020-10-26 17:40:06 -06001063 if self.compress != 'none':
1064 self.uncomp_size = len(indata)
Simon Glass0d1e95a2022-01-09 20:14:04 -07001065 data = comp_util.compress(indata, self.compress)
Simon Glass87c96292020-10-26 17:40:06 -06001066 return data
Simon Glassb35fb172021-03-18 20:25:04 +13001067
1068 @classmethod
1069 def UseExpanded(cls, node, etype, new_etype):
1070 """Check whether to use an expanded entry type
1071
1072 This is called by Entry.Create() when it finds an expanded version of
1073 an entry type (e.g. 'u-boot-expanded'). If this method returns True then
1074 it will be used (e.g. in place of 'u-boot'). If it returns False, it is
1075 ignored.
1076
1077 Args:
1078 node: Node object containing information about the entry to
1079 create
1080 etype: Original entry type being used
1081 new_etype: New entry type proposed
1082
1083 Returns:
1084 True to use this entry type, False to use the original one
1085 """
Simon Glassf3385a52022-01-29 14:14:15 -07001086 tout.info("Node '%s': etype '%s': %s selected" %
Simon Glassb35fb172021-03-18 20:25:04 +13001087 (node.path, etype, new_etype))
1088 return True
Simon Glass943bf782021-11-23 21:09:50 -07001089
1090 def CheckAltFormats(self, alt_formats):
1091 """Add any alternative formats supported by this entry type
1092
1093 Args:
1094 alt_formats (dict): Dict to add alt_formats to:
1095 key: Name of alt format
1096 value: Help text
1097 """
1098 pass
Simon Glass386c63c2022-01-09 20:13:50 -07001099
1100 def AddBintools(self, tools):
1101 """Add the bintools used by this entry type
1102
1103 Args:
1104 tools (dict of Bintool):
1105 """
1106 pass
1107
1108 @classmethod
1109 def AddBintool(self, tools, name):
1110 """Add a new bintool to the tools used by this etype
1111
1112 Args:
1113 name: Name of the tool
1114 """
1115 btool = bintool.Bintool.create(name)
1116 tools[name] = btool
1117 return btool
Alper Nebi Yasakee813c82022-02-09 22:02:35 +03001118
1119 def SetUpdateHash(self, update_hash):
1120 """Set whether this entry's "hash" subnode should be updated
1121
1122 Args:
1123 update_hash: True if hash should be updated, False if not
1124 """
1125 self.update_hash = update_hash