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 | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 7 | from collections import namedtuple |
Simon Glass | 7ccca83 | 2019-10-31 07:42:59 -0600 | [diff] [blame] | 8 | import importlib |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 9 | import os |
Simon Glass | 7a602fd | 2022-01-12 13:10:36 -0700 | [diff] [blame] | 10 | import pathlib |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 11 | import sys |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 12 | |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 13 | from binman import bintool |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 14 | from binman import comp_util |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 15 | from dtoc import fdt_util |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 16 | from patman import tools |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 17 | from patman.tools import to_hex, to_hex_size |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 18 | from patman import tout |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 19 | |
| 20 | modules = {} |
| 21 | |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 22 | # This is imported if needed |
| 23 | state = None |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 24 | |
| 25 | # An argument which can be passed to entries on the command line, in lieu of |
| 26 | # device-tree properties. |
| 27 | EntryArg = namedtuple('EntryArg', ['name', 'datatype']) |
| 28 | |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 29 | # Information about an entry for use when displaying summaries |
| 30 | EntryInfo = namedtuple('EntryInfo', ['indent', 'name', 'etype', 'size', |
| 31 | 'image_pos', 'uncomp_size', 'offset', |
| 32 | 'entry']) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 33 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 34 | class Entry(object): |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 35 | """An Entry in the section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 36 | |
| 37 | An entry corresponds to a single node in the device-tree description |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 38 | 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] | 39 | Entries can be placed either right next to each other, or with padding |
| 40 | between them. The type of the entry determines the data that is in it. |
| 41 | |
| 42 | This class is not used by itself. All entry objects are subclasses of |
| 43 | Entry. |
| 44 | |
| 45 | Attributes: |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 46 | section: Section object containing this entry |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 47 | node: The node that created this entry |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 48 | offset: Offset of entry within the section, None if not known yet (in |
| 49 | which case it will be calculated by Pack()) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 50 | size: Entry size in bytes, None if not known |
Simon Glass | 1fdb487 | 2019-10-31 07:43:02 -0600 | [diff] [blame] | 51 | pre_reset_size: size as it was before ResetForPack(). This allows us to |
| 52 | keep track of the size we started with and detect size changes |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 53 | uncomp_size: Size of uncompressed data in bytes, if the entry is |
| 54 | compressed, else None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 55 | contents_size: Size of contents in bytes, 0 by default |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 56 | align: Entry start offset alignment relative to the start of the |
| 57 | containing section, or None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 58 | align_size: Entry size alignment, or None |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 59 | align_end: Entry end offset alignment relative to the start of the |
| 60 | containing section, or None |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 61 | pad_before: Number of pad bytes before the contents when it is placed |
| 62 | in the containing section, 0 if none. The pad bytes become part of |
| 63 | the entry. |
| 64 | pad_after: Number of pad bytes after the contents when it is placed in |
| 65 | the containing section, 0 if none. The pad bytes become part of |
| 66 | the entry. |
| 67 | data: Contents of entry (string of bytes). This does not include |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 68 | padding created by pad_before or pad_after. If the entry is |
| 69 | compressed, this contains the compressed data. |
| 70 | uncomp_data: Original uncompressed data, if this entry is compressed, |
| 71 | else None |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 72 | compress: Compression algoithm used (e.g. 'lz4'), 'none' if none |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 73 | orig_offset: Original offset value read from node |
| 74 | orig_size: Original size value read from node |
Simon Glass | b8f9037 | 2020-09-01 05:13:57 -0600 | [diff] [blame] | 75 | missing: True if this entry is missing its contents |
| 76 | allow_missing: Allow children of this entry to be missing (used by |
| 77 | subclasses such as Entry_section) |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 78 | allow_fake: Allow creating a dummy fake file if the blob file is not |
| 79 | available. This is mainly used for testing. |
Simon Glass | b8f9037 | 2020-09-01 05:13:57 -0600 | [diff] [blame] | 80 | external: True if this entry contains an external binary blob |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 81 | bintools: Bintools used by this entry (only populated for Image) |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 82 | missing_bintools: List of missing bintools for this entry |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 83 | update_hash: True if this entry's "hash" subnode should be |
| 84 | updated with a hash of the entry contents |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 85 | """ |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 86 | def __init__(self, section, etype, node, name_prefix=''): |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 87 | # Put this here to allow entry-docs and help to work without libfdt |
| 88 | global state |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 89 | from binman import state |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 90 | |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 91 | self.section = section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 92 | self.etype = etype |
| 93 | self._node = node |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 94 | self.name = node and (name_prefix + node.name) or 'none' |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 95 | self.offset = None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 96 | self.size = None |
Simon Glass | 1fdb487 | 2019-10-31 07:43:02 -0600 | [diff] [blame] | 97 | self.pre_reset_size = None |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 98 | self.uncomp_size = None |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 99 | self.data = None |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 100 | self.uncomp_data = None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 101 | self.contents_size = 0 |
| 102 | self.align = None |
| 103 | self.align_size = None |
| 104 | self.align_end = None |
| 105 | self.pad_before = 0 |
| 106 | self.pad_after = 0 |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 107 | self.offset_unset = False |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 108 | self.image_pos = None |
Simon Glass | 9ed7170 | 2021-11-23 11:03:43 -0700 | [diff] [blame] | 109 | self.expand_size = False |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 110 | self.compress = 'none' |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 111 | self.missing = False |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 112 | self.faked = False |
Simon Glass | b8f9037 | 2020-09-01 05:13:57 -0600 | [diff] [blame] | 113 | self.external = False |
| 114 | self.allow_missing = False |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 115 | self.allow_fake = False |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 116 | self.bintools = {} |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 117 | self.missing_bintools = [] |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 118 | self.update_hash = True |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 119 | |
| 120 | @staticmethod |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 121 | def FindEntryClass(etype, expanded): |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 122 | """Look up the entry class for a node. |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 123 | |
| 124 | Args: |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 125 | node_node: Path name of Node object containing information about |
| 126 | the entry to create (used for errors) |
| 127 | etype: Entry type to use |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 128 | expanded: Use the expanded version of etype |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 129 | |
| 130 | Returns: |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 131 | The entry class object if found, else None if not found and expanded |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 132 | is True, else a tuple: |
| 133 | module name that could not be found |
| 134 | exception received |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 135 | """ |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 136 | # Convert something like 'u-boot@0' to 'u_boot' since we are only |
| 137 | # interested in the type. |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 138 | module_name = etype.replace('-', '_') |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 139 | |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 140 | if '@' in module_name: |
| 141 | module_name = module_name.split('@')[0] |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 142 | if expanded: |
| 143 | module_name += '_expanded' |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 144 | module = modules.get(module_name) |
| 145 | |
Simon Glass | 691198c | 2018-06-01 09:38:15 -0600 | [diff] [blame] | 146 | # Also allow entry-type modules to be brought in from the etype directory. |
| 147 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 148 | # Import the module if we have not already done so. |
| 149 | if not module: |
| 150 | try: |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 151 | module = importlib.import_module('binman.etype.' + module_name) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 152 | except ImportError as e: |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 153 | if expanded: |
| 154 | return None |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 155 | return module_name, e |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 156 | modules[module_name] = module |
| 157 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 158 | # Look up the expected class name |
| 159 | return getattr(module, 'Entry_%s' % module_name) |
| 160 | |
| 161 | @staticmethod |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 162 | def Lookup(node_path, etype, expanded, missing_etype=False): |
| 163 | """Look up the entry class for a node. |
| 164 | |
| 165 | Args: |
| 166 | node_node (str): Path name of Node object containing information |
| 167 | about the entry to create (used for errors) |
| 168 | etype (str): Entry type to use |
| 169 | expanded (bool): Use the expanded version of etype |
| 170 | missing_etype (bool): True to default to a blob etype if the |
| 171 | requested etype is not found |
| 172 | |
| 173 | Returns: |
| 174 | The entry class object if found, else None if not found and expanded |
| 175 | is True |
| 176 | |
| 177 | Raise: |
| 178 | ValueError if expanded is False and the class is not found |
| 179 | """ |
| 180 | # Convert something like 'u-boot@0' to 'u_boot' since we are only |
| 181 | # interested in the type. |
| 182 | cls = Entry.FindEntryClass(etype, expanded) |
| 183 | if cls is None: |
| 184 | return None |
| 185 | elif isinstance(cls, tuple): |
| 186 | if missing_etype: |
| 187 | cls = Entry.FindEntryClass('blob', False) |
| 188 | if isinstance(cls, tuple): # This should not fail |
| 189 | module_name, e = cls |
| 190 | raise ValueError( |
| 191 | "Unknown entry type '%s' in node '%s' (expected etype/%s.py, error '%s'" % |
| 192 | (etype, node_path, module_name, e)) |
| 193 | return cls |
| 194 | |
| 195 | @staticmethod |
| 196 | def Create(section, node, etype=None, expanded=False, missing_etype=False): |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 197 | """Create a new entry for a node. |
| 198 | |
| 199 | Args: |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 200 | section (entry_Section): Section object containing this node |
| 201 | node (Node): Node object containing information about the entry to |
| 202 | create |
| 203 | etype (str): Entry type to use, or None to work it out (used for |
| 204 | tests) |
| 205 | expanded (bool): Use the expanded version of etype |
| 206 | missing_etype (bool): True to default to a blob etype if the |
| 207 | requested etype is not found |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 208 | |
| 209 | Returns: |
| 210 | A new Entry object of the correct type (a subclass of Entry) |
| 211 | """ |
| 212 | if not etype: |
| 213 | etype = fdt_util.GetString(node, 'type', node.name) |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 214 | obj = Entry.Lookup(node.path, etype, expanded, missing_etype) |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 215 | if obj and expanded: |
| 216 | # Check whether to use the expanded entry |
| 217 | new_etype = etype + '-expanded' |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 218 | can_expand = not fdt_util.GetBool(node, 'no-expanded') |
| 219 | if can_expand and obj.UseExpanded(node, etype, new_etype): |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 220 | etype = new_etype |
| 221 | else: |
| 222 | obj = None |
| 223 | if not obj: |
Simon Glass | b9028bc | 2021-11-23 21:09:49 -0700 | [diff] [blame] | 224 | obj = Entry.Lookup(node.path, etype, False, missing_etype) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 225 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 226 | # Call its constructor to get the object we want. |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 227 | return obj(section, etype, node) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 228 | |
| 229 | def ReadNode(self): |
| 230 | """Read entry information from the node |
| 231 | |
Simon Glass | 2c360cf | 2019-07-20 12:23:45 -0600 | [diff] [blame] | 232 | This must be called as the first thing after the Entry is created. |
| 233 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 234 | This reads all the fields we recognise from the node, ready for use. |
| 235 | """ |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 236 | if 'pos' in self._node.props: |
| 237 | self.Raise("Please use 'offset' instead of 'pos'") |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 238 | self.offset = fdt_util.GetInt(self._node, 'offset') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 239 | self.size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 240 | self.orig_offset = fdt_util.GetInt(self._node, 'orig-offset') |
| 241 | self.orig_size = fdt_util.GetInt(self._node, 'orig-size') |
| 242 | if self.GetImage().copy_to_orig: |
| 243 | self.orig_offset = self.offset |
| 244 | self.orig_size = self.size |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 245 | |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 246 | # These should not be set in input files, but are set in an FDT map, |
| 247 | # which is also read by this code. |
| 248 | self.image_pos = fdt_util.GetInt(self._node, 'image-pos') |
| 249 | self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size') |
| 250 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 251 | self.align = fdt_util.GetInt(self._node, 'align') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 252 | if tools.not_power_of_two(self.align): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 253 | raise ValueError("Node '%s': Alignment %s must be a power of two" % |
| 254 | (self._node.path, self.align)) |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 255 | if self.section and self.align is None: |
| 256 | self.align = self.section.align_default |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 257 | self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0) |
| 258 | self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0) |
| 259 | self.align_size = fdt_util.GetInt(self._node, 'align-size') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 260 | if tools.not_power_of_two(self.align_size): |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 261 | self.Raise("Alignment size %s must be a power of two" % |
| 262 | self.align_size) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 263 | self.align_end = fdt_util.GetInt(self._node, 'align-end') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 264 | self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset') |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 265 | self.expand_size = fdt_util.GetBool(self._node, 'expand-size') |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 266 | self.missing_msg = fdt_util.GetString(self._node, 'missing-msg') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 267 | |
Simon Glass | a1301a2 | 2020-10-26 17:40:06 -0600 | [diff] [blame] | 268 | # This is only supported by blobs and sections at present |
| 269 | self.compress = fdt_util.GetString(self._node, 'compress', 'none') |
| 270 | |
Simon Glass | 3732ec3 | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 271 | def GetDefaultFilename(self): |
| 272 | return None |
| 273 | |
Simon Glass | 267112e | 2019-07-20 12:23:28 -0600 | [diff] [blame] | 274 | def GetFdts(self): |
| 275 | """Get the device trees used by this entry |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 276 | |
| 277 | Returns: |
Simon Glass | 267112e | 2019-07-20 12:23:28 -0600 | [diff] [blame] | 278 | Empty dict, if this entry is not a .dtb, otherwise: |
| 279 | Dict: |
| 280 | key: Filename from this entry (without the path) |
Simon Glass | 684a4f1 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 281 | value: Tuple: |
Simon Glass | 8235dd8 | 2021-03-18 20:25:02 +1300 | [diff] [blame] | 282 | Entry object for this dtb |
Simon Glass | 684a4f1 | 2019-07-20 12:23:31 -0600 | [diff] [blame] | 283 | Filename of file containing this dtb |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 284 | """ |
Simon Glass | 267112e | 2019-07-20 12:23:28 -0600 | [diff] [blame] | 285 | return {} |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 286 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 287 | def ExpandEntries(self): |
Simon Glass | fcb2a7c | 2021-03-18 20:24:52 +1300 | [diff] [blame] | 288 | """Expand out entries which produce other entries |
| 289 | |
| 290 | Some entries generate subnodes automatically, from which sub-entries |
| 291 | are then created. This method allows those to be added to the binman |
| 292 | definition for the current image. An entry which implements this method |
| 293 | should call state.AddSubnode() to add a subnode and can add properties |
| 294 | with state.AddString(), etc. |
| 295 | |
| 296 | An example is 'files', which produces a section containing a list of |
| 297 | files. |
| 298 | """ |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 299 | pass |
| 300 | |
Simon Glass | acd6c6e | 2020-10-26 17:40:17 -0600 | [diff] [blame] | 301 | def AddMissingProperties(self, have_image_pos): |
| 302 | """Add new properties to the device tree as needed for this entry |
| 303 | |
| 304 | Args: |
| 305 | have_image_pos: True if this entry has an image position. This can |
| 306 | be False if its parent section is compressed, since compression |
| 307 | groups all entries together into a compressed block of data, |
| 308 | obscuring the start of each individual child entry |
| 309 | """ |
| 310 | for prop in ['offset', 'size']: |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 311 | if not prop in self._node.props: |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 312 | state.AddZeroProp(self._node, prop) |
Simon Glass | acd6c6e | 2020-10-26 17:40:17 -0600 | [diff] [blame] | 313 | if have_image_pos and 'image-pos' not in self._node.props: |
| 314 | state.AddZeroProp(self._node, 'image-pos') |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 315 | if self.GetImage().allow_repack: |
| 316 | if self.orig_offset is not None: |
| 317 | state.AddZeroProp(self._node, 'orig-offset', True) |
| 318 | if self.orig_size is not None: |
| 319 | state.AddZeroProp(self._node, 'orig-size', True) |
| 320 | |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 321 | if self.compress != 'none': |
| 322 | state.AddZeroProp(self._node, 'uncomp-size') |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 323 | |
| 324 | if self.update_hash: |
| 325 | err = state.CheckAddHashProp(self._node) |
| 326 | if err: |
| 327 | self.Raise(err) |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 328 | |
| 329 | def SetCalculatedProperties(self): |
| 330 | """Set the value of device-tree properties calculated by binman""" |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 331 | state.SetInt(self._node, 'offset', self.offset) |
| 332 | state.SetInt(self._node, 'size', self.size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 333 | base = self.section.GetRootSkipAtStart() if self.section else 0 |
Simon Glass | acd6c6e | 2020-10-26 17:40:17 -0600 | [diff] [blame] | 334 | if self.image_pos is not None: |
Simon Glass | eb943b1 | 2020-11-02 12:55:44 -0700 | [diff] [blame] | 335 | state.SetInt(self._node, 'image-pos', self.image_pos - base) |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 336 | if self.GetImage().allow_repack: |
| 337 | if self.orig_offset is not None: |
| 338 | state.SetInt(self._node, 'orig-offset', self.orig_offset, True) |
| 339 | if self.orig_size is not None: |
| 340 | state.SetInt(self._node, 'orig-size', self.orig_size, True) |
Simon Glass | aa2fcf9 | 2019-07-08 14:25:30 -0600 | [diff] [blame] | 341 | if self.uncomp_size is not None: |
| 342 | state.SetInt(self._node, 'uncomp-size', self.uncomp_size) |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 343 | |
| 344 | if self.update_hash: |
| 345 | state.CheckSetHashValue(self._node, self.GetData) |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 346 | |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 347 | def ProcessFdt(self, fdt): |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 348 | """Allow entries to adjust the device tree |
| 349 | |
| 350 | Some entries need to adjust the device tree for their purposes. This |
| 351 | may involve adding or deleting properties. |
| 352 | |
| 353 | Returns: |
| 354 | True if processing is complete |
| 355 | False if processing could not be completed due to a dependency. |
| 356 | This will cause the entry to be retried after others have been |
| 357 | called |
| 358 | """ |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 359 | return True |
| 360 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 361 | def SetPrefix(self, prefix): |
| 362 | """Set the name prefix for a node |
| 363 | |
| 364 | Args: |
| 365 | prefix: Prefix to set, or '' to not use a prefix |
| 366 | """ |
| 367 | if prefix: |
| 368 | self.name = prefix + self.name |
| 369 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 370 | def SetContents(self, data): |
| 371 | """Set the contents of an entry |
| 372 | |
| 373 | This sets both the data and content_size properties |
| 374 | |
| 375 | Args: |
Simon Glass | d17dfea | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 376 | data: Data to set to the contents (bytes) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 377 | """ |
| 378 | self.data = data |
| 379 | self.contents_size = len(self.data) |
| 380 | |
| 381 | def ProcessContentsUpdate(self, data): |
Simon Glass | d17dfea | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 382 | """Update the contents of an entry, after the size is fixed |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 383 | |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 384 | This checks that the new data is the same size as the old. If the size |
| 385 | has changed, this triggers a re-run of the packing algorithm. |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 386 | |
| 387 | Args: |
Simon Glass | d17dfea | 2019-07-08 14:25:33 -0600 | [diff] [blame] | 388 | data: Data to set to the contents (bytes) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 389 | |
| 390 | Raises: |
| 391 | ValueError if the new data size is not the same as the old |
| 392 | """ |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 393 | size_ok = True |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 394 | new_size = len(data) |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 395 | if state.AllowEntryExpansion() and new_size > self.contents_size: |
| 396 | # self.data will indicate the new size needed |
| 397 | size_ok = False |
| 398 | elif state.AllowEntryContraction() and new_size < self.contents_size: |
| 399 | size_ok = False |
| 400 | |
| 401 | # If not allowed to change, try to deal with it or give up |
| 402 | if size_ok: |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 403 | if new_size > self.contents_size: |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 404 | self.Raise('Cannot update entry size from %d to %d' % |
| 405 | (self.contents_size, new_size)) |
| 406 | |
| 407 | # Don't let the data shrink. Pad it if necessary |
| 408 | if size_ok and new_size < self.contents_size: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 409 | data += tools.get_bytes(0, self.contents_size - new_size) |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 410 | |
| 411 | if not size_ok: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 412 | tout.debug("Entry '%s' size change from %s to %s" % ( |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 413 | self._node.path, to_hex(self.contents_size), |
| 414 | to_hex(new_size))) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 415 | self.SetContents(data) |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 416 | return size_ok |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 417 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 418 | def ObtainContents(self): |
| 419 | """Figure out the contents of an entry. |
| 420 | |
| 421 | Returns: |
| 422 | True if the contents were found, False if another call is needed |
| 423 | after the other entries are processed. |
| 424 | """ |
| 425 | # No contents by default: subclasses can implement this |
| 426 | return True |
| 427 | |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 428 | def ResetForPack(self): |
| 429 | """Reset offset/size fields so that packing can be done again""" |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 430 | self.Detail('ResetForPack: offset %s->%s, size %s->%s' % |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 431 | (to_hex(self.offset), to_hex(self.orig_offset), |
| 432 | to_hex(self.size), to_hex(self.orig_size))) |
Simon Glass | 1fdb487 | 2019-10-31 07:43:02 -0600 | [diff] [blame] | 433 | self.pre_reset_size = self.size |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 434 | self.offset = self.orig_offset |
| 435 | self.size = self.orig_size |
| 436 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 437 | def Pack(self, offset): |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 438 | """Figure out how to pack the entry into the section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 439 | |
| 440 | Most of the time the entries are not fully specified. There may be |
| 441 | an alignment but no size. In that case we take the size from the |
| 442 | contents of the entry. |
| 443 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 444 | 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] | 445 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 446 | Once this function is complete, both the offset and size of the |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 447 | entry will be know. |
| 448 | |
| 449 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 450 | Current section offset pointer |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 451 | |
| 452 | Returns: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 453 | New section offset pointer (after this entry) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 454 | """ |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 455 | self.Detail('Packing: offset=%s, size=%s, content_size=%x' % |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 456 | (to_hex(self.offset), to_hex(self.size), |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 457 | self.contents_size)) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 458 | if self.offset is None: |
| 459 | if self.offset_unset: |
| 460 | self.Raise('No offset set with offset-unset: should another ' |
| 461 | 'entry provide this correct offset?') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 462 | self.offset = tools.align(offset, self.align) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 463 | needed = self.pad_before + self.contents_size + self.pad_after |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 464 | needed = tools.align(needed, self.align_size) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 465 | size = self.size |
| 466 | if not size: |
| 467 | size = needed |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 468 | new_offset = self.offset + size |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 469 | aligned_offset = tools.align(new_offset, self.align_end) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 470 | if aligned_offset != new_offset: |
| 471 | size = aligned_offset - self.offset |
| 472 | new_offset = aligned_offset |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 473 | |
| 474 | if not self.size: |
| 475 | self.size = size |
| 476 | |
| 477 | if self.size < needed: |
| 478 | self.Raise("Entry contents size is %#x (%d) but entry size is " |
| 479 | "%#x (%d)" % (needed, needed, self.size, self.size)) |
| 480 | # Check that the alignment is correct. It could be wrong if the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 481 | # and offset or size values were provided (i.e. not calculated), but |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 482 | # conflict with the provided alignment values |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 483 | if self.size != tools.align(self.size, self.align_size): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 484 | self.Raise("Size %#x (%d) does not match align-size %#x (%d)" % |
| 485 | (self.size, self.size, self.align_size, self.align_size)) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 486 | if self.offset != tools.align(self.offset, self.align): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 487 | self.Raise("Offset %#x (%d) does not match align %#x (%d)" % |
| 488 | (self.offset, self.offset, self.align, self.align)) |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 489 | self.Detail(' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' % |
| 490 | (self.offset, self.size, self.contents_size, new_offset)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 491 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 492 | return new_offset |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 493 | |
| 494 | def Raise(self, msg): |
| 495 | """Convenience function to raise an error referencing a node""" |
| 496 | raise ValueError("Node '%s': %s" % (self._node.path, msg)) |
| 497 | |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 498 | def Info(self, msg): |
| 499 | """Convenience function to log info referencing a node""" |
| 500 | tag = "Info '%s'" % self._node.path |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 501 | tout.detail('%30s: %s' % (tag, msg)) |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 502 | |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 503 | def Detail(self, msg): |
| 504 | """Convenience function to log detail referencing a node""" |
| 505 | tag = "Node '%s'" % self._node.path |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 506 | tout.detail('%30s: %s' % (tag, msg)) |
Simon Glass | b6dff4c | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 507 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 508 | def GetEntryArgsOrProps(self, props, required=False): |
| 509 | """Return the values of a set of properties |
| 510 | |
| 511 | Args: |
| 512 | props: List of EntryArg objects |
| 513 | |
| 514 | Raises: |
| 515 | ValueError if a property is not found |
| 516 | """ |
| 517 | values = [] |
| 518 | missing = [] |
| 519 | for prop in props: |
| 520 | python_prop = prop.name.replace('-', '_') |
| 521 | if hasattr(self, python_prop): |
| 522 | value = getattr(self, python_prop) |
| 523 | else: |
| 524 | value = None |
| 525 | if value is None: |
| 526 | value = self.GetArg(prop.name, prop.datatype) |
| 527 | if value is None and required: |
| 528 | missing.append(prop.name) |
| 529 | values.append(value) |
| 530 | if missing: |
Simon Glass | 3fb2540 | 2021-01-06 21:35:16 -0700 | [diff] [blame] | 531 | self.GetImage().MissingArgs(self, missing) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 532 | return values |
| 533 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 534 | def GetPath(self): |
| 535 | """Get the path of a node |
| 536 | |
| 537 | Returns: |
| 538 | Full path of the node for this entry |
| 539 | """ |
| 540 | return self._node.path |
| 541 | |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 542 | def GetData(self, required=True): |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 543 | """Get the contents of an entry |
| 544 | |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 545 | Args: |
| 546 | required: True if the data must be present, False if it is OK to |
| 547 | return None |
| 548 | |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 549 | Returns: |
| 550 | bytes content of the entry, excluding any padding. If the entry is |
| 551 | compressed, the compressed data is returned |
| 552 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 553 | self.Detail('GetData: size %s' % to_hex_size(self.data)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 554 | return self.data |
| 555 | |
Simon Glass | e17220f | 2020-11-02 12:55:43 -0700 | [diff] [blame] | 556 | def GetPaddedData(self, data=None): |
| 557 | """Get the data for an entry including any padding |
| 558 | |
| 559 | Gets the entry data and uses its section's pad-byte value to add padding |
| 560 | before and after as defined by the pad-before and pad-after properties. |
| 561 | |
| 562 | This does not consider alignment. |
| 563 | |
| 564 | Returns: |
| 565 | Contents of the entry along with any pad bytes before and |
| 566 | after it (bytes) |
| 567 | """ |
| 568 | if data is None: |
| 569 | data = self.GetData() |
| 570 | return self.section.GetPaddedDataForEntry(self, data) |
| 571 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 572 | def GetOffsets(self): |
Simon Glass | 224bc66 | 2019-07-08 13:18:30 -0600 | [diff] [blame] | 573 | """Get the offsets for siblings |
| 574 | |
| 575 | Some entry types can contain information about the position or size of |
| 576 | other entries. An example of this is the Intel Flash Descriptor, which |
| 577 | knows where the Intel Management Engine section should go. |
| 578 | |
| 579 | If this entry knows about the position of other entries, it can specify |
| 580 | this by returning values here |
| 581 | |
| 582 | Returns: |
| 583 | Dict: |
| 584 | key: Entry type |
| 585 | value: List containing position and size of the given entry |
Simon Glass | ed365eb | 2019-07-08 13:18:39 -0600 | [diff] [blame] | 586 | type. Either can be None if not known |
Simon Glass | 224bc66 | 2019-07-08 13:18:30 -0600 | [diff] [blame] | 587 | """ |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 588 | return {} |
| 589 | |
Simon Glass | ed365eb | 2019-07-08 13:18:39 -0600 | [diff] [blame] | 590 | def SetOffsetSize(self, offset, size): |
| 591 | """Set the offset and/or size of an entry |
| 592 | |
| 593 | Args: |
| 594 | offset: New offset, or None to leave alone |
| 595 | size: New size, or None to leave alone |
| 596 | """ |
| 597 | if offset is not None: |
| 598 | self.offset = offset |
| 599 | if size is not None: |
| 600 | self.size = size |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 601 | |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 602 | def SetImagePos(self, image_pos): |
| 603 | """Set the position in the image |
| 604 | |
| 605 | Args: |
| 606 | image_pos: Position of this entry in the image |
| 607 | """ |
| 608 | self.image_pos = image_pos + self.offset |
| 609 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 610 | def ProcessContents(self): |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 611 | """Do any post-packing updates of entry contents |
| 612 | |
| 613 | This function should call ProcessContentsUpdate() to update the entry |
| 614 | contents, if necessary, returning its return value here. |
| 615 | |
| 616 | Args: |
| 617 | data: Data to set to the contents (bytes) |
| 618 | |
| 619 | Returns: |
| 620 | True if the new data size is OK, False if expansion is needed |
| 621 | |
| 622 | Raises: |
| 623 | ValueError if the new data size is not the same as the old and |
| 624 | state.AllowEntryExpansion() is False |
| 625 | """ |
| 626 | return True |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 627 | |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 628 | def WriteSymbols(self, section): |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 629 | """Write symbol values into binary files for access at run time |
| 630 | |
| 631 | Args: |
Simon Glass | 8a6f56e | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 632 | section: Section containing the entry |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 633 | """ |
| 634 | pass |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 635 | |
Simon Glass | 55f6807 | 2020-10-26 17:40:18 -0600 | [diff] [blame] | 636 | def CheckEntries(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 637 | """Check that the entry offsets are correct |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 638 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 639 | This is used for entries which have extra offset requirements (other |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 640 | than having to be fully inside their section). Sub-classes can implement |
| 641 | this function and raise if there is a problem. |
| 642 | """ |
| 643 | pass |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 644 | |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 645 | @staticmethod |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 646 | def GetStr(value): |
| 647 | if value is None: |
| 648 | return '<none> ' |
| 649 | return '%08x' % value |
| 650 | |
| 651 | @staticmethod |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 652 | def WriteMapLine(fd, indent, name, offset, size, image_pos): |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 653 | print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent, |
| 654 | Entry.GetStr(offset), Entry.GetStr(size), |
| 655 | name), file=fd) |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 656 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 657 | def WriteMap(self, fd, indent): |
| 658 | """Write a map of the entry to a .map file |
| 659 | |
| 660 | Args: |
| 661 | fd: File to write the map to |
| 662 | indent: Curent indent level of map (0=none, 1=one level, etc.) |
| 663 | """ |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 664 | self.WriteMapLine(fd, indent, self.name, self.offset, self.size, |
| 665 | self.image_pos) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 666 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 667 | def GetEntries(self): |
| 668 | """Return a list of entries contained by this entry |
| 669 | |
| 670 | Returns: |
| 671 | List of entries, or None if none. A normal entry has no entries |
| 672 | within it so will return None |
| 673 | """ |
| 674 | return None |
| 675 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 676 | def GetArg(self, name, datatype=str): |
| 677 | """Get the value of an entry argument or device-tree-node property |
| 678 | |
| 679 | Some node properties can be provided as arguments to binman. First check |
| 680 | the entry arguments, and fall back to the device tree if not found |
| 681 | |
| 682 | Args: |
| 683 | name: Argument name |
| 684 | datatype: Data type (str or int) |
| 685 | |
| 686 | Returns: |
| 687 | Value of argument as a string or int, or None if no value |
| 688 | |
| 689 | Raises: |
| 690 | ValueError if the argument cannot be converted to in |
| 691 | """ |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 692 | value = state.GetEntryArg(name) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 693 | if value is not None: |
| 694 | if datatype == int: |
| 695 | try: |
| 696 | value = int(value) |
| 697 | except ValueError: |
| 698 | self.Raise("Cannot convert entry arg '%s' (value '%s') to integer" % |
| 699 | (name, value)) |
| 700 | elif datatype == str: |
| 701 | pass |
| 702 | else: |
| 703 | raise ValueError("GetArg() internal error: Unknown data type '%s'" % |
| 704 | datatype) |
| 705 | else: |
| 706 | value = fdt_util.GetDatatype(self._node, name, datatype) |
| 707 | return value |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 708 | |
| 709 | @staticmethod |
| 710 | def WriteDocs(modules, test_missing=None): |
| 711 | """Write out documentation about the various entry types to stdout |
| 712 | |
| 713 | Args: |
| 714 | modules: List of modules to include |
| 715 | test_missing: Used for testing. This is a module to report |
| 716 | as missing |
| 717 | """ |
| 718 | print('''Binman Entry Documentation |
| 719 | =========================== |
| 720 | |
| 721 | This file describes the entry types supported by binman. These entry types can |
| 722 | be placed in an image one by one to build up a final firmware image. It is |
| 723 | fairly easy to create new entry types. Just add a new file to the 'etype' |
| 724 | directory. You can use the existing entries as examples. |
| 725 | |
| 726 | Note that some entries are subclasses of others, using and extending their |
| 727 | features to produce new behaviours. |
| 728 | |
| 729 | |
| 730 | ''') |
| 731 | modules = sorted(modules) |
| 732 | |
| 733 | # Don't show the test entry |
| 734 | if '_testing' in modules: |
| 735 | modules.remove('_testing') |
| 736 | missing = [] |
| 737 | for name in modules: |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 738 | module = Entry.Lookup('WriteDocs', name, False) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 739 | docs = getattr(module, '__doc__') |
| 740 | if test_missing == name: |
| 741 | docs = None |
| 742 | if docs: |
| 743 | lines = docs.splitlines() |
| 744 | first_line = lines[0] |
| 745 | rest = [line[4:] for line in lines[1:]] |
| 746 | hdr = 'Entry: %s: %s' % (name.replace('_', '-'), first_line) |
| 747 | print(hdr) |
| 748 | print('-' * len(hdr)) |
| 749 | print('\n'.join(rest)) |
| 750 | print() |
| 751 | print() |
| 752 | else: |
| 753 | missing.append(name) |
| 754 | |
| 755 | if missing: |
| 756 | raise ValueError('Documentation is missing for modules: %s' % |
| 757 | ', '.join(missing)) |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 758 | |
| 759 | def GetUniqueName(self): |
| 760 | """Get a unique name for a node |
| 761 | |
| 762 | Returns: |
| 763 | String containing a unique name for a node, consisting of the name |
| 764 | of all ancestors (starting from within the 'binman' node) separated |
| 765 | by a dot ('.'). This can be useful for generating unique filesnames |
| 766 | in the output directory. |
| 767 | """ |
| 768 | name = self.name |
| 769 | node = self._node |
| 770 | while node.parent: |
| 771 | node = node.parent |
| 772 | if node.name == 'binman': |
| 773 | break |
| 774 | name = '%s.%s' % (node.name, name) |
| 775 | return name |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 776 | |
| 777 | def ExpandToLimit(self, limit): |
| 778 | """Expand an entry so that it ends at the given offset limit""" |
| 779 | if self.offset + self.size < limit: |
| 780 | self.size = limit - self.offset |
| 781 | # Request the contents again, since changing the size requires that |
| 782 | # the data grows. This should not fail, but check it to be sure. |
| 783 | if not self.ObtainContents(): |
| 784 | self.Raise('Cannot obtain contents when expanding entry') |
Simon Glass | c4056b8 | 2019-07-08 13:18:38 -0600 | [diff] [blame] | 785 | |
| 786 | def HasSibling(self, name): |
| 787 | """Check if there is a sibling of a given name |
| 788 | |
| 789 | Returns: |
| 790 | True if there is an entry with this name in the the same section, |
| 791 | else False |
| 792 | """ |
| 793 | return name in self.section.GetEntries() |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 794 | |
| 795 | def GetSiblingImagePos(self, name): |
| 796 | """Return the image position of the given sibling |
| 797 | |
| 798 | Returns: |
| 799 | Image position of sibling, or None if the sibling has no position, |
| 800 | or False if there is no such sibling |
| 801 | """ |
| 802 | if not self.HasSibling(name): |
| 803 | return False |
| 804 | return self.section.GetEntries()[name].image_pos |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 805 | |
| 806 | @staticmethod |
| 807 | def AddEntryInfo(entries, indent, name, etype, size, image_pos, |
| 808 | uncomp_size, offset, entry): |
| 809 | """Add a new entry to the entries list |
| 810 | |
| 811 | Args: |
| 812 | entries: List (of EntryInfo objects) to add to |
| 813 | indent: Current indent level to add to list |
| 814 | name: Entry name (string) |
| 815 | etype: Entry type (string) |
| 816 | size: Entry size in bytes (int) |
| 817 | image_pos: Position within image in bytes (int) |
| 818 | uncomp_size: Uncompressed size if the entry uses compression, else |
| 819 | None |
| 820 | offset: Entry offset within parent in bytes (int) |
| 821 | entry: Entry object |
| 822 | """ |
| 823 | entries.append(EntryInfo(indent, name, etype, size, image_pos, |
| 824 | uncomp_size, offset, entry)) |
| 825 | |
| 826 | def ListEntries(self, entries, indent): |
| 827 | """Add files in this entry to the list of entries |
| 828 | |
| 829 | This can be overridden by subclasses which need different behaviour. |
| 830 | |
| 831 | Args: |
| 832 | entries: List (of EntryInfo objects) to add to |
| 833 | indent: Current indent level to add to list |
| 834 | """ |
| 835 | self.AddEntryInfo(entries, indent, self.name, self.etype, self.size, |
| 836 | self.image_pos, self.uncomp_size, self.offset, self) |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 837 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 838 | def ReadData(self, decomp=True, alt_format=None): |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 839 | """Read the data for an entry from the image |
| 840 | |
| 841 | This is used when the image has been read in and we want to extract the |
| 842 | data for a particular entry from that image. |
| 843 | |
| 844 | Args: |
| 845 | decomp: True to decompress any compressed data before returning it; |
| 846 | False to return the raw, uncompressed data |
| 847 | |
| 848 | Returns: |
| 849 | Entry data (bytes) |
| 850 | """ |
| 851 | # Use True here so that we get an uncompressed section to work from, |
| 852 | # although compressed sections are currently not supported |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 853 | tout.debug("ReadChildData section '%s', entry '%s'" % |
Simon Glass | 4d8151f | 2019-09-25 08:56:21 -0600 | [diff] [blame] | 854 | (self.section.GetPath(), self.GetPath())) |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 855 | data = self.section.ReadChildData(self, decomp, alt_format) |
Simon Glass | 0cd8ace | 2019-07-20 12:24:04 -0600 | [diff] [blame] | 856 | return data |
Simon Glass | af8c45c | 2019-07-20 12:23:41 -0600 | [diff] [blame] | 857 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 858 | def ReadChildData(self, child, decomp=True, alt_format=None): |
Simon Glass | 4d8151f | 2019-09-25 08:56:21 -0600 | [diff] [blame] | 859 | """Read the data for a particular child entry |
Simon Glass | 23f0047 | 2019-09-25 08:56:20 -0600 | [diff] [blame] | 860 | |
| 861 | This reads data from the parent and extracts the piece that relates to |
| 862 | the given child. |
| 863 | |
| 864 | Args: |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 865 | child (Entry): Child entry to read data for (must be valid) |
| 866 | decomp (bool): True to decompress any compressed data before |
| 867 | returning it; False to return the raw, uncompressed data |
| 868 | alt_format (str): Alternative format to read in, or None |
Simon Glass | 23f0047 | 2019-09-25 08:56:20 -0600 | [diff] [blame] | 869 | |
| 870 | Returns: |
| 871 | Data for the child (bytes) |
| 872 | """ |
| 873 | pass |
| 874 | |
Simon Glass | af8c45c | 2019-07-20 12:23:41 -0600 | [diff] [blame] | 875 | def LoadData(self, decomp=True): |
| 876 | data = self.ReadData(decomp) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 877 | self.contents_size = len(data) |
Simon Glass | af8c45c | 2019-07-20 12:23:41 -0600 | [diff] [blame] | 878 | self.ProcessContentsUpdate(data) |
| 879 | self.Detail('Loaded data size %x' % len(data)) |
Simon Glass | 990b174 | 2019-07-20 12:23:46 -0600 | [diff] [blame] | 880 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 881 | def GetAltFormat(self, data, alt_format): |
| 882 | """Read the data for an extry in an alternative format |
| 883 | |
| 884 | Supported formats are list in the documentation for each entry. An |
| 885 | example is fdtmap which provides . |
| 886 | |
| 887 | Args: |
| 888 | data (bytes): Data to convert (this should have been produced by the |
| 889 | entry) |
| 890 | alt_format (str): Format to use |
| 891 | |
| 892 | """ |
| 893 | pass |
| 894 | |
Simon Glass | 990b174 | 2019-07-20 12:23:46 -0600 | [diff] [blame] | 895 | def GetImage(self): |
| 896 | """Get the image containing this entry |
| 897 | |
| 898 | Returns: |
| 899 | Image object containing this entry |
| 900 | """ |
| 901 | return self.section.GetImage() |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 902 | |
| 903 | def WriteData(self, data, decomp=True): |
| 904 | """Write the data to an entry in the image |
| 905 | |
| 906 | This is used when the image has been read in and we want to replace the |
| 907 | data for a particular entry in that image. |
| 908 | |
| 909 | The image must be re-packed and written out afterwards. |
| 910 | |
| 911 | Args: |
| 912 | data: Data to replace it with |
| 913 | decomp: True to compress the data if needed, False if data is |
| 914 | already compressed so should be used as is |
| 915 | |
| 916 | Returns: |
| 917 | True if the data did not result in a resize of this entry, False if |
| 918 | the entry must be resized |
| 919 | """ |
Simon Glass | 1fdb487 | 2019-10-31 07:43:02 -0600 | [diff] [blame] | 920 | if self.size is not None: |
| 921 | self.contents_size = self.size |
| 922 | else: |
| 923 | self.contents_size = self.pre_reset_size |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 924 | ok = self.ProcessContentsUpdate(data) |
| 925 | self.Detail('WriteData: size=%x, ok=%s' % (len(data), ok)) |
Simon Glass | d34af7a | 2019-07-20 12:24:05 -0600 | [diff] [blame] | 926 | section_ok = self.section.WriteChildData(self) |
| 927 | return ok and section_ok |
| 928 | |
| 929 | def WriteChildData(self, child): |
| 930 | """Handle writing the data in a child entry |
| 931 | |
| 932 | This should be called on the child's parent section after the child's |
Simon Glass | e796f24 | 2021-11-23 11:03:44 -0700 | [diff] [blame] | 933 | data has been updated. It should update any data structures needed to |
| 934 | validate that the update is successful. |
Simon Glass | d34af7a | 2019-07-20 12:24:05 -0600 | [diff] [blame] | 935 | |
| 936 | This base-class implementation does nothing, since the base Entry object |
| 937 | does not have any children. |
| 938 | |
| 939 | Args: |
| 940 | child: Child Entry that was written |
| 941 | |
| 942 | Returns: |
| 943 | True if the section could be updated successfully, False if the |
Simon Glass | e796f24 | 2021-11-23 11:03:44 -0700 | [diff] [blame] | 944 | data is such that the section could not update |
Simon Glass | d34af7a | 2019-07-20 12:24:05 -0600 | [diff] [blame] | 945 | """ |
| 946 | return True |
Simon Glass | 1145376 | 2019-07-20 12:23:55 -0600 | [diff] [blame] | 947 | |
| 948 | def GetSiblingOrder(self): |
| 949 | """Get the relative order of an entry amoung its siblings |
| 950 | |
| 951 | Returns: |
| 952 | 'start' if this entry is first among siblings, 'end' if last, |
| 953 | otherwise None |
| 954 | """ |
| 955 | entries = list(self.section.GetEntries().values()) |
| 956 | if entries: |
| 957 | if self == entries[0]: |
| 958 | return 'start' |
| 959 | elif self == entries[-1]: |
| 960 | return 'end' |
| 961 | return 'middle' |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 962 | |
| 963 | def SetAllowMissing(self, allow_missing): |
| 964 | """Set whether a section allows missing external blobs |
| 965 | |
| 966 | Args: |
| 967 | allow_missing: True if allowed, False if not allowed |
| 968 | """ |
| 969 | # This is meaningless for anything other than sections |
| 970 | pass |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 971 | |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 972 | def SetAllowFakeBlob(self, allow_fake): |
| 973 | """Set whether a section allows to create a fake blob |
| 974 | |
| 975 | Args: |
| 976 | allow_fake: True if allowed, False if not allowed |
| 977 | """ |
Simon Glass | ceb5f91 | 2022-01-09 20:13:46 -0700 | [diff] [blame] | 978 | self.allow_fake = allow_fake |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 979 | |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 980 | def CheckMissing(self, missing_list): |
| 981 | """Check if any entries in this section have missing external blobs |
| 982 | |
| 983 | If there are missing blobs, the entries are added to the list |
| 984 | |
| 985 | Args: |
| 986 | missing_list: List of Entry objects to be added to |
| 987 | """ |
| 988 | if self.missing: |
| 989 | missing_list.append(self) |
Simon Glass | b8f9037 | 2020-09-01 05:13:57 -0600 | [diff] [blame] | 990 | |
Simon Glass | 7a602fd | 2022-01-12 13:10:36 -0700 | [diff] [blame] | 991 | def check_fake_fname(self, fname): |
| 992 | """If the file is missing and the entry allows fake blobs, fake it |
| 993 | |
| 994 | Sets self.faked to True if faked |
| 995 | |
| 996 | Args: |
| 997 | fname (str): Filename to check |
| 998 | |
| 999 | Returns: |
| 1000 | fname (str): Filename of faked file |
| 1001 | """ |
| 1002 | if self.allow_fake and not pathlib.Path(fname).is_file(): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1003 | outfname = tools.get_output_filename(os.path.basename(fname)) |
Simon Glass | 7a602fd | 2022-01-12 13:10:36 -0700 | [diff] [blame] | 1004 | with open(outfname, "wb") as out: |
| 1005 | out.truncate(1024) |
| 1006 | self.faked = True |
| 1007 | return outfname |
| 1008 | return fname |
| 1009 | |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 1010 | def CheckFakedBlobs(self, faked_blobs_list): |
| 1011 | """Check if any entries in this section have faked external blobs |
| 1012 | |
| 1013 | If there are faked blobs, the entries are added to the list |
| 1014 | |
| 1015 | Args: |
| 1016 | fake_blobs_list: List of Entry objects to be added to |
| 1017 | """ |
| 1018 | # This is meaningless for anything other than blobs |
| 1019 | pass |
| 1020 | |
Simon Glass | b8f9037 | 2020-09-01 05:13:57 -0600 | [diff] [blame] | 1021 | def GetAllowMissing(self): |
| 1022 | """Get whether a section allows missing external blobs |
| 1023 | |
| 1024 | Returns: |
| 1025 | True if allowed, False if not allowed |
| 1026 | """ |
| 1027 | return self.allow_missing |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 1028 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 1029 | def record_missing_bintool(self, bintool): |
| 1030 | """Record a missing bintool that was needed to produce this entry |
| 1031 | |
| 1032 | Args: |
| 1033 | bintool (Bintool): Bintool that was missing |
| 1034 | """ |
| 1035 | self.missing_bintools.append(bintool) |
| 1036 | |
| 1037 | def check_missing_bintools(self, missing_list): |
| 1038 | """Check if any entries in this section have missing bintools |
| 1039 | |
| 1040 | If there are missing bintools, these are added to the list |
| 1041 | |
| 1042 | Args: |
| 1043 | missing_list: List of Bintool objects to be added to |
| 1044 | """ |
| 1045 | missing_list += self.missing_bintools |
| 1046 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 1047 | def GetHelpTags(self): |
| 1048 | """Get the tags use for missing-blob help |
| 1049 | |
| 1050 | Returns: |
| 1051 | list of possible tags, most desirable first |
| 1052 | """ |
| 1053 | return list(filter(None, [self.missing_msg, self.name, self.etype])) |
Simon Glass | a1301a2 | 2020-10-26 17:40:06 -0600 | [diff] [blame] | 1054 | |
| 1055 | def CompressData(self, indata): |
| 1056 | """Compress data according to the entry's compression method |
| 1057 | |
| 1058 | Args: |
| 1059 | indata: Data to compress |
| 1060 | |
| 1061 | Returns: |
| 1062 | Compressed data (first word is the compressed size) |
| 1063 | """ |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 1064 | self.uncomp_data = indata |
Simon Glass | a1301a2 | 2020-10-26 17:40:06 -0600 | [diff] [blame] | 1065 | if self.compress != 'none': |
| 1066 | self.uncomp_size = len(indata) |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 1067 | data = comp_util.compress(indata, self.compress) |
Simon Glass | a1301a2 | 2020-10-26 17:40:06 -0600 | [diff] [blame] | 1068 | return data |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 1069 | |
| 1070 | @classmethod |
| 1071 | def UseExpanded(cls, node, etype, new_etype): |
| 1072 | """Check whether to use an expanded entry type |
| 1073 | |
| 1074 | This is called by Entry.Create() when it finds an expanded version of |
| 1075 | an entry type (e.g. 'u-boot-expanded'). If this method returns True then |
| 1076 | it will be used (e.g. in place of 'u-boot'). If it returns False, it is |
| 1077 | ignored. |
| 1078 | |
| 1079 | Args: |
| 1080 | node: Node object containing information about the entry to |
| 1081 | create |
| 1082 | etype: Original entry type being used |
| 1083 | new_etype: New entry type proposed |
| 1084 | |
| 1085 | Returns: |
| 1086 | True to use this entry type, False to use the original one |
| 1087 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 1088 | tout.info("Node '%s': etype '%s': %s selected" % |
Simon Glass | 2f85941 | 2021-03-18 20:25:04 +1300 | [diff] [blame] | 1089 | (node.path, etype, new_etype)) |
| 1090 | return True |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 1091 | |
| 1092 | def CheckAltFormats(self, alt_formats): |
| 1093 | """Add any alternative formats supported by this entry type |
| 1094 | |
| 1095 | Args: |
| 1096 | alt_formats (dict): Dict to add alt_formats to: |
| 1097 | key: Name of alt format |
| 1098 | value: Help text |
| 1099 | """ |
| 1100 | pass |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 1101 | |
| 1102 | def AddBintools(self, tools): |
| 1103 | """Add the bintools used by this entry type |
| 1104 | |
| 1105 | Args: |
| 1106 | tools (dict of Bintool): |
| 1107 | """ |
| 1108 | pass |
| 1109 | |
| 1110 | @classmethod |
| 1111 | def AddBintool(self, tools, name): |
| 1112 | """Add a new bintool to the tools used by this etype |
| 1113 | |
| 1114 | Args: |
| 1115 | name: Name of the tool |
| 1116 | """ |
| 1117 | btool = bintool.Bintool.create(name) |
| 1118 | tools[name] = btool |
| 1119 | return btool |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 1120 | |
| 1121 | def SetUpdateHash(self, update_hash): |
| 1122 | """Set whether this entry's "hash" subnode should be updated |
| 1123 | |
| 1124 | Args: |
| 1125 | update_hash: True if hash should be updated, False if not |
| 1126 | """ |
| 1127 | self.update_hash = update_hash |
Simon Glass | 6fba35c | 2022-02-08 11:50:00 -0700 | [diff] [blame] | 1128 | |
| 1129 | def collect_contents_to_file(self, entries, prefix): |
| 1130 | """Put the contents of a list of entries into a file |
| 1131 | |
| 1132 | Args: |
| 1133 | entries (list of Entry): Entries to collect |
| 1134 | prefix (str): Filename prefix of file to write to |
| 1135 | |
| 1136 | If any entry does not have contents yet, this function returns False |
| 1137 | for the data. |
| 1138 | |
| 1139 | Returns: |
| 1140 | Tuple: |
Simon Glass | 43a98cc | 2022-03-05 20:18:58 -0700 | [diff] [blame^] | 1141 | bytes: Concatenated data from all the entries (or None) |
| 1142 | str: Filename of file written (or None if no data) |
| 1143 | str: Unique portion of filename (or None if no data) |
Simon Glass | 6fba35c | 2022-02-08 11:50:00 -0700 | [diff] [blame] | 1144 | """ |
| 1145 | data = b'' |
| 1146 | for entry in entries: |
| 1147 | # First get the input data and put it in a file. If not available, |
| 1148 | # try later. |
| 1149 | if not entry.ObtainContents(): |
Simon Glass | 43a98cc | 2022-03-05 20:18:58 -0700 | [diff] [blame^] | 1150 | return None, None, None |
Simon Glass | 6fba35c | 2022-02-08 11:50:00 -0700 | [diff] [blame] | 1151 | data += entry.GetData() |
| 1152 | uniq = self.GetUniqueName() |
| 1153 | fname = tools.get_output_filename(f'{prefix}.{uniq}') |
| 1154 | tools.write_file(fname, data) |
| 1155 | return data, fname, uniq |