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