blob: 6a2c6e0d92e5b0cb0fff2dcce436490f4e2cbc9f [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 Glass30732662018-06-01 09:38:20 -06007from __future__ import print_function
8
Simon Glass91710b32018-07-17 13:25:32 -06009from collections import namedtuple
10
Simon Glass2574ef62016-11-25 20:15:51 -070011# importlib was introduced in Python 2.7 but there was a report of it not
12# working in 2.7.12, so we work around this:
13# http://lists.denx.de/pipermail/u-boot/2016-October/269729.html
14try:
15 import importlib
16 have_importlib = True
17except:
18 have_importlib = False
19
Simon Glass691198c2018-06-01 09:38:15 -060020import os
21import sys
Simon Glass29aa7362018-09-14 04:57:19 -060022
23import fdt_util
24import state
Simon Glass2574ef62016-11-25 20:15:51 -070025import tools
Simon Glassb6dff4c2019-07-20 12:23:36 -060026from tools import ToHex, ToHexSize
Simon Glass233a26a92019-07-08 14:25:49 -060027import tout
Simon Glass2574ef62016-11-25 20:15:51 -070028
29modules = {}
30
Simon Glass691198c2018-06-01 09:38:15 -060031our_path = os.path.dirname(os.path.realpath(__file__))
32
Simon Glass91710b32018-07-17 13:25:32 -060033
34# An argument which can be passed to entries on the command line, in lieu of
35# device-tree properties.
36EntryArg = namedtuple('EntryArg', ['name', 'datatype'])
37
Simon Glass6b156f82019-07-08 14:25:43 -060038# Information about an entry for use when displaying summaries
39EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size',
40 'image_pos', 'uncomp_size', 'offset',
41 'entry'])
Simon Glass91710b32018-07-17 13:25:32 -060042
Simon Glass2574ef62016-11-25 20:15:51 -070043class Entry(object):
Simon Glassad5a7712018-06-01 09:38:14 -060044 """An Entry in the section
Simon Glass2574ef62016-11-25 20:15:51 -070045
46 An entry corresponds to a single node in the device-tree description
Simon Glassad5a7712018-06-01 09:38:14 -060047 of the section. Each entry ends up being a part of the final section.
Simon Glass2574ef62016-11-25 20:15:51 -070048 Entries can be placed either right next to each other, or with padding
49 between them. The type of the entry determines the data that is in it.
50
51 This class is not used by itself. All entry objects are subclasses of
52 Entry.
53
54 Attributes:
Simon Glass3a9a2b82018-07-17 13:25:28 -060055 section: Section object containing this entry
Simon Glass2574ef62016-11-25 20:15:51 -070056 node: The node that created this entry
Simon Glasse8561af2018-08-01 15:22:37 -060057 offset: Offset of entry within the section, None if not known yet (in
58 which case it will be calculated by Pack())
Simon Glass2574ef62016-11-25 20:15:51 -070059 size: Entry size in bytes, None if not known
Simon Glassaa2fcf92019-07-08 14:25:30 -060060 uncomp_size: Size of uncompressed data in bytes, if the entry is
61 compressed, else None
Simon Glass2574ef62016-11-25 20:15:51 -070062 contents_size: Size of contents in bytes, 0 by default
Simon Glasse8561af2018-08-01 15:22:37 -060063 align: Entry start offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070064 align_size: Entry size alignment, or None
Simon Glasse8561af2018-08-01 15:22:37 -060065 align_end: Entry end offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070066 pad_before: Number of pad bytes before the contents, 0 if none
67 pad_after: Number of pad bytes after the contents, 0 if none
68 data: Contents of entry (string of bytes)
Simon Glassaa2fcf92019-07-08 14:25:30 -060069 compress: Compression algoithm used (e.g. 'lz4'), 'none' if none
Simon Glasse61b6f62019-07-08 14:25:37 -060070 orig_offset: Original offset value read from node
71 orig_size: Original size value read from node
Simon Glass2574ef62016-11-25 20:15:51 -070072 """
Simon Glass2c360cf2019-07-20 12:23:45 -060073 def __init__(self, section, etype, node, name_prefix=''):
Simon Glassad5a7712018-06-01 09:38:14 -060074 self.section = section
Simon Glass2574ef62016-11-25 20:15:51 -070075 self.etype = etype
76 self._node = node
Simon Glass3b78d532018-06-01 09:38:21 -060077 self.name = node and (name_prefix + node.name) or 'none'
Simon Glasse8561af2018-08-01 15:22:37 -060078 self.offset = None
Simon Glass2574ef62016-11-25 20:15:51 -070079 self.size = None
Simon Glassaa2fcf92019-07-08 14:25:30 -060080 self.uncomp_size = None
Simon Glass5c350162018-07-17 13:25:47 -060081 self.data = None
Simon Glass2574ef62016-11-25 20:15:51 -070082 self.contents_size = 0
83 self.align = None
84 self.align_size = None
85 self.align_end = None
86 self.pad_before = 0
87 self.pad_after = 0
Simon Glasse8561af2018-08-01 15:22:37 -060088 self.offset_unset = False
Simon Glass9dcc8612018-08-01 15:22:42 -060089 self.image_pos = None
Simon Glassfa79a812018-09-14 04:57:29 -060090 self._expand_size = False
Simon Glassaa2fcf92019-07-08 14:25:30 -060091 self.compress = 'none'
Simon Glass2574ef62016-11-25 20:15:51 -070092
93 @staticmethod
Simon Glass75502932019-07-08 14:25:31 -060094 def Lookup(node_path, etype):
Simon Glass969616c2018-07-17 13:25:36 -060095 """Look up the entry class for a node.
Simon Glass2574ef62016-11-25 20:15:51 -070096
97 Args:
Simon Glass969616c2018-07-17 13:25:36 -060098 node_node: Path name of Node object containing information about
99 the entry to create (used for errors)
100 etype: Entry type to use
Simon Glass2574ef62016-11-25 20:15:51 -0700101
102 Returns:
Simon Glass969616c2018-07-17 13:25:36 -0600103 The entry class object if found, else None
Simon Glass2574ef62016-11-25 20:15:51 -0700104 """
Simon Glasse76a3e62018-06-01 09:38:11 -0600105 # Convert something like 'u-boot@0' to 'u_boot' since we are only
106 # interested in the type.
Simon Glass2574ef62016-11-25 20:15:51 -0700107 module_name = etype.replace('-', '_')
Simon Glasse76a3e62018-06-01 09:38:11 -0600108 if '@' in module_name:
109 module_name = module_name.split('@')[0]
Simon Glass2574ef62016-11-25 20:15:51 -0700110 module = modules.get(module_name)
111
Simon Glass691198c2018-06-01 09:38:15 -0600112 # Also allow entry-type modules to be brought in from the etype directory.
113
Simon Glass2574ef62016-11-25 20:15:51 -0700114 # Import the module if we have not already done so.
115 if not module:
Simon Glass691198c2018-06-01 09:38:15 -0600116 old_path = sys.path
117 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glass2574ef62016-11-25 20:15:51 -0700118 try:
119 if have_importlib:
120 module = importlib.import_module(module_name)
121 else:
122 module = __import__(module_name)
Simon Glass969616c2018-07-17 13:25:36 -0600123 except ImportError as e:
124 raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" %
125 (etype, node_path, module_name, e))
Simon Glass691198c2018-06-01 09:38:15 -0600126 finally:
127 sys.path = old_path
Simon Glass2574ef62016-11-25 20:15:51 -0700128 modules[module_name] = module
129
Simon Glass969616c2018-07-17 13:25:36 -0600130 # Look up the expected class name
131 return getattr(module, 'Entry_%s' % module_name)
132
133 @staticmethod
134 def Create(section, node, etype=None):
135 """Create a new entry for a node.
136
137 Args:
138 section: Section object containing this node
139 node: Node object containing information about the entry to
140 create
141 etype: Entry type to use, or None to work it out (used for tests)
142
143 Returns:
144 A new Entry object of the correct type (a subclass of Entry)
145 """
146 if not etype:
147 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glass75502932019-07-08 14:25:31 -0600148 obj = Entry.Lookup(node.path, etype)
Simon Glass969616c2018-07-17 13:25:36 -0600149
Simon Glass2574ef62016-11-25 20:15:51 -0700150 # Call its constructor to get the object we want.
Simon Glassad5a7712018-06-01 09:38:14 -0600151 return obj(section, etype, node)
Simon Glass2574ef62016-11-25 20:15:51 -0700152
153 def ReadNode(self):
154 """Read entry information from the node
155
Simon Glass2c360cf2019-07-20 12:23:45 -0600156 This must be called as the first thing after the Entry is created.
157
Simon Glass2574ef62016-11-25 20:15:51 -0700158 This reads all the fields we recognise from the node, ready for use.
159 """
Simon Glass24b97442018-07-17 13:25:51 -0600160 if 'pos' in self._node.props:
161 self.Raise("Please use 'offset' instead of 'pos'")
Simon Glasse8561af2018-08-01 15:22:37 -0600162 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glass2574ef62016-11-25 20:15:51 -0700163 self.size = fdt_util.GetInt(self._node, 'size')
Simon Glassfb30e292019-07-20 12:23:51 -0600164 self.orig_offset = fdt_util.GetInt(self._node, 'orig-offset')
165 self.orig_size = fdt_util.GetInt(self._node, 'orig-size')
166 if self.GetImage().copy_to_orig:
167 self.orig_offset = self.offset
168 self.orig_size = self.size
Simon Glasse61b6f62019-07-08 14:25:37 -0600169
Simon Glassb8424fa2019-07-08 14:25:46 -0600170 # These should not be set in input files, but are set in an FDT map,
171 # which is also read by this code.
172 self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
173 self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
174
Simon Glass2574ef62016-11-25 20:15:51 -0700175 self.align = fdt_util.GetInt(self._node, 'align')
176 if tools.NotPowerOfTwo(self.align):
177 raise ValueError("Node '%s': Alignment %s must be a power of two" %
178 (self._node.path, self.align))
179 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
180 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
181 self.align_size = fdt_util.GetInt(self._node, 'align-size')
182 if tools.NotPowerOfTwo(self.align_size):
Simon Glass39dd2152019-07-08 14:25:47 -0600183 self.Raise("Alignment size %s must be a power of two" %
184 self.align_size)
Simon Glass2574ef62016-11-25 20:15:51 -0700185 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glasse8561af2018-08-01 15:22:37 -0600186 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glassfa79a812018-09-14 04:57:29 -0600187 self.expand_size = fdt_util.GetBool(self._node, 'expand-size')
Simon Glass2574ef62016-11-25 20:15:51 -0700188
Simon Glass3732ec32018-09-14 04:57:18 -0600189 def GetDefaultFilename(self):
190 return None
191
Simon Glass267112e2019-07-20 12:23:28 -0600192 def GetFdts(self):
193 """Get the device trees used by this entry
Simon Glass0c9d5b52018-09-14 04:57:22 -0600194
195 Returns:
Simon Glass267112e2019-07-20 12:23:28 -0600196 Empty dict, if this entry is not a .dtb, otherwise:
197 Dict:
198 key: Filename from this entry (without the path)
Simon Glass684a4f12019-07-20 12:23:31 -0600199 value: Tuple:
200 Fdt object for this dtb, or None if not available
201 Filename of file containing this dtb
Simon Glass0c9d5b52018-09-14 04:57:22 -0600202 """
Simon Glass267112e2019-07-20 12:23:28 -0600203 return {}
Simon Glass0c9d5b52018-09-14 04:57:22 -0600204
Simon Glassac6328c2018-09-14 04:57:28 -0600205 def ExpandEntries(self):
206 pass
207
Simon Glasse22f8fa2018-07-06 10:27:41 -0600208 def AddMissingProperties(self):
209 """Add new properties to the device tree as needed for this entry"""
Simon Glass9dcc8612018-08-01 15:22:42 -0600210 for prop in ['offset', 'size', 'image-pos']:
Simon Glasse22f8fa2018-07-06 10:27:41 -0600211 if not prop in self._node.props:
Simon Glassc8135dc2018-09-14 04:57:21 -0600212 state.AddZeroProp(self._node, prop)
Simon Glassfb30e292019-07-20 12:23:51 -0600213 if self.GetImage().allow_repack:
214 if self.orig_offset is not None:
215 state.AddZeroProp(self._node, 'orig-offset', True)
216 if self.orig_size is not None:
217 state.AddZeroProp(self._node, 'orig-size', True)
218
Simon Glassaa2fcf92019-07-08 14:25:30 -0600219 if self.compress != 'none':
220 state.AddZeroProp(self._node, 'uncomp-size')
Simon Glassae7cf032018-09-14 04:57:31 -0600221 err = state.CheckAddHashProp(self._node)
222 if err:
223 self.Raise(err)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600224
225 def SetCalculatedProperties(self):
226 """Set the value of device-tree properties calculated by binman"""
Simon Glassc8135dc2018-09-14 04:57:21 -0600227 state.SetInt(self._node, 'offset', self.offset)
228 state.SetInt(self._node, 'size', self.size)
Simon Glass39dd2152019-07-08 14:25:47 -0600229 base = self.section.GetRootSkipAtStart() if self.section else 0
230 state.SetInt(self._node, 'image-pos', self.image_pos - base)
Simon Glassfb30e292019-07-20 12:23:51 -0600231 if self.GetImage().allow_repack:
232 if self.orig_offset is not None:
233 state.SetInt(self._node, 'orig-offset', self.orig_offset, True)
234 if self.orig_size is not None:
235 state.SetInt(self._node, 'orig-size', self.orig_size, True)
Simon Glassaa2fcf92019-07-08 14:25:30 -0600236 if self.uncomp_size is not None:
237 state.SetInt(self._node, 'uncomp-size', self.uncomp_size)
Simon Glassae7cf032018-09-14 04:57:31 -0600238 state.CheckSetHashValue(self._node, self.GetData)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600239
Simon Glass92307732018-07-06 10:27:40 -0600240 def ProcessFdt(self, fdt):
Simon Glasse219aa42018-09-14 04:57:24 -0600241 """Allow entries to adjust the device tree
242
243 Some entries need to adjust the device tree for their purposes. This
244 may involve adding or deleting properties.
245
246 Returns:
247 True if processing is complete
248 False if processing could not be completed due to a dependency.
249 This will cause the entry to be retried after others have been
250 called
251 """
Simon Glass92307732018-07-06 10:27:40 -0600252 return True
253
Simon Glass3b78d532018-06-01 09:38:21 -0600254 def SetPrefix(self, prefix):
255 """Set the name prefix for a node
256
257 Args:
258 prefix: Prefix to set, or '' to not use a prefix
259 """
260 if prefix:
261 self.name = prefix + self.name
262
Simon Glass2e1169f2018-07-06 10:27:19 -0600263 def SetContents(self, data):
264 """Set the contents of an entry
265
266 This sets both the data and content_size properties
267
268 Args:
Simon Glassd17dfea2019-07-08 14:25:33 -0600269 data: Data to set to the contents (bytes)
Simon Glass2e1169f2018-07-06 10:27:19 -0600270 """
271 self.data = data
272 self.contents_size = len(self.data)
273
274 def ProcessContentsUpdate(self, data):
Simon Glassd17dfea2019-07-08 14:25:33 -0600275 """Update the contents of an entry, after the size is fixed
Simon Glass2e1169f2018-07-06 10:27:19 -0600276
Simon Glassec849852019-07-08 14:25:35 -0600277 This checks that the new data is the same size as the old. If the size
278 has changed, this triggers a re-run of the packing algorithm.
Simon Glass2e1169f2018-07-06 10:27:19 -0600279
280 Args:
Simon Glassd17dfea2019-07-08 14:25:33 -0600281 data: Data to set to the contents (bytes)
Simon Glass2e1169f2018-07-06 10:27:19 -0600282
283 Raises:
284 ValueError if the new data size is not the same as the old
285 """
Simon Glassec849852019-07-08 14:25:35 -0600286 size_ok = True
Simon Glasse61b6f62019-07-08 14:25:37 -0600287 new_size = len(data)
Simon Glass9d8ee322019-07-20 12:23:58 -0600288 if state.AllowEntryExpansion() and new_size > self.contents_size:
289 # self.data will indicate the new size needed
290 size_ok = False
291 elif state.AllowEntryContraction() and new_size < self.contents_size:
292 size_ok = False
293
294 # If not allowed to change, try to deal with it or give up
295 if size_ok:
Simon Glasse61b6f62019-07-08 14:25:37 -0600296 if new_size > self.contents_size:
Simon Glass9d8ee322019-07-20 12:23:58 -0600297 self.Raise('Cannot update entry size from %d to %d' %
298 (self.contents_size, new_size))
299
300 # Don't let the data shrink. Pad it if necessary
301 if size_ok and new_size < self.contents_size:
302 data += tools.GetBytes(0, self.contents_size - new_size)
303
304 if not size_ok:
305 tout.Debug("Entry '%s' size change from %s to %s" % (
306 self._node.path, ToHex(self.contents_size),
307 ToHex(new_size)))
Simon Glass2e1169f2018-07-06 10:27:19 -0600308 self.SetContents(data)
Simon Glassec849852019-07-08 14:25:35 -0600309 return size_ok
Simon Glass2e1169f2018-07-06 10:27:19 -0600310
Simon Glass2574ef62016-11-25 20:15:51 -0700311 def ObtainContents(self):
312 """Figure out the contents of an entry.
313
314 Returns:
315 True if the contents were found, False if another call is needed
316 after the other entries are processed.
317 """
318 # No contents by default: subclasses can implement this
319 return True
320
Simon Glasse61b6f62019-07-08 14:25:37 -0600321 def ResetForPack(self):
322 """Reset offset/size fields so that packing can be done again"""
Simon Glassb6dff4c2019-07-20 12:23:36 -0600323 self.Detail('ResetForPack: offset %s->%s, size %s->%s' %
324 (ToHex(self.offset), ToHex(self.orig_offset),
325 ToHex(self.size), ToHex(self.orig_size)))
Simon Glasse61b6f62019-07-08 14:25:37 -0600326 self.offset = self.orig_offset
327 self.size = self.orig_size
328
Simon Glasse8561af2018-08-01 15:22:37 -0600329 def Pack(self, offset):
Simon Glassad5a7712018-06-01 09:38:14 -0600330 """Figure out how to pack the entry into the section
Simon Glass2574ef62016-11-25 20:15:51 -0700331
332 Most of the time the entries are not fully specified. There may be
333 an alignment but no size. In that case we take the size from the
334 contents of the entry.
335
Simon Glasse8561af2018-08-01 15:22:37 -0600336 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glass2574ef62016-11-25 20:15:51 -0700337
Simon Glasse8561af2018-08-01 15:22:37 -0600338 Once this function is complete, both the offset and size of the
Simon Glass2574ef62016-11-25 20:15:51 -0700339 entry will be know.
340
341 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600342 Current section offset pointer
Simon Glass2574ef62016-11-25 20:15:51 -0700343
344 Returns:
Simon Glasse8561af2018-08-01 15:22:37 -0600345 New section offset pointer (after this entry)
Simon Glass2574ef62016-11-25 20:15:51 -0700346 """
Simon Glassb6dff4c2019-07-20 12:23:36 -0600347 self.Detail('Packing: offset=%s, size=%s, content_size=%x' %
348 (ToHex(self.offset), ToHex(self.size),
349 self.contents_size))
Simon Glasse8561af2018-08-01 15:22:37 -0600350 if self.offset is None:
351 if self.offset_unset:
352 self.Raise('No offset set with offset-unset: should another '
353 'entry provide this correct offset?')
354 self.offset = tools.Align(offset, self.align)
Simon Glass2574ef62016-11-25 20:15:51 -0700355 needed = self.pad_before + self.contents_size + self.pad_after
356 needed = tools.Align(needed, self.align_size)
357 size = self.size
358 if not size:
359 size = needed
Simon Glasse8561af2018-08-01 15:22:37 -0600360 new_offset = self.offset + size
361 aligned_offset = tools.Align(new_offset, self.align_end)
362 if aligned_offset != new_offset:
363 size = aligned_offset - self.offset
364 new_offset = aligned_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700365
366 if not self.size:
367 self.size = size
368
369 if self.size < needed:
370 self.Raise("Entry contents size is %#x (%d) but entry size is "
371 "%#x (%d)" % (needed, needed, self.size, self.size))
372 # Check that the alignment is correct. It could be wrong if the
Simon Glasse8561af2018-08-01 15:22:37 -0600373 # and offset or size values were provided (i.e. not calculated), but
Simon Glass2574ef62016-11-25 20:15:51 -0700374 # conflict with the provided alignment values
375 if self.size != tools.Align(self.size, self.align_size):
376 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
377 (self.size, self.size, self.align_size, self.align_size))
Simon Glasse8561af2018-08-01 15:22:37 -0600378 if self.offset != tools.Align(self.offset, self.align):
379 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
380 (self.offset, self.offset, self.align, self.align))
Simon Glassb6dff4c2019-07-20 12:23:36 -0600381 self.Detail(' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' %
382 (self.offset, self.size, self.contents_size, new_offset))
Simon Glass2574ef62016-11-25 20:15:51 -0700383
Simon Glasse8561af2018-08-01 15:22:37 -0600384 return new_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700385
386 def Raise(self, msg):
387 """Convenience function to raise an error referencing a node"""
388 raise ValueError("Node '%s': %s" % (self._node.path, msg))
389
Simon Glassb6dff4c2019-07-20 12:23:36 -0600390 def Detail(self, msg):
391 """Convenience function to log detail referencing a node"""
392 tag = "Node '%s'" % self._node.path
393 tout.Detail('%30s: %s' % (tag, msg))
394
Simon Glass91710b32018-07-17 13:25:32 -0600395 def GetEntryArgsOrProps(self, props, required=False):
396 """Return the values of a set of properties
397
398 Args:
399 props: List of EntryArg objects
400
401 Raises:
402 ValueError if a property is not found
403 """
404 values = []
405 missing = []
406 for prop in props:
407 python_prop = prop.name.replace('-', '_')
408 if hasattr(self, python_prop):
409 value = getattr(self, python_prop)
410 else:
411 value = None
412 if value is None:
413 value = self.GetArg(prop.name, prop.datatype)
414 if value is None and required:
415 missing.append(prop.name)
416 values.append(value)
417 if missing:
418 self.Raise('Missing required properties/entry args: %s' %
419 (', '.join(missing)))
420 return values
421
Simon Glass2574ef62016-11-25 20:15:51 -0700422 def GetPath(self):
423 """Get the path of a node
424
425 Returns:
426 Full path of the node for this entry
427 """
428 return self._node.path
429
430 def GetData(self):
Simon Glassb6dff4c2019-07-20 12:23:36 -0600431 self.Detail('GetData: size %s' % ToHexSize(self.data))
Simon Glass2574ef62016-11-25 20:15:51 -0700432 return self.data
433
Simon Glasse8561af2018-08-01 15:22:37 -0600434 def GetOffsets(self):
Simon Glass224bc662019-07-08 13:18:30 -0600435 """Get the offsets for siblings
436
437 Some entry types can contain information about the position or size of
438 other entries. An example of this is the Intel Flash Descriptor, which
439 knows where the Intel Management Engine section should go.
440
441 If this entry knows about the position of other entries, it can specify
442 this by returning values here
443
444 Returns:
445 Dict:
446 key: Entry type
447 value: List containing position and size of the given entry
Simon Glassed365eb2019-07-08 13:18:39 -0600448 type. Either can be None if not known
Simon Glass224bc662019-07-08 13:18:30 -0600449 """
Simon Glass2574ef62016-11-25 20:15:51 -0700450 return {}
451
Simon Glassed365eb2019-07-08 13:18:39 -0600452 def SetOffsetSize(self, offset, size):
453 """Set the offset and/or size of an entry
454
455 Args:
456 offset: New offset, or None to leave alone
457 size: New size, or None to leave alone
458 """
459 if offset is not None:
460 self.offset = offset
461 if size is not None:
462 self.size = size
Simon Glass2574ef62016-11-25 20:15:51 -0700463
Simon Glass9dcc8612018-08-01 15:22:42 -0600464 def SetImagePos(self, image_pos):
465 """Set the position in the image
466
467 Args:
468 image_pos: Position of this entry in the image
469 """
470 self.image_pos = image_pos + self.offset
471
Simon Glass2574ef62016-11-25 20:15:51 -0700472 def ProcessContents(self):
Simon Glassec849852019-07-08 14:25:35 -0600473 """Do any post-packing updates of entry contents
474
475 This function should call ProcessContentsUpdate() to update the entry
476 contents, if necessary, returning its return value here.
477
478 Args:
479 data: Data to set to the contents (bytes)
480
481 Returns:
482 True if the new data size is OK, False if expansion is needed
483
484 Raises:
485 ValueError if the new data size is not the same as the old and
486 state.AllowEntryExpansion() is False
487 """
488 return True
Simon Glass4ca8e042017-11-13 18:55:01 -0700489
Simon Glass8a6f56e2018-06-01 09:38:13 -0600490 def WriteSymbols(self, section):
Simon Glass4ca8e042017-11-13 18:55:01 -0700491 """Write symbol values into binary files for access at run time
492
493 Args:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600494 section: Section containing the entry
Simon Glass4ca8e042017-11-13 18:55:01 -0700495 """
496 pass
Simon Glassa91e1152018-06-01 09:38:16 -0600497
Simon Glasse8561af2018-08-01 15:22:37 -0600498 def CheckOffset(self):
499 """Check that the entry offsets are correct
Simon Glassa91e1152018-06-01 09:38:16 -0600500
Simon Glasse8561af2018-08-01 15:22:37 -0600501 This is used for entries which have extra offset requirements (other
Simon Glassa91e1152018-06-01 09:38:16 -0600502 than having to be fully inside their section). Sub-classes can implement
503 this function and raise if there is a problem.
504 """
505 pass
Simon Glass30732662018-06-01 09:38:20 -0600506
Simon Glass3a9a2b82018-07-17 13:25:28 -0600507 @staticmethod
Simon Glasscd817d52018-09-14 04:57:36 -0600508 def GetStr(value):
509 if value is None:
510 return '<none> '
511 return '%08x' % value
512
513 @staticmethod
Simon Glass7eca7922018-07-17 13:25:49 -0600514 def WriteMapLine(fd, indent, name, offset, size, image_pos):
Simon Glasscd817d52018-09-14 04:57:36 -0600515 print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
516 Entry.GetStr(offset), Entry.GetStr(size),
517 name), file=fd)
Simon Glass3a9a2b82018-07-17 13:25:28 -0600518
Simon Glass30732662018-06-01 09:38:20 -0600519 def WriteMap(self, fd, indent):
520 """Write a map of the entry to a .map file
521
522 Args:
523 fd: File to write the map to
524 indent: Curent indent level of map (0=none, 1=one level, etc.)
525 """
Simon Glass7eca7922018-07-17 13:25:49 -0600526 self.WriteMapLine(fd, indent, self.name, self.offset, self.size,
527 self.image_pos)
Simon Glass91710b32018-07-17 13:25:32 -0600528
Simon Glass704784b2018-07-17 13:25:38 -0600529 def GetEntries(self):
530 """Return a list of entries contained by this entry
531
532 Returns:
533 List of entries, or None if none. A normal entry has no entries
534 within it so will return None
535 """
536 return None
537
Simon Glass91710b32018-07-17 13:25:32 -0600538 def GetArg(self, name, datatype=str):
539 """Get the value of an entry argument or device-tree-node property
540
541 Some node properties can be provided as arguments to binman. First check
542 the entry arguments, and fall back to the device tree if not found
543
544 Args:
545 name: Argument name
546 datatype: Data type (str or int)
547
548 Returns:
549 Value of argument as a string or int, or None if no value
550
551 Raises:
552 ValueError if the argument cannot be converted to in
553 """
Simon Glass29aa7362018-09-14 04:57:19 -0600554 value = state.GetEntryArg(name)
Simon Glass91710b32018-07-17 13:25:32 -0600555 if value is not None:
556 if datatype == int:
557 try:
558 value = int(value)
559 except ValueError:
560 self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" %
561 (name, value))
562 elif datatype == str:
563 pass
564 else:
565 raise ValueError("GetArg() internal error: Unknown data type '%s'" %
566 datatype)
567 else:
568 value = fdt_util.GetDatatype(self._node, name, datatype)
569 return value
Simon Glass969616c2018-07-17 13:25:36 -0600570
571 @staticmethod
572 def WriteDocs(modules, test_missing=None):
573 """Write out documentation about the various entry types to stdout
574
575 Args:
576 modules: List of modules to include
577 test_missing: Used for testing. This is a module to report
578 as missing
579 """
580 print('''Binman Entry Documentation
581===========================
582
583This file describes the entry types supported by binman. These entry types can
584be placed in an image one by one to build up a final firmware image. It is
585fairly easy to create new entry types. Just add a new file to the 'etype'
586directory. You can use the existing entries as examples.
587
588Note that some entries are subclasses of others, using and extending their
589features to produce new behaviours.
590
591
592''')
593 modules = sorted(modules)
594
595 # Don't show the test entry
596 if '_testing' in modules:
597 modules.remove('_testing')
598 missing = []
599 for name in modules:
Simon Glass0baeab72019-07-08 14:25:32 -0600600 if name.startswith('__'):
601 continue
Simon Glass75502932019-07-08 14:25:31 -0600602 module = Entry.Lookup(name, name)
Simon Glass969616c2018-07-17 13:25:36 -0600603 docs = getattr(module, '__doc__')
604 if test_missing == name:
605 docs = None
606 if docs:
607 lines = docs.splitlines()
608 first_line = lines[0]
609 rest = [line[4:] for line in lines[1:]]
610 hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line)
611 print(hdr)
612 print('-' * len(hdr))
613 print('\n'.join(rest))
614 print()
615 print()
616 else:
617 missing.append(name)
618
619 if missing:
620 raise ValueError('Documentation is missing for modules: %s' %
621 ', '.join(missing))
Simon Glass639505b2018-09-14 04:57:11 -0600622
623 def GetUniqueName(self):
624 """Get a unique name for a node
625
626 Returns:
627 String containing a unique name for a node, consisting of the name
628 of all ancestors (starting from within the 'binman' node) separated
629 by a dot ('.'). This can be useful for generating unique filesnames
630 in the output directory.
631 """
632 name = self.name
633 node = self._node
634 while node.parent:
635 node = node.parent
636 if node.name == 'binman':
637 break
638 name = '%s.%s' % (node.name, name)
639 return name
Simon Glassfa79a812018-09-14 04:57:29 -0600640
641 def ExpandToLimit(self, limit):
642 """Expand an entry so that it ends at the given offset limit"""
643 if self.offset + self.size < limit:
644 self.size = limit - self.offset
645 # Request the contents again, since changing the size requires that
646 # the data grows. This should not fail, but check it to be sure.
647 if not self.ObtainContents():
648 self.Raise('Cannot obtain contents when expanding entry')
Simon Glassc4056b82019-07-08 13:18:38 -0600649
650 def HasSibling(self, name):
651 """Check if there is a sibling of a given name
652
653 Returns:
654 True if there is an entry with this name in the the same section,
655 else False
656 """
657 return name in self.section.GetEntries()
Simon Glasscec34ba2019-07-08 14:25:28 -0600658
659 def GetSiblingImagePos(self, name):
660 """Return the image position of the given sibling
661
662 Returns:
663 Image position of sibling, or None if the sibling has no position,
664 or False if there is no such sibling
665 """
666 if not self.HasSibling(name):
667 return False
668 return self.section.GetEntries()[name].image_pos
Simon Glass6b156f82019-07-08 14:25:43 -0600669
670 @staticmethod
671 def AddEntryInfo(entries, indent, name, etype, size, image_pos,
672 uncomp_size, offset, entry):
673 """Add a new entry to the entries list
674
675 Args:
676 entries: List (of EntryInfo objects) to add to
677 indent: Current indent level to add to list
678 name: Entry name (string)
679 etype: Entry type (string)
680 size: Entry size in bytes (int)
681 image_pos: Position within image in bytes (int)
682 uncomp_size: Uncompressed size if the entry uses compression, else
683 None
684 offset: Entry offset within parent in bytes (int)
685 entry: Entry object
686 """
687 entries.append(EntryInfo(indent, name, etype, size, image_pos,
688 uncomp_size, offset, entry))
689
690 def ListEntries(self, entries, indent):
691 """Add files in this entry to the list of entries
692
693 This can be overridden by subclasses which need different behaviour.
694
695 Args:
696 entries: List (of EntryInfo objects) to add to
697 indent: Current indent level to add to list
698 """
699 self.AddEntryInfo(entries, indent, self.name, self.etype, self.size,
700 self.image_pos, self.uncomp_size, self.offset, self)
Simon Glass4c613bf2019-07-08 14:25:50 -0600701
702 def ReadData(self, decomp=True):
703 """Read the data for an entry from the image
704
705 This is used when the image has been read in and we want to extract the
706 data for a particular entry from that image.
707
708 Args:
709 decomp: True to decompress any compressed data before returning it;
710 False to return the raw, uncompressed data
711
712 Returns:
713 Entry data (bytes)
714 """
715 # Use True here so that we get an uncompressed section to work from,
716 # although compressed sections are currently not supported
Simon Glass0cd8ace2019-07-20 12:24:04 -0600717 data = self.section.ReadChildData(self, decomp)
718 return data
Simon Glassaf8c45c2019-07-20 12:23:41 -0600719
720 def LoadData(self, decomp=True):
721 data = self.ReadData(decomp)
Simon Glass072959a2019-07-20 12:23:50 -0600722 self.contents_size = len(data)
Simon Glassaf8c45c2019-07-20 12:23:41 -0600723 self.ProcessContentsUpdate(data)
724 self.Detail('Loaded data size %x' % len(data))
Simon Glass990b1742019-07-20 12:23:46 -0600725
726 def GetImage(self):
727 """Get the image containing this entry
728
729 Returns:
730 Image object containing this entry
731 """
732 return self.section.GetImage()
Simon Glass072959a2019-07-20 12:23:50 -0600733
734 def WriteData(self, data, decomp=True):
735 """Write the data to an entry in the image
736
737 This is used when the image has been read in and we want to replace the
738 data for a particular entry in that image.
739
740 The image must be re-packed and written out afterwards.
741
742 Args:
743 data: Data to replace it with
744 decomp: True to compress the data if needed, False if data is
745 already compressed so should be used as is
746
747 Returns:
748 True if the data did not result in a resize of this entry, False if
749 the entry must be resized
750 """
751 self.contents_size = self.size
752 ok = self.ProcessContentsUpdate(data)
753 self.Detail('WriteData: size=%x, ok=%s' % (len(data), ok))
Simon Glassd34af7a2019-07-20 12:24:05 -0600754 section_ok = self.section.WriteChildData(self)
755 return ok and section_ok
756
757 def WriteChildData(self, child):
758 """Handle writing the data in a child entry
759
760 This should be called on the child's parent section after the child's
761 data has been updated. It
762
763 This base-class implementation does nothing, since the base Entry object
764 does not have any children.
765
766 Args:
767 child: Child Entry that was written
768
769 Returns:
770 True if the section could be updated successfully, False if the
771 data is such that the section could not updat
772 """
773 return True
Simon Glass11453762019-07-20 12:23:55 -0600774
775 def GetSiblingOrder(self):
776 """Get the relative order of an entry amoung its siblings
777
778 Returns:
779 'start' if this entry is first among siblings, 'end' if last,
780 otherwise None
781 """
782 entries = list(self.section.GetEntries().values())
783 if entries:
784 if self == entries[0]:
785 return 'start'
786 elif self == entries[-1]:
787 return 'end'
788 return 'middle'