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