2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-11-26 03:15:51 +00:00
|
|
|
# Copyright (c) 2016 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Creates binary images from input files controlled by a description
|
|
|
|
#
|
|
|
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
import os
|
2018-07-17 19:25:32 +00:00
|
|
|
import re
|
2016-11-26 03:15:51 +00:00
|
|
|
import sys
|
|
|
|
import tools
|
|
|
|
|
|
|
|
import command
|
2017-11-14 01:55:00 +00:00
|
|
|
import elf
|
2016-11-26 03:15:51 +00:00
|
|
|
from image import Image
|
|
|
|
import tout
|
|
|
|
|
|
|
|
# List of images we plan to create
|
|
|
|
# Make this global so that it can be referenced from tests
|
|
|
|
images = OrderedDict()
|
|
|
|
|
2018-07-06 16:27:40 +00:00
|
|
|
# Records the device-tree files known to binman, keyed by filename (e.g.
|
|
|
|
# 'u-boot-spl.dtb')
|
|
|
|
fdt_files = {}
|
|
|
|
|
2018-07-17 19:25:32 +00:00
|
|
|
# Arguments passed to binman to provide arguments to entries
|
|
|
|
entry_args = {}
|
|
|
|
|
2018-07-06 16:27:40 +00:00
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
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 13:38:29 +00:00
|
|
|
def _FindBinmanNode(dtb):
|
2016-11-26 03:15:51 +00:00
|
|
|
"""Find the 'binman' node in the device tree
|
|
|
|
|
|
|
|
Args:
|
2017-05-27 13:38:29 +00:00
|
|
|
dtb: Fdt object to scan
|
2016-11-26 03:15:51 +00:00
|
|
|
Returns:
|
|
|
|
Node object of /binman node, or None if not found
|
|
|
|
"""
|
2017-05-27 13:38:29 +00:00
|
|
|
for node in dtb.GetRoot().subnodes:
|
2016-11-26 03:15:51 +00:00
|
|
|
if node.name == 'binman':
|
|
|
|
return node
|
|
|
|
return None
|
|
|
|
|
2018-07-06 16:27:40 +00:00
|
|
|
def GetFdt(fname):
|
|
|
|
"""Get the Fdt object for a particular device-tree filename
|
|
|
|
|
|
|
|
Binman keeps track of at least one device-tree file called u-boot.dtb but
|
|
|
|
can also have others (e.g. for SPL). This function looks up the given
|
|
|
|
filename and returns the associated Fdt object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fname: Filename to look up (e.g. 'u-boot.dtb').
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Fdt object associated with the filename
|
|
|
|
"""
|
|
|
|
return fdt_files[fname]
|
|
|
|
|
|
|
|
def GetFdtPath(fname):
|
|
|
|
return fdt_files[fname]._fname
|
|
|
|
|
2018-07-17 19:25:32 +00:00
|
|
|
def SetEntryArgs(args):
|
|
|
|
global entry_args
|
|
|
|
|
|
|
|
entry_args = {}
|
|
|
|
if args:
|
|
|
|
for arg in args:
|
|
|
|
m = re.match('([^=]*)=(.*)', arg)
|
|
|
|
if not m:
|
|
|
|
raise ValueError("Invalid entry arguemnt '%s'" % arg)
|
|
|
|
entry_args[m.group(1)] = m.group(2)
|
|
|
|
|
|
|
|
def GetEntryArg(name):
|
|
|
|
return entry_args.get(name)
|
|
|
|
|
2018-07-17 19:25:36 +00:00
|
|
|
def WriteEntryDocs(modules, test_missing=None):
|
|
|
|
from entry import Entry
|
|
|
|
Entry.WriteDocs(modules, test_missing)
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
def Binman(options, args):
|
|
|
|
"""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:
|
|
|
|
options: Command line options object
|
|
|
|
args: Command line arguments (list of strings)
|
|
|
|
"""
|
|
|
|
global images
|
|
|
|
|
|
|
|
if options.full_help:
|
|
|
|
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
|
|
|
|
|
|
|
|
# Try to figure out which device tree contains our image description
|
|
|
|
if options.dt:
|
|
|
|
dtb_fname = options.dt
|
|
|
|
else:
|
|
|
|
board = options.board
|
|
|
|
if not board:
|
|
|
|
raise ValueError('Must provide a board to process (use -b <board>)')
|
|
|
|
board_pathname = os.path.join(options.build_dir, board)
|
|
|
|
dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
|
|
|
|
if not options.indir:
|
|
|
|
options.indir = ['.']
|
|
|
|
options.indir.append(board_pathname)
|
|
|
|
|
|
|
|
try:
|
2018-07-17 19:25:34 +00: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
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
tout.Init(options.verbosity)
|
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 01:55:01 +00:00
|
|
|
elf.debug = options.debug
|
2016-11-26 03:15:51 +00:00
|
|
|
try:
|
|
|
|
tools.SetInputDirs(options.indir)
|
|
|
|
tools.PrepareOutputDir(options.outdir, options.preserve)
|
2018-07-17 19:25:32 +00:00
|
|
|
SetEntryArgs(options.entry_arg)
|
2018-07-06 16:27:40 +00: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)
|
|
|
|
fname = tools.GetOutputFilename('u-boot-out.dtb')
|
|
|
|
with open(dtb_fname) as infd:
|
|
|
|
with open(fname, 'wb') as outfd:
|
|
|
|
outfd.write(infd.read())
|
|
|
|
dtb = fdt.FdtScan(fname)
|
|
|
|
|
|
|
|
# Note the file so that GetFdt() can find it
|
|
|
|
fdt_files['u-boot.dtb'] = dtb
|
2017-05-27 13:38:29 +00:00
|
|
|
node = _FindBinmanNode(dtb)
|
2016-11-26 03:15:51 +00:00
|
|
|
if not node:
|
|
|
|
raise ValueError("Device tree '%s' does not have a 'binman' "
|
|
|
|
"node" % dtb_fname)
|
2018-07-06 16:27:40 +00:00
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
images = _ReadImageDesc(node)
|
2018-07-06 16:27:40 +00: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-01 21:22:37 +00:00
|
|
|
# entry offsets remain the same.
|
2018-07-06 16:27:40 +00:00
|
|
|
for image in images.values():
|
2018-07-06 16:27:41 +00:00
|
|
|
if options.update_fdt:
|
|
|
|
image.AddMissingProperties()
|
2018-07-06 16:27:40 +00:00
|
|
|
image.ProcessFdt(dtb)
|
|
|
|
|
|
|
|
dtb.Pack()
|
|
|
|
dtb.Flush()
|
|
|
|
|
2016-11-26 03:15:51 +00: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-01 21:22:37 +00:00
|
|
|
image.GetEntryOffsets()
|
2016-11-26 03:15:51 +00:00
|
|
|
image.PackEntries()
|
|
|
|
image.CheckSize()
|
|
|
|
image.CheckEntries()
|
2018-08-01 21:22:42 +00:00
|
|
|
image.SetImagePos()
|
2018-07-06 16:27:41 +00:00
|
|
|
if options.update_fdt:
|
|
|
|
image.SetCalculatedProperties()
|
2016-11-26 03:15:51 +00:00
|
|
|
image.ProcessEntryContents()
|
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 01:55:01 +00:00
|
|
|
image.WriteSymbols()
|
2016-11-26 03:15:51 +00:00
|
|
|
image.BuildImage()
|
2018-06-01 15:38:20 +00:00
|
|
|
if options.map:
|
|
|
|
image.WriteMap()
|
2018-07-06 16:27:42 +00:00
|
|
|
with open(fname, 'wb') as outfd:
|
|
|
|
outfd.write(dtb.GetContents())
|
2016-11-26 03:15:51 +00:00
|
|
|
finally:
|
|
|
|
tools.FinaliseOutputDir()
|
|
|
|
finally:
|
|
|
|
tout.Uninit()
|
|
|
|
|
|
|
|
return 0
|