blob: 963f9b96638d15928f4d06098042123ad3469198 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass2574ef62016-11-25 20:15:51 -07005# Creates binary images from input files controlled by a description
6#
7
8from collections import OrderedDict
Simon Glass220ff5f2020-08-05 13:27:46 -06009import glob
Jan Kiszka6d7c40c2023-04-22 16:42:48 +020010try:
11 import importlib.resources
Simon Glass245c52c2023-06-01 10:22:25 -060012except ImportError: # pragma: no cover
Jan Kiszka6d7c40c2023-04-22 16:42:48 +020013 # for Python 3.6
14 import importlib_resources
Simon Glass2574ef62016-11-25 20:15:51 -070015import os
Simon Glassc1dc2f82020-08-29 11:36:14 -060016import pkg_resources
Simon Glassa820af72020-09-06 10:39:09 -060017import re
Simon Glassc1dc2f82020-08-29 11:36:14 -060018
Simon Glass2574ef62016-11-25 20:15:51 -070019import sys
Simon Glass2574ef62016-11-25 20:15:51 -070020
Simon Glass4eae9252022-01-09 20:13:50 -070021from binman import bintool
Simon Glassc585dd42020-04-17 18:09:03 -060022from binman import cbfs_util
Simon Glass7d3e4072022-08-07 09:46:46 -060023from binman import elf
24from binman import entry
Simon Glassfc792842023-07-18 07:24:04 -060025from dtoc import fdt_util
Simon Glass131444f2023-02-23 18:18:04 -070026from u_boot_pylib import command
27from u_boot_pylib import tools
28from u_boot_pylib import tout
Simon Glass2574ef62016-11-25 20:15:51 -070029
Simon Glass2a0fa982022-02-11 13:23:21 -070030# These are imported if needed since they import libfdt
31state = None
32Image = None
33
Simon Glass2574ef62016-11-25 20:15:51 -070034# List of images we plan to create
35# Make this global so that it can be referenced from tests
36images = OrderedDict()
37
Simon Glassa820af72020-09-06 10:39:09 -060038# Help text for each type of missing blob, dict:
39# key: Value of the entry's 'missing-msg' or entry name
40# value: Text for the help
41missing_blob_help = {}
42
Simon Glass55ab0b62021-03-18 20:25:06 +130043def _ReadImageDesc(binman_node, use_expanded):
Simon Glass2574ef62016-11-25 20:15:51 -070044 """Read the image descriptions from the /binman node
45
46 This normally produces a single Image object called 'image'. But if
47 multiple images are present, they will all be returned.
48
49 Args:
50 binman_node: Node object of the /binman node
Simon Glass55ab0b62021-03-18 20:25:06 +130051 use_expanded: True if the FDT will be updated with the entry information
Simon Glass2574ef62016-11-25 20:15:51 -070052 Returns:
53 OrderedDict of Image objects, each of which describes an image
54 """
Simon Glass2a0fa982022-02-11 13:23:21 -070055 # For Image()
56 # pylint: disable=E1102
Simon Glass2574ef62016-11-25 20:15:51 -070057 images = OrderedDict()
58 if 'multiple-images' in binman_node.props:
59 for node in binman_node.subnodes:
Simon Glass9909c112023-07-18 07:24:05 -060060 if 'template' not in node.name:
61 images[node.name] = Image(node.name, node,
62 use_expanded=use_expanded)
Simon Glass2574ef62016-11-25 20:15:51 -070063 else:
Simon Glass55ab0b62021-03-18 20:25:06 +130064 images['image'] = Image('image', binman_node, use_expanded=use_expanded)
Simon Glass2574ef62016-11-25 20:15:51 -070065 return images
66
Simon Glass22c92ca2017-05-27 07:38:29 -060067def _FindBinmanNode(dtb):
Simon Glass2574ef62016-11-25 20:15:51 -070068 """Find the 'binman' node in the device tree
69
70 Args:
Simon Glass22c92ca2017-05-27 07:38:29 -060071 dtb: Fdt object to scan
Simon Glass2574ef62016-11-25 20:15:51 -070072 Returns:
73 Node object of /binman node, or None if not found
74 """
Simon Glass22c92ca2017-05-27 07:38:29 -060075 for node in dtb.GetRoot().subnodes:
Simon Glass2574ef62016-11-25 20:15:51 -070076 if node.name == 'binman':
77 return node
78 return None
79
Simon Glassa820af72020-09-06 10:39:09 -060080def _ReadMissingBlobHelp():
81 """Read the missing-blob-help file
82
83 This file containins help messages explaining what to do when external blobs
84 are missing.
85
86 Returns:
87 Dict:
88 key: Message tag (str)
89 value: Message text (str)
90 """
91
92 def _FinishTag(tag, msg, result):
93 if tag:
94 result[tag] = msg.rstrip()
95 tag = None
96 msg = ''
97 return tag, msg
98
99 my_data = pkg_resources.resource_string(__name__, 'missing-blob-help')
100 re_tag = re.compile('^([-a-z0-9]+):$')
101 result = {}
102 tag = None
103 msg = ''
104 for line in my_data.decode('utf-8').splitlines():
105 if not line.startswith('#'):
106 m_tag = re_tag.match(line)
107 if m_tag:
108 _, msg = _FinishTag(tag, msg, result)
109 tag = m_tag.group(1)
110 elif tag:
111 msg += line + '\n'
112 _FinishTag(tag, msg, result)
113 return result
114
Jonas Karlmanda423fc2023-07-18 20:34:39 +0000115def _ShowBlobHelp(level, path, text, fname):
116 tout.do_output(level, '%s (%s):' % (path, fname))
Simon Glassa820af72020-09-06 10:39:09 -0600117 for line in text.splitlines():
Jonas Karlman34f520c2023-07-18 20:34:35 +0000118 tout.do_output(level, ' %s' % line)
Jonas Karlmana0335b42023-07-18 20:34:37 +0000119 tout.do_output(level, '')
Simon Glassa820af72020-09-06 10:39:09 -0600120
Jonas Karlman34f520c2023-07-18 20:34:35 +0000121def _ShowHelpForMissingBlobs(level, missing_list):
Simon Glassa820af72020-09-06 10:39:09 -0600122 """Show help for each missing blob to help the user take action
123
124 Args:
125 missing_list: List of Entry objects to show help for
126 """
127 global missing_blob_help
128
129 if not missing_blob_help:
130 missing_blob_help = _ReadMissingBlobHelp()
131
132 for entry in missing_list:
133 tags = entry.GetHelpTags()
134
135 # Show the first match help message
Jonas Karlmanda423fc2023-07-18 20:34:39 +0000136 shown_help = False
Simon Glassa820af72020-09-06 10:39:09 -0600137 for tag in tags:
138 if tag in missing_blob_help:
Jonas Karlmanda423fc2023-07-18 20:34:39 +0000139 _ShowBlobHelp(level, entry._node.path, missing_blob_help[tag],
140 entry.GetDefaultFilename())
141 shown_help = True
Simon Glassa820af72020-09-06 10:39:09 -0600142 break
Jonas Karlmanda423fc2023-07-18 20:34:39 +0000143 # Or a generic help message
144 if not shown_help:
145 _ShowBlobHelp(level, entry._node.path, "Missing blob",
146 entry.GetDefaultFilename())
Simon Glassa820af72020-09-06 10:39:09 -0600147
Simon Glass220ff5f2020-08-05 13:27:46 -0600148def GetEntryModules(include_testing=True):
149 """Get a set of entry class implementations
150
151 Returns:
152 Set of paths to entry class filenames
153 """
Simon Glassc1dc2f82020-08-29 11:36:14 -0600154 glob_list = pkg_resources.resource_listdir(__name__, 'etype')
155 glob_list = [fname for fname in glob_list if fname.endswith('.py')]
Simon Glass220ff5f2020-08-05 13:27:46 -0600156 return set([os.path.splitext(os.path.basename(item))[0]
157 for item in glob_list
158 if include_testing or '_testing' not in item])
159
Simon Glass29aa7362018-09-14 04:57:19 -0600160def WriteEntryDocs(modules, test_missing=None):
161 """Write out documentation for all entries
Simon Glass92307732018-07-06 10:27:40 -0600162
163 Args:
Simon Glass29aa7362018-09-14 04:57:19 -0600164 modules: List of Module objects to get docs for
Simon Glass620c4462022-01-09 20:14:11 -0700165 test_missing: Used for testing only, to force an entry's documentation
Simon Glass29aa7362018-09-14 04:57:19 -0600166 to show as missing even if it is present. Should be set to None in
167 normal use.
Simon Glass92307732018-07-06 10:27:40 -0600168 """
Simon Glassc585dd42020-04-17 18:09:03 -0600169 from binman.entry import Entry
Simon Glass969616c2018-07-17 13:25:36 -0600170 Entry.WriteDocs(modules, test_missing)
171
Simon Glassb2fd11d2019-07-08 14:25:48 -0600172
Simon Glass620c4462022-01-09 20:14:11 -0700173def write_bintool_docs(modules, test_missing=None):
174 """Write out documentation for all bintools
175
176 Args:
177 modules: List of Module objects to get docs for
178 test_missing: Used for testing only, to force an entry's documentation
179 to show as missing even if it is present. Should be set to None in
180 normal use.
181 """
182 bintool.Bintool.WriteDocs(modules, test_missing)
183
184
Simon Glassb2fd11d2019-07-08 14:25:48 -0600185def ListEntries(image_fname, entry_paths):
186 """List the entries in an image
187
188 This decodes the supplied image and displays a table of entries from that
189 image, preceded by a header.
190
191 Args:
192 image_fname: Image filename to process
193 entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*',
194 'section/u-boot'])
195 """
196 image = Image.FromFile(image_fname)
197
198 entries, lines, widths = image.GetListEntries(entry_paths)
199
200 num_columns = len(widths)
201 for linenum, line in enumerate(lines):
202 if linenum == 1:
203 # Print header line
204 print('-' * (sum(widths) + num_columns * 2))
205 out = ''
206 for i, item in enumerate(line):
207 width = -widths[i]
208 if item.startswith('>'):
209 width = -width
210 item = item[1:]
211 txt = '%*s ' % (width, item)
212 out += txt
213 print(out.rstrip())
214
Simon Glass4c613bf2019-07-08 14:25:50 -0600215
216def ReadEntry(image_fname, entry_path, decomp=True):
217 """Extract an entry from an image
218
219 This extracts the data from a particular entry in an image
220
221 Args:
222 image_fname: Image filename to process
223 entry_path: Path to entry to extract
224 decomp: True to return uncompressed data, if the data is compress
225 False to return the raw data
226
227 Returns:
228 data extracted from the entry
229 """
Simon Glassb9ba4e02019-08-24 07:22:44 -0600230 global Image
Simon Glass90cd6f02020-08-05 13:27:47 -0600231 from binman.image import Image
Simon Glassb9ba4e02019-08-24 07:22:44 -0600232
Simon Glass4c613bf2019-07-08 14:25:50 -0600233 image = Image.FromFile(image_fname)
Stefan Herbrechtsmeier47250302022-08-19 16:25:22 +0200234 image.CollectBintools()
Simon Glass4c613bf2019-07-08 14:25:50 -0600235 entry = image.FindEntryPath(entry_path)
236 return entry.ReadData(decomp)
237
238
Simon Glass637958f2021-11-23 21:09:50 -0700239def ShowAltFormats(image):
240 """Show alternative formats available for entries in the image
241
242 This shows a list of formats available.
243
244 Args:
245 image (Image): Image to check
246 """
247 alt_formats = {}
248 image.CheckAltFormats(alt_formats)
249 print('%-10s %-20s %s' % ('Flag (-F)', 'Entry type', 'Description'))
250 for name, val in alt_formats.items():
251 entry, helptext = val
252 print('%-10s %-20s %s' % (name, entry.etype, helptext))
253
254
Simon Glass980a2842019-07-08 14:25:52 -0600255def ExtractEntries(image_fname, output_fname, outdir, entry_paths,
Simon Glass637958f2021-11-23 21:09:50 -0700256 decomp=True, alt_format=None):
Simon Glass980a2842019-07-08 14:25:52 -0600257 """Extract the data from one or more entries and write it to files
258
259 Args:
260 image_fname: Image filename to process
261 output_fname: Single output filename to use if extracting one file, None
262 otherwise
263 outdir: Output directory to use (for any number of files), else None
264 entry_paths: List of entry paths to extract
Simon Glassd48f94e2019-07-20 12:24:12 -0600265 decomp: True to decompress the entry data
Simon Glass980a2842019-07-08 14:25:52 -0600266
267 Returns:
268 List of EntryInfo records that were written
269 """
270 image = Image.FromFile(image_fname)
Stefan Herbrechtsmeier47250302022-08-19 16:25:22 +0200271 image.CollectBintools()
Simon Glass980a2842019-07-08 14:25:52 -0600272
Simon Glass637958f2021-11-23 21:09:50 -0700273 if alt_format == 'list':
274 ShowAltFormats(image)
275 return
276
Simon Glass980a2842019-07-08 14:25:52 -0600277 # Output an entry to a single file, as a special case
278 if output_fname:
279 if not entry_paths:
Simon Glassa772d3f2019-07-20 12:24:14 -0600280 raise ValueError('Must specify an entry path to write with -f')
Simon Glass980a2842019-07-08 14:25:52 -0600281 if len(entry_paths) != 1:
Simon Glassa772d3f2019-07-20 12:24:14 -0600282 raise ValueError('Must specify exactly one entry path to write with -f')
Simon Glass980a2842019-07-08 14:25:52 -0600283 entry = image.FindEntryPath(entry_paths[0])
Simon Glass637958f2021-11-23 21:09:50 -0700284 data = entry.ReadData(decomp, alt_format)
Simon Glass80025522022-01-29 14:14:04 -0700285 tools.write_file(output_fname, data)
Simon Glass011f1b32022-01-29 14:14:15 -0700286 tout.notice("Wrote %#x bytes to file '%s'" % (len(data), output_fname))
Simon Glass980a2842019-07-08 14:25:52 -0600287 return
288
289 # Otherwise we will output to a path given by the entry path of each entry.
290 # This means that entries will appear in subdirectories if they are part of
291 # a sub-section.
292 einfos = image.GetListEntries(entry_paths)[0]
Simon Glass011f1b32022-01-29 14:14:15 -0700293 tout.notice('%d entries match and will be written' % len(einfos))
Simon Glass980a2842019-07-08 14:25:52 -0600294 for einfo in einfos:
295 entry = einfo.entry
Simon Glass637958f2021-11-23 21:09:50 -0700296 data = entry.ReadData(decomp, alt_format)
Simon Glass980a2842019-07-08 14:25:52 -0600297 path = entry.GetPath()[1:]
298 fname = os.path.join(outdir, path)
299
300 # If this entry has children, create a directory for it and put its
301 # data in a file called 'root' in that directory
302 if entry.GetEntries():
Simon Glass4ef93d92021-03-18 20:24:51 +1300303 if fname and not os.path.exists(fname):
Simon Glass980a2842019-07-08 14:25:52 -0600304 os.makedirs(fname)
305 fname = os.path.join(fname, 'root')
Simon Glass011f1b32022-01-29 14:14:15 -0700306 tout.notice("Write entry '%s' size %x to '%s'" %
Simon Glass08349452021-01-06 21:35:13 -0700307 (entry.GetPath(), len(data), fname))
Simon Glass80025522022-01-29 14:14:04 -0700308 tools.write_file(fname, data)
Simon Glass980a2842019-07-08 14:25:52 -0600309 return einfos
310
311
Simon Glass274bd0e2019-07-20 12:24:13 -0600312def BeforeReplace(image, allow_resize):
313 """Handle getting an image ready for replacing entries in it
314
315 Args:
316 image: Image to prepare
317 """
318 state.PrepareFromLoadedData(image)
Alper Nebi Yasake63ca5a2022-03-27 18:31:45 +0300319 image.CollectBintools()
Lukas Funke6767d8f2023-07-18 13:53:10 +0200320 image.LoadData(decomp=False)
Simon Glass274bd0e2019-07-20 12:24:13 -0600321
322 # If repacking, drop the old offset/size values except for the original
323 # ones, so we are only left with the constraints.
Alper Nebi Yasak00c68f12022-03-27 18:31:46 +0300324 if image.allow_repack and allow_resize:
Simon Glass274bd0e2019-07-20 12:24:13 -0600325 image.ResetForPack()
326
327
328def ReplaceOneEntry(image, entry, data, do_compress, allow_resize):
329 """Handle replacing a single entry an an image
330
331 Args:
332 image: Image to update
333 entry: Entry to write
334 data: Data to replace with
335 do_compress: True to compress the data if needed, False if data is
336 already compressed so should be used as is
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 """
340 if not entry.WriteData(data, do_compress):
341 if not image.allow_repack:
342 entry.Raise('Entry data size does not match, but allow-repack is not present for this image')
343 if not allow_resize:
344 entry.Raise('Entry data size does not match, but resize is disabled')
345
346
347def AfterReplace(image, allow_resize, write_map):
348 """Handle write out an image after replacing entries in it
349
350 Args:
351 image: Image to write
352 allow_resize: True to allow entries to change size (this does a re-pack
353 of the entries), False to raise an exception
354 write_map: True to write a map file
355 """
Simon Glass011f1b32022-01-29 14:14:15 -0700356 tout.info('Processing image')
Simon Glass274bd0e2019-07-20 12:24:13 -0600357 ProcessImage(image, update_fdt=True, write_map=write_map,
358 get_contents=False, allow_resize=allow_resize)
359
360
361def WriteEntryToImage(image, entry, data, do_compress=True, allow_resize=True,
362 write_map=False):
363 BeforeReplace(image, allow_resize)
Simon Glass011f1b32022-01-29 14:14:15 -0700364 tout.info('Writing data to %s' % entry.GetPath())
Simon Glass274bd0e2019-07-20 12:24:13 -0600365 ReplaceOneEntry(image, entry, data, do_compress, allow_resize)
366 AfterReplace(image, allow_resize=allow_resize, write_map=write_map)
367
368
Simon Glassd48f94e2019-07-20 12:24:12 -0600369def WriteEntry(image_fname, entry_path, data, do_compress=True,
370 allow_resize=True, write_map=False):
Simon Glass3971c952019-07-20 12:24:11 -0600371 """Replace an entry in an image
372
373 This replaces the data in a particular entry in an image. This size of the
374 new data must match the size of the old data unless allow_resize is True.
375
376 Args:
377 image_fname: Image filename to process
378 entry_path: Path to entry to extract
379 data: Data to replace with
Simon Glassd48f94e2019-07-20 12:24:12 -0600380 do_compress: True to compress the data if needed, False if data is
Simon Glass3971c952019-07-20 12:24:11 -0600381 already compressed so should be used as is
382 allow_resize: True to allow entries to change size (this does a re-pack
383 of the entries), False to raise an exception
Simon Glassd48f94e2019-07-20 12:24:12 -0600384 write_map: True to write a map file
Simon Glass3971c952019-07-20 12:24:11 -0600385
386 Returns:
387 Image object that was updated
388 """
Simon Glass011f1b32022-01-29 14:14:15 -0700389 tout.info("Write entry '%s', file '%s'" % (entry_path, image_fname))
Simon Glass3971c952019-07-20 12:24:11 -0600390 image = Image.FromFile(image_fname)
Stefan Herbrechtsmeier47250302022-08-19 16:25:22 +0200391 image.CollectBintools()
Simon Glass3971c952019-07-20 12:24:11 -0600392 entry = image.FindEntryPath(entry_path)
Simon Glass274bd0e2019-07-20 12:24:13 -0600393 WriteEntryToImage(image, entry, data, do_compress=do_compress,
394 allow_resize=allow_resize, write_map=write_map)
Simon Glass3971c952019-07-20 12:24:11 -0600395
Simon Glass3971c952019-07-20 12:24:11 -0600396 return image
397
Simon Glass30033c22019-07-20 12:24:15 -0600398
399def ReplaceEntries(image_fname, input_fname, indir, entry_paths,
400 do_compress=True, allow_resize=True, write_map=False):
401 """Replace the data from one or more entries from input files
402
403 Args:
404 image_fname: Image filename to process
Jan Kiszka8ea44432021-11-11 08:13:30 +0100405 input_fname: Single input filename to use if replacing one file, None
Simon Glass30033c22019-07-20 12:24:15 -0600406 otherwise
407 indir: Input directory to use (for any number of files), else None
Jan Kiszka8ea44432021-11-11 08:13:30 +0100408 entry_paths: List of entry paths to replace
Simon Glass30033c22019-07-20 12:24:15 -0600409 do_compress: True if the input data is uncompressed and may need to be
410 compressed if the entry requires it, False if the data is already
411 compressed.
412 write_map: True to write a map file
413
414 Returns:
415 List of EntryInfo records that were written
416 """
Jan Kiszkaafc8f292021-11-11 08:14:18 +0100417 image_fname = os.path.abspath(image_fname)
Simon Glass30033c22019-07-20 12:24:15 -0600418 image = Image.FromFile(image_fname)
419
Simon Glass49b77e82023-03-02 17:02:44 -0700420 image.mark_build_done()
421
Simon Glass30033c22019-07-20 12:24:15 -0600422 # Replace an entry from a single file, as a special case
423 if input_fname:
424 if not entry_paths:
425 raise ValueError('Must specify an entry path to read with -f')
426 if len(entry_paths) != 1:
427 raise ValueError('Must specify exactly one entry path to write with -f')
428 entry = image.FindEntryPath(entry_paths[0])
Simon Glass80025522022-01-29 14:14:04 -0700429 data = tools.read_file(input_fname)
Simon Glass011f1b32022-01-29 14:14:15 -0700430 tout.notice("Read %#x bytes from file '%s'" % (len(data), input_fname))
Simon Glass30033c22019-07-20 12:24:15 -0600431 WriteEntryToImage(image, entry, data, do_compress=do_compress,
432 allow_resize=allow_resize, write_map=write_map)
433 return
434
435 # Otherwise we will input from a path given by the entry path of each entry.
436 # This means that files must appear in subdirectories if they are part of
437 # a sub-section.
438 einfos = image.GetListEntries(entry_paths)[0]
Simon Glass011f1b32022-01-29 14:14:15 -0700439 tout.notice("Replacing %d matching entries in image '%s'" %
Simon Glass30033c22019-07-20 12:24:15 -0600440 (len(einfos), image_fname))
441
442 BeforeReplace(image, allow_resize)
443
444 for einfo in einfos:
445 entry = einfo.entry
446 if entry.GetEntries():
Simon Glass011f1b32022-01-29 14:14:15 -0700447 tout.info("Skipping section entry '%s'" % entry.GetPath())
Simon Glass30033c22019-07-20 12:24:15 -0600448 continue
449
450 path = entry.GetPath()[1:]
451 fname = os.path.join(indir, path)
452
453 if os.path.exists(fname):
Simon Glass011f1b32022-01-29 14:14:15 -0700454 tout.notice("Write entry '%s' from file '%s'" %
Simon Glass30033c22019-07-20 12:24:15 -0600455 (entry.GetPath(), fname))
Simon Glass80025522022-01-29 14:14:04 -0700456 data = tools.read_file(fname)
Simon Glass30033c22019-07-20 12:24:15 -0600457 ReplaceOneEntry(image, entry, data, do_compress, allow_resize)
458 else:
Simon Glass011f1b32022-01-29 14:14:15 -0700459 tout.warning("Skipping entry '%s' from missing file '%s'" %
Simon Glass30033c22019-07-20 12:24:15 -0600460 (entry.GetPath(), fname))
461
462 AfterReplace(image, allow_resize=allow_resize, write_map=write_map)
463 return image
464
Ivan Mikhaylov8a803862023-03-08 01:13:39 +0000465def SignEntries(image_fname, input_fname, privatekey_fname, algo, entry_paths,
466 write_map=False):
467 """Sign and replace the data from one or more entries from input files
468
469 Args:
470 image_fname: Image filename to process
471 input_fname: Single input filename to use if replacing one file, None
472 otherwise
473 algo: Hashing algorithm
474 entry_paths: List of entry paths to sign
475 privatekey_fname: Private key filename
476 write_map (bool): True to write the map file
477 """
478 image_fname = os.path.abspath(image_fname)
479 image = Image.FromFile(image_fname)
480
Ivan Mikhaylov3cfcaa4d2023-03-08 01:13:40 +0000481 image.mark_build_done()
482
Ivan Mikhaylov8a803862023-03-08 01:13:39 +0000483 BeforeReplace(image, allow_resize=True)
484
485 for entry_path in entry_paths:
486 entry = image.FindEntryPath(entry_path)
487 entry.UpdateSignatures(privatekey_fname, algo, input_fname)
488
489 AfterReplace(image, allow_resize=True, write_map=write_map)
Simon Glass30033c22019-07-20 12:24:15 -0600490
Simon Glassfc792842023-07-18 07:24:04 -0600491def _ProcessTemplates(parent):
492 """Handle any templates in the binman description
493
494 Args:
495 parent: Binman node to process (typically /binman)
496
Simon Glass09490b02023-07-22 21:43:52 -0600497 Returns:
498 bool: True if any templates were processed
499
Simon Glassfc792842023-07-18 07:24:04 -0600500 Search though each target node looking for those with an 'insert-template'
501 property. Use that as a list of references to template nodes to use to
502 adjust the target node.
503
504 Processing involves copying each subnode of the template node into the
505 target node.
506
Simon Glassaa6e0552023-07-18 07:24:07 -0600507 This is done recursively, so templates can be at any level of the binman
508 image, e.g. inside a section.
Simon Glassfc792842023-07-18 07:24:04 -0600509
510 See 'Templates' in the Binman documnentation for details.
511 """
Simon Glass09490b02023-07-22 21:43:52 -0600512 found = False
Simon Glassfc792842023-07-18 07:24:04 -0600513 for node in parent.subnodes:
514 tmpl = fdt_util.GetPhandleList(node, 'insert-template')
515 if tmpl:
516 node.copy_subnodes_from_phandles(tmpl)
Simon Glass09490b02023-07-22 21:43:52 -0600517 found = True
518
519 found |= _ProcessTemplates(node)
520 return found
Simon Glassfc792842023-07-18 07:24:04 -0600521
Simon Glass55ab0b62021-03-18 20:25:06 +1300522def PrepareImagesAndDtbs(dtb_fname, select_images, update_fdt, use_expanded):
Simon Glassd3151ff2019-07-20 12:23:27 -0600523 """Prepare the images to be processed and select the device tree
524
525 This function:
526 - reads in the device tree
527 - finds and scans the binman node to create all entries
528 - selects which images to build
529 - Updates the device tress with placeholder properties for offset,
530 image-pos, etc.
531
532 Args:
533 dtb_fname: Filename of the device tree file to use (.dts or .dtb)
534 selected_images: List of images to output, or None for all
535 update_fdt: True to update the FDT wth entry offsets, etc.
Simon Glass55ab0b62021-03-18 20:25:06 +1300536 use_expanded: True to use expanded versions of entries, if available.
537 So if 'u-boot' is called for, we use 'u-boot-expanded' instead. This
538 is needed if update_fdt is True (although tests may disable it)
Simon Glass31ee50f2020-09-01 05:13:55 -0600539
540 Returns:
541 OrderedDict of images:
542 key: Image name (str)
543 value: Image object
Simon Glassd3151ff2019-07-20 12:23:27 -0600544 """
545 # Import these here in case libfdt.py is not available, in which case
546 # the above help option still works.
Simon Glassc585dd42020-04-17 18:09:03 -0600547 from dtoc import fdt
548 from dtoc import fdt_util
Simon Glassd3151ff2019-07-20 12:23:27 -0600549 global images
550
551 # Get the device tree ready by compiling it and copying the compiled
552 # output into a file in our output directly. Then scan it for use
553 # in binman.
554 dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
Simon Glass80025522022-01-29 14:14:04 -0700555 fname = tools.get_output_filename('u-boot.dtb.out')
556 tools.write_file(fname, tools.read_file(dtb_fname))
Simon Glassd3151ff2019-07-20 12:23:27 -0600557 dtb = fdt.FdtScan(fname)
558
559 node = _FindBinmanNode(dtb)
560 if not node:
561 raise ValueError("Device tree '%s' does not have a 'binman' "
562 "node" % dtb_fname)
563
Simon Glass09490b02023-07-22 21:43:52 -0600564 if _ProcessTemplates(node):
565 dtb.Sync(True)
566 fname = tools.get_output_filename('u-boot.dtb.tmpl1')
567 tools.write_file(fname, dtb.GetContents())
Simon Glassfc792842023-07-18 07:24:04 -0600568
Simon Glass55ab0b62021-03-18 20:25:06 +1300569 images = _ReadImageDesc(node, use_expanded)
Simon Glassd3151ff2019-07-20 12:23:27 -0600570
571 if select_images:
572 skip = []
573 new_images = OrderedDict()
574 for name, image in images.items():
575 if name in select_images:
576 new_images[name] = image
577 else:
578 skip.append(name)
579 images = new_images
Simon Glass011f1b32022-01-29 14:14:15 -0700580 tout.notice('Skipping images: %s' % ', '.join(skip))
Simon Glassd3151ff2019-07-20 12:23:27 -0600581
582 state.Prepare(images, dtb)
583
584 # Prepare the device tree by making sure that any missing
585 # properties are added (e.g. 'pos' and 'size'). The values of these
586 # may not be correct yet, but we add placeholders so that the
587 # size of the device tree is correct. Later, in
588 # SetCalculatedProperties() we will insert the correct values
589 # without changing the device-tree size, thus ensuring that our
590 # entry offsets remain the same.
591 for image in images.values():
Simon Glassf86ddad2022-03-05 20:19:00 -0700592 image.gen_entries()
Stefan Herbrechtsmeier47250302022-08-19 16:25:22 +0200593 image.CollectBintools()
Simon Glassd3151ff2019-07-20 12:23:27 -0600594 if update_fdt:
Simon Glassacd6c6e2020-10-26 17:40:17 -0600595 image.AddMissingProperties(True)
Simon Glassd3151ff2019-07-20 12:23:27 -0600596 image.ProcessFdt(dtb)
597
Simon Glass5a300602019-07-20 12:23:29 -0600598 for dtb_item in state.GetAllFdts():
Simon Glassd3151ff2019-07-20 12:23:27 -0600599 dtb_item.Sync(auto_resize=True)
600 dtb_item.Pack()
601 dtb_item.Flush()
602 return images
603
604
Simon Glassf8a54bc2019-07-20 12:23:56 -0600605def ProcessImage(image, update_fdt, write_map, get_contents=True,
Heiko Thiery6d451362022-01-06 11:49:41 +0100606 allow_resize=True, allow_missing=False,
607 allow_fake_blobs=False):
Simon Glassb766c5e52019-07-20 12:23:24 -0600608 """Perform all steps for this image, including checking and # writing it.
609
610 This means that errors found with a later image will be reported after
611 earlier images are already completed and written, but that does not seem
612 important.
613
614 Args:
615 image: Image to process
616 update_fdt: True to update the FDT wth entry offsets, etc.
617 write_map: True to write a map file
Simon Glass072959a2019-07-20 12:23:50 -0600618 get_contents: True to get the image contents from files, etc., False if
619 the contents is already present
Simon Glassf8a54bc2019-07-20 12:23:56 -0600620 allow_resize: True to allow entries to change size (this does a re-pack
621 of the entries), False to raise an exception
Simon Glass5d94cc62020-07-09 18:39:38 -0600622 allow_missing: Allow blob_ext objects to be missing
Heiko Thiery6d451362022-01-06 11:49:41 +0100623 allow_fake_blobs: Allow blob_ext objects to be faked with dummy files
Simon Glassa003cd32020-07-09 18:39:40 -0600624
625 Returns:
Heiko Thiery6d451362022-01-06 11:49:41 +0100626 True if one or more external blobs are missing or faked,
627 False if all are present
Simon Glassb766c5e52019-07-20 12:23:24 -0600628 """
Simon Glass072959a2019-07-20 12:23:50 -0600629 if get_contents:
Simon Glass5d94cc62020-07-09 18:39:38 -0600630 image.SetAllowMissing(allow_missing)
Heiko Thiery6d451362022-01-06 11:49:41 +0100631 image.SetAllowFakeBlob(allow_fake_blobs)
Simon Glass072959a2019-07-20 12:23:50 -0600632 image.GetEntryContents()
Simon Glass1e9e61c2023-01-07 14:07:12 -0700633 image.drop_absent()
Simon Glassb766c5e52019-07-20 12:23:24 -0600634 image.GetEntryOffsets()
635
636 # We need to pack the entries to figure out where everything
637 # should be placed. This sets the offset/size of each entry.
638 # However, after packing we call ProcessEntryContents() which
639 # may result in an entry changing size. In that case we need to
640 # do another pass. Since the device tree often contains the
641 # final offset/size information we try to make space for this in
642 # AddMissingProperties() above. However, if the device is
643 # compressed we cannot know this compressed size in advance,
644 # since changing an offset from 0x100 to 0x104 (for example) can
645 # alter the compressed size of the device tree. So we need a
646 # third pass for this.
Simon Glass37fdd142019-07-20 12:24:06 -0600647 passes = 5
Simon Glassb766c5e52019-07-20 12:23:24 -0600648 for pack_pass in range(passes):
649 try:
650 image.PackEntries()
Simon Glassb766c5e52019-07-20 12:23:24 -0600651 except Exception as e:
652 if write_map:
653 fname = image.WriteMap()
654 print("Wrote map file '%s' to show errors" % fname)
655 raise
656 image.SetImagePos()
657 if update_fdt:
658 image.SetCalculatedProperties()
Simon Glass5a300602019-07-20 12:23:29 -0600659 for dtb_item in state.GetAllFdts():
Simon Glassb766c5e52019-07-20 12:23:24 -0600660 dtb_item.Sync()
Simon Glassf8a54bc2019-07-20 12:23:56 -0600661 dtb_item.Flush()
Simon Glasse5943412019-08-24 07:23:12 -0600662 image.WriteSymbols()
Simon Glassb766c5e52019-07-20 12:23:24 -0600663 sizes_ok = image.ProcessEntryContents()
664 if sizes_ok:
665 break
666 image.ResetForPack()
Simon Glass011f1b32022-01-29 14:14:15 -0700667 tout.info('Pack completed after %d pass(es)' % (pack_pass + 1))
Simon Glassb766c5e52019-07-20 12:23:24 -0600668 if not sizes_ok:
Simon Glass9d8ee322019-07-20 12:23:58 -0600669 image.Raise('Entries changed size after packing (tried %s passes)' %
Simon Glassb766c5e52019-07-20 12:23:24 -0600670 passes)
671
Simon Glassb766c5e52019-07-20 12:23:24 -0600672 image.BuildImage()
673 if write_map:
674 image.WriteMap()
Simon Glass63328f12023-01-07 14:07:15 -0700675
Simon Glassa003cd32020-07-09 18:39:40 -0600676 missing_list = []
677 image.CheckMissing(missing_list)
678 if missing_list:
Jonas Karlmana0335b42023-07-18 20:34:37 +0000679 tout.error("Image '%s' is missing external blobs and is non-functional: %s\n" %
Jonas Karlman34f520c2023-07-18 20:34:35 +0000680 (image.name, ' '.join([e.name for e in missing_list])))
681 _ShowHelpForMissingBlobs(tout.ERROR, missing_list)
Simon Glass63328f12023-01-07 14:07:15 -0700682
Heiko Thiery6d451362022-01-06 11:49:41 +0100683 faked_list = []
684 image.CheckFakedBlobs(faked_list)
685 if faked_list:
Simon Glass011f1b32022-01-29 14:14:15 -0700686 tout.warning(
Jonas Karlmana0335b42023-07-18 20:34:37 +0000687 "Image '%s' has faked external blobs and is non-functional: %s\n" %
Simon Glassf9f34032022-01-09 20:13:45 -0700688 (image.name, ' '.join([os.path.basename(e.GetDefaultFilename())
689 for e in faked_list])))
Simon Glass63328f12023-01-07 14:07:15 -0700690
691 optional_list = []
692 image.CheckOptional(optional_list)
693 if optional_list:
694 tout.warning(
Jonas Karlmana0335b42023-07-18 20:34:37 +0000695 "Image '%s' is missing optional external blobs but is still functional: %s\n" %
Simon Glass63328f12023-01-07 14:07:15 -0700696 (image.name, ' '.join([e.name for e in optional_list])))
Jonas Karlman34f520c2023-07-18 20:34:35 +0000697 _ShowHelpForMissingBlobs(tout.WARNING, optional_list)
Simon Glass63328f12023-01-07 14:07:15 -0700698
Simon Glass66152ce2022-01-09 20:14:09 -0700699 missing_bintool_list = []
700 image.check_missing_bintools(missing_bintool_list)
701 if missing_bintool_list:
Simon Glass011f1b32022-01-29 14:14:15 -0700702 tout.warning(
Jonas Karlmana0335b42023-07-18 20:34:37 +0000703 "Image '%s' has missing bintools and is non-functional: %s\n" %
Simon Glass66152ce2022-01-09 20:14:09 -0700704 (image.name, ' '.join([os.path.basename(bintool.name)
705 for bintool in missing_bintool_list])))
706 return any([missing_list, faked_list, missing_bintool_list])
Simon Glassb766c5e52019-07-20 12:23:24 -0600707
708
Simon Glassf46732a2019-07-08 14:25:29 -0600709def Binman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700710 """The main control code for binman
711
712 This assumes that help and test options have already been dealt with. It
713 deals with the core task of building images.
714
715 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600716 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700717 """
Simon Glassb9ba4e02019-08-24 07:22:44 -0600718 global Image
719 global state
720
Simon Glassf46732a2019-07-08 14:25:29 -0600721 if args.full_help:
Simon Glassfeb80cc2023-02-23 18:18:20 -0700722 with importlib.resources.path('binman', 'README.rst') as readme:
723 tools.print_full_help(str(readme))
Simon Glass2574ef62016-11-25 20:15:51 -0700724 return 0
725
Simon Glassb9ba4e02019-08-24 07:22:44 -0600726 # Put these here so that we can import this module without libfdt
Simon Glass90cd6f02020-08-05 13:27:47 -0600727 from binman.image import Image
Simon Glassc585dd42020-04-17 18:09:03 -0600728 from binman import state
Simon Glassb9ba4e02019-08-24 07:22:44 -0600729
Simon Glass9a1c7262023-02-22 12:14:49 -0700730 tool_paths = []
731 if args.toolpath:
732 tool_paths += args.toolpath
733 if args.tooldir:
734 tool_paths.append(args.tooldir)
735 tools.set_tool_paths(tool_paths or None)
736 bintool.Bintool.set_tool_dir(args.tooldir)
737
Ivan Mikhaylov8a803862023-03-08 01:13:39 +0000738 if args.cmd in ['ls', 'extract', 'replace', 'tool', 'sign']:
Simon Glass9b7f5002019-07-20 12:23:53 -0600739 try:
Simon Glass011f1b32022-01-29 14:14:15 -0700740 tout.init(args.verbosity)
Simon Glassb9b9b272023-03-02 17:02:42 -0700741 if args.cmd == 'replace':
742 tools.prepare_output_dir(args.outdir, args.preserve)
743 else:
744 tools.prepare_output_dir(None)
Simon Glassdf08cbb2019-09-15 18:10:36 -0600745 if args.cmd == 'ls':
746 ListEntries(args.image, args.paths)
Simon Glassb2fd11d2019-07-08 14:25:48 -0600747
Simon Glassdf08cbb2019-09-15 18:10:36 -0600748 if args.cmd == 'extract':
749 ExtractEntries(args.image, args.filename, args.outdir, args.paths,
Simon Glass637958f2021-11-23 21:09:50 -0700750 not args.uncompressed, args.format)
Simon Glass980a2842019-07-08 14:25:52 -0600751
Simon Glassdf08cbb2019-09-15 18:10:36 -0600752 if args.cmd == 'replace':
753 ReplaceEntries(args.image, args.filename, args.indir, args.paths,
754 do_compress=not args.compressed,
755 allow_resize=not args.fix_size, write_map=args.map)
Simon Glass4eae9252022-01-09 20:13:50 -0700756
Ivan Mikhaylov8a803862023-03-08 01:13:39 +0000757 if args.cmd == 'sign':
758 SignEntries(args.image, args.file, args.key, args.algo, args.paths)
759
Simon Glass4eae9252022-01-09 20:13:50 -0700760 if args.cmd == 'tool':
Simon Glass4eae9252022-01-09 20:13:50 -0700761 if args.list:
762 bintool.Bintool.list_all()
763 elif args.fetch:
764 if not args.bintools:
765 raise ValueError(
766 "Please specify bintools to fetch or 'all' or 'missing'")
767 bintool.Bintool.fetch_tools(bintool.FETCH_ANY,
768 args.bintools)
769 else:
770 raise ValueError("Invalid arguments to 'tool' subcommand")
Simon Glassdf08cbb2019-09-15 18:10:36 -0600771 except:
772 raise
Simon Glass30033c22019-07-20 12:24:15 -0600773 finally:
Simon Glass80025522022-01-29 14:14:04 -0700774 tools.finalise_output_dir()
Simon Glass30033c22019-07-20 12:24:15 -0600775 return 0
776
Simon Glassadfb8492021-11-03 21:09:18 -0600777 elf_params = None
778 if args.update_fdt_in_elf:
779 elf_params = args.update_fdt_in_elf.split(',')
780 if len(elf_params) != 4:
781 raise ValueError('Invalid args %s to --update-fdt-in-elf: expected infile,outfile,begin_sym,end_sym' %
782 elf_params)
783
Simon Glass2574ef62016-11-25 20:15:51 -0700784 # Try to figure out which device tree contains our image description
Simon Glassf46732a2019-07-08 14:25:29 -0600785 if args.dt:
786 dtb_fname = args.dt
Simon Glass2574ef62016-11-25 20:15:51 -0700787 else:
Simon Glassf46732a2019-07-08 14:25:29 -0600788 board = args.board
Simon Glass2574ef62016-11-25 20:15:51 -0700789 if not board:
790 raise ValueError('Must provide a board to process (use -b <board>)')
Simon Glassf46732a2019-07-08 14:25:29 -0600791 board_pathname = os.path.join(args.build_dir, board)
Simon Glass2574ef62016-11-25 20:15:51 -0700792 dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
Simon Glassf46732a2019-07-08 14:25:29 -0600793 if not args.indir:
794 args.indir = ['.']
795 args.indir.append(board_pathname)
Simon Glass2574ef62016-11-25 20:15:51 -0700796
797 try:
Simon Glass011f1b32022-01-29 14:14:15 -0700798 tout.init(args.verbosity)
Simon Glassf46732a2019-07-08 14:25:29 -0600799 elf.debug = args.debug
800 cbfs_util.VERBOSE = args.verbosity > 2
801 state.use_fake_dtb = args.fake_dtb
Simon Glass55ab0b62021-03-18 20:25:06 +1300802
803 # Normally we replace the 'u-boot' etype with 'u-boot-expanded', etc.
804 # When running tests this can be disabled using this flag. When not
805 # updating the FDT in image, it is not needed by binman, but we use it
806 # for consistency, so that the images look the same to U-Boot at
807 # runtime.
808 use_expanded = not args.no_expanded
Simon Glass2574ef62016-11-25 20:15:51 -0700809 try:
Simon Glass80025522022-01-29 14:14:04 -0700810 tools.set_input_dirs(args.indir)
811 tools.prepare_output_dir(args.outdir, args.preserve)
Simon Glassf46732a2019-07-08 14:25:29 -0600812 state.SetEntryArgs(args.entry_arg)
Simon Glass76f496d2021-07-06 10:36:37 -0600813 state.SetThreads(args.threads)
Simon Glass92307732018-07-06 10:27:40 -0600814
Simon Glassd3151ff2019-07-20 12:23:27 -0600815 images = PrepareImagesAndDtbs(dtb_fname, args.image,
Simon Glass55ab0b62021-03-18 20:25:06 +1300816 args.update_fdt, use_expanded)
Heiko Thiery6d451362022-01-06 11:49:41 +0100817
Simon Glass76f496d2021-07-06 10:36:37 -0600818 if args.test_section_timeout:
819 # Set the first image to timeout, used in testThreadTimeout()
820 images[list(images.keys())[0]].test_section_timeout = True
Heiko Thiery6d451362022-01-06 11:49:41 +0100821 invalid = False
Simon Glass66152ce2022-01-09 20:14:09 -0700822 bintool.Bintool.set_missing_list(
823 args.force_missing_bintools.split(',') if
824 args.force_missing_bintools else None)
Simon Glass7d3e4072022-08-07 09:46:46 -0600825
826 # Create the directory here instead of Entry.check_fake_fname()
827 # since that is called from a threaded context so different threads
828 # may race to create the directory
829 if args.fake_ext_blobs:
830 entry.Entry.create_fake_dir()
831
Simon Glass2574ef62016-11-25 20:15:51 -0700832 for image in images.values():
Heiko Thiery6d451362022-01-06 11:49:41 +0100833 invalid |= ProcessImage(image, args.update_fdt, args.map,
834 allow_missing=args.allow_missing,
835 allow_fake_blobs=args.fake_ext_blobs)
Simon Glassbdb40312018-09-14 04:57:20 -0600836
837 # Write the updated FDTs to our output files
Simon Glass5a300602019-07-20 12:23:29 -0600838 for dtb_item in state.GetAllFdts():
Simon Glass80025522022-01-29 14:14:04 -0700839 tools.write_file(dtb_item._fname, dtb_item.GetContents())
Simon Glassbdb40312018-09-14 04:57:20 -0600840
Simon Glassadfb8492021-11-03 21:09:18 -0600841 if elf_params:
842 data = state.GetFdtForEtype('u-boot-dtb').GetContents()
843 elf.UpdateFile(*elf_params, data)
844
Simon Glass6bce5dc2022-11-09 19:14:42 -0700845 # This can only be True if -M is provided, since otherwise binman
846 # would have raised an error already
Heiko Thiery6d451362022-01-06 11:49:41 +0100847 if invalid:
Jonas Karlmana0335b42023-07-18 20:34:37 +0000848 msg = 'Some images are invalid'
Simon Glass6bce5dc2022-11-09 19:14:42 -0700849 if args.ignore_missing:
850 tout.warning(msg)
851 else:
852 tout.error(msg)
853 return 103
Simon Glass748a1d42021-07-06 10:36:41 -0600854
855 # Use this to debug the time take to pack the image
856 #state.TimingShow()
Simon Glass2574ef62016-11-25 20:15:51 -0700857 finally:
Simon Glass80025522022-01-29 14:14:04 -0700858 tools.finalise_output_dir()
Simon Glass2574ef62016-11-25 20:15:51 -0700859 finally:
Simon Glass011f1b32022-01-29 14:14:15 -0700860 tout.uninit()
Simon Glass2574ef62016-11-25 20:15:51 -0700861
862 return 0