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:
|
|
|
|
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 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-06-01 15:38:12 +00:00
|
|
|
self._section = bsection.Section('main-section', self._node)
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2018-06-01 15:38:12 +00:00
|
|
|
self._section.GetEntryPositions()
|
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
|
|
|
|
|
|
|
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 15:38:12 +00:00
|
|
|
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):
|
|
|
|
"""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:
|
|
|
|
print('%8s %8s %s' % ('Position', 'Size', 'Name'), file=fd)
|
|
|
|
self._section.WriteMap(fd, 0)
|