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 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 5 | # Creates binary images from input files controlled by a description |
| 6 | # |
| 7 | |
| 8 | from collections import OrderedDict |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 9 | import glob |
Jan Kiszka | 6d7c40c | 2023-04-22 16:42:48 +0200 | [diff] [blame] | 10 | try: |
| 11 | import importlib.resources |
Simon Glass | 245c52c | 2023-06-01 10:22:25 -0600 | [diff] [blame] | 12 | except ImportError: # pragma: no cover |
Jan Kiszka | 6d7c40c | 2023-04-22 16:42:48 +0200 | [diff] [blame] | 13 | # for Python 3.6 |
| 14 | import importlib_resources |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 15 | import os |
Simon Glass | c1dc2f8 | 2020-08-29 11:36:14 -0600 | [diff] [blame] | 16 | import pkg_resources |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 17 | import re |
Simon Glass | c1dc2f8 | 2020-08-29 11:36:14 -0600 | [diff] [blame] | 18 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 19 | import sys |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 20 | |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 21 | from binman import bintool |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 22 | from binman import cbfs_util |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 23 | from binman import elf |
| 24 | from binman import entry |
Simon Glass | fc79284 | 2023-07-18 07:24:04 -0600 | [diff] [blame] | 25 | from dtoc import fdt_util |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 26 | from u_boot_pylib import command |
| 27 | from u_boot_pylib import tools |
| 28 | from u_boot_pylib import tout |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 29 | |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 30 | # These are imported if needed since they import libfdt |
| 31 | state = None |
| 32 | Image = None |
| 33 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 34 | # List of images we plan to create |
| 35 | # Make this global so that it can be referenced from tests |
| 36 | images = OrderedDict() |
| 37 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 38 | # Help text for each type of missing blob, dict: |
| 39 | # key: Value of the entry's 'missing-msg' or entry name |
| 40 | # value: Text for the help |
| 41 | missing_blob_help = {} |
| 42 | |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 43 | def _ReadImageDesc(binman_node, use_expanded): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 44 | """Read the image descriptions from the /binman node |
| 45 | |
| 46 | This normally produces a single Image object called 'image'. But if |
| 47 | multiple images are present, they will all be returned. |
| 48 | |
| 49 | Args: |
| 50 | binman_node: Node object of the /binman node |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 51 | use_expanded: True if the FDT will be updated with the entry information |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 52 | Returns: |
| 53 | OrderedDict of Image objects, each of which describes an image |
| 54 | """ |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 55 | # For Image() |
| 56 | # pylint: disable=E1102 |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 57 | images = OrderedDict() |
| 58 | if 'multiple-images' in binman_node.props: |
| 59 | for node in binman_node.subnodes: |
Simon Glass | 9909c11 | 2023-07-18 07:24:05 -0600 | [diff] [blame^] | 60 | if 'template' not in node.name: |
| 61 | images[node.name] = Image(node.name, node, |
| 62 | use_expanded=use_expanded) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 63 | else: |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 64 | images['image'] = Image('image', binman_node, use_expanded=use_expanded) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 65 | return images |
| 66 | |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 67 | def _FindBinmanNode(dtb): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 68 | """Find the 'binman' node in the device tree |
| 69 | |
| 70 | Args: |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 71 | dtb: Fdt object to scan |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 72 | Returns: |
| 73 | Node object of /binman node, or None if not found |
| 74 | """ |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 75 | for node in dtb.GetRoot().subnodes: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 76 | if node.name == 'binman': |
| 77 | return node |
| 78 | return None |
| 79 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 80 | def _ReadMissingBlobHelp(): |
| 81 | """Read the missing-blob-help file |
| 82 | |
| 83 | This file containins help messages explaining what to do when external blobs |
| 84 | are missing. |
| 85 | |
| 86 | Returns: |
| 87 | Dict: |
| 88 | key: Message tag (str) |
| 89 | value: Message text (str) |
| 90 | """ |
| 91 | |
| 92 | def _FinishTag(tag, msg, result): |
| 93 | if tag: |
| 94 | result[tag] = msg.rstrip() |
| 95 | tag = None |
| 96 | msg = '' |
| 97 | return tag, msg |
| 98 | |
| 99 | my_data = pkg_resources.resource_string(__name__, 'missing-blob-help') |
| 100 | re_tag = re.compile('^([-a-z0-9]+):$') |
| 101 | result = {} |
| 102 | tag = None |
| 103 | msg = '' |
| 104 | for line in my_data.decode('utf-8').splitlines(): |
| 105 | if not line.startswith('#'): |
| 106 | m_tag = re_tag.match(line) |
| 107 | if m_tag: |
| 108 | _, msg = _FinishTag(tag, msg, result) |
| 109 | tag = m_tag.group(1) |
| 110 | elif tag: |
| 111 | msg += line + '\n' |
| 112 | _FinishTag(tag, msg, result) |
| 113 | return result |
| 114 | |
| 115 | def _ShowBlobHelp(path, text): |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 116 | tout.warning('\n%s:' % path) |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 117 | for line in text.splitlines(): |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 118 | tout.warning(' %s' % line) |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 119 | |
| 120 | def _ShowHelpForMissingBlobs(missing_list): |
| 121 | """Show help for each missing blob to help the user take action |
| 122 | |
| 123 | Args: |
| 124 | missing_list: List of Entry objects to show help for |
| 125 | """ |
| 126 | global missing_blob_help |
| 127 | |
| 128 | if not missing_blob_help: |
| 129 | missing_blob_help = _ReadMissingBlobHelp() |
| 130 | |
| 131 | for entry in missing_list: |
| 132 | tags = entry.GetHelpTags() |
| 133 | |
| 134 | # Show the first match help message |
| 135 | for tag in tags: |
| 136 | if tag in missing_blob_help: |
| 137 | _ShowBlobHelp(entry._node.path, missing_blob_help[tag]) |
| 138 | break |
| 139 | |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 140 | def GetEntryModules(include_testing=True): |
| 141 | """Get a set of entry class implementations |
| 142 | |
| 143 | Returns: |
| 144 | Set of paths to entry class filenames |
| 145 | """ |
Simon Glass | c1dc2f8 | 2020-08-29 11:36:14 -0600 | [diff] [blame] | 146 | glob_list = pkg_resources.resource_listdir(__name__, 'etype') |
| 147 | glob_list = [fname for fname in glob_list if fname.endswith('.py')] |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 148 | return set([os.path.splitext(os.path.basename(item))[0] |
| 149 | for item in glob_list |
| 150 | if include_testing or '_testing' not in item]) |
| 151 | |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 152 | def WriteEntryDocs(modules, test_missing=None): |
| 153 | """Write out documentation for all entries |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 154 | |
| 155 | Args: |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 156 | modules: List of Module objects to get docs for |
Simon Glass | 620c446 | 2022-01-09 20:14:11 -0700 | [diff] [blame] | 157 | test_missing: Used for testing only, to force an entry's documentation |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 158 | to show as missing even if it is present. Should be set to None in |
| 159 | normal use. |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 160 | """ |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 161 | from binman.entry import Entry |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 162 | Entry.WriteDocs(modules, test_missing) |
| 163 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 164 | |
Simon Glass | 620c446 | 2022-01-09 20:14:11 -0700 | [diff] [blame] | 165 | def write_bintool_docs(modules, test_missing=None): |
| 166 | """Write out documentation for all bintools |
| 167 | |
| 168 | Args: |
| 169 | modules: List of Module objects to get docs for |
| 170 | test_missing: Used for testing only, to force an entry's documentation |
| 171 | to show as missing even if it is present. Should be set to None in |
| 172 | normal use. |
| 173 | """ |
| 174 | bintool.Bintool.WriteDocs(modules, test_missing) |
| 175 | |
| 176 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 177 | def ListEntries(image_fname, entry_paths): |
| 178 | """List the entries in an image |
| 179 | |
| 180 | This decodes the supplied image and displays a table of entries from that |
| 181 | image, preceded by a header. |
| 182 | |
| 183 | Args: |
| 184 | image_fname: Image filename to process |
| 185 | entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*', |
| 186 | 'section/u-boot']) |
| 187 | """ |
| 188 | image = Image.FromFile(image_fname) |
| 189 | |
| 190 | entries, lines, widths = image.GetListEntries(entry_paths) |
| 191 | |
| 192 | num_columns = len(widths) |
| 193 | for linenum, line in enumerate(lines): |
| 194 | if linenum == 1: |
| 195 | # Print header line |
| 196 | print('-' * (sum(widths) + num_columns * 2)) |
| 197 | out = '' |
| 198 | for i, item in enumerate(line): |
| 199 | width = -widths[i] |
| 200 | if item.startswith('>'): |
| 201 | width = -width |
| 202 | item = item[1:] |
| 203 | txt = '%*s ' % (width, item) |
| 204 | out += txt |
| 205 | print(out.rstrip()) |
| 206 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 207 | |
| 208 | def ReadEntry(image_fname, entry_path, decomp=True): |
| 209 | """Extract an entry from an image |
| 210 | |
| 211 | This extracts the data from a particular entry in an image |
| 212 | |
| 213 | Args: |
| 214 | image_fname: Image filename to process |
| 215 | entry_path: Path to entry to extract |
| 216 | decomp: True to return uncompressed data, if the data is compress |
| 217 | False to return the raw data |
| 218 | |
| 219 | Returns: |
| 220 | data extracted from the entry |
| 221 | """ |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 222 | global Image |
Simon Glass | 90cd6f0 | 2020-08-05 13:27:47 -0600 | [diff] [blame] | 223 | from binman.image import Image |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 224 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 225 | image = Image.FromFile(image_fname) |
Stefan Herbrechtsmeier | 4725030 | 2022-08-19 16:25:22 +0200 | [diff] [blame] | 226 | image.CollectBintools() |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 227 | entry = image.FindEntryPath(entry_path) |
| 228 | return entry.ReadData(decomp) |
| 229 | |
| 230 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 231 | def ShowAltFormats(image): |
| 232 | """Show alternative formats available for entries in the image |
| 233 | |
| 234 | This shows a list of formats available. |
| 235 | |
| 236 | Args: |
| 237 | image (Image): Image to check |
| 238 | """ |
| 239 | alt_formats = {} |
| 240 | image.CheckAltFormats(alt_formats) |
| 241 | print('%-10s %-20s %s' % ('Flag (-F)', 'Entry type', 'Description')) |
| 242 | for name, val in alt_formats.items(): |
| 243 | entry, helptext = val |
| 244 | print('%-10s %-20s %s' % (name, entry.etype, helptext)) |
| 245 | |
| 246 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 247 | def ExtractEntries(image_fname, output_fname, outdir, entry_paths, |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 248 | decomp=True, alt_format=None): |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 249 | """Extract the data from one or more entries and write it to files |
| 250 | |
| 251 | Args: |
| 252 | image_fname: Image filename to process |
| 253 | output_fname: Single output filename to use if extracting one file, None |
| 254 | otherwise |
| 255 | outdir: Output directory to use (for any number of files), else None |
| 256 | entry_paths: List of entry paths to extract |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 257 | decomp: True to decompress the entry data |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 258 | |
| 259 | Returns: |
| 260 | List of EntryInfo records that were written |
| 261 | """ |
| 262 | image = Image.FromFile(image_fname) |
Stefan Herbrechtsmeier | 4725030 | 2022-08-19 16:25:22 +0200 | [diff] [blame] | 263 | image.CollectBintools() |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 264 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 265 | if alt_format == 'list': |
| 266 | ShowAltFormats(image) |
| 267 | return |
| 268 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 269 | # Output an entry to a single file, as a special case |
| 270 | if output_fname: |
| 271 | if not entry_paths: |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 272 | raise ValueError('Must specify an entry path to write with -f') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 273 | if len(entry_paths) != 1: |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 274 | raise ValueError('Must specify exactly one entry path to write with -f') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 275 | entry = image.FindEntryPath(entry_paths[0]) |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 276 | data = entry.ReadData(decomp, alt_format) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 277 | tools.write_file(output_fname, data) |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 278 | tout.notice("Wrote %#x bytes to file '%s'" % (len(data), output_fname)) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 279 | return |
| 280 | |
| 281 | # Otherwise we will output to a path given by the entry path of each entry. |
| 282 | # This means that entries will appear in subdirectories if they are part of |
| 283 | # a sub-section. |
| 284 | einfos = image.GetListEntries(entry_paths)[0] |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 285 | tout.notice('%d entries match and will be written' % len(einfos)) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 286 | for einfo in einfos: |
| 287 | entry = einfo.entry |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 288 | data = entry.ReadData(decomp, alt_format) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 289 | path = entry.GetPath()[1:] |
| 290 | fname = os.path.join(outdir, path) |
| 291 | |
| 292 | # If this entry has children, create a directory for it and put its |
| 293 | # data in a file called 'root' in that directory |
| 294 | if entry.GetEntries(): |
Simon Glass | 4ef93d9 | 2021-03-18 20:24:51 +1300 | [diff] [blame] | 295 | if fname and not os.path.exists(fname): |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 296 | os.makedirs(fname) |
| 297 | fname = os.path.join(fname, 'root') |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 298 | tout.notice("Write entry '%s' size %x to '%s'" % |
Simon Glass | 0834945 | 2021-01-06 21:35:13 -0700 | [diff] [blame] | 299 | (entry.GetPath(), len(data), fname)) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 300 | tools.write_file(fname, data) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 301 | return einfos |
| 302 | |
| 303 | |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 304 | def BeforeReplace(image, allow_resize): |
| 305 | """Handle getting an image ready for replacing entries in it |
| 306 | |
| 307 | Args: |
| 308 | image: Image to prepare |
| 309 | """ |
| 310 | state.PrepareFromLoadedData(image) |
| 311 | image.LoadData() |
Alper Nebi Yasak | e63ca5a | 2022-03-27 18:31:45 +0300 | [diff] [blame] | 312 | image.CollectBintools() |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 313 | |
| 314 | # If repacking, drop the old offset/size values except for the original |
| 315 | # ones, so we are only left with the constraints. |
Alper Nebi Yasak | 00c68f1 | 2022-03-27 18:31:46 +0300 | [diff] [blame] | 316 | if image.allow_repack and allow_resize: |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 317 | image.ResetForPack() |
| 318 | |
| 319 | |
| 320 | def ReplaceOneEntry(image, entry, data, do_compress, allow_resize): |
| 321 | """Handle replacing a single entry an an image |
| 322 | |
| 323 | Args: |
| 324 | image: Image to update |
| 325 | entry: Entry to write |
| 326 | data: Data to replace with |
| 327 | do_compress: True to compress the data if needed, False if data is |
| 328 | already compressed so should be used as is |
| 329 | allow_resize: True to allow entries to change size (this does a re-pack |
| 330 | of the entries), False to raise an exception |
| 331 | """ |
| 332 | if not entry.WriteData(data, do_compress): |
| 333 | if not image.allow_repack: |
| 334 | entry.Raise('Entry data size does not match, but allow-repack is not present for this image') |
| 335 | if not allow_resize: |
| 336 | entry.Raise('Entry data size does not match, but resize is disabled') |
| 337 | |
| 338 | |
| 339 | def AfterReplace(image, allow_resize, write_map): |
| 340 | """Handle write out an image after replacing entries in it |
| 341 | |
| 342 | Args: |
| 343 | image: Image to write |
| 344 | allow_resize: True to allow entries to change size (this does a re-pack |
| 345 | of the entries), False to raise an exception |
| 346 | write_map: True to write a map file |
| 347 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 348 | tout.info('Processing image') |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 349 | ProcessImage(image, update_fdt=True, write_map=write_map, |
| 350 | get_contents=False, allow_resize=allow_resize) |
| 351 | |
| 352 | |
| 353 | def WriteEntryToImage(image, entry, data, do_compress=True, allow_resize=True, |
| 354 | write_map=False): |
| 355 | BeforeReplace(image, allow_resize) |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 356 | tout.info('Writing data to %s' % entry.GetPath()) |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 357 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 358 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 359 | |
| 360 | |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 361 | def WriteEntry(image_fname, entry_path, data, do_compress=True, |
| 362 | allow_resize=True, write_map=False): |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 363 | """Replace an entry in an image |
| 364 | |
| 365 | This replaces the data in a particular entry in an image. This size of the |
| 366 | new data must match the size of the old data unless allow_resize is True. |
| 367 | |
| 368 | Args: |
| 369 | image_fname: Image filename to process |
| 370 | entry_path: Path to entry to extract |
| 371 | data: Data to replace with |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 372 | do_compress: True to compress the data if needed, False if data is |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 373 | already compressed so should be used as is |
| 374 | allow_resize: True to allow entries to change size (this does a re-pack |
| 375 | of the entries), False to raise an exception |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 376 | write_map: True to write a map file |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 377 | |
| 378 | Returns: |
| 379 | Image object that was updated |
| 380 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 381 | tout.info("Write entry '%s', file '%s'" % (entry_path, image_fname)) |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 382 | image = Image.FromFile(image_fname) |
Stefan Herbrechtsmeier | 4725030 | 2022-08-19 16:25:22 +0200 | [diff] [blame] | 383 | image.CollectBintools() |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 384 | entry = image.FindEntryPath(entry_path) |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 385 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 386 | allow_resize=allow_resize, write_map=write_map) |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 387 | |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 388 | return image |
| 389 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 390 | |
| 391 | def ReplaceEntries(image_fname, input_fname, indir, entry_paths, |
| 392 | do_compress=True, allow_resize=True, write_map=False): |
| 393 | """Replace the data from one or more entries from input files |
| 394 | |
| 395 | Args: |
| 396 | image_fname: Image filename to process |
Jan Kiszka | 8ea4443 | 2021-11-11 08:13:30 +0100 | [diff] [blame] | 397 | input_fname: Single input filename to use if replacing one file, None |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 398 | otherwise |
| 399 | indir: Input directory to use (for any number of files), else None |
Jan Kiszka | 8ea4443 | 2021-11-11 08:13:30 +0100 | [diff] [blame] | 400 | entry_paths: List of entry paths to replace |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 401 | do_compress: True if the input data is uncompressed and may need to be |
| 402 | compressed if the entry requires it, False if the data is already |
| 403 | compressed. |
| 404 | write_map: True to write a map file |
| 405 | |
| 406 | Returns: |
| 407 | List of EntryInfo records that were written |
| 408 | """ |
Jan Kiszka | afc8f29 | 2021-11-11 08:14:18 +0100 | [diff] [blame] | 409 | image_fname = os.path.abspath(image_fname) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 410 | image = Image.FromFile(image_fname) |
| 411 | |
Simon Glass | 49b77e8 | 2023-03-02 17:02:44 -0700 | [diff] [blame] | 412 | image.mark_build_done() |
| 413 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 414 | # Replace an entry from a single file, as a special case |
| 415 | if input_fname: |
| 416 | if not entry_paths: |
| 417 | raise ValueError('Must specify an entry path to read with -f') |
| 418 | if len(entry_paths) != 1: |
| 419 | raise ValueError('Must specify exactly one entry path to write with -f') |
| 420 | entry = image.FindEntryPath(entry_paths[0]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 421 | data = tools.read_file(input_fname) |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 422 | tout.notice("Read %#x bytes from file '%s'" % (len(data), input_fname)) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 423 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 424 | allow_resize=allow_resize, write_map=write_map) |
| 425 | return |
| 426 | |
| 427 | # Otherwise we will input from a path given by the entry path of each entry. |
| 428 | # This means that files must appear in subdirectories if they are part of |
| 429 | # a sub-section. |
| 430 | einfos = image.GetListEntries(entry_paths)[0] |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 431 | tout.notice("Replacing %d matching entries in image '%s'" % |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 432 | (len(einfos), image_fname)) |
| 433 | |
| 434 | BeforeReplace(image, allow_resize) |
| 435 | |
| 436 | for einfo in einfos: |
| 437 | entry = einfo.entry |
| 438 | if entry.GetEntries(): |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 439 | tout.info("Skipping section entry '%s'" % entry.GetPath()) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 440 | continue |
| 441 | |
| 442 | path = entry.GetPath()[1:] |
| 443 | fname = os.path.join(indir, path) |
| 444 | |
| 445 | if os.path.exists(fname): |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 446 | tout.notice("Write entry '%s' from file '%s'" % |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 447 | (entry.GetPath(), fname)) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 448 | data = tools.read_file(fname) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 449 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 450 | else: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 451 | tout.warning("Skipping entry '%s' from missing file '%s'" % |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 452 | (entry.GetPath(), fname)) |
| 453 | |
| 454 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 455 | return image |
| 456 | |
Ivan Mikhaylov | 8a80386 | 2023-03-08 01:13:39 +0000 | [diff] [blame] | 457 | def SignEntries(image_fname, input_fname, privatekey_fname, algo, entry_paths, |
| 458 | write_map=False): |
| 459 | """Sign and replace the data from one or more entries from input files |
| 460 | |
| 461 | Args: |
| 462 | image_fname: Image filename to process |
| 463 | input_fname: Single input filename to use if replacing one file, None |
| 464 | otherwise |
| 465 | algo: Hashing algorithm |
| 466 | entry_paths: List of entry paths to sign |
| 467 | privatekey_fname: Private key filename |
| 468 | write_map (bool): True to write the map file |
| 469 | """ |
| 470 | image_fname = os.path.abspath(image_fname) |
| 471 | image = Image.FromFile(image_fname) |
| 472 | |
Ivan Mikhaylov | 3cfcaa4d | 2023-03-08 01:13:40 +0000 | [diff] [blame] | 473 | image.mark_build_done() |
| 474 | |
Ivan Mikhaylov | 8a80386 | 2023-03-08 01:13:39 +0000 | [diff] [blame] | 475 | BeforeReplace(image, allow_resize=True) |
| 476 | |
| 477 | for entry_path in entry_paths: |
| 478 | entry = image.FindEntryPath(entry_path) |
| 479 | entry.UpdateSignatures(privatekey_fname, algo, input_fname) |
| 480 | |
| 481 | AfterReplace(image, allow_resize=True, write_map=write_map) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 482 | |
Simon Glass | fc79284 | 2023-07-18 07:24:04 -0600 | [diff] [blame] | 483 | def _ProcessTemplates(parent): |
| 484 | """Handle any templates in the binman description |
| 485 | |
| 486 | Args: |
| 487 | parent: Binman node to process (typically /binman) |
| 488 | |
| 489 | Search though each target node looking for those with an 'insert-template' |
| 490 | property. Use that as a list of references to template nodes to use to |
| 491 | adjust the target node. |
| 492 | |
| 493 | Processing involves copying each subnode of the template node into the |
| 494 | target node. |
| 495 | |
| 496 | For now this is not done recursively, so templates must be at the top level |
| 497 | of the binman image. |
| 498 | |
| 499 | See 'Templates' in the Binman documnentation for details. |
| 500 | """ |
| 501 | for node in parent.subnodes: |
| 502 | tmpl = fdt_util.GetPhandleList(node, 'insert-template') |
| 503 | if tmpl: |
| 504 | node.copy_subnodes_from_phandles(tmpl) |
| 505 | |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 506 | def PrepareImagesAndDtbs(dtb_fname, select_images, update_fdt, use_expanded): |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 507 | """Prepare the images to be processed and select the device tree |
| 508 | |
| 509 | This function: |
| 510 | - reads in the device tree |
| 511 | - finds and scans the binman node to create all entries |
| 512 | - selects which images to build |
| 513 | - Updates the device tress with placeholder properties for offset, |
| 514 | image-pos, etc. |
| 515 | |
| 516 | Args: |
| 517 | dtb_fname: Filename of the device tree file to use (.dts or .dtb) |
| 518 | selected_images: List of images to output, or None for all |
| 519 | update_fdt: True to update the FDT wth entry offsets, etc. |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 520 | use_expanded: True to use expanded versions of entries, if available. |
| 521 | So if 'u-boot' is called for, we use 'u-boot-expanded' instead. This |
| 522 | is needed if update_fdt is True (although tests may disable it) |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 523 | |
| 524 | Returns: |
| 525 | OrderedDict of images: |
| 526 | key: Image name (str) |
| 527 | value: Image object |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 528 | """ |
| 529 | # Import these here in case libfdt.py is not available, in which case |
| 530 | # the above help option still works. |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 531 | from dtoc import fdt |
| 532 | from dtoc import fdt_util |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 533 | global images |
| 534 | |
| 535 | # Get the device tree ready by compiling it and copying the compiled |
| 536 | # output into a file in our output directly. Then scan it for use |
| 537 | # in binman. |
| 538 | dtb_fname = fdt_util.EnsureCompiled(dtb_fname) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 539 | fname = tools.get_output_filename('u-boot.dtb.out') |
| 540 | tools.write_file(fname, tools.read_file(dtb_fname)) |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 541 | dtb = fdt.FdtScan(fname) |
| 542 | |
| 543 | node = _FindBinmanNode(dtb) |
| 544 | if not node: |
| 545 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 546 | "node" % dtb_fname) |
| 547 | |
Simon Glass | fc79284 | 2023-07-18 07:24:04 -0600 | [diff] [blame] | 548 | _ProcessTemplates(node) |
| 549 | |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 550 | images = _ReadImageDesc(node, use_expanded) |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 551 | |
| 552 | if select_images: |
| 553 | skip = [] |
| 554 | new_images = OrderedDict() |
| 555 | for name, image in images.items(): |
| 556 | if name in select_images: |
| 557 | new_images[name] = image |
| 558 | else: |
| 559 | skip.append(name) |
| 560 | images = new_images |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 561 | tout.notice('Skipping images: %s' % ', '.join(skip)) |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 562 | |
| 563 | state.Prepare(images, dtb) |
| 564 | |
| 565 | # Prepare the device tree by making sure that any missing |
| 566 | # properties are added (e.g. 'pos' and 'size'). The values of these |
| 567 | # may not be correct yet, but we add placeholders so that the |
| 568 | # size of the device tree is correct. Later, in |
| 569 | # SetCalculatedProperties() we will insert the correct values |
| 570 | # without changing the device-tree size, thus ensuring that our |
| 571 | # entry offsets remain the same. |
| 572 | for image in images.values(): |
Simon Glass | f86ddad | 2022-03-05 20:19:00 -0700 | [diff] [blame] | 573 | image.gen_entries() |
Stefan Herbrechtsmeier | 4725030 | 2022-08-19 16:25:22 +0200 | [diff] [blame] | 574 | image.CollectBintools() |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 575 | if update_fdt: |
Simon Glass | acd6c6e | 2020-10-26 17:40:17 -0600 | [diff] [blame] | 576 | image.AddMissingProperties(True) |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 577 | image.ProcessFdt(dtb) |
| 578 | |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 579 | for dtb_item in state.GetAllFdts(): |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 580 | dtb_item.Sync(auto_resize=True) |
| 581 | dtb_item.Pack() |
| 582 | dtb_item.Flush() |
| 583 | return images |
| 584 | |
| 585 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 586 | def ProcessImage(image, update_fdt, write_map, get_contents=True, |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 587 | allow_resize=True, allow_missing=False, |
| 588 | allow_fake_blobs=False): |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 589 | """Perform all steps for this image, including checking and # writing it. |
| 590 | |
| 591 | This means that errors found with a later image will be reported after |
| 592 | earlier images are already completed and written, but that does not seem |
| 593 | important. |
| 594 | |
| 595 | Args: |
| 596 | image: Image to process |
| 597 | update_fdt: True to update the FDT wth entry offsets, etc. |
| 598 | write_map: True to write a map file |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 599 | get_contents: True to get the image contents from files, etc., False if |
| 600 | the contents is already present |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 601 | allow_resize: True to allow entries to change size (this does a re-pack |
| 602 | of the entries), False to raise an exception |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 603 | allow_missing: Allow blob_ext objects to be missing |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 604 | allow_fake_blobs: Allow blob_ext objects to be faked with dummy files |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 605 | |
| 606 | Returns: |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 607 | True if one or more external blobs are missing or faked, |
| 608 | False if all are present |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 609 | """ |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 610 | if get_contents: |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 611 | image.SetAllowMissing(allow_missing) |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 612 | image.SetAllowFakeBlob(allow_fake_blobs) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 613 | image.GetEntryContents() |
Simon Glass | 1e9e61c | 2023-01-07 14:07:12 -0700 | [diff] [blame] | 614 | image.drop_absent() |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 615 | image.GetEntryOffsets() |
| 616 | |
| 617 | # We need to pack the entries to figure out where everything |
| 618 | # should be placed. This sets the offset/size of each entry. |
| 619 | # However, after packing we call ProcessEntryContents() which |
| 620 | # may result in an entry changing size. In that case we need to |
| 621 | # do another pass. Since the device tree often contains the |
| 622 | # final offset/size information we try to make space for this in |
| 623 | # AddMissingProperties() above. However, if the device is |
| 624 | # compressed we cannot know this compressed size in advance, |
| 625 | # since changing an offset from 0x100 to 0x104 (for example) can |
| 626 | # alter the compressed size of the device tree. So we need a |
| 627 | # third pass for this. |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 628 | passes = 5 |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 629 | for pack_pass in range(passes): |
| 630 | try: |
| 631 | image.PackEntries() |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 632 | except Exception as e: |
| 633 | if write_map: |
| 634 | fname = image.WriteMap() |
| 635 | print("Wrote map file '%s' to show errors" % fname) |
| 636 | raise |
| 637 | image.SetImagePos() |
| 638 | if update_fdt: |
| 639 | image.SetCalculatedProperties() |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 640 | for dtb_item in state.GetAllFdts(): |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 641 | dtb_item.Sync() |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 642 | dtb_item.Flush() |
Simon Glass | e594341 | 2019-08-24 07:23:12 -0600 | [diff] [blame] | 643 | image.WriteSymbols() |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 644 | sizes_ok = image.ProcessEntryContents() |
| 645 | if sizes_ok: |
| 646 | break |
| 647 | image.ResetForPack() |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 648 | tout.info('Pack completed after %d pass(es)' % (pack_pass + 1)) |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 649 | if not sizes_ok: |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 650 | image.Raise('Entries changed size after packing (tried %s passes)' % |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 651 | passes) |
| 652 | |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 653 | image.BuildImage() |
| 654 | if write_map: |
| 655 | image.WriteMap() |
Simon Glass | 63328f1 | 2023-01-07 14:07:15 -0700 | [diff] [blame] | 656 | |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 657 | missing_list = [] |
| 658 | image.CheckMissing(missing_list) |
| 659 | if missing_list: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 660 | tout.warning("Image '%s' is missing external blobs and is non-functional: %s" % |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 661 | (image.name, ' '.join([e.name for e in missing_list]))) |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 662 | _ShowHelpForMissingBlobs(missing_list) |
Simon Glass | 63328f1 | 2023-01-07 14:07:15 -0700 | [diff] [blame] | 663 | |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 664 | faked_list = [] |
| 665 | image.CheckFakedBlobs(faked_list) |
| 666 | if faked_list: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 667 | tout.warning( |
Simon Glass | f9f3403 | 2022-01-09 20:13:45 -0700 | [diff] [blame] | 668 | "Image '%s' has faked external blobs and is non-functional: %s" % |
| 669 | (image.name, ' '.join([os.path.basename(e.GetDefaultFilename()) |
| 670 | for e in faked_list]))) |
Simon Glass | 63328f1 | 2023-01-07 14:07:15 -0700 | [diff] [blame] | 671 | |
| 672 | optional_list = [] |
| 673 | image.CheckOptional(optional_list) |
| 674 | if optional_list: |
| 675 | tout.warning( |
| 676 | "Image '%s' is missing external blobs but is still functional: %s" % |
| 677 | (image.name, ' '.join([e.name for e in optional_list]))) |
| 678 | _ShowHelpForMissingBlobs(optional_list) |
| 679 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 680 | missing_bintool_list = [] |
| 681 | image.check_missing_bintools(missing_bintool_list) |
| 682 | if missing_bintool_list: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 683 | tout.warning( |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 684 | "Image '%s' has missing bintools and is non-functional: %s" % |
| 685 | (image.name, ' '.join([os.path.basename(bintool.name) |
| 686 | for bintool in missing_bintool_list]))) |
| 687 | return any([missing_list, faked_list, missing_bintool_list]) |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 688 | |
| 689 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 690 | def Binman(args): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 691 | """The main control code for binman |
| 692 | |
| 693 | This assumes that help and test options have already been dealt with. It |
| 694 | deals with the core task of building images. |
| 695 | |
| 696 | Args: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 697 | args: Command line arguments Namespace object |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 698 | """ |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 699 | global Image |
| 700 | global state |
| 701 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 702 | if args.full_help: |
Simon Glass | feb80cc | 2023-02-23 18:18:20 -0700 | [diff] [blame] | 703 | with importlib.resources.path('binman', 'README.rst') as readme: |
| 704 | tools.print_full_help(str(readme)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 705 | return 0 |
| 706 | |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 707 | # Put these here so that we can import this module without libfdt |
Simon Glass | 90cd6f0 | 2020-08-05 13:27:47 -0600 | [diff] [blame] | 708 | from binman.image import Image |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 709 | from binman import state |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 710 | |
Simon Glass | 9a1c726 | 2023-02-22 12:14:49 -0700 | [diff] [blame] | 711 | tool_paths = [] |
| 712 | if args.toolpath: |
| 713 | tool_paths += args.toolpath |
| 714 | if args.tooldir: |
| 715 | tool_paths.append(args.tooldir) |
| 716 | tools.set_tool_paths(tool_paths or None) |
| 717 | bintool.Bintool.set_tool_dir(args.tooldir) |
| 718 | |
Ivan Mikhaylov | 8a80386 | 2023-03-08 01:13:39 +0000 | [diff] [blame] | 719 | if args.cmd in ['ls', 'extract', 'replace', 'tool', 'sign']: |
Simon Glass | 9b7f500 | 2019-07-20 12:23:53 -0600 | [diff] [blame] | 720 | try: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 721 | tout.init(args.verbosity) |
Simon Glass | b9b9b27 | 2023-03-02 17:02:42 -0700 | [diff] [blame] | 722 | if args.cmd == 'replace': |
| 723 | tools.prepare_output_dir(args.outdir, args.preserve) |
| 724 | else: |
| 725 | tools.prepare_output_dir(None) |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 726 | if args.cmd == 'ls': |
| 727 | ListEntries(args.image, args.paths) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 728 | |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 729 | if args.cmd == 'extract': |
| 730 | ExtractEntries(args.image, args.filename, args.outdir, args.paths, |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 731 | not args.uncompressed, args.format) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 732 | |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 733 | if args.cmd == 'replace': |
| 734 | ReplaceEntries(args.image, args.filename, args.indir, args.paths, |
| 735 | do_compress=not args.compressed, |
| 736 | allow_resize=not args.fix_size, write_map=args.map) |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 737 | |
Ivan Mikhaylov | 8a80386 | 2023-03-08 01:13:39 +0000 | [diff] [blame] | 738 | if args.cmd == 'sign': |
| 739 | SignEntries(args.image, args.file, args.key, args.algo, args.paths) |
| 740 | |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 741 | if args.cmd == 'tool': |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 742 | if args.list: |
| 743 | bintool.Bintool.list_all() |
| 744 | elif args.fetch: |
| 745 | if not args.bintools: |
| 746 | raise ValueError( |
| 747 | "Please specify bintools to fetch or 'all' or 'missing'") |
| 748 | bintool.Bintool.fetch_tools(bintool.FETCH_ANY, |
| 749 | args.bintools) |
| 750 | else: |
| 751 | raise ValueError("Invalid arguments to 'tool' subcommand") |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 752 | except: |
| 753 | raise |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 754 | finally: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 755 | tools.finalise_output_dir() |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 756 | return 0 |
| 757 | |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 758 | elf_params = None |
| 759 | if args.update_fdt_in_elf: |
| 760 | elf_params = args.update_fdt_in_elf.split(',') |
| 761 | if len(elf_params) != 4: |
| 762 | raise ValueError('Invalid args %s to --update-fdt-in-elf: expected infile,outfile,begin_sym,end_sym' % |
| 763 | elf_params) |
| 764 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 765 | # Try to figure out which device tree contains our image description |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 766 | if args.dt: |
| 767 | dtb_fname = args.dt |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 768 | else: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 769 | board = args.board |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 770 | if not board: |
| 771 | raise ValueError('Must provide a board to process (use -b <board>)') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 772 | board_pathname = os.path.join(args.build_dir, board) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 773 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 774 | if not args.indir: |
| 775 | args.indir = ['.'] |
| 776 | args.indir.append(board_pathname) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 777 | |
| 778 | try: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 779 | tout.init(args.verbosity) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 780 | elf.debug = args.debug |
| 781 | cbfs_util.VERBOSE = args.verbosity > 2 |
| 782 | state.use_fake_dtb = args.fake_dtb |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 783 | |
| 784 | # Normally we replace the 'u-boot' etype with 'u-boot-expanded', etc. |
| 785 | # When running tests this can be disabled using this flag. When not |
| 786 | # updating the FDT in image, it is not needed by binman, but we use it |
| 787 | # for consistency, so that the images look the same to U-Boot at |
| 788 | # runtime. |
| 789 | use_expanded = not args.no_expanded |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 790 | try: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 791 | tools.set_input_dirs(args.indir) |
| 792 | tools.prepare_output_dir(args.outdir, args.preserve) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 793 | state.SetEntryArgs(args.entry_arg) |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 794 | state.SetThreads(args.threads) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 795 | |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 796 | images = PrepareImagesAndDtbs(dtb_fname, args.image, |
Simon Glass | 55ab0b6 | 2021-03-18 20:25:06 +1300 | [diff] [blame] | 797 | args.update_fdt, use_expanded) |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 798 | |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 799 | if args.test_section_timeout: |
| 800 | # Set the first image to timeout, used in testThreadTimeout() |
| 801 | images[list(images.keys())[0]].test_section_timeout = True |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 802 | invalid = False |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 803 | bintool.Bintool.set_missing_list( |
| 804 | args.force_missing_bintools.split(',') if |
| 805 | args.force_missing_bintools else None) |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 806 | |
| 807 | # Create the directory here instead of Entry.check_fake_fname() |
| 808 | # since that is called from a threaded context so different threads |
| 809 | # may race to create the directory |
| 810 | if args.fake_ext_blobs: |
| 811 | entry.Entry.create_fake_dir() |
| 812 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 813 | for image in images.values(): |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 814 | invalid |= ProcessImage(image, args.update_fdt, args.map, |
| 815 | allow_missing=args.allow_missing, |
| 816 | allow_fake_blobs=args.fake_ext_blobs) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 817 | |
| 818 | # Write the updated FDTs to our output files |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 819 | for dtb_item in state.GetAllFdts(): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 820 | tools.write_file(dtb_item._fname, dtb_item.GetContents()) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 821 | |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 822 | if elf_params: |
| 823 | data = state.GetFdtForEtype('u-boot-dtb').GetContents() |
| 824 | elf.UpdateFile(*elf_params, data) |
| 825 | |
Simon Glass | 6bce5dc | 2022-11-09 19:14:42 -0700 | [diff] [blame] | 826 | # This can only be True if -M is provided, since otherwise binman |
| 827 | # would have raised an error already |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 828 | if invalid: |
Simon Glass | 6bce5dc | 2022-11-09 19:14:42 -0700 | [diff] [blame] | 829 | msg = '\nSome images are invalid' |
| 830 | if args.ignore_missing: |
| 831 | tout.warning(msg) |
| 832 | else: |
| 833 | tout.error(msg) |
| 834 | return 103 |
Simon Glass | 748a1d4 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 835 | |
| 836 | # Use this to debug the time take to pack the image |
| 837 | #state.TimingShow() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 838 | finally: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 839 | tools.finalise_output_dir() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 840 | finally: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 841 | tout.uninit() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 842 | |
| 843 | return 0 |