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