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>
|
|
|
|
#
|
|
|
|
# Class for an image, the output of binman
|
|
|
|
#
|
|
|
|
|
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
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
from operator import attrgetter
|
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
|
|
|
import re
|
|
|
|
import sys
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
import fdt_util
|
2018-06-01 15:38:12 +00:00
|
|
|
import bsection
|
2016-11-26 03:15:51 +00:00
|
|
|
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
|
|
|
|
_filename: Output filename for image
|
2018-06-01 15:38:12 +00:00
|
|
|
_sections: Sections present in this image (may be one or more)
|
2018-06-01 15:38:19 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
test: True if this is being called from a test of Images. This this case
|
|
|
|
there is no device tree defining the structure of the section, so
|
|
|
|
we create a section manually.
|
2016-11-26 03:15:51 +00:00
|
|
|
"""
|
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
|
|
|
def __init__(self, name, node, test=False):
|
2016-11-26 03:15:51 +00:00
|
|
|
self._node = node
|
|
|
|
self._name = name
|
|
|
|
self._size = None
|
|
|
|
self._filename = '%s.bin' % self._name
|
2018-06-01 15:38:12 +00:00
|
|
|
if test:
|
2018-09-14 10:57:33 +00:00
|
|
|
self._section = bsection.Section('main-section', None, self._node,
|
|
|
|
self, True)
|
2018-06-01 15:38:12 +00:00
|
|
|
else:
|
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
|
|
|
self._ReadNode()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
def _ReadNode(self):
|
|
|
|
"""Read properties from the image node"""
|
|
|
|
self._size = fdt_util.GetInt(self._node, 'size')
|
|
|
|
filename = fdt_util.GetString(self._node, 'filename')
|
|
|
|
if filename:
|
|
|
|
self._filename = filename
|
2018-09-14 10:57:33 +00:00
|
|
|
self._section = bsection.Section('main-section', None, self._node, self)
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2019-07-08 20:25:37 +00:00
|
|
|
def Raise(self, msg):
|
|
|
|
"""Convenience function to raise an error referencing an image"""
|
|
|
|
raise ValueError("Image '%s': %s" % (self._node.path, msg))
|
|
|
|
|
2018-09-14 10:57:22 +00:00
|
|
|
def GetFdtSet(self):
|
|
|
|
"""Get the set of device tree files used by this image"""
|
|
|
|
return self._section.GetFdtSet()
|
|
|
|
|
2018-09-14 10:57:28 +00:00
|
|
|
def ExpandEntries(self):
|
|
|
|
"""Expand out any entries which have calculated sub-entries
|
|
|
|
|
|
|
|
Some entries are expanded out at runtime, e.g. 'files', which produces
|
|
|
|
a section containing a list of files. Process these entries so that
|
|
|
|
this information is added to the device tree.
|
|
|
|
"""
|
|
|
|
self._section.ExpandEntries()
|
|
|
|
|
2018-07-06 16:27:41 +00:00
|
|
|
def AddMissingProperties(self):
|
|
|
|
"""Add properties that are not present in the device tree
|
|
|
|
|
2018-08-01 21:22:37 +00:00
|
|
|
When binman has completed packing the entries the offset and size of
|
2018-07-06 16:27:41 +00:00
|
|
|
each entry are known. But before this the device tree may not specify
|
|
|
|
these. Add any missing properties, with a dummy value, so that the
|
|
|
|
size of the entry is correct. That way we can insert the correct values
|
|
|
|
later.
|
|
|
|
"""
|
|
|
|
self._section.AddMissingProperties()
|
|
|
|
|
2018-07-06 16:27:40 +00:00
|
|
|
def ProcessFdt(self, fdt):
|
2018-09-14 10:57:24 +00:00
|
|
|
"""Allow entries to adjust the device tree
|
|
|
|
|
|
|
|
Some entries need to adjust the device tree for their purposes. This
|
|
|
|
may involve adding or deleting properties.
|
|
|
|
"""
|
2018-07-06 16:27:40 +00:00
|
|
|
return self._section.ProcessFdt(fdt)
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
def GetEntryContents(self):
|
2018-06-01 15:38:12 +00:00
|
|
|
"""Call ObtainContents() for the section
|
2016-11-26 03:15:51 +00:00
|
|
|
"""
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.GetEntryContents()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2018-08-01 21:22:37 +00:00
|
|
|
def GetEntryOffsets(self):
|
|
|
|
"""Handle entries that want to set the offset/size of other entries
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2018-08-01 21:22:37 +00:00
|
|
|
This calls each entry's GetOffsets() method. If it returns a list
|
2016-11-26 03:15:51 +00:00
|
|
|
of entries to update, it updates them.
|
|
|
|
"""
|
2018-08-01 21:22:37 +00:00
|
|
|
self._section.GetEntryOffsets()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2019-07-08 20:25:37 +00:00
|
|
|
def ResetForPack(self):
|
|
|
|
"""Reset offset/size fields so that packing can be done again"""
|
|
|
|
self._section.ResetForPack()
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
def PackEntries(self):
|
|
|
|
"""Pack all entries into the image"""
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.PackEntries()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2018-06-01 15:38:12 +00:00
|
|
|
def CheckSize(self):
|
|
|
|
"""Check that the image contents does not exceed its size, etc."""
|
|
|
|
self._size = self._section.CheckSize()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
def CheckEntries(self):
|
|
|
|
"""Check that entries do not overlap or extend outside the image"""
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.CheckEntries()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2018-07-06 16:27:41 +00:00
|
|
|
def SetCalculatedProperties(self):
|
|
|
|
self._section.SetCalculatedProperties()
|
|
|
|
|
2018-08-01 21:22:42 +00:00
|
|
|
def SetImagePos(self):
|
|
|
|
self._section.SetImagePos(0)
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
def ProcessEntryContents(self):
|
|
|
|
"""Call the ProcessContents() method for each entry
|
|
|
|
|
|
|
|
This is intended to adjust the contents as needed by the entry type.
|
2019-07-08 20:25:35 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the new data size is OK, False if expansion is needed
|
2016-11-26 03:15:51 +00:00
|
|
|
"""
|
2019-07-08 20:25:35 +00:00
|
|
|
return self._section.ProcessEntryContents()
|
2016-11-26 03:15:51 +00:00
|
|
|
|
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
|
|
|
def WriteSymbols(self):
|
|
|
|
"""Write symbol values into binary files for access at run time"""
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.WriteSymbols()
|
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
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
def BuildImage(self):
|
|
|
|
"""Write the image to a file"""
|
|
|
|
fname = tools.GetOutputFilename(self._filename)
|
|
|
|
with open(fname, 'wb') as fd:
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.BuildSection(fd, 0)
|
2016-11-26 03:15:51 +00:00
|
|
|
|
2018-06-01 15:38:12 +00:00
|
|
|
def GetEntries(self):
|
|
|
|
return self._section.GetEntries()
|
2018-06-01 15:38:20 +00:00
|
|
|
|
|
|
|
def WriteMap(self):
|
2018-09-14 10:57:36 +00:00
|
|
|
"""Write a map of the image to a .map file
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Filename of map file written
|
|
|
|
"""
|
2018-06-01 15:38:20 +00:00
|
|
|
filename = '%s.map' % self._name
|
|
|
|
fname = tools.GetOutputFilename(filename)
|
|
|
|
with open(fname, 'w') as fd:
|
2018-07-17 19:25:49 +00:00
|
|
|
print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
|
|
|
|
file=fd)
|
2018-06-01 15:38:20 +00:00
|
|
|
self._section.WriteMap(fd, 0)
|
2018-09-14 10:57:36 +00:00
|
|
|
return fname
|