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
|
|
|
|
#
|
|
|
|
# Base class for all entries
|
|
|
|
#
|
|
|
|
|
2018-06-01 15:38:20 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
# 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
|
2018-06-01 15:38:15 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2016-11-26 03:15:51 +00:00
|
|
|
import tools
|
|
|
|
|
|
|
|
modules = {}
|
|
|
|
|
2018-06-01 15:38:15 +00:00
|
|
|
our_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
class Entry(object):
|
2018-06-01 15:38:14 +00:00
|
|
|
"""An Entry in the section
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
An entry corresponds to a single node in the device-tree description
|
2018-06-01 15:38:14 +00:00
|
|
|
of the section. Each entry ends up being a part of the final section.
|
2016-11-26 03:15:51 +00:00
|
|
|
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:
|
2018-06-01 15:38:14 +00:00
|
|
|
section: The section containing this entry
|
2016-11-26 03:15:51 +00:00
|
|
|
node: The node that created this entry
|
2018-06-01 15:38:14 +00:00
|
|
|
pos: Absolute position of entry within the section, None if not known
|
2016-11-26 03:15:51 +00:00
|
|
|
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)
|
|
|
|
"""
|
2018-06-01 15:38:21 +00:00
|
|
|
def __init__(self, section, etype, node, read_node=True, name_prefix=''):
|
2018-06-01 15:38:14 +00:00
|
|
|
self.section = section
|
2016-11-26 03:15:51 +00:00
|
|
|
self.etype = etype
|
|
|
|
self._node = node
|
2018-06-01 15:38:21 +00:00
|
|
|
self.name = node and (name_prefix + node.name) or 'none'
|
2016-11-26 03:15:51 +00:00
|
|
|
self.pos = None
|
|
|
|
self.size = None
|
2018-07-06 16:27:19 +00:00
|
|
|
self.data = ''
|
2016-11-26 03:15:51 +00:00
|
|
|
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
|
2018-06-01 15:38:14 +00:00
|
|
|
def Create(section, node, etype=None):
|
2016-11-26 03:15:51 +00:00
|
|
|
"""Create a new entry for a node.
|
|
|
|
|
|
|
|
Args:
|
2018-06-01 15:38:14 +00:00
|
|
|
section: Image object containing this node
|
2016-11-26 03:15:51 +00:00
|
|
|
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)
|
2018-06-01 15:38:11 +00:00
|
|
|
|
|
|
|
# Convert something like 'u-boot@0' to 'u_boot' since we are only
|
|
|
|
# interested in the type.
|
2016-11-26 03:15:51 +00:00
|
|
|
module_name = etype.replace('-', '_')
|
2018-06-01 15:38:11 +00:00
|
|
|
if '@' in module_name:
|
|
|
|
module_name = module_name.split('@')[0]
|
2016-11-26 03:15:51 +00:00
|
|
|
module = modules.get(module_name)
|
|
|
|
|
2018-06-01 15:38:15 +00:00
|
|
|
# Also allow entry-type modules to be brought in from the etype directory.
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
# Import the module if we have not already done so.
|
|
|
|
if not module:
|
2018-06-01 15:38:15 +00:00
|
|
|
old_path = sys.path
|
|
|
|
sys.path.insert(0, os.path.join(our_path, 'etype'))
|
2016-11-26 03:15:51 +00:00
|
|
|
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))
|
2018-06-01 15:38:15 +00:00
|
|
|
finally:
|
|
|
|
sys.path = old_path
|
2016-11-26 03:15:51 +00:00
|
|
|
modules[module_name] = module
|
|
|
|
|
|
|
|
# Call its constructor to get the object we want.
|
|
|
|
obj = getattr(module, 'Entry_%s' % module_name)
|
2018-06-01 15:38:14 +00:00
|
|
|
return obj(section, etype, node)
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2018-07-06 16:27:41 +00:00
|
|
|
def AddMissingProperties(self):
|
|
|
|
"""Add new properties to the device tree as needed for this entry"""
|
|
|
|
for prop in ['pos', 'size']:
|
|
|
|
if not prop in self._node.props:
|
|
|
|
self._node.AddZeroProp(prop)
|
|
|
|
|
|
|
|
def SetCalculatedProperties(self):
|
|
|
|
"""Set the value of device-tree properties calculated by binman"""
|
|
|
|
self._node.SetInt('pos', self.pos)
|
|
|
|
self._node.SetInt('size', self.size)
|
|
|
|
|
2018-07-06 16:27:40 +00:00
|
|
|
def ProcessFdt(self, fdt):
|
|
|
|
return True
|
|
|
|
|
2018-06-01 15:38:21 +00:00
|
|
|
def SetPrefix(self, prefix):
|
|
|
|
"""Set the name prefix for a node
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prefix: Prefix to set, or '' to not use a prefix
|
|
|
|
"""
|
|
|
|
if prefix:
|
|
|
|
self.name = prefix + self.name
|
|
|
|
|
2018-07-06 16:27:19 +00:00
|
|
|
def SetContents(self, data):
|
|
|
|
"""Set the contents of an entry
|
|
|
|
|
|
|
|
This sets both the data and content_size properties
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: Data to set to the contents (string)
|
|
|
|
"""
|
|
|
|
self.data = data
|
|
|
|
self.contents_size = len(self.data)
|
|
|
|
|
|
|
|
def ProcessContentsUpdate(self, data):
|
|
|
|
"""Update the contens of an entry, after the size is fixed
|
|
|
|
|
|
|
|
This checks that the new data is the same size as the old.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: Data to set to the contents (string)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError if the new data size is not the same as the old
|
|
|
|
"""
|
|
|
|
if len(data) != self.contents_size:
|
|
|
|
self.Raise('Cannot update entry size from %d to %d' %
|
|
|
|
(len(data), self.contents_size))
|
|
|
|
self.SetContents(data)
|
|
|
|
|
2016-11-26 03:15:51 +00:00
|
|
|
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):
|
2018-06-01 15:38:14 +00:00
|
|
|
"""Figure out how to pack the entry into the section
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
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:
|
2018-06-01 15:38:14 +00:00
|
|
|
Current section position pointer
|
2016-11-26 03:15:51 +00:00
|
|
|
|
|
|
|
Returns:
|
2018-06-01 15:38:14 +00:00
|
|
|
New section position pointer (after this entry)
|
2016-11-26 03:15:51 +00:00
|
|
|
"""
|
|
|
|
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
|
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
|
|
|
|
2018-06-01 15:38:13 +00:00
|
|
|
def WriteSymbols(self, section):
|
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
|
|
|
"""Write symbol values into binary files for access at run time
|
|
|
|
|
|
|
|
Args:
|
2018-06-01 15:38:13 +00:00
|
|
|
section: Section containing the entry
|
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
|
|
|
"""
|
|
|
|
pass
|
2018-06-01 15:38:16 +00:00
|
|
|
|
|
|
|
def CheckPosition(self):
|
|
|
|
"""Check that the entry positions are correct
|
|
|
|
|
|
|
|
This is used for entries which have extra position requirements (other
|
|
|
|
than having to be fully inside their section). Sub-classes can implement
|
|
|
|
this function and raise if there is a problem.
|
|
|
|
"""
|
|
|
|
pass
|
2018-06-01 15:38:20 +00:00
|
|
|
|
|
|
|
def WriteMap(self, fd, indent):
|
|
|
|
"""Write a map of the entry to a .map file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fd: File to write the map to
|
|
|
|
indent: Curent indent level of map (0=none, 1=one level, etc.)
|
|
|
|
"""
|
|
|
|
print('%s%08x %08x %s' % (' ' * indent, self.pos, self.size,
|
|
|
|
self.name), file=fd)
|