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