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