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