2018-05-07 06:58:06 +09:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-11-26 12:15:51 +09:00
|
|
|
# Copyright (c) 2016 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Creates binary images from input files controlled by a description
|
|
|
|
#
|
|
|
|
|
2019-05-15 06:53:37 +09:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-11-26 12:15:51 +09:00
|
|
|
from collections import OrderedDict
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tools
|
|
|
|
|
2019-07-09 04:18:53 +09:00
|
|
|
import cbfs_util
|
2016-11-26 12:15:51 +09:00
|
|
|
import command
|
2017-11-14 10:55:00 +09:00
|
|
|
import elf
|
2016-11-26 12:15:51 +09:00
|
|
|
from image import Image
|
2018-09-14 19:57:19 +09:00
|
|
|
import state
|
2016-11-26 12:15:51 +09:00
|
|
|
import tout
|
|
|
|
|
|
|
|
# List of images we plan to create
|
|
|
|
# Make this global so that it can be referenced from tests
|
|
|
|
images = OrderedDict()
|
|
|
|
|
|
|
|
def _ReadImageDesc(binman_node):
|
|
|
|
"""Read the image descriptions from the /binman node
|
|
|
|
|
|
|
|
This normally produces a single Image object called 'image'. But if
|
|
|
|
multiple images are present, they will all be returned.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
binman_node: Node object of the /binman node
|
|
|
|
Returns:
|
|
|
|
OrderedDict of Image objects, each of which describes an image
|
|
|
|
"""
|
|
|
|
images = OrderedDict()
|
|
|
|
if 'multiple-images' in binman_node.props:
|
|
|
|
for node in binman_node.subnodes:
|
|
|
|
images[node.name] = Image(node.name, node)
|
|
|
|
else:
|
|
|
|
images['image'] = Image('image', binman_node)
|
|
|
|
return images
|
|
|
|
|
2017-05-27 22:38:29 +09:00
|
|
|
def _FindBinmanNode(dtb):
|
2016-11-26 12:15:51 +09:00
|
|
|
"""Find the 'binman' node in the device tree
|
|
|
|
|
|
|
|
Args:
|
2017-05-27 22:38:29 +09:00
|
|
|
dtb: Fdt object to scan
|
2016-11-26 12:15:51 +09:00
|
|
|
Returns:
|
|
|
|
Node object of /binman node, or None if not found
|
|
|
|
"""
|
2017-05-27 22:38:29 +09:00
|
|
|
for node in dtb.GetRoot().subnodes:
|
2016-11-26 12:15:51 +09:00
|
|
|
if node.name == 'binman':
|
|
|
|
return node
|
|
|
|
return None
|
|
|
|
|
2018-09-14 19:57:19 +09:00
|
|
|
def WriteEntryDocs(modules, test_missing=None):
|
|
|
|
"""Write out documentation for all entries
|
2018-07-07 01:27:40 +09:00
|
|
|
|
|
|
|
Args:
|
2018-09-14 19:57:19 +09:00
|
|
|
modules: List of Module objects to get docs for
|
|
|
|
test_missing: Used for testing only, to force an entry's documeentation
|
|
|
|
to show as missing even if it is present. Should be set to None in
|
|
|
|
normal use.
|
2018-07-07 01:27:40 +09:00
|
|
|
"""
|
2018-07-18 04:25:36 +09:00
|
|
|
from entry import Entry
|
|
|
|
Entry.WriteDocs(modules, test_missing)
|
|
|
|
|
2019-07-09 05:25:48 +09:00
|
|
|
|
|
|
|
def ListEntries(image_fname, entry_paths):
|
|
|
|
"""List the entries in an image
|
|
|
|
|
|
|
|
This decodes the supplied image and displays a table of entries from that
|
|
|
|
image, preceded by a header.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
image_fname: Image filename to process
|
|
|
|
entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*',
|
|
|
|
'section/u-boot'])
|
|
|
|
"""
|
|
|
|
image = Image.FromFile(image_fname)
|
|
|
|
|
|
|
|
entries, lines, widths = image.GetListEntries(entry_paths)
|
|
|
|
|
|
|
|
num_columns = len(widths)
|
|
|
|
for linenum, line in enumerate(lines):
|
|
|
|
if linenum == 1:
|
|
|
|
# Print header line
|
|
|
|
print('-' * (sum(widths) + num_columns * 2))
|
|
|
|
out = ''
|
|
|
|
for i, item in enumerate(line):
|
|
|
|
width = -widths[i]
|
|
|
|
if item.startswith('>'):
|
|
|
|
width = -width
|
|
|
|
item = item[1:]
|
|
|
|
txt = '%*s ' % (width, item)
|
|
|
|
out += txt
|
|
|
|
print(out.rstrip())
|
|
|
|
|
2019-07-09 05:25:29 +09:00
|
|
|
def Binman(args):
|
2016-11-26 12:15:51 +09:00
|
|
|
"""The main control code for binman
|
|
|
|
|
|
|
|
This assumes that help and test options have already been dealt with. It
|
|
|
|
deals with the core task of building images.
|
|
|
|
|
|
|
|
Args:
|
2019-07-09 05:25:29 +09:00
|
|
|
args: Command line arguments Namespace object
|
2016-11-26 12:15:51 +09:00
|
|
|
"""
|
|
|
|
global images
|
|
|
|
|
2019-07-09 05:25:29 +09:00
|
|
|
if args.full_help:
|
2016-11-26 12:15:51 +09:00
|
|
|
pager = os.getenv('PAGER')
|
|
|
|
if not pager:
|
|
|
|
pager = 'more'
|
|
|
|
fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
|
|
|
|
'README')
|
|
|
|
command.Run(pager, fname)
|
|
|
|
return 0
|
|
|
|
|
2019-07-09 05:25:48 +09:00
|
|
|
if args.cmd == 'ls':
|
|
|
|
ListEntries(args.image, args.paths)
|
|
|
|
return 0
|
|
|
|
|
2016-11-26 12:15:51 +09:00
|
|
|
# Try to figure out which device tree contains our image description
|
2019-07-09 05:25:29 +09:00
|
|
|
if args.dt:
|
|
|
|
dtb_fname = args.dt
|
2016-11-26 12:15:51 +09:00
|
|
|
else:
|
2019-07-09 05:25:29 +09:00
|
|
|
board = args.board
|
2016-11-26 12:15:51 +09:00
|
|
|
if not board:
|
|
|
|
raise ValueError('Must provide a board to process (use -b <board>)')
|
2019-07-09 05:25:29 +09:00
|
|
|
board_pathname = os.path.join(args.build_dir, board)
|
2016-11-26 12:15:51 +09:00
|
|
|
dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
|
2019-07-09 05:25:29 +09:00
|
|
|
if not args.indir:
|
|
|
|
args.indir = ['.']
|
|
|
|
args.indir.append(board_pathname)
|
2016-11-26 12:15:51 +09:00
|
|
|
|
|
|
|
try:
|
2018-07-18 04:25:34 +09:00
|
|
|
# Import these here in case libfdt.py is not available, in which case
|
|
|
|
# the above help option still works.
|
|
|
|
import fdt
|
|
|
|
import fdt_util
|
|
|
|
|
2019-07-09 05:25:29 +09:00
|
|
|
tout.Init(args.verbosity)
|
|
|
|
elf.debug = args.debug
|
|
|
|
cbfs_util.VERBOSE = args.verbosity > 2
|
|
|
|
state.use_fake_dtb = args.fake_dtb
|
2016-11-26 12:15:51 +09:00
|
|
|
try:
|
2019-07-09 05:25:29 +09:00
|
|
|
tools.SetInputDirs(args.indir)
|
|
|
|
tools.PrepareOutputDir(args.outdir, args.preserve)
|
|
|
|
tools.SetToolPaths(args.toolpath)
|
|
|
|
state.SetEntryArgs(args.entry_arg)
|
2018-07-07 01:27:40 +09:00
|
|
|
|
|
|
|
# Get the device tree ready by compiling it and copying the compiled
|
|
|
|
# output into a file in our output directly. Then scan it for use
|
|
|
|
# in binman.
|
|
|
|
dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
|
2018-09-14 19:57:24 +09:00
|
|
|
fname = tools.GetOutputFilename('u-boot.dtb.out')
|
|
|
|
tools.WriteFile(fname, tools.ReadFile(dtb_fname))
|
2018-07-07 01:27:40 +09:00
|
|
|
dtb = fdt.FdtScan(fname)
|
|
|
|
|
2017-05-27 22:38:29 +09:00
|
|
|
node = _FindBinmanNode(dtb)
|
2016-11-26 12:15:51 +09:00
|
|
|
if not node:
|
|
|
|
raise ValueError("Device tree '%s' does not have a 'binman' "
|
|
|
|
"node" % dtb_fname)
|
2018-07-07 01:27:40 +09:00
|
|
|
|
2016-11-26 12:15:51 +09:00
|
|
|
images = _ReadImageDesc(node)
|
2018-07-07 01:27:40 +09:00
|
|
|
|
2019-07-09 05:25:29 +09:00
|
|
|
if args.image:
|
2018-09-14 19:57:12 +09:00
|
|
|
skip = []
|
2019-05-18 13:00:45 +09:00
|
|
|
new_images = OrderedDict()
|
2019-05-15 06:53:41 +09:00
|
|
|
for name, image in images.items():
|
2019-07-09 05:25:29 +09:00
|
|
|
if name in args.image:
|
2019-05-18 13:00:45 +09:00
|
|
|
new_images[name] = image
|
|
|
|
else:
|
2018-09-14 19:57:12 +09:00
|
|
|
skip.append(name)
|
2019-05-18 13:00:45 +09:00
|
|
|
images = new_images
|
2019-07-09 05:25:29 +09:00
|
|
|
if skip and args.verbosity >= 2:
|
2019-05-15 06:53:37 +09:00
|
|
|
print('Skipping images: %s' % ', '.join(skip))
|
2018-09-14 19:57:12 +09:00
|
|
|
|
2018-09-14 19:57:22 +09:00
|
|
|
state.Prepare(images, dtb)
|
2018-09-14 19:57:20 +09:00
|
|
|
|
2018-07-07 01:27:40 +09:00
|
|
|
# Prepare the device tree by making sure that any missing
|
|
|
|
# properties are added (e.g. 'pos' and 'size'). The values of these
|
|
|
|
# may not be correct yet, but we add placeholders so that the
|
|
|
|
# size of the device tree is correct. Later, in
|
|
|
|
# SetCalculatedProperties() we will insert the correct values
|
|
|
|
# without changing the device-tree size, thus ensuring that our
|
2018-08-02 06:22:37 +09:00
|
|
|
# entry offsets remain the same.
|
2018-07-07 01:27:40 +09:00
|
|
|
for image in images.values():
|
2018-09-14 19:57:28 +09:00
|
|
|
image.ExpandEntries()
|
2019-07-09 05:25:29 +09:00
|
|
|
if args.update_fdt:
|
2018-07-07 01:27:41 +09:00
|
|
|
image.AddMissingProperties()
|
2018-07-07 01:27:40 +09:00
|
|
|
image.ProcessFdt(dtb)
|
|
|
|
|
2018-09-14 19:57:20 +09:00
|
|
|
for dtb_item in state.GetFdts():
|
|
|
|
dtb_item.Sync(auto_resize=True)
|
|
|
|
dtb_item.Pack()
|
|
|
|
dtb_item.Flush()
|
2018-07-07 01:27:40 +09:00
|
|
|
|
2016-11-26 12:15:51 +09:00
|
|
|
for image in images.values():
|
|
|
|
# Perform all steps for this image, including checking and
|
|
|
|
# writing it. This means that errors found with a later
|
|
|
|
# image will be reported after earlier images are already
|
|
|
|
# completed and written, but that does not seem important.
|
|
|
|
image.GetEntryContents()
|
2018-08-02 06:22:37 +09:00
|
|
|
image.GetEntryOffsets()
|
2019-07-09 05:25:37 +09:00
|
|
|
|
|
|
|
# We need to pack the entries to figure out where everything
|
|
|
|
# should be placed. This sets the offset/size of each entry.
|
|
|
|
# However, after packing we call ProcessEntryContents() which
|
|
|
|
# may result in an entry changing size. In that case we need to
|
|
|
|
# do another pass. Since the device tree often contains the
|
|
|
|
# final offset/size information we try to make space for this in
|
|
|
|
# AddMissingProperties() above. However, if the device is
|
|
|
|
# compressed we cannot know this compressed size in advance,
|
|
|
|
# since changing an offset from 0x100 to 0x104 (for example) can
|
|
|
|
# alter the compressed size of the device tree. So we need a
|
|
|
|
# third pass for this.
|
|
|
|
passes = 3
|
|
|
|
for pack_pass in range(passes):
|
|
|
|
try:
|
|
|
|
image.PackEntries()
|
|
|
|
image.CheckSize()
|
|
|
|
image.CheckEntries()
|
|
|
|
except Exception as e:
|
|
|
|
if args.map:
|
|
|
|
fname = image.WriteMap()
|
|
|
|
print("Wrote map file '%s' to show errors" % fname)
|
|
|
|
raise
|
|
|
|
image.SetImagePos()
|
|
|
|
if args.update_fdt:
|
|
|
|
image.SetCalculatedProperties()
|
|
|
|
for dtb_item in state.GetFdts():
|
|
|
|
dtb_item.Sync()
|
|
|
|
sizes_ok = image.ProcessEntryContents()
|
|
|
|
if sizes_ok:
|
|
|
|
break
|
|
|
|
image.ResetForPack()
|
|
|
|
if not sizes_ok:
|
|
|
|
image.Raise('Entries expanded after packing (tried %s passes)' %
|
|
|
|
passes)
|
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 10:55:01 +09:00
|
|
|
image.WriteSymbols()
|
2016-11-26 12:15:51 +09:00
|
|
|
image.BuildImage()
|
2019-07-09 05:25:29 +09:00
|
|
|
if args.map:
|
2018-06-02 00:38:20 +09:00
|
|
|
image.WriteMap()
|
2018-09-14 19:57:20 +09:00
|
|
|
|
|
|
|
# Write the updated FDTs to our output files
|
|
|
|
for dtb_item in state.GetFdts():
|
|
|
|
tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
|
|
|
|
|
2016-11-26 12:15:51 +09:00
|
|
|
finally:
|
|
|
|
tools.FinaliseOutputDir()
|
|
|
|
finally:
|
|
|
|
tout.Uninit()
|
|
|
|
|
|
|
|
return 0
|