Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 4 | # Base class for all entries |
| 5 | # |
| 6 | |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 9 | from collections import namedtuple |
| 10 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 11 | # 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 |
| 14 | try: |
| 15 | import importlib |
| 16 | have_importlib = True |
| 17 | except: |
| 18 | have_importlib = False |
| 19 | |
Simon Glass | badf0ec | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 20 | import os |
| 21 | import sys |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 22 | |
| 23 | import fdt_util |
| 24 | import state |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 25 | import tools |
Simon Glass | eea264e | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 26 | import tout |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 27 | |
| 28 | modules = {} |
| 29 | |
Simon Glass | badf0ec | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 30 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 31 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 32 | |
| 33 | # An argument which can be passed to entries on the command line, in lieu of |
| 34 | # device-tree properties. |
| 35 | EntryArg = namedtuple('EntryArg', ['name', 'datatype']) |
| 36 | |
Simon Glass | 41b8ba0 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 37 | # Information about an entry for use when displaying summaries |
| 38 | EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size', |
| 39 | 'image_pos', 'uncomp_size', 'offset', |
| 40 | 'entry']) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 41 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 42 | class Entry(object): |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 43 | """An Entry in the section |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 44 | |
| 45 | An entry corresponds to a single node in the device-tree description |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 46 | of the section. Each entry ends up being a part of the final section. |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 47 | Entries can be placed either right next to each other, or with padding |
| 48 | between them. The type of the entry determines the data that is in it. |
| 49 | |
| 50 | This class is not used by itself. All entry objects are subclasses of |
| 51 | Entry. |
| 52 | |
| 53 | Attributes: |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 54 | section: Section object containing this entry |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 55 | node: The node that created this entry |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 56 | offset: Offset of entry within the section, None if not known yet (in |
| 57 | which case it will be calculated by Pack()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 58 | size: Entry size in bytes, None if not known |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 59 | uncomp_size: Size of uncompressed data in bytes, if the entry is |
| 60 | compressed, else None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 61 | contents_size: Size of contents in bytes, 0 by default |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 62 | align: Entry start offset alignment, or None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 63 | align_size: Entry size alignment, or None |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 64 | align_end: Entry end offset alignment, or None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 65 | pad_before: Number of pad bytes before the contents, 0 if none |
| 66 | pad_after: Number of pad bytes after the contents, 0 if none |
| 67 | data: Contents of entry (string of bytes) |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 68 | compress: Compression algoithm used (e.g. 'lz4'), 'none' if none |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 69 | orig_offset: Original offset value read from node |
| 70 | orig_size: Original size value read from node |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 71 | """ |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 72 | def __init__(self, section, etype, node, read_node=True, name_prefix=''): |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 73 | self.section = section |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 74 | self.etype = etype |
| 75 | self._node = node |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 76 | self.name = node and (name_prefix + node.name) or 'none' |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 77 | self.offset = None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 78 | self.size = None |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 79 | self.uncomp_size = None |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 80 | self.data = None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 81 | self.contents_size = 0 |
| 82 | self.align = None |
| 83 | self.align_size = None |
| 84 | self.align_end = None |
| 85 | self.pad_before = 0 |
| 86 | self.pad_after = 0 |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 87 | self.offset_unset = False |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 88 | self.image_pos = None |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 89 | self._expand_size = False |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 90 | self.compress = 'none' |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 91 | if read_node: |
| 92 | self.ReadNode() |
| 93 | |
| 94 | @staticmethod |
Simon Glass | c073ced | 2019-07-08 14:25:31 -0600 | [diff] [blame] | 95 | def Lookup(node_path, etype): |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 96 | """Look up the entry class for a node. |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 97 | |
| 98 | Args: |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 99 | node_node: Path name of Node object containing information about |
| 100 | the entry to create (used for errors) |
| 101 | etype: Entry type to use |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 102 | |
| 103 | Returns: |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 104 | The entry class object if found, else None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 105 | """ |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 106 | # Convert something like 'u-boot@0' to 'u_boot' since we are only |
| 107 | # interested in the type. |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 108 | module_name = etype.replace('-', '_') |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 109 | if '@' in module_name: |
| 110 | module_name = module_name.split('@')[0] |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 111 | module = modules.get(module_name) |
| 112 | |
Simon Glass | badf0ec | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 113 | # Also allow entry-type modules to be brought in from the etype directory. |
| 114 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 115 | # Import the module if we have not already done so. |
| 116 | if not module: |
Simon Glass | badf0ec | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 117 | old_path = sys.path |
| 118 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 119 | try: |
| 120 | if have_importlib: |
| 121 | module = importlib.import_module(module_name) |
| 122 | else: |
| 123 | module = __import__(module_name) |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 124 | except ImportError as e: |
| 125 | raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" % |
| 126 | (etype, node_path, module_name, e)) |
Simon Glass | badf0ec | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 127 | finally: |
| 128 | sys.path = old_path |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 129 | modules[module_name] = module |
| 130 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 131 | # Look up the expected class name |
| 132 | return getattr(module, 'Entry_%s' % module_name) |
| 133 | |
| 134 | @staticmethod |
| 135 | def Create(section, node, etype=None): |
| 136 | """Create a new entry for a node. |
| 137 | |
| 138 | Args: |
| 139 | section: Section object containing this node |
| 140 | node: Node object containing information about the entry to |
| 141 | create |
| 142 | etype: Entry type to use, or None to work it out (used for tests) |
| 143 | |
| 144 | Returns: |
| 145 | A new Entry object of the correct type (a subclass of Entry) |
| 146 | """ |
| 147 | if not etype: |
| 148 | etype = fdt_util.GetString(node, 'type', node.name) |
Simon Glass | c073ced | 2019-07-08 14:25:31 -0600 | [diff] [blame] | 149 | obj = Entry.Lookup(node.path, etype) |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 150 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 151 | # Call its constructor to get the object we want. |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 152 | return obj(section, etype, node) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 153 | |
| 154 | def ReadNode(self): |
| 155 | """Read entry information from the node |
| 156 | |
| 157 | This reads all the fields we recognise from the node, ready for use. |
| 158 | """ |
Simon Glass | 15a587c | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 159 | if 'pos' in self._node.props: |
| 160 | self.Raise("Please use 'offset' instead of 'pos'") |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 161 | self.offset = fdt_util.GetInt(self._node, 'offset') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 162 | self.size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 163 | self.orig_offset = self.offset |
| 164 | self.orig_size = self.size |
| 165 | |
Simon Glass | ffded75 | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 166 | # These should not be set in input files, but are set in an FDT map, |
| 167 | # which is also read by this code. |
| 168 | self.image_pos = fdt_util.GetInt(self._node, 'image-pos') |
| 169 | self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size') |
| 170 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 171 | self.align = fdt_util.GetInt(self._node, 'align') |
| 172 | if tools.NotPowerOfTwo(self.align): |
| 173 | raise ValueError("Node '%s': Alignment %s must be a power of two" % |
| 174 | (self._node.path, self.align)) |
| 175 | self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0) |
| 176 | self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0) |
| 177 | self.align_size = fdt_util.GetInt(self._node, 'align-size') |
| 178 | if tools.NotPowerOfTwo(self.align_size): |
Simon Glass | 8beb11e | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 179 | self.Raise("Alignment size %s must be a power of two" % |
| 180 | self.align_size) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 181 | self.align_end = fdt_util.GetInt(self._node, 'align-end') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 182 | self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset') |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 183 | self.expand_size = fdt_util.GetBool(self._node, 'expand-size') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 184 | |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 185 | def GetDefaultFilename(self): |
| 186 | return None |
| 187 | |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 188 | def GetFdtSet(self): |
| 189 | """Get the set of device trees used by this entry |
| 190 | |
| 191 | Returns: |
| 192 | Set containing the filename from this entry, if it is a .dtb, else |
| 193 | an empty set |
| 194 | """ |
| 195 | fname = self.GetDefaultFilename() |
| 196 | # It would be better to use isinstance(self, Entry_blob_dtb) here but |
| 197 | # we cannot access Entry_blob_dtb |
| 198 | if fname and fname.endswith('.dtb'): |
Simon Glass | d141f6c | 2019-05-14 15:53:39 -0600 | [diff] [blame] | 199 | return set([fname]) |
| 200 | return set() |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 201 | |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 202 | def ExpandEntries(self): |
| 203 | pass |
| 204 | |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 205 | def AddMissingProperties(self): |
| 206 | """Add new properties to the device tree as needed for this entry""" |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 207 | for prop in ['offset', 'size', 'image-pos']: |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 208 | if not prop in self._node.props: |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 209 | state.AddZeroProp(self._node, prop) |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 210 | if self.compress != 'none': |
| 211 | state.AddZeroProp(self._node, 'uncomp-size') |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 212 | err = state.CheckAddHashProp(self._node) |
| 213 | if err: |
| 214 | self.Raise(err) |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 215 | |
| 216 | def SetCalculatedProperties(self): |
| 217 | """Set the value of device-tree properties calculated by binman""" |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 218 | state.SetInt(self._node, 'offset', self.offset) |
| 219 | state.SetInt(self._node, 'size', self.size) |
Simon Glass | 8beb11e | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 220 | base = self.section.GetRootSkipAtStart() if self.section else 0 |
| 221 | state.SetInt(self._node, 'image-pos', self.image_pos - base) |
Simon Glass | 8287ee8 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 222 | if self.uncomp_size is not None: |
| 223 | state.SetInt(self._node, 'uncomp-size', self.uncomp_size) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 224 | state.CheckSetHashValue(self._node, self.GetData) |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 225 | |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 226 | def ProcessFdt(self, fdt): |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 227 | """Allow entries to adjust the device tree |
| 228 | |
| 229 | Some entries need to adjust the device tree for their purposes. This |
| 230 | may involve adding or deleting properties. |
| 231 | |
| 232 | Returns: |
| 233 | True if processing is complete |
| 234 | False if processing could not be completed due to a dependency. |
| 235 | This will cause the entry to be retried after others have been |
| 236 | called |
| 237 | """ |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 238 | return True |
| 239 | |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 240 | def SetPrefix(self, prefix): |
| 241 | """Set the name prefix for a node |
| 242 | |
| 243 | Args: |
| 244 | prefix: Prefix to set, or '' to not use a prefix |
| 245 | """ |
| 246 | if prefix: |
| 247 | self.name = prefix + self.name |
| 248 | |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 249 | def SetContents(self, data): |
| 250 | """Set the contents of an entry |
| 251 | |
| 252 | This sets both the data and content_size properties |
| 253 | |
| 254 | Args: |
Simon Glass | 5b463fc | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 255 | data: Data to set to the contents (bytes) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 256 | """ |
| 257 | self.data = data |
| 258 | self.contents_size = len(self.data) |
| 259 | |
| 260 | def ProcessContentsUpdate(self, data): |
Simon Glass | 5b463fc | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 261 | """Update the contents of an entry, after the size is fixed |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 262 | |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 263 | This checks that the new data is the same size as the old. If the size |
| 264 | has changed, this triggers a re-run of the packing algorithm. |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 265 | |
| 266 | Args: |
Simon Glass | 5b463fc | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 267 | data: Data to set to the contents (bytes) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 268 | |
| 269 | Raises: |
| 270 | ValueError if the new data size is not the same as the old |
| 271 | """ |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 272 | size_ok = True |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 273 | new_size = len(data) |
| 274 | if state.AllowEntryExpansion(): |
| 275 | if new_size > self.contents_size: |
Simon Glass | eea264e | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 276 | tout.Debug("Entry '%s' size change from %#x to %#x" % ( |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 277 | self._node.path, self.contents_size, new_size)) |
| 278 | # self.data will indicate the new size needed |
| 279 | size_ok = False |
| 280 | elif new_size != self.contents_size: |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 281 | self.Raise('Cannot update entry size from %d to %d' % |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 282 | (self.contents_size, new_size)) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 283 | self.SetContents(data) |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 284 | return size_ok |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 285 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 286 | def ObtainContents(self): |
| 287 | """Figure out the contents of an entry. |
| 288 | |
| 289 | Returns: |
| 290 | True if the contents were found, False if another call is needed |
| 291 | after the other entries are processed. |
| 292 | """ |
| 293 | # No contents by default: subclasses can implement this |
| 294 | return True |
| 295 | |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 296 | def ResetForPack(self): |
| 297 | """Reset offset/size fields so that packing can be done again""" |
| 298 | self.offset = self.orig_offset |
| 299 | self.size = self.orig_size |
| 300 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 301 | def Pack(self, offset): |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 302 | """Figure out how to pack the entry into the section |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 303 | |
| 304 | Most of the time the entries are not fully specified. There may be |
| 305 | an alignment but no size. In that case we take the size from the |
| 306 | contents of the entry. |
| 307 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 308 | If an entry has no hard-coded offset, it will be placed at @offset. |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 309 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 310 | Once this function is complete, both the offset and size of the |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 311 | entry will be know. |
| 312 | |
| 313 | Args: |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 314 | Current section offset pointer |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 315 | |
| 316 | Returns: |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 317 | New section offset pointer (after this entry) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 318 | """ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 319 | if self.offset is None: |
| 320 | if self.offset_unset: |
| 321 | self.Raise('No offset set with offset-unset: should another ' |
| 322 | 'entry provide this correct offset?') |
| 323 | self.offset = tools.Align(offset, self.align) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 324 | needed = self.pad_before + self.contents_size + self.pad_after |
| 325 | needed = tools.Align(needed, self.align_size) |
| 326 | size = self.size |
| 327 | if not size: |
| 328 | size = needed |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 329 | new_offset = self.offset + size |
| 330 | aligned_offset = tools.Align(new_offset, self.align_end) |
| 331 | if aligned_offset != new_offset: |
| 332 | size = aligned_offset - self.offset |
| 333 | new_offset = aligned_offset |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 334 | |
| 335 | if not self.size: |
| 336 | self.size = size |
| 337 | |
| 338 | if self.size < needed: |
| 339 | self.Raise("Entry contents size is %#x (%d) but entry size is " |
| 340 | "%#x (%d)" % (needed, needed, self.size, self.size)) |
| 341 | # Check that the alignment is correct. It could be wrong if the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 342 | # and offset or size values were provided (i.e. not calculated), but |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 343 | # conflict with the provided alignment values |
| 344 | if self.size != tools.Align(self.size, self.align_size): |
| 345 | self.Raise("Size %#x (%d) does not match align-size %#x (%d)" % |
| 346 | (self.size, self.size, self.align_size, self.align_size)) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 347 | if self.offset != tools.Align(self.offset, self.align): |
| 348 | self.Raise("Offset %#x (%d) does not match align %#x (%d)" % |
| 349 | (self.offset, self.offset, self.align, self.align)) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 350 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 351 | return new_offset |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 352 | |
| 353 | def Raise(self, msg): |
| 354 | """Convenience function to raise an error referencing a node""" |
| 355 | raise ValueError("Node '%s': %s" % (self._node.path, msg)) |
| 356 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 357 | def GetEntryArgsOrProps(self, props, required=False): |
| 358 | """Return the values of a set of properties |
| 359 | |
| 360 | Args: |
| 361 | props: List of EntryArg objects |
| 362 | |
| 363 | Raises: |
| 364 | ValueError if a property is not found |
| 365 | """ |
| 366 | values = [] |
| 367 | missing = [] |
| 368 | for prop in props: |
| 369 | python_prop = prop.name.replace('-', '_') |
| 370 | if hasattr(self, python_prop): |
| 371 | value = getattr(self, python_prop) |
| 372 | else: |
| 373 | value = None |
| 374 | if value is None: |
| 375 | value = self.GetArg(prop.name, prop.datatype) |
| 376 | if value is None and required: |
| 377 | missing.append(prop.name) |
| 378 | values.append(value) |
| 379 | if missing: |
| 380 | self.Raise('Missing required properties/entry args: %s' % |
| 381 | (', '.join(missing))) |
| 382 | return values |
| 383 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 384 | def GetPath(self): |
| 385 | """Get the path of a node |
| 386 | |
| 387 | Returns: |
| 388 | Full path of the node for this entry |
| 389 | """ |
| 390 | return self._node.path |
| 391 | |
| 392 | def GetData(self): |
| 393 | return self.data |
| 394 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 395 | def GetOffsets(self): |
Simon Glass | ed7dd5e | 2019-07-08 13:18:30 -0600 | [diff] [blame] | 396 | """Get the offsets for siblings |
| 397 | |
| 398 | Some entry types can contain information about the position or size of |
| 399 | other entries. An example of this is the Intel Flash Descriptor, which |
| 400 | knows where the Intel Management Engine section should go. |
| 401 | |
| 402 | If this entry knows about the position of other entries, it can specify |
| 403 | this by returning values here |
| 404 | |
| 405 | Returns: |
| 406 | Dict: |
| 407 | key: Entry type |
| 408 | value: List containing position and size of the given entry |
Simon Glass | cf54904 | 2019-07-08 13:18:39 -0600 | [diff] [blame] | 409 | type. Either can be None if not known |
Simon Glass | ed7dd5e | 2019-07-08 13:18:30 -0600 | [diff] [blame] | 410 | """ |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 411 | return {} |
| 412 | |
Simon Glass | cf54904 | 2019-07-08 13:18:39 -0600 | [diff] [blame] | 413 | def SetOffsetSize(self, offset, size): |
| 414 | """Set the offset and/or size of an entry |
| 415 | |
| 416 | Args: |
| 417 | offset: New offset, or None to leave alone |
| 418 | size: New size, or None to leave alone |
| 419 | """ |
| 420 | if offset is not None: |
| 421 | self.offset = offset |
| 422 | if size is not None: |
| 423 | self.size = size |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 424 | |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 425 | def SetImagePos(self, image_pos): |
| 426 | """Set the position in the image |
| 427 | |
| 428 | Args: |
| 429 | image_pos: Position of this entry in the image |
| 430 | """ |
| 431 | self.image_pos = image_pos + self.offset |
| 432 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 433 | def ProcessContents(self): |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 434 | """Do any post-packing updates of entry contents |
| 435 | |
| 436 | This function should call ProcessContentsUpdate() to update the entry |
| 437 | contents, if necessary, returning its return value here. |
| 438 | |
| 439 | Args: |
| 440 | data: Data to set to the contents (bytes) |
| 441 | |
| 442 | Returns: |
| 443 | True if the new data size is OK, False if expansion is needed |
| 444 | |
| 445 | Raises: |
| 446 | ValueError if the new data size is not the same as the old and |
| 447 | state.AllowEntryExpansion() is False |
| 448 | """ |
| 449 | return True |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 450 | |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 451 | def WriteSymbols(self, section): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 452 | """Write symbol values into binary files for access at run time |
| 453 | |
| 454 | Args: |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 455 | section: Section containing the entry |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 456 | """ |
| 457 | pass |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 458 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 459 | def CheckOffset(self): |
| 460 | """Check that the entry offsets are correct |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 461 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 462 | This is used for entries which have extra offset requirements (other |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 463 | than having to be fully inside their section). Sub-classes can implement |
| 464 | this function and raise if there is a problem. |
| 465 | """ |
| 466 | pass |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 467 | |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 468 | @staticmethod |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 469 | def GetStr(value): |
| 470 | if value is None: |
| 471 | return '<none> ' |
| 472 | return '%08x' % value |
| 473 | |
| 474 | @staticmethod |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 475 | def WriteMapLine(fd, indent, name, offset, size, image_pos): |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 476 | print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent, |
| 477 | Entry.GetStr(offset), Entry.GetStr(size), |
| 478 | name), file=fd) |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 479 | |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 480 | def WriteMap(self, fd, indent): |
| 481 | """Write a map of the entry to a .map file |
| 482 | |
| 483 | Args: |
| 484 | fd: File to write the map to |
| 485 | indent: Curent indent level of map (0=none, 1=one level, etc.) |
| 486 | """ |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 487 | self.WriteMapLine(fd, indent, self.name, self.offset, self.size, |
| 488 | self.image_pos) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 489 | |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 490 | def GetEntries(self): |
| 491 | """Return a list of entries contained by this entry |
| 492 | |
| 493 | Returns: |
| 494 | List of entries, or None if none. A normal entry has no entries |
| 495 | within it so will return None |
| 496 | """ |
| 497 | return None |
| 498 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 499 | def GetArg(self, name, datatype=str): |
| 500 | """Get the value of an entry argument or device-tree-node property |
| 501 | |
| 502 | Some node properties can be provided as arguments to binman. First check |
| 503 | the entry arguments, and fall back to the device tree if not found |
| 504 | |
| 505 | Args: |
| 506 | name: Argument name |
| 507 | datatype: Data type (str or int) |
| 508 | |
| 509 | Returns: |
| 510 | Value of argument as a string or int, or None if no value |
| 511 | |
| 512 | Raises: |
| 513 | ValueError if the argument cannot be converted to in |
| 514 | """ |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 515 | value = state.GetEntryArg(name) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 516 | if value is not None: |
| 517 | if datatype == int: |
| 518 | try: |
| 519 | value = int(value) |
| 520 | except ValueError: |
| 521 | self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" % |
| 522 | (name, value)) |
| 523 | elif datatype == str: |
| 524 | pass |
| 525 | else: |
| 526 | raise ValueError("GetArg() internal error: Unknown data type '%s'" % |
| 527 | datatype) |
| 528 | else: |
| 529 | value = fdt_util.GetDatatype(self._node, name, datatype) |
| 530 | return value |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 531 | |
| 532 | @staticmethod |
| 533 | def WriteDocs(modules, test_missing=None): |
| 534 | """Write out documentation about the various entry types to stdout |
| 535 | |
| 536 | Args: |
| 537 | modules: List of modules to include |
| 538 | test_missing: Used for testing. This is a module to report |
| 539 | as missing |
| 540 | """ |
| 541 | print('''Binman Entry Documentation |
| 542 | =========================== |
| 543 | |
| 544 | This file describes the entry types supported by binman. These entry types can |
| 545 | be placed in an image one by one to build up a final firmware image. It is |
| 546 | fairly easy to create new entry types. Just add a new file to the 'etype' |
| 547 | directory. You can use the existing entries as examples. |
| 548 | |
| 549 | Note that some entries are subclasses of others, using and extending their |
| 550 | features to produce new behaviours. |
| 551 | |
| 552 | |
| 553 | ''') |
| 554 | modules = sorted(modules) |
| 555 | |
| 556 | # Don't show the test entry |
| 557 | if '_testing' in modules: |
| 558 | modules.remove('_testing') |
| 559 | missing = [] |
| 560 | for name in modules: |
Simon Glass | e430440 | 2019-07-08 14:25:32 -0600 | [diff] [blame] | 561 | if name.startswith('__'): |
| 562 | continue |
Simon Glass | c073ced | 2019-07-08 14:25:31 -0600 | [diff] [blame] | 563 | module = Entry.Lookup(name, name) |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 564 | docs = getattr(module, '__doc__') |
| 565 | if test_missing == name: |
| 566 | docs = None |
| 567 | if docs: |
| 568 | lines = docs.splitlines() |
| 569 | first_line = lines[0] |
| 570 | rest = [line[4:] for line in lines[1:]] |
| 571 | hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line) |
| 572 | print(hdr) |
| 573 | print('-' * len(hdr)) |
| 574 | print('\n'.join(rest)) |
| 575 | print() |
| 576 | print() |
| 577 | else: |
| 578 | missing.append(name) |
| 579 | |
| 580 | if missing: |
| 581 | raise ValueError('Documentation is missing for modules: %s' % |
| 582 | ', '.join(missing)) |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 583 | |
| 584 | def GetUniqueName(self): |
| 585 | """Get a unique name for a node |
| 586 | |
| 587 | Returns: |
| 588 | String containing a unique name for a node, consisting of the name |
| 589 | of all ancestors (starting from within the 'binman' node) separated |
| 590 | by a dot ('.'). This can be useful for generating unique filesnames |
| 591 | in the output directory. |
| 592 | """ |
| 593 | name = self.name |
| 594 | node = self._node |
| 595 | while node.parent: |
| 596 | node = node.parent |
| 597 | if node.name == 'binman': |
| 598 | break |
| 599 | name = '%s.%s' % (node.name, name) |
| 600 | return name |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 601 | |
| 602 | def ExpandToLimit(self, limit): |
| 603 | """Expand an entry so that it ends at the given offset limit""" |
| 604 | if self.offset + self.size < limit: |
| 605 | self.size = limit - self.offset |
| 606 | # Request the contents again, since changing the size requires that |
| 607 | # the data grows. This should not fail, but check it to be sure. |
| 608 | if not self.ObtainContents(): |
| 609 | self.Raise('Cannot obtain contents when expanding entry') |
Simon Glass | fa1c937 | 2019-07-08 13:18:38 -0600 | [diff] [blame] | 610 | |
| 611 | def HasSibling(self, name): |
| 612 | """Check if there is a sibling of a given name |
| 613 | |
| 614 | Returns: |
| 615 | True if there is an entry with this name in the the same section, |
| 616 | else False |
| 617 | """ |
| 618 | return name in self.section.GetEntries() |
Simon Glass | cf22894 | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 619 | |
| 620 | def GetSiblingImagePos(self, name): |
| 621 | """Return the image position of the given sibling |
| 622 | |
| 623 | Returns: |
| 624 | Image position of sibling, or None if the sibling has no position, |
| 625 | or False if there is no such sibling |
| 626 | """ |
| 627 | if not self.HasSibling(name): |
| 628 | return False |
| 629 | return self.section.GetEntries()[name].image_pos |
Simon Glass | 41b8ba0 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 630 | |
| 631 | @staticmethod |
| 632 | def AddEntryInfo(entries, indent, name, etype, size, image_pos, |
| 633 | uncomp_size, offset, entry): |
| 634 | """Add a new entry to the entries list |
| 635 | |
| 636 | Args: |
| 637 | entries: List (of EntryInfo objects) to add to |
| 638 | indent: Current indent level to add to list |
| 639 | name: Entry name (string) |
| 640 | etype: Entry type (string) |
| 641 | size: Entry size in bytes (int) |
| 642 | image_pos: Position within image in bytes (int) |
| 643 | uncomp_size: Uncompressed size if the entry uses compression, else |
| 644 | None |
| 645 | offset: Entry offset within parent in bytes (int) |
| 646 | entry: Entry object |
| 647 | """ |
| 648 | entries.append(EntryInfo(indent, name, etype, size, image_pos, |
| 649 | uncomp_size, offset, entry)) |
| 650 | |
| 651 | def ListEntries(self, entries, indent): |
| 652 | """Add files in this entry to the list of entries |
| 653 | |
| 654 | This can be overridden by subclasses which need different behaviour. |
| 655 | |
| 656 | Args: |
| 657 | entries: List (of EntryInfo objects) to add to |
| 658 | indent: Current indent level to add to list |
| 659 | """ |
| 660 | self.AddEntryInfo(entries, indent, self.name, self.etype, self.size, |
| 661 | self.image_pos, self.uncomp_size, self.offset, self) |
Simon Glass | f667e45 | 2019-07-08 14:25:50 -0600 | [diff] [blame^] | 662 | |
| 663 | def ReadData(self, decomp=True): |
| 664 | """Read the data for an entry from the image |
| 665 | |
| 666 | This is used when the image has been read in and we want to extract the |
| 667 | data for a particular entry from that image. |
| 668 | |
| 669 | Args: |
| 670 | decomp: True to decompress any compressed data before returning it; |
| 671 | False to return the raw, uncompressed data |
| 672 | |
| 673 | Returns: |
| 674 | Entry data (bytes) |
| 675 | """ |
| 676 | # Use True here so that we get an uncompressed section to work from, |
| 677 | # although compressed sections are currently not supported |
| 678 | data = self.section.ReadData(True) |
| 679 | tout.Info('%s: Reading data from offset %#x-%#x, size %#x (avail %#x)' % |
| 680 | (self.GetPath(), self.offset, self.offset + self.size, |
| 681 | self.size, len(data))) |
| 682 | return data[self.offset:self.offset + self.size] |