2018-05-07 05:58:06 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-11-26 11:15:51 +08: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 09:55:01 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-11-26 11:15:51 +08: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 09:55:01 +08:00
|
|
|
import re
|
|
|
|
import sys
|
2016-11-26 11:15:51 +08:00
|
|
|
|
|
|
|
import fdt_util
|
2018-06-01 23:38:12 +08:00
|
|
|
import bsection
|
2016-11-26 11:15:51 +08: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 23:38:12 +08:00
|
|
|
_sections: Sections present in this image (may be one or more)
|
2018-06-01 23:38:19 +08: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 11:15:51 +08: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 09:55:01 +08:00
|
|
|
def __init__(self, name, node, test=False):
|
2016-11-26 11:15:51 +08:00
|
|
|
self._node = node
|
|
|
|
self._name = name
|
|
|
|
self._size = None
|
|
|
|
self._filename = '%s.bin' % self._name
|
2018-06-01 23:38:12 +08:00
|
|
|
if test:
|
|
|
|
self._section = bsection.Section('main-section', self._node, True)
|
|
|
|
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 09:55:01 +08:00
|
|
|
self._ReadNode()
|
2016-11-26 11:15:51 +08: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-06-01 23:38:12 +08:00
|
|
|
self._section = bsection.Section('main-section', self._node)
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-09-14 18:57:22 +08:00
|
|
|
def GetFdtSet(self):
|
|
|
|
"""Get the set of device tree files used by this image"""
|
|
|
|
return self._section.GetFdtSet()
|
|
|
|
|
2018-07-07 00:27:41 +08:00
|
|
|
def AddMissingProperties(self):
|
|
|
|
"""Add properties that are not present in the device tree
|
|
|
|
|
2018-08-02 05:22:37 +08:00
|
|
|
When binman has completed packing the entries the offset and size of
|
2018-07-07 00:27:41 +08: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-07 00:27:40 +08:00
|
|
|
def ProcessFdt(self, fdt):
|
|
|
|
return self._section.ProcessFdt(fdt)
|
|
|
|
|
2016-11-26 11:15:51 +08:00
|
|
|
def GetEntryContents(self):
|
2018-06-01 23:38:12 +08:00
|
|
|
"""Call ObtainContents() for the section
|
2016-11-26 11:15:51 +08:00
|
|
|
"""
|
2018-06-01 23:38:12 +08:00
|
|
|
self._section.GetEntryContents()
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-08-02 05:22:37 +08:00
|
|
|
def GetEntryOffsets(self):
|
|
|
|
"""Handle entries that want to set the offset/size of other entries
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-08-02 05:22:37 +08:00
|
|
|
This calls each entry's GetOffsets() method. If it returns a list
|
2016-11-26 11:15:51 +08:00
|
|
|
of entries to update, it updates them.
|
|
|
|
"""
|
2018-08-02 05:22:37 +08:00
|
|
|
self._section.GetEntryOffsets()
|
2016-11-26 11:15:51 +08:00
|
|
|
|
|
|
|
def PackEntries(self):
|
|
|
|
"""Pack all entries into the image"""
|
2018-06-01 23:38:12 +08:00
|
|
|
self._section.PackEntries()
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-06-01 23:38:12 +08:00
|
|
|
def CheckSize(self):
|
|
|
|
"""Check that the image contents does not exceed its size, etc."""
|
|
|
|
self._size = self._section.CheckSize()
|
2016-11-26 11:15:51 +08:00
|
|
|
|
|
|
|
def CheckEntries(self):
|
|
|
|
"""Check that entries do not overlap or extend outside the image"""
|
2018-06-01 23:38:12 +08:00
|
|
|
self._section.CheckEntries()
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-07-07 00:27:41 +08:00
|
|
|
def SetCalculatedProperties(self):
|
|
|
|
self._section.SetCalculatedProperties()
|
|
|
|
|
2018-08-02 05:22:42 +08:00
|
|
|
def SetImagePos(self):
|
|
|
|
self._section.SetImagePos(0)
|
|
|
|
|
2016-11-26 11:15:51 +08:00
|
|
|
def ProcessEntryContents(self):
|
|
|
|
"""Call the ProcessContents() method for each entry
|
|
|
|
|
|
|
|
This is intended to adjust the contents as needed by the entry type.
|
|
|
|
"""
|
2018-06-01 23:38:12 +08:00
|
|
|
self._section.ProcessEntryContents()
|
2016-11-26 11:15:51 +08: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 09:55:01 +08:00
|
|
|
def WriteSymbols(self):
|
|
|
|
"""Write symbol values into binary files for access at run time"""
|
2018-06-01 23:38:12 +08: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 09:55:01 +08:00
|
|
|
|
2016-11-26 11:15:51 +08:00
|
|
|
def BuildImage(self):
|
|
|
|
"""Write the image to a file"""
|
|
|
|
fname = tools.GetOutputFilename(self._filename)
|
|
|
|
with open(fname, 'wb') as fd:
|
2018-06-01 23:38:12 +08:00
|
|
|
self._section.BuildSection(fd, 0)
|
2016-11-26 11:15:51 +08:00
|
|
|
|
2018-06-01 23:38:12 +08:00
|
|
|
def GetEntries(self):
|
|
|
|
return self._section.GetEntries()
|
2018-06-01 23:38:20 +08:00
|
|
|
|
|
|
|
def WriteMap(self):
|
|
|
|
"""Write a map of the image to a .map file"""
|
|
|
|
filename = '%s.map' % self._name
|
|
|
|
fname = tools.GetOutputFilename(filename)
|
|
|
|
with open(fname, 'w') as fd:
|
2018-07-18 03:25:49 +08:00
|
|
|
print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
|
|
|
|
file=fd)
|
2018-06-01 23:38:20 +08:00
|
|
|
self._section.WriteMap(fd, 0)
|