blob: 7da69ba38de477bf077a396e55ba80ab84613411 [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
Simon Glass2574ef62016-11-25 20:15:51 -070010import os
Simon Glassc1dc2f82020-08-29 11:36:14 -060011import pkg_resources
Simon Glassa820af72020-09-06 10:39:09 -060012import re
Simon Glassc1dc2f82020-08-29 11:36:14 -060013
Simon Glass2574ef62016-11-25 20:15:51 -070014import sys
Simon Glassa997ea52020-04-17 18:09:04 -060015from patman import tools
Simon Glass2574ef62016-11-25 20:15:51 -070016
Simon Glassc585dd42020-04-17 18:09:03 -060017from binman import cbfs_util
18from binman import elf
Simon Glassa997ea52020-04-17 18:09:04 -060019from patman import command
20from patman import tout
Simon Glass2574ef62016-11-25 20:15:51 -070021
22# List of images we plan to create
23# Make this global so that it can be referenced from tests
24images = OrderedDict()
25
Simon Glassa820af72020-09-06 10:39:09 -060026# Help text for each type of missing blob, dict:
27# key: Value of the entry's 'missing-msg' or entry name
28# value: Text for the help
29missing_blob_help = {}
30
Simon Glass55ab0b62021-03-18 20:25:06 +130031def _ReadImageDesc(binman_node, use_expanded):
Simon Glass2574ef62016-11-25 20:15:51 -070032 """Read the image descriptions from the /binman node
33
34 This normally produces a single Image object called 'image'. But if
35 multiple images are present, they will all be returned.
36
37 Args:
38 binman_node: Node object of the /binman node
Simon Glass55ab0b62021-03-18 20:25:06 +130039 use_expanded: True if the FDT will be updated with the entry information
Simon Glass2574ef62016-11-25 20:15:51 -070040 Returns:
41 OrderedDict of Image objects, each of which describes an image
42 """
43 images = OrderedDict()
44 if 'multiple-images' in binman_node.props:
45 for node in binman_node.subnodes:
Simon Glass55ab0b62021-03-18 20:25:06 +130046 images[node.name] = Image(node.name, node,
47 use_expanded=use_expanded)
Simon Glass2574ef62016-11-25 20:15:51 -070048 else:
Simon Glass55ab0b62021-03-18 20:25:06 +130049 images['image'] = Image('image', binman_node, use_expanded=use_expanded)
Simon Glass2574ef62016-11-25 20:15:51 -070050 return images
51
Simon Glass22c92ca2017-05-27 07:38:29 -060052def _FindBinmanNode(dtb):
Simon Glass2574ef62016-11-25 20:15:51 -070053 """Find the 'binman' node in the device tree
54
55 Args:
Simon Glass22c92ca2017-05-27 07:38:29 -060056 dtb: Fdt object to scan
Simon Glass2574ef62016-11-25 20:15:51 -070057 Returns:
58 Node object of /binman node, or None if not found
59 """
Simon Glass22c92ca2017-05-27 07:38:29 -060060 for node in dtb.GetRoot().subnodes:
Simon Glass2574ef62016-11-25 20:15:51 -070061 if node.name == 'binman':
62 return node
63 return None
64
Simon Glassa820af72020-09-06 10:39:09 -060065def _ReadMissingBlobHelp():
66 """Read the missing-blob-help file
67
68 This file containins help messages explaining what to do when external blobs
69 are missing.
70
71 Returns:
72 Dict:
73 key: Message tag (str)
74 value: Message text (str)
75 """
76
77 def _FinishTag(tag, msg, result):
78 if tag:
79 result[tag] = msg.rstrip()
80 tag = None
81 msg = ''
82 return tag, msg
83
84 my_data = pkg_resources.resource_string(__name__, 'missing-blob-help')
85 re_tag = re.compile('^([-a-z0-9]+):$')
86 result = {}
87 tag = None
88 msg = ''
89 for line in my_data.decode('utf-8').splitlines():
90 if not line.startswith('#'):
91 m_tag = re_tag.match(line)
92 if m_tag:
93 _, msg = _FinishTag(tag, msg, result)
94 tag = m_tag.group(1)
95 elif tag:
96 msg += line + '\n'
97 _FinishTag(tag, msg, result)
98 return result
99
100def _ShowBlobHelp(path, text):
101 tout.Warning('\n%s:' % path)
102 for line in text.splitlines():
103 tout.Warning(' %s' % line)
104
105def _ShowHelpForMissingBlobs(missing_list):
106 """Show help for each missing blob to help the user take action
107
108 Args:
109 missing_list: List of Entry objects to show help for
110 """
111 global missing_blob_help
112
113 if not missing_blob_help:
114 missing_blob_help = _ReadMissingBlobHelp()
115
116 for entry in missing_list:
117 tags = entry.GetHelpTags()
118
119 # Show the first match help message
120 for tag in tags:
121 if tag in missing_blob_help:
122 _ShowBlobHelp(entry._node.path, missing_blob_help[tag])
123 break
124
Simon Glass220ff5f2020-08-05 13:27:46 -0600125def GetEntryModules(include_testing=True):
126 """Get a set of entry class implementations
127
128 Returns:
129 Set of paths to entry class filenames
130 """
Simon Glassc1dc2f82020-08-29 11:36:14 -0600131 glob_list = pkg_resources.resource_listdir(__name__, 'etype')
132 glob_list = [fname for fname in glob_list if fname.endswith('.py')]
Simon Glass220ff5f2020-08-05 13:27:46 -0600133 return set([os.path.splitext(os.path.basename(item))[0]
134 for item in glob_list
135 if include_testing or '_testing' not in item])
136
Simon Glass29aa7362018-09-14 04:57:19 -0600137def WriteEntryDocs(modules, test_missing=None):
138 """Write out documentation for all entries
Simon Glass92307732018-07-06 10:27:40 -0600139
140 Args:
Simon Glass29aa7362018-09-14 04:57:19 -0600141 modules: List of Module objects to get docs for
142 test_missing: Used for testing only, to force an entry's documeentation
143 to show as missing even if it is present. Should be set to None in
144 normal use.
Simon Glass92307732018-07-06 10:27:40 -0600145 """
Simon Glassc585dd42020-04-17 18:09:03 -0600146 from binman.entry import Entry
Simon Glass969616c2018-07-17 13:25:36 -0600147 Entry.WriteDocs(modules, test_missing)
148
Simon Glassb2fd11d2019-07-08 14:25:48 -0600149
150def ListEntries(image_fname, entry_paths):
151 """List the entries in an image
152
153 This decodes the supplied image and displays a table of entries from that
154 image, preceded by a header.
155
156 Args:
157 image_fname: Image filename to process
158 entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*',
159 'section/u-boot'])
160 """
161 image = Image.FromFile(image_fname)
162
163 entries, lines, widths = image.GetListEntries(entry_paths)
164
165 num_columns = len(widths)
166 for linenum, line in enumerate(lines):
167 if linenum == 1:
168 # Print header line
169 print('-' * (sum(widths) + num_columns * 2))
170 out = ''
171 for i, item in enumerate(line):
172 width = -widths[i]
173 if item.startswith('>'):
174 width = -width
175 item = item[1:]
176 txt = '%*s ' % (width, item)
177 out += txt
178 print(out.rstrip())
179
Simon Glass4c613bf2019-07-08 14:25:50 -0600180
181def ReadEntry(image_fname, entry_path, decomp=True):
182 """Extract an entry from an image
183
184 This extracts the data from a particular entry in an image
185
186 Args:
187 image_fname: Image filename to process
188 entry_path: Path to entry to extract
189 decomp: True to return uncompressed data, if the data is compress
190 False to return the raw data
191
192 Returns:
193 data extracted from the entry
194 """
Simon Glassb9ba4e02019-08-24 07:22:44 -0600195 global Image
Simon Glass90cd6f02020-08-05 13:27:47 -0600196 from binman.image import Image
Simon Glassb9ba4e02019-08-24 07:22:44 -0600197
Simon Glass4c613bf2019-07-08 14:25:50 -0600198 image = Image.FromFile(image_fname)
199 entry = image.FindEntryPath(entry_path)
200 return entry.ReadData(decomp)
201
202
Simon Glass980a2842019-07-08 14:25:52 -0600203def ExtractEntries(image_fname, output_fname, outdir, entry_paths,
204 decomp=True):
205 """Extract the data from one or more entries and write it to files
206
207 Args:
208 image_fname: Image filename to process
209 output_fname: Single output filename to use if extracting one file, None
210 otherwise
211 outdir: Output directory to use (for any number of files), else None
212 entry_paths: List of entry paths to extract
Simon Glassd48f94e2019-07-20 12:24:12 -0600213 decomp: True to decompress the entry data
Simon Glass980a2842019-07-08 14:25:52 -0600214
215 Returns:
216 List of EntryInfo records that were written
217 """
218 image = Image.FromFile(image_fname)
219
220 # Output an entry to a single file, as a special case
221 if output_fname:
222 if not entry_paths:
Simon Glassa772d3f2019-07-20 12:24:14 -0600223 raise ValueError('Must specify an entry path to write with -f')
Simon Glass980a2842019-07-08 14:25:52 -0600224 if len(entry_paths) != 1:
Simon Glassa772d3f2019-07-20 12:24:14 -0600225 raise ValueError('Must specify exactly one entry path to write with -f')
Simon Glass980a2842019-07-08 14:25:52 -0600226 entry = image.FindEntryPath(entry_paths[0])
227 data = entry.ReadData(decomp)
228 tools.WriteFile(output_fname, data)
229 tout.Notice("Wrote %#x bytes to file '%s'" % (len(data), output_fname))
230 return
231
232 # Otherwise we will output to a path given by the entry path of each entry.
233 # This means that entries will appear in subdirectories if they are part of
234 # a sub-section.
235 einfos = image.GetListEntries(entry_paths)[0]
236 tout.Notice('%d entries match and will be written' % len(einfos))
237 for einfo in einfos:
238 entry = einfo.entry
239 data = entry.ReadData(decomp)
240 path = entry.GetPath()[1:]
241 fname = os.path.join(outdir, path)
242
243 # If this entry has children, create a directory for it and put its
244 # data in a file called 'root' in that directory
245 if entry.GetEntries():
Simon Glass4ef93d92021-03-18 20:24:51 +1300246 if fname and not os.path.exists(fname):
Simon Glass980a2842019-07-08 14:25:52 -0600247 os.makedirs(fname)
248 fname = os.path.join(fname, 'root')
Simon Glass08349452021-01-06 21:35:13 -0700249 tout.Notice("Write entry '%s' size %x to '%s'" %
250 (entry.GetPath(), len(data), fname))
Simon Glass980a2842019-07-08 14:25:52 -0600251 tools.WriteFile(fname, data)
252 return einfos
253
254
Simon Glass274bd0e2019-07-20 12:24:13 -0600255def BeforeReplace(image, allow_resize):
256 """Handle getting an image ready for replacing entries in it
257
258 Args:
259 image: Image to prepare
260 """
261 state.PrepareFromLoadedData(image)
262 image.LoadData()
263
264 # If repacking, drop the old offset/size values except for the original
265 # ones, so we are only left with the constraints.
266 if allow_resize:
267 image.ResetForPack()
268
269
270def ReplaceOneEntry(image, entry, data, do_compress, allow_resize):
271 """Handle replacing a single entry an an image
272
273 Args:
274 image: Image to update
275 entry: Entry to write
276 data: Data to replace with
277 do_compress: True to compress the data if needed, False if data is
278 already compressed so should be used as is
279 allow_resize: True to allow entries to change size (this does a re-pack
280 of the entries), False to raise an exception
281 """
282 if not entry.WriteData(data, do_compress):
283 if not image.allow_repack:
284 entry.Raise('Entry data size does not match, but allow-repack is not present for this image')
285 if not allow_resize:
286 entry.Raise('Entry data size does not match, but resize is disabled')
287
288
289def AfterReplace(image, allow_resize, write_map):
290 """Handle write out an image after replacing entries in it
291
292 Args:
293 image: Image to write
294 allow_resize: True to allow entries to change size (this does a re-pack
295 of the entries), False to raise an exception
296 write_map: True to write a map file
297 """
298 tout.Info('Processing image')
299 ProcessImage(image, update_fdt=True, write_map=write_map,
300 get_contents=False, allow_resize=allow_resize)
301
302
303def WriteEntryToImage(image, entry, data, do_compress=True, allow_resize=True,
304 write_map=False):
305 BeforeReplace(image, allow_resize)
306 tout.Info('Writing data to %s' % entry.GetPath())
307 ReplaceOneEntry(image, entry, data, do_compress, allow_resize)
308 AfterReplace(image, allow_resize=allow_resize, write_map=write_map)
309
310
Simon Glassd48f94e2019-07-20 12:24:12 -0600311def WriteEntry(image_fname, entry_path, data, do_compress=True,
312 allow_resize=True, write_map=False):
Simon Glass3971c952019-07-20 12:24:11 -0600313 """Replace an entry in an image
314
315 This replaces the data in a particular entry in an image. This size of the
316 new data must match the size of the old data unless allow_resize is True.
317
318 Args:
319 image_fname: Image filename to process
320 entry_path: Path to entry to extract
321 data: Data to replace with
Simon Glassd48f94e2019-07-20 12:24:12 -0600322 do_compress: True to compress the data if needed, False if data is
Simon Glass3971c952019-07-20 12:24:11 -0600323 already compressed so should be used as is
324 allow_resize: True to allow entries to change size (this does a re-pack
325 of the entries), False to raise an exception
Simon Glassd48f94e2019-07-20 12:24:12 -0600326 write_map: True to write a map file
Simon Glass3971c952019-07-20 12:24:11 -0600327
328 Returns:
329 Image object that was updated
330 """
Simon Glass274bd0e2019-07-20 12:24:13 -0600331 tout.Info("Write entry '%s', file '%s'" % (entry_path, image_fname))
Simon Glass3971c952019-07-20 12:24:11 -0600332 image = Image.FromFile(image_fname)
333 entry = image.FindEntryPath(entry_path)
Simon Glass274bd0e2019-07-20 12:24:13 -0600334 WriteEntryToImage(image, entry, data, do_compress=do_compress,
335 allow_resize=allow_resize, write_map=write_map)
Simon Glass3971c952019-07-20 12:24:11 -0600336
Simon Glass3971c952019-07-20 12:24:11 -0600337 return image
338
Simon Glass30033c22019-07-20 12:24:15 -0600339
340def ReplaceEntries(image_fname, input_fname, indir, entry_paths,
341 do_compress=True, allow_resize=True, write_map=False):
342 """Replace the data from one or more entries from input files
343
344 Args:
345 image_fname: Image filename to process
Jan Kiszka8ea44432021-11-11 08:13:30 +0100346 input_fname: Single input filename to use if replacing one file, None
Simon Glass30033c22019-07-20 12:24:15 -0600347 otherwise
348 indir: Input directory to use (for any number of files), else None
Jan Kiszka8ea44432021-11-11 08:13:30 +0100349 entry_paths: List of entry paths to replace
Simon Glass30033c22019-07-20 12:24:15 -0600350 do_compress: True if the input data is uncompressed and may need to be
351 compressed if the entry requires it, False if the data is already
352 compressed.
353 write_map: True to write a map file
354
355 Returns:
356 List of EntryInfo records that were written
357 """
Jan Kiszkaafc8f292021-11-11 08:14:18 +0100358 image_fname = os.path.abspath(image_fname)
Simon Glass30033c22019-07-20 12:24:15 -0600359 image = Image.FromFile(image_fname)
360
361 # Replace an entry from a single file, as a special case
362 if input_fname:
363 if not entry_paths:
364 raise ValueError('Must specify an entry path to read with -f')
365 if len(entry_paths) != 1:
366 raise ValueError('Must specify exactly one entry path to write with -f')
367 entry = image.FindEntryPath(entry_paths[0])
368 data = tools.ReadFile(input_fname)
369 tout.Notice("Read %#x bytes from file '%s'" % (len(data), input_fname))
370 WriteEntryToImage(image, entry, data, do_compress=do_compress,
371 allow_resize=allow_resize, write_map=write_map)
372 return
373
374 # Otherwise we will input from a path given by the entry path of each entry.
375 # This means that files must appear in subdirectories if they are part of
376 # a sub-section.
377 einfos = image.GetListEntries(entry_paths)[0]
378 tout.Notice("Replacing %d matching entries in image '%s'" %
379 (len(einfos), image_fname))
380
381 BeforeReplace(image, allow_resize)
382
383 for einfo in einfos:
384 entry = einfo.entry
385 if entry.GetEntries():
386 tout.Info("Skipping section entry '%s'" % entry.GetPath())
387 continue
388
389 path = entry.GetPath()[1:]
390 fname = os.path.join(indir, path)
391
392 if os.path.exists(fname):
393 tout.Notice("Write entry '%s' from file '%s'" %
394 (entry.GetPath(), fname))
395 data = tools.ReadFile(fname)
396 ReplaceOneEntry(image, entry, data, do_compress, allow_resize)
397 else:
398 tout.Warning("Skipping entry '%s' from missing file '%s'" %
399 (entry.GetPath(), fname))
400
401 AfterReplace(image, allow_resize=allow_resize, write_map=write_map)
402 return image
403
404
Simon Glass55ab0b62021-03-18 20:25:06 +1300405def PrepareImagesAndDtbs(dtb_fname, select_images, update_fdt, use_expanded):
Simon Glassd3151ff2019-07-20 12:23:27 -0600406 """Prepare the images to be processed and select the device tree
407
408 This function:
409 - reads in the device tree
410 - finds and scans the binman node to create all entries
411 - selects which images to build
412 - Updates the device tress with placeholder properties for offset,
413 image-pos, etc.
414
415 Args:
416 dtb_fname: Filename of the device tree file to use (.dts or .dtb)
417 selected_images: List of images to output, or None for all
418 update_fdt: True to update the FDT wth entry offsets, etc.
Simon Glass55ab0b62021-03-18 20:25:06 +1300419 use_expanded: True to use expanded versions of entries, if available.
420 So if 'u-boot' is called for, we use 'u-boot-expanded' instead. This
421 is needed if update_fdt is True (although tests may disable it)
Simon Glass31ee50f2020-09-01 05:13:55 -0600422
423 Returns:
424 OrderedDict of images:
425 key: Image name (str)
426 value: Image object
Simon Glassd3151ff2019-07-20 12:23:27 -0600427 """
428 # Import these here in case libfdt.py is not available, in which case
429 # the above help option still works.
Simon Glassc585dd42020-04-17 18:09:03 -0600430 from dtoc import fdt
431 from dtoc import fdt_util
Simon Glassd3151ff2019-07-20 12:23:27 -0600432 global images
433
434 # Get the device tree ready by compiling it and copying the compiled
435 # output into a file in our output directly. Then scan it for use
436 # in binman.
437 dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
438 fname = tools.GetOutputFilename('u-boot.dtb.out')
439 tools.WriteFile(fname, tools.ReadFile(dtb_fname))
440 dtb = fdt.FdtScan(fname)
441
442 node = _FindBinmanNode(dtb)
443 if not node:
444 raise ValueError("Device tree '%s' does not have a 'binman' "
445 "node" % dtb_fname)
446
Simon Glass55ab0b62021-03-18 20:25:06 +1300447 images = _ReadImageDesc(node, use_expanded)
Simon Glassd3151ff2019-07-20 12:23:27 -0600448
449 if select_images:
450 skip = []
451 new_images = OrderedDict()
452 for name, image in images.items():
453 if name in select_images:
454 new_images[name] = image
455 else:
456 skip.append(name)
457 images = new_images
458 tout.Notice('Skipping images: %s' % ', '.join(skip))
459
460 state.Prepare(images, dtb)
461
462 # Prepare the device tree by making sure that any missing
463 # properties are added (e.g. 'pos' and 'size'). The values of these
464 # may not be correct yet, but we add placeholders so that the
465 # size of the device tree is correct. Later, in
466 # SetCalculatedProperties() we will insert the correct values
467 # without changing the device-tree size, thus ensuring that our
468 # entry offsets remain the same.
469 for image in images.values():
470 image.ExpandEntries()
471 if update_fdt:
Simon Glassacd6c6e2020-10-26 17:40:17 -0600472 image.AddMissingProperties(True)
Simon Glassd3151ff2019-07-20 12:23:27 -0600473 image.ProcessFdt(dtb)
474
Simon Glass5a300602019-07-20 12:23:29 -0600475 for dtb_item in state.GetAllFdts():
Simon Glassd3151ff2019-07-20 12:23:27 -0600476 dtb_item.Sync(auto_resize=True)
477 dtb_item.Pack()
478 dtb_item.Flush()
479 return images
480
481
Simon Glassf8a54bc2019-07-20 12:23:56 -0600482def ProcessImage(image, update_fdt, write_map, get_contents=True,
Simon Glass5d94cc62020-07-09 18:39:38 -0600483 allow_resize=True, allow_missing=False):
Simon Glassb766c5e52019-07-20 12:23:24 -0600484 """Perform all steps for this image, including checking and # writing it.
485
486 This means that errors found with a later image will be reported after
487 earlier images are already completed and written, but that does not seem
488 important.
489
490 Args:
491 image: Image to process
492 update_fdt: True to update the FDT wth entry offsets, etc.
493 write_map: True to write a map file
Simon Glass072959a2019-07-20 12:23:50 -0600494 get_contents: True to get the image contents from files, etc., False if
495 the contents is already present
Simon Glassf8a54bc2019-07-20 12:23:56 -0600496 allow_resize: True to allow entries to change size (this does a re-pack
497 of the entries), False to raise an exception
Simon Glass5d94cc62020-07-09 18:39:38 -0600498 allow_missing: Allow blob_ext objects to be missing
Simon Glassa003cd32020-07-09 18:39:40 -0600499
500 Returns:
501 True if one or more external blobs are missing, False if all are present
Simon Glassb766c5e52019-07-20 12:23:24 -0600502 """
Simon Glass072959a2019-07-20 12:23:50 -0600503 if get_contents:
Simon Glass5d94cc62020-07-09 18:39:38 -0600504 image.SetAllowMissing(allow_missing)
Simon Glass072959a2019-07-20 12:23:50 -0600505 image.GetEntryContents()
Simon Glassb766c5e52019-07-20 12:23:24 -0600506 image.GetEntryOffsets()
507
508 # We need to pack the entries to figure out where everything
509 # should be placed. This sets the offset/size of each entry.
510 # However, after packing we call ProcessEntryContents() which
511 # may result in an entry changing size. In that case we need to
512 # do another pass. Since the device tree often contains the
513 # final offset/size information we try to make space for this in
514 # AddMissingProperties() above. However, if the device is
515 # compressed we cannot know this compressed size in advance,
516 # since changing an offset from 0x100 to 0x104 (for example) can
517 # alter the compressed size of the device tree. So we need a
518 # third pass for this.
Simon Glass37fdd142019-07-20 12:24:06 -0600519 passes = 5
Simon Glassb766c5e52019-07-20 12:23:24 -0600520 for pack_pass in range(passes):
521 try:
522 image.PackEntries()
Simon Glassb766c5e52019-07-20 12:23:24 -0600523 except Exception as e:
524 if write_map:
525 fname = image.WriteMap()
526 print("Wrote map file '%s' to show errors" % fname)
527 raise
528 image.SetImagePos()
529 if update_fdt:
530 image.SetCalculatedProperties()
Simon Glass5a300602019-07-20 12:23:29 -0600531 for dtb_item in state.GetAllFdts():
Simon Glassb766c5e52019-07-20 12:23:24 -0600532 dtb_item.Sync()
Simon Glassf8a54bc2019-07-20 12:23:56 -0600533 dtb_item.Flush()
Simon Glasse5943412019-08-24 07:23:12 -0600534 image.WriteSymbols()
Simon Glassb766c5e52019-07-20 12:23:24 -0600535 sizes_ok = image.ProcessEntryContents()
536 if sizes_ok:
537 break
538 image.ResetForPack()
Simon Glass6bf9b472019-08-24 07:23:13 -0600539 tout.Info('Pack completed after %d pass(es)' % (pack_pass + 1))
Simon Glassb766c5e52019-07-20 12:23:24 -0600540 if not sizes_ok:
Simon Glass9d8ee322019-07-20 12:23:58 -0600541 image.Raise('Entries changed size after packing (tried %s passes)' %
Simon Glassb766c5e52019-07-20 12:23:24 -0600542 passes)
543
Simon Glassb766c5e52019-07-20 12:23:24 -0600544 image.BuildImage()
545 if write_map:
546 image.WriteMap()
Simon Glassa003cd32020-07-09 18:39:40 -0600547 missing_list = []
548 image.CheckMissing(missing_list)
549 if missing_list:
550 tout.Warning("Image '%s' is missing external blobs and is non-functional: %s" %
551 (image.name, ' '.join([e.name for e in missing_list])))
Simon Glassa820af72020-09-06 10:39:09 -0600552 _ShowHelpForMissingBlobs(missing_list)
Simon Glassa003cd32020-07-09 18:39:40 -0600553 return bool(missing_list)
Simon Glassb766c5e52019-07-20 12:23:24 -0600554
555
Simon Glassf46732a2019-07-08 14:25:29 -0600556def Binman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700557 """The main control code for binman
558
559 This assumes that help and test options have already been dealt with. It
560 deals with the core task of building images.
561
562 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600563 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700564 """
Simon Glassb9ba4e02019-08-24 07:22:44 -0600565 global Image
566 global state
567
Simon Glassf46732a2019-07-08 14:25:29 -0600568 if args.full_help:
Paul Barker25ecd972021-09-08 12:38:01 +0100569 tools.PrintFullHelp(
570 os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'README.rst')
571 )
Simon Glass2574ef62016-11-25 20:15:51 -0700572 return 0
573
Simon Glassb9ba4e02019-08-24 07:22:44 -0600574 # Put these here so that we can import this module without libfdt
Simon Glass90cd6f02020-08-05 13:27:47 -0600575 from binman.image import Image
Simon Glassc585dd42020-04-17 18:09:03 -0600576 from binman import state
Simon Glassb9ba4e02019-08-24 07:22:44 -0600577
Simon Glassdf08cbb2019-09-15 18:10:36 -0600578 if args.cmd in ['ls', 'extract', 'replace']:
Simon Glass9b7f5002019-07-20 12:23:53 -0600579 try:
Simon Glassdf08cbb2019-09-15 18:10:36 -0600580 tout.Init(args.verbosity)
Simon Glass9b7f5002019-07-20 12:23:53 -0600581 tools.PrepareOutputDir(None)
Simon Glassdf08cbb2019-09-15 18:10:36 -0600582 if args.cmd == 'ls':
583 ListEntries(args.image, args.paths)
Simon Glassb2fd11d2019-07-08 14:25:48 -0600584
Simon Glassdf08cbb2019-09-15 18:10:36 -0600585 if args.cmd == 'extract':
586 ExtractEntries(args.image, args.filename, args.outdir, args.paths,
587 not args.uncompressed)
Simon Glass980a2842019-07-08 14:25:52 -0600588
Simon Glassdf08cbb2019-09-15 18:10:36 -0600589 if args.cmd == 'replace':
590 ReplaceEntries(args.image, args.filename, args.indir, args.paths,
591 do_compress=not args.compressed,
592 allow_resize=not args.fix_size, write_map=args.map)
593 except:
594 raise
Simon Glass30033c22019-07-20 12:24:15 -0600595 finally:
596 tools.FinaliseOutputDir()
597 return 0
598
Simon Glassadfb8492021-11-03 21:09:18 -0600599 elf_params = None
600 if args.update_fdt_in_elf:
601 elf_params = args.update_fdt_in_elf.split(',')
602 if len(elf_params) != 4:
603 raise ValueError('Invalid args %s to --update-fdt-in-elf: expected infile,outfile,begin_sym,end_sym' %
604 elf_params)
605
Simon Glass2574ef62016-11-25 20:15:51 -0700606 # Try to figure out which device tree contains our image description
Simon Glassf46732a2019-07-08 14:25:29 -0600607 if args.dt:
608 dtb_fname = args.dt
Simon Glass2574ef62016-11-25 20:15:51 -0700609 else:
Simon Glassf46732a2019-07-08 14:25:29 -0600610 board = args.board
Simon Glass2574ef62016-11-25 20:15:51 -0700611 if not board:
612 raise ValueError('Must provide a board to process (use -b <board>)')
Simon Glassf46732a2019-07-08 14:25:29 -0600613 board_pathname = os.path.join(args.build_dir, board)
Simon Glass2574ef62016-11-25 20:15:51 -0700614 dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
Simon Glassf46732a2019-07-08 14:25:29 -0600615 if not args.indir:
616 args.indir = ['.']
617 args.indir.append(board_pathname)
Simon Glass2574ef62016-11-25 20:15:51 -0700618
619 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600620 tout.Init(args.verbosity)
621 elf.debug = args.debug
622 cbfs_util.VERBOSE = args.verbosity > 2
623 state.use_fake_dtb = args.fake_dtb
Simon Glass55ab0b62021-03-18 20:25:06 +1300624
625 # Normally we replace the 'u-boot' etype with 'u-boot-expanded', etc.
626 # When running tests this can be disabled using this flag. When not
627 # updating the FDT in image, it is not needed by binman, but we use it
628 # for consistency, so that the images look the same to U-Boot at
629 # runtime.
630 use_expanded = not args.no_expanded
Simon Glass2574ef62016-11-25 20:15:51 -0700631 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600632 tools.SetInputDirs(args.indir)
633 tools.PrepareOutputDir(args.outdir, args.preserve)
634 tools.SetToolPaths(args.toolpath)
635 state.SetEntryArgs(args.entry_arg)
Simon Glass76f496d2021-07-06 10:36:37 -0600636 state.SetThreads(args.threads)
Simon Glass92307732018-07-06 10:27:40 -0600637
Simon Glassd3151ff2019-07-20 12:23:27 -0600638 images = PrepareImagesAndDtbs(dtb_fname, args.image,
Simon Glass55ab0b62021-03-18 20:25:06 +1300639 args.update_fdt, use_expanded)
Simon Glass76f496d2021-07-06 10:36:37 -0600640 if args.test_section_timeout:
641 # Set the first image to timeout, used in testThreadTimeout()
642 images[list(images.keys())[0]].test_section_timeout = True
Simon Glassa003cd32020-07-09 18:39:40 -0600643 missing = False
Simon Glass2574ef62016-11-25 20:15:51 -0700644 for image in images.values():
Simon Glassa003cd32020-07-09 18:39:40 -0600645 missing |= ProcessImage(image, args.update_fdt, args.map,
646 allow_missing=args.allow_missing)
Simon Glassbdb40312018-09-14 04:57:20 -0600647
648 # Write the updated FDTs to our output files
Simon Glass5a300602019-07-20 12:23:29 -0600649 for dtb_item in state.GetAllFdts():
Simon Glassbdb40312018-09-14 04:57:20 -0600650 tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
651
Simon Glassadfb8492021-11-03 21:09:18 -0600652 if elf_params:
653 data = state.GetFdtForEtype('u-boot-dtb').GetContents()
654 elf.UpdateFile(*elf_params, data)
655
Simon Glassa003cd32020-07-09 18:39:40 -0600656 if missing:
Simon Glassa820af72020-09-06 10:39:09 -0600657 tout.Warning("\nSome images are invalid")
Simon Glass748a1d42021-07-06 10:36:41 -0600658
659 # Use this to debug the time take to pack the image
660 #state.TimingShow()
Simon Glass2574ef62016-11-25 20:15:51 -0700661 finally:
662 tools.FinaliseOutputDir()
663 finally:
664 tout.Uninit()
665
666 return 0