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