提交 36737f22 编写于 作者: T Tom Rini
......@@ -903,6 +903,12 @@ u-boot.ldr: u-boot
$(LDR) -T $(CONFIG_CPU) -c $@ $< $(LDR_FLAGS)
$(BOARD_SIZE_CHECK)
# binman
# ---------------------------------------------------------------------------
quiet_cmd_binman = BINMAN $@
cmd_binman = $(srctree)/tools/binman/binman -d u-boot.dtb -O . \
-I . -I $(srctree)/board/$(BOARDDIR) $<
OBJCOPYFLAGS_u-boot.ldr.hex := -I binary -O ihex
OBJCOPYFLAGS_u-boot.ldr.srec := -I binary -O srec
......@@ -1047,50 +1053,11 @@ endif
# x86 uses a large ROM. We fill it with 0xff, put the 16-bit stuff (including
# reset vector) at the top, Intel ME descriptor at the bottom, and U-Boot in
# the middle.
# the middle. This is handled by binman based on an image description in the
# board's device tree.
ifneq ($(CONFIG_X86_RESET_VECTOR),)
rom: u-boot.rom FORCE
IFDTOOL=$(objtree)/tools/ifdtool
IFDTOOL_FLAGS = -f 0:$(objtree)/u-boot.dtb
IFDTOOL_FLAGS += -m 0x$(shell $(NM) u-boot |grep _dt_ucode_base_size |cut -d' ' -f1)
IFDTOOL_FLAGS += -U $(CONFIG_SYS_TEXT_BASE):$(objtree)/u-boot-nodtb.bin
IFDTOOL_FLAGS += -w $(CONFIG_SYS_X86_START16):$(objtree)/u-boot-x86-16bit.bin
IFDTOOL_FLAGS += -C
ifneq ($(CONFIG_HAVE_INTEL_ME),)
IFDTOOL_ME_FLAGS = -D $(srctree)/board/$(BOARDDIR)/descriptor.bin
IFDTOOL_ME_FLAGS += -i ME:$(srctree)/board/$(BOARDDIR)/me.bin
endif
ifneq ($(CONFIG_HAVE_MRC),)
IFDTOOL_FLAGS += -w $(CONFIG_X86_MRC_ADDR):$(srctree)/board/$(BOARDDIR)/mrc.bin
endif
ifneq ($(CONFIG_HAVE_FSP),)
IFDTOOL_FLAGS += -w $(CONFIG_FSP_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_FSP_FILE)
endif
ifneq ($(CONFIG_HAVE_CMC),)
IFDTOOL_FLAGS += -w $(CONFIG_CMC_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_CMC_FILE)
endif
ifneq ($(CONFIG_HAVE_VGA_BIOS),)
IFDTOOL_FLAGS += -w $(CONFIG_VGA_BIOS_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_VGA_BIOS_FILE)
endif
ifneq ($(CONFIG_HAVE_REFCODE),)
IFDTOOL_FLAGS += -w $(CONFIG_X86_REFCODE_ADDR):refcode.bin
endif
quiet_cmd_ifdtool = IFDTOOL $@
cmd_ifdtool = $(IFDTOOL) -c -r $(CONFIG_ROM_SIZE) u-boot.tmp;
ifneq ($(CONFIG_HAVE_INTEL_ME),)
cmd_ifdtool += $(IFDTOOL) $(IFDTOOL_ME_FLAGS) u-boot.tmp;
endif
cmd_ifdtool += $(IFDTOOL) $(IFDTOOL_FLAGS) u-boot.tmp;
cmd_ifdtool += mv u-boot.tmp $@
refcode.bin: $(srctree)/board/$(BOARDDIR)/refcode.bin FORCE
$(call if_changed,copy)
......@@ -1100,7 +1067,7 @@ cmd_ldr = $(LD) $(LDFLAGS_$(@F)) \
u-boot.rom: u-boot-x86-16bit.bin u-boot.bin FORCE \
$(if $(CONFIG_HAVE_REFCODE),refcode.bin)
$(call if_changed,ifdtool)
$(call if_changed,binman)
OBJCOPYFLAGS_u-boot-x86-16bit.bin := -O binary -j .start16 -j .resetvec
u-boot-x86-16bit.bin: u-boot FORCE
......@@ -1108,10 +1075,8 @@ u-boot-x86-16bit.bin: u-boot FORCE
endif
ifneq ($(CONFIG_ARCH_SUNXI),)
OBJCOPYFLAGS_u-boot-sunxi-with-spl.bin = -I binary -O binary \
--pad-to=$(CONFIG_SPL_PAD_TO) --gap-fill=0xff
u-boot-sunxi-with-spl.bin: spl/sunxi-spl.bin u-boot.img FORCE
$(call if_changed,pad_cat)
u-boot-sunxi-with-spl.bin: spl/sunxi-spl.bin u-boot.img u-boot.dtb FORCE
$(call if_changed,binman)
endif
ifneq ($(CONFIG_TEGRA),)
......
#include <config.h>
/ {
binman {
filename = "u-boot-sunxi-with-spl.bin";
pad-byte = <0xff>;
blob {
filename = "spl/sunxi-spl.bin";
};
u-boot-img {
pos = <CONFIG_SPL_PAD_TO>;
};
};
};
/*
* Copyright (C) 2016 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
/ {
host1x@50000000 {
u-boot,dm-pre-reloc;
dc@54200000 {
u-boot,dm-pre-reloc;
};
};
};
......@@ -27,9 +27,7 @@
};
host1x@50000000 {
u-boot,dm-pre-reloc;
dc@54200000 {
u-boot,dm-pre-reloc;
display-timings {
timing@0 {
clock-frequency = <69500000>;
......
/ {
host1x@50000000 {
u-boot,dm-pre-reloc;
dc@54200000 {
u-boot,dm-pre-reloc;
};
};
};
......@@ -10,7 +10,6 @@
interrupt-parent = <&lic>;
host1x@50000000 {
u-boot,dm-pre-reloc;
compatible = "nvidia,tegra20-host1x", "simple-bus";
reg = <0x50000000 0x00024000>;
interrupts = <GIC_SPI 65 IRQ_TYPE_LEVEL_HIGH>, /* syncpt */
......@@ -78,7 +77,6 @@
};
dc@54200000 {
u-boot,dm-pre-reloc;
compatible = "nvidia,tegra20-dc";
reg = <0x54200000 0x00040000>;
interrupts = <GIC_SPI 73 IRQ_TYPE_LEVEL_HIGH>;
......
/*
* Copyright (C) 2016 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <u-boot.dtsi>
#ifdef CONFIG_ROM_SIZE
/ {
binman {
u-boot-with-ucode-ptr {
optional-ucode;
};
};
};
#endif
/*
* Copyright (C) 2016 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <config.h>
#ifdef CONFIG_ROM_SIZE
/ {
binman {
filename = "u-boot.rom";
end-at-4gb;
sort-by-pos;
pad-byte = <0xff>;
size = <CONFIG_ROM_SIZE>;
#ifdef CONFIG_HAVE_INTEL_ME
intel-descriptor {
};
intel-me {
};
#endif
u-boot-with-ucode-ptr {
pos = <CONFIG_SYS_TEXT_BASE>;
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
align = <16>;
};
#ifdef CONFIG_HAVE_MRC
intel-mrc {
pos = <CONFIG_X86_MRC_ADDR>;
};
#endif
#ifdef CONFIG_HAVE_FSP
intel-fsp {
pos = <CONFIG_FSP_ADDR>;
};
#endif
#ifdef CONFIG_HAVE_CMC
intel-cmc {
pos = <CONFIG_CMC_ADDR>;
};
#endif
#ifdef CONFIG_HAVE_VGA_BIOS
intel-vga {
pos = <CONFIG_VGA_BIOS_ADDR>;
};
#endif
#ifdef CONFIG_HAVE_REFCODE
intel-refcode {
pos = <CONFIG_X86_REFCODE_ADDR>;
};
#endif
x86-start16 {
pos = <CONFIG_SYS_X86_START16>;
};
};
};
#endif
......@@ -164,10 +164,30 @@ cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(UBOOTINCLUDE) \
ld_flags = $(LDFLAGS) $(ldflags-y)
dts_dir = $(srctree)/arch/$(ARCH)/dts
# Try these files in order to find the U-Boot-specific .dtsi include file
u_boot_dtsi_options = $(wildcard $(dts_dir)/$(basename $(notdir $<))-u-boot.dtsi) \
$(wildcard $(dts_dir)/$(subst $\",,$(CONFIG_SYS_SOC))-u-boot.dtsi) \
$(wildcard $(dts_dir)/$(subst $\",,$(CONFIG_SYS_CPU))-u-boot.dtsi) \
$(wildcard $(dts_dir)/$(subst $\",,$(CONFIG_SYS_VENDOR))-u-boot.dtsi) \
$(wildcard $(dts_dir)/u-boot.dtsi)
# Uncomment for debugging
# $(warning u_boot_dtsi_options: $(u_boot_dtsi_options))
# We use the first match
u_boot_dtsi = $(firstword $(u_boot_dtsi_options))
# Modified for U-Boot
dtc_cpp_flags = -Wp,-MD,$(depfile).pre.tmp -nostdinc \
-I$(srctree)/arch/$(ARCH)/dts \
-I$(srctree)/arch/$(ARCH)/dts/include \
-Iinclude \
-I$(srctree)/include \
-I$(srctree)/arch/$(ARCH)/include \
-include $(srctree)/include/linux/kconfig.h \
-D__ASSEMBLY__ \
-undef -D__DTS__
# Finds the multi-part object the current object will be linked into
......@@ -288,8 +308,11 @@ $(obj)/%.dtb.S: $(obj)/%.dtb
quiet_cmd_dtc = DTC $@
# Modified for U-Boot
# Bring in any U-Boot-specific include after the '/dts-v1/;' header
cmd_dtc = mkdir -p $(dir ${dtc-tmp}) ; \
$(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
cat $< $(if $(u_boot_dtsi),\
| sed 's%^/ {$$%\#include \"$(u_boot_dtsi)\"\n&%') | \
$(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) - ; \
$(DTC) -O dtb -o $@ -b 0 \
-i $(dir $<) $(DTC_FLAGS) \
-d $(depfile).dtc.tmp $(dtc-tmp) ; \
......
此差异已折叠。
binman.py
\ No newline at end of file
#!/usr/bin/python
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Creates binary images from input files controlled by a description
#
"""See README for more information"""
import os
import sys
import traceback
import unittest
# Bring in the patman and dtoc libraries
our_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(our_path, '../patman'))
sys.path.append(os.path.join(our_path, '../dtoc'))
# Also allow entry-type modules to be brought in from the etype directory.
sys.path.append(os.path.join(our_path, 'etype'))
import cmdline
import command
import control
def RunTests():
"""Run the functional tests and any embedded doctests"""
import entry_test
import fdt_test
import func_test
import test
import doctest
result = unittest.TestResult()
for module in []:
suite = doctest.DocTestSuite(module)
suite.run(result)
sys.argv = [sys.argv[0]]
for module in (func_test.TestFunctional, fdt_test.TestFdt,
entry_test.TestEntry):
suite = unittest.TestLoader().loadTestsFromTestCase(module)
suite.run(result)
print result
for test, err in result.errors:
print test.id(), err
for test, err in result.failures:
print err
def RunTestCoverage():
"""Run the tests and check that we get 100% coverage"""
# This uses the build output from sandbox_spl to get _libfdt.so
cmd = ('PYTHONPATH=%s/sandbox_spl/tools coverage run '
'--include "tools/binman/*.py" --omit "*test*,*binman.py" '
'tools/binman/binman.py -t' % options.build_dir)
os.system(cmd)
stdout = command.Output('coverage', 'report')
coverage = stdout.splitlines()[-1].split(' ')[-1]
if coverage != '100%':
print stdout
print "Type 'coverage html' to get a report in htmlcov/index.html"
raise ValueError('Coverage error: %s, but should be 100%%' % coverage)
def RunBinman(options, args):
"""Main entry point to binman once arguments are parsed
Args:
options: Command-line options
args: Non-option arguments
"""
ret_code = 0
# For testing: This enables full exception traces.
#options.debug = True
if not options.debug:
sys.tracebacklimit = 0
if options.test:
RunTests()
elif options.test_coverage:
RunTestCoverage()
elif 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)
else:
try:
ret_code = control.Binman(options, args)
except Exception as e:
print 'binman: %s' % e
if options.debug:
print
traceback.print_exc()
ret_code = 1
return ret_code
if __name__ == "__main__":
(options, args) = cmdline.ParseArgs(sys.argv)
ret_code = RunBinman(options, args)
sys.exit(ret_code)
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Command-line parser for binman
#
from optparse import OptionParser
def ParseArgs(argv):
"""Parse the binman command-line arguments
Args:
argv: List of string arguments
Returns:
Tuple (options, args) with the command-line options and arugments.
options provides access to the options (e.g. option.debug)
args is a list of string arguments
"""
parser = OptionParser()
parser.add_option('-b', '--board', type='string',
help='Board name to build')
parser.add_option('-B', '--build-dir', type='string', default='b',
help='Directory containing the build output')
parser.add_option('-d', '--dt', type='string',
help='Configuration file (.dtb) to use')
parser.add_option('-D', '--debug', action='store_true',
help='Enabling debugging (provides a full traceback on error)')
parser.add_option('-I', '--indir', action='append',
help='Add a path to a directory to use for input files')
parser.add_option('-H', '--full-help', action='store_true',
default=False, help='Display the README file')
parser.add_option('-O', '--outdir', type='string',
action='store', help='Path to directory to use for intermediate and '
'output files')
parser.add_option('-p', '--preserve', action='store_true',\
help='Preserve temporary output directory even if option -O is not '
'given')
parser.add_option('-t', '--test', action='store_true',
default=False, help='run tests')
parser.add_option('-T', '--test-coverage', action='store_true',
default=False, help='run tests and check for 100% coverage')
parser.add_option('-v', '--verbosity', default=1,
type='int', help='Control verbosity: 0=silent, 1=progress, 3=full, '
'4=debug')
parser.usage += """
Create images for a board from a set of binaries. It is controlled by a
description in the board device tree."""
return parser.parse_args(argv)
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Creates binary images from input files controlled by a description
#
from collections import OrderedDict
import os
import sys
import tools
import command
import fdt_select
import fdt_util
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()
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
def _FindBinmanNode(fdt):
"""Find the 'binman' node in the device tree
Args:
fdt: Fdt object to scan
Returns:
Node object of /binman node, or None if not found
"""
for node in fdt.GetRoot().subnodes:
if node.name == 'binman':
return node
return None
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:
tout.Init(options.verbosity)
try:
tools.SetInputDirs(options.indir)
tools.PrepareOutputDir(options.outdir, options.preserve)
fdt = fdt_select.FdtScan(dtb_fname)
node = _FindBinmanNode(fdt)
if not node:
raise ValueError("Device tree '%s' does not have a 'binman' "
"node" % dtb_fname)
images = _ReadImageDesc(node)
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()
image.GetEntryPositions()
image.PackEntries()
image.CheckSize()
image.CheckEntries()
image.ProcessEntryContents()
image.BuildImage()
finally:
tools.FinaliseOutputDir()
finally:
tout.Uninit()
return 0
#
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Test for the Entry class
import collections
import unittest
import entry
class TestEntry(unittest.TestCase):
def testEntryContents(self):
"""Test the Entry bass class"""
base_entry = entry.Entry(None, None, None, read_node=False)
self.assertEqual(True, base_entry.ObtainContents())
def testUnknownEntry(self):
"""Test that unknown entry types are detected"""
Node = collections.namedtuple('Node', ['name', 'path'])
node = Node('invalid-name', 'invalid-path')
with self.assertRaises(ValueError) as e:
entry.Entry.Create(None, node, node.name)
self.assertIn("Unknown entry type 'invalid-name' in node "
"'invalid-path'", str(e.exception))
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for testing purposes. Not used in real images.
#
from entry import Entry
import fdt_util
import tools
class Entry__testing(Entry):
def __init__(self, image, etype, node):
Entry.__init__(self, image, etype, node)
def ObtainContents(self):
self.data = 'a'
self.contents_size = len(self.data)
return True
def ReadContents(self):
return True
def GetPositions(self):
return {'invalid-entry': [1, 2]}
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for blobs, which are binary objects read from files
#
from entry import Entry
import fdt_util
import tools
class Entry_blob(Entry):
def __init__(self, image, etype, node):
Entry.__init__(self, image, etype, node)
self._filename = fdt_util.GetString(self._node, "filename", self.etype)
def ObtainContents(self):
self._filename = self.GetDefaultFilename()
self._pathname = tools.GetInputFilename(self._filename)
self.ReadContents()
return True
def ReadContents(self):
with open(self._pathname) as fd:
# We assume the data is small enough to fit into memory. If this
# is used for large filesystem image that might not be true.
# In that case, Image.BuildImage() could be adjusted to use a
# new Entry method which can read in chunks. Then we could copy
# the data in chunks and avoid reading it all at once. For now
# this seems like an unnecessary complication.
self.data = fd.read()
self.contents_size = len(self.data)
return True
def GetDefaultFilename(self):
return self._filename
# Copyright (c) 2016 Google, Inc
#
# SPDX-License-Identifier: GPL-2.0+
#
# Base class for all entries
#
# importlib was introduced in Python 2.7 but there was a report of it not
# working in 2.7.12, so we work around this:
# http://lists.denx.de/pipermail/u-boot/2016-October/269729.html
try:
import importlib
have_importlib = True
except:
have_importlib = False
import fdt_util
import tools
modules = {}
class Entry(object):
"""An Entry in the image
An entry corresponds to a single node in the device-tree description
of the image. Each entry ends up being a part of the final image.
Entries can be placed either right next to each other, or with padding
between them. The type of the entry determines the data that is in it.
This class is not used by itself. All entry objects are subclasses of
Entry.
Attributes:
image: The image containing this entry
node: The node that created this entry
pos: Absolute position of entry within the image, None if not known
size: Entry size in bytes, None if not known
contents_size: Size of contents in bytes, 0 by default
align: Entry start position alignment, or None
align_size: Entry size alignment, or None
align_end: Entry end position alignment, or None
pad_before: Number of pad bytes before the contents, 0 if none
pad_after: Number of pad bytes after the contents, 0 if none
data: Contents of entry (string of bytes)
"""
def __init__(self, image, etype, node, read_node=True):
self.image = image
self.etype = etype
self._node = node
self.pos = None
self.size = None
self.contents_size = 0
self.align = None
self.align_size = None
self.align_end = None
self.pad_before = 0
self.pad_after = 0
self.pos_unset = False
if read_node:
self.ReadNode()
@staticmethod
def Create(image, node, etype=None):
"""Create a new entry for a node.
Args:
image: Image object containing this node
node: Node object containing information about the entry to create
etype: Entry type to use, or None to work it out (used for tests)
Returns:
A new Entry object of the correct type (a subclass of Entry)
"""
if not etype:
etype = fdt_util.GetString(node, 'type', node.name)
module_name = etype.replace('-', '_')
module = modules.get(module_name)
# Import the module if we have not already done so.
if not module:
try:
if have_importlib:
module = importlib.import_module(module_name)
else:
module = __import__(module_name)
except ImportError:
raise ValueError("Unknown entry type '%s' in node '%s'" %
(etype, node.path))
modules[module_name] = module
# Call its constructor to get the object we want.
obj = getattr(module, 'Entry_%s' % module_name)
return obj(image, etype, node)
def ReadNode(self):
"""Read entry information from the node
This reads all the fields we recognise from the node, ready for use.
"""
self.pos = fdt_util.GetInt(self._node, 'pos')
self.size = fdt_util.GetInt(self._node, 'size')
self.align = fdt_util.GetInt(self._node, 'align')
if tools.NotPowerOfTwo(self.align):
raise ValueError("Node '%s': Alignment %s must be a power of two" %
(self._node.path, self.align))
self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
self.align_size = fdt_util.GetInt(self._node, 'align-size')
if tools.NotPowerOfTwo(self.align_size):
raise ValueError("Node '%s': Alignment size %s must be a power "
"of two" % (self._node.path, self.align_size))
self.align_end = fdt_util.GetInt(self._node, 'align-end')
self.pos_unset = fdt_util.GetBool(self._node, 'pos-unset')
def ObtainContents(self):
"""Figure out the contents of an entry.
Returns:
True if the contents were found, False if another call is needed
after the other entries are processed.
"""
# No contents by default: subclasses can implement this
return True
def Pack(self, pos):
"""Figure out how to pack the entry into the image
Most of the time the entries are not fully specified. There may be
an alignment but no size. In that case we take the size from the
contents of the entry.
If an entry has no hard-coded position, it will be placed at @pos.
Once this function is complete, both the position and size of the
entry will be know.
Args:
Current image position pointer
Returns:
New image position pointer (after this entry)
"""
if self.pos is None:
if self.pos_unset:
self.Raise('No position set with pos-unset: should another '
'entry provide this correct position?')
self.pos = tools.Align(pos, self.align)
needed = self.pad_before + self.contents_size + self.pad_after
needed = tools.Align(needed, self.align_size)
size = self.size
if not size:
size = needed
new_pos = self.pos + size
aligned_pos = tools.Align(new_pos, self.align_end)
if aligned_pos != new_pos:
size = aligned_pos - self.pos
new_pos = aligned_pos
if not self.size:
self.size = size
if self.size < needed:
self.Raise("Entry contents size is %#x (%d) but entry size is "
"%#x (%d)" % (needed, needed, self.size, self.size))
# Check that the alignment is correct. It could be wrong if the
# and pos or size values were provided (i.e. not calculated), but
# conflict with the provided alignment values
if self.size != tools.Align(self.size, self.align_size):
self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
(self.size, self.size, self.align_size, self.align_size))
if self.pos != tools.Align(self.pos, self.align):
self.Raise("Position %#x (%d) does not match align %#x (%d)" %
(self.pos, self.pos, self.align, self.align))
return new_pos
def Raise(self, msg):
"""Convenience function to raise an error referencing a node"""
raise ValueError("Node '%s': %s" % (self._node.path, msg))
def GetPath(self):
"""Get the path of a node
Returns:
Full path of the node for this entry
"""
return self._node.path
def GetData(self):
return self.data
def GetPositions(self):
return {}
def SetPositionSize(self, pos, size):
self.pos = pos
self.size = size
def ProcessContents(self):
pass
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for Intel Chip Microcode binary blob
#
from entry import Entry
from blob import Entry_blob
class Entry_intel_cmc(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'cmc.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for 'u-boot'
#
import struct
from entry import Entry
from blob import Entry_blob
FD_SIGNATURE = struct.pack('<L', 0x0ff0a55a)
MAX_REGIONS = 5
(REGION_DESCRIPTOR, REGION_BIOS, REGION_ME, REGION_GBE,
REGION_PDATA) = range(5)
class Region:
def __init__(self, data, frba, region_num):
pos = frba + region_num * 4
val = struct.unpack('<L', data[pos:pos + 4])[0]
self.base = (val & 0xfff) << 12
self.limit = ((val & 0x0fff0000) >> 4) | 0xfff
self.size = self.limit - self.base + 1
class Entry_intel_descriptor(Entry_blob):
"""Intel flash descriptor block (4KB)
This is placed at the start of flash and provides information about
the SPI flash regions. In particular it provides the base address and
size of the ME region, allowing us to place the ME binary in the right
place.
"""
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
self._regions = []
def GetDefaultFilename(self):
return 'descriptor.bin'
def GetPositions(self):
pos = self.data.find(FD_SIGNATURE)
if pos == -1:
self.Raise('Cannot find FD signature')
flvalsig, flmap0, flmap1, flmap2 = struct.unpack('<LLLL',
self.data[pos:pos + 16])
frba = ((flmap0 >> 16) & 0xff) << 4
for i in range(MAX_REGIONS):
self._regions.append(Region(self.data, frba, i))
# Set the offset for ME only, for now, since the others are not used
return {'intel-me': [self._regions[REGION_ME].base,
self._regions[REGION_ME].size]}
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for Intel Firmware Support Package binary blob
#
from entry import Entry
from blob import Entry_blob
class Entry_intel_fsp(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'fsp.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for Intel Management Engine binary blob
#
from entry import Entry
from blob import Entry_blob
class Entry_intel_me(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'me.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for Intel Memory Reference Code binary blob
#
from entry import Entry
from blob import Entry_blob
class Entry_intel_mrc(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'mrc.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for x86 VGA ROM binary blob
#
from entry import Entry
from blob import Entry_blob
class Entry_intel_vga(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'vga.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for U-Boot binary
#
from entry import Entry
from blob import Entry_blob
class Entry_u_boot(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'u-boot.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for U-Boot device tree
#
from entry import Entry
from blob import Entry_blob
class Entry_u_boot_dtb(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'u-boot.dtb'
# Copyright (c) 2016 Google, Inc
## Written by Simon Glass <sjg@chromium.org>
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for U-Boot device tree with the microcode removed
#
import fdt_select
from entry import Entry
from blob import Entry_blob
import tools
class Entry_u_boot_dtb_with_ucode(Entry_blob):
"""A U-Boot device tree file, with the microcode removed
See Entry_u_boot_ucode for full details of the 3 entries involved in this
process.
"""
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
self.ucode_data = ''
self.collate = False
self.ucode_offset = None
self.ucode_size = None
def GetDefaultFilename(self):
return 'u-boot.dtb'
def ObtainContents(self):
Entry_blob.ObtainContents(self)
# If the image does not need microcode, there is nothing to do
ucode_dest_entry = self.image.FindEntryType('u-boot-spl-with-ucode-ptr')
if not ucode_dest_entry or not ucode_dest_entry.target_pos:
ucode_dest_entry = self.image.FindEntryType('u-boot-with-ucode-ptr')
if not ucode_dest_entry or not ucode_dest_entry.target_pos:
return True
# Create a new file to hold the copied device tree
dtb_name = 'u-boot-dtb-with-ucode.dtb'
fname = tools.GetOutputFilename(dtb_name)
with open(fname, 'wb') as fd:
fd.write(self.data)
# Remove the microcode
fdt = fdt_select.FdtScan(fname)
fdt.Scan()
ucode = fdt.GetNode('/microcode')
if not ucode:
raise self.Raise("No /microcode node found in '%s'" % fname)
# There's no need to collate it (move all microcode into one place)
# if we only have one chunk of microcode.
self.collate = len(ucode.subnodes) > 1
for node in ucode.subnodes:
data_prop = node.props.get('data')
if data_prop:
self.ucode_data += ''.join(data_prop.bytes)
if not self.collate:
poffset = data_prop.GetOffset()
if poffset is None:
# We cannot obtain a property offset. Collate instead.
self.collate = True
else:
# Find the offset in the device tree of the ucode data
self.ucode_offset = poffset + 12
self.ucode_size = len(data_prop.bytes)
if self.collate:
prop = node.DeleteProp('data')
if self.collate:
fdt.Pack()
fdt.Flush()
# Make this file the contents of this entry
self._pathname = fname
self.ReadContents()
return True
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for U-Boot binary
#
from entry import Entry
from blob import Entry_blob
class Entry_u_boot_img(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'u-boot.img'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for 'u-boot-nodtb.bin'
#
from entry import Entry
from blob import Entry_blob
class Entry_u_boot_nodtb(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'u-boot-nodtb.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for spl/u-boot-spl.bin
#
from entry import Entry
from blob import Entry_blob
class Entry_u_boot_spl(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'spl/u-boot-spl.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for BSS padding for spl/u-boot-spl.bin. This padding
# can be added after the SPL binary to ensure that anything concatenated
# to it will appear to SPL to be at the end of BSS rather than the start.
#
import command
from entry import Entry
from blob import Entry_blob
import tools
class Entry_u_boot_spl_bss_pad(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def ObtainContents(self):
fname = tools.GetInputFilename('spl/u-boot-spl')
args = [['nm', fname], ['grep', '__bss_size']]
out = command.RunPipe(args, capture=True).stdout.splitlines()
bss_size = int(out[0].split()[0], 16)
self.data = chr(0) * bss_size
self.contents_size = bss_size
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for an SPL binary with an embedded microcode pointer
#
import struct
import command
from entry import Entry
from blob import Entry_blob
from u_boot_with_ucode_ptr import Entry_u_boot_with_ucode_ptr
import tools
class Entry_u_boot_spl_with_ucode_ptr(Entry_u_boot_with_ucode_ptr):
"""U-Boot SPL with embedded microcode pointer
See Entry_u_boot_ucode for full details of the entries involved in this
process.
"""
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
self.elf_fname = 'spl/u-boot-spl'
def GetDefaultFilename(self):
return 'spl/u-boot-spl.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for a U-Boot binary with an embedded microcode pointer
#
from entry import Entry
from blob import Entry_blob
import tools
class Entry_u_boot_ucode(Entry_blob):
"""U-Boot microcode block
U-Boot on x86 needs a single block of microcode. This is collected from
the various microcode update nodes in the device tree. It is also unable
to read the microcode from the device tree on platforms that use FSP
(Firmware Support Package) binaries, because the API requires that the
microcode is supplied before there is any SRAM available to use (i.e.
the FSP sets up the SRAM / cache-as-RAM but does so in the call that
requires the microcode!). To keep things simple, all x86 platforms handle
microcode the same way in U-Boot (even non-FSP platforms). This is that
a table is placed at _dt_ucode_base_size containing the base address and
size of the microcode. This is either passed to the FSP (for FSP
platforms), or used to set up the microcode (for non-FSP platforms).
This all happens in the build system since it is the only way to get
the microcode into a single blob and accessible without SRAM.
There are two cases to handle. If there is only one microcode blob in
the device tree, then the ucode pointer it set to point to that. This
entry (u-boot-ucode) is empty. If there is more than one update, then
this entry holds the concatenation of all updates, and the device tree
entry (u-boot-dtb-with-ucode) is updated to remove the microcode. This
last step ensures that that the microcode appears in one contiguous
block in the image and is not unnecessarily duplicated in the device
tree. It is referred to as 'collation' here.
Entry types that have a part to play in handling microcode:
Entry_u_boot_with_ucode_ptr:
Contains u-boot-nodtb.bin (i.e. U-Boot without the device tree).
It updates it with the address and size of the microcode so that
U-Boot can find it early on start-up.
Entry_u_boot_dtb_with_ucode:
Contains u-boot.dtb. It stores the microcode in a
'self.ucode_data' property, which is then read by this class to
obtain the microcode if needed. If collation is performed, it
removes the microcode from the device tree.
Entry_u_boot_ucode:
This class. If collation is enabled it reads the microcode from
the Entry_u_boot_dtb_with_ucode entry, and uses it as the
contents of this entry.
"""
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def ObtainContents(self):
# If the image does not need microcode, there is nothing to do
ucode_dest_entry = self.image.FindEntryType('u-boot-with-ucode-ptr')
if ucode_dest_entry and not ucode_dest_entry.target_pos:
self.data = ''
return True
# Get the microcode from the device tree entry
fdt_entry = self.image.FindEntryType('u-boot-dtb-with-ucode')
if not fdt_entry or not fdt_entry.ucode_data:
return False
if not fdt_entry.collate:
# This section can be empty
self.data = ''
return True
# Write it out to a file
dtb_name = 'u-boot-ucode.bin'
fname = tools.GetOutputFilename(dtb_name)
with open(fname, 'wb') as fd:
fd.write(fdt_entry.ucode_data)
self._pathname = fname
self.ReadContents()
return True
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for a U-Boot binary with an embedded microcode pointer
#
import struct
import command
from entry import Entry
from blob import Entry_blob
import fdt_util
import tools
class Entry_u_boot_with_ucode_ptr(Entry_blob):
"""U-Boot with embedded microcode pointer
See Entry_u_boot_ucode for full details of the 3 entries involved in this
process.
"""
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
self.elf_fname = 'u-boot'
self.target_pos = None
def GetDefaultFilename(self):
return 'u-boot-nodtb.bin'
def ObtainContents(self):
# Figure out where to put the microcode pointer
fname = tools.GetInputFilename(self.elf_fname)
args = [['nm', fname], ['grep', '-w', '_dt_ucode_base_size']]
out = (command.RunPipe(args, capture=True, raise_on_error=False).
stdout.splitlines())
if len(out) == 1:
self.target_pos = int(out[0].split()[0], 16)
elif not fdt_util.GetBool(self._node, 'optional-ucode'):
self.Raise('Cannot locate _dt_ucode_base_size symbol in u-boot')
return Entry_blob.ObtainContents(self)
def ProcessContents(self):
# If the image does not need microcode, there is nothing to do
if not self.target_pos:
return
# Get the position of the microcode
ucode_entry = self.image.FindEntryType('u-boot-ucode')
if not ucode_entry:
self.Raise('Cannot find microcode region u-boot-ucode')
# Check the target pos is in the image. If it is not, then U-Boot is
# being linked incorrectly, or is being placed at the wrong position
# in the image.
#
# The image must be set up so that U-Boot is placed at the
# flash address to which it is linked. For example, if
# CONFIG_SYS_TEXT_BASE is 0xfff00000, and the ROM is 8MB, then
# the U-Boot region must start at position 7MB in the image. In this
# case the ROM starts at 0xff800000, so the position of the first
# entry in the image corresponds to that.
if (self.target_pos < self.pos or
self.target_pos >= self.pos + self.size):
self.Raise('Microcode pointer _dt_ucode_base_size at %08x is '
'outside the image ranging from %08x to %08x' %
(self.target_pos, self.pos, self.pos + self.size))
# Get the microcode, either from u-boot-ucode or u-boot-dtb-with-ucode.
# If we have left the microcode in the device tree, then it will be
# in the former. If we extracted the microcode from the device tree
# and collated it in one place, it will be in the latter.
if ucode_entry.size:
pos, size = ucode_entry.pos, ucode_entry.size
else:
dtb_entry = self.image.FindEntryType('u-boot-dtb-with-ucode')
if not dtb_entry:
self.Raise('Cannot find microcode region u-boot-dtb-with-ucode')
pos = dtb_entry.pos + dtb_entry.ucode_offset
size = dtb_entry.ucode_size
# Write the microcode position and size into the entry
pos_and_size = struct.pack('<2L', pos, size)
self.target_pos -= self.pos
self.data = (self.data[:self.target_pos] + pos_and_size +
self.data[self.target_pos + 8:])
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for the 16-bit x86 start-up code for U-Boot
#
from entry import Entry
from blob import Entry_blob
class Entry_x86_start16(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'u-boot-x86-16bit.bin'
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Entry-type module for the 16-bit x86 start-up code for U-Boot SPL
#
from entry import Entry
from blob import Entry_blob
class Entry_x86_start16_spl(Entry_blob):
def __init__(self, image, etype, node):
Entry_blob.__init__(self, image, etype, node)
def GetDefaultFilename(self):
return 'spl/u-boot-x86-16bit-spl.bin'
#
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Test for the fdt modules
import os
import sys
import tempfile
import unittest
from fdt_select import FdtScan
import fdt_util
import tools
class TestFdt(unittest.TestCase):
@classmethod
def setUpClass(self):
self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
self._indir = tempfile.mkdtemp(prefix='binmant.')
tools.PrepareOutputDir(self._indir, True)
def TestFile(self, fname):
return os.path.join(self._binman_dir, 'test', fname)
def GetCompiled(self, fname):
return fdt_util.EnsureCompiled(self.TestFile(fname))
def _DeleteProp(self, fdt):
node = fdt.GetNode('/microcode/update@0')
node.DeleteProp('data')
def testFdtNormal(self):
fname = self.GetCompiled('34_x86_ucode.dts')
fdt = FdtScan(fname)
self._DeleteProp(fdt)
def testFdtFallback(self):
fname = self.GetCompiled('34_x86_ucode.dts')
fdt = FdtScan(fname, True)
fdt.GetProp('/microcode/update@0', 'data')
self.assertEqual('fred',
fdt.GetProp('/microcode/update@0', 'none', default='fred'))
self.assertEqual('12345678 12345679',
fdt.GetProp('/microcode/update@0', 'data', typespec='x'))
self._DeleteProp(fdt)
此差异已折叠。
# Copyright (c) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#
# SPDX-License-Identifier: GPL-2.0+
#
# Class for an image, the output of binman
#
from collections import OrderedDict
from operator import attrgetter
import entry
from entry import Entry
import fdt_util
import tools
class Image:
"""A Image, representing an output from binman
An image is comprised of a collection of entries each containing binary
data. The image size must be large enough to hold all of this data.
This class implements the various operations needed for images.
Atrtributes:
_node: Node object that contains the image definition in device tree
_name: Image name
_size: Image size in bytes, or None if not known yet
_align_size: Image size alignment, or None
_pad_before: Number of bytes before the first entry starts. This
effectively changes the place where entry position 0 starts
_pad_after: Number of bytes after the last entry ends. The last
entry will finish on or before this boundary
_pad_byte: Byte to use to pad the image where there is no entry
_filename: Output filename for image
_sort: True if entries should be sorted by position, False if they
must be in-order in the device tree description
_skip_at_start: Number of bytes before the first entry starts. These
effecively adjust the starting position of entries. For example,
if _pad_before is 16, then the first entry would start at 16.
An entry with pos = 20 would in fact be written at position 4
in the image file.
_end_4gb: Indicates that the image ends at the 4GB boundary. This is
used for x86 images, which want to use positions such that a
memory address (like 0xff800000) is the first entry position.
This causes _skip_at_start to be set to the starting memory
address.
_entries: OrderedDict() of entries
"""
def __init__(self, name, node):
self._node = node
self._name = name
self._size = None
self._align_size = None
self._pad_before = 0
self._pad_after = 0
self._pad_byte = 0
self._filename = '%s.bin' % self._name
self._sort = False
self._skip_at_start = 0
self._end_4gb = False
self._entries = OrderedDict()
self._ReadNode()
self._ReadEntries()
def _ReadNode(self):
"""Read properties from the image node"""
self._size = fdt_util.GetInt(self._node, 'size')
self._align_size = fdt_util.GetInt(self._node, 'align-size')
if tools.NotPowerOfTwo(self._align_size):
self._Raise("Alignment size %s must be a power of two" %
self._align_size)
self._pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
self._pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
self._pad_byte = fdt_util.GetInt(self._node, 'pad-byte', 0)
filename = fdt_util.GetString(self._node, 'filename')
if filename:
self._filename = filename
self._sort = fdt_util.GetBool(self._node, 'sort-by-pos')
self._end_4gb = fdt_util.GetBool(self._node, 'end-at-4gb')
if self._end_4gb and not self._size:
self._Raise("Image size must be provided when using end-at-4gb")
if self._end_4gb:
self._skip_at_start = 0x100000000 - self._size
def CheckSize(self):
"""Check that the image contents does not exceed its size, etc."""
contents_size = 0
for entry in self._entries.values():
contents_size = max(contents_size, entry.pos + entry.size)
contents_size -= self._skip_at_start
size = self._size
if not size:
size = self._pad_before + contents_size + self._pad_after
size = tools.Align(size, self._align_size)
if self._size and contents_size > self._size:
self._Raise("contents size %#x (%d) exceeds image size %#x (%d)" %
(contents_size, contents_size, self._size, self._size))
if not self._size:
self._size = size
if self._size != tools.Align(self._size, self._align_size):
self._Raise("Size %#x (%d) does not match align-size %#x (%d)" %
(self._size, self._size, self._align_size, self._align_size))
def _Raise(self, msg):
"""Raises an error for this image
Args:
msg: Error message to use in the raise string
Raises:
ValueError()
"""
raise ValueError("Image '%s': %s" % (self._node.path, msg))
def _ReadEntries(self):
for node in self._node.subnodes:
self._entries[node.name] = Entry.Create(self, node)
def FindEntryType(self, etype):
"""Find an entry type in the image
Args:
etype: Entry type to find
Returns:
entry matching that type, or None if not found
"""
for entry in self._entries.values():
if entry.etype == etype:
return entry
return None
def GetEntryContents(self):
"""Call ObtainContents() for each entry
This calls each entry's ObtainContents() a few times until they all
return True. We stop calling an entry's function once it returns
True. This allows the contents of one entry to depend on another.
After 3 rounds we give up since it's likely an error.
"""
todo = self._entries.values()
for passnum in range(3):
next_todo = []
for entry in todo:
if not entry.ObtainContents():
next_todo.append(entry)
todo = next_todo
if not todo:
break
def _SetEntryPosSize(self, name, pos, size):
"""Set the position and size of an entry
Args:
name: Entry name to update
pos: New position
size: New size
"""
entry = self._entries.get(name)
if not entry:
self._Raise("Unable to set pos/size for unknown entry '%s'" % name)
entry.SetPositionSize(self._skip_at_start + pos, size)
def GetEntryPositions(self):
"""Handle entries that want to set the position/size of other entries
This calls each entry's GetPositions() method. If it returns a list
of entries to update, it updates them.
"""
for entry in self._entries.values():
pos_dict = entry.GetPositions()
for name, info in pos_dict.iteritems():
self._SetEntryPosSize(name, *info)
def PackEntries(self):
"""Pack all entries into the image"""
pos = self._skip_at_start
for entry in self._entries.values():
pos = entry.Pack(pos)
def _SortEntries(self):
"""Sort entries by position"""
entries = sorted(self._entries.values(), key=lambda entry: entry.pos)
self._entries.clear()
for entry in entries:
self._entries[entry._node.name] = entry
def CheckEntries(self):
"""Check that entries do not overlap or extend outside the image"""
if self._sort:
self._SortEntries()
pos = 0
prev_name = 'None'
for entry in self._entries.values():
if (entry.pos < self._skip_at_start or
entry.pos >= self._skip_at_start + self._size):
entry.Raise("Position %#x (%d) is outside the image starting "
"at %#x (%d)" %
(entry.pos, entry.pos, self._skip_at_start,
self._skip_at_start))
if entry.pos < pos:
entry.Raise("Position %#x (%d) overlaps with previous entry '%s' "
"ending at %#x (%d)" %
(entry.pos, entry.pos, prev_name, pos, pos))
pos = entry.pos + entry.size
prev_name = entry.GetPath()
def ProcessEntryContents(self):
"""Call the ProcessContents() method for each entry
This is intended to adjust the contents as needed by the entry type.
"""
for entry in self._entries.values():
entry.ProcessContents()
def BuildImage(self):
"""Write the image to a file"""
fname = tools.GetOutputFilename(self._filename)
with open(fname, 'wb') as fd:
fd.write(chr(self._pad_byte) * self._size)
for entry in self._entries.values():
data = entry.GetData()
fd.seek(self._pad_before + entry.pos - self._skip_at_start)
fd.write(data)
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
not-a-valid-type {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
multiple-images;
image1 {
u-boot {
};
};
image2 {
pad-before = <3>;
pad-after = <5>;
u-boot {
};
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
align = <23>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
};
u-boot-align {
type = "u-boot";
align = <16>;
};
u-boot-size {
type = "u-boot";
size = <23>;
};
u-boot-next {
type = "u-boot";
};
u-boot-fixed {
type = "u-boot";
pos = <61>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
pad-before = <3>;
pad-after = <5>;
};
u-boot-align-size-nop {
type = "u-boot";
align-size = <4>;
};
u-boot-align-size {
type = "u-boot";
align = <16>;
align-size = <32>;
};
u-boot-align-end {
type = "u-boot";
align-end = <64>;
};
u-boot-align-both {
type = "u-boot";
align= <64>;
align-end = <128>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
align = <5>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
align-size = <55>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
pos = <5>;
align = <4>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
size = <5>;
align-size = <4>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
};
u-boot-align {
type = "u-boot";
pos = <3>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
size = <3>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <3>;
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <7>;
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
align-size = <16>;
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <7>;
align-size = <8>;
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
align-size = <131>;
u-boot {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
pad-byte = <0xff>;
u-boot-spl {
};
u-boot {
pos = <12>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
multiple-images;
image1 {
filename = "test-name";
u-boot {
};
};
image2 {
filename = "test-name.xx";
u-boot {
};
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
blob {
filename = "blobfile";
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
u-boot {
pos = <10>;
};
u-boot-spl {
pos = <5>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
};
u-boot-spl {
pos = <0>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot-nodtb {
};
u-boot-dtb {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
u-boot {
pos = <0xfffffff0>;
};
u-boot-spl {
pos = <0xfffffff7>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <16>;
u-boot {
pos = <0>;
};
u-boot-spl {
pos = <0xfffffff7>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <16>;
u-boot {
pos = <0xfffffff0>;
};
u-boot-spl {
pos = <0xfffffff7>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <16>;
intel-me {
pos-unset;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x800000>;
intel-descriptor {
};
intel-me {
pos-unset;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <16>;
intel-vga {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <16>;
x86-start16 {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
update@1 {
data = <0xabcd0000 0x78235609>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot-img {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
};
u-boot-ucode {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
update@1 {
data = <0xabcd0000 0x78235609>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
update@1 {
data = <0xabcd0000 0x78235609>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
size = <0x200>;
u-boot-with-ucode-ptr {
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
update@1 {
data = <0xabcd0000 0x78235609>;
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
_testing {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <16>;
intel-fsp {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
size = <16>;
intel-cmc {
};
};
};
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
sort-by-pos;
end-at-4gb;
size = <0x200>;
u-boot-with-ucode-ptr {
optional-ucode;
};
u-boot-dtb-with-ucode {
};
u-boot-ucode {
};
};
microcode {
update@0 {
data = <0x12345678 0x12345679>;
};
update@1 {
data = <0xabcd0000 0x78235609>;
};
};
};
/*
* Copyright (c) 2016 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*
* Simple program to create a bad _dt_ucode_base_size symbol to create an
* error when it is used. This is used by binman tests.
*
* Build with:
* cc -march=i386 -m32 -o u_boot_no_ucode_ptr -T u_boot_ucode_ptr.lds \
-nostdlib u_boot_no_ucode_ptr.c
*/
static unsigned long not__dt_ucode_base_size[2]
__attribute__((section(".ucode"))) = {1, 2};
/*
* Copyright (c) 2016 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*
* Simple program to create a _dt_ucode_base_size symbol which can be read
* by 'nm'. This is used by binman tests.
*
* Build with:
* cc -march=i386 -m32 -o u_boot_ucode_ptr -T u_boot_ucode_ptr.lds -nostdlib \
u_boot_ucode_ptr.c
*/
static unsigned long _dt_ucode_base_size[2]
__attribute__((section(".ucode"))) = {1, 2};
/*
* Copyright (c) 2016 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)
SECTIONS
{
. = 0xfffffdf0;
_start = .;
.ucode : {
*(.ucode)
}
}
......@@ -33,16 +33,9 @@
#define FLREG_BASE(reg) ((reg & 0x00000fff) << 12);
#define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff);
enum input_file_type_t {
IF_normal,
IF_fdt,
IF_uboot,
};
struct input_file {
char *fname;
unsigned int addr;
enum input_file_type_t type;
};
/**
......@@ -760,219 +753,6 @@ static int write_data(char *image, int size, unsigned int addr,
return write_size;
}
static int scan_ucode(const void *blob, char *ucode_base, int *countp,
const char **datap, int *data_sizep)
{
const char *data = NULL;
int node, count;
int data_size;
char *ucode;
for (node = 0, count = 0, ucode = ucode_base; node >= 0; count++) {
node = fdt_node_offset_by_compatible(blob, node,
"intel,microcode");
if (node < 0)
break;
data = fdt_getprop(blob, node, "data", &data_size);
if (!data) {
debug("Missing microcode data in FDT '%s': %s\n",
fdt_get_name(blob, node, NULL),
fdt_strerror(data_size));
return -ENOENT;
}
if (ucode_base)
memcpy(ucode, data, data_size);
ucode += data_size;
}
if (countp)
*countp = count;
if (datap)
*datap = data;
if (data_sizep)
*data_sizep = data_size;
return ucode - ucode_base;
}
static int remove_ucode(char *blob)
{
int node, count;
int ret;
/* Keep going until we find no more microcode to remove */
do {
for (node = 0, count = 0; node >= 0;) {
int ret;
node = fdt_node_offset_by_compatible(blob, node,
"intel,microcode");
if (node < 0)
break;
ret = fdt_delprop(blob, node, "data");
/*
* -FDT_ERR_NOTFOUND means we already removed the
* data for this one, so we just continue.
* 0 means we did remove it, so offsets may have
* changed and we need to restart our scan.
* Anything else indicates an error we should report.
*/
if (ret == -FDT_ERR_NOTFOUND)
continue;
else if (!ret)
node = 0;
else
return ret;
}
} while (count);
/* Pack down to remove excees space */
ret = fdt_pack(blob);
if (ret)
return ret;
return fdt_totalsize(blob);
}
static int write_ucode(char *image, int size, struct input_file *fdt,
int fdt_size, unsigned int ucode_ptr,
int collate_ucode)
{
const char *data = NULL;
char *ucode_buf;
const void *blob;
char *ucode_base;
uint32_t *ptr;
int ucode_size;
int data_size;
int offset;
int count;
int ret;
blob = (void *)image + (uint32_t)(fdt->addr + size);
debug("DTB at %lx\n", (char *)blob - image);
/* Find out about the micrcode we have */
ucode_size = scan_ucode(blob, NULL, &count, &data, &data_size);
if (ucode_size < 0)
return ucode_size;
if (!count) {
debug("No microcode found in FDT\n");
return -ENOENT;
}
if (count > 1 && !collate_ucode) {
fprintf(stderr,
"Cannot handle multiple microcode blocks - please use -C flag to collate them\n");
return -EMLINK;
}
/*
* Collect the microcode into a buffer, remove it from the device
* tree and place it immediately above the (now smaller) device tree.
*/
if (collate_ucode && count > 1) {
ucode_buf = malloc(ucode_size);
if (!ucode_buf) {
fprintf(stderr,
"Out of memory for microcode (%d bytes)\n",
ucode_size);
return -ENOMEM;
}
ret = scan_ucode(blob, ucode_buf, NULL, NULL, NULL);
if (ret < 0)
return ret;
/* Remove the microcode from the device tree */
ret = remove_ucode((char *)blob);
if (ret < 0) {
debug("Could not remove FDT microcode: %s\n",
fdt_strerror(ret));
return -EINVAL;
}
debug("Collated %d microcode block(s)\n", count);
debug("Device tree reduced from %x to %x bytes\n",
fdt_size, ret);
fdt_size = ret;
/*
* Place microcode area immediately above the FDT, aligned
* to a 16-byte boundary.
*/
ucode_base = (char *)(((unsigned long)blob + fdt_size + 15) &
~15);
data = ucode_base;
data_size = ucode_size;
memcpy(ucode_base, ucode_buf, ucode_size);
free(ucode_buf);
}
offset = (uint32_t)(ucode_ptr + size);
ptr = (void *)image + offset;
ptr[0] = (data - image) - size;
ptr[1] = data_size;
debug("Wrote microcode pointer at %x: addr=%x, size=%x\n", ucode_ptr,
ptr[0], ptr[1]);
return (collate_ucode ? data + data_size : (char *)blob + fdt_size) -
image;
}
/**
* write_uboot() - Write U-Boot, device tree and microcode pointer
*
* This writes U-Boot into a place in the flash, followed by its device tree.
* The microcode pointer is written so that U-Boot can find the microcode in
* the device tree very early in boot.
*
* @image: Pointer to image
* @size: Size of image in bytes
* @uboot: Input file information for u-boot.bin
* @fdt: Input file information for u-boot.dtb
* @ucode_ptr: Address in U-Boot where the microcode pointer should be placed
* @return 0 if OK, -ve on error
*/
static int write_uboot(char *image, int size, struct input_file *uboot,
struct input_file *fdt, unsigned int ucode_ptr,
int collate_ucode, int *offset_uboot_top,
int *offset_uboot_start)
{
int uboot_size, fdt_size;
int uboot_top;
uboot_size = write_data(image, size, uboot->addr, uboot->fname, 0, 0);
if (uboot_size < 0)
return uboot_size;
fdt->addr = uboot->addr + uboot_size;
debug("U-Boot size %#x, FDT at %#x\n", uboot_size, fdt->addr);
fdt_size = write_data(image, size, fdt->addr, fdt->fname, 0, 0);
if (fdt_size < 0)
return fdt_size;
uboot_top = (uint32_t)(fdt->addr + size) + fdt_size;
if (ucode_ptr) {
uboot_top = write_ucode(image, size, fdt, fdt_size, ucode_ptr,
collate_ucode);
if (uboot_top < 0)
return uboot_top;
}
if (offset_uboot_top && offset_uboot_start) {
*offset_uboot_top = uboot_top;
*offset_uboot_start = (uint32_t)(uboot->addr + size);
}
return 0;
}
static void print_version(void)
{
printf("ifdtool v%s -- ", IFDTOOL_VERSION);
......@@ -1034,7 +814,7 @@ int main(int argc, char *argv[])
int mode_dump = 0, mode_extract = 0, mode_inject = 0;
int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
int create = 0, collate_ucode = 0;
int create = 0;
char *region_type_string = NULL, *inject_fname = NULL;
char *desc_fname = NULL, *addr_str = NULL;
int region_type = -1, inputfreq = 0;
......@@ -1047,14 +827,12 @@ int main(int argc, char *argv[])
char *outfile = NULL;
struct stat buf;
int size = 0;
unsigned int ucode_ptr = 0;
bool have_uboot = false;
int bios_fd;
char *image;
int ret;
static struct option long_options[] = {
{"create", 0, NULL, 'c'},
{"collate-microcode", 0, NULL, 'C'},
{"dump", 0, NULL, 'd'},
{"descriptor", 1, NULL, 'D'},
{"em100", 0, NULL, 'e'},
......@@ -1062,7 +840,6 @@ int main(int argc, char *argv[])
{"fdt", 1, NULL, 'f'},
{"inject", 1, NULL, 'i'},
{"lock", 0, NULL, 'l'},
{"microcode", 1, NULL, 'm'},
{"romsize", 1, NULL, 'r'},
{"spifreq", 1, NULL, 's'},
{"unlock", 0, NULL, 'u'},
......@@ -1073,15 +850,12 @@ int main(int argc, char *argv[])
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "cCdD:ef:hi:lm:r:s:uU:vw:x?",
while ((opt = getopt_long(argc, argv, "cdD:ef:hi:lr:s:uU:vw:x?",
long_options, &option_index)) != EOF) {
switch (opt) {
case 'c':
create = 1;
break;
case 'C':
collate_ucode = 1;
break;
case 'd':
mode_dump = 1;
break;
......@@ -1119,9 +893,6 @@ int main(int argc, char *argv[])
case 'l':
mode_locked = 1;
break;
case 'm':
ucode_ptr = strtoul(optarg, NULL, 0);
break;
case 'r':
rom_size = strtol(optarg, NULL, 0);
debug("ROM size %d\n", rom_size);
......@@ -1166,12 +937,6 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
ifile->addr = strtoll(optarg, NULL, 0);
ifile->type = opt == 'f' ? IF_fdt :
opt == 'U' ? IF_uboot : IF_normal;
if (ifile->type == IF_fdt)
fdt = ifile;
else if (ifile->type == IF_uboot)
have_uboot = true;
wr_num++;
} else {
fprintf(stderr,
......@@ -1302,18 +1067,9 @@ int main(int argc, char *argv[])
for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
ifile = &input_file[wr_idx];
if (ifile->type == IF_fdt) {
continue;
} else if (ifile->type == IF_uboot) {
ret = write_uboot(image, size, ifile, fdt,
ucode_ptr, collate_ucode,
&offset_uboot_top,
&offset_uboot_start);
} else {
ret = write_data(image, size, ifile->addr,
ifile->fname, offset_uboot_top,
offset_uboot_start);
}
ret = write_data(image, size, ifile->addr,
ifile->fname, offset_uboot_top,
offset_uboot_start);
if (ret < 0)
break;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册