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 | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 78 | if read_node: |
| 79 | self.ReadNode() |
| 80 | |
| 81 | @staticmethod |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 82 | def Lookup(section, node_path, etype): |
| 83 | """Look up the entry class for a node. |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 84 | |
| 85 | Args: |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 86 | section: Section object containing this node |
| 87 | node_node: Path name of Node object containing information about |
| 88 | the entry to create (used for errors) |
| 89 | etype: Entry type to use |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 90 | |
| 91 | Returns: |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 92 | The entry class object if found, else None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | """ |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 94 | # Convert something like 'u-boot@0' to 'u_boot' since we are only |
| 95 | # interested in the type. |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 96 | module_name = etype.replace('-', '_') |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 97 | if '@' in module_name: |
| 98 | module_name = module_name.split('@')[0] |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 99 | module = modules.get(module_name) |
| 100 | |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 101 | # Also allow entry-type modules to be brought in from the etype directory. |
| 102 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 103 | # Import the module if we have not already done so. |
| 104 | if not module: |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 105 | old_path = sys.path |
| 106 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 107 | try: |
| 108 | if have_importlib: |
| 109 | module = importlib.import_module(module_name) |
| 110 | else: |
| 111 | module = __import__(module_name) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 112 | except ImportError as e: |
| 113 | raise ValueError("Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" % |
| 114 | (etype, node_path, module_name, e)) |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 115 | finally: |
| 116 | sys.path = old_path |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 117 | modules[module_name] = module |
| 118 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 119 | # Look up the expected class name |
| 120 | return getattr(module, 'Entry_%s' % module_name) |
| 121 | |
| 122 | @staticmethod |
| 123 | def Create(section, node, etype=None): |
| 124 | """Create a new entry for a node. |
| 125 | |
| 126 | Args: |
| 127 | section: Section object containing this node |
| 128 | node: Node object containing information about the entry to |
| 129 | create |
| 130 | etype: Entry type to use, or None to work it out (used for tests) |
| 131 | |
| 132 | Returns: |
| 133 | A new Entry object of the correct type (a subclass of Entry) |
| 134 | """ |
| 135 | if not etype: |
| 136 | etype = fdt_util.GetString(node, 'type', node.name) |
| 137 | obj = Entry.Lookup(section, node.path, etype) |
| 138 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 139 | # Call its constructor to get the object we want. |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 140 | return obj(section, etype, node) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 141 | |
| 142 | def ReadNode(self): |
| 143 | """Read entry information from the node |
| 144 | |
| 145 | This reads all the fields we recognise from the node, ready for use. |
| 146 | """ |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 147 | if 'pos' in self._node.props: |
| 148 | self.Raise("Please use 'offset' instead of 'pos'") |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 149 | self.offset = fdt_util.GetInt(self._node, 'offset') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 150 | self.size = fdt_util.GetInt(self._node, 'size') |
| 151 | self.align = fdt_util.GetInt(self._node, 'align') |
| 152 | if tools.NotPowerOfTwo(self.align): |
| 153 | raise ValueError("Node '%s': Alignment %s must be a power of two" % |
| 154 | (self._node.path, self.align)) |
| 155 | self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0) |
| 156 | self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0) |
| 157 | self.align_size = fdt_util.GetInt(self._node, 'align-size') |
| 158 | if tools.NotPowerOfTwo(self.align_size): |
| 159 | raise ValueError("Node '%s': Alignment size %s must be a power " |
| 160 | "of two" % (self._node.path, self.align_size)) |
| 161 | self.align_end = fdt_util.GetInt(self._node, 'align-end') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 162 | self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 163 | |
Simon Glass | 3732ec3 | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 164 | def GetDefaultFilename(self): |
| 165 | return None |
| 166 | |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 167 | def AddMissingProperties(self): |
| 168 | """Add new properties to the device tree as needed for this entry""" |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 169 | for prop in ['offset', 'size', 'image-pos']: |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 170 | if not prop in self._node.props: |
| 171 | self._node.AddZeroProp(prop) |
| 172 | |
| 173 | def SetCalculatedProperties(self): |
| 174 | """Set the value of device-tree properties calculated by binman""" |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 175 | self._node.SetInt('offset', self.offset) |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 176 | self._node.SetInt('size', self.size) |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 177 | self._node.SetInt('image-pos', self.image_pos) |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 178 | |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 179 | def ProcessFdt(self, fdt): |
| 180 | return True |
| 181 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 182 | def SetPrefix(self, prefix): |
| 183 | """Set the name prefix for a node |
| 184 | |
| 185 | Args: |
| 186 | prefix: Prefix to set, or '' to not use a prefix |
| 187 | """ |
| 188 | if prefix: |
| 189 | self.name = prefix + self.name |
| 190 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 191 | def SetContents(self, data): |
| 192 | """Set the contents of an entry |
| 193 | |
| 194 | This sets both the data and content_size properties |
| 195 | |
| 196 | Args: |
| 197 | data: Data to set to the contents (string) |
| 198 | """ |
| 199 | self.data = data |
| 200 | self.contents_size = len(self.data) |
| 201 | |
| 202 | def ProcessContentsUpdate(self, data): |
| 203 | """Update the contens of an entry, after the size is fixed |
| 204 | |
| 205 | This checks that the new data is the same size as the old. |
| 206 | |
| 207 | Args: |
| 208 | data: Data to set to the contents (string) |
| 209 | |
| 210 | Raises: |
| 211 | ValueError if the new data size is not the same as the old |
| 212 | """ |
| 213 | if len(data) != self.contents_size: |
| 214 | self.Raise('Cannot update entry size from %d to %d' % |
| 215 | (len(data), self.contents_size)) |
| 216 | self.SetContents(data) |
| 217 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 218 | def ObtainContents(self): |
| 219 | """Figure out the contents of an entry. |
| 220 | |
| 221 | Returns: |
| 222 | True if the contents were found, False if another call is needed |
| 223 | after the other entries are processed. |
| 224 | """ |
| 225 | # No contents by default: subclasses can implement this |
| 226 | return True |
| 227 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 228 | def Pack(self, offset): |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 229 | """Figure out how to pack the entry into the section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 230 | |
| 231 | Most of the time the entries are not fully specified. There may be |
| 232 | an alignment but no size. In that case we take the size from the |
| 233 | contents of the entry. |
| 234 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 235 | 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] | 236 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 237 | Once this function is complete, both the offset and size of the |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 238 | entry will be know. |
| 239 | |
| 240 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 241 | Current section offset pointer |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 242 | |
| 243 | Returns: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 244 | New section offset pointer (after this entry) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 245 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 246 | if self.offset is None: |
| 247 | if self.offset_unset: |
| 248 | self.Raise('No offset set with offset-unset: should another ' |
| 249 | 'entry provide this correct offset?') |
| 250 | self.offset = tools.Align(offset, self.align) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 251 | needed = self.pad_before + self.contents_size + self.pad_after |
| 252 | needed = tools.Align(needed, self.align_size) |
| 253 | size = self.size |
| 254 | if not size: |
| 255 | size = needed |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 256 | new_offset = self.offset + size |
| 257 | aligned_offset = tools.Align(new_offset, self.align_end) |
| 258 | if aligned_offset != new_offset: |
| 259 | size = aligned_offset - self.offset |
| 260 | new_offset = aligned_offset |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 261 | |
| 262 | if not self.size: |
| 263 | self.size = size |
| 264 | |
| 265 | if self.size < needed: |
| 266 | self.Raise("Entry contents size is %#x (%d) but entry size is " |
| 267 | "%#x (%d)" % (needed, needed, self.size, self.size)) |
| 268 | # Check that the alignment is correct. It could be wrong if the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 269 | # and offset or size values were provided (i.e. not calculated), but |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 270 | # conflict with the provided alignment values |
| 271 | if self.size != tools.Align(self.size, self.align_size): |
| 272 | self.Raise("Size %#x (%d) does not match align-size %#x (%d)" % |
| 273 | (self.size, self.size, self.align_size, self.align_size)) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 274 | if self.offset != tools.Align(self.offset, self.align): |
| 275 | self.Raise("Offset %#x (%d) does not match align %#x (%d)" % |
| 276 | (self.offset, self.offset, self.align, self.align)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 277 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 278 | return new_offset |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 279 | |
| 280 | def Raise(self, msg): |
| 281 | """Convenience function to raise an error referencing a node""" |
| 282 | raise ValueError("Node '%s': %s" % (self._node.path, msg)) |
| 283 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 284 | def GetEntryArgsOrProps(self, props, required=False): |
| 285 | """Return the values of a set of properties |
| 286 | |
| 287 | Args: |
| 288 | props: List of EntryArg objects |
| 289 | |
| 290 | Raises: |
| 291 | ValueError if a property is not found |
| 292 | """ |
| 293 | values = [] |
| 294 | missing = [] |
| 295 | for prop in props: |
| 296 | python_prop = prop.name.replace('-', '_') |
| 297 | if hasattr(self, python_prop): |
| 298 | value = getattr(self, python_prop) |
| 299 | else: |
| 300 | value = None |
| 301 | if value is None: |
| 302 | value = self.GetArg(prop.name, prop.datatype) |
| 303 | if value is None and required: |
| 304 | missing.append(prop.name) |
| 305 | values.append(value) |
| 306 | if missing: |
| 307 | self.Raise('Missing required properties/entry args: %s' % |
| 308 | (', '.join(missing))) |
| 309 | return values |
| 310 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 311 | def GetPath(self): |
| 312 | """Get the path of a node |
| 313 | |
| 314 | Returns: |
| 315 | Full path of the node for this entry |
| 316 | """ |
| 317 | return self._node.path |
| 318 | |
| 319 | def GetData(self): |
| 320 | return self.data |
| 321 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 322 | def GetOffsets(self): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 323 | return {} |
| 324 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 325 | def SetOffsetSize(self, pos, size): |
| 326 | self.offset = pos |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 327 | self.size = size |
| 328 | |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 329 | def SetImagePos(self, image_pos): |
| 330 | """Set the position in the image |
| 331 | |
| 332 | Args: |
| 333 | image_pos: Position of this entry in the image |
| 334 | """ |
| 335 | self.image_pos = image_pos + self.offset |
| 336 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 337 | def ProcessContents(self): |
| 338 | pass |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 339 | |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 340 | def WriteSymbols(self, section): |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 341 | """Write symbol values into binary files for access at run time |
| 342 | |
| 343 | Args: |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 344 | section: Section containing the entry |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 345 | """ |
| 346 | pass |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 347 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 348 | def CheckOffset(self): |
| 349 | """Check that the entry offsets are correct |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 350 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 351 | This is used for entries which have extra offset requirements (other |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 352 | than having to be fully inside their section). Sub-classes can implement |
| 353 | this function and raise if there is a problem. |
| 354 | """ |
| 355 | pass |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 356 | |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 357 | @staticmethod |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 358 | def WriteMapLine(fd, indent, name, offset, size, image_pos): |
| 359 | print('%08x %s%08x %08x %s' % (image_pos, ' ' * indent, offset, |
| 360 | size, name), file=fd) |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 361 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 362 | def WriteMap(self, fd, indent): |
| 363 | """Write a map of the entry to a .map file |
| 364 | |
| 365 | Args: |
| 366 | fd: File to write the map to |
| 367 | indent: Curent indent level of map (0=none, 1=one level, etc.) |
| 368 | """ |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 369 | self.WriteMapLine(fd, indent, self.name, self.offset, self.size, |
| 370 | self.image_pos) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 371 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 372 | def GetEntries(self): |
| 373 | """Return a list of entries contained by this entry |
| 374 | |
| 375 | Returns: |
| 376 | List of entries, or None if none. A normal entry has no entries |
| 377 | within it so will return None |
| 378 | """ |
| 379 | return None |
| 380 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 381 | def GetArg(self, name, datatype=str): |
| 382 | """Get the value of an entry argument or device-tree-node property |
| 383 | |
| 384 | Some node properties can be provided as arguments to binman. First check |
| 385 | the entry arguments, and fall back to the device tree if not found |
| 386 | |
| 387 | Args: |
| 388 | name: Argument name |
| 389 | datatype: Data type (str or int) |
| 390 | |
| 391 | Returns: |
| 392 | Value of argument as a string or int, or None if no value |
| 393 | |
| 394 | Raises: |
| 395 | ValueError if the argument cannot be converted to in |
| 396 | """ |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame^] | 397 | value = state.GetEntryArg(name) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 398 | if value is not None: |
| 399 | if datatype == int: |
| 400 | try: |
| 401 | value = int(value) |
| 402 | except ValueError: |
| 403 | self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" % |
| 404 | (name, value)) |
| 405 | elif datatype == str: |
| 406 | pass |
| 407 | else: |
| 408 | raise ValueError("GetArg() internal error: Unknown data type '%s'" % |
| 409 | datatype) |
| 410 | else: |
| 411 | value = fdt_util.GetDatatype(self._node, name, datatype) |
| 412 | return value |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 413 | |
| 414 | @staticmethod |
| 415 | def WriteDocs(modules, test_missing=None): |
| 416 | """Write out documentation about the various entry types to stdout |
| 417 | |
| 418 | Args: |
| 419 | modules: List of modules to include |
| 420 | test_missing: Used for testing. This is a module to report |
| 421 | as missing |
| 422 | """ |
| 423 | print('''Binman Entry Documentation |
| 424 | =========================== |
| 425 | |
| 426 | This file describes the entry types supported by binman. These entry types can |
| 427 | be placed in an image one by one to build up a final firmware image. It is |
| 428 | fairly easy to create new entry types. Just add a new file to the 'etype' |
| 429 | directory. You can use the existing entries as examples. |
| 430 | |
| 431 | Note that some entries are subclasses of others, using and extending their |
| 432 | features to produce new behaviours. |
| 433 | |
| 434 | |
| 435 | ''') |
| 436 | modules = sorted(modules) |
| 437 | |
| 438 | # Don't show the test entry |
| 439 | if '_testing' in modules: |
| 440 | modules.remove('_testing') |
| 441 | missing = [] |
| 442 | for name in modules: |
| 443 | module = Entry.Lookup(name, name, name) |
| 444 | docs = getattr(module, '__doc__') |
| 445 | if test_missing == name: |
| 446 | docs = None |
| 447 | if docs: |
| 448 | lines = docs.splitlines() |
| 449 | first_line = lines[0] |
| 450 | rest = [line[4:] for line in lines[1:]] |
| 451 | hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line) |
| 452 | print(hdr) |
| 453 | print('-' * len(hdr)) |
| 454 | print('\n'.join(rest)) |
| 455 | print() |
| 456 | print() |
| 457 | else: |
| 458 | missing.append(name) |
| 459 | |
| 460 | if missing: |
| 461 | raise ValueError('Documentation is missing for modules: %s' % |
| 462 | ', '.join(missing)) |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 463 | |
| 464 | def GetUniqueName(self): |
| 465 | """Get a unique name for a node |
| 466 | |
| 467 | Returns: |
| 468 | String containing a unique name for a node, consisting of the name |
| 469 | of all ancestors (starting from within the 'binman' node) separated |
| 470 | by a dot ('.'). This can be useful for generating unique filesnames |
| 471 | in the output directory. |
| 472 | """ |
| 473 | name = self.name |
| 474 | node = self._node |
| 475 | while node.parent: |
| 476 | node = node.parent |
| 477 | if node.name == 'binman': |
| 478 | break |
| 479 | name = '%s.%s' % (node.name, name) |
| 480 | return name |