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