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