blob: d159d90e4c52d31b17058d12562b98be1654aae3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3#
Simon Glass2574ef62016-11-25 20:15:51 -07004# Base class for all entries
5#
6
Simon Glass91710b32018-07-17 13:25:32 -06007from collections import namedtuple
Simon Glass7ccca832019-10-31 07:42:59 -06008import importlib
Simon Glass691198c2018-06-01 09:38:15 -06009import os
Simon Glass7a602fd2022-01-12 13:10:36 -070010import pathlib
Simon Glass691198c2018-06-01 09:38:15 -060011import sys
Simon Glass7d3e4072022-08-07 09:46:46 -060012import time
Simon Glass29aa7362018-09-14 04:57:19 -060013
Simon Glass4eae9252022-01-09 20:13:50 -070014from binman import bintool
Simon Glass3ac7d832022-01-09 20:14:03 -070015from binman import comp_util
Simon Glassc585dd42020-04-17 18:09:03 -060016from dtoc import fdt_util
Simon Glassa997ea52020-04-17 18:09:04 -060017from patman import tools
Simon Glass80025522022-01-29 14:14:04 -070018from patman.tools import to_hex, to_hex_size
Simon Glassa997ea52020-04-17 18:09:04 -060019from patman import tout
Simon Glass2574ef62016-11-25 20:15:51 -070020
21modules = {}
22
Simon Glass2a0fa982022-02-11 13:23:21 -070023# This is imported if needed
24state = None
Simon Glass91710b32018-07-17 13:25:32 -060025
26# An argument which can be passed to entries on the command line, in lieu of
27# device-tree properties.
28EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
29
Simon Glass6b156f82019-07-08 14:25:43 -060030# Information about an entry for use when displaying summaries
31EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
32 'image_pos', 'uncomp_size', 'offset',
33 'entry'])
Simon Glass91710b32018-07-17 13:25:32 -060034
Simon Glass2574ef62016-11-25 20:15:51 -070035class Entry(object):
Simon Glassad5a7712018-06-01 09:38:14 -060036 """An Entry in the section
Simon Glass2574ef62016-11-25 20:15:51 -070037
38 An entry corresponds to a single node in the device-tree description
Simon Glassad5a7712018-06-01 09:38:14 -060039 of the section. Each entry ends up being a part of the final section.
Simon Glass2574ef62016-11-25 20:15:51 -070040 Entries can be placed either right next to each other, or with padding
41 between them. The type of the entry determines the data that is in it.
42
43 This class is not used by itself. All entry objects are subclasses of
44 Entry.
45
46 Attributes:
Simon Glass3a9a2b82018-07-17 13:25:28 -060047 section: Section object containing this entry
Simon Glass2574ef62016-11-25 20:15:51 -070048 node: The node that created this entry
Simon Glasse8561af2018-08-01 15:22:37 -060049 offset: Offset of entry within the section, None if not known yet (in
50 which case it will be calculated by Pack())
Simon Glass2574ef62016-11-25 20:15:51 -070051 size: Entry size in bytes, None if not known
Simon Glass1fdb4872019-10-31 07:43:02 -060052 pre_reset_size: size as it was before ResetForPack(). This allows us to
53 keep track of the size we started with and detect size changes
Simon Glassaa2fcf92019-07-08 14:25:30 -060054 uncomp_size: Size of uncompressed data in bytes, if the entry is
55 compressed, else None
Simon Glass2574ef62016-11-25 20:15:51 -070056 contents_size: Size of contents in bytes, 0 by default
Simon Glassafb9caa2020-10-26 17:40:10 -060057 align: Entry start offset alignment relative to the start of the
58 containing section, or None
Simon Glass2574ef62016-11-25 20:15:51 -070059 align_size: Entry size alignment, or None
Simon Glassafb9caa2020-10-26 17:40:10 -060060 align_end: Entry end offset alignment relative to the start of the
61 containing section, or None
Simon Glassd12599d2020-10-26 17:40:09 -060062 pad_before: Number of pad bytes before the contents when it is placed
63 in the containing section, 0 if none. The pad bytes become part of
64 the entry.
65 pad_after: Number of pad bytes after the contents when it is placed in
66 the containing section, 0 if none. The pad bytes become part of
67 the entry.
68 data: Contents of entry (string of bytes). This does not include
Simon Glass789b34402020-10-26 17:40:15 -060069 padding created by pad_before or pad_after. If the entry is
70 compressed, this contains the compressed data.
71 uncomp_data: Original uncompressed data, if this entry is compressed,
72 else None
Simon Glassaa2fcf92019-07-08 14:25:30 -060073 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
Simon Glasse61b6f62019-07-08 14:25:37 -060074 orig_offset: Original offset value read from node
75 orig_size: Original size value read from node
Simon Glassb8f90372020-09-01 05:13:57 -060076 missing: True if this entry is missing its contents
77 allow_missing: Allow children of this entry to be missing (used by
78 subclasses such as Entry_section)
Heiko Thiery6d451362022-01-06 11:49:41 +010079 allow_fake: Allow creating a dummy fake file if the blob file is not
80 available. This is mainly used for testing.
Simon Glassb8f90372020-09-01 05:13:57 -060081 external: True if this entry contains an external binary blob
Simon Glass4eae9252022-01-09 20:13:50 -070082 bintools: Bintools used by this entry (only populated for Image)
Simon Glass66152ce2022-01-09 20:14:09 -070083 missing_bintools: List of missing bintools for this entry
Alper Nebi Yasak1e4ffd82022-02-09 22:02:35 +030084 update_hash: True if this entry's "hash" subnode should be
85 updated with a hash of the entry contents
Simon Glass7d3e4072022-08-07 09:46:46 -060086 fake_fname: Fake filename, if one was created, else None
Simon Glass0cf5bce2022-08-13 11:40:44 -060087 required_props (dict of str): Properties which must be present. This can
88 be added to by subclasses
Simon Glass2574ef62016-11-25 20:15:51 -070089 """
Simon Glass7d3e4072022-08-07 09:46:46 -060090 fake_dir = None
91
Simon Glass2c360cf2019-07-20 12:23:45 -060092 def __init__(self, section, etype, node, name_prefix=''):
Simon Glassb9ba4e02019-08-24 07:22:44 -060093 # Put this here to allow entry-docs and help to work without libfdt
94 global state
Simon Glassc585dd42020-04-17 18:09:03 -060095 from binman import state
Simon Glassb9ba4e02019-08-24 07:22:44 -060096
Simon Glassad5a7712018-06-01 09:38:14 -060097 self.section = section
Simon Glass2574ef62016-11-25 20:15:51 -070098 self.etype = etype
99 self._node = node
Simon Glass3b78d532018-06-01 09:38:21 -0600100 self.name = node and (name_prefix + node.name) or 'none'
Simon Glasse8561af2018-08-01 15:22:37 -0600101 self.offset = None
Simon Glass2574ef62016-11-25 20:15:51 -0700102 self.size = None
Simon Glass1fdb4872019-10-31 07:43:02 -0600103 self.pre_reset_size = None
Simon Glassaa2fcf92019-07-08 14:25:30 -0600104 self.uncomp_size = None
Simon Glass5c350162018-07-17 13:25:47 -0600105 self.data = None
Simon Glass789b34402020-10-26 17:40:15 -0600106 self.uncomp_data = None
Simon Glass2574ef62016-11-25 20:15:51 -0700107 self.contents_size = 0
108 self.align = None
109 self.align_size = None
110 self.align_end = None
111 self.pad_before = 0
112 self.pad_after = 0
Simon Glasse8561af2018-08-01 15:22:37 -0600113 self.offset_unset = False
Simon Glass9dcc8612018-08-01 15:22:42 -0600114 self.image_pos = None
Simon Glassdd156a42022-03-05 20:18:59 -0700115 self.extend_size = False
Simon Glassaa2fcf92019-07-08 14:25:30 -0600116 self.compress = 'none'
Simon Glassa003cd32020-07-09 18:39:40 -0600117 self.missing = False
Heiko Thiery6d451362022-01-06 11:49:41 +0100118 self.faked = False
Simon Glassb8f90372020-09-01 05:13:57 -0600119 self.external = False
120 self.allow_missing = False
Heiko Thiery6d451362022-01-06 11:49:41 +0100121 self.allow_fake = False
Simon Glass4eae9252022-01-09 20:13:50 -0700122 self.bintools = {}
Simon Glass66152ce2022-01-09 20:14:09 -0700123 self.missing_bintools = []
Alper Nebi Yasak1e4ffd82022-02-09 22:02:35 +0300124 self.update_hash = True
Simon Glass7d3e4072022-08-07 09:46:46 -0600125 self.fake_fname = None
Simon Glass0cf5bce2022-08-13 11:40:44 -0600126 self.required_props = []
Simon Glass2574ef62016-11-25 20:15:51 -0700127
128 @staticmethod
Simon Glassb9028bc2021-11-23 21:09:49 -0700129 def FindEntryClass(etype, expanded):
Simon Glass969616c2018-07-17 13:25:36 -0600130 """Look up the entry class for a node.
Simon Glass2574ef62016-11-25 20:15:51 -0700131
132 Args:
Simon Glass969616c2018-07-17 13:25:36 -0600133 node_node: Path name of Node object containing information about
134 the entry to create (used for errors)
135 etype: Entry type to use
Simon Glass2f859412021-03-18 20:25:04 +1300136 expanded: Use the expanded version of etype
Simon Glass2574ef62016-11-25 20:15:51 -0700137
138 Returns:
Simon Glass2f859412021-03-18 20:25:04 +1300139 The entry class object if found, else None if not found and expanded
Simon Glassb9028bc2021-11-23 21:09:49 -0700140 is True, else a tuple:
141 module name that could not be found
142 exception received
Simon Glass2574ef62016-11-25 20:15:51 -0700143 """
Simon Glasse76a3e62018-06-01 09:38:11 -0600144 # Convert something like 'u-boot@0' to 'u_boot' since we are only
145 # interested in the type.
Simon Glass2574ef62016-11-25 20:15:51 -0700146 module_name = etype.replace('-', '_')
Simon Glass2f859412021-03-18 20:25:04 +1300147
Simon Glasse76a3e62018-06-01 09:38:11 -0600148 if '@' in module_name:
149 module_name = module_name.split('@')[0]
Simon Glass2f859412021-03-18 20:25:04 +1300150 if expanded:
151 module_name += '_expanded'
Simon Glass2574ef62016-11-25 20:15:51 -0700152 module = modules.get(module_name)
153
Simon Glass691198c2018-06-01 09:38:15 -0600154 # Also allow entry-type modules to be brought in from the etype directory.
155
Simon Glass2574ef62016-11-25 20:15:51 -0700156 # Import the module if we have not already done so.
157 if not module:
158 try:
Simon Glassc585dd42020-04-17 18:09:03 -0600159 module = importlib.import_module('binman.etype.' + module_name)
Simon Glass969616c2018-07-17 13:25:36 -0600160 except ImportError as e:
Simon Glass2f859412021-03-18 20:25:04 +1300161 if expanded:
162 return None
Simon Glassb9028bc2021-11-23 21:09:49 -0700163 return module_name, e
Simon Glass2574ef62016-11-25 20:15:51 -0700164 modules[module_name] = module
165
Simon Glass969616c2018-07-17 13:25:36 -0600166 # Look up the expected class name
167 return getattr(module, 'Entry_%s' % module_name)
168
169 @staticmethod
Simon Glassb9028bc2021-11-23 21:09:49 -0700170 def Lookup(node_path, etype, expanded, missing_etype=False):
171 """Look up the entry class for a node.
172
173 Args:
174 node_node (str): Path name of Node object containing information
175 about the entry to create (used for errors)
176 etype (str): Entry type to use
177 expanded (bool): Use the expanded version of etype
178 missing_etype (bool): True to default to a blob etype if the
179 requested etype is not found
180
181 Returns:
182 The entry class object if found, else None if not found and expanded
183 is True
184
185 Raise:
186 ValueError if expanded is False and the class is not found
187 """
188 # Convert something like 'u-boot@0' to 'u_boot' since we are only
189 # interested in the type.
190 cls = Entry.FindEntryClass(etype, expanded)
191 if cls is None:
192 return None
193 elif isinstance(cls, tuple):
194 if missing_etype:
195 cls = Entry.FindEntryClass('blob', False)
196 if isinstance(cls, tuple): # This should not fail
197 module_name, e = cls
198 raise ValueError(
199 "Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
200 (etype, node_path, module_name, e))
201 return cls
202
203 @staticmethod
204 def Create(section, node, etype=None, expanded=False, missing_etype=False):
Simon Glass969616c2018-07-17 13:25:36 -0600205 """Create a new entry for a node.
206
207 Args:
Simon Glassb9028bc2021-11-23 21:09:49 -0700208 section (entry_Section): Section object containing this node
209 node (Node): Node object containing information about the entry to
210 create
211 etype (str): Entry type to use, or None to work it out (used for
212 tests)
213 expanded (bool): Use the expanded version of etype
214 missing_etype (bool): True to default to a blob etype if the
215 requested etype is not found
Simon Glass969616c2018-07-17 13:25:36 -0600216
217 Returns:
218 A new Entry object of the correct type (a subclass of Entry)
219 """
220 if not etype:
221 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glassb9028bc2021-11-23 21:09:49 -0700222 obj = Entry.Lookup(node.path, etype, expanded, missing_etype)
Simon Glass2f859412021-03-18 20:25:04 +1300223 if obj and expanded:
224 # Check whether to use the expanded entry
225 new_etype = etype + '-expanded'
Simon Glass7098b7f2021-03-21 18:24:30 +1300226 can_expand = not fdt_util.GetBool(node, 'no-expanded')
227 if can_expand and obj.UseExpanded(node, etype, new_etype):
Simon Glass2f859412021-03-18 20:25:04 +1300228 etype = new_etype
229 else:
230 obj = None
231 if not obj:
Simon Glassb9028bc2021-11-23 21:09:49 -0700232 obj = Entry.Lookup(node.path, etype, False, missing_etype)
Simon Glass969616c2018-07-17 13:25:36 -0600233
Simon Glass2574ef62016-11-25 20:15:51 -0700234 # Call its constructor to get the object we want.
Simon Glassad5a7712018-06-01 09:38:14 -0600235 return obj(section, etype, node)
Simon Glass2574ef62016-11-25 20:15:51 -0700236
237 def ReadNode(self):
238 """Read entry information from the node
239
Simon Glass2c360cf2019-07-20 12:23:45 -0600240 This must be called as the first thing after the Entry is created.
241
Simon Glass2574ef62016-11-25 20:15:51 -0700242 This reads all the fields we recognise from the node, ready for use.
243 """
Simon Glass0cf5bce2022-08-13 11:40:44 -0600244 self.ensure_props()
Simon Glass24b97442018-07-17 13:25:51 -0600245 if 'pos' in self._node.props:
246 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glassdd156a42022-03-05 20:18:59 -0700247 if 'expand-size' in self._node.props:
248 self.Raise("Please use 'extend-size' instead of 'expand-size'")
Simon Glasse8561af2018-08-01 15:22:37 -0600249 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glass2574ef62016-11-25 20:15:51 -0700250 self.size = fdt_util.GetInt(self._node, 'size')
Simon Glassfb30e292019-07-20 12:23:51 -0600251 self.orig_offset = fdt_util.GetInt(self._node, 'orig-offset')
252 self.orig_size = fdt_util.GetInt(self._node, 'orig-size')
253 if self.GetImage().copy_to_orig:
254 self.orig_offset = self.offset
255 self.orig_size = self.size
Simon Glasse61b6f62019-07-08 14:25:37 -0600256
Simon Glassb8424fa2019-07-08 14:25:46 -0600257 # These should not be set in input files, but are set in an FDT map,
258 # which is also read by this code.
259 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
260 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
261
Simon Glass2574ef62016-11-25 20:15:51 -0700262 self.align = fdt_util.GetInt(self._node, 'align')
Simon Glass80025522022-01-29 14:14:04 -0700263 if tools.not_power_of_two(self.align):
Simon Glass2574ef62016-11-25 20:15:51 -0700264 raise ValueError("Node '%s': Alignment %s must be a power of two" %
265 (self._node.path, self.align))
Simon Glassf427c5f2021-03-21 18:24:33 +1300266 if self.section and self.align is None:
267 self.align = self.section.align_default
Simon Glass2574ef62016-11-25 20:15:51 -0700268 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
269 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
270 self.align_size = fdt_util.GetInt(self._node, 'align-size')
Simon Glass80025522022-01-29 14:14:04 -0700271 if tools.not_power_of_two(self.align_size):
Simon Glass39dd2152019-07-08 14:25:47 -0600272 self.Raise("Alignment size %s must be a power of two" %
273 self.align_size)
Simon Glass2574ef62016-11-25 20:15:51 -0700274 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glasse8561af2018-08-01 15:22:37 -0600275 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassdd156a42022-03-05 20:18:59 -0700276 self.extend_size = fdt_util.GetBool(self._node, 'extend-size')
Simon Glassa820af72020-09-06 10:39:09 -0600277 self.missing_msg = fdt_util.GetString(self._node, 'missing-msg')
Simon Glass2574ef62016-11-25 20:15:51 -0700278
Simon Glassa1301a22020-10-26 17:40:06 -0600279 # This is only supported by blobs and sections at present
280 self.compress = fdt_util.GetString(self._node, 'compress', 'none')
281
Simon Glass3732ec32018-09-14 04:57:18 -0600282 def GetDefaultFilename(self):
283 return None
284
Simon Glass267112e2019-07-20 12:23:28 -0600285 def GetFdts(self):
286 """Get the device trees used by this entry
Simon Glass0c9d5b52018-09-14 04:57:22 -0600287
288 Returns:
Simon Glass267112e2019-07-20 12:23:28 -0600289 Empty dict, if this entry is not a .dtb, otherwise:
290 Dict:
291 key: Filename from this entry (without the path)
Simon Glass684a4f12019-07-20 12:23:31 -0600292 value: Tuple:
Simon Glass8235dd82021-03-18 20:25:02 +1300293 Entry object for this dtb
Simon Glass684a4f12019-07-20 12:23:31 -0600294 Filename of file containing this dtb
Simon Glass0c9d5b52018-09-14 04:57:22 -0600295 """
Simon Glass267112e2019-07-20 12:23:28 -0600296 return {}
Simon Glass0c9d5b52018-09-14 04:57:22 -0600297
Simon Glassf86ddad2022-03-05 20:19:00 -0700298 def gen_entries(self):
299 """Allow entries to generate other entries
Simon Glassfcb2a7c2021-03-18 20:24:52 +1300300
301 Some entries generate subnodes automatically, from which sub-entries
302 are then created. This method allows those to be added to the binman
303 definition for the current image. An entry which implements this method
304 should call state.AddSubnode() to add a subnode and can add properties
305 with state.AddString(), etc.
306
307 An example is 'files', which produces a section containing a list of
308 files.
309 """
Simon Glassac6328c2018-09-14 04:57:28 -0600310 pass
311
Simon Glassacd6c6e2020-10-26 17:40:17 -0600312 def AddMissingProperties(self, have_image_pos):
313 """Add new properties to the device tree as needed for this entry
314
315 Args:
316 have_image_pos: True if this entry has an image position. This can
317 be False if its parent section is compressed, since compression
318 groups all entries together into a compressed block of data,
319 obscuring the start of each individual child entry
320 """
321 for prop in ['offset', 'size']:
Simon Glasse22f8fa2018-07-06 10:27:41 -0600322 if not prop in self._node.props:
Simon Glassc8135dc2018-09-14 04:57:21 -0600323 state.AddZeroProp(self._node, prop)
Simon Glassacd6c6e2020-10-26 17:40:17 -0600324 if have_image_pos and 'image-pos' not in self._node.props:
325 state.AddZeroProp(self._node, 'image-pos')
Simon Glassfb30e292019-07-20 12:23:51 -0600326 if self.GetImage().allow_repack:
327 if self.orig_offset is not None:
328 state.AddZeroProp(self._node, 'orig-offset', True)
329 if self.orig_size is not None:
330 state.AddZeroProp(self._node, 'orig-size', True)
331
Simon Glassaa2fcf92019-07-08 14:25:30 -0600332 if self.compress != 'none':
333 state.AddZeroProp(self._node, 'uncomp-size')
Alper Nebi Yasak1e4ffd82022-02-09 22:02:35 +0300334
335 if self.update_hash:
336 err = state.CheckAddHashProp(self._node)
337 if err:
338 self.Raise(err)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600339
340 def SetCalculatedProperties(self):
341 """Set the value of device-tree properties calculated by binman"""
Simon Glassc8135dc2018-09-14 04:57:21 -0600342 state.SetInt(self._node, 'offset', self.offset)
343 state.SetInt(self._node, 'size', self.size)
Simon Glass39dd2152019-07-08 14:25:47 -0600344 base = self.section.GetRootSkipAtStart() if self.section else 0
Simon Glassacd6c6e2020-10-26 17:40:17 -0600345 if self.image_pos is not None:
Simon Glasseb943b12020-11-02 12:55:44 -0700346 state.SetInt(self._node, 'image-pos', self.image_pos - base)
Simon Glassfb30e292019-07-20 12:23:51 -0600347 if self.GetImage().allow_repack:
348 if self.orig_offset is not None:
349 state.SetInt(self._node, 'orig-offset', self.orig_offset, True)
350 if self.orig_size is not None:
351 state.SetInt(self._node, 'orig-size', self.orig_size, True)
Simon Glassaa2fcf92019-07-08 14:25:30 -0600352 if self.uncomp_size is not None:
353 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
Alper Nebi Yasak1e4ffd82022-02-09 22:02:35 +0300354
355 if self.update_hash:
356 state.CheckSetHashValue(self._node, self.GetData)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600357
Simon Glass92307732018-07-06 10:27:40 -0600358 def ProcessFdt(self, fdt):
Simon Glasse219aa42018-09-14 04:57:24 -0600359 """Allow entries to adjust the device tree
360
361 Some entries need to adjust the device tree for their purposes. This
362 may involve adding or deleting properties.
363
364 Returns:
365 True if processing is complete
366 False if processing could not be completed due to a dependency.
367 This will cause the entry to be retried after others have been
368 called
369 """
Simon Glass92307732018-07-06 10:27:40 -0600370 return True
371
Simon Glass3b78d532018-06-01 09:38:21 -0600372 def SetPrefix(self, prefix):
373 """Set the name prefix for a node
374
375 Args:
376 prefix: Prefix to set, or '' to not use a prefix
377 """
378 if prefix:
379 self.name = prefix + self.name
380
Simon Glass2e1169f2018-07-06 10:27:19 -0600381 def SetContents(self, data):
382 """Set the contents of an entry
383
384 This sets both the data and content_size properties
385
386 Args:
Simon Glassd17dfea2019-07-08 14:25:33 -0600387 data: Data to set to the contents (bytes)
Simon Glass2e1169f2018-07-06 10:27:19 -0600388 """
389 self.data = data
390 self.contents_size = len(self.data)
391
392 def ProcessContentsUpdate(self, data):
Simon Glassd17dfea2019-07-08 14:25:33 -0600393 """Update the contents of an entry, after the size is fixed
Simon Glass2e1169f2018-07-06 10:27:19 -0600394
Simon Glassec849852019-07-08 14:25:35 -0600395 This checks that the new data is the same size as the old. If the size
396 has changed, this triggers a re-run of the packing algorithm.
Simon Glass2e1169f2018-07-06 10:27:19 -0600397
398 Args:
Simon Glassd17dfea2019-07-08 14:25:33 -0600399 data: Data to set to the contents (bytes)
Simon Glass2e1169f2018-07-06 10:27:19 -0600400
401 Raises:
402 ValueError if the new data size is not the same as the old
403 """
Simon Glassec849852019-07-08 14:25:35 -0600404 size_ok = True
Simon Glasse61b6f62019-07-08 14:25:37 -0600405 new_size = len(data)
Simon Glass9d8ee322019-07-20 12:23:58 -0600406 if state.AllowEntryExpansion() and new_size > self.contents_size:
407 # self.data will indicate the new size needed
408 size_ok = False
409 elif state.AllowEntryContraction() and new_size < self.contents_size:
410 size_ok = False
411
412 # If not allowed to change, try to deal with it or give up
413 if size_ok:
Simon Glasse61b6f62019-07-08 14:25:37 -0600414 if new_size > self.contents_size:
Simon Glass9d8ee322019-07-20 12:23:58 -0600415 self.Raise('Cannot update entry size from %d to %d' %
416 (self.contents_size, new_size))
417
418 # Don't let the data shrink. Pad it if necessary
419 if size_ok and new_size < self.contents_size:
Simon Glass80025522022-01-29 14:14:04 -0700420 data += tools.get_bytes(0, self.contents_size - new_size)
Simon Glass9d8ee322019-07-20 12:23:58 -0600421
422 if not size_ok:
Simon Glass011f1b32022-01-29 14:14:15 -0700423 tout.debug("Entry '%s' size change from %s to %s" % (
Simon Glass80025522022-01-29 14:14:04 -0700424 self._node.path, to_hex(self.contents_size),
425 to_hex(new_size)))
Simon Glass2e1169f2018-07-06 10:27:19 -0600426 self.SetContents(data)
Simon Glassec849852019-07-08 14:25:35 -0600427 return size_ok
Simon Glass2e1169f2018-07-06 10:27:19 -0600428
Simon Glassfc5a1682022-03-05 20:19:05 -0700429 def ObtainContents(self, skip_entry=None, fake_size=0):
Simon Glass2574ef62016-11-25 20:15:51 -0700430 """Figure out the contents of an entry.
431
Simon Glassfc5a1682022-03-05 20:19:05 -0700432 Args:
433 skip_entry (Entry): Entry to skip when obtaining section contents
434 fake_size (int): Size of fake file to create if needed
435
Simon Glass2574ef62016-11-25 20:15:51 -0700436 Returns:
437 True if the contents were found, False if another call is needed
438 after the other entries are processed.
439 """
440 # No contents by default: subclasses can implement this
441 return True
442
Simon Glasse61b6f62019-07-08 14:25:37 -0600443 def ResetForPack(self):
444 """Reset offset/size fields so that packing can be done again"""
Simon Glassb6dff4c2019-07-20 12:23:36 -0600445 self.Detail('ResetForPack: offset %s->%s, size %s->%s' %
Simon Glass80025522022-01-29 14:14:04 -0700446 (to_hex(self.offset), to_hex(self.orig_offset),
447 to_hex(self.size), to_hex(self.orig_size)))
Simon Glass1fdb4872019-10-31 07:43:02 -0600448 self.pre_reset_size = self.size
Simon Glasse61b6f62019-07-08 14:25:37 -0600449 self.offset = self.orig_offset
450 self.size = self.orig_size
451
Simon Glasse8561af2018-08-01 15:22:37 -0600452 def Pack(self, offset):
Simon Glassad5a7712018-06-01 09:38:14 -0600453 """Figure out how to pack the entry into the section
Simon Glass2574ef62016-11-25 20:15:51 -0700454
455 Most of the time the entries are not fully specified. There may be
456 an alignment but no size. In that case we take the size from the
457 contents of the entry.
458
Simon Glasse8561af2018-08-01 15:22:37 -0600459 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glass2574ef62016-11-25 20:15:51 -0700460
Simon Glasse8561af2018-08-01 15:22:37 -0600461 Once this function is complete, both the offset and size of the
Simon Glass2574ef62016-11-25 20:15:51 -0700462 entry will be know.
463
464 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600465 Current section offset pointer
Simon Glass2574ef62016-11-25 20:15:51 -0700466
467 Returns:
Simon Glasse8561af2018-08-01 15:22:37 -0600468 New section offset pointer (after this entry)
Simon Glass2574ef62016-11-25 20:15:51 -0700469 """
Simon Glassb6dff4c2019-07-20 12:23:36 -0600470 self.Detail('Packing: offset=%s, size=%s, content_size=%x' %
Simon Glass80025522022-01-29 14:14:04 -0700471 (to_hex(self.offset), to_hex(self.size),
Simon Glassb6dff4c2019-07-20 12:23:36 -0600472 self.contents_size))
Simon Glasse8561af2018-08-01 15:22:37 -0600473 if self.offset is None:
474 if self.offset_unset:
475 self.Raise('No offset set with offset-unset: should another '
476 'entry provide this correct offset?')
Simon Glass80025522022-01-29 14:14:04 -0700477 self.offset = tools.align(offset, self.align)
Simon Glass2574ef62016-11-25 20:15:51 -0700478 needed = self.pad_before + self.contents_size + self.pad_after
Simon Glass80025522022-01-29 14:14:04 -0700479 needed = tools.align(needed, self.align_size)
Simon Glass2574ef62016-11-25 20:15:51 -0700480 size = self.size
481 if not size:
482 size = needed
Simon Glasse8561af2018-08-01 15:22:37 -0600483 new_offset = self.offset + size
Simon Glass80025522022-01-29 14:14:04 -0700484 aligned_offset = tools.align(new_offset, self.align_end)
Simon Glasse8561af2018-08-01 15:22:37 -0600485 if aligned_offset != new_offset:
486 size = aligned_offset - self.offset
487 new_offset = aligned_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700488
489 if not self.size:
490 self.size = size
491
492 if self.size < needed:
493 self.Raise("Entry contents size is %#x (%d) but entry size is "
494 "%#x (%d)" % (needed, needed, self.size, self.size))
495 # Check that the alignment is correct. It could be wrong if the
Simon Glasse8561af2018-08-01 15:22:37 -0600496 # and offset or size values were provided (i.e. not calculated), but
Simon Glass2574ef62016-11-25 20:15:51 -0700497 # conflict with the provided alignment values
Simon Glass80025522022-01-29 14:14:04 -0700498 if self.size != tools.align(self.size, self.align_size):
Simon Glass2574ef62016-11-25 20:15:51 -0700499 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
500 (self.size, self.size, self.align_size, self.align_size))
Simon Glass80025522022-01-29 14:14:04 -0700501 if self.offset != tools.align(self.offset, self.align):
Simon Glasse8561af2018-08-01 15:22:37 -0600502 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
503 (self.offset, self.offset, self.align, self.align))
Simon Glassb6dff4c2019-07-20 12:23:36 -0600504 self.Detail(' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' %
505 (self.offset, self.size, self.contents_size, new_offset))
Simon Glass2574ef62016-11-25 20:15:51 -0700506
Simon Glasse8561af2018-08-01 15:22:37 -0600507 return new_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700508
509 def Raise(self, msg):
510 """Convenience function to raise an error referencing a node"""
511 raise ValueError("Node '%s': %s" % (self._node.path, msg))
512
Simon Glasse1915782021-03-21 18:24:31 +1300513 def Info(self, msg):
514 """Convenience function to log info referencing a node"""
515 tag = "Info '%s'" % self._node.path
Simon Glass011f1b32022-01-29 14:14:15 -0700516 tout.detail('%30s: %s' % (tag, msg))
Simon Glasse1915782021-03-21 18:24:31 +1300517
Simon Glassb6dff4c2019-07-20 12:23:36 -0600518 def Detail(self, msg):
519 """Convenience function to log detail referencing a node"""
520 tag = "Node '%s'" % self._node.path
Simon Glass011f1b32022-01-29 14:14:15 -0700521 tout.detail('%30s: %s' % (tag, msg))
Simon Glassb6dff4c2019-07-20 12:23:36 -0600522
Simon Glass91710b32018-07-17 13:25:32 -0600523 def GetEntryArgsOrProps(self, props, required=False):
524 """Return the values of a set of properties
525
526 Args:
527 props: List of EntryArg objects
528
529 Raises:
530 ValueError if a property is not found
531 """
532 values = []
533 missing = []
534 for prop in props:
535 python_prop = prop.name.replace('-', '_')
536 if hasattr(self, python_prop):
537 value = getattr(self, python_prop)
538 else:
539 value = None
540 if value is None:
541 value = self.GetArg(prop.name, prop.datatype)
542 if value is None and required:
543 missing.append(prop.name)
544 values.append(value)
545 if missing:
Simon Glass3fb25402021-01-06 21:35:16 -0700546 self.GetImage().MissingArgs(self, missing)
Simon Glass91710b32018-07-17 13:25:32 -0600547 return values
548
Simon Glass2574ef62016-11-25 20:15:51 -0700549 def GetPath(self):
550 """Get the path of a node
551
552 Returns:
553 Full path of the node for this entry
554 """
555 return self._node.path
556
Simon Glass27a7f772021-03-21 18:24:32 +1300557 def GetData(self, required=True):
Simon Glass72eeff12020-10-26 17:40:16 -0600558 """Get the contents of an entry
559
Simon Glass27a7f772021-03-21 18:24:32 +1300560 Args:
561 required: True if the data must be present, False if it is OK to
562 return None
563
Simon Glass72eeff12020-10-26 17:40:16 -0600564 Returns:
565 bytes content of the entry, excluding any padding. If the entry is
566 compressed, the compressed data is returned
567 """
Simon Glass80025522022-01-29 14:14:04 -0700568 self.Detail('GetData: size %s' % to_hex_size(self.data))
Simon Glass2574ef62016-11-25 20:15:51 -0700569 return self.data
570
Simon Glasse17220f2020-11-02 12:55:43 -0700571 def GetPaddedData(self, data=None):
572 """Get the data for an entry including any padding
573
574 Gets the entry data and uses its section's pad-byte value to add padding
575 before and after as defined by the pad-before and pad-after properties.
576
577 This does not consider alignment.
578
579 Returns:
580 Contents of the entry along with any pad bytes before and
581 after it (bytes)
582 """
583 if data is None:
584 data = self.GetData()
585 return self.section.GetPaddedDataForEntry(self, data)
586
Simon Glasse8561af2018-08-01 15:22:37 -0600587 def GetOffsets(self):
Simon Glass224bc662019-07-08 13:18:30 -0600588 """Get the offsets for siblings
589
590 Some entry types can contain information about the position or size of
591 other entries. An example of this is the Intel Flash Descriptor, which
592 knows where the Intel Management Engine section should go.
593
594 If this entry knows about the position of other entries, it can specify
595 this by returning values here
596
597 Returns:
598 Dict:
599 key: Entry type
600 value: List containing position and size of the given entry
Simon Glassed365eb2019-07-08 13:18:39 -0600601 type. Either can be None if not known
Simon Glass224bc662019-07-08 13:18:30 -0600602 """
Simon Glass2574ef62016-11-25 20:15:51 -0700603 return {}
604
Simon Glassed365eb2019-07-08 13:18:39 -0600605 def SetOffsetSize(self, offset, size):
606 """Set the offset and/or size of an entry
607
608 Args:
609 offset: New offset, or None to leave alone
610 size: New size, or None to leave alone
611 """
612 if offset is not None:
613 self.offset = offset
614 if size is not None:
615 self.size = size
Simon Glass2574ef62016-11-25 20:15:51 -0700616
Simon Glass9dcc8612018-08-01 15:22:42 -0600617 def SetImagePos(self, image_pos):
618 """Set the position in the image
619
620 Args:
621 image_pos: Position of this entry in the image
622 """
623 self.image_pos = image_pos + self.offset
624
Simon Glass2574ef62016-11-25 20:15:51 -0700625 def ProcessContents(self):
Simon Glassec849852019-07-08 14:25:35 -0600626 """Do any post-packing updates of entry contents
627
628 This function should call ProcessContentsUpdate() to update the entry
629 contents, if necessary, returning its return value here.
630
631 Args:
632 data: Data to set to the contents (bytes)
633
634 Returns:
635 True if the new data size is OK, False if expansion is needed
636
637 Raises:
638 ValueError if the new data size is not the same as the old and
639 state.AllowEntryExpansion() is False
640 """
641 return True
Simon Glass4ca8e042017-11-13 18:55:01 -0700642
Simon Glass8a6f56e2018-06-01 09:38:13 -0600643 def WriteSymbols(self, section):
Simon Glass4ca8e042017-11-13 18:55:01 -0700644 """Write symbol values into binary files for access at run time
645
646 Args:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600647 section: Section containing the entry
Simon Glass4ca8e042017-11-13 18:55:01 -0700648 """
649 pass
Simon Glassa91e1152018-06-01 09:38:16 -0600650
Simon Glass55f68072020-10-26 17:40:18 -0600651 def CheckEntries(self):
Simon Glasse8561af2018-08-01 15:22:37 -0600652 """Check that the entry offsets are correct
Simon Glassa91e1152018-06-01 09:38:16 -0600653
Simon Glasse8561af2018-08-01 15:22:37 -0600654 This is used for entries which have extra offset requirements (other
Simon Glassa91e1152018-06-01 09:38:16 -0600655 than having to be fully inside their section). Sub-classes can implement
656 this function and raise if there is a problem.
657 """
658 pass
Simon Glass30732662018-06-01 09:38:20 -0600659
Simon Glass3a9a2b82018-07-17 13:25:28 -0600660 @staticmethod
Simon Glasscd817d52018-09-14 04:57:36 -0600661 def GetStr(value):
662 if value is None:
663 return '<none> '
664 return '%08x' % value
665
666 @staticmethod
Simon Glass7eca7922018-07-17 13:25:49 -0600667 def WriteMapLine(fd, indent, name, offset, size, image_pos):
Simon Glasscd817d52018-09-14 04:57:36 -0600668 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
669 Entry.GetStr(offset), Entry.GetStr(size),
670 name), file=fd)
Simon Glass3a9a2b82018-07-17 13:25:28 -0600671
Simon Glass30732662018-06-01 09:38:20 -0600672 def WriteMap(self, fd, indent):
673 """Write a map of the entry to a .map file
674
675 Args:
676 fd: File to write the map to
677 indent: Curent indent level of map (0=none, 1=one level, etc.)
678 """
Simon Glass7eca7922018-07-17 13:25:49 -0600679 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
680 self.image_pos)
Simon Glass91710b32018-07-17 13:25:32 -0600681
Simon Glass704784b2018-07-17 13:25:38 -0600682 def GetEntries(self):
683 """Return a list of entries contained by this entry
684
685 Returns:
686 List of entries, or None if none. A normal entry has no entries
687 within it so will return None
688 """
689 return None
690
Simon Glass91710b32018-07-17 13:25:32 -0600691 def GetArg(self, name, datatype=str):
692 """Get the value of an entry argument or device-tree-node property
693
694 Some node properties can be provided as arguments to binman. First check
695 the entry arguments, and fall back to the device tree if not found
696
697 Args:
698 name: Argument name
699 datatype: Data type (str or int)
700
701 Returns:
702 Value of argument as a string or int, or None if no value
703
704 Raises:
705 ValueError if the argument cannot be converted to in
706 """
Simon Glass29aa7362018-09-14 04:57:19 -0600707 value = state.GetEntryArg(name)
Simon Glass91710b32018-07-17 13:25:32 -0600708 if value is not None:
709 if datatype == int:
710 try:
711 value = int(value)
712 except ValueError:
713 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
714 (name, value))
715 elif datatype == str:
716 pass
717 else:
718 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
719 datatype)
720 else:
721 value = fdt_util.GetDatatype(self._node, name, datatype)
722 return value
Simon Glass969616c2018-07-17 13:25:36 -0600723
724 @staticmethod
725 def WriteDocs(modules, test_missing=None):
726 """Write out documentation about the various entry types to stdout
727
728 Args:
729 modules: List of modules to include
730 test_missing: Used for testing. This is a module to report
731 as missing
732 """
733 print('''Binman Entry Documentation
734===========================
735
736This file describes the entry types supported by binman. These entry types can
737be placed in an image one by one to build up a final firmware image. It is
738fairly easy to create new entry types. Just add a new file to the 'etype'
739directory. You can use the existing entries as examples.
740
741Note that some entries are subclasses of others, using and extending their
742features to produce new behaviours.
743
744
745''')
746 modules = sorted(modules)
747
748 # Don't show the test entry
749 if '_testing' in modules:
750 modules.remove('_testing')
751 missing = []
752 for name in modules:
Simon Glass2f859412021-03-18 20:25:04 +1300753 module = Entry.Lookup('WriteDocs', name, False)
Simon Glass969616c2018-07-17 13:25:36 -0600754 docs = getattr(module, '__doc__')
755 if test_missing == name:
756 docs = None
757 if docs:
758 lines = docs.splitlines()
759 first_line = lines[0]
760 rest = [line[4:] for line in lines[1:]]
761 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
Simon Glassa7c97782022-08-07 16:33:25 -0600762
763 # Create a reference for use by rST docs
764 ref_name = f'etype_{module.__name__[6:]}'.lower()
765 print('.. _%s:' % ref_name)
766 print()
Simon Glass969616c2018-07-17 13:25:36 -0600767 print(hdr)
768 print('-' * len(hdr))
769 print('\n'.join(rest))
770 print()
771 print()
772 else:
773 missing.append(name)
774
775 if missing:
776 raise ValueError('Documentation is missing for modules: %s' %
777 ', '.join(missing))
Simon Glass639505b2018-09-14 04:57:11 -0600778
779 def GetUniqueName(self):
780 """Get a unique name for a node
781
782 Returns:
783 String containing a unique name for a node, consisting of the name
784 of all ancestors (starting from within the 'binman' node) separated
785 by a dot ('.'). This can be useful for generating unique filesnames
786 in the output directory.
787 """
788 name = self.name
789 node = self._node
790 while node.parent:
791 node = node.parent
Alper Nebi Yasak5cff63f2022-03-27 18:31:44 +0300792 if node.name in ('binman', '/'):
Simon Glass639505b2018-09-14 04:57:11 -0600793 break
794 name = '%s.%s' % (node.name, name)
795 return name
Simon Glassfa79a812018-09-14 04:57:29 -0600796
Simon Glassdd156a42022-03-05 20:18:59 -0700797 def extend_to_limit(self, limit):
798 """Extend an entry so that it ends at the given offset limit"""
Simon Glassfa79a812018-09-14 04:57:29 -0600799 if self.offset + self.size < limit:
800 self.size = limit - self.offset
801 # Request the contents again, since changing the size requires that
802 # the data grows. This should not fail, but check it to be sure.
803 if not self.ObtainContents():
804 self.Raise('Cannot obtain contents when expanding entry')
Simon Glassc4056b82019-07-08 13:18:38 -0600805
806 def HasSibling(self, name):
807 """Check if there is a sibling of a given name
808
809 Returns:
810 True if there is an entry with this name in the the same section,
811 else False
812 """
813 return name in self.section.GetEntries()
Simon Glasscec34ba2019-07-08 14:25:28 -0600814
815 def GetSiblingImagePos(self, name):
816 """Return the image position of the given sibling
817
818 Returns:
819 Image position of sibling, or None if the sibling has no position,
820 or False if there is no such sibling
821 """
822 if not self.HasSibling(name):
823 return False
824 return self.section.GetEntries()[name].image_pos
Simon Glass6b156f82019-07-08 14:25:43 -0600825
826 @staticmethod
827 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
828 uncomp_size, offset, entry):
829 """Add a new entry to the entries list
830
831 Args:
832 entries: List (of EntryInfo objects) to add to
833 indent: Current indent level to add to list
834 name: Entry name (string)
835 etype: Entry type (string)
836 size: Entry size in bytes (int)
837 image_pos: Position within image in bytes (int)
838 uncomp_size: Uncompressed size if the entry uses compression, else
839 None
840 offset: Entry offset within parent in bytes (int)
841 entry: Entry object
842 """
843 entries.append(EntryInfo(indent, name, etype, size, image_pos,
844 uncomp_size, offset, entry))
845
846 def ListEntries(self, entries, indent):
847 """Add files in this entry to the list of entries
848
849 This can be overridden by subclasses which need different behaviour.
850
851 Args:
852 entries: List (of EntryInfo objects) to add to
853 indent: Current indent level to add to list
854 """
855 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
856 self.image_pos, self.uncomp_size, self.offset, self)
Simon Glass4c613bf2019-07-08 14:25:50 -0600857
Simon Glass637958f2021-11-23 21:09:50 -0700858 def ReadData(self, decomp=True, alt_format=None):
Simon Glass4c613bf2019-07-08 14:25:50 -0600859 """Read the data for an entry from the image
860
861 This is used when the image has been read in and we want to extract the
862 data for a particular entry from that image.
863
864 Args:
865 decomp: True to decompress any compressed data before returning it;
866 False to return the raw, uncompressed data
867
868 Returns:
869 Entry data (bytes)
870 """
871 # Use True here so that we get an uncompressed section to work from,
872 # although compressed sections are currently not supported
Simon Glass011f1b32022-01-29 14:14:15 -0700873 tout.debug("ReadChildData section '%s', entry '%s'" %
Simon Glass4d8151f2019-09-25 08:56:21 -0600874 (self.section.GetPath(), self.GetPath()))
Simon Glass637958f2021-11-23 21:09:50 -0700875 data = self.section.ReadChildData(self, decomp, alt_format)
Simon Glass0cd8ace2019-07-20 12:24:04 -0600876 return data
Simon Glassaf8c45c2019-07-20 12:23:41 -0600877
Simon Glass637958f2021-11-23 21:09:50 -0700878 def ReadChildData(self, child, decomp=True, alt_format=None):
Simon Glass4d8151f2019-09-25 08:56:21 -0600879 """Read the data for a particular child entry
Simon Glass23f00472019-09-25 08:56:20 -0600880
881 This reads data from the parent and extracts the piece that relates to
882 the given child.
883
884 Args:
Simon Glass637958f2021-11-23 21:09:50 -0700885 child (Entry): Child entry to read data for (must be valid)
886 decomp (bool): True to decompress any compressed data before
887 returning it; False to return the raw, uncompressed data
888 alt_format (str): Alternative format to read in, or None
Simon Glass23f00472019-09-25 08:56:20 -0600889
890 Returns:
891 Data for the child (bytes)
892 """
893 pass
894
Simon Glassaf8c45c2019-07-20 12:23:41 -0600895 def LoadData(self, decomp=True):
896 data = self.ReadData(decomp)
Simon Glass072959a2019-07-20 12:23:50 -0600897 self.contents_size = len(data)
Simon Glassaf8c45c2019-07-20 12:23:41 -0600898 self.ProcessContentsUpdate(data)
899 self.Detail('Loaded data size %x' % len(data))
Simon Glass990b1742019-07-20 12:23:46 -0600900
Simon Glass637958f2021-11-23 21:09:50 -0700901 def GetAltFormat(self, data, alt_format):
902 """Read the data for an extry in an alternative format
903
904 Supported formats are list in the documentation for each entry. An
905 example is fdtmap which provides .
906
907 Args:
908 data (bytes): Data to convert (this should have been produced by the
909 entry)
910 alt_format (str): Format to use
911
912 """
913 pass
914
Simon Glass990b1742019-07-20 12:23:46 -0600915 def GetImage(self):
916 """Get the image containing this entry
917
918 Returns:
919 Image object containing this entry
920 """
921 return self.section.GetImage()
Simon Glass072959a2019-07-20 12:23:50 -0600922
923 def WriteData(self, data, decomp=True):
924 """Write the data to an entry in the image
925
926 This is used when the image has been read in and we want to replace the
927 data for a particular entry in that image.
928
929 The image must be re-packed and written out afterwards.
930
931 Args:
932 data: Data to replace it with
933 decomp: True to compress the data if needed, False if data is
934 already compressed so should be used as is
935
936 Returns:
937 True if the data did not result in a resize of this entry, False if
938 the entry must be resized
939 """
Simon Glass1fdb4872019-10-31 07:43:02 -0600940 if self.size is not None:
941 self.contents_size = self.size
942 else:
943 self.contents_size = self.pre_reset_size
Simon Glass072959a2019-07-20 12:23:50 -0600944 ok = self.ProcessContentsUpdate(data)
945 self.Detail('WriteData: size=%x, ok=%s' % (len(data), ok))
Simon Glassd34af7a2019-07-20 12:24:05 -0600946 section_ok = self.section.WriteChildData(self)
947 return ok and section_ok
948
949 def WriteChildData(self, child):
950 """Handle writing the data in a child entry
951
952 This should be called on the child's parent section after the child's
Simon Glasse796f242021-11-23 11:03:44 -0700953 data has been updated. It should update any data structures needed to
954 validate that the update is successful.
Simon Glassd34af7a2019-07-20 12:24:05 -0600955
956 This base-class implementation does nothing, since the base Entry object
957 does not have any children.
958
959 Args:
960 child: Child Entry that was written
961
962 Returns:
963 True if the section could be updated successfully, False if the
Simon Glasse796f242021-11-23 11:03:44 -0700964 data is such that the section could not update
Simon Glassd34af7a2019-07-20 12:24:05 -0600965 """
966 return True
Simon Glass11453762019-07-20 12:23:55 -0600967
968 def GetSiblingOrder(self):
969 """Get the relative order of an entry amoung its siblings
970
971 Returns:
972 'start' if this entry is first among siblings, 'end' if last,
973 otherwise None
974 """
975 entries = list(self.section.GetEntries().values())
976 if entries:
977 if self == entries[0]:
978 return 'start'
979 elif self == entries[-1]:
980 return 'end'
981 return 'middle'
Simon Glass5d94cc62020-07-09 18:39:38 -0600982
983 def SetAllowMissing(self, allow_missing):
984 """Set whether a section allows missing external blobs
985
986 Args:
987 allow_missing: True if allowed, False if not allowed
988 """
989 # This is meaningless for anything other than sections
990 pass
Simon Glassa003cd32020-07-09 18:39:40 -0600991
Heiko Thiery6d451362022-01-06 11:49:41 +0100992 def SetAllowFakeBlob(self, allow_fake):
993 """Set whether a section allows to create a fake blob
994
995 Args:
996 allow_fake: True if allowed, False if not allowed
997 """
Simon Glassceb5f912022-01-09 20:13:46 -0700998 self.allow_fake = allow_fake
Heiko Thiery6d451362022-01-06 11:49:41 +0100999
Simon Glassa003cd32020-07-09 18:39:40 -06001000 def CheckMissing(self, missing_list):
1001 """Check if any entries in this section have missing external blobs
1002
1003 If there are missing blobs, the entries are added to the list
1004
1005 Args:
1006 missing_list: List of Entry objects to be added to
1007 """
1008 if self.missing:
1009 missing_list.append(self)
Simon Glassb8f90372020-09-01 05:13:57 -06001010
Simon Glass8c0533b2022-03-05 20:19:04 -07001011 def check_fake_fname(self, fname, size=0):
Simon Glass7a602fd2022-01-12 13:10:36 -07001012 """If the file is missing and the entry allows fake blobs, fake it
1013
1014 Sets self.faked to True if faked
1015
1016 Args:
1017 fname (str): Filename to check
Simon Glass8c0533b2022-03-05 20:19:04 -07001018 size (int): Size of fake file to create
Simon Glass7a602fd2022-01-12 13:10:36 -07001019
1020 Returns:
Simon Glass214d36f2022-03-05 20:19:03 -07001021 tuple:
1022 fname (str): Filename of faked file
1023 bool: True if the blob was faked, False if not
Simon Glass7a602fd2022-01-12 13:10:36 -07001024 """
1025 if self.allow_fake and not pathlib.Path(fname).is_file():
Simon Glass7d3e4072022-08-07 09:46:46 -06001026 if not self.fake_fname:
1027 outfname = os.path.join(self.fake_dir, os.path.basename(fname))
1028 with open(outfname, "wb") as out:
1029 out.truncate(size)
1030 tout.info(f"Entry '{self._node.path}': Faked blob '{outfname}'")
1031 self.fake_fname = outfname
Simon Glass7a602fd2022-01-12 13:10:36 -07001032 self.faked = True
Simon Glass7d3e4072022-08-07 09:46:46 -06001033 return self.fake_fname, True
Simon Glass214d36f2022-03-05 20:19:03 -07001034 return fname, False
Simon Glass7a602fd2022-01-12 13:10:36 -07001035
Heiko Thiery6d451362022-01-06 11:49:41 +01001036 def CheckFakedBlobs(self, faked_blobs_list):
1037 """Check if any entries in this section have faked external blobs
1038
1039 If there are faked blobs, the entries are added to the list
1040
1041 Args:
1042 fake_blobs_list: List of Entry objects to be added to
1043 """
1044 # This is meaningless for anything other than blobs
1045 pass
1046
Simon Glassb8f90372020-09-01 05:13:57 -06001047 def GetAllowMissing(self):
1048 """Get whether a section allows missing external blobs
1049
1050 Returns:
1051 True if allowed, False if not allowed
1052 """
1053 return self.allow_missing
Simon Glassa820af72020-09-06 10:39:09 -06001054
Simon Glass66152ce2022-01-09 20:14:09 -07001055 def record_missing_bintool(self, bintool):
1056 """Record a missing bintool that was needed to produce this entry
1057
1058 Args:
1059 bintool (Bintool): Bintool that was missing
1060 """
1061 self.missing_bintools.append(bintool)
1062
1063 def check_missing_bintools(self, missing_list):
1064 """Check if any entries in this section have missing bintools
1065
1066 If there are missing bintools, these are added to the list
1067
1068 Args:
1069 missing_list: List of Bintool objects to be added to
1070 """
1071 missing_list += self.missing_bintools
1072
Simon Glassa820af72020-09-06 10:39:09 -06001073 def GetHelpTags(self):
1074 """Get the tags use for missing-blob help
1075
1076 Returns:
1077 list of possible tags, most desirable first
1078 """
1079 return list(filter(None, [self.missing_msg, self.name, self.etype]))
Simon Glassa1301a22020-10-26 17:40:06 -06001080
1081 def CompressData(self, indata):
1082 """Compress data according to the entry's compression method
1083
1084 Args:
1085 indata: Data to compress
1086
1087 Returns:
1088 Compressed data (first word is the compressed size)
1089 """
Simon Glass789b34402020-10-26 17:40:15 -06001090 self.uncomp_data = indata
Simon Glassa1301a22020-10-26 17:40:06 -06001091 if self.compress != 'none':
1092 self.uncomp_size = len(indata)
Simon Glassdd5c14ec2022-01-09 20:14:04 -07001093 data = comp_util.compress(indata, self.compress)
Simon Glassa1301a22020-10-26 17:40:06 -06001094 return data
Simon Glass2f859412021-03-18 20:25:04 +13001095
1096 @classmethod
1097 def UseExpanded(cls, node, etype, new_etype):
1098 """Check whether to use an expanded entry type
1099
1100 This is called by Entry.Create() when it finds an expanded version of
1101 an entry type (e.g. 'u-boot-expanded'). If this method returns True then
1102 it will be used (e.g. in place of 'u-boot'). If it returns False, it is
1103 ignored.
1104
1105 Args:
1106 node: Node object containing information about the entry to
1107 create
1108 etype: Original entry type being used
1109 new_etype: New entry type proposed
1110
1111 Returns:
1112 True to use this entry type, False to use the original one
1113 """
Simon Glass011f1b32022-01-29 14:14:15 -07001114 tout.info("Node '%s': etype '%s': %s selected" %
Simon Glass2f859412021-03-18 20:25:04 +13001115 (node.path, etype, new_etype))
1116 return True
Simon Glass637958f2021-11-23 21:09:50 -07001117
1118 def CheckAltFormats(self, alt_formats):
1119 """Add any alternative formats supported by this entry type
1120
1121 Args:
1122 alt_formats (dict): Dict to add alt_formats to:
1123 key: Name of alt format
1124 value: Help text
1125 """
1126 pass
Simon Glass4eae9252022-01-09 20:13:50 -07001127
Simon Glassfff147a2022-03-05 20:19:02 -07001128 def AddBintools(self, btools):
Simon Glass4eae9252022-01-09 20:13:50 -07001129 """Add the bintools used by this entry type
1130
1131 Args:
Simon Glassfff147a2022-03-05 20:19:02 -07001132 btools (dict of Bintool):
Simon Glass4eae9252022-01-09 20:13:50 -07001133 """
1134 pass
1135
1136 @classmethod
1137 def AddBintool(self, tools, name):
1138 """Add a new bintool to the tools used by this etype
1139
1140 Args:
1141 name: Name of the tool
1142 """
1143 btool = bintool.Bintool.create(name)
1144 tools[name] = btool
1145 return btool
Alper Nebi Yasak1e4ffd82022-02-09 22:02:35 +03001146
1147 def SetUpdateHash(self, update_hash):
1148 """Set whether this entry's "hash" subnode should be updated
1149
1150 Args:
1151 update_hash: True if hash should be updated, False if not
1152 """
1153 self.update_hash = update_hash
Simon Glass6fba35c2022-02-08 11:50:00 -07001154
Simon Glassfc5a1682022-03-05 20:19:05 -07001155 def collect_contents_to_file(self, entries, prefix, fake_size=0):
Simon Glass6fba35c2022-02-08 11:50:00 -07001156 """Put the contents of a list of entries into a file
1157
1158 Args:
1159 entries (list of Entry): Entries to collect
1160 prefix (str): Filename prefix of file to write to
Simon Glassfc5a1682022-03-05 20:19:05 -07001161 fake_size (int): Size of fake file to create if needed
Simon Glass6fba35c2022-02-08 11:50:00 -07001162
1163 If any entry does not have contents yet, this function returns False
1164 for the data.
1165
1166 Returns:
1167 Tuple:
Simon Glass43a98cc2022-03-05 20:18:58 -07001168 bytes: Concatenated data from all the entries (or None)
1169 str: Filename of file written (or None if no data)
1170 str: Unique portion of filename (or None if no data)
Simon Glass6fba35c2022-02-08 11:50:00 -07001171 """
1172 data = b''
1173 for entry in entries:
1174 # First get the input data and put it in a file. If not available,
1175 # try later.
Simon Glassfc5a1682022-03-05 20:19:05 -07001176 if not entry.ObtainContents(fake_size=fake_size):
Simon Glass43a98cc2022-03-05 20:18:58 -07001177 return None, None, None
Simon Glass6fba35c2022-02-08 11:50:00 -07001178 data += entry.GetData()
1179 uniq = self.GetUniqueName()
1180 fname = tools.get_output_filename(f'{prefix}.{uniq}')
1181 tools.write_file(fname, data)
1182 return data, fname, uniq
Simon Glass7d3e4072022-08-07 09:46:46 -06001183
1184 @classmethod
1185 def create_fake_dir(cls):
1186 """Create the directory for fake files"""
1187 cls.fake_dir = tools.get_output_filename('binman-fake')
1188 if not os.path.exists(cls.fake_dir):
1189 os.mkdir(cls.fake_dir)
1190 tout.notice(f"Fake-blob dir is '{cls.fake_dir}'")
Simon Glass0cf5bce2022-08-13 11:40:44 -06001191
1192 def ensure_props(self):
1193 """Raise an exception if properties are missing
1194
1195 Args:
1196 prop_list (list of str): List of properties to check for
1197
1198 Raises:
1199 ValueError: Any property is missing
1200 """
1201 not_present = []
1202 for prop in self.required_props:
1203 if not prop in self._node.props:
1204 not_present.append(prop)
1205 if not_present:
1206 self.Raise(f"'{self.etype}' entry is missing properties: {' '.join(not_present)}")